[img] Basic Line Editor
 MDFS::Info.Comp.Spectrum.ProgTips.Editors.LineEdit/htm Search  

The Spectrum INPUT command uses the bottom of the screen. On most other platforms, you can use INPUT
anywhere on the screen. This code provides a simple line editor to allow you to enter characters with the input
line displayed anywhere in the main screen.

DELETE deletes the previous character, ENTER returns the current line. All other control codes are ignored. All
symbol characters can be entered by pressing Symbol-Shift and the relevant key. © is entered with Symbol-Shift-I.
The text is displayed where-ever the current cursor is, so you can display a prompt before calling, for instance
PRINT "Enter filename: ";:GOSUB 9500


This allows no limit to the number of characters entered.

Call with:   GOSUB 9500
Returns:    a$ holds the text after ENTER is pressed, k$ corrupted
Download: lineedit.tap (4K).

9500 REM Simple line editor, returns a$=text entered
9505 LET a$=""
9520 PRINT CHR$143;CHR$8;
9530 PAUSE 0:LET k$=INKEY$:IF k$="" THEN GOTO 9530
9535 IF k$=CHR$6 THEN IF PEEK 23658<9 THEN POKE 23658,8-PEEK 23658
9540 IF k$=CHR$12 THEN IF LEN a$ THEN LET a$=a$(TO LENa$-1):PRINT " ";CHR$8;CHR$8;:GOTO 9520
9550 IF k$=CHR$13 THEN PRINT " ":RETURN
9570 IF k$>CHR$172 THEN LET k$="|*][<=>=<>*}{\~©"(CODE k$-194-20*(k$>CHR$220)+35*(k$<CHR$192))
9575 REM In the above line,        <= >= <>    ©   are the Spectrum characters
9580 IF k$>=" " AND k$<CHR$128 THEN LET a$=a$+k$:PRINT k$;
9590 GOTO 9520

This variant allows you to specify a maximum line length.

Call with:   GOSUB 9500
or             LET max=<linelength>:GOSUB 9505
Returns:    a$ holds the text after ENTER is pressed, k$ corrupted, max preserved
Download: lineedit.tap (4K).

9500 REM Simple line editor, returns a$=text entered, allows max to be set
9504 LET max=768
9505 LET a$=""
9520 PRINT CHR$143;CHR$8;
9530 PAUSE 0:LET k$=INKEY$:IF k$="" THEN GOTO 9530
9535 IF k$=CHR$6 THEN IF PEEK 23658<9 THEN POKE 23658,8-PEEK 23658
9540 IF k$=CHR$12 THEN IF LEN a$ THEN LET a$=a$(TO LENa$-1):PRINT " ";CHR$8;CHR$8;:GOTO 9520
9550 IF k$=CHR$13 THEN PRINT " ":RETURN
9560 IF LEN a$>=max THEN GOTO 9530
9570 IF k$>CHR$172 THEN LET k$="|*][<=>=<>*}{\~©"(CODE k$-194-20*(k$>CHR$220)+35*(k$<CHR$192))
9575 REM In the above line,        <= >= <>    ©   are the Spectrum characters
9580 IF k$>=" " AND k$<CHR$128 THEN LET a$=a$+k$:PRINT k$;
9590 GOTO 9520

This allows you to specify a maximum line length and a pad character.

Call with:   GOSUB 9500
or             LET max=<linelength>:GOSUB 9505
or             LET max=<linelength>:LET pad=<character>:GOSUB 9510
Returns:    a$ holds the text after ENTER is pressed, k$, pad corrupted, max preserved
Download: lineedit.tap (4K).

9500 REM Simple line editor, returns a$=text entered, allows max and pad to be set
9504 LET max=768
9505 LET pad=0
9510 LET a$=""
9518 IF pad THEN FOR a=1 TO max:PRINT CHR$pad;:NEXT a:FOR a=1 TO max:PRINT CHR$8;:NEXT a
9519 IF pad=0 THEN LET pad=32
9520 PRINT CHR$143;CHR$8;
9530 PAUSE 0:LET k$=INKEY$:IF k$="" THEN GOTO 9530
9535 IF k$=CHR$6 THEN IF PEEK 23658<9 THEN POKE 23658,8-PEEK 23658
9540 IF k$=CHR$12 THEN IF LEN a$ THEN PRINT (CHR$pad+" ")(1+(LENa$=max));CHR$8;CHR$8;:LET a$=a$(TO LENa$-1):GOTO 9520
9550 IF k$=CHR$13 THEN PRINT (CHR$pad+" ")(1+(LENa$=max)):RETURN
9560 IF LEN a$>=max THEN GOTO 9530
9570 IF k$>CHR$172 THEN LET k$="|*][<=>=<>*}{\~©"(CODE k$-194-20*(k$>CHR$220)+35*(k$<CHR$192))
9575 REM In the above line,        <= >= <>    ©   are the Spectrum characters
9580 IF k$>=" " AND k$<CHR$128 THEN LET a$=a$+k$:PRINT k$;
9590 GOTO 9520

To show the state of the CAPS LOCK, change line 9520 to:

9520 PRINT FLASH 1;CHR$(76-(PEEK 23658)/8*9);CHR$8;

Best viewed with Any Browser Valid HTML 4.0! Authored by J.G.Harston
Last update: 12-Nov-2019