Function: diff-refresh-hunk

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

Signature

(diff-refresh-hunk &optional IGNORE-WHITESPACE)

Documentation

Re-diff the current hunk.

View in manual

Probably introduced at or before Emacs version 28.1.

Key Bindings

Source Code

;; Defined in /usr/src/emacs/lisp/vc/diff-mode.el.gz
(defun diff-refresh-hunk (&optional ignore-whitespace)
  "Re-diff the current hunk."
  (interactive)
  (let* ((char-offset (- (point) (diff-beginning-of-hunk t)))
	 (opt-type (pcase (char-after)
                     (?@ "-u")
                     (?* "-c")))
	 (line-nb (and (or (looking-at "[^0-9]+\\([0-9]+\\)")
			   (error "Can't find line number"))
		       (string-to-number (match-string 1))))
	 (inhibit-read-only t)
	 (hunk (delete-and-extract-region
		(point) (save-excursion (diff-end-of-hunk) (point))))
	 (lead (make-string (1- line-nb) ?\n)) ;Line nums start at 1.
	 (file1 (make-temp-file "diff1"))
	 (file2 (make-temp-file "diff2"))
	 (coding-system-for-read buffer-file-coding-system)
	 opts old new)
    (when ignore-whitespace
      (setq opts (ensure-list diff-ignore-whitespace-switches)))
    (when opt-type
      (setq opts (cons opt-type opts)))

    (unwind-protect
	(save-excursion
	  (setq old (diff-hunk-text hunk nil char-offset))
	  (setq new (diff-hunk-text hunk t char-offset))
	  (write-region (concat lead (car old)) nil file1 nil 'nomessage)
	  (write-region (concat lead (car new)) nil file2 nil 'nomessage)
	  (with-temp-buffer
	    (let ((status
		   (apply #'call-process
			  `(,diff-command nil t nil
                                         ,@opts ,file1 ,file2))))
	      (pcase status
		(0 nil)                 ;Nothing to reformat.
		(1 (goto-char (point-min))
                   ;; Remove the file-header.
                   (when (re-search-forward diff-hunk-header-re nil t)
                     (delete-region (point-min) (match-beginning 0))))
		(_ (goto-char (point-max))
		   (unless (bolp) (insert "\n"))
		   (insert hunk)))
	      (setq hunk (buffer-string))
	      (unless (memq status '(0 1))
		(error "Diff returned: %s" status)))))
      ;; Whatever happens, put back some equivalent text: either the new
      ;; one or the original one in case some error happened.
      (insert hunk)
      (delete-file file1)
      (delete-file file2))))