Add new TODO entry.
[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]
155 ["Mark Log Entry for Diff" set-mark-command]
156 ["Diff Revisions" log-view-diff]
ac3f4c6f 157 ["Visit Version" log-view-find-revision]
a3f5d84d 158 ["Annotate Version" log-view-annotate-version]
d621bc0a
DN
159 ["Next Log Entry" log-view-msg-next]
160 ["Previous Log Entry" log-view-msg-prev]
161 ["Next File" log-view-file-next]
162 ["Previous File" log-view-file-prev]))
163
5b467bf4
SM
164(defvar log-view-mode-hook nil
165 "Hook run at the end of `log-view-mode'.")
166
d842de85 167(defface log-view-file
5b467bf4 168 '((((class color) (background light))
1fd714a4
RS
169 (:background "grey70" :weight bold))
170 (t (:weight bold)))
5b467bf4
SM
171 "Face for the file header line in `log-view-mode'."
172 :group 'log-view)
d842de85
MB
173;; backward-compatibility alias
174(put 'log-view-file-face 'face-alias 'log-view-file)
175(defvar log-view-file-face 'log-view-file)
5b467bf4 176
d842de85 177(defface log-view-message
5b467bf4
SM
178 '((((class color) (background light))
179 (:background "grey85"))
1fd714a4 180 (t (:weight bold)))
5b467bf4
SM
181 "Face for the message header line in `log-view-mode'."
182 :group 'log-view)
d842de85
MB
183;; backward-compatibility alias
184(put 'log-view-message-face 'face-alias 'log-view-message)
185(defvar log-view-message-face 'log-view-message)
5b467bf4 186
18b2e5b9 187(defvar log-view-file-re
95588f91
SM
188 (concat "^\\(?:Working file: \\(?1:.+\\)" ;RCS and CVS.
189 ;; Subversion has no such thing??
190 "\\|\\(?:SCCS/s\\.\\|Changes to \\)\\(?1:.+\\):" ;SCCS and Darcs.
2ef3b3fe
SM
191 "\\)\n") ;Include the \n for font-lock reasons.
192 "Regexp matching the text identifying the file.
193The match group number 1 should match the file name itself.")
aa230d94 194
18b2e5b9 195(defvar log-view-message-re
95588f91
SM
196 (concat "^\\(?:revision \\(?1:[.0-9]+\\)\\(?:\t.*\\)?" ; RCS and CVS.
197 "\\|r\\(?1:[0-9]+\\) | .* | .*" ; Subversion.
198 "\\|D \\(?1:[.0-9]+\\) .*" ; SCCS.
62c5f375
SM
199 ;; Darcs doesn't have revision names. VC-darcs uses patch names
200 ;; instead. Darcs patch names are hashcodes, which do not appear
201 ;; in the log output :-(, but darcs accepts any prefix of the log
202 ;; message as a patch name, so we match the first line of the log
203 ;; message.
204 ;; First loosely match the date format.
205 (concat "\\|[^ \n].*[^0-9\n][0-9][0-9]:[0-9][0-9][^0-9\n].*[^ \n]"
206 ;;Email of user and finally Msg, used as revision name.
95588f91 207 " .*@.*\n\\(?: \\* \\(?1:.*\\)\\)?")
2ef3b3fe
SM
208 "\\)$")
209 "Regexp matching the text identifying a revision.
210The match group number 1 should match the revision number itself.")
5b467bf4 211
18b2e5b9
SM
212(defvar log-view-font-lock-keywords
213 ;; We use `eval' so as to use the buffer-local value of log-view-file-re
214 ;; and log-view-message-re, if applicable.
215 '((eval . `(,log-view-file-re
216 (1 (if (boundp 'cvs-filename-face) cvs-filename-face))
217 (0 log-view-file-face append)))
218 (eval . `(,log-view-message-re . log-view-message-face))))
5bdc71d0 219
5b467bf4
SM
220(defconst log-view-font-lock-defaults
221 '(log-view-font-lock-keywords t nil nil nil))
222
edb33387 223;;;;
5b467bf4 224;;;; Actual code
edb33387 225;;;;
5b467bf4
SM
226
227;;;###autoload
cdbb990f 228(define-derived-mode log-view-mode fundamental-mode "Log-View"
5b467bf4 229 "Major mode for browsing CVS log output."
99cb8c8b 230 (setq buffer-read-only t)
5b467bf4 231 (set (make-local-variable 'font-lock-defaults) log-view-font-lock-defaults)
a3f5d84d
DN
232 (set (make-local-variable 'beginning-of-defun-function)
233 'log-view-beginning-of-defun)
234 (set (make-local-variable 'end-of-defun-function)
235 'log-view-end-of-defun)
5b467bf4
SM
236 (set (make-local-variable 'cvs-minor-wrap-function) 'log-view-minor-wrap))
237
238;;;;
239;;;; Navigation
240;;;;
241
cdbb990f
SM
242;; define log-view-{msg,file}-{next,prev}
243(easy-mmode-define-navigation log-view-msg log-view-message-re "log message")
244(easy-mmode-define-navigation log-view-file log-view-file-re "file")
5b467bf4 245
3e87f5fc
SM
246(defun log-view-goto-rev (rev)
247 (goto-char (point-min))
248 (ignore-errors
249 (while (not (equal rev (log-view-current-tag)))
250 (log-view-msg-next))
251 t))
252
5b467bf4
SM
253;;;;
254;;;; Linkage to PCL-CVS (mostly copied from cvs-status.el)
255;;;;
256
257(defconst log-view-dir-re "^cvs[.ex]* [a-z]+: Logging \\(.+\\)$")
258
259(defun log-view-current-file ()
260 (save-excursion
261 (forward-line 1)
262 (or (re-search-backward log-view-file-re nil t)
22f4e5c1 263 (re-search-forward log-view-file-re nil t)
ccf89694 264 (error "Unable to determine the current file"))
95588f91 265 (let* ((file (match-string 1))
5b467bf4
SM
266 (cvsdir (and (re-search-backward log-view-dir-re nil t)
267 (match-string 1)))
e57a1038
SM
268 (pcldir (and (boundp 'cvs-pcl-cvs-dirchange-re)
269 (re-search-backward cvs-pcl-cvs-dirchange-re nil t)
5b467bf4
SM
270 (match-string 1)))
271 (dir ""))
272 (let ((default-directory ""))
273 (when pcldir (setq dir (expand-file-name pcldir dir)))
99cb8c8b
SS
274 (when cvsdir (setq dir (expand-file-name cvsdir dir))))
275 (expand-file-name file dir))))
5b467bf4 276
99cb8c8b 277(defun log-view-current-tag (&optional where)
cdbb990f 278 (save-excursion
99cb8c8b 279 (when where (goto-char where))
cdbb990f
SM
280 (forward-line 1)
281 (let ((pt (point)))
282 (when (re-search-backward log-view-message-re nil t)
3a44b2ae 283 (let ((rev (match-string-no-properties 1)))
cdbb990f
SM
284 (unless (re-search-forward log-view-file-re pt t)
285 rev))))))
5b467bf4 286
83db937c 287(defun log-view-toggle-mark-entry ()
a0902360
DN
288 "Toggle the marked state for the log entry at point.
289Individual log entries can be marked and unmarked. The marked
290entries are denoted by changing their background color.
291`log-view-get-marked' returns the list of tags for the marked
292log entries."
83db937c
DN
293 (interactive)
294 (save-excursion
295 (forward-line 1)
296 (let ((pt (point)))
297 (when (re-search-backward log-view-message-re nil t)
298 (let ((beg (match-beginning 0))
299 end ov ovlist found tag)
300 (unless (re-search-forward log-view-file-re pt t)
3a44b2ae
DN
301 ;; Look to see if the current entry is marked.
302 (setq found (get-char-property (point) 'log-view-self))
83db937c 303 (if found
3a44b2ae
DN
304 (delete-overlay found)
305 ;; Create an overlay that covers this entry and change
813e8939 306 ;; its color.
83db937c 307 (setq tag (log-view-current-tag (point)))
83db937c 308 (forward-line 1)
3a44b2ae 309 (setq end
83db937c
DN
310 (if (re-search-forward log-view-message-re nil t)
311 (match-beginning 0)
312 (point-max)))
313 (setq ov (make-overlay beg end))
314 (overlay-put ov 'face 'log-view-file)
3a44b2ae
DN
315 ;; This is used to check if the overlay is present.
316 (overlay-put ov 'log-view-self ov)
83db937c
DN
317 (overlay-put ov 'log-view-marked tag))))))))
318
3a44b2ae 319(defun log-view-get-marked ()
a0902360 320 "Return the list of tags for the marked log entries."
3a44b2ae
DN
321 (save-excursion
322 (let ((pos (point-min))
323 marked-list ov)
324 (while (setq pos (next-single-property-change pos 'face))
325 (when (setq ov (get-char-property pos 'log-view-self))
326 (push (overlay-get ov 'log-view-marked) marked-list)
327 (setq pos (overlay-end ov))))
328 marked-list)))
329
a3f5d84d
DN
330(defun log-view-beginning-of-defun ()
331 ;; This assumes that a log entry starts with a line matching
332 ;; `log-view-message-re'. Modes that derive from `log-view-mode'
333 ;; for which this assumption is not valid will have to provide
334 ;; another implementation of this function. `log-view-msg-prev'
335 ;; does a similar job to this function, we can't use it here
336 ;; directly because it prints messages that are not appropriate in
337 ;; this context and it does not move to the beginning of the buffer
338 ;; when the point is before the first log entry.
339
340 ;; `log-view-beginning-of-defun' and `log-view-end-of-defun' have
341 ;; been checked to work with logs produced by RCS, CVS, git,
342 ;; mercurial and subversion.
343
344 (re-search-backward log-view-message-re nil 'move))
345
346(defun log-view-end-of-defun ()
347 ;; The idea in this function is to search for the beginning of the
348 ;; next log entry using `log-view-message-re' and then go back one
349 ;; line when finding it. Modes that derive from `log-view-mode' for
350 ;; which this assumption is not valid will have to provide another
351 ;; implementation of this function.
352
353 ;; Look back and if there is no entry there it means we are before
354 ;; the first log entry, so go forward until finding one.
355 (unless (save-excursion (re-search-backward log-view-message-re nil t))
356 (re-search-forward log-view-message-re nil t))
357
358 ;; In case we are at the end of log entry going forward a line will
359 ;; make us find the next entry when searching. If we are inside of
360 ;; an entry going forward a line will still keep the point inside
361 ;; the same entry.
362 (forward-line 1)
363
364 ;; In case we are at the beginning of an entry, move past it.
365 (when (looking-at log-view-message-re)
366 (goto-char (match-end 0))
367 (forward-line 1))
368
369 ;; Search for the start of the next log entry. Go to the end of the
370 ;; buffer if we could not find a next entry.
371 (when (re-search-forward log-view-message-re nil 'move)
372 (goto-char (match-beginning 0))
373 (forward-line -1)))
374
aa230d94
SM
375(defvar cvs-minor-current-files)
376(defvar cvs-branch-prefix)
377(defvar cvs-secondary-branch-prefix)
378
5b467bf4
SM
379(defun log-view-minor-wrap (buf f)
380 (let ((data (with-current-buffer buf
243afed7
SM
381 (let* ((beg (point))
382 (end (if mark-active (mark) (point)))
cbc98273
JL
383 (fr (log-view-current-tag beg))
384 (to (log-view-current-tag end)))
385 (when (string-equal fr to)
386 (save-excursion
387 (goto-char end)
388 (log-view-msg-next)
389 (setq to (log-view-current-tag))))
390 (cons
243afed7
SM
391 ;; The first revision has to be the one at point, for
392 ;; operations that only take one revision
393 ;; (e.g. cvs-mode-edit).
394 (cons (log-view-current-file) fr)
395 (cons (log-view-current-file) to))))))
5b467bf4
SM
396 (let ((cvs-branch-prefix (cdar data))
397 (cvs-secondary-branch-prefix (and (cdar data) (cddr data)))
398 (cvs-minor-current-files
399 (cons (caar data)
400 (when (and (cadr data) (not (equal (caar data) (cadr data))))
401 (list (cadr data)))))
402 ;; FIXME: I need to force because the fileinfos are UNKNOWN
403 (cvs-force-command "/F"))
404 (funcall f))))
405
ac3f4c6f 406(defun log-view-find-revision (pos)
c0313667
AS
407 "Visit the version at point."
408 (interactive "d")
409 (save-excursion
410 (goto-char pos)
ac3f4c6f 411 (switch-to-buffer (vc-find-revision (log-view-current-file)
c0313667
AS
412 (log-view-current-tag)))))
413
9b64a7f0
ER
414
415(defun log-view-extract-comment ()
416 "Parse comment from around the current point in the log."
417 (save-excursion
418 (let (st en (backend (vc-backend (log-view-current-file))))
419 (log-view-end-of-defun)
420 (cond ((eq backend 'SVN)
421 (forward-line -1)))
422 (setq en (point))
423 (log-view-beginning-of-defun)
424 (cond ((memq backend '(SCCS RCS CVS MCVS SVN))
425 (forward-line 2))
426 ((eq backend 'Hg)
427 (forward-line 4)
428 (re-search-forward "summary: *" nil t)))
429 (setq st (point))
430 (buffer-substring st en))))
431
8df5b06d
GM
432(declare-function vc-modify-change-comment "vc" (files rev oldcomment))
433
9b64a7f0
ER
434(defun log-view-modify-change-comment ()
435 "Edit the change comment displayed at point."
436 (interactive)
437 (vc-modify-change-comment (list (log-view-current-file))
438 (log-view-current-tag)
439 (log-view-extract-comment)))
440
a3f5d84d
DN
441(defun log-view-annotate-version (pos)
442 "Annotate the version at point."
443 (interactive "d")
444 (save-excursion
445 (goto-char pos)
446 (switch-to-buffer (vc-annotate (log-view-current-file)
447 (log-view-current-tag)))))
448
467ee23f
SM
449;;
450;; diff
451;;
99cb8c8b
SS
452
453(defun log-view-diff (beg end)
e8171d36
JL
454 "Get the diff between two revisions.
455If the mark is not active or the mark is on the revision at point,
456get the diff between the revision at point and its previous revision.
457Otherwise, get the diff between the revisions where the region starts
458and ends."
da4ae7d3
JL
459 (interactive
460 (list (if mark-active (region-beginning) (point))
461 (if mark-active (region-end) (point))))
99cb8c8b
SS
462 (let ((fr (log-view-current-tag beg))
463 (to (log-view-current-tag end)))
464 (when (string-equal fr to)
465 (save-excursion
466 (goto-char end)
467 (log-view-msg-next)
468 (setq to (log-view-current-tag))))
dce693f8 469 (vc-version-diff (list (log-view-current-file)) to fr)))
99cb8c8b 470
5b467bf4 471(provide 'log-view)
cdbb990f 472
9be92b96 473;; arch-tag: 0d64220b-ce7e-4f62-9c2a-6b04c2f81f4f
5b467bf4 474;;; log-view.el ends here