(Fxw_color_values): Return 3-element list. Doc fix.
[emacs.git] / lisp / log-edit.el
bloba9816ea664931147c7422e524aaddb77297a1f2b
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, 2009 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 of the License, or
14 ;; (at your option) 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. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;; Todo:
28 ;; - Move in VC's code
29 ;; - Add compatibility for VC's hook variables
31 ;;; Code:
33 (eval-when-compile (require 'cl))
34 (require 'add-log) ; for all the ChangeLog goodies
35 (require 'pcvs-util)
36 (require 'ring)
38 ;;;;
39 ;;;; Global Variables
40 ;;;;
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-")
49 ;; compiler pacifiers
50 (defvar cvs-buffer)
53 ;; The main keymap
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)
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)
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"]))
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)))
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)
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")
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)))
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)
127 (defcustom log-edit-hook '(log-edit-insert-cvs-template
128 log-edit-insert-changelog)
129 "Hook run at the end of `log-edit'."
130 :group 'log-edit
131 :type '(hook :options (log-edit-insert-changelog
132 log-edit-insert-cvs-rcstemplate
133 log-edit-insert-cvs-template
134 log-edit-insert-filenames)))
136 (defcustom log-edit-mode-hook (if (boundp 'vc-log-mode-hook) vc-log-mode-hook)
137 "Hook run when entering `log-edit-mode'."
138 :group 'log-edit
139 :type 'hook)
141 (defcustom log-edit-done-hook nil
142 "Hook run before doing the actual commit.
143 This hook can be used to cleanup the message, enforce various
144 conventions, or to allow recording the message in some other database,
145 such as a bug-tracking system. The list of files about to be committed
146 can be obtained from `log-edit-files'."
147 :group 'log-edit
148 :type '(hook :options (log-edit-set-common-indentation
149 log-edit-add-to-changelog)))
151 (defcustom log-edit-strip-single-file-name t
152 "If non-nil, remove file name from single-file log entries."
153 :type 'boolean)
155 (defvar cvs-changelog-full-paragraphs t)
156 (make-obsolete-variable 'cvs-changelog-full-paragraphs
157 'log-edit-changelog-full-paragraphs
158 "21.1")
160 (defvar log-edit-changelog-full-paragraphs cvs-changelog-full-paragraphs
161 "*If non-nil, include full ChangeLog paragraphs in the log.
162 This may be set in the ``local variables'' section of a ChangeLog, to
163 indicate the policy for that ChangeLog.
165 A ChangeLog paragraph is a bunch of log text containing no blank lines;
166 a paragraph usually describes a set of changes with a single purpose,
167 but perhaps spanning several functions in several files. Changes in
168 different paragraphs are unrelated.
170 You could argue that the log entry for a file should contain the
171 full ChangeLog paragraph mentioning the change to the file, even though
172 it may mention other files, because that gives you the full context you
173 need to understand the change. This is the behavior you get when this
174 variable is set to t.
176 On the other hand, you could argue that the log entry for a change
177 should contain only the text for the changes which occurred in that
178 file, because the log is per-file. This is the behavior you get
179 when this variable is set to nil.")
181 ;;;; Internal global or buffer-local vars
183 (defconst log-edit-files-buf "*log-edit-files*")
184 (defvar log-edit-initial-files nil)
185 (defvar log-edit-callback nil)
186 (defvar log-edit-diff-function nil)
187 (defvar log-edit-listfun nil)
188 (defvar log-edit-parent-buffer nil)
190 ;;; Originally taken from VC-Log mode
192 (defconst log-edit-maximum-comment-ring-size 32
193 "Maximum number of saved comments in the comment ring.")
194 (defvar log-edit-comment-ring (make-ring log-edit-maximum-comment-ring-size))
195 (defvar log-edit-comment-ring-index nil)
196 (defvar log-edit-last-comment-match "")
198 (defun log-edit-new-comment-index (stride len)
199 "Return the comment index STRIDE elements from the current one.
200 LEN is the length of `log-edit-comment-ring'."
201 (mod (cond
202 (log-edit-comment-ring-index (+ log-edit-comment-ring-index stride))
203 ;; Initialize the index on the first use of this command
204 ;; so that the first M-p gets index 0, and the first M-n gets
205 ;; index -1.
206 ((> stride 0) (1- stride))
207 (t stride))
208 len))
210 (defun log-edit-previous-comment (arg)
211 "Cycle backwards through comment history.
212 With a numeric prefix ARG, go back ARG comments."
213 (interactive "*p")
214 (let ((len (ring-length log-edit-comment-ring)))
215 (if (<= len 0)
216 (progn (message "Empty comment ring") (ding))
217 ;; Don't use `erase-buffer' because we don't want to `widen'.
218 (delete-region (point-min) (point-max))
219 (setq log-edit-comment-ring-index (log-edit-new-comment-index arg len))
220 (message "Comment %d" (1+ log-edit-comment-ring-index))
221 (insert (ring-ref log-edit-comment-ring log-edit-comment-ring-index)))))
223 (defun log-edit-next-comment (arg)
224 "Cycle forwards through comment history.
225 With a numeric prefix ARG, go forward ARG comments."
226 (interactive "*p")
227 (log-edit-previous-comment (- arg)))
229 (defun log-edit-comment-search-backward (str &optional stride)
230 "Search backwards through comment history for substring match of STR.
231 If the optional argument STRIDE is present, that is a step-width to use
232 when going through the comment ring."
233 ;; Why substring rather than regexp ? -sm
234 (interactive
235 (list (read-string "Comment substring: " nil nil log-edit-last-comment-match)))
236 (unless stride (setq stride 1))
237 (if (string= str "")
238 (setq str log-edit-last-comment-match)
239 (setq log-edit-last-comment-match str))
240 (let* ((str (regexp-quote str))
241 (len (ring-length log-edit-comment-ring))
242 (n (log-edit-new-comment-index stride len)))
243 (while (progn (when (or (>= n len) (< n 0)) (error "Not found"))
244 (not (string-match str (ring-ref log-edit-comment-ring n))))
245 (setq n (+ n stride)))
246 (setq log-edit-comment-ring-index n)
247 (log-edit-previous-comment 0)))
249 (defun log-edit-comment-search-forward (str)
250 "Search forwards through comment history for a substring match of STR."
251 (interactive
252 (list (read-string "Comment substring: " nil nil log-edit-last-comment-match)))
253 (log-edit-comment-search-backward str -1))
255 (defun log-edit-comment-to-change-log (&optional whoami file-name)
256 "Enter last VC comment into the change log for the current file.
257 WHOAMI (interactive prefix) non-nil means prompt for user name
258 and site. FILE-NAME is the name of the change log; if nil, use
259 `change-log-default-name'.
261 This may be useful as a `log-edit-checkin-hook' to update change logs
262 automatically."
263 (interactive (if current-prefix-arg
264 (list current-prefix-arg
265 (prompt-for-change-log-name))))
266 (let (;; Extract the comment first so we get any error before doing anything.
267 (comment (ring-ref log-edit-comment-ring 0))
268 ;; Don't let add-change-log-entry insert a defun name.
269 (add-log-current-defun-function 'ignore)
270 end)
271 ;; Call add-log to do half the work.
272 (add-change-log-entry whoami file-name t t)
273 ;; Insert the VC comment, leaving point before it.
274 (setq end (save-excursion (insert comment) (point-marker)))
275 (if (looking-at "\\s *\\s(")
276 ;; It starts with an open-paren, as in "(foo): Frobbed."
277 ;; So remove the ": " add-log inserted.
278 (delete-char -2))
279 ;; Canonicalize the white space between the file name and comment.
280 (just-one-space)
281 ;; Indent rest of the text the same way add-log indented the first line.
282 (let ((indentation (current-indentation)))
283 (save-excursion
284 (while (< (point) end)
285 (forward-line 1)
286 (indent-to indentation))
287 (setq end (point))))
288 ;; Fill the inserted text, preserving open-parens at bol.
289 (let ((paragraph-start (concat paragraph-start "\\|\\s *\\s(")))
290 (beginning-of-line)
291 (fill-region (point) end))
292 ;; Canonicalize the white space at the end of the entry so it is
293 ;; separated from the next entry by a single blank line.
294 (skip-syntax-forward " " end)
295 (delete-char (- (skip-syntax-backward " ")))
296 (or (eobp) (looking-at "\n\n")
297 (insert "\n"))))
299 ;; Compatibility with old names.
300 (define-obsolete-variable-alias 'vc-comment-ring 'log-edit-comment-ring "22.1")
301 (define-obsolete-variable-alias 'vc-comment-ring-index 'log-edit-comment-ring-index "22.1")
302 (define-obsolete-function-alias 'vc-previous-comment 'log-edit-previous-comment "22.1")
303 (define-obsolete-function-alias 'vc-next-comment 'log-edit-next-comment "22.1")
304 (define-obsolete-function-alias 'vc-comment-search-reverse 'log-edit-comment-search-backward "22.1")
305 (define-obsolete-function-alias 'vc-comment-search-forward 'log-edit-comment-search-forward "22.1")
306 (define-obsolete-function-alias 'vc-comment-to-change-log 'log-edit-comment-to-change-log "22.1")
309 ;;; Actual code
312 (defvar log-edit-font-lock-keywords
313 '(("\\`\\(Summary:\\)\\(.*\\)"
314 (1 font-lock-keyword-face)
315 (2 font-lock-function-name-face))))
317 ;;;###autoload
318 (defun log-edit (callback &optional setup params buffer &rest ignore)
319 "Setup a buffer to enter a log message.
320 \\<log-edit-mode-map>The buffer will be put in `log-edit-mode'.
321 If SETUP is non-nil, the buffer is then erased and `log-edit-hook' is run.
322 Mark and point will be set around the entire contents of the buffer so
323 that it is easy to kill the contents of the buffer with \\[kill-region].
324 Once you're done editing the message, pressing \\[log-edit-done] will call
325 `log-edit-done' which will end up calling CALLBACK to do the actual commit.
327 PARAMS if non-nil is an alist. Possible keys and associated values:
328 `log-edit-listfun' -- function taking no arguments that returns the list of
329 files that are concerned by the current operation (using relative names);
330 `log-edit-diff-function' -- function taking no arguments that
331 displays a diff of the files concerned by the current operation.
333 If BUFFER is non-nil `log-edit' will jump to that buffer, use it to edit the
334 log message and go back to the current buffer when done. Otherwise, it
335 uses the current buffer."
336 (let ((parent (current-buffer)))
337 (if buffer (pop-to-buffer buffer))
338 (when (and log-edit-setup-invert (not (eq setup 'force)))
339 (setq setup (not setup)))
340 (when setup (erase-buffer))
341 (log-edit-mode)
342 (set (make-local-variable 'log-edit-callback) callback)
343 (if (listp params)
344 (dolist (crt params)
345 (set (make-local-variable (car crt)) (cdr crt)))
346 ;; For backward compatibility with log-edit up to version 22.2
347 ;; accept non-list PARAMS to mean `log-edit-list'.
348 (set (make-local-variable 'log-edit-listfun) params))
350 (if buffer (set (make-local-variable 'log-edit-parent-buffer) parent))
351 (set (make-local-variable 'log-edit-initial-files) (log-edit-files))
352 (when setup (run-hooks 'log-edit-hook))
353 (goto-char (point-min)) (push-mark (point-max))
354 (message "%s" (substitute-command-keys
355 "Press \\[log-edit-done] when you are done editing."))))
357 (define-derived-mode log-edit-mode text-mode "Log-Edit"
358 "Major mode for editing version-control log messages.
359 When done editing the log entry, just type \\[log-edit-done] which
360 will trigger the actual commit of the file(s).
361 Several other handy support commands are provided of course and
362 the package from which this is used might also provide additional
363 commands (under C-x v for VC, for example).
365 \\{log-edit-mode-map}"
366 (set (make-local-variable 'font-lock-defaults)
367 '(log-edit-font-lock-keywords t))
368 (make-local-variable 'log-edit-comment-ring-index))
370 (defun log-edit-hide-buf (&optional buf where)
371 (when (setq buf (get-buffer (or buf log-edit-files-buf)))
372 (let ((win (get-buffer-window buf where)))
373 (if win (ignore-errors (delete-window win))))
374 (bury-buffer buf)))
376 (defun log-edit-done ()
377 "Finish editing the log message and commit the files.
378 If you want to abort the commit, simply delete the buffer."
379 (interactive)
380 ;; Get rid of trailing empty lines
381 (goto-char (point-max))
382 (skip-syntax-backward " ")
383 (when (equal (char-after) ?\n) (forward-char 1))
384 (delete-region (point) (point-max))
385 ;; Check for final newline
386 (if (and (> (point-max) (point-min))
387 (/= (char-before (point-max)) ?\n)
388 (or (eq log-edit-require-final-newline t)
389 (and log-edit-require-final-newline
390 (y-or-n-p
391 (format "Buffer %s does not end in newline. Add one? "
392 (buffer-name))))))
393 (save-excursion
394 (goto-char (point-max))
395 (insert ?\n)))
396 (let ((comment (buffer-string)))
397 (when (or (ring-empty-p log-edit-comment-ring)
398 (not (equal comment (ring-ref log-edit-comment-ring 0))))
399 (ring-insert log-edit-comment-ring comment)))
400 (let ((win (get-buffer-window log-edit-files-buf)))
401 (if (and log-edit-confirm
402 (not (and (eq log-edit-confirm 'changed)
403 (equal (log-edit-files) log-edit-initial-files)))
404 (progn
405 (log-edit-show-files)
406 (not (y-or-n-p "Really commit? "))))
407 (progn (when (not win) (log-edit-hide-buf))
408 (message "Oh, well! Later maybe?"))
409 (run-hooks 'log-edit-done-hook)
410 (log-edit-hide-buf)
411 (unless (or log-edit-keep-buffer (not log-edit-parent-buffer))
412 (cvs-bury-buffer (current-buffer) log-edit-parent-buffer))
413 (call-interactively log-edit-callback))))
415 (defun log-edit-files ()
416 "Return the list of files that are about to be committed."
417 (ignore-errors (funcall log-edit-listfun)))
419 (defun log-edit-mode-help ()
420 "Provide help for the `log-edit-mode-map'."
421 (interactive)
422 (if (eq last-command 'log-edit-mode-help)
423 (describe-function major-mode)
424 (message "%s"
425 (substitute-command-keys
426 "Type `\\[log-edit-done]' to finish commit. Try `\\[describe-function] log-edit-done' for more help."))))
428 (defcustom log-edit-common-indent 0
429 "Minimum indentation to use in `log-edit-set-common-indentation'."
430 :group 'log-edit
431 :type 'integer)
433 (defun log-edit-set-common-indentation ()
434 "(Un)Indent the current buffer rigidly to `log-edit-common-indent'."
435 (save-excursion
436 (let ((common (point-max)))
437 (goto-char (point-min))
438 (while (< (point) (point-max))
439 (if (not (looking-at "^[ \t]*$"))
440 (setq common (min common (current-indentation))))
441 (forward-line 1))
442 (indent-rigidly (point-min) (point-max)
443 (- log-edit-common-indent common)))))
445 (defun log-edit-show-diff ()
446 "Show the diff for the files to be committed."
447 (interactive)
448 (if (functionp log-edit-diff-function)
449 (funcall log-edit-diff-function)
450 (error "Diff functionality has not been setup")))
452 (defun log-edit-show-files ()
453 "Show the list of files to be committed."
454 (interactive)
455 (let* ((files (log-edit-files))
456 (buf (get-buffer-create log-edit-files-buf)))
457 (with-current-buffer buf
458 (log-edit-hide-buf buf 'all)
459 (setq buffer-read-only nil)
460 (erase-buffer)
461 (cvs-insert-strings files)
462 (setq buffer-read-only t)
463 (goto-char (point-min))
464 (save-selected-window
465 (cvs-pop-to-buffer-same-frame buf)
466 (shrink-window-if-larger-than-buffer)
467 (selected-window)))))
469 (defun log-edit-insert-cvs-template ()
470 "Insert the template specified by the CVS administrator, if any.
471 This simply uses the local CVS/Template file."
472 (interactive)
473 (when (or (interactive-p) (= (point-min) (point-max)))
474 (when (file-readable-p "CVS/Template")
475 (insert-file-contents "CVS/Template"))))
477 (defun log-edit-insert-cvs-rcstemplate ()
478 "Insert the rcstemplate from the CVS repository.
479 This contacts the repository to get the rcstemplate file and
480 can thus take some time."
481 (interactive)
482 (when (or (interactive-p) (= (point-min) (point-max)))
483 (when (file-readable-p "CVS/Root")
484 ;; Ignore the stderr stuff, even if it's an error.
485 (call-process "cvs" nil '(t nil) nil
486 "checkout" "-p" "CVSROOT/rcstemplate"))))
488 (defun log-edit-insert-filenames ()
489 "Insert the list of files that are to be committed."
490 (interactive)
491 (insert "Affected files: \n"
492 (mapconcat 'identity (log-edit-files) " \n")))
494 (defun log-edit-add-to-changelog ()
495 "Insert this log message into the appropriate ChangeLog file."
496 (interactive)
497 ;; Yuck!
498 (unless (string= (buffer-string) (ring-ref log-edit-comment-ring 0))
499 (ring-insert log-edit-comment-ring (buffer-string)))
500 (dolist (f (log-edit-files))
501 (let ((buffer-file-name (expand-file-name f)))
502 (save-excursion
503 (log-edit-comment-to-change-log)))))
505 (defvar log-edit-changelog-use-first nil)
506 (defun log-edit-insert-changelog (&optional use-first)
507 "Insert a log message by looking at the ChangeLog.
508 The idea is to write your ChangeLog entries first, and then use this
509 command to commit your changes.
511 To select default log text, we:
512 - find the ChangeLog entries for the files to be checked in,
513 - verify that the top entry in the ChangeLog is on the current date
514 and by the current user; if not, we don't provide any default text,
515 - search the ChangeLog entry for paragraphs containing the names of
516 the files we're checking in, and finally
517 - use those paragraphs as the log text.
519 If the optional prefix arg USE-FIRST is given (via \\[universal-argument]),
520 or if the command is repeated a second time in a row, use the first log entry
521 regardless of user name or time."
522 (interactive "P")
523 (let ((log-edit-changelog-use-first
524 (or use-first (eq last-command 'log-edit-insert-changelog))))
525 (log-edit-insert-changelog-entries (log-edit-files)))
526 (log-edit-set-common-indentation)
527 (goto-char (point-min))
528 (when (and log-edit-strip-single-file-name (looking-at "\\*\\s-+"))
529 (forward-line 1)
530 (when (not (re-search-forward "^\\*\\s-+" nil t))
531 (goto-char (point-min))
532 (skip-chars-forward "^():")
533 (skip-chars-forward ": ")
534 (delete-region (point-min) (point)))))
536 ;;;;
537 ;;;; functions for getting commit message from ChangeLog a file...
538 ;;;; Courtesy Jim Blandy
539 ;;;;
541 (defun log-edit-narrow-changelog ()
542 "Narrow to the top page of the current buffer, a ChangeLog file.
543 Actually, the narrowed region doesn't include the date line.
544 A \"page\" in a ChangeLog file is the area between two dates."
545 (or (eq major-mode 'change-log-mode)
546 (error "log-edit-narrow-changelog: current buffer isn't a ChangeLog"))
548 (goto-char (point-min))
550 ;; Skip date line and subsequent blank lines.
551 (forward-line 1)
552 (if (looking-at "[ \t\n]*\n")
553 (goto-char (match-end 0)))
555 (let ((start (point)))
556 (forward-page 1)
557 (narrow-to-region start (point))
558 (goto-char (point-min))))
560 (defun log-edit-changelog-paragraph ()
561 "Return the bounds of the ChangeLog paragraph containing point.
562 If we are between paragraphs, return the previous paragraph."
563 (save-excursion
564 (beginning-of-line)
565 (if (looking-at "^[ \t]*$")
566 (skip-chars-backward " \t\n" (point-min)))
567 (list (progn
568 (if (re-search-backward "^[ \t]*\n" nil 'or-to-limit)
569 (goto-char (match-end 0)))
570 (point))
571 (if (re-search-forward "^[ \t\n]*$" nil t)
572 (match-beginning 0)
573 (point-max)))))
575 (defun log-edit-changelog-subparagraph ()
576 "Return the bounds of the ChangeLog subparagraph containing point.
577 A subparagraph is a block of non-blank lines beginning with an asterisk.
578 If we are between sub-paragraphs, return the previous subparagraph."
579 (save-excursion
580 (end-of-line)
581 (if (search-backward "*" nil t)
582 (list (progn (beginning-of-line) (point))
583 (progn
584 (forward-line 1)
585 (if (re-search-forward "^[ \t]*[\n*]" nil t)
586 (match-beginning 0)
587 (point-max))))
588 (list (point) (point)))))
590 (defun log-edit-changelog-entry ()
591 "Return the bounds of the ChangeLog entry containing point.
592 The variable `log-edit-changelog-full-paragraphs' decides whether an
593 \"entry\" is a paragraph or a subparagraph; see its documentation string
594 for more details."
595 (if log-edit-changelog-full-paragraphs
596 (log-edit-changelog-paragraph)
597 (log-edit-changelog-subparagraph)))
599 (defvar user-full-name)
600 (defvar user-mail-address)
601 (defun log-edit-changelog-ours-p ()
602 "See if ChangeLog entry at point is for the current user, today.
603 Return non-nil if it is."
604 ;; Code adapted from add-change-log-entry.
605 (let ((name (or (and (boundp 'add-log-full-name) add-log-full-name)
606 (and (fboundp 'user-full-name) (user-full-name))
607 (and (boundp 'user-full-name) user-full-name)))
608 (mail (or (and (boundp 'add-log-mailing-address) add-log-mailing-address)
609 ;;(and (fboundp 'user-mail-address) (user-mail-address))
610 (and (boundp 'user-mail-address) user-mail-address)))
611 (time (or (and (boundp 'add-log-time-format)
612 (functionp add-log-time-format)
613 (funcall add-log-time-format))
614 (format-time-string "%Y-%m-%d"))))
615 (looking-at (if log-edit-changelog-use-first
616 "[^ \t]"
617 (regexp-quote (format "%s %s <%s>" time name mail))))))
619 (defun log-edit-changelog-entries (file)
620 "Return the ChangeLog entries for FILE, and the ChangeLog they came from.
621 The return value looks like this:
622 (LOGBUFFER (ENTRYSTART . ENTRYEND) ...)
623 where LOGBUFFER is the name of the ChangeLog buffer, and each
624 \(ENTRYSTART . ENTRYEND\) pair is a buffer region."
625 (let ((changelog-file-name
626 (let ((default-directory
627 (file-name-directory (expand-file-name file)))
628 (visiting-buffer (find-buffer-visiting file)))
629 ;; If there is a buffer visiting FILE, and it has a local
630 ;; value for `change-log-default-name', use that.
631 (if (and visiting-buffer
632 (local-variable-p 'change-log-default-name
633 visiting-buffer))
634 (with-current-buffer visiting-buffer
635 change-log-default-name)
636 ;; `find-change-log' uses `change-log-default-name' if set
637 ;; and sets it before exiting, so we need to work around
638 ;; that memoizing which is undesired here
639 (setq change-log-default-name nil)
640 (find-change-log)))))
641 (with-current-buffer (find-file-noselect changelog-file-name)
642 (unless (eq major-mode 'change-log-mode) (change-log-mode))
643 (goto-char (point-min))
644 (if (looking-at "\\s-*\n") (goto-char (match-end 0)))
645 (if (not (log-edit-changelog-ours-p))
646 (list (current-buffer))
647 (save-restriction
648 (log-edit-narrow-changelog)
649 (goto-char (point-min))
651 ;; Search for the name of FILE relative to the ChangeLog. If that
652 ;; doesn't occur anywhere, they're not using full relative
653 ;; filenames in the ChangeLog, so just look for FILE; we'll accept
654 ;; some false positives.
655 (let ((pattern (file-relative-name
656 file (file-name-directory changelog-file-name))))
657 (if (or (string= pattern "")
658 (not (save-excursion
659 (search-forward pattern nil t))))
660 (setq pattern (file-name-nondirectory file)))
662 (setq pattern (concat "\\(^\\|[^[:alnum:]]\\)"
663 pattern
664 "\\($\\|[^[:alnum:]]\\)"))
666 (let (texts)
667 (while (re-search-forward pattern nil t)
668 (let ((entry (log-edit-changelog-entry)))
669 (push entry texts)
670 (goto-char (elt entry 1))))
672 (cons (current-buffer) texts))))))))
674 (defun log-edit-changelog-insert-entries (buffer regions)
675 "Insert those regions in BUFFER specified in REGIONS.
676 Sort REGIONS front-to-back first."
677 (let ((regions (sort regions 'car-less-than-car))
678 (last))
679 (dolist (region regions)
680 (when (and last (< last (car region))) (newline))
681 (setq last (elt region 1))
682 (apply 'insert-buffer-substring buffer region))))
684 (defun log-edit-insert-changelog-entries (files)
685 "Given a list of files FILES, insert the ChangeLog entries for them."
686 (let ((buffer-entries nil))
688 ;; Add each buffer to buffer-entries, and associate it with the list
689 ;; of entries we want from that file.
690 (dolist (file files)
691 (let* ((entries (log-edit-changelog-entries file))
692 (pair (assq (car entries) buffer-entries)))
693 (if pair
694 (setcdr pair (cvs-union (cdr pair) (cdr entries)))
695 (push entries buffer-entries))))
697 ;; Now map over each buffer in buffer-entries, sort the entries for
698 ;; each buffer, and extract them as strings.
699 (dolist (buffer-entry buffer-entries)
700 (log-edit-changelog-insert-entries (car buffer-entry) (cdr buffer-entry))
701 (when (cdr buffer-entry) (newline)))))
703 (provide 'log-edit)
705 ;; arch-tag: 8089b39c-983b-4e83-93cd-ed0a64c7fdcc
706 ;;; log-edit.el ends here