]> code.delx.au - gnu-emacs-elpa/blob - diff-hl.el
* diff-hl-add-highlighting: Return the overlay
[gnu-emacs-elpa] / diff-hl.el
1 ;;; diff-hl.el --- Highlight uncommitted changes -*- 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.4.6
7 ;; Package-Requires: ((cl-lib "0.2"))
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' highlights uncommitted changes on the left fringe of the
27 ;; window, allows you to jump between the hunks and revert them selectively.
28
29 ;; Provided commands:
30 ;;
31 ;; `diff-hl-diff-goto-hunk' C-x v =
32 ;; `diff-hl-revert-hunk' C-x v n
33 ;; `diff-hl-previous-hunk' C-x v [
34 ;; `diff-hl-next-hunk' C-x v ]
35 ;;
36 ;; The mode takes advantage of `smartrep' if it is installed.
37
38 ;; Add either of the following to your init file.
39 ;;
40 ;; To use it in all buffers:
41 ;;
42 ;; (global-diff-hl-mode)
43 ;;
44 ;; Only in `prog-mode' buffers, with `vc-dir' integration:
45 ;;
46 ;; (add-hook 'prog-mode-hook 'turn-on-diff-hl-mode)
47 ;; (add-hook 'vc-dir-mode-hook 'turn-on-diff-hl-mode)
48
49 ;;; Code:
50
51 (require 'diff-mode)
52 (require 'vc)
53 (require 'vc-dir)
54 (eval-when-compile
55 (require 'cl-lib)
56 (require 'vc-git)
57 (require 'vc-hg)
58 (require 'face-remap))
59
60 (defgroup diff-hl nil
61 "VC diff fringe highlighting"
62 :group 'vc)
63
64 (defface diff-hl-insert
65 '((default :inherit diff-added)
66 (((class color)) :foreground "green4"))
67 "Face used to highlight inserted lines."
68 :group 'diff-hl)
69
70 (defface diff-hl-delete
71 '((default :inherit diff-removed)
72 (((class color)) :foreground "red3"))
73 "Face used to highlight deleted lines."
74 :group 'diff-hl)
75
76 (defface diff-hl-change
77 '((default :foreground "blue3")
78 (((class color) (min-colors 88) (background light))
79 :background "#ddddff")
80 (((class color) (min-colors 88) (background dark))
81 :background "#333355"))
82 "Face used to highlight changed lines."
83 :group 'diff-hl)
84
85 (defface diff-hl-unknown
86 '((default :inherit diff-header))
87 "Face used to highlight unregistered files.")
88
89 (defcustom diff-hl-draw-borders t
90 "Non-nil to draw borders around fringe indicators."
91 :group 'diff-hl
92 :type 'boolean)
93
94 (defcustom diff-hl-fringe-bmp-function 'diff-hl-fringe-bmp-from-pos
95 "Function to choose the fringe bitmap for a given change type
96 and position within a hunk. Should accept two arguments."
97 :group 'diff-hl
98 :type '(choice (const diff-hl-fringe-bmp-from-pos)
99 (const diff-hl-fringe-bmp-from-type)
100 function))
101
102 (defvar diff-hl-reference-revision nil
103 "Revision to diff against. nil means the most recent one.")
104
105 (defun diff-hl-define-bitmaps ()
106 (let* ((scale (if (and (boundp 'text-scale-mode-amount)
107 (numberp text-scale-mode-amount))
108 (expt text-scale-mode-step text-scale-mode-amount)
109 1))
110 (spacing (or (default-value 'line-spacing) 0))
111 (h (round (+ (* (frame-char-height) scale)
112 (if (floatp spacing)
113 (* (frame-char-height) spacing)
114 spacing))))
115 (w (frame-parameter nil 'left-fringe))
116 (middle (make-vector h (expt 2 (1- w))))
117 (ones (1- (expt 2 w)))
118 (top (copy-sequence middle))
119 (bottom (copy-sequence middle))
120 (single (copy-sequence middle)))
121 (aset top 0 ones)
122 (aset bottom (1- h) ones)
123 (aset single 0 ones)
124 (aset single (1- h) ones)
125 (define-fringe-bitmap 'diff-hl-bmp-top top h w 'top)
126 (define-fringe-bitmap 'diff-hl-bmp-middle middle h w 'center)
127 (define-fringe-bitmap 'diff-hl-bmp-bottom bottom h w 'bottom)
128 (define-fringe-bitmap 'diff-hl-bmp-single single h w 'top)
129 (let* ((w2 (- w 2))
130 (delete-row (1- (expr 2 w2)))
131 (middle-bit (expt 2 (1- (/ w2 2))))
132 (insert-bmp (make-vector 2 (* 3 middle-bit))))
133 (define-fringe-bitmap 'diff-hl-bmp-delete (make-vector 2 delete-row))
134 (aset insert-bmp middle-bit delete-row)
135 (aset insert-bmp (1+ middle-bit) delete-row)
136 (define-fringe-bitmap 'diff-hl-bmp-insert insert-bmp)
137 (define-fringe-bitmap 'diff-hl-bmp-change (make-vector
138 w (* 3 middle-bit))))))
139
140 (defvar diff-hl-spec-cache (make-hash-table :test 'equal))
141
142 (defun diff-hl-fringe-spec (type pos)
143 (let* ((key (list type pos diff-hl-fringe-bmp-function))
144 (val (gethash key diff-hl-spec-cache)))
145 (unless val
146 (let* ((face-sym (intern (format "diff-hl-%s" type)))
147 (bmp-sym (funcall diff-hl-fringe-bmp-function type pos)))
148 (setq val (propertize " " 'display `((left-fringe ,bmp-sym ,face-sym))))
149 (puthash key val diff-hl-spec-cache)))
150 val))
151
152 (defun diff-hl-fringe-bmp-from-pos (type pos)
153 (intern (format "diff-hl-bmp-%s" pos)))
154
155 (defun diff-hl-fringe-bmp-from-type (type pos)
156 (if (eq type 'unknown)
157 'question-mark
158 (intern (format "diff-hl-bmp-%s" type))))
159
160 (defmacro diff-hl-with-diff-switches (body)
161 `(let ((vc-git-diff-switches nil)
162 (vc-hg-diff-switches nil)
163 (vc-svn-diff-switches nil)
164 (vc-diff-switches '("-U0"))
165 (vc-disable-async-diff t))
166 ,body))
167
168 (defun diff-hl-changes ()
169 (let* ((file buffer-file-name)
170 (backend (vc-backend file)))
171 (when backend
172 (let ((state (vc-state file backend)))
173 (cond
174 ((or (eq state 'edited)
175 (and (eq state 'up-to-date)
176 ;; VC state is stale in after-revert-hook.
177 (or revert-buffer-in-progress-p
178 ;; Diffing against an older revision.
179 diff-hl-reference-revision)))
180 (let* ((buf-name " *diff-hl* ")
181 diff-auto-refine-mode
182 res)
183 (diff-hl-with-diff-switches
184 (vc-call-backend backend 'diff (list file)
185 diff-hl-reference-revision nil
186 buf-name))
187 (with-current-buffer buf-name
188 (goto-char (point-min))
189 (unless (eobp)
190 (diff-beginning-of-hunk t)
191 (while (looking-at diff-hunk-header-re-unified)
192 (let ((line (string-to-number (match-string 3)))
193 (len (let ((m (match-string 4)))
194 (if m (string-to-number m) 1)))
195 (beg (point)))
196 (diff-end-of-hunk)
197 (let* ((inserts (diff-count-matches "^\\+" beg (point)))
198 (deletes (diff-count-matches "^-" beg (point)))
199 (type (cond ((zerop deletes) 'insert)
200 ((zerop inserts) 'delete)
201 (t 'change))))
202 (when (eq type 'delete)
203 (setq len 1)
204 (cl-incf line))
205 (push (list line len type) res))))))
206 (nreverse res)))
207 ((eq state 'added)
208 `((1 ,(line-number-at-pos (point-max)) insert)))
209 ((eq state 'removed)
210 `((1 ,(line-number-at-pos (point-max)) delete))))))))
211
212 (defun diff-hl-update ()
213 (let ((changes (diff-hl-changes))
214 (current-line 1))
215 (diff-hl-remove-overlays)
216 (save-excursion
217 (goto-char (point-min))
218 (dolist (c changes)
219 (cl-destructuring-bind (line len type) c
220 (forward-line (- line current-line))
221 (setq current-line line)
222 (let ((hunk-beg (point)))
223 (while (cl-plusp len)
224 (diff-hl-add-highlighting
225 type
226 (cond
227 ((not diff-hl-draw-borders) 'empty)
228 ((and (= len 1) (= line current-line)) 'single)
229 ((= len 1) 'bottom)
230 ((= line current-line) 'top)
231 (t 'middle)))
232 (forward-line 1)
233 (cl-incf current-line)
234 (cl-decf len))
235 (let ((h (make-overlay hunk-beg (point)))
236 (hook '(diff-hl-overlay-modified)))
237 (overlay-put h 'diff-hl t)
238 (overlay-put h 'diff-hl-hunk t)
239 (overlay-put h 'modification-hooks hook)
240 (overlay-put h 'insert-in-front-hooks hook)
241 (overlay-put h 'insert-behind-hooks hook))))))))
242
243 (defun diff-hl-add-highlighting (type shape)
244 (let ((o (make-overlay (point) (point))))
245 (overlay-put o 'diff-hl t)
246 (overlay-put o 'before-string (diff-hl-fringe-spec type shape))
247 o))
248
249 (defun diff-hl-remove-overlays ()
250 (dolist (o (overlays-in (point-min) (point-max)))
251 (when (overlay-get o 'diff-hl) (delete-overlay o))))
252
253 (defun diff-hl-overlay-modified (ov after-p _beg _end &optional _length)
254 "Delete the hunk overlay and all our line overlays inside it."
255 (unless after-p
256 (when (overlay-buffer ov)
257 (save-restriction
258 (narrow-to-region (overlay-start ov) (overlay-end ov))
259 (diff-hl-remove-overlays))
260 (delete-overlay ov))))
261
262 (defvar diff-hl-timer nil)
263
264 (defun diff-hl-edit (_beg _end _len)
265 "DTRT when we've `undo'-ne the buffer into unmodified state."
266 (when undo-in-progress
267 (when diff-hl-timer
268 (cancel-timer diff-hl-timer))
269 (setq diff-hl-timer
270 (run-with-idle-timer 0.01 nil #'diff-hl-after-undo (current-buffer)))))
271
272 (defun diff-hl-after-undo (buffer)
273 (with-current-buffer buffer
274 (unless (buffer-modified-p)
275 (diff-hl-update))))
276
277 (defun diff-hl-diff-goto-hunk ()
278 "Run VC diff command and go to the line corresponding to the current."
279 (interactive)
280 (vc-buffer-sync)
281 (let* ((line (line-number-at-pos))
282 (buffer (current-buffer)))
283 (vc-diff-internal t (vc-deduce-fileset) diff-hl-reference-revision nil t)
284 (vc-exec-after `(if (< (line-number-at-pos (point-max)) 3)
285 (with-current-buffer ,buffer (diff-hl-remove-overlays))
286 (diff-hl-diff-skip-to ,line)
287 (setq vc-sentinel-movepoint (point))))))
288
289 (defun diff-hl-diff-skip-to (line)
290 "In `diff-mode', skip to the hunk and line corresponding to LINE
291 in the source file, or the last line of the hunk above it."
292 (diff-hunk-next)
293 (let (found)
294 (while (and (looking-at diff-hunk-header-re-unified) (not found))
295 (let ((hunk-line (string-to-number (match-string 3)))
296 (len (let ((m (match-string 4)))
297 (if m (string-to-number m) 1))))
298 (if (> line (+ hunk-line len))
299 (diff-hunk-next)
300 (setq found t)
301 (if (< line hunk-line)
302 ;; Retreat to the previous hunk.
303 (forward-line -1)
304 (let ((to-go (1+ (- line hunk-line))))
305 (while (cl-plusp to-go)
306 (forward-line 1)
307 (unless (looking-at "^-")
308 (cl-decf to-go))))))))))
309
310 (defun diff-hl-revert-hunk ()
311 "Revert the diff hunk with changes at or above the point."
312 (interactive)
313 (vc-buffer-sync)
314 (let ((diff-buffer (generate-new-buffer-name "*diff-hl*"))
315 (buffer (current-buffer))
316 (line (save-excursion
317 (unless (diff-hl-hunk-overlay-at (point))
318 (diff-hl-previous-hunk))
319 (line-number-at-pos)))
320 (fileset (vc-deduce-fileset)))
321 (unwind-protect
322 (progn
323 (vc-diff-internal nil fileset diff-hl-reference-revision nil
324 nil diff-buffer)
325 (vc-exec-after
326 `(let (beg-line end-line)
327 (when (eobp)
328 (with-current-buffer ,buffer (diff-hl-remove-overlays))
329 (error "Buffer is up-to-date"))
330 (diff-hl-diff-skip-to ,line)
331 (save-excursion
332 (while (looking-at "[-+]") (forward-line 1))
333 (setq end-line (line-number-at-pos (point)))
334 (unless (eobp) (diff-split-hunk)))
335 (unless (looking-at "[-+]") (forward-line -1))
336 (while (looking-at "[-+]") (forward-line -1))
337 (setq beg-line (line-number-at-pos (point)))
338 (unless (looking-at "@")
339 (forward-line 1)
340 (diff-split-hunk))
341 (let ((wbh (window-body-height)))
342 (if (>= wbh (- end-line beg-line))
343 (recenter (/ (+ wbh (- beg-line end-line) 2) 2))
344 (recenter 1)))
345 (unless (yes-or-no-p (format "Revert current hunk in %s?"
346 ,(cl-caadr fileset)))
347 (error "Revert canceled"))
348 (let ((diff-advance-after-apply-hunk nil))
349 (diff-apply-hunk t))
350 (with-current-buffer ,buffer
351 (save-buffer))
352 (message "Hunk reverted"))))
353 (quit-windows-on diff-buffer))))
354
355 (defun diff-hl-hunk-overlay-at (pos)
356 (cl-loop for o in (overlays-in pos (1+ pos))
357 when (overlay-get o 'diff-hl-hunk)
358 return o))
359
360 (defun diff-hl-next-hunk (&optional backward)
361 "Go to the beginning of the next hunk in the current buffer."
362 (interactive)
363 (let ((pos (save-excursion
364 (catch 'found
365 (while (not (if backward (bobp) (eobp)))
366 (goto-char (if backward
367 (previous-overlay-change (point))
368 (next-overlay-change (point))))
369 (let ((o (diff-hl-hunk-overlay-at (point))))
370 (when (and o (= (overlay-start o) (point)))
371 (throw 'found (overlay-start o)))))))))
372 (if pos
373 (goto-char pos)
374 (error "No further hunks found"))))
375
376 (defun diff-hl-previous-hunk ()
377 "Go to the beginning of the previous hunk in the current buffer."
378 (interactive)
379 (diff-hl-next-hunk t))
380
381 ;;;###autoload
382 (define-minor-mode diff-hl-mode
383 "Toggle VC diff fringe highlighting."
384 :lighter "" :keymap `(([remap vc-diff] . diff-hl-diff-goto-hunk)
385 (,(kbd "C-x v n") . diff-hl-revert-hunk)
386 (,(kbd "C-x v [") . diff-hl-previous-hunk)
387 (,(kbd "C-x v ]") . diff-hl-next-hunk))
388 (if diff-hl-mode
389 (progn
390 (when (window-system) ;; No fringes in the console.
391 (unless (fringe-bitmap-p 'diff-hl-bmp-empty)
392 (diff-hl-define-bitmaps)
393 (define-fringe-bitmap 'diff-hl-bmp-empty [0] 1 1 'center)))
394 (add-hook 'after-save-hook 'diff-hl-update nil t)
395 (add-hook 'after-change-functions 'diff-hl-edit nil t)
396 (if vc-mode
397 (diff-hl-update)
398 (add-hook 'find-file-hook 'diff-hl-update t t))
399 (add-hook 'vc-checkin-hook 'diff-hl-update nil t)
400 (add-hook 'after-revert-hook 'diff-hl-update nil t)
401 (add-hook 'text-scale-mode-hook 'diff-hl-define-bitmaps nil t))
402 (remove-hook 'after-save-hook 'diff-hl-update t)
403 (remove-hook 'after-change-functions 'diff-hl-edit t)
404 (remove-hook 'find-file-hook 'diff-hl-update t)
405 (remove-hook 'vc-checkin-hook 'diff-hl-update t)
406 (remove-hook 'after-revert-hook 'diff-hl-update t)
407 (remove-hook 'text-scale-mode-hook 'diff-hl-define-bitmaps t)
408 (diff-hl-remove-overlays)))
409
410 (when (require 'smartrep nil t)
411 (let (smart-keys)
412 (cl-labels ((scan (map)
413 (map-keymap
414 (lambda (event binding)
415 (if (consp binding)
416 (scan binding)
417 (when (characterp event)
418 (push (cons (string event) binding) smart-keys))))
419 map)))
420 (scan diff-hl-mode-map)
421 (smartrep-define-key diff-hl-mode-map "C-x v" smart-keys))))
422
423 (defun diff-hl-dir-update ()
424 (dolist (pair (if (vc-dir-marked-files)
425 (vc-dir-marked-only-files-and-states)
426 (vc-dir-child-files-and-states)))
427 (when (eq 'up-to-date (cdr pair))
428 (let ((buffer (find-buffer-visiting (car pair))))
429 (when buffer
430 (with-current-buffer buffer
431 (diff-hl-remove-overlays)))))))
432
433 (define-minor-mode diff-hl-dir-mode
434 "Toggle `diff-hl-mode' integration in a `vc-dir-mode' buffer."
435 :lighter ""
436 (if diff-hl-dir-mode
437 (add-hook 'vc-checkin-hook 'diff-hl-dir-update t t)
438 (remove-hook 'vc-checkin-hook 'diff-hl-dir-update t)))
439
440 ;;;###autoload
441 (defun turn-on-diff-hl-mode ()
442 "Turn on `diff-hl-mode' or `diff-hl-dir-mode' in a buffer if appropriate."
443 (cond
444 (buffer-file-name
445 (diff-hl-mode 1))
446 ((eq major-mode 'vc-dir-mode)
447 (diff-hl-dir-mode 1))))
448
449 ;;;###autoload
450 (define-globalized-minor-mode global-diff-hl-mode diff-hl-mode
451 turn-on-diff-hl-mode :after-hook (diff-hl-global-mode-change))
452
453 (defun diff-hl-global-mode-change ()
454 (unless global-diff-hl-mode
455 (dolist (buf (buffer-list))
456 (with-current-buffer buf
457 (when diff-hl-dir-mode
458 (diff-hl-dir-mode -1))))))
459
460 (provide 'diff-hl)
461
462 ;;; diff-hl.el ends here