home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / p2demo21.exe / PEL / MOTION.PEL < prev    next >
Text File  |  1995-03-22  |  12KB  |  461 lines

  1. # $Header:   P:\source\wmacros\motion.pev   1.24   22 Mar 1995 11:00:30   pfhmlw0  $
  2.  
  3. ##############################################################################
  4. #
  5. #       Compuware Corporation
  6. #         31440 Northwestern Highway
  7. #           Farmington Hills, Michigan 48334-2564
  8. #
  9. #   This source code listing contains information that is
  10. #   proprietary to Compuware Corporation and may not be copied
  11. #   duplicated, translated, transmitted, stored, retrieved
  12. #   or in any manner or by any method conveyed or disclosed
  13. #   to a third party or parties without express written
  14. #   permission from Compuware Corporation.
  15. #
  16. #  
  17. ##############################################################################
  18.  
  19. #### $Workfile:   motion.pel  $: support for cursor motion
  20.  
  21. local   consecutive_homes = 0
  22. local   consecutive_ends  = 0
  23.  
  24. # change current position so that it matches the indicated window extremity.
  25. #
  26. # see also: scroll_window_... in windows.pel, which change window
  27. # orientation without affecting the current position.
  28. #
  29.  
  30. global function goto_window_top()                               #PUBLIC #VOID
  31. {
  32.    return up( distance_to_window_top() )
  33. }
  34.  
  35. global function goto_window_right()                             #PUBLIC #VOID
  36. {
  37.    return right( distance_to_window_right() )
  38. }
  39.  
  40. global function goto_window_left()                              #PUBLIC #VOID
  41. {
  42. #   return left( distance_to_window_left() )
  43.     return goto_pos(0, window_margin + 1)
  44. }
  45.  
  46. global function goto_window_bottom()                            #PUBLIC #VOID
  47. {
  48.    return down( distance_to_window_bottom() )
  49. }
  50.  
  51. global function goto_window_middle()                            #PUBLIC #VOID
  52. {
  53.    current_line += distance_to_window_middle()
  54. }
  55.  
  56. #  home_key()
  57. #
  58. #  move the cursor depending on the number of consecutive times the
  59. #  <Home> key is typed:
  60. #       one time    -   move to the beginning of the current line
  61. #       two times   -   move to the upper left corner of the window
  62. #       three times -   move to the beginning of the buffer
  63. global function home_key(keycode)
  64. {
  65.    if (argcount() ? (prev_key == keycode) \
  66.                   : (prev_key == KEYCODE_HOME || prev_key == KEYCODE_NUM_HOME))
  67.    {
  68.       consecutive_homes++
  69.  
  70.       if (linecommands_enabled && linenumber_width && \
  71.           !and(window_flags, WINDOW_ASCII_DUMP))
  72.       {
  73.          if (consecutive_homes == 2)      # <Home><Home>
  74.             goto_bolc()
  75.          else if (consecutive_homes == 3) # <Home><Home><Home>
  76.             goto_window_top()
  77.          else                             # <Home><Home><Home><Home>
  78.             goto_buffer_top()
  79.       }
  80.       else
  81.       {
  82.          if (consecutive_homes == 2)      # <Home><Home>
  83.             {
  84.             goto_window_top()
  85.             goto_bol()
  86.             }
  87.          else                             # <Home><Home><Home>
  88.             goto_buffer_top()
  89.       }
  90.    }
  91.    else
  92.    {
  93.       consecutive_homes = 1
  94.       goto_bol()                          # <Home>
  95.    }
  96. }
  97.  
  98. #  end_key()
  99. #
  100. #  move the cursor depending on the number of consecutive times the
  101. #  <End> key is typed:
  102. #       one time    -   move to the end of the current line
  103. #       two times   -   move to the end of the last line in the window
  104. #       three times -   move to the end of the buffer
  105. global function end_key(keycode)
  106. {
  107.    if (argcount() ? (prev_key == keycode) \
  108.                   : (prev_key == KEYCODE_END || prev_key == KEYCODE_NUM_END))
  109.    {
  110.       if (++consecutive_ends == 2)
  111.       {
  112.          goto_window_bottom()    # <End><End>
  113.          goto_eol()
  114.       }
  115.       else
  116.          goto_buffer_bottom()    # <End><End><End>
  117.    }
  118.    else
  119.    {
  120.       consecutive_ends = 1
  121.       goto_eol()                      # <End>
  122.    }
  123. }
  124.  
  125. ## goto_line() -- move the cursor to column one of the specified line
  126. #
  127. global function goto_line( line )                               #PUBLIC #INT
  128. {           
  129.    line += 0
  130.  
  131.    if ( !argcount() ) 
  132.       line = current_line
  133.    else if ( line < 1) 
  134.       line = 1
  135.  
  136.    return goto_pos( line, 1 )
  137. }
  138.  
  139. global function goto_line_key()
  140. {
  141.    local   str, i
  142.  
  143.    str = prompt_history( "GOTOLINE", "Goto line: ", "", 1, 1, "linenum" )
  144.    if (!(str ~ "^[ \t]*$" ))
  145.    {
  146.       i = atoi(str)
  147.       if (i)
  148.       {
  149.          goto_line( i )
  150.          message( "" )
  151.       }
  152.    }
  153.    return TRUE
  154. }
  155.  
  156. ## prompt the user for a motion destination; and then go there
  157. #       digits are interpreted as a line number
  158. #       Alt-digit is interpreted as a mark location.
  159.  
  160. global function goto_line_or_mark()
  161. {
  162.    local   str, i
  163.  
  164.    attach_event_handler( EVENT.INVALID_PCHAR, function_id( "maybe_goto_mark" ))
  165.  
  166.    str = prompt( "Goto line: ", "", "goto_line_or_mark_dialog" )
  167.    if (str ~ "^[ \t]*$")
  168.       return
  169.    else
  170.    {
  171.       message( "" )
  172.  
  173.       if( str ~ "^Mark #[0-9]$" )
  174.       {
  175.          i = atoi( substr( str, 7 ))
  176.          goto_bookmark( i ? i : 10 )
  177.       }
  178.       else if( str && ( i = atoi( str )))
  179.          goto_line( i )
  180.       else
  181.          goto_bookmark(str)
  182.    
  183.       delete_event( EVENT.INVALID_PCHAR, function_id( "maybe_goto_mark" ))
  184.    }
  185. }
  186.  
  187. function maybe_goto_mark()
  188. {
  189.    local mid = alt_digit_p( current_key )
  190.  
  191.    if( mid != -1 )
  192.       prompt_response = "Mark #" mid
  193. }
  194.  
  195. function alt_digit_p( key )
  196. {
  197.    if( key == KEYCODE_ALT_1 )
  198.       return 1
  199.    else if( key == KEYCODE_ALT_2 )
  200.       return 2
  201.    else if( key == KEYCODE_ALT_3 )
  202.       return 3
  203.    else if( key == KEYCODE_ALT_4 )
  204.       return 4
  205.    else if( key == KEYCODE_ALT_5 )
  206.       return 5
  207.    else if( key == KEYCODE_ALT_6 )
  208.       return 6
  209.    else if( key == KEYCODE_ALT_7 )
  210.       return 7
  211.    else if( key == KEYCODE_ALT_8 )
  212.       return 8
  213.    else if( key == KEYCODE_ALT_9 )
  214.       return 9
  215.    else if( key == KEYCODE_ALT_0 )
  216.       return 0
  217.    else
  218.       return -1
  219. }
  220.  
  221. ## word search functions
  222.  
  223. global function next_word( n, patt )                            #PUBLIC #INT
  224. {
  225.    search_count = ( n ? 0+n : 1 )
  226.  
  227.    return search(( patt ? patt : "<" ), SEARCH_FWD_REGEX_ADV )
  228. }
  229.  
  230. global function prev_word( n, patt )                            #PUBLIC #INT
  231. {
  232.    # to move past last character found
  233.    search_count = ( n ? 0+n : 1)
  234.    return search(( patt ? patt : "<" ), SEARCH_BKWD_REGEX_ADV )
  235. }
  236.  
  237.  
  238. ## vertical "word" motion
  239.  
  240. global function up_whitespace()                                 #PUBLIC #VOID
  241. {
  242.    # Move up, in the same column, until we enter whitespace
  243.    while( !in_whitespace() && up() ){
  244.    }
  245.  
  246.    # Move up, in the same column, until we leave whitespace
  247.    while( up() && in_whitespace() ){
  248.    }
  249. }
  250.  
  251. global function down_whitespace()                               #PUBLIC #VOID
  252. {
  253.    # Move down, in the same column, until we enter whitespace
  254.    while( !in_whitespace() && down() ){
  255.    }
  256.  
  257.    # Move down, in the same column, until we leave whitespace
  258.    while( down() && in_whitespace() ){
  259.    }
  260. }
  261.  
  262. # determine whether the cursor is in whitespace
  263. global function in_whitespace()
  264. {
  265.    return  and( buffer_flags, BUFFER_IN_VIRTUAL_SPACE )    \
  266.                    || ( read_buffer(1) !~ /[^ \t]/ )
  267. }
  268.  
  269.  
  270. # Scroll to the right or left 1 page, while trying to keep the cursor
  271. # in the same relative location within the window.
  272. #
  273. global function right_page()                               #PUBLIC #VOID
  274. {
  275.    scroll_horizontal((window_text_width - linenumber_width), 1)
  276. }
  277.  
  278. global function left_page()                                #PUBLIC #VOID
  279. {
  280.    scroll_horizontal(-(window_text_width - linenumber_width), 1)
  281. }
  282.  
  283. # Scroll to the right or left 1/2 page, while trying to keep the cursor
  284. # in the same relative location within the window.
  285. #
  286. global function right_half_page()                               #PUBLIC #VOID
  287. {
  288.    scroll_horizontal((window_text_width - linenumber_width) / 2, 1)
  289. }
  290.  
  291. global function left_half_page()                                #PUBLIC #VOID
  292. {
  293.    scroll_horizontal(-((window_text_width - linenumber_width) / 2), 1)
  294. }
  295.  
  296. ## next/previous line ala' VI: move to first non-blank character
  297.  
  298. function next_line( n )                                         #PUBLIC #VOID
  299. {
  300.    if( !argcount())
  301.       n = 1
  302.  
  303.    down( n )
  304.    skip_whitespace()
  305. }
  306.  
  307. function prev_line( n )                                         #PUBLIC #VOID
  308. {
  309.    if( !argcount())
  310.       n = 1
  311.  
  312.    up( n )
  313.    skip_whitespace()
  314. }
  315.  
  316. function skip_whitespace()                                      #PUBLIC #VOID
  317. {
  318.    local sflags = SEARCH_MAXIMAL_MATCH + SEARCH_FORWARD + SEARCH_REGEX
  319.    # next_word is similar, but fails on lines containing only whitespace
  320.    
  321.    goto_bol()
  322.    search( "^[ \t]*\\c", sflags )
  323. }
  324.  
  325.  
  326. ## rs_up(), rs_down(), rs_page_up(), rs_page_down()
  327. #       - vertical motion commands designed to operate in real space only.
  328. # If the cursor is past the end of a line, or in virtual space past a
  329. # tab, the cursor is moved left to the nearest "real" column, while the
  330. # "virtual" column position is saved.
  331. #
  332.  
  333. local   vert_column     # save the column we were in in the previous line
  334. local   vert_command    # the previous vertical motion command
  335.  
  336. # move up one line and if in real space only, restore the column to
  337. # what it was at the first consecutive up or down sequence of moves.
  338. #
  339. global function rs_up()
  340. {
  341.    if ( prev_command != vert_command )
  342.    {
  343.       vert_column = current_column
  344.       up()
  345.    }
  346.    else
  347.    {
  348.       current_column = vert_column
  349.       up()
  350.    }
  351.  
  352.    if ( and( buffer_flags, BUFFER_POSITION_IS_VIRTUAL ))
  353.       prev_char()
  354.  
  355.    vert_command = current_command
  356. }
  357.  
  358. # move up one line and if in real space only, restore the column to
  359. # what it was at the first consecutive up or down sequence of moves.
  360. #
  361. global function rs_down()
  362. {
  363.    if ( prev_command != vert_command )
  364.    {
  365.       vert_column = current_column
  366.       down()
  367.    }
  368.    else
  369.    {
  370.       down()
  371.       current_column = vert_column
  372.    }
  373.  
  374.    if ( and( buffer_flags, BUFFER_POSITION_IS_VIRTUAL ))
  375.       prev_char()
  376.  
  377.    vert_command = current_command
  378. }
  379.  
  380. # move up one page and if in real space only, restore the column to
  381. # what it was at the first consecutive up or down sequence of moves.
  382. #
  383. global function rs_page_up()
  384. {
  385.    if ( prev_command != vert_command )
  386.    {
  387.       vert_column = current_column
  388.       page_up()
  389.    }
  390.    else
  391.    {
  392.       page_up()
  393.       current_column = vert_column
  394.    }
  395.  
  396.    if ( and( buffer_flags, BUFFER_POSITION_IS_VIRTUAL ))
  397.       prev_char()
  398.  
  399.    vert_command = current_command
  400. }
  401.  
  402. # move up one page and if in real space only, restore the column to
  403. # what it was at the first consecutive up or down sequence of moves.
  404. #
  405. global function rs_page_down()
  406. {
  407.    if ( prev_command != vert_command )
  408.    {
  409.       vert_column = current_column
  410.       page_down()
  411.    }
  412.    else
  413.    {
  414.       page_down()
  415.       current_column = vert_column
  416.    }
  417.  
  418.    if ( and( buffer_flags, BUFFER_POSITION_IS_VIRTUAL ))
  419.       prev_char()
  420.  
  421.    vert_command = current_command
  422. }
  423.  
  424. ## keypad_motion()
  425. #
  426. # perform default cursor motion actions given the keycode for a keypad key
  427. #
  428. global function keypad_motion( keyCode )
  429. {
  430.    if (keyCode == KEYCODE_UP                 || keyCode == KEYCODE_NUM_UP)
  431.       current_line--
  432.    else if (keyCode == KEYCODE_NUM_DOWN      || keyCode == KEYCODE_DOWN)
  433.       current_line++
  434.    else if (keyCode == KEYCODE_NUM_LEFT      || keyCode == KEYCODE_LEFT)
  435.       current_column--
  436.    else if (keyCode == KEYCODE_NUM_RIGHT     || keyCode == KEYCODE_RIGHT)
  437.       current_column++
  438.    else if (keyCode == KEYCODE_NUM_HOME      || keyCode == KEYCODE_HOME)
  439.       goto_bol()
  440.    else if (keyCode == KEYCODE_NUM_END       || keyCode == KEYCODE_END)
  441.       goto_eol()
  442.    else if (keyCode == KEYCODE_NUM_PAGEUP    || keyCode == KEYCODE_PAGEUP)
  443.       page_up()
  444.    else if (keyCode == KEYCODE_NUM_PAGEDOWN  || keyCode == KEYCODE_PAGEDOWN)
  445.       page_down()
  446.    else if (keyCode == KEYCODE_CTRL_NUM_HOME || keyCode == KEYCODE_CTRL_NUM_HOME)
  447.       goto_window_top()
  448.    else if (keyCode == KEYCODE_CTRL_NUM_END  || keyCode == KEYCODE_CTRL_END)
  449.       goto_window_bottom()
  450.    else if (keyCode == KEYCODE_CENTER )
  451.       center_cursor()
  452. #  else if (keyCode == KEYCODE_LEFT_PRESS )
  453. #     menu_mouse_key()
  454. #  else if (keyCode == KEYCODE_RIGHT_PRESS )
  455. #     {} # do nothing
  456.    else
  457.       return 0
  458.  
  459.    return 1
  460. }
  461.