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)

Documentation

Apply the current hunk to the source file and go to the next.

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, REVERSE the hunk.

View in manual

Key Bindings

Source Code

;; Defined in /usr/src/emacs/lisp/vc/diff-mode.el.gz
(defun diff-apply-hunk (&optional reverse)
  "Apply the current hunk to the source file and go to the next.
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, REVERSE the hunk."
  (interactive "P")
  (diff-beginning-of-hunk t)
  (pcase-let ((`(,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 nil reverse)))
    (cond
     ((null line-offset)
      (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)))))))
      (error "%s"
	     (substitute-command-keys
              (format "Use %s\\[diff-apply-hunk] to apply it to the other file"
                      (if (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)"))
     (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) (+ (car pos) (cdr new)))
      (diff-hunk-status-msg line-offset (xor switched reverse) nil)
      (when diff-advance-after-apply-hunk
	(diff-hunk-next))))))