Rob Riepel <riepel at networking.stanford.edu>
[emacs.git] / lisp / log-view.el
blob1ee96916ffb0379a3501618fc6e90ad53414ba2f
1 ;;; log-view.el --- Major mode for browsing RCS/CVS/SCCS log output
3 ;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005,
4 ;; 2006, 2007 Free Software Foundation, Inc.
6 ;; Author: Stefan Monnier <monnier@iro.umontreal.ca>
7 ;; Keywords: rcs sccs cvs log version-control
9 ;; This file is part of GNU Emacs.
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.
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.
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.
26 ;;; Commentary:
28 ;; Major mode to browse revision log histories.
29 ;; Currently supports the format output by:
30 ;; RCS, SCCS, CVS, Subversion, and DaRCS.
32 ;; Examples of log output:
34 ;;;; RCS/CVS:
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.
49 ;;;; SCCS:
51 ;;;; Subversion:
53 ;;;; Darcs:
55 ;; Changes to darcsum.el:
56 ;;
57 ;; Mon Nov 28 15:19:38 GMT 2005 Dave Love <fx@gnu.org>
58 ;; * Abstract process startup into darcsum-start-process. Use TERM=dumb.
59 ;; TERM=dumb avoids escape characters, at least, for any old darcs that
60 ;; doesn't understand DARCS_DONT_COLOR & al.
61 ;;
62 ;; Thu Nov 24 15:20:45 GMT 2005 Dave Love <fx@gnu.org>
63 ;; * darcsum-mode-related changes.
64 ;; Don't call font-lock-mode (unnecessary) or use-local-map (redundant).
65 ;; Use mode-class 'special. Add :group.
66 ;; Add trailing-whitespace option to mode hook and fix
67 ;; darcsum-display-changeset not to use trailing whitespace.
69 ;;; Todo:
71 ;; - add ability to modify a log-entry (via cvs-mode-admin ;-)
72 ;; - remove references to cvs-*
73 ;; - make it easier to add support for new backends without changing the code.
75 ;;; Code:
77 (eval-when-compile (require 'cl))
78 (require 'pcvs-util)
79 (autoload 'vc-find-revision "vc")
80 (autoload 'vc-version-diff "vc")
82 (defvar cvs-minor-wrap-function)
84 (defgroup log-view nil
85 "Major mode for browsing log output of RCS/CVS/SCCS."
86 :group 'pcl-cvs
87 :prefix "log-view-")
89 (easy-mmode-defmap log-view-mode-map
90 '(("q" . quit-window)
91 ("z" . kill-this-buffer)
92 ("m" . log-view-toggle-mark-entry)
93 ;; ("e" . cvs-mode-edit-log)
94 ("d" . log-view-diff)
95 ("a" . log-view-annotate-version)
96 ("f" . log-view-find-revision)
97 ("n" . log-view-msg-next)
98 ("p" . log-view-msg-prev)
99 ("\t" . log-view-msg-next)
100 ([backtab] . log-view-msg-prev)
101 ("N" . log-view-file-next)
102 ("P" . log-view-file-prev)
103 ("\M-n" . log-view-file-next)
104 ("\M-p" . log-view-file-prev))
105 "Log-View's keymap."
106 :group 'log-view
107 ;; Here I really need either buffer-local keymap-inheritance
108 ;; or a minor-mode-map with lower precedence than the local map.
109 :inherit (if (boundp 'cvs-mode-map) cvs-mode-map))
111 (easy-menu-define log-view-mode-menu log-view-mode-map
112 "Log-View Display Menu"
113 `("Log-View"
114 ;; XXX Do we need menu entries for these?
115 ;; ["Quit" quit-window]
116 ;; ["Kill This Buffer" kill-this-buffer]
117 ["Mark Log Entry for Diff" set-mark-command]
118 ["Diff Revisions" log-view-diff]
119 ["Visit Version" log-view-find-revision]
120 ["Annotate Version" log-view-annotate-version]
121 ["Next Log Entry" log-view-msg-next]
122 ["Previous Log Entry" log-view-msg-prev]
123 ["Next File" log-view-file-next]
124 ["Previous File" log-view-file-prev]))
126 (defvar log-view-mode-hook nil
127 "Hook run at the end of `log-view-mode'.")
129 (defface log-view-file
130 '((((class color) (background light))
131 (:background "grey70" :weight bold))
132 (t (:weight bold)))
133 "Face for the file header line in `log-view-mode'."
134 :group 'log-view)
135 ;; backward-compatibility alias
136 (put 'log-view-file-face 'face-alias 'log-view-file)
137 (defvar log-view-file-face 'log-view-file)
139 (defface log-view-message
140 '((((class color) (background light))
141 (:background "grey85"))
142 (t (:weight bold)))
143 "Face for the message header line in `log-view-mode'."
144 :group 'log-view)
145 ;; backward-compatibility alias
146 (put 'log-view-message-face 'face-alias 'log-view-message)
147 (defvar log-view-message-face 'log-view-message)
149 (defvar log-view-file-re
150 (concat "^\\(?:Working file: \\(?1:.+\\)" ;RCS and CVS.
151 ;; Subversion has no such thing??
152 "\\|\\(?:SCCS/s\\.\\|Changes to \\)\\(?1:.+\\):" ;SCCS and Darcs.
153 "\\)\n") ;Include the \n for font-lock reasons.
154 "Regexp matching the text identifying the file.
155 The match group number 1 should match the file name itself.")
157 (defvar log-view-message-re
158 (concat "^\\(?:revision \\(?1:[.0-9]+\\)\\(?:\t.*\\)?" ; RCS and CVS.
159 "\\|r\\(?1:[0-9]+\\) | .* | .*" ; Subversion.
160 "\\|D \\(?1:[.0-9]+\\) .*" ; SCCS.
161 ;; Darcs doesn't have revision names. VC-darcs uses patch names
162 ;; instead. Darcs patch names are hashcodes, which do not appear
163 ;; in the log output :-(, but darcs accepts any prefix of the log
164 ;; message as a patch name, so we match the first line of the log
165 ;; message.
166 ;; First loosely match the date format.
167 (concat "\\|[^ \n].*[^0-9\n][0-9][0-9]:[0-9][0-9][^0-9\n].*[^ \n]"
168 ;;Email of user and finally Msg, used as revision name.
169 " .*@.*\n\\(?: \\* \\(?1:.*\\)\\)?")
170 "\\)$")
171 "Regexp matching the text identifying a revision.
172 The match group number 1 should match the revision number itself.")
174 (defvar log-view-font-lock-keywords
175 ;; We use `eval' so as to use the buffer-local value of log-view-file-re
176 ;; and log-view-message-re, if applicable.
177 '((eval . `(,log-view-file-re
178 (1 (if (boundp 'cvs-filename-face) cvs-filename-face))
179 (0 log-view-file-face append)))
180 (eval . `(,log-view-message-re . log-view-message-face))))
182 (defconst log-view-font-lock-defaults
183 '(log-view-font-lock-keywords t nil nil nil))
185 ;;;;
186 ;;;; Actual code
187 ;;;;
189 ;;;###autoload
190 (define-derived-mode log-view-mode fundamental-mode "Log-View"
191 "Major mode for browsing CVS log output."
192 (setq buffer-read-only t)
193 (set (make-local-variable 'font-lock-defaults) log-view-font-lock-defaults)
194 (set (make-local-variable 'beginning-of-defun-function)
195 'log-view-beginning-of-defun)
196 (set (make-local-variable 'end-of-defun-function)
197 'log-view-end-of-defun)
198 (set (make-local-variable 'cvs-minor-wrap-function) 'log-view-minor-wrap))
200 ;;;;
201 ;;;; Navigation
202 ;;;;
204 ;; define log-view-{msg,file}-{next,prev}
205 (easy-mmode-define-navigation log-view-msg log-view-message-re "log message")
206 (easy-mmode-define-navigation log-view-file log-view-file-re "file")
208 (defun log-view-goto-rev (rev)
209 (goto-char (point-min))
210 (ignore-errors
211 (while (not (equal rev (log-view-current-tag)))
212 (log-view-msg-next))
215 ;;;;
216 ;;;; Linkage to PCL-CVS (mostly copied from cvs-status.el)
217 ;;;;
219 (defconst log-view-dir-re "^cvs[.ex]* [a-z]+: Logging \\(.+\\)$")
221 (defun log-view-current-file ()
222 (save-excursion
223 (forward-line 1)
224 (or (re-search-backward log-view-file-re nil t)
225 (re-search-forward log-view-file-re nil t)
226 (error "Unable to determine the current file"))
227 (let* ((file (match-string 1))
228 (cvsdir (and (re-search-backward log-view-dir-re nil t)
229 (match-string 1)))
230 (pcldir (and (boundp 'cvs-pcl-cvs-dirchange-re)
231 (re-search-backward cvs-pcl-cvs-dirchange-re nil t)
232 (match-string 1)))
233 (dir ""))
234 (let ((default-directory ""))
235 (when pcldir (setq dir (expand-file-name pcldir dir)))
236 (when cvsdir (setq dir (expand-file-name cvsdir dir))))
237 (expand-file-name file dir))))
239 (defun log-view-current-tag (&optional where)
240 (save-excursion
241 (when where (goto-char where))
242 (forward-line 1)
243 (let ((pt (point)))
244 (when (re-search-backward log-view-message-re nil t)
245 (let ((rev (match-string-no-properties 1)))
246 (unless (re-search-forward log-view-file-re pt t)
247 rev))))))
249 (defun log-view-toggle-mark-entry ()
250 "Toggle the marked state for the log entry at point.
251 Individual log entries can be marked and unmarked. The marked
252 entries are denoted by changing their background color.
253 `log-view-get-marked' returns the list of tags for the marked
254 log entries."
255 (interactive)
256 (save-excursion
257 (forward-line 1)
258 (let ((pt (point)))
259 (when (re-search-backward log-view-message-re nil t)
260 (let ((beg (match-beginning 0))
261 end ov ovlist found tag)
262 (unless (re-search-forward log-view-file-re pt t)
263 ;; Look to see if the current entry is marked.
264 (setq found (get-char-property (point) 'log-view-self))
265 (if found
266 (delete-overlay found)
267 ;; Create an overlay that covers this entry and change
268 ;; it's color.
269 (setq tag (log-view-current-tag (point)))
270 (forward-line 1)
271 (setq end
272 (if (re-search-forward log-view-message-re nil t)
273 (match-beginning 0)
274 (point-max)))
275 (setq ov (make-overlay beg end))
276 (overlay-put ov 'face 'log-view-file)
277 ;; This is used to check if the overlay is present.
278 (overlay-put ov 'log-view-self ov)
279 (overlay-put ov 'log-view-marked tag))))))))
281 (defun log-view-get-marked ()
282 "Return the list of tags for the marked log entries."
283 (save-excursion
284 (let ((pos (point-min))
285 marked-list ov)
286 (while (setq pos (next-single-property-change pos 'face))
287 (when (setq ov (get-char-property pos 'log-view-self))
288 (push (overlay-get ov 'log-view-marked) marked-list)
289 (setq pos (overlay-end ov))))
290 marked-list)))
292 (defun log-view-beginning-of-defun ()
293 ;; This assumes that a log entry starts with a line matching
294 ;; `log-view-message-re'. Modes that derive from `log-view-mode'
295 ;; for which this assumption is not valid will have to provide
296 ;; another implementation of this function. `log-view-msg-prev'
297 ;; does a similar job to this function, we can't use it here
298 ;; directly because it prints messages that are not appropriate in
299 ;; this context and it does not move to the beginning of the buffer
300 ;; when the point is before the first log entry.
302 ;; `log-view-beginning-of-defun' and `log-view-end-of-defun' have
303 ;; been checked to work with logs produced by RCS, CVS, git,
304 ;; mercurial and subversion.
306 (re-search-backward log-view-message-re nil 'move))
308 (defun log-view-end-of-defun ()
309 ;; The idea in this function is to search for the beginning of the
310 ;; next log entry using `log-view-message-re' and then go back one
311 ;; line when finding it. Modes that derive from `log-view-mode' for
312 ;; which this assumption is not valid will have to provide another
313 ;; implementation of this function.
315 ;; Look back and if there is no entry there it means we are before
316 ;; the first log entry, so go forward until finding one.
317 (unless (save-excursion (re-search-backward log-view-message-re nil t))
318 (re-search-forward log-view-message-re nil t))
320 ;; In case we are at the end of log entry going forward a line will
321 ;; make us find the next entry when searching. If we are inside of
322 ;; an entry going forward a line will still keep the point inside
323 ;; the same entry.
324 (forward-line 1)
326 ;; In case we are at the beginning of an entry, move past it.
327 (when (looking-at log-view-message-re)
328 (goto-char (match-end 0))
329 (forward-line 1))
331 ;; Search for the start of the next log entry. Go to the end of the
332 ;; buffer if we could not find a next entry.
333 (when (re-search-forward log-view-message-re nil 'move)
334 (goto-char (match-beginning 0))
335 (forward-line -1)))
337 (defvar cvs-minor-current-files)
338 (defvar cvs-branch-prefix)
339 (defvar cvs-secondary-branch-prefix)
341 (defun log-view-minor-wrap (buf f)
342 (let ((data (with-current-buffer buf
343 (let* ((beg (point))
344 (end (if mark-active (mark) (point)))
345 (fr (log-view-current-tag beg))
346 (to (log-view-current-tag end)))
347 (when (string-equal fr to)
348 (save-excursion
349 (goto-char end)
350 (log-view-msg-next)
351 (setq to (log-view-current-tag))))
352 (cons
353 ;; The first revision has to be the one at point, for
354 ;; operations that only take one revision
355 ;; (e.g. cvs-mode-edit).
356 (cons (log-view-current-file) fr)
357 (cons (log-view-current-file) to))))))
358 (let ((cvs-branch-prefix (cdar data))
359 (cvs-secondary-branch-prefix (and (cdar data) (cddr data)))
360 (cvs-minor-current-files
361 (cons (caar data)
362 (when (and (cadr data) (not (equal (caar data) (cadr data))))
363 (list (cadr data)))))
364 ;; FIXME: I need to force because the fileinfos are UNKNOWN
365 (cvs-force-command "/F"))
366 (funcall f))))
368 (defun log-view-find-revision (pos)
369 "Visit the version at point."
370 (interactive "d")
371 (save-excursion
372 (goto-char pos)
373 (switch-to-buffer (vc-find-revision (log-view-current-file)
374 (log-view-current-tag)))))
376 (defun log-view-annotate-version (pos)
377 "Annotate the version at point."
378 (interactive "d")
379 (save-excursion
380 (goto-char pos)
381 (switch-to-buffer (vc-annotate (log-view-current-file)
382 (log-view-current-tag)))))
385 ;; diff
388 (defun log-view-diff (beg end)
389 "Get the diff between two revisions.
390 If the mark is not active or the mark is on the revision at point,
391 get the diff between the revision at point and its previous revision.
392 Otherwise, get the diff between the revisions where the region starts
393 and ends."
394 (interactive
395 (list (if mark-active (region-beginning) (point))
396 (if mark-active (region-end) (point))))
397 (let ((fr (log-view-current-tag beg))
398 (to (log-view-current-tag end)))
399 (when (string-equal fr to)
400 (save-excursion
401 (goto-char end)
402 (log-view-msg-next)
403 (setq to (log-view-current-tag))))
404 (vc-version-diff (list (log-view-current-file)) to fr)))
406 (provide 'log-view)
408 ;; arch-tag: 0d64220b-ce7e-4f62-9c2a-6b04c2f81f4f
409 ;;; log-view.el ends here