1 ;;; diff-hl.el --- VC diff fringe highlighting -*- lexical-binding: t -*-
3 ;; Author: Dmitry Gutov <dgutov@yandex.ru>
6 ;; This file is not part of GNU Emacs.
8 ;; This file is free software: you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation, either version 3 of the License, or
11 ;; (at your option) any later version.
13 ;; This file is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 (require 'face-remap))
30 "VC diff fringe highlighting"
33 (defface diff-hl-insert
34 '((t :inherit diff-added))
35 "Face used to highlight inserted lines."
38 (defface diff-hl-delete
39 '((t :inherit diff-removed))
40 "Face used to highlight deleted lines."
43 (defface diff-hl-change
46 (((class color) (min-colors 88) (background light))
47 :background "#ddddff")
48 (((class color) (min-colors 88) (background dark))
49 :background "#333355"))
50 "Face used to highlight changed lines."
53 (defcustom diff-hl-draw-borders t
54 "Non-nil to draw borders around fringe indicators."
58 (defun diff-hl-define-bitmaps ()
59 (let* ((scale (if (and (boundp 'text-scale-mode-amount)
60 (plusp text-scale-mode-amount))
61 (expt text-scale-mode-step text-scale-mode-amount)
63 (h (round (* (frame-char-height) scale)))
64 (w (frame-parameter nil 'left-fringe))
65 (middle (make-vector h (expt 2 (1- w))))
66 (ones (1- (expt 2 w)))
67 (top (copy-sequence middle))
68 (bottom (copy-sequence middle))
69 (single (copy-sequence middle)))
71 (aset bottom (1- h) ones)
73 (aset single (1- h) ones)
74 (define-fringe-bitmap 'diff-hl-bmp-top top h w 'top)
75 (define-fringe-bitmap 'diff-hl-bmp-middle middle h w 'center)
76 (define-fringe-bitmap 'diff-hl-bmp-bottom bottom h w 'bottom)
77 (define-fringe-bitmap 'diff-hl-bmp-single single h w 'top)))
80 (define-fringe-bitmap 'diff-hl-bmp-empty [0] 1 1 'center)
81 (diff-hl-define-bitmaps))
83 (defvar diff-hl-spec-cache (make-hash-table :test 'equal))
85 (defun diff-hl-fringe-spec (type pos)
86 (let* ((key (cons type pos))
87 (val (gethash key diff-hl-spec-cache)))
89 (let* ((face-sym (intern (concat "diff-hl-" (symbol-name type))))
90 (bmp-sym (intern (concat "diff-hl-bmp-" (symbol-name pos)))))
91 (setq val (propertize " " 'display `((left-fringe ,bmp-sym ,face-sym))))
92 (puthash key val diff-hl-spec-cache)))
95 (defmacro diff-hl-with-diff-switches (body)
96 `(let ((vc-git-diff-switches nil)
97 (vc-hg-diff-switches nil)
98 (vc-diff-switches '("-U0"))
99 (vc-disable-async-diff t))
102 (defun diff-hl-changes ()
103 (let* ((buf-name " *diff-hl* ")
104 (file (buffer-file-name))
105 (backend (vc-backend file))
108 (diff-hl-with-diff-switches
109 (vc-call-backend backend 'diff (list file) nil nil buf-name))
110 (with-current-buffer buf-name
111 (goto-char (point-min))
113 (diff-beginning-of-hunk t)
114 (while (looking-at diff-hunk-header-re-unified)
115 (let ((line (string-to-number (match-string 3)))
116 (len (let ((m (match-string 4)))
117 (if m (string-to-number m) 1)))
120 (let* ((inserts (diff-count-matches "^\\+" beg (point)))
121 (deletes (diff-count-matches "^-" beg (point)))
122 (type (cond ((zerop deletes) 'insert)
123 ((zerop inserts) 'delete)
125 (push (list line len type) res)))))))
128 (defun diff-hl-update ()
129 (let ((changes (diff-hl-changes))
131 (diff-hl-remove-overlays)
133 (goto-char (point-min))
135 (destructuring-bind (line len type) c
136 (when (eq type 'delete)
139 (forward-line (- line current-line))
140 (setq current-line line)
141 (let ((hunk-beg (point)))
143 (let ((o (make-overlay (point) (line-end-position))))
144 (overlay-put o 'diff-hl t)
145 (overlay-put o 'before-string
149 ((not diff-hl-draw-borders) 'empty)
150 ((and (= len 1) (= line current-line)) 'single)
152 ((= line current-line) 'top)
157 (let ((h (make-overlay hunk-beg (point)))
158 (hook '(diff-hl-overlay-modified)))
159 (overlay-put h 'diff-hl t)
160 (overlay-put h 'modification-hooks hook)
161 (overlay-put h 'insert-in-front-hooks hook))))))))
163 (defun diff-hl-remove-overlays ()
164 (dolist (o (overlays-in (point-min) (point-max)))
165 (when (overlay-get o 'diff-hl) (delete-overlay o))))
167 (defun diff-hl-overlay-modified (ov after-p _beg _end &optional _length)
168 ;; Delete the overlay and all our overlays inside it.
171 (narrow-to-region (overlay-start ov) (overlay-end ov))
172 (diff-hl-remove-overlays))
173 (delete-overlay ov)))
175 (defvar diff-hl-timer nil)
177 (defun diff-hl-edit (_beg _end _len)
178 ;; DTRT when we've `undo'-ed the buffer into unmodified state.
179 (when undo-in-progress
181 (cancel-timer diff-hl-timer))
183 (run-with-idle-timer 0.01 nil #'diff-hl-after-undo (current-buffer)))))
185 (defun diff-hl-after-undo (buffer)
186 (with-current-buffer buffer
187 (unless (buffer-modified-p)
190 (defun diff-hl-diff-goto-hunk ()
191 "Open diff buffer and skip to the line corresponding to current."
194 (let* ((line (line-number-at-pos))
195 (buffer (current-buffer)))
196 (vc-diff-internal t (vc-deduce-fileset) nil nil t)
197 (vc-exec-after `(if (eobp)
198 (with-current-buffer ,buffer (diff-hl-remove-overlays))
199 (diff-hl-diff-skip-to ,line)))))
201 (defun diff-hl-diff-skip-to (line)
202 "In `diff-mode', skip to the hunk and line corresponding to LINE
203 in the source file, or the last line of the hunk above it."
206 (while (and (looking-at diff-hunk-header-re-unified) (not found))
207 (let ((hunk-line (string-to-number (match-string 3)))
208 (len (let ((m (match-string 4)))
209 (if m (string-to-number m) 1))))
210 (if (> line (+ hunk-line len))
213 (if (< line hunk-line)
214 ;; Retreat to the previous hunk.
216 (let ((to-go (1+ (- line hunk-line))))
219 (unless (looking-at "^-")
220 (decf to-go))))))))))
222 (defun diff-hl-revert-hunk ()
225 (let ((diff-buffer (generate-new-buffer-name "*diff-hl*"))
226 (buffer (current-buffer))
227 (line (line-number-at-pos))
228 (fileset (vc-deduce-fileset)))
231 (diff-hl-with-diff-switches
232 (vc-diff-internal nil fileset nil nil nil diff-buffer))
236 (with-current-buffer ,buffer (diff-hl-remove-overlays))
237 (error "Buffer is up-to-date"))
238 (diff-hl-diff-skip-to ,line)
241 (unless (yes-or-no-p (format "Revert current hunk in %s?"
243 (error "Revert canceled")))
244 (let ((diff-advance-after-apply-hunk nil))
246 (with-current-buffer ,buffer
248 (message "Hunk reverted"))))
249 (quit-windows-on diff-buffer))))
252 (define-minor-mode diff-hl-mode
253 "Toggle display of VC diff indicators in the left fringe."
254 :lighter "" :keymap `(([remap vc-diff] . diff-hl-diff-goto-hunk)
255 (,(kbd "C-x v n") . diff-hl-revert-hunk))
258 (add-hook 'after-save-hook 'diff-hl-update nil t)
259 (add-hook 'after-change-functions 'diff-hl-edit nil t)
262 (add-hook 'find-file-hook 'diff-hl-update t t))
263 (add-hook 'vc-checkin-hook 'diff-hl-update nil t)
264 (add-hook 'text-scale-mode-hook 'diff-hl-define-bitmaps nil t))
265 (remove-hook 'after-save-hook 'diff-hl-update t)
266 (remove-hook 'after-change-functions 'diff-hl-edit t)
267 (remove-hook 'find-file-hook 'diff-hl-update t)
268 (remove-hook 'vc-checkin-hook 'diff-hl-update t)
269 (remove-hook 'text-scale-mode-hook 'diff-hl-define-bitmaps t)
270 (diff-hl-remove-overlays)))
272 (defun turn-on-diff-hl-mode ()
273 ;; FIXME: Why is this called twice for each buffer?
274 ;; Isn't fundamental-mode supposed to not run any hooks?
275 (and buffer-file-name (not (eq major-mode (default-value 'major-mode)))
276 (window-system) ;; No fringes in the console.
280 (define-globalized-minor-mode global-diff-hl-mode diff-hl-mode
281 turn-on-diff-hl-mode)