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