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