






program keys;

type  fkey      = string[63];
      allkeys   = array[1..32] of fkey;
      filename =   string[8]   ;
      filetype =   string[3]   ;
      fullname =   string[14]  ;
var   keylist   :allkeys;
      keynum    :integer;
      i,j       :integer;
      response  :char;
      name :       filename    ;
      ftype :      filetype    ;
      UseFile :    fullname    ;
      drive :      char        ;
      X,Y  :       integer     ;
      diskfile :   text;
{$I filetools.pas}

procedure InitKeylist(var keylist:allkeys);
var i,j     :integer;
    anykey  :fkey;
begin
 for i := 1 to 32 do
  keylist[i] := '';
 for i := 1 to 32 do
  begin
   case i of
   1..10: begin
           str(i,anykey);
           if i = 10 then
            insert(' ',anykey,1)
           else
            insert('  ',anykey,1);
          end;
   11    :anykey :=' ^1';
   12..20:begin
           str(i-11,anykey);
            insert(' s',anykey,1);
          end;
   21    :anykey := 's10';
   22    :anykey := '^s1';
   23..27:begin
           str(i-21,anykey);
           insert(' ^',anykey,1);
          end;
   28..32:begin
           str(i-26,anykey);
           insert('^s',anykey,1);
          end;
   end;     {...............case }
   insert(':',anykey,4);
   keylist[i] := anykey;
  end;
end;




procedure Display(keylist:allkeys);
var   i   :integer;
      row    :integer;
      anykey :fkey;
begin
 for i := 1 to 32 do
  begin
    row := (i div 2) + 9;
    if (i mod 2 <> 0) then
     begin
      gotoXY(1,row);
      ClrEol;
     end
    else
     gotoXY(40,row-1);

   anykey := copy(keylist[i],1,36);
   write(anykey);
  end;
end;


procedure PointTo(var keynum:integer;
                  var response:char);
var   i   :integer;
      row    :integer;
begin
 i := keynum;
 repeat
  begin
    row := (i div 2) + 9;
    if (i mod 2 <> 0) then
     begin
      gotoXY(1,row);
     end
    else
     gotoXY(40,row-1);
    read(kbd,response);
    delay(50);
    case response of
     ^H: i := i - 1;
     ^L: i := i + 1;
     ^K: i := i - 2;
     ^V: i := i + 2;
     ^M: i := i + 1;
    end;  { case }
    response := UpCase(response);
    if i > 32 then
     i := 1;
    if i < 1 then
     i := 32;
 end;
until (response = 'E') or (response = 'Q') or (response = 'X') or (response = 'S');
keynum := i;
end;

procedure EditKey(var keylist:allkeys;
                      keynum:integer);
var     quit    :boolean;
        i       :integer;
        response:char;
        firstPart,lastPart :string[65];
begin
 gotoXY(1,1);
 ClrEol;
 delay(50);
 write(keylist[keynum]);
 delay(75);
 i := 5;
 quit := false;
 repeat
  gotoXY(i,1);
  read(kbd,response);
  case response of
   ^H: i := i - 1;
   ^L: i := i + 1;
   ^Y: quit := true;
   #$7F:begin
         delete(keylist[keynum],i,1);  { #7f is the DEL key }
         gotoXY(1,1);
         clrEol;
         write(keylist[keynum]);
         delay(100);
         gotoXY(i,1);
        end;
  else
   begin
    write(response);
    delay(100);
    firstPart := copy(keylist[keynum],1,i-1);
    lastPart := copy(keylist[keynum],i+1,68);
    keylist[keynum] := concat(firstPart,response,lastPart);
    i := i + 1;
  end;
 end;  {........case}
    if (i > 67) or (i < 5) then
     quit := true;

 until quit;
end;


procedure Menu;
begin
 gotoXY(1,3);
 writeln('         Function Keys for TeleVideo.........');
 writeln('     (E)dit  -  (S)ave  -  e(X)it');
 writeln('          arrow selects keys      left/right/del arrow to edit ');
 write('                                         use ^Y to quit edit ');
end;

procedure SaveIt(keylist:allkeys);
var    i       :integer;
begin
 NewFileName(drive,name,ftype,1,1);
  if name = ' ' then
   write(^G)
  else
   begin
    assign(diskFile,drive + ':' + name + '.' + ftype);
    rewrite(diskfile);
    for i := 1 to 32 do
     begin
      write(diskfile,#$9B#$7C + chr(48 + i) + copy(keylist[i],4,67) + #$19);
     end;
    close(diskfile);
   end;
end;




begin { --------------------- main -------------------- }
 ClrScr;
 Menu;
 InitKeyList(keylist);
 keynum := 1;
 Display(keylist);
 repeat
  pointTo(keynum,response);
  Case response of
   'E': begin
         editKey(keylist,keynum);
         delay(150);
         display(keylist);
        end;
   'S':SaveIt(keylist);
  end; {........case }
 until response = 'X';
end.

{  I have not bothered to comment this stuff because I have never seen
  anyone else who actually uses turbo pascal on the televideo.  If you
  play around with pascal I would like to hear from you

                              G.T.J.                                   }
