]> git.eshelyaron.com Git - emacs.git/commitdiff
Add a new command to regenerate a hunk in diff-mode
authorDima Kogan <dima@secretsauce.net>
Fri, 30 Oct 2020 13:04:06 +0000 (14:04 +0100)
committerLars Ingebrigtsen <larsi@gnus.org>
Fri, 30 Oct 2020 13:04:06 +0000 (14:04 +0100)
* lisp/vc/diff-mode.el (diff-refresh-hunk): New function (bug#44312).
(diff-mode-map): Bind C-c C-l.

doc/emacs/files.texi
etc/NEWS
lisp/vc/diff-mode.el

index 51e8bd1382f089efb4e7a274f5bad506b424004e..eb4353b67841f0c33d0de1106097a15c972d0f0d 100644 (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
index 11783ef319202c76812e279b5dcb34995a620bcd..4cc66aef6bc01c4cdf69a98b63aa28ec3403232d 100644 (file)
--- 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.
index 7c9ad25eb31071957c3421134edbbbfd0234da56..5aeb8feb99015e07a22e124919555577ec90da60 100644 (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))