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