]> code.delx.au - gnu-emacs-elpa/blob - diff-hl.el
Fix edge case with deletion at the end of a hunk
[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 (let ((hunk-beg (point)))
138 (while (plusp len)
139 (let ((o (make-overlay (point) (line-end-position))))
140 (overlay-put o 'diff-hl t)
141 (overlay-put o 'before-string
142 (diff-hl-fringe-spec
143 type
144 (cond
145 ((not diff-hl-draw-borders) 'empty)
146 ((and (= len 1) (= line current-line)) 'single)
147 ((= len 1) 'bottom)
148 ((= line current-line) 'top)
149 (t 'middle)))))
150 (forward-line 1)
151 (incf current-line)
152 (decf len))
153 (let ((h (make-overlay hunk-beg (point)))
154 (hook '(diff-hl-overlay-modified)))
155 (overlay-put h 'diff-hl t)
156 (overlay-put h 'modification-hooks hook)
157 (overlay-put h 'insert-in-front-hooks hook))))))))
158
159 (defun diff-hl-remove-overlays ()
160 (dolist (o (overlays-in (point-min) (point-max)))
161 (when (overlay-get o 'diff-hl) (delete-overlay o))))
162
163 (defun diff-hl-overlay-modified (ov after-p _beg _end &optional _length)
164 ;; Delete the overlay and all our overlays inside it.
165 (unless after-p
166 (save-restriction
167 (narrow-to-region (overlay-start ov) (overlay-end ov))
168 (diff-hl-remove-overlays))
169 (delete-overlay ov)))
170
171 (defvar diff-hl-timer nil)
172
173 (defun diff-hl-edit (_beg _end _len)
174 ;; DTRT when we've `undo'-ed the buffer into unmodified state.
175 (when undo-in-progress
176 (when diff-hl-timer
177 (cancel-timer diff-hl-timer))
178 (setq diff-hl-timer
179 (run-with-idle-timer 0.01 nil #'diff-hl-after-undo (current-buffer)))))
180
181 (defun diff-hl-after-undo (buffer)
182 (with-current-buffer buffer
183 (unless (buffer-modified-p)
184 (diff-hl-update))))
185
186 (defun diff-hl-diff-goto-hunk ()
187 "Open diff buffer and skip to the line corresponding to current."
188 (interactive)
189 (vc-buffer-sync)
190 (let* ((line (line-number-at-pos)))
191 (vc-diff-internal t (vc-deduce-fileset) nil nil t)
192 (vc-exec-after `(diff-hl-diff-skip-to ,line))))
193
194 (defun diff-hl-diff-skip-to (line)
195 (unless (eobp)
196 (diff-hunk-next)
197 (let (found)
198 (while (and (looking-at diff-hunk-header-re-unified) (not found))
199 (let ((hunk-line (string-to-number (match-string 3)))
200 (len (let ((m (match-string 4)))
201 (if m (string-to-number m) 1))))
202 (if (> line (+ hunk-line len))
203 (diff-hunk-next)
204 (setq found t)
205 (let ((to-go (1+ (- line hunk-line))))
206 (while (plusp to-go)
207 (forward-line 1)
208 (unless (looking-at "^-")
209 (decf to-go))))))))))
210
211 ;;;###autoload
212 (define-minor-mode diff-hl-mode
213 "Toggle display of VC diff indicators in the left fringe."
214 :lighter "" :keymap '(([remap vc-diff] . diff-hl-diff-goto-hunk))
215 (if diff-hl-mode
216 (progn
217 (add-hook 'after-save-hook 'diff-hl-update nil t)
218 (add-hook 'after-change-functions 'diff-hl-edit nil t)
219 (if vc-mode
220 (diff-hl-update)
221 (add-hook 'find-file-hook 'diff-hl-update t t))
222 (add-hook 'vc-checkin-hook 'diff-hl-update nil t)
223 (add-hook 'text-scale-mode-hook 'diff-hl-define-bitmaps nil t))
224 (remove-hook 'after-save-hook 'diff-hl-update t)
225 (remove-hook 'after-change-functions 'diff-hl-edit t)
226 (remove-hook 'find-file-hook 'diff-hl-update t)
227 (remove-hook 'vc-checkin-hook 'diff-hl-update t)
228 (remove-hook 'text-scale-mode-hook 'diff-hl-define-bitmaps t)
229 (diff-hl-remove-overlays)))
230
231 (defun turn-on-diff-hl-mode ()
232 ;; FIXME: Why is this called twice for each buffer?
233 ;; Isn't fundamental-mode supposed to not run any hooks?
234 (and buffer-file-name (not (eq major-mode (default-value 'major-mode)))
235 (window-system) ;; No fringes in the console.
236 (diff-hl-mode 1)))
237
238 ;;;###autoload
239 (define-globalized-minor-mode global-diff-hl-mode diff-hl-mode
240 turn-on-diff-hl-mode)
241
242 (provide 'diff-hl)