]> code.delx.au - gnu-emacs-elpa/blob - diff-hl-flydiff.el
Add diff-hl-flydiff as a separate file
[gnu-emacs-elpa] / diff-hl-flydiff.el
1 ;; Copyright (C) 2012-2013 Free Software Foundation, Inc.
2
3 ;; Author: Jonathan Hayase <PythonNut@gmail.com>
4 ;; URL: https://github.com/dgutov/diff-hl
5
6 ;; This file is part of GNU Emacs.
7
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.
12
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.
17
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/>.
20
21 ;;; Commentary:
22
23 ;;; Code:
24
25 (require 'diff-hl)
26
27 (defvar diff-hl-flydiff-modified-tick 0)
28 (defvar diff-hl-flydiff-timer)
29 (make-variable-buffer-local 'diff-hl-flydiff-modified-tick)
30
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)
35 (or
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
40 (if str
41 (if (string-match "^\\(refs/heads/\\)?\\(.+\\)$" str)
42 (match-string 2 str)
43 str))))))
44
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")))
49
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)
54 (substring rev 0 7)))
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))))
59
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)))
64
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))))
71 (expand-file-name
72 (concat (make-auto-save-file-name)
73 ".~" (subst-char-in-string
74 ?/ ?_ rev)
75 (unless manual ".") "~")
76 temporary-file-directory)))
77
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
87 (let ((failed t)
88 (coding-system-for-read 'no-conversion)
89 (coding-system-for-write 'no-conversion))
90 (unwind-protect
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))))
97 (setq failed nil)
98 (when (and failed (file-exists-p filename))
99 (delete-file filename)))))))
100 filename))
101
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'."
105 (interactive)
106 (vc-ensure-vc-buffer)
107 (with-current-buffer (get-buffer (current-buffer))
108 (let ((rev (diff-hl-flydiff-create-revision
109 buffer-file-name
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/")
114 "/dev/shm/"
115 temporary-file-directory)))
116 (diff-no-select rev (current-buffer) "-U 0" 'noasync
117 (get-buffer-create " *diff-hl-diff*")))))
118
119
120 (defun diff-hl-flydiff/update (old-fun &optional auto)
121 (unless (and auto
122 (or
123 (= diff-hl-flydiff-modified-tick (buffer-modified-tick))
124 (file-remote-p default-directory)
125 (not (buffer-modified-p))))
126 (funcall old-fun)))
127
128 (defun diff-hl-flydiff/changes (&rest args)
129 (let* ((file buffer-file-name)
130 (backend (vc-backend file)))
131 (when backend
132 (let ((state (vc-state file backend)))
133 (cond
134 ((or
135 (buffer-modified-p)
136 (eq state 'edited)
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))
145 (unless (eobp)
146 (ignore-errors
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)))
152 (beg (point)))
153 (diff-end-of-hunk)
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)
158 (t 'change))))
159 (when (eq type 'delete)
160 (setq len 1)
161 (cl-incf line))
162 (push (list line len type) res))))))
163 (setq diff-hl-flydiff-modified-tick (buffer-modified-tick))
164 (nreverse res)))
165 ((eq state 'added)
166 `((1 ,(line-number-at-pos (point-max)) insert)))
167 ((eq state 'removed)
168 `((1 ,(line-number-at-pos (point-max)) delete))))))))
169
170 (defun diff-hl-flydiff/overlay-modified (&rest args))
171
172 ;;;###autoload
173 (define-minor-mode diff-hl-flydiff-mode
174 "Highlight diffs on-the-fly"
175 :lighter ""
176 :global t
177 (if diff-hl-flydiff-mode
178 (progn
179 (require 'nadvice)
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)
186
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)))
190
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)
195
196 (cancel-timer diff-hl-flydiff-timer)
197 (when diff-hl-mode
198 (add-hook 'after-change-functions 'diff-hl-edit nil t))))
199
200 (provide 'diff-hl-flydiff)