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