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