]> code.delx.au - gnu-emacs-elpa/blob - diff-hl.el
498b3060ba4fc415be1348f1bbfdb96b40a90d99
[gnu-emacs-elpa] / diff-hl.el
1 ;;; diff-hl.el --- VC diff fringe highlighting -*- lexical-binding: t -*-
2
3 ;; Author: Dmitry Gutov <dgutov@yandex.ru>
4 ;; Keywords: vc, diff
5
6 ;; This file is not part of GNU Emacs.
7
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.
12
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.
17
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/>.
20
21 (require 'diff-mode)
22 (require 'vc)
23 (eval-when-compile
24 (require 'cl)
25 (require 'vc-git)
26 (require 'vc-hg))
27
28 (defface diff-hl-insert
29 '((t :inherit diff-added))
30 "Face used to highlight inserted lines."
31 :group 'diff-hl)
32
33 (defface diff-hl-delete
34 '((t :inherit diff-removed))
35 "Face used to highlight deleted lines."
36 :group 'diff-hl)
37
38 (defface diff-hl-change
39 '((((class color) (min-colors 88) (background light))
40 :background "#ddddff")
41 (((class color) (min-colors 88) (background dark))
42 :background "#333355"))
43 "Face used to highlight changed lines."
44 :group 'diff-hl)
45
46 (define-fringe-bitmap 'diff-hl-empty [0] 1 1 'center)
47
48 (defun diff-hl-changes ()
49 (let* ((buf-name " *vc-diff-hl* ")
50 (vc-git-diff-switches nil)
51 (vc-hg-diff-switches nil)
52 (vc-diff-switches '("-U0"))
53 (vc-disable-async-diff t)
54 (file (buffer-file-name))
55 (backend (vc-backend file))
56 res)
57 (when backend
58 (vc-call-backend backend 'diff (list file) nil nil buf-name)
59 (with-current-buffer buf-name
60 (goto-char (point-min))
61 (unless (eobp)
62 (diff-beginning-of-hunk t)
63 (while (looking-at diff-hunk-header-re-unified)
64 (let ((line (string-to-number (match-string 3)))
65 (len (let ((m (match-string 4)))
66 (if m (string-to-number m) 1)))
67 (beg (point)))
68 (diff-end-of-hunk)
69 (let* ((inserts (diff-count-matches "^\\+" beg (point)))
70 (deletes (diff-count-matches "^-" beg (point)))
71 (type (cond ((zerop deletes) 'insert)
72 ((zerop inserts) 'delete)
73 (t 'change))))
74 (push (list line len type) res)))))))
75 (nreverse res)))
76
77 (eval-and-compile
78 (dolist (type '(insert delete change))
79 (let* ((type-str (symbol-name type))
80 (spec-sym (intern (concat "diff-hl-" type-str "-spec")))
81 (face-sym (intern (concat "diff-hl-" type-str))))
82 (eval `(defconst ,spec-sym
83 ,(propertize " " 'display
84 `((left-fringe diff-hl-empty ,face-sym))))))))
85
86 (defun diff-hl-update ()
87 (let ((changes (diff-hl-changes))
88 (current-line 1))
89 (diff-hl-remove-overlays)
90 (save-excursion
91 (goto-char (point-min))
92 (dolist (c changes)
93 (destructuring-bind (line len type) c
94 (when (eq type 'delete)
95 (setq len 1)
96 (incf line))
97 (forward-line (- line current-line))
98 (setq current-line line)
99 (while (plusp len)
100 (let ((o (make-overlay (point) (line-end-position))))
101 (overlay-put o 'diff-hl t)
102 (overlay-put o 'before-string
103 (case type
104 ('insert diff-hl-insert-spec)
105 ('delete diff-hl-delete-spec)
106 ('change diff-hl-change-spec)))
107 (overlay-put o 'modification-hooks '(diff-hl-overlay-modified))
108 (overlay-put o 'insert-in-front-hooks '(diff-hl-overlay-modified)))
109 (forward-line 1)
110 (incf current-line)
111 (decf len)))))))
112
113 (defun diff-hl-remove-overlays ()
114 (dolist (o (overlays-in (point-min) (point-max)))
115 (when (overlay-get o 'diff-hl) (delete-overlay o))))
116
117 (defun diff-hl-overlay-modified (ov after-p _beg _end &optional _length)
118 ;; Do the simplest possible thing, for now.
119 (when after-p (delete-overlay ov)))
120
121 (defvar diff-hl-timer nil)
122
123 (defun diff-hl-edit (_beg _end _len)
124 ;; DTRT when we've `undo'-ed the buffer into unmodified state.
125 (when undo-in-progress
126 (when diff-hl-timer
127 (cancel-timer diff-hl-timer))
128 (setq diff-hl-timer
129 (run-with-idle-timer 0.01 nil #'diff-hl-after-undo (current-buffer)))))
130
131 (defun diff-hl-after-undo (buffer)
132 (with-current-buffer buffer
133 (unless (buffer-modified-p)
134 (diff-hl-update))))
135
136 ;;;###autoload
137 (define-minor-mode diff-hl-mode
138 "Toggle display of VC diff indicators in the left fringe."
139 :lighter ""
140 (if diff-hl-mode
141 (progn
142 (add-hook 'after-save-hook 'diff-hl-update nil t)
143 (add-hook 'after-change-functions 'diff-hl-edit nil t)
144 (if vc-mode
145 (diff-hl-update)
146 (add-hook 'find-file-hook 'diff-hl-update t t)))
147 (remove-hook 'after-save-hook 'diff-hl-update t)
148 (remove-hook 'after-change-functions 'diff-hl-edit t)
149 (remove-hook 'find-file-hook 'diff-hl-update t)
150 (diff-hl-remove-overlays)))
151
152 (defun turn-on-diff-hl-mode ()
153 ;; FIXME: Why is this called twice for each buffer?
154 ;; Isn't fundamental-mode supposed to not run any hooks?
155 (when (and buffer-file-name (not (eq major-mode (default-value 'major-mode))))
156 (diff-hl-mode 1)))
157
158 ;;;###autoload
159 (define-globalized-minor-mode global-diff-hl-mode diff-hl-mode
160 turn-on-diff-hl-mode)
161
162 (provide 'diff-hl)