; *********************************************************************
; EDIT08
; Pascal editor file 08
; B.Bridgwater 5.12.83
; *********************************************************************
;
;------------------------------------------------------------------------
search ;Search subroutine , called by find & replace routines.
;
; 'Multiple occurence' matching is achieved by a general backtracking
; algorithm, using a stack of records, one for each '*-match', holding
; the following data -
;
; +-+
; |0| linbufX of multsym concerned
; +-+
; |1| lo }
; +-+    } Text address of match start
; |2| hi }
; +-+
; |3| lo }
; +-+    } Number of object accepted by match
; |4| hi }
; +-+
;
; The first record is reserved (by chknrep) for match-start pointer.
;
;Entry - MS   --> Search start position
;        EOSP --> Top bit set charcater at required end of search.
;        Search-string in stracc, terminated by termch.
;
;Exit  - CC  Occurence of findstring found.
;            MS  --> match
;            ME  --> matchend + 1
;        CS  Search failed.
;
       ldyIM 0
       sty sstckX
       ;
       ;Start of attempted match. See if have reached TEXP - either old
       ;cursor position, or end of search block.
       ;
       lda MS
       sta ME
       cmp TEXP
       lda MS+1
       sta ME+1
       sbc TEXP+1
       bcc srchct
       ;
       lda texpflag
       bne srchfl
       ;
       jsr movetofoundposition
       ;
       lda GS
       sta CPOS
       lda GS+1
       sta CPOS+1
       ;
       inc texpflag
       ;
       lda ENDP
       sta TEXP
       lda ENDP+1
       sta TEXP+1
       ;
       ldyim 0
       beq srchct
       ;
srchfl sec
       rts
;
srchL1 inc ME
       bne srchct
       inc ME+1
       ;
srchct ;Expects Y (= linbufX), ME --> next char valid.
       ;
       ldaAY stracc
       cmpIM termsym
       beq srchfd
       cmpIM multsym
       beq srchMI
       jsr compmobj
       beq srchL1
       ;
srchin ;Increment current multiple match, or advance MS if there
       ;isn't one. Search fails when MS reaches EOT.
       ;
       ldx sstckX
       bne srchFM
       ;
       inc MS
       bne search
       inc MS+1
       bne search
;
srchfd ;Match found. Set sstk[0] = MS, (ME-MS).
       ;
       lda MS
       sta ssttlostk
       lda MS+1
       sta sstthistk
       ;
       sec
       lda ME
       sbc MS
       sta scntlostk
       lda ME+1
       sbc MS+1
       sta scnthistk
       ;
       clc
       rts
       ;
srchFM ;Let most recent multiple match accept another occurence of
       ;find-object, if one exists.
       ;
       clc
       ldaAX ssttlostk
       adcAX scntlostk
       sta ME
       ldaAX sstthistk
       adcAX scnthistk
       sta ME+1
       ;
       ldaAX sindexstk
       tay
       ;
       jsr compmobj
       bne srchbk
       ;
       ldx sstckX
       ;
       incAX scntlostk
       bne srchL1
       incAX scnthistk
       bne srchL1
       ;
srchbk ;Can't increment this multiple-match, so backtrack to the
       ;previous one.
       ;
       dec sstckX
       ;
       jmp srchin
       ;
srchMI ;'Push' a new record onto backtrack-stack. Initially accept
       ;0 characters into multiple match.
       ;
       inc sstckX
       ;
       ldx sstckX
       ;
       iny
       tya
       staAX sindexstk
       ;
       lda ME
       staAX ssttlostk
       lda ME+1
       staAX sstthistk
       ;
       ldaIM 0
       staAX scntlostk
       staAX scnthistk
       ;
       jsr compmobj ;Cheap way to advance Y to next find-object.
       ;
       jmp srchct
;------------------------------------------------------------------------
comofl ;Have hit end of search text, so fail match.
       ;
       rola ;Cheap NE from CS (ME+1 cmp EOSP+1).
       rts
;
compmobj ;Compares stracc,Y & (ME).
;
;Exit  - Y indexes next ssobj
;        EQ  Objects match.
;        NE  Objects don't match.
;
       ;Match fails if have hit end of seach text.
       ;
       lda ME
       cmp ENDP
       bne comoco
       lda ME+1
       cmp ENDP+1
       beq comofl
       ;
comoco ;Check for & store absence/presence of notsym, then branch depending
       ;whether trying to match constant, wildcard, subrange or set item.
       ;
       ldxIM 0
       ldaAY stracc
       cmpIM notsym
       php
       bne comoNN
       iny
comoNN iny
       ldaAY stracc-1
       bmi comome
       cmpIX ME
       bne cmoprN
       beq cmoprY
       ;
comome cmpIM wildsym
       beq cmoprY
       cmpIM alphasym
       beq comoal
       cmpIM digsym
       beq cmodg1
       cmpIM subrsym
       bne cmoset
       ;
       iny
       iny
       ;
       ldaIX ME
       cmpAY stracc-2
       bcc cmoprN
       cmpAY stracc-1
       bcc cmoprY
       bne cmoprN
       beq cmoprY
       ;
comoal ldaIX ME
       cmpIM "_"
       beq cmoprY
       ;
       cmpIM "A"
       bcc cmodg2
       cmpIM "z"+1
       bcs cmoprN
       cmpIM "Z"+1
       bcc cmoprY
       cmpIM "a"
       bcc cmoprN
       bcs cmoprY
       ;
cmodg1 ldaIX ME
cmodg2 cmpIM "0"
       bcc cmoprN
       cmpIM "9"+1
       bcs cmoprN
       bcc cmoprY
       ;            
cmoset ldaAY stracc
       sta nextX
       iny
       ;
cmoSel jsr compmobj
       beq cmoseY
       cpy nextX
       bne cmoSel
       ;
cmoprN plp
       rts
       ;
cmoseY ldy nextX
       ;
cmoprY pla
       andIM 2
       rts
;------------------------------------------------------------------------
chknrep ;Check for size, then replace found string with replace string.
;
;Entry - Match just found; s*****stk[0] set up for %0 ('&')
;        replindex = start index of replace part in stracc
;
;Exit  - Found string replaced.
;
;
       jsr movetofoundposition
       ;
       lda ME
       sta GE
       lda ME+1
       sta GE+1
       ;
       lda replindex
       sta lnbufX
       ;
chnrlp lda GS
       cmp MS
       lda GS+1
       sbc MS+1
       bcs chnrRerr
       ;
       ldy lnbufX
       inc lnbufX
       ldaAY stracc
       bpl chnrsi
       cmpIM termsym
       beq chnrex
       cmpIM fieldsym
       beq chnrfi
       ;
       ;Must be an '&'. Has been frigged to look like MM 0
       ;
       ldyIM 0
       beq chnram
       ;
chnrfi inc lnbufX
       ;
       ldaAY stracc+1
       tax
       ;
       ldaAX fieldmmxtab
       bmi chnrmf
       ;
       tay
       beq chnrms
       ;
       clc
       ldaAY ssttlostk
       adcAY scntlostk
       sta temp
       ldaAY sstthistk
       adcAY scnthistk
       bne chnrtm
       ;
chnrms lda MS
       sta temp
       lda MS+1
chnrtm sta temp+1
       ;
       ldaAX fieldofftab
       tay
       ldaIY temp
       ;
chnrsi ldxIM 0







aAY sstthistk
       sta ARGP+1
       ;
       lda GS
       sta VARP
       lda GS+1
       sta VARP+1
       ;
       clc
       ldaAY scntlostk
       tax
       adc GS
       sta GS
       ldaAY scnthistk
       tay
       adc GS+1
       sta GS+1
       ;
       jsr copybk
       ;
       jmp chnrlp
       ;
chnrex rts
;
chnrRerr jmp brkX6 ;'No room'
;------------------------------------------------------------------------


 lnk edit09
