(x_draw_relief_rect): Remove unused variable `dpy'.
[emacs.git] / lisp / log-view.el
blob3f0e91e325e02808be17cc662a4efe88ab5958e8
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, 2008 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-version "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" . set-mark-command)
93 ;; ("e" . cvs-mode-edit-log)
94 ("d" . log-view-diff)
95 ("a" . log-view-annotate-version)
96 ("f" . log-view-find-version)
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-version]
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 (defconst log-view-file-re
150 (concat "^\\(?:Working file: \\(.+\\)" ;RCS and CVS.
151 "\\|\\(?:SCCS/s\\.\\|Changes to \\)\\(.+\\):" ;SCCS and Darcs.
152 "\\)\n")) ;Include the \n for font-lock reasons.
154 (defconst log-view-message-re
155 (concat "^\\(?:revision \\([.0-9]+\\)\\(?:\t.*\\)?" ; RCS and CVS.
156 "\\|r\\([0-9]+\\) | .* | .*" ; Subversion.
157 "\\|D \\([.0-9]+\\) .*" ; SCCS.
158 ;; Darcs doesn't have revision names. VC-darcs uses patch names
159 ;; instead. Darcs patch names are hashcodes, which do not appear
160 ;; in the log output :-(, but darcs accepts any prefix of the log
161 ;; message as a patch name, so we match the first line of the log
162 ;; message.
163 ;; First loosely match the date format.
164 (concat "\\|[^ \n].*[^0-9\n][0-9][0-9]:[0-9][0-9][^0-9\n].*[^ \n]"
165 ;;Email of user and finally Msg, used as revision name.
166 " .*@.*\n\\(?: \\* \\(.*\\)\\)?")
167 "\\)$")
168 "Regexp matching the text identifying a revision.")
170 (defvar log-view-font-lock-keywords
171 ;; We use `eval' so as to use the buffer-local value of log-view-file-re
172 ;; and log-view-message-re, if applicable.
173 '((eval . `(,log-view-file-re
174 (1 (if (boundp 'cvs-filename-face) cvs-filename-face) nil t)
175 (2 (if (boundp 'cvs-filename-face) cvs-filename-face) nil t)
176 (0 log-view-file-face append)))
177 (eval . `(,log-view-message-re . log-view-message-face))))
179 (defconst log-view-font-lock-defaults
180 '(log-view-font-lock-keywords t nil nil nil))
182 ;;;;
183 ;;;; Actual code
184 ;;;;
186 ;;;###autoload
187 (define-derived-mode log-view-mode fundamental-mode "Log-View"
188 "Major mode for browsing CVS log output."
189 (setq buffer-read-only t)
190 (set (make-local-variable 'font-lock-defaults) log-view-font-lock-defaults)
191 (set (make-local-variable 'beginning-of-defun-function)
192 'log-view-beginning-of-defun)
193 (set (make-local-variable 'end-of-defun-function)
194 'log-view-end-of-defun)
195 (set (make-local-variable 'cvs-minor-wrap-function) 'log-view-minor-wrap))
197 ;;;;
198 ;;;; Navigation
199 ;;;;
201 ;; define log-view-{msg,file}-{next,prev}
202 (easy-mmode-define-navigation log-view-msg log-view-message-re "log message")
203 (easy-mmode-define-navigation log-view-file log-view-file-re "file")
205 (defun log-view-goto-rev (rev)
206 (goto-char (point-min))
207 (ignore-errors
208 (while (not (equal rev (log-view-current-tag)))
209 (log-view-msg-next))
212 ;;;;
213 ;;;; Linkage to PCL-CVS (mostly copied from cvs-status.el)
214 ;;;;
216 (defconst log-view-dir-re "^cvs[.ex]* [a-z]+: Logging \\(.+\\)$")
218 (defun log-view-current-file ()
219 (save-excursion
220 (forward-line 1)
221 (or (re-search-backward log-view-file-re nil t)
222 (re-search-forward log-view-file-re))
223 (let* ((file (or (match-string 1) (match-string 2)))
224 (cvsdir (and (re-search-backward log-view-dir-re nil t)
225 (match-string 1)))
226 (pcldir (and (boundp 'cvs-pcl-cvs-dirchange-re)
227 (re-search-backward cvs-pcl-cvs-dirchange-re nil t)
228 (match-string 1)))
229 (dir ""))
230 (let ((default-directory ""))
231 (when pcldir (setq dir (expand-file-name pcldir dir)))
232 (when cvsdir (setq dir (expand-file-name cvsdir dir))))
233 (expand-file-name file dir))))
235 (defun log-view-current-tag (&optional where)
236 (save-excursion
237 (when where (goto-char where))
238 (forward-line 1)
239 (let ((pt (point)))
240 (when (re-search-backward log-view-message-re nil t)
241 (let (rev)
242 ;; Find the subgroup that matched.
243 (dotimes (i (/ (length (match-data 'integers)) 2))
244 (setq rev (or rev (match-string (1+ i)))))
245 (unless (re-search-forward log-view-file-re pt t)
246 rev))))))
248 (defun log-view-beginning-of-defun ()
249 ;; This assumes that a log entry starts with a line matching
250 ;; `log-view-message-re'. Modes that derive from `log-view-mode'
251 ;; for which this assumption is not valid will have to provide
252 ;; another implementation of this function. `log-view-msg-prev'
253 ;; does a similar job to this function, we can't use it here
254 ;; directly because it prints messages that are not appropriate in
255 ;; this context and it does not move to the beginning of the buffer
256 ;; when the point is before the first log entry.
258 ;; `log-view-beginning-of-defun' and `log-view-end-of-defun' have
259 ;; been checked to work with logs produced by RCS, CVS, git,
260 ;; mercurial and subversion.
262 (re-search-backward log-view-message-re nil 'move))
264 (defun log-view-end-of-defun ()
265 ;; The idea in this function is to search for the beginning of the
266 ;; next log entry using `log-view-message-re' and then go back one
267 ;; line when finding it. Modes that derive from `log-view-mode' for
268 ;; which this assumption is not valid will have to provide another
269 ;; implementation of this function.
271 ;; Look back and if there is no entry there it means we are before
272 ;; the first log entry, so go forward until finding one.
273 (unless (save-excursion (re-search-backward log-view-message-re nil t))
274 (re-search-forward log-view-message-re nil t))
276 ;; In case we are at the end of log entry going forward a line will
277 ;; make us find the next entry when searching. If we are inside of
278 ;; an entry going forward a line will still keep the point inside
279 ;; the same entry.
280 (forward-line 1)
282 ;; In case we are at the beginning of an entry, move past it.
283 (when (looking-at log-view-message-re)
284 (goto-char (match-end 0))
285 (forward-line 1))
287 ;; Search for the start of the next log entry. Go to the end of the
288 ;; buffer if we could not find a next entry.
289 (when (re-search-forward log-view-message-re nil 'move)
290 (goto-char (match-beginning 0))
291 (forward-line -1)))
293 (defvar cvs-minor-current-files)
294 (defvar cvs-branch-prefix)
295 (defvar cvs-secondary-branch-prefix)
297 (defun log-view-minor-wrap (buf f)
298 (let ((data (with-current-buffer buf
299 (let* ((beg (point))
300 (end (if mark-active (mark) (point)))
301 (fr (log-view-current-tag beg))
302 (to (log-view-current-tag end)))
303 (when (string-equal fr to)
304 (save-excursion
305 (goto-char end)
306 (log-view-msg-next)
307 (setq to (log-view-current-tag))))
308 (cons
309 ;; The first revision has to be the one at point, for
310 ;; operations that only take one revision
311 ;; (e.g. cvs-mode-edit).
312 (cons (log-view-current-file) fr)
313 (cons (log-view-current-file) to))))))
314 (let ((cvs-branch-prefix (cdar data))
315 (cvs-secondary-branch-prefix (and (cdar data) (cddr data)))
316 (cvs-minor-current-files
317 (cons (caar data)
318 (when (and (cadr data) (not (equal (caar data) (cadr data))))
319 (list (cadr data)))))
320 ;; FIXME: I need to force because the fileinfos are UNKNOWN
321 (cvs-force-command "/F"))
322 (funcall f))))
324 (defun log-view-find-version (pos)
325 "Visit the version at point."
326 (interactive "d")
327 (save-excursion
328 (goto-char pos)
329 (switch-to-buffer (vc-find-version (log-view-current-file)
330 (log-view-current-tag)))))
332 (defun log-view-annotate-version (pos)
333 "Annotate the version at point."
334 (interactive "d")
335 (save-excursion
336 (goto-char pos)
337 (switch-to-buffer (vc-annotate (log-view-current-file)
338 (log-view-current-tag)))))
341 ;; diff
344 (defun log-view-diff (beg end)
345 "Get the diff between two revisions.
346 If the mark is not active or the mark is on the revision at point,
347 get the diff between the revision at point and its previous revision.
348 Otherwise, get the diff between the revisions where the region starts
349 and ends."
350 (interactive
351 (list (if mark-active (region-beginning) (point))
352 (if mark-active (region-end) (point))))
353 (let ((fr (log-view-current-tag beg))
354 (to (log-view-current-tag end)))
355 (when (string-equal fr to)
356 (save-excursion
357 (goto-char end)
358 (log-view-msg-next)
359 (setq to (log-view-current-tag))))
360 (vc-version-diff (log-view-current-file) to fr)))
362 (provide 'log-view)
364 ;; arch-tag: 0d64220b-ce7e-4f62-9c2a-6b04c2f81f4f
365 ;;; log-view.el ends here