From: Juri Linkov <juri@linkov.net>
To: 66113@debbugs.gnu.org
Subject: bug#66113: Apply the entire diff buffer
Date: Wed, 20 Sep 2023 09:46:18 +0300 [thread overview]
Message-ID: <86o7hx4a11.fsf@mail.linkov.net> (raw)
[-- 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)
next reply other threads:[~2023-09-20 6:46 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-09-20 6:46 Juri Linkov [this message]
2023-09-22 1:38 ` bug#66113: Apply the entire diff buffer 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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=86o7hx4a11.fsf@mail.linkov.net \
--to=juri@linkov.net \
--cc=66113@debbugs.gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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.