]> code.delx.au - gnu-emacs-elpa/blob - diff-hl.el
silence byte-compiler
[gnu-emacs-elpa] / diff-hl.el
1 ;;; diff-hl.el --- Highlight uncommitted changes -*- lexical-binding: t -*-
2
3 ;; Copyright (C) 2012-2014 Free Software Foundation, Inc.
4
5 ;; Author: Dmitry Gutov <dgutov@yandex.ru>
6 ;; URL: https://github.com/dgutov/diff-hl
7 ;; Keywords: vc, diff
8 ;; Version: 1.7.0
9 ;; Package-Requires: ((cl-lib "0.2"))
10
11 ;; This file is part of GNU Emacs.
12
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
17
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
22
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25
26 ;;; Commentary:
27
28 ;; `diff-hl-mode' highlights uncommitted changes on the left side of
29 ;; the window (using the fringe, by default), allows you to jump
30 ;; between the hunks and revert them selectively.
31
32 ;; Provided commands:
33 ;;
34 ;; `diff-hl-diff-goto-hunk' C-x v =
35 ;; `diff-hl-revert-hunk' C-x v n
36 ;; `diff-hl-previous-hunk' C-x v [
37 ;; `diff-hl-next-hunk' C-x v ]
38 ;;
39 ;; The mode takes advantage of `smartrep' if it is installed.
40
41 ;; Add either of the following to your init file.
42 ;;
43 ;; To use it in all buffers:
44 ;;
45 ;; (global-diff-hl-mode)
46 ;;
47 ;; Only in `prog-mode' buffers, with `vc-dir' integration:
48 ;;
49 ;; (add-hook 'prog-mode-hook 'turn-on-diff-hl-mode)
50 ;; (add-hook 'vc-dir-mode-hook 'turn-on-diff-hl-mode)
51
52 ;;; Code:
53
54 (require 'fringe)
55 (require 'diff-mode)
56 (require 'vc)
57 (require 'vc-dir)
58 (eval-when-compile
59 (require 'cl-lib)
60 (require 'vc-git)
61 (require 'vc-hg)
62 (require 'face-remap)
63 (declare-function smartrep-define-key 'smartrep))
64
65 (defgroup diff-hl nil
66 "VC diff highlighting on the side of a window"
67 :group 'vc)
68
69 (defface diff-hl-insert
70 '((default :inherit diff-added)
71 (((class color)) :foreground "green4"))
72 "Face used to highlight inserted lines."
73 :group 'diff-hl)
74
75 (defface diff-hl-delete
76 '((default :inherit diff-removed)
77 (((class color)) :foreground "red3"))
78 "Face used to highlight deleted lines."
79 :group 'diff-hl)
80
81 (defface diff-hl-change
82 '((default :foreground "blue3")
83 (((class color) (min-colors 88) (background light))
84 :background "#ddddff")
85 (((class color) (min-colors 88) (background dark))
86 :background "#333355"))
87 "Face used to highlight changed lines."
88 :group 'diff-hl)
89
90 (defcustom diff-hl-command-prefix (kbd "C-x v")
91 "The prefix for all `diff-hl' commands."
92 :group 'diff-hl
93 :type 'string)
94
95 (defcustom diff-hl-draw-borders t
96 "Non-nil to draw borders around fringe indicators."
97 :group 'diff-hl
98 :type 'boolean)
99
100 (defcustom diff-hl-highlight-function 'diff-hl-highlight-on-fringe
101 "Function to highlight the current line. Its arguments are
102 overlay, change type and position within a hunk."
103 :group 'diff-hl
104 :type 'function)
105
106 (defcustom diff-hl-fringe-bmp-function 'diff-hl-fringe-bmp-from-pos
107 "Function to choose the fringe bitmap for a given change type
108 and position within a hunk. Should accept two arguments."
109 :group 'diff-hl
110 :type '(choice (const diff-hl-fringe-bmp-from-pos)
111 (const diff-hl-fringe-bmp-from-type)
112 function))
113
114 (defcustom diff-hl-fringe-face-function 'diff-hl-fringe-face-from-type
115 "Function to choose the fringe face for a given change type
116 and position within a hunk. Should accept two arguments."
117 :group 'diff-hl
118 :type 'function)
119
120 (defvar diff-hl-reference-revision nil
121 "Revision to diff against. nil means the most recent one.")
122
123 (defun diff-hl-define-bitmaps ()
124 (let* ((scale (if (and (boundp 'text-scale-mode-amount)
125 (numberp text-scale-mode-amount))
126 (expt text-scale-mode-step text-scale-mode-amount)
127 1))
128 (spacing (or (and (display-graphic-p) (default-value 'line-spacing)) 0))
129 (h (+ (ceiling (* (frame-char-height) scale))
130 (if (floatp spacing)
131 (truncate (* (frame-char-height) spacing))
132 spacing)))
133 (w (frame-parameter nil 'left-fringe))
134 (middle (make-vector h (expt 2 (1- w))))
135 (ones (1- (expt 2 w)))
136 (top (copy-sequence middle))
137 (bottom (copy-sequence middle))
138 (single (copy-sequence middle)))
139 (aset top 0 ones)
140 (aset bottom (1- h) ones)
141 (aset single 0 ones)
142 (aset single (1- h) ones)
143 (define-fringe-bitmap 'diff-hl-bmp-top top h w 'top)
144 (define-fringe-bitmap 'diff-hl-bmp-middle middle h w 'center)
145 (define-fringe-bitmap 'diff-hl-bmp-bottom bottom h w 'bottom)
146 (define-fringe-bitmap 'diff-hl-bmp-single single h w 'top)
147 (define-fringe-bitmap 'diff-hl-bmp-i [3 3 0 3 3 3 3 3 3 3] nil 2 'center)
148 (let* ((w2 (* (/ w 2) 2))
149 ;; When fringes are disabled, it's easier to fix up the width,
150 ;; instead of doing nothing (#20).
151 (w2 (if (zerop w2) 2 w2))
152 (delete-row (- (expt 2 (1- w2)) 2))
153 (middle-pos (1- (/ w2 2)))
154 (middle-bit (expt 2 middle-pos))
155 (insert-bmp (make-vector w2 (* 3 middle-bit))))
156 (define-fringe-bitmap 'diff-hl-bmp-delete (make-vector 2 delete-row) w2 w2)
157 (aset insert-bmp 0 0)
158 (aset insert-bmp middle-pos delete-row)
159 (aset insert-bmp (1+ middle-pos) delete-row)
160 (aset insert-bmp (1- w2) 0)
161 (define-fringe-bitmap 'diff-hl-bmp-insert insert-bmp w2 w2)
162 )))
163
164 (defun diff-hl-maybe-define-bitmaps ()
165 (when (window-system) ;; No fringes in the console.
166 (unless (fringe-bitmap-p 'diff-hl-bmp-empty)
167 (diff-hl-define-bitmaps)
168 (define-fringe-bitmap 'diff-hl-bmp-empty [0] 1 1 'center))))
169
170 (defvar diff-hl-spec-cache (make-hash-table :test 'equal))
171
172 (defun diff-hl-fringe-spec (type pos)
173 (let* ((key (list type pos diff-hl-fringe-bmp-function))
174 (val (gethash key diff-hl-spec-cache)))
175 (unless val
176 (let* ((face-sym (funcall diff-hl-fringe-face-function type pos))
177 (bmp-sym (funcall diff-hl-fringe-bmp-function type pos)))
178 (setq val (propertize " " 'display `((left-fringe ,bmp-sym ,face-sym))))
179 (puthash key val diff-hl-spec-cache)))
180 val))
181
182 (defun diff-hl-fringe-face-from-type (type _pos)
183 (intern (format "diff-hl-%s" type)))
184
185 (defun diff-hl-fringe-bmp-from-pos (_type pos)
186 (intern (format "diff-hl-bmp-%s" pos)))
187
188 (defun diff-hl-fringe-bmp-from-type (type _pos)
189 (cl-case type
190 (unknown 'question-mark)
191 (change 'exclamation-mark)
192 (ignored 'diff-hl-bmp-i)
193 (t (intern (format "diff-hl-bmp-%s" type)))))
194
195 (defvar vc-svn-diff-switches)
196
197 (defmacro diff-hl-with-diff-switches (body)
198 `(let ((vc-git-diff-switches nil)
199 (vc-hg-diff-switches nil)
200 (vc-svn-diff-switches nil)
201 (vc-diff-switches '("-U0"))
202 (vc-disable-async-diff t))
203 ,body))
204
205 (defun diff-hl-changes ()
206 (let* ((file buffer-file-name)
207 (backend (vc-backend file)))
208 (when backend
209 (let ((state (vc-state file backend)))
210 (cond
211 ((or (eq state 'edited)
212 (and (eq state 'up-to-date)
213 ;; VC state is stale in after-revert-hook.
214 (or revert-buffer-in-progress-p
215 ;; Diffing against an older revision.
216 diff-hl-reference-revision)))
217 (let* ((buf-name " *diff-hl* ")
218 diff-auto-refine-mode
219 res)
220 (diff-hl-with-diff-switches
221 (vc-call-backend backend 'diff (list file)
222 diff-hl-reference-revision nil
223 buf-name))
224 (with-current-buffer buf-name
225 (goto-char (point-min))
226 (unless (eobp)
227 (ignore-errors
228 (diff-beginning-of-hunk t))
229 (while (looking-at diff-hunk-header-re-unified)
230 (let ((line (string-to-number (match-string 3)))
231 (len (let ((m (match-string 4)))
232 (if m (string-to-number m) 1)))
233 (beg (point)))
234 (diff-end-of-hunk)
235 (let* ((inserts (diff-count-matches "^\\+" beg (point)))
236 (deletes (diff-count-matches "^-" beg (point)))
237 (type (cond ((zerop deletes) 'insert)
238 ((zerop inserts) 'delete)
239 (t 'change))))
240 (when (eq type 'delete)
241 (setq len 1)
242 (cl-incf line))
243 (push (list line len type) res))))))
244 (nreverse res)))
245 ((eq state 'added)
246 `((1 ,(line-number-at-pos (point-max)) insert)))
247 ((eq state 'removed)
248 `((1 ,(line-number-at-pos (point-max)) delete))))))))
249
250 (defun diff-hl-update ()
251 (let ((changes (diff-hl-changes))
252 (current-line 1))
253 (diff-hl-remove-overlays)
254 (save-excursion
255 (goto-char (point-min))
256 (dolist (c changes)
257 (cl-destructuring-bind (line len type) c
258 (forward-line (- line current-line))
259 (setq current-line line)
260 (let ((hunk-beg (point)))
261 (while (cl-plusp len)
262 (diff-hl-add-highlighting
263 type
264 (cond
265 ((not diff-hl-draw-borders) 'empty)
266 ((and (= len 1) (= line current-line)) 'single)
267 ((= len 1) 'bottom)
268 ((= line current-line) 'top)
269 (t 'middle)))
270 (forward-line 1)
271 (cl-incf current-line)
272 (cl-decf len))
273 (let ((h (make-overlay hunk-beg (point)))
274 (hook '(diff-hl-overlay-modified)))
275 (overlay-put h 'diff-hl t)
276 (overlay-put h 'diff-hl-hunk t)
277 (overlay-put h 'modification-hooks hook)
278 (overlay-put h 'insert-in-front-hooks hook)
279 (overlay-put h 'insert-behind-hooks hook))))))))
280
281 (defun diff-hl-add-highlighting (type shape)
282 (let ((o (make-overlay (point) (point))))
283 (overlay-put o 'diff-hl t)
284 (funcall diff-hl-highlight-function o type shape)
285 o))
286
287 (defun diff-hl-highlight-on-fringe (ovl type shape)
288 (overlay-put ovl 'before-string (diff-hl-fringe-spec type shape)))
289
290 (defun diff-hl-remove-overlays ()
291 (dolist (o (overlays-in (point-min) (point-max)))
292 (when (overlay-get o 'diff-hl) (delete-overlay o))))
293
294 (defun diff-hl-overlay-modified (ov after-p _beg _end &optional _length)
295 "Delete the hunk overlay and all our line overlays inside it."
296 (unless after-p
297 (when (overlay-buffer ov)
298 (save-restriction
299 (narrow-to-region (overlay-start ov) (overlay-end ov))
300 (diff-hl-remove-overlays))
301 (delete-overlay ov))))
302
303 (defvar diff-hl-timer nil)
304
305 (defun diff-hl-edit (_beg _end _len)
306 "DTRT when we've `undo'-ne the buffer into unmodified state."
307 (when undo-in-progress
308 (when diff-hl-timer
309 (cancel-timer diff-hl-timer))
310 (setq diff-hl-timer
311 (run-with-idle-timer 0.01 nil #'diff-hl-after-undo (current-buffer)))))
312
313 (defun diff-hl-after-undo (buffer)
314 (with-current-buffer buffer
315 (unless (buffer-modified-p)
316 (diff-hl-update))))
317
318 (defun diff-hl-diff-goto-hunk ()
319 "Run VC diff command and go to the line corresponding to the current."
320 (interactive)
321 (vc-buffer-sync)
322 (let* ((line (line-number-at-pos))
323 (buffer (current-buffer)))
324 (vc-diff-internal t (vc-deduce-fileset) diff-hl-reference-revision nil t)
325 (vc-exec-after `(if (< (line-number-at-pos (point-max)) 3)
326 (with-current-buffer ,buffer (diff-hl-remove-overlays))
327 (diff-hl-diff-skip-to ,line)
328 (setq vc-sentinel-movepoint (point))))))
329
330 (defun diff-hl-diff-skip-to (line)
331 "In `diff-mode', skip to the hunk and line corresponding to LINE
332 in the source file, or the last line of the hunk above it."
333 (diff-hunk-next)
334 (let (found)
335 (while (and (looking-at diff-hunk-header-re-unified) (not found))
336 (let ((hunk-line (string-to-number (match-string 3)))
337 (len (let ((m (match-string 4)))
338 (if m (string-to-number m) 1))))
339 (if (> line (+ hunk-line len))
340 (diff-hunk-next)
341 (setq found t)
342 (if (< line hunk-line)
343 ;; Retreat to the previous hunk.
344 (forward-line -1)
345 (let ((to-go (1+ (- line hunk-line))))
346 (while (cl-plusp to-go)
347 (forward-line 1)
348 (unless (looking-at "^-")
349 (cl-decf to-go))))))))))
350
351 (defun diff-hl-revert-hunk ()
352 "Revert the diff hunk with changes at or above the point."
353 (interactive)
354 (vc-buffer-sync)
355 (let ((diff-buffer (generate-new-buffer-name "*diff-hl*"))
356 (buffer (current-buffer))
357 (line (save-excursion
358 (unless (diff-hl-hunk-overlay-at (point))
359 (diff-hl-previous-hunk))
360 (line-number-at-pos)))
361 (fileset (vc-deduce-fileset)))
362 (unwind-protect
363 (progn
364 (vc-diff-internal nil fileset diff-hl-reference-revision nil
365 nil diff-buffer)
366 (vc-exec-after
367 `(let (beg-line end-line)
368 (when (eobp)
369 (with-current-buffer ,buffer (diff-hl-remove-overlays))
370 (error "Buffer is up-to-date"))
371 (diff-hl-diff-skip-to ,line)
372 (save-excursion
373 (while (looking-at "[-+]") (forward-line 1))
374 (setq end-line (line-number-at-pos (point)))
375 (unless (eobp) (diff-split-hunk)))
376 (unless (looking-at "[-+]") (forward-line -1))
377 (while (looking-at "[-+]") (forward-line -1))
378 (setq beg-line (line-number-at-pos (point)))
379 (unless (looking-at "@")
380 (forward-line 1)
381 (diff-split-hunk))
382 (let ((wbh (window-body-height)))
383 (if (>= wbh (- end-line beg-line))
384 (recenter (/ (+ wbh (- beg-line end-line) 2) 2))
385 (recenter 1)))
386 (unless (yes-or-no-p (format "Revert current hunk in %s?"
387 ,(cl-caadr fileset)))
388 (error "Revert canceled"))
389 (let ((diff-advance-after-apply-hunk nil))
390 (diff-apply-hunk t))
391 (with-current-buffer ,buffer
392 (save-buffer))
393 (message "Hunk reverted"))))
394 (quit-windows-on diff-buffer))))
395
396 (defun diff-hl-hunk-overlay-at (pos)
397 (cl-loop for o in (overlays-in pos (1+ pos))
398 when (overlay-get o 'diff-hl-hunk)
399 return o))
400
401 (defun diff-hl-next-hunk (&optional backward)
402 "Go to the beginning of the next hunk in the current buffer."
403 (interactive)
404 (let ((pos (save-excursion
405 (catch 'found
406 (while (not (if backward (bobp) (eobp)))
407 (goto-char (if backward
408 (previous-overlay-change (point))
409 (next-overlay-change (point))))
410 (let ((o (diff-hl-hunk-overlay-at (point))))
411 (when (and o (= (overlay-start o) (point)))
412 (throw 'found (overlay-start o)))))))))
413 (if pos
414 (goto-char pos)
415 (error "No further hunks found"))))
416
417 (defun diff-hl-previous-hunk ()
418 "Go to the beginning of the previous hunk in the current buffer."
419 (interactive)
420 (diff-hl-next-hunk t))
421
422 (define-prefix-command 'diff-hl-command-map)
423
424 (let ((map diff-hl-command-map))
425 (define-key map "n" 'diff-hl-revert-hunk)
426 (define-key map "[" 'diff-hl-previous-hunk)
427 (define-key map "]" 'diff-hl-next-hunk)
428 map)
429
430 ;;;###autoload
431 (define-minor-mode diff-hl-mode
432 "Toggle VC diff highlighting."
433 :lighter "" :keymap `(([remap vc-diff] . diff-hl-diff-goto-hunk)
434 (,diff-hl-command-prefix . diff-hl-command-map))
435 (if diff-hl-mode
436 (progn
437 (diff-hl-maybe-define-bitmaps)
438 (add-hook 'after-save-hook 'diff-hl-update nil t)
439 (add-hook 'after-change-functions 'diff-hl-edit nil t)
440 (add-hook (if vc-mode
441 ;; Defer until the end of this hook, so that its
442 ;; elements can modify the update behavior.
443 'diff-hl-mode-on-hook
444 ;; If we're only opening the file now,
445 ;; `vc-find-file-hook' likely hasn't run yet, so
446 ;; let's wait until the state information is
447 ;; saved, in order not to fetch it twice.
448 'find-file-hook)
449 'diff-hl-update t t)
450 (add-hook 'vc-checkin-hook 'diff-hl-update nil t)
451 (add-hook 'after-revert-hook 'diff-hl-update nil t)
452 ;; Magit does call `auto-revert-handler', but it usually
453 ;; doesn't do much, because `buffer-stale--default-function'
454 ;; doesn't care about changed VC state.
455 ;; https://github.com/magit/magit/issues/603
456 (add-hook 'magit-revert-buffer-hook 'diff-hl-update nil t)
457 ;; Magit 2+ doesn't do the above and calls this instead,
458 ;; but only when it doesn't call `revert-buffer':
459 (add-hook 'magit-not-reverted-hook 'diff-hl-update nil t)
460 (add-hook 'auto-revert-mode-hook 'diff-hl-update nil t)
461 (add-hook 'text-scale-mode-hook 'diff-hl-define-bitmaps nil t))
462 (remove-hook 'after-save-hook 'diff-hl-update t)
463 (remove-hook 'after-change-functions 'diff-hl-edit t)
464 (remove-hook 'find-file-hook 'diff-hl-update t)
465 (remove-hook 'vc-checkin-hook 'diff-hl-update t)
466 (remove-hook 'after-revert-hook 'diff-hl-update t)
467 (remove-hook 'magit-revert-buffer-hook 'diff-hl-update t)
468 (remove-hook 'magit-not-reverted-hook 'diff-hl-update t)
469 (remove-hook 'auto-revert-mode-hook 'diff-hl-update t)
470 (remove-hook 'text-scale-mode-hook 'diff-hl-define-bitmaps t)
471 (diff-hl-remove-overlays)))
472
473 (when (require 'smartrep nil t)
474 (let (smart-keys)
475 (cl-labels ((scan (map)
476 (map-keymap
477 (lambda (event binding)
478 (if (consp binding)
479 (scan binding)
480 (when (characterp event)
481 (push (cons (string event) binding) smart-keys))))
482 map)))
483 (scan diff-hl-command-map)
484 (smartrep-define-key diff-hl-mode-map diff-hl-command-prefix smart-keys))))
485
486 (defun diff-hl-dir-update ()
487 (dolist (pair (if (vc-dir-marked-files)
488 (vc-dir-marked-only-files-and-states)
489 (vc-dir-child-files-and-states)))
490 (when (eq 'up-to-date (cdr pair))
491 (let ((buffer (find-buffer-visiting (car pair))))
492 (when buffer
493 (with-current-buffer buffer
494 (diff-hl-remove-overlays)))))))
495
496 (define-minor-mode diff-hl-dir-mode
497 "Toggle `diff-hl-mode' integration in a `vc-dir-mode' buffer."
498 :lighter ""
499 (if diff-hl-dir-mode
500 (add-hook 'vc-checkin-hook 'diff-hl-dir-update t t)
501 (remove-hook 'vc-checkin-hook 'diff-hl-dir-update t)))
502
503 ;;;###autoload
504 (defun turn-on-diff-hl-mode ()
505 "Turn on `diff-hl-mode' or `diff-hl-dir-mode' in a buffer if appropriate."
506 (cond
507 (buffer-file-name
508 (diff-hl-mode 1))
509 ((eq major-mode 'vc-dir-mode)
510 (diff-hl-dir-mode 1))))
511
512 ;;;###autoload
513 (define-globalized-minor-mode global-diff-hl-mode diff-hl-mode
514 turn-on-diff-hl-mode :after-hook (diff-hl-global-mode-change))
515
516 (defun diff-hl-global-mode-change ()
517 (unless global-diff-hl-mode
518 (dolist (buf (buffer-list))
519 (with-current-buffer buf
520 (when diff-hl-dir-mode
521 (diff-hl-dir-mode -1))))))
522
523 (provide 'diff-hl)
524
525 ;;; diff-hl.el ends here