1 ;;; log-edit.el --- Major mode for editing CVS commit messages -*- lexical-binding: t -*-
3 ;; Copyright (C) 1999-2016 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-tiny-change t
721 "Non-nil means rewrite (tiny change).")
723 (defvar log-edit-rewrite-fixes nil
724 "Rule to rewrite bug numbers into Fixes: headers.
725 The value should be of the form (REGEXP . REPLACEMENT)
726 where REGEXP should match the expression referring to a bug number
727 in the text, and REPLACEMENT is an expression to pass to `replace-match'
728 to build the Fixes: header.")
729 (put 'log-edit-rewrite-fixes
'safe-local-variable
730 (lambda (v) (and (stringp (car-safe v
)) (stringp (cdr v
)))))
732 (defun log-edit-add-field (field value
)
734 (if (save-excursion (re-search-backward (concat "^" field
":\\([ \t]*\\)$")
736 (replace-match (concat " " value
) t t nil
1)
737 (insert field
": " value
"\n" (if (looking-at "\n") "" "\n"))))
739 (defun log-edit-insert-changelog (&optional use-first
)
740 "Insert a log message by looking at the ChangeLog.
741 The idea is to write your ChangeLog entries first, and then use this
742 command to commit your changes.
744 To select default log text, we:
745 - find the ChangeLog entries for the files to be checked in,
746 - verify that the top entry in the ChangeLog is on the current date
747 and by the current user; if not, we don't provide any default text,
748 - search the ChangeLog entry for paragraphs containing the names of
749 the files we're checking in, and finally
750 - use those paragraphs as the log text.
752 If the optional prefix arg USE-FIRST is given (via \\[universal-argument]),
753 or if the command is repeated a second time in a row, use the first log entry
754 regardless of user name or time."
757 (let ((eoh (save-excursion (rfc822-goto-eoh) (point))))
758 (when (<= (point) eoh
)
760 (if (looking-at "\n") (forward-char 1))))
762 (let ((log-edit-changelog-use-first
763 (or use-first
(eq last-command
'log-edit-insert-changelog
))))
764 (log-edit-insert-changelog-entries (log-edit-files)))))
765 (log-edit-set-common-indentation)
766 ;; Add an Author: field if appropriate.
767 (when author
(log-edit-add-field "Author" (car author
)))
768 ;; Add a Fixes: field if applicable.
769 (when (consp log-edit-rewrite-fixes
)
771 (when (re-search-forward (car log-edit-rewrite-fixes
) nil t
)
772 (let ((start (match-beginning 0))
774 (fixes (match-substitute-replacement
775 (cdr log-edit-rewrite-fixes
))))
776 (delete-region start end
)
777 (log-edit-add-field "Fixes" fixes
))))
778 (and log-edit-strip-single-file-name
779 (progn (rfc822-goto-eoh)
780 (if (looking-at "\n") (forward-char 1))
781 (looking-at "\\*\\s-+"))
782 (let ((start (point)))
784 (when (not (re-search-forward "^\\*\\s-+" nil t
))
786 (skip-chars-forward "^():")
787 (skip-chars-forward ": ")
788 (delete-region start
(point)))))
789 ;; FIXME also add "Co-authored-by" when appropriate.
790 ;; Bzr accepts multiple --author arguments, others (?) don't.
791 (and log-edit-rewrite-tiny-change
792 (eq 'tiny
(cdr author
))
793 (goto-char (point-max))
794 (insert "\nCopyright-paperwork-exempt: yes\n")))))
797 ;;;; functions for getting commit message from ChangeLog a file...
798 ;;;; Courtesy Jim Blandy
801 (defun log-edit-narrow-changelog ()
802 "Narrow to the top page of the current buffer, a ChangeLog file.
803 Actually, the narrowed region doesn't include the date line.
804 A \"page\" in a ChangeLog file is the area between two dates."
805 (or (eq major-mode
'change-log-mode
)
806 (error "log-edit-narrow-changelog: current buffer isn't a ChangeLog"))
808 (goto-char (point-min))
810 ;; Skip date line and subsequent blank lines.
812 (if (looking-at "[ \t\n]*\n")
813 (goto-char (match-end 0)))
815 (let ((start (point)))
817 (narrow-to-region start
(point))
818 (goto-char (point-min))))
820 (defun log-edit-changelog-paragraph ()
821 "Return the bounds of the ChangeLog paragraph containing point.
822 If we are between paragraphs, return the previous paragraph."
824 (if (looking-at "^[ \t]*$")
825 (skip-chars-backward " \t\n" (point-min)))
827 (if (re-search-backward "^[ \t]*\n" nil
'or-to-limit
)
828 (goto-char (match-end 0)))
830 (if (re-search-forward "^[ \t\n]*$" nil t
)
834 (defun log-edit-changelog-subparagraph ()
835 "Return the bounds of the ChangeLog subparagraph containing point.
836 A subparagraph is a block of non-blank lines beginning with an asterisk.
837 If we are between sub-paragraphs, return the previous subparagraph."
839 (if (search-backward "*" nil t
)
840 (list (progn (beginning-of-line) (point))
843 (if (re-search-forward "^[ \t]*[\n*]" nil t
)
846 (list (point) (point))))
848 (defun log-edit-changelog-entry ()
849 "Return the bounds of the ChangeLog entry containing point.
850 The variable `log-edit-changelog-full-paragraphs' decides whether an
851 \"entry\" is a paragraph or a subparagraph; see its documentation string
854 (if log-edit-changelog-full-paragraphs
855 (log-edit-changelog-paragraph)
856 (log-edit-changelog-subparagraph))))
858 (defvar user-full-name
)
859 (defvar user-mail-address
)
861 (defvar log-edit-author
) ;Dynamically scoped.
863 (defun log-edit-changelog-ours-p ()
864 "See if ChangeLog entry at point is for the current user, today.
865 Return non-nil if it is."
866 ;; Code adapted from add-change-log-entry.
867 (let ((name (or (and (boundp 'add-log-full-name
) add-log-full-name
)
868 (and (fboundp 'user-full-name
) (user-full-name))
869 (and (boundp 'user-full-name
) user-full-name
)))
870 (mail (or (and (boundp 'add-log-mailing-address
) add-log-mailing-address
)
871 ;;(and (fboundp 'user-mail-address) (user-mail-address))
872 (and (boundp 'user-mail-address
) user-mail-address
)))
873 (time (or (and (boundp 'add-log-time-format
)
874 (functionp add-log-time-format
)
875 (funcall add-log-time-format
876 nil add-log-time-zone-rule
))
877 (format-time-string "%Y-%m-%d"))))
878 (if (null log-edit-changelog-use-first
)
879 (looking-at (regexp-quote (format "%s %s <%s>" time name mail
)))
880 ;; Check the author, to potentially add it as a "Author: " header.
881 ;; FIXME This accumulates multiple authors, but only when there
882 ;; are multiple ChangeLog files. It should also check for
883 ;; multiple authors in each individual entry.
884 (when (looking-at "[^ \t]")
885 (when (and (boundp 'log-edit-author
)
886 (not (looking-at (format ".+ .+ <%s>"
887 (regexp-quote mail
))))
888 (looking-at ".+ \\(.+ <.+>\\) *\\((tiny change)\\)?"))
889 (let ((author (replace-regexp-in-string " " " "
891 (unless (and log-edit-author
892 (string-match (regexp-quote author
)
893 (car log-edit-author
)))
894 (if (not log-edit-author
)
895 (setq log-edit-author
896 (cons author
(if (match-string 2) 'tiny
)))
897 (setcar log-edit-author
898 (concat (car log-edit-author
) ", " author
))
899 (and (match-string 2) (not (cdr log-edit-author
))
900 (setcdr log-edit-author
'tiny
))))))
903 (defun log-edit-changelog-entries (file)
904 "Return the ChangeLog entries for FILE, and the ChangeLog they came from.
905 The return value looks like this:
906 (LOGBUFFER (ENTRYSTART ENTRYEND) ...)
907 where LOGBUFFER is the name of the ChangeLog buffer, and each
908 \(ENTRYSTART . ENTRYEND) pair is a buffer region."
909 (let ((changelog-file-name
910 (let ((default-directory
911 (file-name-directory (expand-file-name file
)))
912 (visiting-buffer (find-buffer-visiting file
)))
913 ;; If there is a buffer visiting FILE, and it has a local
914 ;; value for `change-log-default-name', use that.
915 (if (and visiting-buffer
916 (local-variable-p 'change-log-default-name
918 (with-current-buffer visiting-buffer
919 change-log-default-name
)
920 ;; `find-change-log' uses `change-log-default-name' if set
921 ;; and sets it before exiting, so we need to work around
922 ;; that memoizing which is undesired here.
923 (setq change-log-default-name nil
)
924 (find-change-log)))))
925 (when (or (find-buffer-visiting changelog-file-name
)
926 (file-exists-p changelog-file-name
))
927 (with-current-buffer (find-file-noselect changelog-file-name
)
928 (unless (eq major-mode
'change-log-mode
) (change-log-mode))
929 (goto-char (point-min))
930 (if (looking-at "\\s-*\n") (goto-char (match-end 0)))
931 (if (not (log-edit-changelog-ours-p))
932 (list (current-buffer))
934 (log-edit-narrow-changelog)
935 (goto-char (point-min))
937 (let ((pattern (log-edit-changelog--pattern file
938 changelog-file-name
)))
941 (while (and (not (eobp)) (re-search-forward pattern nil t
))
942 (let ((entry (log-edit-changelog-entry)))
943 (if (< (elt entry
1) (max (1+ pos
) (point)))
944 ;; This is not relevant, actually.
947 ;; Make sure we make progress.
948 (setq pos
(max (1+ pos
) (elt entry
1)))
951 (cons (current-buffer) texts
)))))))))
953 (defun log-edit-changelog--pattern (file changelog-file-name
)
954 (if (eq (aref file
(1- (length file
))) ?
/)
955 ;; Match any files inside this directory.
956 (concat "^\t\\* " (unless (string= file
"./") file
))
957 ;; Search for the name of FILE relative to the ChangeLog. If that
958 ;; doesn't occur anywhere, they're not using full relative
959 ;; filenames in the ChangeLog, so just look for FILE; we'll accept
960 ;; some false positives.
961 (let ((pattern (file-relative-name
962 file
(file-name-directory changelog-file-name
))))
963 ;; FIXME: When can the above return an empty string?
964 (if (or (string= pattern
"")
966 (search-forward pattern nil t
))))
967 (setq pattern
(file-name-nondirectory file
)))
968 (setq pattern
(concat "\\(^\\|[^[:alnum:]]\\)"
969 (regexp-quote pattern
)
970 "\\($\\|[^[:alnum:]]\\)")))))
972 (defun log-edit-changelog-insert-entries (buffer beg end
&rest files
)
973 "Insert the text from BUFFER between BEG and END.
974 Rename relative filenames in the ChangeLog entry as FILES."
975 (let ((opoint (point))
976 (log-name (buffer-file-name buffer
))
977 (case-fold-search nil
)
979 (insert-buffer-substring buffer beg end
)
980 (setq bound
(point-marker))
983 ;; FIXME: f can be a directory, a (possibly indirect) parent
984 ;; of the ChangeLog file.
987 (when (re-search-forward
988 (concat "\\(^\\|[ \t]\\)\\("
989 (file-relative-name f
(file-name-directory log-name
))
992 (replace-match f t t nil
2)))))
993 ;; Eliminate tabs at the beginning of the line.
996 (while (re-search-forward "^\\(\t+\\)" bound t
)
997 (replace-match "")))))
999 (defun log-edit-insert-changelog-entries (files)
1000 "Given a list of files FILES, insert the ChangeLog entries for them."
1001 (let ((log-entries nil
)
1002 (log-edit-author nil
))
1003 ;; Note that any ChangeLog entry can apply to more than one file.
1004 ;; Here we construct a log-entries list with elements of the form
1005 ;; ((LOGBUFFER ENTRYSTART ENTRYEND) FILE1 FILE2...)
1006 (dolist (file files
)
1007 (let* ((entries (log-edit-changelog-entries file
))
1010 (dolist (region (cdr entries
))
1011 (setq key
(cons buf region
))
1012 (if (setq entry
(assoc key log-entries
))
1013 (setcdr entry
(append (cdr entry
) (list file
)))
1014 (push (list key file
) log-entries
)))))
1015 ;; Now map over log-entries, and extract the strings.
1016 (dolist (log-entry (nreverse log-entries
))
1017 (apply 'log-edit-changelog-insert-entries
1018 (append (car log-entry
) (cdr log-entry
)))
1020 ;; No newline after the last entry.
1025 (defun log-edit-toggle-header (header value
)
1026 "Toggle a boolean-type header in the current buffer.
1027 See `log-edit-set-header' for details."
1028 (log-edit-set-header header value t
))
1030 (defun log-edit-set-header (header value
&optional toggle
)
1031 "Set the value of HEADER to VALUE in the current buffer.
1032 If TOGGLE is non-nil, and the value of HEADER already is VALUE,
1033 clear it. Make sure there is an empty line after the headers.
1034 Return t if toggled on (or TOGGLE is nil), otherwise nil."
1036 (line (concat header
": " value
"\n")))
1040 (narrow-to-region (point-min) (point))
1041 (goto-char (point-min))
1042 (if (re-search-forward (concat "^" header
":"
1043 log-edit-header-contents-regexp
)
1045 (if (setq val
(not (and toggle
(string= (match-string 1) value
))))
1046 (replace-match line t t
)
1047 (replace-match "" t t nil
1))
1050 (delete-horizontal-space)
1051 (unless (looking-at "\n")
1055 (defun log-edit-extract-headers (headers comment
)
1056 "Extract headers from COMMENT to form command line arguments.
1057 HEADERS should be an alist with elements (HEADER . CMDARG)
1058 or (HEADER . FUNCTION) associating headers to command line
1059 options and the result is then a list of the form (MSG ARGUMENTS...)
1060 where MSG is the remaining text from COMMENT.
1061 FUNCTION should be a function of one argument that takes the
1062 header value and returns the list of strings to be appended to
1063 ARGUMENTS. CMDARG will be added to ARGUMENTS followed by the
1064 header value. If \"Summary\" is not in HEADERS, then the
1065 \"Summary\" header is extracted anyway and put back as the first
1070 (narrow-to-region (point-min) (point))
1071 (let ((case-fold-search t
)
1074 (dolist (header (if (assoc "Summary" headers
) headers
1075 (cons '("Summary" . t
) headers
)))
1076 (goto-char (point-min))
1077 (while (re-search-forward (concat "^" (car header
)
1078 ":" log-edit-header-contents-regexp
)
1080 (let ((txt (match-string 1)))
1081 (replace-match "" t t
)
1082 (if (eq t
(cdr header
))
1084 (if (functionp (cdr header
))
1085 (setq res
(nconc res
(funcall (cdr header
) txt
)))
1087 (push (or (cdr header
) (car header
)) res
))))))
1088 ;; Remove header separator if the header is empty.
1090 (goto-char (point-min))
1091 (when (looking-at "\\([ \t]*\n\\)+")
1092 (delete-region (match-beginning 0) (match-end 0)))
1093 (if summary
(insert summary
"\n\n"))
1094 (cons (buffer-string) res
))))
1098 ;;; log-edit.el ends here