]> code.delx.au - gnu-emacs-elpa/blob - diff-hl.el
Fix compilation warning
[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 (require 'face-remap))
28
29 (defgroup diff-hl nil
30 "VC diff fringe highlighting"
31 :group 'vc)
32
33 (defface diff-hl-insert
34 '((t :inherit diff-added))
35 "Face used to highlight inserted lines."
36 :group 'diff-hl)
37
38 (defface diff-hl-delete
39 '((t :inherit diff-removed))
40 "Face used to highlight deleted lines."
41 :group 'diff-hl)
42
43 (defface diff-hl-change
44 '((default
45 :foreground "blue")
46 (((class color) (min-colors 88) (background light))
47 :background "#ddddff")
48 (((class color) (min-colors 88) (background dark))
49 :background "#333355"))
50 "Face used to highlight changed lines."
51 :group 'diff-hl)
52
53 (defcustom diff-hl-draw-borders t
54 "Non-nil to draw borders around fringe indicators."
55 :group 'diff-hl
56 :type 'boolean)
57
58 (defun diff-hl-define-bitmaps ()
59 (let* ((scale (if (and (boundp 'text-scale-mode-amount)
60 (plusp text-scale-mode-amount))
61 (expt text-scale-mode-step text-scale-mode-amount)
62 1))
63 (h (round (* (frame-char-height) scale)))
64 (w (frame-parameter nil 'left-fringe))
65 (middle (make-vector h (expt 2 (1- w))))
66 (ones (1- (expt 2 w)))
67 (top (copy-sequence middle))
68 (bottom (copy-sequence middle))
69 (single (copy-sequence middle)))
70 (aset top 0 ones)
71 (aset bottom (1- h) ones)
72 (aset single 0 ones)
73 (aset single (1- h) ones)
74 (define-fringe-bitmap 'diff-hl-bmp-top top h w 'top)
75 (define-fringe-bitmap 'diff-hl-bmp-middle middle h w 'center)
76 (define-fringe-bitmap 'diff-hl-bmp-bottom bottom h w 'bottom)
77 (define-fringe-bitmap 'diff-hl-bmp-single single h w 'top)))
78
79 (when (window-system)
80 (define-fringe-bitmap 'diff-hl-bmp-empty [0] 1 1 'center)
81 (diff-hl-define-bitmaps))
82
83 (defvar diff-hl-spec-cache (make-hash-table :test 'equal))
84
85 (defun diff-hl-fringe-spec (type pos)
86 (let* ((key (cons type pos))
87 (val (gethash key diff-hl-spec-cache)))
88 (unless val
89 (let* ((face-sym (intern (concat "diff-hl-" (symbol-name type))))
90 (bmp-sym (intern (concat "diff-hl-bmp-" (symbol-name pos)))))
91 (setq val (propertize " " 'display `((left-fringe ,bmp-sym ,face-sym))))
92 (puthash key val diff-hl-spec-cache)))
93 val))
94
95 (defun diff-hl-changes ()
96 (let* ((buf-name " *vc-diff-hl* ")
97 (vc-git-diff-switches nil)
98 (vc-hg-diff-switches nil)
99 (vc-diff-switches '("-U0"))
100 (vc-disable-async-diff t)
101 (file (buffer-file-name))
102 (backend (vc-backend file))
103 res)
104 (when backend
105 (vc-call-backend backend 'diff (list file) nil nil buf-name)
106 (with-current-buffer buf-name
107 (goto-char (point-min))
108 (unless (eobp)
109 (diff-beginning-of-hunk t)
110 (while (looking-at diff-hunk-header-re-unified)
111 (let ((line (string-to-number (match-string 3)))
112 (len (let ((m (match-string 4)))
113 (if m (string-to-number m) 1)))
114 (beg (point)))
115 (diff-end-of-hunk)
116 (let* ((inserts (diff-count-matches "^\\+" beg (point)))
117 (deletes (diff-count-matches "^-" beg (point)))
118 (type (cond ((zerop deletes) 'insert)
119 ((zerop inserts) 'delete)
120 (t 'change))))
121 (push (list line len type) res)))))))
122 (nreverse res)))
123
124 (defun diff-hl-update ()
125 (let ((changes (diff-hl-changes))
126 (current-line 1))
127 (diff-hl-remove-overlays)
128 (save-excursion
129 (goto-char (point-min))
130 (dolist (c changes)
131 (destructuring-bind (line len type) c
132 (when (eq type 'delete)
133 (setq len 1)
134 (incf line))
135 (forward-line (- line current-line))
136 (setq current-line line)
137 (while (plusp len)
138 (let ((o (make-overlay (point) (line-end-position))))
139 (overlay-put o 'diff-hl t)
140 (overlay-put o 'before-string
141 (diff-hl-fringe-spec
142 type
143 (cond ((not diff-hl-draw-borders) 'empty)
144 ((and (= len 1) (= line current-line)) 'single)
145 ((= len 1) 'bottom)
146 ((= line current-line) 'top)
147 (t 'middle))))
148 (overlay-put o 'modification-hooks '(diff-hl-overlay-modified))
149 (overlay-put o 'insert-in-front-hooks '(diff-hl-overlay-modified)))
150 (forward-line 1)
151 (incf current-line)
152 (decf len)))))))
153
154 (defun diff-hl-remove-overlays ()
155 (dolist (o (overlays-in (point-min) (point-max)))
156 (when (overlay-get o 'diff-hl) (delete-overlay o))))
157
158 (defun diff-hl-overlay-modified (ov after-p _beg _end &optional _length)
159 ;; Do the simplest possible thing, for now.
160 (when after-p (delete-overlay ov)))
161
162 (defvar diff-hl-timer nil)
163
164 (defun diff-hl-edit (_beg _end _len)
165 ;; DTRT when we've `undo'-ed the buffer into unmodified state.
166 (when undo-in-progress
167 (when diff-hl-timer
168 (cancel-timer diff-hl-timer))
169 (setq diff-hl-timer
170 (run-with-idle-timer 0.01 nil #'diff-hl-after-undo (current-buffer)))))
171
172 (defun diff-hl-after-undo (buffer)
173 (with-current-buffer buffer
174 (unless (buffer-modified-p)
175 (diff-hl-update))))
176
177 (defun diff-hl-diff-goto-hunk ()
178 "Open diff buffer and skip to the line corresponding to current."
179 (interactive)
180 (vc-buffer-sync)
181 (let* ((line (line-number-at-pos)))
182 (vc-diff-internal t (vc-deduce-fileset) nil nil t)
183 (vc-exec-after `(diff-hl-diff-skip-to ,line))))
184
185 (defun diff-hl-diff-skip-to (line)
186 (unless (eobp)
187 (diff-beginning-of-hunk t)
188 (let (found)
189 (while (and (looking-at diff-hunk-header-re-unified) (not found))
190 (let ((hunk-line (string-to-number (match-string 3)))
191 (len (let ((m (match-string 4)))
192 (if m (string-to-number m) 1))))
193 (if (> line (+ hunk-line len))
194 (diff-end-of-hunk)
195 (setq found t)
196 (let ((to-go (1+ (- line hunk-line))))
197 (while (plusp to-go)
198 (forward-line 1)
199 (unless (looking-at "^-")
200 (decf to-go))))))))))
201
202 ;;;###autoload
203 (define-minor-mode diff-hl-mode
204 "Toggle display of VC diff indicators in the left fringe."
205 :lighter "" :keymap '(([remap vc-diff] . diff-hl-diff-goto-hunk))
206 (if diff-hl-mode
207 (progn
208 (add-hook 'after-save-hook 'diff-hl-update nil t)
209 (add-hook 'after-change-functions 'diff-hl-edit nil t)
210 (if vc-mode
211 (diff-hl-update)
212 (add-hook 'find-file-hook 'diff-hl-update t t))
213 (add-hook 'vc-checkin-hook 'diff-hl-update nil t)
214 (add-hook 'text-scale-mode-hook 'diff-hl-define-bitmaps nil t))
215 (remove-hook 'after-save-hook 'diff-hl-update t)
216 (remove-hook 'after-change-functions 'diff-hl-edit t)
217 (remove-hook 'find-file-hook 'diff-hl-update t)
218 (remove-hook 'vc-checkin-hook 'diff-hl-update t)
219 (remove-hook 'text-scale-mode-hook 'diff-hl-define-bitmaps t)
220 (diff-hl-remove-overlays)))
221
222 (defun turn-on-diff-hl-mode ()
223 ;; FIXME: Why is this called twice for each buffer?
224 ;; Isn't fundamental-mode supposed to not run any hooks?
225 (and buffer-file-name (not (eq major-mode (default-value 'major-mode)))
226 (window-system) ;; No fringes in the console.
227 (diff-hl-mode 1)))
228
229 ;;;###autoload
230 (define-globalized-minor-mode global-diff-hl-mode diff-hl-mode
231 turn-on-diff-hl-mode)
232
233 (provide 'diff-hl)