Update copyright year to 2014 by running admin/update-copyright.
[emacs.git] / lisp / vc / log-edit.el
blobc807cde82509c0aeef0d031015cadb51b15760b9
1 ;;; log-edit.el --- Major mode for editing CVS commit messages -*- lexical-binding: t -*-
3 ;; Copyright (C) 1999-2014 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)
35 (require 'message)
37 ;;;;
38 ;;;; Global Variables
39 ;;;;
41 (defgroup log-edit nil
42 "Major mode for editing RCS and CVS commit messages."
43 :group 'pcl-cvs
44 :group 'vc ; It's used by VC.
45 :version "21.1"
46 :prefix "log-edit-")
48 ;; compiler pacifiers
49 (defvar cvs-buffer)
52 ;; The main keymap
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)
57 ("\C-c\C-d" . log-edit-show-diff)
58 ("\C-c\C-f" . log-edit-show-files)
59 ("\C-c\C-k" . log-edit-kill-buffer)
60 ("\C-a" . log-edit-beginning-of-line)
61 ("\M-n" . log-edit-next-comment)
62 ("\M-p" . log-edit-previous-comment)
63 ("\M-r" . log-edit-comment-search-backward)
64 ("\M-s" . log-edit-comment-search-forward)
65 ("\C-c?" . log-edit-mode-help))
66 "Keymap for the `log-edit-mode' (to edit version control log messages)."
67 :group 'log-edit)
69 ;; Compatibility with old names. Should we bother ?
70 (defvar vc-log-mode-map log-edit-mode-map)
71 (defvar vc-log-entry-mode vc-log-mode-map)
73 (easy-menu-define log-edit-menu log-edit-mode-map
74 "Menu used for `log-edit-mode'."
75 '("Log-Edit"
76 ["Done" log-edit-done
77 :help "Exit log-edit and proceed with the actual action."]
78 "--"
79 ["Insert ChangeLog" log-edit-insert-changelog
80 :help "Insert a log message by looking at the ChangeLog"]
81 ["Add to ChangeLog" log-edit-add-to-changelog
82 :help "Insert this log message into the appropriate ChangeLog file"]
83 "--"
84 ["Show diff" log-edit-show-diff
85 :help "Show the diff for the files to be committed."]
86 ["List files" log-edit-show-files
87 :help "Show the list of relevant files."]
88 "--"
89 ["Previous comment" log-edit-previous-comment
90 :help "Cycle backwards through comment history"]
91 ["Next comment" log-edit-next-comment
92 :help "Cycle forwards through comment history."]
93 ["Search comment forward" log-edit-comment-search-forward
94 :help "Search forwards through comment history for a substring match of str"]
95 ["Search comment backward" log-edit-comment-search-backward
96 :help "Search backwards through comment history for substring match of str"]))
98 (defcustom log-edit-confirm 'changed
99 "If non-nil, `log-edit-done' will request confirmation.
100 If 'changed, only request confirmation if the list of files has
101 changed since the beginning of the log-edit session."
102 :group 'log-edit
103 :type '(choice (const changed) (const t) (const nil)))
105 (defcustom log-edit-keep-buffer nil
106 "If non-nil, don't hide the buffer after `log-edit-done'."
107 :group 'log-edit
108 :type 'boolean)
110 (defcustom log-edit-require-final-newline t
111 "Enforce a newline at the end of commit log messages.
112 Enforce it silently if t, query if non-nil and don't do anything if nil."
113 :group 'log-edit
114 :type '(choice (const ask) (const t) (const nil)))
116 (defcustom log-edit-setup-invert nil
117 "Non-nil means `log-edit' should invert the meaning of its SETUP arg.
118 If SETUP is 'force, this variable has no effect."
119 :group 'log-edit
120 :type 'boolean)
122 (defcustom log-edit-setup-add-author nil
123 "Non-nil means `log-edit' may add the `Author:' header.
124 This applies when its SETUP argument is non-nil."
125 :version "24.4"
126 :group 'log-edit
127 :type 'boolean
128 :safe 'booleanp)
130 (defcustom log-edit-hook '(log-edit-insert-message-template
131 log-edit-insert-cvs-template
132 log-edit-insert-changelog
133 log-edit-show-files)
134 "Hook run at the end of `log-edit'."
135 :group 'log-edit
136 :type '(hook :options (log-edit-insert-message-template
137 log-edit-insert-cvs-rcstemplate
138 log-edit-insert-cvs-template
139 log-edit-insert-changelog
140 log-edit-insert-filenames
141 log-edit-insert-filenames-without-changelog
142 log-edit-show-files)))
144 (defcustom log-edit-mode-hook (if (boundp 'vc-log-mode-hook) vc-log-mode-hook)
145 "Hook run when entering `log-edit-mode'."
146 :group 'log-edit
147 :type 'hook)
149 (defcustom log-edit-done-hook nil
150 "Hook run before doing the actual commit.
151 This hook can be used to cleanup the message, enforce various
152 conventions, or to allow recording the message in some other database,
153 such as a bug-tracking system. The list of files about to be committed
154 can be obtained from `log-edit-files'."
155 :group 'log-edit
156 :type '(hook :options (log-edit-set-common-indentation
157 log-edit-add-to-changelog)))
159 (defcustom log-edit-strip-single-file-name nil
160 "If non-nil, remove file name from single-file log entries."
161 :type 'boolean
162 :safe 'booleanp
163 :group 'log-edit
164 :version "24.1")
166 (defvar log-edit-changelog-full-paragraphs t
167 "If non-nil, include full ChangeLog paragraphs in the log.
168 This may be set in the ``local variables'' section of a ChangeLog, to
169 indicate the policy for that ChangeLog.
171 A ChangeLog paragraph is a bunch of log text containing no blank lines;
172 a paragraph usually describes a set of changes with a single purpose,
173 but perhaps spanning several functions in several files. Changes in
174 different paragraphs are unrelated.
176 You could argue that the log entry for a file should contain the
177 full ChangeLog paragraph mentioning the change to the file, even though
178 it may mention other files, because that gives you the full context you
179 need to understand the change. This is the behavior you get when this
180 variable is set to t.
182 On the other hand, you could argue that the log entry for a change
183 should contain only the text for the changes which occurred in that
184 file, because the log is per-file. This is the behavior you get
185 when this variable is set to nil.")
187 ;;;; Internal global or buffer-local vars
189 (defconst log-edit-files-buf "*log-edit-files*")
190 (defvar log-edit-initial-files nil)
191 (defvar log-edit-callback nil)
192 (defvar log-edit-diff-function nil)
193 (defvar log-edit-listfun nil)
195 (defvar log-edit-parent-buffer nil)
197 (defvar log-edit-vc-backend nil
198 "VC fileset corresponding to the current log.")
200 ;;; Originally taken from VC-Log mode
202 (defconst log-edit-maximum-comment-ring-size 32
203 "Maximum number of saved comments in the comment ring.")
204 (define-obsolete-variable-alias 'vc-comment-ring 'log-edit-comment-ring "22.1")
205 (defvar log-edit-comment-ring (make-ring log-edit-maximum-comment-ring-size))
206 (define-obsolete-variable-alias 'vc-comment-ring-index
207 'log-edit-comment-ring-index "22.1")
208 (defvar log-edit-comment-ring-index nil)
209 (defvar log-edit-last-comment-match "")
211 (defun log-edit-new-comment-index (stride len)
212 "Return the comment index STRIDE elements from the current one.
213 LEN is the length of `log-edit-comment-ring'."
214 (mod (cond
215 (log-edit-comment-ring-index (+ log-edit-comment-ring-index stride))
216 ;; Initialize the index on the first use of this command
217 ;; so that the first M-p gets index 0, and the first M-n gets
218 ;; index -1.
219 ((> stride 0) (1- stride))
220 (t stride))
221 len))
223 (defun log-edit-previous-comment (arg)
224 "Cycle backwards through comment history.
225 With a numeric prefix ARG, go back ARG comments."
226 (interactive "*p")
227 (let ((len (ring-length log-edit-comment-ring)))
228 (if (<= len 0)
229 (progn (message "Empty comment ring") (ding))
230 ;; Don't use `erase-buffer' because we don't want to `widen'.
231 (delete-region (point-min) (point-max))
232 (setq log-edit-comment-ring-index (log-edit-new-comment-index arg len))
233 (message "Comment %d" (1+ log-edit-comment-ring-index))
234 (insert (ring-ref log-edit-comment-ring log-edit-comment-ring-index)))))
236 (defun log-edit-next-comment (arg)
237 "Cycle forwards through comment history.
238 With a numeric prefix ARG, go forward ARG comments."
239 (interactive "*p")
240 (log-edit-previous-comment (- arg)))
242 (defun log-edit-comment-search-backward (str &optional stride)
243 "Search backwards through comment history for substring match of STR.
244 If the optional argument STRIDE is present, that is a step-width to use
245 when going through the comment ring."
246 ;; Why substring rather than regexp ? -sm
247 (interactive
248 (list (read-string "Comment substring: " nil nil log-edit-last-comment-match)))
249 (unless stride (setq stride 1))
250 (if (string= str "")
251 (setq str log-edit-last-comment-match)
252 (setq log-edit-last-comment-match str))
253 (let* ((str (regexp-quote str))
254 (len (ring-length log-edit-comment-ring))
255 (n (log-edit-new-comment-index stride len)))
256 (while (progn (when (or (>= n len) (< n 0)) (error "Not found"))
257 (not (string-match str (ring-ref log-edit-comment-ring n))))
258 (setq n (+ n stride)))
259 (setq log-edit-comment-ring-index n)
260 (log-edit-previous-comment 0)))
262 (defun log-edit-comment-search-forward (str)
263 "Search forwards through comment history for a substring match of STR."
264 (interactive
265 (list (read-string "Comment substring: " nil nil log-edit-last-comment-match)))
266 (log-edit-comment-search-backward str -1))
268 (defun log-edit-comment-to-change-log (&optional whoami file-name)
269 "Enter last VC comment into the change log for the current file.
270 WHOAMI (interactive prefix) non-nil means prompt for user name
271 and site. FILE-NAME is the name of the change log; if nil, use
272 `change-log-default-name'.
274 This may be useful as a `log-edit-checkin-hook' to update change logs
275 automatically."
276 (interactive (if current-prefix-arg
277 (list current-prefix-arg
278 (prompt-for-change-log-name))))
279 (let (;; Extract the comment first so we get any error before doing anything.
280 (comment (ring-ref log-edit-comment-ring 0))
281 ;; Don't let add-change-log-entry insert a defun name.
282 (add-log-current-defun-function 'ignore)
283 end)
284 ;; Call add-log to do half the work.
285 (add-change-log-entry whoami file-name t t)
286 ;; Insert the VC comment, leaving point before it.
287 (setq end (save-excursion (insert comment) (point-marker)))
288 (if (looking-at "\\s *\\s(")
289 ;; It starts with an open-paren, as in "(foo): Frobbed."
290 ;; So remove the ": " add-log inserted.
291 (delete-char -2))
292 ;; Canonicalize the white space between the file name and comment.
293 (just-one-space)
294 ;; Indent rest of the text the same way add-log indented the first line.
295 (let ((indentation (current-indentation)))
296 (save-excursion
297 (while (< (point) end)
298 (forward-line 1)
299 (indent-to indentation))
300 (setq end (point))))
301 ;; Fill the inserted text, preserving open-parens at bol.
302 (let ((paragraph-start (concat paragraph-start "\\|\\s *\\s(")))
303 (beginning-of-line)
304 (fill-region (point) end))
305 ;; Canonicalize the white space at the end of the entry so it is
306 ;; separated from the next entry by a single blank line.
307 (skip-syntax-forward " " end)
308 (delete-char (- (skip-syntax-backward " ")))
309 (or (eobp) (looking-at "\n\n")
310 (insert "\n"))))
312 ;; Compatibility with old names.
313 (define-obsolete-function-alias 'vc-previous-comment 'log-edit-previous-comment "22.1")
314 (define-obsolete-function-alias 'vc-next-comment 'log-edit-next-comment "22.1")
315 (define-obsolete-function-alias 'vc-comment-search-reverse 'log-edit-comment-search-backward "22.1")
316 (define-obsolete-function-alias 'vc-comment-search-forward 'log-edit-comment-search-forward "22.1")
317 (define-obsolete-function-alias 'vc-comment-to-change-log 'log-edit-comment-to-change-log "22.1")
320 ;;; Actual code
323 (defface log-edit-summary '((t :inherit font-lock-function-name-face))
324 "Face for the summary in `log-edit-mode' buffers.")
326 (defface log-edit-header '((t :inherit font-lock-keyword-face))
327 "Face for the headers in `log-edit-mode' buffers.")
329 (defface log-edit-unknown-header '((t :inherit font-lock-comment-face))
330 "Face for unknown headers in `log-edit-mode' buffers.")
332 (defvar log-edit-headers-alist '(("Summary" . log-edit-summary)
333 ("Fixes") ("Author"))
334 "AList of known headers and the face to use to highlight them.")
336 (defconst log-edit-header-contents-regexp
337 "[ \t]*\\(.*\\(\n[ \t].*\\)*\\)\n?"
338 "Regular expression matching a header field.
339 The first subexpression is the actual text of the field.")
341 (defun log-edit-match-to-eoh (_limit)
342 ;; FIXME: copied from message-match-to-eoh.
343 (let ((start (point)))
344 (rfc822-goto-eoh)
345 ;; Typical situation: some temporary change causes the header to be
346 ;; incorrect, so EOH comes earlier than intended: the last lines of the
347 ;; intended headers are now not considered part of the header any more,
348 ;; so they don't have the multiline property set. When the change is
349 ;; completed and the header has its correct shape again, the lack of the
350 ;; multiline property means we won't rehighlight the last lines of
351 ;; the header.
352 (if (< (point) start)
353 nil ;No header within start..limit.
354 ;; Here we disregard LIMIT so that we may extend the area again.
355 (set-match-data (list start (point)))
356 (point))))
358 (defvar log-edit-font-lock-keywords
359 ;; Copied/inspired by message-font-lock-keywords.
360 `((log-edit-match-to-eoh
361 (,(concat "^\\(\\([[:alpha:]-]+\\):\\)" log-edit-header-contents-regexp)
362 (progn (goto-char (match-beginning 0)) (match-end 0)) nil
363 (1 (if (assoc-string (match-string 2) log-edit-headers-alist t)
364 'log-edit-header
365 'log-edit-unknown-header)
366 nil lax)
367 ;; From `log-edit-header-contents-regexp':
368 (3 (or (cdr (assoc-string (match-string 2) log-edit-headers-alist t))
369 'log-edit-header)
370 nil lax))
371 ("^\n"
372 (progn (goto-char (match-end 0)) (1+ (match-end 0))) nil
373 (0 '(:height 0.1 :inverse-video t))))))
375 (defvar log-edit-font-lock-gnu-style nil
376 "If non-nil, highlight common failures to follow the GNU coding standards.")
377 (put 'log-edit-font-lock-gnu-style 'safe-local-variable 'booleanp)
379 (defconst log-edit-font-lock-gnu-keywords
380 ;; Use
381 ;; * foo.el (bla, bli)
382 ;; (blo, blu): Toto.
383 ;; Rather than
384 ;; * foo.el (bla, bli,
385 ;; blo, blu): Toto.
386 '(("^[ \t]*\\(?:\\* .*\\)?\\(([^\n)]*,\\s-*\\)$"
387 (1 '(face font-lock-warning-face
388 help-echo "Continue function lists with \")\\n(\".") t))
389 ;; Don't leave a lone word on a single line.
390 ;;("^\\s-*\\(\\S-*[^\n:)]\\)\\s-*$" (1 font-lock-warning-face t))
391 ;; Don't cut a sentence right after the first word (better to move
392 ;; the sentence on the next line, then).
393 ;;("[.:]\\s-+\\(\\sw+\\)\\s-*$" (1 font-lock-warning-face t))
394 ;; Change Log entries should use present tense.
395 ("):[ \t\n]*[[:alpha:]]+\\(ed\\)\\>"
396 (1 '(face font-lock-warning-face help-echo "Use present tense.") t))
397 ;; Change log entries start with a capital letter.
398 ("): [a-z]" (0 '(face font-lock-warning-face help-echo "Capitalize.") t))
399 ("[^[:upper:]]\\(\\. [[:upper:]]\\)"
400 (1 '(face font-lock-warning-face
401 help-echo "Use two spaces to end a sentence") t))
402 ("^("
403 (0 (let ((beg (max (point-min) (- (match-beginning 0) 2))))
404 (put-text-property beg (match-end 0) 'font-lock-multiline t)
405 (if (eq (char-syntax (char-after beg)) ?w)
406 '(face font-lock-warning-face
407 help-echo "Punctuate previous line.")))
411 (defun log-edit-font-lock-keywords ()
412 (if log-edit-font-lock-gnu-style
413 (append log-edit-font-lock-keywords
414 log-edit-font-lock-gnu-keywords)
415 log-edit-font-lock-keywords))
417 ;;;###autoload
418 (defun log-edit (callback &optional setup params buffer mode &rest _ignore)
419 "Setup a buffer to enter a log message.
420 The buffer is put in mode MODE or `log-edit-mode' if MODE is nil.
421 \\<log-edit-mode-map>
422 If SETUP is non-nil, erase the buffer and run `log-edit-hook'.
423 Set mark and point around the entire contents of the buffer, so
424 that it is easy to kill the contents of the buffer with
425 \\[kill-region]. Once the user is done editing the message,
426 invoking the command \\[log-edit-done] (`log-edit-done') will
427 call CALLBACK to do the actual commit.
429 PARAMS if non-nil is an alist of variables and buffer-local
430 values to give them in the Log Edit buffer. Possible keys and
431 associated values:
432 `log-edit-listfun' -- function taking no arguments that returns the list of
433 files that are concerned by the current operation (using relative names);
434 `log-edit-diff-function' -- function taking no arguments that
435 displays a diff of the files concerned by the current operation.
436 `vc-log-fileset' -- the VC fileset to be committed (if any).
438 If BUFFER is non-nil `log-edit' will jump to that buffer, use it
439 to edit the log message and go back to the current buffer when
440 done. Otherwise, it uses the current buffer."
441 (let ((parent (current-buffer)))
442 (if buffer (pop-to-buffer buffer))
443 (when (and log-edit-setup-invert (not (eq setup 'force)))
444 (setq setup (not setup)))
445 (if mode
446 (funcall mode)
447 (log-edit-mode))
448 (set (make-local-variable 'log-edit-callback) callback)
449 (if (listp params)
450 (dolist (crt params)
451 (set (make-local-variable (car crt)) (cdr crt)))
452 ;; For backward compatibility with log-edit up to version 22.2
453 ;; accept non-list PARAMS to mean `log-edit-list'.
454 (set (make-local-variable 'log-edit-listfun) params))
456 (if buffer (set (make-local-variable 'log-edit-parent-buffer) parent))
457 (set (make-local-variable 'log-edit-initial-files) (log-edit-files))
458 (when setup
459 (erase-buffer)
460 (run-hooks 'log-edit-hook))
461 (push-mark (point-max))
462 (message "%s" (substitute-command-keys
463 "Press \\[log-edit-done] when you are done editing."))))
465 (define-derived-mode log-edit-mode text-mode "Log-Edit"
466 "Major mode for editing version-control log messages.
467 When done editing the log entry, just type \\[log-edit-done] which
468 will trigger the actual commit of the file(s).
469 Several other handy support commands are provided of course and
470 the package from which this is used might also provide additional
471 commands (under C-x v for VC, for example).
473 \\{log-edit-mode-map}"
474 (set (make-local-variable 'font-lock-defaults)
475 '(log-edit-font-lock-keywords t))
476 (make-local-variable 'log-edit-comment-ring-index)
477 (add-hook 'kill-buffer-hook 'log-edit-remember-comment nil t)
478 (hack-dir-local-variables-non-file-buffer))
480 (defun log-edit-hide-buf (&optional buf where)
481 (when (setq buf (get-buffer (or buf log-edit-files-buf)))
482 ;; FIXME: Should use something like `quit-windows-on' here, but
483 ;; that function never deletes this buffer's window because it
484 ;; was created using `cvs-pop-to-buffer-same-frame'.
485 (save-selected-window
486 (let ((win (get-buffer-window buf where)))
487 (if win (ignore-errors (delete-window win))))
488 (bury-buffer buf))))
490 (defun log-edit-remember-comment (&optional comment)
491 (unless comment (setq comment (buffer-string)))
492 (when (or (ring-empty-p log-edit-comment-ring)
493 (not (equal comment (ring-ref log-edit-comment-ring 0))))
494 (ring-insert log-edit-comment-ring comment)))
496 (defun log-edit-done ()
497 "Finish editing the log message and commit the files.
498 If you want to abort the commit, simply delete the buffer."
499 (interactive)
500 ;; Clean up empty headers.
501 (goto-char (point-min))
502 (while (looking-at (concat "^[a-z]*:" log-edit-header-contents-regexp))
503 (let ((beg (match-beginning 0)))
504 (goto-char (match-end 0))
505 (if (string-match "\\`[ \n\t]*\\'" (match-string 1))
506 (delete-region beg (point)))))
507 ;; Get rid of leading empty lines.
508 (goto-char (point-min))
509 (when (looking-at "\\([ \t]*\n\\)+")
510 (delete-region (match-beginning 0) (match-end 0)))
511 ;; Get rid of trailing empty lines
512 (goto-char (point-max))
513 (skip-syntax-backward " ")
514 (when (equal (char-after) ?\n) (forward-char 1))
515 (delete-region (point) (point-max))
516 ;; Check for final newline
517 (if (and (> (point-max) (point-min))
518 (/= (char-before (point-max)) ?\n)
519 (or (eq log-edit-require-final-newline t)
520 (and log-edit-require-final-newline
521 (y-or-n-p
522 (format "Buffer %s does not end in newline. Add one? "
523 (buffer-name))))))
524 (save-excursion
525 (goto-char (point-max))
526 (insert ?\n)))
527 (log-edit-remember-comment)
528 (let ((win (get-buffer-window log-edit-files-buf)))
529 (if (and log-edit-confirm
530 (not (and (eq log-edit-confirm 'changed)
531 (equal (log-edit-files) log-edit-initial-files)))
532 (progn
533 (log-edit-show-files)
534 (not (y-or-n-p "Really commit? "))))
535 (progn (when (not win) (log-edit-hide-buf))
536 (message "Oh, well! Later maybe?"))
537 (run-hooks 'log-edit-done-hook)
538 (log-edit-hide-buf)
539 (unless (or log-edit-keep-buffer (not log-edit-parent-buffer))
540 (cvs-bury-buffer (current-buffer) log-edit-parent-buffer))
541 (call-interactively log-edit-callback))))
543 (defun log-edit-kill-buffer ()
544 "Kill the current buffer.
545 Also saves its contents in the comment history and hides
546 `log-edit-files-buf'."
547 (interactive)
548 (log-edit-hide-buf)
549 (let ((buf (current-buffer)))
550 (quit-windows-on buf)
551 (kill-buffer buf)))
553 (defun log-edit-files ()
554 "Return the list of files that are about to be committed."
555 (ignore-errors (funcall log-edit-listfun)))
557 (defun log-edit-mode-help ()
558 "Provide help for the `log-edit-mode-map'."
559 (interactive)
560 (if (eq last-command 'log-edit-mode-help)
561 (describe-function major-mode)
562 (message "%s"
563 (substitute-command-keys
564 "Type `\\[log-edit-done]' to finish commit. Try `\\[describe-function] log-edit-done' for more help."))))
566 (defcustom log-edit-common-indent 0
567 "Minimum indentation to use in `log-edit-set-common-indentation'."
568 :group 'log-edit
569 :type 'integer)
571 (defun log-edit-set-common-indentation ()
572 "(Un)Indent the current buffer rigidly to `log-edit-common-indent'."
573 (save-excursion
574 (let ((common (point-max)))
575 (rfc822-goto-eoh)
576 (while (< (point) (point-max))
577 (if (not (looking-at "^[ \t]*$"))
578 (setq common (min common (current-indentation))))
579 (forward-line 1))
580 (rfc822-goto-eoh)
581 (indent-rigidly (point) (point-max)
582 (- log-edit-common-indent common)))))
584 (defun log-edit-show-diff ()
585 "Show the diff for the files to be committed."
586 (interactive)
587 (if (functionp log-edit-diff-function)
588 (funcall log-edit-diff-function)
589 (error "Diff functionality has not been setup")))
591 (defun log-edit-show-files ()
592 "Show the list of files to be committed."
593 (interactive)
594 (let* ((files (log-edit-files))
595 (buf (get-buffer-create log-edit-files-buf)))
596 (with-current-buffer buf
597 (log-edit-hide-buf buf 'all)
598 (setq buffer-read-only nil)
599 (erase-buffer)
600 (cvs-insert-strings files)
601 (setq buffer-read-only t)
602 (goto-char (point-min))
603 (save-selected-window
604 (cvs-pop-to-buffer-same-frame buf)
605 (shrink-window-if-larger-than-buffer)
606 (set-window-dedicated-p (selected-window) t)
607 (selected-window)))))
609 (defun log-edit-beginning-of-line (&optional n)
610 "Move point to beginning of header value or to beginning of line.
612 It works the same as `message-beginning-of-line', but it uses a
613 different header separator appropriate for `log-edit-mode'."
614 (interactive "p")
615 (let ((mail-header-separator ""))
616 (message-beginning-of-line n)))
618 (defun log-edit-empty-buffer-p ()
619 "Return non-nil if the buffer is \"empty\"."
620 (or (= (point-min) (point-max))
621 (save-excursion
622 (goto-char (point-min))
623 (while (and (looking-at "^\\([a-zA-Z]+: ?\\)?$")
624 (zerop (forward-line 1))))
625 (eobp))))
627 (defun log-edit-insert-message-template ()
628 "Insert the default template with Summary and Author."
629 (interactive)
630 (when (or (called-interactively-p 'interactive)
631 (log-edit-empty-buffer-p))
632 (insert "Summary: ")
633 (when log-edit-setup-add-author
634 (insert "\nAuthor: "))
635 (insert "\n\n")
636 (message-position-point)))
638 (defun log-edit-insert-cvs-template ()
639 "Insert the template specified by the CVS administrator, if any.
640 This simply uses the local CVS/Template file."
641 (interactive)
642 (when (or (called-interactively-p 'interactive)
643 (log-edit-empty-buffer-p))
644 ;; Should the template take precedence over an empty Summary:,
645 ;; ie should we first erase the buffer?
646 (when (file-readable-p "CVS/Template")
647 (goto-char (point-max))
648 (insert-file-contents "CVS/Template"))))
650 (defun log-edit-insert-cvs-rcstemplate ()
651 "Insert the rcstemplate from the CVS repository.
652 This contacts the repository to get the rcstemplate file and
653 can thus take some time."
654 (interactive)
655 (when (or (called-interactively-p 'interactive)
656 (log-edit-empty-buffer-p))
657 (when (file-readable-p "CVS/Root")
658 (goto-char (point-max))
659 ;; Ignore the stderr stuff, even if it's an error.
660 (call-process "cvs" nil '(t nil) nil
661 "checkout" "-p" "CVSROOT/rcstemplate"))))
663 (defun log-edit-insert-filenames ()
664 "Insert the list of files that are to be committed."
665 (interactive)
666 (insert "Affected files: \n"
667 (mapconcat 'identity (log-edit-files) " \n")))
669 (defun log-edit-insert-filenames-without-changelog ()
670 "Insert the list of files that have no ChangeLog message."
671 (interactive)
672 (let ((files
673 (delq nil
674 (mapcar
675 (lambda (file)
676 (unless (or (cdr-safe (log-edit-changelog-entries file))
677 (equal (file-name-nondirectory file) "ChangeLog"))
678 file))
679 (log-edit-files)))))
680 (when files
681 (goto-char (point-max))
682 (insert (mapconcat 'identity files ", ") ": "))))
684 (defun log-edit-add-to-changelog ()
685 "Insert this log message into the appropriate ChangeLog file."
686 (interactive)
687 (log-edit-remember-comment)
688 (dolist (f (log-edit-files))
689 (let ((buffer-file-name (expand-file-name f)))
690 (save-excursion
691 (log-edit-comment-to-change-log)))))
693 (defvar log-edit-changelog-use-first nil)
695 (defvar log-edit-rewrite-fixes nil
696 "Rule to rewrite bug numbers into Fixes: headers.
697 The value should be of the form (REGEXP . REPLACEMENT)
698 where REGEXP should match the expression referring to a bug number
699 in the text, and REPLACEMENT is an expression to pass to `replace-match'
700 to build the Fixes: header.")
701 (put 'log-edit-rewrite-fixes 'safe-local-variable
702 (lambda (v) (and (stringp (car-safe v)) (stringp (cdr v)))))
704 (defun log-edit-add-field (field value)
705 (rfc822-goto-eoh)
706 (if (save-excursion (re-search-backward (concat "^" field ":\\([ \t]*\\)$")
707 nil t))
708 (replace-match (concat " " value) t t nil 1)
709 (insert field ": " value "\n" (if (looking-at "\n") "" "\n"))))
711 (defun log-edit-insert-changelog (&optional use-first)
712 "Insert a log message by looking at the ChangeLog.
713 The idea is to write your ChangeLog entries first, and then use this
714 command to commit your changes.
716 To select default log text, we:
717 - find the ChangeLog entries for the files to be checked in,
718 - verify that the top entry in the ChangeLog is on the current date
719 and by the current user; if not, we don't provide any default text,
720 - search the ChangeLog entry for paragraphs containing the names of
721 the files we're checking in, and finally
722 - use those paragraphs as the log text.
724 If the optional prefix arg USE-FIRST is given (via \\[universal-argument]),
725 or if the command is repeated a second time in a row, use the first log entry
726 regardless of user name or time."
727 (interactive "P")
728 (save-excursion
729 (let ((eoh (save-excursion (rfc822-goto-eoh) (point))))
730 (when (<= (point) eoh)
731 (goto-char eoh)
732 (if (looking-at "\n") (forward-char 1))))
733 (let ((author
734 (let ((log-edit-changelog-use-first
735 (or use-first (eq last-command 'log-edit-insert-changelog))))
736 (log-edit-insert-changelog-entries (log-edit-files)))))
737 (log-edit-set-common-indentation)
738 ;; Add an Author: field if appropriate.
739 (when author (log-edit-add-field "Author" author))
740 ;; Add a Fixes: field if applicable.
741 (when (consp log-edit-rewrite-fixes)
742 (rfc822-goto-eoh)
743 (when (re-search-forward (car log-edit-rewrite-fixes) nil t)
744 (let ((start (match-beginning 0))
745 (end (match-end 0))
746 (fixes (match-substitute-replacement
747 (cdr log-edit-rewrite-fixes))))
748 (delete-region start end)
749 (log-edit-add-field "Fixes" fixes))))
750 (and log-edit-strip-single-file-name
751 (progn (rfc822-goto-eoh)
752 (if (looking-at "\n") (forward-char 1))
753 (looking-at "\\*\\s-+"))
754 (let ((start (point)))
755 (forward-line 1)
756 (when (not (re-search-forward "^\\*\\s-+" nil t))
757 (goto-char start)
758 (skip-chars-forward "^():")
759 (skip-chars-forward ": ")
760 (delete-region start (point))))))))
762 ;;;;
763 ;;;; functions for getting commit message from ChangeLog a file...
764 ;;;; Courtesy Jim Blandy
765 ;;;;
767 (defun log-edit-narrow-changelog ()
768 "Narrow to the top page of the current buffer, a ChangeLog file.
769 Actually, the narrowed region doesn't include the date line.
770 A \"page\" in a ChangeLog file is the area between two dates."
771 (or (eq major-mode 'change-log-mode)
772 (error "log-edit-narrow-changelog: current buffer isn't a ChangeLog"))
774 (goto-char (point-min))
776 ;; Skip date line and subsequent blank lines.
777 (forward-line 1)
778 (if (looking-at "[ \t\n]*\n")
779 (goto-char (match-end 0)))
781 (let ((start (point)))
782 (forward-page 1)
783 (narrow-to-region start (point))
784 (goto-char (point-min))))
786 (defun log-edit-changelog-paragraph ()
787 "Return the bounds of the ChangeLog paragraph containing point.
788 If we are between paragraphs, return the previous paragraph."
789 (beginning-of-line)
790 (if (looking-at "^[ \t]*$")
791 (skip-chars-backward " \t\n" (point-min)))
792 (list (progn
793 (if (re-search-backward "^[ \t]*\n" nil 'or-to-limit)
794 (goto-char (match-end 0)))
795 (point))
796 (if (re-search-forward "^[ \t\n]*$" nil t)
797 (match-beginning 0)
798 (point-max))))
800 (defun log-edit-changelog-subparagraph ()
801 "Return the bounds of the ChangeLog subparagraph containing point.
802 A subparagraph is a block of non-blank lines beginning with an asterisk.
803 If we are between sub-paragraphs, return the previous subparagraph."
804 (end-of-line)
805 (if (search-backward "*" nil t)
806 (list (progn (beginning-of-line) (point))
807 (progn
808 (forward-line 1)
809 (if (re-search-forward "^[ \t]*[\n*]" nil t)
810 (match-beginning 0)
811 (point-max))))
812 (list (point) (point))))
814 (defun log-edit-changelog-entry ()
815 "Return the bounds of the ChangeLog entry containing point.
816 The variable `log-edit-changelog-full-paragraphs' decides whether an
817 \"entry\" is a paragraph or a subparagraph; see its documentation string
818 for more details."
819 (save-excursion
820 (if log-edit-changelog-full-paragraphs
821 (log-edit-changelog-paragraph)
822 (log-edit-changelog-subparagraph))))
824 (defvar user-full-name)
825 (defvar user-mail-address)
827 (defvar log-edit-author) ;Dynamically scoped.
829 (defun log-edit-changelog-ours-p ()
830 "See if ChangeLog entry at point is for the current user, today.
831 Return non-nil if it is."
832 ;; Code adapted from add-change-log-entry.
833 (let ((name (or (and (boundp 'add-log-full-name) add-log-full-name)
834 (and (fboundp 'user-full-name) (user-full-name))
835 (and (boundp 'user-full-name) user-full-name)))
836 (mail (or (and (boundp 'add-log-mailing-address) add-log-mailing-address)
837 ;;(and (fboundp 'user-mail-address) (user-mail-address))
838 (and (boundp 'user-mail-address) user-mail-address)))
839 (time (or (and (boundp 'add-log-time-format)
840 (functionp add-log-time-format)
841 (funcall add-log-time-format))
842 (format-time-string "%Y-%m-%d"))))
843 (if (null log-edit-changelog-use-first)
844 (looking-at (regexp-quote (format "%s %s <%s>" time name mail)))
845 ;; Check the author, to potentially add it as a "Author: " header.
846 (when (looking-at "[^ \t]")
847 (when (and (boundp 'log-edit-author)
848 (not (looking-at (format ".+ .+ <%s>"
849 (regexp-quote mail))))
850 (looking-at ".+ \\(.+ <.+>\\)"))
851 (let ((author (replace-regexp-in-string " " " "
852 (match-string 1))))
853 (unless (and log-edit-author
854 (string-match (regexp-quote author) log-edit-author))
855 (setq log-edit-author
856 (if log-edit-author
857 (concat log-edit-author ", " author)
858 author)))))
859 t))))
861 (defun log-edit-changelog-entries (file)
862 "Return the ChangeLog entries for FILE, and the ChangeLog they came from.
863 The return value looks like this:
864 (LOGBUFFER (ENTRYSTART ENTRYEND) ...)
865 where LOGBUFFER is the name of the ChangeLog buffer, and each
866 \(ENTRYSTART . ENTRYEND\) pair is a buffer region."
867 (let ((changelog-file-name
868 (let ((default-directory
869 (file-name-directory (expand-file-name file)))
870 (visiting-buffer (find-buffer-visiting file)))
871 ;; If there is a buffer visiting FILE, and it has a local
872 ;; value for `change-log-default-name', use that.
873 (if (and visiting-buffer
874 (local-variable-p 'change-log-default-name
875 visiting-buffer))
876 (with-current-buffer visiting-buffer
877 change-log-default-name)
878 ;; `find-change-log' uses `change-log-default-name' if set
879 ;; and sets it before exiting, so we need to work around
880 ;; that memoizing which is undesired here.
881 (setq change-log-default-name nil)
882 (find-change-log)))))
883 (with-current-buffer (find-file-noselect changelog-file-name)
884 (unless (eq major-mode 'change-log-mode) (change-log-mode))
885 (goto-char (point-min))
886 (if (looking-at "\\s-*\n") (goto-char (match-end 0)))
887 (if (not (log-edit-changelog-ours-p))
888 (list (current-buffer))
889 (save-restriction
890 (log-edit-narrow-changelog)
891 (goto-char (point-min))
893 ;; Search for the name of FILE relative to the ChangeLog. If that
894 ;; doesn't occur anywhere, they're not using full relative
895 ;; filenames in the ChangeLog, so just look for FILE; we'll accept
896 ;; some false positives.
897 (let ((pattern (file-relative-name
898 file (file-name-directory changelog-file-name))))
899 (if (or (string= pattern "")
900 (not (save-excursion
901 (search-forward pattern nil t))))
902 (setq pattern (file-name-nondirectory file)))
904 (setq pattern (concat "\\(^\\|[^[:alnum:]]\\)"
905 (regexp-quote pattern)
906 "\\($\\|[^[:alnum:]]\\)"))
908 (let (texts
909 (pos (point)))
910 (while (and (not (eobp)) (re-search-forward pattern nil t))
911 (let ((entry (log-edit-changelog-entry)))
912 (if (< (elt entry 1) (max (1+ pos) (point)))
913 ;; This is not relevant, actually.
915 (push entry texts))
916 ;; Make sure we make progress.
917 (setq pos (max (1+ pos) (elt entry 1)))
918 (goto-char pos)))
920 (cons (current-buffer) texts))))))))
922 (defun log-edit-changelog-insert-entries (buffer beg end &rest files)
923 "Insert the text from BUFFER between BEG and END.
924 Rename relative filenames in the ChangeLog entry as FILES."
925 (let ((opoint (point))
926 (log-name (buffer-file-name buffer))
927 (case-fold-search nil)
928 bound)
929 (insert-buffer-substring buffer beg end)
930 (setq bound (point-marker))
931 (when log-name
932 (dolist (f files)
933 (save-excursion
934 (goto-char opoint)
935 (when (re-search-forward
936 (concat "\\(^\\|[ \t]\\)\\("
937 (file-relative-name f (file-name-directory log-name))
938 "\\)[, :\n]")
939 bound t)
940 (replace-match f t t nil 2)))))
941 ;; Eliminate tabs at the beginning of the line.
942 (save-excursion
943 (goto-char opoint)
944 (while (re-search-forward "^\\(\t+\\)" bound t)
945 (replace-match "")))))
947 (defun log-edit-insert-changelog-entries (files)
948 "Given a list of files FILES, insert the ChangeLog entries for them."
949 (let ((log-entries nil)
950 (log-edit-author nil))
951 ;; Note that any ChangeLog entry can apply to more than one file.
952 ;; Here we construct a log-entries list with elements of the form
953 ;; ((LOGBUFFER ENTRYSTART ENTRYEND) FILE1 FILE2...)
954 (dolist (file files)
955 (let* ((entries (log-edit-changelog-entries file))
956 (buf (car entries))
957 key entry)
958 (dolist (region (cdr entries))
959 (setq key (cons buf region))
960 (if (setq entry (assoc key log-entries))
961 (setcdr entry (append (cdr entry) (list file)))
962 (push (list key file) log-entries)))))
963 ;; Now map over log-entries, and extract the strings.
964 (dolist (log-entry (nreverse log-entries))
965 (apply 'log-edit-changelog-insert-entries
966 (append (car log-entry) (cdr log-entry)))
967 (insert "\n"))
968 log-edit-author))
970 (defun log-edit-toggle-header (header value)
971 "Toggle a boolean-type header in the current buffer.
972 See `log-edit-set-header' for details."
973 (log-edit-set-header header value t))
975 (defun log-edit-set-header (header value &optional toggle)
976 "Set the value of HEADER to VALUE in the current buffer.
977 If TOGGLE is non-nil, and the value of HEADER already is VALUE,
978 clear it. Make sure there is an empty line after the headers.
979 Return t if toggled on (or TOGGLE is nil), otherwise nil."
980 (let ((val t)
981 (line (concat header ": " value "\n")))
982 (save-excursion
983 (save-restriction
984 (rfc822-goto-eoh)
985 (narrow-to-region (point-min) (point))
986 (goto-char (point-min))
987 (if (re-search-forward (concat "^" header ":"
988 log-edit-header-contents-regexp)
989 nil t)
990 (if (setq val (not (and toggle (string= (match-string 1) value))))
991 (replace-match line t t)
992 (replace-match "" t t nil 1))
993 (insert line)))
994 (rfc822-goto-eoh)
995 (delete-horizontal-space)
996 (unless (looking-at "\n")
997 (insert "\n")))
998 val))
1000 (defun log-edit-extract-headers (headers comment)
1001 "Extract headers from COMMENT to form command line arguments.
1002 HEADERS should be an alist with elements (HEADER . CMDARG)
1003 or (HEADER . FUNCTION) associating headers to command line
1004 options and the result is then a list of the form (MSG ARGUMENTS...)
1005 where MSG is the remaining text from COMMENT.
1006 FUNCTION should be a function of one argument that takes the
1007 header value and returns the list of strings to be appended to
1008 ARGUMENTS. CMDARG will be added to ARGUMENTS followed by the
1009 header value. If \"Summary\" is not in HEADERS, then the
1010 \"Summary\" header is extracted anyway and put back as the first
1011 line of MSG."
1012 (with-temp-buffer
1013 (insert comment)
1014 (rfc822-goto-eoh)
1015 (narrow-to-region (point-min) (point))
1016 (let ((case-fold-search t)
1017 (summary ())
1018 (res ()))
1019 (dolist (header (if (assoc "Summary" headers) headers
1020 (cons '("Summary" . t) headers)))
1021 (goto-char (point-min))
1022 (while (re-search-forward (concat "^" (car header)
1023 ":" log-edit-header-contents-regexp)
1024 nil t)
1025 (let ((txt (match-string 1)))
1026 (replace-match "" t t)
1027 (if (eq t (cdr header))
1028 (setq summary txt)
1029 (if (functionp (cdr header))
1030 (setq res (nconc res (funcall (cdr header) txt)))
1031 (push txt res)
1032 (push (or (cdr header) (car header)) res))))))
1033 ;; Remove header separator if the header is empty.
1034 (widen)
1035 (goto-char (point-min))
1036 (when (looking-at "\\([ \t]*\n\\)+")
1037 (delete-region (match-beginning 0) (match-end 0)))
1038 (if summary (insert summary "\n\n"))
1039 (cons (buffer-string) res))))
1041 (provide 'log-edit)
1043 ;;; log-edit.el ends here