Add a new command to regenerate a hunk in diff-mode

* lisp/vc/diff-mode.el (diff-refresh-hunk): New function (bug#44312).
(diff-mode-map): Bind C-c C-l.
This commit is contained in:
Dima Kogan 2020-10-30 14:04:06 +01:00 committed by Lars Ingebrigtsen
parent 32c5f1c7a8
commit 0836335cdf
3 changed files with 36 additions and 8 deletions

View file

@ -1629,6 +1629,10 @@ Convert the entire buffer to unified diff format
unified format to context format. When the mark is active, convert
only the hunks within the region.
@item C-c C-l
@findex diff-refresh-hunk
Re-generate the current hunk (@code{diff-refresh-hunk}).
@item C-c C-w
@findex diff-ignore-whitespace-hunk
Re-generate the current hunk, disregarding changes in whitespace

View file

@ -1208,6 +1208,17 @@ them, using the DEL and INS buttons respectively. This is useful in
Custom buffers, for example, to change the order of the elements in a
list.
** Diff
---
*** New 'diff-mode' font locking face 'diff-error'.
This face is used for error messages from diff.
+++
*** New command 'diff-refresh-hunk'.
This new command (bound to 'C-c C-l') regenerates the current hunk.
** Miscellaneous
+++
@ -1308,10 +1319,6 @@ number [10]", or not have the default displayed at all, like "Enter a
number". (This requires that all callers are altered to use
'format-prompt', though.)
---
*** New 'diff-mode' font locking face 'diff-error'.
This face is used for error messages from diff.
+++
*** New global mode 'global-goto-address-mode'.
This will enable 'goto-address-mode' in all buffers.

View file

@ -208,6 +208,8 @@ and hunk-based syntax highlighting otherwise as a fallback."
;; `d' because it duplicates the context :-( --Stef
("\C-c\C-d" . diff-unified->context)
("\C-c\C-w" . diff-ignore-whitespace-hunk)
;; `l' because it "refreshes" the hunk like C-l refreshes the screen
("\C-c\C-l" . diff-refresh-hunk)
("\C-c\C-b" . diff-refine-hunk) ;No reason for `b' :-(
("\C-c\C-f" . next-error-follow-minor-mode))
"Keymap for `diff-mode'. See also `diff-mode-shared-map'.")
@ -244,6 +246,8 @@ and hunk-based syntax highlighting otherwise as a fallback."
:help "Split the current (unified diff) hunk at point into two hunks"]
["Ignore whitespace changes" diff-ignore-whitespace-hunk
:help "Re-diff the current hunk, ignoring whitespace differences"]
["Recompute the hunk" diff-refresh-hunk
:help "Re-diff the current hunk, keeping the whitespace differences"]
["Highlight fine changes" diff-refine-hunk
:help "Highlight changes of hunk at point at a finer granularity"]
["Kill current hunk" diff-hunk-kill
@ -2045,8 +2049,15 @@ For use in `add-log-current-defun-function'."
(defun diff-ignore-whitespace-hunk ()
"Re-diff the current hunk, ignoring whitespace differences."
(interactive)
(diff-refresh-hunk t))
(defun diff-refresh-hunk (&optional ignore-whitespace)
"Re-diff the current hunk."
(interactive)
(let* ((char-offset (- (point) (diff-beginning-of-hunk t)))
(opts (pcase (char-after) (?@ "-bu") (?* "-bc") (_ "-b")))
(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))))
@ -2057,7 +2068,12 @@ For use in `add-log-current-defun-function'."
(file1 (make-temp-file "diff1"))
(file2 (make-temp-file "diff2"))
(coding-system-for-read buffer-file-coding-system)
old new)
opts old new)
(when ignore-whitespace
(setq opts '("-b")))
(when opt-type
(setq opts (cons opt-type opts)))
(unwind-protect
(save-excursion
(setq old (diff-hunk-text hunk nil char-offset))
@ -2066,8 +2082,9 @@ For use in `add-log-current-defun-function'."
(write-region (concat lead (car new)) nil file2 nil 'nomessage)
(with-temp-buffer
(let ((status
(call-process diff-command nil t nil
opts file1 file2)))
(apply 'call-process
`(,diff-command nil t nil
,@opts ,file1 ,file2))))
(pcase status
(0 nil) ;Nothing to reformat.
(1 (goto-char (point-min))