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

  1. # $Header:   P:/source/ppee/macros/wp.pev   1.38   10 Aug 1990 16:11:34   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:   wp.pel  $:  word processing support
  20.  
  21.  
  22. local ALT_KEY = 0x08 
  23.  
  24. local wp_priorContextLeft
  25.  
  26. ### toggle word processing (word-wrap) mode for the current buffer
  27.  
  28. global function toggle_wp( on ){                #PUBLIC #VOID
  29.     if ( argcount() )
  30.         wp( on )
  31.     else
  32.         wp()
  33. }
  34.  
  35. global function wp( on ){                    #PUBLIC #VOID
  36.  
  37.     if( argcount() < 1 )
  38.         on = !and( buffer_flags, BUFFER_WP_ENABLED );
  39.     else
  40.         on = 0+on
  41.  
  42.     if ( on ) {
  43.         buffer_flags = or(buffer_flags, BUFFER_WP_ENABLED )
  44.         if ( !scroll_context_left ) {
  45.             # prevent column one from scrolling off the screen
  46.             # during wrap paragraph with default margins
  47.             wp_priorContextLeft = scroll_context_left = 5
  48.         }             
  49.         message( "word processing enabled" )
  50.     } else {
  51.         buffer_flags = and(buffer_flags, not(BUFFER_WP_ENABLED ))
  52.         if ( wp_priorContextLeft == scroll_context_left ) {
  53.             # restore context_left to what it was before WP
  54.             # unless it has been changed in the mean time
  55.             wp_priorContextLeft = scroll_context_left = 0
  56.         }
  57.         message( "word processing disabled" )
  58.     }
  59. }
  60.  
  61.  
  62. ### column and tab-stop shifts left and right
  63.  
  64. function indent_tab_maybe(){
  65.     if( region_type()){
  66.         if( and( keyboard_flags, ALT_KEY ))
  67.             outdent_tabs()
  68.         else
  69.             indent_tabs()
  70.     } else
  71.         insert_key()
  72. }
  73.  
  74. function outdent_tab_maybe(){
  75.     if( region_type())
  76.         outdent_tabs()
  77.     else
  78.         goto_prev_tab() 
  79. }
  80.  
  81.  
  82. global function indent_space_maybe(){
  83.     if( region_type())
  84.         indent_columns()     
  85.     else
  86.         insert_key()
  87. }
  88.  
  89.  
  90. global function outdent_space_maybe(){
  91.     if( region_type()){
  92.         outdent_columns()
  93.     } else {
  94.         backspace()
  95.     }
  96. }
  97.  
  98. global function indent_outdent_space(){
  99.     if( region_type()){
  100.         if( and( keyboard_flags, ALT_KEY ))
  101.             outdent_columns()     # Alt-whatever
  102.         else
  103.             indent_columns()     # whatever
  104.     } else
  105.         insert_key()            # whatever
  106. }
  107.  
  108.  
  109. ### delete_word()
  110.  
  111. function delete_word( n ){
  112.     drop_anchor()
  113.     next_word( n, "^|<|$" )
  114.     delete_chars()
  115. }
  116. ### delete_word()
  117.  
  118. function delete_prev_word( n ){
  119.     drop_anchor()
  120.     prev_word( n, "^|<|$" )
  121.     delete_chars()
  122. }
  123.  
  124.  
  125. ### sentence and paragraph motions:
  126.  
  127. local function s_forw( p, n ){
  128.     local old = search_count
  129.  
  130.     search_count = n ? n : 1
  131.     search( p, SEARCH_FWD_REGEX_MAX + SEARCH_ADVANCE )
  132.     search_count = old
  133. }
  134.  
  135. local function s_back( p, n ){
  136.     local old = search_count
  137.  
  138.     search_count = n ? n : 1
  139.     search( p, SEARCH_BKWD_REGEX_MAX + SEARCH_ADVANCE )
  140.     search_count = old
  141. }
  142.  
  143. local sentence_pattern = "\\.  *\\c"
  144.  
  145. local paragraph_pattern = "^[ \t]*$|^\\.PP|^\\.LP"
  146.  
  147. local section_pattern = "^\\{"    #}
  148.  
  149. function next_sentence( n ){    s_forw( sentence_pattern, n )    }
  150.  
  151. function prev_sentence( n ){    s_back( sentence_pattern, n )    }
  152.  
  153. function next_paragraph( n ){    s_forw( paragraph_pattern, n )    }
  154.  
  155. function prev_paragraph( n ){    s_back( paragraph_pattern, n )    }
  156.  
  157. function next_section( n ){    s_forw( section_pattern, n )    }
  158.  
  159. function prev_section( n ){    s_back( section_pattern, n )    }
  160.  
  161.  
  162.  
  163. ## reverse case of characters a string, upper to lower and lower to upper
  164. #    non-alphabetic characters are unaffected
  165. #
  166. function toreverse( s ){                    #PUBLIC #STR
  167.     local ch, r = ""
  168.     
  169.     while( s ){
  170.         ch = substr( s, 1, 1 )
  171.         s = substr( s, 2 )
  172.         if( islower( ch ))
  173.             ch = toupper( ch )
  174.         else if( isupper( ch ))
  175.             ch = tolower( ch )
  176.         r = r ch
  177.     }
  178.     
  179.     return r
  180. }
  181.  
  182. ## alter case sense of ranges in the current buffer
  183. #    non-alphabetic characters are unaffected
  184. #
  185.  
  186. function upper(){                        #PUBLIC #VOID
  187.     local ch
  188.     
  189.     rgetc_init()
  190.     
  191.     while(( ch = rgetc() )){
  192.         if( islower( ch )){
  193.             ch = toupper( ch )
  194.             rputc( ch )
  195.         }
  196.     }
  197.  
  198.     rgetc_done()
  199. }
  200.  
  201. function lower(){                        #PUBLIC #VOID
  202.     local ch
  203.     
  204.     rgetc_init()
  205.     
  206.     while(( ch = rgetc() )){
  207.         if( isupper( ch )){
  208.             ch = tolower( ch )
  209.             rputc( ch )
  210.         }
  211.     }
  212.  
  213.     rgetc_done()
  214. }
  215.  
  216. function reverse(){                        #PUBLIC #VOID
  217.     local ch
  218.     
  219.     rgetc_init()
  220.     
  221.     while(( ch = rgetc() )){
  222.         if( islower( ch )){
  223.             ch = toupper( ch )
  224.             rputc( ch )
  225.         } else if( isupper( ch )){
  226.             ch = tolower( ch )
  227.             rputc( ch )
  228.         }
  229.     }
  230.  
  231.     rgetc_done()
  232. }
  233.  
  234. ## capitalize the first letter of each word in a selection
  235. #    if there is no selection, capitalize the first letter
  236. #    of the next word.  If the selection starts in the middle
  237. #    of a word, alas, the middle character gets capitalized.
  238.  
  239. function capitalize(){                        #PUBLIC #VOID
  240.     local ch
  241.     
  242.     rgetc_init()
  243.  
  244.     while(( ch = rgetc())){
  245.         if( isalpha( ch )){
  246.             if( islower( ch ))
  247.                 rputc( toupper( ch ))
  248.             if( !next_word( 1 ))
  249.                 break
  250.         }
  251.     }
  252.  
  253.     rgetc_done()
  254. }
  255.  
  256.  
  257. ## rgetc(), rputc( ch ), rgetc_init(), rgetc_done()
  258. #
  259. #  This collection of functions facilitates the sequential replacement
  260. #  of all characters within a marked region.  If no selection is active,
  261. #  operate on the single character under the cursor.
  262. #
  263. #  Normal usage:
  264. #
  265. #        rgetc_init()
  266. #        while (( ch = rgetc())
  267. #            if ( P( ch ))
  268. #                rputc( F( ch ))
  269. #        rgetc_done()
  270. #
  271. # Revision 1.14 of this file included a different implementation
  272. # that cut the selection to a second buffer, processed the entire
  273. # buffer and then reinserted the result.  I thought it would be simpler
  274. # and faster, but it turned out to be more complex, and rather slow.
  275. #
  276. local x0, y0, x1, y1, rt, columnar, offset
  277.  
  278. local function rgetc(){
  279.     local    ch
  280.     local    pastEol
  281.     
  282.     restore_position( 0 )    # pop previously saved
  283.  
  284.     # if column selection, wrap at end of current row
  285.     #
  286.     if ( columnar ){
  287.         while (( pastEol = ( current_column >= x1 )) ||
  288.                 current_column < x0 ){
  289.             if ( !goto_pos( current_line + pastEol, x0 )){
  290.                 return ""
  291.             }
  292.             if ( !and( buffer_flags, BUFFER_IN_VIRTUAL_SPACE )){
  293.                 break
  294.             }
  295.             next_char()
  296.         }
  297.     }
  298.  
  299.     # test for past end of region
  300.     if( buffer_offset >= offset )
  301.         return ""
  302.  
  303.     ch = read_buffer( 1 )
  304.     save_position()
  305.     next_char()
  306.     if( !ch )
  307.         ch = "\n"
  308.  
  309.     return ch
  310. }
  311.  
  312.  
  313. # replace the character in the buffer most recently returned by rgetc
  314. #
  315. local function rputc( ch ){
  316.     restore_position( 1 )
  317.     insert_string( ch )
  318.     delete_chars(1)
  319.     save_position()
  320. }
  321.  
  322. # initialize a collection of variables for rgetc()/rputc() usage.
  323. #    Places the cursor at the upper left corner, and saves the 
  324. #    original position on the save_position stack.
  325. #
  326. #    No selection => process a single character.
  327. #
  328. local function rgetc_init(){
  329.     local tmp
  330.  
  331.     x1 = current_column
  332.     y1 = current_line
  333.     save_position()        # ending position of selection
  334.         
  335.     # if no region is marked we behave as if a single char is marked
  336.  
  337.     if (( rt = region_type()) == NO_SELECTION ){
  338.         x0 = x1++
  339.         y0 = y1
  340.     } else {
  341.  
  342.         # get and remember starting & ending marks & normalize
  343.         # their locations
  344.  
  345.         swap_marks()
  346.  
  347.         x0 = current_column
  348.         y0 = current_line
  349.         save_position()        # starting position of selection
  350.  
  351.         raise_anchor()
  352.  
  353.         # reverse starting & ending positions 
  354.         # if the latter precedes the former
  355.  
  356.         if( y1 < y0 || ( y1 == y0 && x1 < x0 )){
  357.             x0 = x1
  358.             y0 = y1
  359.             x1 = current_column
  360.             y1 = current_line
  361.             goto_pos( y0, x0 )
  362.         } 
  363.  
  364.         # per-selection-type fine-tuning
  365.     
  366.         columnar = 0
  367.         if( rt == COLUMN_SELECTION ){
  368.             columnar = 1
  369.             if( x1 < x0 ){
  370.                 tmp = x0
  371.                 x0 = x1
  372.                 x1 = tmp
  373.             }
  374.             x1++
  375.         } else if( rt == INCLUSIVE_SELECTION ){
  376.             x1++
  377.         } else if( rt == LINE_SELECTION ){
  378.             x0 = x1 = 1
  379.             y1++
  380.         }
  381.     }
  382.  
  383.     # save first byte offset past selection
  384.     goto_pos( y1, x1 )
  385.     offset = buffer_offset
  386.  
  387.     goto_pos( y0, x0 )
  388.     if ( and( buffer_flags, BUFFER_IN_VIRTUAL_SPACE )){
  389.         next_char()
  390.     }
  391.  
  392.     # rgetc() expects to discard a previously saved position:
  393.     save_position()            
  394. }
  395.  
  396. # clean up the save_position stack and restore the original cursor position
  397. #
  398. local function rgetc_done(){
  399.  
  400.     if( rt ){
  401.         restore_position( 1 )
  402.         drop_anchor( rt )
  403.     }
  404.  
  405.     restore_position( 1 )
  406. }
  407.  
  408.  
  409. ## get_region_as_string
  410. #
  411. # Read the characters in a region and return them as a string.
  412. # For columnar selections, the two parameters "sep" and "term"
  413. # specify a string to be used to separate each of the lines of
  414. # the selection and to terminate the last line of the selection,
  415. # respectively.  A suggestion for the values of these strings
  416. # is " " and "\n" or, alternatively, "\n" and "\n".
  417. #
  418. global function get_region_as_str(sep, term) {
  419.     local x0, y0, x1, y1, str, rt, len, line
  420.  
  421.     # if no region is marked we return a null string
  422.     if (( rt = region_type()) == NO_SELECTION )
  423.         return("")
  424.  
  425.     # determine the extent of the marked block
  426.     save_position()
  427.     x0 = current_column
  428.     y0 = current_line
  429.     swap_marks()
  430.     x1 = current_column
  431.     y1 = current_line
  432.  
  433.     # ensure that x0,y0 precedes x1,y1
  434.     if ((y1 < y0) || ((y1 == y0) && (x1 < x0))){
  435.         x1 = x0
  436.         y1 = y0
  437.         x0 = current_column
  438.         y0 = current_line
  439.     } 
  440.  
  441.     # adjust region coordinates depending on type
  442.     if (rt == INCLUSIVE_SELECTION)
  443.         x1++
  444.     else if (rt == LINE_SELECTION) {
  445.         x0 = x1 = 1
  446.         y1++
  447.     }
  448.  
  449.     # read the characters in the selection
  450.     goto_pos(y0, x0)
  451.     line = current_line
  452.     if  (rt == COLUMN_SELECTION) {
  453.         # read characters in the region, add separators, terminator
  454.         len = x1 - x0 + 1
  455.         for (str = ""; line <= y1; line++) {
  456.             str = str read_buffer(len) ((current_line < y1) ? sep : term)
  457.             current_column = x0
  458.             current_line++
  459.         }
  460.     } else {
  461.         # read the lines in the region, add newlines
  462.         for (str = ""; line <= y1; line++) {
  463.             if (current_line < y1)
  464.                 str = str read_buffer() "\n"
  465.             else if (x1 > current_column)
  466.                  str = str read_buffer(x1 - current_column)
  467.             current_column = 1
  468.             current_line++
  469.         }
  470.     }
  471.  
  472.     # clean up
  473.     restore_position(1)
  474.     raise_anchor()
  475.  
  476.     return(str)
  477. }
  478.