Function: projectile-replace--apply

projectile-replace--apply is an interactive and byte-compiled function defined in projectile.el.

Signature

(projectile-replace--apply)

Documentation

Apply every enabled match, grouped by file, then re-run the search.

What actually got written is recorded in projectile-replace--last-apply so projectile-replace-undo can revert it.

Key Bindings

Source Code

;; Defined in ~/.emacs.d/elpa/projectile-20260820.1509/projectile.el
(defun projectile-replace--apply ()
  "Apply every enabled match, grouped by file, then re-run the search.
What actually got written is recorded in `projectile-replace--last-apply'
so `projectile-replace-undo' can revert it."
  (interactive)
  (projectile-replace--ensure-not-scanning)
  (let ((enabled (cl-remove-if-not #'projectile-replace--match-enabled
                                   projectile-replace--matches))
        (replacement projectile-replace--replacement)
        (literal projectile-replace--literal)
        (root projectile-replace--root)
        (term projectile-replace--term)
        (groups (make-hash-table :test 'equal))
        (order nil)
        (applied nil)
        (nfiles 0)
        (nrepl 0)
        (skipped 0))
    (when (null enabled)
      (user-error "No matches are enabled"))
    ;; group the enabled matches by file, preserving first-seen order
    (dolist (m enabled)
      (let ((file (projectile-replace--match-file m)))
        (unless (gethash file groups)
          (push file order))
        (push m (gethash file groups))))
    (dolist (file (nreverse order))
      ;; isolate each file: a read-only file or a write error must not abort
      ;; the batch (leaving earlier files edited and no summary shown)
      (let ((result (condition-case err
                        (projectile-replace--apply-file
                         file (gethash file groups) replacement literal)
                      (error
                       (projectile-replace--skip
                        (file-name-nondirectory file)
                        (error-message-string err))))))
        (if (eq result 'skipped)
            (cl-incf skipped)
          (push (cons file result) applied)
          (cl-incf nfiles)
          (cl-incf nrepl (length result)))))
    ;; an apply that wrote nothing leaves the previous record alone - there's
    ;; nothing new to undo, and dropping it would lose a still-valid undo
    (when applied
      (setq projectile-replace--last-apply
            (projectile-replace--undo-record-create
             :root root :term term :replacement replacement
             :files (nreverse applied))))
    (message "%s"
             (projectile-prepend-project-name
              (format "Replaced %d occurrence%s in %d file%s%s"
                      nrepl (if (= nrepl 1) "" "s")
                      nfiles (if (= nfiles 1) "" "s")
                      (if (> skipped 0)
                          (format " (skipped %d file%s)"
                                  skipped (if (= skipped 1) "" "s"))
                        ""))))
    (projectile-replace--refresh)))