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