]> code.delx.au - gnu-emacs-elpa/blob - diff-hl.el
Hook into checkin from vc-dir mode
[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 ;; URL: https://github.com/dgutov/diff-hl
5 ;; Keywords: vc, diff
6 ;; Version: 1.0.1
7
8 ;; This file is not part of GNU Emacs.
9
10 ;; This file is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
14
15 ;; This file is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
22
23 ;;; Commentary:
24
25 ;; `diff-hl-mode' provides IDE-like highlighting of `vc-diff' results
26 ;; on the left fringe of the current buffer.
27 ;;
28 ;; For full description, see README.md or the home page.
29
30 ;;; Code:
31
32 (require 'diff-mode)
33 (require 'vc)
34 (eval-when-compile
35 (require 'cl)
36 (require 'vc-git)
37 (require 'vc-hg)
38 (require 'face-remap))
39
40 (defgroup diff-hl nil
41 "VC diff fringe highlighting"
42 :group 'vc)
43
44 (defface diff-hl-insert
45 '((t :inherit diff-added))
46 "Face used to highlight inserted lines."
47 :group 'diff-hl)
48
49 (defface diff-hl-delete
50 '((t :inherit diff-removed))
51 "Face used to highlight deleted lines."
52 :group 'diff-hl)
53
54 (defface diff-hl-change
55 '((default
56 :foreground "blue")
57 (((class color) (min-colors 88) (background light))
58 :background "#ddddff")
59 (((class color) (min-colors 88) (background dark))
60 :background "#333355"))
61 "Face used to highlight changed lines."
62 :group 'diff-hl)
63
64 (defcustom diff-hl-draw-borders t
65 "Non-nil to draw borders around fringe indicators."
66 :group 'diff-hl
67 :type 'boolean)
68
69 (defun diff-hl-define-bitmaps ()
70 (let* ((scale (if (and (boundp 'text-scale-mode-amount)
71 (plusp text-scale-mode-amount))
72 (expt text-scale-mode-step text-scale-mode-amount)
73 1))
74 (h (round (* (frame-char-height) scale)))
75 (w (frame-parameter nil 'left-fringe))
76 (middle (make-vector h (expt 2 (1- w))))
77 (ones (1- (expt 2 w)))
78 (top (copy-sequence middle))
79 (bottom (copy-sequence middle))
80 (single (copy-sequence middle)))
81 (aset top 0 ones)
82 (aset bottom (1- h) ones)
83 (aset single 0 ones)
84 (aset single (1- h) ones)
85 (define-fringe-bitmap 'diff-hl-bmp-top top h w 'top)
86 (define-fringe-bitmap 'diff-hl-bmp-middle middle h w 'center)
87 (define-fringe-bitmap 'diff-hl-bmp-bottom bottom h w 'bottom)
88 (define-fringe-bitmap 'diff-hl-bmp-single single h w 'top)))
89
90 (when (window-system)
91 (define-fringe-bitmap 'diff-hl-bmp-empty [0] 1 1 'center)
92 (diff-hl-define-bitmaps))
93
94 (defvar diff-hl-spec-cache (make-hash-table :test 'equal))
95
96 (defun diff-hl-fringe-spec (type pos)
97 (let* ((key (cons type pos))
98 (val (gethash key diff-hl-spec-cache)))
99 (unless val
100 (let* ((face-sym (intern (concat "diff-hl-" (symbol-name type))))
101 (bmp-sym (intern (concat "diff-hl-bmp-" (symbol-name pos)))))
102 (setq val (propertize " " 'display `((left-fringe ,bmp-sym ,face-sym))))
103 (puthash key val diff-hl-spec-cache)))
104 val))
105
106 (defmacro diff-hl-with-diff-switches (body)
107 `(let ((vc-git-diff-switches nil)
108 (vc-hg-diff-switches nil)
109 (vc-diff-switches '("-U0"))
110 (vc-disable-async-diff t))
111 ,body))
112
113 (defun diff-hl-changes ()
114 (let* ((buf-name " *diff-hl* ")
115 (file (buffer-file-name))
116 (backend (vc-backend file))
117 res)
118 (when (and backend (not (eq (vc-state file backend) 'up-to-date)))
119 (diff-hl-with-diff-switches
120 (vc-call-backend backend 'diff (list file) nil nil buf-name))
121 (with-current-buffer buf-name
122 (goto-char (point-min))
123 (unless (eobp)
124 (diff-beginning-of-hunk t)
125 (while (looking-at diff-hunk-header-re-unified)
126 (let ((line (string-to-number (match-string 3)))
127 (len (let ((m (match-string 4)))
128 (if m (string-to-number m) 1)))
129 (beg (point)))
130 (diff-end-of-hunk)
131 (let* ((inserts (diff-count-matches "^\\+" beg (point)))
132 (deletes (diff-count-matches "^-" beg (point)))
133 (type (cond ((zerop deletes) 'insert)
134 ((zerop inserts) 'delete)
135 (t 'change))))
136 (push (list line len type) res)))))))
137 (nreverse res)))
138
139 (defun diff-hl-update ()
140 (let ((changes (diff-hl-changes))
141 (current-line 1))
142 (diff-hl-remove-overlays)
143 (save-excursion
144 (goto-char (point-min))
145 (dolist (c changes)
146 (destructuring-bind (line len type) c
147 (when (eq type 'delete)
148 (setq len 1)
149 (incf line))
150 (forward-line (- line current-line))
151 (setq current-line line)
152 (let ((hunk-beg (point)))
153 (while (plusp len)
154 (let ((o (make-overlay (point) (line-end-position))))
155 (overlay-put o 'diff-hl t)
156 (overlay-put o 'before-string
157 (diff-hl-fringe-spec
158 type
159 (cond
160 ((not diff-hl-draw-borders) 'empty)
161 ((and (= len 1) (= line current-line)) 'single)
162 ((= len 1) 'bottom)
163 ((= line current-line) 'top)
164 (t 'middle)))))
165 (forward-line 1)
166 (incf current-line)
167 (decf len))
168 (let ((h (make-overlay hunk-beg (point)))
169 (hook '(diff-hl-overlay-modified)))
170 (overlay-put h 'diff-hl t)
171 (overlay-put h 'modification-hooks hook)
172 (overlay-put h 'insert-in-front-hooks hook))))))))
173
174 (defun diff-hl-remove-overlays ()
175 (dolist (o (overlays-in (point-min) (point-max)))
176 (when (overlay-get o 'diff-hl) (delete-overlay o))))
177
178 (defun diff-hl-overlay-modified (ov after-p _beg _end &optional _length)
179 ;; Delete the overlay and all our overlays inside it.
180 (unless after-p
181 (save-restriction
182 (narrow-to-region (overlay-start ov) (overlay-end ov))
183 (diff-hl-remove-overlays))
184 (delete-overlay ov)))
185
186 (defvar diff-hl-timer nil)
187
188 (defun diff-hl-edit (_beg _end _len)
189 ;; DTRT when we've `undo'-ed the buffer into unmodified state.
190 (when undo-in-progress
191 (when diff-hl-timer
192 (cancel-timer diff-hl-timer))
193 (setq diff-hl-timer
194 (run-with-idle-timer 0.01 nil #'diff-hl-after-undo (current-buffer)))))
195
196 (defun diff-hl-after-undo (buffer)
197 (with-current-buffer buffer
198 (unless (buffer-modified-p)
199 (diff-hl-update))))
200
201 (defun diff-hl-diff-goto-hunk ()
202 "Run VC diff command and go to the line corresponding to current."
203 (interactive)
204 (vc-buffer-sync)
205 (let* ((line (line-number-at-pos))
206 (buffer (current-buffer)))
207 (vc-diff-internal t (vc-deduce-fileset) nil nil t)
208 (vc-exec-after `(if (eobp)
209 (with-current-buffer ,buffer (diff-hl-remove-overlays))
210 (diff-hl-diff-skip-to ,line)))))
211
212 (defun diff-hl-diff-skip-to (line)
213 "In `diff-mode', skip to the hunk and line corresponding to LINE
214 in the source file, or the last line of the hunk above it."
215 (diff-hunk-next)
216 (let (found)
217 (while (and (looking-at diff-hunk-header-re-unified) (not found))
218 (let ((hunk-line (string-to-number (match-string 3)))
219 (len (let ((m (match-string 4)))
220 (if m (string-to-number m) 1))))
221 (if (> line (+ hunk-line len))
222 (diff-hunk-next)
223 (setq found t)
224 (if (< line hunk-line)
225 ;; Retreat to the previous hunk.
226 (forward-line -1)
227 (let ((to-go (1+ (- line hunk-line))))
228 (while (plusp to-go)
229 (forward-line 1)
230 (unless (looking-at "^-")
231 (decf to-go))))))))))
232
233 (defun diff-hl-revert-hunk ()
234 "Revert the diff hunk with changes at or above the point."
235 (interactive)
236 (vc-buffer-sync)
237 (let ((diff-buffer (generate-new-buffer-name "*diff-hl*"))
238 (buffer (current-buffer))
239 (line (line-number-at-pos))
240 (fileset (vc-deduce-fileset)))
241 (unwind-protect
242 (progn
243 (diff-hl-with-diff-switches
244 (vc-diff-internal nil fileset nil nil nil diff-buffer))
245 (vc-exec-after
246 `(progn
247 (when (eobp)
248 (with-current-buffer ,buffer (diff-hl-remove-overlays))
249 (error "Buffer is up-to-date"))
250 (diff-hl-diff-skip-to ,line)
251 (save-restriction
252 (diff-restrict-view)
253 (unless (yes-or-no-p (format "Revert current hunk in %s?"
254 ,(caadr fileset)))
255 (error "Revert canceled")))
256 (let ((diff-advance-after-apply-hunk nil))
257 (diff-apply-hunk t))
258 (with-current-buffer ,buffer
259 (save-buffer))
260 (message "Hunk reverted"))))
261 (quit-windows-on diff-buffer))))
262
263 ;;;###autoload
264 (define-minor-mode diff-hl-mode
265 "Toggle display of VC diff indicators in the left fringe."
266 :lighter "" :keymap `(([remap vc-diff] . diff-hl-diff-goto-hunk)
267 (,(kbd "C-x v n") . diff-hl-revert-hunk))
268 (if diff-hl-mode
269 (progn
270 (add-hook 'after-save-hook 'diff-hl-update nil t)
271 (add-hook 'after-change-functions 'diff-hl-edit nil t)
272 (if vc-mode
273 (diff-hl-update)
274 (add-hook 'find-file-hook 'diff-hl-update t t))
275 (add-hook 'vc-checkin-hook 'diff-hl-update nil t)
276 (add-hook 'text-scale-mode-hook 'diff-hl-define-bitmaps nil t))
277 (remove-hook 'after-save-hook 'diff-hl-update t)
278 (remove-hook 'after-change-functions 'diff-hl-edit t)
279 (remove-hook 'find-file-hook 'diff-hl-update t)
280 (remove-hook 'vc-checkin-hook 'diff-hl-update t)
281 (remove-hook 'text-scale-mode-hook 'diff-hl-define-bitmaps t)
282 (diff-hl-remove-overlays)))
283
284 (defun diff-hl-dir-update ()
285 (dolist (pair (vc-dir-marked-only-files-and-states))
286 (when (eq 'up-to-date (cdr pair))
287 (let ((buffer (find-buffer-visiting (car pair))))
288 (when buffer
289 (with-current-buffer buffer
290 (diff-hl-remove-overlays)))))))
291
292 ;;;###autoload
293 (define-minor-mode diff-hl-dir-mode
294 "Toggle `diff-hl-mode' link in `vc-dir-mode' buffer."
295 :lighter ""
296 (if diff-hl-dir-mode
297 (add-hook 'vc-checkin-hook 'diff-hl-dir-update t t)
298 (remove-hook 'vc-checkin-hook 'diff-hl-dir-update t)))
299
300 (defun turn-on-diff-hl-mode ()
301 (when (window-system) ;; No fringes in the console.
302 (cond
303 ;; FIXME: Why is this called twice for each buffer?
304 ;; Isn't fundamental-mode supposed to not run any hooks?
305 ((and buffer-file-name (not (eq major-mode (default-value 'major-mode))))
306 (diff-hl-mode 1))
307 ((eq major-mode 'vc-dir-mode)
308 (diff-hl-dir-mode 1)))))
309
310 ;;;###autoload
311 (define-globalized-minor-mode global-diff-hl-mode diff-hl-mode
312 turn-on-diff-hl-mode)
313
314 (provide 'diff-hl)
315
316 ;;; diff-hl.el ends here