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