home *** CD-ROM | disk | FTP | other *** search
- # $Header: P:/source/ppee/macros/bookmark.pev 1.67 27 Aug 1990 15:17:20 skipr $
-
- ##############################################################################
- #
- # Sage Software - POLYTRON Division
- # 1700 NW 167th Place
- # Beaverton, OR 97006
- #
- # Copyright 1990, Sage Software, Inc.
- #
- # Permission is hereby granted for licensed users of Sage Professional
- # Editor and PolyAwk to copy and modify this source code for their own
- # personal use. These derivative works may be distributed only to other
- # licensed Sage Professional Editor and PolyAwk users. All other usage
- # is prohibited without express written permission from Sage Software.
- #
- ##############################################################################
-
- #### $Workfile: bookmark.pel $: Support for Marked Blocks
-
-
- # global markid assignments
-
- local MIN_BOOKMARK = 1 # window/buffer/pos bookmarks
- local MAX_BOOKMARK = 10
-
- global TEMP_MARK = 999 # generic temporary
- # values above this are illegal
-
- # selection types (constants):
-
- global NO_SELECTION = 0
- global NORMAL_SELECTION = 1
- global COLUMN_SELECTION = 2
- global LINE_SELECTION = 3
- global INCLUSIVE_SELECTION = 4
-
-
- ### system-wide bookmarks
-
- local bookmark_table # buffer name corresponding to each bookmark
-
- ## enhanced goto_bookmark function designed to be bound to a key
- #
- global function goto_bookmark_key() { #PUBLIC
- local num
- local bookmarksInUse
- local n = 0
- local i
-
- # get list of available bookmarks
- for ( i in bookmark_table ) {
- if ( n++ ) {
- bookmarksInUse = bookmarksInUse " " i
- } else {
- bookmarksInUse = num = i
- }
- }
-
- if ( n == 0 ) {
- warning( "No bookmarks defined." )
- return
- }
-
- if ( n > 1 ) {
- num = 0 + prompt( "Enter bookmark to jump to [" \
- bookmarksInUse "]: ", num )
- }
-
- goto_bookmark( num )
- }
-
- global function goto_bookmark( num ){
- local name
-
- if ( 0+num == 0 ){
- num = 0 + prompt( sprintf(
- "Enter bookmark to jump to [%d..%d]: ",
- MIN_BOOKMARK, MAX_BOOKMARK ))
- }
-
- num = validate_bookmark( num )
-
- if ( num in bookmark_table ){
- name = bookmark_table[ num ]
- if ( name != window_name ){
- next_window( name )
- if ( name != window_name ){
- next_buffer( name )
- if ( name != buffer_name ) {
- delete bookmark_table[ num ]
- error( "Cannot find buffer %s, associated with bookmark %d", \
- name, \
- num )
- }
- }
- }
- goto_mark( num )
- display_filename()
- } else
- message( "Bookmark %d is undefined.", num )
- }
-
- ## place a bookmark at the cursor (companion to goto_bookmark)
- #
- global function place_bookmark( num ){ #PUBLIC
-
- local i
-
- # If no argument was given, prompt for a bookmark number to
- # use, offering the next available bookmark as a default.
-
- if ( 0+num == 0 ) {
- num = i = MIN_BOOKMARK
- for ( ; i < MAX_BOOKMARK; i++ ) {
- if ( !(i in bookmark_table )) {
- num = i
- break
- }
- }
- num = 0 + prompt( sprintf(
- "Bookmark # to drop [%d..%d]: ",
- MIN_BOOKMARK, MAX_BOOKMARK ), num )
- }
-
- num = validate_bookmark( num )
-
- if ( num in bookmark_table \
- && tolower( confirm( "Reuse bookmark #" num "? [yn]",
- "yYnN" )) != "y" ){
- return
- }
-
- create_mark( num );
- bookmark_table[ num ] = buffer_name
- message( "Dropped bookmark #%d", num )
- }
-
- local function validate_bookmark( num ){
- num = 0 + num
-
- if ( num < MIN_BOOKMARK || num > MAX_BOOKMARK ){
- error( "Bookmark's must be in [%d..%d]",
- MIN_BOOKMARK, MAX_BOOKMARK )
- }
- return num
- }
-
-
- function write_block_key()
- {
- local fileToWrite
- local status
-
- if( region_type() == NO_SELECTION ){
- if (buffer_filename) {
- if ( !and( buffer_flags, BUFFER_MODIFIED ) )
- message( buffer_name ": has not been modified. " );
- else {
- message( buffer_name ":" );
- backup_file( buffer_filename );
- status = write_buffer();
- if ((status >= 0) && (!errno))
- message( buffer_name ":" buffer_last_line " lines written" );
-
- ## The system will display the unable to write message
- ##
- #else
- # message( "Unable to write " buffer_name );
- }
- } else {
- warning( "buffer_filename is not defined. " );
- }
- } else {
- fileToWrite = prompt( "Write block as: ", "" )
- if (fileToWrite) {
- backup_file( fileToWrite );
- if (write_marked_block( fileToWrite ))
- message("Write successful.")
- }
- }
- }
-
- local function set_anchor_mark( mtype ) {
- local otype = region_type();
-
- if (otype)
- raise_anchor();
-
- if (otype != mtype)
- drop_anchor( mtype );
- }
-
-
- global function set_exclusive_mark(){
- set_anchor_mark( NORMAL_SELECTION );
- }
-
- global function set_column_mark(){
- set_anchor_mark( COLUMN_SELECTION );
- }
-
- global function set_line_mark(){
- set_anchor_mark( LINE_SELECTION );
- }
-
- global function set_inclusive_mark(){
- set_anchor_mark( INCLUSIVE_SELECTION );
- }
-
- global function copy_to_scrap_key( append){
- message( (region_type() ? "Block" : "Line") " copied to scrap.");
- if (!region_type())
- drop_anchor( LINE_SELECTION );
-
- if (argcount() && append)
- append_to_scrap();
- else
- copy_to_scrap();
- raise_anchor();
- }
-
- global function delete_to_scrap() #PUBLIC #INT
- {
- local status
- status = copy_to_scrap()
- delete_chars()
- return status
- }
-
- #
- # mark_paragraph()
- #
- # mark the current paragraph by first searching backward to the
- # beginning of the paragraph (a blank line) and then searching forwards
- # to the end of the paragraph ( a blank line ). If either search
- # should fail, beep() and restore the initial position.
- #
- global function mark_paragraph() #PUBLIC #INT
- {
- local searchFwdFlags = 21; # search fwd, regex, max
- local searchBkwdFlags = 17; # search bkwd, regex, max
- local markedOk = FALSE
- local bo;
-
-
- if (!search("^[ \t]*$", searchBkwdFlags))
- goto_buffer_top();
-
- search("[ \t]*\\c[^ \t]", searchFwdFlags);
-
- drop_anchor()
- if (!search("^[ \n\t]*$", searchFwdFlags))
- goto_buffer_bottom()
- bo = buffer_offset;
- swap_marks();
- if (buffer_offset == bo) {
- raise_anchor()
- return FALSE;
- }
-
- return TRUE;
- }
-
- ## marked_region_size()
- #
- # Returns the number of characters included in the current highlight.
- # If the currently marked region is a column block, return the area.
- # (Note that column block may include virtual space, so the area may
- # differ from the number of characters in the highlight.)
- #
- global function marked_region_size() {
- local rt = region_type()
- local height, width
- local offset
- local markAtTop
-
- # ensure that a highlight exists
- if ( !rt ) {
- return 0
- }
-
- # determine whether the mark is at the top or bottom of block
- save_position()
- markAtTop = buffer_offset
- swap_marks()
- markAtTop = (markAtTop >= buffer_offset)
-
- # get the position at one corner of the marked region
- save_position()
- if ( rt == COLUMN_SELECTION ) {
- height = current_line
- width = current_column
- } else {
- if ( rt == LINE_SELECTION ) {
- if ( markAtTop ) {
- goto_bol() # include all of the first line
- } else {
- goto_eol() # include all of the last line
- next_char()
- }
- } else if ( rt == INCLUSIVE_SELECTION && !markAtTop ) {
- # include character at mark
- next_char()
- }
- offset = buffer_offset
- }
- restore_position(1)
-
- # get the position at the opposite corner of the marked region
- swap_marks()
- if ( rt == COLUMN_SELECTION ) {
- height = ( height > current_line ) \
- ? height - current_line + 1 \
- : current_line - height + 1
- width = ( width > current_column ) \
- ? width - current_column + 1 \
- : current_column - width + 1
- offset = height * width # area
- } else {
- if ( rt == LINE_SELECTION ) {
- if ( markAtTop ) {
- goto_eol() # include all of the last line
- next_char()
- } else {
- goto_bol() # include all of the first line
- }
- } else if ( rt == INCLUSIVE_SELECTION && markAtTop ) {
- # include character at cursor
- next_char()
- }
- offset -= buffer_offset
- }
-
- # take absolute value
- if ( offset < 0 ) {
- offset = -offset
- }
-
- restore_position( 1 )
- return offset
- }
-