1 ;;; diff-hl.el --- Highlight uncommitted changes -*- lexical-binding: t -*-
3 ;; Copyright (C) 2012-2014 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)
62 (require 'face-remap))
65 "VC diff highlighting on the side of a window"
68 (defface diff-hl-insert
69 '((default :inherit diff-added)
70 (((class color)) :foreground "green4"))
71 "Face used to highlight inserted lines."
74 (defface diff-hl-delete
75 '((default :inherit diff-removed)
76 (((class color)) :foreground "red3"))
77 "Face used to highlight deleted lines."
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."
89 (defcustom diff-hl-command-prefix (kbd "C-x v")
90 "The prefix for all `diff-hl' commands."
94 (defcustom diff-hl-draw-borders t
95 "Non-nil to draw borders around fringe indicators."
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."
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."
109 :type '(choice (const diff-hl-fringe-bmp-from-pos)
110 (const diff-hl-fringe-bmp-from-type)
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."
119 (defvar diff-hl-reference-revision nil
120 "Revision to diff against. nil means the most recent one.")
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)
127 (spacing (or (and (display-graphic-p) (default-value 'line-spacing)) 0))
128 (h (+ (ceiling (* (frame-char-height) scale))
130 (truncate (* (frame-char-height) 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)))
139 (aset bottom (1- h) 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 (let* ((w2 (* (/ w 2) 2))
147 ;; When fringes are disabled, it's easier to fix up the width,
148 ;; instead of doing nothing (#20).
149 (w2 (if (zerop w2) 2 w2))
150 (delete-row (- (expt 2 (1- w2)) 2))
151 (middle-pos (1- (/ w2 2)))
152 (middle-bit (expt 2 middle-pos))
153 (insert-bmp (make-vector w2 (* 3 middle-bit))))
154 (define-fringe-bitmap 'diff-hl-bmp-delete (make-vector 2 delete-row) w2 w2)
155 (aset insert-bmp 0 0)
156 (aset insert-bmp middle-pos delete-row)
157 (aset insert-bmp (1+ middle-pos) delete-row)
158 (aset insert-bmp (1- w2) 0)
159 (define-fringe-bitmap 'diff-hl-bmp-insert insert-bmp w2 w2)
162 (defun diff-hl-maybe-define-bitmaps ()
163 (when (window-system) ;; No fringes in the console.
164 (unless (fringe-bitmap-p 'diff-hl-bmp-empty)
165 (diff-hl-define-bitmaps)
166 (define-fringe-bitmap 'diff-hl-bmp-empty [0] 1 1 'center))))
168 (defvar diff-hl-spec-cache (make-hash-table :test 'equal))
170 (defun diff-hl-fringe-spec (type pos)
171 (let* ((key (list type pos diff-hl-fringe-bmp-function))
172 (val (gethash key diff-hl-spec-cache)))
174 (let* ((face-sym (funcall diff-hl-fringe-face-function type pos))
175 (bmp-sym (funcall diff-hl-fringe-bmp-function type pos)))
176 (setq val (propertize " " 'display `((left-fringe ,bmp-sym ,face-sym))))
177 (puthash key val diff-hl-spec-cache)))
180 (defun diff-hl-fringe-face-from-type (type _pos)
181 (intern (format "diff-hl-%s" type)))
183 (defun diff-hl-fringe-bmp-from-pos (_type pos)
184 (intern (format "diff-hl-bmp-%s" pos)))
186 (defun diff-hl-fringe-bmp-from-type (type _pos)
188 (unknown 'question-mark)
189 (change 'exclamation-mark)
190 (ignored 'filled-square)
191 (t (intern (format "diff-hl-bmp-%s" type)))))
193 (defvar vc-svn-diff-switches)
195 (defmacro diff-hl-with-diff-switches (body)
196 `(let ((vc-git-diff-switches nil)
197 (vc-hg-diff-switches nil)
198 (vc-svn-diff-switches nil)
199 (vc-diff-switches '("-U0"))
200 (vc-disable-async-diff t))
203 (defun diff-hl-changes ()
204 (let* ((file buffer-file-name)
205 (backend (vc-backend file)))
207 (let ((state (vc-state file backend)))
209 ((or (eq state 'edited)
210 (and (eq state 'up-to-date)
211 ;; VC state is stale in after-revert-hook.
212 (or revert-buffer-in-progress-p
213 ;; Diffing against an older revision.
214 diff-hl-reference-revision)))
215 (let* ((buf-name " *diff-hl* ")
216 diff-auto-refine-mode
218 (diff-hl-with-diff-switches
219 (vc-call-backend backend 'diff (list file)
220 diff-hl-reference-revision nil
222 (with-current-buffer buf-name
223 (goto-char (point-min))
226 (diff-beginning-of-hunk t))
227 (while (looking-at diff-hunk-header-re-unified)
228 (let ((line (string-to-number (match-string 3)))
229 (len (let ((m (match-string 4)))
230 (if m (string-to-number m) 1)))
233 (let* ((inserts (diff-count-matches "^\\+" beg (point)))
234 (deletes (diff-count-matches "^-" beg (point)))
235 (type (cond ((zerop deletes) 'insert)
236 ((zerop inserts) 'delete)
238 (when (eq type 'delete)
241 (push (list line len type) res))))))
244 `((1 ,(line-number-at-pos (point-max)) insert)))
246 `((1 ,(line-number-at-pos (point-max)) delete))))))))
248 (defun diff-hl-update ()
249 (let ((changes (diff-hl-changes))
251 (diff-hl-remove-overlays)
253 (goto-char (point-min))
255 (cl-destructuring-bind (line len type) c
256 (forward-line (- line current-line))
257 (setq current-line line)
258 (let ((hunk-beg (point)))
259 (while (cl-plusp len)
260 (diff-hl-add-highlighting
263 ((not diff-hl-draw-borders) 'empty)
264 ((and (= len 1) (= line current-line)) 'single)
266 ((= line current-line) 'top)
269 (cl-incf current-line)
271 (let ((h (make-overlay hunk-beg (point)))
272 (hook '(diff-hl-overlay-modified)))
273 (overlay-put h 'diff-hl t)
274 (overlay-put h 'diff-hl-hunk t)
275 (overlay-put h 'modification-hooks hook)
276 (overlay-put h 'insert-in-front-hooks hook)
277 (overlay-put h 'insert-behind-hooks hook))))))))
279 (defun diff-hl-add-highlighting (type shape)
280 (let ((o (make-overlay (point) (point))))
281 (overlay-put o 'diff-hl t)
282 (funcall diff-hl-highlight-function o type shape)
285 (defun diff-hl-highlight-on-fringe (ovl type shape)
286 (overlay-put ovl 'before-string (diff-hl-fringe-spec type shape)))
288 (defun diff-hl-remove-overlays ()
289 (dolist (o (overlays-in (point-min) (point-max)))
290 (when (overlay-get o 'diff-hl) (delete-overlay o))))
292 (defun diff-hl-overlay-modified (ov after-p _beg _end &optional _length)
293 "Delete the hunk overlay and all our line overlays inside it."
295 (when (overlay-buffer ov)
297 (narrow-to-region (overlay-start ov) (overlay-end ov))
298 (diff-hl-remove-overlays))
299 (delete-overlay ov))))
301 (defvar diff-hl-timer nil)
303 (defun diff-hl-edit (_beg _end _len)
304 "DTRT when we've `undo'-ne the buffer into unmodified state."
305 (when undo-in-progress
307 (cancel-timer diff-hl-timer))
309 (run-with-idle-timer 0.01 nil #'diff-hl-after-undo (current-buffer)))))
311 (defun diff-hl-after-undo (buffer)
312 (with-current-buffer buffer
313 (unless (buffer-modified-p)
316 (defun diff-hl-diff-goto-hunk ()
317 "Run VC diff command and go to the line corresponding to the current."
320 (let* ((line (line-number-at-pos))
321 (buffer (current-buffer)))
322 (vc-diff-internal t (vc-deduce-fileset) diff-hl-reference-revision nil t)
323 (vc-exec-after `(if (< (line-number-at-pos (point-max)) 3)
324 (with-current-buffer ,buffer (diff-hl-remove-overlays))
325 (diff-hl-diff-skip-to ,line)
326 (setq vc-sentinel-movepoint (point))))))
328 (defun diff-hl-diff-skip-to (line)
329 "In `diff-mode', skip to the hunk and line corresponding to LINE
330 in the source file, or the last line of the hunk above it."
333 (while (and (looking-at diff-hunk-header-re-unified) (not found))
334 (let ((hunk-line (string-to-number (match-string 3)))
335 (len (let ((m (match-string 4)))
336 (if m (string-to-number m) 1))))
337 (if (> line (+ hunk-line len))
340 (if (< line hunk-line)
341 ;; Retreat to the previous hunk.
343 (let ((to-go (1+ (- line hunk-line))))
344 (while (cl-plusp to-go)
346 (unless (looking-at "^-")
347 (cl-decf to-go))))))))))
349 (defun diff-hl-revert-hunk ()
350 "Revert the diff hunk with changes at or above the point."
353 (let ((diff-buffer (generate-new-buffer-name "*diff-hl*"))
354 (buffer (current-buffer))
355 (line (save-excursion
356 (unless (diff-hl-hunk-overlay-at (point))
357 (diff-hl-previous-hunk))
358 (line-number-at-pos)))
359 (fileset (vc-deduce-fileset)))
362 (vc-diff-internal nil fileset diff-hl-reference-revision nil
365 `(let (beg-line end-line)
367 (with-current-buffer ,buffer (diff-hl-remove-overlays))
368 (error "Buffer is up-to-date"))
369 (diff-hl-diff-skip-to ,line)
371 (while (looking-at "[-+]") (forward-line 1))
372 (setq end-line (line-number-at-pos (point)))
373 (unless (eobp) (diff-split-hunk)))
374 (unless (looking-at "[-+]") (forward-line -1))
375 (while (looking-at "[-+]") (forward-line -1))
376 (setq beg-line (line-number-at-pos (point)))
377 (unless (looking-at "@")
380 (let ((wbh (window-body-height)))
381 (if (>= wbh (- end-line beg-line))
382 (recenter (/ (+ wbh (- beg-line end-line) 2) 2))
384 (unless (yes-or-no-p (format "Revert current hunk in %s?"
385 ,(cl-caadr fileset)))
386 (error "Revert canceled"))
387 (let ((diff-advance-after-apply-hunk nil))
389 (with-current-buffer ,buffer
391 (message "Hunk reverted"))))
392 (quit-windows-on diff-buffer))))
394 (defun diff-hl-hunk-overlay-at (pos)
395 (cl-loop for o in (overlays-in pos (1+ pos))
396 when (overlay-get o 'diff-hl-hunk)
399 (defun diff-hl-next-hunk (&optional backward)
400 "Go to the beginning of the next hunk in the current buffer."
402 (let ((pos (save-excursion
404 (while (not (if backward (bobp) (eobp)))
405 (goto-char (if backward
406 (previous-overlay-change (point))
407 (next-overlay-change (point))))
408 (let ((o (diff-hl-hunk-overlay-at (point))))
409 (when (and o (= (overlay-start o) (point)))
410 (throw 'found (overlay-start o)))))))))
413 (error "No further hunks found"))))
415 (defun diff-hl-previous-hunk ()
416 "Go to the beginning of the previous hunk in the current buffer."
418 (diff-hl-next-hunk t))
420 (define-prefix-command 'diff-hl-command-map)
422 (let ((map diff-hl-command-map))
423 (define-key map "n" 'diff-hl-revert-hunk)
424 (define-key map "[" 'diff-hl-previous-hunk)
425 (define-key map "]" 'diff-hl-next-hunk)
429 (define-minor-mode diff-hl-mode
430 "Toggle VC diff highlighting."
431 :lighter "" :keymap `(([remap vc-diff] . diff-hl-diff-goto-hunk)
432 (,diff-hl-command-prefix . diff-hl-command-map))
435 (diff-hl-maybe-define-bitmaps)
436 (add-hook 'after-save-hook 'diff-hl-update nil t)
437 (add-hook 'after-change-functions 'diff-hl-edit nil t)
438 (add-hook (if vc-mode
439 ;; Defer until the end of this hook, so that its
440 ;; elements can modify the update behavior.
441 'diff-hl-mode-on-hook
442 ;; If we're only opening the file now,
443 ;; `vc-find-file-hook' likely hasn't run yet, so
444 ;; let's wait until the state information is
445 ;; saved, in order not to fetch it twice.
448 (add-hook 'vc-checkin-hook 'diff-hl-update nil t)
449 (add-hook 'after-revert-hook 'diff-hl-update nil t)
450 ;; Magit does call `auto-revert-handler', but it usually
451 ;; doesn't do much, because `buffer-stale--default-function'
452 ;; doesn't care about changed VC state.
453 ;; https://github.com/magit/magit/issues/603
454 (add-hook 'magit-revert-buffer-hook 'diff-hl-update nil t)
455 (add-hook 'auto-revert-mode-hook 'diff-hl-update nil t)
456 (add-hook 'text-scale-mode-hook 'diff-hl-define-bitmaps nil t))
457 (remove-hook 'after-save-hook 'diff-hl-update t)
458 (remove-hook 'after-change-functions 'diff-hl-edit t)
459 (remove-hook 'find-file-hook 'diff-hl-update t)
460 (remove-hook 'vc-checkin-hook 'diff-hl-update t)
461 (remove-hook 'after-revert-hook 'diff-hl-update t)
462 (remove-hook 'magit-revert-buffer-hook 'diff-hl-update t)
463 (remove-hook 'auto-revert-mode-hook 'diff-hl-update t)
464 (remove-hook 'text-scale-mode-hook 'diff-hl-define-bitmaps t)
465 (diff-hl-remove-overlays)))
467 (when (require 'smartrep nil t)
469 (cl-labels ((scan (map)
471 (lambda (event binding)
474 (when (characterp event)
475 (push (cons (string event) binding) smart-keys))))
477 (scan diff-hl-command-map)
478 (smartrep-define-key diff-hl-mode-map diff-hl-command-prefix smart-keys))))
480 (defun diff-hl-dir-update ()
481 (dolist (pair (if (vc-dir-marked-files)
482 (vc-dir-marked-only-files-and-states)
483 (vc-dir-child-files-and-states)))
484 (when (eq 'up-to-date (cdr pair))
485 (let ((buffer (find-buffer-visiting (car pair))))
487 (with-current-buffer buffer
488 (diff-hl-remove-overlays)))))))
490 (define-minor-mode diff-hl-dir-mode
491 "Toggle `diff-hl-mode' integration in a `vc-dir-mode' buffer."
494 (add-hook 'vc-checkin-hook 'diff-hl-dir-update t t)
495 (remove-hook 'vc-checkin-hook 'diff-hl-dir-update t)))
498 (defun turn-on-diff-hl-mode ()
499 "Turn on `diff-hl-mode' or `diff-hl-dir-mode' in a buffer if appropriate."
503 ((eq major-mode 'vc-dir-mode)
504 (diff-hl-dir-mode 1))))
507 (define-globalized-minor-mode global-diff-hl-mode diff-hl-mode
508 turn-on-diff-hl-mode :after-hook (diff-hl-global-mode-change))
510 (defun diff-hl-global-mode-change ()
511 (unless global-diff-hl-mode
512 (dolist (buf (buffer-list))
513 (with-current-buffer buf
514 (when diff-hl-dir-mode
515 (diff-hl-dir-mode -1))))))
519 ;;; diff-hl.el ends here