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