]> code.delx.au - gnu-emacs-elpa/blob - diff-hl-flydiff.el
Bump the version
[gnu-emacs-elpa] / diff-hl-flydiff.el
1 ;; Copyright (C) 2015, 2016 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 'diff)
30 (unless (require 'nadvice nil t)
31 (error "`diff-hl-flydiff-mode' requires Emacs 24.4 or newer"))
32
33 (defgroup diff-hl-flydiff nil
34 "Highlight changes on the fly"
35 :group 'diff-hl)
36
37 (defcustom diff-hl-flydiff-delay 0.3
38 "The idle delay in seconds before highlighting is updated."
39 :type 'number)
40
41 (defvar diff-hl-flydiff-modified-tick 0)
42 (defvar diff-hl-flydiff-timer nil)
43 (make-variable-buffer-local 'diff-hl-flydiff-modified-tick)
44
45 (defun diff-hl-flydiff/vc-git--symbolic-ref (file)
46 (or
47 (vc-file-getprop file 'vc-git-symbolic-ref)
48 (let* (process-file-side-effects
49 (str (vc-git--run-command-string nil "symbolic-ref" "HEAD")))
50 (vc-file-setprop file 'vc-git-symbolic-ref
51 (if str
52 (if (string-match "^\\(refs/heads/\\)?\\(.+\\)$" str)
53 (match-string 2 str)
54 str))))))
55
56 (defun diff-hl-flydiff/vc-git-working-revision (_file)
57 "Git-specific version of `vc-working-revision'."
58 (let (process-file-side-effects)
59 (vc-git--rev-parse "HEAD")))
60
61 (defun diff-hl-flydiff/vc-git-mode-line-string (file)
62 "Return a string for `vc-mode-line' to put in the mode line for FILE."
63 (let* ((rev (vc-working-revision file))
64 (disp-rev (or (diff-hl-flydiff/vc-git--symbolic-ref file)
65 (substring rev 0 7)))
66 (def-ml (vc-default-mode-line-string 'Git file))
67 (help-echo (get-text-property 0 'help-echo def-ml))
68 (face (get-text-property 0 'face def-ml)))
69 (propertize (replace-regexp-in-string (concat rev "\\'") disp-rev def-ml t t)
70 'face face
71 'help-echo (concat help-echo "\nCurrent revision: " rev))))
72
73 ;; Polyfill concrete revisions for vc-git-working-revision in Emacs 24.4, 24.5
74 (when (version<= emacs-version "25.0")
75 (with-eval-after-load 'vc-git
76 (advice-add 'vc-git-working-revision :override
77 #'diff-hl-flydiff/vc-git-working-revision)
78 (advice-add 'vc-git-mode-line-string :override
79 #'diff-hl-flydiff/vc-git-mode-line-string)))
80
81 (defun diff-hl-flydiff/working-revision (file)
82 "Like vc-working-revision, but always up-to-date"
83 (vc-file-setprop file 'vc-working-revision
84 (vc-call-backend (vc-backend file) 'working-revision file)))
85
86 (defun diff-hl-flydiff-make-temp-file-name (file rev &optional manual)
87 "Return a backup file name for REV or the current version of FILE.
88 If MANUAL is non-nil it means that a name for backups created by
89 the user should be returned."
90 (let* ((auto-save-file-name-transforms
91 `((".*" ,temporary-file-directory t))))
92 (expand-file-name
93 (concat (make-auto-save-file-name)
94 ".~" (subst-char-in-string
95 ?/ ?_ rev)
96 (unless manual ".") "~")
97 temporary-file-directory)))
98
99 (defun diff-hl-flydiff-create-revision (file revision)
100 "Read REVISION of FILE into a buffer and return the buffer."
101 (let ((automatic-backup (diff-hl-flydiff-make-temp-file-name file revision))
102 (filebuf (get-file-buffer file))
103 (filename (diff-hl-flydiff-make-temp-file-name file revision 'manual)))
104 (unless (file-exists-p filename)
105 (if (file-exists-p automatic-backup)
106 (rename-file automatic-backup filename nil)
107 (with-current-buffer filebuf
108 (let ((coding-system-for-read 'no-conversion)
109 (coding-system-for-write 'no-conversion))
110 (condition-case nil
111 (with-temp-file filename
112 (let ((outbuf (current-buffer)))
113 ;; Change buffer to get local value of
114 ;; vc-checkout-switches.
115 (with-current-buffer filebuf
116 (vc-call find-revision file revision outbuf))))
117 (error
118 (when (file-exists-p filename)
119 (delete-file filename))))))))
120 filename))
121
122 (defun diff-hl-flydiff-buffer-with-head (file &optional backend)
123 "View the differences between BUFFER and its associated file.
124 This requires the external program `diff' to be in your `exec-path'."
125 (interactive)
126 (vc-ensure-vc-buffer)
127 (setq diff-hl-flydiff-modified-tick (buffer-modified-tick))
128 (save-current-buffer
129 (let* ((temporary-file-directory
130 (if (file-directory-p "/dev/shm/")
131 "/dev/shm/"
132 temporary-file-directory))
133 (rev (diff-hl-flydiff-create-revision
134 file
135 (diff-hl-flydiff/working-revision file))))
136 (diff-no-select rev (current-buffer) "-U 0 --strip-trailing-cr" 'noasync
137 (get-buffer-create " *diff-hl-diff*")))))
138
139 (defun diff-hl-flydiff-update ()
140 (unless (or
141 (not diff-hl-mode)
142 (= diff-hl-flydiff-modified-tick (buffer-modified-tick))
143 (file-remote-p default-directory))
144 (diff-hl-update)))
145
146 (defun diff-hl-flydiff/modified-p (state)
147 (buffer-modified-p))
148
149 ;;;###autoload
150 (define-minor-mode diff-hl-flydiff-mode
151 "Highlight diffs on-the-fly"
152 :lighter ""
153 :global t
154 (if diff-hl-flydiff-mode
155 (progn
156 (advice-add 'diff-hl-overlay-modified :override #'ignore)
157
158 (advice-add 'diff-hl-modified-p :before-until
159 #'diff-hl-flydiff/modified-p)
160 (advice-add 'diff-hl-changes-buffer :override
161 #'diff-hl-flydiff-buffer-with-head)
162 (setq diff-hl-flydiff-timer
163 (run-with-idle-timer diff-hl-flydiff-delay t #'diff-hl-flydiff-update)))
164
165 (advice-remove 'diff-hl-overlay-modified #'ignore)
166
167 (advice-remove 'diff-hl-modified-p #'diff-hl-flydiff/modified-p)
168 (advice-remove 'diff-hl-changes-buffer #'diff-hl-flydiff-buffer-with-head)
169
170 (and diff-hl-flydiff-timer
171 (cancel-timer diff-hl-flydiff-timer))))
172
173 (provide 'diff-hl-flydiff)