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

  1. # $Header:   P:/source/ppee/macros/motion.pev   1.46   15 Aug 1990 11:48:52   ericj  $
  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:   motion.pel  $: support for cursor motion
  20.  
  21.  
  22. local    consecutive_homes = 0
  23. local    consecutive_ends = 0
  24.  
  25. # change current position so that it matches the indicated window extremity.
  26. #
  27. # see also: scroll_window_... in windows.pel, which change window
  28. # orientation without affecting the current position.
  29. #
  30.  
  31. global function goto_window_top() {                #PUBLIC #VOID
  32.     return up( distance_to_window_top() )
  33. }
  34.  
  35. global function goto_window_right() {                #PUBLIC #VOID
  36.     return right( distance_to_window_right() )
  37. }
  38.  
  39. global function goto_window_left() {                #PUBLIC #VOID
  40.     return left( distance_to_window_left() )
  41. }
  42.  
  43. global function goto_window_bottom() {                #PUBLIC #VOID
  44.     return down( distance_to_window_bottom() )
  45. }
  46.  
  47. global function goto_window_middle() {                #PUBLIC #VOID
  48.     current_line += distance_to_window_middle()
  49. }
  50.  
  51.  
  52. #  home_key()
  53. #
  54. #  move the cursor depending on the number of consecutive times the
  55. #  <Home> key is typed:
  56. #    one time -    move to the beginning of the current line
  57. #    two times -    move to the upper left corner of the window
  58. #    three times -    move to the beginning of the buffer
  59.  
  60. global function home_key() {
  61.  
  62.     if ( and(prev_key,0xff00) == 0x4700) {
  63.         if (++consecutive_homes == 2) {
  64.             goto_window_top()    # <home><home>
  65.         } else {
  66.             goto_buffer_top()    # <home><home><home>
  67.         }
  68.     } else {
  69.         consecutive_homes = 1
  70.         goto_bol()            # <home>
  71.     }
  72. }
  73.  
  74.  
  75. #  end_key()
  76. #
  77. #  move the cursor depending on the number of consecutive times the
  78. #  <End> key is typed:
  79. #    one time -    move to the end of the current line
  80. #    two times -    move to the end of the last line in the window
  81. #    three times -    move to the end of the buffer
  82.  
  83. global function end_key() {
  84.  
  85.     if ( and(prev_key,0xff00) == 0x4f00) {
  86.         if (++consecutive_ends == 2) {
  87.             goto_window_bottom()    # <end><end>
  88.             goto_eol()
  89.         } else {
  90.             goto_buffer_bottom()    # <end><end><end>
  91.         }
  92.     } else {
  93.         consecutive_ends = 1
  94.         goto_eol()            # <end>
  95.     }
  96. }
  97.  
  98.  
  99. global function goto_line_key() {
  100.     local    str, i
  101.  
  102.     str = prompt( "Goto line: ","" )
  103.     if (!(str ~ "^[ \t]*$" )) {
  104.         i = atoi(str)
  105.         if (i) {
  106.             goto_line( i )
  107.             message( "" )
  108.         }
  109.     }
  110.     return TRUE
  111. }
  112.  
  113. ## prompt the user for a motion destination; and then go there
  114. #    digits are interpreted as a line number
  115. #    Alt-digit is interpreted as a mark location.
  116.  
  117. global function goto_line_or_mark(){
  118.     local    str, i
  119.  
  120.     attach_event_handler( EVENT_INVALID_PCHAR,
  121.         function_id( "maybe_goto_mark" ))
  122.  
  123.     str = prompt( "Goto line: ", "" )
  124.     if (str ~ "^[ \t]*$") 
  125.         return
  126.     message( "" )
  127.  
  128.     if( str ~ "^Mark #[0-9]$" ){
  129.         i = atoi( substr( str, 7 ))
  130.         goto_bookmark( i ? i : 10 )
  131.     } else if( str && ( i = atoi( str ))){
  132.         goto_line( i )
  133.     } else
  134.         message( "Invalid goto destination" )
  135.  
  136.     delete_event( EVENT_INVALID_PCHAR,
  137.         function_id( "maybe_goto_mark" ))
  138. }
  139.  
  140. function maybe_goto_mark(){
  141.     local mid = alt_digit_p( current_key )
  142.  
  143.     if( mid != -1 )
  144.         prompt_response = "Mark #" mid
  145. }
  146.  
  147. function alt_digit_p( key ){
  148.     if( key == 30720 )
  149.         return 1
  150.     else if( key == 30976 )
  151.         return 2
  152.     else if( key == 31232 )
  153.         return 3
  154.     else if( key == 31488 )
  155.         return 4
  156.     else if( key == 31744 )
  157.         return 5
  158.     else if( key == 32000 )
  159.         return 6
  160.     else if( key == 32256 )
  161.         return 7
  162.     else if( key == 32512 )
  163.         return 8
  164.     else if( key == 32768 )
  165.         return 9
  166.     else if( key == 33024 )
  167.         return 0
  168.     else
  169.         return -1
  170. }
  171.  
  172. ## word search functions
  173.  
  174. global function next_word( n, patt ){                #PUBLIC #INT
  175.     search_count = ( n ? 0+n : 1 )
  176.  
  177.     return search(( patt ? patt : "<" ), SEARCH_FWD_REGEX_ADV )
  178. }
  179.  
  180. global function prev_word( n, patt ){                #PUBLIC #INT
  181.     search_count = ( n ? 0+n : 1)
  182.  
  183.     return search(( patt ? patt : "<" ), SEARCH_BKWD_REGEX_ADV )
  184. }
  185.  
  186.  
  187. ## next/previous line ala' VI: move to first non-blank character
  188.  
  189. function next_line( n ){                    #PUBLIC #VOID
  190.     if( !argcount())
  191.         n = 1
  192.     down( n )
  193.     skip_whitespace()
  194. }
  195.  
  196. function prev_line( n ){                    #PUBLIC #VOID
  197.     if( !argcount())
  198.         n = 1
  199.     up( n )
  200.     skip_whitespace()
  201. }
  202.  
  203. function skip_whitespace(){                    #PUBLIC #VOID
  204.     local sflags = SEARCH_MAXIMAL_MATCH + SEARCH_FORWARD + SEARCH_REGEX
  205.     # next_word is similar, but fails on lines containing only whitespace
  206.     
  207.     goto_bol()
  208.     search( "^[ \t]*\\c", sflags )
  209. }
  210.  
  211.  
  212. ## rs_up(), rs_down(), rs_page_up(), rs_page_down()
  213. #    - vertical motion commands designed to operate in real space only.
  214. # If the cursor is past the end of a line, or in virtual space past a
  215. # tab, the cursor is moved left to the nearest "real" column, while the
  216. # "virtual" column position is saved.
  217. #
  218.  
  219. local    vert_column    # save the column we were in in the previous line
  220. local    vert_command    # the previous vertical motion command
  221.  
  222. # move up one line and if in real space only, restore the column to
  223. # what it was at the first consecutive up or down sequence of moves.
  224. #
  225. global function rs_up(){
  226.     up()
  227.     if ( prev_command != vert_command ) {
  228.         vert_column = current_column
  229.     } else {
  230.         current_column = vert_column
  231.     }
  232.     if ( and( buffer_flags, BUFFER_POSITION_IS_VIRTUAL )) {
  233.         prev_char()
  234.     }
  235.     vert_command = current_command
  236. }
  237.  
  238. # move up one line and if in real space only, restore the column to
  239. # what it was at the first consecutive up or down sequence of moves.
  240. #
  241. global function rs_down() {
  242.     down()
  243.     if ( prev_command != vert_command ) {
  244.         vert_column = current_column
  245.     } else {
  246.         current_column = vert_column
  247.     }
  248.     if ( and( buffer_flags, BUFFER_POSITION_IS_VIRTUAL )) {
  249.         prev_char()
  250.     }
  251.     vert_command = current_command
  252. }
  253.  
  254. # move up one page and if in real space only, restore the column to
  255. # what it was at the first consecutive up or down sequence of moves.
  256. #
  257. global function rs_up_page(){
  258.     page_up()
  259.     if ( prev_command != vert_command ) {
  260.         vert_column = current_column
  261.     } else {
  262.         current_column = vert_column
  263.     }
  264.     if ( and( buffer_flags, BUFFER_POSITION_IS_VIRTUAL )) {
  265.         prev_char()
  266.     }
  267.     vert_command = current_command
  268. }
  269.  
  270. # move up one page and if in real space only, restore the column to
  271. # what it was at the first consecutive up or down sequence of moves.
  272. #
  273. global function rs_page_down(){
  274.     page_down()
  275.     if ( prev_command != vert_command ) {
  276.         vert_column = current_column
  277.     } else {
  278.         current_column = vert_column
  279.     }
  280.     if ( and( buffer_flags, BUFFER_POSITION_IS_VIRTUAL )) {
  281.         prev_char()
  282.     }
  283.     vert_command = current_command
  284. }
  285.  
  286.  
  287. ## keypad_motion()
  288. #
  289. # perform default cursor motion actions given the scancode for a keypad key
  290. #
  291. global function keypad_motion( scanCode ){
  292.     local    mousePos
  293.  
  294.     if ( scanCode == SCANCODE_UP ) {
  295.         current_line--
  296.     } else if ( scanCode == SCANCODE_DOWN ) {
  297.         current_line++
  298.     } else if ( scanCode == SCANCODE_LEFT ) {
  299.         current_column--
  300.     } else if ( scanCode == SCANCODE_RIGHT ) {
  301.         current_column++
  302.     } else if ( scanCode == SCANCODE_HOME ) {
  303.         goto_bol()
  304.     } else if ( scanCode == SCANCODE_END ) {
  305.         goto_eol()
  306.     } else if ( scanCode == SCANCODE_PGUP ) {
  307.         page_up()
  308.     } else if ( scanCode == SCANCODE_PGDN ) {
  309.         page_down()
  310.     } else if ( scanCode == SCANCODE_CTRL_HOME ) {
  311.         goto_window_top()
  312.     } else if ( scanCode == SCANCODE_CTRL_END ) {
  313.         goto_window_bottom()
  314.     } else if ( scanCode == SCANCODE_CENTER    ) {
  315.         center_cursor()
  316.     } else if ( scanCode == SCANCODE_LEFT_PRESS ) {
  317.         menu_mouse_key()
  318.     } else if ( scanCode == SCANCODE_RIGHT_PRESS ) {
  319.         # do nothing
  320.     } else {
  321.         return 0
  322.     }
  323.  
  324.     return 1
  325. }
  326.