Bug fix for vc-dispatcher split.
[bpt/emacs.git] / lisp / log-view.el
CommitLineData
e57a1038 1;;; log-view.el --- Major mode for browsing RCS/CVS/SCCS log output
5b467bf4 2
aaef169d 3;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005,
8df5b06d 4;; 2006, 2007, 2008 Free Software Foundation, Inc.
5b467bf4 5
cc1eecfd 6;; Author: Stefan Monnier <monnier@iro.umontreal.ca>
e57a1038 7;; Keywords: rcs sccs cvs log version-control
5b467bf4
SM
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
b4aa6026 13;; the Free Software Foundation; either version 3, or (at your option)
5b467bf4
SM
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
086add15
LK
23;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24;; Boston, MA 02110-1301, USA.
5b467bf4
SM
25
26;;; Commentary:
27
aa230d94
SM
28;; Major mode to browse revision log histories.
29;; Currently supports the format output by:
62c5f375 30;; RCS, SCCS, CVS, Subversion, and DaRCS.
aa230d94
SM
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
813e8939
ER
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
62c5f375
SM
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
813e8939
ER
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
aa230d94 107;;; Todo:
5b467bf4 108
5b467bf4 109;; - add ability to modify a log-entry (via cvs-mode-admin ;-)
e57a1038 110;; - remove references to cvs-*
aa230d94 111;; - make it easier to add support for new backends without changing the code.
5b467bf4
SM
112
113;;; Code:
114
115(eval-when-compile (require 'cl))
5b467bf4 116(require 'pcvs-util)
ac3f4c6f 117(autoload 'vc-find-revision "vc")
99cb8c8b 118(autoload 'vc-version-diff "vc")
5b467bf4 119
71f6f911
JB
120(defvar cvs-minor-wrap-function)
121
5b467bf4 122(defgroup log-view nil
e57a1038 123 "Major mode for browsing log output of RCS/CVS/SCCS."
5b467bf4
SM
124 :group 'pcl-cvs
125 :prefix "log-view-")
126
127(easy-mmode-defmap log-view-mode-map
99cb8c8b
SS
128 '(("q" . quit-window)
129 ("z" . kill-this-buffer)
83db937c 130 ("m" . log-view-toggle-mark-entry)
9b64a7f0 131 ("e" . log-view-modify-change-comment)
99cb8c8b 132 ("d" . log-view-diff)
a3f5d84d 133 ("a" . log-view-annotate-version)
ac3f4c6f 134 ("f" . log-view-find-revision)
99cb8c8b 135 ("n" . log-view-msg-next)
cdbb990f 136 ("p" . log-view-msg-prev)
25edda53
DN
137 ("\t" . log-view-msg-next)
138 ([backtab] . log-view-msg-prev)
cdbb990f
SM
139 ("N" . log-view-file-next)
140 ("P" . log-view-file-prev)
e2c2a3e2
KG
141 ("\M-n" . log-view-file-next)
142 ("\M-p" . log-view-file-prev))
5b467bf4
SM
143 "Log-View's keymap."
144 :group 'log-view
e57a1038
SM
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))
5b467bf4 148
d621bc0a
DN
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]
799224fe
DN
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"]
031f1766
DN
163 ["Modify Log Comment" log-view-modify-change-comment
164 :help "Edit the change comment displayed at point"]
165 "-----"
799224fe
DN
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"]))
d621bc0a 174
5b467bf4
SM
175(defvar log-view-mode-hook nil
176 "Hook run at the end of `log-view-mode'.")
177
d842de85 178(defface log-view-file
5b467bf4 179 '((((class color) (background light))
1fd714a4
RS
180 (:background "grey70" :weight bold))
181 (t (:weight bold)))
5b467bf4
SM
182 "Face for the file header line in `log-view-mode'."
183 :group 'log-view)
d842de85
MB
184;; backward-compatibility alias
185(put 'log-view-file-face 'face-alias 'log-view-file)
186(defvar log-view-file-face 'log-view-file)
5b467bf4 187
d842de85 188(defface log-view-message
5b467bf4
SM
189 '((((class color) (background light))
190 (:background "grey85"))
1fd714a4 191 (t (:weight bold)))
5b467bf4
SM
192 "Face for the message header line in `log-view-mode'."
193 :group 'log-view)
d842de85
MB
194;; backward-compatibility alias
195(put 'log-view-message-face 'face-alias 'log-view-message)
196(defvar log-view-message-face 'log-view-message)
5b467bf4 197
18b2e5b9 198(defvar log-view-file-re
95588f91
SM
199 (concat "^\\(?:Working file: \\(?1:.+\\)" ;RCS and CVS.
200 ;; Subversion has no such thing??
201 "\\|\\(?:SCCS/s\\.\\|Changes to \\)\\(?1:.+\\):" ;SCCS and Darcs.
2ef3b3fe
SM
202 "\\)\n") ;Include the \n for font-lock reasons.
203 "Regexp matching the text identifying the file.
204The match group number 1 should match the file name itself.")
aa230d94 205
18b2e5b9 206(defvar log-view-message-re
95588f91
SM
207 (concat "^\\(?:revision \\(?1:[.0-9]+\\)\\(?:\t.*\\)?" ; RCS and CVS.
208 "\\|r\\(?1:[0-9]+\\) | .* | .*" ; Subversion.
209 "\\|D \\(?1:[.0-9]+\\) .*" ; SCCS.
62c5f375
SM
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.
95588f91 218 " .*@.*\n\\(?: \\* \\(?1:.*\\)\\)?")
2ef3b3fe
SM
219 "\\)$")
220 "Regexp matching the text identifying a revision.
221The match group number 1 should match the revision number itself.")
5b467bf4 222
18b2e5b9
SM
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))))
5bdc71d0 230
5b467bf4
SM
231(defconst log-view-font-lock-defaults
232 '(log-view-font-lock-keywords t nil nil nil))
233
edb33387 234;;;;
5b467bf4 235;;;; Actual code
edb33387 236;;;;
5b467bf4
SM
237
238;;;###autoload
cdbb990f 239(define-derived-mode log-view-mode fundamental-mode "Log-View"
5b467bf4 240 "Major mode for browsing CVS log output."
99cb8c8b 241 (setq buffer-read-only t)
5b467bf4 242 (set (make-local-variable 'font-lock-defaults) log-view-font-lock-defaults)
a3f5d84d
DN
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)
5b467bf4
SM
247 (set (make-local-variable 'cvs-minor-wrap-function) 'log-view-minor-wrap))
248
249;;;;
250;;;; Navigation
251;;;;
252
cdbb990f
SM
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")
5b467bf4 256
3e87f5fc
SM
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
5b467bf4
SM
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)
22f4e5c1 274 (re-search-forward log-view-file-re nil t)
ccf89694 275 (error "Unable to determine the current file"))
95588f91 276 (let* ((file (match-string 1))
5b467bf4
SM
277 (cvsdir (and (re-search-backward log-view-dir-re nil t)
278 (match-string 1)))
e57a1038
SM
279 (pcldir (and (boundp 'cvs-pcl-cvs-dirchange-re)
280 (re-search-backward cvs-pcl-cvs-dirchange-re nil t)
5b467bf4
SM
281 (match-string 1)))
282 (dir ""))
283 (let ((default-directory ""))
284 (when pcldir (setq dir (expand-file-name pcldir dir)))
99cb8c8b
SS
285 (when cvsdir (setq dir (expand-file-name cvsdir dir))))
286 (expand-file-name file dir))))
5b467bf4 287
99cb8c8b 288(defun log-view-current-tag (&optional where)
cdbb990f 289 (save-excursion
99cb8c8b 290 (when where (goto-char where))
cdbb990f
SM
291 (forward-line 1)
292 (let ((pt (point)))
293 (when (re-search-backward log-view-message-re nil t)
3a44b2ae 294 (let ((rev (match-string-no-properties 1)))
cdbb990f
SM
295 (unless (re-search-forward log-view-file-re pt t)
296 rev))))))
5b467bf4 297
83db937c 298(defun log-view-toggle-mark-entry ()
a0902360
DN
299 "Toggle the marked state for the log entry at point.
300Individual log entries can be marked and unmarked. The marked
301entries are denoted by changing their background color.
302`log-view-get-marked' returns the list of tags for the marked
303log entries."
83db937c
DN
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)
3a44b2ae
DN
312 ;; Look to see if the current entry is marked.
313 (setq found (get-char-property (point) 'log-view-self))
83db937c 314 (if found
3a44b2ae
DN
315 (delete-overlay found)
316 ;; Create an overlay that covers this entry and change
813e8939 317 ;; its color.
83db937c 318 (setq tag (log-view-current-tag (point)))
83db937c 319 (forward-line 1)
3a44b2ae 320 (setq end
83db937c
DN
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)
3a44b2ae
DN
326 ;; This is used to check if the overlay is present.
327 (overlay-put ov 'log-view-self ov)
83db937c
DN
328 (overlay-put ov 'log-view-marked tag))))))))
329
3a44b2ae 330(defun log-view-get-marked ()
a0902360 331 "Return the list of tags for the marked log entries."
3a44b2ae
DN
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
a3f5d84d
DN
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
aa230d94
SM
386(defvar cvs-minor-current-files)
387(defvar cvs-branch-prefix)
388(defvar cvs-secondary-branch-prefix)
389
5b467bf4
SM
390(defun log-view-minor-wrap (buf f)
391 (let ((data (with-current-buffer buf
243afed7
SM
392 (let* ((beg (point))
393 (end (if mark-active (mark) (point)))
cbc98273
JL
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
243afed7
SM
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))))))
5b467bf4
SM
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
ac3f4c6f 417(defun log-view-find-revision (pos)
c0313667
AS
418 "Visit the version at point."
419 (interactive "d")
420 (save-excursion
421 (goto-char pos)
ac3f4c6f 422 (switch-to-buffer (vc-find-revision (log-view-current-file)
c0313667
AS
423 (log-view-current-tag)))))
424
9b64a7f0
ER
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
8df5b06d
GM
443(declare-function vc-modify-change-comment "vc" (files rev oldcomment))
444
9b64a7f0
ER
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
a3f5d84d
DN
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
467ee23f
SM
460;;
461;; diff
462;;
99cb8c8b
SS
463
464(defun log-view-diff (beg end)
e8171d36
JL
465 "Get the diff between two revisions.
466If the mark is not active or the mark is on the revision at point,
467get the diff between the revision at point and its previous revision.
468Otherwise, get the diff between the revisions where the region starts
469and ends."
da4ae7d3
JL
470 (interactive
471 (list (if mark-active (region-beginning) (point))
472 (if mark-active (region-end) (point))))
99cb8c8b
SS
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))))
dce693f8 480 (vc-version-diff (list (log-view-current-file)) to fr)))
99cb8c8b 481
5b467bf4 482(provide 'log-view)
cdbb990f 483
9be92b96 484;; arch-tag: 0d64220b-ce7e-4f62-9c2a-6b04c2f81f4f
5b467bf4 485;;; log-view.el ends here