]> code.delx.au - gnu-emacs-elpa/blobdiff - diff-hl-flydiff.el
One fewer version check
[gnu-emacs-elpa] / diff-hl-flydiff.el
index 6215ab7bc26960786bdd247498e48a1c5cb60278..0c421cea0cc4653e7fe0c82b1b69ef54724de9ae 100644 (file)
 ;;; Code:
 
 (require 'diff-hl)
-(require 'nadvice)
+(require 'diff)
+(unless (require 'nadvice nil t)
+  (error "`diff-hl-flydiff-mode' requires Emacs 24.4 or newer"))
 
 (defvar diff-hl-flydiff-modified-tick 0)
 (defvar diff-hl-flydiff-timer)
 (make-variable-buffer-local 'diff-hl-flydiff-modified-tick)
 
+(defun diff-hl-flydiff/vc-git--symbolic-ref (file)
+  (or
+   (vc-file-getprop file 'vc-git-symbolic-ref)
+   (let* (process-file-side-effects
+          (str (vc-git--run-command-string nil "symbolic-ref" "HEAD")))
+     (vc-file-setprop file 'vc-git-symbolic-ref
+                      (if str
+                          (if (string-match "^\\(refs/heads/\\)?\\(.+\\)$" str)
+                              (match-string 2 str)
+                            str))))))
+
+(defun diff-hl-flydiff/vc-git-working-revision (_file)
+  "Git-specific version of `vc-working-revision'."
+  (let (process-file-side-effects)
+    (vc-git--rev-parse "HEAD")))
+
+(defun diff-hl-flydiff/vc-git-mode-line-string (file)
+  "Return a string for `vc-mode-line' to put in the mode line for FILE."
+  (let* ((rev (vc-working-revision file))
+         (disp-rev (or (diff-hl-flydiff/vc-git--symbolic-ref file)
+                       (substring rev 0 7)))
+         (def-ml (vc-default-mode-line-string 'Git file))
+         (help-echo (get-text-property 0 'help-echo def-ml))
+         (face   (get-text-property 0 'face def-ml)))
+    (propertize (replace-regexp-in-string (concat rev "\\'") disp-rev def-ml t t)
+                'face face
+                'help-echo (concat help-echo "\nCurrent revision: " rev))))
+
 ;; Polyfill concrete revisions for vc-git-working-revision in Emacs 24.4, 24.5
 (when (version<= emacs-version "25.0")
   (with-eval-after-load 'vc-git
-    (defun vc-git--symbolic-ref (file)
-      (or
-        (vc-file-getprop file 'vc-git-symbolic-ref)
-        (let* (process-file-side-effects
-                (str (vc-git--run-command-string nil "symbolic-ref" "HEAD")))
-          (vc-file-setprop file 'vc-git-symbolic-ref
-            (if str
-              (if (string-match "^\\(refs/heads/\\)?\\(.+\\)$" str)
-                (match-string 2 str)
-                str))))))
-
-    (defun diff-hl-flydiff/vc-git-working-revision (_file)
-      "Git-specific version of `vc-working-revision'."
-      (let (process-file-side-effects)
-        (vc-git--rev-parse "HEAD")))
-
-    (defun diff-hl-flydiff/vc-git-mode-line-string (file)
-      "Return a string for `vc-mode-line' to put in the mode line for FILE."
-      (let* ((rev (vc-working-revision file))
-              (disp-rev (or (vc-git--symbolic-ref file)
-                          (substring rev 0 7)))
-              (def-ml (vc-default-mode-line-string 'Git file))
-              (help-echo (get-text-property 0 'help-echo def-ml)))
-        (propertize (replace-regexp-in-string (concat rev "\\'") disp-rev def-ml t t)
-          'help-echo (concat help-echo "\nCurrent revision: " rev))))
-
     (advice-add 'vc-git-working-revision :override
-      #'diff-hl-flydiff/vc-git-working-revision)
+                #'diff-hl-flydiff/vc-git-working-revision)
     (advice-add 'vc-git-mode-line-string :override
-      #'diff-hl-flydiff/vc-git-mode-line-string)))
+                #'diff-hl-flydiff/vc-git-mode-line-string)))
 
 (defun diff-hl-flydiff/working-revision (file)
   "Like vc-working-revision, but always up-to-date"
@@ -120,7 +124,7 @@ This requires the external program `diff' to be in your `exec-path'."
                temporary-file-directory))
             (rev (diff-hl-flydiff-create-revision file
                    (diff-hl-flydiff/working-revision file))))
-      (diff-no-select rev (current-buffer) "-U 0" 'noasync
+      (diff-no-select rev (current-buffer) "-U 0 --strip-trailing-cr" 'noasync
         (get-buffer-create " *diff-hl-diff*")))))
 
 (defun diff-hl-flydiff/update (old-fun &optional auto)
@@ -132,14 +136,7 @@ This requires the external program `diff' to be in your `exec-path'."
     (funcall old-fun)))
 
 (defun diff-hl-flydiff/modified-p (state)
-  (or
-    (buffer-modified-p)
-    (eq state 'edited)
-    (and (eq state 'up-to-date)
-      ;; VC state is stale in after-revert-hook.
-      (or revert-buffer-in-progress-p
-        ;; Diffing against an older revision.
-        diff-hl-reference-revision))))
+  (buffer-modified-p))
 
 (defun diff-hl-flydiff/update-modified-tick (&rest args)
   (setq diff-hl-flydiff-modified-tick (buffer-modified-tick)))
@@ -154,7 +151,7 @@ This requires the external program `diff' to be in your `exec-path'."
       (advice-add 'diff-hl-update :around #'diff-hl-flydiff/update)
       (advice-add 'diff-hl-overlay-modified :override #'ignore)
 
-      (advice-add 'diff-hl-modified-p :override
+      (advice-add 'diff-hl-modified-p :before-until
         #'diff-hl-flydiff/modified-p)
       (advice-add 'diff-hl-changes-buffer :override
         #'diff-hl-flydiff-buffer-with-head)