git-blame.el: Autoupdate while editing
This adds the support for automatically updating the buffer while editing. A configuration variable git-blame-autoupdate controls whether this should be enabled or not. Signed-off-by: David Kågedal <davidk@lysator.liu.se> Signed-off-by: Junio C Hamano <junkio@cox.net>
David Kågedal committed
Feb 9, 2007 at 09:22 UTC
d2589855df9141ed5885ebd2fe868619d53fa15e
1 file changed
+86
-9
contrib/emacs/git-blame.el
+86
-9
@@ -102,6 +102,9 @@
102
103
(defvar git-blame-ancient-color "dark green")
104
105
+(defvar git-blame-autoupdate t
106
+ "*Automatically update the blame display while editing")
107
+
108
(defvar git-blame-proc nil
109
"The running git-blame process")
110
(make-variable-buffer-local 'git-blame-proc)
@@ -114,6 +117,14 @@
117
"A cache of git-blame information for the current buffer")
118
(make-variable-buffer-local 'git-blame-cache)
119
120
+(defvar git-blame-idle-timer nil
121
+ "An idle timer that updates the blame")
122
+(make-variable-buffer-local 'git-blame-cache)
123
+
124
+(defvar git-blame-update-queue nil
125
+ "A queue of update requests")
126
+(make-variable-buffer-local 'git-blame-update-queue)
127
+
128
(defvar git-blame-mode nil)
129
(make-variable-buffer-local 'git-blame-mode)
130
(unless (assq 'git-blame-mode minor-mode-alist)
@@ -129,6 +140,9 @@
140
(setq git-blame-mode (eq arg 1))
141
(setq git-blame-mode (not git-blame-mode)))
142
(make-local-variable 'git-blame-colors)
143
+ (if git-blame-autoupdate
144
+ (add-hook 'after-change-functions 'git-blame-after-change nil t)
145
+ (remove-hook 'after-change-functions 'git-blame-after-change t))
146
(git-blame-cleanup)
147
(if git-blame-mode
148
(progn
@@ -137,7 +151,8 @@
151
(setq git-blame-colors git-blame-dark-colors)
152
(setq git-blame-colors git-blame-light-colors)))
153
(setq git-blame-cache (make-hash-table :test 'equal))
140
- (git-blame-run))))
154
+ (git-blame-run))
155
+ (cancel-timer git-blame-idle-timer)))
156
157
;;;###autoload
158
(defun git-reblame ()
@@ -148,18 +163,24 @@
163
(git-blame-cleanup)
164
(git-blame-run))
165
151
-(defun git-blame-run ()
166
+(defun git-blame-run (&optional startline endline)
167
(if git-blame-proc
168
;; Should maybe queue up a new run here
169
(message "Already running git blame")
170
(let ((display-buf (current-buffer))
171
(blame-buf (get-buffer-create
157
- (concat " git blame for " (buffer-name)))))
172
+ (concat " git blame for " (buffer-name))))
173
+ (args '("--incremental" "--contents" "-")))
174
+ (if startline
175
+ (setq args (append args
176
+ (list "-L" (format "%d,%d" startline endline)))))
177
+ (setq args (append args
178
+ (list (file-name-nondirectory buffer-file-name))))
179
(setq git-blame-proc
159
- (start-process "git-blame" blame-buf
160
- "git" "blame"
161
- "--incremental" "--contents" "-"
162
- (file-name-nondirectory buffer-file-name)))
180
+ (apply 'start-process
181
+ "git-blame" blame-buf
182
+ "git" "blame"
183
+ args))
184
(with-current-buffer blame-buf
185
(erase-buffer)
186
(make-local-variable 'git-blame-file)
@@ -183,10 +204,28 @@
204
(setq git-blame-overlays nil)
205
(remove-git-blame-text-properties (point-min) (point-max)))
206
207
+(defun git-blame-update-region (start end)
208
+ "Rerun blame to get updates between START and END"
209
+ (let ((overlays (overlays-in start end)))
210
+ (while overlays
211
+ (let ((overlay (pop overlays)))
212
+ (if (< (overlay-start overlay) start)
213
+ (setq start (overlay-start overlay)))
214
+ (if (> (overlay-end overlay) end)
215
+ (setq end (overlay-end overlay)))
216
+ (setq git-blame-overlays (delete overlay git-blame-overlays))
217
+ (delete-overlay overlay))))
218
+ (remove-git-blame-text-properties start end)
219
+ ;; We can be sure that start and end are at line breaks
220
+ (git-blame-run (1+ (count-lines (point-min) start))
221
+ (count-lines (point-min) end)))
222
+
223
(defun git-blame-sentinel (proc status)
224
(with-current-buffer (process-buffer proc)
225
(with-current-buffer git-blame-file
189
- (setq git-blame-proc nil)))
226
+ (setq git-blame-proc nil)
227
+ (if git-blame-update-queue
228
+ (git-blame-delayed-update))))
229
;;(kill-buffer (process-buffer proc))
230
;;(message "git blame finished")
231
)
@@ -241,7 +280,8 @@
280
(save-excursion
281
(set-buffer git-blame-file)
282
(let ((info (gethash hash git-blame-cache))
244
- (inhibit-point-motion-hooks t))
283
+ (inhibit-point-motion-hooks t)
284
+ (inhibit-modification-hooks t))
285
(when (not info)
286
(let ((color (pop git-blame-colors)))
287
(unless color
@@ -298,6 +338,43 @@
338
(message "%s" (nth 4 info))
339
(setq git-blame-last-identification info))))
340
341
+;; (defun git-blame-after-save ()
342
+;; (when git-blame-mode
343
+;; (git-blame-cleanup)
344
+;; (git-blame-run)))
345
+;; (add-hook 'after-save-hook 'git-blame-after-save)
346
+
347
+(defun git-blame-after-change (start end length)
348
+ (when git-blame-mode
349
+ (git-blame-enq-update start end)))
350
+
351
+(defvar git-blame-last-update nil)
352
+(make-variable-buffer-local 'git-blame-last-update)
353
+(defun git-blame-enq-update (start end)
354
+ "Mark the region between START and END as needing blame update"
355
+ ;; Try to be smart and avoid multiple callouts for sequential
356
+ ;; editing
357
+ (cond ((and git-blame-last-update
358
+ (= start (cdr git-blame-last-update)))
359
+ (setcdr git-blame-last-update end))
360
+ ((and git-blame-last-update
361
+ (= end (car git-blame-last-update)))
362
+ (setcar git-blame-last-update start))
363
+ (t
364
+ (setq git-blame-last-update (cons start end))
365
+ (setq git-blame-update-queue (nconc git-blame-update-queue
366
+ (list git-blame-last-update)))))
367
+ (unless (or git-blame-proc git-blame-idle-timer)
368
+ (setq git-blame-idle-timer
369
+ (run-with-idle-timer 0.5 nil 'git-blame-delayed-update))))
370
+
371
+(defun git-blame-delayed-update ()
372
+ (setq git-blame-idle-timer nil)
373
+ (if git-blame-update-queue
374
+ (let ((first (pop git-blame-update-queue))
375
+ (inhibit-point-motion-hooks t))
376
+ (git-blame-update-region (car first) (cdr first)))))
377
+
378
(provide 'git-blame)
379
380
;;; git-blame.el ends here