'
' Type program to replace Desktop Show function.
'
' by S.K. Webb
'
Rez%=Xbios(4)
Gosub Palette_saver
If Rez%=0
  Void Xbios(5,L:-1,L:-1,1)
  Setcolor 0,7,7,7
  Setcolor 2,0,7,0
  Setcolor 1,0,7,0
  Setcolor 3,0,0,0
Endif
If Rez%<2
  Bline%=199
Else
  Bline%=399
Endif
T1$=" Page:  Page:  Line Up:  Line Down:   Start:<Home>  End:<Ins>  Quit:<Undo> "
Command_line$=Space$(128)
Bmove Basepage+129,Varptr(Command_line$),127
For C%=1 To Len(Command_line$)
  If Mid$(Command_line$,C%)=" "
    Mid$(Command_line$,C%)=Chr$(0)
  Endif
Next C%
While Asc(Right$(Command_line$,1))<33
  Exit If Len(Command_line$)<2
  Command_line$=Left$(Command_line$,Len(Command_line$)-1)
Wend
While Asc(Left$(Command_line$,1))<33
  Exit If Len(Command_line$)<2
  Command_line$=Right$(Command_line$,Len(Command_line$)-1)
Wend
If Len(Command_line$)<2
  Fileselect "\*.*","",File$
  If Len(File$)=0
    Gosub End
  Endif
Else
  File$=Command_line$
Endif
Repeat
Until Mousek=0
Hidem
Dim Type$(2200)
F$=String$(82,32)
Cr$=Chr$(13)+Chr$(10)
For I%=1 To 2199
  Type$(I%)=F$
Next I%
Type$(0)=Cr$
Open "I",#1,File$
Ex%=Lof(#1)
If Ex%>255
  Test$=Input$(255,#1)
Else
  Test$=Input$(Ex%,#1)
Endif
Close #1
X0%=Instr(Test$,Cr$)
Fr%=Fre(0)
Ar$=Str$(Fr%-5000)
If Fr%<(Ex%\4)+5000
  Gosub Too_long_abort
Endif
If X0%=0
  Gosub Non_text_abort
Endif
If X0%>242
  Gosub Long_line_abort
Endif
Test$=""
Dim T%((Ex%\4)+1)
Cls
If Rez%<2
  Deffill 3,2,8
Else
  Deffill 1,2,4
Endif
Pbox 0,18,639,Bline%
If Rez%=0
  Get 1,18,319,Bline%,Desk$
  Put 320,18,Desk$
Endif
Print "LOADING   ";File$;" -- Length of File: ";Str$(Ex%);" Bytes"
Bload File$,Varptr(T%(0))
Print "File LOAD is complete. Text FORMAT requires ";Left$(Str$(Ex%/7565),6);" seconds to complete.";
N%=1
I%=0
Li%=I%
Repeat
  Repeat
    A%=Peek(Varptr(T%(0))+I%)
    Inc I%
  Until A%=10
  If I%-Li%=<82
    Bmove Varptr(T%(0))+Li%,Varptr(Type$(N%)),I%-Li%
  Else
    Bmove Varptr(T%(0))+Li%,Varptr(Type$(N%)),80
    Type$(N%)=Left$(Type$(N%),80)+Chr$(13)+Chr$(10)
    Li%=Li%+80
    Inc N%
    If I%-Li%=<82
      Bmove Varptr(T%(0))+Li%,Varptr(Type$(N%)),I%-Li%
    Else
      Bmove Varptr(T%(0))+Li%,Varptr(Type$(N%)),80
      Type$(N%)=Left$(Type$(N%),80)+Chr$(13)+Chr$(10)
      Li%=Li%+80
      Inc N%
      Bmove Varptr(T%(0))+Li%,Varptr(Type$(N%)),I%-Li%
    Endif
  Endif
  Type$(N%)=Left$(Type$(N%),I%-Li%)
  Li%=I%
  Inc N%
Until I%=Ex% Or N%>2199
Dec N%
Top%=N%
Cls
Erase T%()
For I%=Top%+1 To 2199
  Type$(I%)=""
Next I%
Heading%=1
Deftext 1,0,0,6*Rez%+(1*(Rez%-1))
Gosub Screen_print
Do
  C$=Inkey$
  If Len(C$)=2
    C%=Asc(Right$(C$))
  Endif
  If Len(C$)=1
    Cc%=Asc(C$)
  Endif
  If Cc%>48 And Cc%<58
    Index%=Index%+(Jmp%*(Cc%-48)*Heading%)
    Gosub Screen_print
    Cc%=0
  Endif
  If C%=80
    Heading%=1
    Jmp%=23
    Index%=Index%+Jmp%
    Gosub Screen_print
    C%=0
  Endif
  If C%=72
    Heading%=-1
    Jmp%=23
    Index%=Index%-Jmp%
    Gosub Screen_print
    C%=0
  Endif
  If C%=75
    Heading%=-1
    Jmp%=1
    Index%=Index%-Jmp%
    Gosub Screen_print
    C%=0
  Endif
  If C%=77
    Heading%=1
    Jmp%=1
    Index%=Index%+Jmp%
    Gosub Screen_print
    C%=0
  Endif
  If C%=71
    Heading%=1
    Jmp%=23
    Index%=0
    Gosub Screen_print
    C%=0
  Endif
  If C%=97
    Gosub End
  Endif
  If C%=82
    Heading%=-1
    Jmp%=23
    Index%=Top%-24
    Gosub Screen_print
    C%=0
  Endif
Loop
Gosub End
Procedure Screen_print
  Cls
  If Index%>Top%-24
    Index%=Top%-24
  Endif
  If Index%<0
    Index%=0
  Endif
  If 23+Index%=>Top%
    For I%=0+Index% To Top%-1
      Print Type$(I%);
    Next I%
  Else
    For I%=0+Index% To 23+Index%
      Print Type$(I%);
    Next I%
  Endif
  Print At(1,25);Chr$(27);"p";
  For Z%=1 To Len(T1$)
    Void Bios(3,5,Asc(Mid$(T1$,Z%,1)))
  Next Z%
  Print Chr$(27);"q";
Return
Procedure Too_long_abort
  Alrt$="    ERROR! FILE TOO LONG!| File Must Be Text File Less|Than "+Ar$+" Bytes With No Lines|  Longer Than 240 Characters!"
  Alert 3,Alrt$,1,"ABORT",Dummie%
  Gosub End
Return
Procedure Non_text_abort
  Alrt$="    ERROR! NON-TEXT FILE!| File Must Be Text File Less|Than "+Ar$+" Bytes With No Lines|  Longer Than 240 Characters!"
  Alert 3,Alrt$,1,"ABORT",Dummie%
  Gosub End
Return
Procedure Long_line_abort
  Alrt$="   ERROR! LINES TOO LONG!| File Must Be Text File Less|Than "+Ar$+" Bytes With No Lines|  Longer Than 240 Characters!"
  Alert 3,Alrt$,1,"ABORT",Dummie%
  Gosub End
Return
Procedure Palette_saver
  For I%=0 To 15
    Userp$=Userp$+Mki$(Xbios(7,I%,-1))
  Next I%
Return
Procedure End
  Void Xbios(6,L:Varptr(Userp$))
  Void Xbios(5,L:-1,L:-1,Rez%)
  Showm
  System
Return
