]> code.delx.au - gnu-emacs-elpa/blob - diff-hl.el
Abort when diff buffer is empty
[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
28 (defgroup diff-hl nil
29 "VC diff fringe highlighting"
30 :group 'vc)
31
32 (defface diff-hl-insert
33 '((t :inherit diff-added))
34 "Face used to highlight inserted lines."
35 :group 'diff-hl)
36
37 (defface diff-hl-delete
38 '((t :inherit diff-removed))
39 "Face used to highlight deleted lines."
40 :group 'diff-hl)
41
42 (defface diff-hl-change
43 '((default
44 :foreground "blue")
45 (((class color) (min-colors 88) (background light))
46 :background "#ddddff")
47 (((class color) (min-colors 88) (background dark))
48 :background "#333355"))
49 "Face used to highlight changed lines."
50 :group 'diff-hl)
51
52 (defcustom diff-hl-draw-borders t
53 "Non-nil to draw borders around fringe indicators."
54 :group 'diff-hl
55 :type 'boolean)
56
57 (defun diff-hl-define-bitmaps ()
58 (let* ((scale (if (and (boundp 'text-scale-mode-amount)
59 (plusp text-scale-mode-amount))
60 (expt text-scale-mode-step text-scale-mode-amount)
61 1))
62 (h (round (* (frame-char-height) scale)))
63 (w (frame-parameter nil 'left-fringe))
64 (middle (make-vector h (expt 2 (1- w))))
65 (ones (1- (expt 2 w)))
66 (top (copy-sequence middle))
67 (bottom (copy-sequence middle))
68 (single (copy-sequence middle)))
69 (aset top 0 ones)
70 (aset bottom (1- h) ones)
71 (aset single 0 ones)
72 (aset single (1- h) ones)
73 (define-fringe-bitmap 'diff-hl-bmp-top top h w 'top)
74 (define-fringe-bitmap 'diff-hl-bmp-middle middle h w 'center)
75 (define-fringe-bitmap 'diff-hl-bmp-bottom bottom h w 'bottom)
76 (define-fringe-bitmap 'diff-hl-bmp-single single h w 'top)))
77
78 (when (window-system)
79 (define-fringe-bitmap 'diff-hl-bmp-empty [0] 1 1 'center)
80 (diff-hl-define-bitmaps))
81
82 (defvar diff-hl-spec-cache (make-hash-table :test 'equal))
83
84 (defun diff-hl-fringe-spec (type pos)
85 (let* ((key (cons type pos))
86 (val (gethash key diff-hl-spec-cache)))
87 (unless val
88 (let* ((face-sym (intern (concat "diff-hl-" (symbol-name type))))
89 (bmp-sym (intern (concat "diff-hl-bmp-" (symbol-name pos)))))
90 (setq val (propertize " " 'display `((left-fringe ,bmp-sym ,face-sym))))
91 (puthash key val diff-hl-spec-cache)))
92 val))
93
94 (defun diff-hl-changes ()
95 (let* ((buf-name " *vc-diff-hl* ")
96 (vc-git-diff-switches nil)
97 (vc-hg-diff-switches nil)
98 (vc-diff-switches '("-U0"))
99 (vc-disable-async-diff t)
100 (file (buffer-file-name))
101 (backend (vc-backend file))
102 res)
103 (when backend
104 (vc-call-backend backend 'diff (list file) nil nil buf-name)
105 (with-current-buffer buf-name
106 (goto-char (point-min))
107 (unless (eobp)
108 (diff-beginning-of-hunk t)
109 (while (looking-at diff-hunk-header-re-unified)
110 (let ((line (string-to-number (match-string 3)))
111 (len (let ((m (match-string 4)))
112 (if m (string-to-number m) 1)))
113 (beg (point)))
114 (diff-end-of-hunk)
115 (let* ((inserts (diff-count-matches "^\\+" beg (point)))
116 (deletes (diff-count-matches "^-" beg (point)))
117 (type (cond ((zerop deletes) 'insert)
118 ((zerop inserts) 'delete)
119 (t 'change))))
120 (push (list line len type) res)))))))
121 (nreverse res)))
122
123 (defun diff-hl-update ()
124 (let ((changes (diff-hl-changes))
125 (current-line 1))
126 (diff-hl-remove-overlays)
127 (save-excursion
128 (goto-char (point-min))
129 (dolist (c changes)
130 (destructuring-bind (line len type) c
131 (when (eq type 'delete)
132 (setq len 1)
133 (incf line))
134 (forward-line (- line current-line))
135 (setq current-line line)
136 (while (plusp len)
137 (let ((o (make-overlay (point) (line-end-position))))
138 (overlay-put o 'diff-hl t)
139 (overlay-put o 'before-string
140 (diff-hl-fringe-spec
141 type
142 (cond ((not diff-hl-draw-borders) 'empty)
143 ((and (= len 1) (= line current-line)) 'single)
144 ((= len 1) 'bottom)
145 ((= line current-line) 'top)
146 (t 'middle))))
147 (overlay-put o 'modification-hooks '(diff-hl-overlay-modified))
148 (overlay-put o 'insert-in-front-hooks '(diff-hl-overlay-modified)))
149 (forward-line 1)
150 (incf current-line)
151 (decf len)))))))
152
153 (defun diff-hl-remove-overlays ()
154 (dolist (o (overlays-in (point-min) (point-max)))
155 (when (overlay-get o 'diff-hl) (delete-overlay o))))
156
157 (defun diff-hl-overlay-modified (ov after-p _beg _end &optional _length)
158 ;; Do the simplest possible thing, for now.
159 (when after-p (delete-overlay ov)))
160
161 (defvar diff-hl-timer nil)
162
163 (defun diff-hl-edit (_beg _end _len)
164 ;; DTRT when we've `undo'-ed the buffer into unmodified state.
165 (when undo-in-progress
166 (when diff-hl-timer
167 (cancel-timer diff-hl-timer))
168 (setq diff-hl-timer
169 (run-with-idle-timer 0.01 nil #'diff-hl-after-undo (current-buffer)))))
170
171 (defun diff-hl-after-undo (buffer)
172 (with-current-buffer buffer
173 (unless (buffer-modified-p)
174 (diff-hl-update))))
175
176 (defun diff-hl-diff-goto-hunk ()
177 "Open diff buffer and skip to the line corresponding to current."
178 (interactive)
179 (vc-buffer-sync)
180 (let* ((line (line-number-at-pos)))
181 (vc-diff-internal t (vc-deduce-fileset) nil nil t)
182 (vc-exec-after `(diff-hl-diff-skip-to ,line))))
183
184 (defun diff-hl-diff-skip-to (line)
185 (unless (eobp)
186 (diff-beginning-of-hunk t)
187 (let (found)
188 (while (and (looking-at diff-hunk-header-re-unified) (not found))
189 (let ((hunk-line (string-to-number (match-string 3)))
190 (len (let ((m (match-string 4)))
191 (if m (string-to-number m) 1))))
192 (if (> line (+ hunk-line len))
193 (diff-end-of-hunk)
194 (setq found t)
195 (let ((to-go (1+ (- line hunk-line))))
196 (while (plusp to-go)
197 (forward-line 1)
198 (unless (looking-at "^-")
199 (decf to-go))))))))))
200
201 ;;;###autoload
202 (define-minor-mode diff-hl-mode
203 "Toggle display of VC diff indicators in the left fringe."
204 :lighter "" :keymap '(([remap vc-diff] . diff-hl-diff-goto-hunk))
205 (if diff-hl-mode
206 (progn
207 (add-hook 'after-save-hook 'diff-hl-update nil t)
208 (add-hook 'after-change-functions 'diff-hl-edit nil t)
209 (if vc-mode
210 (diff-hl-update)
211 (add-hook 'find-file-hook 'diff-hl-update t t))
212 (add-hook 'vc-checkin-hook 'diff-hl-update nil t)
213 (add-hook 'text-scale-mode-hook 'diff-hl-define-bitmaps nil t))
214 (remove-hook 'after-save-hook 'diff-hl-update t)
215 (remove-hook 'after-change-functions 'diff-hl-edit t)
216 (remove-hook 'find-file-hook 'diff-hl-update t)
217 (remove-hook 'vc-checkin-hook 'diff-hl-update t)
218 (remove-hook 'text-scale-mode-hook 'diff-hl-define-bitmaps t)
219 (diff-hl-remove-overlays)))
220
221 (defun turn-on-diff-hl-mode ()
222 ;; FIXME: Why is this called twice for each buffer?
223 ;; Isn't fundamental-mode supposed to not run any hooks?
224 (and buffer-file-name (not (eq major-mode (default-value 'major-mode)))
225 (window-system) ;; No fringes in the console.
226 (diff-hl-mode 1)))
227
228 ;;;###autoload
229 (define-globalized-minor-mode global-diff-hl-mode diff-hl-mode
230 turn-on-diff-hl-mode)
231
232 (provide 'diff-hl)