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
7 ;; Package-Requires: ((smartrep "0.0.3"))
9 ;; This file is not part of GNU Emacs.
11 ;; This file is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; This file is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;; `diff-hl-mode' provides IDE-like highlighting of `vc-diff' results
27 ;; on the left fringe of the current buffer.
29 ;; For full description, see README.md or the home page.
39 (require 'face-remap))
42 "VC diff fringe highlighting"
45 (defface diff-hl-insert
46 '((t :inherit diff-added))
47 "Face used to highlight inserted lines."
50 (defface diff-hl-delete
51 '((t :inherit diff-removed))
52 "Face used to highlight deleted lines."
55 (defface diff-hl-change
58 (((class color) (min-colors 88) (background light))
59 :background "#ddddff")
60 (((class color) (min-colors 88) (background dark))
61 :background "#333355"))
62 "Face used to highlight changed lines."
65 (defcustom diff-hl-draw-borders t
66 "Non-nil to draw borders around fringe indicators."
70 (defun diff-hl-define-bitmaps ()
71 (let* ((scale (if (and (boundp 'text-scale-mode-amount)
72 (plusp text-scale-mode-amount))
73 (expt text-scale-mode-step text-scale-mode-amount)
75 (h (round (* (frame-char-height) scale)))
76 (w (frame-parameter nil 'left-fringe))
77 (middle (make-vector h (expt 2 (1- w))))
78 (ones (1- (expt 2 w)))
79 (top (copy-sequence middle))
80 (bottom (copy-sequence middle))
81 (single (copy-sequence middle)))
83 (aset bottom (1- h) ones)
85 (aset single (1- h) ones)
86 (define-fringe-bitmap 'diff-hl-bmp-top top h w 'top)
87 (define-fringe-bitmap 'diff-hl-bmp-middle middle h w 'center)
88 (define-fringe-bitmap 'diff-hl-bmp-bottom bottom h w 'bottom)
89 (define-fringe-bitmap 'diff-hl-bmp-single single h w 'top)))
92 (define-fringe-bitmap 'diff-hl-bmp-empty [0] 1 1 'center)
93 (diff-hl-define-bitmaps))
95 (defvar diff-hl-spec-cache (make-hash-table :test 'equal))
97 (defun diff-hl-fringe-spec (type pos)
98 (let* ((key (cons type pos))
99 (val (gethash key diff-hl-spec-cache)))
101 (let* ((face-sym (intern (concat "diff-hl-" (symbol-name type))))
102 (bmp-sym (intern (concat "diff-hl-bmp-" (symbol-name pos)))))
103 (setq val (propertize " " 'display `((left-fringe ,bmp-sym ,face-sym))))
104 (puthash key val diff-hl-spec-cache)))
107 (defmacro diff-hl-with-diff-switches (body)
108 `(let ((vc-git-diff-switches nil)
109 (vc-hg-diff-switches nil)
110 (vc-diff-switches '("-U0"))
111 (vc-disable-async-diff t))
114 (defun diff-hl-changes ()
115 (let* ((buf-name " *diff-hl* ")
116 (file buffer-file-name)
117 (backend (vc-backend file))
119 (when (and backend (not (eq (vc-state file backend) 'up-to-date)))
120 (diff-hl-with-diff-switches
121 (vc-call-backend backend 'diff (list file) nil nil buf-name))
122 (with-current-buffer buf-name
123 (goto-char (point-min))
125 (diff-beginning-of-hunk t)
126 (while (looking-at diff-hunk-header-re-unified)
127 (let ((line (string-to-number (match-string 3)))
128 (len (let ((m (match-string 4)))
129 (if m (string-to-number m) 1)))
132 (let* ((inserts (diff-count-matches "^\\+" beg (point)))
133 (deletes (diff-count-matches "^-" beg (point)))
134 (type (cond ((zerop deletes) 'insert)
135 ((zerop inserts) 'delete)
137 (push (list line len type) res)))))))
140 (defun diff-hl-update ()
141 (let ((changes (diff-hl-changes))
143 (diff-hl-remove-overlays)
145 (goto-char (point-min))
147 (destructuring-bind (line len type) c
148 (when (eq type 'delete)
151 (forward-line (- line current-line))
152 (setq current-line line)
153 (let ((hunk-beg (point)))
155 (let ((o (make-overlay (point) (line-end-position))))
156 (overlay-put o 'diff-hl t)
157 (overlay-put o 'before-string
161 ((not diff-hl-draw-borders) 'empty)
162 ((and (= len 1) (= line current-line)) 'single)
164 ((= line current-line) 'top)
169 (let ((h (make-overlay hunk-beg (point)))
170 (hook '(diff-hl-overlay-modified)))
171 (overlay-put h 'diff-hl t)
172 (overlay-put h 'diff-hl-hunk t)
173 (overlay-put h 'modification-hooks hook)
174 (overlay-put h 'insert-in-front-hooks hook))))))))
176 (defun diff-hl-remove-overlays ()
177 (dolist (o (overlays-in (point-min) (point-max)))
178 (when (overlay-get o 'diff-hl) (delete-overlay o))))
180 (defun diff-hl-overlay-modified (ov after-p _beg _end &optional _length)
181 ;; Delete the overlay and all our overlays inside it.
184 (narrow-to-region (overlay-start ov) (overlay-end ov))
185 (diff-hl-remove-overlays))
186 (delete-overlay ov)))
188 (defvar diff-hl-timer nil)
190 (defun diff-hl-edit (_beg _end _len)
191 ;; DTRT when we've `undo'-ed the buffer into unmodified state.
192 (when undo-in-progress
194 (cancel-timer diff-hl-timer))
196 (run-with-idle-timer 0.01 nil #'diff-hl-after-undo (current-buffer)))))
198 (defun diff-hl-after-undo (buffer)
199 (with-current-buffer buffer
200 (unless (buffer-modified-p)
203 (defun diff-hl-diff-goto-hunk ()
204 "Run VC diff command and go to the line corresponding to current."
207 (let* ((line (line-number-at-pos))
208 (buffer (current-buffer)))
209 (vc-diff-internal t (vc-deduce-fileset) nil nil t)
210 (vc-exec-after `(if (eobp)
211 (with-current-buffer ,buffer (diff-hl-remove-overlays))
212 (diff-hl-diff-skip-to ,line)))))
214 (defun diff-hl-diff-skip-to (line)
215 "In `diff-mode', skip to the hunk and line corresponding to LINE
216 in the source file, or the last line of the hunk above it."
219 (while (and (looking-at diff-hunk-header-re-unified) (not found))
220 (let ((hunk-line (string-to-number (match-string 3)))
221 (len (let ((m (match-string 4)))
222 (if m (string-to-number m) 1))))
223 (if (> line (+ hunk-line len))
226 (if (< line hunk-line)
227 ;; Retreat to the previous hunk.
229 (let ((to-go (1+ (- line hunk-line))))
232 (unless (looking-at "^-")
233 (decf to-go))))))))))
235 (defun diff-hl-revert-hunk ()
236 "Revert the diff hunk with changes at or above the point."
239 (let ((diff-buffer (generate-new-buffer-name "*diff-hl*"))
240 (buffer (current-buffer))
241 (line (save-excursion
242 (unless (diff-hl-hunk-overlay-at (point))
243 (diff-hl-previous-hunk))
244 (line-number-at-pos)))
245 (fileset (vc-deduce-fileset)))
248 (vc-diff-internal nil fileset nil nil nil diff-buffer)
252 (with-current-buffer ,buffer (diff-hl-remove-overlays))
253 (error "Buffer is up-to-date"))
254 (diff-hl-diff-skip-to ,line)
256 (while (looking-at "[-+]") (forward-line 1))
257 (unless (eobp) (diff-split-hunk)))
258 (unless (looking-at "[-+]") (forward-line -1))
259 (while (looking-at "[-+]") (forward-line -1))
260 (unless (looking-at "@")
263 (unless (yes-or-no-p (format "Revert current hunk in %s?"
265 (error "Revert canceled"))
266 (let ((diff-advance-after-apply-hunk nil))
268 (with-current-buffer ,buffer
270 (message "Hunk reverted"))))
271 (quit-windows-on diff-buffer))))
273 (defun diff-hl-hunk-overlay-at (pos)
274 (loop for o in (overlays-at pos)
275 when (overlay-get o 'diff-hl-hunk)
278 (defun diff-hl-next-hunk (&optional backward)
279 "Go to the beginning of the next hunk in the current buffer."
281 (let ((pos (save-excursion
283 (when (and backward (bolp) (not (eobp))) (forward-char))
284 (while (not (if backward (bobp) (eobp)))
285 (goto-char (if backward
286 (1- (previous-overlay-change (point)))
287 (next-overlay-change (point))))
288 (let ((o (diff-hl-hunk-overlay-at (point))))
289 (when (and o (if backward
290 (<= (overlay-end o) (1+ (point)))
291 (>= (overlay-start o) (point))))
292 (throw 'found (overlay-start o)))))))))
295 (error "No further hunks found"))))
297 (defun diff-hl-previous-hunk ()
298 "Go to the beginning of the previous hunk in the current buffer."
300 (diff-hl-next-hunk t))
302 (define-minor-mode diff-hl-mode
303 "Toggle display of VC diff indicators in the left fringe."
304 :lighter "" :keymap `(([remap vc-diff] . diff-hl-diff-goto-hunk)
305 (,(kbd "C-x v n") . diff-hl-revert-hunk)
306 (,(kbd "C-x v [") . diff-hl-previous-hunk)
307 (,(kbd "C-x v ]") . diff-hl-next-hunk))
310 (add-hook 'after-save-hook 'diff-hl-update nil t)
311 (add-hook 'after-change-functions 'diff-hl-edit nil t)
314 (add-hook 'find-file-hook 'diff-hl-update t t))
315 (add-hook 'vc-checkin-hook 'diff-hl-update nil t)
316 (add-hook 'after-revert-hook 'diff-hl-update nil t)
317 (add-hook 'text-scale-mode-hook 'diff-hl-define-bitmaps nil t))
318 (remove-hook 'after-save-hook 'diff-hl-update t)
319 (remove-hook 'after-change-functions 'diff-hl-edit t)
320 (remove-hook 'find-file-hook 'diff-hl-update t)
321 (remove-hook 'vc-checkin-hook 'diff-hl-update t)
322 (remove-hook 'after-revert-hook 'diff-hl-update t)
323 (remove-hook 'text-scale-mode-hook 'diff-hl-define-bitmaps t)
324 (diff-hl-remove-overlays)))
326 (when (require 'smartrep nil t)
328 (dolist (c '("n" "[" "]"))
329 (let* ((cmd (lookup-key diff-hl-mode-map (kbd (concat "C-x v " c)))))
330 (push (cons c cmd) smart-keys)))
331 (smartrep-define-key diff-hl-mode-map "C-x v" smart-keys)))
333 (defun diff-hl-dir-update ()
334 (dolist (pair (if (vc-dir-marked-files)
335 (vc-dir-marked-only-files-and-states)
336 (vc-dir-child-files-and-states)))
337 (when (eq 'up-to-date (cdr pair))
338 (let ((buffer (find-buffer-visiting (car pair))))
340 (with-current-buffer buffer
341 (diff-hl-remove-overlays)))))))
343 (define-minor-mode diff-hl-dir-mode
344 "Toggle `diff-hl-mode' integration in a `vc-dir-mode' buffer."
347 (add-hook 'vc-checkin-hook 'diff-hl-dir-update t t)
348 (remove-hook 'vc-checkin-hook 'diff-hl-dir-update t)))
351 (defun turn-on-diff-hl-mode ()
352 "Turn on `diff-hl-mode' or `diff-hl-dir-mode' in a buffer if appropriate."
353 (when (window-system) ;; No fringes in the console.
357 ((eq major-mode 'vc-dir-mode)
358 (diff-hl-dir-mode 1)))))
360 (defun diff-hl-global-mode-change ()
361 (unless global-diff-hl-mode
362 (dolist (buf (buffer-list))
363 (with-current-buffer buf
364 (when diff-hl-dir-mode
365 (diff-hl-dir-mode -1))))))
368 (define-globalized-minor-mode global-diff-hl-mode diff-hl-mode
369 turn-on-diff-hl-mode :after-hook (diff-hl-global-mode-change))
373 ;;; diff-hl.el ends here