home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / epmmac.zip / ASSIST.E < prev    next >
Text File  |  1996-02-29  |  11KB  |  245 lines

  1. /*****************************************************************************/
  2. /*  Assist interface for E3      Ralph Yozzo, Larry Margolis                 */
  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. /*                                                                           */
  38. /* Updated by LAM to use EGREP to also handle #if, #endif, etc. and COMPILE  */
  39. /* IF, COMPILE ENDIF, etc.                                                   */
  40. /*                                                                           */
  41. /* 1995/02/21  Updated by LAM to also handle SCRIPT list tags and /* */.     */
  42. /* 1995/02/22  Updated by LAM to also handle SGML tags.                      */
  43. /*****************************************************************************/
  44.  
  45. compile if not defined(SMALL)  -- If SMALL not defined, then being separately compiled.
  46.  define INCLUDING_FILE = 'ASSIST.E'
  47. const
  48.    tryinclude 'MYCNF.E'        -- the user's configuration customizations.
  49.  
  50.  compile if not defined(SITE_CONFIG)
  51.     const SITE_CONFIG = 'SITECNF.E'
  52.  compile endif
  53.  compile if SITE_CONFIG
  54.     tryinclude SITE_CONFIG
  55.  compile endif
  56.  
  57. const
  58.  compile if not defined(NLS_LANGUAGE)
  59.    NLS_LANGUAGE = 'ENGLISH'
  60.  compile endif
  61. include NLS_LANGUAGE'.e'
  62.  
  63.  compile if EVERSION >= 6
  64.    EA_comment 'Linkable bracket-matching routines.'
  65.  compile endif
  66. compile endif  -- not defined(SMALL)
  67.  
  68. const GOLD = '(){}[]<>'  -- Parens, braces, brackets & angle brackets.
  69. compile if EPM & not defined(LOCATE_CIRCLE_STYLE)
  70.    LOCATE_CIRCLE_STYLE = 1
  71. compile endif
  72. compile if EVERSION >= '5.60'
  73.  compile if not defined(LOCATE_CIRCLE_COLOR1)
  74.    LOCATE_CIRCLE_COLOR1 = 16777220
  75.  compile endif
  76.  compile if not defined(LOCATE_CIRCLE_COLOR2)
  77.    LOCATE_CIRCLE_COLOR2 = 16777218
  78.  compile endif
  79. compile endif
  80.  
  81. def c_leftbracket, c_rightbracket = call passist()
  82.  
  83. defproc passist
  84. compile if EVERSION >= '5.50'
  85.    call psave_pos(savepos)
  86.    case = 'e'
  87.    id = ''
  88.    force_search = 0
  89. compile endif
  90.    n=1
  91.    c=substr(textline(.line),.col,1)
  92.    if c==' ' & .col > 1 then
  93.       left
  94.       c=substr(textline(.line),.col,1)
  95.    endif
  96.    GETSEARCH search_command -- Save user's search command.
  97.    k=pos(c,GOLD)            --  '(){}[]<>'
  98.    if k then
  99.       search = substr(GOLD,(k+1)%2*2-1,2)
  100. compile if EVERSION >= '5.60'
  101.       incr = 0
  102. compile endif
  103.    else
  104. compile if EVERSION >= '5.60'
  105.                      -- Add '.' to default token_separators & remove ':' for GML markup.
  106.       if pos(c, '*/') then
  107.          seps = '/*'
  108.       else
  109.          seps = ' ~`!.%^&*()-+=][{}|\;?<>,''"'\t
  110.       endif
  111.       if find_token(startcol, endcol,  seps, '/* */') then
  112.          getline line
  113.          if startcol>1 then
  114.             if substr(line, startcol-1, 1)='<' then
  115.                startcol = startcol - 1
  116.             endif
  117.          endif
  118.          id = substr(line, startcol, (endcol-startcol)+1)
  119.          if id='.' & .col > 1 then
  120.             left
  121.             if find_token(startcol, endcol) then
  122.                id = substr(line, startcol, (endcol-startcol)+1)
  123.             endif
  124.          endif
  125.       endif
  126.       if wordpos(id, '#if #ifdef #ifndef #endif #else #elif') then
  127.          search = '\#(if((n?def)|):w|endif)'
  128.          c = substr(id, 2, 1)
  129.          k = (c<>'e')
  130.          if k then  -- move to beginning
  131.             .col = startcol
  132.          else       -- move to end, so first Locate will hit this instance.
  133.             .col = endcol
  134.          endif
  135.          incr = 1
  136.          force_search = substr(id, 3, 1)='l'
  137.       elseif lowcase(id)='compile' &
  138.              wordpos(lowcase(word(substr(line, endcol+1), 1)), 'if endif else elseif') then
  139.          search = 'compile:w(end|)if'
  140.          case = 'c'  -- Case insensitive
  141.          c = lowcase(leftstr(word(substr(line, endcol+1), 1), 1))
  142.          k = (c<>'e')
  143.          if k then  -- move to beginning
  144.             .col = startcol
  145.          else       -- move to end, so first Locate will hit this instance.
  146.             end_line
  147.          endif
  148.          id=lowcase(id)
  149.          incr = 0
  150.          force_search = lowcase(substr(word(substr(line, endcol+1), 1), 2, 1))='l'
  151.       elseif wordpos(lowcase(id), ':ol :eol :ul :eul :sl :esl :dl :edl :parml :eparml') &
  152.              pos(substr(line, endcol+1, 1), '. ') then
  153.          c = substr(id, 2, 1)  -- Character to check to see if it's an end tag
  154.          k = (c<>'e')          -- k = 1 if searching forward; 0 if backwards
  155.          if k then  -- move to beginning
  156.             .col = startcol
  157.             id = substr(id, 2)
  158.          else       -- move to end, so first Locate will hit this instance.
  159.             .col = endcol+1
  160.             id = substr(id, 3)
  161.          endif
  162.          search = '\:e?'id'(\.| )'
  163.          incr = 1              -- offset from match of the char. to compare with 'c' value
  164.          force_search = 0      -- force a search if on an intermediate (like #else).
  165.       elseif wordpos(id, '/* */') then
  166.          c = leftstr(id, 1)    -- Character to check to see if it's the same or the other
  167.          k = (c='/')           -- k = 1 if searching forward; 0 if backwards
  168.          if k then  -- move to beginning
  169.             .col = startcol
  170.          else       -- move to end, so first Locate will hit this instance.
  171.             .col = endcol
  172.          endif
  173.          search = '/\*|\*/'
  174.          incr = 0              -- offset from match of the char. to compare with 'c' value
  175.          force_search = 0      -- force a search if on an intermediate (like #else).
  176.       elseif leftstr(id, 1)='<' then
  177.          c = substr(id, 2, 1)  -- Character to check to see if it's the same or the other
  178.          k = (c<>'/')           -- k = 1 if searching forward; 0 if backwards
  179.          if k then  -- move to beginning
  180.             id = substr(id, 2)  -- Strip off the '<'
  181.             .col = startcol
  182.          else       -- move to end, so first Locate will hit this instance.
  183.             id = substr(id, 3)  -- Strip off the '</'
  184.             .col = endcol+1   -- +1 for the '>' after the tag
  185.          endif
  186. ;        sayerror 'k='k'; c="'c'"; id="'id'"'
  187.          search = '</?\c'id'(>| )'  -- Use \c to not put cursor on angle bracket.
  188.          incr = -1             -- offset from match of the char. to compare with 'c' value
  189.          force_search = 0      -- force a search if on an intermediate (like #else).
  190.       else
  191. compile endif
  192.          sayerror NOT_BALANCEABLE__MSG
  193.          return
  194. compile if EVERSION >= '5.60'
  195.       endif
  196. compile endif
  197.    endif
  198.    if k//2 then direction='+F'; else direction='-R'; endif
  199.    if search='[]' then search='\[\]'; endif
  200. compile if EVERSION >= '5.60'
  201. ;  if search='()' then search='\(\)'; endif  -- Don't need to escape it if inside brackets...
  202.    if id='' then search='['search']'; endif
  203.    if force_search then
  204.       setsearch 'xcom l /'search'/x'case||direction
  205.  compile if defined(HIGHLIGHT_COLOR)
  206.       circleit LOCATE_CIRCLE_STYLE, .line, startcol, endcol, LOCATE_CIRCLE_COLOR1, LOCATE_CIRCLE_COLOR2
  207.  compile endif
  208.    else
  209.       'L '\1 || search\1'x'case||direction
  210.    endif
  211. compile else
  212.    'L /['search']/eg'direction
  213. compile endif
  214.    loop
  215.       repeatfind
  216.       if rc then leave; endif
  217. compile if EVERSION >= '5.60'
  218.       if id='compile' then
  219.          tab_word
  220.          if lowcase(substr(textline(.line), .col+incr, 1)) = c then n=n+1; else n=n-1; endif
  221.          backtab_word
  222.       else
  223.          if substr(textline(.line), .col+incr, 1) = c then n=n+1; else n=n-1; endif
  224.       endif
  225. compile else
  226.       if substr(textline(.line), .col, 1) = c then n=n+1; else n=n-1; endif
  227. compile endif
  228.       if n=0 then leave; endif
  229.    endloop
  230.    SETSEARCH search_command -- Restores user's command so Ctrl-F works.
  231.    if rc=sayerror('String not found') then
  232.       sayerror UNBALANCED_TOKEN__MSG
  233.       call prestore_pos(savepos)
  234.       return
  235.    else
  236.       sayerror 1
  237.    endif
  238. compile if EVERSION >= '5.50'
  239.    newline = .line; newcol = .col
  240.    call prestore_pos(savepos)
  241.    .col = newcol
  242.    .lineg = newline
  243.    right; left        -- scroll_to_cursor
  244. compile endif
  245.