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/>.
27 ;; - Move in VC's code
28 ;; - Add compatibility for VC's hook variables
32 (require 'add-log
) ; for all the ChangeLog goodies
41 (defgroup log-edit nil
42 "Major mode for editing RCS and CVS commit messages."
44 :group
'vc
; It's used by VC.
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)."
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'."
77 :help
"Exit log-edit and proceed with the actual action."]
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"]
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."]
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."
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'."
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."
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."
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."
130 (defcustom log-edit-hook
'(log-edit-insert-message-template
131 log-edit-insert-cvs-template
132 log-edit-insert-changelog
134 "Hook run at the end of `log-edit'."
135 ;; Added log-edit-insert-message-template, moved log-edit-show-files.
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'."
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'."
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."
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'."
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
221 ((> stride
0) (1- stride
))
225 (defun log-edit-previous-comment (arg)
226 "Cycle backwards through comment history.
227 With a numeric prefix ARG, go back ARG comments."
229 (let ((len (ring-length log-edit-comment-ring
)))
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."
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
250 (list (read-string "Comment substring: " nil nil log-edit-last-comment-match
)))
251 (unless stride
(setq stride
1))
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."
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
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
)
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.
294 ;; Canonicalize the white space between the file name and comment.
296 ;; Indent rest of the text the same way add-log indented the first line.
297 (let ((indentation (current-indentation)))
299 (while (< (point) end
)
301 (indent-to indentation
))
303 ;; Fill the inserted text, preserving open-parens at bol.
304 (let ((paragraph-start (concat paragraph-start
"\\|\\s *\\s(")))
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")
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")
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)))
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
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)))
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)))
369 (skip-chars-forward "\n")
370 (and (< start
(line-end-position))
373 (not (re-search-backward "^Summary:[ \t]*[^ \t\n]" nil t
)))
376 (goto-char (match-end 0))
377 (put-text-property (point-min) (point)
378 'jit-lock-defer-multiline t
)
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
)
388 'log-edit-unknown-header
)
390 ;; From `log-edit-header-contents-regexp':
391 (3 (or (cdr (assoc-string (match-string 2) log-edit-headers-alist t
))
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
405 ;; * foo.el (bla, bli)
408 ;; * foo.el (bla, bli,
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
))
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
))
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
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
)))
472 (set (make-local-variable 'log-edit-callback
) callback
)
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))
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
))))
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."
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
547 (format "Buffer %s does not end in newline. Add one? "
550 (goto-char (point-max))
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
)))
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
)
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'."
574 (let ((buf (current-buffer)))
575 (quit-windows-on 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'."
585 (if (eq last-command
'log-edit-mode-help
)
586 (describe-function major-mode
)
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'."
596 (defun log-edit-set-common-indentation ()
597 "(Un)Indent the current buffer rigidly to `log-edit-common-indent'."
599 (let ((common (point-max)))
601 (while (< (point) (point-max))
602 (if (not (looking-at "^[ \t]*$"))
603 (setq common
(min common
(current-indentation))))
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."
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."
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
)
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'."
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))
647 (goto-char (point-min))
648 (while (and (looking-at "^\\([a-zA-Z]+: ?\\)?$")
649 (zerop (forward-line 1))))
652 (defun log-edit-insert-message-template ()
653 "Insert the default template with Summary and Author."
655 (when (or (called-interactively-p 'interactive
)
656 (log-edit-empty-buffer-p))
658 (when log-edit-setup-add-author
659 (insert "\nAuthor: "))
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."
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."
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."
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."
701 (unless (or (cdr-safe (log-edit-changelog-entries file
))
702 (equal (file-name-nondirectory file
) "ChangeLog"))
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."
712 (log-edit-remember-comment)
713 (dolist (f (log-edit-files))
714 (let ((buffer-file-name (expand-file-name f
)))
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
)
731 (if (save-excursion (re-search-backward (concat "^" field
":\\([ \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."
754 (let ((eoh (save-excursion (rfc822-goto-eoh) (point))))
755 (when (<= (point) eoh
)
757 (if (looking-at "\n") (forward-char 1))))
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
)
768 (when (re-search-forward (car log-edit-rewrite-fixes
) nil t
)
769 (let ((start (match-beginning 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)))
781 (when (not (re-search-forward "^\\*\\s-+" nil t
))
783 (skip-chars-forward "^():")
784 (skip-chars-forward ": ")
785 (delete-region start
(point))))))))
788 ;;;; functions for getting commit message from ChangeLog a file...
789 ;;;; Courtesy Jim Blandy
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.
803 (if (looking-at "[ \t\n]*\n")
804 (goto-char (match-end 0)))
806 (let ((start (point)))
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."
815 (if (looking-at "^[ \t]*$")
816 (skip-chars-backward " \t\n" (point-min)))
818 (if (re-search-backward "^[ \t]*\n" nil
'or-to-limit
)
819 (goto-char (match-end 0)))
821 (if (re-search-forward "^[ \t\n]*$" nil t
)
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."
830 (if (search-backward "*" nil t
)
831 (list (progn (beginning-of-line) (point))
834 (if (re-search-forward "^[ \t]*[\n*]" nil t
)
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
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 " " " "
878 (unless (and log-edit-author
879 (string-match (regexp-quote author
) log-edit-author
))
880 (setq log-edit-author
882 (concat log-edit-author
", " author
)
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
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 (with-current-buffer (find-file-noselect changelog-file-name
)
909 (unless (eq major-mode
'change-log-mode
) (change-log-mode))
910 (goto-char (point-min))
911 (if (looking-at "\\s-*\n") (goto-char (match-end 0)))
912 (if (not (log-edit-changelog-ours-p))
913 (list (current-buffer))
915 (log-edit-narrow-changelog)
916 (goto-char (point-min))
918 ;; Search for the name of FILE relative to the ChangeLog. If that
919 ;; doesn't occur anywhere, they're not using full relative
920 ;; filenames in the ChangeLog, so just look for FILE; we'll accept
921 ;; some false positives.
922 (let ((pattern (file-relative-name
923 file
(file-name-directory changelog-file-name
))))
924 (if (or (string= pattern
"")
926 (search-forward pattern nil t
))))
927 (setq pattern
(file-name-nondirectory file
)))
929 (setq pattern
(concat "\\(^\\|[^[:alnum:]]\\)"
930 (regexp-quote pattern
)
931 "\\($\\|[^[:alnum:]]\\)"))
935 (while (and (not (eobp)) (re-search-forward pattern nil t
))
936 (let ((entry (log-edit-changelog-entry)))
937 (if (< (elt entry
1) (max (1+ pos
) (point)))
938 ;; This is not relevant, actually.
941 ;; Make sure we make progress.
942 (setq pos
(max (1+ pos
) (elt entry
1)))
945 (cons (current-buffer) texts
))))))))
947 (defun log-edit-changelog-insert-entries (buffer beg end
&rest files
)
948 "Insert the text from BUFFER between BEG and END.
949 Rename relative filenames in the ChangeLog entry as FILES."
950 (let ((opoint (point))
951 (log-name (buffer-file-name buffer
))
952 (case-fold-search nil
)
954 (insert-buffer-substring buffer beg end
)
955 (setq bound
(point-marker))
960 (when (re-search-forward
961 (concat "\\(^\\|[ \t]\\)\\("
962 (file-relative-name f
(file-name-directory log-name
))
965 (replace-match f t t nil
2)))))
966 ;; Eliminate tabs at the beginning of the line.
969 (while (re-search-forward "^\\(\t+\\)" bound t
)
970 (replace-match "")))))
972 (defun log-edit-insert-changelog-entries (files)
973 "Given a list of files FILES, insert the ChangeLog entries for them."
974 (let ((log-entries nil
)
975 (log-edit-author nil
))
976 ;; Note that any ChangeLog entry can apply to more than one file.
977 ;; Here we construct a log-entries list with elements of the form
978 ;; ((LOGBUFFER ENTRYSTART ENTRYEND) FILE1 FILE2...)
980 (let* ((entries (log-edit-changelog-entries file
))
983 (dolist (region (cdr entries
))
984 (setq key
(cons buf region
))
985 (if (setq entry
(assoc key log-entries
))
986 (setcdr entry
(append (cdr entry
) (list file
)))
987 (push (list key file
) log-entries
)))))
988 ;; Now map over log-entries, and extract the strings.
989 (dolist (log-entry (nreverse log-entries
))
990 (apply 'log-edit-changelog-insert-entries
991 (append (car log-entry
) (cdr log-entry
)))
995 (defun log-edit-toggle-header (header value
)
996 "Toggle a boolean-type header in the current buffer.
997 See `log-edit-set-header' for details."
998 (log-edit-set-header header value t
))
1000 (defun log-edit-set-header (header value
&optional toggle
)
1001 "Set the value of HEADER to VALUE in the current buffer.
1002 If TOGGLE is non-nil, and the value of HEADER already is VALUE,
1003 clear it. Make sure there is an empty line after the headers.
1004 Return t if toggled on (or TOGGLE is nil), otherwise nil."
1006 (line (concat header
": " value
"\n")))
1010 (narrow-to-region (point-min) (point))
1011 (goto-char (point-min))
1012 (if (re-search-forward (concat "^" header
":"
1013 log-edit-header-contents-regexp
)
1015 (if (setq val
(not (and toggle
(string= (match-string 1) value
))))
1016 (replace-match line t t
)
1017 (replace-match "" t t nil
1))
1020 (delete-horizontal-space)
1021 (unless (looking-at "\n")
1025 (defun log-edit-extract-headers (headers comment
)
1026 "Extract headers from COMMENT to form command line arguments.
1027 HEADERS should be an alist with elements (HEADER . CMDARG)
1028 or (HEADER . FUNCTION) associating headers to command line
1029 options and the result is then a list of the form (MSG ARGUMENTS...)
1030 where MSG is the remaining text from COMMENT.
1031 FUNCTION should be a function of one argument that takes the
1032 header value and returns the list of strings to be appended to
1033 ARGUMENTS. CMDARG will be added to ARGUMENTS followed by the
1034 header value. If \"Summary\" is not in HEADERS, then the
1035 \"Summary\" header is extracted anyway and put back as the first
1040 (narrow-to-region (point-min) (point))
1041 (let ((case-fold-search t
)
1044 (dolist (header (if (assoc "Summary" headers
) headers
1045 (cons '("Summary" . t
) headers
)))
1046 (goto-char (point-min))
1047 (while (re-search-forward (concat "^" (car header
)
1048 ":" log-edit-header-contents-regexp
)
1050 (let ((txt (match-string 1)))
1051 (replace-match "" t t
)
1052 (if (eq t
(cdr header
))
1054 (if (functionp (cdr header
))
1055 (setq res
(nconc res
(funcall (cdr header
) txt
)))
1057 (push (or (cdr header
) (car header
)) res
))))))
1058 ;; Remove header separator if the header is empty.
1060 (goto-char (point-min))
1061 (when (looking-at "\\([ \t]*\n\\)+")
1062 (delete-region (match-beginning 0) (match-end 0)))
1063 (if summary
(insert summary
"\n\n"))
1064 (cons (buffer-string) res
))))
1068 ;;; log-edit.el ends here