PR#3
PREFIX /A2OSX.SRC
NEW
INC 1
AUTO 6
				.LIST OFF
				.OP	65C02
				.OR	$2000
				.TF /A2OSX.BOOT/BIN/EDIT
*--------------------------------------
				.INB INC/MACROS.I
				.INB INC/A2OSX.I
*--------------------------------------
ZPPTR1			.EQ ZPBIN
ZPPTR2			.EQ ZPBIN+2
BufPtr			.EQ ZPBIN+4
BufPtrBackup	.EQ ZPBIN+6
ClipboardPtr	.EQ ZPBIN+8
*--------------------------------------
PromptBufferMax	.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 CS.END-CS.START		Code Length To Relocate
				.DA DS.END-DS.START		Data Segment to Allocate
				.DA 0
				.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.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			lda #$ff
				ldy #bSaved
				sta (pData),y
								
				>SYSCALL SYS.GetArgC
				cmp #1
				beq .1					No arg, new file....
				
				lda #1
				>SYSCALL SYS.GetArgA
				
				>SYSCALL SYS.GetFullPathYA
				txa
				ldy #hFileName
				sta (pData),y
				
				jsr LoadFile
				bcc .8					if CS, Failed to load file, keep name, but new file....
				
.1				>PUSHWI 1				buffer size = 0 (+ 1 for ending 0)
				>PUSHBI S.MEM.F.INIT0
				>SYSCALL SYS.GetMem
				bcs .9
				
				txa
				ldy #hBuffer			FileLen already set to 0
				sta (pData),y
				
.8				lda (pPs)
				ora #S.PS.F.EVENT		Now accept events
				sta (pPs)
				clc
.9				rts
*--------------------------------------
CS.RUN			ldy #bCancel
				lda (pData),y
				bne .9
	
				ldy #bInit
				lda (pData),y
				bne .8
	
				>LDYA L.SEQ.DETECT
				>SYSCALL SYS.PrintFYA	Send Query for term W & H
				bcs .9
				
.1				ldy #bCancel
				lda (pData),y
				bne .9
	
				>SYSCALL SYS.Sleep
				
				ldy #ScreenW			Wait for Response from terminal
				lda (pData),y			for W & H
				beq .1
	
				jsr SCRN.Init
				bcs .9
				
				ldy #bInit
				lda #$ff
				sta (pData),y
				
.8				clc
				rts
				
.9				sec				
				rts
*--------------------------------------
CS.DOEVENT		ldy #S.EVT.hDEV			is Event from active IN device?
				lda (pEvent),y
				ldy #S.PS.hINDEV
				cmp (pPs),y
				bne .9
				
				lda (pEvent)
				and #S.EVT.F.KEY		is it a KEY event?
				beq .9
				
				ldy #S.EVT.DATAHI		is it an O or SAPPLE key ?
				lda (pEvent),y
				bmi .9					test only OA, bug in Applewin for #,@...
*				bne	.9
				
				ldy #S.EVT.DATALO	
				lda (pEvent),y
				
				tax
				
				ldy #bEscMode
				lda (pData),y
				asl
				txa
				bcc .1
				
				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 CS.DOEVENT.Esc
				bcs .8					Not translated, exit
				
.1				pha
				ldy #PromptMode
				lda (pData),y
				tax
				pla
				jmp (J.PROMPT,x)

.8				clc
				rts
				
.9				sec
				rts		
*--------------------------------------
CS.DOEVENT.Esc	ldy #bEscMode
				lda #0
				sta (pData),y
				
				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
				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
				
				clc
				rts
*--------------------------------------
CS.QUIT			ldy #hBuffer
				lda (pData),y
				beq .1
				>SYSCALL SYS.FreeMemA
				
.1				ldy #hBufferBackup
				lda (pData),y
				beq .2
				>SYSCALL SYS.FreeMemA
				
.2				ldy #hClipBoard
				lda (pData),y
				beq .3
				>SYSCALL SYS.FreeMemA

.3				ldy #hFileName
				lda (pData),y
				beq .8
				>SYSCALL SYS.FreeMemA

.8				>LDYA L.SEQ.RESET
				>SYSCALL SYS.PrintFYA
				bcs .9
				lda #12
				>SYSCALL SYS.COutA
.9				rts
*--------------------------------------
LoadFile		>PUSHWI 0				Aux type
				>PUSHBI 4				S.FILEINFO.TYPE.TXT
				>PUSHBI	SYS.FOPEN.R
				ldy #hFileName
				lda (pData),y
				
				>SYSCALL SYS.GetMemPtrA
				
				>PUSHYA
				>SYSCALL SYS.LoadFile
				bcs .9
				
				stx hMem
				>STYA TmpLen
				iny
				bne .2
				inc						+1 for ending zero
				
.2				>PUSHYA
				>PUSHBI S.MEM.F.INIT0
				>SYSCALL SYS.GetMem
				bcs .98
				
				>STYA ZPPTR2
				txa
				ldy #hBuffer
				sta (pData),y
				
				lda hMem
				
				>SYSCALL SYS.GetMemPtrA
				>STYA ZPPTR1
				
				>LDYA TmpLen
				>STYA TmpCount

				ldy #0
				
.5				jsr DecTmpCount
				beq .6
				lda (ZPPTR1),y
				sta (ZPPTR2),y
				iny
				bne .5
				inc ZPPTR1+1
				inc ZPPTR2+1
				bra .5
				
.6				ldy #FileLen
				lda TmpLen
				sta (pData),y
				iny
				lda TmpLen+1
				sta (pData),y
				
				jsr .98
				clc
				rts
				
.98				pha
				lda hMem
				>SYSCALL SYS.FreeMemA
				pla				
				sec
.9				rts				
*--------------------------------------
SaveFile		ldy #hBuffer
				lda (pData),y
				>SYSCALL SYS.GetMemPtrA
				>PUSHYA
				
				ldy #FileLen+1
				>PUSHB (pData),y
				dey
				>PUSHB (pData),y
				
				>PUSHWI 0				Aux type
				>PUSHBI 4				S.FILEINFO.TYPE.TXT
				>PUSHBI	SYS.FOPEN.X+SYS.FOPEN.W
				ldy #hFileName
				lda (pData),y
				
				>SYSCALL SYS.GetMemPtrA
				
				>PUSHYA
				
				>SYSCALL SYS.SaveFile
				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 BIN/EDIT.S.BUF
				.INB BIN/EDIT.S.CTRL
				.INB BIN/EDIT.S.SCRN
*--------------------------------------
CS.END
SEQ.DETECT		>CSTRING "Querying terminal capabilities...\e[999;999H\e[6n"
SEQ.INIT		>CSTRING "\ec\e[2;%dr"
SEQ.RESET		>CSTRING "\ec"
SEQ.INV			>CSTRING "\e[7m"
SEQ.NORM		>CSTRING "\e[0m"
SEQ.CEOL		>CSTRING "\e[K"
MSG.TOPBAR		>CSTRING "\e[1;1H\e[7mA2osX Edit:%S\e[K\e[0m"
MSG.STATUSBAR	>CSTRING "\e[%d;1H\e[7mPress Ctrl-A For Help                 Length:%D Col:%d Line:%D\e[K\e[0m"
MSG.STATUSBAR.H	>CSTRING "\e[%d;1H\e[7mPress any key to exit this screen:\e[K\e[0m"
MSG.STATUSBAR.F	>CSTRING "\e[%d;1H\e[7mFind:%S\e[K\e[0m"
MSG.STATUSBAR.R	>CSTRING "\e[%d;1H\e[7mReplace:%S\e[K\e[0m"
MSG.STATUSBAR.S	>CSTRING "\e[%d;1H\e[7mSave:%S\e[K\e[0m"
MSG.STATUSBAR.E	>CSTRING "\e[%d;1H\e[7mError:%h\e[K\e[0m"
MSG.GOTOXY		>CSTRING "\e[%d;%dH"
MSG.HELPLINE	>CSTRING "%S%s\n"
MSG.HELP		>PSTRING "All commands: (* = Not yet implemented)"
				>PSTRING "------------"
				>PSTRING "  Ctrl-A : This help screen"
				>PSTRING "  Ctrl-B : Mark beginning of selection"
				>PSTRING "  Ctrl-C : Copy selection to clipboard"
				>PSTRING "  Ctrl-D : Delete selection"
				>PSTRING "  Ctrl-E : Mark end of selection"
				>PSTRING "  Ctrl-F : * Find a string..."
				>PSTRING "  Ctrl-G : Go to start of current line"
				>PSTRING "  Ctrl-L : Go to end of current line"
				>PSTRING "  Ctrl-N : * Find next"
				>PSTRING "  Ctrl-O : * Page up"
				>PSTRING "  Ctrl-P : * Page down"
				>PSTRING "  Ctrl-Q : Quit"
				>PSTRING "  Ctrl-R : * Replace found string with..."
				>PSTRING "  Ctrl-S : Save file to disk..."
				>PSTRING "  Ctrl-T : Go to top of file"
				>PSTRING "  Ctrl-V : Past clipboard at cursor position"
				>PSTRING "  Ctrl-W : * Erase current line"
				>PSTRING "  Ctrl-X : Cut block to clipboard"
				>PSTRING "  Ctrl-Y : * Go to bottom of file"
				>PSTRING "  Ctrl-Z : * Undo previous change"
				.HS 00
*--------------------------------------
MSG.NEWFILE		>PSTRING "(new file)"
EscChars		>PSTRING "DBAC"
EscAscii		.HS 04080A0B15
hMem			.BS 1
TmpFilename		.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
*--------------------------------------
				.DUMMY
				.OR 0
DS.START
PromptBuffer	.BS PromptBufferMax+1
PromptMode		.BS 1
bCancel			.BS 1
bInit			.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
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 BIN/EDIT.S
ASM
