From 076a74fd4798bd3908aa0cd4ed07ed85e03559c1 Mon Sep 17 00:00:00 2001 From: Sean Whitton Date: Sat, 21 Sep 2024 11:16:18 +0100 Subject: [PATCH 2/2] New command 'diff-discard-hunk' * lisp/vc/diff-mode.el (diff-discard-hunk): New command. (diff-mode-map): Bind it to C-c C-k. (diff-mode-menu): New entry "Discard hunk". * doc/emacs/files.texi (Diff Mode): * etc/NEWS: Document the new command. --- doc/emacs/files.texi | 11 +++++++++++ etc/NEWS | 5 +++++ lisp/vc/diff-mode.el | 23 +++++++++++++++++++++++ 3 files changed, 39 insertions(+) diff --git a/doc/emacs/files.texi b/doc/emacs/files.texi index 709cb0910e6..04ecfabc1ee 100644 --- a/doc/emacs/files.texi +++ b/doc/emacs/files.texi @@ -1682,6 +1682,17 @@ Diff Mode version. If @code{diff-jump-to-old-file} is non-@code{nil}, apply the hunk to the ``old'' version of the file instead. +@findex diff-discard-hunk +@item C-c C-k +Reverse-apply this hunk to the target file, and then kill it +(@code{diff-discard-hunk}). Unless the buffer visiting the target file +was already modified, save it. + +This command is useful in buffers generated by @w{@kbd{C-x v =}} and +@w{@kbd{C-x v D}} (@pxref{Old Revisions}). You can use this command to +remove hunks you never intend to commit, such as temporary debug prints, +and the like. + @findex diff-apply-buffer @item C-c @key{RET} a Apply all the hunks in the buffer (@code{diff-apply-buffer}). If the diff --git a/etc/NEWS b/etc/NEWS index 65e3a484e7a..8da68eded82 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -310,6 +310,11 @@ This option can be customized to nil to inhibit how, by default, 'diff-apply-hunk' displays the changed text after applying the hunk. It can also be let-bound by Lisp code wrapping 'diff-apply-hunk'. ++++ +*** New command 'diff-discard-hunk' bound to C-c C-k. +This command reverse-applies the hunk at point, and then kills it. +This is useful in buffers generated C-x v = and C-x v D. + ** php-ts-mode --- diff --git a/lisp/vc/diff-mode.el b/lisp/vc/diff-mode.el index 8af155c79e0..3a6bc62b78a 100644 --- a/lisp/vc/diff-mode.el +++ b/lisp/vc/diff-mode.el @@ -222,6 +222,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-k" #'diff-discard-hunk "C-c C-m a" #'diff-apply-buffer "C-c C-e" #'diff-ediff-patch "C-c C-n" #'diff-restrict-view @@ -246,6 +247,8 @@ diff-mode-menu :help "Apply the current hunk to the source file and go to the next"] ["Test applying hunk" diff-test-hunk :help "See whether it's possible to apply the current hunk"] + ["Discard hunk" diff-discard-hunk + :help "Reverse-apply and then kill the current hunk."] ["Apply all hunks" diff-apply-buffer :help "Apply all hunks in the current diff buffer"] ["Apply diff with Ediff" diff-ediff-patch @@ -2055,6 +2058,26 @@ diff-kill-applied-hunks (diff-hunk-kill) (diff-hunk-next))))) +(defun diff-discard-hunk () + "Reverse-apply and then kill the hunk at point. +Unless the buffer was already modified, save the changed buffer. + +This command is useful in buffers generated by \\[vc-diff] and \\[vc-root-diff]. +You can use `diff-hunk-kill' to remove hunks you intend to commit later. +You can use this command to remove hunks you never intend to commit, +such as temporary debug prints, and the like." + (interactive) + (let* ((diff-advance-after-apply-hunk nil) + (diff-display-after-apply-hunk nil) + (buffer (car (diff-find-source-location))) + (to-save (and (not (buffer-modified-p buffer)) + buffer))) + (diff-apply-hunk t) + (diff-hunk-kill) + (when to-save + (with-current-buffer to-save + (save-buffer))))) + (defun diff-apply-buffer () "Apply the diff in the entire diff buffer. When applying all hunks was successful, then save the changed buffers." -- 2.39.5