vc-annotate.el 29 KB

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