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 (defvar diff-hl-spec-cache (make-hash-table :test 'equal))
113 (defun diff-hl-fringe-spec (type pos)
114 (let* ((key (cons type pos))
115 (val (gethash key diff-hl-spec-cache)))
117 (let* ((face-sym (intern (concat "diff-hl-" (symbol-name type))))
118 (bmp-sym (intern (concat "diff-hl-bmp-" (symbol-name pos)))))
119 (setq val (propertize " " 'display `((left-fringe ,bmp-sym ,face-sym))))
120 (puthash key val diff-hl-spec-cache)))
123 (defmacro diff-hl-with-diff-switches (body)
124 `(let ((vc-git-diff-switches nil)
125 (vc-hg-diff-switches nil)
126 (vc-svn-diff-switches nil)
127 (vc-diff-switches '("-U0"))
128 (vc-disable-async-diff t))
131 (defun diff-hl-changes ()
132 (let* ((file buffer-file-name)
133 (backend (vc-backend file)))
135 (let ((state (vc-state file backend)))
137 ((or (eq state 'edited)
138 (and (eq state 'up-to-date)
139 ;; VC state is stale in after-revert-hook.
140 revert-buffer-in-progress-p))
141 (let* ((buf-name " *diff-hl* ")
143 (diff-hl-with-diff-switches
144 (vc-call-backend backend 'diff (list file) nil nil buf-name))
145 (with-current-buffer buf-name
146 (goto-char (point-min))
148 (diff-beginning-of-hunk t)
149 (while (looking-at diff-hunk-header-re-unified)
150 (let ((line (string-to-number (match-string 3)))
151 (len (let ((m (match-string 4)))
152 (if m (string-to-number m) 1)))
155 (let* ((inserts (diff-count-matches "^\\+" beg (point)))
156 (deletes (diff-count-matches "^-" beg (point)))
157 (type (cond ((zerop deletes) 'insert)
158 ((zerop inserts) 'delete)
160 (when (eq type 'delete)
163 (push (list line len type) res))))))
166 `((1 ,(line-number-at-pos (point-max)) insert)))
168 `((1 ,(line-number-at-pos (point-max)) delete))))))))
170 (defun diff-hl-update ()
171 (let ((changes (diff-hl-changes))
173 (diff-hl-remove-overlays)
175 (goto-char (point-min))
177 (cl-destructuring-bind (line len type) c
178 (forward-line (- line current-line))
179 (setq current-line line)
180 (let ((hunk-beg (point)))
181 (while (cl-plusp len)
182 (let ((o (make-overlay (point) (point))))
183 (overlay-put o 'diff-hl t)
184 (overlay-put o 'before-string
188 ((not diff-hl-draw-borders) 'empty)
189 ((and (= len 1) (= line current-line)) 'single)
191 ((= line current-line) 'top)
194 (cl-incf current-line)
196 (let ((h (make-overlay hunk-beg (point)))
197 (hook '(diff-hl-overlay-modified)))
198 (overlay-put h 'diff-hl t)
199 (overlay-put h 'diff-hl-hunk t)
200 (overlay-put h 'modification-hooks hook)
201 (overlay-put h 'insert-in-front-hooks hook)
202 (overlay-put h 'insert-behind-hooks hook))))))))
204 (defun diff-hl-remove-overlays ()
205 (dolist (o (overlays-in (point-min) (point-max)))
206 (when (overlay-get o 'diff-hl) (delete-overlay o))))
208 (defun diff-hl-overlay-modified (ov after-p _beg _end &optional _length)
209 "Delete the hunk overlay and all our line overlays inside it."
211 (when (overlay-buffer ov)
213 (narrow-to-region (overlay-start ov) (overlay-end ov))
214 (diff-hl-remove-overlays))
215 (delete-overlay ov))))
217 (defvar diff-hl-timer nil)
219 (defun diff-hl-edit (_beg _end _len)
220 "DTRT when we've `undo'-ne the buffer into unmodified state."
221 (when undo-in-progress
223 (cancel-timer diff-hl-timer))
225 (run-with-idle-timer 0.01 nil #'diff-hl-after-undo (current-buffer)))))
227 (defun diff-hl-after-undo (buffer)
228 (with-current-buffer buffer
229 (unless (buffer-modified-p)
232 (defun diff-hl-diff-goto-hunk ()
233 "Run VC diff command and go to the line corresponding to the current."
236 (let* ((line (line-number-at-pos))
237 (buffer (current-buffer)))
238 (vc-diff-internal t (vc-deduce-fileset) nil nil t)
239 (vc-exec-after `(if (< (line-number-at-pos (point-max)) 3)
240 (with-current-buffer ,buffer (diff-hl-remove-overlays))
241 (diff-hl-diff-skip-to ,line)
242 (setq vc-sentinel-movepoint (point))))))
244 (defun diff-hl-diff-skip-to (line)
245 "In `diff-mode', skip to the hunk and line corresponding to LINE
246 in the source file, or the last line of the hunk above it."
249 (while (and (looking-at diff-hunk-header-re-unified) (not found))
250 (let ((hunk-line (string-to-number (match-string 3)))
251 (len (let ((m (match-string 4)))
252 (if m (string-to-number m) 1))))
253 (if (> line (+ hunk-line len))
256 (if (< line hunk-line)
257 ;; Retreat to the previous hunk.
259 (let ((to-go (1+ (- line hunk-line))))
260 (while (cl-plusp to-go)
262 (unless (looking-at "^-")
263 (cl-decf to-go))))))))))
265 (defun diff-hl-revert-hunk ()
266 "Revert the diff hunk with changes at or above the point."
269 (let ((diff-buffer (generate-new-buffer-name "*diff-hl*"))
270 (buffer (current-buffer))
271 (line (save-excursion
272 (unless (diff-hl-hunk-overlay-at (point))
273 (diff-hl-previous-hunk))
274 (line-number-at-pos)))
275 (fileset (vc-deduce-fileset)))
278 (vc-diff-internal nil fileset nil nil nil diff-buffer)
280 `(let (beg-line end-line)
282 (with-current-buffer ,buffer (diff-hl-remove-overlays))
283 (error "Buffer is up-to-date"))
284 (diff-hl-diff-skip-to ,line)
286 (while (looking-at "[-+]") (forward-line 1))
287 (setq end-line (line-number-at-pos (point)))
288 (unless (eobp) (diff-split-hunk)))
289 (unless (looking-at "[-+]") (forward-line -1))
290 (while (looking-at "[-+]") (forward-line -1))
291 (setq beg-line (line-number-at-pos (point)))
292 (unless (looking-at "@")
295 (let ((wbh (window-body-height)))
296 (if (>= wbh (- end-line beg-line))
297 (recenter (/ (+ wbh (- beg-line end-line) 2) 2))
299 (unless (yes-or-no-p (format "Revert current hunk in %s?"
300 ,(cl-caadr fileset)))
301 (error "Revert canceled"))
302 (let ((diff-advance-after-apply-hunk nil))
304 (with-current-buffer ,buffer
306 (message "Hunk reverted"))))
307 (quit-windows-on diff-buffer))))
309 (defun diff-hl-hunk-overlay-at (pos)
310 (cl-loop for o in (overlays-in pos (1+ pos))
311 when (overlay-get o 'diff-hl-hunk)
314 (defun diff-hl-next-hunk (&optional backward)
315 "Go to the beginning of the next hunk in the current buffer."
317 (let ((pos (save-excursion
319 (while (not (if backward (bobp) (eobp)))
320 (goto-char (if backward
321 (previous-overlay-change (point))
322 (next-overlay-change (point))))
323 (let ((o (diff-hl-hunk-overlay-at (point))))
324 (when (and o (= (overlay-start o) (point)))
325 (throw 'found (overlay-start o)))))))))
328 (error "No further hunks found"))))
330 (defun diff-hl-previous-hunk ()
331 "Go to the beginning of the previous hunk in the current buffer."
333 (diff-hl-next-hunk t))
335 (define-minor-mode diff-hl-mode
336 "Toggle VC diff fringe highlighting."
337 :lighter "" :keymap `(([remap vc-diff] . diff-hl-diff-goto-hunk)
338 (,(kbd "C-x v n") . diff-hl-revert-hunk)
339 (,(kbd "C-x v [") . diff-hl-previous-hunk)
340 (,(kbd "C-x v ]") . diff-hl-next-hunk))
343 (when (window-system) ;; No fringes in the console.
344 (unless (fringe-bitmap-p 'diff-hl-bmp-empty)
345 (diff-hl-define-bitmaps)
346 (define-fringe-bitmap 'diff-hl-bmp-empty [0] 1 1 'center)))
347 (add-hook 'after-save-hook 'diff-hl-update nil t)
348 (add-hook 'after-change-functions 'diff-hl-edit nil t)
351 (add-hook 'find-file-hook 'diff-hl-update t t))
352 (add-hook 'vc-checkin-hook 'diff-hl-update nil t)
353 (add-hook 'after-revert-hook 'diff-hl-update nil t)
354 (add-hook 'text-scale-mode-hook 'diff-hl-define-bitmaps nil t))
355 (remove-hook 'after-save-hook 'diff-hl-update t)
356 (remove-hook 'after-change-functions 'diff-hl-edit t)
357 (remove-hook 'find-file-hook 'diff-hl-update t)
358 (remove-hook 'vc-checkin-hook 'diff-hl-update t)
359 (remove-hook 'after-revert-hook 'diff-hl-update t)
360 (remove-hook 'text-scale-mode-hook 'diff-hl-define-bitmaps t)
361 (diff-hl-remove-overlays)))
363 (when (require 'smartrep nil t)
365 (cl-labels ((scan (map)
367 (lambda (event binding)
370 (when (characterp event)
371 (push (cons (string event) binding) smart-keys))))
373 (scan diff-hl-mode-map)
374 (smartrep-define-key diff-hl-mode-map "C-x v" smart-keys))))
376 (defun diff-hl-dir-update ()
377 (dolist (pair (if (vc-dir-marked-files)
378 (vc-dir-marked-only-files-and-states)
379 (vc-dir-child-files-and-states)))
380 (when (eq 'up-to-date (cdr pair))
381 (let ((buffer (find-buffer-visiting (car pair))))
383 (with-current-buffer buffer
384 (diff-hl-remove-overlays)))))))
386 (define-minor-mode diff-hl-dir-mode
387 "Toggle `diff-hl-mode' integration in a `vc-dir-mode' buffer."
390 (add-hook 'vc-checkin-hook 'diff-hl-dir-update t t)
391 (remove-hook 'vc-checkin-hook 'diff-hl-dir-update t)))
394 (defun turn-on-diff-hl-mode ()
395 "Turn on `diff-hl-mode' or `diff-hl-dir-mode' in a buffer if appropriate."
399 ((eq major-mode 'vc-dir-mode)
400 (diff-hl-dir-mode 1))))
403 (define-globalized-minor-mode global-diff-hl-mode diff-hl-mode
404 turn-on-diff-hl-mode :after-hook (diff-hl-global-mode-change))
406 (defun diff-hl-global-mode-change ()
407 (unless global-diff-hl-mode
408 (dolist (buf (buffer-list))
409 (with-current-buffer buf
410 (when diff-hl-dir-mode
411 (diff-hl-dir-mode -1))))))
415 ;;; diff-hl.el ends here