NEW
  AUTO 3,1
				.LIST OFF
*--------------------------------------
SCRN.Init		>PUSHW L.SEQ.INIT
				>LDA.G ScreenH
				dec
				>PUSHA
				>PUSHBI 1
				>SYSCALL PrintF
				bcs SCRN.Init.9
				
				jsr SCRN.ResetScrollRgn
*--------------------------------------
SCRN.Home		lda #0

				ldy #FileX				Zero FileXY,ScreenXY & CurXY

.1				sta (pData),y
				iny

				cpy #CurY+1
				bne .1

				rts
*--------------------------------------
SCRN.Repaint	jsr SCRN.GotoCurXCurY
				bcs SCRN.Init.9
				
				lda #$ff
				>STA.G bUpdateStatus

				jsr SCRN.UpdateTopBar
				bcs SCRN.Init.9

				jmp SCRN.UpdateMainAndStatus

SCRN.Init.8		clc
SCRN.Init.9		rts
*--------------------------------------
SCRN.InsLineAtCurY
				>LDA.G CurY
				>CMP.G ViewPortHm1
				beq .8
				
				jsr SCRN.GotoCurXCurY
				bcs .9
				
				>PUSHW L.SEQ.SCROLLCURDN
				>LDA.G CurY
				inc
				inc
				>PUSHA
				>LDA.G ScreenH
				dec
				>PUSHA
				>PUSHBI 2
				>SYSCALL PrintF
				bcs .9
				
				jsr SCRN.ResetScrollRgn
				

.8				clc
.9				rts
*--------------------------------------
SCRN.DelLineAtCurY


				clc
				rts
*--------------------------------------
SCRN.ResetScrollRgn
				>PUSHW L.SEQ.SCROLLRGN
				>LDA.G ScreenH
				dec
				>PUSHA
				>PUSHBI 1
				>SYSCALL PrintF
				rts
*--------------------------------------
SCRN.UnSaved	>LDA.G bUnSaved
				bne SCRN.Init.8

				dec
				sta (pData),y			bUnSaved
*--------------------------------------
SCRN.UpdateTopBar
				ldx #SEQ.CURSAVE-SEQS
				jsr SCRN.LineBufInit

				ldy #1
				jsr SCRN.LineBufGotoLineY
				jsr SCRN.ResetBar

				>PUSHW LineBufPtr
				>PUSHW L.MSG.TOPBAR
				>PUSHW A2osX.KVER

				>LDA.G hFileName
				>SYSCALL GetMemPtr
				>PUSHYA
				>LDA.G FileType
				asl
				tax
				>PUSHW L.MSG.FILETYPES,x

				>PUSHBI 6
				jsr SCRN.LineBufSprintf
				bcs .9

				>LDA.G bUnSaved
				beq .2

				lda #'!'
				jsr SCRN.ToLineBuf

.2				ldx #SEQ.CURRESTORE-SEQS
				jsr SCRN.SEQS.ToLineBuf

				jmp SCRN.LineBufOut

.9				rts
*--------------------------------------
SCRN.UpdateViewPort
				stz bRepaint

.1				>LDA.G FileX
				>CMP.G ScreenX
				bcs .2					out of left border ?

				>DEC.G ScreenX			yes
				sec
				ror bRepaint
				bra .1

.2				bit bRepaint
				bmi .30

.21				>LDA.G ScreenX
				clc
				>ADC.G ScreenW
				dec
				>CMP.G FileX
				bcs .3					out of right border ?

				>INC.G ScreenX
				sec
				ror bRepaint
				bra .21

.3				bit bRepaint
.30				bmi .6

.31				>LDA.G FileY+1
				tax
				dey
				lda (pData),y			A,X = FileY

				>CMP.G ScreenY
				iny
				txa
				sbc (pData),y
				bcs .4					out of top border ?

				>DECW.G ScreenY			move viewport up one line....
				bit bRepaint
				bmi .31					skip scroll, will be repainted later

				jsr SCRN.scrollDn		...and move content one line down
				bcc .31
				rts

.4				bit bRepaint
				bmi .6

.41				>LDA.G ScreenY+1
				tax
				dey
				lda (pData),y

				clc
				>ADC.G ViewPortHm1
				bcc .5

				inx

.5				>CMP.G FileY			A,X = screen bottom
				txa
				iny						FileY > screen bottom ?
				sbc (pData),y
				bcs .6					CS : fileY>= screen bottom

				>INCW.G ScreenY			move viewport down one line....
				bit bRepaint
				bmi .41

				jsr SCRN.scrollUp		...and move content one line up
				bcc .41

.9				rts

.6				>LDA.G FileX
				sec
				>SBC.G ScreenX
				>STA.G CurX

				>LDA.G FileY
				sec
				>SBC.G ScreenY
				>STA.G CurY

				jsr BUF.ComputeCOffset
				jsr SCRN.GotoCurXCurY

				bit bRepaint
				bmi SCRN.UpdateMain

				jmp SCRN.UpdateStatusBar
*--------------------------------------
SCRN.UpdateMainAndStatus
				jsr SCRN.UpdateStatusBar
*--------------------------------------
SCRN.UpdateMain	>LDA.G ScreenY
				sta TmpIndex
				iny
				lda (pData),y
				sta TmpIndex+1

				stz TmpByte
				>LDA.G ViewPortH
				bra SCRN.UpdateMain.1

SCRN.UpdateLast	>LDA.G ViewPortHm1
				sta TmpByte
				clc
				>ADC.G ScreenY
				sta TmpIndex
				iny
				lda (pData),y
				adc #0
				sta TmpIndex+1

				lda #1
SCRN.UpdateMain.1
				sta TmpCount

.1				lda TmpIndex
				ldx TmpIndex+1

				jsr BUF.GetLineAX		Y = line length
				bcs .5

				lda TmpByte
				jsr SCRN.UpdateLineAtALenY
				bcs SCRN.UpdateMain.9

.3				inc TmpByte
				inc TmpIndex
				bne .4

				inc TmpIndex+1

.4				dec TmpCount
				bne .1

				bra .8

.5				ldx #0
				ldy TmpByte
				jsr SCRN.GotoXY
				bcs SCRN.UpdateMain.9

				>PUSHW L.SEQ.EDIT
				>PUSHBI 0
				>SYSCALL PrintF
				bcs SCRN.UpdateMain.9

				inc TmpByte
				dec TmpCount
				bne .5

.8				jsr BUF.ComputeCOffset
				jmp SCRN.GotoCurXCurY
SCRN.UpdateMain.9
				rts
*--------------------------------------
SCRN.UpdateCurrentEOL
				jsr SCRN.LineBufInitEdit
				bra SCRN.UpdateLineAtBufPtr
*--------------------------------------
SCRN.UpdateCurrentLine
				>LDA.G ScreenY+1
				tax
				dey
				lda (pData),y
				clc
				>ADC.G CurY
				bcc .1

				inx

.1				jsr BUF.GetLineAX
				bcc .2

				jsr SCRN.LineBufInitEdit
				jmp SCRN.UpdateLineAtBufPtr.EOL

.2				phy
				>LDA.G CurY
				ply
*--------------------------------------
* A = Target Screen Y position
* BufPtr = Text line start, Y = len
*--------------------------------------
SCRN.UpdateLineAtALenY
				sty TmpLen
				inc
				inc
				sta TmpLen+1

				jsr SCRN.LineBufInitEdit

				ldy TmpLen+1
				jsr SCRN.LineBufGotoLineY

				>LDA.G ScreenX
				cmp TmpLen
				bcs SCRN.UpdateLineAtBufPtr.EOL

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

				tya
*				clc
				adc BufPtr
				sta BufPtr
				bcc .2
				inc BufPtr+1

.2

SCRN.UpdateLineAtBufPtr
				>LDA.G ScreenW
				tax

				stz bSelected

.2				lda (BufPtr)
				beq .8

				cmp #C.CR
				beq .8

				jsr BUF.CheckSelected
				eor bSelected
				bpl .5

				jsr SCRN.ToggleSel

.5				lda (BufPtr)
				jsr SCRN.ToLineBuf
				inc BufPtr
				bne .6

				inc BufPtr+1

.6				inc BUF.COffset
				bne .7

				inc BUF.COffset+1

.7				dex
				bne .2

.8				bit bSelected
				bpl SCRN.UpdateLineAtBufPtr.EOL
				jsr SCRN.ToggleSel

SCRN.UpdateLineAtBufPtr.EOL
				ldx #SEQ.CEOL-SEQS
				jsr SCRN.SEQS.ToLineBuf
				jmp SCRN.LineBufOut
*--------------------------------------
SCRN.ToggleSel	ldy #SEQ.NORM-SEQS

				lda bSelected
				eor #$ff
				sta bSelected
				bpl .1

				ldy #SEQ.INV-SEQS

.1				lda SEQS,y
				beq .8

				jsr SCRN.ToLineBuf
				iny
				bra .1

.8
SCRN.ToggleSel.RTS
				clc
				rts
*--------------------------------------
SCRN.UpdateStatusBar
				>LDA.G bUpdateStatus
				bpl SCRN.ToggleSel.RTS

				lda #0
				sta (pData),y

				jsr SCRN.ResetStatusBar
				
				>PUSHW LineBufPtr
				>PUSHW L.MSG.STATUSBAR

				>PUSHW FileLen
				>PUSHW BUF.COffset

				>LDA.G FileX
				inc
				>PUSHA

				>LDA.G FileY
				tax
				iny
				lda (pData),y
				inx
				bne .2

				inc

.2				>PUSHA

				txa
				>PUSHA

				>PUSHBI 7
				jsr SCRN.LineBufSprintf
				bcs .9

				ldx #SEQ.CURRESTORE-SEQS
				jsr SCRN.SEQS.ToLineBuf

				jmp SCRN.LineBufOut

.8				clc
.9				rts
*--------------------------------------
SCRN.ResetStatusBar
				ldx #SEQ.CURSAVE-SEQS
				jsr SCRN.LineBufInit

				>LDA.G ScreenH
				tay
				jsr SCRN.LineBufGotoLineY

SCRN.ResetBar	ldx #SEQ.BAR-SEQS
				jsr SCRN.SEQS.ToLineBuf

				>LDA.G ScreenW
				tax

				lda #C.SPACE

.1				jsr SCRN.ToLineBuf
				dex
				bne .1

				lda #C.CR
				jmp SCRN.ToLineBuf
*--------------------------------------
SCRN.CursorAdjust
.1				jsr BUF.ComputeCOffset
				bcc .2					Y = linelen

				>DECW.G FileY
*				>STZ.G FileX
				bra .1

.2				tya
				>CMP.G FileX
				bcs .8

				>STA.G FileX

.8				clc
				rts
*--------------------------------------
SCRN.GotoCurXCurY
				ldy #CurX
				lda (pData),y
				tax
				ldy #CurY
				lda (pData),y
				tay

SCRN.GotoXY		>PUSHW L.MSG.GOTOXY
				tya
				inc
				inc
				>PUSHA
				txa
				inc
				>PUSHA
				>PUSHBI 2

				>SYSCALL PrintF
				rts
*--------------------------------------
SCRN.scrollUp	>PUSHW L.SEQ.SCROLLUP
				>PUSHBI 0
				>SYSCALL PrintF
				bcs .9

				>LDA.G ScreenY+1
				tax
				dey
				lda (pData),y

*				clc
				>ADC.G ViewPortHm1
				bcc .1

				inx

.1				jsr BUF.GetLineAX
				bcs .9

				phy
				>LDA.G ViewPortHm1
				ply
				jmp SCRN.UpdateLineAtALenY

.9				rts
*--------------------------------------
SCRN.scrollDn	>PUSHW L.SEQ.SCROLLDN
				>PUSHBI 0
				>SYSCALL PrintF
				bcs .9

				>LDA.G ScreenY+1
				tax
				dey
				lda (pData),y

				jsr BUF.GetLineAX
				bcs .9

				lda #0
				jsr SCRN.UpdateLineAtALenY

.9				rts
*--------------------------------------
SCRN.LineBufInitBar
				ldx #SEQ.BAR-SEQS
				bra SCRN.LineBufInit
SCRN.LineBufInitDLG
				ldx #SEQ.DLG-SEQS
				bra SCRN.LineBufInit
SCRN.LineBufInitLBL
				ldx #SEQ.DLG.LBL-SEQS
				bra SCRN.LineBufInit
SCRN.LineBufInitTB
				ldx #SEQ.DLG.TB-SEQS
				bra SCRN.LineBufInit
SCRN.LineBufInitBut
				ldx #SEQ.DLG.BUT-SEQS
				bra SCRN.LineBufInit
SCRN.LineBufInitEdit
				ldx #SEQ.EDIT-SEQS

SCRN.LineBufInit
				>LDYA.G LineBufBase
				>STYA LineBufPtr
*--------------------------------------
SCRN.SEQS.ToLineBuf
				lda SEQS,x

.1				jsr SCRN.ToLineBuf
				inx
				lda SEQS,x
				bne .1

				rts
*--------------------------------------
SCRN.LineBufGotoLineY
				>PUSHW LineBufPtr
				>PUSHW L.MSG.GOTOXY
				tya
				>PUSHA
				>PUSHBI 1
				>PUSHBI 2
*--------------------------------------
SCRN.LineBufSprintf
				>SYSCALL SPrintF
				bcs .9

				tya
				clc
				adc LineBufPtr
				sta LineBufPtr
				bcc .9

				inc LineBufPtr+1
				clc

.9				rts
*--------------------------------------
SCRN.ToLineBuf	sta (LineBufPtr)
				inc LineBufPtr
				bne .8

				inc LineBufPtr+1
.8				rts
*--------------------------------------
SCRN.LineBufOut	lda #0
				sta (LineBufPtr)

				ldy #S.PS.hStdOut
				lda (pPS),y
				>PUSHA
				>PUSHW.G LineBufBase
				>SYSCALL FPutS
				rts
*--------------------------------------
MAN
SAVE usr/src/bin/edit.s.scrn
LOAD usr/src/bin/edit.s
ASM
