]> code.delx.au - gnu-emacs/blob - lisp/vc/log-view.el
Merge changes from emacs-23 branch
[gnu-emacs] / lisp / vc / log-view.el
1 ;;; log-view.el --- Major mode for browsing RCS/CVS/SCCS log output
2
3 ;; Copyright (C) 1999-2011 Free Software Foundation, Inc.
4
5 ;; Author: Stefan Monnier <monnier@iro.umontreal.ca>
6 ;; Keywords: rcs, sccs, cvs, log, vc, tools
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
22
23 ;;; Commentary:
24
25 ;; Major mode to browse revision log histories.
26 ;; Currently supports the format output by:
27 ;; RCS, SCCS, CVS, Subversion, and DaRCS.
28
29 ;; Examples of log output:
30
31 ;;;; RCS/CVS:
32
33 ;; ----------------------------
34 ;; revision 1.35 locked by: turlutut
35 ;; date: 2005-03-22 18:48:38 +0000; author: monnier; state: Exp; lines: +6 -8
36 ;; (gnus-display-time-event-handler):
37 ;; Check display-time-timer at runtime rather than only at load time
38 ;; in case display-time-mode is turned off in the mean time.
39 ;; ----------------------------
40 ;; revision 1.34
41 ;; date: 2005-02-09 15:50:38 +0000; author: kfstorm; state: Exp; lines: +7 -7
42 ;; branches: 1.34.2;
43 ;; Change release version from 21.4 to 22.1 throughout.
44 ;; Change development version from 21.3.50 to 22.0.50.
45
46 ;;;; SCCS:
47
48 ;;;; Subversion:
49
50 ;; ------------------------------------------------------------------------
51 ;; r4622 | ckuethe | 2007-12-23 18:18:01 -0500 (Sun, 23 Dec 2007) | 2 lines
52 ;;
53 ;; uBlox AEK-4T in binary mode. Added to unstable because it breaks gpsfake
54 ;;
55 ;; ------------------------------------------------------------------------
56 ;; r4621 | ckuethe | 2007-12-23 16:48:11 -0500 (Sun, 23 Dec 2007) | 3 lines
57 ;;
58 ;; Add a note about requiring usbfs to use the garmin gps18 (usb)
59 ;; Mention firmware testing the AC12 with firmware BQ00 and BQ04
60 ;;
61 ;; ------------------------------------------------------------------------
62 ;; r4620 | ckuethe | 2007-12-23 15:52:34 -0500 (Sun, 23 Dec 2007) | 1 line
63 ;;
64 ;; add link to latest hardware reference
65 ;; ------------------------------------------------------------------------
66 ;; r4619 | ckuethe | 2007-12-23 14:37:31 -0500 (Sun, 23 Dec 2007) | 1 line
67 ;;
68 ;; there is now a regression test for AC12 without raw data output
69
70 ;;;; Darcs:
71
72 ;; Changes to darcsum.el:
73 ;;
74 ;; Mon Nov 28 15:19:38 GMT 2005 Dave Love <fx@gnu.org>
75 ;; * Abstract process startup into darcsum-start-process. Use TERM=dumb.
76 ;; TERM=dumb avoids escape characters, at least, for any old darcs that
77 ;; doesn't understand DARCS_DONT_COLOR & al.
78 ;;
79 ;; Thu Nov 24 15:20:45 GMT 2005 Dave Love <fx@gnu.org>
80 ;; * darcsum-mode-related changes.
81 ;; Don't call font-lock-mode (unnecessary) or use-local-map (redundant).
82 ;; Use mode-class 'special. Add :group.
83 ;; Add trailing-whitespace option to mode hook and fix
84 ;; darcsum-display-changeset not to use trailing whitespace.
85
86 ;;;; Mercurial
87
88 ;; changeset: 11:8ff1a4166444
89 ;; tag: tip
90 ;; user: Eric S. Raymond <esr@thyrsus.com>
91 ;; date: Wed Dec 26 12:18:58 2007 -0500
92 ;; summary: Explain keywords. Add markup fixes.
93 ;;
94 ;; changeset: 10:20abc7ab09c3
95 ;; user: Eric S. Raymond <esr@thyrsus.com>
96 ;; date: Wed Dec 26 11:37:28 2007 -0500
97 ;; summary: Typo fixes.
98 ;;
99 ;; changeset: 9:ada9f4da88aa
100 ;; user: Eric S. Raymond <esr@thyrsus.com>
101 ;; date: Wed Dec 26 11:23:00 2007 -0500
102 ;; summary: Add RCS example session.
103
104 ;;; Todo:
105
106 ;; - add ability to modify a log-entry (via cvs-mode-admin ;-)
107 ;; - remove references to cvs-*
108 ;; - make it easier to add support for new backends without changing the code.
109
110 ;;; Code:
111
112 (eval-when-compile (require 'cl))
113 (require 'pcvs-util)
114 (autoload 'vc-find-revision "vc")
115 (autoload 'vc-diff-internal "vc")
116
117 (defvar cvs-minor-wrap-function)
118
119 (defgroup log-view nil
120 "Major mode for browsing log output of RCS/CVS/SCCS."
121 :group 'pcl-cvs
122 :prefix "log-view-")
123
124 ;; Needed because log-view-mode-map inherits from widget-keymap. (Bug#5311)
125 (require 'wid-edit)
126
127 (easy-mmode-defmap log-view-mode-map
128 '(
129 ;; FIXME: (copy-keymap special-mode-map) instead
130 ("z" . kill-this-buffer)
131 ("q" . quit-window)
132 ("g" . revert-buffer)
133
134 ("m" . log-view-toggle-mark-entry)
135 ("e" . log-view-modify-change-comment)
136 ("d" . log-view-diff)
137 ("=" . log-view-diff)
138 ("D" . log-view-diff-changeset)
139 ("a" . log-view-annotate-version)
140 ("f" . log-view-find-revision)
141 ("n" . log-view-msg-next)
142 ("p" . log-view-msg-prev)
143 ("\t" . log-view-msg-next)
144 ([backtab] . log-view-msg-prev)
145 ("N" . log-view-file-next)
146 ("P" . log-view-file-prev)
147 ("\M-n" . log-view-file-next)
148 ("\M-p" . log-view-file-prev))
149 "Log-View's keymap."
150 :inherit widget-keymap
151 :group 'log-view)
152
153 (easy-menu-define log-view-mode-menu log-view-mode-map
154 "Log-View Display Menu"
155 `("Log-View"
156 ;; XXX Do we need menu entries for these?
157 ;; ["Quit" quit-window]
158 ;; ["Kill This Buffer" kill-this-buffer]
159 ["Mark Log Entry for Diff" set-mark-command
160 :help ""]
161 ["Diff Revisions" log-view-diff
162 :help "Get the diff between two revisions"]
163 ["Changeset Diff" log-view-diff-changeset
164 :help "Get the changeset diff between two revisions"]
165 ["Visit Version" log-view-find-revision
166 :help "Visit the version at point"]
167 ["Annotate Version" log-view-annotate-version
168 :help "Annotate the version at point"]
169 ["Modify Log Comment" log-view-modify-change-comment
170 :help "Edit the change comment displayed at point"]
171 "-----"
172 ["Next Log Entry" log-view-msg-next
173 :help "Go to the next count'th log message"]
174 ["Previous Log Entry" log-view-msg-prev
175 :help "Go to the previous count'th log message"]
176 ["Next File" log-view-file-next
177 :help "Go to the next count'th file"]
178 ["Previous File" log-view-file-prev
179 :help "Go to the previous count'th file"]))
180
181 (defvar log-view-mode-hook nil
182 "Hook run at the end of `log-view-mode'.")
183
184 (defface log-view-file
185 '((((class color) (background light))
186 (:background "grey70" :weight bold))
187 (t (:weight bold)))
188 "Face for the file header line in `log-view-mode'."
189 :group 'log-view)
190 (define-obsolete-face-alias 'log-view-file-face 'log-view-file "22.1")
191 (defvar log-view-file-face 'log-view-file)
192
193 (defface log-view-message
194 '((((class color) (background light))
195 (:background "grey85"))
196 (t (:weight bold)))
197 "Face for the message header line in `log-view-mode'."
198 :group 'log-view)
199 ;; backward-compatibility alias
200 (define-obsolete-face-alias 'log-view-message-face 'log-view-message "22.1")
201 (defvar log-view-message-face 'log-view-message)
202
203 (defvar log-view-file-re
204 (concat "^\\(?:Working file: \\(?1:.+\\)" ;RCS and CVS.
205 ;; Subversion has no such thing??
206 "\\|\\(?:SCCS/s\\.\\|Changes to \\)\\(?1:.+\\):" ;SCCS and Darcs.
207 "\\)\n") ;Include the \n for font-lock reasons.
208 "Regexp matching the text identifying the file.
209 The match group number 1 should match the file name itself.")
210
211 (defvar log-view-per-file-logs t
212 "Set if to t if the logs are shown one file at a time.")
213
214 (defvar log-view-message-re
215 (concat "^\\(?:revision \\(?1:[.0-9]+\\)\\(?:\t.*\\)?" ; RCS and CVS.
216 "\\|r\\(?1:[0-9]+\\) | .* | .*" ; Subversion.
217 "\\|D \\(?1:[.0-9]+\\) .*" ; SCCS.
218 ;; Darcs doesn't have revision names. VC-darcs uses patch names
219 ;; instead. Darcs patch names are hashcodes, which do not appear
220 ;; in the log output :-(, but darcs accepts any prefix of the log
221 ;; message as a patch name, so we match the first line of the log
222 ;; message.
223 ;; First loosely match the date format.
224 (concat "\\|[^ \n].*[^0-9\n][0-9][0-9]:[0-9][0-9][^0-9\n].*[^ \n]"
225 ;;Email of user and finally Msg, used as revision name.
226 " .*@.*\n\\(?: \\* \\(?1:.*\\)\\)?")
227 "\\)$")
228 "Regexp matching the text identifying a revision.
229 The match group number 1 should match the revision number itself.")
230
231 (defvar log-view-font-lock-keywords
232 ;; We use `eval' so as to use the buffer-local value of log-view-file-re
233 ;; and log-view-message-re, if applicable.
234 '((eval . `(,log-view-file-re
235 (1 (if (boundp 'cvs-filename-face) cvs-filename-face))
236 (0 log-view-file-face append)))
237 (eval . `(,log-view-message-re . log-view-message-face))))
238
239 (defconst log-view-font-lock-defaults
240 '(log-view-font-lock-keywords t nil nil nil))
241
242 (defvar log-view-vc-fileset nil
243 "Set this to the fileset corresponding to the current log.")
244
245 (defvar log-view-vc-backend nil
246 "Set this to the VC backend that created the current log.")
247
248 ;;;;
249 ;;;; Actual code
250 ;;;;
251
252 ;;;###autoload
253 (define-derived-mode log-view-mode special-mode "Log-View"
254 "Major mode for browsing CVS log output."
255 (setq buffer-read-only t)
256 (set (make-local-variable 'font-lock-defaults) log-view-font-lock-defaults)
257 (set (make-local-variable 'beginning-of-defun-function)
258 'log-view-beginning-of-defun)
259 (set (make-local-variable 'end-of-defun-function)
260 'log-view-end-of-defun)
261 (set (make-local-variable 'cvs-minor-wrap-function) 'log-view-minor-wrap)
262 (hack-dir-local-variables-non-file-buffer))
263
264 ;;;;
265 ;;;; Navigation
266 ;;;;
267
268 ;; define log-view-{msg,file}-{next,prev}
269 (easy-mmode-define-navigation log-view-msg log-view-message-re "log message")
270 (easy-mmode-define-navigation log-view-file log-view-file-re "file")
271
272 (defun log-view-goto-rev (rev)
273 (goto-char (point-min))
274 (ignore-errors
275 (while (not (equal rev (log-view-current-tag)))
276 (log-view-msg-next))
277 t))
278
279 ;;;;
280 ;;;; Linkage to PCL-CVS (mostly copied from cvs-status.el)
281 ;;;;
282
283 (defconst log-view-dir-re "^cvs[.ex]* [a-z]+: Logging \\(.+\\)$")
284
285 (defun log-view-current-file ()
286 (save-excursion
287 (forward-line 1)
288 (or (re-search-backward log-view-file-re nil t)
289 (re-search-forward log-view-file-re nil t)
290 (error "Unable to determine the current file"))
291 (let* ((file (match-string 1))
292 (cvsdir (and (re-search-backward log-view-dir-re nil t)
293 (match-string 1)))
294 (pcldir (and (boundp 'cvs-pcl-cvs-dirchange-re)
295 (re-search-backward cvs-pcl-cvs-dirchange-re nil t)
296 (match-string 1)))
297 (dir ""))
298 (let ((default-directory ""))
299 (when pcldir (setq dir (expand-file-name pcldir dir)))
300 (when cvsdir (setq dir (expand-file-name cvsdir dir))))
301 (expand-file-name file dir))))
302
303 (defun log-view-current-tag (&optional where)
304 (save-excursion
305 (when where (goto-char where))
306 (forward-line 1)
307 (let ((pt (point)))
308 (when (re-search-backward log-view-message-re nil t)
309 (let ((rev (match-string-no-properties 1)))
310 (unless (re-search-forward log-view-file-re pt t)
311 rev))))))
312
313 (defun log-view-toggle-mark-entry ()
314 "Toggle the marked state for the log entry at point.
315 Individual log entries can be marked and unmarked. The marked
316 entries are denoted by changing their background color.
317 `log-view-get-marked' returns the list of tags for the marked
318 log entries."
319 (interactive)
320 (save-excursion
321 (forward-line 1)
322 (let ((pt (point)))
323 (when (re-search-backward log-view-message-re nil t)
324 (let ((beg (match-beginning 0))
325 end ov ovlist found tag)
326 (unless (re-search-forward log-view-file-re pt t)
327 ;; Look to see if the current entry is marked.
328 (setq found (get-char-property (point) 'log-view-self))
329 (if found
330 (delete-overlay found)
331 ;; Create an overlay that covers this entry and change
332 ;; its color.
333 (setq tag (log-view-current-tag (point)))
334 (forward-line 1)
335 (setq end
336 (if (re-search-forward log-view-message-re nil t)
337 (match-beginning 0)
338 (point-max)))
339 (setq ov (make-overlay beg end))
340 (overlay-put ov 'face 'log-view-file)
341 ;; This is used to check if the overlay is present.
342 (overlay-put ov 'log-view-self ov)
343 (overlay-put ov 'log-view-marked tag))))))))
344
345 (defun log-view-get-marked ()
346 "Return the list of tags for the marked log entries."
347 (save-excursion
348 (let ((pos (point-min))
349 marked-list ov)
350 (while (setq pos (next-single-property-change pos 'face))
351 (when (setq ov (get-char-property pos 'log-view-self))
352 (push (overlay-get ov 'log-view-marked) marked-list)
353 (setq pos (overlay-end ov))))
354 marked-list)))
355
356 (defun log-view-beginning-of-defun ()
357 ;; This assumes that a log entry starts with a line matching
358 ;; `log-view-message-re'. Modes that derive from `log-view-mode'
359 ;; for which this assumption is not valid will have to provide
360 ;; another implementation of this function. `log-view-msg-prev'
361 ;; does a similar job to this function, we can't use it here
362 ;; directly because it prints messages that are not appropriate in
363 ;; this context and it does not move to the beginning of the buffer
364 ;; when the point is before the first log entry.
365
366 ;; `log-view-beginning-of-defun' and `log-view-end-of-defun' have
367 ;; been checked to work with logs produced by RCS, CVS, git,
368 ;; mercurial and subversion.
369
370 (re-search-backward log-view-message-re nil 'move))
371
372 (defun log-view-end-of-defun ()
373 ;; The idea in this function is to search for the beginning of the
374 ;; next log entry using `log-view-message-re' and then go back one
375 ;; line when finding it. Modes that derive from `log-view-mode' for
376 ;; which this assumption is not valid will have to provide another
377 ;; implementation of this function.
378
379 ;; Look back and if there is no entry there it means we are before
380 ;; the first log entry, so go forward until finding one.
381 (unless (save-excursion (re-search-backward log-view-message-re nil t))
382 (re-search-forward log-view-message-re nil t))
383
384 ;; In case we are at the end of log entry going forward a line will
385 ;; make us find the next entry when searching. If we are inside of
386 ;; an entry going forward a line will still keep the point inside
387 ;; the same entry.
388 (forward-line 1)
389
390 ;; In case we are at the beginning of an entry, move past it.
391 (when (looking-at log-view-message-re)
392 (goto-char (match-end 0))
393 (forward-line 1))
394
395 ;; Search for the start of the next log entry. Go to the end of the
396 ;; buffer if we could not find a next entry.
397 (when (re-search-forward log-view-message-re nil 'move)
398 (goto-char (match-beginning 0))
399 (forward-line -1)))
400
401 (defvar cvs-minor-current-files)
402 (defvar cvs-branch-prefix)
403 (defvar cvs-secondary-branch-prefix)
404
405 (defun log-view-minor-wrap (buf f)
406 (let ((data (with-current-buffer buf
407 (let* ((beg (point))
408 (end (if mark-active (mark) (point)))
409 (fr (log-view-current-tag beg))
410 (to (log-view-current-tag end)))
411 (when (string-equal fr to)
412 (save-excursion
413 (goto-char end)
414 (log-view-msg-next)
415 (setq to (log-view-current-tag))))
416 (cons
417 ;; The first revision has to be the one at point, for
418 ;; operations that only take one revision
419 ;; (e.g. cvs-mode-edit).
420 (cons (log-view-current-file) fr)
421 (cons (log-view-current-file) to))))))
422 (let ((cvs-branch-prefix (cdar data))
423 (cvs-secondary-branch-prefix (and (cdar data) (cddr data)))
424 (cvs-minor-current-files
425 (cons (caar data)
426 (when (and (cadr data) (not (equal (caar data) (cadr data))))
427 (list (cadr data)))))
428 ;; FIXME: I need to force because the fileinfos are UNKNOWN
429 (cvs-force-command "/F"))
430 (funcall f))))
431
432 (defun log-view-find-revision (pos)
433 "Visit the version at point."
434 (interactive "d")
435 (unless log-view-per-file-logs
436 (when (> (length log-view-vc-fileset) 1)
437 (error "Multiple files shown in this buffer, cannot use this command here")))
438 (save-excursion
439 (goto-char pos)
440 (switch-to-buffer (vc-find-revision (if log-view-per-file-logs
441 (log-view-current-file)
442 (car log-view-vc-fileset))
443 (log-view-current-tag)))))
444
445
446 (defun log-view-extract-comment ()
447 "Parse comment from around the current point in the log."
448 (save-excursion
449 (let (st en (backend (vc-backend (log-view-current-file))))
450 (log-view-end-of-defun)
451 (cond ((eq backend 'SVN)
452 (forward-line -1)))
453 (setq en (point))
454 (log-view-beginning-of-defun)
455 (cond ((memq backend '(SCCS RCS CVS MCVS SVN))
456 (forward-line 2))
457 ((eq backend 'Hg)
458 (forward-line 4)
459 (re-search-forward "summary: *" nil t)))
460 (setq st (point))
461 (buffer-substring st en))))
462
463 (declare-function vc-modify-change-comment "vc" (files rev oldcomment))
464
465 (defun log-view-modify-change-comment ()
466 "Edit the change comment displayed at point."
467 (interactive)
468 (vc-modify-change-comment (list (if log-view-per-file-logs
469 (log-view-current-file)
470 (car log-view-vc-fileset)))
471 (log-view-current-tag)
472 (log-view-extract-comment)))
473
474 (defun log-view-annotate-version (pos)
475 "Annotate the version at point."
476 (interactive "d")
477 (unless log-view-per-file-logs
478 (when (> (length log-view-vc-fileset) 1)
479 (error "Multiple files shown in this buffer, cannot use this command here")))
480 (save-excursion
481 (goto-char pos)
482 (vc-annotate (if log-view-per-file-logs
483 (log-view-current-file)
484 (car log-view-vc-fileset))
485 (log-view-current-tag))))
486
487 ;;
488 ;; diff
489 ;;
490
491 (defun log-view-diff (beg end)
492 "Get the diff between two revisions.
493 If the mark is not active or the mark is on the revision at point,
494 get the diff between the revision at point and its previous revision.
495 Otherwise, get the diff between the revisions where the region starts
496 and ends.
497 Contrary to `log-view-diff-changeset', it will only show the part of the
498 changeset that affected the currently considered file(s)."
499 (interactive
500 (list (if mark-active (region-beginning) (point))
501 (if mark-active (region-end) (point))))
502 (let ((fr (log-view-current-tag beg))
503 (to (log-view-current-tag end)))
504 (when (string-equal fr to)
505 (save-excursion
506 (goto-char end)
507 (log-view-msg-next)
508 (setq to (log-view-current-tag))))
509 (vc-diff-internal
510 t (list log-view-vc-backend
511 (if log-view-per-file-logs
512 (list (log-view-current-file))
513 log-view-vc-fileset))
514 to fr)))
515
516 (declare-function vc-diff-internal "vc"
517 (async vc-fileset rev1 rev2 &optional verbose))
518
519 (defun log-view-diff-changeset (beg end)
520 "Get the diff between two revisions.
521 If the mark is not active or the mark is on the revision at point,
522 get the diff between the revision at point and its previous revision.
523 Otherwise, get the diff between the revisions where the region starts
524 and ends.
525 Contrary to `log-view-diff', it will show the whole changeset including
526 the changes that affected other files than the currently considered file(s)."
527 (interactive
528 (list (if mark-active (region-beginning) (point))
529 (if mark-active (region-end) (point))))
530 (when (eq (vc-call-backend log-view-vc-backend 'revision-granularity) 'file)
531 (error "The %s backend does not support changeset diffs" log-view-vc-backend))
532 (let ((fr (log-view-current-tag beg))
533 (to (log-view-current-tag end)))
534 (when (string-equal fr to)
535 ;; TO and FR are the same, look at the previous revision.
536 (setq to (vc-call-backend log-view-vc-backend 'previous-revision nil fr)))
537 (vc-diff-internal
538 t
539 ;; We want to see the diff for all the files in the changeset, so
540 ;; pass NIL for the file list. The value passed here should
541 ;; follow what `vc-deduce-fileset' returns.
542 (list log-view-vc-backend nil)
543 to fr)))
544
545 (provide 'log-view)
546
547 ;;; log-view.el ends here