$auto$=-2
.enabl lc
;15-aug-80 for Conroy C runtime
	.PSECT EDI07
	.TITLE EDI07
	.IDENT /V1/
	.enabl lc
	.mcall wtse$,mrkt$,dir$,callr
	.MACRO return
	rts pc
	.ENDM
	.MACRO push this
	mov this,-(sp)
	.ENDM
	.MACRO pop that
	mov (sp)+,that
	.ENDM
	.globl ttynll
	.globl cursl
	.globl cursr2
	.globl ceol
	.globl fixscn
	.globl bcom		;entry for	b command
	.globl bcom1		;alt entry for b command
	.globl smode		;variable	to determine screen/line mode
	.globl prescn
	.globl postscn
	.globl bottom		;the line # of	the bottom line
	.globl fpage
	.globl fdot
smode:	.word	0		;0= not screen	mode,	<>0 = in screen	mode
	tab	=	11
	esc	=	33
	linefd	=	12
	sdel	=	123
	home	=	110
	bottom	=	24.
	insert	=	114
	delete	=	115
	ceol	=	113
	ceop	=	'J
	rightmost =	80.
	screen	=	1
	line	=	0
	blank	=	40
	lbrk	=	133	;a [
	langle	=	74	;a <
olddot:	.word	0		;previous dot value
I:	.word	0		;local	index
K:	.word	0		;local	index
flag1:	.word	0		;used by dot	checker
cursl:	.word	13.
cursc:	.word	1.
fpage:	.word	'@
fdot:	.word	'H
nocmd:	.word	0		;0= bypass the	regular	ued command
smacro:	.word	0		;the a	and i commands	may be s	macros
multadr:.word	0		;set to 0 if a	multi address cmd in process
mark:	mrkt$	30.,4,1
wait:	wtse$	30.
prescn:
	clr nocmd
	JSR PC,fixscn
	CMP smode,#screen		;IS smode = #screen
	BNE v7.1	;Not if NE
	    mov	r0,-(sp)
	JSR PC,@pretbl(r0)
	    mov	(sp)+,r0
	TST nocmd		;IS nocmd NE #0
	BEQ v7.4	;Not if EQ
		add #10,(sp)		;bump return past	the subroutine	call
v7.4=.			; 
v7.6=.
v7.1=.			; 
v7.3=.
	return
postscn:
	CMP smode,#screen		;IS smode = #screen
	BNE v7.7	;Not if NE
	    mov	r0,-(sp)
	JSR PC,@psttbl(r0)
	MOV #bottom-2,-(SP)
	MOV #1,-(SP)
	CALL CURSR2
JSR PC,TTYNLL
	    mov	(sp)+,r0
v7.7=.			; 
v7.9=.
	return
pretbl:	.WORD	Apre		;APPEND
	.WORD	no.op		;B
	.WORD	Cpre		;CHANGE
	.WORD	Dpre		;DELETE
	.WORD	Epre		;EDIT
	.WORD	Fpre		;FACTS
	.WORD	Gpre		;GLOBAL
	.WORD	ERR2		;H
	.WORD	Ipre		;INSERT
	.WORD	Jpre		;JOIN
	.WORD	Kpre		;KOPY
	.WORD   NO.OP           ;L
	.WORD	Mpre		;MOVE
	.WORD   no.op           ;N
	.WORD	Opre		;OPTIONS
	.WORD	Ppre		;PRINT
	.WORD	Qpre		;QUIT
	.WORD	Rpre		;READ
	.WORD	Spre		;SUBSTITUTE
	.WORD	no.op		;T
	.WORD	upre		;U
	.WORD	Vpre		;EXCLUDE
	.WORD	Wpre		;WRITE
	.WORD	Xpre		;WRITE WITH	NEW VERSION
	.WORD	NO.OP		;Y
	.WORD	Zpre		;Z COMMAND			
psttbl:	.WORD	Apst		;APPEND
	.WORD	no.op		;B
	.WORD	Cpst		;CHANGE
	.WORD	Dpst		;DELETE
	.WORD	Epst		;EDIT
	.WORD	Fpst		;FACTS
	.WORD	Gpst		;GLOBAL
	.WORD	ERR2		;H
	.WORD	Ipst		;INSERT
	.WORD	Jpst		;JOIN
	.WORD	Kpst		;KOPY
	.WORD   NO.OP           ;L
	.WORD	Mpst		;MOVE
	.WORD   no.op           ;N
	.WORD	Opst		;OPTIONS
	.WORD	Ppst		;PRINT
	.WORD	Qpst		;QUIT
	.WORD	Rpst		;READ
	.WORD	Spst		;SUBSTITUTE
	.WORD	no.op		;T
	.WORD	upst		;U
	.WORD	Vpst		;EXCLUDE
	.WORD	Wpst		;WRITE
	.WORD	Xpst		;WRITE WITH	NEW VERSION
	.WORD	NO.OP		;Y
	.WORD	Zpst		;Z COMMAND			
Apre:					;APPEND
	mov cursl,r1			;if its not an	s	macro, clear bottom
	br app1
	Ipre=.				;INSERT
	mov cursl,r1
	dec r1
app1:	clr smacro			;assume not a macro
	JSR PC,getc
	mov r0,peek			;and put it back
	CMP r0,#nl		;IS r0 NE #nl
	BEQ v7.10	;Not if EQ
	    inc	smacro			;its	a	macro
	JSR PC,cmultadr
	JMP v7.13
v7.10=.			; 
	    inc r1
	MOV R1,-(SP)
	MOV #1,-(SP)
	CALL CURSR2
MOV #esc,R0
	CALL TTYCC
MOV #ceop,R0
	CALL TTYCC
	MOV R1,-(SP)
	MOV #1,-(SP)
	CALL CURSR2
JSR PC,TTYNLL
v7.13=.			; 
v7.12=.
	return
Cpre:					;CHANGE
	TST oe		;IS oe NE #0
	BEQ v7.14	;Not if EQ
	    jmp	spre1			;and its really	an s command
v7.14=.			; 
v7.16=.
cpre1:
Dpre:					;DELETE
	return
Epre:					;EDIT
	clr smode
	return
Fpre:					;FACTS
	return
Vpre:					;EXCLUDE
Gpre:					;GLOBAL
	clr smode			;out of screen mode
	mov #1,onum			;turn on line numbers
	return
Jpre:					;JOIN
Kpre:					;KOPY
Mpre:					;MOVE
Opre:					;OPTIONS
	return
Ppre:					;PRINT
	CMP adr1,adr2		;IS adr1 NE adr2
	BEQ v7.17	;Not if EQ
	    clr	smode			;out	of	screen mode and	print
	JMP v7.20
v7.17=.			; 
	    inc	nocmd			;we do it all
	JSR PC,nonzer
	MOV adr1,R0
	SUB dot,R0
	TST r0		;IS r0 LT #0
	BGE v7.21	;Not if GE
		neg r0
v7.21=.			; 
v7.23=.
	    mov	dot,olddot		;save it
	    mov	adr1,dot		;set new dot
	CMP r0,#24.		;IS r0 GT #24.
	BLE v7.24	;Not if LE
	JSR PC,bcom1
	JMP v7.27
v7.24=.			; 
	CMP adr1,olddot		;IS adr1 = olddot
	BNE v7.28	;Not if NE
	JSR PC,redisp
	JMP v7.31
v7.28=.			; 
	CMP adr1,olddot		;IS adr1 HI olddot
	BLOS v7.32	;Not if LOS
	JSR PC,upit
	JMP v7.35
v7.32=.			; 
	JSR PC,downit
v7.35=.			; 
v7.34=.
v7.31=.			; 
v7.30=.
v7.27=.			; 
v7.26=.
v7.20=.			; 
v7.19=.
	return
upit:					;scroll up until dot reaches adr1
	mov olddot,dot
	MOV #1,I	;Init loop index
v7.36=.
	CMP I,#24.	;done loop?
	BGT v7.37	;yes if GT
	    add	#2,dot
	JSR PC,upscroll
	CMP dot,adr1		;IS dot HIS adr1
	BLO v7.38	;Not if LO
	JMP v7.37         ;break
v7.38=.			; 
v7.40=.
	INC I
	JMP v7.36		;back to top
v7.37=.
	return
downit:					;scroll down until dot gets	to adr1
	mov olddot,dot			;need this again
	MOV #1,I	;Init loop index
v7.41=.
	CMP I,#24.	;done loop?
	BGT v7.42	;yes if GT
	    sub	#2,dot			;back	a line
	JSR PC,downscroll
	CMP dot,adr1		;IS dot LOS adr1
	BHI v7.43	;Not if HI
	JMP v7.42         ;break
v7.43=.			; 
v7.45=.
	INC I
	JMP v7.41		;back to top
v7.42=.
	return
Qpre:					;QUIT
Rpre:					;READ
	return
Spre:					;SUBSTITUTE
	TST oe		;IS oe NE #0
	BEQ v7.46	;Not if EQ
	    jmp	cpre1
v7.46=.			; 
v7.48=.
spre1:
	JSR PC,cmultadr
	return
upre:					;U
Wpre:					;WRITE
Xpre:					;WRITE WITH	NEW VERSION
	return
Zpre:					;Z COMMAND			
	clr smode
no.op:	return
Apst:					;APPEND
Ipst:					;INSERT
	TST smacro		;IS smacro = #0
	BNE v7.49	;Not if NE
	    mov	dot,adr1		;it requires this
	    mov	dot,adr2		;and this
	JSR PC,bcom1
	JMP v7.52
v7.49=.			; 
	    jmp	spst1
v7.52=.			; 
v7.51=.
	return
Cpst:					;CHANGE
	TST oe		;IS oe NE #0
	BEQ v7.53	;Not if EQ
	    jmp	spst1
v7.53=.			; 
v7.55=.
cpst1:
	return
Dpst:					;DELETE
	mov dot,adr1			;it requires this
	mov dot,adr2			;and this
	JSR PC,bcom1
	return
Epst:					;EDIT
	return
Fpst:					;FACTS
	callr	sfacts			;in screen mode
Gpst:					;GLOBAL
	return
Jpst:					;JOIN
	return
Kpst:					;KOPY
Mpst:					;MOVE
	jmp dpst			;new	display	needed
Opst:					;OPTIONS
Ppst:					;PRINT
Qpst:					;QUIT
	return
Rpst:					;READ
	jmp dpst
	return
Spst:					;SUBSTITUTE
	TST oe		;IS oe NE #0
	BEQ v7.56	;Not if EQ
	    jmp	spst1
v7.56=.			; 
v7.58=.
spst1:					;This is the sub entry
	TST multadr		;IS multadr NE #0
	BEQ v7.59	;Not if EQ
	    jmp	dpst			;new display
	JMP v7.62
v7.59=.			; 
	JSR PC,redisp
	JSR PC,getc
	    mov	r0,peek			;put it back
	    bic	#40,r0			;make it upper	case
	CMP r0,#'P		;IS r0 = #'P
	BNE v7.63	;Not if NE
		mov #-1,peek		;clear	peek char
v7.63=.			; 
v7.65=.
v7.62=.			; 
v7.61=.
	return
upst:					;U
	JSR PC,redisp
Vpst:					;EXCLUDE
Wpst:					;WRITE
Xpst:					;WRITE WITH	NEW VERSION
Zpst:					;Z COMMAND			
	return
fixscn:
	CMP smode,#screen		;IS smode = #screen
	BNE v7.66	;Not if NE
	MOV R0,-(SP)
MOV #esc,R0
	CALL TTYCC
MOV #'Y,R0
	CALL TTYCC
MOV #'6,R0
	CALL TTYCC
MOV #blank,R0
	CALL TTYCC
MOV #esc,R0
	CALL TTYCC
MOV #ceop,R0
	CALL TTYCC
JSR PC,TTYNLL
	MOV (SP)+,R0
v7.66=.			; 
v7.68=.
	return
bcom:					;The b command does	the BEGIN SCREEN mode
MOV #esc,R0
	CALL TTYCC
MOV #lbrk,R0
	CALL TTYCC
MOV #'?,R0
	CALL TTYCC
MOV #'2,R0
	CALL TTYCC
MOV #'l,R0
	CALL TTYCC
bcom1:
	MOV R0,-(SP)
	MOV R2,-(SP)
	mov #screen,smode		;into screen	mode
	TST adr2		;IS adr2 NE #0
	BEQ v7.69	;Not if EQ
	    mov	adr2,dot		;set dot
v7.69=.			; 
v7.71=.
	MOV dot,r2
	SUB #24.,r2
MOV #esc,R0
	CALL TTYCC
MOV #'H,R0
	CALL TTYCC
MOV #esc,R0
	CALL TTYCC
MOV #'J,R0
	CALL TTYCC
JSR PC,TTYNLL
	MOV #1,I	;Init loop index
v7.72=.
	CMP I,#22.	;done loop?
	BGT v7.73	;yes if GT
	MOV I,-(SP)
	MOV #1,-(SP)
	CALL CURSR2
	CMP i,cursl		;IS i = cursl
	BNE v7.74	;Not if NE
	JSR PC,uline1
v7.74=.			; 
v7.76=.
	JSR PC,get.line
	    mov	#lbuf1,r0		;its	put into lbuf1	null terminated
	JSR PC,xttystr
	CMP i,cursl		;IS i = cursl
	BNE v7.77	;Not if NE
	JSR PC,uline2
v7.77=.			; 
v7.79=.
	    add	#2,r2			;go to next location
	INC I
	JMP v7.72		;back to top
v7.73=.
	MOV (SP)+,R2
	MOV (SP)+,R0
	RTS PC
err2:	mov #2,r1
	jmp error
ttynll:	jmp ttynl
get.line:				;get line	but check	range	of dot
	MOV R0,-(SP)
	clr flag1		;assume ok
	CMP r2,zero		;IS r2 LOS zero
	BHI v7.80	;Not if HI
	    inc	flag1
v7.80=.			; 
v7.82=.
	CMP r2,dol		;IS r2 HI dol
	BLOS v7.83	;Not if LOS
	    inc	flag1
v7.83=.			; 
v7.85=.
	TST flag1		;IS flag1 = #0
	BNE v7.86	;Not if NE
	    mov	(r2),r0		;contents of	pointer	to line
	JSR PC,getlin
	JMP v7.89
v7.86=.			; 
	    movb #' ,lbuf1	;simulate a blank line
	    clrb lbuf1+1	;and	null terminate it
v7.89=.			; 
v7.88=.
	MOV (SP)+,R0
	return
upscroll:
	MOV dot,R2
	ADD #18.,R2
	JSR PC,get.line
	MOV #bottom,-(SP)
	MOV #1,-(SP)
	CALL CURSR2
MOV #esc,R0
	CALL TTYCC
MOV #langle,R0
	CALL TTYCC
MOV #esc,R0
	CALL TTYCC
MOV #'D,R0
	CALL TTYCC
MOV #esc,R0
	CALL TTYCC
MOV #lbrk,R0
	CALL TTYCC
MOV #'?,R0
	CALL TTYCC
MOV #'2,R0
	CALL TTYCC
MOV #'l,R0
	CALL TTYCC
JSR PC,TTYNLL
	MOV cursl,r1
	MOV r1,-(SP)
	MOV #1,-(SP)
	CALL CURSR2
	MOV dot,r2
	JSR PC,get.line
	TST lmode		;IS lmode = #0
	BNE v7.90	;Not if NE
	JSR PC,uline1
	mov #lbuf1,r0
	JSR PC,xttystr
	JSR PC,uline2
v7.90=.			; 
v7.92=.
	MOV cursl,r1
	DEC r1
	MOV r1,-(SP)
	MOV #1,-(SP)
	CALL CURSR2
	MOV dot,r2
	SUB #2,r2
	JSR PC,get.line
	mov #lbuf1,r0
	JSR PC,xttystr
	MOV dot,R2
	ADD #18.,R2
	MOV #bottom-2,-(SP)
	MOV #1,-(SP)
	CALL CURSR2
	JSR PC,get.line
	mov #lbuf1,r0
	JSR PC,xttystr
	MOV #bottom-1,-(SP)
	MOV #rightmost,-(SP)
	CALL CURSR2
MOV #esc,R0
	CALL TTYCC
MOV #ceop,R0
	CALL TTYCC
JSR PC,TTYNLL
	return
Downscroll:
MOV #esc,R0
	CALL TTYCC
MOV #home,R0
	CALL TTYCC
MOV #esc,R0
	CALL TTYCC
MOV #'I,R0
	CALL TTYCC
	MOV cursl,r1
	MOV r1,-(SP)
	MOV #1,-(SP)
	CALL CURSR2
	mov dot,r2
	JSR PC,get.line
	TST lmode		;IS lmode = #0
	BNE v7.93	;Not if NE
	JSR PC,uline1
	mov #lbuf1,r0
	JSR PC,xttystr
	JSR PC,uline2
v7.93=.			; 
v7.95=.
	MOV cursl,r1
	INC r1
	MOV r1,-(SP)
	MOV #1,-(SP)
	CALL CURSR2
	MOV dot,r2
	ADD #2,r2
	JSR PC,get.line
	mov #lbuf1,r0
	JSR PC,xttystr
	MOV dot,R2
	SUB #24.,R2
MOV #esc,R0
	CALL TTYCC
MOV #home,R0
	CALL TTYCC
	JSR PC,get.line
	mov #lbuf1,r0
	JSR PC,xttystr
	MOV #bottom-2,-(SP)
	MOV #rightmost,-(SP)
	CALL CURSR2
MOV #esc,R0
	CALL TTYCC
MOV #ceop,R0
	CALL TTYCC
JSR PC,TTYNLL
	return
redisp:
	push r0
	push r2
	MOV cursl,-(SP)
	MOV #1,-(SP)
	CALL CURSR2
MOV #esc,R0
	CALL TTYCC
MOV #ceol,R0
	CALL TTYCC
JSR PC,TTYNLL
	mov dot,r2
	JSR PC,get.line
	mov #lbuf1,r0
	JSR PC,uline1
	JSR PC,xttystr
	JSR PC,uline2
	pop r2
	pop r0
	return
setfld:
	MOV R0,-(SP)
	JSR PC,getcx
	CMP R0,#nl		;IS R0 NE #nl
	BEQ v7.96	;Not if EQ
	    mov	r0,fpage		;and set page	function
v7.96=.			; 
v7.98=.
	JSR PC,getcx
	CMP R0,#nl		;IS R0 NE #nl
	BEQ v7.99	;Not if EQ
	    mov	r0,fdot			;set	dot function
v7.99=.			; 
v7.101=.
	MOV (SP)+,R0
	return
getcx:					;get char, check for newline, correct case
	JSR PC,getc
	CMP R0,#nl		;IS R0 NE #nl
	BEQ v7.102	;Not if EQ
	CMP R0,#40		;IS R0 = #40
	BNE v7.105	;Not if NE
		mov #'@,r0		;and make it an '@'
v7.105=.			; 
v7.107=.
	    bic	#60,r0			;map to upper	and clear protect bit
	JMP v7.108
v7.102=.			; 
	    mov	r0,peek			;put	it back
v7.108=.			; 
v7.104=.
	return
cmultadr:				;check for multiple	addresses
	clr multadr			;assume single address
	CMP adr1,adr2		;IS adr1 NE adr2
	BEQ v7.109	;Not if EQ
	    inc	multadr			;note	fact
v7.109=.			; 
v7.111=.
	return
uline1:						;underline begin
	push r0
MOV #esc,R0
	CALL TTYCC
MOV #langle,R0
	CALL TTYCC
MOV #esc,R0
	CALL TTYCC
MOV #lbrk,R0
	CALL TTYCC
MOV #'4,R0
	CALL TTYCC
MOV #'m,R0
	CALL TTYCC
JSR PC,TTYNLL
	pop r0
	return
uline2:
	push r0
MOV #esc,R0
	CALL TTYCC
MOV #lbrk,R0
	CALL TTYCC
MOV #'m,R0
	CALL TTYCC
MOV #esc,R0
	CALL TTYCC
MOV #lbrk,R0
	CALL TTYCC
MOV #'?,R0
	CALL TTYCC
MOV #'2,R0
	CALL TTYCC
MOV #'l,R0
	CALL TTYCC
JSR PC,TTYNLL
	pop r0
	return
xttystr:			;do a ttystr but at least 1 char
	CMPB (r0),#nl		;IS (r0) = #nl
	BNE v7.112	;Not if NE
	push r0
MOV #blank,R0
	CALL TTYCC
	pop r0
v7.112=.			; 
v7.114=.
	JSR PC,ttystr
	return
cursr2:
MOV #esc,R0
	CALL TTYCC
MOV #'Y,R0
	CALL TTYCC
        mov (sp),r0                     ; get ret address
        mov 4(sp),(sp)                  ; replace tos with x cord
        mov r0,4(sp)                    ; replace 3os with return address
        pop r0                          ; pop x into r0
        add #31.,r0                     ; convert to teleray cord's
	JSR PC,ttycc
        pop r0                          ; pop y into r0
        add #31.,r0                     ; convert
	JSR PC,ttycc
        rts pc
	.end
