home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PASCAL / BELA.ZIP / EDIT.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1985-01-30  |  5.0 KB  |  206 lines

  1. { Written by Bela Lubkin
  2.              Borland International Technical Support
  3.              12/6/84
  4.  
  5.   This is a set of three routines that can be used in a Turbo Pascal program
  6.   to input data from the user.  Each routine provides WordStar-like editing of
  7.   the input, an undo function, and pre-setting of the input buffer.  I usually
  8.   pre-set the buffer to the old value of the variable.  Then the user can call
  9.   up the old value by simply typing ^R.
  10.  
  11.   Here is a list of the control characters used:
  12.  
  13.   ^A  Move to beginning of line, nondestructive
  14.   ^B  Save current buffer in undo buffer
  15.   ^D  Move forward one
  16.   ^F  Move to end of line (same as ^R)
  17.   ^G  Delete character forward
  18.   ^H  Move back 1, destructive (same as DEL)
  19.   ^M  End of input; accept what is currently visible
  20.   ^N  End of input; accept entire buffer
  21.   ^P  Accept next character as-is (control character prefix)
  22.   ^R  Move to end of line (same as ^F)
  23.   ^S  Move back 1, nondestructive
  24.   ^T  Delete line forward
  25.   ^U  Copy undo buffer into current buffer (undo)
  26.   ^V  Insert on/off
  27.   ^X  Move to beginning of line, destructive
  28.   ^Y  Delete line
  29.   DEL Move back 1, destructive (same as ^H)
  30.  
  31.   The initial contents of both the current buffer and the undo buffer are set
  32.   by the parameter Param.
  33.  
  34.   These routines will work on any version of Turbo Pascal.
  35. }
  36.  
  37.   Type
  38.     Buffer=String[255];
  39.  
  40.  
  41.   Function AskString(Prompt,Param: Buffer): Buffer;
  42.  
  43.     Var
  44.       AS: Buffer;
  45.       Cursor: Integer;
  46.  
  47.  
  48.     Procedure PutC;
  49.       Var
  50.         C: Char;
  51.  
  52.       Begin
  53.         C:=AS[Cursor];
  54.         If C<' ' Then Write('^',Chr(Ord(C)+64))
  55.         Else Write(C);
  56.       End;
  57.  
  58.  
  59.     Procedure UnPutC;
  60.       Var
  61.         C: Char;
  62.  
  63.       Begin
  64.         C:=AS[Cursor];
  65.         Write(#8' '#8);
  66.         If C<' ' Then Write(#8' '#8);
  67.       End;
  68.  
  69.  
  70.     Const
  71.       InsertFlag: Boolean=True;
  72.  
  73.     Var
  74.       Ch: Char;
  75.       WasChar: Boolean;
  76.  
  77.     Begin
  78.       Write(Prompt);
  79.       AS:=Param;
  80.       Cursor:=0;
  81.       Repeat
  82.         Read(Kbd,Ch);
  83.         WasChar:=False;
  84.         Case Ch Of
  85.           ^A,^U,^X,^Y: Begin
  86.                          While Cursor>0 Do
  87.                           Begin
  88.                            UnPutC;
  89.                            If Ch=^X Then Delete(AS,Cursor,1);
  90.                            Cursor:=Cursor-1;
  91.                           End;
  92.                          If Ch=^U Then AS:=Param
  93.                          Else If Ch=^Y Then AS:='';
  94.                        End;
  95.           ^B: Param:=AS;
  96.           ^D: If Length(AS)>Cursor Then
  97.                Begin
  98.                 Cursor:=Cursor+1;
  99.                 PutC;
  100.                End;
  101.           ^F,^R,^N: While Length(AS)>Cursor Do
  102.                      Begin
  103.                       Cursor:=Cursor+1;
  104.                       PutC;
  105.                      End;
  106.           ^G: Delete(AS,Cursor+1,1);
  107.           ^H,^S,#127: If Cursor>0 Then
  108.                        Begin
  109.                         UnPutC;
  110.                         If Ch<>^S Then Delete(AS,Cursor,1);
  111.                         Cursor:=Cursor-1;
  112.                        End;
  113.           ^M:;
  114.           ^P: Begin
  115.                 Read(Kbd,Ch);
  116.                 WasChar:=True;
  117.               End;
  118.           ^T: Delete(AS,Cursor+1,Length(AS));
  119.           ^V: InsertFlag:=Not InsertFlag;
  120.           Else WasChar:=True;
  121.          End;
  122.         If WasChar And (Length(AS)<255) Then
  123.          Begin
  124.           Cursor:=Cursor+1;
  125.           If InsertFlag Then Insert(Ch,AS,Cursor)
  126.           Else AS[Cursor]:=Ch;
  127.           If Cursor>Length(AS) Then AS[0]:=Chr(Cursor);
  128.           PutC;
  129.          End
  130.         Else If WasChar Then Write(^G);
  131.        Until ((Ch=^M) Or (Ch=^N)) And Not WasChar;
  132.       AskString:=Copy(AS,1,Cursor);
  133.     End;
  134.  
  135.  
  136.   Function AskInt(Prompt: Buffer; Param: Integer): Integer;
  137.  
  138.     Var
  139.       Temp: Buffer;
  140.       P,I: Integer;
  141.  
  142.     Begin
  143.       Str(Param,Temp);
  144.       Temp:=AskString(Prompt,Temp);
  145.       Val(Temp,P,I);
  146.       If I=0 Then AskInt:=P
  147.       Else If Length(Temp)=0 Then AskInt:=0
  148.       Else AskInt:=Param;
  149.     End;
  150.  
  151.  
  152.   Function AskReal(Prompt: Buffer; Param: Real): Real;
  153.  
  154.     Var
  155.       Temp: Buffer;
  156.       P: Real;
  157.       I: Integer;
  158.  
  159.     Begin
  160.       Str(Param:1:12,Temp);
  161.       I:=14;
  162.       While Temp[I]='0' Do I:=I-1;
  163.       Temp:=AskString(Prompt,Copy(Temp,1,I));
  164.       Val(Temp,P,I);
  165.       If I=0 Then AskReal:=P
  166.       Else If Length(Temp)=0 Then AskReal:=0.0
  167.       Else AskReal:=Param;
  168.     End;
  169.  
  170.  
  171. { A program to test the routines... remove the next line to enable. }
  172. (*
  173.  
  174.   Var
  175.     X: String[40];
  176.     Y: Integer;
  177.     Z: Real;
  178.  
  179. Begin
  180.   X:='This is a test.';
  181.   Repeat
  182.     X:=AskString('Edit the buffer: ',X);
  183.     WriteLn;
  184.     WriteLn(X);
  185.   Until X='';
  186.   Y:=100;
  187.   Repeat
  188.     Y:=AskInt('Edit the integer: ',Y);
  189.     WriteLn;
  190.     WriteLn(Y);
  191.   Until Y=0;
  192.   Z:=Pi;
  193.   Repeat
  194.     Z:=AskReal('Edit the real: ',Z);
  195.     WriteLn;
  196.     WriteLn(Z:1:11);
  197.   Until Z=0.0;
  198. End.
  199. (**)
  200. 
  201. 
  202.   Z:=Pi;
  203.   Repeat
  204.     Z:=AskReal('Edit the real: ',Z);
  205.     WriteLn;
  206.     WriteLn