1 ;; Copyright (C) 2012-2013 Free Software Foundation, Inc.
3 ;; Author: Jonathan Hayase <PythonNut@gmail.com>
4 ;; URL: https://github.com/dgutov/diff-hl
6 ;; This file is part of GNU Emacs.
8 ;; GNU Emacs is free software: you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation, either version 3 of the License, or
11 ;; (at your option) any later version.
13 ;; GNU Emacs is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 (defvar diff-hl-flydiff-modified-tick 0)
28 (defvar diff-hl-flydiff-timer)
29 (make-variable-buffer-local 'diff-hl-flydiff-modified-tick)
31 ;; Polyfill concrete revisions for vc-git-working-revision in Emacs 24.4, 24.5
32 (when (version<= emacs-version "24.5")
33 (with-eval-after-load 'vc-git
34 (defun vc-git--symbolic-ref (file)
36 (vc-file-getprop file 'vc-git-symbolic-ref)
37 (let* (process-file-side-effects
38 (str (vc-git--run-command-string nil "symbolic-ref" "HEAD")))
39 (vc-file-setprop file 'vc-git-symbolic-ref
41 (if (string-match "^\\(refs/heads/\\)?\\(.+\\)$" str)
45 (defun diff-hl-flydiff/vc-git-working-revision (_file)
46 "Git-specific version of `vc-working-revision'."
47 (let (process-file-side-effects)
48 (vc-git--rev-parse "HEAD")))
50 (defun diff-hl-flydiff/vc-git-mode-line-string (file)
51 "Return a string for `vc-mode-line' to put in the mode line for FILE."
52 (let* ((rev (vc-working-revision file))
53 (disp-rev (or (vc-git--symbolic-ref file)
55 (def-ml (vc-default-mode-line-string 'Git file))
56 (help-echo (get-text-property 0 'help-echo def-ml)))
57 (propertize (replace-regexp-in-string (concat rev "\\'") disp-rev def-ml t t)
58 'help-echo (concat help-echo "\nCurrent revision: " rev))))
60 (advice-add 'vc-git-working-revision :override
61 #'diff-hl-flydiff/vc-git-working-revision)
62 (advice-add 'vc-git-mode-line-string :override
63 #'diff-hl-flydiff/vc-git-mode-line-string)))
65 (defun diff-hl-flydiff-make-temp-file-name (file rev &optional manual)
66 "Return a backup file name for REV or the current version of FILE.
67 If MANUAL is non-nil it means that a name for backups created by
68 the user should be returned."
69 (let* ((auto-save-file-name-transforms
70 `((".*" ,temporary-file-directory t))))
72 (concat (make-auto-save-file-name)
73 ".~" (subst-char-in-string
75 (unless manual ".") "~")
76 temporary-file-directory)))
78 (defun diff-hl-flydiff-create-revision (file revision)
79 "Read REVISION of FILE into a buffer and return the buffer."
80 (let ((automatic-backup (diff-hl-flydiff-make-temp-file-name file revision))
81 (filebuf (get-file-buffer file))
82 (filename (diff-hl-flydiff-make-temp-file-name file revision 'manual)))
83 (unless (file-exists-p filename)
84 (if (file-exists-p automatic-backup)
85 (rename-file automatic-backup filename nil)
86 (with-current-buffer filebuf
88 (coding-system-for-read 'no-conversion)
89 (coding-system-for-write 'no-conversion))
91 (with-temp-file filename
92 (let ((outbuf (current-buffer)))
93 ;; Change buffer to get local value of
94 ;; vc-checkout-switches.
95 (with-current-buffer filebuf
96 (vc-call find-revision file revision outbuf))))
98 (when (and failed (file-exists-p filename))
99 (delete-file filename)))))))
102 (defun diff-hl-flydiff-buffer-with-head ()
103 "View the differences between BUFFER and its associated file.
104 This requires the external program `diff' to be in your `exec-path'."
106 (vc-ensure-vc-buffer)
107 (with-current-buffer (get-buffer (current-buffer))
108 (let ((rev (diff-hl-flydiff-create-revision
110 (vc-working-revision buffer-file-name
111 (vc-responsible-backend buffer-file-name))))
112 (temporary-file-directory
113 (if (file-directory-p "/dev/shm/")
115 temporary-file-directory)))
116 (diff-no-select rev (current-buffer) "-U 0" 'noasync
117 (get-buffer-create " *diff-hl-diff*")))))
120 (defun diff-hl-flydiff/update (old-fun &optional auto)
123 (= diff-hl-flydiff-modified-tick (buffer-modified-tick))
124 (file-remote-p default-directory)
125 (not (buffer-modified-p))))
128 (defun diff-hl-flydiff/changes (&rest args)
129 (let* ((file buffer-file-name)
130 (backend (vc-backend file)))
132 (let ((state (vc-state file backend)))
137 (and (eq state 'up-to-date)
138 ;; VC state is stale in after-revert-hook.
139 (or revert-buffer-in-progress-p
140 ;; Diffing against an older revision.
141 diff-hl-reference-revision)))
142 (let (diff-auto-refine-mode res)
143 (with-current-buffer (diff-hl-flydiff-buffer-with-head)
144 (goto-char (point-min))
147 (diff-beginning-of-hunk t))
148 (while (looking-at diff-hunk-header-re-unified)
149 (let ((line (string-to-number (match-string 3)))
150 (len (let ((m (match-string 4)))
151 (if m (string-to-number m) 1)))
154 (let* ((inserts (diff-count-matches "^\\+" beg (point)))
155 (deletes (diff-count-matches "^-" beg (point)))
156 (type (cond ((zerop deletes) 'insert)
157 ((zerop inserts) 'delete)
159 (when (eq type 'delete)
162 (push (list line len type) res))))))
163 (setq diff-hl-flydiff-modified-tick (buffer-modified-tick))
166 `((1 ,(line-number-at-pos (point-max)) insert)))
168 `((1 ,(line-number-at-pos (point-max)) delete))))))))
170 (defun diff-hl-flydiff/overlay-modified (&rest args))
173 (define-minor-mode diff-hl-flydiff-mode
174 "Highlight diffs on-the-fly"
177 (if diff-hl-flydiff-mode
180 (advice-add 'diff-hl-update :around
181 #'diff-hl-flydiff/update)
182 (advice-add 'diff-hl-changes :override
183 #'diff-hl-flydiff/changes)
184 (advice-add 'diff-hl-overlay-modified :override
185 #'diff-hl-flydiff/overlay-modified)
187 (remove-hook 'after-change-functions #'diff-hl-edit t)
188 (setq diff-hl-flydiff-timer
189 (run-with-idle-timer 0.3 t #'diff-hl-update t)))
191 (advice-remove 'diff-hl-update #'diff-hl-flydiff/update)
192 (advice-remove 'diff-hl-changes #'diff-hl-flydiff/changes)
193 (advice-remove 'diff-hl-overlay-modified
194 #'diff-hl-flydiff/overlay-modified)
196 (cancel-timer diff-hl-flydiff-timer)
198 (add-hook 'after-change-functions 'diff-hl-edit nil t))))
200 (provide 'diff-hl-flydiff)