NEW
PREFIX
AUTO 4,1
				.LIST OFF
*--------------------------------------
BUF.InsertClipboard
				ldy #hClipBoard
				lda (pData),y
				beq .8
				
				>SYSCALL GetMemPtr
				>STYA ClipboardPtr
				
				jsr BUF.ComputeCOffset

				ldy #FileLen
				lda (pData),y
				sec
				ldy #ClipboardLen
				adc (pData),y
				pha
				ldy #FileLen+1
				lda (pData),y
				ldy #ClipboardLen+1
				adc (pData),y
				ply
				jsr BUF.GetNewYA
				bcs .9
				
				>LDYA BUF.COffset
				>STYA TmpCount
				
				jsr BUF.CopyCount

				ldy #ClipboardLen
				lda (pData),y
				sta TmpCount
				iny
				lda (pData),y
				sta TmpCount+1
				
				jsr BUF.CopyClipboard
				
				jsr BUF.CopyTail
				
.4				ldy #ClipboardLen+1
				lda (pData),y
				tax
				dey
				lda (pData),y
				clc
				ldy #FileLen
				adc (pData),y
				sta (pData),y			
				iny
				txa
				adc (pData),y
				sta (pData),y
		
.8				clc	
.9				rts
*--------------------------------------
BUF.InsertA		sta TmpByte
				jsr BUF.ComputeCOffset
				cpy #255
				bne .10
				clc
				rts
				
.10				ldy #FileLen
				lda (pData),y
				sec
				adc #1
				pha
				iny
				lda (pData),y
				adc #0
				ply
				jsr BUF.GetNewYA
				bcs .9
				
				>LDYA BUF.COffset
				>STYA TmpCount
				
.1				jsr BUF.CopyCount

.2				lda TmpByte
				sta (BufPtr)
				inc BufPtr
				bne .3
				inc BufPtr+1
				
.3				jsr BUF.CopyTail
				
.4				>INCW.G FileLen
				
.8				clc	
.9				rts
*--------------------------------------
BUF.DelSel		jsr BUF.GetSelLen
				bcs .8
				>STYA SelLen
				
.10				jsr BUF.ComputeCOffset
								
				ldy #SelStart
				lda (pData),y
				sta TmpCount
				sec
				sbc BUF.COffset
				iny
				lda (pData),y
				sta TmpCount+1
				sbc BUF.COffset+1
				bcs .11
				
				ldy #FileY
				lda (pData),y
				sec
				sbc #1
				sta (pData),y
				iny
				lda (pData),y				
				sbc #0
				sta (pData),y
								
				ldy #FileX
				lda #0
				sta (pData),y

				bra .10

.11				ldy #FileLen
				lda (pData),y
				sec
				sbc SelLen
				pha
				iny
				lda (pData),y
				sbc SelLen+1
				ply
				iny
				bne .1
				inc
.1				jsr BUF.GetNewYA
				bcs .9
			
				jsr BUF.CopyCount
				
				lda BufPtrBackup
				clc
				adc SelLen
				sta BufPtrBackup
				
				lda BufPtrBackup+1
				adc SelLen+1
				sta BufPtrBackup+1
				
				jsr BUF.CopyTail
				
				ldy #FileLen
				lda (pData),y
				sec
				sbc SelLen
				sta (pData),y
				ldy #FileLen+1
				lda (pData),y
				sbc SelLen+1
				sta (pData),y

				jsr BUF.ResetSel
				
.8				clc
.9				rts				
*--------------------------------------
BUF.Del			ldy #FileLen
				lda (pData),y
				iny
				ora (pData),y
				bne .10
				clc						exit with A=0
				rts
		
.10				jsr BUF.ComputeCOffset
				lda BUF.COffset
				clc
				ldy #FileLen
				sbc (pData),y
				lda BUF.COffset+1
				iny
				sbc (pData),y
				bcc .11
				lda #0					exit with A=0
				clc
				rts
		
.11				lda (pData),y
				pha
				dey
				lda (pData),y
				tay
				pla
				jsr BUF.GetNewYA
				bcs .9
				
				>LDYA BUF.COffset
				>STYA TmpCount
				
				jsr BUF.CopyCount

				lda (BufPtrBackup)
				tax						Save deleted char for later

				inc BufPtrBackup
				bne .3
				inc BufPtrBackup+1
				
.3				jsr BUF.CopyTail
				
				ldy #FileLen
				lda (pData),y
				sec
				sbc #1
				sta (pData),y
				iny
				lda (pData),y
				sbc #0
				sta (pData),y
				
				jsr BUF.ResetSel
				txa						Get back deleted char
				
				clc	
.9				rts
*--------------------------------------
BUF.SelToClipboard
				jsr BUF.GetSelLen
				bcs .8
				>STYA TmpCount
				
				ldy #hClipBoard
				lda (pData),y
				beq .1
				
				>SYSCALL FreeMem
				
.1				>LDYA TmpCount
				>SYSCALL GetMem
				bcs .9
				
				>STYA ClipboardPtr
				txa
				ldy #hClipBoard
				sta (pData),y
				
				ldy #ClipBoardLen
				lda TmpCount
				sta (pData),y
				iny
				lda TmpCount+1
				sta (pData),y
				
				ldy #hBuffer
				lda (pData),y
				>SYSCALL GetMemPtr
				>STYA BufPtr
				
				lda BufPtr
				clc
				ldy #SelStart
				adc (pData),y
				sta BufPtr

				lda BufPtr+1
				iny
				adc (pData),y
				sta BufPtr+1
				
				ldy #0
				
.2				jsr DecTmpCount
				beq .8
				lda (BufPtr),y
				sta	(ClipboardPtr),y
				iny
				bne .2
				inc BufPtr+1
				inc ClipboardPtr+1
				bra .2

				jsr BUF.ResetSel
				
.8				clc
.9				rts
*--------------------------------------
BUF.CopyClipboard
				jsr DecTmpCount
				beq .9
				lda (ClipboardPtr)
				sta	(BufPtr)
				inc ClipboardPtr
				bne .1
				inc ClipboardPtr+1
.1				inc BufPtr
				bne BUF.CopyClipboard
				inc BufPtr+1
				bra BUF.CopyClipboard
.9				rts
*--------------------------------------
BUF.CopyCount	jsr DecTmpCount
				beq .9

				lda (BufPtrBackup)
				sta	(BufPtr)

				inc BufPtrBackup
				bne .1
				inc BufPtrBackup+1

.1				inc BufPtr
				bne BUF.CopyCount
				inc BufPtr+1
				bra BUF.CopyCount

.9				rts
*--------------------------------------
BUF.CopyTail	lda (BufPtrBackup)
				sta	(BufPtr)
				beq .9
				
				inc BufPtrBackup
				bne .1
				inc BufPtrBackup+1

.1				inc BufPtr
				bne BUF.CopyTail
				inc BufPtr+1
				bra BUF.CopyTail

.9				rts				
*--------------------------------------
* Out : Y = Line Length
*--------------------------------------
BUF.ComputeCOffset
				jsr BUF.GetLine
				bcs .9
				phy						save line length
				ldy #FileX
				lda (pData),y
				clc
				adc BUF.LOffset
				sta BUF.COffset
				lda #0
				adc BUF.LOffset+1
				sta BUF.COffset+1
				ply
				clc
.9				rts
*--------------------------------------
BUF.GetLine		ldy #FileY+1
				lda (pData),y
				tax
				dey
				lda (pData),y
				
BUF.GetLineAX	sta BUF.TmpLine1
				stx BUF.TmpLine1+1

				stz	BUF.LOffset
				stz	BUF.LOffset+1

				ldy #hBuffer
				lda (pData),y
				>SYSCALL GetMemPtr
				>STYA BufPtr
				
				stz BUF.TmpLine2
				stz BUF.TmpLine2+1
		
.1				ldy #0
				
.2				lda (BufPtr),y
				beq .7
				cmp #13
				beq .3
				
				iny
				bne .2

.3				lda BUF.TmpLine1
				cmp BUF.TmpLine2
				bne .4
				lda BUF.TmpLine1+1
				cmp BUF.TmpLine2+1
				beq .8
				
.4				tya
				sec
				adc BufPtr
				sta BufPtr
				bcc .5
				inc BufPtr+1
				
.5				tya
				sec
				adc BUF.LOffset
				sta BUF.LOffset
				bcc .6
				inc BUF.LOffset+1

.6				inc BUF.TmpLine2
				bne .1
				inc BUF.TmpLine2+1
				bra .1
				
.7				lda BUF.TmpLine1
				cmp BUF.TmpLine2
				bne .9
				lda BUF.TmpLine1+1
				cmp BUF.TmpLine2+1
				bne .9
				
.8				clc						Y = Line length
				rts
				
.9				sec
				rts
*--------------------------------------
BUF.GetCharAtY	tya
				clc
				adc BUF.LOffset
				sta BUF.COffset
				lda #0
				adc BUF.LOffset+1
				sta BUF.COffset+1
				
				lda (BufPtr),y
				clc
				rts
*--------------------------------------
BUF.GetNewYA	>STYA TmpLen
				>LDA.G hBufferBackup
				beq .1

				>SYSCALL FreeMem
				
.1				>LDA.G hBuffer
				>STA.G hBufferBackup
				
				>SYSCALL GetMemPtr
				>STYA BufPtrBackup
				
				>LDYA TmpLen
				>SYSCALL GetMem0
				bcs	.9
				
				>STYA BufPtr
				txa
				>STA.G hBuffer

.9				rts
*--------------------------------------
BUF.ResetSel	>STZW.G SelStart
				>STZW.G SelEnd
				rts
*--------------------------------------
BUF.CheckSelected
				sec
				lda BUF.COffset
				ldy #SelStart
				sbc (pData),y
				lda BUF.COffset+1
				iny
				sbc (pData),y
				bcc .9
				
				lda BUF.COffset
				ldy #SelEnd
				sbc (pData),y
				lda BUF.COffset+1
				iny
				sbc (pData),y
				rts						CC if in selection
				
.9				sec
				rts
*--------------------------------------
* Out : CC if ok, YA=SelLen
*--------------------------------------
BUF.GetSelLen	ldy #SelEnd
				lda (pData),y
				sec
				ldy #SelStart
				sbc (pData),y
			
				tax
				
				ldy #SelEnd+1
				lda (pData),y
				sec
				ldy #SelStart+1
				sbc (pData),y
				bcc .9
				
				phx
				ply
				bne .8
				tax
				beq .9
				
.8				clc
				rts
				
.9				sec
				rts
*--------------------------------------
MAN
SAVE USR/SRC/BIN/EDIT.S.BUF
LOAD USR/SRC/BIN/EDIT.S
ASM
