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