home *** CD-ROM | disk | FTP | other *** search
- # $Header: P:/source/ppee/macros/motion.pev 1.46 15 Aug 1990 11:48:52 ericj $
-
- ##############################################################################
- #
- # 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: motion.pel $: support for cursor motion
-
-
- local consecutive_homes = 0
- local consecutive_ends = 0
-
- # change current position so that it matches the indicated window extremity.
- #
- # see also: scroll_window_... in windows.pel, which change window
- # orientation without affecting the current position.
- #
-
- global function goto_window_top() { #PUBLIC #VOID
- return up( distance_to_window_top() )
- }
-
- global function goto_window_right() { #PUBLIC #VOID
- return right( distance_to_window_right() )
- }
-
- global function goto_window_left() { #PUBLIC #VOID
- return left( distance_to_window_left() )
- }
-
- global function goto_window_bottom() { #PUBLIC #VOID
- return down( distance_to_window_bottom() )
- }
-
- global function goto_window_middle() { #PUBLIC #VOID
- current_line += distance_to_window_middle()
- }
-
-
- # home_key()
- #
- # move the cursor depending on the number of consecutive times the
- # <Home> key is typed:
- # one time - move to the beginning of the current line
- # two times - move to the upper left corner of the window
- # three times - move to the beginning of the buffer
-
- global function home_key() {
-
- if ( and(prev_key,0xff00) == 0x4700) {
- if (++consecutive_homes == 2) {
- goto_window_top() # <home><home>
- } else {
- goto_buffer_top() # <home><home><home>
- }
- } else {
- consecutive_homes = 1
- goto_bol() # <home>
- }
- }
-
-
- # end_key()
- #
- # move the cursor depending on the number of consecutive times the
- # <End> key is typed:
- # one time - move to the end of the current line
- # two times - move to the end of the last line in the window
- # three times - move to the end of the buffer
-
- global function end_key() {
-
- if ( and(prev_key,0xff00) == 0x4f00) {
- if (++consecutive_ends == 2) {
- goto_window_bottom() # <end><end>
- goto_eol()
- } else {
- goto_buffer_bottom() # <end><end><end>
- }
- } else {
- consecutive_ends = 1
- goto_eol() # <end>
- }
- }
-
-
- global function goto_line_key() {
- local str, i
-
- str = prompt( "Goto line: ","" )
- if (!(str ~ "^[ \t]*$" )) {
- i = atoi(str)
- if (i) {
- goto_line( i )
- message( "" )
- }
- }
- return TRUE
- }
-
- ## prompt the user for a motion destination; and then go there
- # digits are interpreted as a line number
- # Alt-digit is interpreted as a mark location.
-
- global function goto_line_or_mark(){
- local str, i
-
- attach_event_handler( EVENT_INVALID_PCHAR,
- function_id( "maybe_goto_mark" ))
-
- str = prompt( "Goto line: ", "" )
- if (str ~ "^[ \t]*$")
- return
- message( "" )
-
- if( str ~ "^Mark #[0-9]$" ){
- i = atoi( substr( str, 7 ))
- goto_bookmark( i ? i : 10 )
- } else if( str && ( i = atoi( str ))){
- goto_line( i )
- } else
- message( "Invalid goto destination" )
-
- delete_event( EVENT_INVALID_PCHAR,
- function_id( "maybe_goto_mark" ))
- }
-
- function maybe_goto_mark(){
- local mid = alt_digit_p( current_key )
-
- if( mid != -1 )
- prompt_response = "Mark #" mid
- }
-
- function alt_digit_p( key ){
- if( key == 30720 )
- return 1
- else if( key == 30976 )
- return 2
- else if( key == 31232 )
- return 3
- else if( key == 31488 )
- return 4
- else if( key == 31744 )
- return 5
- else if( key == 32000 )
- return 6
- else if( key == 32256 )
- return 7
- else if( key == 32512 )
- return 8
- else if( key == 32768 )
- return 9
- else if( key == 33024 )
- return 0
- else
- return -1
- }
-
- ## word search functions
-
- global function next_word( n, patt ){ #PUBLIC #INT
- search_count = ( n ? 0+n : 1 )
-
- return search(( patt ? patt : "<" ), SEARCH_FWD_REGEX_ADV )
- }
-
- global function prev_word( n, patt ){ #PUBLIC #INT
- search_count = ( n ? 0+n : 1)
-
- return search(( patt ? patt : "<" ), SEARCH_BKWD_REGEX_ADV )
- }
-
-
- ## next/previous line ala' VI: move to first non-blank character
-
- function next_line( n ){ #PUBLIC #VOID
- if( !argcount())
- n = 1
- down( n )
- skip_whitespace()
- }
-
- function prev_line( n ){ #PUBLIC #VOID
- if( !argcount())
- n = 1
- up( n )
- skip_whitespace()
- }
-
- function skip_whitespace(){ #PUBLIC #VOID
- local sflags = SEARCH_MAXIMAL_MATCH + SEARCH_FORWARD + SEARCH_REGEX
- # next_word is similar, but fails on lines containing only whitespace
-
- goto_bol()
- search( "^[ \t]*\\c", sflags )
- }
-
-
- ## rs_up(), rs_down(), rs_page_up(), rs_page_down()
- # - vertical motion commands designed to operate in real space only.
- # If the cursor is past the end of a line, or in virtual space past a
- # tab, the cursor is moved left to the nearest "real" column, while the
- # "virtual" column position is saved.
- #
-
- local vert_column # save the column we were in in the previous line
- local vert_command # the previous vertical motion command
-
- # move up one line and if in real space only, restore the column to
- # what it was at the first consecutive up or down sequence of moves.
- #
- global function rs_up(){
- up()
- if ( prev_command != vert_command ) {
- vert_column = current_column
- } else {
- current_column = vert_column
- }
- if ( and( buffer_flags, BUFFER_POSITION_IS_VIRTUAL )) {
- prev_char()
- }
- vert_command = current_command
- }
-
- # move up one line and if in real space only, restore the column to
- # what it was at the first consecutive up or down sequence of moves.
- #
- global function rs_down() {
- down()
- if ( prev_command != vert_command ) {
- vert_column = current_column
- } else {
- current_column = vert_column
- }
- if ( and( buffer_flags, BUFFER_POSITION_IS_VIRTUAL )) {
- prev_char()
- }
- vert_command = current_command
- }
-
- # move up one page and if in real space only, restore the column to
- # what it was at the first consecutive up or down sequence of moves.
- #
- global function rs_up_page(){
- page_up()
- if ( prev_command != vert_command ) {
- vert_column = current_column
- } else {
- current_column = vert_column
- }
- if ( and( buffer_flags, BUFFER_POSITION_IS_VIRTUAL )) {
- prev_char()
- }
- vert_command = current_command
- }
-
- # move up one page and if in real space only, restore the column to
- # what it was at the first consecutive up or down sequence of moves.
- #
- global function rs_page_down(){
- page_down()
- if ( prev_command != vert_command ) {
- vert_column = current_column
- } else {
- current_column = vert_column
- }
- if ( and( buffer_flags, BUFFER_POSITION_IS_VIRTUAL )) {
- prev_char()
- }
- vert_command = current_command
- }
-
-
- ## keypad_motion()
- #
- # perform default cursor motion actions given the scancode for a keypad key
- #
- global function keypad_motion( scanCode ){
- local mousePos
-
- if ( scanCode == SCANCODE_UP ) {
- current_line--
- } else if ( scanCode == SCANCODE_DOWN ) {
- current_line++
- } else if ( scanCode == SCANCODE_LEFT ) {
- current_column--
- } else if ( scanCode == SCANCODE_RIGHT ) {
- current_column++
- } else if ( scanCode == SCANCODE_HOME ) {
- goto_bol()
- } else if ( scanCode == SCANCODE_END ) {
- goto_eol()
- } else if ( scanCode == SCANCODE_PGUP ) {
- page_up()
- } else if ( scanCode == SCANCODE_PGDN ) {
- page_down()
- } else if ( scanCode == SCANCODE_CTRL_HOME ) {
- goto_window_top()
- } else if ( scanCode == SCANCODE_CTRL_END ) {
- goto_window_bottom()
- } else if ( scanCode == SCANCODE_CENTER ) {
- center_cursor()
- } else if ( scanCode == SCANCODE_LEFT_PRESS ) {
- menu_mouse_key()
- } else if ( scanCode == SCANCODE_RIGHT_PRESS ) {
- # do nothing
- } else {
- return 0
- }
-
- return 1
- }
-