]> code.delx.au - gnu-emacs-elpa/blob - diff-hl.el
diff-hl-define-bitmaps: Fix the latest additions
[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.6
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 (defface diff-hl-unknown
86 '((default :inherit diff-header))
87 "Face used to highlight unregistered files.")
88
89 (defcustom diff-hl-draw-borders t
90 "Non-nil to draw borders around fringe indicators."
91 :group 'diff-hl
92 :type 'boolean)
93
94 (defcustom diff-hl-fringe-bmp-function 'diff-hl-fringe-bmp-from-pos
95 "Function to choose the fringe bitmap for a given change type
96 and position within a hunk. Should accept two arguments."
97 :group 'diff-hl
98 :type '(choice (const diff-hl-fringe-bmp-from-pos)
99 (const diff-hl-fringe-bmp-from-type)
100 function))
101
102 (defvar diff-hl-reference-revision nil
103 "Revision to diff against. nil means the most recent one.")
104
105 (defun diff-hl-define-bitmaps ()
106 (let* ((scale (if (and (boundp 'text-scale-mode-amount)
107 (numberp text-scale-mode-amount))
108 (expt text-scale-mode-step text-scale-mode-amount)
109 1))
110 (spacing (or (default-value 'line-spacing) 0))
111 (h (round (+ (* (frame-char-height) scale)
112 (if (floatp spacing)
113 (* (frame-char-height) spacing)
114 spacing))))
115 (w (frame-parameter nil 'left-fringe))
116 (middle (make-vector h (expt 2 (1- w))))
117 (ones (1- (expt 2 w)))
118 (top (copy-sequence middle))
119 (bottom (copy-sequence middle))
120 (single (copy-sequence middle)))
121 (aset top 0 ones)
122 (aset bottom (1- h) ones)
123 (aset single 0 ones)
124 (aset single (1- h) ones)
125 (define-fringe-bitmap 'diff-hl-bmp-top top h w 'top)
126 (define-fringe-bitmap 'diff-hl-bmp-middle middle h w 'center)
127 (define-fringe-bitmap 'diff-hl-bmp-bottom bottom h w 'bottom)
128 (define-fringe-bitmap 'diff-hl-bmp-single single h w 'top)
129 (let* ((w2 (* (/ w 2) 2))
130 (delete-row (- (expt 2 (1- w2)) 2))
131 (middle-pos (1- (/ w2 2)))
132 (middle-bit (expt 2 middle-pos))
133 (insert-bmp (make-vector w2 (* 3 middle-bit))))
134 (define-fringe-bitmap 'diff-hl-bmp-delete (make-vector 2 delete-row))
135 (aset insert-bmp 0 0)
136 (aset insert-bmp middle-pos delete-row)
137 (aset insert-bmp (1+ middle-pos) delete-row)
138 (aset insert-bmp (1- w2) 0)
139 (define-fringe-bitmap 'diff-hl-bmp-insert insert-bmp)
140 (define-fringe-bitmap 'diff-hl-bmp-change (make-vector
141 w2 (* 3 middle-bit))))))
142
143 (defvar diff-hl-spec-cache (make-hash-table :test 'equal))
144
145 (defun diff-hl-fringe-spec (type pos)
146 (let* ((key (list type pos diff-hl-fringe-bmp-function))
147 (val (gethash key diff-hl-spec-cache)))
148 (unless val
149 (let* ((face-sym (intern (format "diff-hl-%s" type)))
150 (bmp-sym (funcall diff-hl-fringe-bmp-function type pos)))
151 (setq val (propertize " " 'display `((left-fringe ,bmp-sym ,face-sym))))
152 (puthash key val diff-hl-spec-cache)))
153 val))
154
155 (defun diff-hl-fringe-bmp-from-pos (type pos)
156 (intern (format "diff-hl-bmp-%s" pos)))
157
158 (defun diff-hl-fringe-bmp-from-type (type pos)
159 (if (eq type 'unknown)
160 'question-mark
161 (intern (format "diff-hl-bmp-%s" type))))
162
163 (defmacro diff-hl-with-diff-switches (body)
164 `(let ((vc-git-diff-switches nil)
165 (vc-hg-diff-switches nil)
166 (vc-svn-diff-switches nil)
167 (vc-diff-switches '("-U0"))
168 (vc-disable-async-diff t))
169 ,body))
170
171 (defun diff-hl-changes ()
172 (let* ((file buffer-file-name)
173 (backend (vc-backend file)))
174 (when backend
175 (let ((state (vc-state file backend)))
176 (cond
177 ((or (eq state 'edited)
178 (and (eq state 'up-to-date)
179 ;; VC state is stale in after-revert-hook.
180 (or revert-buffer-in-progress-p
181 ;; Diffing against an older revision.
182 diff-hl-reference-revision)))
183 (let* ((buf-name " *diff-hl* ")
184 diff-auto-refine-mode
185 res)
186 (diff-hl-with-diff-switches
187 (vc-call-backend backend 'diff (list file)
188 diff-hl-reference-revision nil
189 buf-name))
190 (with-current-buffer buf-name
191 (goto-char (point-min))
192 (unless (eobp)
193 (diff-beginning-of-hunk t)
194 (while (looking-at diff-hunk-header-re-unified)
195 (let ((line (string-to-number (match-string 3)))
196 (len (let ((m (match-string 4)))
197 (if m (string-to-number m) 1)))
198 (beg (point)))
199 (diff-end-of-hunk)
200 (let* ((inserts (diff-count-matches "^\\+" beg (point)))
201 (deletes (diff-count-matches "^-" beg (point)))
202 (type (cond ((zerop deletes) 'insert)
203 ((zerop inserts) 'delete)
204 (t 'change))))
205 (when (eq type 'delete)
206 (setq len 1)
207 (cl-incf line))
208 (push (list line len type) res))))))
209 (nreverse res)))
210 ((eq state 'added)
211 `((1 ,(line-number-at-pos (point-max)) insert)))
212 ((eq state 'removed)
213 `((1 ,(line-number-at-pos (point-max)) delete))))))))
214
215 (defun diff-hl-update ()
216 (let ((changes (diff-hl-changes))
217 (current-line 1))
218 (diff-hl-remove-overlays)
219 (save-excursion
220 (goto-char (point-min))
221 (dolist (c changes)
222 (cl-destructuring-bind (line len type) c
223 (forward-line (- line current-line))
224 (setq current-line line)
225 (let ((hunk-beg (point)))
226 (while (cl-plusp len)
227 (diff-hl-add-highlighting
228 type
229 (cond
230 ((not diff-hl-draw-borders) 'empty)
231 ((and (= len 1) (= line current-line)) 'single)
232 ((= len 1) 'bottom)
233 ((= line current-line) 'top)
234 (t 'middle)))
235 (forward-line 1)
236 (cl-incf current-line)
237 (cl-decf len))
238 (let ((h (make-overlay hunk-beg (point)))
239 (hook '(diff-hl-overlay-modified)))
240 (overlay-put h 'diff-hl t)
241 (overlay-put h 'diff-hl-hunk t)
242 (overlay-put h 'modification-hooks hook)
243 (overlay-put h 'insert-in-front-hooks hook)
244 (overlay-put h 'insert-behind-hooks hook))))))))
245
246 (defun diff-hl-add-highlighting (type shape)
247 (let ((o (make-overlay (point) (point))))
248 (overlay-put o 'diff-hl t)
249 (overlay-put o 'before-string (diff-hl-fringe-spec type shape))
250 o))
251
252 (defun diff-hl-remove-overlays ()
253 (dolist (o (overlays-in (point-min) (point-max)))
254 (when (overlay-get o 'diff-hl) (delete-overlay o))))
255
256 (defun diff-hl-overlay-modified (ov after-p _beg _end &optional _length)
257 "Delete the hunk overlay and all our line overlays inside it."
258 (unless after-p
259 (when (overlay-buffer ov)
260 (save-restriction
261 (narrow-to-region (overlay-start ov) (overlay-end ov))
262 (diff-hl-remove-overlays))
263 (delete-overlay ov))))
264
265 (defvar diff-hl-timer nil)
266
267 (defun diff-hl-edit (_beg _end _len)
268 "DTRT when we've `undo'-ne the buffer into unmodified state."
269 (when undo-in-progress
270 (when diff-hl-timer
271 (cancel-timer diff-hl-timer))
272 (setq diff-hl-timer
273 (run-with-idle-timer 0.01 nil #'diff-hl-after-undo (current-buffer)))))
274
275 (defun diff-hl-after-undo (buffer)
276 (with-current-buffer buffer
277 (unless (buffer-modified-p)
278 (diff-hl-update))))
279
280 (defun diff-hl-diff-goto-hunk ()
281 "Run VC diff command and go to the line corresponding to the current."
282 (interactive)
283 (vc-buffer-sync)
284 (let* ((line (line-number-at-pos))
285 (buffer (current-buffer)))
286 (vc-diff-internal t (vc-deduce-fileset) diff-hl-reference-revision nil t)
287 (vc-exec-after `(if (< (line-number-at-pos (point-max)) 3)
288 (with-current-buffer ,buffer (diff-hl-remove-overlays))
289 (diff-hl-diff-skip-to ,line)
290 (setq vc-sentinel-movepoint (point))))))
291
292 (defun diff-hl-diff-skip-to (line)
293 "In `diff-mode', skip to the hunk and line corresponding to LINE
294 in the source file, or the last line of the hunk above it."
295 (diff-hunk-next)
296 (let (found)
297 (while (and (looking-at diff-hunk-header-re-unified) (not found))
298 (let ((hunk-line (string-to-number (match-string 3)))
299 (len (let ((m (match-string 4)))
300 (if m (string-to-number m) 1))))
301 (if (> line (+ hunk-line len))
302 (diff-hunk-next)
303 (setq found t)
304 (if (< line hunk-line)
305 ;; Retreat to the previous hunk.
306 (forward-line -1)
307 (let ((to-go (1+ (- line hunk-line))))
308 (while (cl-plusp to-go)
309 (forward-line 1)
310 (unless (looking-at "^-")
311 (cl-decf to-go))))))))))
312
313 (defun diff-hl-revert-hunk ()
314 "Revert the diff hunk with changes at or above the point."
315 (interactive)
316 (vc-buffer-sync)
317 (let ((diff-buffer (generate-new-buffer-name "*diff-hl*"))
318 (buffer (current-buffer))
319 (line (save-excursion
320 (unless (diff-hl-hunk-overlay-at (point))
321 (diff-hl-previous-hunk))
322 (line-number-at-pos)))
323 (fileset (vc-deduce-fileset)))
324 (unwind-protect
325 (progn
326 (vc-diff-internal nil fileset diff-hl-reference-revision nil
327 nil diff-buffer)
328 (vc-exec-after
329 `(let (beg-line end-line)
330 (when (eobp)
331 (with-current-buffer ,buffer (diff-hl-remove-overlays))
332 (error "Buffer is up-to-date"))
333 (diff-hl-diff-skip-to ,line)
334 (save-excursion
335 (while (looking-at "[-+]") (forward-line 1))
336 (setq end-line (line-number-at-pos (point)))
337 (unless (eobp) (diff-split-hunk)))
338 (unless (looking-at "[-+]") (forward-line -1))
339 (while (looking-at "[-+]") (forward-line -1))
340 (setq beg-line (line-number-at-pos (point)))
341 (unless (looking-at "@")
342 (forward-line 1)
343 (diff-split-hunk))
344 (let ((wbh (window-body-height)))
345 (if (>= wbh (- end-line beg-line))
346 (recenter (/ (+ wbh (- beg-line end-line) 2) 2))
347 (recenter 1)))
348 (unless (yes-or-no-p (format "Revert current hunk in %s?"
349 ,(cl-caadr fileset)))
350 (error "Revert canceled"))
351 (let ((diff-advance-after-apply-hunk nil))
352 (diff-apply-hunk t))
353 (with-current-buffer ,buffer
354 (save-buffer))
355 (message "Hunk reverted"))))
356 (quit-windows-on diff-buffer))))
357
358 (defun diff-hl-hunk-overlay-at (pos)
359 (cl-loop for o in (overlays-in pos (1+ pos))
360 when (overlay-get o 'diff-hl-hunk)
361 return o))
362
363 (defun diff-hl-next-hunk (&optional backward)
364 "Go to the beginning of the next hunk in the current buffer."
365 (interactive)
366 (let ((pos (save-excursion
367 (catch 'found
368 (while (not (if backward (bobp) (eobp)))
369 (goto-char (if backward
370 (previous-overlay-change (point))
371 (next-overlay-change (point))))
372 (let ((o (diff-hl-hunk-overlay-at (point))))
373 (when (and o (= (overlay-start o) (point)))
374 (throw 'found (overlay-start o)))))))))
375 (if pos
376 (goto-char pos)
377 (error "No further hunks found"))))
378
379 (defun diff-hl-previous-hunk ()
380 "Go to the beginning of the previous hunk in the current buffer."
381 (interactive)
382 (diff-hl-next-hunk t))
383
384 ;;;###autoload
385 (define-minor-mode diff-hl-mode
386 "Toggle VC diff fringe highlighting."
387 :lighter "" :keymap `(([remap vc-diff] . diff-hl-diff-goto-hunk)
388 (,(kbd "C-x v n") . diff-hl-revert-hunk)
389 (,(kbd "C-x v [") . diff-hl-previous-hunk)
390 (,(kbd "C-x v ]") . diff-hl-next-hunk))
391 (if diff-hl-mode
392 (progn
393 (when (window-system) ;; No fringes in the console.
394 (unless (fringe-bitmap-p 'diff-hl-bmp-empty)
395 (diff-hl-define-bitmaps)
396 (define-fringe-bitmap 'diff-hl-bmp-empty [0] 1 1 'center)))
397 (add-hook 'after-save-hook 'diff-hl-update nil t)
398 (add-hook 'after-change-functions 'diff-hl-edit nil t)
399 (if vc-mode
400 (diff-hl-update)
401 (add-hook 'find-file-hook 'diff-hl-update t t))
402 (add-hook 'vc-checkin-hook 'diff-hl-update nil t)
403 (add-hook 'after-revert-hook 'diff-hl-update nil t)
404 (add-hook 'text-scale-mode-hook 'diff-hl-define-bitmaps nil t))
405 (remove-hook 'after-save-hook 'diff-hl-update t)
406 (remove-hook 'after-change-functions 'diff-hl-edit t)
407 (remove-hook 'find-file-hook 'diff-hl-update t)
408 (remove-hook 'vc-checkin-hook 'diff-hl-update t)
409 (remove-hook 'after-revert-hook 'diff-hl-update t)
410 (remove-hook 'text-scale-mode-hook 'diff-hl-define-bitmaps t)
411 (diff-hl-remove-overlays)))
412
413 (when (require 'smartrep nil t)
414 (let (smart-keys)
415 (cl-labels ((scan (map)
416 (map-keymap
417 (lambda (event binding)
418 (if (consp binding)
419 (scan binding)
420 (when (characterp event)
421 (push (cons (string event) binding) smart-keys))))
422 map)))
423 (scan diff-hl-mode-map)
424 (smartrep-define-key diff-hl-mode-map "C-x v" smart-keys))))
425
426 (defun diff-hl-dir-update ()
427 (dolist (pair (if (vc-dir-marked-files)
428 (vc-dir-marked-only-files-and-states)
429 (vc-dir-child-files-and-states)))
430 (when (eq 'up-to-date (cdr pair))
431 (let ((buffer (find-buffer-visiting (car pair))))
432 (when buffer
433 (with-current-buffer buffer
434 (diff-hl-remove-overlays)))))))
435
436 (define-minor-mode diff-hl-dir-mode
437 "Toggle `diff-hl-mode' integration in a `vc-dir-mode' buffer."
438 :lighter ""
439 (if diff-hl-dir-mode
440 (add-hook 'vc-checkin-hook 'diff-hl-dir-update t t)
441 (remove-hook 'vc-checkin-hook 'diff-hl-dir-update t)))
442
443 ;;;###autoload
444 (defun turn-on-diff-hl-mode ()
445 "Turn on `diff-hl-mode' or `diff-hl-dir-mode' in a buffer if appropriate."
446 (cond
447 (buffer-file-name
448 (diff-hl-mode 1))
449 ((eq major-mode 'vc-dir-mode)
450 (diff-hl-dir-mode 1))))
451
452 ;;;###autoload
453 (define-globalized-minor-mode global-diff-hl-mode diff-hl-mode
454 turn-on-diff-hl-mode :after-hook (diff-hl-global-mode-change))
455
456 (defun diff-hl-global-mode-change ()
457 (unless global-diff-hl-mode
458 (dolist (buf (buffer-list))
459 (with-current-buffer buf
460 (when diff-hl-dir-mode
461 (diff-hl-dir-mode -1))))))
462
463 (provide 'diff-hl)
464
465 ;;; diff-hl.el ends here