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/>.
29 "VC diff fringe highlighting"
32 (defface diff-hl-insert
33 '((t :inherit diff-added))
34 "Face used to highlight inserted lines."
37 (defface diff-hl-delete
38 '((t :inherit diff-removed))
39 "Face used to highlight deleted lines."
42 (defface diff-hl-change
45 (((class color) (min-colors 88) (background light))
46 :background "#ddddff")
47 (((class color) (min-colors 88) (background dark))
48 :background "#333355"))
49 "Face used to highlight changed lines."
52 (defcustom diff-hl-draw-borders t
53 "Non-nil to draw borders around fringe indicators."
57 (defun diff-hl-define-bitmaps ()
58 (let* ((scale (if (and (boundp 'text-scale-mode-amount)
59 (plusp text-scale-mode-amount))
60 (expt text-scale-mode-step text-scale-mode-amount)
62 (h (round (* (frame-char-height) scale)))
63 (w (frame-parameter nil 'left-fringe))
64 (middle (make-vector h (expt 2 (1- w))))
65 (ones (1- (expt 2 w)))
66 (top (copy-sequence middle))
67 (bottom (copy-sequence middle))
68 (single (copy-sequence middle)))
70 (aset bottom (1- h) ones)
72 (aset single (1- h) ones)
73 (define-fringe-bitmap 'diff-hl-bmp-top top h w 'top)
74 (define-fringe-bitmap 'diff-hl-bmp-middle middle h w 'center)
75 (define-fringe-bitmap 'diff-hl-bmp-bottom bottom h w 'bottom)
76 (define-fringe-bitmap 'diff-hl-bmp-single single h w 'top)))
79 (define-fringe-bitmap 'diff-hl-bmp-empty [0] 1 1 'center)
80 (diff-hl-define-bitmaps))
82 (defvar diff-hl-spec-cache (make-hash-table :test 'equal))
84 (defun diff-hl-fringe-spec (type pos)
85 (let* ((key (cons type pos))
86 (val (gethash key diff-hl-spec-cache)))
88 (let* ((face-sym (intern (concat "diff-hl-" (symbol-name type))))
89 (bmp-sym (intern (concat "diff-hl-bmp-" (symbol-name pos)))))
90 (setq val (propertize " " 'display `((left-fringe ,bmp-sym ,face-sym))))
91 (puthash key val diff-hl-spec-cache)))
94 (defun diff-hl-changes ()
95 (let* ((buf-name " *vc-diff-hl* ")
96 (vc-git-diff-switches nil)
97 (vc-hg-diff-switches nil)
98 (vc-diff-switches '("-U0"))
99 (vc-disable-async-diff t)
100 (file (buffer-file-name))
101 (backend (vc-backend file))
104 (vc-call-backend backend 'diff (list file) nil nil buf-name)
105 (with-current-buffer buf-name
106 (goto-char (point-min))
108 (diff-beginning-of-hunk t)
109 (while (looking-at diff-hunk-header-re-unified)
110 (let ((line (string-to-number (match-string 3)))
111 (len (let ((m (match-string 4)))
112 (if m (string-to-number m) 1)))
115 (let* ((inserts (diff-count-matches "^\\+" beg (point)))
116 (deletes (diff-count-matches "^-" beg (point)))
117 (type (cond ((zerop deletes) 'insert)
118 ((zerop inserts) 'delete)
120 (push (list line len type) res)))))))
123 (defun diff-hl-update ()
124 (let ((changes (diff-hl-changes))
126 (diff-hl-remove-overlays)
128 (goto-char (point-min))
130 (destructuring-bind (line len type) c
131 (when (eq type 'delete)
134 (forward-line (- line current-line))
135 (setq current-line line)
137 (let ((o (make-overlay (point) (line-end-position))))
138 (overlay-put o 'diff-hl t)
139 (overlay-put o 'before-string
142 (cond ((not diff-hl-draw-borders) 'empty)
143 ((and (= len 1) (= line current-line)) 'single)
145 ((= line current-line) 'top)
147 (overlay-put o 'modification-hooks '(diff-hl-overlay-modified))
148 (overlay-put o 'insert-in-front-hooks '(diff-hl-overlay-modified)))
153 (defun diff-hl-remove-overlays ()
154 (dolist (o (overlays-in (point-min) (point-max)))
155 (when (overlay-get o 'diff-hl) (delete-overlay o))))
157 (defun diff-hl-overlay-modified (ov after-p _beg _end &optional _length)
158 ;; Do the simplest possible thing, for now.
159 (when after-p (delete-overlay ov)))
161 (defvar diff-hl-timer nil)
163 (defun diff-hl-edit (_beg _end _len)
164 ;; DTRT when we've `undo'-ed the buffer into unmodified state.
165 (when undo-in-progress
167 (cancel-timer diff-hl-timer))
169 (run-with-idle-timer 0.01 nil #'diff-hl-after-undo (current-buffer)))))
171 (defun diff-hl-after-undo (buffer)
172 (with-current-buffer buffer
173 (unless (buffer-modified-p)
176 (defun diff-hl-diff-goto-hunk ()
177 "Open diff buffer and skip to the line corresponding to current."
180 (let* ((line (line-number-at-pos)))
181 (vc-diff-internal t (vc-deduce-fileset) nil nil t)
182 (vc-exec-after `(diff-hl-diff-skip-to ,line))))
184 (defun diff-hl-diff-skip-to (line)
186 (diff-beginning-of-hunk t)
188 (while (and (looking-at diff-hunk-header-re-unified) (not found))
189 (let ((hunk-line (string-to-number (match-string 3)))
190 (len (let ((m (match-string 4)))
191 (if m (string-to-number m) 1))))
192 (if (> line (+ hunk-line len))
195 (let ((to-go (1+ (- line hunk-line))))
198 (unless (looking-at "^-")
199 (decf to-go))))))))))
202 (define-minor-mode diff-hl-mode
203 "Toggle display of VC diff indicators in the left fringe."
204 :lighter "" :keymap '(([remap vc-diff] . diff-hl-diff-goto-hunk))
207 (add-hook 'after-save-hook 'diff-hl-update nil t)
208 (add-hook 'after-change-functions 'diff-hl-edit nil t)
211 (add-hook 'find-file-hook 'diff-hl-update t t))
212 (add-hook 'vc-checkin-hook 'diff-hl-update nil t)
213 (add-hook 'text-scale-mode-hook 'diff-hl-define-bitmaps nil t))
214 (remove-hook 'after-save-hook 'diff-hl-update t)
215 (remove-hook 'after-change-functions 'diff-hl-edit t)
216 (remove-hook 'find-file-hook 'diff-hl-update t)
217 (remove-hook 'vc-checkin-hook 'diff-hl-update t)
218 (remove-hook 'text-scale-mode-hook 'diff-hl-define-bitmaps t)
219 (diff-hl-remove-overlays)))
221 (defun turn-on-diff-hl-mode ()
222 ;; FIXME: Why is this called twice for each buffer?
223 ;; Isn't fundamental-mode supposed to not run any hooks?
224 (and buffer-file-name (not (eq major-mode (default-value 'major-mode)))
225 (window-system) ;; No fringes in the console.
229 (define-globalized-minor-mode global-diff-hl-mode diff-hl-mode
230 turn-on-diff-hl-mode)