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