MDFS::Info.Comp.Spectrum.ProgTips.editor/htm Search

Line Editor

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 bit of 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 used 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

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

Hosted by Force9 Internet - Domain registration by EasySpace - Authored by J.G.Harston
Last update: 14-Jun-2005