Function: diff-apply-buffer

diff-apply-buffer is an interactive and byte-compiled function defined in diff-mode.el.gz.

Signature

(diff-apply-buffer &optional BEG END REVERSE TEST-OR-NO-SAVE)

Documentation

Apply the diff in the entire diff buffer.

Interactively, if the region is active, apply all hunks that the region overlaps; otherwise, apply all hunks. With a prefix argument, reverse-apply the hunks. If applying all hunks succeeds, save the changed buffers. By default apply diffs to new source files; apply them to old files if diff-jump-to-old-file is non-nil.

When called from Lisp, returns nil if buffers were successfully modified and saved, or the number of failed hunk applications otherwise. Optional arguments BEG and END restrict the hunks to be applied to those lying between BEG and END. Optional argument REVERSE means to reverse-apply hunks. Optional argument TEST-OR-NO-SAVE no-save means not to save any changed buffers, test or t means to not actually apply or reverse-apply any hunks, but return the same information: nil if all hunks can be applied, or the number of hunks that can't be applied. Other non-nil values are reserved.

View in manual

Probably introduced at or before Emacs version 30.1.

Key Bindings

Source Code

;; Defined in /usr/src/emacs/lisp/vc/diff-mode.el.gz
(defun diff-apply-buffer (&optional beg end reverse test-or-no-save)
  "Apply the diff in the entire diff buffer.
Interactively, if the region is active, apply all hunks that the region
overlaps; otherwise, apply all hunks.
With a prefix argument, reverse-apply the hunks.
If applying all hunks succeeds, save the changed buffers.
By default apply diffs to new source files; apply them to old
files if `diff-jump-to-old-file' is non-nil.

When called from Lisp, returns nil if buffers were successfully modified
and saved, or the number of failed hunk applications otherwise.
Optional arguments BEG and END restrict the hunks to be applied to those
lying between BEG and END.
Optional argument REVERSE means to reverse-apply hunks.
Optional argument TEST-OR-NO-SAVE `no-save' means not to save any
changed buffers, `test' or t means to not actually apply or
reverse-apply any hunks, but return the same information: nil if
all hunks can be applied, or the number of hunks that can't be
applied.  Other non-nil values are reserved."
  (interactive "R\nP")
  (let ((buffer-edits nil)
        (failures 0)
        (diff-refine nil)
        (test (memq test-or-no-save '(t test))))
    (save-excursion
      (goto-char (or beg (point-min)))
      (diff-beginning-of-hunk t)
      (while (pcase-let ((`(,buf ,line-offset ,pos ,_src ,dst ,switched)
                          (diff-find-source-location nil reverse test)))
               ;; FIXME: Should respect `diff-apply-hunk-to-backup-file'
               ;; similarly to how `diff-apply-buffer' does.
               ;; Prompt for each relevant file.
               (cond ((and line-offset (not switched))
                      (push (cons pos dst)
                            (alist-get buf buffer-edits)))
                     (t (setq failures (1+ failures))))
               (and (not (eq (prog1 (point) (ignore-errors (diff-hunk-next)))
                             (point)))
                    (or (not end) (< (point) end))
                    (looking-at-p diff-hunk-header-re)))))
    (cond ((zerop failures)
           (unless test
             (dolist (buf-edits (reverse buffer-edits))
               (with-current-buffer (car buf-edits)
                 (dolist (edit (cdr buf-edits))
                   (let ((pos (car edit))
                         (dst (cdr edit))
                         (inhibit-read-only t))
                     (goto-char (car pos))
                     (delete-region (car pos) (cdr pos))
                     (insert (car dst))))
                 (unless (eq test-or-no-save 'no-save)
                   (save-buffer))))
             (message (ngettext "%s %d buffer" "%s %d buffers"
                                (length buffer-edits))
                      (if (eq test-or-no-save 'no-save) "Edited" "Saved")
                      (length buffer-edits)))
           nil)
          (t
           (unless test
             (message (ngettext "%d hunk failed; no buffers changed"
                                "%d hunks failed; no buffers changed"
                                failures)
                      failures))
           failures))))