; *********************************************************************
; EDIT04B
; Pascal editor file 04B
; B.Bridgwater 5.12.83
; *********************************************************************
;
;---------------------------------------------------------------------
normalizetextnscreenX
; Entry : gap character positioned
; Exit : gap moved to start of current line, screenX assigned
;
       lda GS
       sta temp
       lda GS+1
       sta temp+1
       ;
       jsr findposition
       sta scrnX
       ;
       sec
       lda GS
       sbc scrnX
       tax
       lda GS+1
       sbcim 0
       tay
       ;
       jmp gpbkXY
       ;
;---------------------------------------------------------------------
findposition
; Entry : temp-> middle of line in 1st half of text
; Exit  : A[Z]  = xcoord (0..page..width) of pointer within line.
;         atemp = page_width - A  
;
       clc ;-1
       lda temp
       sbc pagewidth 
       sta temp
       bcs fipond
       dec temp+1
       ;
fipond ldy pagewidth
fipolp ldaiy temp
       cmpim CR
       beq fipoL2
       ;
fipoE2 ;Entry point, when called from tempbacklines.
       ;
       dey
       bpl fipolp
       bmi findposition
       ;
fipoL2 sty atemp
       sec
       lda pagewidth
       sbc atemp
       rts
;---------------------------------------------------------------------
TPGEfd ; TP := GE + A lines
;
 ldx GE
 ldy GE+1
;
TPXYfwdAlines ;
;
 stx TP
 sty TP+1
;
TPfwdAlines ;Moves pointer TP forward A lines.
;
;Entry - XY --> start of a line >= GE
;        A = No. of lines to advance TP
;Exit  - TP advanced (possibly by less than A lines if hit EOT)
;        count = No. of lines remaining from original request.
;        atemp = Last end of line character.
;
       tax
       stx count
       beq TPTPex
       ;
TPTPlp ldyim 0
TPTPse ldaiy TP
       cmpim CR
       beq TPTPfd
       iny
       cpy pagewidth
       bne TPTPse
       ;
TPTPfd sta atemp
       ;
       tya
       jsr TPpAp1
       bcs TPTPex
       ;
       dec count
       bne TPTPlp
       ;
TPTPex rts
;---------------------------------------------------------------------
TPpAp1 ;
;
;Exit  -  CC TP = TP + (A+1)
;         CS TP+(A+1) = HYMEM, TP unchanged.
;
       sec ;+1
       adc TP
       tax
       ldaim 0
       adc TP+1
       tay
       ;
       cpx HYMEM
       sbc HYMEM+1
       bcs TPpApX
       ;
       stx TP
       sty TP+1
       ;
TPpapX rts
;---------------------------------------------------------------------
TPGSbk ; TP := GS - A lines
;
 ldx GS
 ldy GS+1
;
TPXYbackAlines ;
;
;Entry - A = No. of lines to retreat XY.
;Exit  - TP retreated
;        A = No. of lines was able to go back.
;
       stx TP
       sty TP+1
       ;
       tax
       stx count
       beq TPXYex
       ;
       ldxim 0
       ;
TPXYlp lda TSTART
       cmp TP
       lda TSTART+1
       sbc TP+1
       bcs TPXYab
       ;
       clc ;-1
       lda TP
       sbc pagewidth
       sta TP
       bcs TPXYnd
       dec TP+1
TPXYnd ;
       ldy pagewidth
       ldaiy TP
       cmpim CR
       bne TPXYco
       ;
       lda TP
       sta temp
       lda TP+1
       sta temp+1
       ;
       jsr fipoE2
       beq TPXYco
       ;
       lda TP
       adc atemp
       sta TP
       bcc TPXYco ;+1 from CS
       inc TP+1
       ;
TPXYco inx
       cpx count
       bcc TPXYlp
       ;
TPXYab txa
TPXYex rts
;---------------------------------------------------------------------
mvlnbk ;
;
;Entry - A = No. of lines to move gap back.
;Exit  - Gap moved back A lines. May be moved less if
;        hits start of file.
       ;
       jsr TPGSbk
       ;
;.....................................................................
gpbkTP ;
;
       ldx TP
       ldy TP+1
;
gpbkXY ;Move gap back so that GS := XY
;
       stx ARGP
       sty ARGP+1
       ;
       sec
       lda GS
       sbc ARGP
       stx GS
       tax
       lda GS+1
       sbc ARGP+1
       sty GS+1
       tay
       ;
       stx size
       sty size+1
       ;
       lda GE
       sbc size
       sta VARP
       sta GE
       lda GE+1
       sbc size+1
       sta VARP+1
       sta GE+1
       ;
;---------------------------------------------------------------------
copyfd ;Copy block forward (i.e. to higher address).
;
;Entry - ARGP --> block of size XY
;        VARP --> destination address 
;
;Exit  - ARGP,VARP uncorrupted.
;
       stx size
       sty size+1
;
cpfd2  ;
;
       lda size
       tay
       ora size+1
       beq cpfdex
       ;
       ldx size+1
       ;
       clc
       txa
       adc ARGP+1
       sta ARGP+1
       txa
       adc VARP+1
       sta VARP+1
       ;
       inx
       ;
       bcc cpfdl2
       ;
cpfdlp ldaiy ARGP
       staiy VARP
cpfdl2 dey
       cpyim &FF
       bne cpfdlp
       dex
       beq cpfdex
       dec ARGP+1
       dec VARP+1
       bne cpfdlp
       ;
cpfdex rts
;--------------------------------------------------------------------
mvlnfd ;
;
       jsr TPGEfd
       ;
;.....................................................................
gpfdTP ;
;
       ldx TP
       ldy TP+1
;
gpfdXY ;Move gap forward so that GE := XY
;
       lda GE
       sta ARGP
       lda GE+1
       sta ARGP+1
       ;
       lda GS
       sta VARP
       lda GS+1
       sta VARP+1
       ;
       sec
       txa
       sbc GE
       tax
       tya
       sbc GE+1
       tay
       ;
       clc
       txa
       adc GS
       sta GS
       tya
       adc GS+1
       sta GS+1
       ;
       txa
       adc GE
       sta GE
       tya
       adc GE+1
       sta GE+1
       ;
       jmp copybk
;---------------------------------------------------------------------
finepositiongap ;
;
;Entry - Gap line positioned, without VS in text.
;Exit  - Gap character positioned, with VS added.
;
       jsr addvirtualspaces
       ;
       clc
       lda GE
       adc scrnX
       tax
       lda GE+1
       adcim 0
       tay
       ;
       jmp gpfdXY
;--------------------------------------------------------------------
addvirtualspaces ;
;
       jsr VScount
       sta count
       beq adVSex
       ;
       tax
       ldyim 0
       lda currlength
       jsr insrtX
       ;
       ;Wipe possible visible CR.
       ;
       ldx currlength
       ldy scrnY
       jsr csrXY
       ldaim space
       jsr OSWRCH
       ;
       ldx count
       ldy scrnX
adVSlp dey
       staiy GE
       dex
       bne adVSlp
       ;
adVSex jmp csrscrnXY
;--------------------------------------------------------------------
VScount ;
;
;Exit - A(Z) = Number of virtual spaces.
;
       sec
       lda scrnX
       sbc currlength
       bcs VSctex
       ;
       ldaim 0
       ;
VSctex rts
;--------------------------------------------------------------------
insrtX ;
;
;Entry - Require gap of XY ( > 0) opening up after GE + A
;Exit  - GE -= XY (checked against GS)
;        A chars moved back to new GE, to open up gap
;
       sta atemp
       ;
       jsr checkroomXY
       ;
       ldyim 0
insXlp ldaiy GE
       staiy temp
       iny
       cpy atemp
       bcc insXlp
       ;
       stx GE
       lda temp+1
       sta GE+1
       ;
       rts
;--------------------------------------------------------------------


 lnk edit05
