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