1 ;;; diff-hl.el --- VC diff fringe highlighting -*- lexical-binding: t -*-
3 ;; Author: Dmitry Gutov <dgutov@yandex.ru>
4 ;; URL: https://github.com/dgutov/diff-hl
8 ;; This file is not part of GNU Emacs.
10 ;; This file is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; This file is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;; `diff-hl-mode' provides IDE-like highlighting of `vc-diff' results
26 ;; on the left fringe of the current buffer.
28 ;; For full description, see README.md or the home page.
38 (require 'face-remap))
41 "VC diff fringe highlighting"
44 (defface diff-hl-insert
45 '((t :inherit diff-added))
46 "Face used to highlight inserted lines."
49 (defface diff-hl-delete
50 '((t :inherit diff-removed))
51 "Face used to highlight deleted lines."
54 (defface diff-hl-change
57 (((class color) (min-colors 88) (background light))
58 :background "#ddddff")
59 (((class color) (min-colors 88) (background dark))
60 :background "#333355"))
61 "Face used to highlight changed lines."
64 (defcustom diff-hl-draw-borders t
65 "Non-nil to draw borders around fringe indicators."
69 (defun diff-hl-define-bitmaps ()
70 (let* ((scale (if (and (boundp 'text-scale-mode-amount)
71 (plusp text-scale-mode-amount))
72 (expt text-scale-mode-step text-scale-mode-amount)
74 (h (round (* (frame-char-height) scale)))
75 (w (frame-parameter nil 'left-fringe))
76 (middle (make-vector h (expt 2 (1- w))))
77 (ones (1- (expt 2 w)))
78 (top (copy-sequence middle))
79 (bottom (copy-sequence middle))
80 (single (copy-sequence middle)))
82 (aset bottom (1- h) ones)
84 (aset single (1- h) ones)
85 (define-fringe-bitmap 'diff-hl-bmp-top top h w 'top)
86 (define-fringe-bitmap 'diff-hl-bmp-middle middle h w 'center)
87 (define-fringe-bitmap 'diff-hl-bmp-bottom bottom h w 'bottom)
88 (define-fringe-bitmap 'diff-hl-bmp-single single h w 'top)))
91 (define-fringe-bitmap 'diff-hl-bmp-empty [0] 1 1 'center)
92 (diff-hl-define-bitmaps))
94 (defvar diff-hl-spec-cache (make-hash-table :test 'equal))
96 (defun diff-hl-fringe-spec (type pos)
97 (let* ((key (cons type pos))
98 (val (gethash key diff-hl-spec-cache)))
100 (let* ((face-sym (intern (concat "diff-hl-" (symbol-name type))))
101 (bmp-sym (intern (concat "diff-hl-bmp-" (symbol-name pos)))))
102 (setq val (propertize " " 'display `((left-fringe ,bmp-sym ,face-sym))))
103 (puthash key val diff-hl-spec-cache)))
106 (defmacro diff-hl-with-diff-switches (body)
107 `(let ((vc-git-diff-switches nil)
108 (vc-hg-diff-switches nil)
109 (vc-diff-switches '("-U0"))
110 (vc-disable-async-diff t))
113 (defun diff-hl-changes ()
114 (let* ((buf-name " *diff-hl* ")
115 (file (buffer-file-name))
116 (backend (vc-backend file))
118 (when (and backend (not (eq (vc-state file backend) 'up-to-date)))
119 (diff-hl-with-diff-switches
120 (vc-call-backend backend 'diff (list file) nil nil buf-name))
121 (with-current-buffer buf-name
122 (goto-char (point-min))
124 (diff-beginning-of-hunk t)
125 (while (looking-at diff-hunk-header-re-unified)
126 (let ((line (string-to-number (match-string 3)))
127 (len (let ((m (match-string 4)))
128 (if m (string-to-number m) 1)))
131 (let* ((inserts (diff-count-matches "^\\+" beg (point)))
132 (deletes (diff-count-matches "^-" beg (point)))
133 (type (cond ((zerop deletes) 'insert)
134 ((zerop inserts) 'delete)
136 (push (list line len type) res)))))))
139 (defun diff-hl-update ()
140 (let ((changes (diff-hl-changes))
142 (diff-hl-remove-overlays)
144 (goto-char (point-min))
146 (destructuring-bind (line len type) c
147 (when (eq type 'delete)
150 (forward-line (- line current-line))
151 (setq current-line line)
152 (let ((hunk-beg (point)))
154 (let ((o (make-overlay (point) (line-end-position))))
155 (overlay-put o 'diff-hl t)
156 (overlay-put o 'before-string
160 ((not diff-hl-draw-borders) 'empty)
161 ((and (= len 1) (= line current-line)) 'single)
163 ((= line current-line) 'top)
168 (let ((h (make-overlay hunk-beg (point)))
169 (hook '(diff-hl-overlay-modified)))
170 (overlay-put h 'diff-hl t)
171 (overlay-put h 'modification-hooks hook)
172 (overlay-put h 'insert-in-front-hooks hook))))))))
174 (defun diff-hl-remove-overlays ()
175 (dolist (o (overlays-in (point-min) (point-max)))
176 (when (overlay-get o 'diff-hl) (delete-overlay o))))
178 (defun diff-hl-overlay-modified (ov after-p _beg _end &optional _length)
179 ;; Delete the overlay and all our overlays inside it.
182 (narrow-to-region (overlay-start ov) (overlay-end ov))
183 (diff-hl-remove-overlays))
184 (delete-overlay ov)))
186 (defvar diff-hl-timer nil)
188 (defun diff-hl-edit (_beg _end _len)
189 ;; DTRT when we've `undo'-ed the buffer into unmodified state.
190 (when undo-in-progress
192 (cancel-timer diff-hl-timer))
194 (run-with-idle-timer 0.01 nil #'diff-hl-after-undo (current-buffer)))))
196 (defun diff-hl-after-undo (buffer)
197 (with-current-buffer buffer
198 (unless (buffer-modified-p)
201 (defun diff-hl-diff-goto-hunk ()
202 "Run VC diff command and go to the line corresponding to current."
205 (let* ((line (line-number-at-pos))
206 (buffer (current-buffer)))
207 (vc-diff-internal t (vc-deduce-fileset) nil nil t)
208 (vc-exec-after `(if (eobp)
209 (with-current-buffer ,buffer (diff-hl-remove-overlays))
210 (diff-hl-diff-skip-to ,line)))))
212 (defun diff-hl-diff-skip-to (line)
213 "In `diff-mode', skip to the hunk and line corresponding to LINE
214 in the source file, or the last line of the hunk above it."
217 (while (and (looking-at diff-hunk-header-re-unified) (not found))
218 (let ((hunk-line (string-to-number (match-string 3)))
219 (len (let ((m (match-string 4)))
220 (if m (string-to-number m) 1))))
221 (if (> line (+ hunk-line len))
224 (if (< line hunk-line)
225 ;; Retreat to the previous hunk.
227 (let ((to-go (1+ (- line hunk-line))))
230 (unless (looking-at "^-")
231 (decf to-go))))))))))
233 (defun diff-hl-revert-hunk ()
234 "Revert the diff hunk with changes at or above the point."
237 (let ((diff-buffer (generate-new-buffer-name "*diff-hl*"))
238 (buffer (current-buffer))
239 (line (line-number-at-pos))
240 (fileset (vc-deduce-fileset)))
243 (diff-hl-with-diff-switches
244 (vc-diff-internal nil fileset nil nil nil diff-buffer))
248 (with-current-buffer ,buffer (diff-hl-remove-overlays))
249 (error "Buffer is up-to-date"))
250 (diff-hl-diff-skip-to ,line)
253 (unless (yes-or-no-p (format "Revert current hunk in %s?"
255 (error "Revert canceled")))
256 (let ((diff-advance-after-apply-hunk nil))
258 (with-current-buffer ,buffer
260 (message "Hunk reverted"))))
261 (quit-windows-on diff-buffer))))
264 (define-minor-mode diff-hl-mode
265 "Toggle display of VC diff indicators in the left fringe."
266 :lighter "" :keymap `(([remap vc-diff] . diff-hl-diff-goto-hunk)
267 (,(kbd "C-x v n") . diff-hl-revert-hunk))
270 (add-hook 'after-save-hook 'diff-hl-update nil t)
271 (add-hook 'after-change-functions 'diff-hl-edit nil t)
274 (add-hook 'find-file-hook 'diff-hl-update t t))
275 (add-hook 'vc-checkin-hook 'diff-hl-update nil t)
276 (add-hook 'text-scale-mode-hook 'diff-hl-define-bitmaps nil t))
277 (remove-hook 'after-save-hook 'diff-hl-update t)
278 (remove-hook 'after-change-functions 'diff-hl-edit t)
279 (remove-hook 'find-file-hook 'diff-hl-update t)
280 (remove-hook 'vc-checkin-hook 'diff-hl-update t)
281 (remove-hook 'text-scale-mode-hook 'diff-hl-define-bitmaps t)
282 (diff-hl-remove-overlays)))
284 (defun diff-hl-dir-update ()
285 (dolist (pair (vc-dir-marked-only-files-and-states))
286 (when (eq 'up-to-date (cdr pair))
287 (let ((buffer (find-buffer-visiting (car pair))))
289 (with-current-buffer buffer
290 (diff-hl-remove-overlays)))))))
293 (define-minor-mode diff-hl-dir-mode
294 "Toggle `diff-hl-mode' link in `vc-dir-mode' buffer."
297 (add-hook 'vc-checkin-hook 'diff-hl-dir-update t t)
298 (remove-hook 'vc-checkin-hook 'diff-hl-dir-update t)))
300 (defun turn-on-diff-hl-mode ()
301 (when (window-system) ;; No fringes in the console.
303 ;; FIXME: Why is this called twice for each buffer?
304 ;; Isn't fundamental-mode supposed to not run any hooks?
305 ((and buffer-file-name (not (eq major-mode (default-value 'major-mode))))
307 ((eq major-mode 'vc-dir-mode)
308 (diff-hl-dir-mode 1)))))
311 (define-globalized-minor-mode global-diff-hl-mode diff-hl-mode
312 turn-on-diff-hl-mode)
316 ;;; diff-hl.el ends here