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-Depends: ((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 '((t :inherit diff-added))
66 "Face used to highlight inserted lines."
69 (defface diff-hl-delete
70 '((t :inherit diff-removed))
71 "Face used to highlight deleted lines."
74 (defface diff-hl-change
77 (((class color) (min-colors 88) (background light))
78 :background "#ddddff")
79 (((class color) (min-colors 88) (background dark))
80 :background "#333355"))
81 "Face used to highlight changed lines."
84 (defcustom diff-hl-draw-borders t
85 "Non-nil to draw borders around fringe indicators."
89 (defun diff-hl-define-bitmaps ()
90 (let* ((scale (if (and (boundp 'text-scale-mode-amount)
91 (cl-plusp text-scale-mode-amount))
92 (expt text-scale-mode-step text-scale-mode-amount)
94 (h (round (* (frame-char-height) scale)))
95 (w (frame-parameter nil 'left-fringe))
96 (middle (make-vector h (expt 2 (1- w))))
97 (ones (1- (expt 2 w)))
98 (top (copy-sequence middle))
99 (bottom (copy-sequence middle))
100 (single (copy-sequence middle)))
102 (aset bottom (1- h) ones)
104 (aset single (1- h) ones)
105 (define-fringe-bitmap 'diff-hl-bmp-top top h w 'top)
106 (define-fringe-bitmap 'diff-hl-bmp-middle middle h w 'center)
107 (define-fringe-bitmap 'diff-hl-bmp-bottom bottom h w 'bottom)
108 (define-fringe-bitmap 'diff-hl-bmp-single single h w 'top)))
110 (when (window-system)
111 (define-fringe-bitmap 'diff-hl-bmp-empty [0] 1 1 'center)
112 (diff-hl-define-bitmaps))
114 (defvar diff-hl-spec-cache (make-hash-table :test 'equal))
116 (defun diff-hl-fringe-spec (type pos)
117 (let* ((key (cons type pos))
118 (val (gethash key diff-hl-spec-cache)))
120 (let* ((face-sym (intern (concat "diff-hl-" (symbol-name type))))
121 (bmp-sym (intern (concat "diff-hl-bmp-" (symbol-name pos)))))
122 (setq val (propertize " " 'display `((left-fringe ,bmp-sym ,face-sym))))
123 (puthash key val diff-hl-spec-cache)))
126 (defmacro diff-hl-with-diff-switches (body)
127 `(let ((vc-git-diff-switches nil)
128 (vc-hg-diff-switches nil)
129 (vc-diff-switches '("-U0"))
130 (vc-disable-async-diff t))
133 (defun diff-hl-changes ()
134 (let* ((buf-name " *diff-hl* ")
135 (file buffer-file-name)
136 (backend (vc-backend file))
138 (when (and backend (eq (vc-state file backend) 'edited))
139 (diff-hl-with-diff-switches
140 (vc-call-backend backend 'diff (list file) nil nil buf-name))
141 (with-current-buffer buf-name
142 (goto-char (point-min))
144 (diff-beginning-of-hunk t)
145 (while (looking-at diff-hunk-header-re-unified)
146 (let ((line (string-to-number (match-string 3)))
147 (len (let ((m (match-string 4)))
148 (if m (string-to-number m) 1)))
151 (let* ((inserts (diff-count-matches "^\\+" beg (point)))
152 (deletes (diff-count-matches "^-" beg (point)))
153 (type (cond ((zerop deletes) 'insert)
154 ((zerop inserts) 'delete)
156 (push (list line len type) res)))))))
159 (defun diff-hl-update ()
160 (let ((changes (diff-hl-changes))
162 (diff-hl-remove-overlays)
164 (goto-char (point-min))
166 (cl-destructuring-bind (line len type) c
167 (when (eq type 'delete)
170 (forward-line (- line current-line))
171 (setq current-line line)
172 (let ((hunk-beg (point)))
173 (while (cl-plusp len)
174 (let ((o (make-overlay (point) (line-end-position))))
175 (overlay-put o 'diff-hl t)
176 (overlay-put o 'before-string
180 ((not diff-hl-draw-borders) 'empty)
181 ((and (= len 1) (= line current-line)) 'single)
183 ((= line current-line) 'top)
186 (cl-incf current-line)
188 (let ((h (make-overlay hunk-beg (point)))
189 (hook '(diff-hl-overlay-modified)))
190 (overlay-put h 'diff-hl t)
191 (overlay-put h 'diff-hl-hunk t)
192 (overlay-put h 'modification-hooks hook)
193 (overlay-put h 'insert-in-front-hooks hook))))))))
195 (defun diff-hl-remove-overlays ()
196 (dolist (o (overlays-in (point-min) (point-max)))
197 (when (overlay-get o 'diff-hl) (delete-overlay o))))
199 (defun diff-hl-overlay-modified (ov after-p _beg _end &optional _length)
200 "Delete the overlay and all our overlays inside it."
202 (when (overlay-buffer ov)
204 (narrow-to-region (overlay-start ov) (overlay-end ov))
205 (diff-hl-remove-overlays))
206 (delete-overlay ov))))
208 (defvar diff-hl-timer nil)
210 (defun diff-hl-edit (_beg _end _len)
211 "DTRT when we've `undo'-ne the buffer into unmodified state."
212 (when undo-in-progress
214 (cancel-timer diff-hl-timer))
216 (run-with-idle-timer 0.01 nil #'diff-hl-after-undo (current-buffer)))))
218 (defun diff-hl-after-undo (buffer)
219 (with-current-buffer buffer
220 (unless (buffer-modified-p)
223 (defun diff-hl-diff-goto-hunk ()
224 "Run VC diff command and go to the line corresponding to the current."
227 (let* ((line (line-number-at-pos))
228 (buffer (current-buffer)))
229 (vc-diff-internal t (vc-deduce-fileset) nil nil t)
230 (vc-exec-after `(if (< (line-number-at-pos (point-max)) 3)
231 (with-current-buffer ,buffer (diff-hl-remove-overlays))
232 (diff-hl-diff-skip-to ,line)
233 (setq vc-sentinel-movepoint (point))))))
235 (defun diff-hl-diff-skip-to (line)
236 "In `diff-mode', skip to the hunk and line corresponding to LINE
237 in the source file, or the last line of the hunk above it."
240 (while (and (looking-at diff-hunk-header-re-unified) (not found))
241 (let ((hunk-line (string-to-number (match-string 3)))
242 (len (let ((m (match-string 4)))
243 (if m (string-to-number m) 1))))
244 (if (> line (+ hunk-line len))
247 (if (< line hunk-line)
248 ;; Retreat to the previous hunk.
250 (let ((to-go (1+ (- line hunk-line))))
251 (while (cl-plusp to-go)
253 (unless (looking-at "^-")
254 (cl-decf to-go))))))))))
256 (defun diff-hl-revert-hunk ()
257 "Revert the diff hunk with changes at or above the point."
260 (let ((diff-buffer (generate-new-buffer-name "*diff-hl*"))
261 (buffer (current-buffer))
262 (line (save-excursion
263 (unless (diff-hl-hunk-overlay-at (point))
264 (diff-hl-previous-hunk))
265 (line-number-at-pos)))
266 (fileset (vc-deduce-fileset)))
269 (vc-diff-internal nil fileset nil nil nil diff-buffer)
271 `(let (beg-line end-line)
273 (with-current-buffer ,buffer (diff-hl-remove-overlays))
274 (error "Buffer is up-to-date"))
275 (diff-hl-diff-skip-to ,line)
277 (while (looking-at "[-+]") (forward-line 1))
278 (setq end-line (line-number-at-pos (point)))
279 (unless (eobp) (diff-split-hunk)))
280 (unless (looking-at "[-+]") (forward-line -1))
281 (while (looking-at "[-+]") (forward-line -1))
282 (setq beg-line (line-number-at-pos (point)))
283 (unless (looking-at "@")
286 (let ((wbh (window-body-height)))
287 (if (>= wbh (- end-line beg-line))
288 (recenter (/ (+ wbh (- beg-line end-line) 2) 2))
290 (unless (yes-or-no-p (format "Revert current hunk in %s?"
291 ,(cl-caadr fileset)))
292 (error "Revert canceled"))
293 (let ((diff-advance-after-apply-hunk nil))
295 (with-current-buffer ,buffer
297 (message "Hunk reverted"))))
298 (quit-windows-on diff-buffer))))
300 (defun diff-hl-hunk-overlay-at (pos)
301 (cl-loop for o in (overlays-at pos)
302 when (overlay-get o 'diff-hl-hunk)
305 (defun diff-hl-next-hunk (&optional backward)
306 "Go to the beginning of the next hunk in the current buffer."
308 (let ((pos (save-excursion
310 (while (not (if backward (bobp) (eobp)))
311 (goto-char (if backward
312 (previous-overlay-change (point))
313 (next-overlay-change (point))))
314 (let ((o (diff-hl-hunk-overlay-at (point))))
315 (when (and o (if backward
316 (<= (overlay-end o) (1+ (point)))
317 (>= (overlay-start o) (point))))
318 (throw 'found (overlay-start o)))))))))
321 (error "No further hunks found"))))
323 (defun diff-hl-previous-hunk ()
324 "Go to the beginning of the previous hunk in the current buffer."
326 (diff-hl-next-hunk t))
328 (define-minor-mode diff-hl-mode
329 "Toggle VC diff fringe highlighting."
330 :lighter "" :keymap `(([remap vc-diff] . diff-hl-diff-goto-hunk)
331 (,(kbd "C-x v n") . diff-hl-revert-hunk)
332 (,(kbd "C-x v [") . diff-hl-previous-hunk)
333 (,(kbd "C-x v ]") . diff-hl-next-hunk))
336 (add-hook 'after-save-hook 'diff-hl-update nil t)
337 (add-hook 'after-change-functions 'diff-hl-edit nil t)
340 (add-hook 'find-file-hook 'diff-hl-update t t))
341 (add-hook 'vc-checkin-hook 'diff-hl-update nil t)
342 (add-hook 'after-revert-hook 'diff-hl-update nil t)
343 (add-hook 'text-scale-mode-hook 'diff-hl-define-bitmaps nil t))
344 (remove-hook 'after-save-hook 'diff-hl-update t)
345 (remove-hook 'after-change-functions 'diff-hl-edit t)
346 (remove-hook 'find-file-hook 'diff-hl-update t)
347 (remove-hook 'vc-checkin-hook 'diff-hl-update t)
348 (remove-hook 'after-revert-hook 'diff-hl-update t)
349 (remove-hook 'text-scale-mode-hook 'diff-hl-define-bitmaps t)
350 (diff-hl-remove-overlays)))
352 (when (require 'smartrep nil t)
354 (cl-labels ((scan (map)
356 (lambda (event binding)
359 (when (characterp event)
360 (push (cons (string event) binding) smart-keys))))
362 (scan diff-hl-mode-map)
363 (smartrep-define-key diff-hl-mode-map "C-x v" smart-keys))))
365 (defun diff-hl-dir-update ()
366 (dolist (pair (if (vc-dir-marked-files)
367 (vc-dir-marked-only-files-and-states)
368 (vc-dir-child-files-and-states)))
369 (when (eq 'up-to-date (cdr pair))
370 (let ((buffer (find-buffer-visiting (car pair))))
372 (with-current-buffer buffer
373 (diff-hl-remove-overlays)))))))
375 (define-minor-mode diff-hl-dir-mode
376 "Toggle `diff-hl-mode' integration in a `vc-dir-mode' buffer."
379 (add-hook 'vc-checkin-hook 'diff-hl-dir-update t t)
380 (remove-hook 'vc-checkin-hook 'diff-hl-dir-update t)))
383 (defun turn-on-diff-hl-mode ()
384 "Turn on `diff-hl-mode' or `diff-hl-dir-mode' in a buffer if appropriate."
385 (when (window-system) ;; No fringes in the console.
389 ((eq major-mode 'vc-dir-mode)
390 (diff-hl-dir-mode 1)))))
392 (defun diff-hl-global-mode-change ()
393 (unless global-diff-hl-mode
394 (dolist (buf (buffer-list))
395 (with-current-buffer buf
396 (when diff-hl-dir-mode
397 (diff-hl-dir-mode -1))))))
400 (define-globalized-minor-mode global-diff-hl-mode diff-hl-mode
401 turn-on-diff-hl-mode :after-hook (diff-hl-global-mode-change))
405 ;;; diff-hl.el ends here