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 (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)
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)))
103 (aset bottom (1- h) 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)))
111 (when (window-system)
112 (define-fringe-bitmap 'diff-hl-bmp-empty [0] 1 1 'center)
113 (diff-hl-define-bitmaps))
115 (defvar diff-hl-spec-cache (make-hash-table :test 'equal))
117 (defun diff-hl-fringe-spec (type pos)
118 (let* ((key (cons type pos))
119 (val (gethash key diff-hl-spec-cache)))
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)))
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))
134 (defun diff-hl-changes ()
135 (let* ((file buffer-file-name)
136 (backend (vc-backend file)))
138 (cl-case (vc-state file backend)
140 (let* ((buf-name " *diff-hl* ")
142 (diff-hl-with-diff-switches
143 (vc-call-backend backend 'diff (list file) nil nil buf-name))
144 (with-current-buffer buf-name
145 (goto-char (point-min))
147 (diff-beginning-of-hunk t)
148 (while (looking-at diff-hunk-header-re-unified)
149 (let ((line (string-to-number (match-string 3)))
150 (len (let ((m (match-string 4)))
151 (if m (string-to-number m) 1)))
154 (let* ((inserts (diff-count-matches "^\\+" beg (point)))
155 (deletes (diff-count-matches "^-" beg (point)))
156 (type (cond ((zerop deletes) 'insert)
157 ((zerop inserts) 'delete)
159 (when (eq type 'delete)
162 (push (list line len type) res))))))
165 `((1 ,(line-number-at-pos (point-max)) insert)))
167 `((1 ,(line-number-at-pos (point-max)) delete)))))))
169 (defun diff-hl-update ()
170 (let ((changes (diff-hl-changes))
172 (diff-hl-remove-overlays)
174 (goto-char (point-min))
176 (cl-destructuring-bind (line len type) c
177 (forward-line (- line current-line))
178 (let ((hunk-beg (point)))
180 (setq current-line (+ line len))
181 (let ((h (make-overlay hunk-beg (1- (point))))
182 (hook '(diff-hl-overlay-modified)))
183 (overlay-put h 'diff-hl t)
185 (overlay-put h 'before-string (diff-hl-fringe-spec type 'single))
186 (overlay-put h 'before-string (diff-hl-fringe-spec type 'top))
187 (overlay-put h 'line-prefix (diff-hl-fringe-spec type 'middle))
188 (overlay-put h 'after-string (diff-hl-fringe-spec type 'bottom)))
189 (overlay-put h 'modification-hooks hook)
190 (overlay-put h 'insert-in-front-hooks hook)
191 (overlay-put h 'insert-behind-hooks hook))))))))
193 (defun diff-hl-remove-overlays ()
194 (dolist (o (overlays-in (point-min) (point-max)))
195 (when (overlay-get o 'diff-hl) (delete-overlay o))))
197 (defun diff-hl-overlay-modified (ov after-p _beg _end &optional _length)
198 "Delete the overlay."
200 (when (overlay-buffer ov)
201 (delete-overlay ov))))
203 (defvar diff-hl-timer nil)
205 (defun diff-hl-edit (_beg _end _len)
206 "DTRT when we've `undo'-ne the buffer into unmodified state."
207 (when undo-in-progress
209 (cancel-timer diff-hl-timer))
211 (run-with-idle-timer 0.01 nil #'diff-hl-after-undo (current-buffer)))))
213 (defun diff-hl-after-undo (buffer)
214 (with-current-buffer buffer
215 (unless (buffer-modified-p)
218 (defun diff-hl-diff-goto-hunk ()
219 "Run VC diff command and go to the line corresponding to the current."
222 (let* ((line (line-number-at-pos))
223 (buffer (current-buffer)))
224 (vc-diff-internal t (vc-deduce-fileset) nil nil t)
225 (vc-exec-after `(if (< (line-number-at-pos (point-max)) 3)
226 (with-current-buffer ,buffer (diff-hl-remove-overlays))
227 (diff-hl-diff-skip-to ,line)
228 (setq vc-sentinel-movepoint (point))))))
230 (defun diff-hl-diff-skip-to (line)
231 "In `diff-mode', skip to the hunk and line corresponding to LINE
232 in the source file, or the last line of the hunk above it."
235 (while (and (looking-at diff-hunk-header-re-unified) (not found))
236 (let ((hunk-line (string-to-number (match-string 3)))
237 (len (let ((m (match-string 4)))
238 (if m (string-to-number m) 1))))
239 (if (> line (+ hunk-line len))
242 (if (< line hunk-line)
243 ;; Retreat to the previous hunk.
245 (let ((to-go (1+ (- line hunk-line))))
246 (while (cl-plusp to-go)
248 (unless (looking-at "^-")
249 (cl-decf to-go))))))))))
251 (defun diff-hl-revert-hunk ()
252 "Revert the diff hunk with changes at or above the point."
255 (let ((diff-buffer (generate-new-buffer-name "*diff-hl*"))
256 (buffer (current-buffer))
257 (line (save-excursion
258 (unless (diff-hl-hunk-overlay-at (point))
259 (diff-hl-previous-hunk))
260 (line-number-at-pos)))
261 (fileset (vc-deduce-fileset)))
264 (vc-diff-internal nil fileset nil nil nil diff-buffer)
266 `(let (beg-line end-line)
268 (with-current-buffer ,buffer (diff-hl-remove-overlays))
269 (error "Buffer is up-to-date"))
270 (diff-hl-diff-skip-to ,line)
272 (while (looking-at "[-+]") (forward-line 1))
273 (setq end-line (line-number-at-pos (point)))
274 (unless (eobp) (diff-split-hunk)))
275 (unless (looking-at "[-+]") (forward-line -1))
276 (while (looking-at "[-+]") (forward-line -1))
277 (setq beg-line (line-number-at-pos (point)))
278 (unless (looking-at "@")
281 (let ((wbh (window-body-height)))
282 (if (>= wbh (- end-line beg-line))
283 (recenter (/ (+ wbh (- beg-line end-line) 2) 2))
285 (unless (yes-or-no-p (format "Revert current hunk in %s?"
286 ,(cl-caadr fileset)))
287 (error "Revert canceled"))
288 (let ((diff-advance-after-apply-hunk nil))
290 (with-current-buffer ,buffer
292 (message "Hunk reverted"))))
293 (quit-windows-on diff-buffer))))
295 (defun diff-hl-hunk-overlay-at (pos)
296 (cl-loop for o in (overlays-at pos)
297 when (overlay-get o 'diff-hl)
300 (defun diff-hl-next-hunk (&optional backward)
301 "Go to the beginning of the next hunk in the current buffer."
303 (let ((pos (save-excursion
305 (while (not (if backward (bobp) (eobp)))
306 (goto-char (if backward
307 (previous-overlay-change (point))
308 (next-overlay-change (point))))
309 (let ((o (diff-hl-hunk-overlay-at (point))))
310 (when (and o (if backward
311 (<= (overlay-end o) (1+ (point)))
312 (>= (overlay-start o) (point))))
313 (throw 'found (overlay-start o)))))))))
316 (error "No further hunks found"))))
318 (defun diff-hl-previous-hunk ()
319 "Go to the beginning of the previous hunk in the current buffer."
321 (diff-hl-next-hunk t))
323 (define-minor-mode diff-hl-mode
324 "Toggle VC diff fringe highlighting."
325 :lighter "" :keymap `(([remap vc-diff] . diff-hl-diff-goto-hunk)
326 (,(kbd "C-x v n") . diff-hl-revert-hunk)
327 (,(kbd "C-x v [") . diff-hl-previous-hunk)
328 (,(kbd "C-x v ]") . diff-hl-next-hunk))
331 (add-hook 'after-save-hook 'diff-hl-update nil t)
332 (add-hook 'after-change-functions 'diff-hl-edit nil t)
335 (add-hook 'find-file-hook 'diff-hl-update t t))
336 (add-hook 'vc-checkin-hook 'diff-hl-update nil t)
337 (add-hook 'after-revert-hook 'diff-hl-update nil t)
338 (add-hook 'text-scale-mode-hook 'diff-hl-define-bitmaps nil t))
339 (remove-hook 'after-save-hook 'diff-hl-update t)
340 (remove-hook 'after-change-functions 'diff-hl-edit t)
341 (remove-hook 'find-file-hook 'diff-hl-update t)
342 (remove-hook 'vc-checkin-hook 'diff-hl-update t)
343 (remove-hook 'after-revert-hook 'diff-hl-update t)
344 (remove-hook 'text-scale-mode-hook 'diff-hl-define-bitmaps t)
345 (diff-hl-remove-overlays)))
347 (when (require 'smartrep nil t)
349 (cl-labels ((scan (map)
351 (lambda (event binding)
354 (when (characterp event)
355 (push (cons (string event) binding) smart-keys))))
357 (scan diff-hl-mode-map)
358 (smartrep-define-key diff-hl-mode-map "C-x v" smart-keys))))
360 (defun diff-hl-dir-update ()
361 (dolist (pair (if (vc-dir-marked-files)
362 (vc-dir-marked-only-files-and-states)
363 (vc-dir-child-files-and-states)))
364 (when (eq 'up-to-date (cdr pair))
365 (let ((buffer (find-buffer-visiting (car pair))))
367 (with-current-buffer buffer
368 (diff-hl-remove-overlays)))))))
370 (define-minor-mode diff-hl-dir-mode
371 "Toggle `diff-hl-mode' integration in a `vc-dir-mode' buffer."
374 (add-hook 'vc-checkin-hook 'diff-hl-dir-update t t)
375 (remove-hook 'vc-checkin-hook 'diff-hl-dir-update t)))
378 (defun turn-on-diff-hl-mode ()
379 "Turn on `diff-hl-mode' or `diff-hl-dir-mode' in a buffer if appropriate."
380 (when (window-system) ;; No fringes in the console.
384 ((eq major-mode 'vc-dir-mode)
385 (diff-hl-dir-mode 1)))))
388 (define-globalized-minor-mode global-diff-hl-mode diff-hl-mode
389 turn-on-diff-hl-mode :after-hook (diff-hl-global-mode-change))
391 (defun diff-hl-global-mode-change ()
392 (unless global-diff-hl-mode
393 (dolist (buf (buffer-list))
394 (with-current-buffer buf
395 (when diff-hl-dir-mode
396 (diff-hl-dir-mode -1))))))
400 ;;; diff-hl.el ends here