program zobrazeni_textu;

uses crt;

var F : file;
    soubor : string[12];
    text : string;
    NumRead : word;

procedure Zobraz;
const indexstranky : word = 1;
      Bad   = #255;
      Null  = #0;
      ESC   = #27;
      Home  = #71;
      PgUp  = #73;
      PgDn  = #81;
      Done     : Boolean = False;
var velikost, FSeg, vysledek, konec : word;
    PF : pointer;
    stranky : array[1..2000]of word;
    ch : char;
begin
  assign(F,soubor);
  {$I-}
  reset(F,1);
  {$I+}
  if IOResult <> 0 then
  begin
    writeln('Soubor nebyl nalezen: ',soubor);
    writeln;
    writeln('Stisknete ENTER ...');
    readln;
    halt(1)
  end;
  velikost := filesize(F);
  getmem(PF, velikost);
  FSeg := Seg(PF^);
  BlockRead(F, PF^, velikost);
  FillChar(stranky, SizeOf(stranky), 0);
  Repeat
    clrScr;
    konec := stranky[indexstranky];
    repeat
      write(Chr(Mem[FSeg:konec]));
      inc(konec);
    until (konec = velikost) or (WhereY = 25);
    repeat
      ch := ReadKey;
      if ch = ESC then
        Done := true
      else
        if ch = Null then
          case ReadKey of
            Home:  indexstranky := 1;
            PgUp:  if (indexstranky > 1) then
                     dec(indexstranky);
            PgDn:  if (konec < velikost) then
                   begin
                     Inc(indexstranky);
                     stranky[indexstranky] := konec;
                   end
            else
              ch := Bad
          end;
    until (ch = Null) or Done;
  until Done;
  close(F)
end;

begin
  clrscr;
  writeln('Tento program otevre libovolny soubor k prohlizeni.');
  writeln;
  write('Vloz nazev souboru (pr.: pokus.txt) : ');
  readln(soubor);
  Zobraz;
  writeln;
  writeln('Stiskni ENTER ...');
  readln;
end.