home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / mlepm.zip / matchkey.e < prev    next >
Text File  |  1995-09-15  |  5KB  |  101 lines

  1. /*****************************************************************************/
  2. /*  Assist interface for E3      Ralph Yozzo                                 */
  3. /*                                                                           */
  4. /*  This macro is intended for use with programming language                 */
  5. /*  which have tokens which must be balanced to compile correctly.           */
  6. /*  We shall call these tokens "balanceable tokens" or BalTok for            */
  7. /*  short.                                                                   */
  8. /*                                                                           */
  9. /*  The functions provided include moving from an opening token              */
  10. /*  (e.g., (, {, [ ) to a closing token (e.g., ), }, ] ) and vice versa.     */
  11. /*                                                                           */
  12. /*  KEYS:                                                                    */
  13. /*  Ctrl-[, Ctrl-]  -- move to corresponding BalTok                          */
  14. /*                                                                           */
  15. /*  CONSTANTS:                                                               */
  16. /*  gold -BalTok tokens  are defined in the const gold and additional        */
  17. /*        tokens may be added.                                               */
  18. /*                                                                           */
  19. /*  Example:                                                                 */
  20. /*     if ((c=getch())=='c'                                                  */
  21. /*      &&(d=complicatedisntit(e))){                                         */
  22. /*      lookforbracket();                                                    */
  23. /*     }                                                                     */
  24. /* In the above program segment if one places the cursor on an opening       */
  25. /* parenthesis and presses Ctrl-[ the cursor will move to the corresponding  */
  26. /* closing parenthesis if one exists.  Pressing Ctrl-[ again will reverse    */
  27. /* the process.                                                              */
  28. /*                                                                           */
  29. /* Modified by Larry Margolis to use the GREP option of Locate to search     */
  30. /* for either the opening or closing token, rather than checking a line at   */
  31. /* a time.  I also changed the key from Ctrl-A to Ctrl-[ or -], which are    */
  32. /* newly allowed as definable keys, and deleted the matching of /* and */.   */
  33. /* (The GREP search is much faster than the way Ralph did it, but doesn't    */
  34. /* let you match either of 2 strings.)  Finally, the user's previous search  */
  35. /* arguments are saved and restored, so Ctrl-F (repeatfind) will not be      */
  36. /* affected by this routine.                                                 */
  37. /* Modified by Martin Lafaix to mimic Emacs parent matching.  Opening tokens */
  38. /* insert the matching one, too.  Don't use it if you don't like it! :-)     */
  39. /*****************************************************************************/
  40.  
  41. CONST WANT_CUA_MARKING = 'SWITCH'
  42. const GOLD = '(){}[]<>'  -- Parens, braces, brackets & angle brackets.
  43.  
  44. def '('=
  45.    keyin '()'; left
  46.  
  47. def '['=
  48.    keyin '[]'; left
  49.  
  50. def '{'=
  51.    keyin '{}'; left
  52.  
  53. def ')'=
  54.    keyin ')'
  55.    call massist()
  56.  
  57. def '}'=
  58.    keyin '}'
  59.    call massist()
  60.  
  61. def ']'=
  62.    keyin ']'
  63.    call massist()
  64.  
  65. defproc massist
  66. compile if EVERSION >= '5.50'
  67.    call psave_pos(savepos)
  68. compile endif
  69.    n=1
  70.    c=substr(textline(.line),.col-1,1); left
  71.    GETSEARCH search_command -- Save user's search command.
  72.    k=pos(c,GOLD)            --  '(){}[]'
  73.    search = substr(GOLD,(k+1)%2*2-1,2)
  74.    if search='[]' then search='\[\]'; endif
  75. compile if EVERSION >= '5.60'
  76.    if search='()' then search='\(\)'; endif
  77.    'L /['search']/ex-R'
  78. compile else
  79.    'L /['search']/eg-R'
  80. compile endif
  81.    loop
  82.       repeatfind
  83.       if rc then leave; endif
  84.       if substr(textline(.line), .col, 1) = c then n=n+1; else n=n-1; endif
  85.       if n=0 then leave; endif
  86.    endloop
  87.    if rc=sayerror('String not found') then
  88.       sayerror 'Unbalanced token.'
  89.    else
  90. compile if EVERSION >= '5.60'
  91.    'L /['search']/ex+F'
  92. compile else
  93.    'L /['search']/eg+F'
  94. compile endif
  95.       sayerror 1
  96.    endif
  97.    SETSEARCH search_command -- Restores user's command so Ctrl-F works.
  98. compile if EVERSION >= '5.50'
  99.    call prestore_pos(savepos)
  100. compile endif
  101.