]> code.delx.au - gnu-emacs/blob - lisp/vc/log-view.el
fbcb88b3f729e1e95895ceb6e509566187eeaafb
[gnu-emacs] / lisp / vc / 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, 2010, 2011 Free Software Foundation, Inc.
5
6 ;; Author: Stefan Monnier <monnier@iro.umontreal.ca>
7 ;; Keywords: rcs, sccs, cvs, log, vc, tools
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-diff-internal "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 ;; Needed because log-view-mode-map inherits from widget-keymap. (Bug#5311)
126 (require 'wid-edit)
127
128 (easy-mmode-defmap log-view-mode-map
129 '(("z" . kill-this-buffer)
130 ("q" . quit-window)
131 ("g" . revert-buffer)
132 ("m" . log-view-toggle-mark-entry)
133 ("e" . log-view-modify-change-comment)
134 ("d" . log-view-diff)
135 ("=" . log-view-diff)
136 ("D" . log-view-diff-changeset)
137 ("a" . log-view-annotate-version)
138 ("f" . log-view-find-revision)
139 ("n" . log-view-msg-next)
140 ("p" . log-view-msg-prev)
141 ("\t" . log-view-msg-next)
142 ([backtab] . log-view-msg-prev)
143 ("N" . log-view-file-next)
144 ("P" . log-view-file-prev)
145 ("\M-n" . log-view-file-next)
146 ("\M-p" . log-view-file-prev))
147 "Log-View's keymap."
148 :inherit widget-keymap
149 :group 'log-view)
150
151 (easy-menu-define log-view-mode-menu log-view-mode-map
152 "Log-View Display Menu"
153 `("Log-View"
154 ;; XXX Do we need menu entries for these?
155 ;; ["Quit" quit-window]
156 ;; ["Kill This Buffer" kill-this-buffer]
157 ["Mark Log Entry for Diff" set-mark-command
158 :help ""]
159 ["Diff Revisions" log-view-diff
160 :help "Get the diff between two revisions"]
161 ["Changeset Diff" log-view-diff-changeset
162 :help "Get the changeset diff between two revisions"]
163 ["Visit Version" log-view-find-revision
164 :help "Visit the version at point"]
165 ["Annotate Version" log-view-annotate-version
166 :help "Annotate the version at point"]
167 ["Modify Log Comment" log-view-modify-change-comment
168 :help "Edit the change comment displayed at point"]
169 "-----"
170 ["Next Log Entry" log-view-msg-next
171 :help "Go to the next count'th log message"]
172 ["Previous Log Entry" log-view-msg-prev
173 :help "Go to the previous count'th log message"]
174 ["Next File" log-view-file-next
175 :help "Go to the next count'th file"]
176 ["Previous File" log-view-file-prev
177 :help "Go to the previous count'th file"]))
178
179 (defvar log-view-mode-hook nil
180 "Hook run at the end of `log-view-mode'.")
181
182 (defface log-view-file
183 '((((class color) (background light))
184 (:background "grey70" :weight bold))
185 (t (:weight bold)))
186 "Face for the file header line in `log-view-mode'."
187 :group 'log-view)
188 (define-obsolete-face-alias 'log-view-file-face 'log-view-file "22.1")
189 (defvar log-view-file-face 'log-view-file)
190
191 (defface log-view-message
192 '((((class color) (background light))
193 (:background "grey85"))
194 (t (:weight bold)))
195 "Face for the message header line in `log-view-mode'."
196 :group 'log-view)
197 ;; backward-compatibility alias
198 (define-obsolete-face-alias 'log-view-message-face 'log-view-message "22.1")
199 (defvar log-view-message-face 'log-view-message)
200
201 (defvar log-view-file-re
202 (concat "^\\(?:Working file: \\(?1:.+\\)" ;RCS and CVS.
203 ;; Subversion has no such thing??
204 "\\|\\(?:SCCS/s\\.\\|Changes to \\)\\(?1:.+\\):" ;SCCS and Darcs.
205 "\\)\n") ;Include the \n for font-lock reasons.
206 "Regexp matching the text identifying the file.
207 The match group number 1 should match the file name itself.")
208
209 (defvar log-view-per-file-logs t
210 "Set if to t if the logs are shown one file at a time.")
211
212 (defvar log-view-message-re
213 (concat "^\\(?:revision \\(?1:[.0-9]+\\)\\(?:\t.*\\)?" ; RCS and CVS.
214 "\\|r\\(?1:[0-9]+\\) | .* | .*" ; Subversion.
215 "\\|D \\(?1:[.0-9]+\\) .*" ; SCCS.
216 ;; Darcs doesn't have revision names. VC-darcs uses patch names
217 ;; instead. Darcs patch names are hashcodes, which do not appear
218 ;; in the log output :-(, but darcs accepts any prefix of the log
219 ;; message as a patch name, so we match the first line of the log
220 ;; message.
221 ;; First loosely match the date format.
222 (concat "\\|[^ \n].*[^0-9\n][0-9][0-9]:[0-9][0-9][^0-9\n].*[^ \n]"
223 ;;Email of user and finally Msg, used as revision name.
224 " .*@.*\n\\(?: \\* \\(?1:.*\\)\\)?")
225 "\\)$")
226 "Regexp matching the text identifying a revision.
227 The match group number 1 should match the revision number itself.")
228
229 (defvar log-view-font-lock-keywords
230 ;; We use `eval' so as to use the buffer-local value of log-view-file-re
231 ;; and log-view-message-re, if applicable.
232 '((eval . `(,log-view-file-re
233 (1 (if (boundp 'cvs-filename-face) cvs-filename-face))
234 (0 log-view-file-face append)))
235 (eval . `(,log-view-message-re . log-view-message-face))))
236
237 (defconst log-view-font-lock-defaults
238 '(log-view-font-lock-keywords t nil nil nil))
239
240 (defvar log-view-vc-fileset nil
241 "Set this to the fileset corresponding to the current log.")
242
243 (defvar log-view-vc-backend nil
244 "Set this to the VC backend that created the current log.")
245
246 ;;;;
247 ;;;; Actual code
248 ;;;;
249
250 ;;;###autoload
251 (define-derived-mode log-view-mode special-mode "Log-View"
252 "Major mode for browsing CVS log output."
253 (setq buffer-read-only t)
254 (set (make-local-variable 'font-lock-defaults) log-view-font-lock-defaults)
255 (set (make-local-variable 'beginning-of-defun-function)
256 'log-view-beginning-of-defun)
257 (set (make-local-variable 'end-of-defun-function)
258 'log-view-end-of-defun)
259 (set (make-local-variable 'cvs-minor-wrap-function) 'log-view-minor-wrap)
260 (hack-dir-local-variables-non-file-buffer))
261
262 ;;;;
263 ;;;; Navigation
264 ;;;;
265
266 ;; define log-view-{msg,file}-{next,prev}
267 (easy-mmode-define-navigation log-view-msg log-view-message-re "log message")
268 (easy-mmode-define-navigation log-view-file log-view-file-re "file")
269
270 (defun log-view-goto-rev (rev)
271 (goto-char (point-min))
272 (ignore-errors
273 (while (not (equal rev (log-view-current-tag)))
274 (log-view-msg-next))
275 t))
276
277 ;;;;
278 ;;;; Linkage to PCL-CVS (mostly copied from cvs-status.el)
279 ;;;;
280
281 (defconst log-view-dir-re "^cvs[.ex]* [a-z]+: Logging \\(.+\\)$")
282
283 (defun log-view-current-file ()
284 (save-excursion
285 (forward-line 1)
286 (or (re-search-backward log-view-file-re nil t)
287 (re-search-forward log-view-file-re nil t)
288 (error "Unable to determine the current file"))
289 (let* ((file (match-string 1))
290 (cvsdir (and (re-search-backward log-view-dir-re nil t)
291 (match-string 1)))
292 (pcldir (and (boundp 'cvs-pcl-cvs-dirchange-re)
293 (re-search-backward cvs-pcl-cvs-dirchange-re nil t)
294 (match-string 1)))
295 (dir ""))
296 (let ((default-directory ""))
297 (when pcldir (setq dir (expand-file-name pcldir dir)))
298 (when cvsdir (setq dir (expand-file-name cvsdir dir))))
299 (expand-file-name file dir))))
300
301 (defun log-view-current-tag (&optional where)
302 (save-excursion
303 (when where (goto-char where))
304 (forward-line 1)
305 (let ((pt (point)))
306 (when (re-search-backward log-view-message-re nil t)
307 (let ((rev (match-string-no-properties 1)))
308 (unless (re-search-forward log-view-file-re pt t)
309 rev))))))
310
311 (defun log-view-toggle-mark-entry ()
312 "Toggle the marked state for the log entry at point.
313 Individual log entries can be marked and unmarked. The marked
314 entries are denoted by changing their background color.
315 `log-view-get-marked' returns the list of tags for the marked
316 log entries."
317 (interactive)
318 (save-excursion
319 (forward-line 1)
320 (let ((pt (point)))
321 (when (re-search-backward log-view-message-re nil t)
322 (let ((beg (match-beginning 0))
323 end ov ovlist found tag)
324 (unless (re-search-forward log-view-file-re pt t)
325 ;; Look to see if the current entry is marked.
326 (setq found (get-char-property (point) 'log-view-self))
327 (if found
328 (delete-overlay found)
329 ;; Create an overlay that covers this entry and change
330 ;; its color.
331 (setq tag (log-view-current-tag (point)))
332 (forward-line 1)
333 (setq end
334 (if (re-search-forward log-view-message-re nil t)
335 (match-beginning 0)
336 (point-max)))
337 (setq ov (make-overlay beg end))
338 (overlay-put ov 'face 'log-view-file)
339 ;; This is used to check if the overlay is present.
340 (overlay-put ov 'log-view-self ov)
341 (overlay-put ov 'log-view-marked tag))))))))
342
343 (defun log-view-get-marked ()
344 "Return the list of tags for the marked log entries."
345 (save-excursion
346 (let ((pos (point-min))
347 marked-list ov)
348 (while (setq pos (next-single-property-change pos 'face))
349 (when (setq ov (get-char-property pos 'log-view-self))
350 (push (overlay-get ov 'log-view-marked) marked-list)
351 (setq pos (overlay-end ov))))
352 marked-list)))
353
354 (defun log-view-beginning-of-defun ()
355 ;; This assumes that a log entry starts with a line matching
356 ;; `log-view-message-re'. Modes that derive from `log-view-mode'
357 ;; for which this assumption is not valid will have to provide
358 ;; another implementation of this function. `log-view-msg-prev'
359 ;; does a similar job to this function, we can't use it here
360 ;; directly because it prints messages that are not appropriate in
361 ;; this context and it does not move to the beginning of the buffer
362 ;; when the point is before the first log entry.
363
364 ;; `log-view-beginning-of-defun' and `log-view-end-of-defun' have
365 ;; been checked to work with logs produced by RCS, CVS, git,
366 ;; mercurial and subversion.
367
368 (re-search-backward log-view-message-re nil 'move))
369
370 (defun log-view-end-of-defun ()
371 ;; The idea in this function is to search for the beginning of the
372 ;; next log entry using `log-view-message-re' and then go back one
373 ;; line when finding it. Modes that derive from `log-view-mode' for
374 ;; which this assumption is not valid will have to provide another
375 ;; implementation of this function.
376
377 ;; Look back and if there is no entry there it means we are before
378 ;; the first log entry, so go forward until finding one.
379 (unless (save-excursion (re-search-backward log-view-message-re nil t))
380 (re-search-forward log-view-message-re nil t))
381
382 ;; In case we are at the end of log entry going forward a line will
383 ;; make us find the next entry when searching. If we are inside of
384 ;; an entry going forward a line will still keep the point inside
385 ;; the same entry.
386 (forward-line 1)
387
388 ;; In case we are at the beginning of an entry, move past it.
389 (when (looking-at log-view-message-re)
390 (goto-char (match-end 0))
391 (forward-line 1))
392
393 ;; Search for the start of the next log entry. Go to the end of the
394 ;; buffer if we could not find a next entry.
395 (when (re-search-forward log-view-message-re nil 'move)
396 (goto-char (match-beginning 0))
397 (forward-line -1)))
398
399 (defvar cvs-minor-current-files)
400 (defvar cvs-branch-prefix)
401 (defvar cvs-secondary-branch-prefix)
402
403 (defun log-view-minor-wrap (buf f)
404 (let ((data (with-current-buffer buf
405 (let* ((beg (point))
406 (end (if mark-active (mark) (point)))
407 (fr (log-view-current-tag beg))
408 (to (log-view-current-tag end)))
409 (when (string-equal fr to)
410 (save-excursion
411 (goto-char end)
412 (log-view-msg-next)
413 (setq to (log-view-current-tag))))
414 (cons
415 ;; The first revision has to be the one at point, for
416 ;; operations that only take one revision
417 ;; (e.g. cvs-mode-edit).
418 (cons (log-view-current-file) fr)
419 (cons (log-view-current-file) to))))))
420 (let ((cvs-branch-prefix (cdar data))
421 (cvs-secondary-branch-prefix (and (cdar data) (cddr data)))
422 (cvs-minor-current-files
423 (cons (caar data)
424 (when (and (cadr data) (not (equal (caar data) (cadr data))))
425 (list (cadr data)))))
426 ;; FIXME: I need to force because the fileinfos are UNKNOWN
427 (cvs-force-command "/F"))
428 (funcall f))))
429
430 (defun log-view-find-revision (pos)
431 "Visit the version at point."
432 (interactive "d")
433 (unless log-view-per-file-logs
434 (when (> (length log-view-vc-fileset) 1)
435 (error "Multiple files shown in this buffer, cannot use this command here")))
436 (save-excursion
437 (goto-char pos)
438 (switch-to-buffer (vc-find-revision (if log-view-per-file-logs
439 (log-view-current-file)
440 (car log-view-vc-fileset))
441 (log-view-current-tag)))))
442
443
444 (defun log-view-extract-comment ()
445 "Parse comment from around the current point in the log."
446 (save-excursion
447 (let (st en (backend (vc-backend (log-view-current-file))))
448 (log-view-end-of-defun)
449 (cond ((eq backend 'SVN)
450 (forward-line -1)))
451 (setq en (point))
452 (log-view-beginning-of-defun)
453 (cond ((memq backend '(SCCS RCS CVS MCVS SVN))
454 (forward-line 2))
455 ((eq backend 'Hg)
456 (forward-line 4)
457 (re-search-forward "summary: *" nil t)))
458 (setq st (point))
459 (buffer-substring st en))))
460
461 (declare-function vc-modify-change-comment "vc" (files rev oldcomment))
462
463 (defun log-view-modify-change-comment ()
464 "Edit the change comment displayed at point."
465 (interactive)
466 (vc-modify-change-comment (list (if log-view-per-file-logs
467 (log-view-current-file)
468 (car log-view-vc-fileset)))
469 (log-view-current-tag)
470 (log-view-extract-comment)))
471
472 (defun log-view-annotate-version (pos)
473 "Annotate the version at point."
474 (interactive "d")
475 (unless log-view-per-file-logs
476 (when (> (length log-view-vc-fileset) 1)
477 (error "Multiple files shown in this buffer, cannot use this command here")))
478 (save-excursion
479 (goto-char pos)
480 (vc-annotate (if log-view-per-file-logs
481 (log-view-current-file)
482 (car log-view-vc-fileset))
483 (log-view-current-tag))))
484
485 ;;
486 ;; diff
487 ;;
488
489 (defun log-view-diff (beg end)
490 "Get the diff between two revisions.
491 If the mark is not active or the mark is on the revision at point,
492 get the diff between the revision at point and its previous revision.
493 Otherwise, get the diff between the revisions where the region starts
494 and ends.
495 Contrary to `log-view-diff-changeset', it will only show the part of the
496 changeset that affected the currently considered file(s)."
497 (interactive
498 (list (if mark-active (region-beginning) (point))
499 (if mark-active (region-end) (point))))
500 (let ((fr (log-view-current-tag beg))
501 (to (log-view-current-tag end)))
502 (when (string-equal fr to)
503 (save-excursion
504 (goto-char end)
505 (log-view-msg-next)
506 (setq to (log-view-current-tag))))
507 (vc-diff-internal
508 t (list log-view-vc-backend
509 (if log-view-per-file-logs
510 (list (log-view-current-file))
511 log-view-vc-fileset))
512 to fr)))
513
514 (declare-function vc-diff-internal "vc"
515 (async vc-fileset rev1 rev2 &optional verbose))
516
517 (defun log-view-diff-changeset (beg end)
518 "Get the diff between two revisions.
519 If the mark is not active or the mark is on the revision at point,
520 get the diff between the revision at point and its previous revision.
521 Otherwise, get the diff between the revisions where the region starts
522 and ends.
523 Contrary to `log-view-diff', it will show the whole changeset including
524 the changes that affected other files than the currently considered file(s)."
525 (interactive
526 (list (if mark-active (region-beginning) (point))
527 (if mark-active (region-end) (point))))
528 (when (eq (vc-call-backend log-view-vc-backend 'revision-granularity) 'file)
529 (error "The %s backend does not support changeset diffs" log-view-vc-backend))
530 (let ((fr (log-view-current-tag beg))
531 (to (log-view-current-tag end)))
532 (when (string-equal fr to)
533 ;; TO and FR are the same, look at the previous revision.
534 (setq to (vc-call-backend log-view-vc-backend 'previous-revision nil fr)))
535 (vc-diff-internal
536 t
537 ;; We want to see the diff for all the files in the changeset, so
538 ;; pass NIL for the file list. The value passed here should
539 ;; follow what `vc-deduce-fileset' returns.
540 (list log-view-vc-backend nil)
541 to fr)))
542
543 (provide 'log-view)
544
545 ;; arch-tag: 0d64220b-ce7e-4f62-9c2a-6b04c2f81f4f
546 ;;; log-view.el ends here