Function: diff-delete-other-hunks

diff-delete-other-hunks is an interactive and byte-compiled function defined in diff-mode.el.gz.

Signature

(diff-delete-other-hunks &optional BEG END)

Documentation

Delete hunks other than the current one.

Interactively, if the region is active, delete all hunks that the region overlaps; otherwise delete all hunks except the current one. When calling from Lisp, pass BEG and END as the bounds of the region in which to delete hunks; BEG and END omitted or nil means to delete all the hunks but the one which contains point.

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
;; This is not `diff-kill-other-hunks' because we might need to make
;; copies of file headers in order to ensure the new kill ring entry
;; would be a patch with the same meaning.  That is not implemented
;; because it does not seem like it would be useful.
(defun diff-delete-other-hunks (&optional beg end)
  "Delete hunks other than the current one.
Interactively, if the region is active, delete all hunks that the region
overlaps; otherwise delete all hunks except the current one.
When calling from Lisp, pass BEG and END as the bounds of the region in
which to delete hunks; BEG and END omitted or nil means to delete all
the hunks but the one which contains point."
  (interactive "R")
  (when (buffer-narrowed-p)
    (user-error "Command is not safe in a narrowed buffer"))
  (let ((inhibit-read-only t))
    (save-excursion
      (cond ((xor beg end)
             (error "Require exactly zero or two arguments"))
            (beg
             (goto-char beg)
             (setq beg (car (diff-bounds-of-hunk)))
             (goto-char end)
             (setq end (cadr (diff-bounds-of-hunk))))
            (t
             (pcase-setq `(,beg ,end) (diff-bounds-of-hunk))))
      (delete-region end (point-max))
      (goto-char beg)
      (diff-beginning-of-file)
      (diff-hunk-next)
      (delete-region (point) beg)
      (diff-beginning-of-file-and-junk)
      (delete-region (point-min) (point)))))