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