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