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