Function: diff-revert-and-kill-hunk

diff-revert-and-kill-hunk is an interactive and byte-compiled function defined in diff-mode.el.gz.

Signature

(diff-revert-and-kill-hunk &optional BEG END)

Documentation

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 C-x v = (vc-diff) and C-x v D (vc-root-diff), especially when preparing to commit the patch with C-x v v (vc-next-action). You can use M-k (diff-hunk-kill) to temporarily remove changes that you intend to include in a separate commit or commits, and you can use this command 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.

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.

View in manual

Probably introduced at or before Emacs version 31.1.

Key Bindings

Source Code

;; Defined in /usr/src/emacs/lisp/vc/diff-mode.el.gz
(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].
You can use \\<diff-mode-map>\\[diff-hunk-kill] \
to temporarily remove changes that you intend to
include in a separate commit or commits, and you can use this command
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.

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 "R")
  (when (xor beg end)
    (error "Invalid call to `diff-revert-and-kill-hunk'"))
  (when (or (not diff-ask-before-revert-and-kill-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)
          (unless (looking-at diff-hunk-header-re)
            (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.
      (goto-char end)
      (when-let* ((pos (diff--at-diff-header-p)))
        (goto-char pos))
      (setq beg (copy-marker beg) end (point-marker))
      (unwind-protect
          (cl-loop initially (goto-char beg)
                   do (diff-hunk-kill)
                   until (or (< (point) (marker-position beg))
                             (eql (point) (marker-position end))))
        (set-marker beg nil)
        (set-marker end nil)))))