1 ;;; diff-hl.el --- Highlight uncommitted changes -*- lexical-binding: t -*-
3 ;; Copyright (C) 2012-2013 Free Software Foundation, Inc.
5 ;; Author: Dmitry Gutov <dgutov@yandex.ru>
6 ;; URL: https://github.com/dgutov/diff-hl
9 ;; Package-Requires: ((cl-lib "0.2"))
11 ;; This file is part of GNU Emacs.
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.
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.
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/>.
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.
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 ]
39 ;; The mode takes advantage of `smartrep' if it is installed.
41 ;; Add either of the following to your init file.
43 ;; To use it in all buffers:
45 ;; (global-diff-hl-mode)
47 ;; Only in `prog-mode' buffers, with `vc-dir' integration:
49 ;; (add-hook 'prog-mode-hook 'turn-on-diff-hl-mode)
50 ;; (add-hook 'vc-dir-mode-hook 'turn-on-diff-hl-mode)
61 (require 'face-remap))
64 "VC diff highlighting on the side of a window"
67 (defface diff-hl-insert
68 '((default :inherit diff-added)
69 (((class color)) :foreground "green4"))
70 "Face used to highlight inserted lines."
73 (defface diff-hl-delete
74 '((default :inherit diff-removed)
75 (((class color)) :foreground "red3"))
76 "Face used to highlight deleted lines."
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."
88 (defface diff-hl-unknown
89 '((default :inherit diff-header))
90 "Face used to highlight unregistered files.")
92 (defcustom diff-hl-draw-borders t
93 "Non-nil to draw borders around fringe indicators."
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."
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."
107 :type '(choice (const diff-hl-fringe-bmp-from-pos)
108 (const diff-hl-fringe-bmp-from-type)
111 (defvar diff-hl-reference-revision nil
112 "Revision to diff against. nil means the most recent one.")
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)
119 (spacing (or (default-value 'line-spacing) 0))
120 (h (round (+ (* (frame-char-height) scale)
122 (* (frame-char-height) 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)))
131 (aset bottom (1- h) 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))))
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))))
158 (defvar diff-hl-spec-cache (make-hash-table :test 'equal))
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)))
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)))
170 (defun diff-hl-fringe-bmp-from-pos (_type pos)
171 (intern (format "diff-hl-bmp-%s" pos)))
173 (defun diff-hl-fringe-bmp-from-type (type _pos)
174 (if (eq type 'unknown)
176 (intern (format "diff-hl-bmp-%s" type))))
178 (defvar vc-svn-diff-switches)
180 (defmacro diff-hl-with-diff-switches (body)
181 `(let ((vc-git-diff-switches nil)
182 (vc-hg-diff-switches nil)
183 (vc-svn-diff-switches nil)
184 (vc-diff-switches '("-U0"))
185 (vc-disable-async-diff t))
188 (defun diff-hl-changes ()
189 (let* ((file buffer-file-name)
190 (backend (vc-backend file)))
192 (let ((state (vc-state file backend)))
194 ((or (eq state 'edited)
195 (and (eq state 'up-to-date)
196 ;; VC state is stale in after-revert-hook.
197 (or revert-buffer-in-progress-p
198 ;; Diffing against an older revision.
199 diff-hl-reference-revision)))
200 (let* ((buf-name " *diff-hl* ")
201 diff-auto-refine-mode
203 (diff-hl-with-diff-switches
204 (vc-call-backend backend 'diff (list file)
205 diff-hl-reference-revision nil
207 (with-current-buffer buf-name
208 (goto-char (point-min))
210 (diff-beginning-of-hunk t)
211 (while (looking-at diff-hunk-header-re-unified)
212 (let ((line (string-to-number (match-string 3)))
213 (len (let ((m (match-string 4)))
214 (if m (string-to-number m) 1)))
217 (let* ((inserts (diff-count-matches "^\\+" beg (point)))
218 (deletes (diff-count-matches "^-" beg (point)))
219 (type (cond ((zerop deletes) 'insert)
220 ((zerop inserts) 'delete)
222 (when (eq type 'delete)
225 (push (list line len type) res))))))
228 `((1 ,(line-number-at-pos (point-max)) insert)))
230 `((1 ,(line-number-at-pos (point-max)) delete))))))))
232 (defun diff-hl-update ()
233 (let ((changes (diff-hl-changes))
235 (diff-hl-remove-overlays)
237 (goto-char (point-min))
239 (cl-destructuring-bind (line len type) c
240 (forward-line (- line current-line))
241 (setq current-line line)
242 (let ((hunk-beg (point)))
243 (while (cl-plusp len)
244 (diff-hl-add-highlighting
247 ((not diff-hl-draw-borders) 'empty)
248 ((and (= len 1) (= line current-line)) 'single)
250 ((= line current-line) 'top)
253 (cl-incf current-line)
255 (let ((h (make-overlay hunk-beg (point)))
256 (hook '(diff-hl-overlay-modified)))
257 (overlay-put h 'diff-hl t)
258 (overlay-put h 'diff-hl-hunk t)
259 (overlay-put h 'modification-hooks hook)
260 (overlay-put h 'insert-in-front-hooks hook)
261 (overlay-put h 'insert-behind-hooks hook))))))))
263 (defun diff-hl-add-highlighting (type shape)
264 (let ((o (make-overlay (point) (point))))
265 (overlay-put o 'diff-hl t)
266 (funcall diff-hl-highlight-function o type shape)
269 (defun diff-hl-highlight-on-fringe (ovl type shape)
270 (overlay-put ovl 'before-string (diff-hl-fringe-spec type shape)))
272 (defun diff-hl-remove-overlays ()
273 (dolist (o (overlays-in (point-min) (point-max)))
274 (when (overlay-get o 'diff-hl) (delete-overlay o))))
276 (defun diff-hl-overlay-modified (ov after-p _beg _end &optional _length)
277 "Delete the hunk overlay and all our line overlays inside it."
279 (when (overlay-buffer ov)
281 (narrow-to-region (overlay-start ov) (overlay-end ov))
282 (diff-hl-remove-overlays))
283 (delete-overlay ov))))
285 (defvar diff-hl-timer nil)
287 (defun diff-hl-edit (_beg _end _len)
288 "DTRT when we've `undo'-ne the buffer into unmodified state."
289 (when undo-in-progress
291 (cancel-timer diff-hl-timer))
293 (run-with-idle-timer 0.01 nil #'diff-hl-after-undo (current-buffer)))))
295 (defun diff-hl-after-undo (buffer)
296 (with-current-buffer buffer
297 (unless (buffer-modified-p)
300 (defun diff-hl-diff-goto-hunk ()
301 "Run VC diff command and go to the line corresponding to the current."
304 (let* ((line (line-number-at-pos))
305 (buffer (current-buffer)))
306 (vc-diff-internal t (vc-deduce-fileset) diff-hl-reference-revision nil t)
307 (vc-exec-after `(if (< (line-number-at-pos (point-max)) 3)
308 (with-current-buffer ,buffer (diff-hl-remove-overlays))
309 (diff-hl-diff-skip-to ,line)
310 (setq vc-sentinel-movepoint (point))))))
312 (defun diff-hl-diff-skip-to (line)
313 "In `diff-mode', skip to the hunk and line corresponding to LINE
314 in the source file, or the last line of the hunk above it."
317 (while (and (looking-at diff-hunk-header-re-unified) (not found))
318 (let ((hunk-line (string-to-number (match-string 3)))
319 (len (let ((m (match-string 4)))
320 (if m (string-to-number m) 1))))
321 (if (> line (+ hunk-line len))
324 (if (< line hunk-line)
325 ;; Retreat to the previous hunk.
327 (let ((to-go (1+ (- line hunk-line))))
328 (while (cl-plusp to-go)
330 (unless (looking-at "^-")
331 (cl-decf to-go))))))))))
333 (defun diff-hl-revert-hunk ()
334 "Revert the diff hunk with changes at or above the point."
337 (let ((diff-buffer (generate-new-buffer-name "*diff-hl*"))
338 (buffer (current-buffer))
339 (line (save-excursion
340 (unless (diff-hl-hunk-overlay-at (point))
341 (diff-hl-previous-hunk))
342 (line-number-at-pos)))
343 (fileset (vc-deduce-fileset)))
346 (vc-diff-internal nil fileset diff-hl-reference-revision nil
349 `(let (beg-line end-line)
351 (with-current-buffer ,buffer (diff-hl-remove-overlays))
352 (error "Buffer is up-to-date"))
353 (diff-hl-diff-skip-to ,line)
355 (while (looking-at "[-+]") (forward-line 1))
356 (setq end-line (line-number-at-pos (point)))
357 (unless (eobp) (diff-split-hunk)))
358 (unless (looking-at "[-+]") (forward-line -1))
359 (while (looking-at "[-+]") (forward-line -1))
360 (setq beg-line (line-number-at-pos (point)))
361 (unless (looking-at "@")
364 (let ((wbh (window-body-height)))
365 (if (>= wbh (- end-line beg-line))
366 (recenter (/ (+ wbh (- beg-line end-line) 2) 2))
368 (unless (yes-or-no-p (format "Revert current hunk in %s?"
369 ,(cl-caadr fileset)))
370 (error "Revert canceled"))
371 (let ((diff-advance-after-apply-hunk nil))
373 (with-current-buffer ,buffer
375 (message "Hunk reverted"))))
376 (quit-windows-on diff-buffer))))
378 (defun diff-hl-hunk-overlay-at (pos)
379 (cl-loop for o in (overlays-in pos (1+ pos))
380 when (overlay-get o 'diff-hl-hunk)
383 (defun diff-hl-next-hunk (&optional backward)
384 "Go to the beginning of the next hunk in the current buffer."
386 (let ((pos (save-excursion
388 (while (not (if backward (bobp) (eobp)))
389 (goto-char (if backward
390 (previous-overlay-change (point))
391 (next-overlay-change (point))))
392 (let ((o (diff-hl-hunk-overlay-at (point))))
393 (when (and o (= (overlay-start o) (point)))
394 (throw 'found (overlay-start o)))))))))
397 (error "No further hunks found"))))
399 (defun diff-hl-previous-hunk ()
400 "Go to the beginning of the previous hunk in the current buffer."
402 (diff-hl-next-hunk t))
405 (define-minor-mode diff-hl-mode
406 "Toggle VC diff highlighting."
407 :lighter "" :keymap `(([remap vc-diff] . diff-hl-diff-goto-hunk)
408 (,(kbd "C-x v n") . diff-hl-revert-hunk)
409 (,(kbd "C-x v [") . diff-hl-previous-hunk)
410 (,(kbd "C-x v ]") . diff-hl-next-hunk))
413 (diff-hl-maybe-define-bitmaps)
414 (add-hook 'after-save-hook 'diff-hl-update nil t)
415 (add-hook 'after-change-functions 'diff-hl-edit nil t)
418 (add-hook 'find-file-hook 'diff-hl-update t t))
419 (add-hook 'vc-checkin-hook 'diff-hl-update nil t)
420 (add-hook 'after-revert-hook 'diff-hl-update nil t)
421 (add-hook 'text-scale-mode-hook 'diff-hl-define-bitmaps nil t))
422 (remove-hook 'after-save-hook 'diff-hl-update t)
423 (remove-hook 'after-change-functions 'diff-hl-edit t)
424 (remove-hook 'find-file-hook 'diff-hl-update t)
425 (remove-hook 'vc-checkin-hook 'diff-hl-update t)
426 (remove-hook 'after-revert-hook 'diff-hl-update t)
427 (remove-hook 'text-scale-mode-hook 'diff-hl-define-bitmaps t)
428 (diff-hl-remove-overlays)))
430 (when (require 'smartrep nil t)
432 (cl-labels ((scan (map)
434 (lambda (event binding)
437 (when (characterp event)
438 (push (cons (string event) binding) smart-keys))))
440 (scan diff-hl-mode-map)
441 (smartrep-define-key diff-hl-mode-map "C-x v" smart-keys))))
443 (defun diff-hl-dir-update ()
444 (dolist (pair (if (vc-dir-marked-files)
445 (vc-dir-marked-only-files-and-states)
446 (vc-dir-child-files-and-states)))
447 (when (eq 'up-to-date (cdr pair))
448 (let ((buffer (find-buffer-visiting (car pair))))
450 (with-current-buffer buffer
451 (diff-hl-remove-overlays)))))))
453 (define-minor-mode diff-hl-dir-mode
454 "Toggle `diff-hl-mode' integration in a `vc-dir-mode' buffer."
457 (add-hook 'vc-checkin-hook 'diff-hl-dir-update t t)
458 (remove-hook 'vc-checkin-hook 'diff-hl-dir-update t)))
461 (defun turn-on-diff-hl-mode ()
462 "Turn on `diff-hl-mode' or `diff-hl-dir-mode' in a buffer if appropriate."
466 ((eq major-mode 'vc-dir-mode)
467 (diff-hl-dir-mode 1))))
470 (define-globalized-minor-mode global-diff-hl-mode diff-hl-mode
471 turn-on-diff-hl-mode :after-hook (diff-hl-global-mode-change))
473 (defun diff-hl-global-mode-change ()
474 (unless global-diff-hl-mode
475 (dolist (buf (buffer-list))
476 (with-current-buffer buf
477 (when diff-hl-dir-mode
478 (diff-hl-dir-mode -1))))))
482 ;;; diff-hl.el ends here