]> git.eshelyaron.com Git - emacs.git/commitdiff
* lisp/vc/diff-mode.el (diff-font-lock-prettify): New var
authorStefan Monnier <monnier@iro.umontreal.ca>
Fri, 13 Jul 2018 16:34:50 +0000 (12:34 -0400)
committerStefan Monnier <monnier@iro.umontreal.ca>
Fri, 13 Jul 2018 16:35:25 +0000 (12:35 -0400)
(diff--font-lock-prettify): New function.
(diff-font-lock-keywords): Use it.

etc/NEWS
lisp/vc/diff-mode.el

index 1a1e0d8b70e88b210c443d9609dc113c98f58b8a..c69bbe9d0f65f6ba9361ac9edde55eb02b3434ac 100644 (file)
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -212,6 +212,13 @@ navigation and editing of large files.
 \f
 * Changes in Specialized Modes and Packages in Emacs 27.1
 
+** diff-mode
+*** Hunks are now automatically refined by default
+To disable it, set the new defcustom 'diff-font-lock-refine' to nil.
+
+*** File headers can be shortened, mimicking Magit's diff format
+To enable it, set the new defcustom 'diff-font-lock-prettify to t.
+
 ** Browse-url
 
 *** The function 'browse-url-emacs' can now visit a URL in selected window.
index ffbd9e5479af15f6a94e014b85d1a8d8c98e2e97..b91a2ba45a47c44e5a60bd6ce7ca00233e266380 100644 (file)
@@ -96,6 +96,11 @@ when editing big diffs)."
   :version "27.1"
   :type 'boolean)
 
+(defcustom diff-font-lock-prettify nil
+  "If non-nil, font-lock will try and make the format prettier."
+  :version "27.1"
+  :type 'boolean)
+
 (defvar diff-vc-backend nil
   "The VC backend that created the current Diff buffer, if any.")
 
@@ -396,6 +401,7 @@ and the face `diff-added' for added lines.")
      (1 font-lock-comment-delimiter-face)
      (2 font-lock-comment-face))
     ("^[^-=+*!<>#].*\n" (0 'diff-context))
+    (,#'diff--font-lock-prettify)
     (,#'diff--font-lock-refined)))
 
 (defconst diff-font-lock-defaults
@@ -2195,6 +2201,35 @@ fixed, visit it in a buffer."
                              modified-buffers ", "))
        (message "No trailing whitespace to delete.")))))
 
+
+;;; Prettifying from font-lock
+
+(defun diff--font-lock-prettify (limit)
+  ;; Mimicks the output of Magit's diff.
+  ;; FIXME: This has only been tested with Git's diff output.
+  (when diff-font-lock-prettify
+    (while (re-search-forward "^diff " limit t)
+      (when (save-excursion
+                  (forward-line 0)
+                  (looking-at (eval-when-compile
+                                (concat "diff.*\n"
+                                        "\\(?:\\(?:new file\\|deleted\\).*\n\\)?"
+                                        "\\(?:index.*\n\\)?"
+                                        "--- \\(?:/dev/null\\|a/\\(.*\\)\\)\n"
+                                        "\\+\\+\\+ \\(?:/dev/null\\|b/\\(.*\\)\\)\n"))))
+        (put-text-property (match-beginning 0)
+                           (or (match-beginning 2) (match-beginning 1))
+                           'display (propertize
+                                     (cond
+                                      ((null (match-beginning 1)) "new file  ")
+                                      ((null (match-beginning 2)) "deleted   ")
+                                      (t                          "modified  "))
+                                     'face '(diff-file-header diff-header)))
+        (unless (match-beginning 2)
+          (put-text-property (match-end 1) (1- (match-end 0))
+                             'display "")))))
+  nil)
+
 ;;; Support for converting a diff to diff3 markers via `wiggle'.
 
 ;; Wiggle can be found at http://neil.brown.name/wiggle/ or in your nearest