Merge from emacs--rel--22
[emacs.git] / lisp / log-edit.el
blob11111286d59148b18c4db003ab72ae153e97d2c1
1 ;;; log-edit.el --- Major mode for editing CVS commit messages
3 ;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
6 ;; Author: Stefan Monnier <monnier@iro.umontreal.ca>
7 ;; Keywords: pcl-cvs cvs commit log
9 ;; This file is part of GNU Emacs.
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, or (at your option)
14 ;; any later version.
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.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
26 ;;; Commentary:
28 ;; Todo:
30 ;; - Move in VC's code
31 ;; - Add compatibility for VC's hook variables
33 ;;; Code:
35 (eval-when-compile (require 'cl))
36 (require 'add-log) ; for all the ChangeLog goodies
37 (require 'pcvs-util)
38 (require 'ring)
40 ;;;;
41 ;;;; Global Variables
42 ;;;;
44 (defgroup log-edit nil
45 "Major mode for editing RCS and CVS commit messages."
46 :group 'pcl-cvs
47 :group 'vc ; It's used by VC.
48 :version "21.1"
49 :prefix "log-edit-")
51 ;; compiler pacifiers
52 (defvar cvs-buffer)
55 ;; The main keymap
57 (easy-mmode-defmap log-edit-mode-map
58 `(("\C-c\C-c" . log-edit-done)
59 ("\C-c\C-a" . log-edit-insert-changelog)
60 ("\C-c\C-d" . log-edit-show-diff)
61 ("\C-c\C-f" . log-edit-show-files)
62 ("\M-n" . log-edit-next-comment)
63 ("\M-p" . log-edit-previous-comment)
64 ("\M-r" . log-edit-comment-search-backward)
65 ("\M-s" . log-edit-comment-search-forward)
66 ("\C-c?" . log-edit-mode-help))
67 "Keymap for the `log-edit-mode' (to edit version control log messages)."
68 :group 'log-edit)
70 ;; Compatibility with old names. Should we bother ?
71 (defvar vc-log-mode-map log-edit-mode-map)
72 (defvar vc-log-entry-mode vc-log-mode-map)
74 (easy-menu-define log-edit-menu log-edit-mode-map
75 "Menu used for `log-edit-mode'."
76 '("Log-Edit"
77 ["Done" log-edit-done
78 :help "Exit log-edit and proceed with the actual action."]
79 "--"
80 ["Insert ChangeLog" log-edit-insert-changelog
81 :help "Insert a log message by looking at the ChangeLog"]
82 ["Add to ChangeLog" log-edit-add-to-changelog
83 :help "Insert this log message into the appropriate ChangeLog file"]
84 "--"
85 ["Show diff" log-edit-show-diff
86 :help "Show the diff for the files to be committed."]
87 ["List files" log-edit-show-files
88 :help "Show the list of relevant files."]
89 "--"
90 ["Previous comment" log-edit-previous-comment
91 :help "Cycle backwards through comment history"]
92 ["Next comment" log-edit-next-comment
93 :help "Cycle forwards through comment history."]
94 ["Search comment forward" log-edit-comment-search-forward
95 :help "Search forwards through comment history for a substring match of str"]
96 ["Search comment backward" log-edit-comment-search-backward
97 :help "Search backwards through comment history for substring match of str"]))
99 (defcustom log-edit-confirm 'changed
100 "*If non-nil, `log-edit-done' will request confirmation.
101 If 'changed, only request confirmation if the list of files has
102 changed since the beginning of the log-edit session."
103 :group 'log-edit
104 :type '(choice (const changed) (const t) (const nil)))
106 (defcustom log-edit-keep-buffer nil
107 "*If non-nil, don't hide the buffer after `log-edit-done'."
108 :group 'log-edit
109 :type 'boolean)
111 (defvar cvs-commit-buffer-require-final-newline t)
112 (make-obsolete-variable 'cvs-commit-buffer-require-final-newline
113 'log-edit-require-final-newline
114 "21.1")
116 (defcustom log-edit-require-final-newline
117 cvs-commit-buffer-require-final-newline
118 "*Enforce a newline at the end of commit log messages.
119 Enforce it silently if t, query if non-nil and don't do anything if nil."
120 :group 'log-edit
121 :type '(choice (const ask) (const t) (const nil)))
123 (defcustom log-edit-setup-invert nil
124 "*Non-nil means `log-edit' should invert the meaning of its SETUP arg.
125 If SETUP is 'force, this variable has no effect."
126 :group 'log-edit
127 :type 'boolean)
129 (defcustom log-edit-hook '(log-edit-insert-cvs-template
130 log-edit-insert-changelog)
131 "*Hook run at the end of `log-edit'."
132 :group 'log-edit
133 :type '(hook :options (log-edit-insert-changelog
134 log-edit-insert-cvs-rcstemplate
135 log-edit-insert-cvs-template
136 log-edit-insert-filenames)))
138 (defcustom log-edit-mode-hook (if (boundp 'vc-log-mode-hook) vc-log-mode-hook)
139 "*Hook run when entering `log-edit-mode'."
140 :group 'log-edit
141 :type 'hook)
143 (defcustom log-edit-done-hook nil
144 "*Hook run before doing the actual commit.
145 This hook can be used to cleanup the message, enforce various
146 conventions, or to allow recording the message in some other database,
147 such as a bug-tracking system. The list of files about to be committed
148 can be obtained from `log-edit-files'."
149 :group 'log-edit
150 :type '(hook :options (log-edit-set-common-indentation
151 log-edit-add-to-changelog)))
153 (defvar cvs-changelog-full-paragraphs t)
154 (make-obsolete-variable 'cvs-changelog-full-paragraphs
155 'log-edit-changelog-full-paragraphs
156 "21.1")
158 (defvar log-edit-changelog-full-paragraphs cvs-changelog-full-paragraphs
159 "*If non-nil, include full ChangeLog paragraphs in the log.
160 This may be set in the ``local variables'' section of a ChangeLog, to
161 indicate the policy for that ChangeLog.
163 A ChangeLog paragraph is a bunch of log text containing no blank lines;
164 a paragraph usually describes a set of changes with a single purpose,
165 but perhaps spanning several functions in several files. Changes in
166 different paragraphs are unrelated.
168 You could argue that the log entry for a file should contain the
169 full ChangeLog paragraph mentioning the change to the file, even though
170 it may mention other files, because that gives you the full context you
171 need to understand the change. This is the behavior you get when this
172 variable is set to t.
174 On the other hand, you could argue that the log entry for a change
175 should contain only the text for the changes which occurred in that
176 file, because the log is per-file. This is the behavior you get
177 when this variable is set to nil.")
179 ;;;; Internal global or buffer-local vars
181 (defconst log-edit-files-buf "*log-edit-files*")
182 (defvar log-edit-initial-files nil)
183 (defvar log-edit-callback nil)
184 (defvar log-edit-diff-function nil)
185 (defvar log-edit-listfun nil)
186 (defvar log-edit-parent-buffer nil)
188 ;;; Originally taken from VC-Log mode
190 (defconst log-edit-maximum-comment-ring-size 32
191 "Maximum number of saved comments in the comment ring.")
192 (defvar log-edit-comment-ring (make-ring log-edit-maximum-comment-ring-size))
193 (defvar log-edit-comment-ring-index nil)
194 (defvar log-edit-last-comment-match "")
196 (defun log-edit-new-comment-index (stride len)
197 "Return the comment index STRIDE elements from the current one.
198 LEN is the length of `log-edit-comment-ring'."
199 (mod (cond
200 (log-edit-comment-ring-index (+ log-edit-comment-ring-index stride))
201 ;; Initialize the index on the first use of this command
202 ;; so that the first M-p gets index 0, and the first M-n gets
203 ;; index -1.
204 ((> stride 0) (1- stride))
205 (t stride))
206 len))
208 (defun log-edit-previous-comment (arg)
209 "Cycle backwards through comment history.
210 With a numeric prefix ARG, go back ARG comments."
211 (interactive "*p")
212 (let ((len (ring-length log-edit-comment-ring)))
213 (if (<= len 0)
214 (progn (message "Empty comment ring") (ding))
215 ;; Don't use `erase-buffer' because we don't want to `widen'.
216 (delete-region (point-min) (point-max))
217 (setq log-edit-comment-ring-index (log-edit-new-comment-index arg len))
218 (message "Comment %d" (1+ log-edit-comment-ring-index))
219 (insert (ring-ref log-edit-comment-ring log-edit-comment-ring-index)))))
221 (defun log-edit-next-comment (arg)
222 "Cycle forwards through comment history.
223 With a numeric prefix ARG, go forward ARG comments."
224 (interactive "*p")
225 (log-edit-previous-comment (- arg)))
227 (defun log-edit-comment-search-backward (str &optional stride)
228 "Search backwards through comment history for substring match of STR.
229 If the optional argument STRIDE is present, that is a step-width to use
230 when going through the comment ring."
231 ;; Why substring rather than regexp ? -sm
232 (interactive
233 (list (read-string "Comment substring: " nil nil log-edit-last-comment-match)))
234 (unless stride (setq stride 1))
235 (if (string= str "")
236 (setq str log-edit-last-comment-match)
237 (setq log-edit-last-comment-match str))
238 (let* ((str (regexp-quote str))
239 (len (ring-length log-edit-comment-ring))
240 (n (log-edit-new-comment-index stride len)))
241 (while (progn (when (or (>= n len) (< n 0)) (error "Not found"))
242 (not (string-match str (ring-ref log-edit-comment-ring n))))
243 (setq n (+ n stride)))
244 (setq log-edit-comment-ring-index n)
245 (log-edit-previous-comment 0)))
247 (defun log-edit-comment-search-forward (str)
248 "Search forwards through comment history for a substring match of STR."
249 (interactive
250 (list (read-string "Comment substring: " nil nil log-edit-last-comment-match)))
251 (log-edit-comment-search-backward str -1))
253 (defun log-edit-comment-to-change-log (&optional whoami file-name)
254 "Enter last VC comment into the change log for the current file.
255 WHOAMI (interactive prefix) non-nil means prompt for user name
256 and site. FILE-NAME is the name of the change log; if nil, use
257 `change-log-default-name'.
259 This may be useful as a `log-edit-checkin-hook' to update change logs
260 automatically."
261 (interactive (if current-prefix-arg
262 (list current-prefix-arg
263 (prompt-for-change-log-name))))
264 (let (;; Extract the comment first so we get any error before doing anything.
265 (comment (ring-ref log-edit-comment-ring 0))
266 ;; Don't let add-change-log-entry insert a defun name.
267 (add-log-current-defun-function 'ignore)
268 end)
269 ;; Call add-log to do half the work.
270 (add-change-log-entry whoami file-name t t)
271 ;; Insert the VC comment, leaving point before it.
272 (setq end (save-excursion (insert comment) (point-marker)))
273 (if (looking-at "\\s *\\s(")
274 ;; It starts with an open-paren, as in "(foo): Frobbed."
275 ;; So remove the ": " add-log inserted.
276 (delete-char -2))
277 ;; Canonicalize the white space between the file name and comment.
278 (just-one-space)
279 ;; Indent rest of the text the same way add-log indented the first line.
280 (let ((indentation (current-indentation)))
281 (save-excursion
282 (while (< (point) end)
283 (forward-line 1)
284 (indent-to indentation))
285 (setq end (point))))
286 ;; Fill the inserted text, preserving open-parens at bol.
287 (let ((paragraph-start (concat paragraph-start "\\|\\s *\\s(")))
288 (beginning-of-line)
289 (fill-region (point) end))
290 ;; Canonicalize the white space at the end of the entry so it is
291 ;; separated from the next entry by a single blank line.
292 (skip-syntax-forward " " end)
293 (delete-char (- (skip-syntax-backward " ")))
294 (or (eobp) (looking-at "\n\n")
295 (insert "\n"))))
297 ;; Compatibility with old names.
298 (define-obsolete-variable-alias 'vc-comment-ring 'log-edit-comment-ring "22.1")
299 (define-obsolete-variable-alias 'vc-comment-ring-index 'log-edit-comment-ring-index "22.1")
300 (define-obsolete-function-alias 'vc-previous-comment 'log-edit-previous-comment "22.1")
301 (define-obsolete-function-alias 'vc-next-comment 'log-edit-next-comment "22.1")
302 (define-obsolete-function-alias 'vc-comment-search-reverse 'log-edit-comment-search-backward "22.1")
303 (define-obsolete-function-alias 'vc-comment-search-forward 'log-edit-comment-search-forward "22.1")
304 (define-obsolete-function-alias 'vc-comment-to-change-log 'log-edit-comment-to-change-log "22.1")
307 ;;; Actual code
310 (defvar log-edit-font-lock-keywords
311 '(("\\`\\(Summary:\\)\\(.*\\)"
312 (1 font-lock-keyword-face)
313 (2 font-lock-function-name-face))))
315 ;;;###autoload
316 (defun log-edit (callback &optional setup params buffer &rest ignore)
317 "Setup a buffer to enter a log message.
318 \\<log-edit-mode-map>The buffer will be put in `log-edit-mode'.
319 If SETUP is non-nil, the buffer is then erased and `log-edit-hook' is run.
320 Mark and point will be set around the entire contents of the buffer so
321 that it is easy to kill the contents of the buffer with \\[kill-region].
322 Once you're done editing the message, pressing \\[log-edit-done] will call
323 `log-edit-done' which will end up calling CALLBACK to do the actual commit.
325 PARAMS if non-nil is an alist. Possible keys and associated values:
326 `log-edit-listfun' -- function taking no arguments that returns the list of
327 files that are concerned by the current operation (using relative names);
328 `log-edit-diff-function' -- function taking no arguments that
329 displays a diff of the files concerned by the current operation.
331 If BUFFER is non-nil `log-edit' will jump to that buffer, use it to edit the
332 log message and go back to the current buffer when done. Otherwise, it
333 uses the current buffer."
334 (let ((parent (current-buffer)))
335 (if buffer (pop-to-buffer buffer))
336 (when (and log-edit-setup-invert (not (eq setup 'force)))
337 (setq setup (not setup)))
338 (when setup (erase-buffer))
339 (log-edit-mode)
340 (set (make-local-variable 'log-edit-callback) callback)
341 (if (listp params)
342 (dolist (crt params)
343 (set (make-local-variable (car crt)) (cdr crt)))
344 ;; For backward compatibility with log-edit up to version 22.2
345 ;; accept non-list PARAMS to mean `log-edit-list'.
346 (set (make-local-variable 'log-edit-listfun) params))
348 (if buffer (set (make-local-variable 'log-edit-parent-buffer) parent))
349 (set (make-local-variable 'log-edit-initial-files) (log-edit-files))
350 (when setup (run-hooks 'log-edit-hook))
351 (goto-char (point-min)) (push-mark (point-max))
352 (message "%s" (substitute-command-keys
353 "Press \\[log-edit-done] when you are done editing."))))
355 (define-derived-mode log-edit-mode text-mode "Log-Edit"
356 "Major mode for editing version-control log messages.
357 When done editing the log entry, just type \\[log-edit-done] which
358 will trigger the actual commit of the file(s).
359 Several other handy support commands are provided of course and
360 the package from which this is used might also provide additional
361 commands (under C-x v for VC, for example).
363 \\{log-edit-mode-map}"
364 (set (make-local-variable 'font-lock-defaults)
365 '(log-edit-font-lock-keywords t))
366 (make-local-variable 'log-edit-comment-ring-index))
368 (defun log-edit-hide-buf (&optional buf where)
369 (when (setq buf (get-buffer (or buf log-edit-files-buf)))
370 (let ((win (get-buffer-window buf where)))
371 (if win (ignore-errors (delete-window win))))
372 (bury-buffer buf)))
374 (defun log-edit-done ()
375 "Finish editing the log message and commit the files.
376 If you want to abort the commit, simply delete the buffer."
377 (interactive)
378 ;; Get rid of trailing empty lines
379 (goto-char (point-max))
380 (skip-syntax-backward " ")
381 (when (equal (char-after) ?\n) (forward-char 1))
382 (delete-region (point) (point-max))
383 ;; Check for final newline
384 (if (and (> (point-max) (point-min))
385 (/= (char-before (point-max)) ?\n)
386 (or (eq log-edit-require-final-newline t)
387 (and log-edit-require-final-newline
388 (y-or-n-p
389 (format "Buffer %s does not end in newline. Add one? "
390 (buffer-name))))))
391 (save-excursion
392 (goto-char (point-max))
393 (insert ?\n)))
394 (let ((comment (buffer-string)))
395 (when (or (ring-empty-p log-edit-comment-ring)
396 (not (equal comment (ring-ref log-edit-comment-ring 0))))
397 (ring-insert log-edit-comment-ring comment)))
398 (let ((win (get-buffer-window log-edit-files-buf)))
399 (if (and log-edit-confirm
400 (not (and (eq log-edit-confirm 'changed)
401 (equal (log-edit-files) log-edit-initial-files)))
402 (progn
403 (log-edit-show-files)
404 (not (y-or-n-p "Really commit? "))))
405 (progn (when (not win) (log-edit-hide-buf))
406 (message "Oh, well! Later maybe?"))
407 (run-hooks 'log-edit-done-hook)
408 (log-edit-hide-buf)
409 (unless (or log-edit-keep-buffer (not log-edit-parent-buffer))
410 (cvs-bury-buffer (current-buffer) log-edit-parent-buffer))
411 (call-interactively log-edit-callback))))
413 (defun log-edit-files ()
414 "Return the list of files that are about to be committed."
415 (ignore-errors (funcall log-edit-listfun)))
417 (defun log-edit-mode-help ()
418 "Provide help for the `log-edit-mode-map'."
419 (interactive)
420 (if (eq last-command 'log-edit-mode-help)
421 (describe-function major-mode)
422 (message "%s"
423 (substitute-command-keys
424 "Type `\\[log-edit-done]' to finish commit. Try `\\[describe-function] log-edit-done' for more help."))))
426 (defcustom log-edit-common-indent 0
427 "Minimum indentation to use in `log-edit-set-common-indentation'."
428 :group 'log-edit
429 :type 'integer)
431 (defun log-edit-set-common-indentation ()
432 "(Un)Indent the current buffer rigidly to `log-edit-common-indent'."
433 (save-excursion
434 (let ((common (point-max)))
435 (goto-char (point-min))
436 (while (< (point) (point-max))
437 (if (not (looking-at "^[ \t]*$"))
438 (setq common (min common (current-indentation))))
439 (forward-line 1))
440 (indent-rigidly (point-min) (point-max)
441 (- log-edit-common-indent common)))))
443 (defun log-edit-show-diff ()
444 "Show the diff for the files to be committed."
445 (interactive)
446 (if (functionp log-edit-diff-function)
447 (funcall log-edit-diff-function)
448 (error "Diff functionality has not been setup")))
450 (defun log-edit-show-files ()
451 "Show the list of files to be committed."
452 (interactive)
453 (let* ((files (log-edit-files))
454 (buf (get-buffer-create log-edit-files-buf)))
455 (with-current-buffer buf
456 (log-edit-hide-buf buf 'all)
457 (setq buffer-read-only nil)
458 (erase-buffer)
459 (cvs-insert-strings files)
460 (setq buffer-read-only t)
461 (goto-char (point-min))
462 (save-selected-window
463 (cvs-pop-to-buffer-same-frame buf)
464 (shrink-window-if-larger-than-buffer)
465 (selected-window)))))
467 (defun log-edit-insert-cvs-template ()
468 "Insert the template specified by the CVS administrator, if any.
469 This simply uses the local CVS/Template file."
470 (interactive)
471 (when (or (interactive-p) (= (point-min) (point-max)))
472 (when (file-readable-p "CVS/Template")
473 (insert-file-contents "CVS/Template"))))
475 (defun log-edit-insert-cvs-rcstemplate ()
476 "Insert the rcstemplate from the CVS repository.
477 This contacts the repository to get the rcstemplate file and
478 can thus take some time."
479 (interactive)
480 (when (or (interactive-p) (= (point-min) (point-max)))
481 (when (file-readable-p "CVS/Root")
482 ;; Ignore the stderr stuff, even if it's an error.
483 (call-process "cvs" nil '(t nil) nil
484 "checkout" "-p" "CVSROOT/rcstemplate"))))
486 (defun log-edit-insert-filenames ()
487 "Insert the list of files that are to be committed."
488 (interactive)
489 (insert "Affected files: \n"
490 (mapconcat 'identity (log-edit-files) " \n")))
492 (defun log-edit-add-to-changelog ()
493 "Insert this log message into the appropriate ChangeLog file."
494 (interactive)
495 ;; Yuck!
496 (unless (string= (buffer-string) (ring-ref log-edit-comment-ring 0))
497 (ring-insert log-edit-comment-ring (buffer-string)))
498 (dolist (f (log-edit-files))
499 (let ((buffer-file-name (expand-file-name f)))
500 (save-excursion
501 (log-edit-comment-to-change-log)))))
503 (defvar log-edit-changelog-use-first nil)
504 (defun log-edit-insert-changelog (&optional use-first)
505 "Insert a log message by looking at the ChangeLog.
506 The idea is to write your ChangeLog entries first, and then use this
507 command to commit your changes.
509 To select default log text, we:
510 - find the ChangeLog entries for the files to be checked in,
511 - verify that the top entry in the ChangeLog is on the current date
512 and by the current user; if not, we don't provide any default text,
513 - search the ChangeLog entry for paragraphs containing the names of
514 the files we're checking in, and finally
515 - use those paragraphs as the log text.
517 If the optional prefix arg USE-FIRST is given (via \\[universal-argument]),
518 or if the command is repeated a second time in a row, use the first log entry
519 regardless of user name or time."
520 (interactive "P")
521 (let ((log-edit-changelog-use-first
522 (or use-first (eq last-command 'log-edit-insert-changelog))))
523 (log-edit-insert-changelog-entries (log-edit-files)))
524 (log-edit-set-common-indentation)
525 (goto-char (point-min))
526 (when (looking-at "\\*\\s-+")
527 (forward-line 1)
528 (when (not (re-search-forward "^\\*\\s-+" nil t))
529 (goto-char (point-min))
530 (skip-chars-forward "^():")
531 (skip-chars-forward ": ")
532 (delete-region (point-min) (point)))))
534 ;;;;
535 ;;;; functions for getting commit message from ChangeLog a file...
536 ;;;; Courtesy Jim Blandy
537 ;;;;
539 (defun log-edit-narrow-changelog ()
540 "Narrow to the top page of the current buffer, a ChangeLog file.
541 Actually, the narrowed region doesn't include the date line.
542 A \"page\" in a ChangeLog file is the area between two dates."
543 (or (eq major-mode 'change-log-mode)
544 (error "log-edit-narrow-changelog: current buffer isn't a ChangeLog"))
546 (goto-char (point-min))
548 ;; Skip date line and subsequent blank lines.
549 (forward-line 1)
550 (if (looking-at "[ \t\n]*\n")
551 (goto-char (match-end 0)))
553 (let ((start (point)))
554 (forward-page 1)
555 (narrow-to-region start (point))
556 (goto-char (point-min))))
558 (defun log-edit-changelog-paragraph ()
559 "Return the bounds of the ChangeLog paragraph containing point.
560 If we are between paragraphs, return the previous paragraph."
561 (save-excursion
562 (beginning-of-line)
563 (if (looking-at "^[ \t]*$")
564 (skip-chars-backward " \t\n" (point-min)))
565 (list (progn
566 (if (re-search-backward "^[ \t]*\n" nil 'or-to-limit)
567 (goto-char (match-end 0)))
568 (point))
569 (if (re-search-forward "^[ \t\n]*$" nil t)
570 (match-beginning 0)
571 (point-max)))))
573 (defun log-edit-changelog-subparagraph ()
574 "Return the bounds of the ChangeLog subparagraph containing point.
575 A subparagraph is a block of non-blank lines beginning with an asterisk.
576 If we are between sub-paragraphs, return the previous subparagraph."
577 (save-excursion
578 (end-of-line)
579 (if (search-backward "*" nil t)
580 (list (progn (beginning-of-line) (point))
581 (progn
582 (forward-line 1)
583 (if (re-search-forward "^[ \t]*[\n*]" nil t)
584 (match-beginning 0)
585 (point-max))))
586 (list (point) (point)))))
588 (defun log-edit-changelog-entry ()
589 "Return the bounds of the ChangeLog entry containing point.
590 The variable `log-edit-changelog-full-paragraphs' decides whether an
591 \"entry\" is a paragraph or a subparagraph; see its documentation string
592 for more details."
593 (if log-edit-changelog-full-paragraphs
594 (log-edit-changelog-paragraph)
595 (log-edit-changelog-subparagraph)))
597 (defvar user-full-name)
598 (defvar user-mail-address)
599 (defun log-edit-changelog-ours-p ()
600 "See if ChangeLog entry at point is for the current user, today.
601 Return non-nil if it is."
602 ;; Code adapted from add-change-log-entry.
603 (let ((name (or (and (boundp 'add-log-full-name) add-log-full-name)
604 (and (fboundp 'user-full-name) (user-full-name))
605 (and (boundp 'user-full-name) user-full-name)))
606 (mail (or (and (boundp 'add-log-mailing-address) add-log-mailing-address)
607 ;;(and (fboundp 'user-mail-address) (user-mail-address))
608 (and (boundp 'user-mail-address) user-mail-address)))
609 (time (or (and (boundp 'add-log-time-format)
610 (functionp add-log-time-format)
611 (funcall add-log-time-format))
612 (format-time-string "%Y-%m-%d"))))
613 (looking-at (if log-edit-changelog-use-first
614 "[^ \t]"
615 (regexp-quote (format "%s %s <%s>" time name mail))))))
617 (defun log-edit-changelog-entries (file)
618 "Return the ChangeLog entries for FILE, and the ChangeLog they came from.
619 The return value looks like this:
620 (LOGBUFFER (ENTRYSTART . ENTRYEND) ...)
621 where LOGBUFFER is the name of the ChangeLog buffer, and each
622 \(ENTRYSTART . ENTRYEND\) pair is a buffer region."
623 (let ((changelog-file-name
624 (let ((default-directory
625 (file-name-directory (expand-file-name file)))
626 (visiting-buffer (find-buffer-visiting file)))
627 ;; If there is a buffer visiting FILE, and it has a local
628 ;; value for `change-log-default-name', use that.
629 (if (and visiting-buffer
630 (local-variable-p 'change-log-default-name
631 visiting-buffer))
632 (with-current-buffer visiting-buffer
633 change-log-default-name)
634 ;; `find-change-log' uses `change-log-default-name' if set
635 ;; and sets it before exiting, so we need to work around
636 ;; that memoizing which is undesired here
637 (setq change-log-default-name nil)
638 (find-change-log)))))
639 (with-current-buffer (find-file-noselect changelog-file-name)
640 (unless (eq major-mode 'change-log-mode) (change-log-mode))
641 (goto-char (point-min))
642 (if (looking-at "\\s-*\n") (goto-char (match-end 0)))
643 (if (not (log-edit-changelog-ours-p))
644 (list (current-buffer))
645 (save-restriction
646 (log-edit-narrow-changelog)
647 (goto-char (point-min))
649 ;; Search for the name of FILE relative to the ChangeLog. If that
650 ;; doesn't occur anywhere, they're not using full relative
651 ;; filenames in the ChangeLog, so just look for FILE; we'll accept
652 ;; some false positives.
653 (let ((pattern (file-relative-name
654 file (file-name-directory changelog-file-name))))
655 (if (or (string= pattern "")
656 (not (save-excursion
657 (search-forward pattern nil t))))
658 (setq pattern (file-name-nondirectory file)))
660 (setq pattern (concat "\\(^\\|[^[:alnum:]]\\)"
661 pattern
662 "\\($\\|[^[:alnum:]]\\)"))
664 (let (texts)
665 (while (re-search-forward pattern nil t)
666 (let ((entry (log-edit-changelog-entry)))
667 (push entry texts)
668 (goto-char (elt entry 1))))
670 (cons (current-buffer) texts))))))))
672 (defun log-edit-changelog-insert-entries (buffer regions)
673 "Insert those regions in BUFFER specified in REGIONS.
674 Sort REGIONS front-to-back first."
675 (let ((regions (sort regions 'car-less-than-car))
676 (last))
677 (dolist (region regions)
678 (when (and last (< last (car region))) (newline))
679 (setq last (elt region 1))
680 (apply 'insert-buffer-substring buffer region))))
682 (defun log-edit-insert-changelog-entries (files)
683 "Given a list of files FILES, insert the ChangeLog entries for them."
684 (let ((buffer-entries nil))
686 ;; Add each buffer to buffer-entries, and associate it with the list
687 ;; of entries we want from that file.
688 (dolist (file files)
689 (let* ((entries (log-edit-changelog-entries file))
690 (pair (assq (car entries) buffer-entries)))
691 (if pair
692 (setcdr pair (cvs-union (cdr pair) (cdr entries)))
693 (push entries buffer-entries))))
695 ;; Now map over each buffer in buffer-entries, sort the entries for
696 ;; each buffer, and extract them as strings.
697 (dolist (buffer-entry buffer-entries)
698 (log-edit-changelog-insert-entries (car buffer-entry) (cdr buffer-entry))
699 (when (cdr buffer-entry) (newline)))))
701 (provide 'log-edit)
703 ;; arch-tag: 8089b39c-983b-4e83-93cd-ed0a64c7fdcc
704 ;;; log-edit.el ends here