NEW
PREFIX
AUTO 4,1
				.LIST OFF
				.OP	65C02
				.OR	$2000
				.TF BIN/EDIT
*--------------------------------------
				.INB INC/MACROS.I
				.INB INC/A2OSX.I
				.INB INC/MLI.I
*--------------------------------------
				.DUMMY
				.OR ZPBIN
ZS.START
ZPPTR1			.BS 2
ZPPTR2			.BS 2
BufPtr			.BS 2
BufPtrBackup	.BS 2
ClipboardPtr	.BS 2
hMem			.BS 1
TmpByte			.BS 1
TmpLen			.BS 2
TmpCount		.BS 2
TmpIndex		.BS 2
SelLen			.BS 2
BUF.LOffset		.BS 2
BUF.COffset		.BS 2
BUF.TmpLine1	.BS 2
BUF.TmpLine2	.BS 2
bNotRepaint		.BS 1
bSelected		.BS	1
ZS.END			.ED
*--------------------------------------
PromptBufMax	.EQ 64
PromptModeHelp	.EQ 2
PromptModeFind	.EQ 4
PromptModeReplace	.EQ 6
PromptModeSave	.EQ 8
*--------------------------------------
*			File Header (16 Bytes)
*--------------------------------------
CS.START		cld
				jmp (.1,x)
				.DA #$61				6502,Level 1 (65c02)
				.DA #1					BIN Layout Version 1
				.DA #0
				.DA #0
				.DA CS.END-CS.START		CS
				.DA DS.END-DS.START		DS
				.DA #16					SS
				.DA #ZS.END-ZS.START	Zero Page Size
				.DA 0
*--------------------------------------
* Relocation Table
*--------------------------------------				
.1				.DA CS.INIT
				.DA CS.RUN
				.DA CS.DOEVENT		
				.DA	CS.QUIT
L.SEQ.DETECT	.DA SEQ.DETECT
L.SEQ.INIT		.DA SEQ.INIT
L.SEQ.RESET		.DA SEQ.RESET
L.SEQ.SCROLLUP	.DA SEQ.SCROLLUP
L.SEQ.SCROLLDN	.DA SEQ.SCROLLDN
L.SEQ.INV		.DA SEQ.INV
L.SEQ.NORM		.DA SEQ.NORM
L.SEQ.CEOL		.DA SEQ.CEOL
L.MSG.TOPBAR	.DA MSG.TOPBAR
L.MSG.STATUSBAR	.DA MSG.STATUSBAR
L.MSG.STATUSBAR.H	.DA MSG.STATUSBAR.H
L.MSG.STATUSBAR.F	.DA MSG.STATUSBAR.F
L.MSG.STATUSBAR.R	.DA MSG.STATUSBAR.R
L.MSG.STATUSBAR.S	.DA MSG.STATUSBAR.S
L.MSG.STATUSBAR.E	.DA MSG.STATUSBAR.E
L.MSG.NEWFILE	.DA MSG.NEWFILE
L.MSG.GOTOXY	.DA MSG.GOTOXY
L.MSG.HELPLINE	.DA MSG.HELPLINE
L.MSG.HELP		.DA MSG.HELP
J.PROMPT		.DA Prompt.Editor
				.DA Prompt.Help
				.DA Prompt.Find
				.DA Prompt.Replace
				.DA Prompt.Save
J.CTRL			.DA Ctrl.Invalid
				.DA Ctrl.A
				.DA Ctrl.B
				.DA Ctrl.C
				.DA Ctrl.D
				.DA Ctrl.E
				.DA Ctrl.F
				.DA Ctrl.G
				.DA Ctrl.BS
				.DA Ctrl.TAB
				.DA Ctrl.LF
				.DA Ctrl.VT
				.DA Ctrl.L
				.DA Ctrl.CR
				.DA Ctrl.N
				.DA Ctrl.O
				.DA Ctrl.P
				.DA Ctrl.Q
				.DA Ctrl.R
				.DA Ctrl.S
				.DA Ctrl.T
				.DA Ctrl.FS
				.DA Ctrl.V
				.DA Ctrl.W
				.DA Ctrl.X
				.DA Ctrl.Y
				.DA Ctrl.Z
				.DA Ctrl.ESC
				.DA Ctrl.Invalid
				.DA Ctrl.Invalid
				.DA Ctrl.Invalid
				.DA Ctrl.Invalid
				.DA 0
*--------------------------------------
CS.INIT			clc
				rts
*--------------------------------------
CS.RUN			>PUSHBI 0
				>LDYA L.SEQ.DETECT
				>SYSCALL printf			Send Query for term W & H
				bcs CS.RUN.RTS
				
.1				>SYSCALL GetChar
				bcs CS.RUN.RTS
				
				jsr CharIn
				
				>LDA.G ScreenW			Wait for Response from terminal for W & H
				beq .1
				
				lda #1
				>SYSCALL ArgV
				bcs .2					No arg, new file....
						
				>SYSCALL RealPath
				txa
				>STA.G hFileName
				
				jsr LoadFile
				bcc .3					if CS, Failed to load file, keep name, but new file....
				
.2				>LDYAI 1				buffer size = 0 (+ 1 for ending 0)
				>SYSCALL GetMem0
				bcs CS.RUN.RTS
				
				txa
				>STA.G hBuffer			FileLen already set to 0
				
.3				lda #$ff
				>STA.G bSaved
				
				jsr SCRN.Init
.9				bcs CS.RUN.RTS
				
CS.RUN.LOOP		>SYSCALL GetChar
				bcs CS.RUN.RTS			I/O eror
				
.1				jsr CharIn
				
				>LDA.G bCancel
				bpl CS.RUN.LOOP
	
				>PUSHBI 0
				>LDYA L.SEQ.RESET
				>SYSCALL printf

				lda #0
				sec
CS.RUN.RTS		rts
*--------------------------------------
CS.DOEVENT		sec
				rts
*--------------------------------------
CS.QUIT			ldy #hBuffer
				jsr .8
				
				ldy #hBufferBackup
				jsr .8
				
				ldy #hClipBoard
				jsr .8

				ldy #hFileName
				
.8				lda (pData),y
				beq .9
				>SYSCALL FreeMem
.9				rts
*--------------------------------------
CharIn			tax

				>LDA.G bEscMode

				asl
				txa
				bcc .1					Not in ESC mode
				
				cmp #'['
				beq .8

				ldy #EscBuffer
				lda (pData),y
				inc
				sta (pData),y
				
				clc
				adc #EscBuffer
				tay
				txa
				sta (pData),y
				
				cmp #64

				bcc .8
				jsr CharIn.Esc
				bcs .8					Not translated, exit
				
.1				cmp #C.ESC
				bne .2

				lda #$ff
				>STA.G bEscMode
				inc
				>STA.G EscBuffer

.8				clc
				rts
				
.2				pha
				>LDA.G PromptMode
				tax
				pla
				jmp (J.PROMPT,x)
*--------------------------------------
CharIn.Esc		>STZ.G bEscMode
				
				ldy #EscBuffer
				lda (pData),y
				clc
				adc #EscBuffer
				tay
				
				lda (pData),y
				
				ldx EscChars
				
.1				cmp EscChars,x

				beq .2
				dex
				bne .1
				
				cmp #'R'				Response to cursor position query?
				beq .3
				
				sec
				rts
				
.2				lda EscAscii,x
				clc
				rts
				
.3				stz TmpByte
				
				ldy #EscBuffer+1
				
.4				lda (pData),y
				cmp #';'
				beq .5
				
				and #$0f
				pha
				lda TmpByte
				asl
				asl
				clc
				adc TmpByte
				asl
				sta TmpByte
				pla
				clc
				adc TmpByte
				sta TmpByte
				iny
				bra .4
				
.5				phy

				lda TmpByte
				ldy #ScreenH
				sta (pData),y
				dec						Remove 2 (top & status bar) for viewportH
				dec
				ldy #ViewPortH
				sta (pData),y
				dec
				ldy #ViewPortHm1		Remove 1 more to store offsert to last line of viewport
				sta (pData),y
				
				ply
				
				stz TmpByte
				
				iny
				
.6				lda (pData),y
				cmp #'R'
				beq .7
				
				and #$0f
				pha
				lda TmpByte
				asl
				asl
				clc
				adc TmpByte
				asl
				sta TmpByte
				pla
				clc
				adc TmpByte
				sta TmpByte
				iny
				bra .6
				
.7				lda TmpByte
				ldy #ScreenW
				sta (pData),y
				
				sec
				rts
*--------------------------------------
LoadFile		>PUSHWI 0				Aux type
				>PUSHBI S.FI.T.TXT
				>PUSHBI	O.RDONLY
				>LDA.G hFileName
				
				>SYSCALL GetMemPtr
				
				>SYSCALL LoadTXTFile
				bcs .9
				
				stx hMem
				iny
				bne .1
				inc						Add 1 for \0
				
.1				>SYSCALL GetMem
				bcs .98
				
				>STYA ZPPTR2
				txa
				>STA.G hBuffer
				
				lda hMem
				
				>SYSCALL GetMemPtr
				>STYA ZPPTR1
				
				stz TmpLen
				stz TmpLen+1

				ldy #0
				ldx #0
				
.2				lda (ZPPTR1),y
				beq .8

				cpx #C.CR
				bne .3
				cmp #C.LF
				beq .4
				
.3				jsr PutBytePtr2
				
.4				tax						set previous char				
				iny
				bne .2
				inc ZPPTR1+1
				bra .2
				
.8				>LDYA TmpLen
				>STYA.G FileLen
				
				lda #0
				jsr PutBytePtr2			set ending \0

				jsr .98
				clc
				rts
				
.98				pha
				lda hMem
				>SYSCALL FreeMem
				pla				
				sec
.9				rts				
*--------------------------------------
PutBytePtr2		sta (ZPPTR2)
				inc ZPPTR2
				bne .1
				inc ZPPTR2+1
				
.1				inc TmpLen
				bne .8
				inc TmpLen+1
				
.8				rts		
*--------------------------------------
SaveFile		>PUSHWI 0				Aux type
				>PUSHBI S.FI.T.TXT
				>PUSHBI	O.CREATE+O.WRONLY
				>LDA.G hFileName
				>SYSCALL GetMemPtr

				>SYSCALL FOpen
				bcs .9
				
				pha
				
				>PUSHW.G FileLen
				
				>LDA.G hBuffer
				>SYSCALL GetMemPtr
				>PUSHYA
				
				pla
				pha
			
				>SYSCALL FWrite
				bcc .1

				tax
				pla
				phx
				>SYSCALL FClose
				pla
				sec
				rts
				
.1				pla
				>SYSCALL FClose				
				
.9				rts
*--------------------------------------
DecTmpCount		lda TmpCount
				bne .1
				lda TmpCount+1
				beq .9
				dec TmpCount+1
.1				dec TmpCount
				ora #$ff				make sure NZ
.9				rts				
*--------------------------------------
				.INB USR/SRC/BIN/EDIT.S.BUF
				.INB USR/SRC/BIN/EDIT.S.CTRL
				.INB USR/SRC/BIN/EDIT.S.SCRN
*--------------------------------------
CS.END
SEQ.DETECT		.AZ "Querying terminal capabilities...\e[999;999H\e[6n"
SEQ.INIT		.AZ "\ec\e[7h\e[2;%dr"		\e[?6h
SEQ.RESET		.AZ "\ec"
SEQ.SCROLLUP	.AZ "\e[%d;%dH\eD"
SEQ.SCROLLDN	.AZ "\e[%d;%dH\eM"
SEQ.INV			.AZ "\e[7m"
SEQ.NORM		.AZ "\e[0m"
SEQ.CEOL		.AZ "\e[K"
MSG.TOPBAR		.AZ "\e[1;1HA2osX Edit:%s\e[0m"
MSG.STATUSBAR	.AZ "Press Ctrl-A For Help      Length:%5D,Col:%3d,Line:%5D"
MSG.STATUSBAR.H	.AZ "Press any key to exit this screen:\e[0m"
MSG.STATUSBAR.F	.AZ "Find:%s\e[0m"
MSG.STATUSBAR.R	.AZ "Replace:%s\e[0m"
MSG.STATUSBAR.S	.AZ "Save:%s\e[0m"
MSG.STATUSBAR.E	.AZ "Error:%h\e[0m"
MSG.GOTOXY		.AZ "\e[%d;%dH"
MSG.HELPLINE	.AZ "%S\e[K"
MSG.HELP		>PSTR "All commands: (* = Not yet implemented)"
				>PSTR "------------"
				>PSTR "  Ctrl-A : This help screen"
				>PSTR "  Ctrl-B : Mark beginning of selection"
				>PSTR "  Ctrl-C : Copy selection to clipboard"
				>PSTR "  Ctrl-D : Delete selection"
				>PSTR "  Ctrl-E : Mark end of selection"
				>PSTR "  Ctrl-F : * Find a string..."
				>PSTR "  Ctrl-G : Go to start of current line"
				>PSTR "  Ctrl-L : Go to end of current line"
				>PSTR "  Ctrl-N : * Find next"
				>PSTR "  Ctrl-O : Page up"
				>PSTR "  Ctrl-P : Page down"
				>PSTR "  Ctrl-Q : Quit"
				>PSTR "  Ctrl-R : * Replace found string with..."
				>PSTR "  Ctrl-S : Save file to disk..."
				>PSTR "  Ctrl-T : Go to top of file"
				>PSTR "  Ctrl-V : Past clipboard at cursor position"
				>PSTR "  Ctrl-W : Erase current line"
				>PSTR "  Ctrl-X : Cut block to clipboard"
				>PSTR "  Ctrl-Y : Go to end of file"
				>PSTR "  Ctrl-Z : * Undo previous change"
				.HS 00
*--------------------------------------
MSG.NEWFILE		.AZ "(new file)"
EscChars		>PSTR "DBAC"
EscAscii		.HS 04080A0B15
*--------------------------------------
				.DUMMY
				.OR 0
DS.START
PromptBuf		.BS PromptBufMax+1
PromptBufPtr	.BS 1
PromptMode		.BS 1
bCancel			.BS 1
bSaved			.BS 1
bEscMode		.BS 1
EscBuffer		.BS 16
hFileName		.BS 1
hFind			.BS 1
hReplace		.BS 1
hBuffer			.BS 1
hBufferBackup	.BS 1
FileLen			.BS 2
hClipBoard		.BS 1
ClipBoardLen	.BS 2
ScreenW			.BS 1
ScreenH			.BS 1
ViewPortH		.BS 1
ViewPortHm1		.BS 1
ScreenX			.BS 1
ScreenY			.BS 2
CurX			.BS 1
CurY			.BS 1
FileX			.BS 1
FileY			.BS 2
SelStart		.BS 2
SelEnd			.BS 2
DS.END
				.ED
*--------------------------------------
MAN
SAVE USR/SRC/BIN/EDIT.S
ASM
