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