]> code.delx.au - gnu-emacs-elpa/blob - diff-hl.el
SVN needs nil diff switches too to revert to -U0 context
[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.3
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 (let ((hunk-beg (point)))
180 (forward-line len)
181 (setq current-line (+ line len))
182 (let ((h (make-overlay hunk-beg (1- (point))))
183 (hook '(diff-hl-overlay-modified)))
184 (overlay-put h 'diff-hl t)
185 (if (= len 1)
186 (overlay-put h 'before-string (diff-hl-fringe-spec type 'single))
187 (overlay-put h 'before-string (diff-hl-fringe-spec type 'top))
188 (overlay-put h 'line-prefix (diff-hl-fringe-spec type 'middle))
189 (overlay-put h 'after-string (diff-hl-fringe-spec type 'bottom)))
190 (overlay-put h 'modification-hooks hook)
191 (overlay-put h 'insert-in-front-hooks hook)
192 (overlay-put h 'insert-behind-hooks hook))))))))
193
194 (defun diff-hl-remove-overlays ()
195 (dolist (o (overlays-in (point-min) (point-max)))
196 (when (overlay-get o 'diff-hl) (delete-overlay o))))
197
198 (defun diff-hl-overlay-modified (ov after-p _beg _end &optional _length)
199 "Delete the overlay."
200 (unless after-p
201 (when (overlay-buffer ov)
202 (delete-overlay ov))))
203
204 (defvar diff-hl-timer nil)
205
206 (defun diff-hl-edit (_beg _end _len)
207 "DTRT when we've `undo'-ne the buffer into unmodified state."
208 (when undo-in-progress
209 (when diff-hl-timer
210 (cancel-timer diff-hl-timer))
211 (setq diff-hl-timer
212 (run-with-idle-timer 0.01 nil #'diff-hl-after-undo (current-buffer)))))
213
214 (defun diff-hl-after-undo (buffer)
215 (with-current-buffer buffer
216 (unless (buffer-modified-p)
217 (diff-hl-update))))
218
219 (defun diff-hl-diff-goto-hunk ()
220 "Run VC diff command and go to the line corresponding to the current."
221 (interactive)
222 (vc-buffer-sync)
223 (let* ((line (line-number-at-pos))
224 (buffer (current-buffer)))
225 (vc-diff-internal t (vc-deduce-fileset) nil nil t)
226 (vc-exec-after `(if (< (line-number-at-pos (point-max)) 3)
227 (with-current-buffer ,buffer (diff-hl-remove-overlays))
228 (diff-hl-diff-skip-to ,line)
229 (setq vc-sentinel-movepoint (point))))))
230
231 (defun diff-hl-diff-skip-to (line)
232 "In `diff-mode', skip to the hunk and line corresponding to LINE
233 in the source file, or the last line of the hunk above it."
234 (diff-hunk-next)
235 (let (found)
236 (while (and (looking-at diff-hunk-header-re-unified) (not found))
237 (let ((hunk-line (string-to-number (match-string 3)))
238 (len (let ((m (match-string 4)))
239 (if m (string-to-number m) 1))))
240 (if (> line (+ hunk-line len))
241 (diff-hunk-next)
242 (setq found t)
243 (if (< line hunk-line)
244 ;; Retreat to the previous hunk.
245 (forward-line -1)
246 (let ((to-go (1+ (- line hunk-line))))
247 (while (cl-plusp to-go)
248 (forward-line 1)
249 (unless (looking-at "^-")
250 (cl-decf to-go))))))))))
251
252 (defun diff-hl-revert-hunk ()
253 "Revert the diff hunk with changes at or above the point."
254 (interactive)
255 (vc-buffer-sync)
256 (let ((diff-buffer (generate-new-buffer-name "*diff-hl*"))
257 (buffer (current-buffer))
258 (line (save-excursion
259 (unless (diff-hl-hunk-overlay-at (point))
260 (diff-hl-previous-hunk))
261 (line-number-at-pos)))
262 (fileset (vc-deduce-fileset)))
263 (unwind-protect
264 (progn
265 (vc-diff-internal nil fileset nil nil nil diff-buffer)
266 (vc-exec-after
267 `(let (beg-line end-line)
268 (when (eobp)
269 (with-current-buffer ,buffer (diff-hl-remove-overlays))
270 (error "Buffer is up-to-date"))
271 (diff-hl-diff-skip-to ,line)
272 (save-excursion
273 (while (looking-at "[-+]") (forward-line 1))
274 (setq end-line (line-number-at-pos (point)))
275 (unless (eobp) (diff-split-hunk)))
276 (unless (looking-at "[-+]") (forward-line -1))
277 (while (looking-at "[-+]") (forward-line -1))
278 (setq beg-line (line-number-at-pos (point)))
279 (unless (looking-at "@")
280 (forward-line 1)
281 (diff-split-hunk))
282 (let ((wbh (window-body-height)))
283 (if (>= wbh (- end-line beg-line))
284 (recenter (/ (+ wbh (- beg-line end-line) 2) 2))
285 (recenter 1)))
286 (unless (yes-or-no-p (format "Revert current hunk in %s?"
287 ,(cl-caadr fileset)))
288 (error "Revert canceled"))
289 (let ((diff-advance-after-apply-hunk nil))
290 (diff-apply-hunk t))
291 (with-current-buffer ,buffer
292 (save-buffer))
293 (message "Hunk reverted"))))
294 (quit-windows-on diff-buffer))))
295
296 (defun diff-hl-hunk-overlay-at (pos)
297 (cl-loop for o in (overlays-in pos (1+ pos))
298 when (overlay-get o 'diff-hl)
299 return o))
300
301 (defun diff-hl-next-hunk (&optional backward)
302 "Go to the beginning of the next hunk in the current buffer."
303 (interactive)
304 (let ((pos (save-excursion
305 (catch 'found
306 (while (not (if backward (bobp) (eobp)))
307 (goto-char (if backward
308 (previous-overlay-change (point))
309 (next-overlay-change (point))))
310 (let ((o (diff-hl-hunk-overlay-at (point))))
311 (when (and o (if backward
312 (>= (overlay-end o) (point))
313 (<= (overlay-start o) (point))))
314 (throw 'found (overlay-start o)))))))))
315 (if pos
316 (goto-char pos)
317 (error "No further hunks found"))))
318
319 (defun diff-hl-previous-hunk ()
320 "Go to the beginning of the previous hunk in the current buffer."
321 (interactive)
322 (diff-hl-next-hunk t))
323
324 (define-minor-mode diff-hl-mode
325 "Toggle VC diff fringe highlighting."
326 :lighter "" :keymap `(([remap vc-diff] . diff-hl-diff-goto-hunk)
327 (,(kbd "C-x v n") . diff-hl-revert-hunk)
328 (,(kbd "C-x v [") . diff-hl-previous-hunk)
329 (,(kbd "C-x v ]") . diff-hl-next-hunk))
330 (if diff-hl-mode
331 (progn
332 (when (window-system) ;; No fringes in the console.
333 (unless (fringe-bitmap-p 'diff-hl-bmp-empty)
334 (diff-hl-define-bitmaps)
335 (define-fringe-bitmap 'diff-hl-bmp-empty [0] 1 1 'center)))
336 (add-hook 'after-save-hook 'diff-hl-update nil t)
337 (add-hook 'after-change-functions 'diff-hl-edit nil t)
338 (if vc-mode
339 (diff-hl-update)
340 (add-hook 'find-file-hook 'diff-hl-update t t))
341 (add-hook 'vc-checkin-hook 'diff-hl-update nil t)
342 (add-hook 'after-revert-hook 'diff-hl-update nil t)
343 (add-hook 'text-scale-mode-hook 'diff-hl-define-bitmaps nil t))
344 (remove-hook 'after-save-hook 'diff-hl-update t)
345 (remove-hook 'after-change-functions 'diff-hl-edit t)
346 (remove-hook 'find-file-hook 'diff-hl-update t)
347 (remove-hook 'vc-checkin-hook 'diff-hl-update t)
348 (remove-hook 'after-revert-hook 'diff-hl-update t)
349 (remove-hook 'text-scale-mode-hook 'diff-hl-define-bitmaps t)
350 (diff-hl-remove-overlays)))
351
352 (when (require 'smartrep nil t)
353 (let (smart-keys)
354 (cl-labels ((scan (map)
355 (map-keymap
356 (lambda (event binding)
357 (if (consp binding)
358 (scan binding)
359 (when (characterp event)
360 (push (cons (string event) binding) smart-keys))))
361 map)))
362 (scan diff-hl-mode-map)
363 (smartrep-define-key diff-hl-mode-map "C-x v" smart-keys))))
364
365 (defun diff-hl-dir-update ()
366 (dolist (pair (if (vc-dir-marked-files)
367 (vc-dir-marked-only-files-and-states)
368 (vc-dir-child-files-and-states)))
369 (when (eq 'up-to-date (cdr pair))
370 (let ((buffer (find-buffer-visiting (car pair))))
371 (when buffer
372 (with-current-buffer buffer
373 (diff-hl-remove-overlays)))))))
374
375 (define-minor-mode diff-hl-dir-mode
376 "Toggle `diff-hl-mode' integration in a `vc-dir-mode' buffer."
377 :lighter ""
378 (if diff-hl-dir-mode
379 (add-hook 'vc-checkin-hook 'diff-hl-dir-update t t)
380 (remove-hook 'vc-checkin-hook 'diff-hl-dir-update t)))
381
382 ;;;###autoload
383 (defun turn-on-diff-hl-mode ()
384 "Turn on `diff-hl-mode' or `diff-hl-dir-mode' in a buffer if appropriate."
385 (cond
386 (buffer-file-name
387 (diff-hl-mode 1))
388 ((eq major-mode 'vc-dir-mode)
389 (diff-hl-dir-mode 1))))
390
391 ;;;###autoload
392 (define-globalized-minor-mode global-diff-hl-mode diff-hl-mode
393 turn-on-diff-hl-mode :after-hook (diff-hl-global-mode-change))
394
395 (defun diff-hl-global-mode-change ()
396 (unless global-diff-hl-mode
397 (dolist (buf (buffer-list))
398 (with-current-buffer buf
399 (when diff-hl-dir-mode
400 (diff-hl-dir-mode -1))))))
401
402 (provide 'diff-hl)
403
404 ;;; diff-hl.el ends here