home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / EPMATR.ZIP / CBLOCK.E < prev    next >
Text File  |  1989-07-06  |  10KB  |  312 lines

  1.  
  2.  
  3. defproc FindEndOfCBlock(var EndOfBlockLine, var EndOfBlockCol)
  4. /* This procedure begins looking for the '}' character after the present
  5.    cursor position. It will skip it if it finds it within comments or
  6.    quotation marks.
  7.    Assert: The cursor is presently on the { character.
  8. */
  9.  
  10.    StartLine  = .line;   StartCol   = .col+1;
  11.    BestSoFar = "none";
  12.  
  13.    loop
  14.       .line  = StartLine;   .col = StartCol;
  15.       "l /}/+f"
  16.       if RC then
  17.         call showmessage(" No Action: This block is not terminated!!!");
  18.         return 1; /* zero indicates success */
  19.       else
  20.         FirstTerminatorLine = .line;
  21.         FirstTerminatorCol  = .col;
  22.       endif
  23.  
  24.       .line  = StartLine;   .col = StartCol;
  25.       "l %/*%+f"
  26.       if RC then
  27.         FirstCommentLine = .last+1;
  28.       else
  29.         FirstCommentLine   = .line;
  30.         FirstCommentCol    = .col;
  31.       endif
  32.  
  33.       .line  = StartLine;   .col = StartCol;
  34.       "l %'%+f"
  35.       if RC then
  36.         FirstCharLine   = .last+1;
  37.       else
  38.         FirstCharLine   = .line;
  39.         FirstCharCol    = .col;
  40.       endif
  41.  
  42.       .line  = StartLine;   .col = StartCol;
  43.       'l %"%+f'
  44.       if RC then
  45.         FirstStringLine = .last+1;
  46.       else
  47.         FirstStringLine   = .line;
  48.         FirstStringCol    = .col;
  49.       endif
  50.  
  51.       .line  = StartLine;   .col = StartCol;
  52.       'l %{%+f'
  53.       if RC then
  54.         FirstDeeperLine   = .last+1;
  55.       else
  56.         FirstDeeperLine   = .line;
  57.         FirstDeeperCol    = .col;
  58.       endif
  59.  
  60.       if (((FirstTerminatorLine<FirstCommentLine)) or
  61.           ((FirstTerminatorLine=FirstCommentLine)and
  62.            (FirstTerminatorCol<=FirstCommentCol))) then
  63.         if (((FirstTerminatorLine<FirstCharLine)) or
  64.             ((FirstTerminatorLine=FirstCharLine)and
  65.              (FirstTerminatorCol<=FirstCharCol))) then
  66.           if (((FirstTerminatorLine<FirstStringLine)) or
  67.               ((FirstTerminatorLine=FirstStringLine)and
  68.                (FirstTerminatorCol<=FirstStringCol))) then
  69.             if (((FirstTerminatorLine<FirstDeeperLine)) or
  70.                 ((FirstTerminatorLine=FirstDeeperLine)and
  71.                  (FirstTerminatorCol<=FirstDeeperCol))) then
  72.               BestSoFar = "TheTerminator";
  73.             else
  74.               BestSoFar = "TheDeeper";
  75.             endif
  76.           else
  77.             BestSoFar = "TheString";
  78.           endif
  79.         else
  80.           BestSoFar = "TheChar";
  81.         endif
  82.       else
  83.         if (((FirstCommentLine<FirstCharLine)) or
  84.             ((FirstCommentLine=FirstCharLine)and
  85.              (FirstCommentCol<=FirstCharCol))) then
  86.           if (((FirstCommentLine<FirstStringLine)) or
  87.               ((FirstCommentLine=FirstStringLine)and
  88.                (FirstCommentCol<=FirstStringCol))) then
  89.             if (((FirstCommentLine<FirstDeeperLine)) or
  90.                 ((FirstCommentLine=FirstDeeperLine)and
  91.                  (FirstCommentCol<=FirstDeeperCol))) then
  92.               BestSoFar = "TheComment";
  93.             else
  94.               BestSoFar = "TheDeeper";
  95.             endif
  96.           else
  97.             BestSoFar = "TheString";
  98.           endif
  99.         else
  100.           BestSoFar = "TheChar";
  101.         endif
  102.       endif
  103.       if BestSoFar=="TheChar" then
  104.         if (((FirstCharLine<FirstStringLine)) or
  105.             ((FirstCharLine=FirstStringLine)and
  106.              (FirstCharCol<=FirstStringCol))) then
  107.           if (((FirstCharLine<FirstDeeperLine)) or
  108.               ((FirstCharLine=FirstDeeperLine)and
  109.                (FirstCharCol<=FirstDeeperCol))) then
  110.             BestSoFar = "TheChar";
  111.           else
  112.             BestSoFar = "TheDeeper";
  113.           endif
  114.         else
  115.           BestSoFar = "TheString";
  116.         endif
  117.       endif
  118.       if BestSoFar=="TheString" then
  119.         if (((FirstStringLine<FirstDeeperLine)) or
  120.             ((FirstStringLine=FirstDeeperLine)and
  121.              (FirstStringCol<=FirstDeeperCol))) then
  122.           BestSoFar = "TheString";
  123.         else
  124.           BestSoFar = "TheDeeper";
  125.         endif
  126.       endif
  127.       if BestSoFar=="TheTerminator" then
  128.         /* Mission accomplished. */
  129.         EndOfBlockLine  = FirstTerminatorLine;
  130.         EndOfBlockCol   = FirstTerminatorCol;
  131.         return;
  132.       else
  133.         if BestSoFar=="TheComment" then
  134.           .line  = FirstCommentLine;
  135.           .col   = FirstCommentCol;
  136.           result = FindEndOfCComment(StartLine, StartCol);
  137.           if result then
  138.             return 1;
  139.           endif
  140.           StartCol = StartCol+2;    /* jump the comment terminator */
  141.         else
  142.           if BestSoFar=="TheDeeper" then
  143.             .line  = FirstDeeperLine;
  144.             .col   = FirstDeeperCol;
  145.             result = FindEndOfCBlock(StartLine, StartCol);
  146.             if result then
  147.               return 1;
  148.             endif
  149.             StartCol = StartCol+1;  /* jump the } character */
  150.           else
  151.             if BestSoFar=="TheString" then
  152.               .line  = FirstStringLine;
  153.               .col   = FirstStringCol;
  154.               result = FindEndOfCString(StartLine, StartCol);
  155.               if result then
  156.                 return 1;
  157.               endif
  158.               StartCol = StartCol+1; /* jump the " character */
  159.             else
  160.               .line  = FirstCharLine;
  161.               .col   = FirstCharCol;
  162.               result = FindEndOfCChar(StartLine, StartCol);
  163.               if result then
  164.                 return 1;
  165.               endif
  166.               StartCol = StartCol+1; /* jump the ' character */
  167.             endif
  168.           endif
  169.         endif
  170.       endif
  171.    endloop
  172.  
  173.  
  174. defproc FindEndOfCComment(var EndOfCommentLine, var EndOfCommentCol)
  175. /* This procedure begins looking for a * followed by a / character after the
  176.    present cursor position.
  177. */
  178.    right; right;
  179.    "l %*/%+f"
  180.    if RC then
  181.      call showmessage(" No Action: Unterminated Comment at line" .line);
  182.      return 1;
  183.    else
  184.      EndOfCommentLine   = .line;
  185.      EndOfCommentCol    = .col;
  186.    endif
  187.  
  188.  
  189. defproc FindEndOfCString(var EndOfStringLine, var EndOfStringCol)
  190. /* This procedure begins looking for the final " in a C string that begins at
  191.    the present cursor position.
  192. */
  193.    StartLine = .line; StartCol = .col;
  194.    Done = "false";
  195.    while Done=="false" do
  196.      right;
  197.      'l /"/+f'
  198.      if RC then
  199.        call showmessage(" No Action: String Unterminated at line" StartLine);
  200.        return 1;
  201.      else
  202.        EndOfStringLine    = .line;
  203.        EndOfStringCol     = .col;
  204.      endif
  205.      getline ALine;
  206.      Done = "true";
  207.      while (.col>1) and (substr(ALine,.col-1,1)=='\\')  do
  208.        left;
  209.        if Done=="false" then
  210.          Done="true"
  211.        else
  212.          Done="false"
  213.        endif
  214.      endwhile
  215.      .col = EndOfStringCol;
  216.    endwhile
  217.  
  218.  
  219. defproc FindEndOfCChar(var EndOfCharLine, var EndOfCharCol)
  220. /* This procedure begins looking for the end of a C character literal. The
  221.    search begins at the present cursor position which is assumed to be on the
  222.    first ' of the character literal.
  223. */
  224.    getline ALine;
  225.    if substr(ALine, .col+1, 1)== '\\' then
  226.      EndOfCharCol    = .col+3;
  227.    else
  228.      EndOfCharCol    = .col+2;
  229.    endif
  230.    if substr(ALine, EndOfCharCol, 1)/=="'" then
  231.      call showmessage("No Action: Unterminated character literal @ line" .line);
  232.      return 1;
  233.    endif
  234.    EndOfCharLine   = .line;
  235.  
  236.  
  237. -------------------------------------------------------------------------------
  238. defc blkexpansion_Compress_Blk
  239.   universal COLORCLASS;
  240.   universal HIDNCLASS;
  241. /*
  242. The following procedure returns an error message if the cursor is not on
  243. a { character. If it is on a { character, it stores the content of that
  244. block in a seperate file in a manner in which it can be restored. A
  245. flashing asterix appears on the screen where the block previously was.
  246. */
  247.   "MH_gotoposition"
  248.   oldlinenum  = .line;
  249.   oldcolnum   = .col;
  250.   psave_pos(old_position);
  251.   getline topline, .line;
  252.   if substr(topline, .col, 1) == '{' then
  253.      result = FindEndOfCBlock(EndOfBlockLine, EndOfBlockCol);
  254.      if result then
  255.        /* There was an error. I assume the user was already informed. */
  256.        prestore_pos(old_position);
  257.        return;
  258.      endif
  259.      TheTag = HideCharRegion(oldlinenum, EndOfBlockLine, oldcolnum+1, EndOfBlockCol-1)
  260.      OldInsertState = InsertState();
  261.      if insertstate()=='0' then
  262.        inserttoggle;
  263.      endif
  264.      /* should delete unneeded tag here, but will do it after demo. */
  265.      keyin '*'
  266.      .col = .col-1;
  267.      insert_attribute COLORCLASS, 165,    1, -1, .col, .line;
  268.      insert_attribute COLORCLASS, 165,    0,  1, .col, .line;
  269.      insert_attribute HIDNCLASS,  TheTag, 2, -1, .col, .line;
  270.   else
  271.      call showmessage("No Action: Cursor was not on a block initiator '{'");
  272.   endif
  273.   prestore_pos(old_position);
  274.   sayerror 0;
  275.  
  276.  
  277.  
  278. ------------------------------------------------------------------------------
  279. defc blkexpansion_Expand_Blk;
  280. /*
  281. The following procedure returns an error message if the cursor is not on
  282. a { character. If it is on a { character, but it is not expandable, it also
  283. returns an error message. If the block is expandable, the expansion text will
  284. be found in a separate file and replace the tagged block that the cursor was
  285. on when this function was called.
  286. */
  287.   "MH_gotoposition"
  288.   oldlinenum  = .line;
  289.   oldcolnum   = .col;
  290.   call psave_pos(old_position);
  291.   .col = .col-1;
  292.   "l /{*}/"
  293.   if (.line==oldlinenum) and (.col==oldcolnum-1) and (RC==0) then
  294.                                /* assert: There is a tag here, but we are not
  295.                                           certain that we can resolve it.     */
  296.      query_attribute  TheClass, TheValue, IsPush, -1, oldcolnum, .line;
  297.      .col = oldcolnum; deletechar;
  298.      /* some stuff */
  299.      /**************/
  300.      getfileid TheFile;
  301.      call ExpandCharTag(-300, .col, .line, TheFile, TheValue);
  302.   else
  303.      call showmessage("No Action: Cursor was not on a tagged block initiator.");
  304.   endif
  305.   sayerror 0;
  306.  
  307.  
  308. definit
  309.   "link HIDEIT" ;
  310.   sayerror 0;     -- refresh the screen to hide any messages.
  311.   "enable_attr_keys"
  312.