1 ;;; diff-hl.el --- VC diff highlighting in the buffer window
3 ;; Author: Dmitry Gutov <dgutov@yandex.ru>
6 ;; This file is not part of GNU Emacs.
8 ;; This file is free software: you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation, either version 3 of the License, or
11 ;; (at your option) any later version.
13 ;; This file is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 (eval-when-compile (require 'cl))
25 (defface diff-hl-insert
26 '((t :inherit diff-added))
27 "Face used to highlight inserted lines."
30 (defface diff-hl-delete
31 '((t :inherit diff-removed))
32 "Face used to highlight deleted lines."
35 (defface diff-hl-change
36 '((((class color) (min-colors 88) (background light))
37 :background "#ddddff")
38 (((class color) (min-colors 88) (background dark))
39 :background "#333355"))
40 "Face used to highlight changed lines."
43 (define-fringe-bitmap 'diff-hl-empty [0] 1 1 'center)
45 (defun diff-hl-changes ()
46 (let* ((buf-name " *vc-diff-hl* ")
47 (vc-git-diff-switches nil)
48 (vc-hg-diff-switches nil)
49 (vc-diff-switches '("-U0"))
50 (vc-disable-async-diff t)
51 (file (buffer-file-name))
52 (backend (vc-backend file))
55 (vc-call-backend backend 'diff (list file) nil nil buf-name)
56 (with-current-buffer buf-name
57 (goto-char (point-min))
59 (diff-beginning-of-hunk t)
60 (while (looking-at diff-hunk-header-re-unified)
61 (let ((line (string-to-number (match-string 3)))
62 (len (let ((m (match-string 4)))
63 (if m (string-to-number m) 1)))
66 (let* ((inserts (diff-count-matches "^\\+" beg (point)))
67 (deletes (diff-count-matches "^-" beg (point)))
68 (type (cond ((zerop deletes) 'insert)
69 ((zerop inserts) 'delete)
71 (push (list line len type) res)))))))
75 (dolist (type '(insert delete change))
76 (let* ((type-str (symbol-name type))
77 (spec-sym (intern (concat "diff-hl-" type-str "-spec")))
78 (face-sym (intern (concat "diff-hl-" type-str))))
79 (eval `(defconst ,spec-sym
80 ,(propertize " " 'display
81 `((left-fringe diff-hl-empty ,face-sym))))))))
83 (defun diff-hl-update ()
84 (let ((changes (diff-hl-changes))
87 (goto-char (point-min))
88 (mapc (lambda (o) (when (overlay-get o 'diff-hl) (delete-overlay o)))
89 (overlays-in (point-min) (point-max)))
91 (destructuring-bind (line len type) c
92 (when (eq type 'delete)
95 (forward-line (- line current-line))
96 (setq current-line line)
98 (let ((o (make-overlay (point) (line-end-position))))
99 (overlay-put o 'diff-hl t)
100 (overlay-put o 'before-string
102 ('insert diff-hl-insert-spec)
103 ('delete diff-hl-delete-spec)
104 ('change diff-hl-change-spec)))
105 (overlay-put o 'modification-hooks '(diff-hl-overlay-modified))
106 (overlay-put o 'insert-in-front-hooks '(diff-hl-overlay-modified)))
111 (defun diff-hl-overlay-modified (ov after-p beg end &optional length)
112 ;; Do the simplest possible thing, for now.
113 (when after-p (delete-overlay ov)))
115 (defvar diff-hl-timer nil)
117 (defun diff-hl-edit (beg end len)
118 ;; DTRT when we've `undo'-ed the buffer into unmodified state.
119 (when undo-in-progress
121 (cancel-timer diff-hl-timer))
123 (run-with-idle-timer 0.01 nil #'diff-hl-after-undo (current-buffer)))))
125 (defun diff-hl-after-undo (buffer)
126 (with-current-buffer buffer
127 (unless (buffer-modified-p)
131 (define-minor-mode diff-hl-mode
132 "Toggle display of VC diff indicators in the left fringe."
133 :after-hook (diff-hl-update)
136 (add-hook 'after-save-hook 'diff-hl-update nil t)
137 (add-hook 'after-change-functions 'diff-hl-edit nil t))
138 (remove-hook 'after-save-hook 'diff-hl-update t)
139 (remove-hook 'after-change-functions 'diff-hl-edit t)))
141 (defun turn-on-diff-hl-mode ()
142 (when (buffer-file-name)
146 (define-globalized-minor-mode global-diff-hl-mode diff-hl-mode
147 turn-on-diff-hl-mode)