; move comments over 1 tab	C/;/	;/
; move back 1 tab        	C/	;/;/
; find leading spaces and convert to tabs	4y5y6y
; get 24 leading	1,$c/^                        /			/4p
;      16        	1,$c/^                /		/5p
;       8         	1,$c/^        /	/6p
;
;~G/[78][0-9]/P
; the following	are mpp	macros
;
;
	$MACRO TTY A B C D E ...
	** TTY1	#@1 #@2	#@3 #@4	#@5 #@6	#@7 #@8	#@9 #@A	#@B #@C	
	$ENDM 
;
	$MACRO TTY1
	I%IN <#@1> END <<jsr pc,ttflsh>> <TTY2 <#@1>>
	$ENDM 
;
	$MACRO TTY2
	S%SW 21
	I%IN <#@1> <R1 R2 R3 @R0 @R1 @R2 @R3>	<> <S%SW -21>
	I%SW 21	<MOVB #@1,R0> <I%DF <#@1> R0 <MOVB ##@1,R0>>
	CALL TTYCC
	$ENDM 
;
	$MACRO FUNCTION	set to parm3
	I%IDN	R0 <#@2> <I%ERR	<Cant use	r0 here>>
	TTY ESC	'R
	I%DF R0	<#@3>	<mov #@3,r0>
	call ttycc
	$ENDM 
;
;;;$MACRO CURSOR	TO LINE	COL
;;;	I%IDN	R0 <#@2> <I%ERR	<Cant use	r0 here>>
;;;	TTY ESC	'Y
;;;	MOV #@2,R0
;;;	ADD #31.,R0
;;;	TTY R0
;;;	MOV #@3,R0
;;;	ADD #31.,R0
;;;	TTY R0
;;;$ENDM 
;
	$MACRO CURSOR TO X,Y
	MOV #@2,-(SP)
	MOV #@3,-(SP)
	CALL CURSR2
	$ENDM 
;
	$MACRO return	;this MUST be lower case
	rts pc
	$ENDM 
/
	PROGRAM Eb 	; handle the intra line edit command (l)
	mutask=1 
	.if df mutask
	.MACRO pure
	.psect pur,ro
	.ENDM 
		
	.MACRO impure
	.psect ipur,rw
	.ENDM 
		
	.iff 
		
	.MACRO pure
	.ENDM 
	.MACRO impure
	.ENDM 
		
	.endc 
	.enabl ama
	.nlist sym
	.psect edi11
		
		
	.globl lcom
	.globl lcom2
	.globl lmode
	.globl text
	.globl etext
	.globl expand
	.globl compress
	.globl findend
		
	.MACRO push this
	mov this,-(sp)
	.ENDM 
		
	.MACRO pop that
	mov (sp)+,that
	.ENDM 
		
	.MACRO callr x
	jmp x
	.ENDM 
		
; - - -	 macro library calls
		
		
	.mcall dir$,callr,qiow$,mrkt$,wtse$
		
		
; - - -	 system	macros
		
read:   qiow$	io.rtt,1,1,,iosred,,<ib,ibsize,,tbl>
helpq:  qiow$   io.wvb,1,1,,,,<help,ehelp-help,0>
helpq2: qiow$   io.wvb,1,1,,,,<help2,ehelp2-help2,0>
tofq:   qiow$   io.wvb,1,1,,,,<tof,etof-tof,0>
setmul: qiow$   sf.smc,1,1,,setios,,<setm,esetm-setm>  ;set
getmul: qiow$   sf.gmc,1,1,,setios,,<retm,eretm-retm>  ;get into restore loc
retmul: qiow$   sf.smc,1,1,,setios,,<retm,eretm-retm>  ;restore
setios: .word 0,0
		
	d       =       1       ;make this a 1 for debug mode, 0 for production
		
tbl:        
	.word d+2+4+10+20+40+100+200+400+1000+2000+4000+10000+20000+40000+100000
	.word 1+4+20+40+100+200+400+1000+2000+4000+100000
	.word 0,0,0,0
	.word 0,100000,0,0              ;rubout
	.word 0,0,0,0,0,0
		
help:      
	.byte esc,'Y,bottom-2+40,'!
	.ascii /L=text<- B=tab<- D=del-eol N=end-l  W=wordfind E=enter J=<-- Y=up V=split/
	ehelp=. 
		
		
help2:    
	.byte,esc,'Y,bottom-1+40,'! 
	.ascii /R=text-> U=Undo  T=tabstop A=adjust P=position O=open  K=--> H=down/
		
	ehelp2=. 
tof:    .byte,esc,'Y,12.-1+40,'= 
	.ascii / **  Top of File **/
	etof=. 
	.even 
;                hor-tabs      buf=132         set /nowrap
setm:   .byte   tc.hht,1,    tc.wid,  255.,   tc.acr,  0
	esetm=. 
retm:   .byte   tc.hht,0,    tc.wid,  000.,   tc.acr,  0
	eretm=. 
		
		
;+
; current codes
;
; ^; = (null code for debug)
; ^a   (adjust)
; ^b = reverse tab
; ^c = rsx mcr function (or exits mode)
; ^d = delete to end of line
; ^e = Enter text mode (i.e. insert a line)
; ^f =
; ^g = delete line
; ^h = cursor down
; ^i = tab
; ^j = curser left one
; ^k = curser right one
; ^l   move text left (delete char)
; ^m   carriage return
; ^n
; ^o = open line for inserting characters
; ^p = position to current line at current margin
; ^q = rsx function - can't use
; ^r = move text right (insert char)
; ^s = rsx function
; ^t = tag margin
; ^u = undo all changes on line
; ^v   split line at cursor
; ^w = move word
; ^x = rsx flush typeahead
; ^y = cursor up
; ^z =
; ^? = help message (37)
;-
		
; - - -  local data and symbols
		
	blank   =   40
	bottom  =   24.
	ceol    =   113
	ceop    =   'J
	cr      =   15
		
	con.0   =   0       ;control 0
	con.a   =   1       ;control a
	con.b   =   2       ;control b
	con.c   =   3       ;control c
	con.d   =   4       ;control d
	con.e   =   5       ;control e
	con.f   =   6       ;control f
	con.g   =   7       ;control g
	con.h   =   10      ;control h
	downarrow=con.h 
	con.i   =   11      ;control i
	con.j   =   12      ;controj j
	con.k   =   13      ;controk k
	con.l   =   14      ;control l
	con.o   =   17      ;control o
	con.p   =   20      ;control p
	con.q   =   21      ;control q
	con.r   =   22      ;control r
	con.s   =   23      ;control s
	con.t   =   24      ;control t
	con.u   =   25      ;control u
	con.v   =   26      ;control v
	con.w   =   27      ;control w
	con.x   =   30      ;control x
	con.y   =   31      ;control y
	uparrow = con.y
	con.z   =   32      ;control z
		
	esc          =  33
	tab          =  9.
	space        =  40
	home         =  110
	insert       =  114
	del          =  115
	line         =  0
	col80        =  80.
	rub          =  177
	screen       =  1
	sdel         =  123
	transl       =  'i      ;`i' transmit line
	nl	     =  12
		
	ibsize       =  50.     ;input buffer size
	tsize        =  254.    ;this be larger thanlbs but needs to be local
	tabseen:.word 0                ;0=don't convert tabs on way out, 1=do it
setmar: .word 0                 ;if we have set the special multiple chars <>0
chglin: .word			;change made on line 0=no,>0 =yes
tagset: .word	0		;0=not set, >0 = set
arrows: .word	0		;0=not, 1=yes (command was an up/down arrow)
cycle:  .word   0               ;count times thru word look wraparound
	scurser:.word   0               ;save curser
omode:  .word   0               ;open mode 0=no, >0 =yes
emode:  .word   0               ;Enter mode 0=no, <>0 = yes
iosred: .word        0,0
ios:    .word        0,0
ib:     .blkb        ibsize
ib2:    .blkw        2
lmode:  .word	0
gtemp:  .word   0               ;save whether we were at $
	.even 
	maccmd=177 
up:     .ascii /-1p/<maccmd><2> ;enter through special window to lcom2
	upl=.-up 
dwn:    .ascii /+p/<maccmd><2>  ;enter through special window to lcom2
	dwnl=.-dwn 
restor: .ascii /.p/<maccmd><2>  ;enter through special window to lcom2
	restl=.-restor 
lbcmd:  .ascii /b/<nl>/l/<nl>	;do the b for him
	lbcmdl=.-lbcmd 
	.even 
spat:   .word 0         ;save pattern if control t is set
pat:    .word 0         ;bool, pattern=1,nopat=0
coln:   .word 1         ;column number to l to
text:   .blkb tsize     ;text buffer (current line expanded without tabs)
	etext=.-1       ;pointer to last char in text buffer
	.even 
curser: .word 0         ;curser position on current line
firsttime: .word 0 ;first time into l command (each issuance of l)
sdot:   .word 0 ;save fdot code while here
cnt:    .word 0 ;spare counter
		
		
mark:   mrkt$	30.,4,1
wait:   wtse$	30.
		
	pure 
		
		
		
lcom:              
	IF setmar =  #0
	    dir$ #getmul    ;get current for latter restoration
	    dir$ #setmul
	    inc setmar
	FI 
		
	IF dot = zero AND dot = dol
	    call addline
	    LET dot = zero + #2
	FI 
	mov #1,lmode
		
	IF smode = #0
	    mov #lbcmd,r0
	    mov #lbcmdl,r1
	    callr putbak                    ;put the user in screen & redo command
	FI 
	IF ol NE #0
	    LET r1 = cursl - #1
	    CURSOR to r1,#col80
	    TTY esc sdel
	    mov fpage,r1
	    bic #10,r1
	    mov fdot,sdot
	    mov r1,fdot
	    FUNCTION set to r1
	FI 
		
		
	call getc
	IF r0 = #nl		;no pattern, no number
	    mov pat,spat
	    clr pat
	ELSE 
	    IF r0 = #'#                 ;might be column number
		call getc
		call getnum             ;get number
		mov r1,coln             ;save column
		call getc               ;and get next char
		IF r0 NE #nl            ; must end in nl to be valid
		    mov #2,r1          ; bad command error
		    jmp error          ;
		FI 
		mov pat,spat
		clr pat
			        
	    ELSE 
		mov #1,pat              ;its definitively a pattern
		IFB r0 = #'/
		    call compil                     ;get pattern
		ELSE 
		    mov r0,peek                     ;put back (1st char in pattern)
		    mov #1,r0                       ;use control a as del
		    call compil                     ;compile pattern
		FI 
	    FI 
	FI 
	clr omode                               ;initially not in o mode
	mov #1,tagset                           ;initially not set - cr=down
	clr arrows
	mov coln,curser
	add #text-1,curser			;first time, allways point here
	mov #1,firsttime
; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
;                       Special entry here !!!
;          When a putback of <maccmd><2> is detected the macro
;          processor  gets  control  and  performs a branch to
;          lcom2 The macro processor is not the y command, but
;          rather an internal  mechanism  for  placing  psuedo
;          commands  into  the get char stream which allow one
;          to make use of any user command as  if  it  were  a
;          subroutine. 
; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
lcom2:          
	mov @dot,r0
	call getlin                         ;get current line
	call expand                         ;expand into text buffer without tabs
	clr chglin
	IF arrows =  #0
	    clr firsttime
	    IF pat NE #0                    ;if pattern specified
		mov #text,r2			;start search here
		call matcher
			        
		IF r2 NE #0
		    mov r2,curser
		ELSE 
		    mov #text,curser
		FI 
	    ELSE 
		mov coln,curser
		add #text-1,curser
	    FI 
	ELSE 
	    clr arrows
	FI 
	call tocurs
	TTY END
		   
lcom1:  dir$ #read                         ;just read till  terminator
		   
	call update                        ;update text buffer on input
		   
	IF iosred+2 = #ibsize		;end with no control char?
	    br lcom1			;yes, just update and read again
	FI 
		
	SELECT case iosred+1 of
	CASEB #37			;help message
	    dir$ #helpq
	    dir$ #helpq2
	    call tocurs
	    TTY END
	    jmp lcom1
			
			
	CASEB #rub
			
	    IF curser HI #text
		TTY 10 40 10 END           ;backspace, space, backspace
		dec curser
		movb #space,@curser
		inc chglin
	    FI 
	    jmp lcom1
	CASEB #con.0                             ;for debug
	    IF #debuga = #1
		mov coln,r0
		mov curser,r1
		mov #text,r2
		mov setios,r3
		mov setios+2,r4
		mov dot,r5
		iot 
	    FI 
	    jmp lcom1
	CASEB #con.a                             ;adjust text
	    call adjust                          ; do it all here
	    mov curser,r2                        ; but update screen here
	    DO WHILE r2 LOS #text+col80-1
		TTY @R2                          ;output each char from the
		inc r2                           ;cursor to end of screen
	    OD 
	    call tocurs                          ;reposition cursor
	    TTY END
	    inc chglin
	    jmp lcom1
	CASEB #con.b                             ;back tab
	    call tabback
	    call drag
	    jmp lcom1
	CASEB #con.v                            ;cut (break line)
;              IF curser LT #text
;                  jmp lcom1
;              FI
			
	    mov curser,r1
	    call compress
;              IFB lbuf1 = #nl
;                  jmp lcom1
;              FI
			
	    call cut
	    inc chglin
	    jmp lcom1
	CASEB #con.d                    ;delete to end of line
	    call deleol
	    TTY esc 'K END
	    inc chglin
	    jmp lcom1
	CASEB #con.o                    ;Open line
	    IF omode = #0
		inc omode               ;set flag indicating mode
		call saveln
		call movright           ;move text to right margin
		call deleol             ; clear data on right
		inc chglin
		jmp lcom1
	    ELSE 
omode2:                                      
		call edone
		clr omode
		jmp lcom1
	    FI 
	CASEB #con.i                             ;tabs
	    inc tabseen
	    call tabforward
	    call drag
	    jmp lcom1
			   
	CASEB #con.j                             ;cursor left one
	    IF curser HI #text                   ;don't go past col 1
		dec curser
		call tocurs
		TTY END
	    FI 
	    jmp lcom1
	CASEB #con.k                             ;cursor right one
	    IF curser LO #text+col80-2              ;don't go past end
		inc curser
		call tocurs
		TTY END
	    FI 
	    jmp lcom1
	CASEB #con.l
	    call delchar                        ;the char at the curser
	    TTY esc 'Q                          ;move text left (del char)
	    CURSOR to cursl,#col80-1
	    movb text+col80-3,r1                    ;get new char into display
	    TTY R1
	    movb text+col80-2,r1			;get new char into display
	    TTY R1
	    call tocurs
	    TTY END
	    inc chglin
	    jmp lcom1
	CASEB #con.e                            ;insert line
;                IF emode = #0
;                   call addline
;                   call inline
;                FI
	    com emode
	    call tocurs
	    TTY END
	    jmp lcom1
			
inline:          
	    CURSOR to #bottom-3,#col80
	    TTY esc sdel esc 'J
	    LET r1 = cursl + #1
	    CURSOR to R1,#1
	    TTY esc insert
	    FUNCTION set to FPAGE
	    TTY END
                 dir$ #mark          ;display is too slow
                 dir$ #wait          ;must   give it some time

	    rts pc
	     
	     
	     
	CASEB #con.g
	    LET gtemp = dot - dol
	    IF dol NE zero
		mov dot,adr1
		mov dot,adr2
		call delete                             ;delete line
		TTY esc 'M                              ;and on screen
		  
		LET R2 = dot + #18.                     ;get bottom line
		call get.line                           ;this gets a null if necessary
		  
		CURSOR TO #BOTTOM-2,#1                  ;go down there
		FUNCTION SET TO FDOT                    ;set up function code
		TTY END
		mov #lbuf1,r0                           ;now
		call ttystr                             ;output the bottom line
		  
		call tocurs                             ;but cursor back
		TTY END                                 ;to where it was
	    FI 
	     
	    IF gtemp = #0   AND zero NE dol
		movb #uparrow,iosred+1
		add #2,dot
	    ELSE 
		jmp lcom2
	    FI 
	     
	CASEB #con.p                            ;position at place again
	    IF spat NE #0
		mov spat,pat
	    FI 
			
	    IF    pat NE   #0  
		mov curser,r2	;start at curser +1
		inc r2
		call matcher
		IF r2 = #0
		    mov #text,r2
		    call matcher
		    IF r2 NE #0
			br 3000$
		    FI 
		ELSE 
3000$:                            
		    IF r2 HIS #text+col80-2
			mov #text,curser
		    ELSE 
			mov r2,curser
		    FI 
		FI 
	    ELSE 
		mov coln,curser
		add #text-1,curser
	    FI 
	    call tocurs
	    TTY END
	    jmp lcom1
	CASEB #con.r
	    call inschar                        ;insert a blank at curser
	    TTY esc 'P END                      ;move text right (ins char)
	    inc chglin
	    jmp lcom1
	CASEB #con.t
	    mov curser,coln
	    sub #text,coln
	    inc coln
	    inc tabseen
	    inc tagset
	    mov pat,spat
	    clr pat
	    jmp lcom1
	CASEB #con.u
	    mov #restor,r0
	    mov #restl,r1
	    clr chglin
	    callr putbak                        ;.pl cmd renter   setup curser
	CASEB #con.w
	    call mov2wd
	    call tocurs
	    TTY END
	    jmp lcom1
	ESAC 
		   
	IF omode GT #0
	    jmp omode2				;all exits go here
	FI 
		   
; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  x
;  This is a control point. If the above case statement fails then
;  we  have  a  control  char  which  requires  that we change the
;  current line. When in OPEN TEXT mode (^o) any command  that  is
;  not one of the above is treated as an exit OPEN mode. 
; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  x
		   
	IF chglin GT #0
	    mov #text,r1    ;move all text in `text'
	    call compress   ;move text to lbuf1 with tabs
	    call putlin
	    mov r0,@dot     ;replace line dot
	FI 
		   
; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  x
; The above has output the new line and now we check to see if we go     x
; up or down or quit l mode.                                             x
; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  x
		   
	SELECT case iosred+1 of
	CASEB #uparrow
	    mov #1,arrows
	    br 1000$
	CASEB #esc
1000$:            
	    LET R0 = ZERO + #2
	    IF dot HI  r0
;		mov #up,r0
;		mov #upl,r1
;		callr putbak

		sub #2,dot                  ;go back one line
		LET r2 = dot - #24.         ;
		call get.line               ;get the new top line
		TTY esc home esc insert     ;this will scroll down one
		FUNCTION set to fpage       ;at home we insert a line
		TTY END                     ;
		mov #lbuf1,r0               ;this is the line
		call ttystr                 ;we insert at line 1
		CURSOR to #bottom-2,#col80  ;now delete garbage at the bottom
		TTY esc sdel esc ceop END
		jmp lcom2


	    ELSE 
		dir$ #tofq
		call tocurs
		TTY END
		jmp lcom1
	    FI 
	CASEB #downarrow
	    mov #1,arrows
	    br 20$
	CASEB #cr
20$:                
	    IF emode NE #0  AND arrows NE #1
		call addline
		call inline
			        
	    ELSEIF  dot HIS dol
		call addline		;append null at end
	    FI 

            add #2,dot
            LET r2 = dot + #18.
            call get.line
	    TTY esc home esc del        ;this will do the insert line at bottom
	    CURSOR to #bottom-2,#1      ;redisplay new line here
            FUNCTION set to fpage

            mov #lbuf1,r0
	    call ttystr                 ;output bottom line
            jmp lcom2

;	    mov #dwn,r0
;	    mov #dwnl,r1
;	    callr putbak
			   
;;	CASEB #con.c
;;	    callr post
	CASEB #con.z
	    callr post
	ESAC 
		   
		   
post:                      
	mov sdot,fdot
	IF  ol NE #0                    ;option l, automatic underline on/off
	    LET r1 = cursl - #1
	    CURSOR to r1,#col80
	    TTY esc sdel
	    FUNCTION set to fdot
	FI 
	CURSOR to #bottom-2,#1
	TTY END
	clr lmode
	clr emode
	clr setmar
	dir$ #retmul
	rts pc
		   
;+
; expand - expand lbuf1 into text without tabs
; input
;	lubf1=asciz text string
;output
;	text=expanded asciz text sting
; uses
;	r0,r1,r2,r3
;-
expand:      
	call saval
	clr tabseen
	clr cnt
	mov #text,r0
	mov #etext-text+1,r1
2000$:  movb #space,(r0)+
	sob r1,2000$
		   
	mov #lbuf1,r1
	mov #text,r2
		   
	DO FOREVER
	    call eget
	    IF EQ
		BREAK 
	    FI 
			   
	    inc cnt                 ;limit check
	    IF cnt GT #lbs          ;in case of
		BREAK                ;too large a line, or one not asciz
	    FI 
			
			
			
	    IF r0 = #tab
		mov #1,tabseen
		mov #space,r0
		call eput		;one space minimum per tab
		push tabmsk
		mov #177770,tabmsk
		DO FOREVER
		    call tabstop
		    IF EQ
			BREAK 
		    FI 
		    mov #space,r0
		    call eput
		OD 
		pop tabmsk
	    ELSEIF r0 NE #nl
		call eput
	    FI 
	OD 
	return 
		   
;+
; eget - get chars from the input buffer
; input  r1 = current address into text buffer
; output r1 is updated
;        r0 is the next char
;-
eget:                    
	movb (r1)+,r0
	bic #177400,r0
	rts pc
		   
;+
; eput - put chars into the text buffer, check overflow
; input  r0 = char
;        r2 = current address into text buffer
; output r2 is updated and the char is put @r2
;-
eput:                    
	IF r2 LO #etext
	    movb r0,(r2)+
	FI 
	rts pc
		   
;+
; tabstop - check to see if we are at a tabstop
; input
;	r2 = address in text buffer
; output
;	r3 = 0 (condition codes have been set) if tabstop, else not 0
;-
tabstop:  
	IF r2 HIS #text+col80-1             ;every thing is a tabstop
	    clr r3                       ;after col 80 or we'll loop
	    rts pc                       ;endlessly
	FI 
		
	IF r2 = #text
	    mov #1,r3                    ;force cond codes to non zero
	ELSE 
	    mov r2,r3
	    sub #text,r3
	    bic tabmsk,r3               ;use tab mask
	FI 
	rts pc
;+
; compress - put the text buffer back onto lbuf1
; input - r1 points to text in `text' to compress
; output- lbuf1 has a null and nl terminated string (trimmed on right)
;         (later this will have code to insert tabs at the front)
		   
compress:      
	call saval
;       push r0
;       push r1
;       push r2
	mov #etext,r0				;delete trailing blanks
	DO FOREVER
	    IFB (r0) NE #space AND (r0) NE #nl ;stop on first non blank or nl
		BREAK 
	    FI 
			       
	    IF r0 LO r1                         ;don't go below buffer
		BREAK 
	    FI 
			       
	    dec r0				;next place
	OD 
		   
	clrb 2(r0)
	movb #nl,1(r0)				;put the newline back at end
		   
	mov #lbuf1,r2
	IF tabseen NE #0 AND r1 = #text AND ob = #0
	    call retab                           ;re tab if necessary
	FI 
	DO FOREVER
	    movb (r1)+,(r2)+
	    IF EQ
		BREAK 
	    FI 
	OD 
;	pop r2
;	pop r1
;	pop r0
	rts pc
;+
; update - update text buffer according to what came in
;
; input
;	iosred status block 2 words
;	ib     input buffer
; output
;	text buffer is updated
;	curser is updated
;-
		   
update:      
	IF  iosred+2 LE #0
	    rts pc
	FI 
		   
	inc chglin
		
;	push r0
;	push r1
;	push r2
	call saval
	mov iosred+2,r0
	add #ib,r0			;address of end of input
	clrb (r0)			;asciz it
		   
	mov #ib,r1
	mov curser,r2
	DO FOREVER
	    call eget
	    IF EQ
		BREAK 
	    FI 
	    call eput
	OD 
	mov r2,curser			;update curser
;	pop r2
;	pop r1
;	pop r0
	rts pc
;-
; tabforward - handle forward tabs
;+
		   
tabforward:      
	push r2
	mov curser,r2
	DO FOREVER
	    inc r2
	    IF r2 HIS #text+col80-2
		mov #text+col80-2,curser
		call tocurs
		TTY END
		BREAK 
	    FI 
			       
	    call tabstop
	    IF EQ
		mov r2,curser
		call tocurs
		TTY END
		BREAK 
	    FI 
			       
	OD 
	pop r2
	rts pc
;-
; tabback - handle backtabs
;+
tabback:  
	push r2
	mov curser,r2
	DO FOREVER
	    dec r2
	    IF r2 LOS #text
		mov #text,curser
		call tocurs
		TTY END
		BREAK 
	    FI 
			       
	    call tabstop
	    IF EQ
		mov r2,curser
		call tocurs
		TTY END
		BREAK 
	    FI 
			       
	OD 
	pop r2
	rts pc
		   
;+
; inschar - insert one blank at curser position
;-
inschar:  
	push r0
	mov #etext,r0
	DO FOREVER
	    movb -1(r0),(r0)
	    dec r0
	    IF R0 LOS curser
		movb #space,@curser
		BREAK 
	    FI 
	OD 
	pop r0
	rts pc
;+
; deleol - delete from cursor to end of line (add blanks)
;-
deleol:  
	push r2
	mov curser,r2
	DO WHILE r2 LOS #etext
	    movb #space,(r2)+
	OD 
	pop r2
	rts pc
;+
; delchar - delete one character at curser position
;-
delchar:  
	push r0
	mov curser,r0
	DO FOREVER
	    movb 1(r0),(r0)
	    inc r0
	    IF r0 HI #etext
		movb #space,etext
		BREAK 
	    FI 
	OD 
	pop r0
	rts pc
;+
; adjust - left justify text (up to cursor)
;-
adjust:      
	call saval
;       push r1
;       push r2
	mov curser,r2
	mov r2,r1
	DO FOREVER
	    IFB (r2) NE #space
		BREAK 
	    FI 
	    inc r2
	OD 
		   
	DO FOREVER
	    movb (r2)+,(r1)+
	    IF r2 HI #etext
		DO WHILE r1 LOS #etext
		    movb #space,(r1)+
		OD 
		BREAK 
	    FI 
	OD 
;       pop r2
;       pop r1
	rts pc
;+
; tocurs - output cursor movement codes to position at curser
;-
tocurs:      
	push r1
	mov curser,r1
	sub #text-2,r1
	CURSOR to cursl,r1         ;restore cursor
	pop r1
	rts pc
;+
; mov2wd	-	move to word
;+
mov2wd:      
	clr cycle
	mov curser,scurser
	movb @curser,r0
	call typch
	IF EQ
	    DO FOREVER
		call nextcol
		IF curser = #text
		    BREAK 
		FI 
		movb @curser,r0
		call typch
		IF NE
		    BREAK 
		FI 
	    OD 
	FI 
		   
	DO FOREVER
	    movb @curser,r0
	    call typch
	    IF EQ
		BREAK 
	    FI 
	    call nextcol
			       
	    IF cycle GT #1
		mov scurser,curser
		BREAK 
	    FI 
			       
	OD 
	rts pc
		   
nextcol:  
	inc curser
	IF curser HIS #text+col80-1
	    mov #text,curser
	    inc cycle
	FI 
	rts pc
;+
; typch - check r0 for char type
; input r0
; output condition codes set for either EQ -> alpha/numerical
;					NE -> not alpha/num
;-
typch:           
	IFB r0 HIS #'A   AND   r0 LOS #'Z
	    br 5000$
	FI 
	IFB r0 HIS #'a   AND   r0 LOS #'z
	    br 5000$
	FI 
	IFB r0 HIS #'0   AND   r0 LOS #'9
	    br 5000$
	FI 
	tst #1
	rts pc
5000$:  tst #0
	rts pc
		   
movright:      
	push r0
	push r2
	mov #text+col80,r2
	IFB (r2) = #space
	    dec r2
	    DO FOREVER
			                   
		IFB -(r2) NE #space
		    BREAK 
		FI 
			                   
		IF curser HIS r2
		    BREAK 
		FI 
			                   
		TTY esc 'P END
		nop 
	    OD 
	FI 
	pop r2
	pop r0
	rts pc
		   
saveln:      
	push r1
	push r0
	mov curser,r0
	mov #lbuf3,r1
	DO FOREVER
	    IF r0 HI #etext
		clrb (r1)
		BREAK 
	    FI 
	    movb (r0)+,(r1)+
	OD 
	pop r0
	pop r1
	rts pc
		   
edone:          
		   
	mov #etext,r2
	DO FOREVER                      ;find right most margin
	    IFB (r2) NE #space              ;either first space
		inc r2
		BREAK 
	    FI 
			   
	    IF r2 LOS curser                ;or cursor position
		BREAK 
	    FI 
	    dec r2
	OD 
		   
	mov #lbuf3,r1
	DO FOREVER
	    call eget
	    IF EQ
		BREAK 
	    FI 
	    call eput
	OD 
		   
	CURSOR to cursl,#1
	mov #text,r1
	LOOP for R2 = #1 TO #col80-1
	    TTY @R1
	    inc r1
	NEXT R2
	call tocurs
	TTY END
	rts pc
		
findend: 
	mov #etext,r0
	DO FOREVER
	    IF r0 LOS #text
		mov #text,r0
		BREAK 
	    FI 
			
	    IFB -(r0) NE  #space
		inc r0
		BREAK 
	    FI 
	OD 
	rts pc
		
;+
; matcher - match pbuf compiled match with text
;
;input - pbuf
;	r2 = start of search in text
;output- r2  = address of where text match occurs (or 0=no match)
;-
		
matcher: 
	push r0
	push r1
	push r3
	call findend                ;get end of line in r0
	mov r0,r3                   ;save
	movb #nl,(r0)+
	clrb (r0)                   ;nl and asciiz it for match routine
	mov r2,r0		;get starting location
	mov #pbuf,r1
	mov r0,r2                   ;copy
	DO FOREVER
	    call match
	    IF CC
		BREAK                           ;got match at r2
	    ELSE 
		inc r2
		mov r2,r0                       ;next col to try
		IFB (r0) = #0
		    clr r2
		    BREAK			;with no match
		FI 
	    FI 
	OD 
	movb #space,(r3)+
	movb #space,(r3)+
		
	pop r3
	pop r1
	pop r0
	rts pc
		
addline: 
	call saval
;	push r0
;	push r1
;	push r2
;	push r3
;	push r4
;	push r5
		
	mov #10$,r4                  ; address of routine to get chars
	mov dot,r5                   ; append after here
	call append                  ; do it
	sub #2,dot                   ; back up
		
;	pop r5
;	pop r4
;	pop r3
;	pop r2
;	pop r1
;	pop r0
	rts pc               ;done
		
10$:    mov #nl,lbuf1                ; put a null line into lbuf1
15$:    mov #20$,r4                  ; change input routine to end of file ret
	clc                          ; return one line
	rts pc
20$:    sec                          ; return end of input
	rts pc
		
		
		
		
	addlbuf=. 
	call saval                      ;
	mov #15$,r4                     ;this routine just appends
	mov dot,r5                      ;lbuf1 to the text buffer
	call append                     ;just after dot
	sub #2,dot                      ;it leaves dot in place
	rts pc
		
		
		
;+
; retab - replace leading spaces with tabs
; returns r1 adjusted to new beginning of buffer and
; preceding tabs inserted
;-
retab:    
	push r4
	push r3
	push r0
	mov r1,r3
	clr r4
	DO FOREVER                      ;count the leading spaces
	    IFB (r3)+ NE #space
		BREAK 
	    FI 
	    inc r4
	OD 
	bic #7,r4                       ;round down to number of tab columns
	add r4,r1                       ;this where the text starts
	asr r4
	asr r4                          ;
	asr r4                          ;this many tabs can be used instead
		
	LOOP for r0 = #1 TO r4          ;this many tabs go in
	    dec r1                        ;backup and
	    movb #tab,(r1)                ;precede buffer with a tab here
	NEXT r0
		
	pop r0
	pop r3
	pop r4
	rts pc
;+
;cut - handle the control c (break line at cursor)
;-
cut:        
	call saval
	call deleol                     ;delete  toend in text buffer
	TTY esc 'K END                  ;delete to end of line on screen
	call addlbuf                    ;add the current lbuf1 after dot
	call inline                     ;add it on the screen
	mov #lbuf1,r0                   ;and
	call ttystr                     ;output the new line
	call tocurs                     ;re position cursor
	TTY END
	rts pc
		
		
		
drag:                                   ;drag tag along with curser
;       IF tabmsk = #177774             ;if half tab only not implemented
	   IF oa NE #0
              LET coln = curser - #text + #1
           FI
;        FI
	rts pc


		
		
	.end 
