;; CADLisp Tools - Find & Replace Selected Text
(defun ct-rp:replace-all (text find replacement / position start)
  (setq start 0)
  (while (setq position (vl-string-search find text start))
    (setq text (strcat (substr text 1 position) replacement
                       (substr text (+ position (strlen find) 1)))
          start (+ position (strlen replacement))))
  text)

(defun ct-rp:editable-p (data / flags row)
  (and data
       (setq row (tblsearch "LAYER" (cdr (assoc 8 data))))
       (setq flags (cdr (assoc 70 row)))
       (= 0 (logand flags 20))))

(defun ct-rp:text-value (data / pair value)
  (setq value "")
  (foreach pair data
    (if (member (car pair) '(1 3)) (setq value (strcat value (cdr pair)))))
  value)

(defun ct-rp:mtext-control-p (value)
  (or (vl-string-search "\\" value)
      (vl-string-search "{" value)
      (vl-string-search "}" value)))

(defun ct-rp:text-pairs (value / pairs)
  (setq pairs nil)
  (while (> (strlen value) 250)
    (setq pairs (cons (cons 3 (substr value 1 250)) pairs)
          value (substr value 251)))
  (reverse (cons (cons 1 value) pairs)))

(defun ct-rp:set-value (data value / inserted item new out pair)
  (setq inserted nil new (ct-rp:text-pairs value) out nil)
  (foreach pair data
    (if (member (car pair) '(1 3))
      (if (not inserted)
        (progn
          (foreach item new (setq out (cons item out)))
          (setq inserted T)))
      (setq out (cons pair out))))
  (reverse out))

(defun c:REPTEXT (/ *error* changed data entity find index kind new old replacement set skipped undo-started)
  (defun *error* (message)
    (if undo-started (command-s "_.UNDO" "_End"))
    (if (and message (not (wcmatch (strcase message) "*CANCEL*,*QUIT*,*EXIT*")))
      (prompt (strcat "\nREPTEXT error: " message)))
    (princ))
  (setq find (getstring T "\nFind text: "))
  (if (= find "")
    (prompt "\nFind text cannot be blank.")
    (progn
      (setq replacement (getstring T "\nReplace with: "))
      (prompt "\nSelect TEXT and unformatted MTEXT to update: ")
      (if (setq set (ssget '((0 . "TEXT,MTEXT"))))
        (progn
          (setq index 0 changed 0 skipped 0)
          (if (= 0 (logand 8 (getvar "UNDOCTL")))
            (progn (command "_.UNDO" "_Begin") (setq undo-started T)))
          (repeat (sslength set)
            (setq entity (ssname set index) data (and entity (entget entity))
                  kind (and data (cdr (assoc 0 data))) old (and data (ct-rp:text-value data)))
            (if (and (ct-rp:editable-p data) old
                     (not (and (= kind "MTEXT")
                               (or (ct-rp:mtext-control-p old)
                                   (ct-rp:mtext-control-p replacement)))))
              (progn
                (setq new (ct-rp:replace-all old find replacement))
                (if (/= old new)
                  (if (entmod (ct-rp:set-value data new))
                    (progn (entupd entity) (setq changed (1+ changed)))
                    (setq skipped (1+ skipped)))))
              (setq skipped (1+ skipped)))
            (setq index (1+ index)))
          (if undo-started (progn (command "_.UNDO" "_End") (setq undo-started nil)))
          (prompt (strcat "\nUpdated " (itoa changed) " object(s); skipped " (itoa skipped)
                          " locked, referenced, invalid, or formatted MTEXT object(s).")))
        (prompt "\nNo text selected."))))
  (princ))
