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
43 '((((class color) (min-colors 88) (background light))
44 :background "#ddddff")
45 (((class color) (min-colors 88) (background dark))
46 :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."
58 (define-fringe-bitmap 'diff-hl-bmp-empty [0] 1 1 'center)
59 (let* ((h (frame-char-height))
60 (w (frame-parameter nil 'left-fringe))
61 (middle (make-vector h (expt 2 (1- w))))
62 (ones (1- (expt 2 w)))
63 (top (copy-sequence middle))
64 (bottom (copy-sequence middle))
65 (single (copy-sequence middle)))
67 (aset bottom (1- h) ones)
69 (aset single (1- h) ones)
70 (define-fringe-bitmap 'diff-hl-bmp-top top h 8 'top)
71 (define-fringe-bitmap 'diff-hl-bmp-middle middle h 8 'center)
72 (define-fringe-bitmap 'diff-hl-bmp-bottom bottom h 8 'bottom)
73 (define-fringe-bitmap 'diff-hl-bmp-single single h 8 'center)))
75 (defvar diff-hl-spec-cache (make-hash-table :test 'equal))
77 (defun diff-hl-fringe-spec (type pos)
78 (let* ((key (cons type pos))
79 (val (gethash key diff-hl-spec-cache)))
81 (let* ((face-sym (intern (concat "diff-hl-" (symbol-name type))))
82 (bmp-sym (intern (concat "diff-hl-bmp-" (symbol-name pos)))))
83 (setq val (propertize " " 'display `((left-fringe ,bmp-sym ,face-sym))))
84 (puthash key val diff-hl-spec-cache)))
87 (defun diff-hl-changes ()
88 (let* ((buf-name " *vc-diff-hl* ")
89 (vc-git-diff-switches nil)
90 (vc-hg-diff-switches nil)
91 (vc-diff-switches '("-U0"))
92 (vc-disable-async-diff t)
93 (file (buffer-file-name))
94 (backend (vc-backend file))
97 (vc-call-backend backend 'diff (list file) nil nil buf-name)
98 (with-current-buffer buf-name
99 (goto-char (point-min))
101 (diff-beginning-of-hunk t)
102 (while (looking-at diff-hunk-header-re-unified)
103 (let ((line (string-to-number (match-string 3)))
104 (len (let ((m (match-string 4)))
105 (if m (string-to-number m) 1)))
108 (let* ((inserts (diff-count-matches "^\\+" beg (point)))
109 (deletes (diff-count-matches "^-" beg (point)))
110 (type (cond ((zerop deletes) 'insert)
111 ((zerop inserts) 'delete)
113 (push (list line len type) res)))))))
116 (defun diff-hl-update ()
117 (let ((changes (diff-hl-changes))
119 (diff-hl-remove-overlays)
121 (goto-char (point-min))
123 (destructuring-bind (line len type) c
124 (when (eq type 'delete)
127 (forward-line (- line current-line))
128 (setq current-line line)
130 (let ((o (make-overlay (point) (line-end-position))))
131 (overlay-put o 'diff-hl t)
132 (overlay-put o 'before-string
135 (cond ((not diff-hl-draw-borders) 'empty)
136 ((and (= len 1) (= line current-line)) 'single)
138 ((= line current-line) 'top)
140 (overlay-put o 'modification-hooks '(diff-hl-overlay-modified))
141 (overlay-put o 'insert-in-front-hooks '(diff-hl-overlay-modified)))
146 (defun diff-hl-remove-overlays ()
147 (dolist (o (overlays-in (point-min) (point-max)))
148 (when (overlay-get o 'diff-hl) (delete-overlay o))))
150 (defun diff-hl-overlay-modified (ov after-p _beg _end &optional _length)
151 ;; Do the simplest possible thing, for now.
152 (when after-p (delete-overlay ov)))
154 (defvar diff-hl-timer nil)
156 (defun diff-hl-edit (_beg _end _len)
157 ;; DTRT when we've `undo'-ed the buffer into unmodified state.
158 (when undo-in-progress
160 (cancel-timer diff-hl-timer))
162 (run-with-idle-timer 0.01 nil #'diff-hl-after-undo (current-buffer)))))
164 (defun diff-hl-after-undo (buffer)
165 (with-current-buffer buffer
166 (unless (buffer-modified-p)
170 (define-minor-mode diff-hl-mode
171 "Toggle display of VC diff indicators in the left fringe."
175 (add-hook 'after-save-hook 'diff-hl-update nil t)
176 (add-hook 'after-change-functions 'diff-hl-edit nil t)
179 (add-hook 'find-file-hook 'diff-hl-update t t)))
180 (remove-hook 'after-save-hook 'diff-hl-update t)
181 (remove-hook 'after-change-functions 'diff-hl-edit t)
182 (remove-hook 'find-file-hook 'diff-hl-update t)
183 (diff-hl-remove-overlays)))
185 (defun turn-on-diff-hl-mode ()
186 ;; FIXME: Why is this called twice for each buffer?
187 ;; Isn't fundamental-mode supposed to not run any hooks?
188 (and buffer-file-name (not (eq major-mode (default-value 'major-mode)))
189 (window-system) ;; No fringes in the console.
193 (define-globalized-minor-mode global-diff-hl-mode diff-hl-mode
194 turn-on-diff-hl-mode)