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