unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#73407: 31.0.50; Add diff-discard-hunk
@ 2024-09-21 10:19 Sean Whitton
  2024-09-23 22:52 ` Dmitry Gutov
                   ` (2 more replies)
  0 siblings, 3 replies; 22+ messages in thread
From: Sean Whitton @ 2024-09-21 10:19 UTC (permalink / raw)
  To: 73407; +Cc: dgutov, juri

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

X-debbugs-cc: dgutov@yandex.ru, juri@linkov.net

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

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-New-user-option-diff-display-after-apply-hunk.patch --]
[-- Type: text/x-patch, Size: 2108 bytes --]

From 8d4911ed2db8a1ace388616bff35857559920f84 Mon Sep 17 00:00:00 2001
From: Sean Whitton <spwhitton@spwhitton.name>
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


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #3: 0002-New-command-diff-discard-hunk.patch --]
[-- Type: text/x-patch, Size: 3973 bytes --]

From 076a74fd4798bd3908aa0cd4ed07ed85e03559c1 Mon Sep 17 00:00:00 2001
From: Sean Whitton <spwhitton@spwhitton.name>
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


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

end of thread, other threads:[~2024-09-26 10:52 UTC | newest]

Thread overview: 22+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-09-21 10:19 bug#73407: 31.0.50; Add diff-discard-hunk Sean Whitton
2024-09-23 22:52 ` Dmitry Gutov
2024-09-24  7:53   ` Sean Whitton
2024-09-24 12:27     ` Dmitry Gutov
2024-09-24 15:48       ` Sean Whitton
2024-09-24  6:36 ` Juri Linkov
2024-09-24  8:07   ` Sean Whitton
2024-09-24  8:40   ` Sean Whitton
2024-09-24  8:42     ` Sean Whitton
2024-09-24 12:22       ` Dmitry Gutov
2024-09-24 12:21     ` Eli Zaretskii
2024-09-24 12:23       ` Dmitry Gutov
2024-09-24 14:33         ` Óscar Fuentes
2024-09-24 15:43       ` bug#73407: [PATCH v4] " Sean Whitton
2024-09-24 16:59         ` Juri Linkov
2024-09-24 17:56           ` Sean Whitton
2024-09-24 18:07           ` Eli Zaretskii
2024-09-25  6:36             ` Sean Whitton
2024-09-25 12:17               ` Eli Zaretskii
2024-09-25 19:33                 ` Sean Whitton
2024-09-24 12:20   ` Dmitry Gutov
2024-09-26 10:52 ` bug#73407: 31.0.50; Add diff-revert-and-kill-hunk Sean Whitton

Code repositories for project(s) associated with this public inbox

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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).