Function: projectile-replace--undo-file

projectile-replace--undo-file is a byte-compiled function defined in projectile.el.

Signature

(projectile-replace--undo-file FILE EDITS)

Documentation

Revert EDITS in FILE and return the count, or the symbol skipped.

Mirrors projectile-replace--apply-file in every respect: edits are reverted from the highest position downwards, the live buffer visiting FILE is re-resolved now (so an undo never writes a file behind the back of a buffer visiting it), a clean buffer is saved and a modified one is left for the user, and closed files are rewritten with their own coding system. A file is reverted only if all of its edits still verify, so it comes back whole or not at all.

Source Code

;; Defined in ~/.emacs.d/elpa/projectile-20260820.1509/projectile.el
(defun projectile-replace--undo-file (file edits)
  "Revert EDITS in FILE and return the count, or the symbol `skipped'.
Mirrors `projectile-replace--apply-file' in every respect: edits are
reverted from the highest position downwards, the live buffer visiting
FILE is re-resolved now (so an undo never writes a file behind the back
of a buffer visiting it), a clean buffer is saved and a modified one is
left for the user, and closed files are rewritten with their own coding
system.  A file is reverted only if all of its edits still verify, so it
comes back whole or not at all."
  (let ((descending (sort (copy-sequence edits)
                          (lambda (a b)
                            (> (projectile-replace--undo-edit-beg a)
                               (projectile-replace--undo-edit-beg b)))))
        (buffer (get-file-buffer file)))
    (if (buffer-live-p buffer)
        (with-current-buffer buffer
          (if (not (projectile-replace--undo-valid-p descending))
              (projectile-replace--skip (buffer-name buffer)
                                        "changed since the replace")
            (let ((was-modified (buffer-modified-p)))
              (save-excursion
                (save-restriction
                  (widen)
                  (atomic-change-group
                    (mapc #'projectile-replace--undo-one descending))))
              ;; same rule as applying: a buffer that had unsaved changes of
              ;; its own is edited but not saved on the user's behalf
              (unless was-modified
                (let ((require-final-newline nil))
                  (save-buffer)))
              (length edits))))
      (with-temp-buffer
        (insert-file-contents file)
        (let ((coding last-coding-system-used))
          (if (not (projectile-replace--undo-valid-p descending))
              (projectile-replace--skip (file-name-nondirectory file)
                                        "changed on disk since the replace")
            (mapc #'projectile-replace--undo-one descending)
            (let ((coding-system-for-write coding))
              (write-region (point-min) (point-max) file nil 'no-message))
            (length edits)))))))