]> code.delx.au - gnu-emacs-elpa/blob - diff-hl.el
bb488dc19bf9c8efa55fda2de1ff9de503849881
[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 ;; URL: https://github.com/dgutov/diff-hl
5 ;; Keywords: vc, diff
6 ;; Version: 1.2
7 ;; Package-Requires: ((smartrep "0.0.3"))
8
9 ;; This file is not part of GNU Emacs.
10
11 ;; This file is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
15
16 ;; This file is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23
24 ;;; Commentary:
25
26 ;; `diff-hl-mode' provides IDE-like highlighting of `vc-diff' results
27 ;; on the left fringe of the current buffer.
28 ;;
29 ;; For full description, see README.md or the home page.
30
31 ;;; Code:
32
33 (require 'diff-mode)
34 (require 'vc)
35 (eval-when-compile
36 (require 'cl)
37 (require 'vc-git)
38 (require 'vc-hg)
39 (require 'face-remap))
40
41 (defgroup diff-hl nil
42 "VC diff fringe highlighting"
43 :group 'vc)
44
45 (defface diff-hl-insert
46 '((t :inherit diff-added))
47 "Face used to highlight inserted lines."
48 :group 'diff-hl)
49
50 (defface diff-hl-delete
51 '((t :inherit diff-removed))
52 "Face used to highlight deleted lines."
53 :group 'diff-hl)
54
55 (defface diff-hl-change
56 '((default
57 :foreground "blue")
58 (((class color) (min-colors 88) (background light))
59 :background "#ddddff")
60 (((class color) (min-colors 88) (background dark))
61 :background "#333355"))
62 "Face used to highlight changed lines."
63 :group 'diff-hl)
64
65 (defcustom diff-hl-draw-borders t
66 "Non-nil to draw borders around fringe indicators."
67 :group 'diff-hl
68 :type 'boolean)
69
70 (defun diff-hl-define-bitmaps ()
71 (let* ((scale (if (and (boundp 'text-scale-mode-amount)
72 (plusp text-scale-mode-amount))
73 (expt text-scale-mode-step text-scale-mode-amount)
74 1))
75 (h (round (* (frame-char-height) scale)))
76 (w (frame-parameter nil 'left-fringe))
77 (middle (make-vector h (expt 2 (1- w))))
78 (ones (1- (expt 2 w)))
79 (top (copy-sequence middle))
80 (bottom (copy-sequence middle))
81 (single (copy-sequence middle)))
82 (aset top 0 ones)
83 (aset bottom (1- h) ones)
84 (aset single 0 ones)
85 (aset single (1- h) ones)
86 (define-fringe-bitmap 'diff-hl-bmp-top top h w 'top)
87 (define-fringe-bitmap 'diff-hl-bmp-middle middle h w 'center)
88 (define-fringe-bitmap 'diff-hl-bmp-bottom bottom h w 'bottom)
89 (define-fringe-bitmap 'diff-hl-bmp-single single h w 'top)))
90
91 (when (window-system)
92 (define-fringe-bitmap 'diff-hl-bmp-empty [0] 1 1 'center)
93 (diff-hl-define-bitmaps))
94
95 (defvar diff-hl-spec-cache (make-hash-table :test 'equal))
96
97 (defun diff-hl-fringe-spec (type pos)
98 (let* ((key (cons type pos))
99 (val (gethash key diff-hl-spec-cache)))
100 (unless val
101 (let* ((face-sym (intern (concat "diff-hl-" (symbol-name type))))
102 (bmp-sym (intern (concat "diff-hl-bmp-" (symbol-name pos)))))
103 (setq val (propertize " " 'display `((left-fringe ,bmp-sym ,face-sym))))
104 (puthash key val diff-hl-spec-cache)))
105 val))
106
107 (defmacro diff-hl-with-diff-switches (body)
108 `(let ((vc-git-diff-switches nil)
109 (vc-hg-diff-switches nil)
110 (vc-diff-switches '("-U0"))
111 (vc-disable-async-diff t))
112 ,body))
113
114 (defun diff-hl-changes ()
115 (let* ((buf-name " *diff-hl* ")
116 (file buffer-file-name)
117 (backend (vc-backend file))
118 res)
119 (when (and backend (not (eq (vc-state file backend) 'up-to-date)))
120 (diff-hl-with-diff-switches
121 (vc-call-backend backend 'diff (list file) nil nil buf-name))
122 (with-current-buffer buf-name
123 (goto-char (point-min))
124 (unless (eobp)
125 (diff-beginning-of-hunk t)
126 (while (looking-at diff-hunk-header-re-unified)
127 (let ((line (string-to-number (match-string 3)))
128 (len (let ((m (match-string 4)))
129 (if m (string-to-number m) 1)))
130 (beg (point)))
131 (diff-end-of-hunk)
132 (let* ((inserts (diff-count-matches "^\\+" beg (point)))
133 (deletes (diff-count-matches "^-" beg (point)))
134 (type (cond ((zerop deletes) 'insert)
135 ((zerop inserts) 'delete)
136 (t 'change))))
137 (push (list line len type) res)))))))
138 (nreverse res)))
139
140 (defun diff-hl-update ()
141 (let ((changes (diff-hl-changes))
142 (current-line 1))
143 (diff-hl-remove-overlays)
144 (save-excursion
145 (goto-char (point-min))
146 (dolist (c changes)
147 (destructuring-bind (line len type) c
148 (when (eq type 'delete)
149 (setq len 1)
150 (incf line))
151 (forward-line (- line current-line))
152 (setq current-line line)
153 (let ((hunk-beg (point)))
154 (while (plusp len)
155 (let ((o (make-overlay (point) (line-end-position))))
156 (overlay-put o 'diff-hl t)
157 (overlay-put o 'before-string
158 (diff-hl-fringe-spec
159 type
160 (cond
161 ((not diff-hl-draw-borders) 'empty)
162 ((and (= len 1) (= line current-line)) 'single)
163 ((= len 1) 'bottom)
164 ((= line current-line) 'top)
165 (t 'middle)))))
166 (forward-line 1)
167 (incf current-line)
168 (decf len))
169 (let ((h (make-overlay hunk-beg (point)))
170 (hook '(diff-hl-overlay-modified)))
171 (overlay-put h 'diff-hl t)
172 (overlay-put h 'diff-hl-hunk t)
173 (overlay-put h 'modification-hooks hook)
174 (overlay-put h 'insert-in-front-hooks hook))))))))
175
176 (defun diff-hl-remove-overlays ()
177 (dolist (o (overlays-in (point-min) (point-max)))
178 (when (overlay-get o 'diff-hl) (delete-overlay o))))
179
180 (defun diff-hl-overlay-modified (ov after-p _beg _end &optional _length)
181 ;; Delete the overlay and all our overlays inside it.
182 (unless after-p
183 (save-restriction
184 (narrow-to-region (overlay-start ov) (overlay-end ov))
185 (diff-hl-remove-overlays))
186 (delete-overlay ov)))
187
188 (defvar diff-hl-timer nil)
189
190 (defun diff-hl-edit (_beg _end _len)
191 ;; DTRT when we've `undo'-ed the buffer into unmodified state.
192 (when undo-in-progress
193 (when diff-hl-timer
194 (cancel-timer diff-hl-timer))
195 (setq diff-hl-timer
196 (run-with-idle-timer 0.01 nil #'diff-hl-after-undo (current-buffer)))))
197
198 (defun diff-hl-after-undo (buffer)
199 (with-current-buffer buffer
200 (unless (buffer-modified-p)
201 (diff-hl-update))))
202
203 (defun diff-hl-diff-goto-hunk ()
204 "Run VC diff command and go to the line corresponding to current."
205 (interactive)
206 (vc-buffer-sync)
207 (let* ((line (line-number-at-pos))
208 (buffer (current-buffer)))
209 (vc-diff-internal t (vc-deduce-fileset) nil nil t)
210 (vc-exec-after `(if (eobp)
211 (with-current-buffer ,buffer (diff-hl-remove-overlays))
212 (diff-hl-diff-skip-to ,line)))))
213
214 (defun diff-hl-diff-skip-to (line)
215 "In `diff-mode', skip to the hunk and line corresponding to LINE
216 in the source file, or the last line of the hunk above it."
217 (diff-hunk-next)
218 (let (found)
219 (while (and (looking-at diff-hunk-header-re-unified) (not found))
220 (let ((hunk-line (string-to-number (match-string 3)))
221 (len (let ((m (match-string 4)))
222 (if m (string-to-number m) 1))))
223 (if (> line (+ hunk-line len))
224 (diff-hunk-next)
225 (setq found t)
226 (if (< line hunk-line)
227 ;; Retreat to the previous hunk.
228 (forward-line -1)
229 (let ((to-go (1+ (- line hunk-line))))
230 (while (plusp to-go)
231 (forward-line 1)
232 (unless (looking-at "^-")
233 (decf to-go))))))))))
234
235 (defun diff-hl-revert-hunk ()
236 "Revert the diff hunk with changes at or above the point."
237 (interactive)
238 (vc-buffer-sync)
239 (let ((diff-buffer (generate-new-buffer-name "*diff-hl*"))
240 (buffer (current-buffer))
241 (line (save-excursion
242 (unless (diff-hl-hunk-overlay-at (point))
243 (diff-hl-previous-hunk))
244 (line-number-at-pos)))
245 (fileset (vc-deduce-fileset)))
246 (unwind-protect
247 (progn
248 (vc-diff-internal nil fileset nil nil nil diff-buffer)
249 (vc-exec-after
250 `(progn
251 (when (eobp)
252 (with-current-buffer ,buffer (diff-hl-remove-overlays))
253 (error "Buffer is up-to-date"))
254 (diff-hl-diff-skip-to ,line)
255 (save-excursion
256 (while (looking-at "[-+]") (forward-line 1))
257 (unless (eobp) (diff-split-hunk)))
258 (unless (looking-at "[-+]") (forward-line -1))
259 (while (looking-at "[-+]") (forward-line -1))
260 (unless (looking-at "@")
261 (forward-line 1)
262 (diff-split-hunk))
263 (unless (yes-or-no-p (format "Revert current hunk in %s?"
264 ,(caadr fileset)))
265 (error "Revert canceled"))
266 (let ((diff-advance-after-apply-hunk nil))
267 (diff-apply-hunk t))
268 (with-current-buffer ,buffer
269 (save-buffer))
270 (message "Hunk reverted"))))
271 (quit-windows-on diff-buffer))))
272
273 (defun diff-hl-hunk-overlay-at (pos)
274 (loop for o in (overlays-at pos)
275 when (overlay-get o 'diff-hl-hunk)
276 return o))
277
278 (defun diff-hl-next-hunk (&optional backward)
279 "Go to the beginning of the next hunk in the current buffer."
280 (interactive)
281 (let ((pos (save-excursion
282 (catch 'found
283 (when (and backward (bolp) (not (eobp))) (forward-char))
284 (while (not (if backward (bobp) (eobp)))
285 (goto-char (if backward
286 (1- (previous-overlay-change (point)))
287 (next-overlay-change (point))))
288 (let ((o (diff-hl-hunk-overlay-at (point))))
289 (when (and o (if backward
290 (<= (overlay-end o) (1+ (point)))
291 (>= (overlay-start o) (point))))
292 (throw 'found (overlay-start o)))))))))
293 (if pos
294 (goto-char pos)
295 (error "No further hunks found"))))
296
297 (defun diff-hl-previous-hunk ()
298 "Go to the beginning of the previous hunk in the current buffer."
299 (interactive)
300 (diff-hl-next-hunk t))
301
302 (define-minor-mode diff-hl-mode
303 "Toggle display of VC diff indicators in the left fringe."
304 :lighter "" :keymap `(([remap vc-diff] . diff-hl-diff-goto-hunk)
305 (,(kbd "C-x v n") . diff-hl-revert-hunk)
306 (,(kbd "C-x v [") . diff-hl-previous-hunk)
307 (,(kbd "C-x v ]") . diff-hl-next-hunk))
308 (if diff-hl-mode
309 (progn
310 (add-hook 'after-save-hook 'diff-hl-update nil t)
311 (add-hook 'after-change-functions 'diff-hl-edit nil t)
312 (if vc-mode
313 (diff-hl-update)
314 (add-hook 'find-file-hook 'diff-hl-update t t))
315 (add-hook 'vc-checkin-hook 'diff-hl-update nil t)
316 (add-hook 'after-revert-hook 'diff-hl-update nil t)
317 (add-hook 'text-scale-mode-hook 'diff-hl-define-bitmaps nil t))
318 (remove-hook 'after-save-hook 'diff-hl-update t)
319 (remove-hook 'after-change-functions 'diff-hl-edit t)
320 (remove-hook 'find-file-hook 'diff-hl-update t)
321 (remove-hook 'vc-checkin-hook 'diff-hl-update t)
322 (remove-hook 'after-revert-hook 'diff-hl-update t)
323 (remove-hook 'text-scale-mode-hook 'diff-hl-define-bitmaps t)
324 (diff-hl-remove-overlays)))
325
326 (when (require 'smartrep nil t)
327 (let (smart-keys)
328 (dolist (c '("n" "[" "]"))
329 (let* ((cmd (lookup-key diff-hl-mode-map (kbd (concat "C-x v " c)))))
330 (push (cons c cmd) smart-keys)))
331 (smartrep-define-key diff-hl-mode-map "C-x v" smart-keys)))
332
333 (defun diff-hl-dir-update ()
334 (dolist (pair (if (vc-dir-marked-files)
335 (vc-dir-marked-only-files-and-states)
336 (vc-dir-child-files-and-states)))
337 (when (eq 'up-to-date (cdr pair))
338 (let ((buffer (find-buffer-visiting (car pair))))
339 (when buffer
340 (with-current-buffer buffer
341 (diff-hl-remove-overlays)))))))
342
343 (define-minor-mode diff-hl-dir-mode
344 "Toggle `diff-hl-mode' integration in a `vc-dir-mode' buffer."
345 :lighter ""
346 (if diff-hl-dir-mode
347 (add-hook 'vc-checkin-hook 'diff-hl-dir-update t t)
348 (remove-hook 'vc-checkin-hook 'diff-hl-dir-update t)))
349
350 ;;;###autoload
351 (defun turn-on-diff-hl-mode ()
352 "Turn on `diff-hl-mode' or `diff-hl-dir-mode' in a buffer if appropriate."
353 (when (window-system) ;; No fringes in the console.
354 (cond
355 (buffer-file-name
356 (diff-hl-mode 1))
357 ((eq major-mode 'vc-dir-mode)
358 (diff-hl-dir-mode 1)))))
359
360 (defun diff-hl-global-mode-change ()
361 (unless global-diff-hl-mode
362 (dolist (buf (buffer-list))
363 (with-current-buffer buf
364 (when diff-hl-dir-mode
365 (diff-hl-dir-mode -1))))))
366
367 ;;;###autoload
368 (define-globalized-minor-mode global-diff-hl-mode diff-hl-mode
369 turn-on-diff-hl-mode :after-hook (diff-hl-global-mode-change))
370
371 (provide 'diff-hl)
372
373 ;;; diff-hl.el ends here