]> code.delx.au - gnu-emacs-elpa/blob - diff-hl.el
Short-circuit when file is up-to-date
[gnu-emacs-elpa] / diff-hl.el
1 ;;; diff-hl.el --- VC diff fringe highlighting -*- lexical-binding: t -*-
2
3 ;; Author: Dmitry Gutov <dgutov@yandex.ru>
4 ;; URL: https://github.com/dgutov/diff-hl
5 ;; Keywords: vc, diff
6 ;; Version: 1.0.1
7
8 ;; This file is not part of GNU Emacs.
9
10 ;; This file is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
14
15 ;; This file is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
22
23 ;;; Commentary:
24
25 ;; `diff-hl-mode' provides IDE-like highlighting of `vc-diff' results
26 ;; on the left fringe of the current buffer.
27 ;;
28 ;; For full description, see README.md or the home page.
29
30 ;;; Code:
31
32 (require 'diff-mode)
33 (require 'vc)
34 (eval-when-compile
35 (require 'cl)
36 (require 'vc-git)
37 (require 'vc-hg)
38 (require 'face-remap))
39
40 (defgroup diff-hl nil
41 "VC diff fringe highlighting"
42 :group 'vc)
43
44 (defface diff-hl-insert
45 '((t :inherit diff-added))
46 "Face used to highlight inserted lines."
47 :group 'diff-hl)
48
49 (defface diff-hl-delete
50 '((t :inherit diff-removed))
51 "Face used to highlight deleted lines."
52 :group 'diff-hl)
53
54 (defface diff-hl-change
55 '((default
56 :foreground "blue")
57 (((class color) (min-colors 88) (background light))
58 :background "#ddddff")
59 (((class color) (min-colors 88) (background dark))
60 :background "#333355"))
61 "Face used to highlight changed lines."
62 :group 'diff-hl)
63
64 (defcustom diff-hl-draw-borders t
65 "Non-nil to draw borders around fringe indicators."
66 :group 'diff-hl
67 :type 'boolean)
68
69 (defun diff-hl-define-bitmaps ()
70 (let* ((scale (if (and (boundp 'text-scale-mode-amount)
71 (plusp text-scale-mode-amount))
72 (expt text-scale-mode-step text-scale-mode-amount)
73 1))
74 (h (round (* (frame-char-height) scale)))
75 (w (frame-parameter nil 'left-fringe))
76 (middle (make-vector h (expt 2 (1- w))))
77 (ones (1- (expt 2 w)))
78 (top (copy-sequence middle))
79 (bottom (copy-sequence middle))
80 (single (copy-sequence middle)))
81 (aset top 0 ones)
82 (aset bottom (1- h) ones)
83 (aset single 0 ones)
84 (aset single (1- h) ones)
85 (define-fringe-bitmap 'diff-hl-bmp-top top h w 'top)
86 (define-fringe-bitmap 'diff-hl-bmp-middle middle h w 'center)
87 (define-fringe-bitmap 'diff-hl-bmp-bottom bottom h w 'bottom)
88 (define-fringe-bitmap 'diff-hl-bmp-single single h w 'top)))
89
90 (when (window-system)
91 (define-fringe-bitmap 'diff-hl-bmp-empty [0] 1 1 'center)
92 (diff-hl-define-bitmaps))
93
94 (defvar diff-hl-spec-cache (make-hash-table :test 'equal))
95
96 (defun diff-hl-fringe-spec (type pos)
97 (let* ((key (cons type pos))
98 (val (gethash key diff-hl-spec-cache)))
99 (unless val
100 (let* ((face-sym (intern (concat "diff-hl-" (symbol-name type))))
101 (bmp-sym (intern (concat "diff-hl-bmp-" (symbol-name pos)))))
102 (setq val (propertize " " 'display `((left-fringe ,bmp-sym ,face-sym))))
103 (puthash key val diff-hl-spec-cache)))
104 val))
105
106 (defmacro diff-hl-with-diff-switches (body)
107 `(let ((vc-git-diff-switches nil)
108 (vc-hg-diff-switches nil)
109 (vc-diff-switches '("-U0"))
110 (vc-disable-async-diff t))
111 ,body))
112
113 (defun diff-hl-changes ()
114 (let* ((buf-name " *diff-hl* ")
115 (file (buffer-file-name))
116 (backend (vc-backend file))
117 res)
118 (when (and backend (not (eq (vc-state (buffer-file-name) backend)
119 'up-to-date)))
120 (diff-hl-with-diff-switches
121 (vc-call-backend backend 'diff (list file) nil nil buf-name))
122 (with-current-buffer buf-name
123 (goto-char (point-min))
124 (unless (eobp)
125 (diff-beginning-of-hunk t)
126 (while (looking-at diff-hunk-header-re-unified)
127 (let ((line (string-to-number (match-string 3)))
128 (len (let ((m (match-string 4)))
129 (if m (string-to-number m) 1)))
130 (beg (point)))
131 (diff-end-of-hunk)
132 (let* ((inserts (diff-count-matches "^\\+" beg (point)))
133 (deletes (diff-count-matches "^-" beg (point)))
134 (type (cond ((zerop deletes) 'insert)
135 ((zerop inserts) 'delete)
136 (t 'change))))
137 (push (list line len type) res)))))))
138 (nreverse res)))
139
140 (defun diff-hl-update ()
141 (let ((changes (diff-hl-changes))
142 (current-line 1))
143 (diff-hl-remove-overlays)
144 (save-excursion
145 (goto-char (point-min))
146 (dolist (c changes)
147 (destructuring-bind (line len type) c
148 (when (eq type 'delete)
149 (setq len 1)
150 (incf line))
151 (forward-line (- line current-line))
152 (setq current-line line)
153 (let ((hunk-beg (point)))
154 (while (plusp len)
155 (let ((o (make-overlay (point) (line-end-position))))
156 (overlay-put o 'diff-hl t)
157 (overlay-put o 'before-string
158 (diff-hl-fringe-spec
159 type
160 (cond
161 ((not diff-hl-draw-borders) 'empty)
162 ((and (= len 1) (= line current-line)) 'single)
163 ((= len 1) 'bottom)
164 ((= line current-line) 'top)
165 (t 'middle)))))
166 (forward-line 1)
167 (incf current-line)
168 (decf len))
169 (let ((h (make-overlay hunk-beg (point)))
170 (hook '(diff-hl-overlay-modified)))
171 (overlay-put h 'diff-hl t)
172 (overlay-put h 'modification-hooks hook)
173 (overlay-put h 'insert-in-front-hooks hook))))))))
174
175 (defun diff-hl-remove-overlays ()
176 (dolist (o (overlays-in (point-min) (point-max)))
177 (when (overlay-get o 'diff-hl) (delete-overlay o))))
178
179 (defun diff-hl-overlay-modified (ov after-p _beg _end &optional _length)
180 ;; Delete the overlay and all our overlays inside it.
181 (unless after-p
182 (save-restriction
183 (narrow-to-region (overlay-start ov) (overlay-end ov))
184 (diff-hl-remove-overlays))
185 (delete-overlay ov)))
186
187 (defvar diff-hl-timer nil)
188
189 (defun diff-hl-edit (_beg _end _len)
190 ;; DTRT when we've `undo'-ed the buffer into unmodified state.
191 (when undo-in-progress
192 (when diff-hl-timer
193 (cancel-timer diff-hl-timer))
194 (setq diff-hl-timer
195 (run-with-idle-timer 0.01 nil #'diff-hl-after-undo (current-buffer)))))
196
197 (defun diff-hl-after-undo (buffer)
198 (with-current-buffer buffer
199 (unless (buffer-modified-p)
200 (diff-hl-update))))
201
202 (defun diff-hl-diff-goto-hunk ()
203 "Run VC diff command and go to the line corresponding to current."
204 (interactive)
205 (vc-buffer-sync)
206 (let* ((line (line-number-at-pos))
207 (buffer (current-buffer)))
208 (vc-diff-internal t (vc-deduce-fileset) nil nil t)
209 (vc-exec-after `(if (eobp)
210 (with-current-buffer ,buffer (diff-hl-remove-overlays))
211 (diff-hl-diff-skip-to ,line)))))
212
213 (defun diff-hl-diff-skip-to (line)
214 "In `diff-mode', skip to the hunk and line corresponding to LINE
215 in the source file, or the last line of the hunk above it."
216 (diff-hunk-next)
217 (let (found)
218 (while (and (looking-at diff-hunk-header-re-unified) (not found))
219 (let ((hunk-line (string-to-number (match-string 3)))
220 (len (let ((m (match-string 4)))
221 (if m (string-to-number m) 1))))
222 (if (> line (+ hunk-line len))
223 (diff-hunk-next)
224 (setq found t)
225 (if (< line hunk-line)
226 ;; Retreat to the previous hunk.
227 (forward-line -1)
228 (let ((to-go (1+ (- line hunk-line))))
229 (while (plusp to-go)
230 (forward-line 1)
231 (unless (looking-at "^-")
232 (decf to-go))))))))))
233
234 (defun diff-hl-revert-hunk ()
235 "Revert the diff hunk with changes at or above the point."
236 (interactive)
237 (vc-buffer-sync)
238 (let ((diff-buffer (generate-new-buffer-name "*diff-hl*"))
239 (buffer (current-buffer))
240 (line (line-number-at-pos))
241 (fileset (vc-deduce-fileset)))
242 (unwind-protect
243 (progn
244 (diff-hl-with-diff-switches
245 (vc-diff-internal nil fileset nil nil nil diff-buffer))
246 (vc-exec-after
247 `(progn
248 (when (eobp)
249 (with-current-buffer ,buffer (diff-hl-remove-overlays))
250 (error "Buffer is up-to-date"))
251 (diff-hl-diff-skip-to ,line)
252 (save-restriction
253 (diff-restrict-view)
254 (unless (yes-or-no-p (format "Revert current hunk in %s?"
255 ,(caadr fileset)))
256 (error "Revert canceled")))
257 (let ((diff-advance-after-apply-hunk nil))
258 (diff-apply-hunk t))
259 (with-current-buffer ,buffer
260 (save-buffer))
261 (message "Hunk reverted"))))
262 (quit-windows-on diff-buffer))))
263
264 ;;;###autoload
265 (define-minor-mode diff-hl-mode
266 "Toggle display of VC diff indicators in the left fringe."
267 :lighter "" :keymap `(([remap vc-diff] . diff-hl-diff-goto-hunk)
268 (,(kbd "C-x v n") . diff-hl-revert-hunk))
269 (if diff-hl-mode
270 (progn
271 (add-hook 'after-save-hook 'diff-hl-update nil t)
272 (add-hook 'after-change-functions 'diff-hl-edit nil t)
273 (if vc-mode
274 (diff-hl-update)
275 (add-hook 'find-file-hook 'diff-hl-update t t))
276 (add-hook 'vc-checkin-hook 'diff-hl-update nil t)
277 (add-hook 'text-scale-mode-hook 'diff-hl-define-bitmaps nil t))
278 (remove-hook 'after-save-hook 'diff-hl-update t)
279 (remove-hook 'after-change-functions 'diff-hl-edit t)
280 (remove-hook 'find-file-hook 'diff-hl-update t)
281 (remove-hook 'vc-checkin-hook 'diff-hl-update t)
282 (remove-hook 'text-scale-mode-hook 'diff-hl-define-bitmaps t)
283 (diff-hl-remove-overlays)))
284
285 (defun turn-on-diff-hl-mode ()
286 ;; FIXME: Why is this called twice for each buffer?
287 ;; Isn't fundamental-mode supposed to not run any hooks?
288 (and buffer-file-name (not (eq major-mode (default-value 'major-mode)))
289 (window-system) ;; No fringes in the console.
290 (diff-hl-mode 1)))
291
292 ;;;###autoload
293 (define-globalized-minor-mode global-diff-hl-mode diff-hl-mode
294 turn-on-diff-hl-mode)
295
296 (provide 'diff-hl)
297
298 ;;; diff-hl.el ends here