Misc fixes, and use lexical-binding in more files.
[emacs.git] / lisp / vc / log-view.el
blobd9a06c8a401c0e5480a0bd5fe6481aa013efde80
1 ;;; log-view.el --- Major mode for browsing RCS/CVS/SCCS log output -*- lexical-binding: t -*-
3 ;; Copyright (C) 1999-2011 Free Software Foundation, Inc.
5 ;; Author: Stefan Monnier <monnier@iro.umontreal.ca>
6 ;; Keywords: rcs, sccs, cvs, log, vc, tools
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;; Commentary:
25 ;; Major mode to browse revision log histories.
26 ;; Currently supports the format output by:
27 ;; RCS, SCCS, CVS, Subversion, and DaRCS.
29 ;; Examples of log output:
31 ;;;; RCS/CVS:
33 ;; ----------------------------
34 ;; revision 1.35 locked by: turlutut
35 ;; date: 2005-03-22 18:48:38 +0000; author: monnier; state: Exp; lines: +6 -8
36 ;; (gnus-display-time-event-handler):
37 ;; Check display-time-timer at runtime rather than only at load time
38 ;; in case display-time-mode is turned off in the mean time.
39 ;; ----------------------------
40 ;; revision 1.34
41 ;; date: 2005-02-09 15:50:38 +0000; author: kfstorm; state: Exp; lines: +7 -7
42 ;; branches: 1.34.2;
43 ;; Change release version from 21.4 to 22.1 throughout.
44 ;; Change development version from 21.3.50 to 22.0.50.
46 ;;;; SCCS:
48 ;;;; Subversion:
50 ;; ------------------------------------------------------------------------
51 ;; r4622 | ckuethe | 2007-12-23 18:18:01 -0500 (Sun, 23 Dec 2007) | 2 lines
53 ;; uBlox AEK-4T in binary mode. Added to unstable because it breaks gpsfake
55 ;; ------------------------------------------------------------------------
56 ;; r4621 | ckuethe | 2007-12-23 16:48:11 -0500 (Sun, 23 Dec 2007) | 3 lines
58 ;; Add a note about requiring usbfs to use the garmin gps18 (usb)
59 ;; Mention firmware testing the AC12 with firmware BQ00 and BQ04
61 ;; ------------------------------------------------------------------------
62 ;; r4620 | ckuethe | 2007-12-23 15:52:34 -0500 (Sun, 23 Dec 2007) | 1 line
64 ;; add link to latest hardware reference
65 ;; ------------------------------------------------------------------------
66 ;; r4619 | ckuethe | 2007-12-23 14:37:31 -0500 (Sun, 23 Dec 2007) | 1 line
68 ;; there is now a regression test for AC12 without raw data output
70 ;;;; Darcs:
72 ;; Changes to darcsum.el:
74 ;; Mon Nov 28 15:19:38 GMT 2005 Dave Love <fx@gnu.org>
75 ;; * Abstract process startup into darcsum-start-process. Use TERM=dumb.
76 ;; TERM=dumb avoids escape characters, at least, for any old darcs that
77 ;; doesn't understand DARCS_DONT_COLOR & al.
79 ;; Thu Nov 24 15:20:45 GMT 2005 Dave Love <fx@gnu.org>
80 ;; * darcsum-mode-related changes.
81 ;; Don't call font-lock-mode (unnecessary) or use-local-map (redundant).
82 ;; Use mode-class 'special. Add :group.
83 ;; Add trailing-whitespace option to mode hook and fix
84 ;; darcsum-display-changeset not to use trailing whitespace.
86 ;;;; Mercurial
88 ;; changeset: 11:8ff1a4166444
89 ;; tag: tip
90 ;; user: Eric S. Raymond <esr@thyrsus.com>
91 ;; date: Wed Dec 26 12:18:58 2007 -0500
92 ;; summary: Explain keywords. Add markup fixes.
94 ;; changeset: 10:20abc7ab09c3
95 ;; user: Eric S. Raymond <esr@thyrsus.com>
96 ;; date: Wed Dec 26 11:37:28 2007 -0500
97 ;; summary: Typo fixes.
99 ;; changeset: 9:ada9f4da88aa
100 ;; user: Eric S. Raymond <esr@thyrsus.com>
101 ;; date: Wed Dec 26 11:23:00 2007 -0500
102 ;; summary: Add RCS example session.
104 ;;; Todo:
106 ;; - add ability to modify a log-entry (via cvs-mode-admin ;-)
107 ;; - remove references to cvs-*
108 ;; - make it easier to add support for new backends without changing the code.
110 ;;; Code:
112 (eval-when-compile (require 'cl))
113 (require 'pcvs-util)
114 (autoload 'vc-find-revision "vc")
115 (autoload 'vc-diff-internal "vc")
117 (defvar cvs-minor-wrap-function)
118 (defvar cvs-force-command)
120 (defgroup log-view nil
121 "Major mode for browsing log output of RCS/CVS/SCCS."
122 :group 'pcl-cvs
123 :prefix "log-view-")
125 ;; Needed because log-view-mode-map inherits from widget-keymap. (Bug#5311)
126 (require 'wid-edit)
128 (easy-mmode-defmap log-view-mode-map
130 ;; FIXME: (copy-keymap special-mode-map) instead
131 ("z" . kill-this-buffer)
132 ("q" . quit-window)
133 ("g" . revert-buffer)
134 ("\C-m" . log-view-toggle-entry-display)
136 ("m" . log-view-toggle-mark-entry)
137 ("e" . log-view-modify-change-comment)
138 ("d" . log-view-diff)
139 ("=" . log-view-diff)
140 ("D" . log-view-diff-changeset)
141 ("a" . log-view-annotate-version)
142 ("f" . log-view-find-revision)
143 ("n" . log-view-msg-next)
144 ("p" . log-view-msg-prev)
145 ("\t" . log-view-msg-next)
146 ([backtab] . log-view-msg-prev)
147 ("N" . log-view-file-next)
148 ("P" . log-view-file-prev)
149 ("\M-n" . log-view-file-next)
150 ("\M-p" . log-view-file-prev))
151 "Log-View's keymap."
152 :group 'log-view)
154 (easy-menu-define log-view-mode-menu log-view-mode-map
155 "Log-View Display Menu"
156 `("Log-View"
157 ;; XXX Do we need menu entries for these?
158 ;; ["Quit" quit-window]
159 ;; ["Kill This Buffer" kill-this-buffer]
160 ["Mark Log Entry for Diff" set-mark-command
161 :help ""]
162 ["Diff Revisions" log-view-diff
163 :help "Get the diff between two revisions"]
164 ["Changeset Diff" log-view-diff-changeset
165 :help "Get the changeset diff between two revisions"]
166 ["Visit Version" log-view-find-revision
167 :help "Visit the version at point"]
168 ["Annotate Version" log-view-annotate-version
169 :help "Annotate the version at point"]
170 ["Modify Log Comment" log-view-modify-change-comment
171 :help "Edit the change comment displayed at point"]
172 ["Toggle Details at Point" log-view-toggle-entry-display
173 :active log-view-expanded-log-entry-function]
174 "-----"
175 ["Next Log Entry" log-view-msg-next
176 :help "Go to the next count'th log message"]
177 ["Previous Log Entry" log-view-msg-prev
178 :help "Go to the previous count'th log message"]
179 ["Next File" log-view-file-next
180 :help "Go to the next count'th file"]
181 ["Previous File" log-view-file-prev
182 :help "Go to the previous count'th file"]))
184 (defvar log-view-mode-hook nil
185 "Hook run at the end of `log-view-mode'.")
187 (defvar log-view-expanded-log-entry-function nil
188 "Function returning the detailed description of a Log View entry.
189 It is called by the command `log-view-toggle-entry-display' with
190 one arg, the revision tag (a string), and should return a string.
191 If it is nil, `log-view-toggle-entry-display' does nothing.")
193 (defface log-view-file
194 '((((class color) (background light))
195 (:background "grey70" :weight bold))
196 (t (:weight bold)))
197 "Face for the file header line in `log-view-mode'."
198 :group 'log-view)
199 (define-obsolete-face-alias 'log-view-file-face 'log-view-file "22.1")
200 (defvar log-view-file-face 'log-view-file)
202 (defface log-view-message
203 '((((class color) (background light))
204 (:background "grey85"))
205 (t (:weight bold)))
206 "Face for the message header line in `log-view-mode'."
207 :group 'log-view)
208 ;; backward-compatibility alias
209 (define-obsolete-face-alias 'log-view-message-face 'log-view-message "22.1")
210 (defvar log-view-message-face 'log-view-message)
212 (defvar log-view-file-re
213 (concat "^\\(?:Working file: \\(?1:.+\\)" ;RCS and CVS.
214 ;; Subversion has no such thing??
215 "\\|\\(?:SCCS/s\\.\\|Changes to \\)\\(?1:.+\\):" ;SCCS and Darcs.
216 "\\)\n") ;Include the \n for font-lock reasons.
217 "Regexp matching the text identifying the file.
218 The match group number 1 should match the file name itself.")
220 (defvar log-view-per-file-logs t
221 "Set if to t if the logs are shown one file at a time.")
223 (defvar log-view-message-re
224 (concat "^\\(?:revision \\(?1:[.0-9]+\\)\\(?:\t.*\\)?" ; RCS and CVS.
225 "\\|r\\(?1:[0-9]+\\) | .* | .*" ; Subversion.
226 "\\|D \\(?1:[.0-9]+\\) .*" ; SCCS.
227 ;; Darcs doesn't have revision names. VC-darcs uses patch names
228 ;; instead. Darcs patch names are hashcodes, which do not appear
229 ;; in the log output :-(, but darcs accepts any prefix of the log
230 ;; message as a patch name, so we match the first line of the log
231 ;; message.
232 ;; First loosely match the date format.
233 (concat "\\|[^ \n].*[^0-9\n][0-9][0-9]:[0-9][0-9][^0-9\n].*[^ \n]"
234 ;;Email of user and finally Msg, used as revision name.
235 " .*@.*\n\\(?: \\* \\(?1:.*\\)\\)?")
236 "\\)$")
237 "Regexp matching the text identifying a revision.
238 The match group number 1 should match the revision number itself.")
240 (defvar log-view-font-lock-keywords
241 ;; We use `eval' so as to use the buffer-local value of log-view-file-re
242 ;; and log-view-message-re, if applicable.
243 '((eval . `(,log-view-file-re
244 (1 (if (boundp 'cvs-filename-face) cvs-filename-face))
245 (0 log-view-file-face append)))
246 (eval . `(,log-view-message-re . log-view-message-face))))
248 (defconst log-view-font-lock-defaults
249 '(log-view-font-lock-keywords t nil nil nil))
251 (defvar log-view-vc-fileset nil
252 "Set this to the fileset corresponding to the current log.")
254 (defvar log-view-vc-backend nil
255 "Set this to the VC backend that created the current log.")
257 ;;;;
258 ;;;; Actual code
259 ;;;;
261 ;;;###autoload
262 (define-derived-mode log-view-mode special-mode "Log-View"
263 "Major mode for browsing CVS log output."
264 (setq buffer-read-only t)
265 (set (make-local-variable 'font-lock-defaults) log-view-font-lock-defaults)
266 (set (make-local-variable 'beginning-of-defun-function)
267 'log-view-beginning-of-defun)
268 (set (make-local-variable 'end-of-defun-function)
269 'log-view-end-of-defun)
270 (set (make-local-variable 'cvs-minor-wrap-function) 'log-view-minor-wrap)
271 (hack-dir-local-variables-non-file-buffer))
273 ;;;;
274 ;;;; Navigation
275 ;;;;
277 ;; define log-view-{msg,file}-{next,prev}
278 (easy-mmode-define-navigation log-view-msg log-view-message-re "log message")
279 (easy-mmode-define-navigation log-view-file log-view-file-re "file")
281 (defun log-view-goto-rev (rev)
282 (goto-char (point-min))
283 (ignore-errors
284 (while (not (equal rev (log-view-current-tag)))
285 (log-view-msg-next))
288 ;;;;
289 ;;;; Linkage to PCL-CVS (mostly copied from cvs-status.el)
290 ;;;;
292 (defconst log-view-dir-re "^cvs[.ex]* [a-z]+: Logging \\(.+\\)$")
294 (defun log-view-current-file ()
295 (save-excursion
296 (forward-line 1)
297 (or (re-search-backward log-view-file-re nil t)
298 (re-search-forward log-view-file-re nil t)
299 (error "Unable to determine the current file"))
300 (let* ((file (match-string 1))
301 (cvsdir (and (re-search-backward log-view-dir-re nil t)
302 (match-string 1)))
303 (pcldir (and (boundp 'cvs-pcl-cvs-dirchange-re)
304 (re-search-backward cvs-pcl-cvs-dirchange-re nil t)
305 (match-string 1)))
306 (dir ""))
307 (let ((default-directory ""))
308 (when pcldir (setq dir (expand-file-name pcldir dir)))
309 (when cvsdir (setq dir (expand-file-name cvsdir dir))))
310 (expand-file-name file dir))))
312 (defun log-view-current-entry (&optional pos move)
313 "Return the position and revision tag of the Log View entry at POS.
314 This is a list (BEG TAG), where BEG is a buffer position and TAG
315 is a string. If POS is nil or omitted, it defaults to point.
316 If there is no entry at POS, return nil.
318 If optional arg MOVE is non-nil, move point to BEG if found.
319 Otherwise, don't move point."
320 (let ((looping t)
321 result)
322 (save-excursion
323 (when pos (goto-char pos))
324 (forward-line 1)
325 (while looping
326 (setq pos (re-search-backward log-view-message-re nil 'move)
327 looping (and pos (log-view-inside-comment-p (point)))))
328 (when pos
329 (setq result
330 (list pos (match-string-no-properties 1)))))
331 (and move result (goto-char pos))
332 result))
334 (defun log-view-inside-comment-p (pos)
335 "Return non-nil if POS lies inside an expanded log entry."
336 (eq (get-text-property pos 'log-view-comment) t))
338 (defun log-view-current-tag (&optional pos)
339 "Return the revision tag (a string) of the Log View entry at POS.
340 if POS is omitted or nil, it defaults to point."
341 (cadr (log-view-current-entry pos)))
343 (defun log-view-toggle-mark-entry ()
344 "Toggle the marked state for the log entry at point.
345 Individual log entries can be marked and unmarked. The marked
346 entries are denoted by changing their background color.
347 `log-view-get-marked' returns the list of tags for the marked
348 log entries."
349 (interactive)
350 (save-excursion
351 (let* ((entry (log-view-current-entry nil t))
352 (beg (car entry))
353 found)
354 (when entry
355 ;; Look to see if the current entry is marked.
356 (setq found (get-char-property beg 'log-view-self))
357 (if found
358 (delete-overlay found)
359 ;; Create an overlay covering this entry and change its color.
360 (let* ((end (if (get-text-property beg 'log-view-entry-expanded)
361 (next-single-property-change beg 'log-view-comment)
362 (log-view-end-of-defun)
363 (point)))
364 (ov (make-overlay beg end)))
365 (overlay-put ov 'face 'log-view-file)
366 ;; This is used to check if the overlay is present.
367 (overlay-put ov 'log-view-self ov)
368 (overlay-put ov 'log-view-marked (nth 1 entry))))))))
370 (defun log-view-get-marked ()
371 "Return the list of tags for the marked log entries."
372 (save-excursion
373 (let ((pos (point-min))
374 marked-list ov)
375 (while (setq pos (next-single-property-change pos 'face))
376 (when (setq ov (get-char-property pos 'log-view-self))
377 (push (overlay-get ov 'log-view-marked) marked-list)
378 (setq pos (overlay-end ov))))
379 marked-list)))
381 (defun log-view-toggle-entry-display ()
382 (interactive)
383 ;; Don't do anything unless `log-view-expanded-log-entry-function'
384 ;; is defined in this mode.
385 (when (functionp log-view-expanded-log-entry-function)
386 (let* ((opoint (point))
387 (entry (log-view-current-entry nil t))
388 (beg (car entry))
389 (buffer-read-only nil))
390 (when entry
391 (if (get-text-property beg 'log-view-entry-expanded)
392 ;; If the entry is expanded, collapse it.
393 (let ((pos (next-single-property-change beg 'log-view-comment)))
394 (unless (and pos (log-view-inside-comment-p pos))
395 (error "Broken markup in `log-view-toggle-entry-display'"))
396 (delete-region pos
397 (next-single-property-change pos 'log-view-comment))
398 (put-text-property beg (1+ beg) 'log-view-entry-expanded nil)
399 (if (< opoint pos)
400 (goto-char opoint)))
401 ;; Otherwise, expand the entry.
402 (let ((long-entry (funcall log-view-expanded-log-entry-function
403 (nth 1 entry))))
404 (when long-entry
405 (put-text-property beg (1+ beg) 'log-view-entry-expanded t)
406 (log-view-end-of-defun)
407 (setq beg (point))
408 (insert long-entry "\n")
409 (add-text-properties
410 beg (point)
411 '(font-lock-face font-lock-comment-face log-view-comment t))
412 (goto-char opoint))))))))
414 (defun log-view-beginning-of-defun (&optional arg)
415 "Move backward to the beginning of a Log View entry.
416 With ARG, do it that many times. Negative ARG means move forward
417 to the beginning of the ARGth following entry.
419 This is Log View mode's default `beginning-of-defun-function'.
420 It assumes that a log entry starts with a line matching
421 `log-view-message-re'."
422 (if (or (null arg) (zerop arg))
423 (setq arg 1))
424 (if (< arg 0)
425 (dotimes (n (- arg))
426 (log-view-end-of-defun))
427 (catch 'beginning-of-buffer
428 (dotimes (n arg)
429 (or (log-view-current-entry nil t)
430 (throw 'beginning-of-buffer nil)))
431 (point))))
433 (defun log-view-end-of-defun ()
434 "Move forward to the next Log View entry."
435 (let ((looping t))
436 (if (looking-at log-view-message-re)
437 (goto-char (match-end 0)))
438 (while looping
439 (cond
440 ((re-search-forward log-view-message-re nil 'move)
441 (unless (log-view-inside-comment-p (point))
442 (setq looping nil)
443 (goto-char (match-beginning 0))))
444 ;; Don't advance past the end buttons inserted by
445 ;; `vc-print-log-setup-buttons'.
446 ((looking-back "Show 2X entries Show unlimited entries")
447 (setq looping nil)
448 (forward-line -1))))))
450 (defvar cvs-minor-current-files)
451 (defvar cvs-branch-prefix)
452 (defvar cvs-secondary-branch-prefix)
454 (defun log-view-minor-wrap (buf f)
455 (let ((data (with-current-buffer buf
456 (let* ((beg (point))
457 (end (if mark-active (mark) (point)))
458 (fr (log-view-current-tag beg))
459 (to (log-view-current-tag end)))
460 (when (string-equal fr to)
461 (save-excursion
462 (goto-char end)
463 (log-view-msg-next)
464 (setq to (log-view-current-tag))))
465 (cons
466 ;; The first revision has to be the one at point, for
467 ;; operations that only take one revision
468 ;; (e.g. cvs-mode-edit).
469 (cons (log-view-current-file) fr)
470 (cons (log-view-current-file) to))))))
471 (let ((cvs-branch-prefix (cdar data))
472 (cvs-secondary-branch-prefix (and (cdar data) (cddr data)))
473 (cvs-minor-current-files
474 (cons (caar data)
475 (when (and (cadr data) (not (equal (caar data) (cadr data))))
476 (list (cadr data)))))
477 ;; FIXME: I need to force because the fileinfos are UNKNOWN
478 (cvs-force-command "/F"))
479 (funcall f))))
481 (defun log-view-find-revision (pos)
482 "Visit the version at point."
483 (interactive "d")
484 (unless log-view-per-file-logs
485 (when (> (length log-view-vc-fileset) 1)
486 (error "Multiple files shown in this buffer, cannot use this command here")))
487 (save-excursion
488 (goto-char pos)
489 (switch-to-buffer (vc-find-revision (if log-view-per-file-logs
490 (log-view-current-file)
491 (car log-view-vc-fileset))
492 (log-view-current-tag)))))
495 (defun log-view-extract-comment ()
496 "Parse comment from around the current point in the log."
497 (save-excursion
498 (let (st en (backend (vc-backend (log-view-current-file))))
499 (log-view-end-of-defun)
500 (cond ((eq backend 'SVN)
501 (forward-line -1)))
502 (setq en (point))
503 (log-view-beginning-of-defun)
504 (cond ((memq backend '(SCCS RCS CVS MCVS SVN))
505 (forward-line 2))
506 ((eq backend 'Hg)
507 (forward-line 4)
508 (re-search-forward "summary: *" nil t)))
509 (setq st (point))
510 (buffer-substring st en))))
512 (declare-function vc-modify-change-comment "vc" (files rev oldcomment))
514 (defun log-view-modify-change-comment ()
515 "Edit the change comment displayed at point."
516 (interactive)
517 (vc-modify-change-comment (list (if log-view-per-file-logs
518 (log-view-current-file)
519 (car log-view-vc-fileset)))
520 (log-view-current-tag)
521 (log-view-extract-comment)))
523 (defun log-view-annotate-version (pos)
524 "Annotate the version at point."
525 (interactive "d")
526 (unless log-view-per-file-logs
527 (when (> (length log-view-vc-fileset) 1)
528 (error "Multiple files shown in this buffer, cannot use this command here")))
529 (save-excursion
530 (goto-char pos)
531 (vc-annotate (if log-view-per-file-logs
532 (log-view-current-file)
533 (car log-view-vc-fileset))
534 (log-view-current-tag))))
537 ;; diff
540 (defun log-view-diff (beg end)
541 "Get the diff between two revisions.
542 If the mark is not active or the mark is on the revision at point,
543 get the diff between the revision at point and its previous revision.
544 Otherwise, get the diff between the revisions where the region starts
545 and ends.
546 Contrary to `log-view-diff-changeset', it will only show the part of the
547 changeset that affected the currently considered file(s)."
548 (interactive
549 (list (if mark-active (region-beginning) (point))
550 (if mark-active (region-end) (point))))
551 (let ((fr (log-view-current-tag beg))
552 (to (log-view-current-tag end)))
553 (when (string-equal fr to)
554 (save-excursion
555 (goto-char end)
556 (log-view-msg-next)
557 (setq to (log-view-current-tag))))
558 (vc-diff-internal
559 t (list log-view-vc-backend
560 (if log-view-per-file-logs
561 (list (log-view-current-file))
562 log-view-vc-fileset))
563 to fr)))
565 (declare-function vc-diff-internal "vc"
566 (async vc-fileset rev1 rev2 &optional verbose))
568 (defun log-view-diff-changeset (beg end)
569 "Get the diff between two revisions.
570 If the mark is not active or the mark is on the revision at point,
571 get the diff between the revision at point and its previous revision.
572 Otherwise, get the diff between the revisions where the region starts
573 and ends.
574 Contrary to `log-view-diff', it will show the whole changeset including
575 the changes that affected other files than the currently considered file(s)."
576 (interactive
577 (list (if mark-active (region-beginning) (point))
578 (if mark-active (region-end) (point))))
579 (when (eq (vc-call-backend log-view-vc-backend 'revision-granularity) 'file)
580 (error "The %s backend does not support changeset diffs" log-view-vc-backend))
581 (let ((fr (log-view-current-tag beg))
582 (to (log-view-current-tag end)))
583 (when (string-equal fr to)
584 ;; TO and FR are the same, look at the previous revision.
585 (setq to (vc-call-backend log-view-vc-backend 'previous-revision nil fr)))
586 (vc-diff-internal
588 ;; We want to see the diff for all the files in the changeset, so
589 ;; pass NIL for the file list. The value passed here should
590 ;; follow what `vc-deduce-fileset' returns.
591 (list log-view-vc-backend nil)
592 to fr)))
594 (provide 'log-view)
596 ;;; log-view.el ends here