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