log-edit.el 26.8 KB
Newer Older
Stefan Monnier's avatar
Stefan Monnier committed
1 2
;;; log-edit.el --- Major mode for editing CVS commit messages

Glenn Morris's avatar
Glenn Morris committed
3
;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004,
Glenn Morris's avatar
Glenn Morris committed
4
;;   2005, 2006, 2007 Free Software Foundation, Inc.
Stefan Monnier's avatar
Stefan Monnier committed
5

Stefan Monnier's avatar
Stefan Monnier committed
6
;; Author: Stefan Monnier <monnier@iro.umontreal.ca>
Stefan Monnier's avatar
Stefan Monnier committed
7 8 9 10 11 12
;; Keywords: pcl-cvs cvs commit log

;; This file is part of GNU Emacs.

;; GNU Emacs is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
13
;; the Free Software Foundation; either version 3, or (at your option)
Stefan Monnier's avatar
Stefan Monnier committed
14 15 16 17 18 19 20 21 22
;; any later version.

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING.  If not, write to the
Lute Kamstra's avatar
Lute Kamstra committed
23 24
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.
Stefan Monnier's avatar
Stefan Monnier committed
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39

;;; Commentary:

;; Todo:

;; - Move in VC's code
;; - Add compatibility for VC's hook variables

;;; Code:

(eval-when-compile (require 'cl))
(require 'add-log)			; for all the ChangeLog goodies
(require 'pcvs-util)
(require 'ring)

40
;;;;
Stefan Monnier's avatar
Stefan Monnier committed
41
;;;; Global Variables
42
;;;;
Stefan Monnier's avatar
Stefan Monnier committed
43 44

(defgroup log-edit nil
45
  "Major mode for editing RCS and CVS commit messages."
Stefan Monnier's avatar
Stefan Monnier committed
46
  :group 'pcl-cvs
47 48
  :group 'vc				; It's used by VC.
  :version "21.1"
Stefan Monnier's avatar
Stefan Monnier committed
49 50 51 52 53
  :prefix "log-edit-")

;; compiler pacifiers
(defvar cvs-buffer)

54 55 56

;; The main keymap

Stefan Monnier's avatar
Stefan Monnier committed
57 58 59
(easy-mmode-defmap log-edit-mode-map
  `(("\C-c\C-c" . log-edit-done)
    ("\C-c\C-a" . log-edit-insert-changelog)
60
    ("\C-c\C-d" . log-edit-show-diff)
Stefan Monnier's avatar
Stefan Monnier committed
61
    ("\C-c\C-f" . log-edit-show-files)
62 63 64 65
    ("\M-n"	. log-edit-next-comment)
    ("\M-p"	. log-edit-previous-comment)
    ("\M-r"	. log-edit-comment-search-backward)
    ("\M-s"	. log-edit-comment-search-forward)
Stefan Monnier's avatar
Stefan Monnier committed
66
    ("\C-c?"	. log-edit-mode-help))
67
  "Keymap for the `log-edit-mode' (to edit version control log messages)."
Stefan Monnier's avatar
Stefan Monnier committed
68 69 70 71 72
  :group 'log-edit)

;; Compatibility with old names.  Should we bother ?
(defvar vc-log-mode-map log-edit-mode-map)
(defvar vc-log-entry-mode vc-log-mode-map)
Stefan Monnier's avatar
Stefan Monnier committed
73

Stefan Monnier's avatar
Stefan Monnier committed
74 75 76 77 78 79 80 81 82
(easy-menu-define log-edit-menu log-edit-mode-map
  "Menu used for `log-edit-mode'."
  '("Log-Edit"
    ["Done" log-edit-done
     :help "Exit log-edit and proceed with the actual action."]
    "--"
    ["Insert ChangeLog" log-edit-insert-changelog]
    ["Add to ChangeLog" log-edit-add-to-changelog]
    "--"
83 84
    ["Show diff" log-edit-show-diff
     :help "Show the diff for the files to be committed."]
Stefan Monnier's avatar
Stefan Monnier committed
85 86 87
    ["List files" log-edit-show-files
     :help "Show the list of relevant files."]
    "--"
88 89 90 91
    ["Previous comment"		log-edit-previous-comment]
    ["Next comment"		log-edit-next-comment]
    ["Search comment forward"	log-edit-comment-search-forward]
    ["Search comment backward"	log-edit-comment-search-backward]))
Stefan Monnier's avatar
Stefan Monnier committed
92

93
(defcustom log-edit-confirm 'changed
Stefan Monnier's avatar
Stefan Monnier committed
94 95 96 97 98 99 100 101 102 103 104
  "*If non-nil, `log-edit-done' will request confirmation.
If 'changed, only request confirmation if the list of files has
  changed since the beginning of the log-edit session."
  :group 'log-edit
  :type '(choice (const changed) (const t) (const nil)))

(defcustom log-edit-keep-buffer nil
  "*If non-nil, don't hide the buffer after `log-edit-done'."
  :group 'log-edit
  :type 'boolean)

105 106 107
(defvar cvs-commit-buffer-require-final-newline t)
(make-obsolete-variable 'cvs-commit-buffer-require-final-newline
                        'log-edit-require-final-newline)
Stefan Monnier's avatar
Stefan Monnier committed
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125

(defcustom log-edit-require-final-newline
  cvs-commit-buffer-require-final-newline
  "*Enforce a newline at the end of commit log messages.
Enforce it silently if t, query if non-nil and don't do anything if nil."
  :group 'log-edit
  :type '(choice (const ask) (const t) (const nil)))

(defcustom log-edit-setup-invert nil
  "*Non-nil means `log-edit' should invert the meaning of its SETUP arg.
If SETUP is 'force, this variable has no effect."
  :group 'log-edit
  :type 'boolean)

(defcustom log-edit-hook '(log-edit-insert-cvs-template
			   log-edit-insert-changelog)
  "*Hook run at the end of `log-edit'."
  :group 'log-edit
Stefan Monnier's avatar
Stefan Monnier committed
126 127 128 129
  :type '(hook :options (log-edit-insert-changelog
                         log-edit-insert-cvs-rcstemplate
                         log-edit-insert-cvs-template
			 log-edit-insert-filenames)))
Stefan Monnier's avatar
Stefan Monnier committed
130

131
(defcustom log-edit-mode-hook (if (boundp 'vc-log-mode-hook) vc-log-mode-hook)
Stefan Monnier's avatar
Stefan Monnier committed
132 133 134 135 136 137 138 139 140 141 142
  "*Hook run when entering `log-edit-mode'."
  :group 'log-edit
  :type 'hook)

(defcustom log-edit-done-hook nil
  "*Hook run before doing the actual commit.
This hook can be used to cleanup the message, enforce various
conventions, or to allow recording the message in some other database,
such as a bug-tracking system.  The list of files about to be committed
can be obtained from `log-edit-files'."
  :group 'log-edit
143
  :type '(hook :options (log-edit-set-common-indentation
Stefan Monnier's avatar
Stefan Monnier committed
144 145
			 log-edit-add-to-changelog)))

146 147 148
(defvar cvs-changelog-full-paragraphs t)
(make-obsolete-variable 'cvs-changelog-full-paragraphs
                        'log-edit-changelog-full-paragraphs)
149 150 151

(defvar log-edit-changelog-full-paragraphs cvs-changelog-full-paragraphs
  "*If non-nil, include full ChangeLog paragraphs in the log.
Stefan Monnier's avatar
Stefan Monnier committed
152 153 154 155 156 157 158 159
This may be set in the ``local variables'' section of a ChangeLog, to
indicate the policy for that ChangeLog.

A ChangeLog paragraph is a bunch of log text containing no blank lines;
a paragraph usually describes a set of changes with a single purpose,
but perhaps spanning several functions in several files.  Changes in
different paragraphs are unrelated.

160
You could argue that the log entry for a file should contain the
Stefan Monnier's avatar
Stefan Monnier committed
161 162
full ChangeLog paragraph mentioning the change to the file, even though
it may mention other files, because that gives you the full context you
163
need to understand the change.  This is the behavior you get when this
Stefan Monnier's avatar
Stefan Monnier committed
164 165
variable is set to t.

166
On the other hand, you could argue that the log entry for a change
Stefan Monnier's avatar
Stefan Monnier committed
167
should contain only the text for the changes which occurred in that
168
file, because the log is per-file.  This is the behavior you get
Stefan Monnier's avatar
Stefan Monnier committed
169 170 171 172 173 174 175
when this variable is set to nil.")

;;;; Internal global or buffer-local vars

(defconst log-edit-files-buf "*log-edit-files*")
(defvar log-edit-initial-files nil)
(defvar log-edit-callback nil)
176
(defvar log-edit-diff-function nil)
Stefan Monnier's avatar
Stefan Monnier committed
177
(defvar log-edit-listfun nil)
178
(defvar log-edit-parent-buffer nil)
Stefan Monnier's avatar
Stefan Monnier committed
179

180
;;; Originally taken from VC-Log mode
181

182
(defconst log-edit-maximum-comment-ring-size 32
183
  "Maximum number of saved comments in the comment ring.")
184 185 186
(defvar log-edit-comment-ring (make-ring log-edit-maximum-comment-ring-size))
(defvar log-edit-comment-ring-index nil)
(defvar log-edit-last-comment-match "")
187

188
(defun log-edit-new-comment-index (stride len)
189
  "Return the comment index STRIDE elements from the current one.
190
LEN is the length of `log-edit-comment-ring'."
191
  (mod (cond
192
	(log-edit-comment-ring-index (+ log-edit-comment-ring-index stride))
193 194 195 196 197 198 199
	;; Initialize the index on the first use of this command
	;; so that the first M-p gets index 0, and the first M-n gets
	;; index -1.
	((> stride 0) (1- stride))
	(t stride))
       len))

200
(defun log-edit-previous-comment (arg)
201 202 203
  "Cycle backwards through comment history.
With a numeric prefix ARG, go back ARG comments."
  (interactive "*p")
204
  (let ((len (ring-length log-edit-comment-ring)))
205 206
    (if (<= len 0)
	(progn (message "Empty comment ring") (ding))
Stefan Monnier's avatar
Stefan Monnier committed
207 208
      ;; Don't use `erase-buffer' because we don't want to `widen'.
      (delete-region (point-min) (point-max))
209 210 211
      (setq log-edit-comment-ring-index (log-edit-new-comment-index arg len))
      (message "Comment %d" (1+ log-edit-comment-ring-index))
      (insert (ring-ref log-edit-comment-ring log-edit-comment-ring-index)))))
212

213
(defun log-edit-next-comment (arg)
214 215 216
  "Cycle forwards through comment history.
With a numeric prefix ARG, go forward ARG comments."
  (interactive "*p")
217
  (log-edit-previous-comment (- arg)))
218

219
(defun log-edit-comment-search-backward (str &optional stride)
220 221 222 223 224
  "Search backwards through comment history for substring match of STR.
If the optional argument STRIDE is present, that is a step-width to use
when going through the comment ring."
  ;; Why substring rather than regexp ?   -sm
  (interactive
225
   (list (read-string "Comment substring: " nil nil log-edit-last-comment-match)))
226 227
  (unless stride (setq stride 1))
  (if (string= str "")
228 229
      (setq str log-edit-last-comment-match)
    (setq log-edit-last-comment-match str))
230
  (let* ((str (regexp-quote str))
231 232
	 (len (ring-length log-edit-comment-ring))
	 (n (log-edit-new-comment-index stride len)))
233
    (while (progn (when (or (>= n len) (< n 0)) (error "Not found"))
234
		  (not (string-match str (ring-ref log-edit-comment-ring n))))
235
      (setq n (+ n stride)))
236 237
    (setq log-edit-comment-ring-index n)
    (log-edit-previous-comment 0)))
238

239
(defun log-edit-comment-search-forward (str)
240 241
  "Search forwards through comment history for a substring match of STR."
  (interactive
242 243
   (list (read-string "Comment substring: " nil nil log-edit-last-comment-match)))
  (log-edit-comment-search-backward str -1))
244

245
(defun log-edit-comment-to-change-log (&optional whoami file-name)
246 247 248 249 250
  "Enter last VC comment into the change log for the current file.
WHOAMI (interactive prefix) non-nil means prompt for user name
and site.  FILE-NAME is the name of the change log; if nil, use
`change-log-default-name'.

251
This may be useful as a `log-edit-checkin-hook' to update change logs
252 253 254 255 256
automatically."
  (interactive (if current-prefix-arg
		   (list current-prefix-arg
			 (prompt-for-change-log-name))))
  (let (;; Extract the comment first so we get any error before doing anything.
257
	(comment (ring-ref log-edit-comment-ring 0))
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
	;; Don't let add-change-log-entry insert a defun name.
	(add-log-current-defun-function 'ignore)
	end)
    ;; Call add-log to do half the work.
    (add-change-log-entry whoami file-name t t)
    ;; Insert the VC comment, leaving point before it.
    (setq end (save-excursion (insert comment) (point-marker)))
    (if (looking-at "\\s *\\s(")
	;; It starts with an open-paren, as in "(foo): Frobbed."
	;; So remove the ": " add-log inserted.
	(delete-char -2))
    ;; Canonicalize the white space between the file name and comment.
    (just-one-space)
    ;; Indent rest of the text the same way add-log indented the first line.
    (let ((indentation (current-indentation)))
      (save-excursion
	(while (< (point) end)
	  (forward-line 1)
	  (indent-to indentation))
	(setq end (point))))
    ;; Fill the inserted text, preserving open-parens at bol.
Stefan Monnier's avatar
Stefan Monnier committed
279
    (let ((paragraph-start (concat paragraph-start "\\|\\s *\\s(")))
280 281 282 283 284 285 286 287 288
      (beginning-of-line)
      (fill-region (point) end))
    ;; Canonicalize the white space at the end of the entry so it is
    ;; separated from the next entry by a single blank line.
    (skip-syntax-forward " " end)
    (delete-char (- (skip-syntax-backward " ")))
    (or (eobp) (looking-at "\n\n")
	(insert "\n"))))

289
;; Compatibility with old names.
Stefan Monnier's avatar
Stefan Monnier committed
290 291 292 293 294 295 296
(define-obsolete-variable-alias 'vc-comment-ring 'log-edit-comment-ring "22.1")
(define-obsolete-variable-alias 'vc-comment-ring-index 'log-edit-comment-ring-index "22.1")
(define-obsolete-function-alias 'vc-previous-comment 'log-edit-previous-comment "22.1")
(define-obsolete-function-alias 'vc-next-comment 'log-edit-next-comment "22.1")
(define-obsolete-function-alias 'vc-comment-search-reverse 'log-edit-comment-search-backward "22.1")
(define-obsolete-function-alias 'vc-comment-search-forward 'log-edit-comment-search-forward "22.1")
(define-obsolete-function-alias 'vc-comment-to-change-log 'log-edit-comment-to-change-log "22.1")
297

298 299 300
;;;
;;; Actual code
;;;
Stefan Monnier's avatar
Stefan Monnier committed
301

302
(defvar log-edit-font-lock-keywords
303 304 305 306
  '(("\\`\\(Summary:\\)\\(.*\\)"
     (1 font-lock-keyword-face)
     (2 font-lock-function-name-face))))

Stefan Monnier's avatar
Stefan Monnier committed
307
;;;###autoload
308
(defun log-edit (callback &optional setup params buffer &rest ignore)
Stefan Monnier's avatar
Stefan Monnier committed
309
  "Setup a buffer to enter a log message.
310
\\<log-edit-mode-map>The buffer will be put in `log-edit-mode'.
Stefan Monnier's avatar
Stefan Monnier committed
311 312 313 314
If SETUP is non-nil, the buffer is then erased and `log-edit-hook' is run.
Mark and point will be set around the entire contents of the
buffer so that it is easy to kill the contents of the buffer with \\[kill-region].
Once you're done editing the message, pressing \\[log-edit-done] will call
315
`log-edit-done' which will end up calling CALLBACK to do the actual commit.
316 317 318 319 320 321 322
PARAMS if non-nil is an alist. The keys for the alist can be:
`log-edit-listfun' and `log-edit-diff-function'. The associated
value for `log-edit-listfun' should be a function with not
arguments that returns the list of files that are concerned by
the current operation (using relative names). The associated
value for `log-edit-diff-function' should be a function with no
arguments that displays a diff of the files concerned by the current operation.
323 324 325 326 327 328 329 330 331 332
If BUFFER is non-nil `log-edit' will jump to that buffer, use it to edit the
  log message and go back to the current buffer when done.  Otherwise, it
  uses the current buffer."
  (let ((parent (current-buffer)))
    (if buffer (pop-to-buffer buffer))
    (when (and log-edit-setup-invert (not (eq setup 'force)))
      (setq setup (not setup)))
    (when setup (erase-buffer))
    (log-edit-mode)
    (set (make-local-variable 'log-edit-callback) callback)
333 334 335 336 337 338 339
    (if (listp params)
	(dolist (crt params)
	  (set (make-local-variable (car crt)) (cdr crt)))
      ;; For backward compatibility with log-edit up to version 22.2
      ;; accept non-list PARAMS to mean `log-edit-list'.
      (set (make-local-variable 'log-edit-listfun) params))

340
    (if buffer (set (make-local-variable 'log-edit-parent-buffer) parent))
341
    (set (make-local-variable 'log-edit-initial-files) (log-edit-files))
342 343
    (when setup (run-hooks 'log-edit-hook))
    (goto-char (point-min)) (push-mark (point-max))
Deepak Goel's avatar
Deepak Goel committed
344
    (message "%s" (substitute-command-keys
345
	      "Press \\[log-edit-done] when you are done editing."))))
Stefan Monnier's avatar
Stefan Monnier committed
346 347

(define-derived-mode log-edit-mode text-mode "Log-Edit"
348 349 350 351 352 353 354
  "Major mode for editing version-control log messages.
When done editing the log entry, just type \\[log-edit-done] which
will trigger the actual commit of the file(s).
Several other handy support commands are provided of course and
the package from which this is used might also provide additional
commands (under C-x v for VC, for example).

355
\\{log-edit-mode-map}"
356 357
  (set (make-local-variable 'font-lock-defaults)
       '(log-edit-font-lock-keywords t))
358
  (make-local-variable 'log-edit-comment-ring-index))
Stefan Monnier's avatar
Stefan Monnier committed
359 360 361 362 363 364 365 366 367 368 369

(defun log-edit-hide-buf (&optional buf where)
  (when (setq buf (get-buffer (or buf log-edit-files-buf)))
    (let ((win (get-buffer-window buf where)))
      (if win (ignore-errors (delete-window win))))
    (bury-buffer buf)))

(defun log-edit-done ()
  "Finish editing the log message and commit the files.
If you want to abort the commit, simply delete the buffer."
  (interactive)
370 371 372 373 374 375
  ;; Get rid of trailing empty lines
  (goto-char (point-max))
  (skip-syntax-backward " ")
  (when (equal (char-after) ?\n) (forward-char 1))
  (delete-region (point) (point-max))
  ;; Check for final newline
376 377
  (if (and (> (point-max) (point-min))
	   (/= (char-before (point-max)) ?\n)
Stefan Monnier's avatar
Stefan Monnier committed
378 379 380 381 382 383 384 385
	   (or (eq log-edit-require-final-newline t)
	       (and log-edit-require-final-newline
		    (y-or-n-p
		     (format "Buffer %s does not end in newline.  Add one? "
			     (buffer-name))))))
      (save-excursion
	(goto-char (point-max))
	(insert ?\n)))
386
  (let ((comment (buffer-string)))
387 388 389
    (when (or (ring-empty-p log-edit-comment-ring)
	      (not (equal comment (ring-ref log-edit-comment-ring 0))))
      (ring-insert log-edit-comment-ring comment)))
Stefan Monnier's avatar
Stefan Monnier committed
390 391 392 393 394 395
  (let ((win (get-buffer-window log-edit-files-buf)))
    (if (and log-edit-confirm
	     (not (and (eq log-edit-confirm 'changed)
		       (equal (log-edit-files) log-edit-initial-files)))
	     (progn
	       (log-edit-show-files)
396
	       (not (y-or-n-p "Really commit? "))))
Stefan Monnier's avatar
Stefan Monnier committed
397 398 399 400
	(progn (when (not win) (log-edit-hide-buf))
	       (message "Oh, well!  Later maybe?"))
      (run-hooks 'log-edit-done-hook)
      (log-edit-hide-buf)
401 402
      (unless (or log-edit-keep-buffer (not log-edit-parent-buffer))
	(cvs-bury-buffer (current-buffer) log-edit-parent-buffer))
Stefan Monnier's avatar
Stefan Monnier committed
403 404 405 406 407 408 409 410 411 412 413
      (call-interactively log-edit-callback))))

(defun log-edit-files ()
  "Return the list of files that are about to be committed."
  (ignore-errors (funcall log-edit-listfun)))

(defun log-edit-mode-help ()
  "Provide help for the `log-edit-mode-map'."
  (interactive)
  (if (eq last-command 'log-edit-mode-help)
      (describe-function major-mode)
Deepak Goel's avatar
Deepak Goel committed
414
    (message "%s"
Stefan Monnier's avatar
Stefan Monnier committed
415 416 417
     (substitute-command-keys
      "Type `\\[log-edit-done]' to finish commit.  Try `\\[describe-function] log-edit-done' for more help."))))

418 419 420 421 422 423 424
(defcustom log-edit-common-indent 0
  "Minimum indentation to use in `log-edit-set-common-indentation'."
  :group 'log-edit
  :type 'integer)

(defun log-edit-set-common-indentation ()
  "(Un)Indent the current buffer rigidly to `log-edit-common-indent'."
Stefan Monnier's avatar
Stefan Monnier committed
425 426 427 428 429 430 431
  (save-excursion
    (let ((common (point-max)))
      (goto-char (point-min))
      (while (< (point) (point-max))
        (if (not (looking-at "^[ \t]*$"))
            (setq common (min common (current-indentation))))
        (forward-line 1))
432 433
      (indent-rigidly (point-min) (point-max)
		      (- log-edit-common-indent common)))))
Stefan Monnier's avatar
Stefan Monnier committed
434

435 436 437 438 439
(defun log-edit-show-diff ()
  "Show the diff for the files to be committed."
  (interactive)
  (funcall log-edit-diff-function))

Stefan Monnier's avatar
Stefan Monnier committed
440 441 442 443
(defun log-edit-show-files ()
  "Show the list of files to be committed."
  (interactive)
  (let* ((files (log-edit-files))
444
	 (buf (get-buffer-create log-edit-files-buf)))
Stefan Monnier's avatar
Stefan Monnier committed
445 446 447 448
    (with-current-buffer buf
      (log-edit-hide-buf buf 'all)
      (setq buffer-read-only nil)
      (erase-buffer)
449
      (cvs-insert-strings files)
Stefan Monnier's avatar
Stefan Monnier committed
450 451 452 453 454 455 456 457
      (setq buffer-read-only t)
      (goto-char (point-min))
      (save-selected-window
	(cvs-pop-to-buffer-same-frame buf)
	(shrink-window-if-larger-than-buffer)
	(selected-window)))))

(defun log-edit-insert-cvs-template ()
Stefan Monnier's avatar
Stefan Monnier committed
458 459
  "Insert the template specified by the CVS administrator, if any.
This simply uses the local CVS/Template file."
Stefan Monnier's avatar
Stefan Monnier committed
460
  (interactive)
Stefan Monnier's avatar
Stefan Monnier committed
461 462 463 464 465 466 467 468 469 470
  (when (or (interactive-p) (= (point-min) (point-max)))
    (when (file-readable-p "CVS/Template")
      (insert-file-contents "CVS/Template"))))

(defun log-edit-insert-cvs-rcstemplate ()
  "Insert the rcstemplate from the CVS repository.
This contacts the repository to get the rcstemplate file and
can thus take some time."
  (interactive)
  (when (or (interactive-p) (= (point-min) (point-max)))
471 472 473 474
    (when (file-readable-p "CVS/Root")
      ;; Ignore the stderr stuff, even if it's an error.
      (call-process "cvs" nil '(t nil) nil
                    "checkout" "-p" "CVSROOT/rcstemplate"))))
475

Stefan Monnier's avatar
Stefan Monnier committed
476 477 478 479 480
(defun log-edit-insert-filenames ()
  "Insert the list of files that are to be committed."
  (interactive)
  (insert "Affected files:  \n"
          (mapconcat 'identity (log-edit-files) "  \n")))
Stefan Monnier's avatar
Stefan Monnier committed
481 482 483 484 485

(defun log-edit-add-to-changelog ()
  "Insert this log message into the appropriate ChangeLog file."
  (interactive)
  ;; Yuck!
486 487
  (unless (string= (buffer-string) (ring-ref log-edit-comment-ring 0))
    (ring-insert log-edit-comment-ring (buffer-string)))
Stefan Monnier's avatar
Stefan Monnier committed
488 489 490
  (dolist (f (log-edit-files))
    (let ((buffer-file-name (expand-file-name f)))
      (save-excursion
491
	(log-edit-comment-to-change-log)))))
Stefan Monnier's avatar
Stefan Monnier committed
492

Stefan Monnier's avatar
Stefan Monnier committed
493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523
(defvar log-edit-changelog-use-first nil)
(defun log-edit-insert-changelog (&optional use-first)
  "Insert a log message by looking at the ChangeLog.
The idea is to write your ChangeLog entries first, and then use this
command to commit your changes.

To select default log text, we:
- find the ChangeLog entries for the files to be checked in,
- verify that the top entry in the ChangeLog is on the current date
  and by the current user; if not, we don't provide any default text,
- search the ChangeLog entry for paragraphs containing the names of
  the files we're checking in, and finally
- use those paragraphs as the log text.

If the optional prefix arg USE-FIRST is given (via \\[universal-argument]),
or if the command is repeated a second time in a row, use the first log entry
regardless of user name or time."
  (interactive "P")
  (let ((log-edit-changelog-use-first
	 (or use-first (eq last-command 'log-edit-insert-changelog))))
    (log-edit-insert-changelog-entries (log-edit-files)))
  (log-edit-set-common-indentation)
  (goto-char (point-min))
  (when (looking-at "\\*\\s-+")
    (forward-line 1)
    (when (not (re-search-forward "^\\*\\s-+" nil t))
      (goto-char (point-min))
      (skip-chars-forward "^():")
      (skip-chars-forward ": ")
      (delete-region (point-min) (point)))))

524
;;;;
Stefan Monnier's avatar
Stefan Monnier committed
525 526
;;;; functions for getting commit message from ChangeLog a file...
;;;; Courtesy Jim Blandy
527
;;;;
Stefan Monnier's avatar
Stefan Monnier committed
528

529
(defun log-edit-narrow-changelog ()
Stefan Monnier's avatar
Stefan Monnier committed
530 531 532 533
  "Narrow to the top page of the current buffer, a ChangeLog file.
Actually, the narrowed region doesn't include the date line.
A \"page\" in a ChangeLog file is the area between two dates."
  (or (eq major-mode 'change-log-mode)
534
      (error "log-edit-narrow-changelog: current buffer isn't a ChangeLog"))
Stefan Monnier's avatar
Stefan Monnier committed
535 536 537 538 539 540 541 542 543 544 545 546 547

  (goto-char (point-min))

  ;; Skip date line and subsequent blank lines.
  (forward-line 1)
  (if (looking-at "[ \t\n]*\n")
      (goto-char (match-end 0)))

  (let ((start (point)))
    (forward-page 1)
    (narrow-to-region start (point))
    (goto-char (point-min))))

548
(defun log-edit-changelog-paragraph ()
Stefan Monnier's avatar
Stefan Monnier committed
549 550 551 552 553 554 555 556 557 558 559 560
  "Return the bounds of the ChangeLog paragraph containing point.
If we are between paragraphs, return the previous paragraph."
  (save-excursion
    (beginning-of-line)
    (if (looking-at "^[ \t]*$")
        (skip-chars-backward " \t\n" (point-min)))
    (list (progn
            (if (re-search-backward "^[ \t]*\n" nil 'or-to-limit)
                (goto-char (match-end 0)))
            (point))
          (if (re-search-forward "^[ \t\n]*$" nil t)
              (match-beginning 0)
561
            (point-max)))))
Stefan Monnier's avatar
Stefan Monnier committed
562

563
(defun log-edit-changelog-subparagraph ()
Stefan Monnier's avatar
Stefan Monnier committed
564 565 566 567 568 569 570
  "Return the bounds of the ChangeLog subparagraph containing point.
A subparagraph is a block of non-blank lines beginning with an asterisk.
If we are between sub-paragraphs, return the previous subparagraph."
  (save-excursion
    (end-of-line)
    (if (search-backward "*" nil t)
        (list (progn (beginning-of-line) (point))
571
              (progn
Stefan Monnier's avatar
Stefan Monnier committed
572 573 574 575 576 577
                (forward-line 1)
                (if (re-search-forward "^[ \t]*[\n*]" nil t)
                    (match-beginning 0)
                  (point-max))))
      (list (point) (point)))))

578
(defun log-edit-changelog-entry ()
Stefan Monnier's avatar
Stefan Monnier committed
579
  "Return the bounds of the ChangeLog entry containing point.
580
The variable `log-edit-changelog-full-paragraphs' decides whether an
Stefan Monnier's avatar
Stefan Monnier committed
581 582
\"entry\" is a paragraph or a subparagraph; see its documentation string
for more details."
583 584 585
  (if log-edit-changelog-full-paragraphs
      (log-edit-changelog-paragraph)
    (log-edit-changelog-subparagraph)))
Stefan Monnier's avatar
Stefan Monnier committed
586 587 588

(defvar user-full-name)
(defvar user-mail-address)
589
(defun log-edit-changelog-ours-p ()
Stefan Monnier's avatar
Stefan Monnier committed
590
  "See if ChangeLog entry at point is for the current user, today.
591
Return non-nil if it is."
Stefan Monnier's avatar
Stefan Monnier committed
592 593 594 595 596 597 598 599 600 601 602
  ;; Code adapted from add-change-log-entry.
  (let ((name (or (and (boundp 'add-log-full-name) add-log-full-name)
		  (and (fboundp 'user-full-name) (user-full-name))
		  (and (boundp 'user-full-name) user-full-name)))
        (mail (or (and (boundp 'add-log-mailing-address) add-log-mailing-address)
		  ;;(and (fboundp 'user-mail-address) (user-mail-address))
		  (and (boundp 'user-mail-address) user-mail-address)))
	(time (or (and (boundp 'add-log-time-format)
		       (functionp add-log-time-format)
		       (funcall add-log-time-format))
		  (format-time-string "%Y-%m-%d"))))
Stefan Monnier's avatar
Stefan Monnier committed
603 604 605
    (looking-at (if log-edit-changelog-use-first
                    "[^ \t]"
                  (regexp-quote (format "%s  %s  <%s>" time name mail))))))
Stefan Monnier's avatar
Stefan Monnier committed
606

607
(defun log-edit-changelog-entries (file)
Stefan Monnier's avatar
Stefan Monnier committed
608 609 610 611 612
  "Return the ChangeLog entries for FILE, and the ChangeLog they came from.
The return value looks like this:
  (LOGBUFFER (ENTRYSTART . ENTRYEND) ...)
where LOGBUFFER is the name of the ChangeLog buffer, and each
\(ENTRYSTART . ENTRYEND\) pair is a buffer region."
613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629
  (let ((changelog-file-name
         (let ((default-directory
                 (file-name-directory (expand-file-name file)))
               (visiting-buffer (find-buffer-visiting file)))
           ;; If there is a buffer visiting FILE, and it has a local
           ;; value for `change-log-default-name', use that.
           (if (and visiting-buffer
                    (local-variable-p 'change-log-default-name
                                      visiting-buffer))
               (with-current-buffer visiting-buffer
                 change-log-default-name)
             ;; `find-change-log' uses `change-log-default-name' if set
             ;; and sets it before exiting, so we need to work around
             ;; that memoizing which is undesired here
             (setq change-log-default-name nil)
             (find-change-log)))))
    (with-current-buffer (find-file-noselect changelog-file-name)
Stefan Monnier's avatar
Stefan Monnier committed
630 631 632
      (unless (eq major-mode 'change-log-mode) (change-log-mode))
      (goto-char (point-min))
      (if (looking-at "\\s-*\n") (goto-char (match-end 0)))
633
      (if (not (log-edit-changelog-ours-p))
Stefan Monnier's avatar
Stefan Monnier committed
634 635
	  (list (current-buffer))
	(save-restriction
636
	  (log-edit-narrow-changelog)
Stefan Monnier's avatar
Stefan Monnier committed
637
	  (goto-char (point-min))
638

Stefan Monnier's avatar
Stefan Monnier committed
639 640 641 642 643 644 645 646 647 648 649
	  ;; Search for the name of FILE relative to the ChangeLog.  If that
	  ;; doesn't occur anywhere, they're not using full relative
	  ;; filenames in the ChangeLog, so just look for FILE; we'll accept
	  ;; some false positives.
	  (let ((pattern (file-relative-name
			  file (file-name-directory changelog-file-name))))
	    (if (or (string= pattern "")
		    (not (save-excursion
			   (search-forward pattern nil t))))
		(setq pattern (file-name-nondirectory file)))

650 651 652 653
            (setq pattern (concat "\\(^\\|[^[:alnum:]]\\)"
                                  pattern
                                  "\\($\\|[^[:alnum:]]\\)"))

Stefan Monnier's avatar
Stefan Monnier committed
654
	    (let (texts)
655
	      (while (re-search-forward pattern nil t)
656
		(let ((entry (log-edit-changelog-entry)))
Stefan Monnier's avatar
Stefan Monnier committed
657 658 659 660 661
		  (push entry texts)
		  (goto-char (elt entry 1))))

	      (cons (current-buffer) texts))))))))

662
(defun log-edit-changelog-insert-entries (buffer regions)
Stefan Monnier's avatar
Stefan Monnier committed
663 664 665 666 667 668 669 670 671
  "Insert those regions in BUFFER specified in REGIONS.
Sort REGIONS front-to-back first."
  (let ((regions (sort regions 'car-less-than-car))
        (last))
    (dolist (region regions)
      (when (and last (< last (car region))) (newline))
      (setq last (elt region 1))
      (apply 'insert-buffer-substring buffer region))))

672
(defun log-edit-insert-changelog-entries (files)
Stefan Monnier's avatar
Stefan Monnier committed
673 674 675 676 677 678
  "Given a list of files FILES, insert the ChangeLog entries for them."
  (let ((buffer-entries nil))

    ;; Add each buffer to buffer-entries, and associate it with the list
    ;; of entries we want from that file.
    (dolist (file files)
679
      (let* ((entries (log-edit-changelog-entries file))
Stefan Monnier's avatar
Stefan Monnier committed
680 681 682 683 684 685 686 687
             (pair (assq (car entries) buffer-entries)))
        (if pair
            (setcdr pair (cvs-union (cdr pair) (cdr entries)))
          (push entries buffer-entries))))

    ;; Now map over each buffer in buffer-entries, sort the entries for
    ;; each buffer, and extract them as strings.
    (dolist (buffer-entry buffer-entries)
688
      (log-edit-changelog-insert-entries (car buffer-entry) (cdr buffer-entry))
Stefan Monnier's avatar
Stefan Monnier committed
689 690 691
      (when (cdr buffer-entry) (newline)))))

(provide 'log-edit)
692

693
;; arch-tag: 8089b39c-983b-4e83-93cd-ed0a64c7fdcc
Stefan Monnier's avatar
Stefan Monnier committed
694
;;; log-edit.el ends here