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