1
Fork 0
mirror of git://git.sv.gnu.org/emacs.git synced 2025-12-06 06:20:55 -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

@ -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)
(when (null (diff-apply-buffer beg end t))
(diff-hunk-kill)))))
(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))
;; 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.