Function: diff-apply-buffer
diff-apply-buffer is an interactive and byte-compiled function defined
in diff-mode.el.gz.
Signature
(diff-apply-buffer)
Documentation
Apply the diff in the entire diff buffer.
When applying all hunks was successful, then save the changed buffers.
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 ()
"Apply the diff in the entire diff buffer.
When applying all hunks was successful, then save the changed buffers."
(interactive)
(let ((buffer-edits nil)
(failures 0)
(diff-refine nil))
(save-excursion
(goto-char (point-min))
(diff-beginning-of-hunk t)
(while (pcase-let ((`(,buf ,line-offset ,pos ,_src ,dst ,switched)
(diff-find-source-location nil nil)))
(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)))
(looking-at-p diff-hunk-header-re)))))
(cond ((zerop failures)
(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))))
(save-buffer)))
(message "Saved %d buffers" (length buffer-edits)))
(t
(message "%d hunks failed; no buffers changed" failures)))))