]> code.delx.au - gnu-emacs-elpa/blob - diff-hl.el
Don't overload vc-diff-revert, bind to "C-x v n" instead
[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 (require 'face-remap))
28
29 (defgroup diff-hl nil
30 "VC diff fringe highlighting"
31 :group 'vc)
32
33 (defface diff-hl-insert
34 '((t :inherit diff-added))
35 "Face used to highlight inserted lines."
36 :group 'diff-hl)
37
38 (defface diff-hl-delete
39 '((t :inherit diff-removed))
40 "Face used to highlight deleted lines."
41 :group 'diff-hl)
42
43 (defface diff-hl-change
44 '((default
45 :foreground "blue")
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."
51 :group 'diff-hl)
52
53 (defcustom diff-hl-draw-borders t
54 "Non-nil to draw borders around fringe indicators."
55 :group 'diff-hl
56 :type 'boolean)
57
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)
62 1))
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)))
70 (aset top 0 ones)
71 (aset bottom (1- h) ones)
72 (aset single 0 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)))
78
79 (when (window-system)
80 (define-fringe-bitmap 'diff-hl-bmp-empty [0] 1 1 'center)
81 (diff-hl-define-bitmaps))
82
83 (defvar diff-hl-spec-cache (make-hash-table :test 'equal))
84
85 (defun diff-hl-fringe-spec (type pos)
86 (let* ((key (cons type pos))
87 (val (gethash key diff-hl-spec-cache)))
88 (unless val
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)))
93 val))
94
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))
100 ,body))
101
102 (defun diff-hl-changes ()
103 (let* ((buf-name " *diff-hl* ")
104 (file (buffer-file-name))
105 (backend (vc-backend file))
106 res)
107 (when backend
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))
112 (unless (eobp)
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)))
118 (beg (point)))
119 (diff-end-of-hunk)
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)
124 (t 'change))))
125 (push (list line len type) res)))))))
126 (nreverse res)))
127
128 (defun diff-hl-update ()
129 (let ((changes (diff-hl-changes))
130 (current-line 1))
131 (diff-hl-remove-overlays)
132 (save-excursion
133 (goto-char (point-min))
134 (dolist (c changes)
135 (destructuring-bind (line len type) c
136 (when (eq type 'delete)
137 (setq len 1)
138 (incf line))
139 (forward-line (- line current-line))
140 (setq current-line line)
141 (let ((hunk-beg (point)))
142 (while (plusp len)
143 (let ((o (make-overlay (point) (line-end-position))))
144 (overlay-put o 'diff-hl t)
145 (overlay-put o 'before-string
146 (diff-hl-fringe-spec
147 type
148 (cond
149 ((not diff-hl-draw-borders) 'empty)
150 ((and (= len 1) (= line current-line)) 'single)
151 ((= len 1) 'bottom)
152 ((= line current-line) 'top)
153 (t 'middle)))))
154 (forward-line 1)
155 (incf current-line)
156 (decf len))
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))))))))
162
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))))
166
167 (defun diff-hl-overlay-modified (ov after-p _beg _end &optional _length)
168 ;; Delete the overlay and all our overlays inside it.
169 (unless after-p
170 (save-restriction
171 (narrow-to-region (overlay-start ov) (overlay-end ov))
172 (diff-hl-remove-overlays))
173 (delete-overlay ov)))
174
175 (defvar diff-hl-timer nil)
176
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
180 (when diff-hl-timer
181 (cancel-timer diff-hl-timer))
182 (setq diff-hl-timer
183 (run-with-idle-timer 0.01 nil #'diff-hl-after-undo (current-buffer)))))
184
185 (defun diff-hl-after-undo (buffer)
186 (with-current-buffer buffer
187 (unless (buffer-modified-p)
188 (diff-hl-update))))
189
190 (defun diff-hl-diff-goto-hunk ()
191 "Open diff buffer and skip to the line corresponding to current."
192 (interactive)
193 (vc-buffer-sync)
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)))))
200
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."
204 (diff-hunk-next)
205 (let (found)
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))
211 (diff-hunk-next)
212 (setq found t)
213 (if (< line hunk-line)
214 ;; Retreat to the previous hunk.
215 (forward-line -1)
216 (let ((to-go (1+ (- line hunk-line))))
217 (while (plusp to-go)
218 (forward-line 1)
219 (unless (looking-at "^-")
220 (decf to-go))))))))))
221
222 (defun diff-hl-revert-hunk ()
223 (interactive)
224 (vc-buffer-sync)
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)))
229 (unwind-protect
230 (progn
231 (diff-hl-with-diff-switches
232 (vc-diff-internal nil fileset nil nil nil diff-buffer))
233 (vc-exec-after
234 `(progn
235 (when (eobp)
236 (with-current-buffer ,buffer (diff-hl-remove-overlays))
237 (error "Buffer is up-to-date"))
238 (diff-hl-diff-skip-to ,line)
239 (save-restriction
240 (diff-restrict-view)
241 (unless (yes-or-no-p (format "Revert current hunk in %s?"
242 ,(caadr fileset)))
243 (error "Revert canceled")))
244 (let ((diff-advance-after-apply-hunk nil))
245 (diff-apply-hunk t))
246 (with-current-buffer ,buffer
247 (save-buffer))
248 (message "Hunk reverted"))))
249 (quit-windows-on diff-buffer))))
250
251 ;;;###autoload
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))
256 (if diff-hl-mode
257 (progn
258 (add-hook 'after-save-hook 'diff-hl-update nil t)
259 (add-hook 'after-change-functions 'diff-hl-edit nil t)
260 (if vc-mode
261 (diff-hl-update)
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)))
271
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.
277 (diff-hl-mode 1)))
278
279 ;;;###autoload
280 (define-globalized-minor-mode global-diff-hl-mode diff-hl-mode
281 turn-on-diff-hl-mode)
282
283 (provide 'diff-hl)