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