]> code.delx.au - gnu-emacs-elpa/blob - diff-hl.el
Introduce diff-hl-revert-current-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 (defcustom diff-hl-revert-current-hunk t
59 "Non-nil to only revert current hunk by default.
60 `diff-hl-revert' will do the opposite when called with prefix argument."
61 :group 'diff-hl
62 :type 'boolean)
63
64 (defun diff-hl-define-bitmaps ()
65 (let* ((scale (if (and (boundp 'text-scale-mode-amount)
66 (plusp text-scale-mode-amount))
67 (expt text-scale-mode-step text-scale-mode-amount)
68 1))
69 (h (round (* (frame-char-height) scale)))
70 (w (frame-parameter nil 'left-fringe))
71 (middle (make-vector h (expt 2 (1- w))))
72 (ones (1- (expt 2 w)))
73 (top (copy-sequence middle))
74 (bottom (copy-sequence middle))
75 (single (copy-sequence middle)))
76 (aset top 0 ones)
77 (aset bottom (1- h) ones)
78 (aset single 0 ones)
79 (aset single (1- h) ones)
80 (define-fringe-bitmap 'diff-hl-bmp-top top h w 'top)
81 (define-fringe-bitmap 'diff-hl-bmp-middle middle h w 'center)
82 (define-fringe-bitmap 'diff-hl-bmp-bottom bottom h w 'bottom)
83 (define-fringe-bitmap 'diff-hl-bmp-single single h w 'top)))
84
85 (when (window-system)
86 (define-fringe-bitmap 'diff-hl-bmp-empty [0] 1 1 'center)
87 (diff-hl-define-bitmaps))
88
89 (defvar diff-hl-spec-cache (make-hash-table :test 'equal))
90
91 (defun diff-hl-fringe-spec (type pos)
92 (let* ((key (cons type pos))
93 (val (gethash key diff-hl-spec-cache)))
94 (unless val
95 (let* ((face-sym (intern (concat "diff-hl-" (symbol-name type))))
96 (bmp-sym (intern (concat "diff-hl-bmp-" (symbol-name pos)))))
97 (setq val (propertize " " 'display `((left-fringe ,bmp-sym ,face-sym))))
98 (puthash key val diff-hl-spec-cache)))
99 val))
100
101 (defun diff-hl-changes ()
102 (let* ((buf-name " *vc-diff-hl* ")
103 (vc-git-diff-switches nil)
104 (vc-hg-diff-switches nil)
105 (vc-diff-switches '("-U0"))
106 (vc-disable-async-diff t)
107 (file (buffer-file-name))
108 (backend (vc-backend file))
109 res)
110 (when backend
111 (vc-call-backend backend 'diff (list file) nil nil buf-name)
112 (with-current-buffer buf-name
113 (goto-char (point-min))
114 (unless (eobp)
115 (diff-beginning-of-hunk t)
116 (while (looking-at diff-hunk-header-re-unified)
117 (let ((line (string-to-number (match-string 3)))
118 (len (let ((m (match-string 4)))
119 (if m (string-to-number m) 1)))
120 (beg (point)))
121 (diff-end-of-hunk)
122 (let* ((inserts (diff-count-matches "^\\+" beg (point)))
123 (deletes (diff-count-matches "^-" beg (point)))
124 (type (cond ((zerop deletes) 'insert)
125 ((zerop inserts) 'delete)
126 (t 'change))))
127 (push (list line len type) res)))))))
128 (nreverse res)))
129
130 (defun diff-hl-update ()
131 (let ((changes (diff-hl-changes))
132 (current-line 1))
133 (diff-hl-remove-overlays)
134 (save-excursion
135 (goto-char (point-min))
136 (dolist (c changes)
137 (destructuring-bind (line len type) c
138 (when (eq type 'delete)
139 (setq len 1)
140 (incf line))
141 (forward-line (- line current-line))
142 (setq current-line line)
143 (let ((hunk-beg (point)))
144 (while (plusp len)
145 (let ((o (make-overlay (point) (line-end-position))))
146 (overlay-put o 'diff-hl t)
147 (overlay-put o 'before-string
148 (diff-hl-fringe-spec
149 type
150 (cond
151 ((not diff-hl-draw-borders) 'empty)
152 ((and (= len 1) (= line current-line)) 'single)
153 ((= len 1) 'bottom)
154 ((= line current-line) 'top)
155 (t 'middle)))))
156 (forward-line 1)
157 (incf current-line)
158 (decf len))
159 (let ((h (make-overlay hunk-beg (point)))
160 (hook '(diff-hl-overlay-modified)))
161 (overlay-put h 'diff-hl t)
162 (overlay-put h 'modification-hooks hook)
163 (overlay-put h 'insert-in-front-hooks hook))))))))
164
165 (defun diff-hl-remove-overlays ()
166 (dolist (o (overlays-in (point-min) (point-max)))
167 (when (overlay-get o 'diff-hl) (delete-overlay o))))
168
169 (defun diff-hl-overlay-modified (ov after-p _beg _end &optional _length)
170 ;; Delete the overlay and all our overlays inside it.
171 (unless after-p
172 (save-restriction
173 (narrow-to-region (overlay-start ov) (overlay-end ov))
174 (diff-hl-remove-overlays))
175 (delete-overlay ov)))
176
177 (defvar diff-hl-timer nil)
178
179 (defun diff-hl-edit (_beg _end _len)
180 ;; DTRT when we've `undo'-ed the buffer into unmodified state.
181 (when undo-in-progress
182 (when diff-hl-timer
183 (cancel-timer diff-hl-timer))
184 (setq diff-hl-timer
185 (run-with-idle-timer 0.01 nil #'diff-hl-after-undo (current-buffer)))))
186
187 (defun diff-hl-after-undo (buffer)
188 (with-current-buffer buffer
189 (unless (buffer-modified-p)
190 (diff-hl-update))))
191
192 (defun diff-hl-diff-goto-hunk ()
193 "Open diff buffer and skip to the line corresponding to current."
194 (interactive)
195 (vc-buffer-sync)
196 (let* ((line (line-number-at-pos)))
197 (vc-diff-internal t (vc-deduce-fileset) nil nil t)
198 (vc-exec-after `(diff-hl-diff-skip-to ,line))))
199
200 (defun diff-hl-diff-skip-to (line)
201 (unless (eobp)
202 (diff-hunk-next)
203 (let (found)
204 (while (and (looking-at diff-hunk-header-re-unified) (not found))
205 (let ((hunk-line (string-to-number (match-string 3)))
206 (len (let ((m (match-string 4)))
207 (if m (string-to-number m) 1))))
208 (if (> line (+ hunk-line len))
209 (diff-hunk-next)
210 (setq found t)
211 (let ((to-go (1+ (- line hunk-line))))
212 (while (plusp to-go)
213 (forward-line 1)
214 (unless (looking-at "^-")
215 (decf to-go))))))))))
216
217 (defun diff-hl-revert (arg)
218 (interactive "P")
219 (if (not (diff-xor arg diff-hl-revert-current-hunk))
220 (vc-revert)
221 (vc-buffer-sync)
222 (let ((diff-buffer (generate-new-buffer-name "*diff-hl*"))
223 (buffer (current-buffer))
224 (line (line-number-at-pos))
225 (fileset (vc-deduce-fileset)))
226 (unwind-protect
227 (progn
228 (vc-diff-internal nil fileset nil nil nil diff-buffer)
229 (vc-exec-after
230 `(progn
231 (when (eobp)
232 (with-current-buffer ,buffer (diff-hl-remove-overlays))
233 (error "Buffer is up-to-date"))
234 (diff-hl-diff-skip-to ,line)
235 (save-restriction
236 (diff-restrict-view)
237 (unless (yes-or-no-p (format "Revert current hunk in %s?"
238 ,(caadr fileset)))
239 (error "Revert canceled")))
240 (let ((diff-advance-after-apply-hunk nil))
241 (diff-apply-hunk t)))))
242 (quit-windows-on diff-buffer)
243 (save-buffer)
244 (message "Hunk reverted")))))
245
246 ;;;###autoload
247 (define-minor-mode diff-hl-mode
248 "Toggle display of VC diff indicators in the left fringe."
249 :lighter "" :keymap '(([remap vc-diff] . diff-hl-diff-goto-hunk)
250 ([remap vc-revert] . diff-hl-revert))
251 (if diff-hl-mode
252 (progn
253 (add-hook 'after-save-hook 'diff-hl-update nil t)
254 (add-hook 'after-change-functions 'diff-hl-edit nil t)
255 (if vc-mode
256 (diff-hl-update)
257 (add-hook 'find-file-hook 'diff-hl-update t t))
258 (add-hook 'vc-checkin-hook 'diff-hl-update nil t)
259 (add-hook 'text-scale-mode-hook 'diff-hl-define-bitmaps nil t))
260 (remove-hook 'after-save-hook 'diff-hl-update t)
261 (remove-hook 'after-change-functions 'diff-hl-edit t)
262 (remove-hook 'find-file-hook 'diff-hl-update t)
263 (remove-hook 'vc-checkin-hook 'diff-hl-update t)
264 (remove-hook 'text-scale-mode-hook 'diff-hl-define-bitmaps t)
265 (diff-hl-remove-overlays)))
266
267 (defun turn-on-diff-hl-mode ()
268 ;; FIXME: Why is this called twice for each buffer?
269 ;; Isn't fundamental-mode supposed to not run any hooks?
270 (and buffer-file-name (not (eq major-mode (default-value 'major-mode)))
271 (window-system) ;; No fringes in the console.
272 (diff-hl-mode 1)))
273
274 ;;;###autoload
275 (define-globalized-minor-mode global-diff-hl-mode diff-hl-mode
276 turn-on-diff-hl-mode)
277
278 (provide 'diff-hl)