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