1 ;;; diff-hl.el --- Highlight uncommitted changes -*- lexical-binding: t -*-
3 ;; Author: Dmitry Gutov <dgutov@yandex.ru>
4 ;; URL: https://github.com/dgutov/diff-hl
7 ;; Package-Requires: ((cl-lib "0.2"))
9 ;; This file is not part of GNU Emacs.
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.
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.
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/>.
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.
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 ]
36 ;; The mode takes advantage of `smartrep' if it is installed.
38 ;; Add either of the following to your init file.
40 ;; To use it in all buffers:
42 ;; (global-diff-hl-mode)
44 ;; Only in `prog-mode' buffers, with `vc-dir' integration:
46 ;; (add-hook 'prog-mode-hook 'turn-on-diff-hl-mode)
47 ;; (add-hook 'vc-dir-mode-hook 'turn-on-diff-hl-mode)
58 (require 'face-remap))
61 "VC diff fringe highlighting"
64 (defface diff-hl-insert
65 '((default :inherit diff-added)
66 (((class color)) :foreground "green4"))
67 "Face used to highlight inserted lines."
70 (defface diff-hl-delete
71 '((default :inherit diff-removed)
72 (((class color)) :foreground "red3"))
73 "Face used to highlight deleted lines."
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."
85 (defcustom diff-hl-draw-borders t
86 "Non-nil to draw borders around fringe indicators."
90 (defvar diff-hl-reference-revision nil
91 "Revision to diff against. nil means the most recent one.")
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)
98 (spacing (or (default-value 'line-spacing) 0))
99 (h (round (+ (* (frame-char-height) scale)
101 (* (frame-char-height) 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)))
110 (aset bottom (1- h) 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)))
118 (defvar diff-hl-spec-cache (make-hash-table :test 'equal))
120 (defun diff-hl-fringe-spec (type pos)
121 (let* ((key (cons type pos))
122 (val (gethash key diff-hl-spec-cache)))
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)))
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))
138 (defun diff-hl-changes ()
139 (let* ((file buffer-file-name)
140 (backend (vc-backend file)))
142 (let ((state (vc-state file backend)))
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
153 (diff-hl-with-diff-switches
154 (vc-call-backend backend 'diff (list file)
155 diff-hl-reference-revision nil
157 (with-current-buffer buf-name
158 (goto-char (point-min))
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)))
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)
172 (when (eq type 'delete)
175 (push (list line len type) res))))))
178 `((1 ,(line-number-at-pos (point-max)) insert)))
180 `((1 ,(line-number-at-pos (point-max)) delete))))))))
182 (defun diff-hl-update ()
183 (let ((changes (diff-hl-changes))
185 (diff-hl-remove-overlays)
187 (goto-char (point-min))
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
200 ((not diff-hl-draw-borders) 'empty)
201 ((and (= len 1) (= line current-line)) 'single)
203 ((= line current-line) 'top)
206 (cl-incf current-line)
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))))))))
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))))
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."
223 (when (overlay-buffer ov)
225 (narrow-to-region (overlay-start ov) (overlay-end ov))
226 (diff-hl-remove-overlays))
227 (delete-overlay ov))))
229 (defvar diff-hl-timer nil)
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
235 (cancel-timer diff-hl-timer))
237 (run-with-idle-timer 0.01 nil #'diff-hl-after-undo (current-buffer)))))
239 (defun diff-hl-after-undo (buffer)
240 (with-current-buffer buffer
241 (unless (buffer-modified-p)
244 (defun diff-hl-diff-goto-hunk ()
245 "Run VC diff command and go to the line corresponding to the current."
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))))))
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."
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))
268 (if (< line hunk-line)
269 ;; Retreat to the previous hunk.
271 (let ((to-go (1+ (- line hunk-line))))
272 (while (cl-plusp to-go)
274 (unless (looking-at "^-")
275 (cl-decf to-go))))))))))
277 (defun diff-hl-revert-hunk ()
278 "Revert the diff hunk with changes at or above the point."
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)))
290 (vc-diff-internal nil fileset diff-hl-reference-revision nil
293 `(let (beg-line end-line)
295 (with-current-buffer ,buffer (diff-hl-remove-overlays))
296 (error "Buffer is up-to-date"))
297 (diff-hl-diff-skip-to ,line)
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 "@")
308 (let ((wbh (window-body-height)))
309 (if (>= wbh (- end-line beg-line))
310 (recenter (/ (+ wbh (- beg-line end-line) 2) 2))
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))
317 (with-current-buffer ,buffer
319 (message "Hunk reverted"))))
320 (quit-windows-on diff-buffer))))
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)
327 (defun diff-hl-next-hunk (&optional backward)
328 "Go to the beginning of the next hunk in the current buffer."
330 (let ((pos (save-excursion
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)))))))))
341 (error "No further hunks found"))))
343 (defun diff-hl-previous-hunk ()
344 "Go to the beginning of the previous hunk in the current buffer."
346 (diff-hl-next-hunk t))
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))
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)
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)))
376 (when (require 'smartrep nil t)
378 (cl-labels ((scan (map)
380 (lambda (event binding)
383 (when (characterp event)
384 (push (cons (string event) binding) smart-keys))))
386 (scan diff-hl-mode-map)
387 (smartrep-define-key diff-hl-mode-map "C-x v" smart-keys))))
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))))
396 (with-current-buffer buffer
397 (diff-hl-remove-overlays)))))))
399 (define-minor-mode diff-hl-dir-mode
400 "Toggle `diff-hl-mode' integration in a `vc-dir-mode' buffer."
403 (add-hook 'vc-checkin-hook 'diff-hl-dir-update t t)
404 (remove-hook 'vc-checkin-hook 'diff-hl-dir-update t)))
407 (defun turn-on-diff-hl-mode ()
408 "Turn on `diff-hl-mode' or `diff-hl-dir-mode' in a buffer if appropriate."
412 ((eq major-mode 'vc-dir-mode)
413 (diff-hl-dir-mode 1))))
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))
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))))))
428 ;;; diff-hl.el ends here