]> 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.3.1
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 `(let (beg-line end-line)
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 (setq end-line (line-number-at-pos (point)))
259 (unless (eobp) (diff-split-hunk)))
260 (unless (looking-at "[-+]") (forward-line -1))
261 (while (looking-at "[-+]") (forward-line -1))
262 (setq beg-line (line-number-at-pos (point)))
263 (unless (looking-at "@")
264 (forward-line 1)
265 (diff-split-hunk))
266 (let ((wbh (window-body-height)))
267 (if (>= wbh (- end-line beg-line))
268 (recenter (/ (+ wbh (- beg-line end-line) 2) 2))
269 (recenter 1)))
270 (unless (yes-or-no-p (format "Revert current hunk in %s?"
271 ,(caadr fileset)))
272 (error "Revert canceled"))
273 (let ((diff-advance-after-apply-hunk nil))
274 (diff-apply-hunk t))
275 (with-current-buffer ,buffer
276 (save-buffer))
277 (message "Hunk reverted"))))
278 (quit-windows-on diff-buffer))))
279
280 (defun diff-hl-hunk-overlay-at (pos)
281 (loop for o in (overlays-at pos)
282 when (overlay-get o 'diff-hl-hunk)
283 return o))
284
285 (defun diff-hl-next-hunk (&optional backward)
286 "Go to the beginning of the next hunk in the current buffer."
287 (interactive)
288 (let ((pos (save-excursion
289 (catch 'found
290 (while (not (if backward (bobp) (eobp)))
291 (goto-char (if backward
292 (previous-overlay-change (point))
293 (next-overlay-change (point))))
294 (let ((o (diff-hl-hunk-overlay-at (point))))
295 (when (and o (if backward
296 (<= (overlay-end o) (1+ (point)))
297 (>= (overlay-start o) (point))))
298 (throw 'found (overlay-start o)))))))))
299 (if pos
300 (goto-char pos)
301 (error "No further hunks found"))))
302
303 (defun diff-hl-previous-hunk ()
304 "Go to the beginning of the previous hunk in the current buffer."
305 (interactive)
306 (diff-hl-next-hunk t))
307
308 (define-minor-mode diff-hl-mode
309 "Toggle display of VC diff indicators in the left fringe."
310 :lighter "" :keymap `(([remap vc-diff] . diff-hl-diff-goto-hunk)
311 (,(kbd "C-x v n") . diff-hl-revert-hunk)
312 (,(kbd "C-x v [") . diff-hl-previous-hunk)
313 (,(kbd "C-x v ]") . diff-hl-next-hunk))
314 (if diff-hl-mode
315 (progn
316 (add-hook 'after-save-hook 'diff-hl-update nil t)
317 (add-hook 'after-change-functions 'diff-hl-edit nil t)
318 (if vc-mode
319 (diff-hl-update)
320 (add-hook 'find-file-hook 'diff-hl-update t t))
321 (add-hook 'vc-checkin-hook 'diff-hl-update nil t)
322 (add-hook 'after-revert-hook 'diff-hl-update nil t)
323 (add-hook 'text-scale-mode-hook 'diff-hl-define-bitmaps nil t))
324 (remove-hook 'after-save-hook 'diff-hl-update t)
325 (remove-hook 'after-change-functions 'diff-hl-edit t)
326 (remove-hook 'find-file-hook 'diff-hl-update t)
327 (remove-hook 'vc-checkin-hook 'diff-hl-update t)
328 (remove-hook 'after-revert-hook 'diff-hl-update t)
329 (remove-hook 'text-scale-mode-hook 'diff-hl-define-bitmaps t)
330 (diff-hl-remove-overlays)))
331
332 (when (require 'smartrep nil t)
333 (let (smart-keys)
334 (cl-labels ((scan (map)
335 (map-keymap
336 (lambda (event binding)
337 (if (consp binding)
338 (scan binding)
339 (when (characterp event)
340 (push (cons (string event) binding) smart-keys))))
341 map)))
342 (scan diff-hl-mode-map)
343 (smartrep-define-key diff-hl-mode-map "C-x v" smart-keys))))
344
345 (defun diff-hl-dir-update ()
346 (dolist (pair (if (vc-dir-marked-files)
347 (vc-dir-marked-only-files-and-states)
348 (vc-dir-child-files-and-states)))
349 (when (eq 'up-to-date (cdr pair))
350 (let ((buffer (find-buffer-visiting (car pair))))
351 (when buffer
352 (with-current-buffer buffer
353 (diff-hl-remove-overlays)))))))
354
355 (define-minor-mode diff-hl-dir-mode
356 "Toggle `diff-hl-mode' integration in a `vc-dir-mode' buffer."
357 :lighter ""
358 (if diff-hl-dir-mode
359 (add-hook 'vc-checkin-hook 'diff-hl-dir-update t t)
360 (remove-hook 'vc-checkin-hook 'diff-hl-dir-update t)))
361
362 ;;;###autoload
363 (defun turn-on-diff-hl-mode ()
364 "Turn on `diff-hl-mode' or `diff-hl-dir-mode' in a buffer if appropriate."
365 (when (window-system) ;; No fringes in the console.
366 (cond
367 (buffer-file-name
368 (diff-hl-mode 1))
369 ((eq major-mode 'vc-dir-mode)
370 (diff-hl-dir-mode 1)))))
371
372 (defun diff-hl-global-mode-change ()
373 (unless global-diff-hl-mode
374 (dolist (buf (buffer-list))
375 (with-current-buffer buf
376 (when diff-hl-dir-mode
377 (diff-hl-dir-mode -1))))))
378
379 ;;;###autoload
380 (define-globalized-minor-mode global-diff-hl-mode diff-hl-mode
381 turn-on-diff-hl-mode :after-hook (diff-hl-global-mode-change))
382
383 (provide 'diff-hl)
384
385 ;;; diff-hl.el ends here