* lisp/xml.el: Handle entity and character reference expansion correctly.
[emacs.git] / lisp / vc / log-edit.el
blob5ecd5c44b2e341f39eb267df46727b5486ec7f7c
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 (define-obsolete-variable-alias 'vc-comment-ring 'log-edit-comment-ring "22.1")
199 (defvar log-edit-comment-ring (make-ring log-edit-maximum-comment-ring-size))
200 (define-obsolete-variable-alias 'vc-comment-ring-index
201 'log-edit-comment-ring-index "22.1")
202 (defvar log-edit-comment-ring-index nil)
203 (defvar log-edit-last-comment-match "")
205 (defun log-edit-new-comment-index (stride len)
206 "Return the comment index STRIDE elements from the current one.
207 LEN is the length of `log-edit-comment-ring'."
208 (mod (cond
209 (log-edit-comment-ring-index (+ log-edit-comment-ring-index stride))
210 ;; Initialize the index on the first use of this command
211 ;; so that the first M-p gets index 0, and the first M-n gets
212 ;; index -1.
213 ((> stride 0) (1- stride))
214 (t stride))
215 len))
217 (defun log-edit-previous-comment (arg)
218 "Cycle backwards through comment history.
219 With a numeric prefix ARG, go back ARG comments."
220 (interactive "*p")
221 (let ((len (ring-length log-edit-comment-ring)))
222 (if (<= len 0)
223 (progn (message "Empty comment ring") (ding))
224 ;; Don't use `erase-buffer' because we don't want to `widen'.
225 (delete-region (point-min) (point-max))
226 (setq log-edit-comment-ring-index (log-edit-new-comment-index arg len))
227 (message "Comment %d" (1+ log-edit-comment-ring-index))
228 (insert (ring-ref log-edit-comment-ring log-edit-comment-ring-index)))))
230 (defun log-edit-next-comment (arg)
231 "Cycle forwards through comment history.
232 With a numeric prefix ARG, go forward ARG comments."
233 (interactive "*p")
234 (log-edit-previous-comment (- arg)))
236 (defun log-edit-comment-search-backward (str &optional stride)
237 "Search backwards through comment history for substring match of STR.
238 If the optional argument STRIDE is present, that is a step-width to use
239 when going through the comment ring."
240 ;; Why substring rather than regexp ? -sm
241 (interactive
242 (list (read-string "Comment substring: " nil nil log-edit-last-comment-match)))
243 (unless stride (setq stride 1))
244 (if (string= str "")
245 (setq str log-edit-last-comment-match)
246 (setq log-edit-last-comment-match str))
247 (let* ((str (regexp-quote str))
248 (len (ring-length log-edit-comment-ring))
249 (n (log-edit-new-comment-index stride len)))
250 (while (progn (when (or (>= n len) (< n 0)) (error "Not found"))
251 (not (string-match str (ring-ref log-edit-comment-ring n))))
252 (setq n (+ n stride)))
253 (setq log-edit-comment-ring-index n)
254 (log-edit-previous-comment 0)))
256 (defun log-edit-comment-search-forward (str)
257 "Search forwards through comment history for a substring match of STR."
258 (interactive
259 (list (read-string "Comment substring: " nil nil log-edit-last-comment-match)))
260 (log-edit-comment-search-backward str -1))
262 (defun log-edit-comment-to-change-log (&optional whoami file-name)
263 "Enter last VC comment into the change log for the current file.
264 WHOAMI (interactive prefix) non-nil means prompt for user name
265 and site. FILE-NAME is the name of the change log; if nil, use
266 `change-log-default-name'.
268 This may be useful as a `log-edit-checkin-hook' to update change logs
269 automatically."
270 (interactive (if current-prefix-arg
271 (list current-prefix-arg
272 (prompt-for-change-log-name))))
273 (let (;; Extract the comment first so we get any error before doing anything.
274 (comment (ring-ref log-edit-comment-ring 0))
275 ;; Don't let add-change-log-entry insert a defun name.
276 (add-log-current-defun-function 'ignore)
277 end)
278 ;; Call add-log to do half the work.
279 (add-change-log-entry whoami file-name t t)
280 ;; Insert the VC comment, leaving point before it.
281 (setq end (save-excursion (insert comment) (point-marker)))
282 (if (looking-at "\\s *\\s(")
283 ;; It starts with an open-paren, as in "(foo): Frobbed."
284 ;; So remove the ": " add-log inserted.
285 (delete-char -2))
286 ;; Canonicalize the white space between the file name and comment.
287 (just-one-space)
288 ;; Indent rest of the text the same way add-log indented the first line.
289 (let ((indentation (current-indentation)))
290 (save-excursion
291 (while (< (point) end)
292 (forward-line 1)
293 (indent-to indentation))
294 (setq end (point))))
295 ;; Fill the inserted text, preserving open-parens at bol.
296 (let ((paragraph-start (concat paragraph-start "\\|\\s *\\s(")))
297 (beginning-of-line)
298 (fill-region (point) end))
299 ;; Canonicalize the white space at the end of the entry so it is
300 ;; separated from the next entry by a single blank line.
301 (skip-syntax-forward " " end)
302 (delete-char (- (skip-syntax-backward " ")))
303 (or (eobp) (looking-at "\n\n")
304 (insert "\n"))))
306 ;; Compatibility with old names.
307 (define-obsolete-function-alias 'vc-previous-comment 'log-edit-previous-comment "22.1")
308 (define-obsolete-function-alias 'vc-next-comment 'log-edit-next-comment "22.1")
309 (define-obsolete-function-alias 'vc-comment-search-reverse 'log-edit-comment-search-backward "22.1")
310 (define-obsolete-function-alias 'vc-comment-search-forward 'log-edit-comment-search-forward "22.1")
311 (define-obsolete-function-alias 'vc-comment-to-change-log 'log-edit-comment-to-change-log "22.1")
314 ;;; Actual code
317 (defface log-edit-summary '((t :inherit font-lock-function-name-face))
318 "Face for the summary in `log-edit-mode' buffers.")
320 (defface log-edit-header '((t :inherit font-lock-keyword-face))
321 "Face for the headers in `log-edit-mode' buffers.")
323 (defface log-edit-unknown-header '((t :inherit font-lock-comment-face))
324 "Face for unknown headers in `log-edit-mode' buffers.")
326 (defvar log-edit-headers-alist '(("Summary" . log-edit-summary)
327 ("Fixes") ("Author"))
328 "AList of known headers and the face to use to highlight them.")
330 (defconst log-edit-header-contents-regexp
331 "[ \t]*\\(.*\\(\n[ \t].*\\)*\\)\n?")
333 (defun log-edit-match-to-eoh (_limit)
334 ;; FIXME: copied from message-match-to-eoh.
335 (let ((start (point)))
336 (rfc822-goto-eoh)
337 ;; Typical situation: some temporary change causes the header to be
338 ;; incorrect, so EOH comes earlier than intended: the last lines of the
339 ;; intended headers are now not considered part of the header any more,
340 ;; so they don't have the multiline property set. When the change is
341 ;; completed and the header has its correct shape again, the lack of the
342 ;; multiline property means we won't rehighlight the last lines of
343 ;; the header.
344 (if (< (point) start)
345 nil ;No header within start..limit.
346 ;; Here we disregard LIMIT so that we may extend the area again.
347 (set-match-data (list start (point)))
348 (point))))
350 (defvar log-edit-font-lock-keywords
351 ;; Copied/inspired by message-font-lock-keywords.
352 `((log-edit-match-to-eoh
353 (,(concat "^\\(\\([[:alpha:]]+\\):\\)" log-edit-header-contents-regexp)
354 (progn (goto-char (match-beginning 0)) (match-end 0)) nil
355 (1 (if (assoc (match-string 2) log-edit-headers-alist)
356 'log-edit-header
357 'log-edit-unknown-header)
358 nil lax)
359 ;; From `log-edit-header-contents-regexp':
360 (3 (or (cdr (assoc (match-string 2) log-edit-headers-alist))
361 'log-edit-header)
362 nil lax)))))
364 (defvar log-edit-font-lock-gnu-style nil
365 "If non-nil, highlight common failures to follow the GNU coding standards.")
366 (put 'log-edit-font-lock-gnu-style 'safe-local-variable 'booleanp)
368 (defconst log-edit-font-lock-gnu-keywords
369 ;; Use
370 ;; * foo.el (bla, bli)
371 ;; (blo, blu): Toto.
372 ;; Rather than
373 ;; * foo.el (bla, bli,
374 ;; blo, blu): Toto.
375 '(("^[ \t]*\\(?:\\* .*\\)?\\(([^\n)]*,\\s-*\\)$"
376 (1 '(face font-lock-warning-face
377 help-echo "Continue function lists with \")\\n(\".") t))
378 ;; Don't leave a lone word on a single line.
379 ;;("^\\s-*\\(\\S-*[^\n:)]\\)\\s-*$" (1 font-lock-warning-face t))
380 ;; Don't cut a sentence right after the first word (better to move
381 ;; the sentence on the next line, then).
382 ;;("[.:]\\s-+\\(\\sw+\\)\\s-*$" (1 font-lock-warning-face t))
383 ;; Change Log entries should use present tense.
384 ("):[ \t\n]*[[:alpha:]]+\\(ed\\)\\>"
385 (1 '(face font-lock-warning-face help-echo "Use present tense.") t))
386 ;; Change log entries start with a capital letter.
387 ("): [a-z]" (0 '(face font-lock-warning-face help-echo "Capitalize.") t))
388 ("[^[:upper:]]\\(\\. [[:upper:]]\\)"
389 (1 '(face font-lock-warning-face
390 help-echo "Use two spaces to end a sentence") t))
391 ("^("
392 (0 (let ((beg (max (point-min) (- (match-beginning 0) 2))))
393 (put-text-property beg (match-end 0) 'font-lock-multiline t)
394 (if (eq (char-syntax (char-after beg)) ?w)
395 '(face font-lock-warning-face
396 help-echo "Punctuate previous line.")))
400 (defun log-edit-font-lock-keywords ()
401 (if log-edit-font-lock-gnu-style
402 (append log-edit-font-lock-keywords
403 log-edit-font-lock-gnu-keywords)
404 log-edit-font-lock-keywords))
406 ;;;###autoload
407 (defun log-edit (callback &optional setup params buffer mode &rest _ignore)
408 "Setup a buffer to enter a log message.
409 \\<log-edit-mode-map>The buffer will be put in mode MODE or `log-edit-mode'
410 if MODE is nil.
411 If SETUP is non-nil, the buffer is then erased and `log-edit-hook' is run.
412 Mark and point will be set around the entire contents of the buffer so
413 that it is easy to kill the contents of the buffer with \\[kill-region].
414 Once you're done editing the message, pressing \\[log-edit-done] will call
415 `log-edit-done' which will end up calling CALLBACK to do the actual commit.
417 PARAMS if non-nil is an alist. Possible keys and associated values:
418 `log-edit-listfun' -- function taking no arguments that returns the list of
419 files that are concerned by the current operation (using relative names);
420 `log-edit-diff-function' -- function taking no arguments that
421 displays a diff of the files concerned by the current operation.
423 If BUFFER is non-nil `log-edit' will jump to that buffer, use it to edit the
424 log message and go back to the current buffer when done. Otherwise, it
425 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-extract-headers (headers comment)
906 "Extract headers from COMMENT to form command line arguments.
907 HEADERS should be an alist with elements of the form (HEADER . CMDARG)
908 associating header names to the corresponding cmdline option name and the
909 result is then a list of the form (MSG CMDARG1 HDRTEXT1 CMDARG2 HDRTEXT2...).
910 where MSG is the remaining text from STRING.
911 If \"Summary\" is not in HEADERS, then the \"Summary\" header is extracted
912 anyway and put back as the first line of MSG."
913 (with-temp-buffer
914 (insert comment)
915 (rfc822-goto-eoh)
916 (narrow-to-region (point-min) (point))
917 (let ((case-fold-search t)
918 (summary ())
919 (res ()))
920 (dolist (header (if (assoc "Summary" headers) headers
921 (cons '("Summary" . t) headers)))
922 (goto-char (point-min))
923 (while (re-search-forward (concat "^" (car header)
924 ":" log-edit-header-contents-regexp)
925 nil t)
926 (if (eq t (cdr header))
927 (setq summary (match-string 1))
928 (push (match-string 1) res)
929 (push (or (cdr header) (car header)) res))
930 (replace-match "" t t)))
931 ;; Remove header separator if the header is empty.
932 (widen)
933 (goto-char (point-min))
934 (when (looking-at "\\([ \t]*\n\\)+")
935 (delete-region (match-beginning 0) (match-end 0)))
936 (if summary (insert summary "\n"))
937 (cons (buffer-string) res))))
939 (provide 'log-edit)
941 ;;; log-edit.el ends here