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