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