(combine-run-hooks): New function.
[emacs.git] / lisp / log-edit.el
blob6b238835a9cc7548accd7e5dfc4fcba1b40483b1
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.8 2000/03/05 21:32:21 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 ;; - Remove a single leading `* <file>' in log-edit-insert-changelog
32 ;; - Move in VC's code
33 ;; - Add compatibility for VC's hook variables
34 ;; - add compatibility with cvs-edit.el
36 ;;; Code:
38 (eval-when-compile (require 'cl))
39 (require 'add-log) ; for all the ChangeLog goodies
40 (require 'pcvs-util)
41 (require 'ring)
42 (require 'vc)
44 ;;;;
45 ;;;; Global Variables
46 ;;;;
48 (defgroup log-edit nil
49 "Major mode for editing commit messages for PCL-CVS."
50 :group 'pcl-cvs
51 :prefix "log-edit-")
53 ;; compiler pacifiers
54 (defvar cvs-buffer)
56 (easy-mmode-defmap log-edit-mode-map
57 `(("\C-c\C-c" . log-edit-done)
58 ("\C-c\C-a" . log-edit-insert-changelog)
59 ("\C-c\C-f" . log-edit-show-files)
60 ("\C-c?" . log-edit-mode-help))
61 "Keymap for the `log-edit-mode' (used when editing cvs log messages)."
62 :group 'log-edit
63 :inherit (if (boundp 'vc-log-entry-mode) vc-log-entry-mode))
65 (defcustom log-edit-confirm t
66 "*If non-nil, `log-edit-done' will request confirmation.
67 If 'changed, only request confirmation if the list of files has
68 changed since the beginning of the log-edit session."
69 :group 'log-edit
70 :type '(choice (const changed) (const t) (const nil)))
72 (defcustom log-edit-keep-buffer nil
73 "*If non-nil, don't hide the buffer after `log-edit-done'."
74 :group 'log-edit
75 :type 'boolean)
77 (defvar cvs-commit-buffer-require-final-newline t
78 "Obsolete, use `log-edit-require-final-newline'.")
80 (defcustom log-edit-require-final-newline
81 cvs-commit-buffer-require-final-newline
82 "*Enforce a newline at the end of commit log messages.
83 Enforce it silently if t, query if non-nil and don't do anything if nil."
84 :group 'log-edit
85 :type '(choice (const ask) (const t) (const nil)))
87 (defcustom log-edit-setup-invert nil
88 "*Non-nil means `log-edit' should invert the meaning of its SETUP arg.
89 If SETUP is 'force, this variable has no effect."
90 :group 'log-edit
91 :type 'boolean)
93 (defcustom log-edit-hook '(log-edit-insert-cvs-template
94 log-edit-insert-changelog)
95 "*Hook run at the end of `log-edit'."
96 :group 'log-edit
97 :type '(hook :options (log-edit-insert-cvs-template
98 log-edit-insert-changelog)))
100 (defcustom log-edit-mode-hook nil
101 "*Hook run when entering `log-edit-mode'."
102 :group 'log-edit
103 :type 'hook)
105 (defcustom log-edit-done-hook nil
106 "*Hook run before doing the actual commit.
107 This hook can be used to cleanup the message, enforce various
108 conventions, or to allow recording the message in some other database,
109 such as a bug-tracking system. The list of files about to be committed
110 can be obtained from `log-edit-files'."
111 :group 'log-edit
112 :type '(hook :options (log-edit-delete-common-indentation
113 log-edit-add-to-changelog)))
115 (defvar cvs-changelog-full-paragraphs t
116 "*If non-nil, include full ChangeLog paragraphs in the CVS log.
117 This may be set in the ``local variables'' section of a ChangeLog, to
118 indicate the policy for that ChangeLog.
120 A ChangeLog paragraph is a bunch of log text containing no blank lines;
121 a paragraph usually describes a set of changes with a single purpose,
122 but perhaps spanning several functions in several files. Changes in
123 different paragraphs are unrelated.
125 You could argue that the CVS log entry for a file should contain the
126 full ChangeLog paragraph mentioning the change to the file, even though
127 it may mention other files, because that gives you the full context you
128 need to understand the change. This is the behaviour you get when this
129 variable is set to t.
131 On the other hand, you could argue that the CVS log entry for a change
132 should contain only the text for the changes which occurred in that
133 file, because the CVS log is per-file. This is the behaviour you get
134 when this variable is set to nil.")
136 ;;;; Internal global or buffer-local vars
138 (defconst log-edit-files-buf "*log-edit-files*")
139 (defvar log-edit-initial-files nil)
140 (defvar log-edit-callback nil)
141 (defvar log-edit-listfun nil)
143 ;;;;
144 ;;;; Actual code
145 ;;;;
147 ;;;###autoload
148 (defun log-edit (callback &optional setup listfun &rest ignore)
149 "Setup a buffer to enter a log message.
150 The buffer will be put in `log-edit-mode'.
151 If SETUP is non-nil, the buffer is then erased and `log-edit-hook' is run.
152 Mark and point will be set around the entire contents of the
153 buffer so that it is easy to kill the contents of the buffer with \\[kill-region].
154 Once you're done editing the message, pressing \\[log-edit-done] will call
155 `log-edit-done' which will end up calling CALLBACK to do the actual commit."
156 (when (and log-edit-setup-invert (not (eq setup 'force)))
157 (setq setup (not setup)))
158 (when setup (erase-buffer))
159 (log-edit-mode)
160 (set (make-local-variable 'log-edit-callback) callback)
161 (set (make-local-variable 'log-edit-listfun) listfun)
162 (when setup (run-hooks 'log-edit-hook))
163 (goto-char (point-min)) (push-mark (point-max))
164 (set (make-local-variable 'log-edit-initial-files) (log-edit-files))
165 (message (substitute-command-keys
166 "Press \\[log-edit-done] when you are done editing.")))
168 (define-derived-mode log-edit-mode text-mode "Log-Edit"
169 "Major mode for entering commit messages.
170 This mode is intended for entering messages in a *cvs-commit*
171 buffer when using PCL-CVS. It provides a binding for the
172 \\[log-edit-done] command that should be used when done editing
173 to trigger the actual commit, as well as a few handy support
174 commands.
175 \\{log-edit-mode-map}")
177 (defun log-edit-hide-buf (&optional buf where)
178 (when (setq buf (get-buffer (or buf log-edit-files-buf)))
179 (let ((win (get-buffer-window buf where)))
180 (if win (ignore-errors (delete-window win))))
181 (bury-buffer buf)))
183 (defun log-edit-done ()
184 "Finish editing the log message and commit the files.
185 This can only be used in the *cvs-commit* buffer.
186 With a prefix argument, prompt for cvs commit flags.
187 If you want to abort the commit, simply delete the buffer."
188 (interactive)
189 (if (and (> (point-max) 1)
190 (/= (char-after (1- (point-max))) ?\n)
191 (or (eq log-edit-require-final-newline t)
192 (and log-edit-require-final-newline
193 (y-or-n-p
194 (format "Buffer %s does not end in newline. Add one? "
195 (buffer-name))))))
196 (save-excursion
197 (goto-char (point-max))
198 (insert ?\n)))
199 (if (boundp 'vc-comment-ring) (ring-insert vc-comment-ring (buffer-string)))
200 (let ((win (get-buffer-window log-edit-files-buf)))
201 (if (and log-edit-confirm
202 (not (and (eq log-edit-confirm 'changed)
203 (equal (log-edit-files) log-edit-initial-files)))
204 (progn
205 (log-edit-show-files)
206 (not (y-or-n-p "Really commit ? "))))
207 (progn (when (not win) (log-edit-hide-buf))
208 (message "Oh, well! Later maybe?"))
209 (run-hooks 'log-edit-done-hook)
210 (log-edit-hide-buf)
211 (unless log-edit-keep-buffer
212 (cvs-bury-buffer (current-buffer)
213 (when (boundp 'cvs-buffer) cvs-buffer)))
214 (call-interactively log-edit-callback))))
216 (defun log-edit-files ()
217 "Return the list of files that are about to be committed."
218 (ignore-errors (funcall log-edit-listfun)))
221 (defun log-edit-insert-changelog ()
222 "Insert a log message by looking at the ChangeLog.
223 The idea is to write your ChangeLog entries first, and then use this
224 command to commit your changes.
226 To select default log text, we:
227 - find the ChangeLog entries for the files to be checked in,
228 - verify that the top entry in the ChangeLog is on the current date
229 and by the current user; if not, we don't provide any default text,
230 - search the ChangeLog entry for paragraphs containing the names of
231 the files we're checking in, and finally
232 - use those paragraphs as the log text."
233 (interactive)
234 (cvs-insert-changelog-entries (log-edit-files))
235 (log-edit-delete-common-indentation))
237 (defun log-edit-mode-help ()
238 "Provide help for the `log-edit-mode-map'."
239 (interactive)
240 (if (eq last-command 'log-edit-mode-help)
241 (describe-function major-mode)
242 (message
243 (substitute-command-keys
244 "Type `\\[log-edit-done]' to finish commit. Try `\\[describe-function] log-edit-done' for more help."))))
246 (defun log-edit-delete-common-indentation ()
247 "Unindent the current buffer rigidly until at least one line is flush left."
248 (save-excursion
249 (let ((common (point-max)))
250 (goto-char (point-min))
251 (while (< (point) (point-max))
252 (if (not (looking-at "^[ \t]*$"))
253 (setq common (min common (current-indentation))))
254 (forward-line 1))
255 (indent-rigidly (point-min) (point-max) (- common)))))
257 (defun log-edit-show-files ()
258 "Show the list of files to be committed."
259 (interactive)
260 (let* ((files (log-edit-files))
261 (editbuf (current-buffer))
262 (buf (get-buffer-create "*log-edit-files*")))
263 (with-current-buffer buf
264 (log-edit-hide-buf buf 'all)
265 (setq buffer-read-only nil)
266 (erase-buffer)
267 (insert (mapconcat 'identity files "\n"))
268 (setq buffer-read-only t)
269 (goto-char (point-min))
270 (save-selected-window
271 (cvs-pop-to-buffer-same-frame buf)
272 (shrink-window-if-larger-than-buffer)
273 (selected-window)))))
275 (defun log-edit-insert-cvs-template ()
276 "Insert the template specified by the CVS administrator, if any."
277 (interactive)
278 (when (file-readable-p "CVS/Template")
279 (insert-file-contents "CVS/Template")))
282 (defun log-edit-add-to-changelog ()
283 "Insert this log message into the appropriate ChangeLog file."
284 (interactive)
285 ;; Yuck!
286 (unless (string= (buffer-string) (ring-ref vc-comment-ring 0))
287 (ring-insert vc-comment-ring (buffer-string)))
288 (dolist (f (log-edit-files))
289 (let ((buffer-file-name (expand-file-name f)))
290 (save-excursion
291 (vc-comment-to-change-log)))))
293 ;;;;
294 ;;;; functions for getting commit message from ChangeLog a file...
295 ;;;; Courtesy Jim Blandy
296 ;;;;
298 (defun cvs-narrow-changelog ()
299 "Narrow to the top page of the current buffer, a ChangeLog file.
300 Actually, the narrowed region doesn't include the date line.
301 A \"page\" in a ChangeLog file is the area between two dates."
302 (or (eq major-mode 'change-log-mode)
303 (error "cvs-narrow-changelog: current buffer isn't a ChangeLog"))
305 (goto-char (point-min))
307 ;; Skip date line and subsequent blank lines.
308 (forward-line 1)
309 (if (looking-at "[ \t\n]*\n")
310 (goto-char (match-end 0)))
312 (let ((start (point)))
313 (forward-page 1)
314 (narrow-to-region start (point))
315 (goto-char (point-min))))
317 (defun cvs-changelog-paragraph ()
318 "Return the bounds of the ChangeLog paragraph containing point.
319 If we are between paragraphs, return the previous paragraph."
320 (save-excursion
321 (beginning-of-line)
322 (if (looking-at "^[ \t]*$")
323 (skip-chars-backward " \t\n" (point-min)))
324 (list (progn
325 (if (re-search-backward "^[ \t]*\n" nil 'or-to-limit)
326 (goto-char (match-end 0)))
327 (point))
328 (if (re-search-forward "^[ \t\n]*$" nil t)
329 (match-beginning 0)
330 (point)))))
332 (defun cvs-changelog-subparagraph ()
333 "Return the bounds of the ChangeLog subparagraph containing point.
334 A subparagraph is a block of non-blank lines beginning with an asterisk.
335 If we are between sub-paragraphs, return the previous subparagraph."
336 (save-excursion
337 (end-of-line)
338 (if (search-backward "*" nil t)
339 (list (progn (beginning-of-line) (point))
340 (progn
341 (forward-line 1)
342 (if (re-search-forward "^[ \t]*[\n*]" nil t)
343 (match-beginning 0)
344 (point-max))))
345 (list (point) (point)))))
347 (defun cvs-changelog-entry ()
348 "Return the bounds of the ChangeLog entry containing point.
349 The variable `cvs-changelog-full-paragraphs' decides whether an
350 \"entry\" is a paragraph or a subparagraph; see its documentation string
351 for more details."
352 (if cvs-changelog-full-paragraphs
353 (cvs-changelog-paragraph)
354 (cvs-changelog-subparagraph)))
356 (defvar user-full-name)
357 (defvar user-mail-address)
358 (defun cvs-changelog-ours-p ()
359 "See if ChangeLog entry at point is for the current user, today.
360 Return non-nil iff it is."
361 ;; Code adapted from add-change-log-entry.
362 (let ((name (or (and (boundp 'add-log-full-name) add-log-full-name)
363 (and (fboundp 'user-full-name) (user-full-name))
364 (and (boundp 'user-full-name) user-full-name)))
365 (mail (or (and (boundp 'add-log-mailing-address) add-log-mailing-address)
366 ;;(and (fboundp 'user-mail-address) (user-mail-address))
367 (and (boundp 'user-mail-address) user-mail-address)))
368 (time (or (and (boundp 'add-log-time-format)
369 (functionp add-log-time-format)
370 (funcall add-log-time-format))
371 (format-time-string "%Y-%m-%d"))))
372 (looking-at (regexp-quote (format "%s %s <%s>" time name mail)))))
374 (defun cvs-changelog-entries (file)
375 "Return the ChangeLog entries for FILE, and the ChangeLog they came from.
376 The return value looks like this:
377 (LOGBUFFER (ENTRYSTART . ENTRYEND) ...)
378 where LOGBUFFER is the name of the ChangeLog buffer, and each
379 \(ENTRYSTART . ENTRYEND\) pair is a buffer region."
380 (save-excursion
381 (let ((changelog-file-name
382 (let ((default-directory
383 (file-name-directory (expand-file-name file))))
384 ;; `find-change-log' uses `change-log-default-name' if set
385 ;; and sets it before exiting, so we need to work around
386 ;; that memoizing which is undesired here
387 (setq change-log-default-name nil)
388 (find-change-log))))
389 (set-buffer (find-file-noselect changelog-file-name))
390 (unless (eq major-mode 'change-log-mode) (change-log-mode))
391 (goto-char (point-min))
392 (if (looking-at "\\s-*\n") (goto-char (match-end 0)))
393 (if (not (cvs-changelog-ours-p))
394 (list (current-buffer))
395 (save-restriction
396 (cvs-narrow-changelog)
397 (goto-char (point-min))
399 ;; Search for the name of FILE relative to the ChangeLog. If that
400 ;; doesn't occur anywhere, they're not using full relative
401 ;; filenames in the ChangeLog, so just look for FILE; we'll accept
402 ;; some false positives.
403 (let ((pattern (file-relative-name
404 file (file-name-directory changelog-file-name))))
405 (if (or (string= pattern "")
406 (not (save-excursion
407 (search-forward pattern nil t))))
408 (setq pattern (file-name-nondirectory file)))
410 (let (texts)
411 (while (search-forward pattern nil t)
412 (let ((entry (cvs-changelog-entry)))
413 (push entry texts)
414 (goto-char (elt entry 1))))
416 (cons (current-buffer) texts))))))))
418 (defun cvs-changelog-insert-entries (buffer regions)
419 "Insert those regions in BUFFER specified in REGIONS.
420 Sort REGIONS front-to-back first."
421 (let ((regions (sort regions 'car-less-than-car))
422 (last))
423 (dolist (region regions)
424 (when (and last (< last (car region))) (newline))
425 (setq last (elt region 1))
426 (apply 'insert-buffer-substring buffer region))))
428 (defun cvs-insert-changelog-entries (files)
429 "Given a list of files FILES, insert the ChangeLog entries for them."
430 (let ((buffer-entries nil))
432 ;; Add each buffer to buffer-entries, and associate it with the list
433 ;; of entries we want from that file.
434 (dolist (file files)
435 (let* ((entries (cvs-changelog-entries file))
436 (pair (assq (car entries) buffer-entries)))
437 (if pair
438 (setcdr pair (cvs-union (cdr pair) (cdr entries)))
439 (push entries buffer-entries))))
441 ;; Now map over each buffer in buffer-entries, sort the entries for
442 ;; each buffer, and extract them as strings.
443 (dolist (buffer-entry buffer-entries)
444 (cvs-changelog-insert-entries (car buffer-entry) (cdr buffer-entry))
445 (when (cdr buffer-entry) (newline)))))
447 (provide 'log-edit)
448 ;;; log-edit.el ends here