]> code.delx.au - gnu-emacs-elpa/blobdiff - diff-hl-flydiff.el
Merge pull request #50 from PythonNut/master
[gnu-emacs-elpa] / diff-hl-flydiff.el
index 3d7c2c7f4caea61700e96193bec2368247054b87..b5f6b4c851005ec8a07f6850560bb83f475f331d 100644 (file)
@@ -1,4 +1,4 @@
-;; Copyright (C) 2012-2013  Free Software Foundation, Inc.
+;; Copyright (C) 2015 Free Software Foundation, Inc.
 
 ;; Author:   Jonathan Hayase <PythonNut@gmail.com>
 ;; URL:      https://github.com/dgutov/diff-hl
 
 ;;; Commentary:
 
+;; This mode enables diffing on-the-fly (i.e. without saving the buffer first)
+;; Toggle in all buffers with M-x diff-hl-flydiff-mode
+
 ;;; Code:
 
 (require 'diff-hl)
+(require 'nadvice)
 
 (defvar diff-hl-flydiff-modified-tick 0)
 (defvar diff-hl-flydiff-timer)
 (make-variable-buffer-local 'diff-hl-flydiff-modified-tick)
 
 ;; Polyfill concrete revisions for vc-git-working-revision in Emacs 24.4, 24.5
-(when (version<= emacs-version "24.5")
+(when (version<= emacs-version "25.0")
   (with-eval-after-load 'vc-git
     (defun vc-git--symbolic-ref (file)
       (or
     (advice-add 'vc-git-mode-line-string :override
       #'diff-hl-flydiff/vc-git-mode-line-string)))
 
+(defun diff-hl-flydiff/working-revision (file)
+  "Like vc-working-revision, but always up-to-date"
+  (vc-file-setprop file 'vc-working-revision
+    (vc-call-backend (vc-backend file) 'working-revision file)))
+
 (defun diff-hl-flydiff-make-temp-file-name (file rev &optional manual)
   "Return a backup file name for REV or the current version of FILE.
 If MANUAL is non-nil it means that a name for backups created by
@@ -99,24 +108,21 @@ the user should be returned."
                 (delete-file filename)))))))
     filename))
 
-(defun diff-hl-flydiff-buffer-with-head ()
+(defun diff-hl-flydiff-buffer-with-head (file &optional backend)
   "View the differences between BUFFER and its associated file.
 This requires the external program `diff' to be in your `exec-path'."
   (interactive)
   (vc-ensure-vc-buffer)
   (with-current-buffer (get-buffer (current-buffer))
-    (let ((rev (diff-hl-flydiff-create-revision
-                 buffer-file-name
-                 (vc-working-revision buffer-file-name
-                   (vc-responsible-backend buffer-file-name))))
-           (temporary-file-directory
+    (let* ((temporary-file-directory
              (if (file-directory-p "/dev/shm/")
                "/dev/shm/"
-               temporary-file-directory)))
+               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
         (get-buffer-create " *diff-hl-diff*")))))
 
-
 (defun diff-hl-flydiff/update (old-fun &optional auto)
   (unless (and auto
             (or
@@ -125,49 +131,11 @@ This requires the external program `diff' to be in your `exec-path'."
               (not (buffer-modified-p))))
     (funcall old-fun)))
 
-(defun diff-hl-flydiff/changes (&rest args)
-  (let* ((file buffer-file-name)
-          (backend (vc-backend file)))
-    (when backend
-      (let ((state (vc-state file backend)))
-        (cond
-          ((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)))
-            (let (diff-auto-refine-mode res)
-              (with-current-buffer (diff-hl-flydiff-buffer-with-head)
-                (goto-char (point-min))
-                (unless (eobp)
-                  (ignore-errors
-                    (diff-beginning-of-hunk t))
-                  (while (looking-at diff-hunk-header-re-unified)
-                    (let ((line (string-to-number (match-string 3)))
-                           (len (let ((m (match-string 4)))
-                                  (if m (string-to-number m) 1)))
-                           (beg (point)))
-                      (diff-end-of-hunk)
-                      (let* ((inserts (diff-count-matches "^\\+" beg (point)))
-                              (deletes (diff-count-matches "^-" beg (point)))
-                              (type (cond ((zerop deletes) 'insert)
-                                      ((zerop inserts) 'delete)
-                                      (t 'change))))
-                        (when (eq type 'delete)
-                          (setq len 1)
-                          (cl-incf line))
-                        (push (list line len type) res))))))
-              (setq diff-hl-flydiff-modified-tick (buffer-modified-tick))
-              (nreverse res)))
-          ((eq state 'added)
-            `((1 ,(line-number-at-pos (point-max)) insert)))
-          ((eq state 'removed)
-            `((1 ,(line-number-at-pos (point-max)) delete))))))))
-
-(defun diff-hl-flydiff/overlay-modified (&rest args))
+(defun diff-hl-flydiff/modified-p (state)
+  (buffer-modified-p))
+
+(defun diff-hl-flydiff/update-modified-tick (&rest args)
+  (setq diff-hl-flydiff-modified-tick (buffer-modified-tick)))
 
 ;;;###autoload
 (define-minor-mode diff-hl-flydiff-mode
@@ -176,25 +144,26 @@ This requires the external program `diff' to be in your `exec-path'."
   :global t
   (if diff-hl-flydiff-mode
     (progn
-      (require 'nadvice)
-      (advice-add 'diff-hl-update :around
-        #'diff-hl-flydiff/update)
-      (advice-add 'diff-hl-changes :override
-        #'diff-hl-flydiff/changes)
-      (advice-add 'diff-hl-overlay-modified :override
-        #'diff-hl-flydiff/overlay-modified)
-
-      (remove-hook 'after-change-functions #'diff-hl-edit t)
+      (advice-add 'diff-hl-update :around #'diff-hl-flydiff/update)
+      (advice-add 'diff-hl-overlay-modified :override #'ignore)
+
+      (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)
+      (advice-add 'diff-hl-change :after
+        #'diff-hl-flydiff/update-modified-tick)
+
       (setq diff-hl-flydiff-timer
         (run-with-idle-timer 0.3 t #'diff-hl-update t)))
 
     (advice-remove 'diff-hl-update #'diff-hl-flydiff/update)
-    (advice-remove 'diff-hl-changes #'diff-hl-flydiff/changes)
-    (advice-remove 'diff-hl-overlay-modified
-      #'diff-hl-flydiff/overlay-modified)
+    (advice-remove 'diff-hl-overlay-modified #'ignore)
+
+    (advice-remove 'diff-hl-modified-p #'diff-hl-flydiff/modified-p)
+    (advice-remove 'diff-hl-changes-buffer #'diff-hl-flydiff-buffer-with-head)
+    (advice-remove 'diff-hl-change #'diff-hl-flydiff/update-modified-tick)
 
-    (cancel-timer diff-hl-flydiff-timer)
-    (when diff-hl-mode
-      (add-hook 'after-change-functions 'diff-hl-edit nil t))))
+    (cancel-timer diff-hl-flydiff-timer)))
 
 (provide 'diff-hl-flydiff)