NEW
  AUTO 3,1
				.LIST OFF
*--------------------------------------
BUF.InsertA		sta TmpByte

				jsr BUF.ComputeCOffset
				cpy #255
				beq .8

				>LDYAI 1
				jsr BUF.BufPtrSetup
				bcs .9
				
				jsr BUF.MoveForwardTail
				
.2				lda TmpByte
				sta (BufPtr)

				jmp SCRN.UnSaved

.8				clc	
.9				rts
*--------------------------------------
BUF.InsertTAB	>LDA.G FileX
				and #3					0-3
				eor #$3					3-0
				
				inc
				sta TmpByte
	
				jsr BUF.ComputeCOffset
				tya
				sec
				adc TmpByte
				bcs .8
				
				ldy TmpByte
				lda #0
				jsr BUF.BufPtrSetup
				bcs .9
				
				jsr BUF.MoveForwardTail
				
				lda #C.SPACE
				ldy TmpByte
				dey
				
.2				sta (BufPtr),y

				dey
				bpl .2

				jmp SCRN.UnSaved

.8				stz TmpByte
				clc	
.9				rts				
*--------------------------------------
BUF.DelCharAtCursor
				lda BUF.COffset
				clc
				>ADC.G BufBase
				sta	BufPtrDst
				
				lda BUF.COffset+1
				>ADC.G BufBase+1
				sta	BufPtrDst+1
				
				>LDYA BufPtrDst
				iny
				bne .1
				inc
				
.1				>STYA BufPtr

				lda (BufPtrDst)
				pha						Save deleted char for later

				jsr BUF.MoveBackTail

				lda FileLen
				bne .5
				dec FileLen+1
.5				dec FileLen	

				jsr BUF.ResetSel
				pla						Get back deleted char

				clc
.9				rts
*--------------------------------------
BUF.InsertClipboard
				>LDA.G hClipBoard
				beq .8

				>SYSCALL GetMemPtr
				>STYA ClipboardPtr

				jsr BUF.ComputeCOffset

				>LDYA.G ClipBoardLen 
				jsr BUF.BufPtrSetup
				bcs .9

				jsr BUF.MoveForwardTail
				
				>LDA.G ClipBoardLen
				eor #$ff
				tax
				
				iny
				lda (pData),y			ClipBoardLen+1
				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				jsr BUF.ResetSel
				jsr SCRN.UpdateStatusBar
				jmp SCRN.UnSaved
				
.8				clc	
.9				rts
*--------------------------------------
BUF.DelSel		jsr BUF.GetSelLen
				bcs .9					nothing to delete

				>LDA.G SelStart
				clc
				>ADC.G BufBase
				sta BufPtrDst

				>LDA.G SelStart+1
				>ADC.G BufBase+1
				sta BufPtrDst+1

				lda BufPtrDst
				clc
				adc SelLen
				sta BufPtr

				lda BufPtrDst+1
				adc SelLen+1
				sta BufPtr+1

				jsr BUF.MoveBackTail

				lda FileLen
				sec
				sbc SelLen
				sta FileLen

				lda FileLen+1
				sbc SelLen+1
				sta FileLen+1

.8				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

				>LDA.G BufBase
				clc
				>ADC.G SelStart
				sta BufPtr

				>LDA.G BufBase+1
				>ADC.G SelStart+1
				sta BufPtr+1

				lda SelLen
				>STA.G ClipBoardLen

				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.MoveBackTail
				ldy #0

				lda BufPtr+1
				pha
				
.1				lda (BufPtr),y
				sta	(BufPtrDst),y
				beq .8

				iny
				bne .1

				inc BufPtr+1
				inc BufPtrDst+1
				bra .1

.8				pla
				sta BufPtr+1
	
				rts
*--------------------------------------
BUF.MoveForwardTail	
				ldy #0

				ldx #0
				
.1				lda (BufPtr),y				
				beq .2

				iny
				bne .1
				
				inx
				inc BufPtr+1
				inc BufPtrDst+1
				bra .1
				
.2				txa
				eor #$ff
				pha
				tya
				eor #$ff
				tax						will move tail len+1
				
.3				lda (BufPtr),y
				sta	(BufPtrDst),y

				inx
				bne .4
				
				pla
				inc
				beq .8
				
				pha
				
.4				tya
				bne .5
				
				dec BufPtr+1
				dec BufPtrDst+1
				
.5				dey

				bra .3

.8				rts
*--------------------------------------
* Out : Y = Line Length
*--------------------------------------
BUF.ComputeCOffset
				jsr BUF.GetLine
				bcs .9

				phy						save line length

				>LDA.G FileX
*				clc
				adc BUF.LOffset
				sta BUF.COffset

				lda #0
				adc BUF.LOffset+1
				sta BUF.COffset+1

				ply

				clc
.9				rts
*--------------------------------------
BUF.GetNextLine	sec
				.HS 90					BCC

BUF.GetLine		clc
				>LDA.G FileY
				adc #0
				pha
				iny
				lda (pData),y
				adc #0
				tax
				pla

BUF.GetLineAX	sta BUF.TmpLine1		Requested Line
				stx BUF.TmpLine1+1

				stz BUF.TmpLine2
				stz BUF.TmpLine2+1
		
				stz	BUF.LOffset
				stz	BUF.LOffset+1

				>LDYA.G BufBase
				>STYA BufPtr

.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.ResetSel	lda #$ff
				ldy #SelStart

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

				iny						#SelEnd
				inc						lda #0
				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
*--------------------------------------
* in : Y,A = cnt to insert at COffset 
*--------------------------------------
BUF.BufPtrSetup	>STYA TmpLen

				tya
				clc
				
				adc FileLen
				sta FileLen
				
				tay
				
				lda TmpLen+1
				adc FileLen+1
				
				sta FileLen+1

				cpy BufLen
				sbc BufLen+1
				
				bcc .8

				jsr BUF.BufPtr.Realloc
				bcs .9

				jsr BUF.ComputeCOffset
				
.8				lda BUF.COffset
				clc
				>ADC.G BufBase
				sta BufPtr

				lda BUF.COffset+1
				>ADC.G BufBase+1
				sta BufPtr+1

				lda BufPtr
				clc
				adc TmpLen
				sta BufPtrDst

				lda BufPtr+1
				adc TmpLen+1
				sta BufPtrDst+1
				
				clc
.9				rts
*--------------------------------------
BUF.BufPtr.Realloc
				>PUSHB.G hBuf

				lda FileLen+1
				inc
				inc
				sta BufLen+1
				>PUSHA
				>PUSHBI 0
				
				>SYSCALL Realloc
				bcs .9

				>STYA.G BufBase
				txa
				>STA.G hBuf
.9				rts
*--------------------------------------
MAN
SAVE usr/src/bin/edit.s.buf
LOAD usr/src/bin/edit.s
ASM
