(enum re_opcode_t): New opcode on_failure_jump_nastyloop.
[emacs.git] / lisp / log-edit.el
blob48c327df5468c6b5a38e541c84eed8819418e431
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.2 2000/03/26 21:19:58 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
64 (if (boundp 'vc-log-mode-map) vc-log-mode-map)))
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)))
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)
78 (defvar cvs-commit-buffer-require-final-newline t
79 "Obsolete, use `log-edit-require-final-newline'.")
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)))
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)
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)))
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)
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)))
116 (defvar cvs-changelog-full-paragraphs t
117 "*If non-nil, include full ChangeLog paragraphs in the CVS log.
118 This may be set in the ``local variables'' section of a ChangeLog, to
119 indicate the policy for that ChangeLog.
121 A ChangeLog paragraph is a bunch of log text containing no blank lines;
122 a paragraph usually describes a set of changes with a single purpose,
123 but perhaps spanning several functions in several files. Changes in
124 different paragraphs are unrelated.
126 You could argue that the CVS log entry for a file should contain the
127 full ChangeLog paragraph mentioning the change to the file, even though
128 it may mention other files, because that gives you the full context you
129 need to understand the change. This is the behaviour you get when this
130 variable is set to t.
132 On the other hand, you could argue that the CVS log entry for a change
133 should contain only the text for the changes which occurred in that
134 file, because the CVS log is per-file. This is the behaviour you get
135 when this variable is set to nil.")
137 ;;;; Internal global or buffer-local vars
139 (defconst log-edit-files-buf "*log-edit-files*")
140 (defvar log-edit-initial-files nil)
141 (defvar log-edit-callback nil)
142 (defvar log-edit-listfun nil)
144 ;;;;
145 ;;;; Actual code
146 ;;;;
148 ;;;###autoload
149 (defun log-edit (callback &optional setup listfun &rest ignore)
150 "Setup a buffer to enter a log message.
151 The buffer will be put in `log-edit-mode'.
152 If SETUP is non-nil, the buffer is then erased and `log-edit-hook' is run.
153 Mark and point will be set around the entire contents of the
154 buffer so that it is easy to kill the contents of the buffer with \\[kill-region].
155 Once you're done editing the message, pressing \\[log-edit-done] will call
156 `log-edit-done' which will end up calling CALLBACK to do the actual commit."
157 (when (and log-edit-setup-invert (not (eq setup 'force)))
158 (setq setup (not setup)))
159 (when setup (erase-buffer))
160 (log-edit-mode)
161 (set (make-local-variable 'log-edit-callback) callback)
162 (set (make-local-variable 'log-edit-listfun) listfun)
163 (when setup (run-hooks 'log-edit-hook))
164 (goto-char (point-min)) (push-mark (point-max))
165 (set (make-local-variable 'log-edit-initial-files) (log-edit-files))
166 (message (substitute-command-keys
167 "Press \\[log-edit-done] when you are done editing.")))
169 (define-derived-mode log-edit-mode text-mode "Log-Edit"
170 "Major mode for editing version-control log messages.
171 When done editing the log entry, just type \\[log-edit-done] which
172 will trigger the actual commit of the file(s).
173 Several other handy support commands are provided of course and
174 the package from which this is used might also provide additional
175 commands (under C-x v for VC, for example).
177 \\{log-edit-mode-map}")
179 (defun log-edit-hide-buf (&optional buf where)
180 (when (setq buf (get-buffer (or buf log-edit-files-buf)))
181 (let ((win (get-buffer-window buf where)))
182 (if win (ignore-errors (delete-window win))))
183 (bury-buffer buf)))
185 (defun log-edit-done ()
186 "Finish editing the log message and commit the files.
187 This can only be used in the *cvs-commit* buffer.
188 With a prefix argument, prompt for cvs commit flags.
189 If you want to abort the commit, simply delete the buffer."
190 (interactive)
191 (if (and (> (point-max) 1)
192 (/= (char-after (1- (point-max))) ?\n)
193 (or (eq log-edit-require-final-newline t)
194 (and log-edit-require-final-newline
195 (y-or-n-p
196 (format "Buffer %s does not end in newline. Add one? "
197 (buffer-name))))))
198 (save-excursion
199 (goto-char (point-max))
200 (insert ?\n)))
201 (if (boundp 'vc-comment-ring) (ring-insert vc-comment-ring (buffer-string)))
202 (let ((win (get-buffer-window log-edit-files-buf)))
203 (if (and log-edit-confirm
204 (not (and (eq log-edit-confirm 'changed)
205 (equal (log-edit-files) log-edit-initial-files)))
206 (progn
207 (log-edit-show-files)
208 (not (y-or-n-p "Really commit ? "))))
209 (progn (when (not win) (log-edit-hide-buf))
210 (message "Oh, well! Later maybe?"))
211 (run-hooks 'log-edit-done-hook)
212 (log-edit-hide-buf)
213 (unless log-edit-keep-buffer
214 (cvs-bury-buffer (current-buffer)
215 (when (boundp 'cvs-buffer) cvs-buffer)))
216 (call-interactively log-edit-callback))))
218 (defun log-edit-files ()
219 "Return the list of files that are about to be committed."
220 (ignore-errors (funcall log-edit-listfun)))
223 (defun log-edit-insert-changelog ()
224 "Insert a log message by looking at the ChangeLog.
225 The idea is to write your ChangeLog entries first, and then use this
226 command to commit your changes.
228 To select default log text, we:
229 - find the ChangeLog entries for the files to be checked in,
230 - verify that the top entry in the ChangeLog is on the current date
231 and by the current user; if not, we don't provide any default text,
232 - search the ChangeLog entry for paragraphs containing the names of
233 the files we're checking in, and finally
234 - use those paragraphs as the log text."
235 (interactive)
236 (cvs-insert-changelog-entries (log-edit-files))
237 (log-edit-delete-common-indentation))
239 (defun log-edit-mode-help ()
240 "Provide help for the `log-edit-mode-map'."
241 (interactive)
242 (if (eq last-command 'log-edit-mode-help)
243 (describe-function major-mode)
244 (message
245 (substitute-command-keys
246 "Type `\\[log-edit-done]' to finish commit. Try `\\[describe-function] log-edit-done' for more help."))))
248 (defun log-edit-delete-common-indentation ()
249 "Unindent the current buffer rigidly until at least one line is flush left."
250 (save-excursion
251 (let ((common (point-max)))
252 (goto-char (point-min))
253 (while (< (point) (point-max))
254 (if (not (looking-at "^[ \t]*$"))
255 (setq common (min common (current-indentation))))
256 (forward-line 1))
257 (indent-rigidly (point-min) (point-max) (- common)))))
259 (defun log-edit-show-files ()
260 "Show the list of files to be committed."
261 (interactive)
262 (let* ((files (log-edit-files))
263 (editbuf (current-buffer))
264 (buf (get-buffer-create "*log-edit-files*")))
265 (with-current-buffer buf
266 (log-edit-hide-buf buf 'all)
267 (setq buffer-read-only nil)
268 (erase-buffer)
269 (insert (mapconcat 'identity files "\n"))
270 (setq buffer-read-only t)
271 (goto-char (point-min))
272 (save-selected-window
273 (cvs-pop-to-buffer-same-frame buf)
274 (shrink-window-if-larger-than-buffer)
275 (selected-window)))))
277 (defun log-edit-insert-cvs-template ()
278 "Insert the template specified by the CVS administrator, if any."
279 (interactive)
280 (when (file-readable-p "CVS/Template")
281 (insert-file-contents "CVS/Template")))
284 (defun log-edit-add-to-changelog ()
285 "Insert this log message into the appropriate ChangeLog file."
286 (interactive)
287 ;; Yuck!
288 (unless (string= (buffer-string) (ring-ref vc-comment-ring 0))
289 (ring-insert vc-comment-ring (buffer-string)))
290 (dolist (f (log-edit-files))
291 (let ((buffer-file-name (expand-file-name f)))
292 (save-excursion
293 (vc-comment-to-change-log)))))
295 ;;;;
296 ;;;; functions for getting commit message from ChangeLog a file...
297 ;;;; Courtesy Jim Blandy
298 ;;;;
300 (defun cvs-narrow-changelog ()
301 "Narrow to the top page of the current buffer, a ChangeLog file.
302 Actually, the narrowed region doesn't include the date line.
303 A \"page\" in a ChangeLog file is the area between two dates."
304 (or (eq major-mode 'change-log-mode)
305 (error "cvs-narrow-changelog: current buffer isn't a ChangeLog"))
307 (goto-char (point-min))
309 ;; Skip date line and subsequent blank lines.
310 (forward-line 1)
311 (if (looking-at "[ \t\n]*\n")
312 (goto-char (match-end 0)))
314 (let ((start (point)))
315 (forward-page 1)
316 (narrow-to-region start (point))
317 (goto-char (point-min))))
319 (defun cvs-changelog-paragraph ()
320 "Return the bounds of the ChangeLog paragraph containing point.
321 If we are between paragraphs, return the previous paragraph."
322 (save-excursion
323 (beginning-of-line)
324 (if (looking-at "^[ \t]*$")
325 (skip-chars-backward " \t\n" (point-min)))
326 (list (progn
327 (if (re-search-backward "^[ \t]*\n" nil 'or-to-limit)
328 (goto-char (match-end 0)))
329 (point))
330 (if (re-search-forward "^[ \t\n]*$" nil t)
331 (match-beginning 0)
332 (point)))))
334 (defun cvs-changelog-subparagraph ()
335 "Return the bounds of the ChangeLog subparagraph containing point.
336 A subparagraph is a block of non-blank lines beginning with an asterisk.
337 If we are between sub-paragraphs, return the previous subparagraph."
338 (save-excursion
339 (end-of-line)
340 (if (search-backward "*" nil t)
341 (list (progn (beginning-of-line) (point))
342 (progn
343 (forward-line 1)
344 (if (re-search-forward "^[ \t]*[\n*]" nil t)
345 (match-beginning 0)
346 (point-max))))
347 (list (point) (point)))))
349 (defun cvs-changelog-entry ()
350 "Return the bounds of the ChangeLog entry containing point.
351 The variable `cvs-changelog-full-paragraphs' decides whether an
352 \"entry\" is a paragraph or a subparagraph; see its documentation string
353 for more details."
354 (if cvs-changelog-full-paragraphs
355 (cvs-changelog-paragraph)
356 (cvs-changelog-subparagraph)))
358 (defvar user-full-name)
359 (defvar user-mail-address)
360 (defun cvs-changelog-ours-p ()
361 "See if ChangeLog entry at point is for the current user, today.
362 Return non-nil iff it is."
363 ;; Code adapted from add-change-log-entry.
364 (let ((name (or (and (boundp 'add-log-full-name) add-log-full-name)
365 (and (fboundp 'user-full-name) (user-full-name))
366 (and (boundp 'user-full-name) user-full-name)))
367 (mail (or (and (boundp 'add-log-mailing-address) add-log-mailing-address)
368 ;;(and (fboundp 'user-mail-address) (user-mail-address))
369 (and (boundp 'user-mail-address) user-mail-address)))
370 (time (or (and (boundp 'add-log-time-format)
371 (functionp add-log-time-format)
372 (funcall add-log-time-format))
373 (format-time-string "%Y-%m-%d"))))
374 (looking-at (regexp-quote (format "%s %s <%s>" time name mail)))))
376 (defun cvs-changelog-entries (file)
377 "Return the ChangeLog entries for FILE, and the ChangeLog they came from.
378 The return value looks like this:
379 (LOGBUFFER (ENTRYSTART . ENTRYEND) ...)
380 where LOGBUFFER is the name of the ChangeLog buffer, and each
381 \(ENTRYSTART . ENTRYEND\) pair is a buffer region."
382 (save-excursion
383 (let ((changelog-file-name
384 (let ((default-directory
385 (file-name-directory (expand-file-name file))))
386 ;; `find-change-log' uses `change-log-default-name' if set
387 ;; and sets it before exiting, so we need to work around
388 ;; that memoizing which is undesired here
389 (setq change-log-default-name nil)
390 (find-change-log))))
391 (set-buffer (find-file-noselect changelog-file-name))
392 (unless (eq major-mode 'change-log-mode) (change-log-mode))
393 (goto-char (point-min))
394 (if (looking-at "\\s-*\n") (goto-char (match-end 0)))
395 (if (not (cvs-changelog-ours-p))
396 (list (current-buffer))
397 (save-restriction
398 (cvs-narrow-changelog)
399 (goto-char (point-min))
401 ;; Search for the name of FILE relative to the ChangeLog. If that
402 ;; doesn't occur anywhere, they're not using full relative
403 ;; filenames in the ChangeLog, so just look for FILE; we'll accept
404 ;; some false positives.
405 (let ((pattern (file-relative-name
406 file (file-name-directory changelog-file-name))))
407 (if (or (string= pattern "")
408 (not (save-excursion
409 (search-forward pattern nil t))))
410 (setq pattern (file-name-nondirectory file)))
412 (let (texts)
413 (while (search-forward pattern nil t)
414 (let ((entry (cvs-changelog-entry)))
415 (push entry texts)
416 (goto-char (elt entry 1))))
418 (cons (current-buffer) texts))))))))
420 (defun cvs-changelog-insert-entries (buffer regions)
421 "Insert those regions in BUFFER specified in REGIONS.
422 Sort REGIONS front-to-back first."
423 (let ((regions (sort regions 'car-less-than-car))
424 (last))
425 (dolist (region regions)
426 (when (and last (< last (car region))) (newline))
427 (setq last (elt region 1))
428 (apply 'insert-buffer-substring buffer region))))
430 (defun cvs-insert-changelog-entries (files)
431 "Given a list of files FILES, insert the ChangeLog entries for them."
432 (let ((buffer-entries nil))
434 ;; Add each buffer to buffer-entries, and associate it with the list
435 ;; of entries we want from that file.
436 (dolist (file files)
437 (let* ((entries (cvs-changelog-entries file))
438 (pair (assq (car entries) buffer-entries)))
439 (if pair
440 (setcdr pair (cvs-union (cdr pair) (cdr entries)))
441 (push entries buffer-entries))))
443 ;; Now map over each buffer in buffer-entries, sort the entries for
444 ;; each buffer, and extract them as strings.
445 (dolist (buffer-entry buffer-entries)
446 (cvs-changelog-insert-entries (car buffer-entry) (cdr buffer-entry))
447 (when (cdr buffer-entry) (newline)))))
449 (provide 'log-edit)
451 ;;; Change Log:
452 ;; $log$
454 ;;; log-edit.el ends here