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
8 ;; This file is not part of GNU Emacs.
10 ;; This file is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; This file is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;; `diff-hl-mode' highlights uncommitted changes on the left fringe of the
26 ;; window, allows you to jump between the hunks and revert them selectively.
30 ;; `diff-hl-diff-goto-hunk' C-x v =
31 ;; `diff-hl-revert-hunk' C-x v n
32 ;; `diff-hl-previous-hunk' C-x v [
33 ;; `diff-hl-next-hunk' C-x v ]
35 ;; The mode takes advantage of `smartrep' if it is installed.
37 ;; Add either of the following to your init file.
39 ;; To use it in all buffers:
41 ;; (global-diff-hl-mode)
43 ;; Only in `prog-mode' buffers, with `vc-dir' integration:
45 ;; (add-hook 'prog-mode-hook 'turn-on-diff-hl-mode)
46 ;; (add-hook 'vc-dir-mode-hook 'turn-on-diff-hl-mode)
57 (require 'face-remap))
60 "VC diff fringe highlighting"
63 (defface diff-hl-insert
64 '((t :inherit diff-added))
65 "Face used to highlight inserted lines."
68 (defface diff-hl-delete
69 '((t :inherit diff-removed))
70 "Face used to highlight deleted lines."
73 (defface diff-hl-change
76 (((class color) (min-colors 88) (background light))
77 :background "#ddddff")
78 (((class color) (min-colors 88) (background dark))
79 :background "#333355"))
80 "Face used to highlight changed lines."
83 (defcustom diff-hl-draw-borders t
84 "Non-nil to draw borders around fringe indicators."
88 (defun diff-hl-define-bitmaps ()
89 (let* ((scale (if (and (boundp 'text-scale-mode-amount)
90 (plusp text-scale-mode-amount))
91 (expt text-scale-mode-step text-scale-mode-amount)
93 (h (round (* (frame-char-height) scale)))
94 (w (frame-parameter nil 'left-fringe))
95 (middle (make-vector h (expt 2 (1- w))))
96 (ones (1- (expt 2 w)))
97 (top (copy-sequence middle))
98 (bottom (copy-sequence middle))
99 (single (copy-sequence middle)))
101 (aset bottom (1- h) ones)
103 (aset single (1- h) ones)
104 (define-fringe-bitmap 'diff-hl-bmp-top top h w 'top)
105 (define-fringe-bitmap 'diff-hl-bmp-middle middle h w 'center)
106 (define-fringe-bitmap 'diff-hl-bmp-bottom bottom h w 'bottom)
107 (define-fringe-bitmap 'diff-hl-bmp-single single h w 'top)))
109 (when (window-system)
110 (define-fringe-bitmap 'diff-hl-bmp-empty [0] 1 1 'center)
111 (diff-hl-define-bitmaps))
113 (defvar diff-hl-spec-cache (make-hash-table :test 'equal))
115 (defun diff-hl-fringe-spec (type pos)
116 (let* ((key (cons type pos))
117 (val (gethash key diff-hl-spec-cache)))
119 (let* ((face-sym (intern (concat "diff-hl-" (symbol-name type))))
120 (bmp-sym (intern (concat "diff-hl-bmp-" (symbol-name pos)))))
121 (setq val (propertize " " 'display `((left-fringe ,bmp-sym ,face-sym))))
122 (puthash key val diff-hl-spec-cache)))
125 (defmacro diff-hl-with-diff-switches (body)
126 `(let ((vc-git-diff-switches nil)
127 (vc-hg-diff-switches nil)
128 (vc-diff-switches '("-U0"))
129 (vc-disable-async-diff t))
132 (defun diff-hl-changes ()
133 (let* ((buf-name " *diff-hl* ")
134 (file buffer-file-name)
135 (backend (vc-backend file))
137 (when (and backend (eq (vc-state file backend) 'edited))
138 (diff-hl-with-diff-switches
139 (vc-call-backend backend 'diff (list file) nil nil buf-name))
140 (with-current-buffer buf-name
141 (goto-char (point-min))
143 (diff-beginning-of-hunk t)
144 (while (looking-at diff-hunk-header-re-unified)
145 (let ((line (string-to-number (match-string 3)))
146 (len (let ((m (match-string 4)))
147 (if m (string-to-number m) 1)))
150 (let* ((inserts (diff-count-matches "^\\+" beg (point)))
151 (deletes (diff-count-matches "^-" beg (point)))
152 (type (cond ((zerop deletes) 'insert)
153 ((zerop inserts) 'delete)
155 (push (list line len type) res)))))))
158 (defun diff-hl-update ()
159 (let ((changes (diff-hl-changes))
161 (diff-hl-remove-overlays)
163 (goto-char (point-min))
165 (destructuring-bind (line len type) c
166 (when (eq type 'delete)
169 (forward-line (- line current-line))
170 (setq current-line line)
171 (let ((hunk-beg (point)))
173 (let ((o (make-overlay (point) (line-end-position))))
174 (overlay-put o 'diff-hl t)
175 (overlay-put o 'before-string
179 ((not diff-hl-draw-borders) 'empty)
180 ((and (= len 1) (= line current-line)) 'single)
182 ((= line current-line) 'top)
187 (let ((h (make-overlay hunk-beg (point)))
188 (hook '(diff-hl-overlay-modified)))
189 (overlay-put h 'diff-hl t)
190 (overlay-put h 'diff-hl-hunk t)
191 (overlay-put h 'modification-hooks hook)
192 (overlay-put h 'insert-in-front-hooks hook))))))))
194 (defun diff-hl-remove-overlays ()
195 (dolist (o (overlays-in (point-min) (point-max)))
196 (when (overlay-get o 'diff-hl) (delete-overlay o))))
198 (defun diff-hl-overlay-modified (ov after-p _beg _end &optional _length)
199 "Delete the overlay and all our overlays inside it."
201 (when (overlay-buffer ov)
203 (narrow-to-region (overlay-start ov) (overlay-end ov))
204 (diff-hl-remove-overlays))
205 (delete-overlay ov))))
207 (defvar diff-hl-timer nil)
209 (defun diff-hl-edit (_beg _end _len)
210 "DTRT when we've `undo'-ne the buffer into unmodified state."
211 (when undo-in-progress
213 (cancel-timer diff-hl-timer))
215 (run-with-idle-timer 0.01 nil #'diff-hl-after-undo (current-buffer)))))
217 (defun diff-hl-after-undo (buffer)
218 (with-current-buffer buffer
219 (unless (buffer-modified-p)
222 (defun diff-hl-diff-goto-hunk ()
223 "Run VC diff command and go to the line corresponding to the current."
226 (let* ((line (line-number-at-pos))
227 (buffer (current-buffer)))
228 (vc-diff-internal t (vc-deduce-fileset) nil nil t)
229 (vc-exec-after `(if (< (line-number-at-pos (point-max)) 3)
230 (with-current-buffer ,buffer (diff-hl-remove-overlays))
231 (diff-hl-diff-skip-to ,line)
232 (setq vc-sentinel-movepoint (point))))))
234 (defun diff-hl-diff-skip-to (line)
235 "In `diff-mode', skip to the hunk and line corresponding to LINE
236 in the source file, or the last line of the hunk above it."
239 (while (and (looking-at diff-hunk-header-re-unified) (not found))
240 (let ((hunk-line (string-to-number (match-string 3)))
241 (len (let ((m (match-string 4)))
242 (if m (string-to-number m) 1))))
243 (if (> line (+ hunk-line len))
246 (if (< line hunk-line)
247 ;; Retreat to the previous hunk.
249 (let ((to-go (1+ (- line hunk-line))))
252 (unless (looking-at "^-")
253 (decf to-go))))))))))
255 (defun diff-hl-revert-hunk ()
256 "Revert the diff hunk with changes at or above the point."
259 (let ((diff-buffer (generate-new-buffer-name "*diff-hl*"))
260 (buffer (current-buffer))
261 (line (save-excursion
262 (unless (diff-hl-hunk-overlay-at (point))
263 (diff-hl-previous-hunk))
264 (line-number-at-pos)))
265 (fileset (vc-deduce-fileset)))
268 (vc-diff-internal nil fileset nil nil nil diff-buffer)
270 `(let (beg-line end-line)
272 (with-current-buffer ,buffer (diff-hl-remove-overlays))
273 (error "Buffer is up-to-date"))
274 (diff-hl-diff-skip-to ,line)
276 (while (looking-at "[-+]") (forward-line 1))
277 (setq end-line (line-number-at-pos (point)))
278 (unless (eobp) (diff-split-hunk)))
279 (unless (looking-at "[-+]") (forward-line -1))
280 (while (looking-at "[-+]") (forward-line -1))
281 (setq beg-line (line-number-at-pos (point)))
282 (unless (looking-at "@")
285 (let ((wbh (window-body-height)))
286 (if (>= wbh (- end-line beg-line))
287 (recenter (/ (+ wbh (- beg-line end-line) 2) 2))
289 (unless (yes-or-no-p (format "Revert current hunk in %s?"
291 (error "Revert canceled"))
292 (let ((diff-advance-after-apply-hunk nil))
294 (with-current-buffer ,buffer
296 (message "Hunk reverted"))))
297 (quit-windows-on diff-buffer))))
299 (defun diff-hl-hunk-overlay-at (pos)
300 (loop for o in (overlays-at pos)
301 when (overlay-get o 'diff-hl-hunk)
304 (defun diff-hl-next-hunk (&optional backward)
305 "Go to the beginning of the next hunk in the current buffer."
307 (let ((pos (save-excursion
309 (while (not (if backward (bobp) (eobp)))
310 (goto-char (if backward
311 (previous-overlay-change (point))
312 (next-overlay-change (point))))
313 (let ((o (diff-hl-hunk-overlay-at (point))))
314 (when (and o (if backward
315 (<= (overlay-end o) (1+ (point)))
316 (>= (overlay-start o) (point))))
317 (throw 'found (overlay-start o)))))))))
320 (error "No further hunks found"))))
322 (defun diff-hl-previous-hunk ()
323 "Go to the beginning of the previous hunk in the current buffer."
325 (diff-hl-next-hunk t))
327 (define-minor-mode diff-hl-mode
328 "Toggle VC diff fringe highlighting."
329 :lighter "" :keymap `(([remap vc-diff] . diff-hl-diff-goto-hunk)
330 (,(kbd "C-x v n") . diff-hl-revert-hunk)
331 (,(kbd "C-x v [") . diff-hl-previous-hunk)
332 (,(kbd "C-x v ]") . diff-hl-next-hunk))
335 (add-hook 'after-save-hook 'diff-hl-update nil t)
336 (add-hook 'after-change-functions 'diff-hl-edit nil t)
339 (add-hook 'find-file-hook 'diff-hl-update t t))
340 (add-hook 'vc-checkin-hook 'diff-hl-update nil t)
341 (add-hook 'after-revert-hook 'diff-hl-update nil t)
342 (add-hook 'text-scale-mode-hook 'diff-hl-define-bitmaps nil t))
343 (remove-hook 'after-save-hook 'diff-hl-update t)
344 (remove-hook 'after-change-functions 'diff-hl-edit t)
345 (remove-hook 'find-file-hook 'diff-hl-update t)
346 (remove-hook 'vc-checkin-hook 'diff-hl-update t)
347 (remove-hook 'after-revert-hook 'diff-hl-update t)
348 (remove-hook 'text-scale-mode-hook 'diff-hl-define-bitmaps t)
349 (diff-hl-remove-overlays)))
351 (when (require 'smartrep nil t)
353 (cl-labels ((scan (map)
355 (lambda (event binding)
358 (when (characterp event)
359 (push (cons (string event) binding) smart-keys))))
361 (scan diff-hl-mode-map)
362 (smartrep-define-key diff-hl-mode-map "C-x v" smart-keys))))
364 (defun diff-hl-dir-update ()
365 (dolist (pair (if (vc-dir-marked-files)
366 (vc-dir-marked-only-files-and-states)
367 (vc-dir-child-files-and-states)))
368 (when (eq 'up-to-date (cdr pair))
369 (let ((buffer (find-buffer-visiting (car pair))))
371 (with-current-buffer buffer
372 (diff-hl-remove-overlays)))))))
374 (define-minor-mode diff-hl-dir-mode
375 "Toggle `diff-hl-mode' integration in a `vc-dir-mode' buffer."
378 (add-hook 'vc-checkin-hook 'diff-hl-dir-update t t)
379 (remove-hook 'vc-checkin-hook 'diff-hl-dir-update t)))
382 (defun turn-on-diff-hl-mode ()
383 "Turn on `diff-hl-mode' or `diff-hl-dir-mode' in a buffer if appropriate."
384 (when (window-system) ;; No fringes in the console.
388 ((eq major-mode 'vc-dir-mode)
389 (diff-hl-dir-mode 1)))))
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))))))
399 (define-globalized-minor-mode global-diff-hl-mode diff-hl-mode
400 turn-on-diff-hl-mode :after-hook (diff-hl-global-mode-change))
404 ;;; diff-hl.el ends here