home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 16 / 16.iso / w / w055 / 4.ddi / SOURCES.LIF / BOOKMARK.PEL < prev    next >
Encoding:
Text File  |  1990-09-27  |  8.0 KB  |  343 lines

  1. # $Header:   P:/source/ppee/macros/bookmark.pev   1.67   27 Aug 1990 15:17:20   skipr  $
  2.  
  3. ##############################################################################
  4. #
  5. #           Sage Software - POLYTRON Division
  6. #             1700 NW 167th Place
  7. #               Beaverton, OR 97006
  8. #
  9. #   Copyright 1990, Sage Software, Inc.
  10. #
  11. #   Permission is hereby granted for licensed users of Sage Professional
  12. #   Editor and PolyAwk to copy and modify this source code for their own
  13. #   personal use.  These derivative works may be distributed only to other
  14. #   licensed Sage Professional Editor and PolyAwk users.  All other usage
  15. #   is prohibited without express written permission from Sage Software.
  16. #
  17. ##############################################################################
  18.  
  19. #### $Workfile:   bookmark.pel  $: Support for Marked Blocks
  20.  
  21.  
  22. # global markid assignments
  23.  
  24. local    MIN_BOOKMARK    = 1        # window/buffer/pos bookmarks
  25. local    MAX_BOOKMARK    = 10
  26.  
  27. global    TEMP_MARK    = 999        # generic temporary
  28.                     # values above this are illegal
  29.  
  30. # selection types (constants):
  31.  
  32. global    NO_SELECTION         = 0
  33. global    NORMAL_SELECTION     = 1
  34. global    COLUMN_SELECTION     = 2
  35. global    LINE_SELECTION         = 3
  36. global    INCLUSIVE_SELECTION     = 4
  37.  
  38.  
  39. ### system-wide bookmarks
  40.  
  41. local bookmark_table        # buffer name corresponding to each bookmark
  42.  
  43. ## enhanced goto_bookmark function designed to be bound to a key
  44. #
  45. global function goto_bookmark_key() {                #PUBLIC
  46.     local    num
  47.     local    bookmarksInUse
  48.     local    n = 0
  49.     local    i
  50.  
  51.     # get list of available bookmarks
  52.     for ( i in bookmark_table ) {
  53.         if ( n++ ) {
  54.             bookmarksInUse = bookmarksInUse " " i
  55.         } else {
  56.             bookmarksInUse = num = i
  57.         }
  58.     }
  59.  
  60.     if ( n == 0 ) {
  61.         warning( "No bookmarks defined." )
  62.         return
  63.     }
  64.  
  65.     if ( n > 1 ) {
  66.         num = 0 + prompt( "Enter bookmark to jump to ["    \
  67.                 bookmarksInUse "]: ", num )
  68.     }
  69.  
  70.     goto_bookmark( num )
  71. }
  72.  
  73. global function goto_bookmark( num ){
  74.     local name
  75.  
  76.     if ( 0+num == 0 ){
  77.         num = 0 + prompt( sprintf(
  78.             "Enter bookmark to jump to [%d..%d]: ",
  79.             MIN_BOOKMARK, MAX_BOOKMARK ))
  80.     }
  81.  
  82.     num = validate_bookmark( num )
  83.  
  84.     if ( num in bookmark_table ){
  85.         name = bookmark_table[ num ]
  86.         if ( name != window_name ){
  87.             next_window( name )
  88.             if ( name != window_name ){
  89.                 next_buffer( name )
  90.                 if ( name != buffer_name ) {
  91.                     delete bookmark_table[ num ]
  92.                     error( "Cannot find buffer %s, associated with bookmark %d",    \
  93.                         name,                            \
  94.                         num )
  95.                 }
  96.             }
  97.         }
  98.         goto_mark( num )
  99.         display_filename()
  100.     } else
  101.         message( "Bookmark %d is undefined.", num )
  102. }
  103.  
  104. ## place a bookmark at the cursor (companion to goto_bookmark)
  105. #
  106. global function place_bookmark( num ){                #PUBLIC
  107.  
  108.     local    i
  109.  
  110.     # If no argument was given, prompt for a bookmark number to
  111.     # use, offering the next available bookmark as a default.
  112.  
  113.     if ( 0+num == 0 ) {
  114.         num = i = MIN_BOOKMARK
  115.         for ( ; i < MAX_BOOKMARK; i++ ) {
  116.             if ( !(i in bookmark_table )) {
  117.                 num = i
  118.                 break
  119.             }
  120.         }
  121.         num = 0 + prompt( sprintf(
  122.                 "Bookmark # to drop [%d..%d]: ",
  123.                 MIN_BOOKMARK, MAX_BOOKMARK ), num )
  124.     }
  125.  
  126.     num = validate_bookmark( num )
  127.  
  128.     if ( num in bookmark_table    \
  129.     && tolower( confirm( "Reuse bookmark #" num "? [yn]",
  130.             "yYnN" )) != "y" ){
  131.         return
  132.     }
  133.  
  134.     create_mark( num );
  135.     bookmark_table[ num ] = buffer_name
  136.     message( "Dropped bookmark #%d", num )
  137. }
  138.  
  139. local function validate_bookmark( num ){
  140.     num = 0 + num
  141.  
  142.     if ( num < MIN_BOOKMARK || num > MAX_BOOKMARK ){
  143.         error( "Bookmark's must be in [%d..%d]",
  144.             MIN_BOOKMARK, MAX_BOOKMARK )
  145.     }
  146.     return num
  147. }
  148.  
  149.  
  150. function write_block_key()
  151. {
  152.     local    fileToWrite
  153.     local    status
  154.  
  155.     if( region_type() == NO_SELECTION ){
  156.         if (buffer_filename) {
  157.             if ( !and( buffer_flags, BUFFER_MODIFIED ) )
  158.                 message( buffer_name ": has not been modified. " );
  159.             else {
  160.                 message( buffer_name ":" );
  161.                 backup_file( buffer_filename );
  162.                 status = write_buffer();
  163.                 if ((status >= 0) && (!errno))
  164.                     message( buffer_name ":" buffer_last_line " lines written" );
  165.  
  166.                 ## The system will display the unable to write message
  167.                 ##
  168.                 #else
  169.                 #    message( "Unable to write " buffer_name );
  170.             }
  171.         } else {
  172.             warning( "buffer_filename is not defined. " );
  173.         }
  174.     } else {
  175.         fileToWrite = prompt( "Write block as: ", "" )
  176.         if (fileToWrite)  {
  177.             backup_file( fileToWrite );
  178.             if (write_marked_block( fileToWrite ))
  179.                 message("Write successful.")
  180.         }
  181.     }
  182. }
  183.  
  184. local function set_anchor_mark( mtype ) {
  185.     local    otype = region_type();
  186.  
  187.     if (otype)
  188.         raise_anchor();
  189.  
  190.     if (otype != mtype)
  191.         drop_anchor( mtype );
  192. }
  193.  
  194.  
  195. global function set_exclusive_mark(){
  196.     set_anchor_mark( NORMAL_SELECTION );
  197. }
  198.  
  199. global function set_column_mark(){
  200.     set_anchor_mark( COLUMN_SELECTION );
  201. }
  202.  
  203. global function set_line_mark(){
  204.     set_anchor_mark( LINE_SELECTION );
  205. }
  206.  
  207. global function set_inclusive_mark(){
  208.     set_anchor_mark( INCLUSIVE_SELECTION );
  209. }
  210.  
  211. global function copy_to_scrap_key( append){
  212.     message( (region_type() ? "Block" : "Line") " copied to scrap.");
  213.     if (!region_type())
  214.         drop_anchor( LINE_SELECTION );
  215.  
  216.     if (argcount() && append)
  217.         append_to_scrap();
  218.     else
  219.         copy_to_scrap();
  220.     raise_anchor();
  221. }
  222.  
  223. global function delete_to_scrap()                #PUBLIC #INT
  224. {
  225.     local    status
  226.     status = copy_to_scrap()
  227.     delete_chars()
  228.     return status
  229. }
  230.  
  231. #
  232. # mark_paragraph()
  233. #
  234. # mark the current paragraph by first searching backward to the
  235. # beginning of the paragraph (a blank line) and then searching forwards
  236. # to the end of the paragraph ( a blank line ). If either search
  237. # should fail, beep() and restore the initial position.
  238. #
  239. global function mark_paragraph()                #PUBLIC #INT
  240. {
  241.     local    searchFwdFlags  = 21;    # search fwd, regex, max
  242.     local    searchBkwdFlags = 17;    # search bkwd, regex, max
  243.     local    markedOk  = FALSE
  244.     local    bo;
  245.     
  246.  
  247.     if (!search("^[ \t]*$", searchBkwdFlags))
  248.         goto_buffer_top();
  249.  
  250.     search("[ \t]*\\c[^ \t]", searchFwdFlags);
  251.  
  252.     drop_anchor()
  253.     if (!search("^[ \n\t]*$", searchFwdFlags))
  254.         goto_buffer_bottom()
  255.     bo = buffer_offset;
  256.     swap_marks();
  257.     if (buffer_offset == bo) {
  258.         raise_anchor()
  259.         return FALSE;
  260.     }
  261.  
  262.     return TRUE;
  263. }
  264.  
  265. ## marked_region_size()
  266. #
  267. # Returns the number of characters included in the current highlight.
  268. # If the currently marked region is a column block, return the area.
  269. # (Note that column block may include virtual space, so the area may
  270. # differ from the number of characters in the highlight.)
  271. #
  272. global function marked_region_size() {
  273.     local    rt = region_type()
  274.     local    height, width
  275.     local    offset
  276.     local    markAtTop
  277.  
  278.     # ensure that a highlight exists
  279.     if ( !rt ) {
  280.         return 0
  281.     }
  282.  
  283.     # determine whether the mark is at the top or bottom of block
  284.     save_position()
  285.     markAtTop = buffer_offset
  286.     swap_marks()
  287.     markAtTop = (markAtTop >= buffer_offset)
  288.  
  289.     # get the position at one corner of the marked region
  290.     save_position()
  291.     if ( rt == COLUMN_SELECTION ) {
  292.         height = current_line
  293.         width = current_column
  294.     } else {
  295.         if ( rt == LINE_SELECTION ) {
  296.             if ( markAtTop ) {
  297.                 goto_bol()    # include all of the first line
  298.             } else {
  299.                 goto_eol()    # include all of the last line
  300.                 next_char()
  301.             }
  302.         } else if ( rt == INCLUSIVE_SELECTION && !markAtTop ) {
  303.             # include character at mark
  304.             next_char()
  305.         }
  306.         offset = buffer_offset
  307.     }
  308.     restore_position(1)
  309.  
  310.     # get the position at the opposite corner of the marked region
  311.     swap_marks()
  312.     if ( rt == COLUMN_SELECTION ) {
  313.         height = ( height > current_line )        \
  314.                 ? height - current_line + 1    \
  315.                 : current_line - height + 1
  316.         width = ( width > current_column )        \
  317.                 ? width - current_column + 1    \
  318.                 : current_column - width + 1
  319.         offset = height * width        # area
  320.     } else {
  321.         if ( rt == LINE_SELECTION ) {
  322.             if ( markAtTop ) {
  323.                 goto_eol()    # include all of the last line
  324.                 next_char()
  325.             } else {
  326.                 goto_bol()    # include all of the first line
  327.             }
  328.         } else if ( rt == INCLUSIVE_SELECTION && markAtTop ) {
  329.             # include character at cursor
  330.             next_char()
  331.         }
  332.         offset -= buffer_offset
  333.     }
  334.  
  335.     # take absolute value
  336.     if ( offset < 0 ) {
  337.         offset = -offset
  338.     }
  339.  
  340.     restore_position( 1 )
  341.     return offset
  342. }
  343.