From 0836335cdff727142a8a2831942f42b18cb5c780 Mon Sep 17 00:00:00 2001 From: Dima Kogan Date: Fri, 30 Oct 2020 14:04:06 +0100 Subject: [PATCH] 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. --- doc/emacs/files.texi | 4 ++++ etc/NEWS | 15 +++++++++++---- lisp/vc/diff-mode.el | 25 +++++++++++++++++++++---- 3 files changed, 36 insertions(+), 8 deletions(-) diff --git a/doc/emacs/files.texi b/doc/emacs/files.texi index 51e8bd1382f..eb4353b6784 100644 --- a/doc/emacs/files.texi +++ b/doc/emacs/files.texi @@ -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 diff --git a/etc/NEWS b/etc/NEWS index 11783ef3192..4cc66aef6bc 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -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. diff --git a/lisp/vc/diff-mode.el b/lisp/vc/diff-mode.el index 7c9ad25eb31..5aeb8feb990 100644 --- a/lisp/vc/diff-mode.el +++ b/lisp/vc/diff-mode.el @@ -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)) -- 2.39.2