]> code.delx.au - gnu-emacs-elpa/blob - diff-hl.el
diff-hl-unknown: Inherit from diff-header
[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
248 (defun diff-hl-remove-overlays ()
249 (dolist (o (overlays-in (point-min) (point-max)))
250 (when (overlay-get o 'diff-hl) (delete-overlay o))))
251
252 (defun diff-hl-overlay-modified (ov after-p _beg _end &optional _length)
253 "Delete the hunk overlay and all our line overlays inside it."
254 (unless after-p
255 (when (overlay-buffer ov)
256 (save-restriction
257 (narrow-to-region (overlay-start ov) (overlay-end ov))
258 (diff-hl-remove-overlays))
259 (delete-overlay ov))))
260
261 (defvar diff-hl-timer nil)
262
263 (defun diff-hl-edit (_beg _end _len)
264 "DTRT when we've `undo'-ne the buffer into unmodified state."
265 (when undo-in-progress
266 (when diff-hl-timer
267 (cancel-timer diff-hl-timer))
268 (setq diff-hl-timer
269 (run-with-idle-timer 0.01 nil #'diff-hl-after-undo (current-buffer)))))
270
271 (defun diff-hl-after-undo (buffer)
272 (with-current-buffer buffer
273 (unless (buffer-modified-p)
274 (diff-hl-update))))
275
276 (defun diff-hl-diff-goto-hunk ()
277 "Run VC diff command and go to the line corresponding to the current."
278 (interactive)
279 (vc-buffer-sync)
280 (let* ((line (line-number-at-pos))
281 (buffer (current-buffer)))
282 (vc-diff-internal t (vc-deduce-fileset) diff-hl-reference-revision nil t)
283 (vc-exec-after `(if (< (line-number-at-pos (point-max)) 3)
284 (with-current-buffer ,buffer (diff-hl-remove-overlays))
285 (diff-hl-diff-skip-to ,line)
286 (setq vc-sentinel-movepoint (point))))))
287
288 (defun diff-hl-diff-skip-to (line)
289 "In `diff-mode', skip to the hunk and line corresponding to LINE
290 in the source file, or the last line of the hunk above it."
291 (diff-hunk-next)
292 (let (found)
293 (while (and (looking-at diff-hunk-header-re-unified) (not found))
294 (let ((hunk-line (string-to-number (match-string 3)))
295 (len (let ((m (match-string 4)))
296 (if m (string-to-number m) 1))))
297 (if (> line (+ hunk-line len))
298 (diff-hunk-next)
299 (setq found t)
300 (if (< line hunk-line)
301 ;; Retreat to the previous hunk.
302 (forward-line -1)
303 (let ((to-go (1+ (- line hunk-line))))
304 (while (cl-plusp to-go)
305 (forward-line 1)
306 (unless (looking-at "^-")
307 (cl-decf to-go))))))))))
308
309 (defun diff-hl-revert-hunk ()
310 "Revert the diff hunk with changes at or above the point."
311 (interactive)
312 (vc-buffer-sync)
313 (let ((diff-buffer (generate-new-buffer-name "*diff-hl*"))
314 (buffer (current-buffer))
315 (line (save-excursion
316 (unless (diff-hl-hunk-overlay-at (point))
317 (diff-hl-previous-hunk))
318 (line-number-at-pos)))
319 (fileset (vc-deduce-fileset)))
320 (unwind-protect
321 (progn
322 (vc-diff-internal nil fileset diff-hl-reference-revision nil
323 nil diff-buffer)
324 (vc-exec-after
325 `(let (beg-line end-line)
326 (when (eobp)
327 (with-current-buffer ,buffer (diff-hl-remove-overlays))
328 (error "Buffer is up-to-date"))
329 (diff-hl-diff-skip-to ,line)
330 (save-excursion
331 (while (looking-at "[-+]") (forward-line 1))
332 (setq end-line (line-number-at-pos (point)))
333 (unless (eobp) (diff-split-hunk)))
334 (unless (looking-at "[-+]") (forward-line -1))
335 (while (looking-at "[-+]") (forward-line -1))
336 (setq beg-line (line-number-at-pos (point)))
337 (unless (looking-at "@")
338 (forward-line 1)
339 (diff-split-hunk))
340 (let ((wbh (window-body-height)))
341 (if (>= wbh (- end-line beg-line))
342 (recenter (/ (+ wbh (- beg-line end-line) 2) 2))
343 (recenter 1)))
344 (unless (yes-or-no-p (format "Revert current hunk in %s?"
345 ,(cl-caadr fileset)))
346 (error "Revert canceled"))
347 (let ((diff-advance-after-apply-hunk nil))
348 (diff-apply-hunk t))
349 (with-current-buffer ,buffer
350 (save-buffer))
351 (message "Hunk reverted"))))
352 (quit-windows-on diff-buffer))))
353
354 (defun diff-hl-hunk-overlay-at (pos)
355 (cl-loop for o in (overlays-in pos (1+ pos))
356 when (overlay-get o 'diff-hl-hunk)
357 return o))
358
359 (defun diff-hl-next-hunk (&optional backward)
360 "Go to the beginning of the next hunk in the current buffer."
361 (interactive)
362 (let ((pos (save-excursion
363 (catch 'found
364 (while (not (if backward (bobp) (eobp)))
365 (goto-char (if backward
366 (previous-overlay-change (point))
367 (next-overlay-change (point))))
368 (let ((o (diff-hl-hunk-overlay-at (point))))
369 (when (and o (= (overlay-start o) (point)))
370 (throw 'found (overlay-start o)))))))))
371 (if pos
372 (goto-char pos)
373 (error "No further hunks found"))))
374
375 (defun diff-hl-previous-hunk ()
376 "Go to the beginning of the previous hunk in the current buffer."
377 (interactive)
378 (diff-hl-next-hunk t))
379
380 ;;;###autoload
381 (define-minor-mode diff-hl-mode
382 "Toggle VC diff fringe highlighting."
383 :lighter "" :keymap `(([remap vc-diff] . diff-hl-diff-goto-hunk)
384 (,(kbd "C-x v n") . diff-hl-revert-hunk)
385 (,(kbd "C-x v [") . diff-hl-previous-hunk)
386 (,(kbd "C-x v ]") . diff-hl-next-hunk))
387 (if diff-hl-mode
388 (progn
389 (when (window-system) ;; No fringes in the console.
390 (unless (fringe-bitmap-p 'diff-hl-bmp-empty)
391 (diff-hl-define-bitmaps)
392 (define-fringe-bitmap 'diff-hl-bmp-empty [0] 1 1 'center)))
393 (add-hook 'after-save-hook 'diff-hl-update nil t)
394 (add-hook 'after-change-functions 'diff-hl-edit nil t)
395 (if vc-mode
396 (diff-hl-update)
397 (add-hook 'find-file-hook 'diff-hl-update t t))
398 (add-hook 'vc-checkin-hook 'diff-hl-update nil t)
399 (add-hook 'after-revert-hook 'diff-hl-update nil t)
400 (add-hook 'text-scale-mode-hook 'diff-hl-define-bitmaps nil t))
401 (remove-hook 'after-save-hook 'diff-hl-update t)
402 (remove-hook 'after-change-functions 'diff-hl-edit t)
403 (remove-hook 'find-file-hook 'diff-hl-update t)
404 (remove-hook 'vc-checkin-hook 'diff-hl-update t)
405 (remove-hook 'after-revert-hook 'diff-hl-update t)
406 (remove-hook 'text-scale-mode-hook 'diff-hl-define-bitmaps t)
407 (diff-hl-remove-overlays)))
408
409 (when (require 'smartrep nil t)
410 (let (smart-keys)
411 (cl-labels ((scan (map)
412 (map-keymap
413 (lambda (event binding)
414 (if (consp binding)
415 (scan binding)
416 (when (characterp event)
417 (push (cons (string event) binding) smart-keys))))
418 map)))
419 (scan diff-hl-mode-map)
420 (smartrep-define-key diff-hl-mode-map "C-x v" smart-keys))))
421
422 (defun diff-hl-dir-update ()
423 (dolist (pair (if (vc-dir-marked-files)
424 (vc-dir-marked-only-files-and-states)
425 (vc-dir-child-files-and-states)))
426 (when (eq 'up-to-date (cdr pair))
427 (let ((buffer (find-buffer-visiting (car pair))))
428 (when buffer
429 (with-current-buffer buffer
430 (diff-hl-remove-overlays)))))))
431
432 (define-minor-mode diff-hl-dir-mode
433 "Toggle `diff-hl-mode' integration in a `vc-dir-mode' buffer."
434 :lighter ""
435 (if diff-hl-dir-mode
436 (add-hook 'vc-checkin-hook 'diff-hl-dir-update t t)
437 (remove-hook 'vc-checkin-hook 'diff-hl-dir-update t)))
438
439 ;;;###autoload
440 (defun turn-on-diff-hl-mode ()
441 "Turn on `diff-hl-mode' or `diff-hl-dir-mode' in a buffer if appropriate."
442 (cond
443 (buffer-file-name
444 (diff-hl-mode 1))
445 ((eq major-mode 'vc-dir-mode)
446 (diff-hl-dir-mode 1))))
447
448 ;;;###autoload
449 (define-globalized-minor-mode global-diff-hl-mode diff-hl-mode
450 turn-on-diff-hl-mode :after-hook (diff-hl-global-mode-change))
451
452 (defun diff-hl-global-mode-change ()
453 (unless global-diff-hl-mode
454 (dolist (buf (buffer-list))
455 (with-current-buffer buf
456 (when diff-hl-dir-mode
457 (diff-hl-dir-mode -1))))))
458
459 (provide 'diff-hl)
460
461 ;;; diff-hl.el ends here