1 ;;; log-view.el --- Major mode for browsing revision log histories -*- lexical-binding: t -*-
3 ;; Copyright (C) 1999-2018 Free Software Foundation, Inc.
5 ;; Author: Stefan Monnier <monnier@iro.umontreal.ca>
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 <https://www.gnu.org/licenses/>.
25 ;; Major mode to browse revision log histories.
26 ;; Currently supports the format output by:
27 ;; SCCS, RCS, CVS, Subversion, DaRCS, and Mercurial.
29 ;; Examples of log output:
35 ;; ----------------------------
36 ;; revision 1.35 locked by: turlutut
37 ;; date: 2005-03-22 18:48:38 +0000; author: monnier; state: Exp; lines: +6 -8
38 ;; (gnus-display-time-event-handler):
39 ;; Check display-time-timer at runtime rather than only at load time
40 ;; in case display-time-mode is turned off in the mean time.
41 ;; ----------------------------
43 ;; date: 2005-02-09 15:50:38 +0000; author: kfstorm; state: Exp; lines: +7 -7
45 ;; Change release version from 21.4 to 22.1 throughout.
46 ;; Change development version from 21.3.50 to 22.0.50.
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
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.
88 ;; changeset: 11:8ff1a4166444
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.
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.
113 (autoload 'vc-find-revision
"vc")
114 (autoload 'vc-diff-internal
"vc")
116 (defvar cvs-minor-wrap-function
)
117 (defvar cvs-force-command
)
119 (defgroup log-view nil
120 "Major mode for browsing log output of revision log histories."
124 (easy-mmode-defmap log-view-mode-map
126 ("-" . negative-argument
)
127 ("0" . digit-argument
)
128 ("1" . digit-argument
)
129 ("2" . digit-argument
)
130 ("3" . digit-argument
)
131 ("4" . digit-argument
)
132 ("5" . digit-argument
)
133 ("6" . digit-argument
)
134 ("7" . digit-argument
)
135 ("8" . digit-argument
)
136 ("9" . digit-argument
)
138 ("\C-m" . log-view-toggle-entry-display
)
139 ("m" . log-view-toggle-mark-entry
)
140 ("e" . log-view-modify-change-comment
)
141 ("d" . log-view-diff
)
142 ("=" . log-view-diff
)
143 ("D" . log-view-diff-changeset
)
144 ("a" . log-view-annotate-version
)
145 ("f" . log-view-find-revision
)
146 ("n" . log-view-msg-next
)
147 ("p" . log-view-msg-prev
)
148 ("\t" . log-view-msg-next
)
149 ([backtab] . log-view-msg-prev)
150 ("N" . log-view-file-next)
151 ("P" . log-view-file-prev)
152 ("\M-n" . log-view-file-next)
153 ("\M-p" . log-view-file-prev))
155 :inherit special-mode-map
158 (easy-menu-define log-view-mode-menu log-view-mode-map
159 "Log-View Display Menu"
161 ;; XXX Do we need menu entries for these?
162 ;; ["Quit" quit-window]
163 ;; ["Kill This Buffer" kill-this-buffer]
164 ["Mark Log Entry for Diff" set-mark-command
166 ["Diff Revisions" log-view-diff
167 :help "Get the diff between two revisions"]
168 ["Changeset Diff" log-view-diff-changeset
169 :help "Get the changeset diff between two revisions"]
170 ["Visit Version" log-view-find-revision
171 :help "Visit the version at point"]
172 ["Annotate Version" log-view-annotate-version
173 :help "Annotate the version at point"]
174 ["Modify Log Comment" log-view-modify-change-comment
175 :help "Edit the change comment displayed at point"]
176 ["Toggle Details at Point" log-view-toggle-entry-display
177 :active log-view-expanded-log-entry-function]
179 ["Next Log Entry" log-view-msg-next
180 :help "Go to the next count'th log message"]
181 ["Previous Log Entry" log-view-msg-prev
182 :help "Go to the previous count'th log message"]
183 ["Next File" log-view-file-next
184 :help "Go to the next count'th file"]
185 ["Previous File" log-view-file-prev
186 :help "Go to the previous count'th file"]))
188 (defvar log-view-mode-hook nil
189 "Hook run at the end of `log-view-mode'.")
191 (defvar log-view-expanded-log-entry-function nil
192 "Function returning the detailed description of a Log View entry.
193 It is called by the command `log-view-toggle-entry-display' with
194 one arg, the revision tag (a string), and should return a string.
195 If it is nil, `log-view-toggle-entry-display' does nothing.")
197 (defface log-view-file
198 '((((class color) (background light))
199 (:background "grey70" :weight bold))
201 "Face for the file header line in `log-view-mode'."
204 (defface log-view-message
205 '((((class color) (background light))
206 (:background "grey85"))
208 "Face for the message header line in `log-view-mode'."
211 (defvar log-view-file-re
212 (concat "^\\(?:Working file: \\(?1:.+\\)" ;RCS and CVS.
213 ;; Subversion has no such thing??
214 "\\|\\(?:SCCS/s\\.\\|Changes to \\)\\(?1:.+\\):" ;SCCS and Darcs.
215 "\\)\n") ;Include the \n for font-lock reasons.
216 "Regexp matching the text identifying the file.
217 The match group number 1 should match the file name itself.")
219 (defvar log-view-per-file-logs t
220 "Set if to t if the logs are shown one file at a time.")
222 (defvar log-view-message-re
223 (concat "^\\(?:revision \\(?1:[.0-9]+\\)\\(?:\t.*\\)?" ; RCS and CVS.
224 "\\|r\\(?1:[0-9]+\\) | .* | .*" ; Subversion.
225 "\\|D \\(?1:[.0-9]+\\) .*" ; SCCS.
226 ;; Darcs doesn't have revision names. VC-darcs uses patch names
227 ;; instead. Darcs patch names are hashcodes, which do not appear
228 ;; in the log output :-(, but darcs accepts any prefix of the log
229 ;; message as a patch name, so we match the first line of the log
231 ;; First loosely match the date format.
232 (concat "\\|[^ \n].*[^0-9\n][0-9][0-9]:[0-9][0-9][^0-9\n].*[^ \n]"
233 ;;Email of user and finally Msg, used as revision name.
234 " .*@.*\n\\(?: \\* \\(?1:.*\\)\\)?")
236 "Regexp matching the text identifying a revision.
237 The match group number 1 should match the revision number itself.")
239 (defvar log-view-font-lock-keywords
240 ;; We use `eval' so as to use the buffer-local value of log-view-file-re
241 ;; and log-view-message-re, if applicable.
242 '((eval . `(,log-view-file-re
243 (1 (if (boundp 'cvs-filename-face) cvs-filename-face))
244 (0 'log-view-file append)))
245 (eval . `(,log-view-message-re . 'log-view-message))))
247 (defconst log-view-font-lock-defaults
248 '(log-view-font-lock-keywords t nil nil nil))
250 (defvar log-view-vc-fileset nil
251 "The VC fileset corresponding to the current log.")
253 (defvar log-view-vc-backend nil
254 "The VC backend that created the current log.")
261 (define-derived-mode log-view-mode special-mode "Log-View"
262 "Major mode for browsing CVS log output."
263 (setq buffer-read-only t)
264 (set (make-local-variable 'font-lock-defaults) log-view-font-lock-defaults)
265 (set (make-local-variable 'beginning-of-defun-function)
266 'log-view-beginning-of-defun)
267 (set (make-local-variable 'end-of-defun-function)
268 'log-view-end-of-defun)
269 (set (make-local-variable 'cvs-minor-wrap-function) 'log-view-minor-wrap)
270 (hack-dir-local-variables-non-file-buffer))
276 ;; define log-view-{msg,file}-{next,prev}
277 (easy-mmode-define-navigation log-view-msg log-view-message-re "log message")
278 (easy-mmode-define-navigation log-view-file log-view-file-re "file")
280 (defun log-view-goto-rev (rev)
281 "Go to revision REV."
282 (goto-char (point-min))
284 (while (not (equal rev (log-view-current-tag)))
289 ;;;; Linkage to PCL-CVS (mostly copied from cvs-status.el)
292 (defconst log-view-dir-re "^cvs[.ex]* [a-z]+: Logging \\(.+\\)$")
294 (defun log-view-current-file ()
295 "Return the current file."
298 (or (re-search-backward log-view-file-re nil t)
299 (re-search-forward log-view-file-re nil t)
300 (error "Unable to determine the current file"))
301 (let* ((file (match-string 1))
302 (cvsdir (and (re-search-backward log-view-dir-re nil t)
304 (pcldir (and (boundp 'cvs-pcl-cvs-dirchange-re)
305 (re-search-backward cvs-pcl-cvs-dirchange-re nil t)
308 (let ((default-directory ""))
309 (when pcldir (setq dir (expand-file-name pcldir dir)))
310 (when cvsdir (setq dir (expand-file-name cvsdir dir))))
311 (expand-file-name file dir))))
313 (defun log-view-current-entry (&optional pos move)
314 "Return the position and revision tag of the Log View entry at POS.
315 This is a list (BEG TAG), where BEG is a buffer position and TAG
316 is a string. If POS is nil or omitted, it defaults to point.
317 If there is no entry at POS, return nil.
319 If optional arg MOVE is non-nil, move point to BEG if found.
320 Otherwise, don't move point."
324 (when pos (goto-char pos))
326 ;; Treat "---" separator lines as part of the following revision.
327 (forward-line (if (looking-at "-\\{20,\\}$") 2 1))
329 (setq pos (re-search-backward log-view-message-re nil 'move)
330 looping (and pos (log-view-inside-comment-p (point)))))
333 (list pos (match-string-no-properties 1)))))
334 (and move result (goto-char pos))
337 (defun log-view-inside-comment-p (pos)
338 "Return non-nil if POS lies inside an expanded log entry."
339 (eq (get-text-property pos 'log-view-comment) t))
341 (defun log-view-current-tag (&optional pos)
342 "Return the revision tag (a string) of the Log View entry at POS.
343 if POS is omitted or nil, it defaults to point."
344 (cadr (log-view-current-entry pos)))
346 (defun log-view-toggle-mark-entry ()
347 "Toggle the marked state for the log entry at point.
348 Individual log entries can be marked and unmarked. The marked
349 entries are denoted by changing their background color.
350 `log-view-get-marked' returns the list of tags for the marked
354 (let* ((entry (log-view-current-entry nil t))
358 ;; Look to see if the current entry is marked.
359 (setq found (get-char-property beg 'log-view-self))
361 (delete-overlay found)
362 ;; Create an overlay covering this entry and change its color.
363 (let* ((end (if (get-text-property beg 'log-view-entry-expanded)
364 (next-single-property-change beg 'log-view-comment)
365 (log-view-end-of-defun)
367 (ov (make-overlay beg end)))
368 (overlay-put ov 'face 'log-view-file)
369 ;; This is used to check if the overlay is present.
370 (overlay-put ov 'log-view-self ov)
371 (overlay-put ov 'log-view-marked (nth 1 entry))))))))
373 (defun log-view-get-marked ()
374 "Return the list of tags for the marked log entries."
376 (let ((pos (point-min))
378 (while (setq pos (next-single-property-change pos 'face))
379 (when (setq ov (get-char-property pos 'log-view-self))
380 (push (overlay-get ov 'log-view-marked) marked-list)
381 (setq pos (overlay-end ov))))
384 (defun log-view-toggle-entry-display ()
385 "If possible, expand the current Log View entry.
386 This calls `log-view-expanded-log-entry-function' to do the work."
388 ;; Don't do anything unless `log-view-expanded-log-entry-function'
389 ;; is defined in this mode.
390 (when (functionp log-view-expanded-log-entry-function)
391 (let* ((opoint (point))
392 (entry (log-view-current-entry nil t))
394 (buffer-read-only nil))
396 (if (get-text-property beg 'log-view-entry-expanded)
397 ;; If the entry is expanded, collapse it.
398 (let ((pos (next-single-property-change beg 'log-view-comment)))
399 (unless (and pos (log-view-inside-comment-p pos))
400 (error "Broken markup in `log-view-toggle-entry-display'"))
403 (next-single-property-change pos 'log-view-comment)
405 (put-text-property beg (1+ beg) 'log-view-entry-expanded nil)
408 ;; Otherwise, expand the entry.
409 (let ((long-entry (funcall log-view-expanded-log-entry-function
412 (put-text-property beg (1+ beg) 'log-view-entry-expanded t)
413 (log-view-end-of-defun)
415 (insert long-entry "\n")
418 '(font-lock-face font-lock-comment-face log-view-comment t))
419 (goto-char opoint))))))))
421 (defun log-view-beginning-of-defun (&optional arg)
422 "Move backward to the beginning of a Log View entry.
423 With ARG, do it that many times. Negative ARG means move forward
424 to the beginning of the ARGth following entry.
426 This is Log View mode's default `beginning-of-defun-function'.
427 It assumes that a log entry starts with a line matching
428 `log-view-message-re'."
429 (when (null arg) (setf arg 1))
431 ;; In log view, the end of one defun is the beginning of the
432 ;; next, so punting to log-view-end-of-defun is safe in this
434 (log-view-end-of-defun (- arg))
438 (let ((cur-start (log-view-current-entry)))
440 (cond ((null cur-start)
441 (goto-char (point-min))
443 ((>= (car cur-start) (point))
449 (goto-char (car cur-start))
453 (defun log-view-end-of-defun-1 ()
454 "Move forward to the next Log View entry."
456 (if (looking-at log-view-message-re)
457 (goto-char (match-end 0)))
460 ((re-search-forward log-view-message-re nil 'move)
461 (unless (log-view-inside-comment-p (point))
463 (goto-char (match-beginning 0))))
464 ;; Don't advance past the end buttons inserted by
465 ;; `vc-print-log-setup-buttons'.
466 ((looking-back "Show 2X entries Show unlimited entries"
467 (line-beginning-position))
470 ;; There are no buttons if we've turned on unlimited entries.
472 (setq looping nil))))))
474 (defun log-view-end-of-defun (&optional arg)
475 "Move forward to the next Log View entry.
476 Works like `end-of-defun'."
477 (when (null arg) (setf arg 1))
479 (log-view-beginning-of-defun (- arg))
481 (log-view-end-of-defun-1)
484 (defvar cvs-minor-current-files)
485 (defvar cvs-branch-prefix)
486 (defvar cvs-secondary-branch-prefix)
488 (defun log-view-minor-wrap (buf f)
489 (let ((data (with-current-buffer buf
491 (end (if (use-region-p) (mark) (point)))
492 (fr (log-view-current-tag beg))
493 (to (log-view-current-tag end)))
494 (when (string-equal fr to)
498 (setq to (log-view-current-tag))))
500 ;; The first revision has to be the one at point, for
501 ;; operations that only take one revision
502 ;; (e.g. cvs-mode-edit).
503 (cons (log-view-current-file) fr)
504 (cons (log-view-current-file) to))))))
505 (let ((cvs-branch-prefix (cdar data))
506 (cvs-secondary-branch-prefix (and (cdar data) (cddr data)))
507 (cvs-minor-current-files
509 (when (and (cadr data) (not (equal (caar data) (cadr data))))
510 (list (cadr data)))))
511 ;; FIXME: I need to force because the fileinfos are UNKNOWN
512 (cvs-force-command "/F"))
515 (defun log-view-find-revision (pos)
516 "Visit the version at POS.
517 If called interactively, visit the version at point."
519 (unless log-view-per-file-logs
520 (when (> (length log-view-vc-fileset) 1)
521 (error "Multiple files shown in this buffer, cannot use this command here")))
524 (switch-to-buffer (vc-find-revision (if log-view-per-file-logs
525 (log-view-current-file)
526 (car log-view-vc-fileset))
527 (log-view-current-tag)))))
530 (defun log-view-extract-comment ()
531 "Parse comment from around the current point in the log."
533 (let (st en (backend (vc-backend (log-view-current-file))))
534 (log-view-end-of-defun)
535 (cond ((eq backend 'SVN)
538 (or (log-view-current-entry nil t)
539 (throw 'beginning-of-buffer nil))
540 (cond ((memq backend '(SCCS RCS CVS SVN))
544 (re-search-forward "summary: *" nil t)))
546 (buffer-substring st en))))
548 (declare-function vc-modify-change-comment "vc" (files rev oldcomment))
550 (defun log-view-modify-change-comment ()
551 "Edit the change comment displayed at point."
553 (vc-modify-change-comment (list (if log-view-per-file-logs
554 (log-view-current-file)
555 (car log-view-vc-fileset)))
556 (log-view-current-tag)
557 (log-view-extract-comment)))
559 (defun log-view-annotate-version (pos)
560 "Annotate the version at POS.
561 If called interactively, annotate the version at point."
563 (unless log-view-per-file-logs
564 (when (> (length log-view-vc-fileset) 1)
565 (error "Multiple files shown in this buffer, cannot use this command here")))
568 (vc-annotate (if log-view-per-file-logs
569 (log-view-current-file)
570 (car log-view-vc-fileset))
571 (log-view-current-tag))))
577 (defun log-view-diff (beg end)
578 "Get the diff between two revisions.
579 If the region is inactive or the mark is on the revision at
580 point, get the diff between the revision at point and its
581 previous revision. Otherwise, get the diff between the revisions
582 where the region starts and ends.
584 Unlike `log-view-diff-changeset', this function only shows the
585 part of the changeset which affected the currently considered
588 (list (if (use-region-p) (region-beginning) (point))
589 (if (use-region-p) (region-end) (point))))
590 (log-view-diff-common beg end))
592 (defun log-view-diff-changeset (beg end)
593 "Get the diff between two revisions.
594 If the region is inactive or the mark is on the revision at
595 point, get the diff between the revision at point and its
596 previous revision. Otherwise, get the diff between the revisions
597 where the region starts and ends.
599 Unlike `log-view-diff' this function shows the whole changeset,
600 including changes affecting other files than the currently
603 (list (if (use-region-p) (region-beginning) (point))
604 (if (use-region-p) (region-end) (point))))
605 (when (eq (vc-call-backend log-view-vc-backend 'revision-granularity) 'file)
606 (error "The %s backend does not support changeset diffs" log-view-vc-backend))
607 (let ((default-directory (vc-root-dir)))
608 (log-view-diff-common beg end t)))
610 (defun log-view-diff-common (beg end &optional whole-changeset)
611 (let* ((to (log-view-current-tag beg))
612 (fr-entry (log-view-current-entry end))
613 (fr (cadr fr-entry)))
614 ;; When TO and FR are the same, or when point is on a line after
615 ;; the last entry, look at the previous revision.
616 (when (or (string-equal fr to)
619 (goto-char (car fr-entry))
621 (setq fr (vc-call-backend log-view-vc-backend 'previous-revision nil fr)))
623 t (list log-view-vc-backend
624 ;; The value passed here should follow what
625 ;; `vc-deduce-fileset' returns. If we want to see the
626 ;; diff for all the files in the changeset, pass NIL for
628 (unless whole-changeset
629 (if log-view-per-file-logs
630 (list (log-view-current-file))
631 log-view-vc-fileset)))
636 ;;; log-view.el ends here