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