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