home *** CD-ROM | disk | FTP | other *** search
- # $Header: P:/source/ppee/macros/wp.pev 1.38 10 Aug 1990 16:11:34 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: wp.pel $: word processing support
-
-
- local ALT_KEY = 0x08
-
- local wp_priorContextLeft
-
- ### toggle word processing (word-wrap) mode for the current buffer
-
- global function toggle_wp( on ){ #PUBLIC #VOID
- if ( argcount() )
- wp( on )
- else
- wp()
- }
-
- global function wp( on ){ #PUBLIC #VOID
-
- if( argcount() < 1 )
- on = !and( buffer_flags, BUFFER_WP_ENABLED );
- else
- on = 0+on
-
- if ( on ) {
- buffer_flags = or(buffer_flags, BUFFER_WP_ENABLED )
- if ( !scroll_context_left ) {
- # prevent column one from scrolling off the screen
- # during wrap paragraph with default margins
- wp_priorContextLeft = scroll_context_left = 5
- }
- message( "word processing enabled" )
- } else {
- buffer_flags = and(buffer_flags, not(BUFFER_WP_ENABLED ))
- if ( wp_priorContextLeft == scroll_context_left ) {
- # restore context_left to what it was before WP
- # unless it has been changed in the mean time
- wp_priorContextLeft = scroll_context_left = 0
- }
- message( "word processing disabled" )
- }
- }
-
-
- ### column and tab-stop shifts left and right
-
- function indent_tab_maybe(){
- if( region_type()){
- if( and( keyboard_flags, ALT_KEY ))
- outdent_tabs()
- else
- indent_tabs()
- } else
- insert_key()
- }
-
- function outdent_tab_maybe(){
- if( region_type())
- outdent_tabs()
- else
- goto_prev_tab()
- }
-
-
- global function indent_space_maybe(){
- if( region_type())
- indent_columns()
- else
- insert_key()
- }
-
-
- global function outdent_space_maybe(){
- if( region_type()){
- outdent_columns()
- } else {
- backspace()
- }
- }
-
- global function indent_outdent_space(){
- if( region_type()){
- if( and( keyboard_flags, ALT_KEY ))
- outdent_columns() # Alt-whatever
- else
- indent_columns() # whatever
- } else
- insert_key() # whatever
- }
-
-
- ### delete_word()
-
- function delete_word( n ){
- drop_anchor()
- next_word( n, "^|<|$" )
- delete_chars()
- }
- ### delete_word()
-
- function delete_prev_word( n ){
- drop_anchor()
- prev_word( n, "^|<|$" )
- delete_chars()
- }
-
-
- ### sentence and paragraph motions:
-
- local function s_forw( p, n ){
- local old = search_count
-
- search_count = n ? n : 1
- search( p, SEARCH_FWD_REGEX_MAX + SEARCH_ADVANCE )
- search_count = old
- }
-
- local function s_back( p, n ){
- local old = search_count
-
- search_count = n ? n : 1
- search( p, SEARCH_BKWD_REGEX_MAX + SEARCH_ADVANCE )
- search_count = old
- }
-
- local sentence_pattern = "\\. *\\c"
-
- local paragraph_pattern = "^[ \t]*$|^\\.PP|^\\.LP"
-
- local section_pattern = "^\\{" #}
-
- function next_sentence( n ){ s_forw( sentence_pattern, n ) }
-
- function prev_sentence( n ){ s_back( sentence_pattern, n ) }
-
- function next_paragraph( n ){ s_forw( paragraph_pattern, n ) }
-
- function prev_paragraph( n ){ s_back( paragraph_pattern, n ) }
-
- function next_section( n ){ s_forw( section_pattern, n ) }
-
- function prev_section( n ){ s_back( section_pattern, n ) }
-
-
-
- ## reverse case of characters a string, upper to lower and lower to upper
- # non-alphabetic characters are unaffected
- #
- function toreverse( s ){ #PUBLIC #STR
- local ch, r = ""
-
- while( s ){
- ch = substr( s, 1, 1 )
- s = substr( s, 2 )
- if( islower( ch ))
- ch = toupper( ch )
- else if( isupper( ch ))
- ch = tolower( ch )
- r = r ch
- }
-
- return r
- }
-
- ## alter case sense of ranges in the current buffer
- # non-alphabetic characters are unaffected
- #
-
- function upper(){ #PUBLIC #VOID
- local ch
-
- rgetc_init()
-
- while(( ch = rgetc() )){
- if( islower( ch )){
- ch = toupper( ch )
- rputc( ch )
- }
- }
-
- rgetc_done()
- }
-
- function lower(){ #PUBLIC #VOID
- local ch
-
- rgetc_init()
-
- while(( ch = rgetc() )){
- if( isupper( ch )){
- ch = tolower( ch )
- rputc( ch )
- }
- }
-
- rgetc_done()
- }
-
- function reverse(){ #PUBLIC #VOID
- local ch
-
- rgetc_init()
-
- while(( ch = rgetc() )){
- if( islower( ch )){
- ch = toupper( ch )
- rputc( ch )
- } else if( isupper( ch )){
- ch = tolower( ch )
- rputc( ch )
- }
- }
-
- rgetc_done()
- }
-
- ## capitalize the first letter of each word in a selection
- # if there is no selection, capitalize the first letter
- # of the next word. If the selection starts in the middle
- # of a word, alas, the middle character gets capitalized.
-
- function capitalize(){ #PUBLIC #VOID
- local ch
-
- rgetc_init()
-
- while(( ch = rgetc())){
- if( isalpha( ch )){
- if( islower( ch ))
- rputc( toupper( ch ))
- if( !next_word( 1 ))
- break
- }
- }
-
- rgetc_done()
- }
-
-
- ## rgetc(), rputc( ch ), rgetc_init(), rgetc_done()
- #
- # This collection of functions facilitates the sequential replacement
- # of all characters within a marked region. If no selection is active,
- # operate on the single character under the cursor.
- #
- # Normal usage:
- #
- # rgetc_init()
- # while (( ch = rgetc())
- # if ( P( ch ))
- # rputc( F( ch ))
- # rgetc_done()
- #
- # Revision 1.14 of this file included a different implementation
- # that cut the selection to a second buffer, processed the entire
- # buffer and then reinserted the result. I thought it would be simpler
- # and faster, but it turned out to be more complex, and rather slow.
- #
- local x0, y0, x1, y1, rt, columnar, offset
-
- local function rgetc(){
- local ch
- local pastEol
-
- restore_position( 0 ) # pop previously saved
-
- # if column selection, wrap at end of current row
- #
- if ( columnar ){
- while (( pastEol = ( current_column >= x1 )) ||
- current_column < x0 ){
- if ( !goto_pos( current_line + pastEol, x0 )){
- return ""
- }
- if ( !and( buffer_flags, BUFFER_IN_VIRTUAL_SPACE )){
- break
- }
- next_char()
- }
- }
-
- # test for past end of region
- if( buffer_offset >= offset )
- return ""
-
- ch = read_buffer( 1 )
- save_position()
- next_char()
- if( !ch )
- ch = "\n"
-
- return ch
- }
-
-
- # replace the character in the buffer most recently returned by rgetc
- #
- local function rputc( ch ){
- restore_position( 1 )
- insert_string( ch )
- delete_chars(1)
- save_position()
- }
-
- # initialize a collection of variables for rgetc()/rputc() usage.
- # Places the cursor at the upper left corner, and saves the
- # original position on the save_position stack.
- #
- # No selection => process a single character.
- #
- local function rgetc_init(){
- local tmp
-
- x1 = current_column
- y1 = current_line
- save_position() # ending position of selection
-
- # if no region is marked we behave as if a single char is marked
-
- if (( rt = region_type()) == NO_SELECTION ){
- x0 = x1++
- y0 = y1
- } else {
-
- # get and remember starting & ending marks & normalize
- # their locations
-
- swap_marks()
-
- x0 = current_column
- y0 = current_line
- save_position() # starting position of selection
-
- raise_anchor()
-
- # reverse starting & ending positions
- # if the latter precedes the former
-
- if( y1 < y0 || ( y1 == y0 && x1 < x0 )){
- x0 = x1
- y0 = y1
- x1 = current_column
- y1 = current_line
- goto_pos( y0, x0 )
- }
-
- # per-selection-type fine-tuning
-
- columnar = 0
- if( rt == COLUMN_SELECTION ){
- columnar = 1
- if( x1 < x0 ){
- tmp = x0
- x0 = x1
- x1 = tmp
- }
- x1++
- } else if( rt == INCLUSIVE_SELECTION ){
- x1++
- } else if( rt == LINE_SELECTION ){
- x0 = x1 = 1
- y1++
- }
- }
-
- # save first byte offset past selection
- goto_pos( y1, x1 )
- offset = buffer_offset
-
- goto_pos( y0, x0 )
- if ( and( buffer_flags, BUFFER_IN_VIRTUAL_SPACE )){
- next_char()
- }
-
- # rgetc() expects to discard a previously saved position:
- save_position()
- }
-
- # clean up the save_position stack and restore the original cursor position
- #
- local function rgetc_done(){
-
- if( rt ){
- restore_position( 1 )
- drop_anchor( rt )
- }
-
- restore_position( 1 )
- }
-
-
- ## get_region_as_string
- #
- # Read the characters in a region and return them as a string.
- # For columnar selections, the two parameters "sep" and "term"
- # specify a string to be used to separate each of the lines of
- # the selection and to terminate the last line of the selection,
- # respectively. A suggestion for the values of these strings
- # is " " and "\n" or, alternatively, "\n" and "\n".
- #
- global function get_region_as_str(sep, term) {
- local x0, y0, x1, y1, str, rt, len, line
-
- # if no region is marked we return a null string
- if (( rt = region_type()) == NO_SELECTION )
- return("")
-
- # determine the extent of the marked block
- save_position()
- x0 = current_column
- y0 = current_line
- swap_marks()
- x1 = current_column
- y1 = current_line
-
- # ensure that x0,y0 precedes x1,y1
- if ((y1 < y0) || ((y1 == y0) && (x1 < x0))){
- x1 = x0
- y1 = y0
- x0 = current_column
- y0 = current_line
- }
-
- # adjust region coordinates depending on type
- if (rt == INCLUSIVE_SELECTION)
- x1++
- else if (rt == LINE_SELECTION) {
- x0 = x1 = 1
- y1++
- }
-
- # read the characters in the selection
- goto_pos(y0, x0)
- line = current_line
- if (rt == COLUMN_SELECTION) {
- # read characters in the region, add separators, terminator
- len = x1 - x0 + 1
- for (str = ""; line <= y1; line++) {
- str = str read_buffer(len) ((current_line < y1) ? sep : term)
- current_column = x0
- current_line++
- }
- } else {
- # read the lines in the region, add newlines
- for (str = ""; line <= y1; line++) {
- if (current_line < y1)
- str = str read_buffer() "\n"
- else if (x1 > current_column)
- str = str read_buffer(x1 - current_column)
- current_column = 1
- current_line++
- }
- }
-
- # clean up
- restore_position(1)
- raise_anchor()
-
- return(str)
- }
-