Function: diff-apply-hunk
diff-apply-hunk is an interactive and byte-compiled function defined
in diff-mode.el.gz.
Signature
(diff-apply-hunk &optional REVERSE BEG END)
Documentation
Apply the current hunk to its source file and go to the next hunk.
By default, the new source file is patched, but if the variable
diff-jump-to-old-file is non-nil, then the old source file is
patched instead (some commands, such as diff-goto-source can change
the value of this variable when given an appropriate prefix argument).
With a prefix argument (when called from Lisp, with optional argument REVERSE non-nil), reverse-apply the hunk(s).
Prompt to confirm deleting files and applying hunks to backup files.
Offer to reverse-apply hunks that are already applied.
Interactively, if the region is active, apply all hunks that the
region overlaps. In this mode, fail instead of prompting if any
hunks do not cleanly apply, and do not confirm deletions or
applying hunks to backup files (the same as the command
diff-apply-buffer with an active region, which see).
When called from Lisp with optional arguments BEG and END non-nil, apply all hunks overlapped by the region from BEG to END as though called interactively with an active region delimited by BEG and END.
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-apply-hunk (&optional reverse beg end)
"Apply the current hunk to its source file and go to the next hunk.
By default, the new source file is patched, but if the variable
`diff-jump-to-old-file' is non-nil, then the old source file is
patched instead (some commands, such as `diff-goto-source' can change
the value of this variable when given an appropriate prefix argument).
With a prefix argument (when called from Lisp, with optional argument
REVERSE non-nil), reverse-apply the hunk(s).
Prompt to confirm deleting files and applying hunks to backup files.
Offer to reverse-apply hunks that are already applied.
Interactively, if the region is active, apply all hunks that the
region overlaps. In this mode, fail instead of prompting if any
hunks do not cleanly apply, and do not confirm deletions or
applying hunks to backup files (the same as the command
`diff-apply-buffer' with an active region, which see).
When called from Lisp with optional arguments BEG and END non-nil,
apply all hunks overlapped by the region from BEG to END as though
called interactively with an active region delimited by BEG and
END."
(interactive "P\nR")
(cond*
((xor beg end)
(error "Invalid call to `diff-apply-hunk'"))
(beg
(diff-apply-buffer beg end reverse 'no-save))
(t (diff-beginning-of-hunk t))
((bind*
;; Do not accept BUFFER.REV buffers as source location.
(diff-vc-backend nil)
;; When we detect deletion, we will use the old file name.
(deletion (equal null-device (car (diff-hunk-file-names reverse))))))
((pcase* `(,buf ,line-offset ,pos ,old ,new ,switched)
;; Sometimes we'd like to have the following behavior: if
;; REVERSE go to the new file, otherwise go to the old.
;; But that means that by default we use the old file, which is
;; the opposite of the default for diff-goto-source, and is thus
;; confusing. Also when you don't know about it it's
;; pretty surprising.
;; TODO: make it possible to ask explicitly for this behavior.
;;
;; This is duplicated in diff-test-hunk.
(diff-find-source-location (xor deletion reverse) reverse)))
((null line-offset)
(user-error "Can't find the text to patch"))
((with-current-buffer buf
(and buffer-file-name
(backup-file-name-p buffer-file-name)
(not diff-apply-hunk-to-backup-file)
(not
(setq-local diff-apply-hunk-to-backup-file
(yes-or-no-p
(format "Really apply this hunk to %s? "
(file-name-nondirectory buffer-file-name)))))))
(user-error "%s"
(substitute-command-keys
(format "Use %s\\[diff-apply-hunk] to apply it to the other file"
(and (not reverse) "\\[universal-argument] ")))))
((and switched
;; A reversed patch was detected, perhaps apply it in reverse.
(not (save-window-excursion
(pop-to-buffer buf)
(goto-char (+ (car pos) (cdr old)))
(y-or-n-p
(if reverse
"Hunk hasn't been applied yet; apply it now? "
"Hunk has already been applied; undo it? ")))))
(message "(Nothing done)"))
((and deletion (not switched))
(when (y-or-n-p (format-message "Delete file `%s'?"
(buffer-file-name buf)))
(delete-file (buffer-file-name buf) delete-by-moving-to-trash)
(kill-buffer buf)))
(t
;; Apply the hunk
(with-current-buffer buf
(goto-char (car pos))
(delete-region (car pos) (cdr pos))
(insert (car new)))
;; Display BUF in a window
(set-window-point (display-buffer buf '(nil (inhibit-same-window . t)))
(+ (car pos) (cdr new)))
(diff-hunk-status-msg line-offset (xor switched reverse) nil)
(when diff-advance-after-apply-hunk
(diff-hunk-next)))))