home *** CD-ROM | disk | FTP | other *** search
/ C!T ROM 2 / ctrom_ii_b.zip / ctrom_ii_b / PROGRAM / PASCAL / EDITWIN / EDLINE.PAS < prev    next >
Pascal/Delphi Source File  |  1992-07-03  |  9KB  |  297 lines

  1. {**************************************************************}
  2. {                                                              }
  3. {           Saved as: EDLINE.PAS                               }
  4. {           Language: Turbo Pascal 6                           }
  5. {             Author: Pat Anderson                             }
  6. {            Purpose: ReadLn replacement, some                 }
  7. {                     miscellaneous string manipulation        }
  8. {                     routines                                 }
  9. {      Last modified: Sat 06-20-92                             }
  10. {                                                              }
  11. {**************************************************************}
  12.  
  13. unit EdLine;
  14.  
  15. {--------------------------------------------------------------}
  16.                         INTERFACE
  17. {--------------------------------------------------------------}
  18.  
  19. uses
  20.   Crt,
  21.   Dos,
  22.   Minikit;
  23.  
  24. const
  25.   On   = TRUE;
  26.   Off  = FALSE;
  27.  
  28. var
  29.   SaveAttr : byte;
  30.   CursorPosition,
  31.   StartColumn : byte;
  32.  
  33. function EditLn (var line : string;
  34.                  color_attr : byte;
  35.                  var insert_flag : boolean;
  36.                  var cursor : byte;
  37.                  col,
  38.                  row,
  39.                  field_length : byte;
  40.                  StickAtEnds : boolean;
  41.                  DoWhileWaiting : proc) : char;
  42.  
  43. {function to input and/or edit a string, returns string
  44.  as edited, status of insert/overwrite flag and cursor position,
  45.  in var parameters, and key used to terminate input/editing as
  46.  function return value}
  47.  
  48. {--------------------------------------------------------------}
  49.                       IMPLEMENTATION
  50. {--------------------------------------------------------------}
  51. var
  52.   key : char;
  53.  
  54.  
  55. function EditLn;
  56.   var
  57.     ExitFlag : boolean;
  58.     key : char;
  59.  
  60.   {------------------------------------------------------------}
  61.   {  Nested service routines for EditLn procedure              }
  62.   {------------------------------------------------------------}
  63.  
  64.   procedure CursorRight;        {nested in Editline procedure}
  65.     begin
  66.       Inc (cursor);
  67.       if cursor > col + field_length - 1 then
  68.         if not StickAtEnds then
  69.           ExitFlag := true
  70.         else
  71.           Cursor := Col + Field_Length - 1;
  72.     end; {of CursorRight procedure}
  73.  
  74.   procedure CursorLeft;         {nested in EditLine procedure}
  75.     begin
  76.       Dec (cursor);
  77.       if cursor < col then
  78.         if not StickAtEnds then
  79.           ExitFlag := true
  80.         else
  81.           Cursor := Col;
  82.     end; {of CursorLeft procedure}
  83.  
  84.   procedure CursorFront;        {nested in EditLine procedure}
  85.     begin
  86.       cursor := col;
  87.     end; {of CursorFront procedure}
  88.  
  89.   procedure CursorEnd;          {nested in EditLine procedure}
  90.     var
  91.       position : byte;
  92.     begin
  93.       position := Length (line);
  94.       while line[position] = ' ' do
  95.         Dec (position);
  96.       cursor := col + position
  97.     end; {of CursorEnd procedure}
  98.  
  99.   procedure WordRight;          {nested in EditLine procedure}
  100.     var position : byte;
  101.     begin
  102.       position := cursor - col + 1;
  103.       while line[position] <> ' ' do
  104.         begin
  105.           Inc (position);
  106.           if position = field_length then
  107.             Exit;
  108.         end;
  109.       repeat
  110.         Inc (position);
  111.       until (line[position] <> ' ') OR (position = field_length);
  112.       cursor := col + position - 1;
  113.       if cursor = field_length then
  114.         CursorEnd;
  115.     end; {of WordRight procedure}
  116.  
  117.   procedure WordLeft;           {nested in Editline procedure}
  118.     var position : byte;
  119.     begin
  120.       position := cursor - col + 1;
  121.       while (line[position] <> ' ') AND (position >= 1) do
  122.         Dec (position);
  123.       while (line[position] = ' ') AND (position >= 1) do
  124.         Dec (position);
  125.       while (line[position] <> ' ') AND (position >= 1) do
  126.           Dec (position);
  127.       if position < 1 then
  128.         begin
  129.           cursor := col;
  130.           Exit;
  131.         end;
  132.       cursor := col + position - 1;
  133.       if cursor > col then Inc (cursor)
  134.     end; {of WordLeft procedure}
  135.  
  136.   procedure DoBackSpace;          {nested in EditLine procedure}
  137.     var
  138.       position : byte;
  139.     begin
  140.       if StickAtEnds then
  141.         if cursor = col then
  142.           Exit;
  143.       position := cursor - col + 1;
  144.       Delete (line, position - 1, 1);
  145.       line := line + ' ';
  146.       CursorLeft;
  147.     end; {of DoBackSpace procedure}
  148.  
  149.   procedure DeleteChar;         {nested in EditLine procedure}
  150.     var
  151.       position : byte;
  152.     begin
  153.       position := cursor - col + 1;
  154.       Delete (line, position, 1);
  155.       line := line + ' '
  156.     end; {of DeleteChar procedure}
  157.  
  158.   procedure DeleteWord;         {nested in EditLine procedure}
  159.     var
  160.       position : byte;
  161.     begin
  162.       position := cursor - col + 1;
  163.       repeat
  164.         DeleteChar
  165.       until (COPY(line, position, 1) = ' ');
  166.       DeleteChar
  167.     end; {of DeleteWord procedure}
  168.  
  169.   procedure DeleteEOL;          {nested in EditLine procedure}
  170.     var
  171.       count, position : byte;
  172.     begin
  173.       position := cursor - col + 1;
  174.       count := field_length - position + 1;
  175.       Delete (line, position, count);
  176.       line := Pad (line, field_length)
  177.     end; {of DeleteEOL procedure}
  178.  
  179.   procedure ToggleInsert;       {nested in EditLine procedure}
  180.     begin
  181.       insert_flag := NOT insert_flag;
  182.     end; {of ToggleInsert procedure}
  183.  
  184.   procedure InsertChar;         {nested in EditLine procedure}
  185.     var
  186.       character : string[1];
  187.       position : byte;
  188.     begin
  189.       position := cursor - col + 1;
  190.       (* Delete (line, field_length,1); *)
  191.       character := key;
  192.       Insert (character, line, position);
  193.       if line[field_length + 1] = ' ' then
  194.         Delete (line, field_length + 1, 1)
  195.       else
  196.         ExitFlag := true;
  197.       if Length (line) > field_length then
  198.         ExitFlag := true;
  199.       CursorRight
  200.     end; {of InsertChar procedure}
  201.  
  202.   procedure ReplaceChar;        {nested in EditLine procedure}
  203.     var
  204.       position : byte;
  205.     begin
  206.       position := cursor - col + 1;
  207.       line[position] := key;
  208.       CursorRight;
  209.     end; {of ReplaceChar procedure}
  210.  
  211.   procedure PositionCursor;     {nested in Editline procedure}
  212.     begin
  213.       Gotoxy (cursor, row);
  214.       if insert_flag then
  215.         BlockCursorOn
  216.       else
  217.         NormCursorOn;
  218.     end; {of PositionCursor procedure}
  219.  
  220.   procedure ExtendedCodes;      {nested in EditLine procedure}
  221.     begin
  222.       case key of
  223.         LeftArrow :  CursorLeft; {left arrow}
  224.         RightArrow:  CursorRight; {right arrow}
  225.         Home      :  CursorFront; {Home}
  226.         Del       :  DeleteChar; {Del}
  227.         End_Key   :  CursorEnd; {End}
  228.         Ins       :  ToggleInsert; {Ins}
  229.         CtlLeft   :  WordLeft; {Ctrl-left arrow}
  230.         CtlRight  :  WordRight; {Ctrl-right arrow}
  231.       else
  232.         ExitFlag  := TRUE;
  233.       end; {of case statement}
  234.     end; {of ExtendedCodes procedure}
  235.  
  236.   procedure ControlCodes;       {nested in EditLine procedure}
  237.     begin
  238.       case key of
  239.         BackSpace :  DoBackSpace;
  240.         ^E        :  DeleteEOL;
  241.         ^W, ^T    :  DeleteWord;
  242.         else {any other single control code}
  243.           ExitFlag := TRUE;
  244.       end; {of case statement}
  245.     end; {of ControlCodes procedure}
  246.  
  247.   procedure ActOnKeypress;            {nested in EditLine procedure}
  248.     begin
  249.       if key > #126 then
  250.         Extendedcodes
  251.       else
  252.         if key < #32 then
  253.           ControlCodes
  254.       else
  255.         if insert_flag = TRUE then
  256.           InsertChar
  257.         else ReplaceChar;
  258.     end; {of ActOnKeypress procedure}
  259.  
  260.   procedure DisplayLine;        {nested in EditLine procedure}
  261.     var
  262.       SaveAttr : byte;
  263.     begin
  264.       SaveAttr := TextAttr;
  265.       TextAttr := color_attr;
  266.       CursorOff;
  267.       GotoXY (col, row);
  268.       Write (line);
  269.       TextAttr := SaveAttr;
  270.     end; {of DisplayLine procedure}
  271.  
  272.   begin {MAIN of EditLine procedure}
  273.     ExitFlag := FALSE;
  274.     SaveAttr := TextAttr;
  275.     TextAttr := Edit_Attr;
  276.     StartColumn := Col;
  277.     if Length (line) > field_length then
  278.       line := Copy (line, 1, field_length);
  279.     line := Pad (line, field_length);
  280.     if Cursor = field_length then
  281.       CursorEnd;
  282.     repeat
  283.       begin
  284.         DisplayLine;
  285.         PositionCursor;
  286.         CursorPosition := Cursor;
  287.         key := GetKey (DoWhileWaiting);
  288.         ActOnKeypress;
  289.       end;
  290.     until ExitFlag;
  291.     EditLn := key;
  292.     line := Strip (line);
  293.     TextAttr := SaveAttr;
  294.   end; {of EditLine procedure}
  295.  
  296. end.
  297.