home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / epm603a.zip / EPMMAC.ZIP / ASSIST.E < prev    next >
Text File  |  1995-03-07  |  11KB  |  232 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.    GETSEARCH search_command -- Save user's search command.
  93.    k=pos(c,GOLD)            --  '(){}[]<>'
  94.    if k then
  95.       search = substr(GOLD,(k+1)%2*2-1,2)
  96. compile if EVERSION >= '5.60'
  97.       incr = 0
  98. compile endif
  99.    else
  100. compile if EVERSION >= '5.60'
  101.                      -- Add '.' to default token_separators & remove ':' for GML markup.
  102.                      -- Remove < and / for SGML markup
  103.       if find_token(startcol, endcol, ' ~`!.%^&*()-+=][{}|\;?>,''"'\t) then
  104.          getline line
  105.          id = substr(line, startcol, (endcol-startcol)+1)
  106.       endif
  107.       if id='.' & .col > 1 then
  108.          left
  109.          if find_token(startcol, endcol) then
  110.             id = substr(line, startcol, (endcol-startcol)+1)
  111.          endif
  112.       endif
  113.       if wordpos(id, '#if #ifdef #ifndef #endif #else #elif') then
  114.          search = '\#(if((n?def)|):w|endif)'
  115.          c = substr(id, 2, 1)
  116.          k = (c<>'e')
  117.          if k then  -- move to beginning
  118.             .col = startcol
  119.          else       -- move to end, so first Locate will hit this instance.
  120.             .col = endcol
  121.          endif
  122.          incr = 1
  123.          force_search = substr(id, 3, 1)='l'
  124.       elseif lowcase(id)='compile' &
  125.              wordpos(lowcase(word(substr(line, endcol+1), 1)), 'if endif else elseif') then
  126.          search = 'compile:w(end|)if'
  127.          case = 'c'  -- Case insensitive
  128.          c = lowcase(leftstr(word(substr(line, endcol+1), 1), 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.             end_line
  134.          endif
  135.          id=lowcase(id)
  136.          incr = 0
  137.          force_search = lowcase(substr(word(substr(line, endcol+1), 1), 2, 1))='l'
  138.       elseif wordpos(lowcase(id), ':ol :eol :ul :eul :sl :esl :dl :edl :parml :eparml') &
  139.              pos(substr(line, endcol+1, 1), '. ') then
  140.          c = substr(id, 2, 1)  -- Character to check to see if it's an end tag
  141.          k = (c<>'e')          -- k = 1 if searching forward; 0 if backwards
  142.          if k then  -- move to beginning
  143.             .col = startcol
  144.             id = substr(id, 2)
  145.          else       -- move to end, so first Locate will hit this instance.
  146.             .col = endcol+1
  147.             id = substr(id, 3)
  148.          endif
  149.          search = '\:e?'id'(\.| )'
  150.          incr = 1              -- offset from match of the char. to compare with 'c' value
  151.          force_search = 0      -- force a search if on an intermediate (like #else).
  152.       elseif wordpos(id, '/* */') then
  153.          c = leftstr(id, 1)    -- Character to check to see if it's the same or the other
  154.          k = (c='/')           -- k = 1 if searching forward; 0 if backwards
  155.          if k then  -- move to beginning
  156.             .col = startcol
  157.          else       -- move to end, so first Locate will hit this instance.
  158.             .col = endcol
  159.          endif
  160.          search = '/\*|\*/'
  161.          incr = 0              -- offset from match of the char. to compare with 'c' value
  162.          force_search = 0      -- force a search if on an intermediate (like #else).
  163.       elseif leftstr(id, 1)='<' then
  164.          c = substr(id, 2, 1)  -- Character to check to see if it's the same or the other
  165.          k = (c<>'/')           -- k = 1 if searching forward; 0 if backwards
  166.          if k then  -- move to beginning
  167.             id = substr(id, 2)  -- Strip off the '<'
  168.             .col = startcol
  169.          else       -- move to end, so first Locate will hit this instance.
  170.             id = substr(id, 3)  -- Strip off the '</'
  171.             .col = endcol+1   -- +1 for the '>' after the tag
  172.          endif
  173. ;        sayerror 'k='k'; c="'c'"; id="'id'"'
  174.          search = '<\c/?'id'(>| )'  -- Use /c to not put cursor on angle bracket.
  175.          incr = 0              -- offset from match of the char. to compare with 'c' value
  176.          force_search = 0      -- force a search if on an intermediate (like #else).
  177.       else
  178. compile endif
  179.          sayerror NOT_BALANCEABLE__MSG
  180.          return
  181. compile if EVERSION >= '5.60'
  182.       endif
  183. compile endif
  184.    endif
  185.    if k//2 then direction='+F'; else direction='-R'; endif
  186.    if search='[]' then search='\[\]'; endif
  187. compile if EVERSION >= '5.60'
  188. ;  if search='()' then search='\(\)'; endif  -- Don't need to escape it if inside brackets...
  189.    if id='' then search='['search']'; endif
  190.    if force_search then
  191.       setsearch 'xcom l /'search'/x'case||direction
  192.  compile if defined(HIGHLIGHT_COLOR)
  193.       circleit LOCATE_CIRCLE_STYLE, .line, startcol, endcol, LOCATE_CIRCLE_COLOR1, LOCATE_CIRCLE_COLOR2
  194.  compile endif
  195.    else
  196.       'L '\1 || search\1'x'case||direction
  197.    endif
  198. compile else
  199.    'L /['search']/eg'direction
  200. compile endif
  201.    loop
  202.       repeatfind
  203.       if rc then leave; endif
  204. compile if EVERSION >= '5.60'
  205.       if id='compile' then
  206.          tab_word
  207.          if lowcase(substr(textline(.line), .col+incr, 1)) = c then n=n+1; else n=n-1; endif
  208.          backtab_word
  209.       else
  210.          if substr(textline(.line), .col+incr, 1) = c then n=n+1; else n=n-1; endif
  211.       endif
  212. compile else
  213.       if substr(textline(.line), .col, 1) = c then n=n+1; else n=n-1; endif
  214. compile endif
  215.       if n=0 then leave; endif
  216.    endloop
  217.    SETSEARCH search_command -- Restores user's command so Ctrl-F works.
  218.    if rc=sayerror('String not found') then
  219.       sayerror UNBALANCED_TOKEN__MSG
  220.       call prestore_pos(savepos)
  221.       return
  222.    else
  223.       sayerror 1
  224.    endif
  225. compile if EVERSION >= '5.50'
  226.    newline = .line; newcol = .col
  227.    call prestore_pos(savepos)
  228.    .col = newcol
  229.    .lineg = newline
  230.    right; left        -- scroll_to_cursor
  231. compile endif
  232.