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