1
Fork 0
mirror of git://git.sv.gnu.org/emacs.git synced 2025-12-05 22:20:24 -08:00

Make diff-revert-and-kill-hunk consider an active region

* lisp/vc/diff-mode.el (diff-revert-and-kill-hunk): When the
region is active, operate on all hunks it overlaps.
* doc/emacs/files.texi (Diff Mode):
* etc/NEWS: Document the change.
This commit is contained in:
Sean Whitton 2025-11-25 21:07:04 +00:00
parent f5953186ef
commit 304d4435b1
3 changed files with 40 additions and 9 deletions

View file

@ -1835,7 +1835,8 @@ confirm deletions or applying hunks to backup files.
@item C-c M-u
Revert this hunk, and then remove the hunk from the diffs
(@code{diff-revert-and-kill-hunk}). Save the buffer visiting the target
file.
file. When the region is active, the command reverse-applies and kills
hunks that the region overlaps.
This command is useful in buffers generated by @w{@kbd{C-x v =}} and
@w{@kbd{C-x v D}} (@pxref{Old Revisions}). These buffers present you
@ -1843,7 +1844,7 @@ with a view of the changes you've made, and you can use this command to
undo changes you didn't intend to do, or no longer want.
This is a destructive operation, so by default, this command asks you to
confirm you really want to revert and kill the hunk. You can customize
confirm you really want to revert and kill the hunks. You can customize
@code{diff-ask-before-revert-and-kill-hunk} to control that.
@findex diff-apply-buffer

View file

@ -2031,6 +2031,8 @@ This command reverts the hunk at point (i.e., applies the reverse of the
hunk), and then removes the hunk from the diffs.
This is useful to undo or revert changes, committed and uncommitted, when
you are in buffers generated by 'C-x v =' and 'C-x v D'.
When the region is active, the command reverse-applies and kills hunks
that the region overlaps.
---
*** 'diff-file-prev' and 'diff-hunk-prev' always move to start of header.

View file

@ -2256,8 +2256,10 @@ With a prefix argument, try to REVERSE the hunk."
:type 'boolean
:version "31.1")
(defun diff-revert-and-kill-hunk ()
(defun diff-revert-and-kill-hunk (&optional beg end)
"Reverse-apply and then kill the hunk at point. Save changed buffer.
Interactively, if the region is active, reverse-apply and kill all
hunks that the region overlaps.
This command is useful in buffers generated by \\[vc-diff] and \\[vc-root-diff],
especially when preparing to commit the patch with \\[vc-next-action].
@ -2268,13 +2270,39 @@ to permanently drop changes you didn't intend, or no longer want.
This is a destructive operation, so by default, this command asks you to
confirm you really want to reverse-apply and kill the hunk. You can
customize `diff-ask-before-revert-and-kill-hunk' to control that."
(interactive)
customize `diff-ask-before-revert-and-kill-hunk' to control that.
When called from Lisp with optional arguments BEG and END non-nil,
reverse-apply and kill all hunks overlapped by the region from BEG to
END as though called interactively with an active region delimited by
BEG and END."
(interactive (list (use-region-beginning) (use-region-end)))
(when (xor beg end)
(error "Invalid call to `diff-revert-and-kill-hunk'"))
(when (or (not diff-ask-before-revert-and-kill-hunk)
(yes-or-no-p "Really reverse-apply and kill this hunk?"))
(cl-destructuring-bind (beg end) (diff-bounds-of-hunk)
(y-or-n-p "Really reverse-apply and kill hunk(s)?"))
(if beg
(save-excursion
(goto-char beg)
(setq beg (car (diff-bounds-of-hunk)))
(goto-char end)
(setq end (cadr (diff-bounds-of-hunk))))
(pcase-setq `(,beg ,end) (diff-bounds-of-hunk)))
(when (null (diff-apply-buffer beg end t))
(diff-hunk-kill)))))
;; Use `diff-hunk-kill' because it properly handles file headers,
;; except if we are killing the last hunk we need not be concerned
;; with that. If we are not deleting the very last hunk then
;; exploit how `diff-hunk-kill' will always leave us at the
;; beginning of a hunk (except when killing the very last hunk!).
(if (eql end (point-max))
(let ((inhibit-read-only t))
(kill-region beg end))
(setq end (copy-marker end))
(unwind-protect
(cl-loop initially (goto-char beg)
do (diff-hunk-kill)
until (eql (point) (marker-position end)))
(set-marker end nil))))))
(defun diff-apply-buffer (&optional beg end reverse test-or-no-save)
"Apply the diff in the entire diff buffer.