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