home *** CD-ROM | disk | FTP | other *** search
/ Jason Aller Floppy Collection / 189.img / TCS120S.ZIP / SCRNINPT.PAS < prev    next >
Pascal/Delphi Source File  |  1987-12-21  |  4KB  |  197 lines

  1. unit scrninpt;
  2.  
  3. { Makes it so READLN uses a nice little scrollable region }
  4. { The default input region is the cursor position to the end of the line }
  5.  
  6. {$R-,S-,I-,D-,V-,B-}
  7.  
  8. interface
  9.  
  10. uses dos,crt,
  11.      scrnunit;
  12.  
  13. var scrnin:text;         { For input }
  14.     buflen:integer;
  15.  
  16.  
  17. procedure setinputregion (left,right,line:integer);
  18. procedure setdefaultinput (x:string);
  19. procedure setinputcolor (attr:integer);
  20.  
  21. implementation
  22.  
  23. var scrninbuf:array [0..257] of char;
  24.     x1,x2,y:integer;
  25.     oldinput:string;
  26.  
  27. {$F+}
  28.  
  29. function donothing (var t:textrec):integer;
  30. begin
  31. {  t.bufend:=0;
  32.   t.bufpos:=0;  }
  33.   donothing:=0
  34. end;
  35.  
  36. function scrninchars (var t:textrec):integer;
  37. var s:string;
  38.     len:byte absolute s;
  39.     cx,lx,wid:integer;
  40.     k:char;
  41.     tracking:boolean;
  42.  
  43. const letters:set of char=['A'..'Z','a'..'z'];
  44.  
  45.   procedure drawit;
  46.   var cnt:integer;
  47.   begin
  48.     gotoxy (x1,y);
  49.     write (scrn,copy(s,lx,wid));
  50.     for cnt:=1 to wid-len+lx-1 do write (' ');
  51.     gotoxy (cx-lx+x1,y);
  52.     movecsr
  53.   end;
  54.  
  55.   procedure insert (k:char);
  56.   begin
  57.     if len>=buflen then exit;
  58.     s:=copy(s,1,cx-1)+k+copy(s,cx,255);
  59.     cx:=cx+1
  60.   end;
  61.  
  62.   procedure del;
  63.   begin
  64.     if cx<=len then s:=copy(s,1,cx-1)+copy(s,cx+1,255)
  65.   end;
  66.  
  67.   procedure backspace;
  68.   begin
  69.     if cx>1 then begin
  70.       cx:=cx-1;
  71.       del
  72.     end
  73.   end;
  74.  
  75.   procedure wordleft;
  76.   begin
  77.     if cx=1 then exit;
  78.     cx:=cx-1;
  79.     while (cx>1) and ((s[cx-1] in letters) or (not (s[cx] in letters))) do
  80.       cx:=cx-1
  81.   end;
  82.  
  83.   procedure wordright;
  84.   begin
  85.     if cx>len then exit;
  86.     cx:=cx+1;
  87.     while (cx<=len) and ((s[cx-1] in letters) or (not (s[cx] in letters))) do
  88.       cx:=cx+1;
  89.   end;
  90.  
  91.   procedure delword;
  92.   begin
  93.     while (cx<=len) and (s[cx] in letters) do del;
  94.     while (cx<=len) and (not (s[cx] in letters)) do del
  95.   end;
  96.  
  97.   procedure extended (key:integer);
  98.   begin
  99.     case key of
  100.       71,73:cx:=1;
  101.       75:cx:=cx-1;
  102.       77:cx:=cx+1;
  103.       79,81:cx:=len+1;
  104.       83:del;
  105.       115:wordleft;
  106.       116:wordright;
  107.       117:len:=cx-1;
  108.     end
  109.   end;
  110.  
  111.   procedure normal (k:char);
  112.   begin
  113.     case ord(k) of
  114.       32..126:if len<buflen then insert(k);
  115.       8:backspace;
  116.       27:len:=0;
  117.       127,20:delword
  118.     end
  119.   end;
  120.  
  121. begin
  122.   scrninchars:=0;
  123.   if t.bufend<>t.bufpos then exit;
  124.   pushdarea;
  125.   setcursortracking (false);
  126.   setcolor (curwindowptr^.inputcolor);
  127.   s:=oldinput;
  128.   if x1=0 then begin
  129.     x1:=wherex;
  130.     y:=wherey;
  131.     x2:=curwindowptr^.xsize
  132.   end;
  133.   lx:=1;
  134.   cx:=1;
  135.   wid:=x2-x1+1;
  136.   repeat
  137.     if cx<1 then cx:=1;
  138.     if cx>len then cx:=len+1;
  139.     if lx>cx-5 then lx:=cx-5;
  140.     if lx<cx-wid+5 then lx:=cx-wid+5;
  141.     if lx>len-wid+1 then lx:=len-wid+1;
  142.     if lx>cx then lx:=cx;
  143.     if lx<cx-wid then lx:=cx-wid;
  144.     if lx<1 then lx:=1;
  145.     if not keypressed then drawit;
  146.     k:=readkey;
  147.     if k=#0 then extended(ord(readkey)) else normal(k)
  148.   until k=#13;
  149.   drawit;
  150.   s:=s+#13#10;
  151.   move (s[1],t.bufptr^,length(s));
  152.   x1:=0;
  153.   buflen:=80;
  154.   oldinput:='';
  155.   t.bufpos:=0;
  156.   t.bufend:=len;
  157.   popdarea
  158. end;
  159.  
  160. {$F-}
  161.  
  162. procedure setinputregion (left,right,line:integer);
  163. begin
  164.   x1:=left;
  165.   x2:=right;
  166.   y:=line
  167. end;
  168.  
  169. procedure setdefaultinput (x:string);
  170. begin
  171.   oldinput:=x
  172. end;
  173.  
  174. procedure setinputcolor (attr:integer);
  175. begin
  176.   curwindowptr^.inputcolor:=attr
  177. end;
  178.  
  179. begin
  180.   x1:=0;          { Initialize input stuff }
  181.   buflen:=80;
  182.   oldinput:='';
  183.   with textrec(scrnin) do begin
  184.     mode:=fminput;
  185.     bufptr:=@scrninbuf;
  186.     bufsize:=258;
  187.     openfunc:=@donothing;
  188.     closefunc:=@donothing;
  189.     inoutfunc:=@scrninchars;
  190.     flushfunc:=@donothing;
  191.     bufpos:=0;
  192.     bufend:=0
  193.   end;
  194.   move (scrnin,input,sizeof(textrec))
  195. end.
  196.  
  197.