]> code.delx.au - gnu-emacs-elpa/blob - diff-hl-flydiff.el
c0658318750a5eaadf8d1394cf9813de1ab2673b
[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 ;; This mode enables diffing on-the-fly (i.e. without saving the buffer first)
24 ;; Toggle in all buffers with M-x diff-hl-flydiff-mode
25
26 ;;; Code:
27
28 (require 'diff-hl)
29 (require 'nadvice)
30
31 (defvar diff-hl-flydiff-modified-tick 0)
32 (defvar diff-hl-flydiff-timer)
33 (make-variable-buffer-local 'diff-hl-flydiff-modified-tick)
34
35 ;; Polyfill concrete revisions for vc-git-working-revision in Emacs 24.4, 24.5
36 (when (version<= emacs-version "25.0")
37 (with-eval-after-load 'vc-git
38 (defun vc-git--symbolic-ref (file)
39 (or
40 (vc-file-getprop file 'vc-git-symbolic-ref)
41 (let* (process-file-side-effects
42 (str (vc-git--run-command-string nil "symbolic-ref" "HEAD")))
43 (vc-file-setprop file 'vc-git-symbolic-ref
44 (if str
45 (if (string-match "^\\(refs/heads/\\)?\\(.+\\)$" str)
46 (match-string 2 str)
47 str))))))
48
49 (defun diff-hl-flydiff/vc-git-working-revision (_file)
50 "Git-specific version of `vc-working-revision'."
51 (let (process-file-side-effects)
52 (vc-git--rev-parse "HEAD")))
53
54 (defun diff-hl-flydiff/vc-git-mode-line-string (file)
55 "Return a string for `vc-mode-line' to put in the mode line for FILE."
56 (let* ((rev (vc-working-revision file))
57 (disp-rev (or (vc-git--symbolic-ref file)
58 (substring rev 0 7)))
59 (def-ml (vc-default-mode-line-string 'Git file))
60 (help-echo (get-text-property 0 'help-echo def-ml)))
61 (propertize (replace-regexp-in-string (concat rev "\\'") disp-rev def-ml t t)
62 'help-echo (concat help-echo "\nCurrent revision: " rev))))
63
64 (advice-add 'vc-git-working-revision :override
65 #'diff-hl-flydiff/vc-git-working-revision)
66 (advice-add 'vc-git-mode-line-string :override
67 #'diff-hl-flydiff/vc-git-mode-line-string)))
68
69 (defun diff-hl-flydiff-make-temp-file-name (file rev &optional manual)
70 "Return a backup file name for REV or the current version of FILE.
71 If MANUAL is non-nil it means that a name for backups created by
72 the user should be returned."
73 (let* ((auto-save-file-name-transforms
74 `((".*" ,temporary-file-directory t))))
75 (expand-file-name
76 (concat (make-auto-save-file-name)
77 ".~" (subst-char-in-string
78 ?/ ?_ rev)
79 (unless manual ".") "~")
80 temporary-file-directory)))
81
82 (defun diff-hl-flydiff-create-revision (file revision)
83 "Read REVISION of FILE into a buffer and return the buffer."
84 (let ((automatic-backup (diff-hl-flydiff-make-temp-file-name file revision))
85 (filebuf (get-file-buffer file))
86 (filename (diff-hl-flydiff-make-temp-file-name file revision 'manual)))
87 (unless (file-exists-p filename)
88 (if (file-exists-p automatic-backup)
89 (rename-file automatic-backup filename nil)
90 (with-current-buffer filebuf
91 (let ((failed t)
92 (coding-system-for-read 'no-conversion)
93 (coding-system-for-write 'no-conversion))
94 (unwind-protect
95 (with-temp-file filename
96 (let ((outbuf (current-buffer)))
97 ;; Change buffer to get local value of
98 ;; vc-checkout-switches.
99 (with-current-buffer filebuf
100 (vc-call find-revision file revision outbuf))))
101 (setq failed nil)
102 (when (and failed (file-exists-p filename))
103 (delete-file filename)))))))
104 filename))
105
106 (defun diff-hl-flydiff-buffer-with-head ()
107 "View the differences between BUFFER and its associated file.
108 This requires the external program `diff' to be in your `exec-path'."
109 (interactive)
110 (vc-ensure-vc-buffer)
111 (with-current-buffer (get-buffer (current-buffer))
112 (let* ((file buffer-file-name)
113 (temporary-file-directory
114 (if (file-directory-p "/dev/shm/")
115 "/dev/shm/"
116 temporary-file-directory))
117 (rev (diff-hl-flydiff-create-revision
118 file
119 (vc-working-revision file
120 (vc-responsible-backend file)))))
121 (diff-no-select rev (current-buffer) "-U 0" 'noasync
122 (get-buffer-create " *diff-hl-diff*")))))
123
124
125 (defun diff-hl-flydiff/update (old-fun &optional auto)
126 (unless (and auto
127 (or
128 (= diff-hl-flydiff-modified-tick (buffer-modified-tick))
129 (file-remote-p default-directory)
130 (not (buffer-modified-p))))
131 (funcall old-fun)))
132
133 (defun diff-hl-flydiff/changes (&rest args)
134 (let* ((file buffer-file-name)
135 (backend (vc-backend file)))
136 (when backend
137 (let ((state (vc-state file backend)))
138 (cond
139 ((or
140 (buffer-modified-p)
141 (eq state 'edited)
142 (and (eq state 'up-to-date)
143 ;; VC state is stale in after-revert-hook.
144 (or revert-buffer-in-progress-p
145 ;; Diffing against an older revision.
146 diff-hl-reference-revision)))
147 (let (diff-auto-refine-mode res)
148 (with-current-buffer (diff-hl-flydiff-buffer-with-head)
149 (goto-char (point-min))
150 (unless (eobp)
151 (ignore-errors
152 (diff-beginning-of-hunk t))
153 (while (looking-at diff-hunk-header-re-unified)
154 (let ((line (string-to-number (match-string 3)))
155 (len (let ((m (match-string 4)))
156 (if m (string-to-number m) 1)))
157 (beg (point)))
158 (diff-end-of-hunk)
159 (let* ((inserts (diff-count-matches "^\\+" beg (point)))
160 (deletes (diff-count-matches "^-" beg (point)))
161 (type (cond ((zerop deletes) 'insert)
162 ((zerop inserts) 'delete)
163 (t 'change))))
164 (when (eq type 'delete)
165 (setq len 1)
166 (cl-incf line))
167 (push (list line len type) res))))))
168 (setq diff-hl-flydiff-modified-tick (buffer-modified-tick))
169 (nreverse res)))
170 ((eq state 'added)
171 `((1 ,(line-number-at-pos (point-max)) insert)))
172 ((eq state 'removed)
173 `((1 ,(line-number-at-pos (point-max)) delete))))))))
174
175 ;;;###autoload
176 (define-minor-mode diff-hl-flydiff-mode
177 "Highlight diffs on-the-fly"
178 :lighter ""
179 :global t
180 (if diff-hl-flydiff-mode
181 (progn
182 (advice-add 'diff-hl-update :around #'diff-hl-flydiff/update)
183 (advice-add 'diff-hl-changes :override #'diff-hl-flydiff/changes)
184 (advice-add 'diff-hl-overlay-modified :override #'ignored)
185
186 (setq diff-hl-flydiff-timer
187 (run-with-idle-timer 0.3 t #'diff-hl-update t)))
188
189 (advice-remove 'diff-hl-update #'diff-hl-flydiff/update)
190 (advice-remove 'diff-hl-changes #'diff-hl-flydiff/changes)
191 (advice-remove 'diff-hl-overlay-modified #'ignore)
192
193 (cancel-timer diff-hl-flydiff-timer)))
194
195 (provide 'diff-hl-flydiff)