all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* bug#66113: Apply the entire diff buffer
@ 2023-09-20  6:46 Juri Linkov
  2023-09-22  1:38 ` Dmitry Gutov
  0 siblings, 1 reply; 12+ messages in thread
From: Juri Linkov @ 2023-09-20  6:46 UTC (permalink / raw)
  To: 66113

[-- Attachment #1: Type: text/plain, Size: 111 bytes --]

After discussing on emacs-devel, here is a complete patch for the feature
of applying the entire diff buffer:


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: diff-apply-buffer.patch --]
[-- Type: text/x-diff, Size: 2081 bytes --]

diff --git a/lisp/vc/diff-mode.el b/lisp/vc/diff-mode.el
index d776375d681..4633d5896a7 100644
--- a/lisp/vc/diff-mode.el
+++ b/lisp/vc/diff-mode.el
@@ -216,6 +216,7 @@ diff-mode-map
   "C-x 4 A" #'diff-add-change-log-entries-other-window
   ;; Misc operations.
   "C-c C-a" #'diff-apply-hunk
+  "C-c C-m a" #'diff-apply-buffer
   "C-c C-e" #'diff-ediff-patch
   "C-c C-n" #'diff-restrict-view
   "C-c C-s" #'diff-split-hunk
@@ -2054,6 +2055,40 @@ diff-kill-applied-hunks
           (diff-hunk-kill)
         (diff-hunk-next)))))
 
+(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 ((buffers 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))
+                      (with-current-buffer buf
+                        (goto-char (car pos))
+                        (delete-region (car pos) (cdr pos))
+                        (insert (car dst))
+                        (when buffer-file-name
+                          (push (current-buffer) buffers))))
+                     (t (setq failures (1+ failures))))
+               (not (or (eq (prog1 (point) (diff-hunk-next)) (point))
+                        (eobp))))))
+    (setq buffers (delete-dups buffers))
+    (cond ((zerop failures)
+           (dolist (buf (reverse buffers))
+             (with-current-buffer buf
+               (save-buffer)))
+           (message "Saved %d buffers" (length buffers)))
+          (t
+           (dolist (buf buffers)
+             (with-current-buffer buf
+               (display-buffer buf)))
+           (message "%d hunks failed; no buffers saved" failures)))))
+
 (defalias 'diff-mouse-goto-source #'diff-goto-source)
 
 (defun diff-goto-source (&optional other-file event)

^ permalink raw reply related	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2023-09-27 17:36 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-09-20  6:46 bug#66113: Apply the entire diff buffer Juri Linkov
2023-09-22  1:38 ` Dmitry Gutov
2023-09-22  6:45   ` Juri Linkov
2023-09-22 13:49     ` Dmitry Gutov
2023-09-22 15:48       ` Juri Linkov
2023-09-23 17:52         ` Juri Linkov
2023-09-24  1:34           ` Dmitry Gutov
2023-09-24  7:34             ` Juri Linkov
2023-09-24 10:58               ` Dmitry Gutov
2023-09-25 17:49                 ` Juri Linkov
2023-09-25 23:07                   ` Dmitry Gutov
2023-09-27 17:36                     ` Juri Linkov

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.