Tag: Altair BASIC

  • EDIT.BAS

    EDIT.BAS

    Text Editor Prevents “Starvation” and Programming Hassles

    By Donald Fitchhorn

    MITS

    Computer Notes, October 1977.

    Why I wrote an editor

    My main objective in writing an editor was to complement DISK EXTENDED BASIC’s built-in EDIT feature. This feature is invaluable if the location of a needed change is already known. But what about our friend Bob’s problem? He knew WHAT the problem was but not WHERE to find it. What he needs is a program that will search through the entire file until it finds what he wants. Since there isn’t such an editor in Disk Extended BASIC, I wrote my own in BASIC so that it can be easily changed.

    The search command and others like it where the beginnings of my editor, which now has 14 commands. But before we get into an explanation of this editor, let’s review the definition of an editor.

    What is an editor?

    An editor is a program which permits the addition and deletion of lines and characters in one file to make another file. The second file is the new up-to-date file, and the first file is retained as the backup file. Some editors permit the creation of new files and/or use multiple input files. An editor can be as extensive or as minimal as is necessary for an application.

    PROGRAM FILES

    The EDIT program works on ASCII program files. A program file is any file that looks like a program to BASIC. (see EXAMPLE 1.) BASIC doesn’t care if the whole file is REMark statements; it’s only concerned with whether or not there is a line number at the beginning of each line. (See EXAMPLE 2.)

    (NOTE: The EDIT program cannot handle files saved in binary. Save all files in ASCII, i.e. SAVE "FILENAME", 0, A)

    EXAMPLE #1

    10 'THIS IS A PROGRAM FILE
    20 FOR I = 1 TO 100000
    30 PRINT I;RND(I)*1000;I*RND(I)
    40 ' THIS PRINTS SOME NUMBERS
    50 NEXT
    60 END
    

    EXAMPLE #2

    10 'This is a document program file
    20 'here is the
    30 'text of the
    40 'document !
    50 'this is the end.
    
    EXAMPLE #3
    LOWER CASE IS USER TYPED
    
    run"edit
    EDIT -- VERSION 1.0
    INPUT FILE NAME?time
    >r
    EOF1
    CLEARING..........
    >/1
    5 LPRINT"MINUTES","HUNDREDTHS",,"MINUTES","HUNDREDTHS"
    10 FORI=6TO30STEP-1
    20 J=INT(I/60*100)
    21 K=INT((I/30)/60*100)
    30 LPRINTI,J,,I-30,K
    40 NEXT
    >gJ
    20 J
    >cL2
    30 L2=INT(I/60*100)
    >gJ
    30 LPRINTI,J
    >cL2
    30 LPRINTI,L2,,I-30,K
    >gJ
    EOB
    >x
    BACKUP FILE NAME?time.bak
    OK
    load"time
    OK
    list
    5 LPRINT"MINUTES","HUNDREDTHS",,"MINUTES","HUNDREDTHS"
    10 FOR I=60TO30STEP-1
    20 J=INT(I/60*100)
    21 K=INT((I-30)/60*100)
    30 LPRINTI,J,,I-30,K
    40 NEXT
    OK
    

    Saving documents as programs (EXAMPLE 2 format) allows them to be loaded with BASIC. This allows BASIC to be used to alter, delete, and add lines. Of course, line numbers on the finished document may not be wanted, so a short program that reads the file and PRINTs MID$(LINE$, INSTR(LINE$,"'")+1 will print everything to the right of the (‘). Be sure to use LINE INPUT instead of INPUT when reading up LINE$ to prevent truncation because of commas in the text. The (‘)’s in EXAMPLE 2 are necessary if the program file is to be loaded and saved by BASIC. Without the (‘), BASIC will modify the text line.

    BASIC won’t allow lines to be moved around within the file without retyping each line. But with the EDIT program, line numbers can be changed to whatever is wanted. When the edited file is loaded into BASIC, BASIC will put the lines in numerical order.

    Internal structure

    The EDIT program maintains a double-linked list in memory. Each line (L1$(X)) has a pointer to the previous line [M1(X,0)] and to the next line [M1(X,1)] added to it when it is read in. The array (L1$) that the lines are kept in is divided into two parts — ACTIVE CELLS, which have data in them, and INACTIVE CELLS, available for use as data lines. Deleted lines are linked into the INACTIVE CELLS from the ACTIVE CELLS. Inserted lines are written into the first available INACTIVE CELL and then linked into the ACTIVE CELLS. Pointers are maintained for FIRST ACTIVE CELL (LN), FIRST INACTIVE CELL (IN), DOT or position within cell (H) and CELL that DOT is in (J).

    Commands

    My editor, like many others, uses a single letter to select a command. For example, A will advance DOT one line. Most commands may be preceded by a number or a slash (/) to indicate that hey should be executed more than once. 13A will advance DOT 13 lines. OA will position DOT at the beginning of the current line. /A will advance DOT to the end of the page. All commands that allow a prefix will default to one if none is specified. The following is an explanation of the commands. (See TABLE 1 for a list of commands in alphabetical order. See TABLE 2 for a list divided into four main groups).

    TABLE #1
    
                        |-------OPERATES ON --------|--- ALLOWED ---|
    COMMAND DESCRIPTION | LINE | CHAR | DOT  | FILE | 0 | # |-# | / |
    --------------------|------|------|------|------|---|---|---|---|
       A    ADVANCE     |      |      |  *   |      | * | * | * | * |
    --------------------|------|------|------|------|---|---|---|---|
       B    BEGINNING   |      |      |  *   |      |   |   |   |   |
    --------------------|------|------|------|------|---|---|---|---|
       C    CHANGE      |      |  *   |      |      |   |   |   |   |
    --------------------|------|------|------|------|---|---|---|---|
       D    DELETE      |      |  *   |      |      |   | * |   |   |
    --------------------|------|------|------|------|---|---|---|---|
       E    END         |      |      |  *   |      |   |   |   |   |
    --------------------|------|------|------|------|---|---|---|---|
       G    GET         |      |  *   |      |      |   | * |   |   |
    --------------------|------|------|------|------|---|---|---|---|
       I    INSERT      |  *   |  *   |      |      |   |   |   |   |
    --------------------|------|------|------|------|---|---|---|---|
       J    JUMP        |      |      |  *   |      | * | * | * |   |
    --------------------|------|------|------|------|---|---|---|---|
       K    KILL        |  *   |      |      |      |   | * |   | * |
    --------------------|------|------|------|------|---|---|---|---|
       L    LIST        |  *   |      |      |      |   | * |   | * |
    --------------------|------|------|------|------|---|---|---|---|
       N    NEXT        |      |      |      |  *   |   |   |   |   |
    --------------------|------|------|------|------|---|---|---|---|
       R    READ        |      |      |      |  *   |   |   |   |   |
    --------------------|------|------|------|------|---|---|---|---|
       V    VERIFY      |  *   |      |      |      |   |   |   |   |
    --------------------|------|------|------|------|---|---|---|---|
       X    EXIT        |      |      |      |  *   |   |   |   |   |
    --------------------|------|------|------|------|---|---|---|---|
    
    

    TABLE #2
    COMMANDS THAT AFFECT LINES
    I - Inserts lines until, backslash (\) is entered.
    K - Kills entire line. #K & /K are legal.
    L - Lists entire line. #L & /L are legal.
    V - Prints the current line up to DOT.
        (Verify's the position of DOT).
    
    COMMANDS THAT AFFECT CHARACTERS
    C - Changes last character string gotten with G command to
        something else. Use: Ghere
                             Cthere
        will change here to there.
    D - Delete a character. #D legal.
    G - Get string. Searches for occurrence of string.
        Use: 3Gstuff
        Finds third occurrrence of stuff. #G legal.
    I - Insert characters at DOT. Use: Iabcde
        Will insert abcde in current line at DOT.
    
    COMMANDS THAT MOVE DOT
    A - Moves DOT forward or back the number of lines
        specified. 0A, #A, -#A, /A(same as E) legal.
    B - Moves DOT to beginning of page .
    E - Moves DOT to end of page. (same as /A)
    J - Moves DOT forward or back the number of characters
        specified. 0J, #J, -#J legal.
    
    COMMANDS THAT READ AND WRITE THE FILE
    N - Writes out current page and reads in next page.
    R - Read in a new page.
    X - Writes out current page then reads and writes until
        end of file. Closes and renames files.
    

    How to use edit’s commands

    The commands in EDIT are broken up into four groups, as shown in TABLE 2. The use of these commands will be explained in that order.

    The commands that affect lines will work on one line at a time. Or, in the case of K & L, they may be preceded by a number or slash (/) to indicate that they are to be performed on several succeeding lines. TO begin insertion of lines, type I <return> and then the lines to insert. To tell EDIT that the last line to be inserted has been entered, type backslash ().

    Of the commands that affect characters I & C cannot have a # prefix. #D deletes # characters to the right of DOT. G & C work together to allow getting a string and then changing it to something else. G moved DOT ahead of the Nth occurrence of the string. Then C can be used to change the nth occurrence to a new string. It works like C, except instead of changing one string for another, it inserts a new string ahead of DOT.

    The commands that move DOT are A, B, E, and J. B & E require no other specifiers. They simply move DOT to the beginning or end of the current page. A & J move DOT forward or back a specified number of lines or characters.

    The commands N, R, and X read and write the files. R reads the input file until EOF, until it has read 50 lines, or 2000 characters. It then clears and resets the INACTIVE CELLS. N writes out the current page and then executes and R (reads in the next page). X does a series of N’s until the input file is EOF. Then it closes the files and renames them by giving the input file a backup name and the output file the input file’s old name.

    The best way to learn to use EDIT is to load it in and try a few commands. (See EXAMPLE 3). Once you get the hang of it, the power and versatility will be well worth the time it took t type it in.

    The other side of the coin

    All of the above is really wonderful isn’t it? But this program is not without its limitations.

    1. The commands may or may not work the way the user expects them to. This is a typical problem in any new program because the commands take some getting used to. If, after trying it for a while, the user wants a command to work differently or wants to use another command, just remember that the program is written in BASIC, so modifications are easy.
    2. The program allows working on large files by breaking the file into pages. This works out well except for one thing. No matter what the user does, string space is used. Eventually, all available space will be used. At that time, BASIC has to look through all of the string space, shuffling things around and freeing up no-longer-used bytes so that the program will have some more space. This is commonly referred to as GARBAGE COLLECTING. IT can happen at the most unlikely of times and can take as long as five minutes. Unfortunately to the unsuspecting user, it looks as though the program has bombed BASIC out because CTRL-C and RESET don’t help solve the problem. But patience is rewarded and the program does come back to life.

    Modifications and improvements

    I will leave these up to the reader because they are easy to make. For example, suppose a command is needed to exit the program gracefully without making any changes. Follow these steps:

    1. Pick a command character. How about Q for quit?
    2. Alter line 250 to reflect where the program should go if it sees a Q. Let’s make this 1000.
    3. Put in the necessary code to perform the command. 1000 CLOSE:CLEAR 200:END
    4. Save the new program.

    PROGRAM LISTING

    10 '                 == WRITTEN BY D. L. FITCHHORN ==
    15 '                =   *****  ****   *****  *****   =
    20 '               =    *      *   *    *      *      =
    25 '              =     ***    *   *    *      *       =
    30 '               =    *      *   *    *      *      =
    35 '                =   *****  ****     *      *     =
    40 '                 ==   PROGRAMMER - MITS, INC   ==
    45 '
    50 DEFINT A-Z
    55 CLEAR 15000
    60 DIM L1$(100),L2$(13),M1(100,1):FOR I=0 TO 13:READ L2$(I):NEXT
    65 DATA 
       A - ADVANCE,B - BEGINNING,C - CHANGE,D - DELETE,E - END,G - GET,I - INSERT,
       J - JUMP, K - KILL,L -LIST,N = NEXT,R - READ,V - VERIFY,X - EXIT
    70 PRINT "EDIT -- VERSION 1.0":PRINT
    75 LINE INPUT "INPUT FILE NAME?"; N1$
    80 OPEN "I",1,N1$
    85 N2$="EDIT.TMP"
    90 OPEN "O",2,N2$:PRINT #2,""
    95 I=1:J=1:H=1
    100 '---------------------------------------------------------INPUT COMMAND
    105 K=0
    110 IF A$="" THEN FOR Q9=1 TO 5:PRINT CHR$(7);:NEXT:LINE INPUT ">"; A$
    115 A=0:J1=1:U=0:T=1
    120 IF A$="" THEN 105 ELSE I9=INSTR(A$,"\\"):IF I9<>0 THEN S$=LEFT$(A$,I9-1):
        A$=MID$(A$,I9+1) ELSE S$=A$:A$=""
    125 SS=ASC(S$)
    130 IF 64<SS THEN IF 96<SS THEN SS=SS-32:GOTO 170 ELSE GOTO 170
    135 T=VAL(S$)
    140 S=LEN(STR$(T))
    145 IF T=0 THEN IF LEFT$(S$,1)="/" THEN T=400
    150 S$=MID$(S$,S)
    155 IF T<0 THEN S$=MID$(S$,2)
    160 GOTO 125
    165 '             .A  .B  .C  .D  .E  <F> .G  <H> .I  .J  .K  .L  <M>
    170 ON SS-64 GOTO 195,235,245,265,280,180,290,180,320,360,385,425,180,
                      445,180,180,180,465,180,180,180,515,180,525,180,180
    175 '             .N  <O> <P> <Q> .R  <S> <T> <U> .V  <W> .X  <Y> <Z>
    180 FOR N=0 TO 12:PRINT L2$(N):NEXT:GOTO 105
    185 '
    190 '-----------------------------------------------------------A COMMAND
    195 H=1:IF T=0 THEN GOTO 105 ELSE IF T<0 THEN J1=-1
    200 FOR I3=0 TO T-J1 STEP J1
    205     IF M1(J,0)=0 AND J1=-1 THEN GOTO 105
    210     IF M1(J,1)=-1 AND J1=1 THEN J=M1(J,0):GOTO 105
    215     IF J1=1 THEN J=M1(J,1) ELSE J=M1(J,0)
    220 NEXT
    225 GOTO 105
    230 '-----------------------------------------------------------B COMMAND
    235 J=LN:H=1:GOTO 105
    240 '-----------------------------------------------------------C COMMAND
    245 S$=MID$(S$,2):IF K=0 THEN S=LEN(S$):K=H
    250 IF K=1 THEN L1$(J)=S$+MID$(L1$(J),K+S) ELSE L1$(J)=LEFT$(L1$(J),K-1)+
        S$+MID$(L1$(J),K+S)
    255 PRINT L1$(J):H=K+LEN(S$):GOTO 105
    260 '-----------------------------------------------------------D COMMAND
    265 IF H=1 THEN L1$(J)=MID$(L1$(J),H+T):
        ELSE L1$(J)=LEFT$(L1$(J),H-1)+MID$(L1$(J),H+T)
    270 GOTO 105
    275 '-----------------------------------------------------------E COMMAND
    280 IF M1(J,1)=-1 THEN H=1:GOTO 105 ELSE J=M1(J,1):GOTO 280
    285 '-----------------------------------------------------------G COMMAND
    290 S$=MID$(S$,2):S=LEN(S$):IF S=0 THEN GOTO 105
    295 K=INSTR(H,L1$(J),S$):IF K=0 THEN IF M1(J,1)=-1
        THEN PRINT "EOB":A$="":GOTO 105 ELSE J=M1(J,1):H=1:GOTO 295
    300 U=U+1:IF U<T THEN H=K+S:GOTO 295
    305 PRINT LEFT$(L1$(J),K+S-1):H=K
    310 GOTO 110
    315 '-----------------------------------------------------------I COMMAND
    320 IF MID$(S$,2)<>"" THEN 345 ELSE I2=J:IF J=LN THEN LN=IN
    325 LINE INPUT L1$(IN):IF L1$(IN)"\\" THEN 105
    330 I3=M1(IN,1):M1(IN,1)=I2
    335 M1(IN,0)=M1(I2,0):M1(I2,0)=IN:I4=M1(IN,0)
    340 M1(I4,1)=IN:IN=I3:M1(I3,0)=0:GOTO 325
    345 IF H1=1 THEN L1$(J)=MID$(S$,2)+L1$(J) 
        ELSE L1$(J)=LEFT$(L1$(J),H-1)+MID$(S$,2)+MID$(L1$(J),H)
    350 H=H+LEN(S$):GOTO 105
    355 '-----------------------------------------------------------J COMMAND
    360 IF T=0 THEN H=1:GOTO 105
    365 H=H+T:IF H<1 THEN H=1
    370 IF H>LEN(L1$(J)) THEN H=LEN(L1$(J))
    375 GOTO 105
    380 '-----------------------------------------------------------K COMMAND
    385 H=1:I2=J:I3=M1(J,0):FOR J1=1 TO T
    390 IF M1(J,1)=-1 THEN GOTO 410
    395 I4=M1(J,1):L1$(J)="":M1(I4,0)=I3
    400 M1(IN,0)=J:M1(J,0)=0:M1(J,1)=IN:IN=J
    405 J=I4:NEXT
    410 IF I2=LN THEN LN=J ELSE M1(I3,1)=J
    415 GOTO 105
    420 '-----------------------------------------------------------L COMMAND
    425 I2=J:FOR J1=1 TO T
    430 PRINT L1$(I2):IF M1(I2,1)=-1 THEN GOTO 105 ELSE I2=M1(I2,1):NEXT
    435 GOTO 105
    440 '-----------------------------------------------------------N COMMAND
    445 I2=LN
    450 IF M1(I2,1)=-1 THEN GOTO 465 ELSE PRINT #2,L1$(I2):I2=M1(I2,1)
    455 GOTO 450
    460 '-----------------------------------------------------------R COMMAND
    465 J=1:A#=0:LN=1:I=1:FE=0:GOSUB 470:GOTO 105
    470 IF EOF(1) THEN PRINT "EOF1":I=I-1:FE=1:GOTO 495
    475 LINE INPUT #1,L$:IF L$="" THEN GOTO 470
    480 A#=A#+LEN(L$)
    485 L1$(I)=L$:M1(I,0)=I-1:IF I=1 THEN 490 ELSE M1(I-1,1)=I
    490 IF I=50 OR A#>2000 THEN GOTO 495 ELSE I=I+1:GOTO 470
    495 M1(I,1)=I+1:I=I+1:L1$(I)="END OF BUFFER":M1(I,0)=I-1:M1(I,1)=-1:H=1:IN=I+1
    500 FOR I2=IN TO 100:M1(I2,1)=I2+1:M1(I2,0)=I2-1:NEXT
    505 M1(IN,0)=0:M1(I2-1,1)=-1:RETURN
    510 '-----------------------------------------------------------V COMMAND
    515 PRINT LEFT$(L1$(J),H):GOTO 105
    520 '-----------------------------------------------------------X COMMAND
    525 I2=LN
    530 IF M1(I2,1)=-1 THEN GOTO 535 ELSE PRINT #2,L1$(I2):I2=M1(I2,1):GOTO 530
    535 IF FE=0 THEN I=1:A#=0:GOSUB 470 GOTO 525
    540 CLOSE:ON ERROR GOTO 555:LINE INPUT "BACKUP FILE NAME?";N3$:KILL N3$
    545 NAME N1$ AS N3$
    550 NAME N2$ AS N1$: CLEAR 200:END
    555 IF ERR = 53 THEN GOTO 545: ELSE:ON ERROR GOTO 0