NEW
  AUTO 3,1
				.LIST OFF
*--------------------------------------
* ^A = Help
* ^B = Begin Block
* ^C = Copy Block
* ^D = Delete Block
* ^E = End Block
* ^F = Find...
* ^G = Go To SOL
******** ^H = BS
******** ^I = TAB
******** ^J = LF
******** ^K = VT
* ^L = Go To EOL
******** ^M = CR
* ^N = Find Next
* ^O = Page Up
* ^P = Page Down
* ^Q = Quit
* ^R = Replace...
* ^S = Save
* ^T = Go To Top of file
******** ^U = FS 
* ^V = Paste Block
* ^W = Erase line
* ^X = Cut Block
* ^Y = End Of File
*--------------------------------------
Ctrl.DEL		jsr BUF.ComputeCOffset
	
				lda BUF.COffset
				ora BUF.COffset+1		No Backspace possible
				beq .8

				jsr Ctrl.BS
				bcs .9
				jmp Ctrl.Z

.8				clc
.9				rts
*--------------------------------------
Ctrl.Invalid	
				clc
Ctrl.Invalid.RTS
				rts
*--------------------------------------
Ctrl.A			>STZ.G bUpdateStatus

				>LDYA L.MSG.HELP
				>STYA ZPPTR1

				stz TmpByte

.1				ldx #0
				ldy TmpByte
				jsr SCRN.GotoXY
				bcs .9
				
				lda (ZPPTR1)
				beq .8
				
				>PUSHW L.MSG.HELPLINE
				>PUSHW ZPPTR1
				>PUSHBI 2
				>SYSCALL PrintF
				bcs .9
				
				inc TmpByte
				
				lda (ZPPTR1)
				sec
				adc ZPPTR1
				sta ZPPTR1
				bcc .1
				inc ZPPTR1+1
				bra .1

.8				jsr SCRN.ResetStatusBar

				>PUSHW LineBufPtr
				>PUSHW L.MSG.STATUSBAR.H
				>PUSHBI 0
				jsr SCRN.LineBufSprintf
				bcs .9
				
				jsr SCRN.LineBufOut
				bcs .9

				jsr GetCh
				bcs .9
				
				jmp SCRN.Repaint
				
.9				rts
*--------------------------------------
Ctrl.C			jmp BUF.SelToClipboard
*--------------------------------------
Ctrl.B			jsr BUF.ComputeCOffset

				lda BUF.COffset
				>STA.G SelStart

				lda BUF.COffset+1
				iny
				sta (pData),y
				jmp SCRN.UpdateMain
*--------------------------------------
Ctrl.E			jsr BUF.ComputeCOffset

				lda BUF.COffset
				>STA.G SelEnd

				lda BUF.COffset+1
				iny
				sta (pData),y
				jmp SCRN.UpdateMain
*--------------------------------------
Ctrl.G			>LDA.G FileX
				beq .8

				lda #0
				sta (pData),y
				
				jmp SCRN.UpdateViewPort

.8				clc
				rts
*--------------------------------------
Ctrl.BS			>LDA.G FileX
				beq .1					already at beginning of line

				dec
				sta (pData),y
				bra .3

.1				>LDA.G FileY+1
				tax
				dey
				ora (pData),y
				beq .8

				lda (pData),y
				bne .2

				dex

.2				dec
				jsr BUF.GetLineAX
				bcs .8

				tya
				>STA.G FileX

				>DECW.G FileY

.3				jmp SCRN.UpdateViewPort

.8				clc
				rts
*--------------------------------------
Ctrl.TAB		jsr BUF.InsertTAB
				bcs .9

				lda TmpByte
				beq .9					nothing to do
				
*				clc
				>ADC.G FileX
				sta (pData),y
				
				>LDA.G CurX
				clc
				adc TmpByte
				sta (pData),y
				
				sec
				>SBC.G ScreenX
				sec
				>SBC.G ScreenW
				bcs .1

				jsr SCRN.UpdateCurrentLine
				bcs .9
				
				bra .8
				
.1				lda TmpByte
				clc
				>ADC.G ScreenX
				sta (pData),y

				jsr SCRN.UpdateViewPort
				
.8				jsr SCRN.GotoCurXCurY			
				jmp SCRN.UpdateStatusBar
				
.9				rts
*--------------------------------------
Ctrl.LF			jsr BUF.GetNextLine
				bcs .8

				tya						Y = line length
				>CMP.G FileX
				bcs .2
				
				sta (pData),y

.2				>INCW.G FileY

				jmp SCRN.UpdateViewPort

.8				clc
				rts
*--------------------------------------
Ctrl.VT			>LDA.G FileY+1
				tax
				dey
				ora (pData),y
				beq .8

				lda (pData),y
				bne .1

				dex

.1				dec
				jsr BUF.GetLineAX
				bcs .8

				tya						Y = line length
				>CMP.G FileX
				bcs .2

				sta (pData),y
				
.2				>DECW.G FileY

				jmp SCRN.UpdateViewPort
.8
Ctrl.VT.8		clc
				rts
*--------------------------------------
Ctrl.L			jsr BUF.GetLine
				bcs Ctrl.VT.8

				tya						Y = line length
				>STA.G FileX

				jmp SCRN.UpdateViewPort
*--------------------------------------
Ctrl.CR			lda #C.CR
				jsr BUF.InsertA
				bcs .99

				jsr SCRN.LineBufInitEdit	CEOL

				>STZ.G FileX
				>INCW.G FileY
				
				>STZ.G CurX

				>LDA.G CurY
				>CMP.G ViewPortHm1
				bne .1
				
				jsr SCRN.LineBufOut		Bottom line, all done, just CEOL
				bcs .99
				jmp SCRN.GotoCurXCurY
				
.1				dec
				>CMP.G ViewPortHm1
				beq .6					No scroll needed
				
				>PUSHW LineBufPtr
				>PUSHW L.SEQ.SCROLLCURDN

				>LDA.G CurY
				inc
				inc
				>PUSHA
				>LDA.G ScreenH
				dec
				>PUSHA
				>PUSHBI 2
				jsr SCRN.LineBufSprintf
				
.99				bcs .9

				>PUSHW LineBufPtr
				>PUSHW L.SEQ.SCROLLRGN
				>LDA.G ScreenH
				dec
				>PUSHA
				>PUSHBI 1
				jsr SCRN.LineBufSprintf
				bcs .9

.6				jsr SCRN.LineBufOut
				bcs .9

				>INC.G CurY
				jsr BUF.ComputeCOffset
				jsr SCRN.UpdateCurrentLine
				jmp SCRN.GotoCurXCurY

.9				rts
*--------------------------------------
Ctrl.O			ldy #FileY+1
				lda (pData),y
				tax
				dey
				lda (pData),y

				ldy #ViewPortH
				sec
				sbc (pData),y
				pha
				txa
				sbc #0
				bcc .8
				
				ldy #FileY+1
				sta (pData),y
				dey
				pla
				sta (pData),y

				>LDA.G ScreenY
				sec
				>SBC.G ViewPortH
				>STA.G ScreenY
				>LDA.G ScreenY+1
				sbc #0
				sta (pData),y

				>STZ.G FileX
				>STA.G ScreenX

				jmp SCRN.UpdateMainAndStatus

.8				pla
*--------------------------------------
Ctrl.T			jsr SCRN.Home
				jmp SCRN.UpdateMainAndStatus
*--------------------------------------
Ctrl.P			ldy #FileY+1
				lda (pData),y
				tax
				dey
				lda (pData),y

				ldy #ViewPortH
				clc
				adc (pData),y
				bcc .1

				inx

.1				pha
				phx
				jsr BUF.GetLineAX		A,X = FileY+PAGE
				bcs .8

				ldy #FileY+1
				pla
				sta (pData),y

				dey
				pla
				sta (pData),y
				
				>LDA.G ViewPortH
				clc
				>ADC.G ScreenY
				sta (pData),y
				iny
				lda (pData),y
				adc #0
				sta (pData),y
				
				>STZ.G FileX
				>STA.G ScreenX

				jmp SCRN.UpdateMainAndStatus

.8				pla
				pla
				clc
				rts
*--------------------------------------
Ctrl.Y			jsr BUF.GetNextLine
				bcs .1

				>INCW.G FileY
				bra Ctrl.Y

.1				jsr BUF.GetLine

				tya						Y = line length
				>STA.G FileX

				>LDA.G FileY
				sec
				>SBC.G ViewPortHm1
				tax
				>LDA.G FileY+1
				sbc #0
				bcs .2
				
				>LDA.G FileY
				tax
				lda #0
				
.2				>STA.G ScreenY+1
				dey
				txa
				sta (pData),y
				
				jmp SCRN.UpdateMainAndStatus
*--------------------------------------
Ctrl.F			>LDYA L.DLG.FIND
				jsr DLG.Open
				jsr DLG.Run

				cmp #C.CR
				bne .8
	

.8				jsr DLG.Close
				
				clc
				rts
*--------------------------------------
Ctrl.N
				clc
				rts
*--------------------------------------
Ctrl.R			clc
				rts
*--------------------------------------
Ctrl.Q			>LDA.G bUnSaved
				beq .8
				
				>STZ.G bUpdateStatus

				jsr SCRN.ResetStatusBar

				>PUSHW LineBufPtr
				>PUSHW L.MSG.STATUSBAR.U
				>PUSHBI 0
				jsr SCRN.LineBufSprintf
				bcs .9
				
				jsr SCRN.LineBufOut
				bcs .9

.1				jsr GetCh
				bcs .9
	
				cmp #'a
				bcc .2
				cmp #'z'+1
				bcs .2
				
				eor #$20
				
.2				cmp #'Y'
				beq .8
	
				cmp #'N'
				beq .80
				
				bra .1
	
.8				lda #$ff
				>STA.G bExit

.80				jsr SCRN.UpdateStatusBar
				jmp SCRN.GotoCurXCurY

.9				rts
*--------------------------------------
Ctrl.S			>LDYA L.DLG.SAVE
				jsr DLG.Open
				jsr DLG.Run

				pha
				
				jsr DLG.Close
	
				pla
				cmp #C.CR
				bne .8
				
				jsr FILE.Save
				bcc .1
	
				>PUSHA					ERROR CODE
				
				jsr SCRN.ResetStatusBar

				>PUSHBI 1
				>PUSHW L.MSG.STATUSBAR.E
				bra .2

.1				jsr SCRN.UpdateTopBar
				
				jsr SCRN.ResetStatusBar
				
				>PUSHW LineBufPtr
				>PUSHW L.MSG.STATUSBAR.S
				>LDA.G hFileName
				>SYSCALL GetMemPtr
				>PUSHYA
				
				>PUSHBI 2
				
.2				jsr SCRN.LineBufSprintf

				jsr SCRN.LineBufOut
				bcs .9

.8				jmp SCRN.GotoCurXCurY

.9				rts
*--------------------------------------
Ctrl.FS			jsr BUF.GetLine
				bcs .8

				tya						Y = line length
				>CMP.G FileX
				beq .1					already at end of line

				lda (pData),y
				inc
				sta (pData),y
				bra .3

.1				jsr BUF.GetNextLine
				bcs .8

				>INCW.G FileY

				>STZ.G FileX

.3				jmp SCRN.UpdateViewPort

.8				clc
.9				rts
*--------------------------------------
Ctrl.V			jsr BUF.InsertClipboard
				bcs .9
				jsr SCRN.UnSaved
				jmp SCRN.UpdateMainAndStatus

.9				rts
*--------------------------------------
Ctrl.W			jsr BUF.ComputeCOffset	Y=line len
				phy

				lda BUF.LOffset
				ldy #SelStart
				sta (pData),y
				lda BUF.LOffset+1
				iny
				sta (pData),y

				ply						Get back line len
				clc
				lda (BufPtr),y
				beq .1					last line \0 ending 
				sec						delete CR
.1				tya

				adc BUF.LOffset
				ldy #SelEnd
				sta (pData),y
				lda BUF.LOffset+1
				adc #0
				iny
				sta (pData),y
				bra Ctrl.D
.8
Ctrl.W.RTS		rts
*--------------------------------------
Ctrl.X			jsr BUF.SelToClipboard
				bcs Ctrl.W.RTS

Ctrl.D			jsr BUF.DelSel
				bcs Ctrl.D.8
				jsr BUF.ResetSel
				jsr SCRN.CursorAdjust
				jsr SCRN.UnSaved		will update TopBar
				jmp SCRN.UpdateMainAndStatus
				
Ctrl.D.8		clc
				rts
*--------------------------------------
Ctrl.Z			jsr BUF.ComputeCOffset

				lda BUF.COffset
				eor FileLen
				bne .1
				lda BUF.COffset+1
				eor FileLen+1
				beq Ctrl.D.8

.1				jsr BUF.DelCharAtCursor
				bcs Ctrl.W.RTS

				cmp #C.CR				did we DEL a CR ?
				bne .8

				>LDA.G CurY

				>CMP.G ViewPortHm1
				beq .8					Bottom line, all done
				
				dec
				>CMP.G ViewPortHm1
				beq .8					No scroll needed
				
				>PUSHW L.SEQ.SCROLLCURUP
				>LDA.G CurY
				inc
				inc
				>PUSHA
				>LDA.G ScreenH
				dec
				>PUSHA
				>PUSHBI 2
				>SYSCALL PrintF
				bcs .9

				jsr SCRN.ResetScrollRgn
				bcs .9
				
				jsr SCRN.UpdateCurrentLine
				bcs .9

				jsr SCRN.UpdateLast
				bcs .9
				
.8				jsr SCRN.UpdateCurrentLine
				bcs .9

.80				jsr SCRN.UnSaved
				jsr SCRN.GotoCurXCurY
				jmp SCRN.UpdateStatusBar
.9				rts				
*--------------------------------------
MAN
SAVE usr/src/bin/edit.s.ctrl
LOAD usr/src/bin/edit.s
ASM
