Convert consecutive FSF copyright years to ranges.
[bpt/emacs.git] / lisp / vc / log-edit.el
CommitLineData
5b467bf4
SM
1;;; log-edit.el --- Major mode for editing CVS commit messages
2
73b0cd50 3;; Copyright (C) 1999-2011 Free Software Foundation, Inc.
5b467bf4 4
cc1eecfd 5;; Author: Stefan Monnier <monnier@iro.umontreal.ca>
9766adfb 6;; Keywords: pcl-cvs cvs commit log vc
5b467bf4
SM
7
8;; This file is part of GNU Emacs.
9
eb3fa2cf 10;; GNU Emacs is free software: you can redistribute it and/or modify
5b467bf4 11;; it under the terms of the GNU General Public License as published by
eb3fa2cf
GM
12;; the Free Software Foundation, either version 3 of the License, or
13;; (at your option) any later version.
5b467bf4
SM
14
15;; GNU Emacs is distributed in the hope that it will be useful,
16;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18;; GNU General Public License for more details.
19
20;; You should have received a copy of the GNU General Public License
eb3fa2cf 21;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
5b467bf4
SM
22
23;;; Commentary:
24
25;; Todo:
26
5b467bf4
SM
27;; - Move in VC's code
28;; - Add compatibility for VC's hook variables
5b467bf4
SM
29
30;;; Code:
31
32(eval-when-compile (require 'cl))
33(require 'add-log) ; for all the ChangeLog goodies
34(require 'pcvs-util)
35(require 'ring)
5b467bf4 36
f1180544 37;;;;
5b467bf4 38;;;; Global Variables
f1180544 39;;;;
5b467bf4
SM
40
41(defgroup log-edit nil
bc35d341 42 "Major mode for editing RCS and CVS commit messages."
5b467bf4 43 :group 'pcl-cvs
bc35d341
DL
44 :group 'vc ; It's used by VC.
45 :version "21.1"
5b467bf4
SM
46 :prefix "log-edit-")
47
48;; compiler pacifiers
49(defvar cvs-buffer)
50
12dd83de
SM
51\f
52;; The main keymap
53
5b467bf4
SM
54(easy-mmode-defmap log-edit-mode-map
55 `(("\C-c\C-c" . log-edit-done)
56 ("\C-c\C-a" . log-edit-insert-changelog)
93a142e1 57 ("\C-c\C-d" . log-edit-show-diff)
5b467bf4 58 ("\C-c\C-f" . log-edit-show-files)
9fe89a26
SM
59 ("\M-n" . log-edit-next-comment)
60 ("\M-p" . log-edit-previous-comment)
61 ("\M-r" . log-edit-comment-search-backward)
62 ("\M-s" . log-edit-comment-search-forward)
0916e956 63 ("\C-c?" . log-edit-mode-help))
cdf54749 64 "Keymap for the `log-edit-mode' (to edit version control log messages)."
0916e956
SM
65 :group 'log-edit)
66
67;; Compatibility with old names. Should we bother ?
68(defvar vc-log-mode-map log-edit-mode-map)
69(defvar vc-log-entry-mode vc-log-mode-map)
5b467bf4 70
d247e32d
SM
71(easy-menu-define log-edit-menu log-edit-mode-map
72 "Menu used for `log-edit-mode'."
73 '("Log-Edit"
74 ["Done" log-edit-done
75 :help "Exit log-edit and proceed with the actual action."]
76 "--"
799224fe
DN
77 ["Insert ChangeLog" log-edit-insert-changelog
78 :help "Insert a log message by looking at the ChangeLog"]
79 ["Add to ChangeLog" log-edit-add-to-changelog
80 :help "Insert this log message into the appropriate ChangeLog file"]
d247e32d 81 "--"
93a142e1
DN
82 ["Show diff" log-edit-show-diff
83 :help "Show the diff for the files to be committed."]
d247e32d
SM
84 ["List files" log-edit-show-files
85 :help "Show the list of relevant files."]
86 "--"
799224fe
DN
87 ["Previous comment" log-edit-previous-comment
88 :help "Cycle backwards through comment history"]
89 ["Next comment" log-edit-next-comment
90 :help "Cycle forwards through comment history."]
91 ["Search comment forward" log-edit-comment-search-forward
92 :help "Search forwards through comment history for a substring match of str"]
93 ["Search comment backward" log-edit-comment-search-backward
94 :help "Search backwards through comment history for substring match of str"]))
d247e32d 95
f077c462 96(defcustom log-edit-confirm 'changed
9201cc28 97 "If non-nil, `log-edit-done' will request confirmation.
5b467bf4
SM
98If 'changed, only request confirmation if the list of files has
99 changed since the beginning of the log-edit session."
100 :group 'log-edit
101 :type '(choice (const changed) (const t) (const nil)))
102
103(defcustom log-edit-keep-buffer nil
9201cc28 104 "If non-nil, don't hide the buffer after `log-edit-done'."
5b467bf4
SM
105 :group 'log-edit
106 :type 'boolean)
107
a664185b
JB
108(defvar cvs-commit-buffer-require-final-newline t)
109(make-obsolete-variable 'cvs-commit-buffer-require-final-newline
b8ce3df9
JB
110 'log-edit-require-final-newline
111 "21.1")
5b467bf4
SM
112
113(defcustom log-edit-require-final-newline
114 cvs-commit-buffer-require-final-newline
9201cc28 115 "Enforce a newline at the end of commit log messages.
5b467bf4
SM
116Enforce it silently if t, query if non-nil and don't do anything if nil."
117 :group 'log-edit
118 :type '(choice (const ask) (const t) (const nil)))
119
120(defcustom log-edit-setup-invert nil
9201cc28 121 "Non-nil means `log-edit' should invert the meaning of its SETUP arg.
5b467bf4
SM
122If SETUP is 'force, this variable has no effect."
123 :group 'log-edit
124 :type 'boolean)
125
126(defcustom log-edit-hook '(log-edit-insert-cvs-template
e97a42c1 127 log-edit-show-files
5b467bf4 128 log-edit-insert-changelog)
9201cc28 129 "Hook run at the end of `log-edit'."
5b467bf4 130 :group 'log-edit
f7eeab0d
SM
131 :type '(hook :options (log-edit-insert-changelog
132 log-edit-insert-cvs-rcstemplate
133 log-edit-insert-cvs-template
134 log-edit-insert-filenames)))
5b467bf4 135
3831af62 136(defcustom log-edit-mode-hook (if (boundp 'vc-log-mode-hook) vc-log-mode-hook)
9201cc28 137 "Hook run when entering `log-edit-mode'."
5b467bf4
SM
138 :group 'log-edit
139 :type 'hook)
140
141(defcustom log-edit-done-hook nil
9201cc28 142 "Hook run before doing the actual commit.
5b467bf4
SM
143This hook can be used to cleanup the message, enforce various
144conventions, or to allow recording the message in some other database,
145such as a bug-tracking system. The list of files about to be committed
146can be obtained from `log-edit-files'."
147 :group 'log-edit
43a9a0c4 148 :type '(hook :options (log-edit-set-common-indentation
5b467bf4
SM
149 log-edit-add-to-changelog)))
150
bef4957b 151(defcustom log-edit-strip-single-file-name nil
31764e15 152 "If non-nil, remove file name from single-file log entries."
c6265c10
GM
153 :type 'boolean
154 :safe 'booleanp
155 :group 'log-edit
bef4957b 156 :version "24.1")
31764e15 157
a664185b
JB
158(defvar cvs-changelog-full-paragraphs t)
159(make-obsolete-variable 'cvs-changelog-full-paragraphs
b8ce3df9
JB
160 'log-edit-changelog-full-paragraphs
161 "21.1")
14188021
SM
162
163(defvar log-edit-changelog-full-paragraphs cvs-changelog-full-paragraphs
164 "*If non-nil, include full ChangeLog paragraphs in the log.
5b467bf4
SM
165This may be set in the ``local variables'' section of a ChangeLog, to
166indicate the policy for that ChangeLog.
167
168A ChangeLog paragraph is a bunch of log text containing no blank lines;
169a paragraph usually describes a set of changes with a single purpose,
170but perhaps spanning several functions in several files. Changes in
171different paragraphs are unrelated.
172
14188021 173You could argue that the log entry for a file should contain the
5b467bf4
SM
174full ChangeLog paragraph mentioning the change to the file, even though
175it may mention other files, because that gives you the full context you
5ab405e4 176need to understand the change. This is the behavior you get when this
5b467bf4
SM
177variable is set to t.
178
14188021 179On the other hand, you could argue that the log entry for a change
5b467bf4 180should contain only the text for the changes which occurred in that
5ab405e4 181file, because the log is per-file. This is the behavior you get
5b467bf4
SM
182when this variable is set to nil.")
183
184;;;; Internal global or buffer-local vars
185
186(defconst log-edit-files-buf "*log-edit-files*")
187(defvar log-edit-initial-files nil)
188(defvar log-edit-callback nil)
93a142e1 189(defvar log-edit-diff-function nil)
5b467bf4 190(defvar log-edit-listfun nil)
495b517c 191
cdf54749 192(defvar log-edit-parent-buffer nil)
5b467bf4 193
9fe89a26 194;;; Originally taken from VC-Log mode
12dd83de 195
9fe89a26 196(defconst log-edit-maximum-comment-ring-size 32
12dd83de 197 "Maximum number of saved comments in the comment ring.")
9fe89a26
SM
198(defvar log-edit-comment-ring (make-ring log-edit-maximum-comment-ring-size))
199(defvar log-edit-comment-ring-index nil)
200(defvar log-edit-last-comment-match "")
12dd83de 201
9fe89a26 202(defun log-edit-new-comment-index (stride len)
12dd83de 203 "Return the comment index STRIDE elements from the current one.
9fe89a26 204LEN is the length of `log-edit-comment-ring'."
12dd83de 205 (mod (cond
9fe89a26 206 (log-edit-comment-ring-index (+ log-edit-comment-ring-index stride))
12dd83de
SM
207 ;; Initialize the index on the first use of this command
208 ;; so that the first M-p gets index 0, and the first M-n gets
209 ;; index -1.
210 ((> stride 0) (1- stride))
211 (t stride))
212 len))
213
9fe89a26 214(defun log-edit-previous-comment (arg)
12dd83de
SM
215 "Cycle backwards through comment history.
216With a numeric prefix ARG, go back ARG comments."
217 (interactive "*p")
9fe89a26 218 (let ((len (ring-length log-edit-comment-ring)))
12dd83de
SM
219 (if (<= len 0)
220 (progn (message "Empty comment ring") (ding))
0916e956
SM
221 ;; Don't use `erase-buffer' because we don't want to `widen'.
222 (delete-region (point-min) (point-max))
9fe89a26
SM
223 (setq log-edit-comment-ring-index (log-edit-new-comment-index arg len))
224 (message "Comment %d" (1+ log-edit-comment-ring-index))
225 (insert (ring-ref log-edit-comment-ring log-edit-comment-ring-index)))))
12dd83de 226
9fe89a26 227(defun log-edit-next-comment (arg)
12dd83de
SM
228 "Cycle forwards through comment history.
229With a numeric prefix ARG, go forward ARG comments."
230 (interactive "*p")
9fe89a26 231 (log-edit-previous-comment (- arg)))
12dd83de 232
9fe89a26 233(defun log-edit-comment-search-backward (str &optional stride)
12dd83de
SM
234 "Search backwards through comment history for substring match of STR.
235If the optional argument STRIDE is present, that is a step-width to use
236when going through the comment ring."
237 ;; Why substring rather than regexp ? -sm
238 (interactive
9fe89a26 239 (list (read-string "Comment substring: " nil nil log-edit-last-comment-match)))
12dd83de
SM
240 (unless stride (setq stride 1))
241 (if (string= str "")
9fe89a26
SM
242 (setq str log-edit-last-comment-match)
243 (setq log-edit-last-comment-match str))
12dd83de 244 (let* ((str (regexp-quote str))
9fe89a26
SM
245 (len (ring-length log-edit-comment-ring))
246 (n (log-edit-new-comment-index stride len)))
12dd83de 247 (while (progn (when (or (>= n len) (< n 0)) (error "Not found"))
9fe89a26 248 (not (string-match str (ring-ref log-edit-comment-ring n))))
12dd83de 249 (setq n (+ n stride)))
9fe89a26
SM
250 (setq log-edit-comment-ring-index n)
251 (log-edit-previous-comment 0)))
12dd83de 252
9fe89a26 253(defun log-edit-comment-search-forward (str)
12dd83de
SM
254 "Search forwards through comment history for a substring match of STR."
255 (interactive
9fe89a26
SM
256 (list (read-string "Comment substring: " nil nil log-edit-last-comment-match)))
257 (log-edit-comment-search-backward str -1))
12dd83de 258
9fe89a26 259(defun log-edit-comment-to-change-log (&optional whoami file-name)
12dd83de
SM
260 "Enter last VC comment into the change log for the current file.
261WHOAMI (interactive prefix) non-nil means prompt for user name
262and site. FILE-NAME is the name of the change log; if nil, use
263`change-log-default-name'.
264
9fe89a26 265This may be useful as a `log-edit-checkin-hook' to update change logs
12dd83de
SM
266automatically."
267 (interactive (if current-prefix-arg
268 (list current-prefix-arg
269 (prompt-for-change-log-name))))
12dd83de 270 (let (;; Extract the comment first so we get any error before doing anything.
9fe89a26 271 (comment (ring-ref log-edit-comment-ring 0))
12dd83de
SM
272 ;; Don't let add-change-log-entry insert a defun name.
273 (add-log-current-defun-function 'ignore)
274 end)
275 ;; Call add-log to do half the work.
276 (add-change-log-entry whoami file-name t t)
277 ;; Insert the VC comment, leaving point before it.
278 (setq end (save-excursion (insert comment) (point-marker)))
279 (if (looking-at "\\s *\\s(")
280 ;; It starts with an open-paren, as in "(foo): Frobbed."
281 ;; So remove the ": " add-log inserted.
282 (delete-char -2))
283 ;; Canonicalize the white space between the file name and comment.
284 (just-one-space)
285 ;; Indent rest of the text the same way add-log indented the first line.
286 (let ((indentation (current-indentation)))
287 (save-excursion
288 (while (< (point) end)
289 (forward-line 1)
290 (indent-to indentation))
291 (setq end (point))))
292 ;; Fill the inserted text, preserving open-parens at bol.
0916e956 293 (let ((paragraph-start (concat paragraph-start "\\|\\s *\\s(")))
12dd83de
SM
294 (beginning-of-line)
295 (fill-region (point) end))
296 ;; Canonicalize the white space at the end of the entry so it is
297 ;; separated from the next entry by a single blank line.
298 (skip-syntax-forward " " end)
299 (delete-char (- (skip-syntax-backward " ")))
300 (or (eobp) (looking-at "\n\n")
301 (insert "\n"))))
302
9fe89a26 303;; Compatibility with old names.
f7eeab0d
SM
304(define-obsolete-variable-alias 'vc-comment-ring 'log-edit-comment-ring "22.1")
305(define-obsolete-variable-alias 'vc-comment-ring-index 'log-edit-comment-ring-index "22.1")
306(define-obsolete-function-alias 'vc-previous-comment 'log-edit-previous-comment "22.1")
307(define-obsolete-function-alias 'vc-next-comment 'log-edit-next-comment "22.1")
308(define-obsolete-function-alias 'vc-comment-search-reverse 'log-edit-comment-search-backward "22.1")
309(define-obsolete-function-alias 'vc-comment-search-forward 'log-edit-comment-search-forward "22.1")
310(define-obsolete-function-alias 'vc-comment-to-change-log 'log-edit-comment-to-change-log "22.1")
9fe89a26 311
cdf54749
SM
312;;;
313;;; Actual code
314;;;
5b467bf4 315
e97a42c1
SM
316(defface log-edit-summary '((t :inherit font-lock-function-name-face))
317 "Face for the summary in `log-edit-mode' buffers.")
318
319(defface log-edit-header '((t :inherit font-lock-keyword-face))
320 "Face for the headers in `log-edit-mode' buffers.")
321
322(defface log-edit-unknown-header '((t :inherit font-lock-comment-face))
323 "Face for unknown headers in `log-edit-mode' buffers.")
324
325(defvar log-edit-headers-alist '(("Summary" . log-edit-summary)
326 ("Fixes") ("Author"))
327 "AList of known headers and the face to use to highlight them.")
328
329(defconst log-edit-header-contents-regexp
330 "[ \t]*\\(.*\\(\n[ \t].*\\)*\\)\n?")
331
332(defun log-edit-match-to-eoh (limit)
333 ;; FIXME: copied from message-match-to-eoh.
334 (let ((start (point)))
335 (rfc822-goto-eoh)
336 ;; Typical situation: some temporary change causes the header to be
337 ;; incorrect, so EOH comes earlier than intended: the last lines of the
338 ;; intended headers are now not considered part of the header any more,
339 ;; so they don't have the multiline property set. When the change is
340 ;; completed and the header has its correct shape again, the lack of the
341 ;; multiline property means we won't rehighlight the last lines of
342 ;; the header.
343 (if (< (point) start)
344 nil ;No header within start..limit.
345 ;; Here we disregard LIMIT so that we may extend the area again.
346 (set-match-data (list start (point)))
347 (point))))
348
63fbe552 349(defvar log-edit-font-lock-keywords
e97a42c1
SM
350 ;; Copied/inspired by message-font-lock-keywords.
351 `((log-edit-match-to-eoh
402c8a49 352 (,(concat "^\\(\\([a-z]+\\):\\)" log-edit-header-contents-regexp)
e97a42c1
SM
353 (progn (goto-char (match-beginning 0)) (match-end 0)) nil
354 (1 (if (assoc (match-string 2) log-edit-headers-alist)
355 'log-edit-header
356 'log-edit-unknown-header)
357 nil lax)
402c8a49 358 ;; From `log-edit-header-contents-regexp':
e97a42c1
SM
359 (3 (or (cdr (assoc (match-string 2) log-edit-headers-alist))
360 'log-edit-header)
402c8a49 361 nil lax)))))
165a7fbe 362
5b467bf4 363;;;###autoload
09158997 364(defun log-edit (callback &optional setup params buffer mode &rest ignore)
5b467bf4 365 "Setup a buffer to enter a log message.
09158997
DN
366\\<log-edit-mode-map>The buffer will be put in mode MODE or `log-edit-mode'
367if MODE is nil.
5b467bf4 368If SETUP is non-nil, the buffer is then erased and `log-edit-hook' is run.
2507310c
TTN
369Mark and point will be set around the entire contents of the buffer so
370that it is easy to kill the contents of the buffer with \\[kill-region].
5b467bf4 371Once you're done editing the message, pressing \\[log-edit-done] will call
cdf54749 372`log-edit-done' which will end up calling CALLBACK to do the actual commit.
2507310c
TTN
373
374PARAMS if non-nil is an alist. Possible keys and associated values:
375 `log-edit-listfun' -- function taking no arguments that returns the list of
376 files that are concerned by the current operation (using relative names);
377 `log-edit-diff-function' -- function taking no arguments that
378 displays a diff of the files concerned by the current operation.
379
cdf54749 380If BUFFER is non-nil `log-edit' will jump to that buffer, use it to edit the
2507310c
TTN
381log message and go back to the current buffer when done. Otherwise, it
382uses the current buffer."
cdf54749
SM
383 (let ((parent (current-buffer)))
384 (if buffer (pop-to-buffer buffer))
385 (when (and log-edit-setup-invert (not (eq setup 'force)))
386 (setq setup (not setup)))
e97a42c1
SM
387 (when setup
388 (erase-buffer)
389 (insert "Summary: ")
390 (save-excursion (insert "\n\n")))
09158997
DN
391 (if mode
392 (funcall mode)
393 (log-edit-mode))
cdf54749 394 (set (make-local-variable 'log-edit-callback) callback)
93a142e1
DN
395 (if (listp params)
396 (dolist (crt params)
397 (set (make-local-variable (car crt)) (cdr crt)))
398 ;; For backward compatibility with log-edit up to version 22.2
399 ;; accept non-list PARAMS to mean `log-edit-list'.
400 (set (make-local-variable 'log-edit-listfun) params))
401
cdf54749 402 (if buffer (set (make-local-variable 'log-edit-parent-buffer) parent))
70c2a484 403 (set (make-local-variable 'log-edit-initial-files) (log-edit-files))
cdf54749
SM
404 (when setup (run-hooks 'log-edit-hook))
405 (goto-char (point-min)) (push-mark (point-max))
8a26c165 406 (message "%s" (substitute-command-keys
cdf54749 407 "Press \\[log-edit-done] when you are done editing."))))
5b467bf4
SM
408
409(define-derived-mode log-edit-mode text-mode "Log-Edit"
54877f36
SM
410 "Major mode for editing version-control log messages.
411When done editing the log entry, just type \\[log-edit-done] which
412will trigger the actual commit of the file(s).
413Several other handy support commands are provided of course and
414the package from which this is used might also provide additional
415commands (under C-x v for VC, for example).
416
1be77002 417\\{log-edit-mode-map}"
165a7fbe 418 (set (make-local-variable 'font-lock-defaults)
e97a42c1 419 '(log-edit-font-lock-keywords t t))
8117868f
DN
420 (make-local-variable 'log-edit-comment-ring-index)
421 (hack-dir-local-variables-non-file-buffer))
5b467bf4
SM
422
423(defun log-edit-hide-buf (&optional buf where)
424 (when (setq buf (get-buffer (or buf log-edit-files-buf)))
425 (let ((win (get-buffer-window buf where)))
426 (if win (ignore-errors (delete-window win))))
427 (bury-buffer buf)))
428
429(defun log-edit-done ()
430 "Finish editing the log message and commit the files.
5b467bf4
SM
431If you want to abort the commit, simply delete the buffer."
432 (interactive)
e97a42c1
SM
433 ;; Clean up empty headers.
434 (goto-char (point-min))
435 (while (looking-at (concat "^[a-z]*:" log-edit-header-contents-regexp))
436 (let ((beg (match-beginning 0)))
437 (goto-char (match-end 0))
438 (if (string-match "\\`[ \n\t]*\\'" (match-string 1))
439 (delete-region beg (point)))))
440 ;; Get rid of leading empty lines.
441 (goto-char (point-min))
442 (when (looking-at "\\([ \t]*\n\\)+")
443 (delete-region (match-beginning 0) (match-end 0)))
ffe7dc64
SM
444 ;; Get rid of trailing empty lines
445 (goto-char (point-max))
446 (skip-syntax-backward " ")
447 (when (equal (char-after) ?\n) (forward-char 1))
448 (delete-region (point) (point-max))
449 ;; Check for final newline
cdf54749
SM
450 (if (and (> (point-max) (point-min))
451 (/= (char-before (point-max)) ?\n)
5b467bf4
SM
452 (or (eq log-edit-require-final-newline t)
453 (and log-edit-require-final-newline
454 (y-or-n-p
455 (format "Buffer %s does not end in newline. Add one? "
456 (buffer-name))))))
457 (save-excursion
458 (goto-char (point-max))
459 (insert ?\n)))
1be77002 460 (let ((comment (buffer-string)))
9fe89a26
SM
461 (when (or (ring-empty-p log-edit-comment-ring)
462 (not (equal comment (ring-ref log-edit-comment-ring 0))))
463 (ring-insert log-edit-comment-ring comment)))
5b467bf4
SM
464 (let ((win (get-buffer-window log-edit-files-buf)))
465 (if (and log-edit-confirm
466 (not (and (eq log-edit-confirm 'changed)
467 (equal (log-edit-files) log-edit-initial-files)))
468 (progn
469 (log-edit-show-files)
ce5a3ac0 470 (not (y-or-n-p "Really commit? "))))
5b467bf4
SM
471 (progn (when (not win) (log-edit-hide-buf))
472 (message "Oh, well! Later maybe?"))
473 (run-hooks 'log-edit-done-hook)
474 (log-edit-hide-buf)
cdf54749
SM
475 (unless (or log-edit-keep-buffer (not log-edit-parent-buffer))
476 (cvs-bury-buffer (current-buffer) log-edit-parent-buffer))
5b467bf4
SM
477 (call-interactively log-edit-callback))))
478
479(defun log-edit-files ()
480 "Return the list of files that are about to be committed."
481 (ignore-errors (funcall log-edit-listfun)))
482
5b467bf4
SM
483(defun log-edit-mode-help ()
484 "Provide help for the `log-edit-mode-map'."
485 (interactive)
486 (if (eq last-command 'log-edit-mode-help)
487 (describe-function major-mode)
8a26c165 488 (message "%s"
5b467bf4
SM
489 (substitute-command-keys
490 "Type `\\[log-edit-done]' to finish commit. Try `\\[describe-function] log-edit-done' for more help."))))
491
43a9a0c4
SM
492(defcustom log-edit-common-indent 0
493 "Minimum indentation to use in `log-edit-set-common-indentation'."
494 :group 'log-edit
495 :type 'integer)
496
497(defun log-edit-set-common-indentation ()
498 "(Un)Indent the current buffer rigidly to `log-edit-common-indent'."
5b467bf4
SM
499 (save-excursion
500 (let ((common (point-max)))
e97a42c1 501 (rfc822-goto-eoh)
5b467bf4
SM
502 (while (< (point) (point-max))
503 (if (not (looking-at "^[ \t]*$"))
504 (setq common (min common (current-indentation))))
505 (forward-line 1))
e97a42c1
SM
506 (rfc822-goto-eoh)
507 (indent-rigidly (point) (point-max)
43a9a0c4 508 (- log-edit-common-indent common)))))
5b467bf4 509
93a142e1
DN
510(defun log-edit-show-diff ()
511 "Show the diff for the files to be committed."
512 (interactive)
37b72bf5
DN
513 (if (functionp log-edit-diff-function)
514 (funcall log-edit-diff-function)
515 (error "Diff functionality has not been setup")))
93a142e1 516
5b467bf4
SM
517(defun log-edit-show-files ()
518 "Show the list of files to be committed."
519 (interactive)
520 (let* ((files (log-edit-files))
cdf54749 521 (buf (get-buffer-create log-edit-files-buf)))
5b467bf4
SM
522 (with-current-buffer buf
523 (log-edit-hide-buf buf 'all)
524 (setq buffer-read-only nil)
525 (erase-buffer)
a88e99b5 526 (cvs-insert-strings files)
5b467bf4
SM
527 (setq buffer-read-only t)
528 (goto-char (point-min))
529 (save-selected-window
530 (cvs-pop-to-buffer-same-frame buf)
531 (shrink-window-if-larger-than-buffer)
532 (selected-window)))))
533
534(defun log-edit-insert-cvs-template ()
f7eeab0d
SM
535 "Insert the template specified by the CVS administrator, if any.
536This simply uses the local CVS/Template file."
5b467bf4 537 (interactive)
32226619
JB
538 (when (or (called-interactively-p 'interactive)
539 (= (point-min) (point-max)))
f7eeab0d
SM
540 (when (file-readable-p "CVS/Template")
541 (insert-file-contents "CVS/Template"))))
542
543(defun log-edit-insert-cvs-rcstemplate ()
544 "Insert the rcstemplate from the CVS repository.
545This contacts the repository to get the rcstemplate file and
546can thus take some time."
547 (interactive)
32226619
JB
548 (when (or (called-interactively-p 'interactive)
549 (= (point-min) (point-max)))
21227135
SM
550 (when (file-readable-p "CVS/Root")
551 ;; Ignore the stderr stuff, even if it's an error.
552 (call-process "cvs" nil '(t nil) nil
553 "checkout" "-p" "CVSROOT/rcstemplate"))))
f1180544 554
f7eeab0d
SM
555(defun log-edit-insert-filenames ()
556 "Insert the list of files that are to be committed."
557 (interactive)
558 (insert "Affected files: \n"
559 (mapconcat 'identity (log-edit-files) " \n")))
5b467bf4
SM
560
561(defun log-edit-add-to-changelog ()
562 "Insert this log message into the appropriate ChangeLog file."
563 (interactive)
564 ;; Yuck!
9fe89a26
SM
565 (unless (string= (buffer-string) (ring-ref log-edit-comment-ring 0))
566 (ring-insert log-edit-comment-ring (buffer-string)))
5b467bf4
SM
567 (dolist (f (log-edit-files))
568 (let ((buffer-file-name (expand-file-name f)))
569 (save-excursion
9fe89a26 570 (log-edit-comment-to-change-log)))))
5b467bf4 571
f7eeab0d 572(defvar log-edit-changelog-use-first nil)
ce8794df
SM
573
574(defvar log-edit-rewrite-fixes nil
575 "Rule to rewrite bug numbers into Fixes: headers.
576The value should be of the form (REGEXP . REPLACEMENT)
577where REGEXP should match the expression referring to a bug number
578in the text, and REPLACEMENT is an expression to pass to `replace-match'
579to build the Fixes: header.")
a62b88d4
SM
580(put 'log-edit-rewrite-fixes 'safe-local-variable
581 (lambda (v) (and (stringp (car-safe v)) (stringp (cdr v)))))
ce8794df 582
f7eeab0d
SM
583(defun log-edit-insert-changelog (&optional use-first)
584 "Insert a log message by looking at the ChangeLog.
585The idea is to write your ChangeLog entries first, and then use this
586command to commit your changes.
587
588To select default log text, we:
589- find the ChangeLog entries for the files to be checked in,
590- verify that the top entry in the ChangeLog is on the current date
591 and by the current user; if not, we don't provide any default text,
592- search the ChangeLog entry for paragraphs containing the names of
593 the files we're checking in, and finally
594- use those paragraphs as the log text.
595
596If the optional prefix arg USE-FIRST is given (via \\[universal-argument]),
597or if the command is repeated a second time in a row, use the first log entry
598regardless of user name or time."
599 (interactive "P")
e97a42c1
SM
600 (let ((eoh (save-excursion (rfc822-goto-eoh) (point))))
601 (when (<= (point) eoh)
602 (goto-char eoh)
603 (if (looking-at "\n") (forward-char 1))))
ce8794df
SM
604 (let ((author
605 (let ((log-edit-changelog-use-first
606 (or use-first (eq last-command 'log-edit-insert-changelog))))
607 (log-edit-insert-changelog-entries (log-edit-files)))))
608 (log-edit-set-common-indentation)
609 ;; Add an Author: field if appropriate.
610 (when author
611 (rfc822-goto-eoh)
612 (insert "Author: " author "\n" (if (looking-at "\n") "" "\n")))
613 ;; Add a Fixes: field if applicable.
614 (when (consp log-edit-rewrite-fixes)
615 (rfc822-goto-eoh)
616 (when (re-search-forward (car log-edit-rewrite-fixes) nil t)
617 (let ((start (match-beginning 0))
618 (end (match-end 0))
619 (fixes (match-substitute-replacement
620 (cdr log-edit-rewrite-fixes))))
621 (delete-region start end)
622 (rfc822-goto-eoh)
623 (insert "Fixes: " fixes "\n" (if (looking-at "\n") "" "\n")))))
624 (goto-char (point-min))
625 (when (and log-edit-strip-single-file-name (looking-at "\\*\\s-+"))
626 (forward-line 1)
627 (when (not (re-search-forward "^\\*\\s-+" nil t))
628 (goto-char (point-min))
629 (skip-chars-forward "^():")
630 (skip-chars-forward ": ")
631 (delete-region (point-min) (point))))))
f7eeab0d 632
f1180544 633;;;;
5b467bf4
SM
634;;;; functions for getting commit message from ChangeLog a file...
635;;;; Courtesy Jim Blandy
f1180544 636;;;;
5b467bf4 637
14188021 638(defun log-edit-narrow-changelog ()
5b467bf4
SM
639 "Narrow to the top page of the current buffer, a ChangeLog file.
640Actually, the narrowed region doesn't include the date line.
641A \"page\" in a ChangeLog file is the area between two dates."
642 (or (eq major-mode 'change-log-mode)
14188021 643 (error "log-edit-narrow-changelog: current buffer isn't a ChangeLog"))
5b467bf4
SM
644
645 (goto-char (point-min))
646
647 ;; Skip date line and subsequent blank lines.
648 (forward-line 1)
649 (if (looking-at "[ \t\n]*\n")
650 (goto-char (match-end 0)))
651
652 (let ((start (point)))
653 (forward-page 1)
654 (narrow-to-region start (point))
655 (goto-char (point-min))))
656
14188021 657(defun log-edit-changelog-paragraph ()
5b467bf4
SM
658 "Return the bounds of the ChangeLog paragraph containing point.
659If we are between paragraphs, return the previous paragraph."
8390fb80
SM
660 (beginning-of-line)
661 (if (looking-at "^[ \t]*$")
662 (skip-chars-backward " \t\n" (point-min)))
663 (list (progn
664 (if (re-search-backward "^[ \t]*\n" nil 'or-to-limit)
665 (goto-char (match-end 0)))
666 (point))
667 (if (re-search-forward "^[ \t\n]*$" nil t)
668 (match-beginning 0)
669 (point-max))))
5b467bf4 670
14188021 671(defun log-edit-changelog-subparagraph ()
5b467bf4
SM
672 "Return the bounds of the ChangeLog subparagraph containing point.
673A subparagraph is a block of non-blank lines beginning with an asterisk.
674If we are between sub-paragraphs, return the previous subparagraph."
5b467bf4
SM
675 (end-of-line)
676 (if (search-backward "*" nil t)
677 (list (progn (beginning-of-line) (point))
bc35d341 678 (progn
5b467bf4
SM
679 (forward-line 1)
680 (if (re-search-forward "^[ \t]*[\n*]" nil t)
681 (match-beginning 0)
682 (point-max))))
8390fb80 683 (list (point) (point))))
5b467bf4 684
14188021 685(defun log-edit-changelog-entry ()
5b467bf4 686 "Return the bounds of the ChangeLog entry containing point.
14188021 687The variable `log-edit-changelog-full-paragraphs' decides whether an
5b467bf4
SM
688\"entry\" is a paragraph or a subparagraph; see its documentation string
689for more details."
8390fb80
SM
690 (save-excursion
691 (if log-edit-changelog-full-paragraphs
692 (log-edit-changelog-paragraph)
693 (log-edit-changelog-subparagraph))))
5b467bf4
SM
694
695(defvar user-full-name)
696(defvar user-mail-address)
ce8794df
SM
697
698(defvar log-edit-author) ;Dynamically scoped.
699
14188021 700(defun log-edit-changelog-ours-p ()
5b467bf4 701 "See if ChangeLog entry at point is for the current user, today.
4837b516 702Return non-nil if it is."
5b467bf4
SM
703 ;; Code adapted from add-change-log-entry.
704 (let ((name (or (and (boundp 'add-log-full-name) add-log-full-name)
705 (and (fboundp 'user-full-name) (user-full-name))
706 (and (boundp 'user-full-name) user-full-name)))
707 (mail (or (and (boundp 'add-log-mailing-address) add-log-mailing-address)
708 ;;(and (fboundp 'user-mail-address) (user-mail-address))
709 (and (boundp 'user-mail-address) user-mail-address)))
710 (time (or (and (boundp 'add-log-time-format)
711 (functionp add-log-time-format)
712 (funcall add-log-time-format))
713 (format-time-string "%Y-%m-%d"))))
ce8794df
SM
714 (if (null log-edit-changelog-use-first)
715 (looking-at (regexp-quote (format "%s %s <%s>" time name mail)))
716 ;; Check the author, to potentially add it as a "Author: " header.
717 (when (looking-at "[^ \t]")
718 (when (and (boundp 'log-edit-author)
719 (not (looking-at (format ".+ .+ <%s>"
720 (regexp-quote mail))))
721 (looking-at ".+ \\(.+ <.+>\\)"))
722 (let ((author (replace-regexp-in-string " " " "
723 (match-string 1))))
724 (unless (and log-edit-author
725 (string-match (regexp-quote author) log-edit-author))
726 (setq log-edit-author
727 (if log-edit-author
728 (concat log-edit-author ", " author)
729 author)))))
730 t))))
5b467bf4 731
14188021 732(defun log-edit-changelog-entries (file)
5b467bf4
SM
733 "Return the ChangeLog entries for FILE, and the ChangeLog they came from.
734The return value looks like this:
bef4957b 735 (LOGBUFFER (ENTRYSTART ENTRYEND) ...)
5b467bf4
SM
736where LOGBUFFER is the name of the ChangeLog buffer, and each
737\(ENTRYSTART . ENTRYEND\) pair is a buffer region."
d944ee49
SM
738 (let ((changelog-file-name
739 (let ((default-directory
740 (file-name-directory (expand-file-name file)))
741 (visiting-buffer (find-buffer-visiting file)))
742 ;; If there is a buffer visiting FILE, and it has a local
743 ;; value for `change-log-default-name', use that.
744 (if (and visiting-buffer
745 (local-variable-p 'change-log-default-name
746 visiting-buffer))
747 (with-current-buffer visiting-buffer
748 change-log-default-name)
749 ;; `find-change-log' uses `change-log-default-name' if set
750 ;; and sets it before exiting, so we need to work around
751 ;; that memoizing which is undesired here
752 (setq change-log-default-name nil)
753 (find-change-log)))))
754 (with-current-buffer (find-file-noselect changelog-file-name)
5b467bf4
SM
755 (unless (eq major-mode 'change-log-mode) (change-log-mode))
756 (goto-char (point-min))
757 (if (looking-at "\\s-*\n") (goto-char (match-end 0)))
14188021 758 (if (not (log-edit-changelog-ours-p))
5b467bf4
SM
759 (list (current-buffer))
760 (save-restriction
14188021 761 (log-edit-narrow-changelog)
5b467bf4 762 (goto-char (point-min))
f1180544 763
5b467bf4
SM
764 ;; Search for the name of FILE relative to the ChangeLog. If that
765 ;; doesn't occur anywhere, they're not using full relative
766 ;; filenames in the ChangeLog, so just look for FILE; we'll accept
767 ;; some false positives.
768 (let ((pattern (file-relative-name
769 file (file-name-directory changelog-file-name))))
770 (if (or (string= pattern "")
771 (not (save-excursion
772 (search-forward pattern nil t))))
773 (setq pattern (file-name-nondirectory file)))
774
b543ff57 775 (setq pattern (concat "\\(^\\|[^[:alnum:]]\\)"
9b2a758a 776 (regexp-quote pattern)
b543ff57
NR
777 "\\($\\|[^[:alnum:]]\\)"))
778
8390fb80
SM
779 (let (texts
780 (pos (point)))
781 (while (and (not (eobp)) (re-search-forward pattern nil t))
14188021 782 (let ((entry (log-edit-changelog-entry)))
8390fb80
SM
783 (if (< (elt entry 1) (max (1+ pos) (point)))
784 ;; This is not relevant, actually.
785 nil
786 (push entry texts))
787 ;; Make sure we make progress.
788 (setq pos (max (1+ pos) (elt entry 1)))
789 (goto-char pos)))
5b467bf4
SM
790
791 (cons (current-buffer) texts))))))))
792
bef4957b
CY
793(defun log-edit-changelog-insert-entries (buffer beg end &rest files)
794 "Insert the text from BUFFER between BEG and END.
795Rename relative filenames in the ChangeLog entry as FILES."
796 (let ((opoint (point))
797 (log-name (buffer-file-name buffer))
798 (case-fold-search nil)
799 bound)
800 (insert-buffer-substring buffer beg end)
801 (setq bound (point-marker))
802 (when log-name
803 (dolist (f files)
804 (save-excursion
805 (goto-char opoint)
806 (when (re-search-forward
807 (concat "\\(^\\|[ \t]\\)\\("
808 (file-relative-name f (file-name-directory log-name))
809 "\\)[, :\n]")
810 bound t)
811 (replace-match f t t nil 2)))))
812 ;; Eliminate tabs at the beginning of the line.
813 (save-excursion
814 (goto-char opoint)
815 (while (re-search-forward "^\\(\t+\\)" bound t)
816 (replace-match "")))))
5b467bf4 817
14188021 818(defun log-edit-insert-changelog-entries (files)
5b467bf4 819 "Given a list of files FILES, insert the ChangeLog entries for them."
ce8794df
SM
820 (let ((log-entries nil)
821 (log-edit-author nil))
bef4957b
CY
822 ;; Note that any ChangeLog entry can apply to more than one file.
823 ;; Here we construct a log-entries list with elements of the form
824 ;; ((LOGBUFFER ENTRYSTART ENTRYEND) FILE1 FILE2...)
5b467bf4 825 (dolist (file files)
14188021 826 (let* ((entries (log-edit-changelog-entries file))
bef4957b
CY
827 (buf (car entries))
828 key entry)
829 (dolist (region (cdr entries))
830 (setq key (cons buf region))
831 (if (setq entry (assoc key log-entries))
832 (setcdr entry (append (cdr entry) (list file)))
833 (push (list key file) log-entries)))))
834 ;; Now map over log-entries, and extract the strings.
835 (dolist (log-entry (nreverse log-entries))
836 (apply 'log-edit-changelog-insert-entries
837 (append (car log-entry) (cdr log-entry)))
ce8794df
SM
838 (insert "\n"))
839 log-edit-author))
5b467bf4 840
e97a42c1
SM
841(defun log-edit-extract-headers (headers comment)
842 "Extract headers from COMMENT to form command line arguments.
843HEADERS should be an alist with elements of the form (HEADER . CMDARG)
844associating header names to the corresponding cmdline option name and the
845result is then a list of the form (MSG CMDARG1 HDRTEXT1 CMDARG2 HDRTEXT2...).
846where MSG is the remaining text from STRING.
847If \"Summary\" is not in HEADERS, then the \"Summary\" header is extracted
848anyway and put back as the first line of MSG."
849 (with-temp-buffer
850 (insert comment)
851 (rfc822-goto-eoh)
852 (narrow-to-region (point-min) (point))
853 (let ((case-fold-search t)
854 (summary ())
855 (res ()))
856 (dolist (header (if (assoc "Summary" headers) headers
857 (cons '("Summary" . t) headers)))
858 (goto-char (point-min))
859 (while (re-search-forward (concat "^" (car header)
860 ":" log-edit-header-contents-regexp)
861 nil t)
862 (if (eq t (cdr header))
863 (setq summary (match-string 1))
864 (push (match-string 1) res)
865 (push (or (cdr header) (car header)) res))
866 (replace-match "" t t)))
867 ;; Remove header separator if the header is empty.
868 (widen)
09158997 869 (goto-char (point-min))
e97a42c1
SM
870 (when (looking-at "\\([ \t]*\n\\)+")
871 (delete-region (match-beginning 0) (match-end 0)))
872 (if summary (insert summary "\n"))
873 (cons (buffer-string) res))))
09158997 874
5b467bf4 875(provide 'log-edit)
54877f36 876
5b467bf4 877;;; log-edit.el ends here