program cursor (input,output,datei);
const c_right = chr(29);
      c_left = chr(157);
      c_up = chr(145);
      c_down = chr(17);
      c_home = chr(19);
      zeilenmax = 400;
      spaltenmax = 80;
      tc_right = chr(29);
      tc_left = chr(15);
      tc_up = chr(81);
      tc_down = chr(17);
      t_return = chr(13);
      t_esc = chr(27);
      brk = chr(03);
      esc = chr(27);
      clscr = chr(147);
      t_ins = chr(20);
      t_del = chr(13);
type ascii = array[1..zeilenmax,1..spaltenmax] of char;
var zeile: integer;
    spalte: integer;
    zeichen: ascii;
    taste: char;
    position: integer;
    speicherung: integer;
    puffer: char;
    datei: text;
function dosxqq(nummer,wert: word): char; extern;
procedure scrolling;
begin
if position=0
then
          begin
          write(esc,chr(87),chr(13),c_up);
          for spalte:=1 to 80 do
                    begin
                    if zeichen[zeile,spalte]=chr(0)
                    then
                              begin
                              write(chr(13));
                              break
                              end;
                    write(zeichen[zeile,spalte]);
                    if zeichen[zeile,spalte]=chr(13) then break;
                    end;
          write(c_home);
          position:=1; spalte:=1
          end;
if position=25
then
          begin
          write(chr(13),c_up);
          for spalte:=1 to 80 do
                    begin
                    if zeichen[zeile,spalte]=chr(0)
                    then
                              begin
                              write(chr(13));
                              break
                              end;
                    write(zeichen[zeile,spalte]);
                    if zeichen[:eile,spalte]=chr(13) then break;
                    end;
          write(c_up);
          position:=24; spalte:=1
          end;
end; (* scrolling *)
procedure laden;
begin
zeile:=1; spalte:=1;
reset(datei);
while not eof(datei) do
          begin
          if eoln(datei)
          then
                    begin
                    zeichen[zeile,spalte]:=chr(13);
                    spalte:=0; zeile:=zeile+1
                    end
          else
                    begin
                    zeichen[zeile,spalte]:=datei^
                    end;
          get(datei);
          spalte:=spalte+1
          end;
close(datei)
end; (* laden *)
procedure listen;
begin
write(clscr);
for zeile:=1 to 24 do
          begin
          for spalte:=1 to 80 do
                    begin
                    if zeichen[zeile,spalte]=chr(0)
                    then
                              begin
                              write(chr(13));
                              break
                              end;
                    write(zeichen[zeile,spalte]);
                    if zeichen[zeile,spalte]=chr(13) then break;
                    end
          end
end; (* listen *)
procedure abspeichern;
begin
rewrite(datei);
for speicherung:=1 to zeile do
          begin
          for spalte:=1 to 80 do
                    begin      
                    datei^:= zeichen[speicherung,spalte];    
                    put(datei);
                    if zeichen[speicherung,spalte]=chr(13) then break;
                    end;
          datei^:=chr(10);
          put(datei)
          end;
close(datei)
end; (* abspeichern *)
procedure delete;
begin
end; (* delete *)
procedure insert;
begin
end; (* insert *)
procedure meldung;
begin
end; (* meldung *)
begin
meldung;
laden;
listen;
write(c_home);
zeile:=1; spalte:=1; position:=1;
repeat                 
          scrolling;
          taste:=dosxqq(#08,0);
          if taste in
          [tc_right,chr(0),tc_down,t_return,t_esc,t_ins]
          then
                    begin
                    if taste=tc_right 
                    then
                              begin
                              if spalte<80
                              then
                                        begin
                                        write(c_right);
                                        spalte:=spalte+1
                                        end
                              end;
                    (* ------------------------------ *)
                    if taste=chr(0)
                    then
                              begin
                              taste:= dosxqq(#08,0);
                    (* ------------------------------ *)
                    if taste=tc_left
                    then
                              begin
                              if spalte>1
                              then
                                        begin
                                        write(c_left);
                                        spalte:=spalte-1
                                        end
                              end;
                    if taste=tc_up
                    then
                              begin
                              write(c_up);
                              zeile:=zeile-1;
                              position:=position-1;
                              if zeile=0
                              then
                                        begin
                                        zeile:=1;
                                        position:=1
                                        end
                              end;
                    if taste=t_del
                    then
                              begin
                              delete
                              end
                    (* ------------------------------ *)
                              end;
                    (* ------------------------------ *)
                    if taste=tc_down
                    then
                              begin
                              write(c_down);
                              zeile:=zeile+1;
                              position:=position+1
                              end;
                    if taste=t_return
                    then
                              begin
                              write(chr(13));
                              zeichen[zeile,spalte]:=taste;
                              zeile:=zeile+1;
                              position:=position+1;
                              spalte:=1
                              end;
                    if taste=t_esc
                    then
                              begin
                              abspeichern
                              end;
                    if taste=t_ins
                    then
                              begin
                              insert
                              end
                    end
          else
                    begin
                    write(taste);
                    zeichen[zeile,spalte]:=taste;
                    spalte:=spalte+1;
                    if spalte=81
                    then
                              begin
                              zeile:=zeile+1;
                              spalte:=1
                              end
                    end;
until taste=brk
end.
