ccl: add integer overflow checks
[emacs.git] / lisp / vc / vc-annotate.el
blobb6ecc4c1d750561c3df808b881ddf0fa41c61670
1 ;;; vc-annotate.el --- VC Annotate Support
3 ;; Copyright (C) 1997-1998, 2000-2011 Free Software Foundation, Inc.
5 ;; Author: Martin Lorentzson <emwson@emw.ericsson.se>
6 ;; Maintainer: FSF
7 ;; Keywords: vc tools
8 ;; Package: vc
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;; Commentary:
28 (require 'vc-hooks)
29 (require 'vc)
31 ;;; Code:
32 (eval-when-compile
33 (require 'cl))
35 (defcustom vc-annotate-display-mode 'fullscale
36 "Which mode to color the output of \\[vc-annotate] with by default."
37 :type '(choice (const :tag "By Color Map Range" nil)
38 (const :tag "Scale to Oldest" scale)
39 (const :tag "Scale Oldest->Newest" fullscale)
40 (number :tag "Specify Fractional Number of Days"
41 :value "20.5"))
42 :group 'vc)
44 (defcustom vc-annotate-color-map
45 (if (and (tty-display-color-p) (<= (display-color-cells) 8))
46 ;; A custom sorted TTY colormap
47 (let* ((colors
48 (sort
49 (delq nil
50 (mapcar (lambda (x)
51 (if (not (or
52 (string-equal (car x) "white")
53 (string-equal (car x) "black") ))
54 (car x)))
55 (tty-color-alist)))
56 (lambda (a b)
57 (cond
58 ((or (string-equal a "red") (string-equal b "blue")) t)
59 ((or (string-equal b "red") (string-equal a "blue")) nil)
60 ((string-equal a "yellow") t)
61 ((string-equal b "yellow") nil)
62 ((string-equal a "cyan") t)
63 ((string-equal b "cyan") nil)
64 ((string-equal a "green") t)
65 ((string-equal b "green") nil)
66 ((string-equal a "magenta") t)
67 ((string-equal b "magenta") nil)
68 (t (string< a b))))))
69 (date 20.)
70 (delta (/ (- 360. date) (1- (length colors)))))
71 (mapcar (lambda (x)
72 (prog1
73 (cons date x)
74 (setq date (+ date delta)))) colors))
75 ;; Normal colormap: hue stepped from 0-240deg, value=1., saturation=0.75
76 '(( 20. . "#FF3F3F")
77 ( 40. . "#FF6C3F")
78 ( 60. . "#FF993F")
79 ( 80. . "#FFC63F")
80 (100. . "#FFF33F")
81 (120. . "#DDFF3F")
82 (140. . "#B0FF3F")
83 (160. . "#83FF3F")
84 (180. . "#56FF3F")
85 (200. . "#3FFF56")
86 (220. . "#3FFF83")
87 (240. . "#3FFFB0")
88 (260. . "#3FFFDD")
89 (280. . "#3FF3FF")
90 (300. . "#3FC6FF")
91 (320. . "#3F99FF")
92 (340. . "#3F6CFF")
93 (360. . "#3F3FFF")))
94 "Association list of age versus color, for \\[vc-annotate].
95 Ages are given in units of fractional days. Default is eighteen
96 steps using a twenty day increment, from red to blue. For TTY
97 displays with 8 or fewer colors, the default is red to blue with
98 all other colors between (excluding black and white)."
99 :type 'alist
100 :group 'vc)
102 (defcustom vc-annotate-very-old-color "#3F3FFF"
103 "Color for lines older than the current color range in \\[vc-annotate]."
104 :type 'string
105 :group 'vc)
107 (defcustom vc-annotate-background "black"
108 "Background color for \\[vc-annotate].
109 Default color is used if nil."
110 :type '(choice (const :tag "Default background" nil) (color))
111 :group 'vc)
113 (defcustom vc-annotate-menu-elements '(2 0.5 0.1 0.01)
114 "Menu elements for the mode-specific menu of VC-Annotate mode.
115 List of factors, used to expand/compress the time scale. See `vc-annotate'."
116 :type '(repeat number)
117 :group 'vc)
119 (defvar vc-annotate-mode-map
120 (let ((m (make-sparse-keymap)))
121 (define-key m "a" 'vc-annotate-revision-previous-to-line)
122 (define-key m "d" 'vc-annotate-show-diff-revision-at-line)
123 (define-key m "=" 'vc-annotate-show-diff-revision-at-line)
124 (define-key m "D" 'vc-annotate-show-changeset-diff-revision-at-line)
125 (define-key m "f" 'vc-annotate-find-revision-at-line)
126 (define-key m "j" 'vc-annotate-revision-at-line)
127 (define-key m "l" 'vc-annotate-show-log-revision-at-line)
128 (define-key m "n" 'vc-annotate-next-revision)
129 (define-key m "p" 'vc-annotate-prev-revision)
130 (define-key m "w" 'vc-annotate-working-revision)
131 (define-key m "v" 'vc-annotate-toggle-annotation-visibility)
132 (define-key m "v" 'vc-annotate-toggle-annotation-visibility)
133 (define-key m "\C-m" 'vc-annotate-goto-line)
135 "Local keymap used for VC-Annotate mode.")
137 ;;; Annotate functionality
139 ;; Declare globally instead of additional parameter to
140 ;; temp-buffer-show-function (not possible to pass more than one
141 ;; parameter). The use of annotate-ratio is deprecated in favor of
142 ;; annotate-mode, which replaces it with the more sensible "span-to
143 ;; days", along with autoscaling support.
144 (defvar vc-annotate-ratio nil "Global variable.")
146 ;; internal buffer-local variables
147 (defvar vc-annotate-backend nil)
148 (defvar vc-annotate-parent-file nil)
149 (defvar vc-annotate-parent-rev nil)
150 (defvar vc-annotate-parent-display-mode nil)
152 (defconst vc-annotate-font-lock-keywords
153 ;; The fontification is done by vc-annotate-lines instead of font-lock.
154 '((vc-annotate-lines)))
156 (define-derived-mode vc-annotate-mode special-mode "Annotate"
157 "Major mode for output buffers of the `vc-annotate' command.
159 You can use the mode-specific menu to alter the time-span of the used
160 colors. See variable `vc-annotate-menu-elements' for customizing the
161 menu items."
162 ;; Frob buffer-invisibility-spec so that if it is originally a naked t,
163 ;; it will become a list, to avoid initial annotations being invisible.
164 (add-to-invisibility-spec 'foo)
165 (remove-from-invisibility-spec 'foo)
166 (set (make-local-variable 'truncate-lines) t)
167 (set (make-local-variable 'font-lock-defaults)
168 '(vc-annotate-font-lock-keywords t))
169 (hack-dir-local-variables-non-file-buffer))
171 (defun vc-annotate-toggle-annotation-visibility ()
172 "Toggle whether or not the annotation is visible."
173 (interactive)
174 (funcall (if (memq 'vc-annotate-annotation buffer-invisibility-spec)
175 'remove-from-invisibility-spec
176 'add-to-invisibility-spec)
177 'vc-annotate-annotation)
178 (force-window-update (current-buffer)))
180 (defun vc-annotate-display-default (ratio)
181 "Display the output of \\[vc-annotate] using the default color range.
182 The color range is given by `vc-annotate-color-map', scaled by RATIO.
183 The current time is used as the offset."
184 (interactive (progn (kill-local-variable 'vc-annotate-color-map) '(1.0)))
185 (message "Redisplaying annotation...")
186 (vc-annotate-display ratio)
187 (message "Redisplaying annotation...done"))
189 (defun vc-annotate-oldest-in-map (color-map)
190 "Return the oldest time in the COLOR-MAP."
191 ;; Since entries should be sorted, we can just use the last one.
192 (caar (last color-map)))
194 (defun vc-annotate-get-time-set-line-props ()
195 (let ((bol (point))
196 (date (vc-call-backend vc-annotate-backend 'annotate-time))
197 (inhibit-read-only t))
198 (assert (>= (point) bol))
199 (put-text-property bol (point) 'invisible 'vc-annotate-annotation)
200 date))
202 (defun vc-annotate-display-autoscale (&optional full)
203 "Highlight the output of \\[vc-annotate] using an autoscaled color map.
204 Autoscaling means that the map is scaled from the current time to the
205 oldest annotation in the buffer, or, with prefix argument FULL, to
206 cover the range from the oldest annotation to the newest."
207 (interactive "P")
208 (let ((newest 0.0)
209 (oldest 999999.) ;Any CVS users at the founding of Rome?
210 (current (vc-annotate-convert-time (current-time)))
211 date)
212 (message "Redisplaying annotation...")
213 ;; Run through this file and find the oldest and newest dates annotated.
214 (save-excursion
215 (goto-char (point-min))
216 (while (not (eobp))
217 (when (setq date (vc-annotate-get-time-set-line-props))
218 (when (> date newest)
219 (setq newest date))
220 (when (< date oldest)
221 (setq oldest date)))
222 (forward-line 1)))
223 (vc-annotate-display
224 (/ (- (if full newest current) oldest)
225 (vc-annotate-oldest-in-map vc-annotate-color-map))
226 (if full newest))
227 (message "Redisplaying annotation...done \(%s\)"
228 (if full
229 (format "Spanned from %.1f to %.1f days old"
230 (- current oldest)
231 (- current newest))
232 (format "Spanned to %.1f days old" (- current oldest))))))
234 ;; Menu -- Using easymenu.el
235 (easy-menu-define vc-annotate-mode-menu vc-annotate-mode-map
236 "VC Annotate Display Menu"
237 `("VC-Annotate"
238 ["By Color Map Range" (unless (null vc-annotate-display-mode)
239 (setq vc-annotate-display-mode nil)
240 (vc-annotate-display-select))
241 :style toggle :selected (null vc-annotate-display-mode)]
242 ,@(let ((oldest-in-map (vc-annotate-oldest-in-map vc-annotate-color-map)))
243 (mapcar (lambda (element)
244 (let ((days (* element oldest-in-map)))
245 `[,(format "Span %.1f days" days)
246 (vc-annotate-display-select nil ,days)
247 :style toggle :selected
248 (eql vc-annotate-display-mode ,days) ]))
249 vc-annotate-menu-elements))
250 ["Span ..."
251 (vc-annotate-display-select
252 nil (float (string-to-number (read-string "Span how many days? "))))]
253 "--"
254 ["Span to Oldest"
255 (unless (eq vc-annotate-display-mode 'scale)
256 (vc-annotate-display-select nil 'scale))
257 :help
258 "Use an autoscaled color map from the oldest annotation to the current time"
259 :style toggle :selected
260 (eq vc-annotate-display-mode 'scale)]
261 ["Span Oldest->Newest"
262 (unless (eq vc-annotate-display-mode 'fullscale)
263 (vc-annotate-display-select nil 'fullscale))
264 :help
265 "Use an autoscaled color map from the oldest to the newest annotation"
266 :style toggle :selected
267 (eq vc-annotate-display-mode 'fullscale)]
268 "--"
269 ["Toggle annotation visibility" vc-annotate-toggle-annotation-visibility
270 :help
271 "Toggle whether the annotation is visible or not"]
272 ["Annotate previous revision" vc-annotate-prev-revision
273 :help "Visit the annotation of the revision previous to this one"]
274 ["Annotate next revision" vc-annotate-next-revision
275 :help "Visit the annotation of the revision after this one"]
276 ["Annotate revision at line" vc-annotate-revision-at-line
277 :help
278 "Visit the annotation of the revision identified in the current line"]
279 ["Annotate revision previous to line" vc-annotate-revision-previous-to-line
280 :help "Visit the annotation of the revision before the revision at line"]
281 ["Annotate latest revision" vc-annotate-working-revision
282 :help "Visit the annotation of the working revision of this file"]
283 "--"
284 ["Show log of revision at line" vc-annotate-show-log-revision-at-line
285 :help "Visit the log of the revision at line"]
286 ["Show diff of revision at line" vc-annotate-show-diff-revision-at-line
287 :help "Visit the diff of the revision at line from its previous revision"]
288 ["Show changeset diff of revision at line"
289 vc-annotate-show-changeset-diff-revision-at-line
290 :enable
291 (eq 'repository (vc-call-backend ,vc-annotate-backend 'revision-granularity))
292 :help "Visit the diff of the revision at line from its previous revision"]
293 ["Visit revision at line" vc-annotate-find-revision-at-line
294 :help "Visit the revision identified in the current line"]))
296 (defun vc-annotate-display-select (&optional buffer mode)
297 "Highlight the output of \\[vc-annotate].
298 By default, the current buffer is highlighted, unless overridden by
299 BUFFER. `vc-annotate-display-mode' specifies the highlighting mode to
300 use; you may override this using the second optional arg MODE."
301 (interactive)
302 (when mode (setq vc-annotate-display-mode mode))
303 (pop-to-buffer (or buffer (current-buffer)))
304 (cond ((null vc-annotate-display-mode)
305 ;; The ratio is global, thus relative to the global color-map.
306 (kill-local-variable 'vc-annotate-color-map)
307 (vc-annotate-display-default (or vc-annotate-ratio 1.0)))
308 ;; One of the auto-scaling modes
309 ((eq vc-annotate-display-mode 'scale)
310 (vc-exec-after `(vc-annotate-display-autoscale)))
311 ((eq vc-annotate-display-mode 'fullscale)
312 (vc-exec-after `(vc-annotate-display-autoscale t)))
313 ((numberp vc-annotate-display-mode) ; A fixed number of days lookback
314 (vc-annotate-display-default
315 (/ vc-annotate-display-mode
316 (vc-annotate-oldest-in-map vc-annotate-color-map))))
317 (t (error "No such display mode: %s"
318 vc-annotate-display-mode))))
320 ;;;###autoload
321 (defun vc-annotate (file rev &optional display-mode buf move-point-to vc-bk)
322 "Display the edit history of the current FILE using colors.
324 This command creates a buffer that shows, for each line of the current
325 file, when it was last edited and by whom. Additionally, colors are
326 used to show the age of each line--blue means oldest, red means
327 youngest, and intermediate colors indicate intermediate ages. By
328 default, the time scale stretches back one year into the past;
329 everything that is older than that is shown in blue.
331 With a prefix argument, this command asks two questions in the
332 minibuffer. First, you may enter a revision number REV; then the buffer
333 displays and annotates that revision instead of the working revision
334 \(type RET in the minibuffer to leave that default unchanged). Then,
335 you are prompted for the time span in days which the color range
336 should cover. For example, a time span of 20 days means that changes
337 over the past 20 days are shown in red to blue, according to their
338 age, and everything that is older than that is shown in blue.
340 If MOVE-POINT-TO is given, move the point to that line.
342 If VC-BK is given used that VC backend.
344 Customization variables:
346 `vc-annotate-menu-elements' customizes the menu elements of the
347 mode-specific menu. `vc-annotate-color-map' and
348 `vc-annotate-very-old-color' define the mapping of time to colors.
349 `vc-annotate-background' specifies the background color."
350 (interactive
351 (save-current-buffer
352 (vc-ensure-vc-buffer)
353 (list buffer-file-name
354 (let ((def (vc-working-revision buffer-file-name)))
355 (if (null current-prefix-arg) def
356 (vc-read-revision
357 (format "Annotate from revision (default %s): " def)
358 (list buffer-file-name) nil def)))
359 (if (null current-prefix-arg)
360 vc-annotate-display-mode
361 (float (string-to-number
362 (read-string "Annotate span days (default 20): "
363 nil nil "20")))))))
364 (vc-ensure-vc-buffer)
365 (setq vc-annotate-display-mode display-mode) ;Not sure why. --Stef
366 (let* ((temp-buffer-name (format "*Annotate %s (rev %s)*" (buffer-name) rev))
367 (temp-buffer-show-function 'vc-annotate-display-select)
368 ;; If BUF is specified, we presume the caller maintains current line,
369 ;; so we don't need to do it here. This implementation may give
370 ;; strange results occasionally in the case of REV != WORKFILE-REV.
371 (current-line (or move-point-to (unless buf
372 (save-restriction
373 (widen)
374 (line-number-at-pos))))))
375 (message "Annotating...")
376 ;; If BUF is specified it tells in which buffer we should put the
377 ;; annotations. This is used when switching annotations to another
378 ;; revision, so we should update the buffer's name.
379 (when buf (with-current-buffer buf
380 (rename-buffer temp-buffer-name t)
381 ;; In case it had to be uniquified.
382 (setq temp-buffer-name (buffer-name))))
383 (with-output-to-temp-buffer temp-buffer-name
384 (let ((backend (or vc-bk (vc-backend file)))
385 (coding-system-for-read buffer-file-coding-system))
386 (vc-call-backend backend 'annotate-command file
387 (get-buffer temp-buffer-name) rev)
388 ;; we must setup the mode first, and then set our local
389 ;; variables before the show-function is called at the exit of
390 ;; with-output-to-temp-buffer
391 (with-current-buffer temp-buffer-name
392 (unless (equal major-mode 'vc-annotate-mode)
393 (vc-annotate-mode))
394 (set (make-local-variable 'vc-annotate-backend) backend)
395 (set (make-local-variable 'vc-annotate-parent-file) file)
396 (set (make-local-variable 'vc-annotate-parent-rev) rev)
397 (set (make-local-variable 'vc-annotate-parent-display-mode)
398 display-mode))))
400 (with-current-buffer temp-buffer-name
401 (vc-exec-after
402 `(progn
403 ;; Ideally, we'd rather not move point if the user has already
404 ;; moved it elsewhere, but really point here is not the position
405 ;; of the user's cursor :-(
406 (when ,current-line ;(and (bobp))
407 (goto-line ,current-line)
408 (setq vc-sentinel-movepoint (point)))
409 (unless (active-minibuffer-window)
410 (message "Annotating... done")))))))
412 (defun vc-annotate-prev-revision (prefix)
413 "Visit the annotation of the revision previous to this one.
415 With a numeric prefix argument, annotate the revision that many
416 revisions previous."
417 (interactive "p")
418 (vc-annotate-warp-revision (- 0 prefix)))
420 (defun vc-annotate-next-revision (prefix)
421 "Visit the annotation of the revision after this one.
423 With a numeric prefix argument, annotate the revision that many
424 revisions after."
425 (interactive "p")
426 (vc-annotate-warp-revision prefix))
428 (defun vc-annotate-working-revision ()
429 "Visit the annotation of the working revision of this file."
430 (interactive)
431 (if (not (equal major-mode 'vc-annotate-mode))
432 (message "Cannot be invoked outside of a vc annotate buffer")
433 (let ((warp-rev (vc-working-revision vc-annotate-parent-file)))
434 (if (equal warp-rev vc-annotate-parent-rev)
435 (message "Already at revision %s" warp-rev)
436 (vc-annotate-warp-revision warp-rev)))))
438 (defun vc-annotate-extract-revision-at-line ()
439 "Extract the revision number of the current line.
440 Return a cons (REV . FILENAME)."
441 ;; This function must be invoked from a buffer in vc-annotate-mode
442 (let ((rev (vc-call-backend vc-annotate-backend
443 'annotate-extract-revision-at-line)))
444 (if (or (null rev) (consp rev))
446 (cons rev vc-annotate-parent-file))))
448 (defun vc-annotate-revision-at-line ()
449 "Visit the annotation of the revision identified in the current line."
450 (interactive)
451 (if (not (equal major-mode 'vc-annotate-mode))
452 (message "Cannot be invoked outside of a vc annotate buffer")
453 (let ((rev-at-line (vc-annotate-extract-revision-at-line)))
454 (if (not rev-at-line)
455 (message "Cannot extract revision number from the current line")
456 (if (and (equal (car rev-at-line) vc-annotate-parent-rev)
457 (string= (cdr rev-at-line) vc-annotate-parent-file))
458 (message "Already at revision %s" rev-at-line)
459 (vc-annotate-warp-revision (car rev-at-line) (cdr rev-at-line)))))))
461 (defun vc-annotate-find-revision-at-line ()
462 "Visit the revision identified in the current line."
463 (interactive)
464 (if (not (equal major-mode 'vc-annotate-mode))
465 (message "Cannot be invoked outside of a vc annotate buffer")
466 (let ((rev-at-line (vc-annotate-extract-revision-at-line)))
467 (if (not rev-at-line)
468 (message "Cannot extract revision number from the current line")
469 (switch-to-buffer-other-window
470 (vc-find-revision (cdr rev-at-line) (car rev-at-line) vc-annotate-backend))))))
472 (defun vc-annotate-revision-previous-to-line ()
473 "Visit the annotation of the revision before the revision at line."
474 (interactive)
475 (if (not (equal major-mode 'vc-annotate-mode))
476 (message "Cannot be invoked outside of a vc annotate buffer")
477 (let* ((rev-at-line (vc-annotate-extract-revision-at-line))
478 (prev-rev nil)
479 (rev (car rev-at-line))
480 (fname (cdr rev-at-line)))
481 (if (not rev-at-line)
482 (message "Cannot extract revision number from the current line")
483 (setq prev-rev
484 (vc-call-backend vc-annotate-backend 'previous-revision
485 fname rev))
486 (vc-annotate-warp-revision prev-rev fname)))))
488 (defvar log-view-vc-backend)
489 (defvar log-view-vc-fileset)
491 (defun vc-annotate-show-log-revision-at-line ()
492 "Visit the log of the revision at line.
493 If the VC backend supports it, only show the log entry for the revision.
494 If a *vc-change-log* buffer exists and already shows a log for
495 the file in question, search for the log entry required and move point."
496 (interactive)
497 (if (not (equal major-mode 'vc-annotate-mode))
498 (message "Cannot be invoked outside of a vc annotate buffer")
499 (let ((rev-at-line (vc-annotate-extract-revision-at-line)))
500 (if (not rev-at-line)
501 (message "Cannot extract revision number from the current line")
502 (let ((backend vc-annotate-backend)
503 (log-buf (get-buffer "*vc-change-log*"))
504 pos)
505 (if (and
506 log-buf
507 ;; Look for a log buffer that already displays the correct file.
508 (with-current-buffer log-buf
509 (and (eq backend log-view-vc-backend)
510 (null (cdr log-view-vc-fileset))
511 (string= (car log-view-vc-fileset) (cdr rev-at-line))
512 ;; Check if the entry we require can be found.
513 (vc-call-backend
514 backend 'show-log-entry (car rev-at-line))
515 (setq pos (point)))))
516 (progn
517 (pop-to-buffer log-buf)
518 (goto-char pos))
519 ;; Ask the backend to display a single log entry.
520 (vc-print-log-internal
521 vc-annotate-backend (list (cdr rev-at-line))
522 (car rev-at-line) t 1)))))))
524 (defun vc-annotate-show-diff-revision-at-line-internal (filediff)
525 (if (not (equal major-mode 'vc-annotate-mode))
526 (message "Cannot be invoked outside of a vc annotate buffer")
527 (let* ((rev-at-line (vc-annotate-extract-revision-at-line))
528 (prev-rev nil)
529 (rev (car rev-at-line))
530 (fname (cdr rev-at-line)))
531 (if (not rev-at-line)
532 (message "Cannot extract revision number from the current line")
533 (setq prev-rev
534 (vc-call-backend vc-annotate-backend 'previous-revision
535 (if filediff fname nil) rev))
536 (if (not prev-rev)
537 (message "Cannot diff from any revision prior to %s" rev)
538 (save-window-excursion
539 (vc-diff-internal
541 ;; The value passed here should follow what
542 ;; `vc-deduce-fileset' returns.
543 (list vc-annotate-backend
544 (if filediff
545 (list fname)
546 nil))
547 prev-rev rev))
548 (switch-to-buffer "*vc-diff*"))))))
550 (defun vc-annotate-show-diff-revision-at-line ()
551 "Visit the diff of the revision at line from its previous revision."
552 (interactive)
553 (vc-annotate-show-diff-revision-at-line-internal t))
555 (defun vc-annotate-show-changeset-diff-revision-at-line ()
556 "Visit the diff of the revision at line from its previous revision for all files in the changeset."
557 (interactive)
558 (when (eq 'file (vc-call-backend vc-annotate-backend 'revision-granularity))
559 (error "The %s backend does not support changeset diffs" vc-annotate-backend))
560 (vc-annotate-show-diff-revision-at-line-internal nil))
562 (defun vc-annotate-warp-revision (revspec &optional file)
563 "Annotate the revision described by REVSPEC.
565 If REVSPEC is a positive integer, warp that many revisions forward,
566 if possible, otherwise echo a warning message. If REVSPEC is a
567 negative integer, warp that many revisions backward, if possible,
568 otherwise echo a warning message. If REVSPEC is a string, then it
569 describes a revision number, so warp to that revision."
570 (if (not (equal major-mode 'vc-annotate-mode))
571 (message "Cannot be invoked outside of a vc annotate buffer")
572 (let* ((buf (current-buffer))
573 (oldline (line-number-at-pos))
574 (revspeccopy revspec)
575 (newrev nil))
576 (cond
577 ((and (integerp revspec) (> revspec 0))
578 (setq newrev vc-annotate-parent-rev)
579 (while (and (> revspec 0) newrev)
580 (setq newrev (vc-call-backend vc-annotate-backend 'next-revision
581 (or file vc-annotate-parent-file) newrev))
582 (setq revspec (1- revspec)))
583 (unless newrev
584 (message "Cannot increment %d revisions from revision %s"
585 revspeccopy vc-annotate-parent-rev)))
586 ((and (integerp revspec) (< revspec 0))
587 (setq newrev vc-annotate-parent-rev)
588 (while (and (< revspec 0) newrev)
589 (setq newrev (vc-call-backend vc-annotate-backend 'previous-revision
590 (or file vc-annotate-parent-file) newrev))
591 (setq revspec (1+ revspec)))
592 (unless newrev
593 (message "Cannot decrement %d revisions from revision %s"
594 (- 0 revspeccopy) vc-annotate-parent-rev)))
595 ((stringp revspec) (setq newrev revspec))
596 (t (error "Invalid argument to vc-annotate-warp-revision")))
597 (when newrev
598 (vc-annotate (or file vc-annotate-parent-file) newrev
599 vc-annotate-parent-display-mode
601 ;; Pass the current line so that vc-annotate will
602 ;; place the point in the line.
603 (min oldline (progn (goto-char (point-max))
604 (forward-line -1)
605 (line-number-at-pos)))
606 vc-annotate-backend)))))
608 (defun vc-annotate-compcar (threshold a-list)
609 "Test successive cons cells of A-LIST against THRESHOLD.
610 Return the first cons cell with a car that is not less than THRESHOLD,
611 nil if no such cell exists."
612 (let ((i 1)
613 (tmp-cons (car a-list)))
614 (while (and tmp-cons (< (car tmp-cons) threshold))
615 (setq tmp-cons (car (nthcdr i a-list)))
616 (setq i (+ i 1)))
617 tmp-cons)) ; Return the appropriate value
619 (defun vc-annotate-convert-time (time)
620 "Convert a time value to a floating-point number of days.
621 The argument TIME is a list as returned by `current-time' or
622 `encode-time', only the first two elements of that list are considered."
623 (/ (+ (* (float (car time)) (lsh 1 16)) (cadr time)) 24 3600))
625 (defun vc-annotate-difference (&optional offset)
626 "Return the time span in days to the next annotation.
627 This calls the backend function annotate-time, and returns the
628 difference in days between the time returned and the current time,
629 or OFFSET if present."
630 (let ((next-time (vc-annotate-get-time-set-line-props)))
631 (when next-time
632 (- (or offset
633 (vc-call-backend vc-annotate-backend 'annotate-current-time))
634 next-time))))
636 (defun vc-default-annotate-current-time (backend)
637 "Return the current time, encoded as fractional days."
638 (vc-annotate-convert-time (current-time)))
640 (defvar vc-annotate-offset nil)
642 (defun vc-annotate-display (ratio &optional offset)
643 "Highlight `vc-annotate' output in the current buffer.
644 RATIO is the expansion that should be applied to `vc-annotate-color-map'.
645 The annotations are relative to the current time, unless overridden by OFFSET."
646 (when (/= ratio 1.0)
647 (set (make-local-variable 'vc-annotate-color-map)
648 (mapcar (lambda (elem) (cons (* (car elem) ratio) (cdr elem)))
649 vc-annotate-color-map)))
650 (set (make-local-variable 'vc-annotate-offset) offset)
651 (font-lock-mode 1))
653 (defun vc-annotate-lines (limit)
654 (while (< (point) limit)
655 (let ((difference (vc-annotate-difference vc-annotate-offset))
656 (start (point))
657 (end (progn (forward-line 1) (point))))
658 (when difference
659 (let* ((color (or (vc-annotate-compcar difference vc-annotate-color-map)
660 (cons nil vc-annotate-very-old-color)))
661 ;; substring from index 1 to remove any leading `#' in the name
662 (face-name (concat "vc-annotate-face-"
663 (if (string-equal
664 (substring (cdr color) 0 1) "#")
665 (substring (cdr color) 1)
666 (cdr color))))
667 ;; Make the face if not done.
668 (face (or (intern-soft face-name)
669 (let ((tmp-face (make-face (intern face-name))))
670 (set-face-foreground tmp-face (cdr color))
671 (when vc-annotate-background
672 (set-face-background tmp-face
673 vc-annotate-background))
674 tmp-face)))) ; Return the face
675 (put-text-property start end 'face face)))))
676 ;; Pretend to font-lock there were no matches.
677 nil)
679 (defun vc-annotate-goto-line ()
680 "Go to the line corresponding to the current VC Annotate line."
681 (interactive)
682 (unless (eq major-mode 'vc-annotate-mode)
683 (error "Not in a VC-Annotate buffer"))
684 (let ((line (save-restriction
685 (widen)
686 (line-number-at-pos)))
687 (rev vc-annotate-parent-rev))
688 (pop-to-buffer
689 (or (and (buffer-live-p vc-parent-buffer)
690 vc-parent-buffer)
691 (and (file-exists-p vc-annotate-parent-file)
692 (find-file-noselect vc-annotate-parent-file))
693 (error "File not found: %s" vc-annotate-parent-file)))
694 (save-restriction
695 (widen)
696 (goto-char (point-min))
697 (forward-line (1- line))
698 (recenter))
699 ;; Issue a warning if the lines might be incorrect.
700 (cond
701 ((buffer-modified-p)
702 (message "Buffer modified; annotated line numbers may be incorrect"))
703 ((not (eq (vc-state buffer-file-name) 'up-to-date))
704 (message "File is not up-to-date; annotated line numbers may be incorrect"))
705 ((not (equal rev (vc-working-revision buffer-file-name)))
706 (message "Annotations were for revision %s; line numbers may be incorrect"
707 rev)))))
709 (provide 'vc-annotate)
711 ;;; vc-annotate.el ends here