 Rem File: EditcmdG for Dndbbs Version v5.0a Release r2.0
 Rem $Include: 'edit.inc'

 On Error Goto ErrMain0
 If Chained2=False Then
    Call Open.Arrays
    Call Get.Config
    Call Increase.Files
    Call Make.FileNames
    Call Open.Files
    Call Reset.Cursor
    Call Detect.Share
    Call Save.Screen2(0)
    Call Passwrd9(Var,"g")
    If Var=False Then
       Call Terminate.Program
       End
    Endif
    Call Read.Rows(Var)
    Max.Row=Var
 Endif
 Call Scrnedit
MainExit0:
 Close
 Call Clear.Status.Line
 If Chained2 Then
    Chain DND.Path+"EDIT.EXE"
 Endif
 Call Terminate.Program
ErrMain0:
 Resume MainExit0

Sub Scrnedit
 ON LOCAL ERROR GOTO Error.Trap72
 DIM LineX As String
 DIM VarX AS STRING * 1
 DIM VarY AS STRING * 1
 Dim Linux As Integer
 Call Edit.Status.Line
 If Command$<>Nul Then
    FileName = Ltrim$(Command$)
    FileName = Rtrim$(Command$)
    If Left$(Filename,1)=Chr$(34) Then
       Filename=Mid$(Filename,2)
       If Right$(Filename,1)=Chr$(34) Then
          Filename=Left$(Filename,Len(Filename)-1)
       Endif
    Endif
    If Filename="/?" Or Filename="-?" Then
       Call Boot.Usage
       Exit Sub
    Endif
    V=Instr(Filename,"/LINUX")
    If V Then
       Linux=-1
       Filename=Left$(Filename,V-1)+Mid$(Filename,V+7)
    Endif
    Filename = Ltrim$(Rtrim$(FileName))
    OutX$ = Ltrim$(Rtrim$(FileName))
    For V=Len(OutX$) To 1 Step -1
       If Mid$(OutX$,V,1)="\" Then
          OutX$=MId$(OutX$,V+1)
          Exit For
       Endif
    Next
    If Mid$(OutX$,2,1)=":" Then
       OutX$=Mid$(OutX$,3)
    Endif
    Goto 102
 Endif
101
 DO WHILE Lost.Carrier = False
    DO WHILE Lost.Carrier = False
       Call Edit.Status.Line
       OutX$ = Nul
       If TempC=False Then
          Locate Max.Row-1,1,1
          Graphics.Off = False
          Strng = "Screen editor utility:"
          CALL IO.O
          Graphics.Off = True
          Strng = RT$(632,"[1]")+"Catalog"
          CALL IO.O
          Strng = RT$(633,"[2]")+"Logoff"
          CALL IO.O
          Strng = RT$(634,"[3]")+"Notice"
          CALL IO.O
          Strng = RT$(635,"[4]")+"Prelog"
          CALL IO.O
          Strng = RT$(636,"[5]")+"Welcome"
          CALL IO.O
          If Local.Mode Or Sysop Then
             Strng=RT$(648,"[6]")+"Other"
             Call IO.O
          Endif
       Endif
       Graphics.Off = False
       No.Input.Out = "Q"
       Line.Length=TempD
       If Local.Mode Or Sysop Then
          Strng = "Enter option(1-6, Q to Quit)? "
       Else
          Strng = "Enter option(1-5, Q to Quit)? "
       Endif
       CALL IO.I
       SELECT CASE UCASE$(Out2)
       CASE "1"
          If RT$(632,"[1]")="[*]" Then
             Strng="Command restricted.."
             Call IO.O
          Else
             FileName = Catalog.FileName
             OutX$ = "Catalog"
          Endif
       CASE "2"
          If RT$(633,"[2]")="[*]" Then
             Strng="Command restricted.."
             Call IO.O
          Else
             FileName = Logoff.FileName
             OutX$ = "Logoff"
          Endif
       CASE "3"
          If RT$(634,"[3]")="[*]" Then
             Strng="Command restricted.."
             Call IO.O
          Else
             FileName = Notice.FileName
             OutX$ = "Notice"
          Endif
       CASE "4"
          If RT$(635,"[4]")="[*]" Then
             Strng="Command restricted.."
             Call IO.O
          Else
             FileName = Prelog.FileName
             OutX$ = "Prelog"
          Endif
       CASE "5"
          If RT$(636,"[5]")="[*]" Then
             Strng="Command restricted.."
             Call IO.O
          Else
             FileName = Welcome.FileName
             OutX$ = "Welcome"
          Endif
       Case "6"
          If Local.Mode Or Sysop Then
             If RT$(648,"[6]")="[*]" Then
                Strng="Command restricted.."
                Call IO.O
             Else
                Strng = "Filename: "
                Call IO.I
                If Len(Out2) Then
                   FileName = Out2
                   OutX$ = FileName
                   For V=Len(OutX$) To 1 Step -1
                      If Mid$(OutX$,V,1)="\" Then
                         OutX$=MId$(OutX$,V+1)
                         Exit For
                      Endif
                   Next
                   If Mid$(OutX$,2,1)=":" Then
                      OutX$=Mid$(OutX$,3)
                   Endif
                Endif
             Endif
          Endif
       CASE "Q"
          OutX$=Nul
          EXIT DO
       END SELECT
102
       If Linux=0 Then
          Strng = "Load file in Linux form(y/n)?"
          No.Input.Out="Y"
          CALL IO.I
          IF Yes THEN
             Linux=-1
          Endif
       Endif

       Max.Lines = False
       Max.File = 1024
       REDIM Temp.ArrayS(1024) AS STRING
       IF LEN(OutX$) THEN
	  CLOSE #TempFile1
          IF DIR$(FileName) = Nul Then ' create file
             OPEN FileName FOR OUTPUT AS #TempFile1
          ELSE
             OPEN FileName FOR BINARY AS #TempFile1 ' read file in binary
             Var! = 0!
             LineX = Nul
             DO
                Var! = Var! + 1!
                IF Var! > LOF(TempFile1) THEN
                   If Len(LineX) Then
                      Max.Lines = Max.Lines + 1
                      IF Max.Lines > Max.File THEN
                         Max.File = Max.File + 1
                         Redim Preserve Temp.ArrayS(Max.File) AS STRING
                      END IF
                      Temp.ArrayS(Max.Lines) = LineX
                   Endif
                   EXIT DO
                END IF
                GET #TempFile1, Var!, VarX
                LineFeed = 0
                If Linux Then
                   IF VarX = CHR$(10) THEN
                      LineFeed=-1
                   END IF
                Endif
                IF VarX = CHR$(13) THEN
                   IF Var! + 1! <= LOF(TempFile1) THEN
                      GET #TempFile1, Var! + 1!, VarY
                      IF VarY = CHR$(10) THEN
                         Var! = Var! + 1!
                         LineFeed = -1
                      END IF
                   END IF
                END IF
                IF LineFeed THEN
                   Max.Lines = Max.Lines + 1
                   IF Max.Lines > Max.File THEN
                      Max.File = Max.File + 1
                      Redim Preserve Temp.ArrayS(Max.File) AS STRING
                   END IF
                   Temp.ArrayS(Max.Lines) = LineX
                   LineX = Nul
                ELSE
                   LineX = LineX + VarX
                END IF
             LOOP
          Endif
          Exit Do
       END IF
    LOOP
    If OutX$=Nul Then
       Exit Do
    Endif
    Xcoor = 1
    Ycoor = 1
    Column = 1
    Row = 1
    If Max.Lines = 0 Then
       Line.Number = 0
    Else
       Line.Number = 1
    Endif
    OutX$ = LCASE$(OutX$)
    MID$(OutX$, 1, 1) = UCASE$(MID$(OutX$, 1, 1))
    Graphics.Off = True
    GOSUB Display.Screen
    DO WHILE Lost.Carrier = False
       CALL Modem.ANSI(4, 33, 0)
       CALL ScreenANSI(4, 33, 0)

       CALL Modem.ANSI(4, 44, 30)
       CALL ScreenANSI(4, 44, 30)

       CALL Modem.ANSI(2, 24, 1)
       CALL ScreenANSI(2, 24, 1)

       V = Column + Ycoor - 1
       Var# = Cdbl(Line.Number)
       If Var# = 0# Then
          Var$ = " 0"
       Else
          Call Amount(Var#,Var$)
       Endif

       If V = 0 Then
          Var2$ = " 0"
       Else
          Call Amount(Cdbl(V),Var2$)
       Endif

       Strng = "Row" + Var$ + " Column" + Var2$

       If V > 0 And V <= Len(Temp.ArrayS(Line.Number)) Then
          V2 = Asc(Mid$(Temp.ArrayS(Line.Number), V, 1))
          Strng = Strng + " Ascii" + Str$(V2)

          Select Case V2 ' parse control codes
          Case 0
             Strng = Strng + " (nul)"
          Case 1
             Strng = Strng + " (soh)"
          Case 2
             Strng = Strng + " (stx)"
          Case 3
             Strng = Strng + " (etx)"
          Case 4
             Strng = Strng + " (eot)"
          Case 5
             Strng = Strng + " (enq)"
          Case 6
             Strng = Strng + " (ack)"
          Case 7
             Strng = Strng + " (bel)"
          Case 8
             Strng = Strng + " (bs)"
          Case 9
             Strng = Strng + " (tab)"
          Case 10
             Strng = Strng + " (lf)"
          Case 11
             Strng = Strng + " (vt)"
          Case 12
             Strng = Strng + " (np)"
          Case 13
             Strng = Strng + " (cr)"
          Case 14
             Strng = Strng + " (so)"
          Case 15
             Strng = Strng + " (si)"
          Case 16
             Strng = Strng + " (dle)"
          Case 17
             Strng = Strng + " (dc1)" ' xon
          Case 18
             Strng = Strng + " (dc2)"
          Case 19
             Strng = Strng + " (dc3)" ' xoff
          Case 20
             Strng = Strng + " (dc4)"
          Case 21
             Strng = Strng + " (nak)"
          Case 22
             Strng = Strng + " (syn)"
          Case 23
             Strng = Strng + " (etb)"
          Case 24
             Strng = Strng + " (can)"
          Case 25
             Strng = Strng + " (em)"
          Case 26
             Strng = Strng + " (eof)"
          Case 27
             Strng = Strng + " (esc)"
          Case 28
             Strng = Strng + " (fs)"
          Case 29
             Strng = Strng + " (gs)"
          Case 30
             Strng = Strng + " (rs)"
          Case 31
             Strng = Strng + " (us)"
          Case 32
             Strng = Strng + " (spc)"
          Case 255
             Strng = Strng + " (?)"
          Case Else
             Strng = Strng + " (" + Chr$(V2) + ")"
          End Select
       Endif

       Strng = Left$(Strng, 41)
       Strng = Strng + SPACE$(41 - LEN(Strng))
       CALL Scrn.Output(Strng)

       CALL Modem.ANSI(2, Xcoor, Ycoor)
       CALL ScreenANSI(2, Xcoor, Ycoor)

       CALL Modem.ANSI(4, 37, 0)
       CALL ScreenANSI(4, 37, 0)

       CALL Modem.ANSI(4, 40, 15)
       CALL ScreenANSI(4, 40, 15)

       TempX$ = Nul
       DO WHILE TempX$ = Nul
	  IF Lost.Carrier THEN
             EXIT DO
          END IF
          CALL Read.ANSI.Input
          If TempX$ = Chr$(0) + Chr$(0) Then ' Control-Break
             Lost.Carrier=True
             Exit Do
          Endif
          Select Case TempX$
          Case Chr$(4) ' ^D = control-right
             TempX$ = Chr$(0) + Chr$(116)
          Case Chr$(6) ' ^F = control-left
             TempX$ = Chr$(0) + Chr$(115)
          Case Chr$(19) ' ^S = control-home
             TempX$ = Chr$(0) + Chr$(119)
          Case Chr$(24) ' ^X = control-end
             TempX$ = Chr$(0) + Chr$(117)
          Case Chr$(21) ' ^U = pageup
             TempX$ = Chr$(0) + Chr$(73)
          Case Chr$(14) ' ^N = pagedown
             TempX$ = Chr$(0) + Chr$(81)
          Case Chr$(22) ' ^V = insert
             TempX$ = Chr$(0) + Chr$(82)
          End Select
       LOOP
       SELECT CASE LEN(TempX$)
       CASE 1
	  SELECT CASE TempX$
          CASE CHR$(13) ' insert line
             If Max.Lines = 0 Then ' init array
                Line.Number = 1
                Temp.ArrayS(1) = Nul
                Max.Lines = 1
             Else
                ' enter always extends array
                IF Max.Lines + 1 > Max.File THEN
                   Max.File = Max.File + 1
                   Redim Preserve Temp.ArrayS(Max.File) AS STRING
                END IF
                IF Max.Lines < Max.File THEN
                   ' above
                   IF Column + Ycoor - 1 = 1 THEN
                      FOR Temp1 = Max.Lines TO Line.Number STEP -1
                         Temp.ArrayS(Temp1 + 1) = Temp.ArrayS(Temp1)
                      NEXT
                      Temp.ArrayS(Line.Number) = Nul
                      Max.Lines = Max.Lines + 1
                      Line.Number = Line.Number + 1
                      IF Xcoor < 23 THEN
                         Xcoor = Xcoor + 1
                      END IF
                   ELSE
                      ' below
                      IF Column + Ycoor - 1 > LEN(Temp.ArrayS(Line.Number)) THEN
                         Line.Number = Line.Number + 1 ' below
                         FOR Temp1 = Max.Lines TO Line.Number STEP -1
                            Temp.ArrayS(Temp1 + 1) = Temp.ArrayS(Temp1)
                         NEXT
                         Temp.ArrayS(Line.Number) = Nul
                         Max.Lines = Max.Lines + 1
                         IF Xcoor < 23 THEN
                            Xcoor = Xcoor + 1
                         END IF
                      ELSE
                         ' split line
                         FOR Temp1 = Max.Lines TO Line.Number + 1 STEP -1
                            Temp.ArrayS(Temp1 + 1) = Temp.ArrayS(Temp1)
                         NEXT
                         T$ = Temp.ArrayS(Line.Number)
                         U$ = MID$(T$, Column + Ycoor - 1)
                         T$ = LEFT$(T$, Column + Ycoor - 2)
                         Temp.ArrayS(Line.Number) = T$
                         Line.Number = Line.Number + 1 ' below
                         Temp.ArrayS(Line.Number) = U$
                         Max.Lines = Max.Lines + 1
                         IF Xcoor < 23 THEN
                            Xcoor = Xcoor + 1
                         END IF
                      END IF
                   END IF
                END IF
             Endif
             ' enter always moves to 1,1
             Column = 1
             Ycoor = 1
             GOSUB Display.Screen
	  CASE CHR$(8) ' backspace
	     Q = Column + Ycoor - 1
             IF Line.Number <= 1 AND Q <= 1 THEN ' no backspace at 1,1
                Eat$ = Nul
	     ELSE
                ' remove current line
                IF Temp.ArrayS(Line.Number) = Nul THEN
                   ' pack array
                   FOR Temp1 = Line.Number TO Max.Lines - 1
                      Temp.ArrayS(Temp1) = Temp.ArrayS(Temp1 + 1)
                   NEXT
                   IF Xcoor > 1 THEN
                      Xcoor = Xcoor - 1
                   END IF
                   Line.Number = Line.Number - 1
                   Max.Lines = Max.Lines - 1
                   ' position at eol
                   End.Line = LEN(Temp.ArrayS(Line.Number))
                   IF End.Line >= 80 THEN
                      Column = End.Line - 78
                      Ycoor = End.Line - Column + 2
                   ELSE
                      Ycoor = End.Line - Column + 2
                   END IF
                   GOSUB Display.Screen
                ELSE
                   ' concatenate previous line
                   IF Column + Ycoor - 1 <= 1 THEN
                      IF Line.Number > 1 THEN
                         IF Line.Number <= Max.Lines THEN
                            Var=0
                            If Temp.ArrayS(Line.Number-1)=Nul Then
                               If Ycoor=1 Then
                                  Var=-1
                               Endif
                            Endif

                            ' position at eol
                            If Var=0 Then
                               End.Line = LEN(Temp.ArrayS(Line.Number-1))
                            Endif

                            Temp.ArrayS(Line.Number - 1) = Temp.ArrayS(Line.Number - 1) + Temp.ArrayS(Line.Number)
                            FOR Temp1 = Line.Number TO Max.Lines - 1
                               Temp.ArrayS(Temp1) = Temp.ArrayS(Temp1 + 1)
                            NEXT
                            If Xcoor > 1 Then
                               Xcoor = Xcoor - 1
                            Endif
                            Line.Number = Line.Number - 1
                            Max.Lines = Max.Lines - 1
                            Column = 1
                            Ycoor = 1

                            ' position at eol
                            If Var=0 Then
                               IF End.Line >= 80 THEN
                                  Column = End.Line - 78
                                  Ycoor = End.Line - Column + 2
                               ELSE
                                  Ycoor = End.Line - Column + 2
                               END IF
                            Endif

                            GOSUB Display.Screen
                         END IF
                     END IF
                  ELSE
                      ' backspace 1 character
                      IF Column + Ycoor - 2 > False THEN
                         T$ = Temp.ArrayS(Line.Number)
                         T$ = LEFT$(T$, Column + Ycoor - 3) + MID$(T$, Column + Ycoor - 1)
                         Temp.ArrayS(Line.Number) = T$
                         ' move cursor 1 left
                         IF Ycoor > 1 THEN
                            Ycoor = Ycoor - 1
                            GOSUB Display.Screen
                         ELSE
                            IF Column > 1 THEN
                               Column = Column - 1
                               GOSUB Display.Screen
                            END IF
                         END IF
                      END IF
                   END IF
                END IF
             END IF
	  CASE CHR$(9) ' tab
	     IF Column + Ycoor + 7 <= LEN(Temp.ArrayS(Line.Number)) THEN
                IF Ycoor + 8 < 80 THEN
		   Ycoor = Ycoor + 8
		ELSE
		   Column = Column + 8
		   GOSUB Display.Screen
		END IF
	     ELSE ' end
		End.Line = LEN(Temp.ArrayS(Line.Number))
                IF End.Line >= 80 THEN
                   Column = End.Line - 78
                   Ycoor = End.Line - Column + 2
                   GOSUB Display.Screen
                ELSE
                   Ycoor = End.Line - Column + 2
                END IF
	     END IF
	  CASE CHR$(27) ' escape, exit editor
	     EXIT DO
	  CASE ELSE ' insert character
             SELECT CASE ASC(TempX$)
             CASE 12, 32 TO 126
                If Line.Number > 0 Then
                   T$ = Temp.ArrayS(Line.Number)
                   T$ = LEFT$(T$, Column + Ycoor - 2) + TempX$ + MID$(T$, Column + Ycoor - 1)
                   Temp.ArrayS(Line.Number) = T$
                   IF Column + Ycoor - 1 <= LEN(Temp.ArrayS(Line.Number)) THEN
                      IF Ycoor < 80 THEN
                         Ycoor = Ycoor + 1
                         GOSUB Display.Screen
                      ELSE
                         Column = Column + 1
                         GOSUB Display.Screen
                      END IF
                   END IF
                Endif
             END SELECT
	  END SELECT
       CASE 2
	  Temp = ASC(MID$(TempX$, 2, 1))
	  SELECT CASE Temp
          CASE 0 ' ctrl-break, exit editor
	     EXIT DO
	  CASE 15 ' shift-tab
	     IF Column + Ycoor - 8 > False THEN
		IF Column + Ycoor - 7 > 1 THEN
		   Ycoor = Ycoor - 7
		ELSE
		   IF Column > 8 THEN
		      Column = Column - 7
		      GOSUB Display.Screen
		   END IF
		END IF
	     ELSE ' home
		IF Column > 1 THEN
		   Column = 1
		   Ycoor = 1
		   GOSUB Display.Screen
		ELSE
		   Column = 1
		   Ycoor = 1
		END IF
	     END IF
          CASE 83, 147 ' delete/control-delete
	     Q = Column + Ycoor - 1
             ' no delete at 1,1
             IF (Line.Number <= 1 AND Q < 1) OR Max.Lines < 1 THEN
                Eat$ = Nul
	     ELSE
                ' remove line
                IF Temp.ArrayS(Line.Number) = Nul THEN
                   IF Line.Number = 1 AND Max.Lines = 1 THEN ' 1,1
                      Eat$ = Nul
                   ELSE
                      ' pack array
                      FOR Temp1 = Line.Number TO Max.Lines - 1
                         Temp.ArrayS(Temp1) = Temp.ArrayS(Temp1 + 1)
                      NEXT
                      IF Line.Number = Max.Lines THEN
                         Line.Number = Line.Number - 1
                         IF Xcoor > 1 THEN
                            Xcoor = Xcoor - 1
                         END IF
                      END IF
                      Max.Lines = Max.Lines - 1
                      Column = 1
                      Ycoor = 1
                      GOSUB Display.Screen
                   END IF
                ELSE
                   ' delete line
                   IF LEN(Temp.ArrayS(Line.Number))<1 THEN
                      Temp.ArrayS(Line.Number) = Nul
                      GOSUB Display.Screen
                   ELSE
                      ' concatenate line
                      IF Column + Ycoor - 1 > LEN(Temp.ArrayS(Line.Number)) THEN
                         If Line.Number + 1 <= Max.Lines Then
                            T$ = Temp.ArrayS(Line.Number)
                            T$ = T$ + Temp.ArrayS(Line.Number + 1)
                            Temp.ArrayS(Line.Number) = T$

                            ' pack array
                            FOR Temp1 = Line.Number + 1 TO Max.Lines - 1
                               Temp.ArrayS(Temp1) = Temp.ArrayS(Temp1 + 1)
                            NEXT
                            Max.Lines = Max.Lines - 1

                            GOSUB Display.SCreen
                         Endif
                      ELSE
                         ' delete character
                         T$ = Temp.ArrayS(Line.Number)
                         T$ = LEFT$(T$, Column + Ycoor - 2) + MID$(T$, Column + Ycoor)
                         Temp.ArrayS(Line.Number) = T$
                         GOSUB Display.Screen
                      END IF
                   END IF
                END IF
             END IF
          CASE 71 ' home
	     IF Column > 1 THEN
		Column = 1
		Ycoor = 1
		GOSUB Display.Screen
	     ELSE
		Column = 1
		Ycoor = 1
	     END IF
          CASE 79 ' end
	     End.Line = LEN(Temp.ArrayS(Line.Number))
             IF End.Line >= 80 THEN
                Column = End.Line - 78
                Ycoor = End.Line - Column + 2
                GOSUB Display.Screen
             ELSE
                Ycoor = End.Line - Column + 2
             END IF
          CASE 75, 155 ' left/alt-left
	     IF Ycoor > 1 THEN
		Ycoor = Ycoor - 1
	     ELSE
		IF Column > 1 THEN
		   Column = Column - 1
		   GOSUB Display.Screen
		END IF
	     END IF
          CASE 77, 157 ' right/alt-right
	     IF Column + Ycoor - 1 <= LEN(Temp.ArrayS(Line.Number)) THEN
                IF Ycoor < 80 THEN
		   Ycoor = Ycoor + 1
		ELSE
		   Column = Column + 1
		   GOSUB Display.Screen
		END IF
	     END IF
          CASE 72, 141, 152 ' up/ctrl-up/alt-up
	     Var = False
	     IF Line.Number > 1 THEN
		Line.Number = Line.Number - 1
		IF Xcoor = 1 THEN
		   Var = True
		ELSE
		   Xcoor = Xcoor - 1
		END IF
		End.Line = LEN(Temp.ArrayS(Line.Number)) + 1
		IF Column + Ycoor - 1 > End.Line THEN
		   Ycoor = End.Line - Column + 1
		   IF Ycoor <= 0 THEN
		      Ycoor = End.Line
		      IF Column > 1 THEN
			 Var = True
		      END IF
		      Column = 1
		   END IF
		END IF
		IF Var THEN
		   GOSUB Display.Screen
		END IF
	     END IF
          CASE 80, 145, 160 ' down/ctrl-dn/alt-dn
             Var = False
             IF Line.Number < Max.Lines THEN
		Line.Number = Line.Number + 1
                IF Xcoor >= 22 THEN
                   Var = True
		ELSE
		   Xcoor = Xcoor + 1
		END IF
		End.Line = LEN(Temp.ArrayS(Line.Number)) + 1
		IF Column + Ycoor - 1 > End.Line THEN
		   Ycoor = End.Line - Column + 1
		   IF Ycoor <= 0 THEN
		      Ycoor = End.Line
		      IF Column > 1 THEN
			 Var = True
		      END IF
		      Column = 1
		   END IF
		END IF
		IF Var THEN
		   GOSUB Display.Screen
		END IF
	     END IF
          CASE 73, 132 ' ^U, pageup/ctrl-pageup
	     IF Line.Number > 1 THEN
                IF Line.Number - 22 > 1 THEN
                   Line.Number = Line.Number - 22
		ELSE
		   Line.Number = 1
		END IF
		Xcoor = 1
		End.Line = LEN(Temp.ArrayS(Line.Number)) + 1
		IF Column + Ycoor - 1 > End.Line THEN
		   Ycoor = End.Line - Column + 1
		   IF Ycoor <= 0 THEN
		      Ycoor = End.Line
		      IF Column > 1 THEN
			 Var = True
		      END IF
		      Column = 1
		   END IF
		END IF
		GOSUB Display.Screen
	     END IF
          CASE 81, 118 ' ^N, pagedown/ctrl-pagedn
	     IF Line.Number < Max.Lines THEN
		IF Line.Number + 22 < Max.Lines THEN
		   Line.Number = Line.Number + 22
                   Xcoor = 1
		ELSE
		   Line.Number = Max.Lines
                   IF Max.Lines < 22 THEN
                      Xcoor = Max.Lines
                   ELSE
                      Xcoor = 22
                   END IF
		END IF
		End.Line = LEN(Temp.ArrayS(Line.Number)) + 1
		IF Column + Ycoor - 1 > End.Line THEN
		   Ycoor = End.Line - Column + 1
		   IF Ycoor <= 0 THEN
		      Ycoor = End.Line
		      IF Column > 1 THEN
			 Var = True
		      END IF
		      Column = 1
		   END IF
		END IF
		GOSUB Display.Screen
	     END IF
          CASE 82 ' ^V, insert
             If Line.Number > 0 Then
                ' row 24
                CALL Modem.ANSI(4, 33, 0)
                CALL ScreenANSI(4, 33, 0)
                CALL Modem.ANSI(4, 44, 30)
                CALL ScreenANSI(4, 44, 30)
                CALL Modem.ANSI(2, 24, 1)
                CALL ScreenANSI(2, 24, 1)
                Strng = SPACE$(41)
                CALL Scrn.Output(Strng)
                CALL Modem.ANSI(2, 24, 1)
                CALL ScreenANSI(2, 24, 1)
                Strng = "Enter 3-digit ascii code: "
                CALL Scrn.Output(Strng)
                Var = False
                Ascii$ = Nul
                DO While Lost.Carrier=False
                   Var$ = Nul
                   DO While Lost.Carrier=False
                      Var$ = Inkey$
                      If Len(Var$) Then
                         Exit Do
                      Endif
                      If Local.Mode = False Then
                         Call Check.Carrier
                         Call Get.Modem
                         If Len(Buffer) Then
                            Var$ = Left$(Buffer,1)
                            Buffer = Mid$(Buffer,2)
                            Exit Do
                         Endif
                      Endif
                      If Local.Mode Then
                         InregsX.AX=&H1680
                         InregsX.BX=&H0000
                         Call InterruptX(&H2F,InregsX,OutregsX)
                      Endif
                   LOOP
                   If Len(Var$) = 1 Then
                      If Var$=Chr$(8) Then
                         If Len(Ascii$)>0 Then
                            Ascii$=Left$(Ascii$,Len(Ascii$)-1)
                            Gosub Display.Ascii.Line
                            Var=Var-1
                            CALL Modem.ANSI(2, 24, 26)
                            CALL ScreenANSI(2, 24, 26)
                            Strng = Ascii$ + " "
                            CALL Scrn.Output(Strng)
                            CALL Modem.ANSI(2, 24, 26)
                            CALL ScreenANSI(2, 24, 26)
                            Strng = Ascii$
                            CALL Scrn.Output(Strng)
                         Endif
                      Endif
                      If Var$=Chr$(13) Then
                         Exit Do
                      Endif
                      If Lcase$(Var$) = "x" Or Lcase$(Var$)="o" Or Lcase$(Var$)="b" Then
                         If Var=0 Then
                            Ascii$ = Ascii$ + Lcase$(Var$)
                            Gosub Display.Ascii.Line
                            Var = Var + 1
                            CALL Modem.ANSI(2, 24, 26 + Var)
                            CALL ScreenANSI(2, 24, 26 + Var)
                            Strng = Lcase$(Var$)
                            CALL Scrn.Output(Strng)
                         Endif
                      Endif
                      If Lcase$(Var$) >= "a" And Lcase$(Var$) <= "f" Then
                         If Var>=1 And Var<3 Then
                            If Left$(Ascii$,1)="x" Then
                               Var = Var + 1
                               CALL Modem.ANSI(2, 24, 26 + Var)
                               CALL ScreenANSI(2, 24, 26 + Var)
                               Strng = Var$
                               CALL Scrn.Output(Strng)
                               Ascii$ = Ascii$ + Var$
                            Endif
                         Endif
                      Endif
                      If Left$(Ascii$,1)="o" Then
                         If Var$ >= "0" And Var$ <= "7" Then
                            If Var<4 Then
                               Var = Var + 1
                               CALL Modem.ANSI(2, 24, 26 + Var)
                               CALL ScreenANSI(2, 24, 26 + Var)
                               Strng = Var$
                               CALL Scrn.Output(Strng)
                               Ascii$ = Ascii$ + Var$
                            Endif
                         Endif
                      Else
                         If Left$(Ascii$,1)="b" Then
                            If Var$ = "0" Or Var$ = "1" Then
                               If Var<9 Then
                                  Var = Var + 1
                                  CALL Modem.ANSI(2, 24, 26 + Var)
                                  CALL ScreenANSI(2, 24, 26 + Var)
                                  Strng = Var$
                                  CALL Scrn.Output(Strng)
                                  Ascii$ = Ascii$ + Var$
                               Endif
                            Endif
                         Else
                            If Var$ >= "0" And Var$ <= "9" Then
                               If Var<3 Then
                                  Var = Var + 1
                                  CALL Modem.ANSI(2, 24, 26 + Var)
                                  CALL ScreenANSI(2, 24, 26 + Var)
                                  Strng = Var$
                                  CALL Scrn.Output(Strng)
                                  Ascii$ = Ascii$ + Var$
                               Endif
                            Endif
                         Endif
                      Endif
                   Endif
                LOOP

                ' reset color
                CALL Modem.ANSI(1, 37, 7)
                CALL ScreenANSI(1, 37, 7)
                CALL Modem.ANSI(4, 37, 15)
                CALL ScreenANSI(4, 37, 15)

                ' insert ascii character
                If Left$(Ascii$,1)="x" Then
                   Ascii.Value=Int(Val("&H"+Mid$(Ascii$,2)))
                Else
                   If Left$(Ascii$,1)="o" Then
                      Ascii.Value=Int(Val("&O"+Mid$(Ascii$,2)))
                   Else
                      If Left$(Ascii$,1)="b" Then
                         Ascii$=Mid$(Ascii$,2)
                         Gosub Binary.String
                      Else
                         Ascii.Value = Int(Val(Ascii$))
                      Endif
                   Endif
                Endif
                If Ascii.Value >= 0 And Ascii.Value <= 255 Then
                   TempX$ = Chr$(Ascii.Value)
                   T$ = Temp.ArrayS(Line.Number)
                   T$ = LEFT$(T$, Column + Ycoor - 2) + TempX$ + MID$(T$, Column + Ycoor - 1)
                   Temp.ArrayS(Line.Number) = T$

                   IF Column + Ycoor - 1 <= LEN(Temp.ArrayS(Line.Number)) THEN
                      IF Ycoor < 80 THEN
                         Ycoor = Ycoor + 1
                         GOSUB Display.Screen
                      ELSE
                         Column = Column + 1
                         GOSUB Display.Screen
                      END IF
                   END IF
                Endif
             Endif
	  CASE 116 ' control-right
             IF Column + Ycoor - 1 > LEN(Temp.ArrayS(Line.Number)) THEN ' eol
                If Line.Number + 1 <= Max.Lines Then
                   Line.Number = Line.Number + 1
                   IF Xcoor = 22 THEN
                      Var = True
                   ELSE
                      Xcoor = Xcoor + 1
                   END IF
                   Ycoor = 1
                   Column = 1
                   GOSUB Display.Screen
                Endif
             Else
                P = 0
                FOR Temp2 = Column + Ycoor - 1 TO LEN(Temp.ArrayS(Line.Number))
                   P = P + 1
                   IF MID$(Temp.ArrayS(Line.Number), Temp2, 1) = " " THEN
                      IF MID$(Temp.ArrayS(Line.Number), Temp2 + 1, 1) <> " " THEN
                         EXIT FOR
                      END IF
                   END IF
                NEXT
                'shift right
                FOR Var3 = 1 TO P
                   IF Ycoor < 80 THEN
                      Ycoor = Ycoor + 1
                   ELSE
                      Column = Column + 1
                   END IF
                NEXT
                GOSUB Display.Screen
             Endif
	  CASE 115 ' control-left
             If Column + Ycoor - 1 <= 1 Then
                If Line.Number > 1 Then
                   Line.Number = Line.Number - 1
                   IF Xcoor = 1 THEN
                      Var = True
                   ELSE
                      Xcoor = Xcoor - 1
                   END IF
                   End.Line = LEN(Temp.ArrayS(Line.Number))
                   IF End.Line >= 80 THEN
                      Column = End.Line - 78
                      Ycoor = End.Line - Column + 2
                   ELSE
                      Ycoor = End.Line - Column + 2
                   END IF
                   GOSUB Display.Screen
                Endif
             Else
                P = 0
                V = 0
                FOR Temp2 = Column + Ycoor - 3 TO 1 STEP -1
                   P = P + 1
                   IF MID$(Temp.ArrayS(Line.Number), Temp2, 1) = " " THEN
                      IF MID$(Temp.ArrayS(Line.Number), Temp2 + 1, 1) <> " " THEN
                         V = -1
                         EXIT FOR
                      END IF
                   END IF
                NEXT
                IF V = 0 THEN
                   P = P + 1
                END IF
                ' shift left
                FOR Var3 = 1 TO P
                   IF Ycoor > 1 THEN
                      Ycoor = Ycoor - 1
                   ELSE
                      IF Column > 1 THEN
                         Column = Column - 1
                      END IF
                   END IF
                NEXT
                GOSUB Display.Screen
             Endif
	  CASE 119 ' control-home
             If Max.Lines > 0 Then
                Line.Number = 1
                Xcoor = 1
                Ycoor = 1
                Column = 1
                GOSUB Display.Screen
             Endif
	  CASE 117 ' control-end
             If Max.Lines > 0 Then
                Line.Number = Max.Lines
                IF Max.Lines < 22 THEN
                   Xcoor = Max.Lines
                ELSE
                   Xcoor = 22
                END IF
                Ycoor = 1
                Column = 1
                GOSUB Display.Screen
             Endif
	  END SELECT
       END SELECT
    LOOP
    CALL Modem.ANSI(1, 37, 7)
    CALL ScreenANSI(1, 37, 7)

    CALL Modem.ANSI(3, 0, 0)
    CALL ScreenANSI(3, 0, 0)

    Call Edit.Status.Line

    CALL Modem.ANSI(2, 24, 1)
    CALL ScreenANSI(2, 24, 1)

    Locate Max.Row-1,1,1
    Strng = "Store " + Quote$ + OutX$ + Quote$ + " to disk(y/n)? "
    No.Input.Out="Y"
    CALL IO.I
    IF Yes THEN
       Strng = "Store file in Linux form(y/n)?"
       No.Input.Out="N"
       CALL IO.I
       Store.Linux=Yes
       CLOSE #TempFile1
       If Dir$(FileName)<>"" Then
          Kill FileName
       Endif
       OPEN FileName FOR BINARY AS #TempFile1
       Var!=0!
       FOR Count = 1 TO Max.Lines
          FOR Count2=1 TO LEN(Temp.ArrayS(Count))
             VarX=Mid$(Temp.ArrayS(Count),Count2,1)
             Var!=Var!+1!
             Put TempFile1,Var!,VarX
          NEXT
          If Store.Linux=0 Then
             Var!=Var!+1!
             VarX=Chr$(13)
             Put TempFile1,Var!,VarX
          Endif
          Var!=Var!+1!
          VarX=Chr$(10)
          Put TempFile1,Var!,VarX
          'PRINT #TempFile1, Temp.ArrayS(Count)
       NEXT
       Strng = "File " + Quote$ + OutX$ + Quote$ + " stored to disk"
       If Linux Then Strng=Strng+" in linux form"
       Strng=Strng+"."
       CALL IO.O
    END IF
    ERASE Temp.ArrayS
 LOOP
 EXIT SUB
Display.Ascii.Line:
 CALL Modem.ANSI(4, 33, 0)
 CALL ScreenANSI(4, 33, 0)
 CALL Modem.ANSI(4, 44, 30)
 CALL ScreenANSI(4, 44, 30)
 CALL Modem.ANSI(2, 24, 1)
 CALL ScreenANSI(2, 24, 1)
 Strng = SPACE$(41)
 CALL Scrn.Output(Strng)
 CALL Modem.ANSI(2, 24, 1)
 CALL ScreenANSI(2, 24, 1)
  If Left$(Ascii$,1)="x" Then
     Var2$ = "Enter 2-digit ascii code: " 
  Else
     If Left$(Ascii$,1)="o" Then
        Var2$ = "Enter 3-digit ascii code: "
     Else
        If Left$(Ascii$,1)="b" Then
           Var2$ = "Enter 8-digit ascii code: "
        Else
           Var2$ = "Enter 3-digit ascii code: " 
        Endif
     Endif
  Endif
  CALL Scrn.Output(Var2$)
RETURN

Binary.String:
  Ascii.Value = False
  Binary.Power = False
  FOR Binary.Digit = LEN(Ascii$) TO 1 STEP -1
      OutX2$ = MID$(Ascii$, Binary.Digit, 1)
      SELECT CASE OutX2$
          CASE "0"
              ' nul
          CASE "1"
              Ascii.Value = Ascii.Value + 2 ^ Binary.Power
      END SELECT
      Binary.Power = Binary.Power + 1
  NEXT
RETURN

' display current edit screen
Display.Screen:
 CALL Modem.ANSI(1, 37, 7)
 CALL ScreenANSI(1, 37, 7)

 CALL Modem.ANSI(3, 0, 0)
 CALL ScreenANSI(3, 0, 0)

 CALL Modem.ANSI(2, 1, 1)
 CALL ScreenANSI(2, 1, 1)

 CALL Modem.ANSI(4, 37, 15)
 CALL ScreenANSI(4, 37, 15)

 ' display edit lines
 Count = 0
 If Max.Lines = 0 Then
    Count = 1
 Endif
 If Max.Lines > 0 Then
    DO
       Count = Count + 1
       IF Count = 24 THEN
          EXIT DO
       END IF
       IF Count + Line.Number - Xcoor > Max.Lines THEN
          EXIT DO
       END IF
       CALL Modem.ANSI(2, Count, 1)
       CALL ScreenANSI(2, Count, 1)
       Strng = MID$(Temp.ArrayS(Count + Line.Number - Xcoor), Column)
       IF LEN(Strng) > 80 THEN
          Strng = LEFT$(Strng, 80)
       END IF
       CALL Scrn.Output(Strng)
    LOOP
 Endif

 ' eof marker
 IF Count < 24 THEN
    CALL Modem.ANSI(2, Count, 1)
    CALL ScreenANSI(2, Count, 1)
    Strng = "<end of file>"
    CALL Scrn.Output(Strng)
 END IF

 ' row 24
 CALL Modem.ANSI(4, 33, 0)
 CALL ScreenANSI(4, 33, 0)

 CALL Modem.ANSI(4, 44, 30)
 CALL ScreenANSI(4, 44, 30)

 CALL Modem.ANSI(2, 24, 1)
 CALL ScreenANSI(2, 24, 1)

 Strng = SPACE$(79)
 CALL Scrn.Output(Strng)

 ' eol char
 Color 0, 1
 Locate 24,80,0
 Print " ";
 Locate 23,1,1

 ' row 24
 CALL Modem.ANSI(4, 33, 0)
 CALL ScreenANSI(4, 33, 0)

 CALL Modem.ANSI(4, 44, 30)
 CALL ScreenANSI(4, 44, 30)

 ' row 24 filename
 CALL Modem.ANSI(2, 24, 42)
 CALL ScreenANSI(2, 24, 42)

 ' filename
 Strng = "File: " + OutX$
 If Linux Then
    Strng=Strng+" (binary)"
 Endif
 CALL Scrn.Output(Strng)

 ' reset color
 CALL Modem.ANSI(4, 37, 15)
 CALL ScreenANSI(4, 37, 15)

 ' row 25 (local)
 Call Edit.Status.Line
 RETURN
Error.Resume72:
 Goto 101
Error.Trap72:
 Error.Num = Err
 Erase Temp.ArrayS
 Select Case Error.Num
 Case 7
    Strng="Out of memory."
 Case 9
    Strng="Subscript out of range."
 Case 14
    Strng="Out of string space."
 Case 53
    Strng="File not found."
 Case 64
    Strng="Bad filename."
 Case 76
    Strng="Path not found."
 Case Else
    Strng="Error"+Str$(Err)+" loading file."
 End Select
 Call IO.O
 Resume Error.Resume72
End Sub

Sub Boot.Usage
 Call Clear.Status.Line
 Call Save.Screen2(-1)
 Print
 Color 15
 Print "Editcmdg - full screen editor."
 Color 14
 Print "Usage:"
 Color 15
 Print " Editcmdg [/linux] <filename>"
 Color 14
 Print "Where:"
 Color 15
 Print " /linux reads binary file."
 Print " Filename is the file to load."
 Color 7
 End
End Sub

Sub Amount(Var#,Var$)
 IF Var# = 0# THEN
    Var$ = " 0"
 ELSE
    If Instr(Str$(Var#),"D") Then
       Var$=Str$(Var#)
    Else
       Var$=" "+Form$(Var#)
    Endif
 END IF
End Sub
