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