[img] Line Editor
 MDFS::Info.Comp.Spectrum.Code.Editors.editor/htm Search  

The BBC and Amstrad CPC line editor have a copy cursor that lets you copy characters from elsewhere on the
screen. The CPC also lets you move the text cursor within the input line. This code replicates this for the
Spectrum, along with three line delete functions: delete to start, delete to end, and delete whole line.

The routine is called with GOSUB 1000. It returns the entered line in a$ when ENTER is pressed, or returns a$
set to CHR$27 if SHIFT-SYMBOL is pressed, as an Escape key.

You can download the routine as plain text editor.txt (2K) and Spectrum Tapefile editor.tap (3K).

Keyboard Layout

The editor routine uses the following keyboard layout.
              Spectrum keyboard layout with JGH line editor
              =============================================

                            <-- COPY CURSOR MOVEMENT -->
+-----++-----++-----++-----++-----++-----++-----++-----++-----++-----+
|COPY ||CAPS || DEL>||LEFT ||LEFT ||DOWN || UP  ||RIGHT||RIGHT||<DEL |
|  !  ||  @  ||  #  ||  $  ||  %  ||  &  ||  '  ||  (  ||  )  ||  _  |
|  1  ||  2  ||  3  ||  4  ||  5  ||  6  ||  7  ||  8  ||  9  ||  0  |
+-----++-----++-----++-----++-----++-----++-----++-----++-----++-----+
  +-----++-----++-----++-----++-----++-----++-----++-----++-----++-----+
  |  Q  ||  W  ||  E  ||  R  ||  T  ||  Y  ||  U  ||  I  ||  O  ||  P  |
  |DEL<-||<DEL>||DEL->||  <  ||  >  ||  [  ||  ]  || (C) ||  ;  ||  "  |
  |  q  ||  w  ||  e  ||  r  ||  t  ||  y  ||  u  ||  i  ||  o  ||  p  |
  +-----++-----++-----++-----++-----++-----++-----++-----++-----++-----+
     +-----++-----++-----++-----++-----++-----++-----++-----++-----++-----+
     |  A  ||  S  ||  D  ||  F  ||  G  ||  H  ||  J  ||  K  ||  L  ||ENTER|
     |  ~  ||  |  ||  \  ||  {  ||  }  ||  ^  ||  -  ||  +  ||  =  ||ENTER|
     |  a  ||  s  ||  d  ||  f  ||  g  ||  h  ||  j  ||  k  ||  l  ||ENTER|
     +-----++-----++-----++-----++-----++-----++-----++-----++-----++-----+
+--------++-----++-----++-----++-----++-----++-----++-----++-----++--------+
|        ||  Z  ||  X  ||  C  ||  V  ||  B  ||  N  ||  M  || ESC || BREAK  |
|  ESC   ||  :  ||  `  ||  ?  ||  /  ||  *  ||  ,  ||  .  ||     || SPACE  |
| SHIFT  ||  z  ||  x  ||  c  ||  v  ||  b  ||  n  ||  m  || SYM || SPACE  |
+--------++-----++-----++-----++-----++-----++-----++-----++-----++--------+

Notes:
     Top line: Shift+key            SS-Q Deletes to start of line
  Middle line: Symbol+key           SS-W Deletes whole line
  Bottom line: unshifted key        SS-E Deletes to end of line

Line Editor Routine

 1000 REM Line editor
 1010 LETflg=0:LETcopy=0:LETcopx=0:LETcop=0:LETa$="":LETb$=""
 1030 PRINTOVER1;CHR$143;CHR$8;
 1040 GOSUB1150:REM Wait for a keypress
 1041 IFcop=0THENLETcopx=33-PEEK23688:LETcopy=24-PEEK23689
 1050 IFq$<" "THENGOTO1100       :REM Deal with control codes
 1060 IFq$>CHR$127THENGOSUB1200  :REM Convert symbols
 1070 POKE23692,255:POKE16415,255:PRINTq$;:GOSUB1120:PRINTb$;:IFflg=40THENPRINT" ";
 1075 GOSUB1130
 1076 IFPEEK16415<>255THENFORz=1TO32:PRINTCHR$8;:NEXTz
 1080 LETa$=a$+q$
 1090 GOTO1030
 1091 :
 1092 :
 1093 PRINTb$;" ":LETa$=CHR$27:RETURN
 1095 PRINTb$;" ":LETa$=a$+b$:RETURN
 1100 LETflg=0:GOSUB1100+50*CODEq$
 1110 GOTO1030+flg:REM Loop back to a point specified by flg
 1119 :
 1120 REM Save cursor positions
 1121 LETp1=PEEK23684:LETp2=PEEK23685:LETp3=PEEK23688:LETp4=PEEK23689:RETURN
 1129 :
 1130 REM Restore cursor positions
 1131 POKE23684,p1:POKE23685,p2:POKE23688,p3:POKE23689,p4:RETURN
 1140 :
 1150 REM Wait for a keypress
 1155 IFcop=1THENGOSUB1120:PRINTATcopy,copx;OVER1;INK8;PAPER8;BRIGHT8;FLASH1;" ";:GOSUB1130
 1160 PAUSE50
 1170 LETq$=INKEY$:IFq$=""THENGOTO1170
 1175 IFcop=1THENGOSUB1120:PRINTATcopy,copx;OVER1;INK8;PAPER8;BRIGHT8;FLASH0;" ";:GOSUB1130
 1180 RETURN
 1195 :
 1199 REM Convert symbols
 1200 LETq$=(CHR$127+"|!][123!}{\~")(CODEq$-193+22*(q$<CHR$195)-20*(q$>CHR$205)):IFq$>"@"THENRETURN
 1205 PRINTOVER1;CHR$128;CHR$8;
 1206 IFq$="1"THENGOTO1250
 1207 IFq$="3"THENGOTO1240
 1210 LETq$="":IFb$=""THENRETURN
 1220 FORz=1TOLENb$:PRINT" ";:NEXTz:FORz=1TOLENb$:PRINTCHR$8;:NEXTz:LETb$="":RETURN
 1240 PRINTb$;:LETa$=a$+b$:LETb$=""
 1250 LETq$="":IFa$=""THENRETURN
 1255 FORz=1TOLENa$
 1260   PRINTCHR$8;:GOSUB1120:PRINTb$;" ";:GOSUB1130
 1265 NEXTz:LETa$=""
 1290 RETURN
 1295 :
 1300 REM Shift-3 - Forwards delete
 1301 IFb$=""THENBEEP1/10,0:LETflg=10:RETURN
 1310 LETb$=b$(2TO):PRINTOVER1;CHR$128;CHR$8;
 1320 LETq$="":LETflg=40:RETURN
 1340 :
 1350 REM Shift-4 - Back one
 1351 IFa$=""THENLETflg=10:RETURN
 1360 PRINTOVER1;CHR$143;CHR$8;CHR$8;
 1370 LETb$=a$(LENa$)+b$:LETa$=a$(TOLENa$-1)
 1380 RETURN
 1390 :
 1400 REM Shift-2 - Caps lock
 1410 LETcaps=INT(PEEK23658/8):LETcaps=1-(caps-2*INT(caps/2)):LETflg=10:POKE23658,8*caps
 1420 RETURN
 1440 :
 1450 REM Shift-1 - Edit
 1451 LETq$=SCREEN$(copy,copx)
 1460 LETcop1=cop:GOSUB1550:LETcop=cop1:LETflg=11:RETURN
 1470 :
 1490 LETflg=10:LETcop=1:RETURN
 1495 :
 1500 REM Shift-5 - Left
 1501 LETcopx=copx-1:IFcopx>-1THENGOTO1490
 1510 LETcopx=31:GOTO1650
 1540 :
 1550 REM Shift-8 - Right
 1551 LETcopx=copx+1:IFcopx<32THENGOTO1490
 1560 LETcopx=0:REM Continue in to...
 1570 :
 1600 REM Shift-6 - Down
 1601 LETcopy=copy+1:IFcopy>21THENLETcopy=0
 1610 GOTO1490
 1640 :
 1650 REM Shift-6 - Up
 1651 LETcopy=copy-1:IFcopy<0THENLETcopy=21
 1660 GOTO1490
 1690 :
 1700 REM Shift-0 - Delete
 1701 IFa$=""THENBEEP1/10,0:LETflg=10:RETURN
 1710 PRINTCHR$8;"  ";CHR$8;CHR$8;:LETa$=a$(TOLENa$-1)
 1720 LETflg=40:LETq$="":RETURN
 1740 :
 1750 REM Enter - Exit routine
 1751 LETflg=65:RETURN
 1790 :
 1800 REM Shift-Shift - Escape
 1810 LETflg=63:RETURN
 1840 :
 1850 REM Shift-9 - Forward one
 1851 IFb$=""THENLETflg=10:RETURN
 1860 LETa$=a$+b$(1):LETb$=b$(2TO)
 1870 PRINTOVER1;CHR$143;
 1880 RETURN

Best viewed with Any Browser Valid HTML 4.0! Authored by J.G.Harston
Last update: 14-Jun-2005