Add 2010 to copyright years.
[bpt/emacs.git] / lisp / log-edit.el
1 ;;; log-edit.el --- Major mode for editing CVS commit messages
2
3 ;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
5
6 ;; Author: Stefan Monnier <monnier@iro.umontreal.ca>
7 ;; Keywords: pcl-cvs cvs commit log
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23
24 ;;; Commentary:
25
26 ;; Todo:
27
28 ;; - Move in VC's code
29 ;; - Add compatibility for VC's hook variables
30
31 ;;; Code:
32
33 (eval-when-compile (require 'cl))
34 (require 'add-log) ; for all the ChangeLog goodies
35 (require 'pcvs-util)
36 (require 'ring)
37
38 ;;;;
39 ;;;; Global Variables
40 ;;;;
41
42 (defgroup log-edit nil
43 "Major mode for editing RCS and CVS commit messages."
44 :group 'pcl-cvs
45 :group 'vc ; It's used by VC.
46 :version "21.1"
47 :prefix "log-edit-")
48
49 ;; compiler pacifiers
50 (defvar cvs-buffer)
51
52 \f
53 ;; The main keymap
54
55 (easy-mmode-defmap log-edit-mode-map
56 `(("\C-c\C-c" . log-edit-done)
57 ("\C-c\C-a" . log-edit-insert-changelog)
58 ("\C-c\C-d" . log-edit-show-diff)
59 ("\C-c\C-f" . log-edit-show-files)
60 ("\M-n" . log-edit-next-comment)
61 ("\M-p" . log-edit-previous-comment)
62 ("\M-r" . log-edit-comment-search-backward)
63 ("\M-s" . log-edit-comment-search-forward)
64 ("\C-c?" . log-edit-mode-help))
65 "Keymap for the `log-edit-mode' (to edit version control log messages)."
66 :group 'log-edit)
67
68 ;; Compatibility with old names. Should we bother ?
69 (defvar vc-log-mode-map log-edit-mode-map)
70 (defvar vc-log-entry-mode vc-log-mode-map)
71
72 (easy-menu-define log-edit-menu log-edit-mode-map
73 "Menu used for `log-edit-mode'."
74 '("Log-Edit"
75 ["Done" log-edit-done
76 :help "Exit log-edit and proceed with the actual action."]
77 "--"
78 ["Insert ChangeLog" log-edit-insert-changelog
79 :help "Insert a log message by looking at the ChangeLog"]
80 ["Add to ChangeLog" log-edit-add-to-changelog
81 :help "Insert this log message into the appropriate ChangeLog file"]
82 "--"
83 ["Show diff" log-edit-show-diff
84 :help "Show the diff for the files to be committed."]
85 ["List files" log-edit-show-files
86 :help "Show the list of relevant files."]
87 "--"
88 ["Previous comment" log-edit-previous-comment
89 :help "Cycle backwards through comment history"]
90 ["Next comment" log-edit-next-comment
91 :help "Cycle forwards through comment history."]
92 ["Search comment forward" log-edit-comment-search-forward
93 :help "Search forwards through comment history for a substring match of str"]
94 ["Search comment backward" log-edit-comment-search-backward
95 :help "Search backwards through comment history for substring match of str"]))
96
97 (defcustom log-edit-confirm 'changed
98 "If non-nil, `log-edit-done' will request confirmation.
99 If 'changed, only request confirmation if the list of files has
100 changed since the beginning of the log-edit session."
101 :group 'log-edit
102 :type '(choice (const changed) (const t) (const nil)))
103
104 (defcustom log-edit-keep-buffer nil
105 "If non-nil, don't hide the buffer after `log-edit-done'."
106 :group 'log-edit
107 :type 'boolean)
108
109 (defvar cvs-commit-buffer-require-final-newline t)
110 (make-obsolete-variable 'cvs-commit-buffer-require-final-newline
111 'log-edit-require-final-newline
112 "21.1")
113
114 (defcustom log-edit-require-final-newline
115 cvs-commit-buffer-require-final-newline
116 "Enforce a newline at the end of commit log messages.
117 Enforce it silently if t, query if non-nil and don't do anything if nil."
118 :group 'log-edit
119 :type '(choice (const ask) (const t) (const nil)))
120
121 (defcustom log-edit-setup-invert nil
122 "Non-nil means `log-edit' should invert the meaning of its SETUP arg.
123 If SETUP is 'force, this variable has no effect."
124 :group 'log-edit
125 :type 'boolean)
126
127 (defcustom log-edit-hook '(log-edit-insert-cvs-template
128 log-edit-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 t
152 "If non-nil, remove file name from single-file log entries."
153 :type 'boolean)
154
155 (defvar cvs-changelog-full-paragraphs t)
156 (make-obsolete-variable 'cvs-changelog-full-paragraphs
157 'log-edit-changelog-full-paragraphs
158 "21.1")
159
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.
164
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.
169
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.
175
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.")
180
181 ;;;; Internal global or buffer-local vars
182
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)
189
190 ;;; Originally taken from VC-Log mode
191
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 "")
197
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))
209
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)))))
222
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)))
228
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)))
248
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))
254
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'.
260
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"))))
298
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")
307
308 ;;;
309 ;;; Actual code
310 ;;;
311
312 (defvar log-edit-font-lock-keywords
313 '(("\\`\\(Summary:\\)\\(.*\\)"
314 (1 font-lock-keyword-face)
315 (2 font-lock-function-name-face))))
316
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.
326
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.
332
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))
349
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."))))
356
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).
364
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))
369
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)))
375
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))))
414
415 (defun log-edit-files ()
416 "Return the list of files that are about to be committed."
417 (ignore-errors (funcall log-edit-listfun)))
418
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."))))
427
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)
432
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)))))
444
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")))
451
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)))))
468
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 (called-interactively-p 'interactive)
474 (= (point-min) (point-max)))
475 (when (file-readable-p "CVS/Template")
476 (insert-file-contents "CVS/Template"))))
477
478 (defun log-edit-insert-cvs-rcstemplate ()
479 "Insert the rcstemplate from the CVS repository.
480 This contacts the repository to get the rcstemplate file and
481 can thus take some time."
482 (interactive)
483 (when (or (called-interactively-p 'interactive)
484 (= (point-min) (point-max)))
485 (when (file-readable-p "CVS/Root")
486 ;; Ignore the stderr stuff, even if it's an error.
487 (call-process "cvs" nil '(t nil) nil
488 "checkout" "-p" "CVSROOT/rcstemplate"))))
489
490 (defun log-edit-insert-filenames ()
491 "Insert the list of files that are to be committed."
492 (interactive)
493 (insert "Affected files: \n"
494 (mapconcat 'identity (log-edit-files) " \n")))
495
496 (defun log-edit-add-to-changelog ()
497 "Insert this log message into the appropriate ChangeLog file."
498 (interactive)
499 ;; Yuck!
500 (unless (string= (buffer-string) (ring-ref log-edit-comment-ring 0))
501 (ring-insert log-edit-comment-ring (buffer-string)))
502 (dolist (f (log-edit-files))
503 (let ((buffer-file-name (expand-file-name f)))
504 (save-excursion
505 (log-edit-comment-to-change-log)))))
506
507 (defvar log-edit-changelog-use-first nil)
508 (defun log-edit-insert-changelog (&optional use-first)
509 "Insert a log message by looking at the ChangeLog.
510 The idea is to write your ChangeLog entries first, and then use this
511 command to commit your changes.
512
513 To select default log text, we:
514 - find the ChangeLog entries for the files to be checked in,
515 - verify that the top entry in the ChangeLog is on the current date
516 and by the current user; if not, we don't provide any default text,
517 - search the ChangeLog entry for paragraphs containing the names of
518 the files we're checking in, and finally
519 - use those paragraphs as the log text.
520
521 If the optional prefix arg USE-FIRST is given (via \\[universal-argument]),
522 or if the command is repeated a second time in a row, use the first log entry
523 regardless of user name or time."
524 (interactive "P")
525 (let ((log-edit-changelog-use-first
526 (or use-first (eq last-command 'log-edit-insert-changelog))))
527 (log-edit-insert-changelog-entries (log-edit-files)))
528 (log-edit-set-common-indentation)
529 (goto-char (point-min))
530 (when (and log-edit-strip-single-file-name (looking-at "\\*\\s-+"))
531 (forward-line 1)
532 (when (not (re-search-forward "^\\*\\s-+" nil t))
533 (goto-char (point-min))
534 (skip-chars-forward "^():")
535 (skip-chars-forward ": ")
536 (delete-region (point-min) (point)))))
537
538 ;;;;
539 ;;;; functions for getting commit message from ChangeLog a file...
540 ;;;; Courtesy Jim Blandy
541 ;;;;
542
543 (defun log-edit-narrow-changelog ()
544 "Narrow to the top page of the current buffer, a ChangeLog file.
545 Actually, the narrowed region doesn't include the date line.
546 A \"page\" in a ChangeLog file is the area between two dates."
547 (or (eq major-mode 'change-log-mode)
548 (error "log-edit-narrow-changelog: current buffer isn't a ChangeLog"))
549
550 (goto-char (point-min))
551
552 ;; Skip date line and subsequent blank lines.
553 (forward-line 1)
554 (if (looking-at "[ \t\n]*\n")
555 (goto-char (match-end 0)))
556
557 (let ((start (point)))
558 (forward-page 1)
559 (narrow-to-region start (point))
560 (goto-char (point-min))))
561
562 (defun log-edit-changelog-paragraph ()
563 "Return the bounds of the ChangeLog paragraph containing point.
564 If we are between paragraphs, return the previous paragraph."
565 (beginning-of-line)
566 (if (looking-at "^[ \t]*$")
567 (skip-chars-backward " \t\n" (point-min)))
568 (list (progn
569 (if (re-search-backward "^[ \t]*\n" nil 'or-to-limit)
570 (goto-char (match-end 0)))
571 (point))
572 (if (re-search-forward "^[ \t\n]*$" nil t)
573 (match-beginning 0)
574 (point-max))))
575
576 (defun log-edit-changelog-subparagraph ()
577 "Return the bounds of the ChangeLog subparagraph containing point.
578 A subparagraph is a block of non-blank lines beginning with an asterisk.
579 If we are between sub-paragraphs, return the previous subparagraph."
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))))
589
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 (save-excursion
596 (if log-edit-changelog-full-paragraphs
597 (log-edit-changelog-paragraph)
598 (log-edit-changelog-subparagraph))))
599
600 (defvar user-full-name)
601 (defvar user-mail-address)
602 (defun log-edit-changelog-ours-p ()
603 "See if ChangeLog entry at point is for the current user, today.
604 Return non-nil if it is."
605 ;; Code adapted from add-change-log-entry.
606 (let ((name (or (and (boundp 'add-log-full-name) add-log-full-name)
607 (and (fboundp 'user-full-name) (user-full-name))
608 (and (boundp 'user-full-name) user-full-name)))
609 (mail (or (and (boundp 'add-log-mailing-address) add-log-mailing-address)
610 ;;(and (fboundp 'user-mail-address) (user-mail-address))
611 (and (boundp 'user-mail-address) user-mail-address)))
612 (time (or (and (boundp 'add-log-time-format)
613 (functionp add-log-time-format)
614 (funcall add-log-time-format))
615 (format-time-string "%Y-%m-%d"))))
616 (looking-at (if log-edit-changelog-use-first
617 "[^ \t]"
618 (regexp-quote (format "%s %s <%s>" time name mail))))))
619
620 (defun log-edit-changelog-entries (file)
621 "Return the ChangeLog entries for FILE, and the ChangeLog they came from.
622 The return value looks like this:
623 (LOGBUFFER (ENTRYSTART . ENTRYEND) ...)
624 where LOGBUFFER is the name of the ChangeLog buffer, and each
625 \(ENTRYSTART . ENTRYEND\) pair is a buffer region."
626 (let ((changelog-file-name
627 (let ((default-directory
628 (file-name-directory (expand-file-name file)))
629 (visiting-buffer (find-buffer-visiting file)))
630 ;; If there is a buffer visiting FILE, and it has a local
631 ;; value for `change-log-default-name', use that.
632 (if (and visiting-buffer
633 (local-variable-p 'change-log-default-name
634 visiting-buffer))
635 (with-current-buffer visiting-buffer
636 change-log-default-name)
637 ;; `find-change-log' uses `change-log-default-name' if set
638 ;; and sets it before exiting, so we need to work around
639 ;; that memoizing which is undesired here
640 (setq change-log-default-name nil)
641 (find-change-log)))))
642 (with-current-buffer (find-file-noselect changelog-file-name)
643 (unless (eq major-mode 'change-log-mode) (change-log-mode))
644 (goto-char (point-min))
645 (if (looking-at "\\s-*\n") (goto-char (match-end 0)))
646 (if (not (log-edit-changelog-ours-p))
647 (list (current-buffer))
648 (save-restriction
649 (log-edit-narrow-changelog)
650 (goto-char (point-min))
651
652 ;; Search for the name of FILE relative to the ChangeLog. If that
653 ;; doesn't occur anywhere, they're not using full relative
654 ;; filenames in the ChangeLog, so just look for FILE; we'll accept
655 ;; some false positives.
656 (let ((pattern (file-relative-name
657 file (file-name-directory changelog-file-name))))
658 (if (or (string= pattern "")
659 (not (save-excursion
660 (search-forward pattern nil t))))
661 (setq pattern (file-name-nondirectory file)))
662
663 (setq pattern (concat "\\(^\\|[^[:alnum:]]\\)"
664 pattern
665 "\\($\\|[^[:alnum:]]\\)"))
666
667 (let (texts
668 (pos (point)))
669 (while (and (not (eobp)) (re-search-forward pattern nil t))
670 (let ((entry (log-edit-changelog-entry)))
671 (if (< (elt entry 1) (max (1+ pos) (point)))
672 ;; This is not relevant, actually.
673 nil
674 (push entry texts))
675 ;; Make sure we make progress.
676 (setq pos (max (1+ pos) (elt entry 1)))
677 (goto-char pos)))
678
679 (cons (current-buffer) texts))))))))
680
681 (defun log-edit-changelog-insert-entries (buffer regions)
682 "Insert those regions in BUFFER specified in REGIONS.
683 Sort REGIONS front-to-back first."
684 (let ((regions (sort regions 'car-less-than-car))
685 (last))
686 (dolist (region regions)
687 (when (and last (< last (car region))) (newline))
688 (setq last (elt region 1))
689 (apply 'insert-buffer-substring buffer region))))
690
691 (defun log-edit-insert-changelog-entries (files)
692 "Given a list of files FILES, insert the ChangeLog entries for them."
693 (let ((buffer-entries nil))
694
695 ;; Add each buffer to buffer-entries, and associate it with the list
696 ;; of entries we want from that file.
697 (dolist (file files)
698 (let* ((entries (log-edit-changelog-entries file))
699 (pair (assq (car entries) buffer-entries)))
700 (if pair
701 (setcdr pair (cvs-union (cdr pair) (cdr entries)))
702 (push entries buffer-entries))))
703
704 ;; Now map over each buffer in buffer-entries, sort the entries for
705 ;; each buffer, and extract them as strings.
706 (dolist (buffer-entry buffer-entries)
707 (log-edit-changelog-insert-entries (car buffer-entry) (cdr buffer-entry))
708 (when (cdr buffer-entry) (newline)))))
709
710 (provide 'log-edit)
711
712 ;; arch-tag: 8089b39c-983b-4e83-93cd-ed0a64c7fdcc
713 ;;; log-edit.el ends here