]> code.delx.au - gnu-emacs-elpa/blob - diff-hl.el
Draw borders around fringe indicators
[gnu-emacs-elpa] / diff-hl.el
1 ;;; diff-hl.el --- VC diff fringe highlighting -*- lexical-binding: t -*-
2
3 ;; Author: Dmitry Gutov <dgutov@yandex.ru>
4 ;; Keywords: vc, diff
5
6 ;; This file is not part of GNU Emacs.
7
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.
12
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.
17
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/>.
20
21 (require 'diff-mode)
22 (require 'vc)
23 (eval-when-compile
24 (require 'cl)
25 (require 'vc-git)
26 (require 'vc-hg))
27
28 (defgroup diff-hl nil
29 "VC diff fringe highlighting"
30 :group 'vc)
31
32 (defface diff-hl-insert
33 '((t :inherit diff-added))
34 "Face used to highlight inserted lines."
35 :group 'diff-hl)
36
37 (defface diff-hl-delete
38 '((t :inherit diff-removed))
39 "Face used to highlight deleted lines."
40 :group 'diff-hl)
41
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")
47 (((class color))
48 :foreground "blue"))
49 "Face used to highlight changed lines."
50 :group 'diff-hl)
51
52 (defcustom diff-hl-draw-borders t
53 "Non-nil to draw borders around fringe indicators."
54 :group 'diff-hl
55 :type 'boolean)
56
57 (when (window-system)
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)))
66 (aset top 0 ones)
67 (aset bottom (1- h) ones)
68 (aset single 0 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)))
74
75 (defvar diff-hl-spec-cache (make-hash-table :test 'equal))
76
77 (defun diff-hl-fringe-spec (type pos)
78 (let* ((key (cons type pos))
79 (val (gethash key diff-hl-spec-cache)))
80 (unless val
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)))
85 val))
86
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))
95 res)
96 (when backend
97 (vc-call-backend backend 'diff (list file) nil nil buf-name)
98 (with-current-buffer buf-name
99 (goto-char (point-min))
100 (unless (eobp)
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)))
106 (beg (point)))
107 (diff-end-of-hunk)
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)
112 (t 'change))))
113 (push (list line len type) res)))))))
114 (nreverse res)))
115
116 (defun diff-hl-update ()
117 (let ((changes (diff-hl-changes))
118 (current-line 1))
119 (diff-hl-remove-overlays)
120 (save-excursion
121 (goto-char (point-min))
122 (dolist (c changes)
123 (destructuring-bind (line len type) c
124 (when (eq type 'delete)
125 (setq len 1)
126 (incf line))
127 (forward-line (- line current-line))
128 (setq current-line line)
129 (while (plusp len)
130 (let ((o (make-overlay (point) (line-end-position))))
131 (overlay-put o 'diff-hl t)
132 (overlay-put o 'before-string
133 (diff-hl-fringe-spec
134 type
135 (cond ((not diff-hl-draw-borders) 'empty)
136 ((and (= len 1) (= line current-line)) 'single)
137 ((= len 1) 'bottom)
138 ((= line current-line) 'top)
139 (t 'middle))))
140 (overlay-put o 'modification-hooks '(diff-hl-overlay-modified))
141 (overlay-put o 'insert-in-front-hooks '(diff-hl-overlay-modified)))
142 (forward-line 1)
143 (incf current-line)
144 (decf len)))))))
145
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))))
149
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)))
153
154 (defvar diff-hl-timer nil)
155
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
159 (when diff-hl-timer
160 (cancel-timer diff-hl-timer))
161 (setq diff-hl-timer
162 (run-with-idle-timer 0.01 nil #'diff-hl-after-undo (current-buffer)))))
163
164 (defun diff-hl-after-undo (buffer)
165 (with-current-buffer buffer
166 (unless (buffer-modified-p)
167 (diff-hl-update))))
168
169 ;;;###autoload
170 (define-minor-mode diff-hl-mode
171 "Toggle display of VC diff indicators in the left fringe."
172 :lighter ""
173 (if diff-hl-mode
174 (progn
175 (add-hook 'after-save-hook 'diff-hl-update nil t)
176 (add-hook 'after-change-functions 'diff-hl-edit nil t)
177 (if vc-mode
178 (diff-hl-update)
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)))
184
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.
190 (diff-hl-mode 1)))
191
192 ;;;###autoload
193 (define-globalized-minor-mode global-diff-hl-mode diff-hl-mode
194 turn-on-diff-hl-mode)
195
196 (provide 'diff-hl)