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 (defface diff-hl-unknown
86 '((default :inherit diff-nonexistent))
87 "Face used to highlight unregistered files.")
89 (defcustom diff-hl-draw-borders t
90 "Non-nil to draw borders around fringe indicators."
94 (defvar diff-hl-reference-revision nil
95 "Revision to diff against. nil means the most recent one.")
97 (defun diff-hl-define-bitmaps ()
98 (let* ((scale (if (and (boundp 'text-scale-mode-amount)
99 (numberp text-scale-mode-amount))
100 (expt text-scale-mode-step text-scale-mode-amount)
102 (spacing (or (default-value 'line-spacing) 0))
103 (h (round (+ (* (frame-char-height) scale)
105 (* (frame-char-height) spacing)
107 (w (frame-parameter nil 'left-fringe))
108 (middle (make-vector h (expt 2 (1- w))))
109 (ones (1- (expt 2 w)))
110 (top (copy-sequence middle))
111 (bottom (copy-sequence middle))
112 (single (copy-sequence middle)))
114 (aset bottom (1- h) ones)
116 (aset single (1- h) ones)
117 (define-fringe-bitmap 'diff-hl-bmp-top top h w 'top)
118 (define-fringe-bitmap 'diff-hl-bmp-middle middle h w 'center)
119 (define-fringe-bitmap 'diff-hl-bmp-bottom bottom h w 'bottom)
120 (define-fringe-bitmap 'diff-hl-bmp-single single h w 'top)))
122 (defvar diff-hl-spec-cache (make-hash-table :test 'equal))
124 (defun diff-hl-fringe-spec (type pos)
125 (let* ((key (cons type pos))
126 (val (gethash key diff-hl-spec-cache)))
128 (let* ((face-sym (intern (concat "diff-hl-" (symbol-name type))))
129 (bmp-sym (intern (concat "diff-hl-bmp-" (symbol-name pos)))))
130 (setq val (propertize " " 'display `((left-fringe ,bmp-sym ,face-sym))))
131 (puthash key val diff-hl-spec-cache)))
134 (defmacro diff-hl-with-diff-switches (body)
135 `(let ((vc-git-diff-switches nil)
136 (vc-hg-diff-switches nil)
137 (vc-svn-diff-switches nil)
138 (vc-diff-switches '("-U0"))
139 (vc-disable-async-diff t))
142 (defun diff-hl-changes ()
143 (let* ((file buffer-file-name)
144 (backend (vc-backend file)))
146 (let ((state (vc-state file backend)))
148 ((or (eq state 'edited)
149 (and (eq state 'up-to-date)
150 ;; VC state is stale in after-revert-hook.
151 (or revert-buffer-in-progress-p
152 ;; Diffing against an older revision.
153 diff-hl-reference-revision)))
154 (let* ((buf-name " *diff-hl* ")
155 diff-auto-refine-mode
157 (diff-hl-with-diff-switches
158 (vc-call-backend backend 'diff (list file)
159 diff-hl-reference-revision nil
161 (with-current-buffer buf-name
162 (goto-char (point-min))
164 (diff-beginning-of-hunk t)
165 (while (looking-at diff-hunk-header-re-unified)
166 (let ((line (string-to-number (match-string 3)))
167 (len (let ((m (match-string 4)))
168 (if m (string-to-number m) 1)))
171 (let* ((inserts (diff-count-matches "^\\+" beg (point)))
172 (deletes (diff-count-matches "^-" beg (point)))
173 (type (cond ((zerop deletes) 'insert)
174 ((zerop inserts) 'delete)
176 (when (eq type 'delete)
179 (push (list line len type) res))))))
182 `((1 ,(line-number-at-pos (point-max)) insert)))
184 `((1 ,(line-number-at-pos (point-max)) delete))))))))
186 (defun diff-hl-update ()
187 (let ((changes (diff-hl-changes))
189 (diff-hl-remove-overlays)
191 (goto-char (point-min))
193 (cl-destructuring-bind (line len type) c
194 (forward-line (- line current-line))
195 (setq current-line line)
196 (let ((hunk-beg (point)))
197 (while (cl-plusp len)
198 (diff-hl-add-highlighting
201 ((not diff-hl-draw-borders) 'empty)
202 ((and (= len 1) (= line current-line)) 'single)
204 ((= line current-line) 'top)
207 (cl-incf current-line)
209 (let ((h (make-overlay hunk-beg (point)))
210 (hook '(diff-hl-overlay-modified)))
211 (overlay-put h 'diff-hl t)
212 (overlay-put h 'diff-hl-hunk t)
213 (overlay-put h 'modification-hooks hook)
214 (overlay-put h 'insert-in-front-hooks hook)
215 (overlay-put h 'insert-behind-hooks hook))))))))
217 (defun diff-hl-add-highlighting (type shape)
218 (let ((o (make-overlay (point) (point))))
219 (overlay-put o 'diff-hl t)
220 (overlay-put o 'before-string (diff-hl-fringe-spec type shape))))
222 (defun diff-hl-remove-overlays ()
223 (dolist (o (overlays-in (point-min) (point-max)))
224 (when (overlay-get o 'diff-hl) (delete-overlay o))))
226 (defun diff-hl-overlay-modified (ov after-p _beg _end &optional _length)
227 "Delete the hunk overlay and all our line overlays inside it."
229 (when (overlay-buffer ov)
231 (narrow-to-region (overlay-start ov) (overlay-end ov))
232 (diff-hl-remove-overlays))
233 (delete-overlay ov))))
235 (defvar diff-hl-timer nil)
237 (defun diff-hl-edit (_beg _end _len)
238 "DTRT when we've `undo'-ne the buffer into unmodified state."
239 (when undo-in-progress
241 (cancel-timer diff-hl-timer))
243 (run-with-idle-timer 0.01 nil #'diff-hl-after-undo (current-buffer)))))
245 (defun diff-hl-after-undo (buffer)
246 (with-current-buffer buffer
247 (unless (buffer-modified-p)
250 (defun diff-hl-diff-goto-hunk ()
251 "Run VC diff command and go to the line corresponding to the current."
254 (let* ((line (line-number-at-pos))
255 (buffer (current-buffer)))
256 (vc-diff-internal t (vc-deduce-fileset) diff-hl-reference-revision nil t)
257 (vc-exec-after `(if (< (line-number-at-pos (point-max)) 3)
258 (with-current-buffer ,buffer (diff-hl-remove-overlays))
259 (diff-hl-diff-skip-to ,line)
260 (setq vc-sentinel-movepoint (point))))))
262 (defun diff-hl-diff-skip-to (line)
263 "In `diff-mode', skip to the hunk and line corresponding to LINE
264 in the source file, or the last line of the hunk above it."
267 (while (and (looking-at diff-hunk-header-re-unified) (not found))
268 (let ((hunk-line (string-to-number (match-string 3)))
269 (len (let ((m (match-string 4)))
270 (if m (string-to-number m) 1))))
271 (if (> line (+ hunk-line len))
274 (if (< line hunk-line)
275 ;; Retreat to the previous hunk.
277 (let ((to-go (1+ (- line hunk-line))))
278 (while (cl-plusp to-go)
280 (unless (looking-at "^-")
281 (cl-decf to-go))))))))))
283 (defun diff-hl-revert-hunk ()
284 "Revert the diff hunk with changes at or above the point."
287 (let ((diff-buffer (generate-new-buffer-name "*diff-hl*"))
288 (buffer (current-buffer))
289 (line (save-excursion
290 (unless (diff-hl-hunk-overlay-at (point))
291 (diff-hl-previous-hunk))
292 (line-number-at-pos)))
293 (fileset (vc-deduce-fileset)))
296 (vc-diff-internal nil fileset diff-hl-reference-revision nil
299 `(let (beg-line end-line)
301 (with-current-buffer ,buffer (diff-hl-remove-overlays))
302 (error "Buffer is up-to-date"))
303 (diff-hl-diff-skip-to ,line)
305 (while (looking-at "[-+]") (forward-line 1))
306 (setq end-line (line-number-at-pos (point)))
307 (unless (eobp) (diff-split-hunk)))
308 (unless (looking-at "[-+]") (forward-line -1))
309 (while (looking-at "[-+]") (forward-line -1))
310 (setq beg-line (line-number-at-pos (point)))
311 (unless (looking-at "@")
314 (let ((wbh (window-body-height)))
315 (if (>= wbh (- end-line beg-line))
316 (recenter (/ (+ wbh (- beg-line end-line) 2) 2))
318 (unless (yes-or-no-p (format "Revert current hunk in %s?"
319 ,(cl-caadr fileset)))
320 (error "Revert canceled"))
321 (let ((diff-advance-after-apply-hunk nil))
323 (with-current-buffer ,buffer
325 (message "Hunk reverted"))))
326 (quit-windows-on diff-buffer))))
328 (defun diff-hl-hunk-overlay-at (pos)
329 (cl-loop for o in (overlays-in pos (1+ pos))
330 when (overlay-get o 'diff-hl-hunk)
333 (defun diff-hl-next-hunk (&optional backward)
334 "Go to the beginning of the next hunk in the current buffer."
336 (let ((pos (save-excursion
338 (while (not (if backward (bobp) (eobp)))
339 (goto-char (if backward
340 (previous-overlay-change (point))
341 (next-overlay-change (point))))
342 (let ((o (diff-hl-hunk-overlay-at (point))))
343 (when (and o (= (overlay-start o) (point)))
344 (throw 'found (overlay-start o)))))))))
347 (error "No further hunks found"))))
349 (defun diff-hl-previous-hunk ()
350 "Go to the beginning of the previous hunk in the current buffer."
352 (diff-hl-next-hunk t))
355 (define-minor-mode diff-hl-mode
356 "Toggle VC diff fringe highlighting."
357 :lighter "" :keymap `(([remap vc-diff] . diff-hl-diff-goto-hunk)
358 (,(kbd "C-x v n") . diff-hl-revert-hunk)
359 (,(kbd "C-x v [") . diff-hl-previous-hunk)
360 (,(kbd "C-x v ]") . diff-hl-next-hunk))
363 (when (window-system) ;; No fringes in the console.
364 (unless (fringe-bitmap-p 'diff-hl-bmp-empty)
365 (diff-hl-define-bitmaps)
366 (define-fringe-bitmap 'diff-hl-bmp-empty [0] 1 1 'center)))
367 (add-hook 'after-save-hook 'diff-hl-update nil t)
368 (add-hook 'after-change-functions 'diff-hl-edit nil t)
371 (add-hook 'find-file-hook 'diff-hl-update t t))
372 (add-hook 'vc-checkin-hook 'diff-hl-update nil t)
373 (add-hook 'after-revert-hook 'diff-hl-update nil t)
374 (add-hook 'text-scale-mode-hook 'diff-hl-define-bitmaps nil t))
375 (remove-hook 'after-save-hook 'diff-hl-update t)
376 (remove-hook 'after-change-functions 'diff-hl-edit t)
377 (remove-hook 'find-file-hook 'diff-hl-update t)
378 (remove-hook 'vc-checkin-hook 'diff-hl-update t)
379 (remove-hook 'after-revert-hook 'diff-hl-update t)
380 (remove-hook 'text-scale-mode-hook 'diff-hl-define-bitmaps t)
381 (diff-hl-remove-overlays)))
383 (when (require 'smartrep nil t)
385 (cl-labels ((scan (map)
387 (lambda (event binding)
390 (when (characterp event)
391 (push (cons (string event) binding) smart-keys))))
393 (scan diff-hl-mode-map)
394 (smartrep-define-key diff-hl-mode-map "C-x v" smart-keys))))
396 (defun diff-hl-dir-update ()
397 (dolist (pair (if (vc-dir-marked-files)
398 (vc-dir-marked-only-files-and-states)
399 (vc-dir-child-files-and-states)))
400 (when (eq 'up-to-date (cdr pair))
401 (let ((buffer (find-buffer-visiting (car pair))))
403 (with-current-buffer buffer
404 (diff-hl-remove-overlays)))))))
406 (define-minor-mode diff-hl-dir-mode
407 "Toggle `diff-hl-mode' integration in a `vc-dir-mode' buffer."
410 (add-hook 'vc-checkin-hook 'diff-hl-dir-update t t)
411 (remove-hook 'vc-checkin-hook 'diff-hl-dir-update t)))
414 (defun turn-on-diff-hl-mode ()
415 "Turn on `diff-hl-mode' or `diff-hl-dir-mode' in a buffer if appropriate."
419 ((eq major-mode 'vc-dir-mode)
420 (diff-hl-dir-mode 1))))
423 (define-globalized-minor-mode global-diff-hl-mode diff-hl-mode
424 turn-on-diff-hl-mode :after-hook (diff-hl-global-mode-change))
426 (defun diff-hl-global-mode-change ()
427 (unless global-diff-hl-mode
428 (dolist (buf (buffer-list))
429 (with-current-buffer buf
430 (when diff-hl-dir-mode
431 (diff-hl-dir-mode -1))))))
435 ;;; diff-hl.el ends here