;
; LINE EDITOR MODULE
; MODELED AFTER NORTHSTAR BASIC LINE EDITOR
;
;
	NAM	EDITL
	ENT	LEDIT
	EXT	MOVE,CMPHL,NEGHL,LMOV
;
; 00.00 8/30/79 T.A.S.
; 00.01 8/20/81 RLR - MAYBE NOW IT WILL WORK
; 00.02 9/20/81 RLR - EASY TO USE REWORK
;
DOS	EQU	5
CR	EQU	13
BKSPC	EQU	8
BELL	EQU	7
SPC	EQU	' '
OLDDEL	EQU	'%'
DEL	EQU	07FH
LF	EQU	10
;
; COMES IN WITH LINE BUFFER IN DE
;
LEDIT:	LXI	H,NEWBF	!
	SHLD	NBUFF	; SAVE NEW LINE BUFF. POINTER
	LDAX	D	!
	MOV	M,A	! INIT MAX LENGTH (SAME AS USER BUFF)
	INX	H
	XRA	A
	STA	TOG	! CLEAR INSERT TOGGLE TOO
	MOV	M,A
	INX	H
	MVI	M,CR
	SHLD	CPOINT
;
	XCHG
	SHLD	OBUFF	; AND THE OLD LINE
	INX	H
	MOV	C,M
	INX	H	; POINT TO TEXT
	SHLD	OPOINT
	DCX	H
	INR	C
	INR	C
	DCX	H
	LXI	D,ORIGBF ; SAVE ORIGINAL BUFFER 
	XCHG
	CALL	MOVE
;
	MVI	A,CR	; RR - TERMINATE INPUT WITH CR
	STAX	D
	MOV	M,A
;
	LXI	H,0
	DAD	SP	; PUT RETURN ADDRESS IN COMMAND TABLE
	SHLD 	STAK
;
;
CMD:	CALL	CHRIN	; INPUT-NO ECHO
;
	CPI	SPC	; GREATER THAN OR EQUAL SPACE
	JGE	CNGCHR
	MOV	B,A
	LXI	H,CMDTAB ; POINT TO COMMAND TABLE
	MVI	D,0
	ADD	A	; *2 FOR DOUBLE BYTE
	MOV	E,A	; ADD OFFSET
	DAD	D
	MOV	A,M	; PICKUP COMMAND ADDRESS
	MOV	E,A
	INX	H	; DE @ COMMAND ADDRESS
	MOV	D,M
	ORA	D	; ZERO IS INVALID COMMAND #
	MOV	A,B
	JZ	CNGCHR
	XCHG		; GET IT IN THE RIGHT PLACE
	PCHL		; AND EXECUTE
;
;
; COMMAND TABLE - OF CTL-LETTERS
;			  LETTER
CMDTAB:	DW	0	; @
	DW	RUBOT	; A	; CTL-A = LEFT CURSOR
	DW	0	; B	;    ... 
	DW	0	; C	;    ... 
	DW	RCURS	; D	; CTL-D = RIGHT CURSOR
	DW	0	; E	;    ... 
	DW	FIND	; F	; CTL-F = FIND NEXT CHAR
	DW	0	; G
	DW	RUBOT	; H	; CTL-H BACKSPC + RUBOUT ON NEW LINE
	DW	0	; I - TAB (NORMAL)
	DW	MOVIT	; J	; CTL-J = MOVE REST OF OLD TO NEW
	DW	0	; K
	DW	0	; L
	DW	EXIT	; M	; ON CARRIAGE RETURN QUIT (CTL-M)
	DW	0	; N
	DW	0	; O
	DW	0	; P
	DW	0	; Q
	DW	REDIT	; R	; RE-EDIT LINE
	DW	0	; S
	DW	TOGGLE	; T	; CTL-T	 TOGGLE INSERT
	DW	REDIT	; U	; CTL-U RE-EDIT
	DW	0	; V
	DW	0	; W
	DW	DELOT	; X	; ERASE ONE OLD CHAR
	DW	0	; Y
	DW	0	; Z
	DW	ABORT	; [	(ESC)
	DW	0	; \
	DW	0	; ]
	DW	0	; ^
	DW	DELOT	; UNDERSCORE
;
; END OF COMMAND TABLE
;
	EJECT
;
CRLF:	PUSH	D		; OUTPUT NEW LINE
	MVI	E,CR
	CALL	CHROT
	MVI	E,LF
	CALL	CHROT
	POP	D
	RET
;
; OUTPUT "FILTERED" CHAR TO CONSOLE
;
CHROT1:	PUSH	D
	PUSH	B
	PUSH	PSW
      .IF
	CPI	SPC
	JGE	.ELSE
	CPI	9
	JEQ	.ELSE
      .THEN			; CONTROL CHAR
	MVI	E,'^'
	CALL	CHROT
	POP	PSW
	PUSH	PSW
	ADI	'@'
	MOV	E,A
	CALL	CHROT
      .ELSE			; NORMAL CHAR OR TAB
	MOV	E,A
	CALL	CHROT
      .ENDIF
	POP	PSW
	POP	B
	POP	D
	RET
;
; STRAIGHT OUTPUT
;
CHROT:	PUSH	B
	MVI	C,2
	CALL	DOS
	POP	B
	RET
;
CHRIN:	MVI	C,80H	; GET AN INPUT CHAR
	JMP	DOS
;
; ERROR CHAR
;
ERR:	MVI	E,BELL
	CALL	CHROT
	JMP	CMD
	EJECT
;
; PASSIVE (NORMAL) CHARACTER
;
CNGCHR:	LHLD	CPOINT
	MOV	M,A
	INX	H
	SHLD	CPOINT
	CALL	CHROT1
	MVI	M,CR
;
	LDA	TOG
	ORA	A
	JNZ	CMD
	LHLD	OPOINT
	MOV	A,M	; GET CHAR JUST PAST
	INX	H
	SHLD	OPOINT
; RR - IF PAST RETURN THEN RIPPLE IT OUT
	CPI	CR
	JNE	CMD
	MOV	M,A
	JMP	CMD
	EJECT
;
; LET'S GET DOWN TO BUSINESS
;
;
; RIGHT CURSOR MOVEMENT
;
RCURS:	CALL	RTCURS	; RR
	JC	ERR
	JMP	CMD
;
;  SHIFT CURSOR ONE RIGHT
;	CY=ERROR (IF AT CR)
;
RTCURS:	LHLD	OPOINT	; PICK UP OLD POINTER
	XCHG
	LHLD	CPOINT	; GET BUFFER ADDRESS
	LDAX	D	; GET OLD CHAR
	MOV	M,A	; SAVE AS NEW
	INX	H	; UPDATE CURRENT
	MVI	M,CR
;
	CPI	CR	;ÿÿ¾ÞÀÀOV
INATED
	STC
	RZ		; ERROR (CY)
	ORA	A
	STC
	RZ
;
	SHLD	CPOINT
	XCHG
	INX	H	; UPDATE OLD POINTER
	SHLD	OPOINT
	CALL	CHROT1
	ORA	A	; OK(NOT CY)
	RET
;
FIND:	CALL	RTCURS	; MOVE RIGHT ONE
	JC	ERR	; UNLESS ERR
;
	CALL	CHRIN	; GET MATCH CHAR
;
MVREST:
;
; ALL WE DO NOW IS MOVE OLD->NEW 
;	UNTIL (CR) OR (CHAR MATCH)
;
	LHLD	CPOINT
	XCHG
	LHLD	OPOINT
	MOV	C,A	; C=CHAR TO MATCH
;
MOVLOP:	MOV	A,M
	CPI	CR
	JZ	EXTLP1
	ORA	A
	JZ	EXTLP1
;
	CMP	C
	JZ	EXTLOP
;
	STAX	D
	CALL	CHROT1	; OUTPUT CHARACTER
	INX	D
	INX	H
	JMP	MOVLOP
;
EXTLP1:	MVI	A,CR
EXTLOP:	STAX	D
	SHLD	OPOINT
	XCHG
	SHLD	CPOINT
	JMP	CMD
;
;
RUBOT:	LHLD	NBUFF	; RUBOUT A CHAR
	XCHG
	LHLD	CPOINT
	INX	D
	INX	D
	CALL	CMPHL
	JZ	ERR	; GET OUT (NO MORE TO THE LEFT)
;
	MOV	A,M
	DCX	H
	SHLD	CPOINT
	MVI	M,CR	; TERMINATE BUFFER
;
	MVI	E,BKSPC
	CALL	CHROT
	MVI	E,SPC
	CALL	CHROT
	MVI	E,BKSPC
	CALL	CHROT
;
	LDA	TOG
	ORA	A
	JNZ	CMD
	LHLD	OPOINT
	DCX	H
	SHLD	OPOINT
	JMP	CMD
;
;   DELETE AN OLD CHAR
;     ECHO OLDDDEL
;
DELOT:	LHLD	OPOINT
	MOV	A,M
	CPI	CR
	JEQ	ERR	; AT END OF OLD LINE
	INX	H
	SHLD	OPOINT
; ECHO OLD CHAR DEL MARK
	MVI	A,OLDDEL
	CALL	CHROT1
	JMP	CMD
;
;
MOVIT:	MVI	A,CR
	JMP	MVREST
;
;  FLIP INSET FLAG
;
TOGGLE:	LDA	TOG
	CMA
	STA	TOG
	MVI	E,'>'
	ORA	A
	JZ	TPUT
	MVI	E,'<'
TPUT:	CALL	CHROT
	JMP	CMD
;
UPDATE:	LHLD	NBUFF
	INX	H
	PUSH	H
	INX	H
	CALL	NEGHL
	XCHG
	LHLD	CPOINT
	DAD	D
	MOV	A,L
	POP	H
	MOV	M,A
	RET
	EJECT
;
;  RETURN ORIGINAL TEXT
;
ABORT:	LXI	D,ORIGBF
	JMP	DONE
;
;  EXIT MODULE - WITH NEW LINE IN BUFFER
;
EXIT:	CALL	UPDATE
	LXI	D,NEWBF
;
; DO A NEW LINE AND RETURN PROPER TEXT LINE
;
DONE:	LHLD	OBUFF	; MOVE PROPER TEXT TO OUTPUT BUFF
	MOV	C,M
	INR	C
	INR	C	; C = BUFFER LENGTH
	CALL	MOVE
;
	CALL	CRLF	; OUTPUT A NEW LINE
	LHLD	STAK
	SPHL
	RET
;
; RE-EDIT THE TEXT
;
REDIT:	CALL	CHROT1	; ECHO (^R)
	CALL	UPDATE
	CALL	CRLF
	MVI	C,CR	; COPY TO CR
	LHLD	NBUFF
	XCHG
	LHLD	OBUFF
	MVI	C,CR
	CALL	LMOV	; MOVE DE -> HL	  = NBUFF - OBUFF
	MVI	M,CR
	LHÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿROT1BUFF
	INX	H
	SHLD	CPOINT
	XRA	A
	STA	TOG	; TURN OFF INSERT
	JMP	CMD
;
;
	RAM
;
; BUFFER SPACE AND LOCALS
;
STAK:	DS	2	; RETURN ADDRESS
TOG:	DS	1	; INSERT TOGGLE SWITCH
OBUFF:	DS	2	; OLD BUFFER ADDRESS
NBUFF:	DS	2	; NEW "		   "
;
OPOINT:	DS	2	; OLD POINTER
CPOINT:	DS	2	; CURRENT POINTER
;
ORIGBF:	DS	128
NEWBF:	DS	128
;
