aboutsummaryrefslogtreecommitdiffstats
path: root/lisp/vc/diff-mode.el
diff options
context:
space:
mode:
authorJuri Linkov2023-09-27 20:36:07 +0300
committerJuri Linkov2023-09-27 20:39:33 +0300
commitd813f71ffe62844f5d4cc97cb54f3877dcf55a06 (patch)
tree701ed5d5e8e4a868222cbe7a19250a66ebb7da96 /lisp/vc/diff-mode.el
parent2d25f071b6cc3970a8b32343a997b6ff927945d1 (diff)
downloademacs-d813f71ffe62844f5d4cc97cb54f3877dcf55a06.tar.gz
emacs-d813f71ffe62844f5d4cc97cb54f3877dcf55a06.zip
* lisp/vc/diff-mode.el (diff-apply-buffer): New command (bug#66113).
(diff-mode-map): Bind 'diff-apply-buffer' to 'C-c C-m a'.
Diffstat (limited to 'lisp/vc/diff-mode.el')
-rw-r--r--lisp/vc/diff-mode.el35
1 files changed, 35 insertions, 0 deletions
diff --git a/lisp/vc/diff-mode.el b/lisp/vc/diff-mode.el
index d776375d681..24a925eda73 100644
--- a/lisp/vc/diff-mode.el
+++ b/lisp/vc/diff-mode.el
@@ -216,6 +216,7 @@ The default \"-b\" means to ignore whitespace-only changes,
216 "C-x 4 A" #'diff-add-change-log-entries-other-window 216 "C-x 4 A" #'diff-add-change-log-entries-other-window
217 ;; Misc operations. 217 ;; Misc operations.
218 "C-c C-a" #'diff-apply-hunk 218 "C-c C-a" #'diff-apply-hunk
219 "C-c C-m a" #'diff-apply-buffer
219 "C-c C-e" #'diff-ediff-patch 220 "C-c C-e" #'diff-ediff-patch
220 "C-c C-n" #'diff-restrict-view 221 "C-c C-n" #'diff-restrict-view
221 "C-c C-s" #'diff-split-hunk 222 "C-c C-s" #'diff-split-hunk
@@ -2054,6 +2055,40 @@ With a prefix argument, try to REVERSE the hunk."
2054 (diff-hunk-kill) 2055 (diff-hunk-kill)
2055 (diff-hunk-next))))) 2056 (diff-hunk-next)))))
2056 2057
2058(defun diff-apply-buffer ()
2059 "Apply the diff in the entire diff buffer.
2060When applying all hunks was successful, then save the changed buffers."
2061 (interactive)
2062 (let ((buffer-edits nil)
2063 (failures 0)
2064 (diff-refine nil))
2065 (save-excursion
2066 (goto-char (point-min))
2067 (diff-beginning-of-hunk t)
2068 (while (pcase-let ((`(,buf ,line-offset ,pos ,_src ,dst ,switched)
2069 (diff-find-source-location nil nil)))
2070 (cond ((and line-offset (not switched))
2071 (push (cons pos dst)
2072 (alist-get buf buffer-edits)))
2073 (t (setq failures (1+ failures))))
2074 (and (not (eq (prog1 (point) (ignore-errors (diff-hunk-next)))
2075 (point)))
2076 (looking-at-p diff-hunk-header-re)))))
2077 (cond ((zerop failures)
2078 (dolist (buf-edits (reverse buffer-edits))
2079 (with-current-buffer (car buf-edits)
2080 (dolist (edit (cdr buf-edits))
2081 (let ((pos (car edit))
2082 (dst (cdr edit))
2083 (inhibit-read-only t))
2084 (goto-char (car pos))
2085 (delete-region (car pos) (cdr pos))
2086 (insert (car dst))))
2087 (save-buffer)))
2088 (message "Saved %d buffers" (length buffer-edits)))
2089 (t
2090 (message "%d hunks failed; no buffers changed" failures)))))
2091
2057(defalias 'diff-mouse-goto-source #'diff-goto-source) 2092(defalias 'diff-mouse-goto-source #'diff-goto-source)
2058 2093
2059(defun diff-goto-source (&optional other-file event) 2094(defun diff-goto-source (&optional other-file event)