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