]> code.delx.au - gnu-emacs/blob - lisp/log-edit.el
(feature-symbols, file-provides, file-requires): Use mapc.
[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.4 2000/05/10 22:20:51 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 ;; Get rid of trailing empty lines
193 (goto-char (point-max))
194 (skip-syntax-backward " ")
195 (when (equal (char-after) ?\n) (forward-char 1))
196 (delete-region (point) (point-max))
197 ;; Check for final newline
198 (if (and (> (point-max) 1)
199 (/= (char-after (1- (point-max))) ?\n)
200 (or (eq log-edit-require-final-newline t)
201 (and log-edit-require-final-newline
202 (y-or-n-p
203 (format "Buffer %s does not end in newline. Add one? "
204 (buffer-name))))))
205 (save-excursion
206 (goto-char (point-max))
207 (insert ?\n)))
208 (if (boundp 'vc-comment-ring) (ring-insert vc-comment-ring (buffer-string)))
209 (let ((win (get-buffer-window log-edit-files-buf)))
210 (if (and log-edit-confirm
211 (not (and (eq log-edit-confirm 'changed)
212 (equal (log-edit-files) log-edit-initial-files)))
213 (progn
214 (log-edit-show-files)
215 (not (y-or-n-p "Really commit ? "))))
216 (progn (when (not win) (log-edit-hide-buf))
217 (message "Oh, well! Later maybe?"))
218 (run-hooks 'log-edit-done-hook)
219 (log-edit-hide-buf)
220 (unless log-edit-keep-buffer
221 (cvs-bury-buffer (current-buffer)
222 (when (boundp 'cvs-buffer) cvs-buffer)))
223 (call-interactively log-edit-callback))))
224
225 (defun log-edit-files ()
226 "Return the list of files that are about to be committed."
227 (ignore-errors (funcall log-edit-listfun)))
228
229
230 (defun log-edit-insert-changelog ()
231 "Insert a log message by looking at the ChangeLog.
232 The idea is to write your ChangeLog entries first, and then use this
233 command to commit your changes.
234
235 To select default log text, we:
236 - find the ChangeLog entries for the files to be checked in,
237 - verify that the top entry in the ChangeLog is on the current date
238 and by the current user; if not, we don't provide any default text,
239 - search the ChangeLog entry for paragraphs containing the names of
240 the files we're checking in, and finally
241 - use those paragraphs as the log text."
242 (interactive)
243 (log-edit-insert-changelog-entries (log-edit-files))
244 (log-edit-delete-common-indentation)
245 (goto-char (point-min))
246 (when (looking-at "\\*\\s-+")
247 (forward-line 1)
248 (when (not (re-search-forward "^\\*\\s-+" nil t))
249 (goto-char (point-min))
250 (skip-chars-forward "^():")
251 (skip-chars-forward ": ")
252 (delete-region (point-min) (point)))))
253
254 (defun log-edit-mode-help ()
255 "Provide help for the `log-edit-mode-map'."
256 (interactive)
257 (if (eq last-command 'log-edit-mode-help)
258 (describe-function major-mode)
259 (message
260 (substitute-command-keys
261 "Type `\\[log-edit-done]' to finish commit. Try `\\[describe-function] log-edit-done' for more help."))))
262
263 (defun log-edit-delete-common-indentation ()
264 "Unindent the current buffer rigidly until at least one line is flush left."
265 (save-excursion
266 (let ((common (point-max)))
267 (goto-char (point-min))
268 (while (< (point) (point-max))
269 (if (not (looking-at "^[ \t]*$"))
270 (setq common (min common (current-indentation))))
271 (forward-line 1))
272 (indent-rigidly (point-min) (point-max) (- common)))))
273
274 (defun log-edit-show-files ()
275 "Show the list of files to be committed."
276 (interactive)
277 (let* ((files (log-edit-files))
278 (editbuf (current-buffer))
279 (buf (get-buffer-create "*log-edit-files*")))
280 (with-current-buffer buf
281 (log-edit-hide-buf buf 'all)
282 (setq buffer-read-only nil)
283 (erase-buffer)
284 (insert (mapconcat 'identity files "\n"))
285 (setq buffer-read-only t)
286 (goto-char (point-min))
287 (save-selected-window
288 (cvs-pop-to-buffer-same-frame buf)
289 (shrink-window-if-larger-than-buffer)
290 (selected-window)))))
291
292 (defun log-edit-insert-cvs-template ()
293 "Insert the template specified by the CVS administrator, if any."
294 (interactive)
295 (when (file-readable-p "CVS/Template")
296 (insert-file-contents "CVS/Template")))
297
298
299 (defun log-edit-add-to-changelog ()
300 "Insert this log message into the appropriate ChangeLog file."
301 (interactive)
302 ;; Yuck!
303 (unless (string= (buffer-string) (ring-ref vc-comment-ring 0))
304 (ring-insert vc-comment-ring (buffer-string)))
305 (dolist (f (log-edit-files))
306 (let ((buffer-file-name (expand-file-name f)))
307 (save-excursion
308 (vc-comment-to-change-log)))))
309
310 ;;;;
311 ;;;; functions for getting commit message from ChangeLog a file...
312 ;;;; Courtesy Jim Blandy
313 ;;;;
314
315 (defun log-edit-narrow-changelog ()
316 "Narrow to the top page of the current buffer, a ChangeLog file.
317 Actually, the narrowed region doesn't include the date line.
318 A \"page\" in a ChangeLog file is the area between two dates."
319 (or (eq major-mode 'change-log-mode)
320 (error "log-edit-narrow-changelog: current buffer isn't a ChangeLog"))
321
322 (goto-char (point-min))
323
324 ;; Skip date line and subsequent blank lines.
325 (forward-line 1)
326 (if (looking-at "[ \t\n]*\n")
327 (goto-char (match-end 0)))
328
329 (let ((start (point)))
330 (forward-page 1)
331 (narrow-to-region start (point))
332 (goto-char (point-min))))
333
334 (defun log-edit-changelog-paragraph ()
335 "Return the bounds of the ChangeLog paragraph containing point.
336 If we are between paragraphs, return the previous paragraph."
337 (save-excursion
338 (beginning-of-line)
339 (if (looking-at "^[ \t]*$")
340 (skip-chars-backward " \t\n" (point-min)))
341 (list (progn
342 (if (re-search-backward "^[ \t]*\n" nil 'or-to-limit)
343 (goto-char (match-end 0)))
344 (point))
345 (if (re-search-forward "^[ \t\n]*$" nil t)
346 (match-beginning 0)
347 (point)))))
348
349 (defun log-edit-changelog-subparagraph ()
350 "Return the bounds of the ChangeLog subparagraph containing point.
351 A subparagraph is a block of non-blank lines beginning with an asterisk.
352 If we are between sub-paragraphs, return the previous subparagraph."
353 (save-excursion
354 (end-of-line)
355 (if (search-backward "*" nil t)
356 (list (progn (beginning-of-line) (point))
357 (progn
358 (forward-line 1)
359 (if (re-search-forward "^[ \t]*[\n*]" nil t)
360 (match-beginning 0)
361 (point-max))))
362 (list (point) (point)))))
363
364 (defun log-edit-changelog-entry ()
365 "Return the bounds of the ChangeLog entry containing point.
366 The variable `log-edit-changelog-full-paragraphs' decides whether an
367 \"entry\" is a paragraph or a subparagraph; see its documentation string
368 for more details."
369 (if log-edit-changelog-full-paragraphs
370 (log-edit-changelog-paragraph)
371 (log-edit-changelog-subparagraph)))
372
373 (defvar user-full-name)
374 (defvar user-mail-address)
375 (defun log-edit-changelog-ours-p ()
376 "See if ChangeLog entry at point is for the current user, today.
377 Return non-nil iff it is."
378 ;; Code adapted from add-change-log-entry.
379 (let ((name (or (and (boundp 'add-log-full-name) add-log-full-name)
380 (and (fboundp 'user-full-name) (user-full-name))
381 (and (boundp 'user-full-name) user-full-name)))
382 (mail (or (and (boundp 'add-log-mailing-address) add-log-mailing-address)
383 ;;(and (fboundp 'user-mail-address) (user-mail-address))
384 (and (boundp 'user-mail-address) user-mail-address)))
385 (time (or (and (boundp 'add-log-time-format)
386 (functionp add-log-time-format)
387 (funcall add-log-time-format))
388 (format-time-string "%Y-%m-%d"))))
389 (looking-at (regexp-quote (format "%s %s <%s>" time name mail)))))
390
391 (defun log-edit-changelog-entries (file)
392 "Return the ChangeLog entries for FILE, and the ChangeLog they came from.
393 The return value looks like this:
394 (LOGBUFFER (ENTRYSTART . ENTRYEND) ...)
395 where LOGBUFFER is the name of the ChangeLog buffer, and each
396 \(ENTRYSTART . ENTRYEND\) pair is a buffer region."
397 (save-excursion
398 (let ((changelog-file-name
399 (let ((default-directory
400 (file-name-directory (expand-file-name file))))
401 ;; `find-change-log' uses `change-log-default-name' if set
402 ;; and sets it before exiting, so we need to work around
403 ;; that memoizing which is undesired here
404 (setq change-log-default-name nil)
405 (find-change-log))))
406 (set-buffer (find-file-noselect changelog-file-name))
407 (unless (eq major-mode 'change-log-mode) (change-log-mode))
408 (goto-char (point-min))
409 (if (looking-at "\\s-*\n") (goto-char (match-end 0)))
410 (if (not (log-edit-changelog-ours-p))
411 (list (current-buffer))
412 (save-restriction
413 (log-edit-narrow-changelog)
414 (goto-char (point-min))
415
416 ;; Search for the name of FILE relative to the ChangeLog. If that
417 ;; doesn't occur anywhere, they're not using full relative
418 ;; filenames in the ChangeLog, so just look for FILE; we'll accept
419 ;; some false positives.
420 (let ((pattern (file-relative-name
421 file (file-name-directory changelog-file-name))))
422 (if (or (string= pattern "")
423 (not (save-excursion
424 (search-forward pattern nil t))))
425 (setq pattern (file-name-nondirectory file)))
426
427 (let (texts)
428 (while (search-forward pattern nil t)
429 (let ((entry (log-edit-changelog-entry)))
430 (push entry texts)
431 (goto-char (elt entry 1))))
432
433 (cons (current-buffer) texts))))))))
434
435 (defun log-edit-changelog-insert-entries (buffer regions)
436 "Insert those regions in BUFFER specified in REGIONS.
437 Sort REGIONS front-to-back first."
438 (let ((regions (sort regions 'car-less-than-car))
439 (last))
440 (dolist (region regions)
441 (when (and last (< last (car region))) (newline))
442 (setq last (elt region 1))
443 (apply 'insert-buffer-substring buffer region))))
444
445 (defun log-edit-insert-changelog-entries (files)
446 "Given a list of files FILES, insert the ChangeLog entries for them."
447 (let ((buffer-entries nil))
448
449 ;; Add each buffer to buffer-entries, and associate it with the list
450 ;; of entries we want from that file.
451 (dolist (file files)
452 (let* ((entries (log-edit-changelog-entries file))
453 (pair (assq (car entries) buffer-entries)))
454 (if pair
455 (setcdr pair (cvs-union (cdr pair) (cdr entries)))
456 (push entries buffer-entries))))
457
458 ;; Now map over each buffer in buffer-entries, sort the entries for
459 ;; each buffer, and extract them as strings.
460 (dolist (buffer-entry buffer-entries)
461 (log-edit-changelog-insert-entries (car buffer-entry) (cdr buffer-entry))
462 (when (cdr buffer-entry) (newline)))))
463
464 (provide 'log-edit)
465
466 ;;; Change Log:
467 ;; $log$
468
469 ;;; log-edit.el ends here