(PER_CHAR_METRIC): Removed because not used.
[emacs.git] / lisp / log-edit.el
blob824eaae3f0e0de703708cf191b219951df9892d4
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.10 2000/11/03 20:12:09 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 ;; - Move in VC's code
32 ;; - Add compatibility for VC's hook variables
34 ;;; Code:
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)
42 ;;;;
43 ;;;; Global Variables
44 ;;;;
46 (defgroup log-edit nil
47 "Major mode for editing RCS and CVS commit messages."
48 :group 'pcl-cvs
49 :group 'vc ; It's used by VC.
50 :version "21.1"
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 (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]))
83 (defcustom log-edit-confirm 'changed
84 "*If non-nil, `log-edit-done' will request confirmation.
85 If '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)))
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)
95 (defvar cvs-commit-buffer-require-final-newline t
96 "Obsolete, use `log-edit-require-final-newline'.")
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.
101 Enforce 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)))
105 (defcustom log-edit-setup-invert nil
106 "*Non-nil means `log-edit' should invert the meaning of its SETUP arg.
107 If SETUP is 'force, this variable has no effect."
108 :group 'log-edit
109 :type 'boolean)
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)))
118 (defcustom log-edit-mode-hook (if (boundp 'vc-log-mode-hook) vc-log-mode-hook)
119 "*Hook run when entering `log-edit-mode'."
120 :group 'log-edit
121 :type 'hook)
123 (defcustom log-edit-done-hook nil
124 "*Hook run before doing the actual commit.
125 This hook can be used to cleanup the message, enforce various
126 conventions, or to allow recording the message in some other database,
127 such as a bug-tracking system. The list of files about to be committed
128 can 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)))
133 (defvar cvs-changelog-full-paragraphs t
134 "Obsolete, use `log-edit-changelog-full-paragraphs'.")
136 (defvar log-edit-changelog-full-paragraphs cvs-changelog-full-paragraphs
137 "*If non-nil, include full ChangeLog paragraphs in the log.
138 This may be set in the ``local variables'' section of a ChangeLog, to
139 indicate the policy for that ChangeLog.
141 A ChangeLog paragraph is a bunch of log text containing no blank lines;
142 a paragraph usually describes a set of changes with a single purpose,
143 but perhaps spanning several functions in several files. Changes in
144 different paragraphs are unrelated.
146 You could argue that the log entry for a file should contain the
147 full ChangeLog paragraph mentioning the change to the file, even though
148 it may mention other files, because that gives you the full context you
149 need to understand the change. This is the behaviour you get when this
150 variable is set to t.
152 On the other hand, you could argue that the log entry for a change
153 should contain only the text for the changes which occurred in that
154 file, because the log is per-file. This is the behaviour you get
155 when this variable is set to nil.")
157 ;;;; Internal global or buffer-local vars
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)
164 ;;;;
165 ;;;; Actual code
166 ;;;;
168 ;;;###autoload
169 (defun log-edit (callback &optional setup listfun &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 (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.")))
189 (define-derived-mode log-edit-mode text-mode "Log-Edit"
190 "Major mode for editing version-control log messages.
191 When done editing the log entry, just type \\[log-edit-done] which
192 will trigger the actual commit of the file(s).
193 Several other handy support commands are provided of course and
194 the package from which this is used might also provide additional
195 commands (under C-x v for VC, for example).
197 \\{log-edit-mode-map}"
198 (make-local-variable 'vc-comment-ring-index))
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)))
206 (defun log-edit-done ()
207 "Finish editing the log message and commit the files.
208 If you want to abort the commit, simply delete the buffer."
209 (interactive)
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
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)))
226 (let ((comment (buffer-string)))
227 (when (and (boundp 'vc-comment-ring)
228 (ring-p vc-comment-ring)
229 (not (ring-empty-p vc-comment-ring))
230 (not (equal comment (ring-ref vc-comment-ring 0))))
231 (ring-insert vc-comment-ring comment)))
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))))
248 (defun log-edit-files ()
249 "Return the list of files that are about to be committed."
250 (ignore-errors (funcall log-edit-listfun)))
253 (defun log-edit-insert-changelog ()
254 "Insert a log message by looking at the ChangeLog.
255 The idea is to write your ChangeLog entries first, and then use this
256 command to commit your changes.
258 To 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)
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 "^():")
274 (skip-chars-forward ": ")
275 (delete-region (point-min) (point)))))
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."))))
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)))))
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)
307 (cvs-insert-strings files)
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)))))
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")))
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)))))
333 ;;;;
334 ;;;; functions for getting commit message from ChangeLog a file...
335 ;;;; Courtesy Jim Blandy
336 ;;;;
338 (defun log-edit-narrow-changelog ()
339 "Narrow to the top page of the current buffer, a ChangeLog file.
340 Actually, the narrowed region doesn't include the date line.
341 A \"page\" in a ChangeLog file is the area between two dates."
342 (or (eq major-mode 'change-log-mode)
343 (error "log-edit-narrow-changelog: current buffer isn't a ChangeLog"))
345 (goto-char (point-min))
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)))
352 (let ((start (point)))
353 (forward-page 1)
354 (narrow-to-region start (point))
355 (goto-char (point-min))))
357 (defun log-edit-changelog-paragraph ()
358 "Return the bounds of the ChangeLog paragraph containing point.
359 If 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)))))
372 (defun log-edit-changelog-subparagraph ()
373 "Return the bounds of the ChangeLog subparagraph containing point.
374 A subparagraph is a block of non-blank lines beginning with an asterisk.
375 If 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))
380 (progn
381 (forward-line 1)
382 (if (re-search-forward "^[ \t]*[\n*]" nil t)
383 (match-beginning 0)
384 (point-max))))
385 (list (point) (point)))))
387 (defun log-edit-changelog-entry ()
388 "Return the bounds of the ChangeLog entry containing point.
389 The variable `log-edit-changelog-full-paragraphs' decides whether an
390 \"entry\" is a paragraph or a subparagraph; see its documentation string
391 for more details."
392 (if log-edit-changelog-full-paragraphs
393 (log-edit-changelog-paragraph)
394 (log-edit-changelog-subparagraph)))
396 (defvar user-full-name)
397 (defvar user-mail-address)
398 (defun log-edit-changelog-ours-p ()
399 "See if ChangeLog entry at point is for the current user, today.
400 Return 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)))))
414 (defun log-edit-changelog-entries (file)
415 "Return the ChangeLog entries for FILE, and the ChangeLog they came from.
416 The return value looks like this:
417 (LOGBUFFER (ENTRYSTART . ENTRYEND) ...)
418 where LOGBUFFER is the name of the ChangeLog buffer, and each
419 \(ENTRYSTART . ENTRYEND\) pair is a buffer region."
420 (save-excursion
421 (let ((changelog-file-name
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)))
433 (if (not (log-edit-changelog-ours-p))
434 (list (current-buffer))
435 (save-restriction
436 (log-edit-narrow-changelog)
437 (goto-char (point-min))
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)))
450 (let (texts)
451 (while (search-forward pattern nil t)
452 (let ((entry (log-edit-changelog-entry)))
453 (push entry texts)
454 (goto-char (elt entry 1))))
456 (cons (current-buffer) texts))))))))
458 (defun log-edit-changelog-insert-entries (buffer regions)
459 "Insert those regions in BUFFER specified in REGIONS.
460 Sort 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))))
468 (defun log-edit-insert-changelog-entries (files)
469 "Given a list of files FILES, insert the ChangeLog entries for them."
470 (let ((buffer-entries nil))
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)
475 (let* ((entries (log-edit-changelog-entries file))
476 (pair (assq (car entries) buffer-entries)))
477 (if pair
478 (setcdr pair (cvs-union (cdr pair) (cdr entries)))
479 (push entries buffer-entries))))
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)
484 (log-edit-changelog-insert-entries (car buffer-entry) (cdr buffer-entry))
485 (when (cdr buffer-entry) (newline)))))
487 (provide 'log-edit)
489 ;;; Change Log:
490 ;; $log$
492 ;;; log-edit.el ends here