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