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-header))
87 "Face used to highlight unregistered files.")
89 (defcustom diff-hl-draw-borders t
90 "Non-nil to draw borders around fringe indicators."
94 (defcustom diff-hl-fringe-bmp-function 'diff-hl-fringe-bmp-from-pos
95 "Function to choose the fringe bitmap for a given change type
96 and position within a hunk. Should accept two arguments."
98 :type '(choice (const diff-hl-fringe-bmp-from-pos)
99 (const diff-hl-fringe-bmp-from-type)
102 (defvar diff-hl-reference-revision nil
103 "Revision to diff against. nil means the most recent one.")
105 (defun diff-hl-define-bitmaps ()
106 (let* ((scale (if (and (boundp 'text-scale-mode-amount)
107 (numberp text-scale-mode-amount))
108 (expt text-scale-mode-step text-scale-mode-amount)
110 (spacing (or (default-value 'line-spacing) 0))
111 (h (round (+ (* (frame-char-height) scale)
113 (* (frame-char-height) spacing)
115 (w (frame-parameter nil 'left-fringe))
116 (middle (make-vector h (expt 2 (1- w))))
117 (ones (1- (expt 2 w)))
118 (top (copy-sequence middle))
119 (bottom (copy-sequence middle))
120 (single (copy-sequence middle)))
122 (aset bottom (1- h) ones)
124 (aset single (1- h) ones)
125 (define-fringe-bitmap 'diff-hl-bmp-top top h w 'top)
126 (define-fringe-bitmap 'diff-hl-bmp-middle middle h w 'center)
127 (define-fringe-bitmap 'diff-hl-bmp-bottom bottom h w 'bottom)
128 (define-fringe-bitmap 'diff-hl-bmp-single single h w 'top)
130 (delete-row (1- (expr 2 w2)))
131 (middle-bit (expt 2 (1- (/ w2 2))))
132 (insert-bmp (make-vector 2 (* 3 middle-bit))))
133 (define-fringe-bitmap 'diff-hl-bmp-delete (make-vector 2 delete-row))
134 (aset insert-bmp middle-bit delete-row)
135 (aset insert-bmp (1+ middle-bit) delete-row)
136 (define-fringe-bitmap 'diff-hl-bmp-insert insert-bmp)
137 (define-fringe-bitmap 'diff-hl-bmp-change (make-vector
138 w (* 3 middle-bit))))))
140 (defvar diff-hl-spec-cache (make-hash-table :test 'equal))
142 (defun diff-hl-fringe-spec (type pos)
143 (let* ((key (list type pos diff-hl-fringe-bmp-function))
144 (val (gethash key diff-hl-spec-cache)))
146 (let* ((face-sym (intern (format "diff-hl-%s" type)))
147 (bmp-sym (funcall diff-hl-fringe-bmp-function type pos)))
148 (setq val (propertize " " 'display `((left-fringe ,bmp-sym ,face-sym))))
149 (puthash key val diff-hl-spec-cache)))
152 (defun diff-hl-fringe-bmp-from-pos (type pos)
153 (intern (format "diff-hl-bmp-%s" pos)))
155 (defun diff-hl-fringe-bmp-from-type (type pos)
156 (if (eq type 'unknown)
158 (intern (format "diff-hl-bmp-%s" type))))
160 (defmacro diff-hl-with-diff-switches (body)
161 `(let ((vc-git-diff-switches nil)
162 (vc-hg-diff-switches nil)
163 (vc-svn-diff-switches nil)
164 (vc-diff-switches '("-U0"))
165 (vc-disable-async-diff t))
168 (defun diff-hl-changes ()
169 (let* ((file buffer-file-name)
170 (backend (vc-backend file)))
172 (let ((state (vc-state file backend)))
174 ((or (eq state 'edited)
175 (and (eq state 'up-to-date)
176 ;; VC state is stale in after-revert-hook.
177 (or revert-buffer-in-progress-p
178 ;; Diffing against an older revision.
179 diff-hl-reference-revision)))
180 (let* ((buf-name " *diff-hl* ")
181 diff-auto-refine-mode
183 (diff-hl-with-diff-switches
184 (vc-call-backend backend 'diff (list file)
185 diff-hl-reference-revision nil
187 (with-current-buffer buf-name
188 (goto-char (point-min))
190 (diff-beginning-of-hunk t)
191 (while (looking-at diff-hunk-header-re-unified)
192 (let ((line (string-to-number (match-string 3)))
193 (len (let ((m (match-string 4)))
194 (if m (string-to-number m) 1)))
197 (let* ((inserts (diff-count-matches "^\\+" beg (point)))
198 (deletes (diff-count-matches "^-" beg (point)))
199 (type (cond ((zerop deletes) 'insert)
200 ((zerop inserts) 'delete)
202 (when (eq type 'delete)
205 (push (list line len type) res))))))
208 `((1 ,(line-number-at-pos (point-max)) insert)))
210 `((1 ,(line-number-at-pos (point-max)) delete))))))))
212 (defun diff-hl-update ()
213 (let ((changes (diff-hl-changes))
215 (diff-hl-remove-overlays)
217 (goto-char (point-min))
219 (cl-destructuring-bind (line len type) c
220 (forward-line (- line current-line))
221 (setq current-line line)
222 (let ((hunk-beg (point)))
223 (while (cl-plusp len)
224 (diff-hl-add-highlighting
227 ((not diff-hl-draw-borders) 'empty)
228 ((and (= len 1) (= line current-line)) 'single)
230 ((= line current-line) 'top)
233 (cl-incf current-line)
235 (let ((h (make-overlay hunk-beg (point)))
236 (hook '(diff-hl-overlay-modified)))
237 (overlay-put h 'diff-hl t)
238 (overlay-put h 'diff-hl-hunk t)
239 (overlay-put h 'modification-hooks hook)
240 (overlay-put h 'insert-in-front-hooks hook)
241 (overlay-put h 'insert-behind-hooks hook))))))))
243 (defun diff-hl-add-highlighting (type shape)
244 (let ((o (make-overlay (point) (point))))
245 (overlay-put o 'diff-hl t)
246 (overlay-put o 'before-string (diff-hl-fringe-spec type shape))
249 (defun diff-hl-remove-overlays ()
250 (dolist (o (overlays-in (point-min) (point-max)))
251 (when (overlay-get o 'diff-hl) (delete-overlay o))))
253 (defun diff-hl-overlay-modified (ov after-p _beg _end &optional _length)
254 "Delete the hunk overlay and all our line overlays inside it."
256 (when (overlay-buffer ov)
258 (narrow-to-region (overlay-start ov) (overlay-end ov))
259 (diff-hl-remove-overlays))
260 (delete-overlay ov))))
262 (defvar diff-hl-timer nil)
264 (defun diff-hl-edit (_beg _end _len)
265 "DTRT when we've `undo'-ne the buffer into unmodified state."
266 (when undo-in-progress
268 (cancel-timer diff-hl-timer))
270 (run-with-idle-timer 0.01 nil #'diff-hl-after-undo (current-buffer)))))
272 (defun diff-hl-after-undo (buffer)
273 (with-current-buffer buffer
274 (unless (buffer-modified-p)
277 (defun diff-hl-diff-goto-hunk ()
278 "Run VC diff command and go to the line corresponding to the current."
281 (let* ((line (line-number-at-pos))
282 (buffer (current-buffer)))
283 (vc-diff-internal t (vc-deduce-fileset) diff-hl-reference-revision nil t)
284 (vc-exec-after `(if (< (line-number-at-pos (point-max)) 3)
285 (with-current-buffer ,buffer (diff-hl-remove-overlays))
286 (diff-hl-diff-skip-to ,line)
287 (setq vc-sentinel-movepoint (point))))))
289 (defun diff-hl-diff-skip-to (line)
290 "In `diff-mode', skip to the hunk and line corresponding to LINE
291 in the source file, or the last line of the hunk above it."
294 (while (and (looking-at diff-hunk-header-re-unified) (not found))
295 (let ((hunk-line (string-to-number (match-string 3)))
296 (len (let ((m (match-string 4)))
297 (if m (string-to-number m) 1))))
298 (if (> line (+ hunk-line len))
301 (if (< line hunk-line)
302 ;; Retreat to the previous hunk.
304 (let ((to-go (1+ (- line hunk-line))))
305 (while (cl-plusp to-go)
307 (unless (looking-at "^-")
308 (cl-decf to-go))))))))))
310 (defun diff-hl-revert-hunk ()
311 "Revert the diff hunk with changes at or above the point."
314 (let ((diff-buffer (generate-new-buffer-name "*diff-hl*"))
315 (buffer (current-buffer))
316 (line (save-excursion
317 (unless (diff-hl-hunk-overlay-at (point))
318 (diff-hl-previous-hunk))
319 (line-number-at-pos)))
320 (fileset (vc-deduce-fileset)))
323 (vc-diff-internal nil fileset diff-hl-reference-revision nil
326 `(let (beg-line end-line)
328 (with-current-buffer ,buffer (diff-hl-remove-overlays))
329 (error "Buffer is up-to-date"))
330 (diff-hl-diff-skip-to ,line)
332 (while (looking-at "[-+]") (forward-line 1))
333 (setq end-line (line-number-at-pos (point)))
334 (unless (eobp) (diff-split-hunk)))
335 (unless (looking-at "[-+]") (forward-line -1))
336 (while (looking-at "[-+]") (forward-line -1))
337 (setq beg-line (line-number-at-pos (point)))
338 (unless (looking-at "@")
341 (let ((wbh (window-body-height)))
342 (if (>= wbh (- end-line beg-line))
343 (recenter (/ (+ wbh (- beg-line end-line) 2) 2))
345 (unless (yes-or-no-p (format "Revert current hunk in %s?"
346 ,(cl-caadr fileset)))
347 (error "Revert canceled"))
348 (let ((diff-advance-after-apply-hunk nil))
350 (with-current-buffer ,buffer
352 (message "Hunk reverted"))))
353 (quit-windows-on diff-buffer))))
355 (defun diff-hl-hunk-overlay-at (pos)
356 (cl-loop for o in (overlays-in pos (1+ pos))
357 when (overlay-get o 'diff-hl-hunk)
360 (defun diff-hl-next-hunk (&optional backward)
361 "Go to the beginning of the next hunk in the current buffer."
363 (let ((pos (save-excursion
365 (while (not (if backward (bobp) (eobp)))
366 (goto-char (if backward
367 (previous-overlay-change (point))
368 (next-overlay-change (point))))
369 (let ((o (diff-hl-hunk-overlay-at (point))))
370 (when (and o (= (overlay-start o) (point)))
371 (throw 'found (overlay-start o)))))))))
374 (error "No further hunks found"))))
376 (defun diff-hl-previous-hunk ()
377 "Go to the beginning of the previous hunk in the current buffer."
379 (diff-hl-next-hunk t))
382 (define-minor-mode diff-hl-mode
383 "Toggle VC diff fringe highlighting."
384 :lighter "" :keymap `(([remap vc-diff] . diff-hl-diff-goto-hunk)
385 (,(kbd "C-x v n") . diff-hl-revert-hunk)
386 (,(kbd "C-x v [") . diff-hl-previous-hunk)
387 (,(kbd "C-x v ]") . diff-hl-next-hunk))
390 (when (window-system) ;; No fringes in the console.
391 (unless (fringe-bitmap-p 'diff-hl-bmp-empty)
392 (diff-hl-define-bitmaps)
393 (define-fringe-bitmap 'diff-hl-bmp-empty [0] 1 1 'center)))
394 (add-hook 'after-save-hook 'diff-hl-update nil t)
395 (add-hook 'after-change-functions 'diff-hl-edit nil t)
398 (add-hook 'find-file-hook 'diff-hl-update t t))
399 (add-hook 'vc-checkin-hook 'diff-hl-update nil t)
400 (add-hook 'after-revert-hook 'diff-hl-update nil t)
401 (add-hook 'text-scale-mode-hook 'diff-hl-define-bitmaps nil t))
402 (remove-hook 'after-save-hook 'diff-hl-update t)
403 (remove-hook 'after-change-functions 'diff-hl-edit t)
404 (remove-hook 'find-file-hook 'diff-hl-update t)
405 (remove-hook 'vc-checkin-hook 'diff-hl-update t)
406 (remove-hook 'after-revert-hook 'diff-hl-update t)
407 (remove-hook 'text-scale-mode-hook 'diff-hl-define-bitmaps t)
408 (diff-hl-remove-overlays)))
410 (when (require 'smartrep nil t)
412 (cl-labels ((scan (map)
414 (lambda (event binding)
417 (when (characterp event)
418 (push (cons (string event) binding) smart-keys))))
420 (scan diff-hl-mode-map)
421 (smartrep-define-key diff-hl-mode-map "C-x v" smart-keys))))
423 (defun diff-hl-dir-update ()
424 (dolist (pair (if (vc-dir-marked-files)
425 (vc-dir-marked-only-files-and-states)
426 (vc-dir-child-files-and-states)))
427 (when (eq 'up-to-date (cdr pair))
428 (let ((buffer (find-buffer-visiting (car pair))))
430 (with-current-buffer buffer
431 (diff-hl-remove-overlays)))))))
433 (define-minor-mode diff-hl-dir-mode
434 "Toggle `diff-hl-mode' integration in a `vc-dir-mode' buffer."
437 (add-hook 'vc-checkin-hook 'diff-hl-dir-update t t)
438 (remove-hook 'vc-checkin-hook 'diff-hl-dir-update t)))
441 (defun turn-on-diff-hl-mode ()
442 "Turn on `diff-hl-mode' or `diff-hl-dir-mode' in a buffer if appropriate."
446 ((eq major-mode 'vc-dir-mode)
447 (diff-hl-dir-mode 1))))
450 (define-globalized-minor-mode global-diff-hl-mode diff-hl-mode
451 turn-on-diff-hl-mode :after-hook (diff-hl-global-mode-change))
453 (defun diff-hl-global-mode-change ()
454 (unless global-diff-hl-mode
455 (dolist (buf (buffer-list))
456 (with-current-buffer buf
457 (when diff-hl-dir-mode
458 (diff-hl-dir-mode -1))))))
462 ;;; diff-hl.el ends here