1 ;;; log-edit.el --- Major mode for editing CVS commit messages -*- lexical-binding: t -*-
3 ;; Copyright (C) 1999-2013 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/>.
27 ;; - Move in VC's code
28 ;; - Add compatibility for VC's hook variables
32 (require 'add-log
) ; for all the ChangeLog goodies
40 (defgroup log-edit nil
41 "Major mode for editing RCS and CVS commit messages."
43 :group
'vc
; It's used by VC.
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)."
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'."
74 :help
"Exit log-edit and proceed with the actual action."]
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"]
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."]
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."
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'."
107 (defcustom log-edit-require-final-newline t
108 "Enforce a newline at the end of commit log messages.
109 Enforce it silently if t, query if non-nil and don't do anything if nil."
111 :type
'(choice (const ask
) (const t
) (const nil
)))
113 (defcustom log-edit-setup-invert nil
114 "Non-nil means `log-edit' should invert the meaning of its SETUP arg.
115 If SETUP is 'force, this variable has no effect."
119 (defcustom log-edit-hook
'(log-edit-insert-cvs-template
121 log-edit-insert-changelog
)
122 "Hook run at the end of `log-edit'."
124 :type
'(hook :options
(log-edit-insert-changelog
125 log-edit-insert-cvs-rcstemplate
126 log-edit-insert-cvs-template
127 log-edit-insert-filenames
)))
129 (defcustom log-edit-mode-hook
(if (boundp 'vc-log-mode-hook
) vc-log-mode-hook
)
130 "Hook run when entering `log-edit-mode'."
134 (defcustom log-edit-done-hook nil
135 "Hook run before doing the actual commit.
136 This hook can be used to cleanup the message, enforce various
137 conventions, or to allow recording the message in some other database,
138 such as a bug-tracking system. The list of files about to be committed
139 can be obtained from `log-edit-files'."
141 :type
'(hook :options
(log-edit-set-common-indentation
142 log-edit-add-to-changelog
)))
144 (defcustom log-edit-strip-single-file-name nil
145 "If non-nil, remove file name from single-file log entries."
151 (defvar log-edit-changelog-full-paragraphs t
152 "If non-nil, include full ChangeLog paragraphs in the log.
153 This may be set in the ``local variables'' section of a ChangeLog, to
154 indicate the policy for that ChangeLog.
156 A ChangeLog paragraph is a bunch of log text containing no blank lines;
157 a paragraph usually describes a set of changes with a single purpose,
158 but perhaps spanning several functions in several files. Changes in
159 different paragraphs are unrelated.
161 You could argue that the log entry for a file should contain the
162 full ChangeLog paragraph mentioning the change to the file, even though
163 it may mention other files, because that gives you the full context you
164 need to understand the change. This is the behavior you get when this
165 variable is set to t.
167 On the other hand, you could argue that the log entry for a change
168 should contain only the text for the changes which occurred in that
169 file, because the log is per-file. This is the behavior you get
170 when this variable is set to nil.")
172 ;;;; Internal global or buffer-local vars
174 (defconst log-edit-files-buf
"*log-edit-files*")
175 (defvar log-edit-initial-files nil
)
176 (defvar log-edit-callback nil
)
177 (defvar log-edit-diff-function nil
)
178 (defvar log-edit-listfun nil
)
180 (defvar log-edit-parent-buffer nil
)
182 (defvar log-edit-vc-backend nil
183 "VC fileset corresponding to the current log.")
185 ;;; Originally taken from VC-Log mode
187 (defconst log-edit-maximum-comment-ring-size
32
188 "Maximum number of saved comments in the comment ring.")
189 (define-obsolete-variable-alias 'vc-comment-ring
'log-edit-comment-ring
"22.1")
190 (defvar log-edit-comment-ring
(make-ring log-edit-maximum-comment-ring-size
))
191 (define-obsolete-variable-alias 'vc-comment-ring-index
192 'log-edit-comment-ring-index
"22.1")
193 (defvar log-edit-comment-ring-index nil
)
194 (defvar log-edit-last-comment-match
"")
196 (defun log-edit-new-comment-index (stride len
)
197 "Return the comment index STRIDE elements from the current one.
198 LEN is the length of `log-edit-comment-ring'."
200 (log-edit-comment-ring-index (+ log-edit-comment-ring-index stride
))
201 ;; Initialize the index on the first use of this command
202 ;; so that the first M-p gets index 0, and the first M-n gets
204 ((> stride
0) (1- stride
))
208 (defun log-edit-previous-comment (arg)
209 "Cycle backwards through comment history.
210 With a numeric prefix ARG, go back ARG comments."
212 (let ((len (ring-length log-edit-comment-ring
)))
214 (progn (message "Empty comment ring") (ding))
215 ;; Don't use `erase-buffer' because we don't want to `widen'.
216 (delete-region (point-min) (point-max))
217 (setq log-edit-comment-ring-index
(log-edit-new-comment-index arg len
))
218 (message "Comment %d" (1+ log-edit-comment-ring-index
))
219 (insert (ring-ref log-edit-comment-ring log-edit-comment-ring-index
)))))
221 (defun log-edit-next-comment (arg)
222 "Cycle forwards through comment history.
223 With a numeric prefix ARG, go forward ARG comments."
225 (log-edit-previous-comment (- arg
)))
227 (defun log-edit-comment-search-backward (str &optional stride
)
228 "Search backwards through comment history for substring match of STR.
229 If the optional argument STRIDE is present, that is a step-width to use
230 when going through the comment ring."
231 ;; Why substring rather than regexp ? -sm
233 (list (read-string "Comment substring: " nil nil log-edit-last-comment-match
)))
234 (unless stride
(setq stride
1))
236 (setq str log-edit-last-comment-match
)
237 (setq log-edit-last-comment-match str
))
238 (let* ((str (regexp-quote str
))
239 (len (ring-length log-edit-comment-ring
))
240 (n (log-edit-new-comment-index stride len
)))
241 (while (progn (when (or (>= n len
) (< n
0)) (error "Not found"))
242 (not (string-match str
(ring-ref log-edit-comment-ring n
))))
243 (setq n
(+ n stride
)))
244 (setq log-edit-comment-ring-index n
)
245 (log-edit-previous-comment 0)))
247 (defun log-edit-comment-search-forward (str)
248 "Search forwards through comment history for a substring match of STR."
250 (list (read-string "Comment substring: " nil nil log-edit-last-comment-match
)))
251 (log-edit-comment-search-backward str -
1))
253 (defun log-edit-comment-to-change-log (&optional whoami file-name
)
254 "Enter last VC comment into the change log for the current file.
255 WHOAMI (interactive prefix) non-nil means prompt for user name
256 and site. FILE-NAME is the name of the change log; if nil, use
257 `change-log-default-name'.
259 This may be useful as a `log-edit-checkin-hook' to update change logs
261 (interactive (if current-prefix-arg
262 (list current-prefix-arg
263 (prompt-for-change-log-name))))
264 (let (;; Extract the comment first so we get any error before doing anything.
265 (comment (ring-ref log-edit-comment-ring
0))
266 ;; Don't let add-change-log-entry insert a defun name.
267 (add-log-current-defun-function 'ignore
)
269 ;; Call add-log to do half the work.
270 (add-change-log-entry whoami file-name t t
)
271 ;; Insert the VC comment, leaving point before it.
272 (setq end
(save-excursion (insert comment
) (point-marker)))
273 (if (looking-at "\\s *\\s(")
274 ;; It starts with an open-paren, as in "(foo): Frobbed."
275 ;; So remove the ": " add-log inserted.
277 ;; Canonicalize the white space between the file name and comment.
279 ;; Indent rest of the text the same way add-log indented the first line.
280 (let ((indentation (current-indentation)))
282 (while (< (point) end
)
284 (indent-to indentation
))
286 ;; Fill the inserted text, preserving open-parens at bol.
287 (let ((paragraph-start (concat paragraph-start
"\\|\\s *\\s(")))
289 (fill-region (point) end
))
290 ;; Canonicalize the white space at the end of the entry so it is
291 ;; separated from the next entry by a single blank line.
292 (skip-syntax-forward " " end
)
293 (delete-char (- (skip-syntax-backward " ")))
294 (or (eobp) (looking-at "\n\n")
297 ;; Compatibility with old names.
298 (define-obsolete-function-alias 'vc-previous-comment
'log-edit-previous-comment
"22.1")
299 (define-obsolete-function-alias 'vc-next-comment
'log-edit-next-comment
"22.1")
300 (define-obsolete-function-alias 'vc-comment-search-reverse
'log-edit-comment-search-backward
"22.1")
301 (define-obsolete-function-alias 'vc-comment-search-forward
'log-edit-comment-search-forward
"22.1")
302 (define-obsolete-function-alias 'vc-comment-to-change-log
'log-edit-comment-to-change-log
"22.1")
308 (defface log-edit-summary
'((t :inherit font-lock-function-name-face
))
309 "Face for the summary in `log-edit-mode' buffers.")
311 (defface log-edit-header
'((t :inherit font-lock-keyword-face
))
312 "Face for the headers in `log-edit-mode' buffers.")
314 (defface log-edit-unknown-header
'((t :inherit font-lock-comment-face
))
315 "Face for unknown headers in `log-edit-mode' buffers.")
317 (defvar log-edit-headers-alist
'(("Summary" . log-edit-summary
)
318 ("Fixes") ("Author"))
319 "AList of known headers and the face to use to highlight them.")
321 (defconst log-edit-header-contents-regexp
322 "[ \t]*\\(.*\\(\n[ \t].*\\)*\\)\n?"
323 "Regular expression matching a header field.
324 The first subexpression is the actual text of the field.")
326 (defun log-edit-match-to-eoh (_limit)
327 ;; FIXME: copied from message-match-to-eoh.
328 (let ((start (point)))
330 ;; Typical situation: some temporary change causes the header to be
331 ;; incorrect, so EOH comes earlier than intended: the last lines of the
332 ;; intended headers are now not considered part of the header any more,
333 ;; so they don't have the multiline property set. When the change is
334 ;; completed and the header has its correct shape again, the lack of the
335 ;; multiline property means we won't rehighlight the last lines of
337 (if (< (point) start
)
338 nil
;No header within start..limit.
339 ;; Here we disregard LIMIT so that we may extend the area again.
340 (set-match-data (list start
(point)))
343 (defvar log-edit-font-lock-keywords
344 ;; Copied/inspired by message-font-lock-keywords.
345 `((log-edit-match-to-eoh
346 (,(concat "^\\(\\([[:alpha:]-]+\\):\\)" log-edit-header-contents-regexp
)
347 (progn (goto-char (match-beginning 0)) (match-end 0)) nil
348 (1 (if (assoc-string (match-string 2) log-edit-headers-alist t
)
350 'log-edit-unknown-header
)
352 ;; From `log-edit-header-contents-regexp':
353 (3 (or (cdr (assoc-string (match-string 2) log-edit-headers-alist t
))
357 (progn (goto-char (match-end 0)) (1+ (match-end 0))) nil
358 (0 '(:height
0.1 :inverse-video t
))))))
360 (defvar log-edit-font-lock-gnu-style nil
361 "If non-nil, highlight common failures to follow the GNU coding standards.")
362 (put 'log-edit-font-lock-gnu-style
'safe-local-variable
'booleanp
)
364 (defconst log-edit-font-lock-gnu-keywords
366 ;; * foo.el (bla, bli)
369 ;; * foo.el (bla, bli,
371 '(("^[ \t]*\\(?:\\* .*\\)?\\(([^\n)]*,\\s-*\\)$"
372 (1 '(face font-lock-warning-face
373 help-echo
"Continue function lists with \")\\n(\".") t
))
374 ;; Don't leave a lone word on a single line.
375 ;;("^\\s-*\\(\\S-*[^\n:)]\\)\\s-*$" (1 font-lock-warning-face t))
376 ;; Don't cut a sentence right after the first word (better to move
377 ;; the sentence on the next line, then).
378 ;;("[.:]\\s-+\\(\\sw+\\)\\s-*$" (1 font-lock-warning-face t))
379 ;; Change Log entries should use present tense.
380 ("):[ \t\n]*[[:alpha:]]+\\(ed\\)\\>"
381 (1 '(face font-lock-warning-face help-echo
"Use present tense.") t
))
382 ;; Change log entries start with a capital letter.
383 ("): [a-z]" (0 '(face font-lock-warning-face help-echo
"Capitalize.") t
))
384 ("[^[:upper:]]\\(\\. [[:upper:]]\\)"
385 (1 '(face font-lock-warning-face
386 help-echo
"Use two spaces to end a sentence") t
))
388 (0 (let ((beg (max (point-min) (- (match-beginning 0) 2))))
389 (put-text-property beg
(match-end 0) 'font-lock-multiline t
)
390 (if (eq (char-syntax (char-after beg
)) ?w
)
391 '(face font-lock-warning-face
392 help-echo
"Punctuate previous line.")))
396 (defun log-edit-font-lock-keywords ()
397 (if log-edit-font-lock-gnu-style
398 (append log-edit-font-lock-keywords
399 log-edit-font-lock-gnu-keywords
)
400 log-edit-font-lock-keywords
))
403 (defun log-edit (callback &optional setup params buffer mode
&rest _ignore
)
404 "Setup a buffer to enter a log message.
405 The buffer is put in mode MODE or `log-edit-mode' if MODE is nil.
406 \\<log-edit-mode-map>
407 If SETUP is non-nil, erase the buffer and run `log-edit-hook'.
408 Set mark and point around the entire contents of the buffer, so
409 that it is easy to kill the contents of the buffer with
410 \\[kill-region]. Once the user is done editing the message,
411 invoking the command \\[log-edit-done] (`log-edit-done') will
412 call CALLBACK to do the actual commit.
414 PARAMS if non-nil is an alist of variables and buffer-local
415 values to give them in the Log Edit buffer. Possible keys and
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.
421 `vc-log-fileset' -- the VC fileset to be committed (if any).
423 If BUFFER is non-nil `log-edit' will jump to that buffer, use it
424 to edit the log message and go back to the current buffer when
425 done. Otherwise, it uses the current buffer."
426 (let ((parent (current-buffer)))
427 (if buffer
(pop-to-buffer buffer
))
428 (when (and log-edit-setup-invert
(not (eq setup
'force
)))
429 (setq setup
(not setup
)))
432 (insert "Summary: \nAuthor: ")
433 (save-excursion (insert "\n\n")))
437 (set (make-local-variable 'log-edit-callback
) callback
)
440 (set (make-local-variable (car crt
)) (cdr crt
)))
441 ;; For backward compatibility with log-edit up to version 22.2
442 ;; accept non-list PARAMS to mean `log-edit-list'.
443 (set (make-local-variable 'log-edit-listfun
) params
))
445 (if buffer
(set (make-local-variable 'log-edit-parent-buffer
) parent
))
446 (set (make-local-variable 'log-edit-initial-files
) (log-edit-files))
447 (when setup
(run-hooks 'log-edit-hook
))
448 (goto-char (point-min)) (push-mark (point-max))
449 (message "%s" (substitute-command-keys
450 "Press \\[log-edit-done] when you are done editing."))))
452 (define-derived-mode log-edit-mode text-mode
"Log-Edit"
453 "Major mode for editing version-control log messages.
454 When done editing the log entry, just type \\[log-edit-done] which
455 will trigger the actual commit of the file(s).
456 Several other handy support commands are provided of course and
457 the package from which this is used might also provide additional
458 commands (under C-x v for VC, for example).
460 \\{log-edit-mode-map}"
461 (set (make-local-variable 'font-lock-defaults
)
462 '(log-edit-font-lock-keywords t
))
463 (make-local-variable 'log-edit-comment-ring-index
)
464 (hack-dir-local-variables-non-file-buffer))
466 (defun log-edit-hide-buf (&optional buf where
)
467 (when (setq buf
(get-buffer (or buf log-edit-files-buf
)))
468 (let ((win (get-buffer-window buf where
)))
469 (if win
(ignore-errors (delete-window win
))))
472 (defun log-edit-done ()
473 "Finish editing the log message and commit the files.
474 If you want to abort the commit, simply delete the buffer."
476 ;; Clean up empty headers.
477 (goto-char (point-min))
478 (while (looking-at (concat "^[a-z]*:" log-edit-header-contents-regexp
))
479 (let ((beg (match-beginning 0)))
480 (goto-char (match-end 0))
481 (if (string-match "\\`[ \n\t]*\\'" (match-string 1))
482 (delete-region beg
(point)))))
483 ;; Get rid of leading empty lines.
484 (goto-char (point-min))
485 (when (looking-at "\\([ \t]*\n\\)+")
486 (delete-region (match-beginning 0) (match-end 0)))
487 ;; Get rid of trailing empty lines
488 (goto-char (point-max))
489 (skip-syntax-backward " ")
490 (when (equal (char-after) ?
\n) (forward-char 1))
491 (delete-region (point) (point-max))
492 ;; Check for final newline
493 (if (and (> (point-max) (point-min))
494 (/= (char-before (point-max)) ?
\n)
495 (or (eq log-edit-require-final-newline t
)
496 (and log-edit-require-final-newline
498 (format "Buffer %s does not end in newline. Add one? "
501 (goto-char (point-max))
503 (let ((comment (buffer-string)))
504 (when (or (ring-empty-p log-edit-comment-ring
)
505 (not (equal comment
(ring-ref log-edit-comment-ring
0))))
506 (ring-insert log-edit-comment-ring comment
)))
507 (let ((win (get-buffer-window log-edit-files-buf
)))
508 (if (and log-edit-confirm
509 (not (and (eq log-edit-confirm
'changed
)
510 (equal (log-edit-files) log-edit-initial-files
)))
512 (log-edit-show-files)
513 (not (y-or-n-p "Really commit? "))))
514 (progn (when (not win
) (log-edit-hide-buf))
515 (message "Oh, well! Later maybe?"))
516 (run-hooks 'log-edit-done-hook
)
518 (unless (or log-edit-keep-buffer
(not log-edit-parent-buffer
))
519 (cvs-bury-buffer (current-buffer) log-edit-parent-buffer
))
520 (call-interactively log-edit-callback
))))
522 (defun log-edit-files ()
523 "Return the list of files that are about to be committed."
524 (ignore-errors (funcall log-edit-listfun
)))
526 (defun log-edit-mode-help ()
527 "Provide help for the `log-edit-mode-map'."
529 (if (eq last-command
'log-edit-mode-help
)
530 (describe-function major-mode
)
532 (substitute-command-keys
533 "Type `\\[log-edit-done]' to finish commit. Try `\\[describe-function] log-edit-done' for more help."))))
535 (defcustom log-edit-common-indent
0
536 "Minimum indentation to use in `log-edit-set-common-indentation'."
540 (defun log-edit-set-common-indentation ()
541 "(Un)Indent the current buffer rigidly to `log-edit-common-indent'."
543 (let ((common (point-max)))
545 (while (< (point) (point-max))
546 (if (not (looking-at "^[ \t]*$"))
547 (setq common
(min common
(current-indentation))))
550 (indent-rigidly (point) (point-max)
551 (- log-edit-common-indent common
)))))
553 (defun log-edit-show-diff ()
554 "Show the diff for the files to be committed."
556 (if (functionp log-edit-diff-function
)
557 (funcall log-edit-diff-function
)
558 (error "Diff functionality has not been setup")))
560 (defun log-edit-show-files ()
561 "Show the list of files to be committed."
563 (let* ((files (log-edit-files))
564 (buf (get-buffer-create log-edit-files-buf
)))
565 (with-current-buffer buf
566 (log-edit-hide-buf buf
'all
)
567 (setq buffer-read-only nil
)
569 (cvs-insert-strings files
)
570 (setq buffer-read-only t
)
571 (goto-char (point-min))
572 (save-selected-window
573 (cvs-pop-to-buffer-same-frame buf
)
574 (shrink-window-if-larger-than-buffer)
575 (selected-window)))))
577 (defun log-edit-empty-buffer-p ()
578 "Return non-nil if the buffer is \"empty\"."
579 (or (= (point-min) (point-max))
581 (goto-char (point-min))
582 (while (and (looking-at "^\\([a-zA-Z]+: ?\\)?$")
583 (zerop (forward-line 1))))
586 (defun log-edit-insert-cvs-template ()
587 "Insert the template specified by the CVS administrator, if any.
588 This simply uses the local CVS/Template file."
590 (when (or (called-interactively-p 'interactive
)
591 (log-edit-empty-buffer-p))
592 ;; Should the template take precedence over an empty Summary:,
593 ;; ie should we first erase the buffer?
594 (when (file-readable-p "CVS/Template")
595 (goto-char (point-max))
596 (insert-file-contents "CVS/Template"))))
598 (defun log-edit-insert-cvs-rcstemplate ()
599 "Insert the rcstemplate from the CVS repository.
600 This contacts the repository to get the rcstemplate file and
601 can thus take some time."
603 (when (or (called-interactively-p 'interactive
)
604 (log-edit-empty-buffer-p))
605 (when (file-readable-p "CVS/Root")
606 (goto-char (point-max))
607 ;; Ignore the stderr stuff, even if it's an error.
608 (call-process "cvs" nil
'(t nil
) nil
609 "checkout" "-p" "CVSROOT/rcstemplate"))))
611 (defun log-edit-insert-filenames ()
612 "Insert the list of files that are to be committed."
614 (insert "Affected files: \n"
615 (mapconcat 'identity
(log-edit-files) " \n")))
617 (defun log-edit-add-to-changelog ()
618 "Insert this log message into the appropriate ChangeLog file."
621 (unless (string= (buffer-string) (ring-ref log-edit-comment-ring
0))
622 (ring-insert log-edit-comment-ring
(buffer-string)))
623 (dolist (f (log-edit-files))
624 (let ((buffer-file-name (expand-file-name f
)))
626 (log-edit-comment-to-change-log)))))
628 (defvar log-edit-changelog-use-first nil
)
630 (defvar log-edit-rewrite-fixes nil
631 "Rule to rewrite bug numbers into Fixes: headers.
632 The value should be of the form (REGEXP . REPLACEMENT)
633 where REGEXP should match the expression referring to a bug number
634 in the text, and REPLACEMENT is an expression to pass to `replace-match'
635 to build the Fixes: header.")
636 (put 'log-edit-rewrite-fixes
'safe-local-variable
637 (lambda (v) (and (stringp (car-safe v
)) (stringp (cdr v
)))))
639 (defun log-edit-add-field (field value
)
641 (if (save-excursion (re-search-backward (concat "^" field
":\\([ \t]*\\)$")
643 (replace-match (concat " " value
) t t nil
1)
644 (insert field
": " value
"\n" (if (looking-at "\n") "" "\n"))))
646 (defun log-edit-insert-changelog (&optional use-first
)
647 "Insert a log message by looking at the ChangeLog.
648 The idea is to write your ChangeLog entries first, and then use this
649 command to commit your changes.
651 To select default log text, we:
652 - find the ChangeLog entries for the files to be checked in,
653 - verify that the top entry in the ChangeLog is on the current date
654 and by the current user; if not, we don't provide any default text,
655 - search the ChangeLog entry for paragraphs containing the names of
656 the files we're checking in, and finally
657 - use those paragraphs as the log text.
659 If the optional prefix arg USE-FIRST is given (via \\[universal-argument]),
660 or if the command is repeated a second time in a row, use the first log entry
661 regardless of user name or time."
663 (let ((eoh (save-excursion (rfc822-goto-eoh) (point))))
664 (when (<= (point) eoh
)
666 (if (looking-at "\n") (forward-char 1))))
668 (let ((log-edit-changelog-use-first
669 (or use-first
(eq last-command
'log-edit-insert-changelog
))))
670 (log-edit-insert-changelog-entries (log-edit-files)))))
671 (log-edit-set-common-indentation)
672 ;; Add an Author: field if appropriate.
673 (when author
(log-edit-add-field "Author" author
))
674 ;; Add a Fixes: field if applicable.
675 (when (consp log-edit-rewrite-fixes
)
677 (when (re-search-forward (car log-edit-rewrite-fixes
) nil t
)
678 (let ((start (match-beginning 0))
680 (fixes (match-substitute-replacement
681 (cdr log-edit-rewrite-fixes
))))
682 (delete-region start end
)
683 (log-edit-add-field "Fixes" fixes
))))
684 (and log-edit-strip-single-file-name
685 (progn (rfc822-goto-eoh)
686 (if (looking-at "\n") (forward-char 1))
687 (looking-at "\\*\\s-+"))
688 (let ((start (point)))
690 (when (not (re-search-forward "^\\*\\s-+" nil t
))
692 (skip-chars-forward "^():")
693 (skip-chars-forward ": ")
694 (delete-region start
(point)))))
695 (goto-char (point-min))))
698 ;;;; functions for getting commit message from ChangeLog a file...
699 ;;;; Courtesy Jim Blandy
702 (defun log-edit-narrow-changelog ()
703 "Narrow to the top page of the current buffer, a ChangeLog file.
704 Actually, the narrowed region doesn't include the date line.
705 A \"page\" in a ChangeLog file is the area between two dates."
706 (or (eq major-mode
'change-log-mode
)
707 (error "log-edit-narrow-changelog: current buffer isn't a ChangeLog"))
709 (goto-char (point-min))
711 ;; Skip date line and subsequent blank lines.
713 (if (looking-at "[ \t\n]*\n")
714 (goto-char (match-end 0)))
716 (let ((start (point)))
718 (narrow-to-region start
(point))
719 (goto-char (point-min))))
721 (defun log-edit-changelog-paragraph ()
722 "Return the bounds of the ChangeLog paragraph containing point.
723 If we are between paragraphs, return the previous paragraph."
725 (if (looking-at "^[ \t]*$")
726 (skip-chars-backward " \t\n" (point-min)))
728 (if (re-search-backward "^[ \t]*\n" nil
'or-to-limit
)
729 (goto-char (match-end 0)))
731 (if (re-search-forward "^[ \t\n]*$" nil t
)
735 (defun log-edit-changelog-subparagraph ()
736 "Return the bounds of the ChangeLog subparagraph containing point.
737 A subparagraph is a block of non-blank lines beginning with an asterisk.
738 If we are between sub-paragraphs, return the previous subparagraph."
740 (if (search-backward "*" nil t
)
741 (list (progn (beginning-of-line) (point))
744 (if (re-search-forward "^[ \t]*[\n*]" nil t
)
747 (list (point) (point))))
749 (defun log-edit-changelog-entry ()
750 "Return the bounds of the ChangeLog entry containing point.
751 The variable `log-edit-changelog-full-paragraphs' decides whether an
752 \"entry\" is a paragraph or a subparagraph; see its documentation string
755 (if log-edit-changelog-full-paragraphs
756 (log-edit-changelog-paragraph)
757 (log-edit-changelog-subparagraph))))
759 (defvar user-full-name
)
760 (defvar user-mail-address
)
762 (defvar log-edit-author
) ;Dynamically scoped.
764 (defun log-edit-changelog-ours-p ()
765 "See if ChangeLog entry at point is for the current user, today.
766 Return non-nil if it is."
767 ;; Code adapted from add-change-log-entry.
768 (let ((name (or (and (boundp 'add-log-full-name
) add-log-full-name
)
769 (and (fboundp 'user-full-name
) (user-full-name))
770 (and (boundp 'user-full-name
) user-full-name
)))
771 (mail (or (and (boundp 'add-log-mailing-address
) add-log-mailing-address
)
772 ;;(and (fboundp 'user-mail-address) (user-mail-address))
773 (and (boundp 'user-mail-address
) user-mail-address
)))
774 (time (or (and (boundp 'add-log-time-format
)
775 (functionp add-log-time-format
)
776 (funcall add-log-time-format
))
777 (format-time-string "%Y-%m-%d"))))
778 (if (null log-edit-changelog-use-first
)
779 (looking-at (regexp-quote (format "%s %s <%s>" time name mail
)))
780 ;; Check the author, to potentially add it as a "Author: " header.
781 (when (looking-at "[^ \t]")
782 (when (and (boundp 'log-edit-author
)
783 (not (looking-at (format ".+ .+ <%s>"
784 (regexp-quote mail
))))
785 (looking-at ".+ \\(.+ <.+>\\)"))
786 (let ((author (replace-regexp-in-string " " " "
788 (unless (and log-edit-author
789 (string-match (regexp-quote author
) log-edit-author
))
790 (setq log-edit-author
792 (concat log-edit-author
", " author
)
796 (defun log-edit-changelog-entries (file)
797 "Return the ChangeLog entries for FILE, and the ChangeLog they came from.
798 The return value looks like this:
799 (LOGBUFFER (ENTRYSTART ENTRYEND) ...)
800 where LOGBUFFER is the name of the ChangeLog buffer, and each
801 \(ENTRYSTART . ENTRYEND\) pair is a buffer region."
802 (let ((changelog-file-name
803 (let ((default-directory
804 (file-name-directory (expand-file-name file
)))
805 (visiting-buffer (find-buffer-visiting file
)))
806 ;; If there is a buffer visiting FILE, and it has a local
807 ;; value for `change-log-default-name', use that.
808 (if (and visiting-buffer
809 (local-variable-p 'change-log-default-name
811 (with-current-buffer visiting-buffer
812 change-log-default-name
)
813 ;; `find-change-log' uses `change-log-default-name' if set
814 ;; and sets it before exiting, so we need to work around
815 ;; that memoizing which is undesired here.
816 (setq change-log-default-name nil
)
817 (find-change-log)))))
818 (with-current-buffer (find-file-noselect changelog-file-name
)
819 (unless (eq major-mode
'change-log-mode
) (change-log-mode))
820 (goto-char (point-min))
821 (if (looking-at "\\s-*\n") (goto-char (match-end 0)))
822 (if (not (log-edit-changelog-ours-p))
823 (list (current-buffer))
825 (log-edit-narrow-changelog)
826 (goto-char (point-min))
828 ;; Search for the name of FILE relative to the ChangeLog. If that
829 ;; doesn't occur anywhere, they're not using full relative
830 ;; filenames in the ChangeLog, so just look for FILE; we'll accept
831 ;; some false positives.
832 (let ((pattern (file-relative-name
833 file
(file-name-directory changelog-file-name
))))
834 (if (or (string= pattern
"")
836 (search-forward pattern nil t
))))
837 (setq pattern
(file-name-nondirectory file
)))
839 (setq pattern
(concat "\\(^\\|[^[:alnum:]]\\)"
840 (regexp-quote pattern
)
841 "\\($\\|[^[:alnum:]]\\)"))
845 (while (and (not (eobp)) (re-search-forward pattern nil t
))
846 (let ((entry (log-edit-changelog-entry)))
847 (if (< (elt entry
1) (max (1+ pos
) (point)))
848 ;; This is not relevant, actually.
851 ;; Make sure we make progress.
852 (setq pos
(max (1+ pos
) (elt entry
1)))
855 (cons (current-buffer) texts
))))))))
857 (defun log-edit-changelog-insert-entries (buffer beg end
&rest files
)
858 "Insert the text from BUFFER between BEG and END.
859 Rename relative filenames in the ChangeLog entry as FILES."
860 (let ((opoint (point))
861 (log-name (buffer-file-name buffer
))
862 (case-fold-search nil
)
864 (insert-buffer-substring buffer beg end
)
865 (setq bound
(point-marker))
870 (when (re-search-forward
871 (concat "\\(^\\|[ \t]\\)\\("
872 (file-relative-name f
(file-name-directory log-name
))
875 (replace-match f t t nil
2)))))
876 ;; Eliminate tabs at the beginning of the line.
879 (while (re-search-forward "^\\(\t+\\)" bound t
)
880 (replace-match "")))))
882 (defun log-edit-insert-changelog-entries (files)
883 "Given a list of files FILES, insert the ChangeLog entries for them."
884 (let ((log-entries nil
)
885 (log-edit-author nil
))
886 ;; Note that any ChangeLog entry can apply to more than one file.
887 ;; Here we construct a log-entries list with elements of the form
888 ;; ((LOGBUFFER ENTRYSTART ENTRYEND) FILE1 FILE2...)
890 (let* ((entries (log-edit-changelog-entries file
))
893 (dolist (region (cdr entries
))
894 (setq key
(cons buf region
))
895 (if (setq entry
(assoc key log-entries
))
896 (setcdr entry
(append (cdr entry
) (list file
)))
897 (push (list key file
) log-entries
)))))
898 ;; Now map over log-entries, and extract the strings.
899 (dolist (log-entry (nreverse log-entries
))
900 (apply 'log-edit-changelog-insert-entries
901 (append (car log-entry
) (cdr log-entry
)))
905 (defun log-edit-toggle-header (header value
)
906 "Toggle a boolean-type header in the current buffer.
907 If the value of HEADER is VALUE, clear it. Otherwise, add the
908 header if it's not present and set it to VALUE. Then make sure
909 there is an empty line after the headers. Return t if toggled
912 (line (concat header
": " value
"\n")))
916 (narrow-to-region (point-min) (point))
917 (goto-char (point-min))
918 (if (re-search-forward (concat "^" header
":"
919 log-edit-header-contents-regexp
)
921 (if (setq val
(not (string= (match-string 1) value
)))
922 (replace-match line t t
)
923 (replace-match "" t t nil
1))
926 (delete-horizontal-space)
927 (unless (looking-at "\n")
931 (defun log-edit-extract-headers (headers comment
)
932 "Extract headers from COMMENT to form command line arguments.
933 HEADERS should be an alist with elements (HEADER . CMDARG)
934 or (HEADER . FUNCTION) associating headers to command line
935 options and the result is then a list of the form (MSG ARGUMENTS...)
936 where MSG is the remaining text from COMMENT.
937 FUNCTION should be a function of one argument that takes the
938 header value and returns the list of strings to be appended to
939 ARGUMENTS. CMDARG will be added to ARGUMENTS followed by the
940 header value. If \"Summary\" is not in HEADERS, then the
941 \"Summary\" header is extracted anyway and put back as the first
946 (narrow-to-region (point-min) (point))
947 (let ((case-fold-search t
)
950 (dolist (header (if (assoc "Summary" headers
) headers
951 (cons '("Summary" . t
) headers
)))
952 (goto-char (point-min))
953 (while (re-search-forward (concat "^" (car header
)
954 ":" log-edit-header-contents-regexp
)
956 (let ((txt (match-string 1)))
957 (replace-match "" t t
)
958 (if (eq t
(cdr header
))
960 (if (functionp (cdr header
))
961 (setq res
(nconc res
(funcall (cdr header
) txt
)))
963 (push (or (cdr header
) (car header
)) res
))))))
964 ;; Remove header separator if the header is empty.
966 (goto-char (point-min))
967 (when (looking-at "\\([ \t]*\n\\)+")
968 (delete-region (match-beginning 0) (match-end 0)))
969 (if summary
(insert summary
"\n"))
970 (cons (buffer-string) res
))))
974 ;;; log-edit.el ends here