log-edit.el 37.9 KB
Newer Older
1
;;; log-edit.el --- Major mode for editing CVS commit messages -*- lexical-binding: t -*-
Stefan Monnier's avatar
Stefan Monnier committed
2

3
;; Copyright (C) 1999-2012  Free Software Foundation, Inc.
Stefan Monnier's avatar
Stefan Monnier committed
4

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

;; This file is part of GNU Emacs.

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

;; 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
21
;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
Stefan Monnier's avatar
Stefan Monnier committed
22 23 24 25 26 27 28 29 30 31 32 33 34 35

;;; Commentary:

;; Todo:

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

;;; Code:

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

36
;;;;
Stefan Monnier's avatar
Stefan Monnier committed
37
;;;; Global Variables
38
;;;;
Stefan Monnier's avatar
Stefan Monnier committed
39 40

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

;; compiler pacifiers
(defvar cvs-buffer)

50 51 52

;; The main keymap

Stefan Monnier's avatar
Stefan Monnier committed
53 54 55
(easy-mmode-defmap log-edit-mode-map
  `(("\C-c\C-c" . log-edit-done)
    ("\C-c\C-a" . log-edit-insert-changelog)
56
    ("\C-c\C-d" . log-edit-show-diff)
Stefan Monnier's avatar
Stefan Monnier committed
57
    ("\C-c\C-f" . log-edit-show-files)
58 59 60 61
    ("\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
62
    ("\C-c?"	. log-edit-mode-help))
63
  "Keymap for the `log-edit-mode' (to edit version control log messages)."
Stefan Monnier's avatar
Stefan Monnier committed
64 65 66 67 68
  :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
69

Stefan Monnier's avatar
Stefan Monnier committed
70 71 72 73 74 75
(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."]
    "--"
76 77 78 79
    ["Insert ChangeLog" log-edit-insert-changelog
     :help "Insert a log message by looking at the ChangeLog"]
    ["Add to ChangeLog" log-edit-add-to-changelog
     :help "Insert this log message into the appropriate ChangeLog file"]
Stefan Monnier's avatar
Stefan Monnier committed
80
    "--"
81 82
    ["Show diff" log-edit-show-diff
     :help "Show the diff for the files to be committed."]
Stefan Monnier's avatar
Stefan Monnier committed
83 84 85
    ["List files" log-edit-show-files
     :help "Show the list of relevant files."]
    "--"
86 87 88 89 90 91 92 93
    ["Previous comment"		log-edit-previous-comment
     :help "Cycle backwards through comment history"]
    ["Next comment"		log-edit-next-comment
     :help "Cycle forwards through comment history."]
    ["Search comment forward"	log-edit-comment-search-forward
     :help "Search forwards through comment history for a substring match of str"]
    ["Search comment backward"	log-edit-comment-search-backward
     :help "Search backwards through comment history for substring match of str"]))
Stefan Monnier's avatar
Stefan Monnier committed
94

95
(defcustom log-edit-confirm 'changed
Lute Kamstra's avatar
Lute Kamstra committed
96
  "If non-nil, `log-edit-done' will request confirmation.
Stefan Monnier's avatar
Stefan Monnier committed
97 98 99 100 101 102
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
Lute Kamstra's avatar
Lute Kamstra committed
103
  "If non-nil, don't hide the buffer after `log-edit-done'."
Stefan Monnier's avatar
Stefan Monnier committed
104 105 106
  :group 'log-edit
  :type 'boolean)

107
(defcustom log-edit-require-final-newline t
Lute Kamstra's avatar
Lute Kamstra committed
108
  "Enforce a newline at the end of commit log messages.
Stefan Monnier's avatar
Stefan Monnier committed
109 110 111 112 113
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
Lute Kamstra's avatar
Lute Kamstra committed
114
  "Non-nil means `log-edit' should invert the meaning of its SETUP arg.
Stefan Monnier's avatar
Stefan Monnier committed
115 116 117 118 119
If SETUP is 'force, this variable has no effect."
  :group 'log-edit
  :type 'boolean)

(defcustom log-edit-hook '(log-edit-insert-cvs-template
120
                           log-edit-show-files
Stefan Monnier's avatar
Stefan Monnier committed
121
			   log-edit-insert-changelog)
Lute Kamstra's avatar
Lute Kamstra committed
122
  "Hook run at the end of `log-edit'."
Stefan Monnier's avatar
Stefan Monnier committed
123
  :group 'log-edit
Stefan Monnier's avatar
Stefan Monnier committed
124 125 126 127
  :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
128

129
(defcustom log-edit-mode-hook (if (boundp 'vc-log-mode-hook) vc-log-mode-hook)
Lute Kamstra's avatar
Lute Kamstra committed
130
  "Hook run when entering `log-edit-mode'."
Stefan Monnier's avatar
Stefan Monnier committed
131 132 133 134
  :group 'log-edit
  :type 'hook)

(defcustom log-edit-done-hook nil
Lute Kamstra's avatar
Lute Kamstra committed
135
  "Hook run before doing the actual commit.
Stefan Monnier's avatar
Stefan Monnier committed
136 137 138 139 140
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
141
  :type '(hook :options (log-edit-set-common-indentation
Stefan Monnier's avatar
Stefan Monnier committed
142 143
			 log-edit-add-to-changelog)))

144
(defcustom log-edit-strip-single-file-name nil
145
  "If non-nil, remove file name from single-file log entries."
146 147 148
  :type 'boolean
  :safe 'booleanp
  :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.
Stefan Monnier's avatar
Stefan Monnier committed
153 154 155 156 157 158 159 160
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.

161
You could argue that the log entry for a file should contain the
Stefan Monnier's avatar
Stefan Monnier committed
162 163
full ChangeLog paragraph mentioning the change to the file, even though
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
Stefan Monnier's avatar
Stefan Monnier committed
165 166
variable is set to t.

167
On the other hand, you could argue that the log entry for a change
Stefan Monnier's avatar
Stefan Monnier committed
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
Stefan Monnier's avatar
Stefan Monnier committed
170 171 172 173 174 175 176
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)
177
(defvar log-edit-diff-function nil)
Stefan Monnier's avatar
Stefan Monnier committed
178
(defvar log-edit-listfun nil)
179

180
(defvar log-edit-parent-buffer nil)
Stefan Monnier's avatar
Stefan Monnier committed
181

182 183 184
(defvar log-edit-vc-backend nil
  "VC fileset corresponding to the current log.")

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 192
(define-obsolete-variable-alias 'vc-comment-ring-index
  'log-edit-comment-ring-index "22.1")
193 194
(defvar log-edit-comment-ring-index nil)
(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 202 203 204 205 206 207
	;; 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))

208
(defun log-edit-previous-comment (arg)
209 210 211
  "Cycle backwards through comment history.
With a numeric prefix ARG, go back ARG comments."
  (interactive "*p")
212
  (let ((len (ring-length log-edit-comment-ring)))
213 214
    (if (<= len 0)
	(progn (message "Empty comment ring") (ding))
Stefan Monnier's avatar
Stefan Monnier committed
215 216
      ;; Don't use `erase-buffer' because we don't want to `widen'.
      (delete-region (point-min) (point-max))
217 218 219
      (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)))))
220

221
(defun log-edit-next-comment (arg)
222 223 224
  "Cycle forwards through comment history.
With a numeric prefix ARG, go forward ARG comments."
  (interactive "*p")
225
  (log-edit-previous-comment (- arg)))
226

227
(defun log-edit-comment-search-backward (str &optional stride)
228 229 230 231 232
  "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
233
   (list (read-string "Comment substring: " nil nil log-edit-last-comment-match)))
234 235
  (unless stride (setq stride 1))
  (if (string= str "")
236 237
      (setq str log-edit-last-comment-match)
    (setq log-edit-last-comment-match str))
238
  (let* ((str (regexp-quote str))
239 240
	 (len (ring-length log-edit-comment-ring))
	 (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 245
    (setq log-edit-comment-ring-index n)
    (log-edit-previous-comment 0)))
246

247
(defun log-edit-comment-search-forward (str)
248 249
  "Search forwards through comment history for a substring match of STR."
  (interactive
250 251
   (list (read-string "Comment substring: " nil nil log-edit-last-comment-match)))
  (log-edit-comment-search-backward str -1))
252

253
(defun log-edit-comment-to-change-log (&optional whoami file-name)
254 255 256 257 258
  "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'.

259
This may be useful as a `log-edit-checkin-hook' to update change logs
260 261 262 263 264
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.
265
	(comment (ring-ref log-edit-comment-ring 0))
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
	;; 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
287
    (let ((paragraph-start (concat paragraph-start "\\|\\s *\\s(")))
288 289 290 291 292 293 294 295 296
      (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"))))

297
;; Compatibility with old names.
Stefan Monnier's avatar
Stefan Monnier committed
298 299 300 301 302
(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")
303

304 305 306
;;;
;;; Actual code
;;;
Stefan Monnier's avatar
Stefan Monnier committed
307

308 309 310 311 312 313 314 315 316 317 318 319 320 321
(defface log-edit-summary '((t :inherit font-lock-function-name-face))
  "Face for the summary in `log-edit-mode' buffers.")

(defface log-edit-header '((t :inherit font-lock-keyword-face))
  "Face for the headers in `log-edit-mode' buffers.")

(defface log-edit-unknown-header '((t :inherit font-lock-comment-face))
  "Face for unknown headers in `log-edit-mode' buffers.")

(defvar log-edit-headers-alist '(("Summary" . log-edit-summary)
                                 ("Fixes") ("Author"))
  "AList of known headers and the face to use to highlight them.")

(defconst log-edit-header-contents-regexp
322 323 324
  "[ \t]*\\(.*\\(\n[ \t].*\\)*\\)\n?"
  "Regular expression matching a header field.
The first subexpression is the actual text of the field.")
325

326
(defun log-edit-match-to-eoh (_limit)
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
  ;; FIXME: copied from message-match-to-eoh.
  (let ((start (point)))
    (rfc822-goto-eoh)
    ;; Typical situation: some temporary change causes the header to be
    ;; incorrect, so EOH comes earlier than intended: the last lines of the
    ;; intended headers are now not considered part of the header any more,
    ;; so they don't have the multiline property set.  When the change is
    ;; completed and the header has its correct shape again, the lack of the
    ;; multiline property means we won't rehighlight the last lines of
    ;; the header.
    (if (< (point) start)
        nil                             ;No header within start..limit.
      ;; Here we disregard LIMIT so that we may extend the area again.
      (set-match-data (list start (point)))
      (point))))

343
(defvar log-edit-font-lock-keywords
344 345
  ;; Copied/inspired by message-font-lock-keywords.
  `((log-edit-match-to-eoh
346
     (,(concat "^\\(\\([[:alpha:]-]+\\):\\)" log-edit-header-contents-regexp)
347
      (progn (goto-char (match-beginning 0)) (match-end 0)) nil
348
      (1 (if (assoc-string (match-string 2) log-edit-headers-alist t)
349 350 351
             'log-edit-header
           'log-edit-unknown-header)
         nil lax)
352
      ;; From `log-edit-header-contents-regexp':
353
      (3 (or (cdr (assoc-string (match-string 2) log-edit-headers-alist t))
354
             'log-edit-header)
355 356 357 358
         nil lax))
     ("^\n"
      (progn (goto-char (match-end 0)) (1+ (match-end 0))) nil
      (0 '(:height 0.1 :inverse-video t))))))
359

360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
(defvar log-edit-font-lock-gnu-style nil
  "If non-nil, highlight common failures to follow the GNU coding standards.")
(put 'log-edit-font-lock-gnu-style 'safe-local-variable 'booleanp)

(defconst log-edit-font-lock-gnu-keywords
    ;; Use
    ;;   * foo.el (bla, bli)
    ;;   (blo, blu): Toto.
    ;; Rather than
    ;;   * foo.el (bla, bli,
    ;;   blo, blu): Toto.
  '(("^[ \t]*\\(?:\\* .*\\)?\\(([^\n)]*,\\s-*\\)$"
     (1 '(face font-lock-warning-face
          help-echo "Continue function lists with \")\\n(\".") t))
    ;; Don't leave a lone word on a single line.
    ;;("^\\s-*\\(\\S-*[^\n:)]\\)\\s-*$" (1 font-lock-warning-face t))
    ;; Don't cut a sentence right after the first word (better to move
    ;; the sentence on the next line, then).
    ;;("[.:]\\s-+\\(\\sw+\\)\\s-*$" (1 font-lock-warning-face t))
    ;; Change Log entries should use present tense.
    ("):[ \t\n]*[[:alpha:]]+\\(ed\\)\\>"
     (1 '(face font-lock-warning-face help-echo "Use present tense.") t))
    ;; Change log entries start with a capital letter.
    ("): [a-z]" (0 '(face font-lock-warning-face help-echo "Capitalize.") t))
    ("[^[:upper:]]\\(\\. [[:upper:]]\\)"
     (1 '(face font-lock-warning-face
          help-echo "Use two spaces to end a sentence") t))
    ("^("
     (0 (let ((beg (max (point-min) (- (match-beginning 0) 2))))
          (put-text-property beg (match-end 0) 'font-lock-multiline t)
          (if (eq (char-syntax (char-after beg)) ?w)
              '(face font-lock-warning-face
                help-echo "Punctuate previous line.")))
        t))
    ))

(defun log-edit-font-lock-keywords ()
  (if log-edit-font-lock-gnu-style
      (append log-edit-font-lock-keywords
              log-edit-font-lock-gnu-keywords)
    log-edit-font-lock-keywords))

Stefan Monnier's avatar
Stefan Monnier committed
402
;;;###autoload
403
(defun log-edit (callback &optional setup params buffer mode &rest _ignore)
Stefan Monnier's avatar
Stefan Monnier committed
404
  "Setup a buffer to enter a log message.
405 406 407 408 409 410 411 412 413 414 415 416
The buffer is put in mode MODE or `log-edit-mode' if MODE is nil.
\\<log-edit-mode-map>
If SETUP is non-nil, erase the buffer and run `log-edit-hook'.
Set mark and point around the entire contents of the buffer, so
that it is easy to kill the contents of the buffer with
\\[kill-region].  Once the user is done editing the message,
invoking the command \\[log-edit-done] (`log-edit-done') will
call CALLBACK to do the actual commit.

PARAMS if non-nil is an alist of variables and buffer-local
values to give them in the Log Edit buffer.  Possible keys and
associated values:
Thien-Thi Nguyen's avatar
Thien-Thi Nguyen committed
417 418 419 420
 `log-edit-listfun' -- function taking no arguments that returns the list of
 files that are concerned by the current operation (using relative names);
 `log-edit-diff-function' -- function taking no arguments that
 displays a diff of the files concerned by the current operation.
421
 `vc-log-fileset' -- the VC fileset to be committed (if any).
Thien-Thi Nguyen's avatar
Thien-Thi Nguyen committed
422

423 424 425
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."
426 427 428 429
  (let ((parent (current-buffer)))
    (if buffer (pop-to-buffer buffer))
    (when (and log-edit-setup-invert (not (eq setup 'force)))
      (setq setup (not setup)))
430 431
    (when setup
      (erase-buffer)
432
      (insert "Summary: \nAuthor: ")
433
      (save-excursion (insert "\n\n")))
434 435 436
    (if mode
	(funcall mode)
      (log-edit-mode))
437
    (set (make-local-variable 'log-edit-callback) callback)
438 439 440 441 442 443 444
    (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))

445
    (if buffer (set (make-local-variable 'log-edit-parent-buffer) parent))
446
    (set (make-local-variable 'log-edit-initial-files) (log-edit-files))
447 448
    (when setup (run-hooks 'log-edit-hook))
    (goto-char (point-min)) (push-mark (point-max))
Deepak Goel's avatar
Deepak Goel committed
449
    (message "%s" (substitute-command-keys
450
	      "Press \\[log-edit-done] when you are done editing."))))
Stefan Monnier's avatar
Stefan Monnier committed
451 452

(define-derived-mode log-edit-mode text-mode "Log-Edit"
453 454 455 456 457 458 459
  "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).

460
\\{log-edit-mode-map}"
461
  (set (make-local-variable 'font-lock-defaults)
462
       '(log-edit-font-lock-keywords t))
463 464
  (make-local-variable 'log-edit-comment-ring-index)
  (hack-dir-local-variables-non-file-buffer))
Stefan Monnier's avatar
Stefan Monnier committed
465 466 467 468 469 470 471 472 473 474 475

(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)
476 477 478 479 480 481 482 483 484 485 486
  ;; Clean up empty headers.
  (goto-char (point-min))
  (while (looking-at (concat "^[a-z]*:" log-edit-header-contents-regexp))
    (let ((beg (match-beginning 0)))
      (goto-char (match-end 0))
      (if (string-match "\\`[ \n\t]*\\'" (match-string 1))
          (delete-region beg (point)))))
  ;; Get rid of leading empty lines.
  (goto-char (point-min))
  (when (looking-at "\\([ \t]*\n\\)+")
    (delete-region (match-beginning 0) (match-end 0)))
487 488 489 490 491 492
  ;; 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
493 494
  (if (and (> (point-max) (point-min))
	   (/= (char-before (point-max)) ?\n)
Stefan Monnier's avatar
Stefan Monnier committed
495 496 497 498 499 500 501 502
	   (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)))
503
  (let ((comment (buffer-string)))
504 505 506
    (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
507 508 509 510 511 512
  (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)
513
	       (not (y-or-n-p "Really commit? "))))
Stefan Monnier's avatar
Stefan Monnier committed
514 515 516 517
	(progn (when (not win) (log-edit-hide-buf))
	       (message "Oh, well!  Later maybe?"))
      (run-hooks 'log-edit-done-hook)
      (log-edit-hide-buf)
518 519
      (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
520 521 522 523 524 525 526 527 528 529 530
      (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
531
    (message "%s"
Stefan Monnier's avatar
Stefan Monnier committed
532 533 534
     (substitute-command-keys
      "Type `\\[log-edit-done]' to finish commit.  Try `\\[describe-function] log-edit-done' for more help."))))

535 536 537 538 539 540 541
(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
542 543
  (save-excursion
    (let ((common (point-max)))
544
      (rfc822-goto-eoh)
Stefan Monnier's avatar
Stefan Monnier committed
545 546 547 548
      (while (< (point) (point-max))
        (if (not (looking-at "^[ \t]*$"))
            (setq common (min common (current-indentation))))
        (forward-line 1))
549 550
      (rfc822-goto-eoh)
      (indent-rigidly (point) (point-max)
551
		      (- log-edit-common-indent common)))))
Stefan Monnier's avatar
Stefan Monnier committed
552

553 554 555
(defun log-edit-show-diff ()
  "Show the diff for the files to be committed."
  (interactive)
556 557 558
  (if (functionp log-edit-diff-function)
      (funcall log-edit-diff-function)
    (error "Diff functionality has not been setup")))
559

Stefan Monnier's avatar
Stefan Monnier committed
560 561 562 563
(defun log-edit-show-files ()
  "Show the list of files to be committed."
  (interactive)
  (let* ((files (log-edit-files))
564
	 (buf (get-buffer-create log-edit-files-buf)))
Stefan Monnier's avatar
Stefan Monnier committed
565 566 567 568
    (with-current-buffer buf
      (log-edit-hide-buf buf 'all)
      (setq buffer-read-only nil)
      (erase-buffer)
569
      (cvs-insert-strings files)
Stefan Monnier's avatar
Stefan Monnier committed
570 571 572 573 574 575 576
      (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)))))

Glenn Morris's avatar
Glenn Morris committed
577 578 579 580 581
(defun log-edit-empty-buffer-p ()
  "Return non-nil if the buffer is \"empty\"."
  (or (= (point-min) (point-max))
      (save-excursion
        (goto-char (point-min))
582
        (while (and (looking-at "^\\([a-zA-Z]+: ?\\)?$")
Glenn Morris's avatar
Glenn Morris committed
583 584 585
                    (zerop (forward-line 1))))
        (eobp))))

Stefan Monnier's avatar
Stefan Monnier committed
586
(defun log-edit-insert-cvs-template ()
Stefan Monnier's avatar
Stefan Monnier committed
587 588
  "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
589
  (interactive)
590
  (when (or (called-interactively-p 'interactive)
Glenn Morris's avatar
Glenn Morris committed
591 592 593
            (log-edit-empty-buffer-p))
    ;; Should the template take precedence over an empty Summary:,
    ;; ie should we first erase the buffer?
Stefan Monnier's avatar
Stefan Monnier committed
594
    (when (file-readable-p "CVS/Template")
Glenn Morris's avatar
Glenn Morris committed
595
      (goto-char (point-max))
Stefan Monnier's avatar
Stefan Monnier committed
596 597 598 599 600 601 602
      (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)
603
  (when (or (called-interactively-p 'interactive)
Glenn Morris's avatar
Glenn Morris committed
604
            (log-edit-empty-buffer-p))
605
    (when (file-readable-p "CVS/Root")
Glenn Morris's avatar
Glenn Morris committed
606
      (goto-char (point-max))
607 608 609
      ;; Ignore the stderr stuff, even if it's an error.
      (call-process "cvs" nil '(t nil) nil
                    "checkout" "-p" "CVSROOT/rcstemplate"))))
610

Stefan Monnier's avatar
Stefan Monnier committed
611 612 613 614 615
(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
616 617 618 619 620

(defun log-edit-add-to-changelog ()
  "Insert this log message into the appropriate ChangeLog file."
  (interactive)
  ;; Yuck!
621 622
  (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
623 624 625
  (dolist (f (log-edit-files))
    (let ((buffer-file-name (expand-file-name f)))
      (save-excursion
626
	(log-edit-comment-to-change-log)))))
Stefan Monnier's avatar
Stefan Monnier committed
627

Stefan Monnier's avatar
Stefan Monnier committed
628
(defvar log-edit-changelog-use-first nil)
629 630 631 632 633 634 635

(defvar log-edit-rewrite-fixes nil
  "Rule to rewrite bug numbers into Fixes: headers.
The value should be of the form (REGEXP . REPLACEMENT)
where REGEXP should match the expression referring to a bug number
in the text, and REPLACEMENT is an expression to pass to `replace-match'
to build the Fixes: header.")
636 637
(put 'log-edit-rewrite-fixes 'safe-local-variable
     (lambda (v) (and (stringp (car-safe v)) (stringp (cdr v)))))
638

639 640 641 642 643 644 645
(defun log-edit-add-field (field value)
  (rfc822-goto-eoh)
  (if (save-excursion (re-search-backward (concat "^" field ":\\([ \t]*\\)$")
                                          nil t))
      (replace-match (concat " " value) t t nil 1)
    (insert field ": " value "\n" (if (looking-at "\n") "" "\n"))))

Stefan Monnier's avatar
Stefan Monnier committed
646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662
(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")
663 664 665 666
  (let ((eoh (save-excursion (rfc822-goto-eoh) (point))))
    (when (<= (point) eoh)
      (goto-char eoh)
      (if (looking-at "\n") (forward-char 1))))
667 668 669 670 671 672
  (let ((author
         (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)
    ;; Add an Author: field if appropriate.
673
    (when author (log-edit-add-field "Author" author))
674 675 676 677 678 679 680 681 682
    ;; Add a Fixes: field if applicable.
    (when (consp log-edit-rewrite-fixes)
      (rfc822-goto-eoh)
      (when (re-search-forward (car log-edit-rewrite-fixes) nil t)
        (let ((start (match-beginning 0))
              (end (match-end 0))
              (fixes (match-substitute-replacement
                      (cdr log-edit-rewrite-fixes))))
          (delete-region start end)
683
          (log-edit-add-field "Fixes" fixes))))
Glenn Morris's avatar
Glenn Morris committed
684 685 686 687 688 689 690 691 692 693 694 695
    (and log-edit-strip-single-file-name
         (progn (rfc822-goto-eoh)
                (if (looking-at "\n") (forward-char 1))
                (looking-at "\\*\\s-+"))
         (let ((start (point)))
           (forward-line 1)
           (when (not (re-search-forward "^\\*\\s-+" nil t))
             (goto-char start)
             (skip-chars-forward "^():")
             (skip-chars-forward ": ")
             (delete-region start (point)))))
    (goto-char (point-min))))
Stefan Monnier's avatar
Stefan Monnier committed
696

697
;;;;
Stefan Monnier's avatar
Stefan Monnier committed
698 699
;;;; functions for getting commit message from ChangeLog a file...
;;;; Courtesy Jim Blandy
700
;;;;
Stefan Monnier's avatar
Stefan Monnier committed
701

702
(defun log-edit-narrow-changelog ()
Stefan Monnier's avatar
Stefan Monnier committed
703 704 705 706
  "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)
707
      (error "log-edit-narrow-changelog: current buffer isn't a ChangeLog"))
Stefan Monnier's avatar
Stefan Monnier committed
708 709 710 711 712 713 714 715 716 717 718 719 720

  (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))))

721
(defun log-edit-changelog-paragraph ()
Stefan Monnier's avatar
Stefan Monnier committed
722 723
  "Return the bounds of the ChangeLog paragraph containing point.
If we are between paragraphs, return the previous paragraph."
724 725 726 727 728 729 730 731 732 733
  (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)
          (point-max))))
Stefan Monnier's avatar
Stefan Monnier committed
734

735
(defun log-edit-changelog-subparagraph ()
Stefan Monnier's avatar
Stefan Monnier committed
736 737 738 739 740 741
  "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."
    (end-of-line)
    (if (search-backward "*" nil t)
        (list (progn (beginning-of-line) (point))
742
              (progn
Stefan Monnier's avatar
Stefan Monnier committed
743 744 745 746
                (forward-line 1)
                (if (re-search-forward "^[ \t]*[\n*]" nil t)
                    (match-beginning 0)
                  (point-max))))
747
    (list (point) (point))))
Stefan Monnier's avatar
Stefan Monnier committed
748

749
(defun log-edit-changelog-entry ()
Stefan Monnier's avatar
Stefan Monnier committed
750
  "Return the bounds of the ChangeLog entry containing point.
751
The variable `log-edit-changelog-full-paragraphs' decides whether an
Stefan Monnier's avatar
Stefan Monnier committed
752 753
\"entry\" is a paragraph or a subparagraph; see its documentation string
for more details."
754 755 756 757
  (save-excursion
    (if log-edit-changelog-full-paragraphs
        (log-edit-changelog-paragraph)
      (log-edit-changelog-subparagraph))))
Stefan Monnier's avatar
Stefan Monnier committed
758 759 760

(defvar user-full-name)
(defvar user-mail-address)
761 762 763

(defvar log-edit-author)                ;Dynamically scoped.

764
(defun log-edit-changelog-ours-p ()
Stefan Monnier's avatar
Stefan Monnier committed
765
  "See if ChangeLog entry at point is for the current user, today.
766
Return non-nil if it is."
Stefan Monnier's avatar
Stefan Monnier committed
767 768 769 770 771 772 773 774 775 776 777
  ;; 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"))))
778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794
    (if (null log-edit-changelog-use-first)
        (looking-at (regexp-quote (format "%s  %s  <%s>" time name mail)))
      ;; Check the author, to potentially add it as a "Author: " header.
      (when (looking-at "[^ \t]")
        (when (and (boundp 'log-edit-author)
                   (not (looking-at (format ".+  .+  <%s>"
                                            (regexp-quote mail))))
                   (looking-at ".+  \\(.+  <.+>\\)"))
          (let ((author (replace-regexp-in-string "  " " "
                                                  (match-string 1))))
            (unless (and log-edit-author
                         (string-match (regexp-quote author) log-edit-author))
              (setq log-edit-author
                    (if log-edit-author
                        (concat log-edit-author ", " author)
                      author)))))
        t))))
Stefan Monnier's avatar
Stefan Monnier committed
795

796
(defun log-edit-changelog-entries (file)
Stefan Monnier's avatar
Stefan Monnier committed
797 798
  "Return the ChangeLog entries for FILE, and the ChangeLog they came from.
The return value looks like this:
799
  (LOGBUFFER (ENTRYSTART ENTRYEND) ...)
Stefan Monnier's avatar
Stefan Monnier committed
800 801
where LOGBUFFER is the name of the ChangeLog buffer, and each
\(ENTRYSTART . ENTRYEND\) pair is a buffer region."
802 803 804 805 806 807 808 809 810 811 812 813 814
  (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
815
             ;; that memoizing which is undesired here.
816 817 818
             (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
819 820 821
      (unless (eq major-mode 'change-log-mode) (change-log-mode))
      (goto-char (point-min))
      (if (looking-at "\\s-*\n") (goto-char (match-end 0)))
822
      (if (not (log-edit-changelog-ours-p))
Stefan Monnier's avatar
Stefan Monnier committed
823 824
	  (list (current-buffer))
	(save-restriction
825
	  (log-edit-narrow-changelog)
Stefan Monnier's avatar
Stefan Monnier committed
826
	  (goto-char (point-min))
827

Stefan Monnier's avatar
Stefan Monnier committed
828 829 830 831 832 833 834 835 836 837 838
	  ;; 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)))

839
            (setq pattern (concat "\\(^\\|[^[:alnum:]]\\)"
840
                                  (regexp-quote pattern)
841 842
                                  "\\($\\|[^[:alnum:]]\\)"))

843 844 845
	    (let (texts
                  (pos (point)))
	      (while (and (not (eobp)) (re-search-forward pattern nil t))
846
		(let ((entry (log-edit-changelog-entry)))
847 848 849 850 851 852 853
                  (if (< (elt entry 1) (max (1+ pos) (point)))
                      ;; This is not relevant, actually.
                      nil
                    (push entry texts))
                  ;; Make sure we make progress.
                  (setq pos (max (1+ pos) (elt entry 1)))
		  (goto-char pos)))
Stefan Monnier's avatar
Stefan Monnier committed
854 855 856

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

857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880
(defun log-edit-changelog-insert-entries (buffer beg end &rest files)
  "Insert the text from BUFFER between BEG and END.
Rename relative filenames in the ChangeLog entry as FILES."
  (let ((opoint (point))
	(log-name (buffer-file-name buffer))
	(case-fold-search nil)
	bound)
    (insert-buffer-substring buffer beg end)
    (setq bound (point-marker))
    (when log-name
      (dolist (f files)
	(save-excursion
	  (goto-char opoint)
	  (when (re-search-forward
		 (concat "\\(^\\|[ \t]\\)\\("
			 (file-relative-name f (file-name-directory log-name))
			 "\\)[, :\n]")
		 bound t)
	    (replace-match f t t nil 2)))))
    ;; Eliminate tabs at the beginning of the line.
    (save-excursion
      (goto-char opoint)
      (while (re-search-forward "^\\(\t+\\)" bound t)
	(replace-match "")))))
Stefan Monnier's avatar
Stefan Monnier committed
881

882
(defun log-edit-insert-changelog-entries (files)
Stefan Monnier's avatar
Stefan Monnier committed
883
  "Given a list of files FILES, insert the ChangeLog entries for them."
884 885
  (let ((log-entries nil)
        (log-edit-author nil))
886 887 888
    ;; Note that any ChangeLog entry can apply to more than one file.
    ;; Here we construct a log-entries list with elements of the form
    ;;   ((LOGBUFFER ENTRYSTART ENTRYEND) FILE1 FILE2...)
Stefan Monnier's avatar
Stefan Monnier committed
889
    (dolist (file files)
890
      (let* ((entries (log-edit-changelog-entries file))
891 892 893 894 895 896 897 898 899 900 901
	     (buf (car entries))
	     key entry)
	(dolist (region (cdr entries))
	  (setq key (cons buf region))
	  (if (setq entry (assoc key log-entries))
	      (setcdr entry (append (cdr entry) (list file)))
	    (push (list key file) log-entries)))))
    ;; Now map over log-entries, and extract the strings.
    (dolist (log-entry (nreverse log-entries))
      (apply 'log-edit-changelog-insert-entries
	     (append (car log-entry) (cdr log-entry)))
902 903
      (insert "\n"))
    log-edit-author))
Stefan Monnier's avatar
Stefan Monnier committed
904

905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930
(defun log-edit-toggle-header (header value)
  "Toggle a boolean-type header in the current buffer.
If the value of HEADER is VALUE, clear it.  Otherwise, add the
header if it's not present and set it to VALUE.  Then make sure
there is an empty line after the headers.  Return t if toggled
on, otherwise nil."
  (let ((val t)
        (line (concat header ": " value "\n")))
    (save-excursion
      (save-restriction
        (rfc822-goto-eoh)
        (narrow-to-region (point-min) (point))
        (goto-char (point-min))
        (if (re-search-forward (concat "^" header ":"
                                       log-edit-header-contents-regexp)
                               nil t)
            (if (setq val (not (string= (match-string 1) value)))
                (replace-match line t t)
              (replace-match "" t t nil 1))
          (insert line)))
      (rfc822-goto-eoh)
      (delete-horizontal-space)
      (unless (looking-at "\n")
        (insert "\n")))
    val))

931 932
(defun log-edit-extract-headers (headers comment)
  "Extract headers from COMMENT to form command line arguments.
933 934 935 936 937 938 939 940 941 942
HEADERS should be an alist with elements (HEADER . CMDARG)
or (HEADER . FUNCTION) associating headers to command line
options and the result is then a list of the form (MSG ARGUMENTS...)
where MSG is the remaining text from COMMENT.
FUNCTION should be a function of one argument that takes the
header value and returns the list of strings to be appended to
ARGUMENTS.  CMDARG will be added to ARGUMENTS followed by the
header value.  If \"Summary\" is not in HEADERS, then the
\"Summary\" header is extracted anyway and put back as the first
line of MSG."
943 944 945 946 947 948 949 950 951 952 953 954 955 956 957
  (with-temp-buffer
    (insert comment)
    (rfc822-goto-eoh)
    (narrow-to-region (point-min) (point))
    (let ((case-fold-search t)
          (summary ())
          (res ()))
      (dolist (header (if (assoc "Summary" headers) headers
                        (cons '("Summary" . t) headers)))
        (goto-char (point-min))
        (while (re-search-forward (concat "^" (car header)
                                          ":" log-edit-header-contents-regexp)
                                  nil t)
          (if (eq t (cdr header))
              (setq summary (match-string 1))
958 959 960 961
            (if (functionp (cdr header))
                (setq res (nconc res (funcall (cdr header) (match-string 1))))
              (push (match-string 1) res)
              (push (or (cdr header) (car header)) res)))
962 963 964
          (replace-match "" t t)))
      ;; Remove header separator if the header is empty.
      (widen)
965
      (goto-char (point-min))
966 967 968 969
      (when (looking-at "\\([ \t]*\n\\)+")
        (delete-region (match-beginning 0) (match-end 0)))
      (if summary (insert summary "\n"))
      (cons (buffer-string) res))))
970

Stefan Monnier's avatar
Stefan Monnier committed
971
(provide 'log-edit)
972

Stefan Monnier's avatar
Stefan Monnier committed
973
;;; log-edit.el ends here