home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload / ShartewareOverload.cdr / progm / tptools.zip / FIRSTED.ZIP / EDTEXT.PAS < prev    next >
Pascal/Delphi Source File  |  1987-12-21  |  7KB  |  236 lines

  1. {                          EDTEXT.PAS
  2.                              ED 4.0
  3.              Copyright (c) 1985, 87 by Borland International, Inc.            }
  4.  
  5. {$I eddirect.inc}
  6.  
  7. unit EdText;
  8.   {-Text processing commands}
  9.  
  10. interface
  11.  
  12. uses
  13.   crt,                       {Basic video operations - standard unit}
  14.   Dos,                       {DOS calls - standard unit}
  15.   Errors,                    {Runtime error handler}
  16.   EdVars,                    {Global types and declarations}
  17.   EdScrn1,                   {Fast screen writing routines}
  18.   EdString,                  {String primitives}
  19.   EdPtrOp,                   {Primitive pointer operations}
  20.   EdCmds,                    {Maps keystrokes to commands}
  21.   int24,                     {DOS critical error handler}
  22.   Message,                   {Message system}
  23.   EdUser,                    {User keyboard input, line editing and error reporting}
  24.   EdMemOp,                   {Text buffer allocation and deallocation}
  25.   EdBack;                    {Background processes}
  26.  
  27. procedure EdPromptGotoCol;
  28.   {-Process goto col #n command}
  29.  
  30. procedure EdPromptGotoLine;
  31.   {-Process goto line #n command}
  32.  
  33. procedure EdShowMemory;
  34.   {-Show free memory for text}
  35.  
  36. procedure EdSetMarker(M : Byte);
  37.   {-Process set text marker command}
  38.  
  39. procedure EdSetUndoLimit;
  40.   {-Prompt for and set the global undo limit}
  41.  
  42. procedure EdTab;
  43.   {-Process tab command}
  44.  
  45.   {==========================================================================}
  46.  
  47. implementation
  48.  
  49.  
  50.   {***}
  51.   procedure EdPromptGotoCol;
  52.     {-Process goto col #n command}
  53.   var
  54.     Cno : Integer;
  55.  
  56.   begin                      {EdPromptGotoCol}
  57.     Cno := EdGetnumber(EdGetMessage(311), CurWin^.ColNo);
  58.     if (Cno > 0) and (Cno <= MaxLineLength) then
  59.       EdGotoColumn(Cno);
  60.   end;                       {EdPromptGotoCol}
  61.  
  62.   {***}
  63.   procedure EdPromptGotoLine;
  64.     {-Process goto line #n command}
  65.   var
  66.     Lno : Word;
  67.  
  68.   begin                      {EdPromptGotoLine}
  69.     {Assure line count is up to date}
  70.     IntrFlag := NoInterr;
  71.     EdGenLineNo;
  72.     IntrFlag := Interr;
  73.     Lno := EdGetnumber(EdGetMessage(312), CurWin^.Clineno);
  74.     if Lno > 0 then
  75.       EdGotoLine(Lno);
  76.   end;                       {EdPromptGotoLine}
  77.  
  78.   procedure EdShowMemory;
  79.     {-Show free memory for text}
  80.   var
  81.     Ch : Char;
  82.  
  83.   begin                      {EdShowMemory}
  84.     EdDisplayPromptWindow(EdCalcMemory+'-'+EdGetMessage(305), 1, [#27], Ch);
  85.   end;                       {EdShowMemory}
  86.  
  87.   procedure EdSetMarker(M : Byte);
  88.     {-Process set text marker command}
  89.  
  90.   begin                      {EdSetMarker}
  91.     if not(M in [0..MaxMarker]) then begin
  92.       EdErrormsg(28);
  93.       Exit;
  94.     end;
  95.     with Marker[M] do begin
  96.       {Reset any previous version of mark}
  97.       if EdPtrNotNil(Line) then
  98.         EdClrFlag(Line, InMark);
  99.       {Set new mark}
  100.       with CurWin^ do begin
  101.         if (Line = CurLine) and (Col = ColNo) then
  102.           {Setting mark on current mark, clears mark}
  103.           EdSetPtrNil(Line)
  104.         else begin
  105.           Line := CurLine;
  106.           Col := ColNo;
  107.         end;
  108.       end;
  109.     end;
  110.     {Turn on mark display}
  111.     MarkHide := True;
  112.     EdToggleTextMarker;
  113.   end;                       {EdSetMarker}
  114.  
  115.   procedure EdSetUndoLimit;
  116.     {-Prompt for and set the global undo limit}
  117.   var
  118.     Empty : Boolean;
  119.  
  120.   begin                      {EdSetUndoLimit}
  121.     EdSetNumber(UndoLimit, 361, 0, MaxInt, Empty);
  122.     if Abortcmd or GotError or Empty then
  123.       Exit;
  124.     SaveUndoLimit := UndoLimit;
  125.   end;                       {EdSetUndoLimit}
  126.  
  127.   {***}
  128.   procedure EdTab;
  129.     {-Process tab command}
  130.  
  131.     {***}
  132.     procedure EdInsertTab(TabPos : Integer);
  133.       {-Move current Colno to Tabpos by inserting spaces}
  134.  
  135.     begin                    {EdInsertTab}
  136.       with CurWin^ do begin
  137.         {Insert the spaces when appropriate}
  138.         if InsertFlag and (ColNo <= EdTextLength(CurLine)) then
  139.           if not(EdInsertSpace(CurLine, ColNo, TabPos-ColNo)) then
  140.             Exit
  141.           else
  142.             Modified := True;
  143.         {Move cursor to tab position}
  144.         ColNo := TabPos;
  145.       end;
  146.     end;                     {EdInsertTab}
  147.  
  148.     procedure EdSmartTab;
  149.       {-Process tab command ala Turbo Pascal "smart tabs"}
  150.     var
  151.       P : PlineDesc;
  152.       T : Integer;
  153.  
  154.       function EdNextIndentCol(P : PlineDesc; Start : Integer) : Integer;
  155.         {-Return the column number where next tab past start is}
  156.       var
  157.         Next : Integer;
  158.         Len : Integer;
  159.  
  160.       begin                  {EdNextIndentCol}
  161.  
  162.         Len := EdTextLength(P);
  163.  
  164.         if Len = 0 then
  165.           Next := 0
  166.         else if (Start <= Len) then begin
  167.           Next := Start;
  168.           with P^ do begin
  169.             if (Txt^[Start] <> Blank) then
  170.               {Start is in a word - advance to next blank}
  171.               while (Next <= Len) and (Txt^[Next] <> Blank) do
  172.                 Inc(Next);
  173.  
  174.             {In white space - advance to next non-blank}
  175.             while (Next <= Len) and (Txt^[Next] = Blank) do
  176.               Inc(Next);
  177.           end;
  178.         end else
  179.           {Positioned beyond end of line, tab "wraps"}
  180.           Next := 1;
  181.  
  182.         EdNextIndentCol := Next;
  183.       end;                   {EdNextIndentCol}
  184.  
  185.       function EdFollowingIndent : Integer;
  186.         {-Return the indent of any following non-blank line}
  187.       var
  188.         P : PlineDesc;
  189.  
  190.       begin                  {EdFollowingIndent}
  191.         EdFollowingIndent := 0;
  192.         with CurWin^ do
  193.           if EdPtrNotNil(CurLine^.FwdLink) then begin
  194.             P := CurLine;
  195.             repeat
  196.               EdFwdPtr(P);
  197.             until EdPtrIsNil(P) or (EdTextLength(P) <> 0);
  198.             if EdPtrNotNil(P) then
  199.               EdFollowingIndent := EdNextIndentCol(P, 1);
  200.           end;
  201.       end;                   {EdFollowingIndent}
  202.  
  203.     begin                    {EdSmartTab}
  204.       with CurWin^ do begin
  205.  
  206.         {Check previous line}
  207.         P := CurLine^.Backlink;
  208.         if EdPtrNotNil(P) then
  209.           T := EdNextIndentCol(P, ColNo)
  210.         else
  211.           T := 0;
  212.  
  213.         if T = 0 then begin
  214.           {Previous line blank or non-existent}
  215.           {Scan following lines for a non empty one}
  216.           T := EdFollowingIndent;
  217.           if T > ColNo then
  218.             EdInsertTab(T);
  219.         end else if T > ColNo then
  220.           {Insert spaces at the current cursor}
  221.           EdInsertTab(T)
  222.         else if (ColNo > EdTextLength(CurLine)) then begin
  223.           {Tab cursor to following line}
  224.           T := EdFollowingIndent;
  225.           if T > ColNo then
  226.             EdInsertTab(T);
  227.         end;
  228.       end;
  229.     end;                     {EdSmartTab}
  230.  
  231.   begin                      {EdTab}
  232.     EdSmartTab;
  233.   end;                       {EdTab}
  234.  
  235. end.
  236.