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