; *********************************************************************
; EDIT0A
; Pascal editor file 0A
; B.Bridgwater 4.1.84
; *********************************************************************
;
;-----------------------------------------------------------------------
;Marks are stored in SMATlo,hi (Set Mark Address Table), where the
;addresses refer to positions in the 2nd half of text; when updated
;(any number of times), this address is translated into a value in
;UMATlo,hi (Used Mark Address Table), where the addresses refer to
;positions in the text as it is currently partitioned.
;-----------------------------------------------------------------------
markupdate ;Update marks for current position of gap.
;
;Exit  -  marksbeforecsr = No. of marks before GE.
;
       jsr mkudchunksize
;
mkudknowngap ;
;
       ldaIM 0  
       sta marksbeforecsr
       ;
       ldx markX
       ;
mkudlp dex
       bmi mkudex
       ;
       ldyzx SMATlo
       styzx UMATlo
       cpy GE
       ldazx SMAThi
       stazx UMAThi
       sbc GE+1
       bcs mkudlp
       ;
       inc marksbeforecsr
       ;
       sec
       tya
       sbc chunksize
       stazx UMATlo
       ldazx UMAThi
       sbc chunksize+1
       stazx UMAThi
       ;
       bcs mkudlp
       ;
mkudex rts
;-----------------------------------------------------------------------
mkudchunksize ;chunksize := size of gap to correct for.
;
       sec
       lda GE
       sbc GS
       sta chunksize
       lda GE+1
       sbc GS+1
       sta chunksize+1
       rts
;-----------------------------------------------------------------------
clrmarks ;Clear, count update & order mark(s), ready for use.
;
;Entry - Mark1 (markstk) and/or Mark2 (markstk+2) possibly set.
;                                                            
;Exit  - Gap finepositioned
;        Marks ordered so that Mark1 <= Mark2
;        Marks cleared & status updated
;        A[Z] = nummarks = No. of marks
;
       ;Save (GE - GS) for updating marks, then finepositiongap
       ;
       jsr mkudchunksize
       ;
       jsr finepositiongap
       ;
       jsr mkudknowngap
       ;
       ;
       ;If Mark2 (exists) < Mark1, then swap.
       ;
       ldx markX
       cpxim 2
       bne clrmco
       ;
       lda UMATlo+1
       cmp UMATlo
       lda UMAThi+1
       sbc UMAThi
       bcs clrmco
       ;
       lda UMATlo
       ldy UMATlo+1
       sta UMATlo+1
       sty UMATlo
       lda UMAThi
       ldy UMAThi+1
       sta UMAThi+1
       sty UMAThi
       ;        
clrmco lda markX
       sta nummarks
       ;
       ldaim 0
       sta markX
       ;
       jsr status
       ;
       lda nummarks
       ;
       rts
;---------------------------------------------------------------------
mkrefuse ;Complain if any marks are set.
;
       lda markX
       bne mkrfEr
       ;
       rts
;
mkrfEr jmp brkX20
;---------------------------------------------------------------------
nmblok ;Normalizes a marked block of text, to ease use.
;
;Entry  -  Marks updated (gap fine positioned). TEXP = GE
;
;Exit   -  'Bad marking' error if 2 marks were set.
;       -  EQ - 0 marks were set.
;          NE - 1 mark was set (csr-mark or mark-csr):
o start of block,
               XY = end of b    ;
       ldx TEXP
       ldy TEXP+1
       ;
nblkex rts
;-----------------------------------------------------------------------
dfinit ;Seperated this way to avoid grotty command-line escape.
;
       jsr clrmarks
       cmpim 2
       beq badmarking
       ;
       rts
;
dfblok ;Normalize a 'marked' block (defaulting to whole) of text.
;
;Exit  -  TEXP = Old cursor position (in end of text).
;         ENDP = End of 'marked' block.
;
       jsr nmblok
       bne dblkEP
       ;
       jsr stfile
       ;
       ldx TMAX
       ldy TMAX+1
       ;
dblkEP stx ENDP
       sty ENDP+1
       ;
       rts
;-----------------------------------------------------------------------
mvblok ;Ensure good marking (m-m-c or c-m-m) for a block 'move-op'
;
;Exit  -  CC: marks are after cursor
;         CS: marks are before cursor
;
       jsr clrmarks
       cmpim 2
       bne badmarking
       ;
       lda marksbeforecsr
       cmpim 1
       beq badmarking
       ;
       rts
;-----------------------------------------------------------------------
badmarking jmp brkX18 ;'Bad marking'
;---------------------------------------------------------------------
setmark ;
;
       jsr finepositiongap
       ;
       ldx markX
       cpxim 2
       beq badmarking
       ;
smrkst ;Save mark address.
       ;
       lda GE   
       stazx SMATlo
       lda GE+1
       stazx SMAThi
       ;
       jsr normalizetextnscreenX
       ;
       inc markX
       ;
       jmp status
;---------------------------------------------------------------------
clearmarks ;
;
       jsr clrmarks
       ;
       jmp normalizetextnscreenX
;---------------------------------------------------------------------
mkdel ;
;
       jsr clrmarks
       jsr nmblok
       beq badmarking ;Stop delete from defaulting to 'NEW' !
       ;
       stx GE
       sty GE+1
       ;
       jmp normalizetextnscreenX
;---------------------------------------------------------------------
mkcpy ;Marked copy. Either m-m-c or c-m-m.
;
       jsr mvblok
       ;
       ;XY = (Mark2-Mark1) - size of block to be copied.
       ;   
       sec
       lda UMATlo+1
       sbc UMATlo
       tax
       lda UMAThi+1
       sbc UMAThi
       tay
       ;
       jsr checkroomXY
       ;
       ;Copy the block, then normalize text.
       ;
       lda UMATlo
       sta ARGP
       lda UMAThi
       sta ARGP+1
       ;
       stx GE
       stx VARP
       sty GE+1
       sty VARP+1
       ;
       jsr cpfd2
       ;
       jmp normalizetextnscreenX
;---------------------------------------------------------------------
mkmve ;
;
       ;If marks are before cursor, then do a bit of tweaking to
       ;change it into case where thay are after it.
       ;
       jsr mvblok
       bcc mkmvco
       ;
       ldx UMATlo
       ldy UMAThi
       ;
       sec
       lda GS
       sbc UMATlo+1
       sta temp
       lda GS+1
       sbc UMAThi+1
       sta temp+1 
       ;
       lda GE
       sta UMATlo+1
       sbc temp
       sta UMATlo
       lda GE+1
       sta UMAThi+1
       sbc temp+1
       sta UMAThi
       ;
       jsr gpbkXY
       ;
mkmvco ;Perform move chunk-by-chunk via a buffer of size &100
       ;
       ;chunksize = min(Mark2-Mark1,&100)
       ;
       sec
       lda UMATlo+1
       sbc UMATlo
       tax
       lda UMAThi+1
       sbc UMAThi
       tay
       ;
       beq mkmvCS ;Mark2-Mark1 < &100
       ;
       ldxIM 0
       ldyIM 1
       ;
mkmvCS stx chunksize
       sty chunksize+1
       ;
hen have finished.


       ;********************ta ARGP
       lda UMAThi+1
       sbc chunksize+1
       sta ARGP+1
       ;
       ldaIM :LSB: movebuff
       sta VARP
       ldaIM :MSB: movebuff
       sta VARP+1
       ;
       ;** ldx chunksize
       ;** ldy chunksize+1
       ;
       jsr copyfd
       ;
       ;*********************************************************
       ;Shift up text to close gap, and create new one **********
       ;*********************************************************
       ;
       sec
       lda ARGP
       sbc GE
       tax ;ARGP assumed uncorrupted by
       lda ARGP+1
       sbc GE+1
       tay ;moveblockfwd above.
       ;
       clc
       lda GE
       sta ARGP
       adc chunksize
       sta VARP
       lda GE+1
       sta ARGP+1
       adc chunksize+1
       sta VARP+1
       ;
       lda UMATlo
       adc chunksize
       sta UMATlo
       lda UMAThi
       adc chunksize+1
       sta UMAThi
       ;
       jsr copyfd
       ;
       ;*********************************************************
       ;Move block out of buffer into created gap ***************
       ;*********************************************************
       ;
       ldaIM :LSB: movebuff
       sta ARGP
       ldaIM :MSB: movebuff
       sta ARGP+1
       ;
       lda GE
       sta VARP
       lda GE+1
       sta VARP+1
       ;
       ldx chunksize
       ldy chunksize+1
       ;
       jsr copyfd
       ;
       ;*********************************************************
       ;Loop until chunksize is zero ****************************
       ;*********************************************************
       ;
       jmp mkmvco
       ;
mkmvex ;If marks were before cursor, then should re-tweak things.
       ;
       lda marksbeforecsr
       beq mkmvnm
       ;
       ldx UMATlo+1
       ldy UMAThi+1
       jsr gpfdXY
       ;
mkmvnm jmp normalizetextnscreenX
;---------------------------------------------------------------------

 end
