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

				lda BufLen
				sec
				ldy #ClipboardLen
				adc (pData),y
				pha
				
				lda BufLen+1
				iny
				adc (pData),y
				
				ply
				jsr BUF.GetNewYA
				bcs .9
				
				>LDYA BUF.COffset
				jsr BUF.Start2YA

				jsr BUF.CopyClipboard
				
				jsr BUF.CopyTail
				
.4				ldy #ClipboardLen
				lda (pData),y
				clc
				adc BufLen
				sta BufLen
				
				iny
				lda (pData),y
				adc BufLen+1
				sta BufLen+1
		
.8				clc	
.9				rts
*--------------------------------------
BUF.InsertA		sta TmpByte
				jsr BUF.ComputeCOffset
				cpy #255
				bne .10
				clc
				rts
				
.10				lda BufLen
				clc
				adc #2
				pha
				lda BufLen+1
				adc #0
				ply
				jsr BUF.GetNewYA
				bcs .9
				
				>LDYA BUF.COffset
				jsr BUF.Start2YA

.2				lda TmpByte
				sta (BufPtr)
				inc BufPtr
				bne .3
				inc BufPtr+1
				
.3				jsr BUF.CopyTail
				
.4				lda BufLen
				clc
				adc #1
				sta BufLen
				bcc .9
				
				inc BufLen+1
				
				clc	
.9				rts
*--------------------------------------
BUF.DelSel		jsr BUF.GetSelLen
				bcs .8

				lda BufLen
				sec
				sbc SelLen
				tay
				lda BufLen+1
				sbc SelLen+1
				
				iny
				bne .1
				inc
				
.1				jsr BUF.GetNewYA
				bcs .9
			
				>LDYA.G SelStart
				jsr BUF.Start2YA
				
				lda BufPtrBackup
				clc
				adc SelLen
				sta BufPtrBackup
				
				lda BufPtrBackup+1
				adc SelLen+1
				sta BufPtrBackup+1
				
				jsr BUF.CopyTail
				
				lda BufLen
				sec
				sbc SelLen
				sta BufLen
				
				lda BufLen+1
				sbc SelLen+1
				sta BufLen+1

				jsr BUF.ResetSel
				
.8				clc
.9				rts				
*--------------------------------------
* Erase char at CurXY
*--------------------------------------
BUF.DelCharAtCursor
				>LDYA BufLen
				jsr BUF.GetNewYA		BufferLen-1
				bcs .9

				>LDYA BUF.COffset
				jsr BUF.Start2YA

.3				lda (BufPtrBackup)
				pha						Save deleted char for later

				inc BufPtrBackup
				bne .4
				inc BufPtrBackup+1
				
.4				jsr BUF.CopyTail
				
				lda BufLen
				bne .5
				
				dec BufLen+1
				
.5				dec BufLen	
				
				jsr BUF.ResetSel
				pla						Get back deleted char

				clc
.9				rts
*--------------------------------------
BUF.SelToClipboard
				jsr BUF.GetSelLen
				bcs .8
				
				>LDA.G hClipBoard
				beq .1
				
				>SYSCALL FreeMem
				>STZ.G hClipBoard
				
.1				>LDYA SelLen
				>SYSCALL GetMem
				bcs .9
				
				>STYA ClipboardPtr
				txa
				>STA.G hClipBoard
				
				ldy #hBuffer
				lda (pData),y
				>SYSCALL GetMemPtr
				
				pha
				tya
				
*				clc
				>ADC.G SelStart
				sta BufPtr

				pla
				>ADC.G SelStart+1
				sta BufPtr+1
				
				lda SelLen
				ldy #ClipBoardLen
				sta (pData),y
				eor #$ff
				tax
				
				iny
				lda SelLen+1
				sta (pData),y
				eor #$ff
				pha
				
				ldy #0
				
.2				inx
				bne .3
				
				pla
				inc
				beq .8
				
				pha
				
.3				lda (BufPtr),y
				sta	(ClipboardPtr),y
				iny
				bne .2
				
				inc BufPtr+1
				inc ClipboardPtr+1
				bra .2

.8				clc
.9				rts
*--------------------------------------
BUF.CopyClipboard
				>LDA.G ClipboardLen
				eor #$ff
				tax
				
				iny
				lda (pData),y
				eor #$ff
				pha
				
				ldy #0
				
.1				inx
				bne .2
				
				pla
				inc
				beq .3
				
				pha
				
.2				lda (ClipboardPtr),y
				sta	(BufPtr),y
				iny
				bne .1
				
				inc ClipboardPtr+1
				inc BufPtr+1
				bra .1
				
.3				tya
				clc
				adc ClipboardPtr
				sta ClipboardPtr
				bcc BUF.CopyAddY
				
				inc ClipboardPtr+1
				
BUF.CopyAddY	tya
				clc
				adc BufPtr
				sta BufPtr
				bcc .2
				
				inc BufPtr+1
				
.2				rts
*--------------------------------------
BUF.Start2YA	eor #$ff
				pha
				
				tya
				eor #$ff
				tax
				
				ldy #0
				
.1				inx
				bne .2
				
				pla
				inc
				beq .3
				
				pha
				
.2				lda (BufPtrBackup),y
				sta	(BufPtr),y
				iny
				bne .1

				inc BufPtrBackup+1
				inc BufPtr+1
				bra .1
				
.3				tya
				clc
				adc BufPtrBackup
				sta BufPtrBackup
				bcc BUF.CopyAddY
				
				inc BufPtrBackup+1
				bra BUF.CopyAddY
*--------------------------------------
BUF.CopyTail	ldy #0

.1				lda (BufPtrBackup),y
				sta	(BufPtr),y
				beq .8
				
				iny
				bne .1
				
				inc BufPtrBackup+1
				inc BufPtr+1
				bra .1

.8				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 #C.CR
				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.GetNewYA	phy
				pha
				
				>LDA.G hBufferBackup
				beq .1

				>SYSCALL FreeMem
				
.1				>LDA.G hBuffer
				>STA.G hBufferBackup
				
				>SYSCALL GetMemPtr
				>STYA BufPtrBackup
				>STZ.G hBuffer				
				pla
				ply
				>SYSCALL GetMem
				bcs	.9
				
				>STYA BufPtr
				txa
				>STA.G hBuffer

.9				rts
*--------------------------------------
BUF.ResetSel	lda #$ff
				ldy #SelStart

				sta (pData),y
				iny
				sta (pData),y

				iny						#SelEnd
				
				inc
				sta (pData),y
				iny
				sta (pData),y
				
				rts
*--------------------------------------
BUF.CheckSelected
				lda BUF.COffset
				ldy #SelStart
				cmp (pData),y
				
				lda BUF.COffset+1
				iny
				sbc (pData),y
				bcc .9					CC:COffset < SelStart
				
				iny						#SelEnd
				lda (pData),y
				clc
				sbc BUF.COffset
				
				iny
				lda (pData),y
				sbc BUF.COffset+1
*										CC:SelEnd < COffset 			
.9				lda #0
				ror						$80 = selected
				rts
*--------------------------------------
BUF.GetSelLen	ldy #SelEnd
				lda (pData),y
				sec
				ldy #SelStart
				sbc (pData),y
			
				sta SelLen
				tax
				
				ldy #SelEnd+1
				lda (pData),y

				ldy #SelStart+1
				sbc (pData),y
				sta SelLen+1
				bcc .9
				bne .8
				txa
				
				beq .9
				
.8				clc
				rts
				
.9				sec
				rts
*--------------------------------------
MAN
SAVE USR/SRC/BIN/EDIT.S.BUF
LOAD USR/SRC/BIN/EDIT.S
ASM
