home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional / OS2PRO194.ISO / os2 / wps / editor / epmtools / cstepm / fold.e < prev    next >
Encoding:
Text File  |  1993-03-27  |  4.5 KB  |  126 lines

  1. /* Fold.e, by Larry Margolis
  2.  
  3.   Code for folding of procedures.  Place the cursor on a brace or parenthesis,
  4.   and enter FOLD in the EPM command dialog.  The code between the character
  5.   the cursor is on and the matching bracket will be folded (actually, displayed
  6.   in a 1-point type).  Place the cursor inside the folded text and enter the
  7.   EXPAND command to restore it.
  8.  
  9.   Installation:  This can be included in your base .ex files by adding the line:
  10.      include 'fold.e'
  11.   to your MYSTUFF.E or MYEXTRA.E, or compiled as a separate .ex file and linked
  12.   when you want to use it.  If compiled separately, you can also enter the FOLD
  13.   command to fold text without linking fold.ex, and FOLD 0 or FOLD OFF to
  14.   expand folded text.  (That's what the DEFMAIN accomplishes.)
  15. */
  16.  
  17. compile if EVERSION < '5.50'
  18.    Error:  This only supports EPM 5.50 or above - mixed font support is required.
  19. compile endif
  20.  
  21. const GOLD = '(){}[]<>'  -- Parens, braces, brackets & angle brackets.
  22.  
  23. compile if not defined('SMALL')  -- being compiled as a stand-alone module
  24. defmain
  25.    uparg = upcase(arg(1))
  26.    if uparg=0 | uparg='OFF' then
  27.       'expand'
  28.    elseif uparg=1 | uparg='ON' | uparg='' then
  29.       'fold'
  30.    else
  31.       gold2='     '
  32.       do i=1 to length(GOLD)
  33.          gold2 = gold2 substr(GOLD, i, 1)
  34.       enddo
  35.       call winmessagebox('Fold', 'Place cursor on one of:'\n gold2\n'then enter FOLD to fold text.  Place cursor inside folded text and enter FOLD OFF to expand it.', 16384+48) -- MOVEABLE + MB_INFORMATION
  36.    endif
  37. compile endif
  38.  
  39. const
  40.    COLOR_CLASS = 1
  41.    BOOKMARK_CLASS = 13
  42.    STYLE_CLASS =  14
  43.    FONT_CLASS =  16
  44.  
  45.    FIND_NEXT_ATTR_SUBOP = 1
  46.    FIND_PREV_ATTR_SUBOP = 2
  47.    FIND_MATCH_ATTR_SUBOP = 3
  48.    DELETE_ATTR_SUBOP = 16
  49.  
  50. defc fold =
  51.    call psave_pos(savepos)
  52.    n=1
  53.    c=substr(textline(.line),.col,1)
  54.    GETSEARCH search_command -- Save user's search command.
  55.    k=pos(c,GOLD)            --  '(){}[]<>'
  56.    if not k then sayerror 'Not a balanceable character.'; return; endif
  57.    fstline=.line; fstcol=.col
  58.    search = substr(GOLD,(k+1)%2*2-1,2)
  59.    if k//2 then direction='+F'; else direction='-R'; endif
  60.    if search='[]' then search='\[\]'; endif
  61. compile if EVERSION >= '5.60'
  62.    if search='()' then search='\(\)'; endif
  63.    'xcom L /['search']/ex'direction     -- Use extended grep; much faster.
  64. compile else
  65.    'xcom L /['search']/eg'direction
  66. compile endif
  67.    loop
  68.       repeatfind
  69.       if rc then leave; endif
  70.       if substr(textline(.line), .col, 1) = c then n=n+1; else n=n-1; endif
  71.       if n=0 then leave; endif
  72.    endloop
  73.    if rc=sayerror('String not found') then
  74.       sayerror 'Unbalanced token.'
  75.    else
  76.       sayerror 1
  77.    endif
  78.    SETSEARCH search_command -- Restores user's command so Ctrl-F works.
  79.    lstline = .line; lstcol = .col
  80.    if not rc then
  81.       getfileid fid
  82.       fontid=registerfont('Courier', 1, 0)
  83.       call attribute_on(4)  -- Mixed fonts flag
  84.       if not (k//2) then  -- adjust the order
  85.          parse value fstline lstline fstcol lstcol with lstline fstline lstcol fstcol
  86.       endif
  87.       fstcol = fstcol + 1        -- Want to display the brace, paren, etc.
  88.       if leftstr(textline(lstline), lstcol-1) = '' then
  89.          lstcol = 0
  90.       else
  91.          lstcol = lstcol - 1
  92.       endif
  93. ;getfileid fld, 'fold.e'
  94. ;insertline '; k='k'; k//2='k//2'; not1='(not k//2)'; not2='(not (k//2))'; Insert_Attribute_Pair(16,' fontid',' fstline',' lstline',' fstcol',' lstcol',' fid')', fld.last+1, fld
  95.       oldmod = .modify
  96.       Insert_Attribute_Pair(FONT_CLASS, fontid, fstline, lstline, fstcol, lstcol, fid)
  97.       .modify = oldmod
  98.    endif
  99.    call prestore_pos(savepos)
  100.  
  101. defc expand =
  102.    fontid=registerfont('Courier', 1, 0)
  103.    class = 16  -- STYLE_CLASS
  104.    line=.line; col=.col; offst=0
  105.    do forever
  106.       attribute_action FIND_PREV_ATTR_SUBOP, class, offst, col, line
  107.       if class=0 then   -- not found
  108.          sayerror 'Not found - cursor must be after start of folded text.'
  109.          return
  110.       endif
  111.       query_attribute class, val, IsPush, offst, col, line
  112.       if IsPush & val=fontid then leave; endif
  113.       offst = offst-1
  114.    enddo
  115.               -- OK, at this point we've got the beginning attribute.
  116.    fstline = line; fstcol = col; fstoff = offst
  117.    oldmod = .modify
  118.    attribute_action FIND_MATCH_ATTR_SUBOP, class, offst, col, line
  119.    if class then
  120.       attribute_action DELETE_ATTR_SUBOP, class, offst, col, line
  121.    endif
  122.    class = 16  -- STYLE_CLASS
  123.    attribute_action DELETE_ATTR_SUBOP, class, fstoff, fstcol, fstline
  124.    .modify = oldmod
  125.  
  126.