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 (defcustom diff-hl-revert-current-hunk t
59 "Non-nil to only revert current hunk by default.
60 `diff-hl-revert' will do the opposite when called with prefix argument."
64 (defun diff-hl-define-bitmaps ()
65 (let* ((scale (if (and (boundp 'text-scale-mode-amount)
66 (plusp text-scale-mode-amount))
67 (expt text-scale-mode-step text-scale-mode-amount)
69 (h (round (* (frame-char-height) scale)))
70 (w (frame-parameter nil 'left-fringe))
71 (middle (make-vector h (expt 2 (1- w))))
72 (ones (1- (expt 2 w)))
73 (top (copy-sequence middle))
74 (bottom (copy-sequence middle))
75 (single (copy-sequence middle)))
77 (aset bottom (1- h) ones)
79 (aset single (1- h) ones)
80 (define-fringe-bitmap 'diff-hl-bmp-top top h w 'top)
81 (define-fringe-bitmap 'diff-hl-bmp-middle middle h w 'center)
82 (define-fringe-bitmap 'diff-hl-bmp-bottom bottom h w 'bottom)
83 (define-fringe-bitmap 'diff-hl-bmp-single single h w 'top)))
86 (define-fringe-bitmap 'diff-hl-bmp-empty [0] 1 1 'center)
87 (diff-hl-define-bitmaps))
89 (defvar diff-hl-spec-cache (make-hash-table :test 'equal))
91 (defun diff-hl-fringe-spec (type pos)
92 (let* ((key (cons type pos))
93 (val (gethash key diff-hl-spec-cache)))
95 (let* ((face-sym (intern (concat "diff-hl-" (symbol-name type))))
96 (bmp-sym (intern (concat "diff-hl-bmp-" (symbol-name pos)))))
97 (setq val (propertize " " 'display `((left-fringe ,bmp-sym ,face-sym))))
98 (puthash key val diff-hl-spec-cache)))
101 (defun diff-hl-changes ()
102 (let* ((buf-name " *vc-diff-hl* ")
103 (vc-git-diff-switches nil)
104 (vc-hg-diff-switches nil)
105 (vc-diff-switches '("-U0"))
106 (vc-disable-async-diff t)
107 (file (buffer-file-name))
108 (backend (vc-backend file))
111 (vc-call-backend backend 'diff (list file) nil nil buf-name)
112 (with-current-buffer buf-name
113 (goto-char (point-min))
115 (diff-beginning-of-hunk t)
116 (while (looking-at diff-hunk-header-re-unified)
117 (let ((line (string-to-number (match-string 3)))
118 (len (let ((m (match-string 4)))
119 (if m (string-to-number m) 1)))
122 (let* ((inserts (diff-count-matches "^\\+" beg (point)))
123 (deletes (diff-count-matches "^-" beg (point)))
124 (type (cond ((zerop deletes) 'insert)
125 ((zerop inserts) 'delete)
127 (push (list line len type) res)))))))
130 (defun diff-hl-update ()
131 (let ((changes (diff-hl-changes))
133 (diff-hl-remove-overlays)
135 (goto-char (point-min))
137 (destructuring-bind (line len type) c
138 (when (eq type 'delete)
141 (forward-line (- line current-line))
142 (setq current-line line)
143 (let ((hunk-beg (point)))
145 (let ((o (make-overlay (point) (line-end-position))))
146 (overlay-put o 'diff-hl t)
147 (overlay-put o 'before-string
151 ((not diff-hl-draw-borders) 'empty)
152 ((and (= len 1) (= line current-line)) 'single)
154 ((= line current-line) 'top)
159 (let ((h (make-overlay hunk-beg (point)))
160 (hook '(diff-hl-overlay-modified)))
161 (overlay-put h 'diff-hl t)
162 (overlay-put h 'modification-hooks hook)
163 (overlay-put h 'insert-in-front-hooks hook))))))))
165 (defun diff-hl-remove-overlays ()
166 (dolist (o (overlays-in (point-min) (point-max)))
167 (when (overlay-get o 'diff-hl) (delete-overlay o))))
169 (defun diff-hl-overlay-modified (ov after-p _beg _end &optional _length)
170 ;; Delete the overlay and all our overlays inside it.
173 (narrow-to-region (overlay-start ov) (overlay-end ov))
174 (diff-hl-remove-overlays))
175 (delete-overlay ov)))
177 (defvar diff-hl-timer nil)
179 (defun diff-hl-edit (_beg _end _len)
180 ;; DTRT when we've `undo'-ed the buffer into unmodified state.
181 (when undo-in-progress
183 (cancel-timer diff-hl-timer))
185 (run-with-idle-timer 0.01 nil #'diff-hl-after-undo (current-buffer)))))
187 (defun diff-hl-after-undo (buffer)
188 (with-current-buffer buffer
189 (unless (buffer-modified-p)
192 (defun diff-hl-diff-goto-hunk ()
193 "Open diff buffer and skip to the line corresponding to current."
196 (let* ((line (line-number-at-pos)))
197 (vc-diff-internal t (vc-deduce-fileset) nil nil t)
198 (vc-exec-after `(diff-hl-diff-skip-to ,line))))
200 (defun diff-hl-diff-skip-to (line)
204 (while (and (looking-at diff-hunk-header-re-unified) (not found))
205 (let ((hunk-line (string-to-number (match-string 3)))
206 (len (let ((m (match-string 4)))
207 (if m (string-to-number m) 1))))
208 (if (> line (+ hunk-line len))
211 (let ((to-go (1+ (- line hunk-line))))
214 (unless (looking-at "^-")
215 (decf to-go))))))))))
217 (defun diff-hl-revert (arg)
219 (if (not (diff-xor arg diff-hl-revert-current-hunk))
222 (let ((diff-buffer (generate-new-buffer-name "*diff-hl*"))
223 (buffer (current-buffer))
224 (line (line-number-at-pos))
225 (fileset (vc-deduce-fileset)))
228 (vc-diff-internal nil fileset nil nil nil diff-buffer)
232 (with-current-buffer ,buffer (diff-hl-remove-overlays))
233 (error "Buffer is up-to-date"))
234 (diff-hl-diff-skip-to ,line)
237 (unless (yes-or-no-p (format "Revert current hunk in %s?"
239 (error "Revert canceled")))
240 (let ((diff-advance-after-apply-hunk nil))
241 (diff-apply-hunk t)))))
242 (quit-windows-on diff-buffer)
244 (message "Hunk reverted")))))
247 (define-minor-mode diff-hl-mode
248 "Toggle display of VC diff indicators in the left fringe."
249 :lighter "" :keymap '(([remap vc-diff] . diff-hl-diff-goto-hunk)
250 ([remap vc-revert] . diff-hl-revert))
253 (add-hook 'after-save-hook 'diff-hl-update nil t)
254 (add-hook 'after-change-functions 'diff-hl-edit nil t)
257 (add-hook 'find-file-hook 'diff-hl-update t t))
258 (add-hook 'vc-checkin-hook 'diff-hl-update nil t)
259 (add-hook 'text-scale-mode-hook 'diff-hl-define-bitmaps nil t))
260 (remove-hook 'after-save-hook 'diff-hl-update t)
261 (remove-hook 'after-change-functions 'diff-hl-edit t)
262 (remove-hook 'find-file-hook 'diff-hl-update t)
263 (remove-hook 'vc-checkin-hook 'diff-hl-update t)
264 (remove-hook 'text-scale-mode-hook 'diff-hl-define-bitmaps t)
265 (diff-hl-remove-overlays)))
267 (defun turn-on-diff-hl-mode ()
268 ;; FIXME: Why is this called twice for each buffer?
269 ;; Isn't fundamental-mode supposed to not run any hooks?
270 (and buffer-file-name (not (eq major-mode (default-value 'major-mode)))
271 (window-system) ;; No fringes in the console.
275 (define-globalized-minor-mode global-diff-hl-mode diff-hl-mode
276 turn-on-diff-hl-mode)