]> code.delx.au - gnu-emacs-elpa/blob - diff-hl-flydiff.el
Update docs to reflect new diff-hl-flydiff-mode
[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 '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/working-revision (file)
70 "Like vc-working-revision, but always up-to-date"
71 (vc-file-setprop file 'vc-working-revision
72 (vc-call-backend (vc-backend file) 'working-revision file)))
73
74 (defun diff-hl-flydiff-make-temp-file-name (file rev &optional manual)
75 "Return a backup file name for REV or the current version of FILE.
76 If MANUAL is non-nil it means that a name for backups created by
77 the user should be returned."
78 (let* ((auto-save-file-name-transforms
79 `((".*" ,temporary-file-directory t))))
80 (expand-file-name
81 (concat (make-auto-save-file-name)
82 ".~" (subst-char-in-string
83 ?/ ?_ rev)
84 (unless manual ".") "~")
85 temporary-file-directory)))
86
87 (defun diff-hl-flydiff-create-revision (file revision)
88 "Read REVISION of FILE into a buffer and return the buffer."
89 (let ((automatic-backup (diff-hl-flydiff-make-temp-file-name file revision))
90 (filebuf (get-file-buffer file))
91 (filename (diff-hl-flydiff-make-temp-file-name file revision 'manual)))
92 (unless (file-exists-p filename)
93 (if (file-exists-p automatic-backup)
94 (rename-file automatic-backup filename nil)
95 (with-current-buffer filebuf
96 (let ((failed t)
97 (coding-system-for-read 'no-conversion)
98 (coding-system-for-write 'no-conversion))
99 (unwind-protect
100 (with-temp-file filename
101 (let ((outbuf (current-buffer)))
102 ;; Change buffer to get local value of
103 ;; vc-checkout-switches.
104 (with-current-buffer filebuf
105 (vc-call find-revision file revision outbuf))))
106 (setq failed nil)
107 (when (and failed (file-exists-p filename))
108 (delete-file filename)))))))
109 filename))
110
111 (defun diff-hl-flydiff-buffer-with-head (file &optional backend)
112 "View the differences between BUFFER and its associated file.
113 This requires the external program `diff' to be in your `exec-path'."
114 (interactive)
115 (vc-ensure-vc-buffer)
116 (with-current-buffer (get-buffer (current-buffer))
117 (let* ((temporary-file-directory
118 (if (file-directory-p "/dev/shm/")
119 "/dev/shm/"
120 temporary-file-directory))
121 (rev (diff-hl-flydiff-create-revision file
122 (diff-hl-flydiff/working-revision file))))
123 (diff-no-select rev (current-buffer) "-U 0" 'noasync
124 (get-buffer-create " *diff-hl-diff*")))))
125
126 (defun diff-hl-flydiff/update (old-fun &optional auto)
127 (unless (and auto
128 (or
129 (= diff-hl-flydiff-modified-tick (buffer-modified-tick))
130 (file-remote-p default-directory)
131 (not (buffer-modified-p))))
132 (funcall old-fun)))
133
134 (defun diff-hl-flydiff/modified-p (state)
135 (buffer-modified-p))
136
137 (defun diff-hl-flydiff/update-modified-tick (&rest args)
138 (setq diff-hl-flydiff-modified-tick (buffer-modified-tick)))
139
140 ;;;###autoload
141 (define-minor-mode diff-hl-flydiff-mode
142 "Highlight diffs on-the-fly"
143 :lighter ""
144 :global t
145 (if diff-hl-flydiff-mode
146 (progn
147 (advice-add 'diff-hl-update :around #'diff-hl-flydiff/update)
148 (advice-add 'diff-hl-overlay-modified :override #'ignore)
149
150 (advice-add 'diff-hl-modified-p :before-until
151 #'diff-hl-flydiff/modified-p)
152 (advice-add 'diff-hl-changes-buffer :override
153 #'diff-hl-flydiff-buffer-with-head)
154 (advice-add 'diff-hl-change :after
155 #'diff-hl-flydiff/update-modified-tick)
156
157 (setq diff-hl-flydiff-timer
158 (run-with-idle-timer 0.3 t #'diff-hl-update t)))
159
160 (advice-remove 'diff-hl-update #'diff-hl-flydiff/update)
161 (advice-remove 'diff-hl-overlay-modified #'ignore)
162
163 (advice-remove 'diff-hl-modified-p #'diff-hl-flydiff/modified-p)
164 (advice-remove 'diff-hl-changes-buffer #'diff-hl-flydiff-buffer-with-head)
165 (advice-remove 'diff-hl-change #'diff-hl-flydiff/update-modified-tick)
166
167 (cancel-timer diff-hl-flydiff-timer)))
168
169 (provide 'diff-hl-flydiff)