NEW
  AUTO 3,1
				.LIST OFF
*--------------------------------------
FILE.New		jsr BUF.ResetSel
				jsr SCRN.UpdateStatusBar

				>LDYA L.MSG.NEWFILE
				
FILE.New.1		>PUSHYA
				>PUSHWI 0				Allocate
				>SYSCALL RealPath
				bcs .9
				
				txa
				>STA.G hFileName

				>LDYAI 1024
				>STYA BufLen
				>SYSCALL GetMem
				bcs .9

				>STYA BufPtr
				>STYA.G BufBase
				
				txa
				>STA.G hBuf				FileLen already set to 0
				
				lda #0
				sta (BufPtr)
				
				stz FileLen
				stz FileLen+1
				
				jsr SCRN.Home
				clc
				
.9				rts
*--------------------------------------
FILE.Load		jsr BUF.ResetSel
				jsr SCRN.UpdateStatusBar
				
				>LDA.G hFileName
				>SYSCALL GetMemPtr
				>PUSHYA
				>PUSHEA.G STATBUF
				>SYSCALL Stat
				bcc .1
				
				>LDA.G hFileName
				>SYSCALL GetMemPtr
				jmp FILE.New.1
	
.1				>LDYAI 256
				>SYSCALL GetMem
				bcs .99

				>STYA ZPPTR1
				stx TmpByte

				>LDA.G STATBUF+S.STAT.P.TYPE
				cmp #S.FI.T.TXT
				bne .2

				jsr FILE.Load.TXT
				jmp FILE.Close
				
.2				cmp #$FA				S-C/ASM?
				bne .9

				jsr FILE.Load.ASM
				jmp FILE.Close
				
.9				lda TmpByte
				>SYSCALL FreeMem
				
				lda #MLI.E.INCFF
				sec
.99				rts
*--------------------------------------
FILE.Load.TXT	stz FileLen
				stz FileLen+1
				
				jsr SCRN.Home
				
				stz TmpIndex			hFile

				>STZ.G FileType

				ldx #S.FI.T.TXT
				jsr FILE.OpenTypeX
				bcs .99
				
				>PUSHA
				>PUSHEA.G STATBUF
				
				>SYSCALL FStat
				bcs .99
				
				jsr FILE.GetBuffer
.99				bcs .9

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

				lda #0
				sta (ZPPTR1),y			Y = line len

				ldx #0
				ldy #0

				lda (ZPPTR1)
				beq .8
				
				eor #C.LF
				bne .2
				
				phy

				lda #2
				>STA.G FileType
				
				ply
				iny
				
.2				lda (ZPPTR1),y
				beq .4

				sta (BufPtr)
				inc BufPtr
				bne .3

				inc BufPtr+1

.3				inx
				iny
				bra .2
				
.4				txa
				clc
				adc FileLen
				sta FileLen
				bcc .1
				
				inc FileLen+1
				bra .1
				
.7				cmp #MLI.E.EOF
				bne .9

.8				lda #0
				sta (BufPtr)
	
				clc
				.HS B0					BCS
.9				sec
				rts
*--------------------------------------
FILE.Load.ASM	stz FileLen
				stz FileLen+1
				
				lda #4
				>STA.G FileType
					
				lda #8
				jsr FILE.GetBufferA		2k buffer
				bcs .9
				
				ldx #$FA
				jsr FILE.OpenTypeX
				bcs .99
			
.1				ldy #3
				jsr FILE.ReadY
				bcs .7

				lda (ZPPTR1)
				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
				.HS B0					BCS
.9				sec
.99				rts
*--------------------------------------
FILE.Load.ASM.Decode
				ldy #0
				
.1				lda (ZPPTR1),y
				beq .8

				iny
				tax
				bmi .2

				jsr FILE.Load.ASM.2Buf
				bcc .1

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

				lda (ZPPTR1),y		Get Repeat Count
				iny
				tax

.4				lda (ZPPTR1),y		Get Repeat Char
				
				jsr FILE.Load.ASM.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.ASM.2Buf
				bcs .9

				dex
				bne .6
				
				bra .1
				
.8				lda #C.CR
*--------------------------------------
FILE.Load.ASM.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
				rts
*--------------------------------------
FILE.Save		>LDYAI 256
				>SYSCALL GetMem
				bcs FILE.Save.9

				>STYA ZPPTR1
				stx TmpByte
				
				>LDA.G FileType
				tax
				
				jmp (J.SAVE,x)

FILE.Save.9		rts				
*--------------------------------------
FILE.Save.CR	clc
				.HS B0					BCS
FILE.Save.CRLF	sec	
				ror TmpIndex+1
				
				ldx #S.FI.T.TXT
				jsr FILE.CreateTypeX
				
				bcs FILE.Save.9

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

				ldy #$ff

.2				iny
				lda (BufPtr),y
				sta (ZPPTR1),y
				beq .3
				
				cmp #C.CR
				bne .2
				
				iny
				
				bit TmpIndex+1
				bpl .3
				
				iny
				
				lda #C.LF
				sta (ZPPTR1),y
				
.3				tya
				beq .8
				
				clc
				adc BufPtr
				sta BufPtr
				bcc .7
				
				inc BufPtr+1
				
.7				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 #$FA				S-C/ASM
				jsr FILE.CreateTypeX
				bcs FILE.Save.99

				>LDYA.G	BufBase
				>STYA BufPtr				
				
.1				lda (BufPtr)
				beq .8				
				
				ldy #1
				lda TmpLen
				sta (ZPPTR1),y
				iny
				
				lda TmpLen+1
				sta (ZPPTR1),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 (ZPPTR1),y
				iny
				dex
				bpl .63

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

				sta (ZPPTR1),y
				iny
				
				tya
		
				sta (ZPPTR1)

				jsr FILE.WriteY
				bcs FILE.Close
				
 				inc TmpLen
				bne .1
				
				inc TmpLen+1
				bra .1
				
.8 				>STZ.G bUnSaved
*--------------------------------------
FILE.Close		php
				pha
				lda TmpByte
				beq .1
				>SYSCALL FreeMem
				
.1				lda TmpIndex
				beq .2
				
				>SYSCALL FClose
				
.2				pla
				plp
				rts				
*--------------------------------------
FILE.OpenTypeX	lda #O.RDONLY
				bra FILE.Open
FILE.CreateTypeX
				lda #O.CREATE+O.WRONLY

FILE.Open		stz TmpIndex

				phx
				pha
				>LDA.G hFileName
				>SYSCALL GetMemPtr
				>PUSHYA
				pla						access
				>PUSHA
				pla						type
				>PUSHA
				>PUSHWZ					Aux type
				>SYSCALL FOpen
				bcs .9
				sta TmpIndex
				
.9				rts
*--------------------------------------
FILE.ReadY		phy

				>PUSHB TmpIndex
				>PUSHW ZPPTR1

				ply
				lda #0
				>PUSHYA
				>SYSCALL FRead
				rts
*--------------------------------------
FILE.WriteY		phy
				
				>PUSHB TmpIndex
				>PUSHW ZPPTR1
				
				ply
				lda #0
				>PUSHYA
				>SYSCALL FWrite
				rts
*--------------------------------------
FILE.GetBuffer	>LDA.G STATBUF+S.STAT.SIZE+3
				dey
				ora (pData),y
				bne FILE.GetBuffer.9
				
				dey
				lda (pData),y			Size HI
				inc
				inc

FILE.GetBufferA	ldy #0					Size LO
				>STYA BufLen
				>SYSCALL GetMem
				bcs .99

				>STYA BufPtr
				>STYA.G BufBase
				txa
				>STA.G hBuf
		
.99				rts

FILE.GetBuffer.9
				lda #E.FTB
				sec
				rts
*--------------------------------------
MAN
SAVE usr/src/bin/edit.s.file
LOAD usr/src/bin/edit.s
ASM
