]> code.delx.au - gnu-emacs-elpa/blob - diff-hl.el
Move fringe bitmap initialization into minor mode def function.
[gnu-emacs-elpa] / diff-hl.el
1 ;;; diff-hl.el --- Highlight uncommitted changes -*- 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.4.2
7 ;; Package-Requires: ((cl-lib "0.2"))
8
9 ;; This file is not part of GNU Emacs.
10
11 ;; This file is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
15
16 ;; This file is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23
24 ;;; Commentary:
25
26 ;; `diff-hl-mode' highlights uncommitted changes on the left fringe of the
27 ;; window, allows you to jump between the hunks and revert them selectively.
28
29 ;; Provided commands:
30 ;;
31 ;; `diff-hl-diff-goto-hunk' C-x v =
32 ;; `diff-hl-revert-hunk' C-x v n
33 ;; `diff-hl-previous-hunk' C-x v [
34 ;; `diff-hl-next-hunk' C-x v ]
35 ;;
36 ;; The mode takes advantage of `smartrep' if it is installed.
37
38 ;; Add either of the following to your init file.
39 ;;
40 ;; To use it in all buffers:
41 ;;
42 ;; (global-diff-hl-mode)
43 ;;
44 ;; Only in `prog-mode' buffers, with `vc-dir' integration:
45 ;;
46 ;; (add-hook 'prog-mode-hook 'turn-on-diff-hl-mode)
47 ;; (add-hook 'vc-dir-mode-hook 'turn-on-diff-hl-mode)
48
49 ;;; Code:
50
51 (require 'diff-mode)
52 (require 'vc)
53 (require 'vc-dir)
54 (eval-when-compile
55 (require 'cl-lib)
56 (require 'vc-git)
57 (require 'vc-hg)
58 (require 'face-remap))
59
60 (defgroup diff-hl nil
61 "VC diff fringe highlighting"
62 :group 'vc)
63
64 (defface diff-hl-insert
65 '((default :inherit diff-added)
66 (((class color)) :foreground "green4"))
67 "Face used to highlight inserted lines."
68 :group 'diff-hl)
69
70 (defface diff-hl-delete
71 '((default :inherit diff-removed)
72 (((class color)) :foreground "red3"))
73 "Face used to highlight deleted lines."
74 :group 'diff-hl)
75
76 (defface diff-hl-change
77 '((default :foreground "blue3")
78 (((class color) (min-colors 88) (background light))
79 :background "#ddddff")
80 (((class color) (min-colors 88) (background dark))
81 :background "#333355"))
82 "Face used to highlight changed lines."
83 :group 'diff-hl)
84
85 (defcustom diff-hl-draw-borders t
86 "Non-nil to draw borders around fringe indicators."
87 :group 'diff-hl
88 :type 'boolean)
89
90 (defun diff-hl-define-bitmaps ()
91 (let* ((scale (if (and (boundp 'text-scale-mode-amount)
92 (cl-plusp text-scale-mode-amount))
93 (expt text-scale-mode-step text-scale-mode-amount)
94 1))
95 (h (round (* (frame-char-height) scale)))
96 (w (frame-parameter nil 'left-fringe))
97 (middle (make-vector h (expt 2 (1- w))))
98 (ones (1- (expt 2 w)))
99 (top (copy-sequence middle))
100 (bottom (copy-sequence middle))
101 (single (copy-sequence middle)))
102 (aset top 0 ones)
103 (aset bottom (1- h) ones)
104 (aset single 0 ones)
105 (aset single (1- h) ones)
106 (define-fringe-bitmap 'diff-hl-bmp-top top h w 'top)
107 (define-fringe-bitmap 'diff-hl-bmp-middle middle h w 'center)
108 (define-fringe-bitmap 'diff-hl-bmp-bottom bottom h w 'bottom)
109 (define-fringe-bitmap 'diff-hl-bmp-single single h w 'top)))
110
111 (defvar diff-hl-spec-cache (make-hash-table :test 'equal))
112
113 (defun diff-hl-fringe-spec (type pos)
114 (let* ((key (cons type pos))
115 (val (gethash key diff-hl-spec-cache)))
116 (unless val
117 (let* ((face-sym (intern (concat "diff-hl-" (symbol-name type))))
118 (bmp-sym (intern (concat "diff-hl-bmp-" (symbol-name pos)))))
119 (setq val (propertize " " 'display `((left-fringe ,bmp-sym ,face-sym))))
120 (puthash key val diff-hl-spec-cache)))
121 val))
122
123 (defmacro diff-hl-with-diff-switches (body)
124 `(let ((vc-git-diff-switches nil)
125 (vc-hg-diff-switches nil)
126 (vc-diff-switches '("-U0"))
127 (vc-disable-async-diff t))
128 ,body))
129
130 (defun diff-hl-changes ()
131 (let* ((file buffer-file-name)
132 (backend (vc-backend file)))
133 (when backend
134 (let ((state (vc-state file backend)))
135 (cond
136 ((or (eq state 'edited)
137 (and (eq state 'up-to-date)
138 ;; VC state is stale in after-revert-hook.
139 revert-buffer-in-progress-p))
140 (let* ((buf-name " *diff-hl* ")
141 res)
142 (diff-hl-with-diff-switches
143 (vc-call-backend backend 'diff (list file) nil nil buf-name))
144 (with-current-buffer buf-name
145 (goto-char (point-min))
146 (unless (eobp)
147 (diff-beginning-of-hunk t)
148 (while (looking-at diff-hunk-header-re-unified)
149 (let ((line (string-to-number (match-string 3)))
150 (len (let ((m (match-string 4)))
151 (if m (string-to-number m) 1)))
152 (beg (point)))
153 (diff-end-of-hunk)
154 (let* ((inserts (diff-count-matches "^\\+" beg (point)))
155 (deletes (diff-count-matches "^-" beg (point)))
156 (type (cond ((zerop deletes) 'insert)
157 ((zerop inserts) 'delete)
158 (t 'change))))
159 (when (eq type 'delete)
160 (setq len 1)
161 (cl-incf line))
162 (push (list line len type) res))))))
163 (nreverse res)))
164 ((eq state 'added)
165 `((1 ,(line-number-at-pos (point-max)) insert)))
166 ((eq state 'removed)
167 `((1 ,(line-number-at-pos (point-max)) delete))))))))
168
169 (defun diff-hl-update ()
170 (let ((changes (diff-hl-changes))
171 (current-line 1))
172 (diff-hl-remove-overlays)
173 (save-excursion
174 (goto-char (point-min))
175 (dolist (c changes)
176 (cl-destructuring-bind (line len type) c
177 (forward-line (- line current-line))
178 (let ((hunk-beg (point)))
179 (forward-line len)
180 (setq current-line (+ line len))
181 (let ((h (make-overlay hunk-beg (1- (point))))
182 (hook '(diff-hl-overlay-modified)))
183 (overlay-put h 'diff-hl t)
184 (if (= len 1)
185 (overlay-put h 'before-string (diff-hl-fringe-spec type 'single))
186 (overlay-put h 'before-string (diff-hl-fringe-spec type 'top))
187 (overlay-put h 'line-prefix (diff-hl-fringe-spec type 'middle))
188 (overlay-put h 'after-string (diff-hl-fringe-spec type 'bottom)))
189 (overlay-put h 'modification-hooks hook)
190 (overlay-put h 'insert-in-front-hooks hook)
191 (overlay-put h 'insert-behind-hooks hook))))))))
192
193 (defun diff-hl-remove-overlays ()
194 (dolist (o (overlays-in (point-min) (point-max)))
195 (when (overlay-get o 'diff-hl) (delete-overlay o))))
196
197 (defun diff-hl-overlay-modified (ov after-p _beg _end &optional _length)
198 "Delete the overlay."
199 (unless after-p
200 (when (overlay-buffer ov)
201 (delete-overlay ov))))
202
203 (defvar diff-hl-timer nil)
204
205 (defun diff-hl-edit (_beg _end _len)
206 "DTRT when we've `undo'-ne the buffer into unmodified state."
207 (when undo-in-progress
208 (when diff-hl-timer
209 (cancel-timer diff-hl-timer))
210 (setq diff-hl-timer
211 (run-with-idle-timer 0.01 nil #'diff-hl-after-undo (current-buffer)))))
212
213 (defun diff-hl-after-undo (buffer)
214 (with-current-buffer buffer
215 (unless (buffer-modified-p)
216 (diff-hl-update))))
217
218 (defun diff-hl-diff-goto-hunk ()
219 "Run VC diff command and go to the line corresponding to the current."
220 (interactive)
221 (vc-buffer-sync)
222 (let* ((line (line-number-at-pos))
223 (buffer (current-buffer)))
224 (vc-diff-internal t (vc-deduce-fileset) nil nil t)
225 (vc-exec-after `(if (< (line-number-at-pos (point-max)) 3)
226 (with-current-buffer ,buffer (diff-hl-remove-overlays))
227 (diff-hl-diff-skip-to ,line)
228 (setq vc-sentinel-movepoint (point))))))
229
230 (defun diff-hl-diff-skip-to (line)
231 "In `diff-mode', skip to the hunk and line corresponding to LINE
232 in the source file, or the last line of the hunk above it."
233 (diff-hunk-next)
234 (let (found)
235 (while (and (looking-at diff-hunk-header-re-unified) (not found))
236 (let ((hunk-line (string-to-number (match-string 3)))
237 (len (let ((m (match-string 4)))
238 (if m (string-to-number m) 1))))
239 (if (> line (+ hunk-line len))
240 (diff-hunk-next)
241 (setq found t)
242 (if (< line hunk-line)
243 ;; Retreat to the previous hunk.
244 (forward-line -1)
245 (let ((to-go (1+ (- line hunk-line))))
246 (while (cl-plusp to-go)
247 (forward-line 1)
248 (unless (looking-at "^-")
249 (cl-decf to-go))))))))))
250
251 (defun diff-hl-revert-hunk ()
252 "Revert the diff hunk with changes at or above the point."
253 (interactive)
254 (vc-buffer-sync)
255 (let ((diff-buffer (generate-new-buffer-name "*diff-hl*"))
256 (buffer (current-buffer))
257 (line (save-excursion
258 (unless (diff-hl-hunk-overlay-at (point))
259 (diff-hl-previous-hunk))
260 (line-number-at-pos)))
261 (fileset (vc-deduce-fileset)))
262 (unwind-protect
263 (progn
264 (vc-diff-internal nil fileset nil nil nil diff-buffer)
265 (vc-exec-after
266 `(let (beg-line end-line)
267 (when (eobp)
268 (with-current-buffer ,buffer (diff-hl-remove-overlays))
269 (error "Buffer is up-to-date"))
270 (diff-hl-diff-skip-to ,line)
271 (save-excursion
272 (while (looking-at "[-+]") (forward-line 1))
273 (setq end-line (line-number-at-pos (point)))
274 (unless (eobp) (diff-split-hunk)))
275 (unless (looking-at "[-+]") (forward-line -1))
276 (while (looking-at "[-+]") (forward-line -1))
277 (setq beg-line (line-number-at-pos (point)))
278 (unless (looking-at "@")
279 (forward-line 1)
280 (diff-split-hunk))
281 (let ((wbh (window-body-height)))
282 (if (>= wbh (- end-line beg-line))
283 (recenter (/ (+ wbh (- beg-line end-line) 2) 2))
284 (recenter 1)))
285 (unless (yes-or-no-p (format "Revert current hunk in %s?"
286 ,(cl-caadr fileset)))
287 (error "Revert canceled"))
288 (let ((diff-advance-after-apply-hunk nil))
289 (diff-apply-hunk t))
290 (with-current-buffer ,buffer
291 (save-buffer))
292 (message "Hunk reverted"))))
293 (quit-windows-on diff-buffer))))
294
295 (defun diff-hl-hunk-overlay-at (pos)
296 (cl-loop for o in (overlays-in pos (1+ pos))
297 when (overlay-get o 'diff-hl)
298 return o))
299
300 (defun diff-hl-next-hunk (&optional backward)
301 "Go to the beginning of the next hunk in the current buffer."
302 (interactive)
303 (let ((pos (save-excursion
304 (catch 'found
305 (while (not (if backward (bobp) (eobp)))
306 (goto-char (if backward
307 (previous-overlay-change (point))
308 (next-overlay-change (point))))
309 (let ((o (diff-hl-hunk-overlay-at (point))))
310 (when (and o (if backward
311 (>= (overlay-end o) (point))
312 (<= (overlay-start o) (point))))
313 (throw 'found (overlay-start o)))))))))
314 (if pos
315 (goto-char pos)
316 (error "No further hunks found"))))
317
318 (defun diff-hl-previous-hunk ()
319 "Go to the beginning of the previous hunk in the current buffer."
320 (interactive)
321 (diff-hl-next-hunk t))
322
323 (define-minor-mode diff-hl-mode
324 "Toggle VC diff fringe highlighting."
325 :lighter "" :keymap `(([remap vc-diff] . diff-hl-diff-goto-hunk)
326 (,(kbd "C-x v n") . diff-hl-revert-hunk)
327 (,(kbd "C-x v [") . diff-hl-previous-hunk)
328 (,(kbd "C-x v ]") . diff-hl-next-hunk))
329 (if diff-hl-mode
330 (progn
331 (when (window-system) ;; No fringes in the console.
332 (unless (fringe-bitmap-p 'diff-hl-bmp-empty)
333 (diff-hl-define-bitmaps)
334 (define-fringe-bitmap 'diff-hl-bmp-empty [0] 1 1 'center)))
335 (add-hook 'after-save-hook 'diff-hl-update nil t)
336 (add-hook 'after-change-functions 'diff-hl-edit nil t)
337 (if vc-mode
338 (diff-hl-update)
339 (add-hook 'find-file-hook 'diff-hl-update t t))
340 (add-hook 'vc-checkin-hook 'diff-hl-update nil t)
341 (add-hook 'after-revert-hook 'diff-hl-update nil t)
342 (add-hook 'text-scale-mode-hook 'diff-hl-define-bitmaps nil t))
343 (remove-hook 'after-save-hook 'diff-hl-update t)
344 (remove-hook 'after-change-functions 'diff-hl-edit t)
345 (remove-hook 'find-file-hook 'diff-hl-update t)
346 (remove-hook 'vc-checkin-hook 'diff-hl-update t)
347 (remove-hook 'after-revert-hook 'diff-hl-update t)
348 (remove-hook 'text-scale-mode-hook 'diff-hl-define-bitmaps t)
349 (diff-hl-remove-overlays)))
350
351 (when (require 'smartrep nil t)
352 (let (smart-keys)
353 (cl-labels ((scan (map)
354 (map-keymap
355 (lambda (event binding)
356 (if (consp binding)
357 (scan binding)
358 (when (characterp event)
359 (push (cons (string event) binding) smart-keys))))
360 map)))
361 (scan diff-hl-mode-map)
362 (smartrep-define-key diff-hl-mode-map "C-x v" smart-keys))))
363
364 (defun diff-hl-dir-update ()
365 (dolist (pair (if (vc-dir-marked-files)
366 (vc-dir-marked-only-files-and-states)
367 (vc-dir-child-files-and-states)))
368 (when (eq 'up-to-date (cdr pair))
369 (let ((buffer (find-buffer-visiting (car pair))))
370 (when buffer
371 (with-current-buffer buffer
372 (diff-hl-remove-overlays)))))))
373
374 (define-minor-mode diff-hl-dir-mode
375 "Toggle `diff-hl-mode' integration in a `vc-dir-mode' buffer."
376 :lighter ""
377 (if diff-hl-dir-mode
378 (add-hook 'vc-checkin-hook 'diff-hl-dir-update t t)
379 (remove-hook 'vc-checkin-hook 'diff-hl-dir-update t)))
380
381 ;;;###autoload
382 (defun turn-on-diff-hl-mode ()
383 "Turn on `diff-hl-mode' or `diff-hl-dir-mode' in a buffer if appropriate."
384 (cond
385 (buffer-file-name
386 (diff-hl-mode 1))
387 ((eq major-mode 'vc-dir-mode)
388 (diff-hl-dir-mode 1))))
389
390 ;;;###autoload
391 (define-globalized-minor-mode global-diff-hl-mode diff-hl-mode
392 turn-on-diff-hl-mode :after-hook (diff-hl-global-mode-change))
393
394 (defun diff-hl-global-mode-change ()
395 (unless global-diff-hl-mode
396 (dolist (buf (buffer-list))
397 (with-current-buffer buf
398 (when diff-hl-dir-mode
399 (diff-hl-dir-mode -1))))))
400
401 (provide 'diff-hl)
402
403 ;;; diff-hl.el ends here