 
*  ED7/RESFS    40 col        Apr / 11 / 93
*---------------------
*  All characters
 
*  FindStr, RepStr
 
*-----------------------------
*  Find/Replace string command
*-----------------------------
 
*  Find string entry
*-------------------
 
FS     EQU  $
       BL   @DISPLY           Show command message
       DATA FS00              Return address
       DATA MFSPT             Find string message
       JMP  RFS00             Use RS code
 
*  Replace string entry
*----------------------
 
RS     EQU  $
       BL   @DISPLY           Command display
       DATA RS00              Return address
       DATA MRSPT             RS message
 
*  Common RS/FS continuation
*---------------------------
 
RFS00  EQU  $                 Fetch current RS/FS display
       BL   @MOVE
       DATA RSFBUV
       DATA CTEXT,LLL
       CLR  @UPCASE           Allow lower case
       B    @GETKEY           Get response
 
 
*  Replace string & Find string
*------------------------------
 
RS00   SETO @VRFY             Flag check when found
       SETO @RSFLAG           Check for 3 delimiters
       JMP  RS01
 
FS00   CLR  @RSFLAG           Make sure of it
RS01   MOV  R1,@MKSAV1        Save for <ctrl-O>
       MOV  R3,@MKSAV3        Also
       BL   @VMBWD            Save entered strings
       DATA RSFBUV            VDP storage
       DATA CTEXT,LLL         Command line entry
 
*   Parse command line for Find/Replace string
 
       LI   R8,CTEXT          On command line
       BL   @PRNUMS           Sort out numbers
       JMP  PRSERJ            Bad result
       MOVB *R8+,@DELIM       First non separator as delimiter
       MOV  R8,@STRING        Store string pointer
       BL   @GSLASH           Look for delimiter
       JMP  PRSERJ            Not found
       MOV  R6,@STRL          Length as word
       MOV  R8,@RSTRNG        Start of replace string
       BL   @GSLASH           Find its delimiter
       CLR  R6                Zero if not found
       MOV  R6,@RSTRL         Save its length
       MOV  @NUMCHK,R6        Copy counter
       JEQ  PRRG20            Two numbers on line
 
*  Get no action count for 0,1,3 #s
 
       MOV  *R9+,@CNT         Copy first as count
       INC  @CNT              For skip count by DEC - 0 if no #s
       DEC  R6                3 #s ?
       JLT  PRRG20            Yup
       CLR  *R9               Initialize for 0 or 1 #
       MOV  @D79,@2(R9)       Last column
 
*  Get and check column pointers
 
PRRG20 LI   R8,79             For column check
D79    EQU  $-2
       MOV  *R9,@NUM1         Enter as start column  COLSTA
       C    *R9,R8            Past last ?
       JGT  PRSERJ            Yes - invalid
 
       C    *R9+,*R9          Compare to next #
       JGT  PRSERJ            Can't be bigger
 
       MOV  *R9,@NUM2         End column   COLEND
       C    *R9,R8            Check against max
       JGT  PRSERJ
 
*  Generate bounding column addresses for search
 
       BL   @RESTLA           Columns start at CTEXT
       LI   R9,LINBUF
       A    R9,@NUM1          Adjust as buffer pointer
       A    R9,@NUM2          Also
 
*  Continue after parsing
 
RFCON  BL   @RESTAB           Restore margins
RS10   BL   @RESTWS           Restore pointers to start search
       LI   R11,FFSLEN
RST11  MOV  @FFS24-2(R11),@FAC-2(R11)
       DECT R11
       JGT  RST11
 
       INC  R2                Start cursor at next char
       MOV  @LINADR,R4        Calc current cursor address
       A    R2,R4             Add Cursor offset to line start
 
*  Check start on present line
 
       C    R4,@NUM1          Cursor address < column start?
       JLT  FS18              Yes -  do search anyway after fixup
       C    R4,@NUM2          No, Cursor addr > column end?
       JLE  FS20              No, ok
 
*  Fetch next line to search
 
FS14   INC  R1                Yes, read next line
FS18   MOV  @NUM1,R4          Restore cursor addr to start
FS20   BL   @GETLC            Get Cline from buffer to @LINADR
       JMP  RSERR             EoF direct return
       MOV  @STRING,R8        Get start of comparison string
       MOV  R8,R12            Make copy
       A    @STRL,R8          End of reference string
       MOV  @NUM2,R6          Loop tightener
       MOVB @WILDC,R13        Fetch wildcard byte
       BL   @FAC              String comparison loop
       JMP  FS14
 
*  Return with string match
 
FS40   MOV  R1,R3             Cline -> Sbias
       MOV  R4,R2             Successful search -> Cursor
       S    @LINADR,R2        Calculate new cursor position
       BL   @SAVEWS           Save new registers for restore
       DEC  @CNT              Ignore while counting
       JGT  RS13              Yup
       JMP  RS15              At the first on now
 
RS13   INC  @SREGS+2          Bump saved Cursor past last find
       JMP  RS10              Have another go
 
RS15   ABS  @RSFLAG           Was it Replace
       JNE  RS30              No - stop when found
RE10J  B    @CMDXIT
 
PRSERJ JMP  PRSERR
 
*  Verify for Replace
*--------------------
 
RS30   ABS  @VRFY             String found, verify for replace ?
       JEQ  REPLCE            No - go replace it
 
VERFY  BL   @MRREM            Release margins
       BL   @MOVE             Write Verify message
       DATA RSMSV             VDP storage
       DATA CPUSCN            Command advice line
       DATA >56               Full length
       BL   @REST10           Restore workspace with out losing it
       LI   R9,VERRT          Return address after answer
       MOV  R9,@PROMPT        Load for special return
       INCT R1                Fake cursor to real line
       B    @COMA20           Get answer
 
*  Process Verify answer
 
VERRT  BL   @RESTLA           Restore line address
       BL   @REST10
       BL   @CNVLC            Convert lower case
VERR05 CB   *R8,@Y            Yes ?
       JEQ  REPLCE            Go replace string
       CB   *R8,@N            No  ?
       JNE  VERR10            No - continue comparisons
 
       A    @STRL,@SREGS+2    Bump past find for restart
       DEC  @SREGS+2
       JMP  RS10
 
VERR10 CB   *R8,@S            Stop ?
       JEQ  RE10J             Exit CMD mode
       CB   *R8,@A            All ?
       JEQ  VERR12            Cancel verify & replace all strings.
       CB   *R8,@CTRLC        <esc> ?
       JEQ  RSERR             Let's hear it
       CB   *R8,@B0F          <esc> ?
       JNE  VERFY             No - loop on keychoice
 
*  Error / <esc> exit
 
PRSERR CLR  @COLNUM
RSERR  B    @ERRMSG           Bloop entry
 
VERR12 LI   R9,LINTBL         Line table
       CLR  *R9+              Cancel special on first line
       CLR  *R9               Second also
       CLR  @VRFY             No verify any more
 
*  Do the string replace
 
REPLCE BL   @BKREPB           Blank replace buffer
       MOV  R4,R14            Curadr -> end of first move
       MOV  @LINADR,R8        Start of data
       A    @DSIX,R8          Skip over line #
       LI   R9,REPBUF         Output buffer
RADR   EQU  $-2
 
*  Construct replace buffer
 
REPL10 C    R8,R14            Copy text up to replace start
       JHE  REPL20            Done
       MOVB *R8+,*R9+         Copy bytes
       JMP  REPL10            Write replace string
 
*  Write in replace string after this
 
REPL20 MOV  @RSTRL,R6         Check replace string length
       JEQ  REPL35            Ignore if null
 
       MOV  @RSTRNG,R10       Fetch pointer to replace string
REPL30 MOVB *R10+,*R9+        Copy bytes over
       DEC  R6                Count off
       JGT  REPL30            More
 
*  Finish rest of line
 
REPL35 MOV  R9,@MARKER        Mark curent place
       DEC  @MARKER           Back off to last char
       A    @STRL,R8          Adjust cursor past find string
REPL40 C    R8,@LINEND        At end ?
       JH   REPL50            Yes
       MOVB *R8+,*R9+         Write rest of line.
       JMP  REPL40            More
 
*  Word-wrap or truncate
 
REPL50 ABS  @WDWRAP           Word wrap mode?
       JNE  REPL80            Yes, go reformat
 
       CLR  @MARKER           Reset marker
       LI   R14,REPBM6        Past line #
RPM6A  EQU  $-2
       A    @RSTRL,@SREGS+2   Push Cline along by Rstring length
       DEC  @SREGS+2          Back off to last char
       A    @RMRG,R14         Blank beyond right margin
       INC  R14               Next char
REPL60 MOVB @DSPACE,*R14+
       CI   R14,REPBUF+80
       JL   REPL60
 
       MOV  @REPBM6,@LINADR   Load for BufMgr
       BL   @UPDA07           Update line in buffer
       MOV  R0,R0             Error?
       JNE  PRSERR            Yes
       B    @RS10             Back for more
 
*  Bail out here if in word-wrap mode
 
REPL80 EQU  $                 Replace string word wrap mode
       LI   R9,REPBND         SRCEND - temp register use
       CLR  @TOPPAR           Reformat from ALL OF LINE.
       LI   R14,RS10          Set up return address
       B    @RENTRY           Go reformat also
 
 
*  Fast code for PAD  for RS/FS
 
FFS24  MOV  R4,R10            Refresh R10 to position in the line
       MOV  R12,R9            Restart reference string
FFS30  C    R9,R8             End of string ?
       JEQ  FFS40             Yes - found a match
       C    R10,R6            No - end of text comparison limit ?
       JH   FFS50             Yes - go get next line
 
       MOVB *R9+,R14          Fetch comparison byte
       CB   R14,R13           Check for wildcard
       JNE  FFS35             No
 
       INC  R10               Step over text char then
       JMP  FFS30             Resume loop
 
FFS35  CB   R14,*R10+         Byte match ?
       JEQ  FFS30             Yes - check next one
 
FFS37  INC  R4                No, bump start of text string
       JMP  FFS24             Start comparison loop again
FFS40  INCT R11               Leave with match
FFS50  B    *R11              Back for next line
FFSLEN EQU  $-FFS24
 
*-----------------------------------------------------------
 
