Don't call turn_on_atimers around `connect' (Bug#5723).
[bpt/emacs.git] / lisp / log-edit.el
CommitLineData
5b467bf4
SM
1;;; log-edit.el --- Major mode for editing CVS commit messages
2
c6265c10
GM
3;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
4;; 2008, 2009, 2010 Free Software Foundation, Inc.
5b467bf4 5
cc1eecfd 6;; Author: Stefan Monnier <monnier@iro.umontreal.ca>
5b467bf4 7;; Keywords: pcl-cvs cvs commit log
5b467bf4
SM
8
9;; This file is part of GNU Emacs.
10
eb3fa2cf 11;; GNU Emacs is free software: you can redistribute it and/or modify
5b467bf4 12;; it under the terms of the GNU General Public License as published by
eb3fa2cf
GM
13;; the Free Software Foundation, either version 3 of the License, or
14;; (at your option) any later version.
5b467bf4
SM
15
16;; GNU Emacs is distributed in the hope that it will be useful,
17;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19;; GNU General Public License for more details.
20
21;; You should have received a copy of the GNU General Public License
eb3fa2cf 22;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
5b467bf4
SM
23
24;;; Commentary:
25
26;; Todo:
27
5b467bf4
SM
28;; - Move in VC's code
29;; - Add compatibility for VC's hook variables
5b467bf4
SM
30
31;;; Code:
32
33(eval-when-compile (require 'cl))
34(require 'add-log) ; for all the ChangeLog goodies
35(require 'pcvs-util)
36(require 'ring)
5b467bf4 37
f1180544 38;;;;
5b467bf4 39;;;; Global Variables
f1180544 40;;;;
5b467bf4
SM
41
42(defgroup log-edit nil
bc35d341 43 "Major mode for editing RCS and CVS commit messages."
5b467bf4 44 :group 'pcl-cvs
bc35d341
DL
45 :group 'vc ; It's used by VC.
46 :version "21.1"
5b467bf4
SM
47 :prefix "log-edit-")
48
49;; compiler pacifiers
50(defvar cvs-buffer)
51
12dd83de
SM
52\f
53;; The main keymap
54
5b467bf4
SM
55(easy-mmode-defmap log-edit-mode-map
56 `(("\C-c\C-c" . log-edit-done)
57 ("\C-c\C-a" . log-edit-insert-changelog)
93a142e1 58 ("\C-c\C-d" . log-edit-show-diff)
5b467bf4 59 ("\C-c\C-f" . log-edit-show-files)
9fe89a26
SM
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)
0916e956 64 ("\C-c?" . log-edit-mode-help))
cdf54749 65 "Keymap for the `log-edit-mode' (to edit version control log messages)."
0916e956
SM
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)
5b467bf4 71
d247e32d
SM
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 "--"
799224fe
DN
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"]
d247e32d 82 "--"
93a142e1
DN
83 ["Show diff" log-edit-show-diff
84 :help "Show the diff for the files to be committed."]
d247e32d
SM
85 ["List files" log-edit-show-files
86 :help "Show the list of relevant files."]
87 "--"
799224fe
DN
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"]))
d247e32d 96
f077c462 97(defcustom log-edit-confirm 'changed
9201cc28 98 "If non-nil, `log-edit-done' will request confirmation.
5b467bf4
SM
99If '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
9201cc28 105 "If non-nil, don't hide the buffer after `log-edit-done'."
5b467bf4
SM
106 :group 'log-edit
107 :type 'boolean)
108
a664185b
JB
109(defvar cvs-commit-buffer-require-final-newline t)
110(make-obsolete-variable 'cvs-commit-buffer-require-final-newline
b8ce3df9
JB
111 'log-edit-require-final-newline
112 "21.1")
5b467bf4
SM
113
114(defcustom log-edit-require-final-newline
115 cvs-commit-buffer-require-final-newline
9201cc28 116 "Enforce a newline at the end of commit log messages.
5b467bf4
SM
117Enforce it silently if t, query if non-nil and don't do anything if nil."
118 :group 'log-edit
119 :type '(choice (const ask) (const t) (const nil)))
120
121(defcustom log-edit-setup-invert nil
9201cc28 122 "Non-nil means `log-edit' should invert the meaning of its SETUP arg.
5b467bf4
SM
123If SETUP is 'force, this variable has no effect."
124 :group 'log-edit
125 :type 'boolean)
126
127(defcustom log-edit-hook '(log-edit-insert-cvs-template
128 log-edit-insert-changelog)
9201cc28 129 "Hook run at the end of `log-edit'."
5b467bf4 130 :group 'log-edit
f7eeab0d
SM
131 :type '(hook :options (log-edit-insert-changelog
132 log-edit-insert-cvs-rcstemplate
133 log-edit-insert-cvs-template
134 log-edit-insert-filenames)))
5b467bf4 135
3831af62 136(defcustom log-edit-mode-hook (if (boundp 'vc-log-mode-hook) vc-log-mode-hook)
9201cc28 137 "Hook run when entering `log-edit-mode'."
5b467bf4
SM
138 :group 'log-edit
139 :type 'hook)
140
141(defcustom log-edit-done-hook nil
9201cc28 142 "Hook run before doing the actual commit.
5b467bf4
SM
143This hook can be used to cleanup the message, enforce various
144conventions, or to allow recording the message in some other database,
145such as a bug-tracking system. The list of files about to be committed
146can be obtained from `log-edit-files'."
147 :group 'log-edit
43a9a0c4 148 :type '(hook :options (log-edit-set-common-indentation
5b467bf4
SM
149 log-edit-add-to-changelog)))
150
31764e15
SM
151(defcustom log-edit-strip-single-file-name t
152 "If non-nil, remove file name from single-file log entries."
c6265c10
GM
153 :type 'boolean
154 :safe 'booleanp
155 :group 'log-edit
156 :version "23.2")
31764e15 157
a664185b
JB
158(defvar cvs-changelog-full-paragraphs t)
159(make-obsolete-variable 'cvs-changelog-full-paragraphs
b8ce3df9
JB
160 'log-edit-changelog-full-paragraphs
161 "21.1")
14188021
SM
162
163(defvar log-edit-changelog-full-paragraphs cvs-changelog-full-paragraphs
164 "*If non-nil, include full ChangeLog paragraphs in the log.
5b467bf4
SM
165This may be set in the ``local variables'' section of a ChangeLog, to
166indicate the policy for that ChangeLog.
167
168A ChangeLog paragraph is a bunch of log text containing no blank lines;
169a paragraph usually describes a set of changes with a single purpose,
170but perhaps spanning several functions in several files. Changes in
171different paragraphs are unrelated.
172
14188021 173You could argue that the log entry for a file should contain the
5b467bf4
SM
174full ChangeLog paragraph mentioning the change to the file, even though
175it may mention other files, because that gives you the full context you
5ab405e4 176need to understand the change. This is the behavior you get when this
5b467bf4
SM
177variable is set to t.
178
14188021 179On the other hand, you could argue that the log entry for a change
5b467bf4 180should contain only the text for the changes which occurred in that
5ab405e4 181file, because the log is per-file. This is the behavior you get
5b467bf4
SM
182when this variable is set to nil.")
183
184;;;; Internal global or buffer-local vars
185
186(defconst log-edit-files-buf "*log-edit-files*")
187(defvar log-edit-initial-files nil)
188(defvar log-edit-callback nil)
93a142e1 189(defvar log-edit-diff-function nil)
5b467bf4 190(defvar log-edit-listfun nil)
09158997
DN
191(defvar log-edit-extra-flags nil
192 "List of extra flags to pass to the check in command.")
193(defvar log-edit-before-checkin-process nil
495b517c 194 "Alist with instructions for processing the commit message before check in.
09158997 195The format is: (REGEXP . INSTRUCTIONS).
495b517c 196All lines matching REGEXP are removed. For example:
09158997 197
495b517c 198\(\"^#.*\" . nil)
09158997 199
09158997
DN
200means: just remove all lines starting with #. This can be used
201to insert lines in the commit buffer that contain, for example, the
202list of files to be committed.
203
495b517c
GM
204\(\"Author: \\\\(.*\\\\)\" . (list \"--author\" (match-string 1)))
205
206means: append (list \"--author\" (match-string 1)) to `log-edit-extra-flags'.")
207
cdf54749 208(defvar log-edit-parent-buffer nil)
5b467bf4 209
9fe89a26 210;;; Originally taken from VC-Log mode
12dd83de 211
9fe89a26 212(defconst log-edit-maximum-comment-ring-size 32
12dd83de 213 "Maximum number of saved comments in the comment ring.")
9fe89a26
SM
214(defvar log-edit-comment-ring (make-ring log-edit-maximum-comment-ring-size))
215(defvar log-edit-comment-ring-index nil)
216(defvar log-edit-last-comment-match "")
12dd83de 217
9fe89a26 218(defun log-edit-new-comment-index (stride len)
12dd83de 219 "Return the comment index STRIDE elements from the current one.
9fe89a26 220LEN is the length of `log-edit-comment-ring'."
12dd83de 221 (mod (cond
9fe89a26 222 (log-edit-comment-ring-index (+ log-edit-comment-ring-index stride))
12dd83de
SM
223 ;; Initialize the index on the first use of this command
224 ;; so that the first M-p gets index 0, and the first M-n gets
225 ;; index -1.
226 ((> stride 0) (1- stride))
227 (t stride))
228 len))
229
9fe89a26 230(defun log-edit-previous-comment (arg)
12dd83de
SM
231 "Cycle backwards through comment history.
232With a numeric prefix ARG, go back ARG comments."
233 (interactive "*p")
9fe89a26 234 (let ((len (ring-length log-edit-comment-ring)))
12dd83de
SM
235 (if (<= len 0)
236 (progn (message "Empty comment ring") (ding))
0916e956
SM
237 ;; Don't use `erase-buffer' because we don't want to `widen'.
238 (delete-region (point-min) (point-max))
9fe89a26
SM
239 (setq log-edit-comment-ring-index (log-edit-new-comment-index arg len))
240 (message "Comment %d" (1+ log-edit-comment-ring-index))
241 (insert (ring-ref log-edit-comment-ring log-edit-comment-ring-index)))))
12dd83de 242
9fe89a26 243(defun log-edit-next-comment (arg)
12dd83de
SM
244 "Cycle forwards through comment history.
245With a numeric prefix ARG, go forward ARG comments."
246 (interactive "*p")
9fe89a26 247 (log-edit-previous-comment (- arg)))
12dd83de 248
9fe89a26 249(defun log-edit-comment-search-backward (str &optional stride)
12dd83de
SM
250 "Search backwards through comment history for substring match of STR.
251If the optional argument STRIDE is present, that is a step-width to use
252when going through the comment ring."
253 ;; Why substring rather than regexp ? -sm
254 (interactive
9fe89a26 255 (list (read-string "Comment substring: " nil nil log-edit-last-comment-match)))
12dd83de
SM
256 (unless stride (setq stride 1))
257 (if (string= str "")
9fe89a26
SM
258 (setq str log-edit-last-comment-match)
259 (setq log-edit-last-comment-match str))
12dd83de 260 (let* ((str (regexp-quote str))
9fe89a26
SM
261 (len (ring-length log-edit-comment-ring))
262 (n (log-edit-new-comment-index stride len)))
12dd83de 263 (while (progn (when (or (>= n len) (< n 0)) (error "Not found"))
9fe89a26 264 (not (string-match str (ring-ref log-edit-comment-ring n))))
12dd83de 265 (setq n (+ n stride)))
9fe89a26
SM
266 (setq log-edit-comment-ring-index n)
267 (log-edit-previous-comment 0)))
12dd83de 268
9fe89a26 269(defun log-edit-comment-search-forward (str)
12dd83de
SM
270 "Search forwards through comment history for a substring match of STR."
271 (interactive
9fe89a26
SM
272 (list (read-string "Comment substring: " nil nil log-edit-last-comment-match)))
273 (log-edit-comment-search-backward str -1))
12dd83de 274
9fe89a26 275(defun log-edit-comment-to-change-log (&optional whoami file-name)
12dd83de
SM
276 "Enter last VC comment into the change log for the current file.
277WHOAMI (interactive prefix) non-nil means prompt for user name
278and site. FILE-NAME is the name of the change log; if nil, use
279`change-log-default-name'.
280
9fe89a26 281This may be useful as a `log-edit-checkin-hook' to update change logs
12dd83de
SM
282automatically."
283 (interactive (if current-prefix-arg
284 (list current-prefix-arg
285 (prompt-for-change-log-name))))
12dd83de 286 (let (;; Extract the comment first so we get any error before doing anything.
9fe89a26 287 (comment (ring-ref log-edit-comment-ring 0))
12dd83de
SM
288 ;; Don't let add-change-log-entry insert a defun name.
289 (add-log-current-defun-function 'ignore)
290 end)
291 ;; Call add-log to do half the work.
292 (add-change-log-entry whoami file-name t t)
293 ;; Insert the VC comment, leaving point before it.
294 (setq end (save-excursion (insert comment) (point-marker)))
295 (if (looking-at "\\s *\\s(")
296 ;; It starts with an open-paren, as in "(foo): Frobbed."
297 ;; So remove the ": " add-log inserted.
298 (delete-char -2))
299 ;; Canonicalize the white space between the file name and comment.
300 (just-one-space)
301 ;; Indent rest of the text the same way add-log indented the first line.
302 (let ((indentation (current-indentation)))
303 (save-excursion
304 (while (< (point) end)
305 (forward-line 1)
306 (indent-to indentation))
307 (setq end (point))))
308 ;; Fill the inserted text, preserving open-parens at bol.
0916e956 309 (let ((paragraph-start (concat paragraph-start "\\|\\s *\\s(")))
12dd83de
SM
310 (beginning-of-line)
311 (fill-region (point) end))
312 ;; Canonicalize the white space at the end of the entry so it is
313 ;; separated from the next entry by a single blank line.
314 (skip-syntax-forward " " end)
315 (delete-char (- (skip-syntax-backward " ")))
316 (or (eobp) (looking-at "\n\n")
317 (insert "\n"))))
318
9fe89a26 319;; Compatibility with old names.
f7eeab0d
SM
320(define-obsolete-variable-alias 'vc-comment-ring 'log-edit-comment-ring "22.1")
321(define-obsolete-variable-alias 'vc-comment-ring-index 'log-edit-comment-ring-index "22.1")
322(define-obsolete-function-alias 'vc-previous-comment 'log-edit-previous-comment "22.1")
323(define-obsolete-function-alias 'vc-next-comment 'log-edit-next-comment "22.1")
324(define-obsolete-function-alias 'vc-comment-search-reverse 'log-edit-comment-search-backward "22.1")
325(define-obsolete-function-alias 'vc-comment-search-forward 'log-edit-comment-search-forward "22.1")
326(define-obsolete-function-alias 'vc-comment-to-change-log 'log-edit-comment-to-change-log "22.1")
9fe89a26 327
cdf54749
SM
328;;;
329;;; Actual code
330;;;
5b467bf4 331
63fbe552 332(defvar log-edit-font-lock-keywords
165a7fbe
SM
333 '(("\\`\\(Summary:\\)\\(.*\\)"
334 (1 font-lock-keyword-face)
335 (2 font-lock-function-name-face))))
336
5b467bf4 337;;;###autoload
09158997 338(defun log-edit (callback &optional setup params buffer mode &rest ignore)
5b467bf4 339 "Setup a buffer to enter a log message.
09158997
DN
340\\<log-edit-mode-map>The buffer will be put in mode MODE or `log-edit-mode'
341if MODE is nil.
5b467bf4 342If SETUP is non-nil, the buffer is then erased and `log-edit-hook' is run.
2507310c
TTN
343Mark and point will be set around the entire contents of the buffer so
344that it is easy to kill the contents of the buffer with \\[kill-region].
5b467bf4 345Once you're done editing the message, pressing \\[log-edit-done] will call
cdf54749 346`log-edit-done' which will end up calling CALLBACK to do the actual commit.
2507310c
TTN
347
348PARAMS if non-nil is an alist. Possible keys and associated values:
349 `log-edit-listfun' -- function taking no arguments that returns the list of
350 files that are concerned by the current operation (using relative names);
351 `log-edit-diff-function' -- function taking no arguments that
352 displays a diff of the files concerned by the current operation.
353
cdf54749 354If BUFFER is non-nil `log-edit' will jump to that buffer, use it to edit the
2507310c
TTN
355log message and go back to the current buffer when done. Otherwise, it
356uses the current buffer."
cdf54749
SM
357 (let ((parent (current-buffer)))
358 (if buffer (pop-to-buffer buffer))
359 (when (and log-edit-setup-invert (not (eq setup 'force)))
360 (setq setup (not setup)))
361 (when setup (erase-buffer))
09158997
DN
362 (if mode
363 (funcall mode)
364 (log-edit-mode))
cdf54749 365 (set (make-local-variable 'log-edit-callback) callback)
93a142e1
DN
366 (if (listp params)
367 (dolist (crt params)
368 (set (make-local-variable (car crt)) (cdr crt)))
369 ;; For backward compatibility with log-edit up to version 22.2
370 ;; accept non-list PARAMS to mean `log-edit-list'.
371 (set (make-local-variable 'log-edit-listfun) params))
372
cdf54749 373 (if buffer (set (make-local-variable 'log-edit-parent-buffer) parent))
70c2a484 374 (set (make-local-variable 'log-edit-initial-files) (log-edit-files))
cdf54749
SM
375 (when setup (run-hooks 'log-edit-hook))
376 (goto-char (point-min)) (push-mark (point-max))
8a26c165 377 (message "%s" (substitute-command-keys
cdf54749 378 "Press \\[log-edit-done] when you are done editing."))))
5b467bf4
SM
379
380(define-derived-mode log-edit-mode text-mode "Log-Edit"
54877f36
SM
381 "Major mode for editing version-control log messages.
382When done editing the log entry, just type \\[log-edit-done] which
383will trigger the actual commit of the file(s).
384Several other handy support commands are provided of course and
385the package from which this is used might also provide additional
386commands (under C-x v for VC, for example).
387
1be77002 388\\{log-edit-mode-map}"
165a7fbe
SM
389 (set (make-local-variable 'font-lock-defaults)
390 '(log-edit-font-lock-keywords t))
8117868f
DN
391 (make-local-variable 'log-edit-comment-ring-index)
392 (hack-dir-local-variables-non-file-buffer))
5b467bf4
SM
393
394(defun log-edit-hide-buf (&optional buf where)
395 (when (setq buf (get-buffer (or buf log-edit-files-buf)))
396 (let ((win (get-buffer-window buf where)))
397 (if win (ignore-errors (delete-window win))))
398 (bury-buffer buf)))
399
400(defun log-edit-done ()
401 "Finish editing the log message and commit the files.
5b467bf4
SM
402If you want to abort the commit, simply delete the buffer."
403 (interactive)
ffe7dc64
SM
404 ;; Get rid of trailing empty lines
405 (goto-char (point-max))
406 (skip-syntax-backward " ")
407 (when (equal (char-after) ?\n) (forward-char 1))
408 (delete-region (point) (point-max))
409 ;; Check for final newline
cdf54749
SM
410 (if (and (> (point-max) (point-min))
411 (/= (char-before (point-max)) ?\n)
5b467bf4
SM
412 (or (eq log-edit-require-final-newline t)
413 (and log-edit-require-final-newline
414 (y-or-n-p
415 (format "Buffer %s does not end in newline. Add one? "
416 (buffer-name))))))
417 (save-excursion
418 (goto-char (point-max))
419 (insert ?\n)))
1be77002 420 (let ((comment (buffer-string)))
9fe89a26
SM
421 (when (or (ring-empty-p log-edit-comment-ring)
422 (not (equal comment (ring-ref log-edit-comment-ring 0))))
423 (ring-insert log-edit-comment-ring comment)))
5b467bf4
SM
424 (let ((win (get-buffer-window log-edit-files-buf)))
425 (if (and log-edit-confirm
426 (not (and (eq log-edit-confirm 'changed)
427 (equal (log-edit-files) log-edit-initial-files)))
428 (progn
429 (log-edit-show-files)
ce5a3ac0 430 (not (y-or-n-p "Really commit? "))))
5b467bf4
SM
431 (progn (when (not win) (log-edit-hide-buf))
432 (message "Oh, well! Later maybe?"))
433 (run-hooks 'log-edit-done-hook)
434 (log-edit-hide-buf)
cdf54749
SM
435 (unless (or log-edit-keep-buffer (not log-edit-parent-buffer))
436 (cvs-bury-buffer (current-buffer) log-edit-parent-buffer))
5b467bf4
SM
437 (call-interactively log-edit-callback))))
438
439(defun log-edit-files ()
440 "Return the list of files that are about to be committed."
441 (ignore-errors (funcall log-edit-listfun)))
442
5b467bf4
SM
443(defun log-edit-mode-help ()
444 "Provide help for the `log-edit-mode-map'."
445 (interactive)
446 (if (eq last-command 'log-edit-mode-help)
447 (describe-function major-mode)
8a26c165 448 (message "%s"
5b467bf4
SM
449 (substitute-command-keys
450 "Type `\\[log-edit-done]' to finish commit. Try `\\[describe-function] log-edit-done' for more help."))))
451
43a9a0c4
SM
452(defcustom log-edit-common-indent 0
453 "Minimum indentation to use in `log-edit-set-common-indentation'."
454 :group 'log-edit
455 :type 'integer)
456
457(defun log-edit-set-common-indentation ()
458 "(Un)Indent the current buffer rigidly to `log-edit-common-indent'."
5b467bf4
SM
459 (save-excursion
460 (let ((common (point-max)))
461 (goto-char (point-min))
462 (while (< (point) (point-max))
463 (if (not (looking-at "^[ \t]*$"))
464 (setq common (min common (current-indentation))))
465 (forward-line 1))
43a9a0c4
SM
466 (indent-rigidly (point-min) (point-max)
467 (- log-edit-common-indent common)))))
5b467bf4 468
93a142e1
DN
469(defun log-edit-show-diff ()
470 "Show the diff for the files to be committed."
471 (interactive)
37b72bf5
DN
472 (if (functionp log-edit-diff-function)
473 (funcall log-edit-diff-function)
474 (error "Diff functionality has not been setup")))
93a142e1 475
5b467bf4
SM
476(defun log-edit-show-files ()
477 "Show the list of files to be committed."
478 (interactive)
479 (let* ((files (log-edit-files))
cdf54749 480 (buf (get-buffer-create log-edit-files-buf)))
5b467bf4
SM
481 (with-current-buffer buf
482 (log-edit-hide-buf buf 'all)
483 (setq buffer-read-only nil)
484 (erase-buffer)
a88e99b5 485 (cvs-insert-strings files)
5b467bf4
SM
486 (setq buffer-read-only t)
487 (goto-char (point-min))
488 (save-selected-window
489 (cvs-pop-to-buffer-same-frame buf)
490 (shrink-window-if-larger-than-buffer)
491 (selected-window)))))
492
493(defun log-edit-insert-cvs-template ()
f7eeab0d
SM
494 "Insert the template specified by the CVS administrator, if any.
495This simply uses the local CVS/Template file."
5b467bf4 496 (interactive)
32226619
JB
497 (when (or (called-interactively-p 'interactive)
498 (= (point-min) (point-max)))
f7eeab0d
SM
499 (when (file-readable-p "CVS/Template")
500 (insert-file-contents "CVS/Template"))))
501
502(defun log-edit-insert-cvs-rcstemplate ()
503 "Insert the rcstemplate from the CVS repository.
504This contacts the repository to get the rcstemplate file and
505can thus take some time."
506 (interactive)
32226619
JB
507 (when (or (called-interactively-p 'interactive)
508 (= (point-min) (point-max)))
21227135
SM
509 (when (file-readable-p "CVS/Root")
510 ;; Ignore the stderr stuff, even if it's an error.
511 (call-process "cvs" nil '(t nil) nil
512 "checkout" "-p" "CVSROOT/rcstemplate"))))
f1180544 513
f7eeab0d
SM
514(defun log-edit-insert-filenames ()
515 "Insert the list of files that are to be committed."
516 (interactive)
517 (insert "Affected files: \n"
518 (mapconcat 'identity (log-edit-files) " \n")))
5b467bf4
SM
519
520(defun log-edit-add-to-changelog ()
521 "Insert this log message into the appropriate ChangeLog file."
522 (interactive)
523 ;; Yuck!
9fe89a26
SM
524 (unless (string= (buffer-string) (ring-ref log-edit-comment-ring 0))
525 (ring-insert log-edit-comment-ring (buffer-string)))
5b467bf4
SM
526 (dolist (f (log-edit-files))
527 (let ((buffer-file-name (expand-file-name f)))
528 (save-excursion
9fe89a26 529 (log-edit-comment-to-change-log)))))
5b467bf4 530
f7eeab0d
SM
531(defvar log-edit-changelog-use-first nil)
532(defun log-edit-insert-changelog (&optional use-first)
533 "Insert a log message by looking at the ChangeLog.
534The idea is to write your ChangeLog entries first, and then use this
535command to commit your changes.
536
537To select default log text, we:
538- find the ChangeLog entries for the files to be checked in,
539- verify that the top entry in the ChangeLog is on the current date
540 and by the current user; if not, we don't provide any default text,
541- search the ChangeLog entry for paragraphs containing the names of
542 the files we're checking in, and finally
543- use those paragraphs as the log text.
544
545If the optional prefix arg USE-FIRST is given (via \\[universal-argument]),
546or if the command is repeated a second time in a row, use the first log entry
547regardless of user name or time."
548 (interactive "P")
549 (let ((log-edit-changelog-use-first
550 (or use-first (eq last-command 'log-edit-insert-changelog))))
551 (log-edit-insert-changelog-entries (log-edit-files)))
552 (log-edit-set-common-indentation)
553 (goto-char (point-min))
31764e15 554 (when (and log-edit-strip-single-file-name (looking-at "\\*\\s-+"))
f7eeab0d
SM
555 (forward-line 1)
556 (when (not (re-search-forward "^\\*\\s-+" nil t))
557 (goto-char (point-min))
558 (skip-chars-forward "^():")
559 (skip-chars-forward ": ")
560 (delete-region (point-min) (point)))))
561
f1180544 562;;;;
5b467bf4
SM
563;;;; functions for getting commit message from ChangeLog a file...
564;;;; Courtesy Jim Blandy
f1180544 565;;;;
5b467bf4 566
14188021 567(defun log-edit-narrow-changelog ()
5b467bf4
SM
568 "Narrow to the top page of the current buffer, a ChangeLog file.
569Actually, the narrowed region doesn't include the date line.
570A \"page\" in a ChangeLog file is the area between two dates."
571 (or (eq major-mode 'change-log-mode)
14188021 572 (error "log-edit-narrow-changelog: current buffer isn't a ChangeLog"))
5b467bf4
SM
573
574 (goto-char (point-min))
575
576 ;; Skip date line and subsequent blank lines.
577 (forward-line 1)
578 (if (looking-at "[ \t\n]*\n")
579 (goto-char (match-end 0)))
580
581 (let ((start (point)))
582 (forward-page 1)
583 (narrow-to-region start (point))
584 (goto-char (point-min))))
585
14188021 586(defun log-edit-changelog-paragraph ()
5b467bf4
SM
587 "Return the bounds of the ChangeLog paragraph containing point.
588If we are between paragraphs, return the previous paragraph."
8390fb80
SM
589 (beginning-of-line)
590 (if (looking-at "^[ \t]*$")
591 (skip-chars-backward " \t\n" (point-min)))
592 (list (progn
593 (if (re-search-backward "^[ \t]*\n" nil 'or-to-limit)
594 (goto-char (match-end 0)))
595 (point))
596 (if (re-search-forward "^[ \t\n]*$" nil t)
597 (match-beginning 0)
598 (point-max))))
5b467bf4 599
14188021 600(defun log-edit-changelog-subparagraph ()
5b467bf4
SM
601 "Return the bounds of the ChangeLog subparagraph containing point.
602A subparagraph is a block of non-blank lines beginning with an asterisk.
603If we are between sub-paragraphs, return the previous subparagraph."
5b467bf4
SM
604 (end-of-line)
605 (if (search-backward "*" nil t)
606 (list (progn (beginning-of-line) (point))
bc35d341 607 (progn
5b467bf4
SM
608 (forward-line 1)
609 (if (re-search-forward "^[ \t]*[\n*]" nil t)
610 (match-beginning 0)
611 (point-max))))
8390fb80 612 (list (point) (point))))
5b467bf4 613
14188021 614(defun log-edit-changelog-entry ()
5b467bf4 615 "Return the bounds of the ChangeLog entry containing point.
14188021 616The variable `log-edit-changelog-full-paragraphs' decides whether an
5b467bf4
SM
617\"entry\" is a paragraph or a subparagraph; see its documentation string
618for more details."
8390fb80
SM
619 (save-excursion
620 (if log-edit-changelog-full-paragraphs
621 (log-edit-changelog-paragraph)
622 (log-edit-changelog-subparagraph))))
5b467bf4
SM
623
624(defvar user-full-name)
625(defvar user-mail-address)
14188021 626(defun log-edit-changelog-ours-p ()
5b467bf4 627 "See if ChangeLog entry at point is for the current user, today.
4837b516 628Return non-nil if it is."
5b467bf4
SM
629 ;; Code adapted from add-change-log-entry.
630 (let ((name (or (and (boundp 'add-log-full-name) add-log-full-name)
631 (and (fboundp 'user-full-name) (user-full-name))
632 (and (boundp 'user-full-name) user-full-name)))
633 (mail (or (and (boundp 'add-log-mailing-address) add-log-mailing-address)
634 ;;(and (fboundp 'user-mail-address) (user-mail-address))
635 (and (boundp 'user-mail-address) user-mail-address)))
636 (time (or (and (boundp 'add-log-time-format)
637 (functionp add-log-time-format)
638 (funcall add-log-time-format))
639 (format-time-string "%Y-%m-%d"))))
f7eeab0d
SM
640 (looking-at (if log-edit-changelog-use-first
641 "[^ \t]"
642 (regexp-quote (format "%s %s <%s>" time name mail))))))
5b467bf4 643
14188021 644(defun log-edit-changelog-entries (file)
5b467bf4
SM
645 "Return the ChangeLog entries for FILE, and the ChangeLog they came from.
646The return value looks like this:
647 (LOGBUFFER (ENTRYSTART . ENTRYEND) ...)
648where LOGBUFFER is the name of the ChangeLog buffer, and each
649\(ENTRYSTART . ENTRYEND\) pair is a buffer region."
d944ee49
SM
650 (let ((changelog-file-name
651 (let ((default-directory
652 (file-name-directory (expand-file-name file)))
653 (visiting-buffer (find-buffer-visiting file)))
654 ;; If there is a buffer visiting FILE, and it has a local
655 ;; value for `change-log-default-name', use that.
656 (if (and visiting-buffer
657 (local-variable-p 'change-log-default-name
658 visiting-buffer))
659 (with-current-buffer visiting-buffer
660 change-log-default-name)
661 ;; `find-change-log' uses `change-log-default-name' if set
662 ;; and sets it before exiting, so we need to work around
663 ;; that memoizing which is undesired here
664 (setq change-log-default-name nil)
665 (find-change-log)))))
666 (with-current-buffer (find-file-noselect changelog-file-name)
5b467bf4
SM
667 (unless (eq major-mode 'change-log-mode) (change-log-mode))
668 (goto-char (point-min))
669 (if (looking-at "\\s-*\n") (goto-char (match-end 0)))
14188021 670 (if (not (log-edit-changelog-ours-p))
5b467bf4
SM
671 (list (current-buffer))
672 (save-restriction
14188021 673 (log-edit-narrow-changelog)
5b467bf4 674 (goto-char (point-min))
f1180544 675
5b467bf4
SM
676 ;; Search for the name of FILE relative to the ChangeLog. If that
677 ;; doesn't occur anywhere, they're not using full relative
678 ;; filenames in the ChangeLog, so just look for FILE; we'll accept
679 ;; some false positives.
680 (let ((pattern (file-relative-name
681 file (file-name-directory changelog-file-name))))
682 (if (or (string= pattern "")
683 (not (save-excursion
684 (search-forward pattern nil t))))
685 (setq pattern (file-name-nondirectory file)))
686
b543ff57
NR
687 (setq pattern (concat "\\(^\\|[^[:alnum:]]\\)"
688 pattern
689 "\\($\\|[^[:alnum:]]\\)"))
690
8390fb80
SM
691 (let (texts
692 (pos (point)))
693 (while (and (not (eobp)) (re-search-forward pattern nil t))
14188021 694 (let ((entry (log-edit-changelog-entry)))
8390fb80
SM
695 (if (< (elt entry 1) (max (1+ pos) (point)))
696 ;; This is not relevant, actually.
697 nil
698 (push entry texts))
699 ;; Make sure we make progress.
700 (setq pos (max (1+ pos) (elt entry 1)))
701 (goto-char pos)))
5b467bf4
SM
702
703 (cons (current-buffer) texts))))))))
704
14188021 705(defun log-edit-changelog-insert-entries (buffer regions)
5b467bf4
SM
706 "Insert those regions in BUFFER specified in REGIONS.
707Sort REGIONS front-to-back first."
708 (let ((regions (sort regions 'car-less-than-car))
709 (last))
710 (dolist (region regions)
711 (when (and last (< last (car region))) (newline))
712 (setq last (elt region 1))
713 (apply 'insert-buffer-substring buffer region))))
714
14188021 715(defun log-edit-insert-changelog-entries (files)
5b467bf4
SM
716 "Given a list of files FILES, insert the ChangeLog entries for them."
717 (let ((buffer-entries nil))
718
719 ;; Add each buffer to buffer-entries, and associate it with the list
720 ;; of entries we want from that file.
721 (dolist (file files)
14188021 722 (let* ((entries (log-edit-changelog-entries file))
5b467bf4
SM
723 (pair (assq (car entries) buffer-entries)))
724 (if pair
725 (setcdr pair (cvs-union (cdr pair) (cdr entries)))
726 (push entries buffer-entries))))
727
728 ;; Now map over each buffer in buffer-entries, sort the entries for
729 ;; each buffer, and extract them as strings.
730 (dolist (buffer-entry buffer-entries)
14188021 731 (log-edit-changelog-insert-entries (car buffer-entry) (cdr buffer-entry))
5b467bf4
SM
732 (when (cdr buffer-entry) (newline)))))
733
09158997
DN
734(defun log-view-process-buffer ()
735 (when log-edit-before-checkin-process
736 (dolist (crt log-edit-before-checkin-process)
737 ;; Remove all lines matching (car crt)
738 ;; Append to `log-edit-extra-flags' the results of (cdr crt).
739 (goto-char (point-min))
740 (while (re-search-forward (car crt) nil t)
741 (when (cdr crt)
742 (setq log-edit-extra-flags (append log-edit-extra-flags (eval (cdr crt)))))
743 (replace-match "" nil t)))))
744
5b467bf4 745(provide 'log-edit)
54877f36 746
b543ff57 747;; arch-tag: 8089b39c-983b-4e83-93cd-ed0a64c7fdcc
5b467bf4 748;;; log-edit.el ends here