NEW
  AUTO 3,1
				.LIST OFF
*--------------------------------------
FILE.New		>LDYA L.MSG.NEWFILE
				
FILE.NewYA		>PUSHYA
				>PUSHWZ					Allocate
				>SYSCALL RealPath
				bcs .9
				
				txa
				>STA.G hFileName
				
				jmp FILE.Reset
				
.9				rts
*--------------------------------------
FILE.Load		jsr FILE.NewYA
				bcs .9
				
				jsr FILE.Reset
				
				>LDYA.G LineBufBase
				>STYA LineBufPtr

				ldx #S.FI.T.TXT
				lda #O.RDONLY
				jsr FILE.OpenAX
				bcs .1
				
				jsr FILE.Load.TXT
				jmp FILE.Close
			
.1				ldx #S.FI.T.ASM
				lda #O.RDONLY
				jsr FILE.OpenAX
				bcs .9
				
				jsr FILE.Load.ASM
				jmp FILE.Close
				
.9				rts				
*--------------------------------------
FILE.Load.TXT	
.1				ldy #0
				jsr FILE.ReadY			read 256
				bcs .7

				sty TmpCount			Count read
				
				ldy #0

.2				lda (LineBufPtr),y
				
				cmp #C.LF
				bne .3
				
				phy
				
				lda #1
				>STA.G FileType
				
				ply
				bra .4

.3				jsr FILE.Load.2Buf
				bcs FILE.Load.RTS

.4				dec TmpCount
				beq .1
				
				iny
				bra .2
				
.7				cmp #MLI.E.EOF
				bne .9

.8				lda #0
				sta (BufPtr)
	
				clc
				rts
				
.9				sec

FILE.Load.RTS	rts
*--------------------------------------
FILE.Load.ASM	lda #2
				>STA.G FileType

.1				ldy #3
				jsr FILE.ReadY
				bcs .7

				lda (LineBufPtr)
				dec
				dec
				dec
				tay
				jsr FILE.ReadY
				bcs .7

				jsr FILE.Load.ASM.Decode

				bcc .1
				
.7				cmp #MLI.E.EOF
				bne .9
				
				lda #0
				sta (BufPtr)
				
				clc
				rts
				
.9				sec
.99				rts
*--------------------------------------
FILE.Load.ASM.Decode
				ldy #0
				
.1				lda (LineBufPtr),y
				beq .8

				iny
				tax
				bmi .2

				jsr FILE.Load.2Buf
				bcc .1

.9				rts
				
.2				cmp #$C0			REPEAT char?
				bne .5

				lda (LineBufPtr),y		Get Repeat Count
				iny
				tax

.4				lda (LineBufPtr),y		Get Repeat Char
				
				jsr FILE.Load.2Buf
				bcs .9

				dex
				bne .4

				iny					Skip Repeat char
				bra .1
				
.5				and #$3F			Compute blank count
				tax

.6				lda #C.SPACE
				
				jsr FILE.Load.2Buf
				bcs .9

				dex
				bne .6
				
				bra .1
				
.8				lda #C.CR
*--------------------------------------
FILE.Load.2Buf	phy
				phx

				sta (BufPtr)
				inc BufPtr
				bne .1

				inc BufPtr+1
				
.1				inc FileLen
				bne .8

				inc FileLen+1
				
				lda FileLen+1
				cmp BufLen+1
				bne .8

				jsr BUF.BufPtr.Realloc
				bcs .9
				
				>LDA.G BufBase
*				clc
				adc FileLen
				sta BufPtr
				iny
				lda (pData),y
				adc FileLen+1
				sta BufPtr+1
				
.8				clc

.9				plx
				ply
FILE.Save.9		rts
*--------------------------------------
FILE.Save		>LDYA.G LineBufBase
				>STYA LineBufPtr
				
				>LDA.G FileType
				asl
				tax
				jmp (J.SAVE,x)
*--------------------------------------
FILE.Save.CR	clc
				.HS B0					BCS
FILE.Save.CRLF	sec	
				ror TmpIndex+1
				
				ldx #S.FI.T.TXT
				lda #O.CREATE+O.WRONLY
				jsr FILE.OpenAX
				
				bcs FILE.Save.9

				>LDYA.G	BufBase
				>STYA BufPtr
	
.1				lda (BufPtr)
				beq .8

				ldy #0

.2				lda (BufPtr)
				beq .4
				
				inc BufPtr
				bne .3

				inc BufPtr+1

.3				sta (LineBufPtr),y
				iny

				cmp #C.CR
				bne .2
				
				bit TmpIndex+1
				bpl .4
				
				lda #C.LF
				sta (LineBufPtr),y

				iny
				
.4				jsr FILE.WriteY
				bcs FILE.Save.99
				
				bra .1

.8 				>STZ.G bUnSaved
FILE.Save.99	jmp FILE.Close
*--------------------------------------
FILE.Save.ASM	stz TmpLen
				stz TmpLen+1
				
				inc TmpLen

				ldx #S.FI.T.ASM
				lda #O.CREATE+O.WRONLY
				jsr FILE.OpenAX
				bcs FILE.Save.99

				>LDYA.G	BufBase
				>STYA BufPtr				
				
.1				lda (BufPtr)
				beq .8				
				
				ldy #1
				lda TmpLen
				sta (LineBufPtr),y
				iny
				
				lda TmpLen+1
				sta (LineBufPtr),y
				
				ldy #0

.2				iny
				lda (BufPtr),y				
				beq .3
				
				cmp #C.CR
				bne .2
				
.3				ldy #3				Initial LEN

.4				lda (BufPtr)
				beq .7
				
				ldx #0
				
.40				inc BufPtr
				bne .5
				
				inc BufPtr+1
				
.5				cmp #C.CR
				beq .7
				
				cmp (BufPtr)		same as next ?
				bne .50				no -> flush
				
				inx
				cpx #$3F
				bne .40
				
.50				cpx #0
				beq .60				only one char....

				cmp #C.SPACE
				beq .61
				
				cpx #3
				bcs .62
				
.63				sta (LineBufPtr),y
				iny
				dex
				bpl .63

				bra .4
				
.62				pha
				lda #$C0
				sta (LineBufPtr),y
				iny
				
				txa
				sta (LineBufPtr),y
				iny
				
				pla
				sta (LineBufPtr),y
				iny
				bra .4
				
.61				txa
				ora #$C0
				
.60				sta (LineBufPtr),y
				iny
				bra .4
				
.7				lda #0

				sta (LineBufPtr),y
				iny
				
				tya
		
				sta (LineBufPtr)

				jsr FILE.WriteY
				bcs FILE.Close
				
 				inc TmpLen
				bne .1
				
				inc TmpLen+1
				bra .1
				
.8 				>STZ.G bUnSaved
*--------------------------------------
FILE.Close		php
				pha
				
				lda TmpIndex			hFile
				beq .2
				
				>SYSCALL FClose
				
.2				pla
				plp
				rts				
*--------------------------------------
FILE.OpenAX		stz TmpIndex			hFile

				phx
				pha
				>LDA.G hFileName
				>SYSCALL GetMemPtr
				>PUSHYA
				pla						access
				>PUSHA
				pla						type
				>PUSHA
				>PUSHWZ					Aux type
				>SYSCALL FOpen
				bcs .9

				sta TmpIndex			hFile
				
.9				rts
*--------------------------------------
FILE.ReadY		phy

				>PUSHB TmpIndex			hFile
				>PUSHW LineBufPtr

				lda #0
				ply
				bne .1
				
				inc
				
.1				>PUSHYA
				>SYSCALL FRead
				rts
*--------------------------------------
FILE.WriteY		phy
				
				>PUSHB TmpIndex			hFile
				>PUSHW LineBufPtr
				
				ply
				lda #0
				>PUSHYA
				>SYSCALL FWrite
FILE.WriteY.RTS	rts
*--------------------------------------
FILE.Reset		>LDA.G hBuf
				beq .1
				
				>SYSCALL FreeMem

.1				>STZ.G hBuf
				>STA.G FileType			TXT/CR

				stz FileLen
				stz FileLen+1
				jsr BUF.ResetSel
				
				>LDYAI FILEBUF.SIZE
				>STYA BufLen
				>SYSCALL GetMem
				bcs FILE.WriteY.RTS

				>STYA BufPtr
				>STYA.G BufBase
				txa
				>STA.G hBuf
		
				lda #0
				sta (BufPtr)
				
				jmp SCRN.UpdateStatusBar
*--------------------------------------
MAN
SAVE usr/src/bin/edit.s.file
LOAD usr/src/bin/edit.s
ASM
