Merge from emacs-24; up to 2012-04-21T14:12:27Z!sdl.web@gmail.com
[emacs.git] / lisp / vc / log-edit.el
blob2abb1ec9c0a89982fc0dd9b949b87d418a5bc628
1 ;;; log-edit.el --- Major mode for editing CVS commit messages -*- lexical-binding: t -*-
3 ;; Copyright (C) 1999-2012 Free Software Foundation, Inc.
5 ;; Author: Stefan Monnier <monnier@iro.umontreal.ca>
6 ;; Keywords: pcl-cvs cvs commit log vc
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 ;; Todo:
27 ;; - Move in VC's code
28 ;; - Add compatibility for VC's hook variables
30 ;;; Code:
32 (eval-when-compile (require 'cl))
33 (require 'add-log) ; for all the ChangeLog goodies
34 (require 'pcvs-util)
35 (require 'ring)
37 ;;;;
38 ;;;; Global Variables
39 ;;;;
41 (defgroup log-edit nil
42 "Major mode for editing RCS and CVS commit messages."
43 :group 'pcl-cvs
44 :group 'vc ; It's used by VC.
45 :version "21.1"
46 :prefix "log-edit-")
48 ;; compiler pacifiers
49 (defvar cvs-buffer)
52 ;; The main keymap
54 (easy-mmode-defmap log-edit-mode-map
55 `(("\C-c\C-c" . log-edit-done)
56 ("\C-c\C-a" . log-edit-insert-changelog)
57 ("\C-c\C-d" . log-edit-show-diff)
58 ("\C-c\C-f" . log-edit-show-files)
59 ("\M-n" . log-edit-next-comment)
60 ("\M-p" . log-edit-previous-comment)
61 ("\M-r" . log-edit-comment-search-backward)
62 ("\M-s" . log-edit-comment-search-forward)
63 ("\C-c?" . log-edit-mode-help))
64 "Keymap for the `log-edit-mode' (to edit version control log messages)."
65 :group 'log-edit)
67 ;; Compatibility with old names. Should we bother ?
68 (defvar vc-log-mode-map log-edit-mode-map)
69 (defvar vc-log-entry-mode vc-log-mode-map)
71 (easy-menu-define log-edit-menu log-edit-mode-map
72 "Menu used for `log-edit-mode'."
73 '("Log-Edit"
74 ["Done" log-edit-done
75 :help "Exit log-edit and proceed with the actual action."]
76 "--"
77 ["Insert ChangeLog" log-edit-insert-changelog
78 :help "Insert a log message by looking at the ChangeLog"]
79 ["Add to ChangeLog" log-edit-add-to-changelog
80 :help "Insert this log message into the appropriate ChangeLog file"]
81 "--"
82 ["Show diff" log-edit-show-diff
83 :help "Show the diff for the files to be committed."]
84 ["List files" log-edit-show-files
85 :help "Show the list of relevant files."]
86 "--"
87 ["Previous comment" log-edit-previous-comment
88 :help "Cycle backwards through comment history"]
89 ["Next comment" log-edit-next-comment
90 :help "Cycle forwards through comment history."]
91 ["Search comment forward" log-edit-comment-search-forward
92 :help "Search forwards through comment history for a substring match of str"]
93 ["Search comment backward" log-edit-comment-search-backward
94 :help "Search backwards through comment history for substring match of str"]))
96 (defcustom log-edit-confirm 'changed
97 "If non-nil, `log-edit-done' will request confirmation.
98 If 'changed, only request confirmation if the list of files has
99 changed since the beginning of the log-edit session."
100 :group 'log-edit
101 :type '(choice (const changed) (const t) (const nil)))
103 (defcustom log-edit-keep-buffer nil
104 "If non-nil, don't hide the buffer after `log-edit-done'."
105 :group 'log-edit
106 :type 'boolean)
108 (defvar cvs-commit-buffer-require-final-newline t)
109 (make-obsolete-variable 'cvs-commit-buffer-require-final-newline
110 'log-edit-require-final-newline
111 "21.1")
113 (defcustom log-edit-require-final-newline
114 cvs-commit-buffer-require-final-newline
115 "Enforce a newline at the end of commit log messages.
116 Enforce it silently if t, query if non-nil and don't do anything if nil."
117 :group 'log-edit
118 :type '(choice (const ask) (const t) (const nil)))
120 (defcustom log-edit-setup-invert nil
121 "Non-nil means `log-edit' should invert the meaning of its SETUP arg.
122 If SETUP is 'force, this variable has no effect."
123 :group 'log-edit
124 :type 'boolean)
126 (defcustom log-edit-hook '(log-edit-insert-cvs-template
127 log-edit-show-files
128 log-edit-insert-changelog)
129 "Hook run at the end of `log-edit'."
130 :group 'log-edit
131 :type '(hook :options (log-edit-insert-changelog
132 log-edit-insert-cvs-rcstemplate
133 log-edit-insert-cvs-template
134 log-edit-insert-filenames)))
136 (defcustom log-edit-mode-hook (if (boundp 'vc-log-mode-hook) vc-log-mode-hook)
137 "Hook run when entering `log-edit-mode'."
138 :group 'log-edit
139 :type 'hook)
141 (defcustom log-edit-done-hook nil
142 "Hook run before doing the actual commit.
143 This hook can be used to cleanup the message, enforce various
144 conventions, or to allow recording the message in some other database,
145 such as a bug-tracking system. The list of files about to be committed
146 can be obtained from `log-edit-files'."
147 :group 'log-edit
148 :type '(hook :options (log-edit-set-common-indentation
149 log-edit-add-to-changelog)))
151 (defcustom log-edit-strip-single-file-name nil
152 "If non-nil, remove file name from single-file log entries."
153 :type 'boolean
154 :safe 'booleanp
155 :group 'log-edit
156 :version "24.1")
158 (defvar cvs-changelog-full-paragraphs t)
159 (make-obsolete-variable 'cvs-changelog-full-paragraphs
160 'log-edit-changelog-full-paragraphs
161 "21.1")
163 (defvar log-edit-changelog-full-paragraphs cvs-changelog-full-paragraphs
164 "If non-nil, include full ChangeLog paragraphs in the log.
165 This may be set in the ``local variables'' section of a ChangeLog, to
166 indicate the policy for that ChangeLog.
168 A ChangeLog paragraph is a bunch of log text containing no blank lines;
169 a paragraph usually describes a set of changes with a single purpose,
170 but perhaps spanning several functions in several files. Changes in
171 different paragraphs are unrelated.
173 You could argue that the log entry for a file should contain the
174 full ChangeLog paragraph mentioning the change to the file, even though
175 it may mention other files, because that gives you the full context you
176 need to understand the change. This is the behavior you get when this
177 variable is set to t.
179 On the other hand, you could argue that the log entry for a change
180 should contain only the text for the changes which occurred in that
181 file, because the log is per-file. This is the behavior you get
182 when this variable is set to nil.")
184 ;;;; Internal global or buffer-local vars
186 (defconst log-edit-files-buf "*log-edit-files*")
187 (defvar log-edit-initial-files nil)
188 (defvar log-edit-callback nil)
189 (defvar log-edit-diff-function nil)
190 (defvar log-edit-listfun nil)
192 (defvar log-edit-parent-buffer nil)
194 ;;; Originally taken from VC-Log mode
196 (defconst log-edit-maximum-comment-ring-size 32
197 "Maximum number of saved comments in the comment ring.")
198 (defvar log-edit-comment-ring (make-ring log-edit-maximum-comment-ring-size))
199 (defvar log-edit-comment-ring-index nil)
200 (defvar log-edit-last-comment-match "")
202 (defun log-edit-new-comment-index (stride len)
203 "Return the comment index STRIDE elements from the current one.
204 LEN is the length of `log-edit-comment-ring'."
205 (mod (cond
206 (log-edit-comment-ring-index (+ log-edit-comment-ring-index stride))
207 ;; Initialize the index on the first use of this command
208 ;; so that the first M-p gets index 0, and the first M-n gets
209 ;; index -1.
210 ((> stride 0) (1- stride))
211 (t stride))
212 len))
214 (defun log-edit-previous-comment (arg)
215 "Cycle backwards through comment history.
216 With a numeric prefix ARG, go back ARG comments."
217 (interactive "*p")
218 (let ((len (ring-length log-edit-comment-ring)))
219 (if (<= len 0)
220 (progn (message "Empty comment ring") (ding))
221 ;; Don't use `erase-buffer' because we don't want to `widen'.
222 (delete-region (point-min) (point-max))
223 (setq log-edit-comment-ring-index (log-edit-new-comment-index arg len))
224 (message "Comment %d" (1+ log-edit-comment-ring-index))
225 (insert (ring-ref log-edit-comment-ring log-edit-comment-ring-index)))))
227 (defun log-edit-next-comment (arg)
228 "Cycle forwards through comment history.
229 With a numeric prefix ARG, go forward ARG comments."
230 (interactive "*p")
231 (log-edit-previous-comment (- arg)))
233 (defun log-edit-comment-search-backward (str &optional stride)
234 "Search backwards through comment history for substring match of STR.
235 If the optional argument STRIDE is present, that is a step-width to use
236 when going through the comment ring."
237 ;; Why substring rather than regexp ? -sm
238 (interactive
239 (list (read-string "Comment substring: " nil nil log-edit-last-comment-match)))
240 (unless stride (setq stride 1))
241 (if (string= str "")
242 (setq str log-edit-last-comment-match)
243 (setq log-edit-last-comment-match str))
244 (let* ((str (regexp-quote str))
245 (len (ring-length log-edit-comment-ring))
246 (n (log-edit-new-comment-index stride len)))
247 (while (progn (when (or (>= n len) (< n 0)) (error "Not found"))
248 (not (string-match str (ring-ref log-edit-comment-ring n))))
249 (setq n (+ n stride)))
250 (setq log-edit-comment-ring-index n)
251 (log-edit-previous-comment 0)))
253 (defun log-edit-comment-search-forward (str)
254 "Search forwards through comment history for a substring match of STR."
255 (interactive
256 (list (read-string "Comment substring: " nil nil log-edit-last-comment-match)))
257 (log-edit-comment-search-backward str -1))
259 (defun log-edit-comment-to-change-log (&optional whoami file-name)
260 "Enter last VC comment into the change log for the current file.
261 WHOAMI (interactive prefix) non-nil means prompt for user name
262 and site. FILE-NAME is the name of the change log; if nil, use
263 `change-log-default-name'.
265 This may be useful as a `log-edit-checkin-hook' to update change logs
266 automatically."
267 (interactive (if current-prefix-arg
268 (list current-prefix-arg
269 (prompt-for-change-log-name))))
270 (let (;; Extract the comment first so we get any error before doing anything.
271 (comment (ring-ref log-edit-comment-ring 0))
272 ;; Don't let add-change-log-entry insert a defun name.
273 (add-log-current-defun-function 'ignore)
274 end)
275 ;; Call add-log to do half the work.
276 (add-change-log-entry whoami file-name t t)
277 ;; Insert the VC comment, leaving point before it.
278 (setq end (save-excursion (insert comment) (point-marker)))
279 (if (looking-at "\\s *\\s(")
280 ;; It starts with an open-paren, as in "(foo): Frobbed."
281 ;; So remove the ": " add-log inserted.
282 (delete-char -2))
283 ;; Canonicalize the white space between the file name and comment.
284 (just-one-space)
285 ;; Indent rest of the text the same way add-log indented the first line.
286 (let ((indentation (current-indentation)))
287 (save-excursion
288 (while (< (point) end)
289 (forward-line 1)
290 (indent-to indentation))
291 (setq end (point))))
292 ;; Fill the inserted text, preserving open-parens at bol.
293 (let ((paragraph-start (concat paragraph-start "\\|\\s *\\s(")))
294 (beginning-of-line)
295 (fill-region (point) end))
296 ;; Canonicalize the white space at the end of the entry so it is
297 ;; separated from the next entry by a single blank line.
298 (skip-syntax-forward " " end)
299 (delete-char (- (skip-syntax-backward " ")))
300 (or (eobp) (looking-at "\n\n")
301 (insert "\n"))))
303 ;; Compatibility with old names.
304 (define-obsolete-variable-alias 'vc-comment-ring 'log-edit-comment-ring "22.1")
305 (define-obsolete-variable-alias 'vc-comment-ring-index 'log-edit-comment-ring-index "22.1")
306 (define-obsolete-function-alias 'vc-previous-comment 'log-edit-previous-comment "22.1")
307 (define-obsolete-function-alias 'vc-next-comment 'log-edit-next-comment "22.1")
308 (define-obsolete-function-alias 'vc-comment-search-reverse 'log-edit-comment-search-backward "22.1")
309 (define-obsolete-function-alias 'vc-comment-search-forward 'log-edit-comment-search-forward "22.1")
310 (define-obsolete-function-alias 'vc-comment-to-change-log 'log-edit-comment-to-change-log "22.1")
313 ;;; Actual code
316 (defface log-edit-summary '((t :inherit font-lock-function-name-face))
317 "Face for the summary in `log-edit-mode' buffers.")
319 (defface log-edit-header '((t :inherit font-lock-keyword-face))
320 "Face for the headers in `log-edit-mode' buffers.")
322 (defface log-edit-unknown-header '((t :inherit font-lock-comment-face))
323 "Face for unknown headers in `log-edit-mode' buffers.")
325 (defvar log-edit-headers-alist '(("Summary" . log-edit-summary)
326 ("Fixes") ("Author"))
327 "AList of known headers and the face to use to highlight them.")
329 (defconst log-edit-header-contents-regexp
330 "[ \t]*\\(.*\\(\n[ \t].*\\)*\\)\n?")
332 (defun log-edit-match-to-eoh (_limit)
333 ;; FIXME: copied from message-match-to-eoh.
334 (let ((start (point)))
335 (rfc822-goto-eoh)
336 ;; Typical situation: some temporary change causes the header to be
337 ;; incorrect, so EOH comes earlier than intended: the last lines of the
338 ;; intended headers are now not considered part of the header any more,
339 ;; so they don't have the multiline property set. When the change is
340 ;; completed and the header has its correct shape again, the lack of the
341 ;; multiline property means we won't rehighlight the last lines of
342 ;; the header.
343 (if (< (point) start)
344 nil ;No header within start..limit.
345 ;; Here we disregard LIMIT so that we may extend the area again.
346 (set-match-data (list start (point)))
347 (point))))
349 (defvar log-edit-font-lock-keywords
350 ;; Copied/inspired by message-font-lock-keywords.
351 `((log-edit-match-to-eoh
352 (,(concat "^\\(\\([[:alpha:]]+\\):\\)" log-edit-header-contents-regexp)
353 (progn (goto-char (match-beginning 0)) (match-end 0)) nil
354 (1 (if (assoc (match-string 2) log-edit-headers-alist)
355 'log-edit-header
356 'log-edit-unknown-header)
357 nil lax)
358 ;; From `log-edit-header-contents-regexp':
359 (3 (or (cdr (assoc (match-string 2) log-edit-headers-alist))
360 'log-edit-header)
361 nil lax)))))
363 (defvar log-edit-font-lock-gnu-style nil
364 "If non-nil, highlight common failures to follow the GNU coding standards.")
365 (put 'log-edit-font-lock-gnu-style 'safe-local-variable 'booleanp)
367 (defconst log-edit-font-lock-gnu-keywords
368 ;; Use
369 ;; * foo.el (bla, bli)
370 ;; (blo, blu): Toto.
371 ;; Rather than
372 ;; * foo.el (bla, bli,
373 ;; blo, blu): Toto.
374 '(("^[ \t]*\\(?:\\* .*\\)?\\(([^\n)]*,\\s-*\\)$"
375 (1 '(face font-lock-warning-face
376 help-echo "Continue function lists with \")\\n(\".") t))
377 ;; Don't leave a lone word on a single line.
378 ;;("^\\s-*\\(\\S-*[^\n:)]\\)\\s-*$" (1 font-lock-warning-face t))
379 ;; Don't cut a sentence right after the first word (better to move
380 ;; the sentence on the next line, then).
381 ;;("[.:]\\s-+\\(\\sw+\\)\\s-*$" (1 font-lock-warning-face t))
382 ;; Change Log entries should use present tense.
383 ("):[ \t\n]*[[:alpha:]]+\\(ed\\)\\>"
384 (1 '(face font-lock-warning-face help-echo "Use present tense.") t))
385 ;; Change log entries start with a capital letter.
386 ("): [a-z]" (0 '(face font-lock-warning-face help-echo "Capitalize.") t))
387 ("[^[:upper:]]\\(\\. [[:upper:]]\\)"
388 (1 '(face font-lock-warning-face
389 help-echo "Use two spaces to end a sentence") t))
390 ("^("
391 (0 (let ((beg (max (point-min) (- (match-beginning 0) 2))))
392 (put-text-property beg (match-end 0) 'font-lock-multiline t)
393 (if (eq (char-syntax (char-after beg)) ?w)
394 '(face font-lock-warning-face
395 help-echo "Punctuate previous line.")))
399 (defun log-edit-font-lock-keywords ()
400 (if log-edit-font-lock-gnu-style
401 (append log-edit-font-lock-keywords
402 log-edit-font-lock-gnu-keywords)
403 log-edit-font-lock-keywords))
405 ;;;###autoload
406 (defun log-edit (callback &optional setup params buffer mode &rest _ignore)
407 "Setup a buffer to enter a log message.
408 \\<log-edit-mode-map>The buffer will be put in mode MODE or `log-edit-mode'
409 if MODE is nil.
410 If SETUP is non-nil, the buffer is then erased and `log-edit-hook' is run.
411 Mark and point will be set around the entire contents of the buffer so
412 that it is easy to kill the contents of the buffer with \\[kill-region].
413 Once you're done editing the message, pressing \\[log-edit-done] will call
414 `log-edit-done' which will end up calling CALLBACK to do the actual commit.
416 PARAMS if non-nil is an alist. Possible keys and associated values:
417 `log-edit-listfun' -- function taking no arguments that returns the list of
418 files that are concerned by the current operation (using relative names);
419 `log-edit-diff-function' -- function taking no arguments that
420 displays a diff of the files concerned by the current operation.
422 If BUFFER is non-nil `log-edit' will jump to that buffer, use it to edit the
423 log message and go back to the current buffer when done. Otherwise, it
424 uses the current buffer."
425 (let ((parent (current-buffer)))
426 (if buffer (pop-to-buffer buffer))
427 (when (and log-edit-setup-invert (not (eq setup 'force)))
428 (setq setup (not setup)))
429 (when setup
430 (erase-buffer)
431 (insert "Summary: \nAuthor: ")
432 (save-excursion (insert "\n\n")))
433 (if mode
434 (funcall mode)
435 (log-edit-mode))
436 (set (make-local-variable 'log-edit-callback) callback)
437 (if (listp params)
438 (dolist (crt params)
439 (set (make-local-variable (car crt)) (cdr crt)))
440 ;; For backward compatibility with log-edit up to version 22.2
441 ;; accept non-list PARAMS to mean `log-edit-list'.
442 (set (make-local-variable 'log-edit-listfun) params))
444 (if buffer (set (make-local-variable 'log-edit-parent-buffer) parent))
445 (set (make-local-variable 'log-edit-initial-files) (log-edit-files))
446 (when setup (run-hooks 'log-edit-hook))
447 (goto-char (point-min)) (push-mark (point-max))
448 (message "%s" (substitute-command-keys
449 "Press \\[log-edit-done] when you are done editing."))))
451 (define-derived-mode log-edit-mode text-mode "Log-Edit"
452 "Major mode for editing version-control log messages.
453 When done editing the log entry, just type \\[log-edit-done] which
454 will trigger the actual commit of the file(s).
455 Several other handy support commands are provided of course and
456 the package from which this is used might also provide additional
457 commands (under C-x v for VC, for example).
459 \\{log-edit-mode-map}"
460 (set (make-local-variable 'font-lock-defaults)
461 '(log-edit-font-lock-keywords t))
462 (make-local-variable 'log-edit-comment-ring-index)
463 (hack-dir-local-variables-non-file-buffer))
465 (defun log-edit-hide-buf (&optional buf where)
466 (when (setq buf (get-buffer (or buf log-edit-files-buf)))
467 (let ((win (get-buffer-window buf where)))
468 (if win (ignore-errors (delete-window win))))
469 (bury-buffer buf)))
471 (defun log-edit-done ()
472 "Finish editing the log message and commit the files.
473 If you want to abort the commit, simply delete the buffer."
474 (interactive)
475 ;; Clean up empty headers.
476 (goto-char (point-min))
477 (while (looking-at (concat "^[a-z]*:" log-edit-header-contents-regexp))
478 (let ((beg (match-beginning 0)))
479 (goto-char (match-end 0))
480 (if (string-match "\\`[ \n\t]*\\'" (match-string 1))
481 (delete-region beg (point)))))
482 ;; Get rid of leading empty lines.
483 (goto-char (point-min))
484 (when (looking-at "\\([ \t]*\n\\)+")
485 (delete-region (match-beginning 0) (match-end 0)))
486 ;; Get rid of trailing empty lines
487 (goto-char (point-max))
488 (skip-syntax-backward " ")
489 (when (equal (char-after) ?\n) (forward-char 1))
490 (delete-region (point) (point-max))
491 ;; Check for final newline
492 (if (and (> (point-max) (point-min))
493 (/= (char-before (point-max)) ?\n)
494 (or (eq log-edit-require-final-newline t)
495 (and log-edit-require-final-newline
496 (y-or-n-p
497 (format "Buffer %s does not end in newline. Add one? "
498 (buffer-name))))))
499 (save-excursion
500 (goto-char (point-max))
501 (insert ?\n)))
502 (let ((comment (buffer-string)))
503 (when (or (ring-empty-p log-edit-comment-ring)
504 (not (equal comment (ring-ref log-edit-comment-ring 0))))
505 (ring-insert log-edit-comment-ring comment)))
506 (let ((win (get-buffer-window log-edit-files-buf)))
507 (if (and log-edit-confirm
508 (not (and (eq log-edit-confirm 'changed)
509 (equal (log-edit-files) log-edit-initial-files)))
510 (progn
511 (log-edit-show-files)
512 (not (y-or-n-p "Really commit? "))))
513 (progn (when (not win) (log-edit-hide-buf))
514 (message "Oh, well! Later maybe?"))
515 (run-hooks 'log-edit-done-hook)
516 (log-edit-hide-buf)
517 (unless (or log-edit-keep-buffer (not log-edit-parent-buffer))
518 (cvs-bury-buffer (current-buffer) log-edit-parent-buffer))
519 (call-interactively log-edit-callback))))
521 (defun log-edit-files ()
522 "Return the list of files that are about to be committed."
523 (ignore-errors (funcall log-edit-listfun)))
525 (defun log-edit-mode-help ()
526 "Provide help for the `log-edit-mode-map'."
527 (interactive)
528 (if (eq last-command 'log-edit-mode-help)
529 (describe-function major-mode)
530 (message "%s"
531 (substitute-command-keys
532 "Type `\\[log-edit-done]' to finish commit. Try `\\[describe-function] log-edit-done' for more help."))))
534 (defcustom log-edit-common-indent 0
535 "Minimum indentation to use in `log-edit-set-common-indentation'."
536 :group 'log-edit
537 :type 'integer)
539 (defun log-edit-set-common-indentation ()
540 "(Un)Indent the current buffer rigidly to `log-edit-common-indent'."
541 (save-excursion
542 (let ((common (point-max)))
543 (rfc822-goto-eoh)
544 (while (< (point) (point-max))
545 (if (not (looking-at "^[ \t]*$"))
546 (setq common (min common (current-indentation))))
547 (forward-line 1))
548 (rfc822-goto-eoh)
549 (indent-rigidly (point) (point-max)
550 (- log-edit-common-indent common)))))
552 (defun log-edit-show-diff ()
553 "Show the diff for the files to be committed."
554 (interactive)
555 (if (functionp log-edit-diff-function)
556 (funcall log-edit-diff-function)
557 (error "Diff functionality has not been setup")))
559 (defun log-edit-show-files ()
560 "Show the list of files to be committed."
561 (interactive)
562 (let* ((files (log-edit-files))
563 (buf (get-buffer-create log-edit-files-buf)))
564 (with-current-buffer buf
565 (log-edit-hide-buf buf 'all)
566 (setq buffer-read-only nil)
567 (erase-buffer)
568 (cvs-insert-strings files)
569 (setq buffer-read-only t)
570 (goto-char (point-min))
571 (save-selected-window
572 (cvs-pop-to-buffer-same-frame buf)
573 (shrink-window-if-larger-than-buffer)
574 (selected-window)))))
576 (defun log-edit-empty-buffer-p ()
577 "Return non-nil if the buffer is \"empty\"."
578 (or (= (point-min) (point-max))
579 (save-excursion
580 (goto-char (point-min))
581 (while (and (looking-at "^\\([a-zA-Z]+: \\)?$")
582 (zerop (forward-line 1))))
583 (eobp))))
585 (defun log-edit-insert-cvs-template ()
586 "Insert the template specified by the CVS administrator, if any.
587 This simply uses the local CVS/Template file."
588 (interactive)
589 (when (or (called-interactively-p 'interactive)
590 (log-edit-empty-buffer-p))
591 ;; Should the template take precedence over an empty Summary:,
592 ;; ie should we first erase the buffer?
593 (when (file-readable-p "CVS/Template")
594 (goto-char (point-max))
595 (insert-file-contents "CVS/Template"))))
597 (defun log-edit-insert-cvs-rcstemplate ()
598 "Insert the rcstemplate from the CVS repository.
599 This contacts the repository to get the rcstemplate file and
600 can thus take some time."
601 (interactive)
602 (when (or (called-interactively-p 'interactive)
603 (log-edit-empty-buffer-p))
604 (when (file-readable-p "CVS/Root")
605 (goto-char (point-max))
606 ;; Ignore the stderr stuff, even if it's an error.
607 (call-process "cvs" nil '(t nil) nil
608 "checkout" "-p" "CVSROOT/rcstemplate"))))
610 (defun log-edit-insert-filenames ()
611 "Insert the list of files that are to be committed."
612 (interactive)
613 (insert "Affected files: \n"
614 (mapconcat 'identity (log-edit-files) " \n")))
616 (defun log-edit-add-to-changelog ()
617 "Insert this log message into the appropriate ChangeLog file."
618 (interactive)
619 ;; Yuck!
620 (unless (string= (buffer-string) (ring-ref log-edit-comment-ring 0))
621 (ring-insert log-edit-comment-ring (buffer-string)))
622 (dolist (f (log-edit-files))
623 (let ((buffer-file-name (expand-file-name f)))
624 (save-excursion
625 (log-edit-comment-to-change-log)))))
627 (defvar log-edit-changelog-use-first nil)
629 (defvar log-edit-rewrite-fixes nil
630 "Rule to rewrite bug numbers into Fixes: headers.
631 The value should be of the form (REGEXP . REPLACEMENT)
632 where REGEXP should match the expression referring to a bug number
633 in the text, and REPLACEMENT is an expression to pass to `replace-match'
634 to build the Fixes: header.")
635 (put 'log-edit-rewrite-fixes 'safe-local-variable
636 (lambda (v) (and (stringp (car-safe v)) (stringp (cdr v)))))
638 (defun log-edit-add-field (field value)
639 (rfc822-goto-eoh)
640 (if (save-excursion (re-search-backward (concat "^" field ":\\([ \t]*\\)$")
641 nil t))
642 (replace-match (concat " " value) t t nil 1)
643 (insert field ": " value "\n" (if (looking-at "\n") "" "\n"))))
645 (defun log-edit-insert-changelog (&optional use-first)
646 "Insert a log message by looking at the ChangeLog.
647 The idea is to write your ChangeLog entries first, and then use this
648 command to commit your changes.
650 To select default log text, we:
651 - find the ChangeLog entries for the files to be checked in,
652 - verify that the top entry in the ChangeLog is on the current date
653 and by the current user; if not, we don't provide any default text,
654 - search the ChangeLog entry for paragraphs containing the names of
655 the files we're checking in, and finally
656 - use those paragraphs as the log text.
658 If the optional prefix arg USE-FIRST is given (via \\[universal-argument]),
659 or if the command is repeated a second time in a row, use the first log entry
660 regardless of user name or time."
661 (interactive "P")
662 (let ((eoh (save-excursion (rfc822-goto-eoh) (point))))
663 (when (<= (point) eoh)
664 (goto-char eoh)
665 (if (looking-at "\n") (forward-char 1))))
666 (let ((author
667 (let ((log-edit-changelog-use-first
668 (or use-first (eq last-command 'log-edit-insert-changelog))))
669 (log-edit-insert-changelog-entries (log-edit-files)))))
670 (log-edit-set-common-indentation)
671 ;; Add an Author: field if appropriate.
672 (when author (log-edit-add-field "Author" author))
673 ;; Add a Fixes: field if applicable.
674 (when (consp log-edit-rewrite-fixes)
675 (rfc822-goto-eoh)
676 (when (re-search-forward (car log-edit-rewrite-fixes) nil t)
677 (let ((start (match-beginning 0))
678 (end (match-end 0))
679 (fixes (match-substitute-replacement
680 (cdr log-edit-rewrite-fixes))))
681 (delete-region start end)
682 (log-edit-add-field "Fixes" fixes))))
683 (and log-edit-strip-single-file-name
684 (progn (rfc822-goto-eoh)
685 (if (looking-at "\n") (forward-char 1))
686 (looking-at "\\*\\s-+"))
687 (let ((start (point)))
688 (forward-line 1)
689 (when (not (re-search-forward "^\\*\\s-+" nil t))
690 (goto-char start)
691 (skip-chars-forward "^():")
692 (skip-chars-forward ": ")
693 (delete-region start (point)))))
694 (goto-char (point-min))))
696 ;;;;
697 ;;;; functions for getting commit message from ChangeLog a file...
698 ;;;; Courtesy Jim Blandy
699 ;;;;
701 (defun log-edit-narrow-changelog ()
702 "Narrow to the top page of the current buffer, a ChangeLog file.
703 Actually, the narrowed region doesn't include the date line.
704 A \"page\" in a ChangeLog file is the area between two dates."
705 (or (eq major-mode 'change-log-mode)
706 (error "log-edit-narrow-changelog: current buffer isn't a ChangeLog"))
708 (goto-char (point-min))
710 ;; Skip date line and subsequent blank lines.
711 (forward-line 1)
712 (if (looking-at "[ \t\n]*\n")
713 (goto-char (match-end 0)))
715 (let ((start (point)))
716 (forward-page 1)
717 (narrow-to-region start (point))
718 (goto-char (point-min))))
720 (defun log-edit-changelog-paragraph ()
721 "Return the bounds of the ChangeLog paragraph containing point.
722 If we are between paragraphs, return the previous paragraph."
723 (beginning-of-line)
724 (if (looking-at "^[ \t]*$")
725 (skip-chars-backward " \t\n" (point-min)))
726 (list (progn
727 (if (re-search-backward "^[ \t]*\n" nil 'or-to-limit)
728 (goto-char (match-end 0)))
729 (point))
730 (if (re-search-forward "^[ \t\n]*$" nil t)
731 (match-beginning 0)
732 (point-max))))
734 (defun log-edit-changelog-subparagraph ()
735 "Return the bounds of the ChangeLog subparagraph containing point.
736 A subparagraph is a block of non-blank lines beginning with an asterisk.
737 If we are between sub-paragraphs, return the previous subparagraph."
738 (end-of-line)
739 (if (search-backward "*" nil t)
740 (list (progn (beginning-of-line) (point))
741 (progn
742 (forward-line 1)
743 (if (re-search-forward "^[ \t]*[\n*]" nil t)
744 (match-beginning 0)
745 (point-max))))
746 (list (point) (point))))
748 (defun log-edit-changelog-entry ()
749 "Return the bounds of the ChangeLog entry containing point.
750 The variable `log-edit-changelog-full-paragraphs' decides whether an
751 \"entry\" is a paragraph or a subparagraph; see its documentation string
752 for more details."
753 (save-excursion
754 (if log-edit-changelog-full-paragraphs
755 (log-edit-changelog-paragraph)
756 (log-edit-changelog-subparagraph))))
758 (defvar user-full-name)
759 (defvar user-mail-address)
761 (defvar log-edit-author) ;Dynamically scoped.
763 (defun log-edit-changelog-ours-p ()
764 "See if ChangeLog entry at point is for the current user, today.
765 Return non-nil if it is."
766 ;; Code adapted from add-change-log-entry.
767 (let ((name (or (and (boundp 'add-log-full-name) add-log-full-name)
768 (and (fboundp 'user-full-name) (user-full-name))
769 (and (boundp 'user-full-name) user-full-name)))
770 (mail (or (and (boundp 'add-log-mailing-address) add-log-mailing-address)
771 ;;(and (fboundp 'user-mail-address) (user-mail-address))
772 (and (boundp 'user-mail-address) user-mail-address)))
773 (time (or (and (boundp 'add-log-time-format)
774 (functionp add-log-time-format)
775 (funcall add-log-time-format))
776 (format-time-string "%Y-%m-%d"))))
777 (if (null log-edit-changelog-use-first)
778 (looking-at (regexp-quote (format "%s %s <%s>" time name mail)))
779 ;; Check the author, to potentially add it as a "Author: " header.
780 (when (looking-at "[^ \t]")
781 (when (and (boundp 'log-edit-author)
782 (not (looking-at (format ".+ .+ <%s>"
783 (regexp-quote mail))))
784 (looking-at ".+ \\(.+ <.+>\\)"))
785 (let ((author (replace-regexp-in-string " " " "
786 (match-string 1))))
787 (unless (and log-edit-author
788 (string-match (regexp-quote author) log-edit-author))
789 (setq log-edit-author
790 (if log-edit-author
791 (concat log-edit-author ", " author)
792 author)))))
793 t))))
795 (defun log-edit-changelog-entries (file)
796 "Return the ChangeLog entries for FILE, and the ChangeLog they came from.
797 The return value looks like this:
798 (LOGBUFFER (ENTRYSTART ENTRYEND) ...)
799 where LOGBUFFER is the name of the ChangeLog buffer, and each
800 \(ENTRYSTART . ENTRYEND\) pair is a buffer region."
801 (let ((changelog-file-name
802 (let ((default-directory
803 (file-name-directory (expand-file-name file)))
804 (visiting-buffer (find-buffer-visiting file)))
805 ;; If there is a buffer visiting FILE, and it has a local
806 ;; value for `change-log-default-name', use that.
807 (if (and visiting-buffer
808 (local-variable-p 'change-log-default-name
809 visiting-buffer))
810 (with-current-buffer visiting-buffer
811 change-log-default-name)
812 ;; `find-change-log' uses `change-log-default-name' if set
813 ;; and sets it before exiting, so we need to work around
814 ;; that memoizing which is undesired here
815 (setq change-log-default-name nil)
816 (find-change-log)))))
817 (with-current-buffer (find-file-noselect changelog-file-name)
818 (unless (eq major-mode 'change-log-mode) (change-log-mode))
819 (goto-char (point-min))
820 (if (looking-at "\\s-*\n") (goto-char (match-end 0)))
821 (if (not (log-edit-changelog-ours-p))
822 (list (current-buffer))
823 (save-restriction
824 (log-edit-narrow-changelog)
825 (goto-char (point-min))
827 ;; Search for the name of FILE relative to the ChangeLog. If that
828 ;; doesn't occur anywhere, they're not using full relative
829 ;; filenames in the ChangeLog, so just look for FILE; we'll accept
830 ;; some false positives.
831 (let ((pattern (file-relative-name
832 file (file-name-directory changelog-file-name))))
833 (if (or (string= pattern "")
834 (not (save-excursion
835 (search-forward pattern nil t))))
836 (setq pattern (file-name-nondirectory file)))
838 (setq pattern (concat "\\(^\\|[^[:alnum:]]\\)"
839 (regexp-quote pattern)
840 "\\($\\|[^[:alnum:]]\\)"))
842 (let (texts
843 (pos (point)))
844 (while (and (not (eobp)) (re-search-forward pattern nil t))
845 (let ((entry (log-edit-changelog-entry)))
846 (if (< (elt entry 1) (max (1+ pos) (point)))
847 ;; This is not relevant, actually.
849 (push entry texts))
850 ;; Make sure we make progress.
851 (setq pos (max (1+ pos) (elt entry 1)))
852 (goto-char pos)))
854 (cons (current-buffer) texts))))))))
856 (defun log-edit-changelog-insert-entries (buffer beg end &rest files)
857 "Insert the text from BUFFER between BEG and END.
858 Rename relative filenames in the ChangeLog entry as FILES."
859 (let ((opoint (point))
860 (log-name (buffer-file-name buffer))
861 (case-fold-search nil)
862 bound)
863 (insert-buffer-substring buffer beg end)
864 (setq bound (point-marker))
865 (when log-name
866 (dolist (f files)
867 (save-excursion
868 (goto-char opoint)
869 (when (re-search-forward
870 (concat "\\(^\\|[ \t]\\)\\("
871 (file-relative-name f (file-name-directory log-name))
872 "\\)[, :\n]")
873 bound t)
874 (replace-match f t t nil 2)))))
875 ;; Eliminate tabs at the beginning of the line.
876 (save-excursion
877 (goto-char opoint)
878 (while (re-search-forward "^\\(\t+\\)" bound t)
879 (replace-match "")))))
881 (defun log-edit-insert-changelog-entries (files)
882 "Given a list of files FILES, insert the ChangeLog entries for them."
883 (let ((log-entries nil)
884 (log-edit-author nil))
885 ;; Note that any ChangeLog entry can apply to more than one file.
886 ;; Here we construct a log-entries list with elements of the form
887 ;; ((LOGBUFFER ENTRYSTART ENTRYEND) FILE1 FILE2...)
888 (dolist (file files)
889 (let* ((entries (log-edit-changelog-entries file))
890 (buf (car entries))
891 key entry)
892 (dolist (region (cdr entries))
893 (setq key (cons buf region))
894 (if (setq entry (assoc key log-entries))
895 (setcdr entry (append (cdr entry) (list file)))
896 (push (list key file) log-entries)))))
897 ;; Now map over log-entries, and extract the strings.
898 (dolist (log-entry (nreverse log-entries))
899 (apply 'log-edit-changelog-insert-entries
900 (append (car log-entry) (cdr log-entry)))
901 (insert "\n"))
902 log-edit-author))
904 (defun log-edit-extract-headers (headers comment)
905 "Extract headers from COMMENT to form command line arguments.
906 HEADERS should be an alist with elements of the form (HEADER . CMDARG)
907 associating header names to the corresponding cmdline option name and the
908 result is then a list of the form (MSG CMDARG1 HDRTEXT1 CMDARG2 HDRTEXT2...).
909 where MSG is the remaining text from STRING.
910 If \"Summary\" is not in HEADERS, then the \"Summary\" header is extracted
911 anyway and put back as the first line of MSG."
912 (with-temp-buffer
913 (insert comment)
914 (rfc822-goto-eoh)
915 (narrow-to-region (point-min) (point))
916 (let ((case-fold-search t)
917 (summary ())
918 (res ()))
919 (dolist (header (if (assoc "Summary" headers) headers
920 (cons '("Summary" . t) headers)))
921 (goto-char (point-min))
922 (while (re-search-forward (concat "^" (car header)
923 ":" log-edit-header-contents-regexp)
924 nil t)
925 (if (eq t (cdr header))
926 (setq summary (match-string 1))
927 (push (match-string 1) res)
928 (push (or (cdr header) (car header)) res))
929 (replace-match "" t t)))
930 ;; Remove header separator if the header is empty.
931 (widen)
932 (goto-char (point-min))
933 (when (looking-at "\\([ \t]*\n\\)+")
934 (delete-region (match-beginning 0) (match-end 0)))
935 (if summary (insert summary "\n"))
936 (cons (buffer-string) res))))
938 (provide 'log-edit)
940 ;;; log-edit.el ends here