Subject: 31.0.50; Add diff-discard-hunk
From: Sean Whitton <[email-masked]>
To: [email-masked]
Date: Sat, 21 Sep 2024 11:19:06 +0100
List-Id: <debbugs-submit.debbugs.gnu.org>

X-debbugs-cc: [email-masked], [email-masked]

These patches add a new command, diff-discard-hunk, inspired by some
functionality from Magit.  It nicely complements a workflow based around
committing with C-x v v from C-x v D.  I have been using it every day
for a year or so, and I think others will find it useful, too.

Posting for comments.  Thanks!

-- 
Sean Whitton
From 8d4911ed2db8a1ace388616bff35857559920f84 Mon Sep 17 00:00:00 2001
From: Sean Whitton <[email-masked]>
Date: Sat, 21 Sep 2024 10:51:23 +0100
Subject: [PATCH 1/2] New user option 'diff-display-after-apply-hunk'

* lisp/vc/diff-mode.el (diff-display-after-apply-hunk): New option.
(diff-apply-hunk): Use it.
* etc/NEWS: Announce the new option.
---
 etc/NEWS             | 6 ++++++
 lisp/vc/diff-mode.el | 7 ++++++-
 2 files changed, 12 insertions(+), 1 deletion(-)

diff --git a/etc/NEWS b/etc/NEWS
index 8bd5c7c9515..65e3a484e7a 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -304,6 +304,12 @@ according to diffs in the current buffer, but without applying the diffs
 to the original text.  If the selected range extends a hunk, the
 command attempts to look up and copy the text in-between the hunks.
 
+---
+*** New user option 'diff-display-after-apply-hunk'.
+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'.
+
 ** php-ts-mode
 
 ---
diff --git a/lisp/vc/diff-mode.el b/lisp/vc/diff-mode.el
index 4810b9ce01c..8af155c79e0 100644
--- a/lisp/vc/diff-mode.el
+++ b/lisp/vc/diff-mode.el
@@ -95,6 +95,10 @@ diff-advance-after-apply-hunk
   "Non-nil means `diff-apply-hunk' will move to the next hunk after applying."
   :type 'boolean)
 
+(defcustom diff-display-after-apply-hunk t
+  "Non-nil means `diff-apply-hunk' will display the buffer it just changed."
+  :type 'boolean)
+
 (defcustom diff-mode-hook nil
   "Run after setting up the `diff-mode' major mode."
   :type 'hook
@@ -2024,7 +2028,8 @@ diff-apply-hunk
 	(delete-region (car pos) (cdr pos))
 	(insert (car new)))
       ;; Display BUF in a window
-      (set-window-point (display-buffer buf) (+ (car pos) (cdr new)))
+      (when diff-display-after-apply-hunk
+        (set-window-point (display-buffer buf) (+ (car pos) (cdr new))))
       (diff-hunk-status-msg line-offset (xor switched reverse) nil)
       (when diff-advance-after-apply-hunk
 	(diff-hunk-next))))))
-- 
2.39.5

From 076a74fd4798bd3908aa0cd4ed07ed85e03559c1 Mon Sep 17 00:00:00 2001
From: Sean Whitton <[email-masked]>
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