*** empty log message ***
[emacs.git] / lisp / log-edit.el
blob0f8dd68d214ec34943e944316406bb7bef2265d8
1 ;;; log-edit.el --- Major mode for editing CVS commit messages
3 ;; Copyright (C) 1999-2000 Free Software Foundation, Inc.
5 ;; Author: Stefan Monnier <monnier@cs.yale.edu>
6 ;; Keywords: pcl-cvs cvs commit log
7 ;; Version: $Name: $
8 ;; Revision: $Id: log-edit.el,v 1.6 2000/06/02 02:28:39 monnier Exp $
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
27 ;;; Commentary:
29 ;; Todo:
31 ;; - Move in VC's code
32 ;; - Add compatibility for VC's hook variables
34 ;;; Code:
36 (eval-when-compile (require 'cl))
37 (require 'add-log) ; for all the ChangeLog goodies
38 (require 'pcvs-util)
39 (require 'ring)
40 (require 'vc)
42 ;;;;
43 ;;;; Global Variables
44 ;;;;
46 (defgroup log-edit nil
47 "Major mode for editing commit messages for PCL-CVS."
48 :group 'pcl-cvs
49 :prefix "log-edit-")
51 ;; compiler pacifiers
52 (defvar cvs-buffer)
54 (easy-mmode-defmap log-edit-mode-map
55 `(("\C-c\C-c" . log-edit-done)
56 ("\C-c\C-a" . log-edit-insert-changelog)
57 ("\C-c\C-f" . log-edit-show-files)
58 ("\C-c?" . log-edit-mode-help))
59 "Keymap for the `log-edit-mode' (used when editing cvs log messages)."
60 :group 'log-edit
61 :inherit (if (boundp 'vc-log-entry-mode) vc-log-entry-mode
62 (if (boundp 'vc-log-mode-map) vc-log-mode-map)))
64 (defcustom log-edit-confirm t
65 "*If non-nil, `log-edit-done' will request confirmation.
66 If 'changed, only request confirmation if the list of files has
67 changed since the beginning of the log-edit session."
68 :group 'log-edit
69 :type '(choice (const changed) (const t) (const nil)))
71 (defcustom log-edit-keep-buffer nil
72 "*If non-nil, don't hide the buffer after `log-edit-done'."
73 :group 'log-edit
74 :type 'boolean)
76 (defvar cvs-commit-buffer-require-final-newline t
77 "Obsolete, use `log-edit-require-final-newline'.")
79 (defcustom log-edit-require-final-newline
80 cvs-commit-buffer-require-final-newline
81 "*Enforce a newline at the end of commit log messages.
82 Enforce it silently if t, query if non-nil and don't do anything if nil."
83 :group 'log-edit
84 :type '(choice (const ask) (const t) (const nil)))
86 (defcustom log-edit-setup-invert nil
87 "*Non-nil means `log-edit' should invert the meaning of its SETUP arg.
88 If SETUP is 'force, this variable has no effect."
89 :group 'log-edit
90 :type 'boolean)
92 (defcustom log-edit-hook '(log-edit-insert-cvs-template
93 log-edit-insert-changelog)
94 "*Hook run at the end of `log-edit'."
95 :group 'log-edit
96 :type '(hook :options (log-edit-insert-cvs-template
97 log-edit-insert-changelog)))
99 (defcustom log-edit-mode-hook (if (boundp 'vc-log-mode-hook) vc-log-mode-hook)
100 "*Hook run when entering `log-edit-mode'."
101 :group 'log-edit
102 :type 'hook)
104 (defcustom log-edit-done-hook nil
105 "*Hook run before doing the actual commit.
106 This hook can be used to cleanup the message, enforce various
107 conventions, or to allow recording the message in some other database,
108 such as a bug-tracking system. The list of files about to be committed
109 can be obtained from `log-edit-files'."
110 :group 'log-edit
111 :type '(hook :options (log-edit-delete-common-indentation
112 log-edit-add-to-changelog)))
114 (defvar cvs-changelog-full-paragraphs t
115 "Obsolete, use `log-edit-changelog-full-paragraphs'.")
117 (defvar log-edit-changelog-full-paragraphs cvs-changelog-full-paragraphs
118 "*If non-nil, include full ChangeLog paragraphs in the log.
119 This may be set in the ``local variables'' section of a ChangeLog, to
120 indicate the policy for that ChangeLog.
122 A ChangeLog paragraph is a bunch of log text containing no blank lines;
123 a paragraph usually describes a set of changes with a single purpose,
124 but perhaps spanning several functions in several files. Changes in
125 different paragraphs are unrelated.
127 You could argue that the log entry for a file should contain the
128 full ChangeLog paragraph mentioning the change to the file, even though
129 it may mention other files, because that gives you the full context you
130 need to understand the change. This is the behaviour you get when this
131 variable is set to t.
133 On the other hand, you could argue that the log entry for a change
134 should contain only the text for the changes which occurred in that
135 file, because the log is per-file. This is the behaviour you get
136 when this variable is set to nil.")
138 ;;;; Internal global or buffer-local vars
140 (defconst log-edit-files-buf "*log-edit-files*")
141 (defvar log-edit-initial-files nil)
142 (defvar log-edit-callback nil)
143 (defvar log-edit-listfun nil)
145 ;;;;
146 ;;;; Actual code
147 ;;;;
149 ;;;###autoload
150 (defun log-edit (callback &optional setup listfun &rest ignore)
151 "Setup a buffer to enter a log message.
152 The buffer will be put in `log-edit-mode'.
153 If SETUP is non-nil, the buffer is then erased and `log-edit-hook' is run.
154 Mark and point will be set around the entire contents of the
155 buffer so that it is easy to kill the contents of the buffer with \\[kill-region].
156 Once you're done editing the message, pressing \\[log-edit-done] will call
157 `log-edit-done' which will end up calling CALLBACK to do the actual commit."
158 (when (and log-edit-setup-invert (not (eq setup 'force)))
159 (setq setup (not setup)))
160 (when setup (erase-buffer))
161 (log-edit-mode)
162 (set (make-local-variable 'log-edit-callback) callback)
163 (set (make-local-variable 'log-edit-listfun) listfun)
164 (when setup (run-hooks 'log-edit-hook))
165 (goto-char (point-min)) (push-mark (point-max))
166 (set (make-local-variable 'log-edit-initial-files) (log-edit-files))
167 (message (substitute-command-keys
168 "Press \\[log-edit-done] when you are done editing.")))
170 (define-derived-mode log-edit-mode text-mode "Log-Edit"
171 "Major mode for editing version-control log messages.
172 When done editing the log entry, just type \\[log-edit-done] which
173 will trigger the actual commit of the file(s).
174 Several other handy support commands are provided of course and
175 the package from which this is used might also provide additional
176 commands (under C-x v for VC, for example).
178 \\{log-edit-mode-map}"
179 (make-local-variable 'vc-comment-ring-index))
181 (defun log-edit-hide-buf (&optional buf where)
182 (when (setq buf (get-buffer (or buf log-edit-files-buf)))
183 (let ((win (get-buffer-window buf where)))
184 (if win (ignore-errors (delete-window win))))
185 (bury-buffer buf)))
187 (defun log-edit-done ()
188 "Finish editing the log message and commit the files.
189 If you want to abort the commit, simply delete the buffer."
190 (interactive)
191 ;; Get rid of trailing empty lines
192 (goto-char (point-max))
193 (skip-syntax-backward " ")
194 (when (equal (char-after) ?\n) (forward-char 1))
195 (delete-region (point) (point-max))
196 ;; Check for final newline
197 (if (and (> (point-max) 1)
198 (/= (char-after (1- (point-max))) ?\n)
199 (or (eq log-edit-require-final-newline t)
200 (and log-edit-require-final-newline
201 (y-or-n-p
202 (format "Buffer %s does not end in newline. Add one? "
203 (buffer-name))))))
204 (save-excursion
205 (goto-char (point-max))
206 (insert ?\n)))
207 (let ((comment (buffer-string)))
208 (when (and (boundp 'vc-comment-ring)
209 (ring-p vc-comment-ring)
210 (not (ring-empty-p vc-comment-ring))
211 (not (equal comment (ring-ref vc-comment-ring 0))))
212 (ring-insert vc-comment-ring comment)))
213 (let ((win (get-buffer-window log-edit-files-buf)))
214 (if (and log-edit-confirm
215 (not (and (eq log-edit-confirm 'changed)
216 (equal (log-edit-files) log-edit-initial-files)))
217 (progn
218 (log-edit-show-files)
219 (not (y-or-n-p "Really commit ? "))))
220 (progn (when (not win) (log-edit-hide-buf))
221 (message "Oh, well! Later maybe?"))
222 (run-hooks 'log-edit-done-hook)
223 (log-edit-hide-buf)
224 (unless log-edit-keep-buffer
225 (cvs-bury-buffer (current-buffer)
226 (when (boundp 'cvs-buffer) cvs-buffer)))
227 (call-interactively log-edit-callback))))
229 (defun log-edit-files ()
230 "Return the list of files that are about to be committed."
231 (ignore-errors (funcall log-edit-listfun)))
234 (defun log-edit-insert-changelog ()
235 "Insert a log message by looking at the ChangeLog.
236 The idea is to write your ChangeLog entries first, and then use this
237 command to commit your changes.
239 To select default log text, we:
240 - find the ChangeLog entries for the files to be checked in,
241 - verify that the top entry in the ChangeLog is on the current date
242 and by the current user; if not, we don't provide any default text,
243 - search the ChangeLog entry for paragraphs containing the names of
244 the files we're checking in, and finally
245 - use those paragraphs as the log text."
246 (interactive)
247 (log-edit-insert-changelog-entries (log-edit-files))
248 (log-edit-delete-common-indentation)
249 (goto-char (point-min))
250 (when (looking-at "\\*\\s-+")
251 (forward-line 1)
252 (when (not (re-search-forward "^\\*\\s-+" nil t))
253 (goto-char (point-min))
254 (skip-chars-forward "^():")
255 (skip-chars-forward ": ")
256 (delete-region (point-min) (point)))))
258 (defun log-edit-mode-help ()
259 "Provide help for the `log-edit-mode-map'."
260 (interactive)
261 (if (eq last-command 'log-edit-mode-help)
262 (describe-function major-mode)
263 (message
264 (substitute-command-keys
265 "Type `\\[log-edit-done]' to finish commit. Try `\\[describe-function] log-edit-done' for more help."))))
267 (defun log-edit-delete-common-indentation ()
268 "Unindent the current buffer rigidly until at least one line is flush left."
269 (save-excursion
270 (let ((common (point-max)))
271 (goto-char (point-min))
272 (while (< (point) (point-max))
273 (if (not (looking-at "^[ \t]*$"))
274 (setq common (min common (current-indentation))))
275 (forward-line 1))
276 (indent-rigidly (point-min) (point-max) (- common)))))
278 (defun log-edit-show-files ()
279 "Show the list of files to be committed."
280 (interactive)
281 (let* ((files (log-edit-files))
282 (editbuf (current-buffer))
283 (buf (get-buffer-create "*log-edit-files*")))
284 (with-current-buffer buf
285 (log-edit-hide-buf buf 'all)
286 (setq buffer-read-only nil)
287 (erase-buffer)
288 (insert (mapconcat 'identity files "\n"))
289 (setq buffer-read-only t)
290 (goto-char (point-min))
291 (save-selected-window
292 (cvs-pop-to-buffer-same-frame buf)
293 (shrink-window-if-larger-than-buffer)
294 (selected-window)))))
296 (defun log-edit-insert-cvs-template ()
297 "Insert the template specified by the CVS administrator, if any."
298 (interactive)
299 (when (file-readable-p "CVS/Template")
300 (insert-file-contents "CVS/Template")))
303 (defun log-edit-add-to-changelog ()
304 "Insert this log message into the appropriate ChangeLog file."
305 (interactive)
306 ;; Yuck!
307 (unless (string= (buffer-string) (ring-ref vc-comment-ring 0))
308 (ring-insert vc-comment-ring (buffer-string)))
309 (dolist (f (log-edit-files))
310 (let ((buffer-file-name (expand-file-name f)))
311 (save-excursion
312 (vc-comment-to-change-log)))))
314 ;;;;
315 ;;;; functions for getting commit message from ChangeLog a file...
316 ;;;; Courtesy Jim Blandy
317 ;;;;
319 (defun log-edit-narrow-changelog ()
320 "Narrow to the top page of the current buffer, a ChangeLog file.
321 Actually, the narrowed region doesn't include the date line.
322 A \"page\" in a ChangeLog file is the area between two dates."
323 (or (eq major-mode 'change-log-mode)
324 (error "log-edit-narrow-changelog: current buffer isn't a ChangeLog"))
326 (goto-char (point-min))
328 ;; Skip date line and subsequent blank lines.
329 (forward-line 1)
330 (if (looking-at "[ \t\n]*\n")
331 (goto-char (match-end 0)))
333 (let ((start (point)))
334 (forward-page 1)
335 (narrow-to-region start (point))
336 (goto-char (point-min))))
338 (defun log-edit-changelog-paragraph ()
339 "Return the bounds of the ChangeLog paragraph containing point.
340 If we are between paragraphs, return the previous paragraph."
341 (save-excursion
342 (beginning-of-line)
343 (if (looking-at "^[ \t]*$")
344 (skip-chars-backward " \t\n" (point-min)))
345 (list (progn
346 (if (re-search-backward "^[ \t]*\n" nil 'or-to-limit)
347 (goto-char (match-end 0)))
348 (point))
349 (if (re-search-forward "^[ \t\n]*$" nil t)
350 (match-beginning 0)
351 (point)))))
353 (defun log-edit-changelog-subparagraph ()
354 "Return the bounds of the ChangeLog subparagraph containing point.
355 A subparagraph is a block of non-blank lines beginning with an asterisk.
356 If we are between sub-paragraphs, return the previous subparagraph."
357 (save-excursion
358 (end-of-line)
359 (if (search-backward "*" nil t)
360 (list (progn (beginning-of-line) (point))
361 (progn
362 (forward-line 1)
363 (if (re-search-forward "^[ \t]*[\n*]" nil t)
364 (match-beginning 0)
365 (point-max))))
366 (list (point) (point)))))
368 (defun log-edit-changelog-entry ()
369 "Return the bounds of the ChangeLog entry containing point.
370 The variable `log-edit-changelog-full-paragraphs' decides whether an
371 \"entry\" is a paragraph or a subparagraph; see its documentation string
372 for more details."
373 (if log-edit-changelog-full-paragraphs
374 (log-edit-changelog-paragraph)
375 (log-edit-changelog-subparagraph)))
377 (defvar user-full-name)
378 (defvar user-mail-address)
379 (defun log-edit-changelog-ours-p ()
380 "See if ChangeLog entry at point is for the current user, today.
381 Return non-nil iff it is."
382 ;; Code adapted from add-change-log-entry.
383 (let ((name (or (and (boundp 'add-log-full-name) add-log-full-name)
384 (and (fboundp 'user-full-name) (user-full-name))
385 (and (boundp 'user-full-name) user-full-name)))
386 (mail (or (and (boundp 'add-log-mailing-address) add-log-mailing-address)
387 ;;(and (fboundp 'user-mail-address) (user-mail-address))
388 (and (boundp 'user-mail-address) user-mail-address)))
389 (time (or (and (boundp 'add-log-time-format)
390 (functionp add-log-time-format)
391 (funcall add-log-time-format))
392 (format-time-string "%Y-%m-%d"))))
393 (looking-at (regexp-quote (format "%s %s <%s>" time name mail)))))
395 (defun log-edit-changelog-entries (file)
396 "Return the ChangeLog entries for FILE, and the ChangeLog they came from.
397 The return value looks like this:
398 (LOGBUFFER (ENTRYSTART . ENTRYEND) ...)
399 where LOGBUFFER is the name of the ChangeLog buffer, and each
400 \(ENTRYSTART . ENTRYEND\) pair is a buffer region."
401 (save-excursion
402 (let ((changelog-file-name
403 (let ((default-directory
404 (file-name-directory (expand-file-name file))))
405 ;; `find-change-log' uses `change-log-default-name' if set
406 ;; and sets it before exiting, so we need to work around
407 ;; that memoizing which is undesired here
408 (setq change-log-default-name nil)
409 (find-change-log))))
410 (set-buffer (find-file-noselect changelog-file-name))
411 (unless (eq major-mode 'change-log-mode) (change-log-mode))
412 (goto-char (point-min))
413 (if (looking-at "\\s-*\n") (goto-char (match-end 0)))
414 (if (not (log-edit-changelog-ours-p))
415 (list (current-buffer))
416 (save-restriction
417 (log-edit-narrow-changelog)
418 (goto-char (point-min))
420 ;; Search for the name of FILE relative to the ChangeLog. If that
421 ;; doesn't occur anywhere, they're not using full relative
422 ;; filenames in the ChangeLog, so just look for FILE; we'll accept
423 ;; some false positives.
424 (let ((pattern (file-relative-name
425 file (file-name-directory changelog-file-name))))
426 (if (or (string= pattern "")
427 (not (save-excursion
428 (search-forward pattern nil t))))
429 (setq pattern (file-name-nondirectory file)))
431 (let (texts)
432 (while (search-forward pattern nil t)
433 (let ((entry (log-edit-changelog-entry)))
434 (push entry texts)
435 (goto-char (elt entry 1))))
437 (cons (current-buffer) texts))))))))
439 (defun log-edit-changelog-insert-entries (buffer regions)
440 "Insert those regions in BUFFER specified in REGIONS.
441 Sort REGIONS front-to-back first."
442 (let ((regions (sort regions 'car-less-than-car))
443 (last))
444 (dolist (region regions)
445 (when (and last (< last (car region))) (newline))
446 (setq last (elt region 1))
447 (apply 'insert-buffer-substring buffer region))))
449 (defun log-edit-insert-changelog-entries (files)
450 "Given a list of files FILES, insert the ChangeLog entries for them."
451 (let ((buffer-entries nil))
453 ;; Add each buffer to buffer-entries, and associate it with the list
454 ;; of entries we want from that file.
455 (dolist (file files)
456 (let* ((entries (log-edit-changelog-entries file))
457 (pair (assq (car entries) buffer-entries)))
458 (if pair
459 (setcdr pair (cvs-union (cdr pair) (cdr entries)))
460 (push entries buffer-entries))))
462 ;; Now map over each buffer in buffer-entries, sort the entries for
463 ;; each buffer, and extract them as strings.
464 (dolist (buffer-entry buffer-entries)
465 (log-edit-changelog-insert-entries (car buffer-entry) (cdr buffer-entry))
466 (when (cdr buffer-entry) (newline)))))
468 (provide 'log-edit)
470 ;;; Change Log:
471 ;; $log$
473 ;;; log-edit.el ends here