db386232fa82b3c6c40641c4e551391920307e9a
[gnus] / lisp / message.el
1 ;;; message.el --- composing mail and news messages
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: mail, news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;; This mode provides mail-sending facilities from within Emacs.  It
28 ;; consists mainly of large chunks of code from the sendmail.el,
29 ;; gnus-msg.el and rnewspost.el files.
30
31 ;;; Code:
32
33 (eval-when-compile (require 'cl))
34 (require 'mailheader)
35 (require 'nnheader)
36 (require 'easymenu)
37 (if (string-match "XEmacs\\|Lucid" emacs-version)
38     (require 'mail-abbrevs)
39   (require 'mailabbrev))
40 (require 'mail-parse)
41 (require 'mm-bodies)
42 (require 'mm-encode)
43 (require 'mml)
44
45 (defgroup message '((user-mail-address custom-variable)
46                     (user-full-name custom-variable))
47   "Mail and news message composing."
48   :link '(custom-manual "(message)Top")
49   :group 'mail
50   :group 'news)
51
52 (put 'user-mail-address 'custom-type 'string)
53 (put 'user-full-name 'custom-type 'string)
54
55 (defgroup message-various nil
56   "Various Message Variables"
57   :link '(custom-manual "(message)Various Message Variables")
58   :group 'message)
59
60 (defgroup message-buffers nil
61   "Message Buffers"
62   :link '(custom-manual "(message)Message Buffers")
63   :group 'message)
64
65 (defgroup message-sending nil
66   "Message Sending"
67   :link '(custom-manual "(message)Sending Variables")
68   :group 'message)
69
70 (defgroup message-interface nil
71   "Message Interface"
72   :link '(custom-manual "(message)Interface")
73   :group 'message)
74
75 (defgroup message-forwarding nil
76   "Message Forwarding"
77   :link '(custom-manual "(message)Forwarding")
78   :group 'message-interface)
79
80 (defgroup message-insertion nil
81   "Message Insertion"
82   :link '(custom-manual "(message)Insertion")
83   :group 'message)
84
85 (defgroup message-headers nil
86   "Message Headers"
87   :link '(custom-manual "(message)Message Headers")
88   :group 'message)
89
90 (defgroup message-news nil
91   "Composing News Messages"
92   :group 'message)
93
94 (defgroup message-mail nil
95   "Composing Mail Messages"
96   :group 'message)
97
98 (defgroup message-faces nil
99   "Faces used for message composing."
100   :group 'message
101   :group 'faces)
102
103 (defcustom message-directory "~/Mail/"
104   "*Directory from which all other mail file variables are derived."
105   :group 'message-various
106   :type 'directory)
107
108 (defcustom message-max-buffers 10
109   "*How many buffers to keep before starting to kill them off."
110   :group 'message-buffers
111   :type 'integer)
112
113 (defcustom message-send-rename-function nil
114   "Function called to rename the buffer after sending it."
115   :group 'message-buffers
116   :type 'function)
117
118 (defcustom message-fcc-handler-function 'message-output
119   "*A function called to save outgoing articles.
120 This function will be called with the name of the file to store the
121 article in.  The default function is `message-output' which saves in Unix
122 mailbox format."
123   :type '(radio (function-item message-output)
124                 (function :tag "Other"))
125   :group 'message-sending)
126
127 (defcustom message-courtesy-message
128   "The following message is a courtesy copy of an article\nthat has been posted to %s as well.\n\n"
129   "*This is inserted at the start of a mailed copy of a posted message.
130 If the string contains the format spec \"%s\", the Newsgroups
131 the article has been posted to will be inserted there.
132 If this variable is nil, no such courtesy message will be added."
133   :group 'message-sending
134   :type 'string)
135
136 (defcustom message-ignored-bounced-headers "^\\(Received\\|Return-Path\\):"
137   "*Regexp that matches headers to be removed in resent bounced mail."
138   :group 'message-interface
139   :type 'regexp)
140
141 ;;;###autoload
142 (defcustom message-from-style 'default
143   "*Specifies how \"From\" headers look.
144
145 If `nil', they contain just the return address like:
146         king@grassland.com
147 If `parens', they look like:
148         king@grassland.com (Elvis Parsley)
149 If `angles', they look like:
150         Elvis Parsley <king@grassland.com>
151
152 Otherwise, most addresses look like `angles', but they look like
153 `parens' if `angles' would need quoting and `parens' would not."
154   :type '(choice (const :tag "simple" nil)
155                  (const parens)
156                  (const angles)
157                  (const default))
158   :group 'message-headers)
159
160 (defcustom message-syntax-checks nil
161   ;; Guess this one shouldn't be easy to customize...
162   "*Controls what syntax checks should not be performed on outgoing posts.
163 To disable checking of long signatures, for instance, add
164  `(signature . disabled)' to this list.
165
166 Don't touch this variable unless you really know what you're doing.
167
168 Checks include subject-cmsg multiple-headers sendsys message-id from
169 long-lines control-chars size new-text redirected-followup signature
170 approved sender empty empty-headers message-id from subject
171 shorten-followup-to existing-newsgroups buffer-file-name unchanged
172 newsgroups."
173   :group 'message-news)
174
175 (defcustom message-required-news-headers
176   '(From Newsgroups Subject Date Message-ID
177          (optional . Organization) Lines
178          (optional . User-Agent))
179   "*Headers to be generated or prompted for when posting an article.
180 RFC977 and RFC1036 require From, Date, Newsgroups, Subject,
181 Message-ID.  Organization, Lines, In-Reply-To, Expires, and
182 User-Agent are optional.  If don't you want message to insert some
183 header, remove it from this list."
184   :group 'message-news
185   :group 'message-headers
186   :type '(repeat sexp))
187
188 (defcustom message-required-mail-headers
189   '(From Subject Date (optional . In-Reply-To) Message-ID Lines
190          (optional . User-Agent))
191   "*Headers to be generated or prompted for when mailing a message.
192 RFC822 required that From, Date, To, Subject and Message-ID be
193 included.  Organization, Lines and User-Agent are optional."
194   :group 'message-mail
195   :group 'message-headers
196   :type '(repeat sexp))
197
198 (defcustom message-deletable-headers '(Message-ID Date Lines)
199   "Headers to be deleted if they already exist and were generated by message previously."
200   :group 'message-headers
201   :type 'sexp)
202
203 (defcustom message-ignored-news-headers
204   "^NNTP-Posting-Host:\\|^Xref:\\|^[BGF]cc:\\|^Resent-Fcc:"
205   "*Regexp of headers to be removed unconditionally before posting."
206   :group 'message-news
207   :group 'message-headers
208   :type 'regexp)
209
210 (defcustom message-ignored-mail-headers "^[GF]cc:\\|^Resent-Fcc:\\|^Xref:"
211   "*Regexp of headers to be removed unconditionally before mailing."
212   :group 'message-mail
213   :group 'message-headers
214   :type 'regexp)
215
216 (defcustom message-ignored-supersedes-headers "^Path:\\|^Date\\|^NNTP-Posting-Host:\\|^Xref:\\|^Lines:\\|^Received:\\|^X-From-Line:\\|^X-Trace:\\|^X-Complaints-To:\\|Return-Path:\\|^Supersedes:\\|^NNTP-Posting-Date:\\|^X-Trace:\\|^X-Complaints-To:"
217   "*Header lines matching this regexp will be deleted before posting.
218 It's best to delete old Path and Date headers before posting to avoid
219 any confusion."
220   :group 'message-interface
221   :type 'regexp)
222
223 (defcustom message-subject-re-regexp "^[ \t]*\\([Rr][Ee]:[ \t]*\\)*[ \t]*"
224   "*Regexp matching \"Re: \" in the subject line."
225   :group 'message-various
226   :type 'regexp)
227
228 ;;;###autoload
229 (defcustom message-signature-separator "^-- *$"
230   "Regexp matching the signature separator."
231   :type 'regexp
232   :group 'message-various)
233
234 (defcustom message-elide-ellipsis "\n[...]\n\n"
235   "*The string which is inserted for elided text."
236   :type 'string
237   :group 'message-various)
238
239 (defcustom message-interactive nil
240   "Non-nil means when sending a message wait for and display errors.
241 nil means let mailer mail back a message to report errors."
242   :group 'message-sending
243   :group 'message-mail
244   :type 'boolean)
245
246 (defcustom message-generate-new-buffers 'unique
247   "*Non-nil means that a new message buffer will be created whenever `message-setup' is called.
248 If this is a function, call that function with three parameters:  The type,
249 the to address and the group name.  (Any of these may be nil.)  The function
250 should return the new buffer name."
251   :group 'message-buffers
252   :type '(choice (const :tag "off" nil)
253                  (const :tag "unique" unique)
254                  (const :tag "unsent" unsent)
255                  (function fun)))
256
257 (defcustom message-kill-buffer-on-exit nil
258   "*Non-nil means that the message buffer will be killed after sending a message."
259   :group 'message-buffers
260   :type 'boolean)
261
262 (defvar gnus-local-organization)
263 (defcustom message-user-organization
264   (or (and (boundp 'gnus-local-organization)
265            (stringp gnus-local-organization)
266            gnus-local-organization)
267       (getenv "ORGANIZATION")
268       t)
269   "*String to be used as an Organization header.
270 If t, use `message-user-organization-file'."
271   :group 'message-headers
272   :type '(choice string
273                  (const :tag "consult file" t)))
274
275 ;;;###autoload
276 (defcustom message-user-organization-file "/usr/lib/news/organization"
277   "*Local news organization file."
278   :type 'file
279   :group 'message-headers)
280
281 (defcustom message-make-forward-subject-function
282   'message-forward-subject-author-subject
283   "*A list of functions that are called to generate a subject header for forwarded messages.
284 The subject generated by the previous function is passed into each
285 successive function.
286
287 The provided functions are:
288
289 * message-forward-subject-author-subject (Source of article (author or
290       newsgroup)), in brackets followed by the subject
291 * message-forward-subject-fwd (Subject of article with 'Fwd:' prepended
292       to it."
293   :group 'message-forwarding
294   :type '(radio (function-item message-forward-subject-author-subject)
295                 (function-item message-forward-subject-fwd)))
296
297 (defcustom message-forward-as-mime t
298   "*If non-nil, forward messages as an inline/rfc822 MIME section.  Otherwise, directly inline the old message in the forwarded message."
299   :group 'message-forwarding
300   :type 'boolean)
301
302 (defcustom message-forward-show-mml t
303   "*If non-nil, forward messages are shown as mml.  Otherwise, forward messages are unchanged."
304   :group 'message-forwarding
305   :type 'boolean)
306
307 (defcustom message-forward-before-signature t
308   "*If non-nil, put forwarded message before signature, else after."
309   :group 'message-forwarding
310   :type 'boolean)
311
312 (defcustom message-wash-forwarded-subjects nil
313   "*If non-nil, try to remove as much old cruft as possible from the subject of messages before generating the new subject of a forward."
314   :group 'message-forwarding
315   :type 'boolean)
316
317 (defcustom message-ignored-resent-headers "^Return-receipt\\|^X-Gnus\\|^Gnus-Warning:"
318   "*All headers that match this regexp will be deleted when resending a message."
319   :group 'message-interface
320   :type 'regexp)
321
322 (defcustom message-forward-ignored-headers "Content-Transfer-Encoding"
323   "*All headers that match this regexp will be deleted when forwarding a message."
324   :group 'message-forwarding
325   :type '(choice (const :tag "None" nil)
326                  regexp))
327
328 (defcustom message-ignored-cited-headers "."
329   "*Delete these headers from the messages you yank."
330   :group 'message-insertion
331   :type 'regexp)
332
333 (defcustom message-cancel-message "I am canceling my own article.\n"
334   "Message to be inserted in the cancel message."
335   :group 'message-interface
336   :type 'string)
337
338 ;; Useful to set in site-init.el
339 ;;;###autoload
340 (defcustom message-send-mail-function 'message-send-mail-with-sendmail
341   "Function to call to send the current buffer as mail.
342 The headers should be delimited by a line whose contents match the
343 variable `mail-header-separator'.
344
345 Valid values include `message-send-mail-with-sendmail' (the default),
346 `message-send-mail-with-mh', `message-send-mail-with-qmail' and
347 `smtpmail-send-it'."
348   :type '(radio (function-item message-send-mail-with-sendmail)
349                 (function-item message-send-mail-with-mh)
350                 (function-item message-send-mail-with-qmail)
351                 (function-item smtpmail-send-it)
352                 (function :tag "Other"))
353   :group 'message-sending
354   :group 'message-mail)
355
356 (defcustom message-send-news-function 'message-send-news
357   "Function to call to send the current buffer as news.
358 The headers should be delimited by a line whose contents match the
359 variable `mail-header-separator'."
360   :group 'message-sending
361   :group 'message-news
362   :type 'function)
363
364 (defcustom message-reply-to-function nil
365   "Function that should return a list of headers.
366 This function should pick out addresses from the To, Cc, and From headers
367 and respond with new To and Cc headers."
368   :group 'message-interface
369   :type 'function)
370
371 (defcustom message-wide-reply-to-function nil
372   "Function that should return a list of headers.
373 This function should pick out addresses from the To, Cc, and From headers
374 and respond with new To and Cc headers."
375   :group 'message-interface
376   :type 'function)
377
378 (defcustom message-followup-to-function nil
379   "Function that should return a list of headers.
380 This function should pick out addresses from the To, Cc, and From headers
381 and respond with new To and Cc headers."
382   :group 'message-interface
383   :type 'function)
384
385 (defcustom message-use-followup-to 'ask
386   "*Specifies what to do with Followup-To header.
387 If nil, always ignore the header.  If it is t, use its value, but
388 query before using the \"poster\" value.  If it is the symbol `ask',
389 always query the user whether to use the value.  If it is the symbol
390 `use', always use the value."
391   :group 'message-interface
392   :type '(choice (const :tag "ignore" nil)
393                  (const use)
394                  (const ask)))
395
396 (defcustom message-sendmail-f-is-evil nil
397   "*Non-nil means that \"-f username\" should not be added to the sendmail command line.
398 Doing so would be even more evil than leaving it out."
399   :group 'message-sending
400   :type 'boolean)
401
402 ;; qmail-related stuff
403 (defcustom message-qmail-inject-program "/var/qmail/bin/qmail-inject"
404   "Location of the qmail-inject program."
405   :group 'message-sending
406   :type 'file)
407
408 (defcustom message-qmail-inject-args nil
409   "Arguments passed to qmail-inject programs.
410 This should be a list of strings, one string for each argument.
411
412 For e.g., if you wish to set the envelope sender address so that bounces
413 go to the right place or to deal with listserv's usage of that address, you
414 might set this variable to '(\"-f\" \"you@some.where\")."
415   :group 'message-sending
416   :type '(repeat string))
417
418 (defvar message-cater-to-broken-inn t
419   "Non-nil means Gnus should not fold the `References' header.
420 Folding `References' makes ancient versions of INN create incorrect
421 NOV lines.")
422
423 (defvar gnus-post-method)
424 (defvar gnus-select-method)
425 (defcustom message-post-method
426   (cond ((and (boundp 'gnus-post-method)
427               (listp gnus-post-method)
428               gnus-post-method)
429          gnus-post-method)
430         ((boundp 'gnus-select-method)
431          gnus-select-method)
432         (t '(nnspool "")))
433   "*Method used to post news.
434 Note that when posting from inside Gnus, for instance, this
435 variable isn't used."
436   :group 'message-news
437   :group 'message-sending
438   ;; This should be the `gnus-select-method' widget, but that might
439   ;; create a dependence to `gnus.el'.
440   :type 'sexp)
441
442 (defcustom message-generate-headers-first nil
443   "*If non-nil, generate all possible headers before composing."
444   :group 'message-headers
445   :type 'boolean)
446
447 (defcustom message-setup-hook nil
448   "Normal hook, run each time a new outgoing message is initialized.
449 The function `message-setup' runs this hook."
450   :group 'message-various
451   :type 'hook)
452
453 (defcustom message-cancel-hook nil
454   "Hook run when cancelling articles."
455   :group 'message-various
456   :type 'hook)
457
458 (defcustom message-signature-setup-hook nil
459   "Normal hook, run each time a new outgoing message is initialized.
460 It is run after the headers have been inserted and before
461 the signature is inserted."
462   :group 'message-various
463   :type 'hook)
464
465 (defcustom message-mode-hook nil
466   "Hook run in message mode buffers."
467   :group 'message-various
468   :type 'hook)
469
470 (defcustom message-header-hook nil
471   "Hook run in a message mode buffer narrowed to the headers."
472   :group 'message-various
473   :type 'hook)
474
475 (defcustom message-header-setup-hook nil
476   "Hook called narrowed to the headers when setting up a message buffer."
477   :group 'message-various
478   :type 'hook)
479
480 ;;;###autoload
481 (defcustom message-citation-line-function 'message-insert-citation-line
482   "*Function called to insert the \"Whomever writes:\" line."
483   :type 'function
484   :group 'message-insertion)
485
486 ;;;###autoload
487 (defcustom message-yank-prefix "> "
488   "*Prefix inserted on the lines of yanked messages."
489   :type 'string
490   :group 'message-insertion)
491
492 (defcustom message-indentation-spaces 3
493   "*Number of spaces to insert at the beginning of each cited line.
494 Used by `message-yank-original' via `message-yank-cite'."
495   :group 'message-insertion
496   :type 'integer)
497
498 ;;;###autoload
499 (defcustom message-cite-function 'message-cite-original
500   "*Function for citing an original message.
501 Predefined functions include `message-cite-original' and
502 `message-cite-original-without-signature'.
503 Note that `message-cite-original' uses `mail-citation-hook' if that is non-nil."
504   :type '(radio (function-item message-cite-original)
505                 (function-item message-cite-original-without-signature)
506                 (function-item sc-cite-original)
507                 (function :tag "Other"))
508   :group 'message-insertion)
509
510 ;;;###autoload
511 (defcustom message-indent-citation-function 'message-indent-citation
512   "*Function for modifying a citation just inserted in the mail buffer.
513 This can also be a list of functions.  Each function can find the
514 citation between (point) and (mark t).  And each function should leave
515 point and mark around the citation text as modified."
516   :type 'function
517   :group 'message-insertion)
518
519 (defvar message-abbrevs-loaded nil)
520
521 ;;;###autoload
522 (defcustom message-signature t
523   "*String to be inserted at the end of the message buffer.
524 If t, the `message-signature-file' file will be inserted instead.
525 If a function, the result from the function will be used instead.
526 If a form, the result from the form will be used instead."
527   :type 'sexp
528   :group 'message-insertion)
529
530 ;;;###autoload
531 (defcustom message-signature-file "~/.signature"
532   "*File containing the text inserted at end of message buffer."
533   :type 'file
534   :group 'message-insertion)
535
536 (defcustom message-distribution-function nil
537   "*Function called to return a Distribution header."
538   :group 'message-news
539   :group 'message-headers
540   :type 'function)
541
542 (defcustom message-expires 14
543   "Number of days before your article expires."
544   :group 'message-news
545   :group 'message-headers
546   :link '(custom-manual "(message)News Headers")
547   :type 'integer)
548
549 (defcustom message-user-path nil
550   "If nil, use the NNTP server name in the Path header.
551 If stringp, use this; if non-nil, use no host name (user name only)."
552   :group 'message-news
553   :group 'message-headers
554   :link '(custom-manual "(message)News Headers")
555   :type '(choice (const :tag "nntp" nil)
556                  (string :tag "name")
557                  (sexp :tag "none" :format "%t" t)))
558
559 (defvar message-reply-buffer nil)
560 (defvar message-reply-headers nil)
561 (defvar message-newsreader nil)
562 (defvar message-mailer nil)
563 (defvar message-sent-message-via nil)
564 (defvar message-checksum nil)
565 (defvar message-send-actions nil
566   "A list of actions to be performed upon successful sending of a message.")
567 (defvar message-exit-actions nil
568   "A list of actions to be performed upon exiting after sending a message.")
569 (defvar message-kill-actions nil
570   "A list of actions to be performed before killing a message buffer.")
571 (defvar message-postpone-actions nil
572   "A list of actions to be performed after postponing a message.")
573
574 (define-widget 'message-header-lines 'text
575   "All header lines must be LFD terminated."
576   :format "%t:%n%v"
577   :valid-regexp "^\\'"
578   :error "All header lines must be newline terminated")
579
580 (defcustom message-default-headers ""
581   "*A string containing header lines to be inserted in outgoing messages.
582 It is inserted before you edit the message, so you can edit or delete
583 these lines."
584   :group 'message-headers
585   :type 'message-header-lines)
586
587 (defcustom message-default-mail-headers ""
588   "*A string of header lines to be inserted in outgoing mails."
589   :group 'message-headers
590   :group 'message-mail
591   :type 'message-header-lines)
592
593 (defcustom message-default-news-headers ""
594   "*A string of header lines to be inserted in outgoing news articles."
595   :group 'message-headers
596   :group 'message-news
597   :type 'message-header-lines)
598
599 ;; Note: could use /usr/ucb/mail instead of sendmail;
600 ;; options -t, and -v if not interactive.
601 (defcustom message-mailer-swallows-blank-line
602   (if (and (string-match "sparc-sun-sunos\\(\\'\\|[^5]\\)"
603                          system-configuration)
604            (file-readable-p "/etc/sendmail.cf")
605            (let ((buffer (get-buffer-create " *temp*")))
606              (unwind-protect
607                  (save-excursion
608                    (set-buffer buffer)
609                    (insert-file-contents "/etc/sendmail.cf")
610                    (goto-char (point-min))
611                    (let ((case-fold-search nil))
612                      (re-search-forward "^OR\\>" nil t)))
613                (kill-buffer buffer))))
614       ;; According to RFC822, "The field-name must be composed of printable
615       ;; ASCII characters (i. e., characters that have decimal values between
616       ;; 33 and 126, except colon)", i. e., any chars except ctl chars,
617       ;; space, or colon.
618       '(looking-at "[ \t]\\|[][!\"#$%&'()*+,-./0-9;<=>?@A-Z\\\\^_`a-z{|}~]+:"))
619   "*Set this non-nil if the system's mailer runs the header and body together.
620 \(This problem exists on Sunos 4 when sendmail is run in remote mode.)
621 The value should be an expression to test whether the problem will
622 actually occur."
623   :group 'message-sending
624   :type 'sexp)
625
626 ;; Ignore errors in case this is used in Emacs 19.
627 ;; Don't use ignore-errors because this is copied into loaddefs.el.
628 ;;;###autoload
629 (ignore-errors
630   (define-mail-user-agent 'message-user-agent
631     'message-mail 'message-send-and-exit
632     'message-kill-buffer 'message-send-hook))
633
634 (defvar message-mh-deletable-headers '(Message-ID Date Lines Sender)
635   "If non-nil, delete the deletable headers before feeding to mh.")
636
637 (defvar message-send-method-alist
638   '((news message-news-p message-send-via-news)
639     (mail message-mail-p message-send-via-mail))
640   "Alist of ways to send outgoing messages.
641 Each element has the form
642
643   \(TYPE PREDICATE FUNCTION)
644
645 where TYPE is a symbol that names the method; PREDICATE is a function
646 called without any parameters to determine whether the message is
647 a message of type TYPE; and FUNCTION is a function to be called if
648 PREDICATE returns non-nil.  FUNCTION is called with one parameter --
649 the prefix.")
650
651 (defvar message-mail-alias-type 'abbrev
652   "*What alias expansion type to use in Message buffers.
653 The default is `abbrev', which uses mailabbrev.  nil switches
654 mail aliases off.")
655
656 (defcustom message-auto-save-directory
657   (nnheader-concat message-directory "drafts/")
658   "*Directory where Message auto-saves buffers if Gnus isn't running.
659 If nil, Message won't auto-save."
660   :group 'message-buffers
661   :type 'directory)
662
663 (defcustom message-buffer-naming-style 'unique
664   "*The way new message buffers are named.
665 Valid valued are `unique' and `unsent'."
666   :group 'message-buffers
667   :type '(choice (const :tag "unique" unique)
668                  (const :tag "unsent" unsent)))
669
670 (defcustom message-default-charset nil
671   "Default charset used in non-MULE XEmacsen."
672   :group 'message
673   :type 'symbol)
674
675 (defcustom message-dont-reply-to-names rmail-dont-reply-to-names
676   "*A regexp specifying names to prune when doing wide replies.
677 A value of nil means exclude your own name only."
678   :group 'message
679   :type '(choice (const :tag "Yourself" nil)
680                  regexp))
681
682 ;;; Internal variables.
683 ;;; Well, not really internal.
684
685 (defvar message-mode-syntax-table
686   (let ((table (copy-syntax-table text-mode-syntax-table)))
687     (modify-syntax-entry ?% ". " table)
688     (modify-syntax-entry ?> ". " table)
689     (modify-syntax-entry ?< ". " table)
690     table)
691   "Syntax table used while in Message mode.")
692
693 (defvar message-mode-abbrev-table text-mode-abbrev-table
694   "Abbrev table used in Message mode buffers.
695 Defaults to `text-mode-abbrev-table'.")
696 (defgroup message-headers nil
697   "Message headers."
698   :link '(custom-manual "(message)Variables")
699   :group 'message)
700
701 (defface message-header-to-face
702   '((((class color)
703       (background dark))
704      (:foreground "green2" :bold t))
705     (((class color)
706       (background light))
707      (:foreground "MidnightBlue" :bold t))
708     (t
709      (:bold t :italic t)))
710   "Face used for displaying From headers."
711   :group 'message-faces)
712
713 (defface message-header-cc-face
714   '((((class color)
715       (background dark))
716      (:foreground "green4" :bold t))
717     (((class color)
718       (background light))
719      (:foreground "MidnightBlue"))
720     (t
721      (:bold t)))
722   "Face used for displaying Cc headers."
723   :group 'message-faces)
724
725 (defface message-header-subject-face
726   '((((class color)
727       (background dark))
728      (:foreground "green3"))
729     (((class color)
730       (background light))
731      (:foreground "navy blue" :bold t))
732     (t
733      (:bold t)))
734   "Face used for displaying subject headers."
735   :group 'message-faces)
736
737 (defface message-header-newsgroups-face
738   '((((class color)
739       (background dark))
740      (:foreground "yellow" :bold t :italic t))
741     (((class color)
742       (background light))
743      (:foreground "blue4" :bold t :italic t))
744     (t
745      (:bold t :italic t)))
746   "Face used for displaying newsgroups headers."
747   :group 'message-faces)
748
749 (defface message-header-other-face
750   '((((class color)
751       (background dark))
752      (:foreground "#b00000"))
753     (((class color)
754       (background light))
755      (:foreground "steel blue"))
756     (t
757      (:bold t :italic t)))
758   "Face used for displaying newsgroups headers."
759   :group 'message-faces)
760
761 (defface message-header-name-face
762   '((((class color)
763       (background dark))
764      (:foreground "DarkGreen"))
765     (((class color)
766       (background light))
767      (:foreground "cornflower blue"))
768     (t
769      (:bold t)))
770   "Face used for displaying header names."
771   :group 'message-faces)
772
773 (defface message-header-xheader-face
774   '((((class color)
775       (background dark))
776      (:foreground "blue"))
777     (((class color)
778       (background light))
779      (:foreground "blue"))
780     (t
781      (:bold t)))
782   "Face used for displaying X-Header headers."
783   :group 'message-faces)
784
785 (defface message-separator-face
786   '((((class color)
787       (background dark))
788      (:foreground "blue3"))
789     (((class color)
790       (background light))
791      (:foreground "brown"))
792     (t
793      (:bold t)))
794   "Face used for displaying the separator."
795   :group 'message-faces)
796
797 (defface message-cited-text-face
798   '((((class color)
799       (background dark))
800      (:foreground "red"))
801     (((class color)
802       (background light))
803      (:foreground "red"))
804     (t
805      (:bold t)))
806   "Face used for displaying cited text names."
807   :group 'message-faces)
808
809 (defface message-mml-face
810   '((((class color)
811       (background dark))
812      (:foreground "ForestGreen"))
813     (((class color)
814       (background light))
815      (:foreground "ForestGreen"))
816     (t
817      (:bold t)))
818   "Face used for displaying MML."
819   :group 'message-faces)
820
821 (defvar message-font-lock-keywords
822   (let* ((cite-prefix "A-Za-z")
823          (cite-suffix (concat cite-prefix "0-9_.@-"))
824          (content "[ \t]*\\(.+\\(\n[ \t].*\\)*\\)"))
825     `((,(concat "^\\([Tt]o:\\)" content)
826        (1 'message-header-name-face)
827        (2 'message-header-to-face nil t))
828       (,(concat "^\\(^[GBF]?[Cc][Cc]:\\|^[Rr]eply-[Tt]o:\\)" content)
829        (1 'message-header-name-face)
830        (2 'message-header-cc-face nil t))
831       (,(concat "^\\([Ss]ubject:\\)" content)
832        (1 'message-header-name-face)
833        (2 'message-header-subject-face nil t))
834       (,(concat "^\\([Nn]ewsgroups:\\|Followup-[Tt]o:\\)" content)
835        (1 'message-header-name-face)
836        (2 'message-header-newsgroups-face nil t))
837       (,(concat "^\\([A-Z][^: \n\t]+:\\)" content)
838        (1 'message-header-name-face)
839        (2 'message-header-other-face nil t))
840       (,(concat "^\\(X-[A-Za-z0-9-]+\\|In-Reply-To\\):" content)
841        (1 'message-header-name-face)
842        (2 'message-header-name-face))
843       ,@(if (and mail-header-separator
844                  (not (equal mail-header-separator "")))
845             `((,(concat "^\\(" (regexp-quote mail-header-separator) "\\)$")
846                1 'message-separator-face))
847           nil)
848       (,(concat "^[ \t]*"
849                 "\\([" cite-prefix "]+[" cite-suffix "]*\\)?"
850                 "[:>|}].*")
851        (0 'message-cited-text-face))
852       ("<#/?\\(multipart\\|part\\|external\\|mml\\).*>"
853        (0 'message-mml-face))))
854   "Additional expressions to highlight in Message mode.")
855
856 ;; XEmacs does it like this.  For Emacs, we have to set the
857 ;; `font-lock-defaults' buffer-local variable.
858 (put 'message-mode 'font-lock-defaults '(message-font-lock-keywords t))
859
860 (defvar message-face-alist
861   '((bold . bold-region)
862     (underline . underline-region)
863     (default . (lambda (b e)
864                  (unbold-region b e)
865                  (ununderline-region b e))))
866   "Alist of mail and news faces for facemenu.
867 The cdr of ech entry is a function for applying the face to a region.")
868
869 (defcustom message-send-hook nil
870   "Hook run before sending messages."
871   :group 'message-various
872   :options '(ispell-message)
873   :type 'hook)
874
875 (defcustom message-send-mail-hook nil
876   "Hook run before sending mail messages."
877   :group 'message-various
878   :type 'hook)
879
880 (defcustom message-send-news-hook nil
881   "Hook run before sending news messages."
882   :group 'message-various
883   :type 'hook)
884
885 (defcustom message-sent-hook nil
886   "Hook run after sending messages."
887   :group 'message-various
888   :type 'hook)
889
890 (defvar message-send-coding-system 'binary
891   "Coding system to encode outgoing mail.")
892
893 (defvar message-draft-coding-system
894   mm-auto-save-coding-system
895   "Coding system to compose mail.")
896
897 (defcustom message-send-mail-partially-limit 1000000
898   "The limitation of messages sent as message/partial.
899 The lower bound of message size in characters, beyond which the message 
900 should be sent in several parts. If it is nil, the size is unlimited."
901   :group 'message-buffers
902   :type '(choice (const :tag "unlimited" nil)
903                  (integer 1000000)))
904
905 ;;; Internal variables.
906
907 (defvar message-buffer-list nil)
908 (defvar message-this-is-news nil)
909 (defvar message-this-is-mail nil)
910 (defvar message-draft-article nil)
911 (defvar message-mime-part nil)
912 (defvar message-posting-charset nil)
913
914 ;; Byte-compiler warning
915 (defvar gnus-active-hashtb)
916 (defvar gnus-read-active-file)
917
918 ;;; Regexp matching the delimiter of messages in UNIX mail format
919 ;;; (UNIX From lines), minus the initial ^.  It should be a copy
920 ;;; of rmail.el's rmail-unix-mail-delimiter.
921 (defvar message-unix-mail-delimiter
922   (let ((time-zone-regexp
923          (concat "\\([A-Z]?[A-Z]?[A-Z][A-Z]\\( DST\\)?"
924                  "\\|[-+]?[0-9][0-9][0-9][0-9]"
925                  "\\|"
926                  "\\) *")))
927     (concat
928      "From "
929
930      ;; Many things can happen to an RFC 822 mailbox before it is put into
931      ;; a `From' line.  The leading phrase can be stripped, e.g.
932      ;; `Joe <@w.x:joe@y.z>' -> `<@w.x:joe@y.z>'.  The <> can be stripped, e.g.
933      ;; `<@x.y:joe@y.z>' -> `@x.y:joe@y.z'.  Everything starting with a CRLF
934      ;; can be removed, e.g.
935      ;;         From: joe@y.z (Joe      K
936      ;;                 User)
937      ;; can yield `From joe@y.z (Joe    K Fri Mar 22 08:11:15 1996', and
938      ;;         From: Joe User
939      ;;                 <joe@y.z>
940      ;; can yield `From Joe User Fri Mar 22 08:11:15 1996'.
941      ;; The mailbox can be removed or be replaced by white space, e.g.
942      ;;         From: "Joe User"{space}{tab}
943      ;;                 <joe@y.z>
944      ;; can yield `From {space}{tab} Fri Mar 22 08:11:15 1996',
945      ;; where {space} and {tab} represent the Ascii space and tab characters.
946      ;; We want to match the results of any of these manglings.
947      ;; The following regexp rejects names whose first characters are
948      ;; obviously bogus, but after that anything goes.
949      "\\([^\0-\b\n-\r\^?].*\\)? "
950
951      ;; The time the message was sent.
952      "\\([^\0-\r \^?]+\\) +"            ; day of the week
953      "\\([^\0-\r \^?]+\\) +"            ; month
954      "\\([0-3]?[0-9]\\) +"              ; day of month
955      "\\([0-2][0-9]:[0-5][0-9]\\(:[0-6][0-9]\\)?\\) *" ; time of day
956
957      ;; Perhaps a time zone, specified by an abbreviation, or by a
958      ;; numeric offset.
959      time-zone-regexp
960
961      ;; The year.
962      " \\([0-9][0-9]+\\) *"
963
964      ;; On some systems the time zone can appear after the year, too.
965      time-zone-regexp
966
967      ;; Old uucp cruft.
968      "\\(remote from .*\\)?"
969
970      "\n"))
971   "Regexp matching the delimiter of messages in UNIX mail format.")
972
973 (defvar message-unsent-separator
974   (concat "^ *---+ +Unsent message follows +---+ *$\\|"
975           "^ *---+ +Returned message +---+ *$\\|"
976           "^Start of returned message$\\|"
977           "^ *---+ +Original message +---+ *$\\|"
978           "^ *--+ +begin message +--+ *$\\|"
979           "^ *---+ +Original message follows +---+ *$\\|"
980           "^ *---+ +Undelivered message follows +---+ *$\\|"
981           "^|? *---+ +Message text follows: +---+ *|?$")
982   "A regexp that matches the separator before the text of a failed message.")
983
984 (defvar message-header-format-alist
985   `((Newsgroups)
986     (To . message-fill-address)
987     (Cc . message-fill-address)
988     (Subject)
989     (In-Reply-To)
990     (Fcc)
991     (Bcc)
992     (Date)
993     (Organization)
994     (Distribution)
995     (Lines)
996     (Expires)
997     (Message-ID)
998     (References . message-shorten-references)
999     (User-Agent))
1000   "Alist used for formatting headers.")
1001
1002 (eval-and-compile
1003   (autoload 'message-setup-toolbar "messagexmas")
1004   (autoload 'mh-new-draft-name "mh-comp")
1005   (autoload 'mh-send-letter "mh-comp")
1006   (autoload 'gnus-point-at-eol "gnus-util")
1007   (autoload 'gnus-point-at-bol "gnus-util")
1008   (autoload 'gnus-output-to-mail "gnus-util")
1009   (autoload 'mail-abbrev-in-expansion-header-p "mailabbrev")
1010   (autoload 'nndraft-request-associate-buffer "nndraft")
1011   (autoload 'nndraft-request-expire-articles "nndraft")
1012   (autoload 'gnus-open-server "gnus-int")
1013   (autoload 'gnus-request-post "gnus-int")
1014   (autoload 'gnus-alive-p "gnus-util")
1015   (autoload 'rmail-output "rmail"))
1016
1017 \f
1018
1019 ;;;
1020 ;;; Utility functions.
1021 ;;;
1022
1023 (defmacro message-y-or-n-p (question show &rest text)
1024   "Ask QUESTION, displaying the rest of the arguments in a temp. buffer if SHOW"
1025   `(message-talkative-question 'y-or-n-p ,question ,show ,@text))
1026
1027 ;; Delete the current line (and the next N lines.);
1028 (defmacro message-delete-line (&optional n)
1029   `(delete-region (progn (beginning-of-line) (point))
1030                   (progn (forward-line ,(or n 1)) (point))))
1031
1032 (defun message-tokenize-header (header &optional separator)
1033   "Split HEADER into a list of header elements.
1034 \",\" is used as the separator."
1035   (if (not header)
1036       nil
1037     (let ((regexp (format "[%s]+" (or separator ",")))
1038           (beg 1)
1039           (first t)
1040           quoted elems paren)
1041       (save-excursion
1042         (message-set-work-buffer)
1043         (insert header)
1044         (goto-char (point-min))
1045         (while (not (eobp))
1046           (if first
1047               (setq first nil)
1048             (forward-char 1))
1049           (cond ((and (> (point) beg)
1050                       (or (eobp)
1051                           (and (looking-at regexp)
1052                                (not quoted)
1053                                (not paren))))
1054                  (push (buffer-substring beg (point)) elems)
1055                  (setq beg (match-end 0)))
1056                 ((eq (char-after) ?\")
1057                  (setq quoted (not quoted)))
1058                 ((and (eq (char-after) ?\()
1059                       (not quoted))
1060                  (setq paren t))
1061                 ((and (eq (char-after) ?\))
1062                       (not quoted))
1063                  (setq paren nil))))
1064         (nreverse elems)))))
1065
1066 (defun message-mail-file-mbox-p (file)
1067   "Say whether FILE looks like a Unix mbox file."
1068   (when (and (file-exists-p file)
1069              (file-readable-p file)
1070              (file-regular-p file))
1071     (with-temp-buffer
1072       (nnheader-insert-file-contents file)
1073       (goto-char (point-min))
1074       (looking-at message-unix-mail-delimiter))))
1075
1076 (defun message-fetch-field (header &optional not-all)
1077   "The same as `mail-fetch-field', only remove all newlines."
1078   (let* ((inhibit-point-motion-hooks t)
1079          (case-fold-search t)
1080          (value (mail-fetch-field header nil (not not-all))))
1081     (when value
1082       (while (string-match "\n[\t ]+" value)
1083         (setq value (replace-match " " t t value)))
1084       ;; We remove all text props.
1085       (format "%s" value))))
1086
1087 (defun message-narrow-to-field ()
1088   "Narrow the buffer to the header on the current line."
1089   (beginning-of-line)
1090   (narrow-to-region
1091    (point)
1092    (progn
1093      (forward-line 1)
1094      (if (re-search-forward "^[^ \n\t]" nil t)
1095          (progn
1096            (beginning-of-line)
1097            (point))
1098        (point-max))))
1099   (goto-char (point-min)))
1100
1101 (defun message-add-header (&rest headers)
1102   "Add the HEADERS to the message header, skipping those already present."
1103   (while headers
1104     (let (hclean)
1105       (unless (string-match "^\\([^:]+\\):[ \t]*[^ \t]" (car headers))
1106         (error "Invalid header `%s'" (car headers)))
1107       (setq hclean (match-string 1 (car headers)))
1108       (save-restriction
1109         (message-narrow-to-headers)
1110         (unless (re-search-forward (concat "^" (regexp-quote hclean) ":") nil t)
1111           (insert (car headers) ?\n))))
1112     (setq headers (cdr headers))))
1113
1114
1115 (defun message-fetch-reply-field (header)
1116   "Fetch FIELD from the message we're replying to."
1117   (when (and message-reply-buffer
1118              (buffer-name message-reply-buffer))
1119     (save-excursion
1120       (set-buffer message-reply-buffer)
1121       (message-fetch-field header))))
1122
1123 (defun message-set-work-buffer ()
1124   (if (get-buffer " *message work*")
1125       (progn
1126         (set-buffer " *message work*")
1127         (erase-buffer))
1128     (set-buffer (get-buffer-create " *message work*"))
1129     (kill-all-local-variables)
1130     (mm-enable-multibyte)))
1131
1132 (defun message-functionp (form)
1133   "Return non-nil if FORM is funcallable."
1134   (or (and (symbolp form) (fboundp form))
1135       (and (listp form) (eq (car form) 'lambda))
1136       (byte-code-function-p form)))
1137
1138 (defun message-strip-subject-re (subject)
1139   "Remove \"Re:\" from subject lines."
1140   (if (string-match message-subject-re-regexp subject)
1141       (substring subject (match-end 0))
1142     subject))
1143
1144 (defun message-remove-header (header &optional is-regexp first reverse)
1145   "Remove HEADER in the narrowed buffer.
1146 If REGEXP, HEADER is a regular expression.
1147 If FIRST, only remove the first instance of the header.
1148 Return the number of headers removed."
1149   (goto-char (point-min))
1150   (let ((regexp (if is-regexp header (concat "^" (regexp-quote header) ":")))
1151         (number 0)
1152         (case-fold-search t)
1153         last)
1154     (while (and (not (eobp))
1155                 (not last))
1156       (if (if reverse
1157               (not (looking-at regexp))
1158             (looking-at regexp))
1159           (progn
1160             (incf number)
1161             (when first
1162               (setq last t))
1163             (delete-region
1164              (point)
1165              ;; There might be a continuation header, so we have to search
1166              ;; until we find a new non-continuation line.
1167              (progn
1168                (forward-line 1)
1169                (if (re-search-forward "^[^ \t]" nil t)
1170                    (goto-char (match-beginning 0))
1171                  (point-max)))))
1172         (forward-line 1)
1173         (if (re-search-forward "^[^ \t]" nil t)
1174             (goto-char (match-beginning 0))
1175           (goto-char (point-max)))))
1176     number))
1177
1178 (defun message-remove-first-header (header)
1179   "Remove the first instance of HEADER if there is more than one."
1180   (let ((count 0)
1181         (regexp (concat "^" (regexp-quote header) ":")))
1182     (save-excursion
1183       (goto-char (point-min))
1184       (while (re-search-forward regexp nil t)
1185         (incf count)))
1186     (while (> count 1)
1187       (message-remove-header header nil t)
1188       (decf count))))
1189
1190 (defun message-narrow-to-headers ()
1191   "Narrow the buffer to the head of the message."
1192   (widen)
1193   (narrow-to-region
1194    (goto-char (point-min))
1195    (if (re-search-forward
1196         (concat "^" (regexp-quote mail-header-separator) "\n") nil t)
1197        (match-beginning 0)
1198      (point-max)))
1199   (goto-char (point-min)))
1200
1201 (defun message-narrow-to-head ()
1202   "Narrow the buffer to the head of the message.
1203 Point is left at the beginning of the narrowed-to region."
1204   (widen)
1205   (narrow-to-region
1206    (goto-char (point-min))
1207    (if (search-forward "\n\n" nil 1)
1208        (1- (point))
1209      (point-max)))
1210   (goto-char (point-min)))
1211
1212 (defun message-narrow-to-headers-or-head ()
1213   "Narrow the buffer to the head of the message."
1214   (widen)
1215   (narrow-to-region
1216    (goto-char (point-min))
1217    (cond
1218     ((re-search-forward
1219       (concat "^" (regexp-quote mail-header-separator) "\n") nil t)
1220      (match-beginning 0))
1221     ((search-forward "\n\n" nil t)
1222      (1- (point)))
1223     (t
1224      (point-max))))
1225   (goto-char (point-min)))
1226
1227 (defun message-news-p ()
1228   "Say whether the current buffer contains a news message."
1229   (and (not message-this-is-mail)
1230        (or message-this-is-news
1231            (save-excursion
1232              (save-restriction
1233                (message-narrow-to-headers)
1234                (and (message-fetch-field "newsgroups")
1235                     (not (message-fetch-field "posted-to"))))))))
1236
1237 (defun message-mail-p ()
1238   "Say whether the current buffer contains a mail message."
1239   (and (not message-this-is-news)
1240        (or message-this-is-mail
1241            (save-excursion
1242              (save-restriction
1243                (message-narrow-to-headers)
1244                (or (message-fetch-field "to")
1245                    (message-fetch-field "cc")
1246                    (message-fetch-field "bcc")))))))
1247
1248 (defun message-next-header ()
1249   "Go to the beginning of the next header."
1250   (beginning-of-line)
1251   (or (eobp) (forward-char 1))
1252   (not (if (re-search-forward "^[^ \t]" nil t)
1253            (beginning-of-line)
1254          (goto-char (point-max)))))
1255
1256 (defun message-sort-headers-1 ()
1257   "Sort the buffer as headers using `message-rank' text props."
1258   (goto-char (point-min))
1259   (require 'sort)
1260   (sort-subr
1261    nil 'message-next-header
1262    (lambda ()
1263      (message-next-header)
1264      (unless (bobp)
1265        (forward-char -1)))
1266    (lambda ()
1267      (or (get-text-property (point) 'message-rank)
1268          10000))))
1269
1270 (defun message-sort-headers ()
1271   "Sort the headers of the current message according to `message-header-format-alist'."
1272   (interactive)
1273   (save-excursion
1274     (save-restriction
1275       (let ((max (1+ (length message-header-format-alist)))
1276             rank)
1277         (message-narrow-to-headers)
1278         (while (re-search-forward "^[^ \n]+:" nil t)
1279           (put-text-property
1280            (match-beginning 0) (1+ (match-beginning 0))
1281            'message-rank
1282            (if (setq rank (length (memq (assq (intern (buffer-substring
1283                                                        (match-beginning 0)
1284                                                        (1- (match-end 0))))
1285                                               message-header-format-alist)
1286                                         message-header-format-alist)))
1287                (- max rank)
1288              (1+ max)))))
1289       (message-sort-headers-1))))
1290
1291 \f
1292
1293 ;;;
1294 ;;; Message mode
1295 ;;;
1296
1297 ;;; Set up keymap.
1298
1299 (defvar message-mode-map nil)
1300
1301 (unless message-mode-map
1302   (setq message-mode-map (make-keymap))
1303   (set-keymap-parent message-mode-map text-mode-map)
1304   (define-key message-mode-map "\C-c?" 'describe-mode)
1305
1306   (define-key message-mode-map "\C-c\C-f\C-t" 'message-goto-to)
1307   (define-key message-mode-map "\C-c\C-f\C-b" 'message-goto-bcc)
1308   (define-key message-mode-map "\C-c\C-f\C-w" 'message-goto-fcc)
1309   (define-key message-mode-map "\C-c\C-f\C-c" 'message-goto-cc)
1310   (define-key message-mode-map "\C-c\C-f\C-s" 'message-goto-subject)
1311   (define-key message-mode-map "\C-c\C-f\C-r" 'message-goto-reply-to)
1312   (define-key message-mode-map "\C-c\C-f\C-n" 'message-goto-newsgroups)
1313   (define-key message-mode-map "\C-c\C-f\C-d" 'message-goto-distribution)
1314   (define-key message-mode-map "\C-c\C-f\C-f" 'message-goto-followup-to)
1315   (define-key message-mode-map "\C-c\C-f\C-k" 'message-goto-keywords)
1316   (define-key message-mode-map "\C-c\C-f\C-u" 'message-goto-summary)
1317   (define-key message-mode-map "\C-c\C-b" 'message-goto-body)
1318   (define-key message-mode-map "\C-c\C-i" 'message-goto-signature)
1319
1320   (define-key message-mode-map "\C-c\C-t" 'message-insert-to)
1321   (define-key message-mode-map "\C-c\C-n" 'message-insert-newsgroups)
1322
1323   (define-key message-mode-map "\C-c\C-y" 'message-yank-original)
1324   (define-key message-mode-map "\C-c\M-\C-y" 'message-yank-buffer)
1325   (define-key message-mode-map "\C-c\C-q" 'message-fill-yanked-message)
1326   (define-key message-mode-map "\C-c\C-w" 'message-insert-signature)
1327   (define-key message-mode-map "\C-c\M-h" 'message-insert-headers)
1328   (define-key message-mode-map "\C-c\C-r" 'message-caesar-buffer-body)
1329   (define-key message-mode-map "\C-c\C-o" 'message-sort-headers)
1330   (define-key message-mode-map "\C-c\M-r" 'message-rename-buffer)
1331
1332   (define-key message-mode-map "\C-c\C-c" 'message-send-and-exit)
1333   (define-key message-mode-map "\C-c\C-s" 'message-send)
1334   (define-key message-mode-map "\C-c\C-k" 'message-kill-buffer)
1335   (define-key message-mode-map "\C-c\C-d" 'message-dont-send)
1336
1337   (define-key message-mode-map "\C-c\C-e" 'message-elide-region)
1338   (define-key message-mode-map "\C-c\C-v" 'message-delete-not-region)
1339   (define-key message-mode-map "\C-c\C-z" 'message-kill-to-signature)
1340   (define-key message-mode-map "\M-\r" 'message-newline-and-reformat)
1341
1342   (define-key message-mode-map "\C-c\C-a" 'mml-attach-file)
1343
1344   (define-key message-mode-map "\t" 'message-tab))
1345
1346 (easy-menu-define
1347  message-mode-menu message-mode-map "Message Menu."
1348  '("Message"
1349    ["Sort Headers" message-sort-headers t]
1350    ["Yank Original" message-yank-original t]
1351    ["Fill Yanked Message" message-fill-yanked-message t]
1352    ["Insert Signature" message-insert-signature t]
1353    ["Caesar (rot13) Message" message-caesar-buffer-body t]
1354    ["Caesar (rot13) Region" message-caesar-region (mark t)]
1355    ["Elide Region" message-elide-region (mark t)]
1356    ["Delete Outside Region" message-delete-not-region (mark t)]
1357    ["Kill To Signature" message-kill-to-signature t]
1358    ["Newline and Reformat" message-newline-and-reformat t]
1359    ["Rename buffer" message-rename-buffer t]
1360    ["Spellcheck" ispell-message t]
1361    ["Attach file as MIME" mml-attach-file t]
1362    "----"
1363    ["Send Message" message-send-and-exit t]
1364    ["Abort Message" message-dont-send t]
1365    ["Kill Message" message-kill-buffer t]))
1366
1367 (easy-menu-define
1368  message-mode-field-menu message-mode-map ""
1369  '("Field"
1370    ["Fetch To" message-insert-to t]
1371    ["Fetch Newsgroups" message-insert-newsgroups t]
1372    "----"
1373    ["To" message-goto-to t]
1374    ["Subject" message-goto-subject t]
1375    ["Cc" message-goto-cc t]
1376    ["Reply-To" message-goto-reply-to t]
1377    ["Summary" message-goto-summary t]
1378    ["Keywords" message-goto-keywords t]
1379    ["Newsgroups" message-goto-newsgroups t]
1380    ["Followup-To" message-goto-followup-to t]
1381    ["Distribution" message-goto-distribution t]
1382    ["Body" message-goto-body t]
1383    ["Signature" message-goto-signature t]))
1384
1385 (defvar facemenu-add-face-function)
1386 (defvar facemenu-remove-face-function)
1387
1388 ;;;###autoload
1389 (defun message-mode ()
1390   "Major mode for editing mail and news to be sent.
1391 Like Text Mode but with these additional commands:
1392 C-c C-s  message-send (send the message)    C-c C-c  message-send-and-exit
1393 C-c C-d  Pospone sending the message        C-c C-k  Kill the message
1394 C-c C-f  move to a header field (and create it if there isn't):
1395          C-c C-f C-t  move to To        C-c C-f C-s  move to Subject
1396          C-c C-f C-c  move to Cc        C-c C-f C-b  move to Bcc
1397          C-c C-f C-w  move to Fcc       C-c C-f C-r  move to Reply-To
1398          C-c C-f C-u  move to Summary   C-c C-f C-n  move to Newsgroups
1399          C-c C-f C-k  move to Keywords  C-c C-f C-d  move to Distribution
1400          C-c C-f C-f  move to Followup-To
1401 C-c C-t  message-insert-to (add a To header to a news followup)
1402 C-c C-n  message-insert-newsgroups (add a Newsgroup header to a news reply)
1403 C-c C-b  message-goto-body (move to beginning of message text).
1404 C-c C-i  message-goto-signature (move to the beginning of the signature).
1405 C-c C-w  message-insert-signature (insert `message-signature-file' file).
1406 C-c C-y  message-yank-original (insert current message, if any).
1407 C-c C-q  message-fill-yanked-message (fill what was yanked).
1408 C-c C-e  message-elide-region (elide the text between point and mark).
1409 C-c C-v  message-delete-not-region (remove the text outside the region).
1410 C-c C-z  message-kill-to-signature (kill the text up to the signature).
1411 C-c C-r  message-caesar-buffer-body (rot13 the message body).
1412 C-c C-a  mml-attach-file (attach a file as MIME).
1413 M-RET    message-newline-and-reformat (break the line and reformat)."
1414   (interactive)
1415   (kill-all-local-variables)
1416   (set (make-local-variable 'message-reply-buffer) nil)
1417   (make-local-variable 'message-send-actions)
1418   (make-local-variable 'message-exit-actions)
1419   (make-local-variable 'message-kill-actions)
1420   (make-local-variable 'message-postpone-actions)
1421   (make-local-variable 'message-draft-article)
1422   (make-local-hook 'kill-buffer-hook)
1423   (set-syntax-table message-mode-syntax-table)
1424   (use-local-map message-mode-map)
1425   (setq local-abbrev-table message-mode-abbrev-table)
1426   (setq major-mode 'message-mode)
1427   (setq mode-name "Message")
1428   (setq buffer-offer-save t)
1429   (make-local-variable 'facemenu-add-face-function)
1430   (make-local-variable 'facemenu-remove-face-function)
1431   (setq facemenu-add-face-function
1432         (lambda (face end)
1433           (let ((face-fun (cdr (assq face message-face-alist))))
1434             (if face-fun
1435                 (funcall face-fun (point) end)
1436               (error "Face %s not configured for %s mode" face mode-name)))
1437           "")
1438         facemenu-remove-face-function t)
1439   (make-local-variable 'paragraph-separate)
1440   (make-local-variable 'paragraph-start)
1441   ;; `-- ' precedes the signature.  `-----' appears at the start of the
1442   ;; lines that delimit forwarded messages.
1443   ;; Lines containing just >= 3 dashes, perhaps after whitespace,
1444   ;; are also sometimes used and should be separators.
1445   (setq paragraph-start
1446         (concat (regexp-quote mail-header-separator)
1447                 "$\\|[ \t]*[a-z0-9A-Z]*>+[ \t]*$\\|[ \t]*$\\|"
1448                 "-- $\\|---+$\\|"
1449                 page-delimiter
1450                 ;;!!! Uhm... shurely this can't be right?
1451                 "[> " (regexp-quote message-yank-prefix) "]+$"))
1452   (setq paragraph-separate paragraph-start)
1453   (make-local-variable 'message-reply-headers)
1454   (setq message-reply-headers nil)
1455   (make-local-variable 'message-newsreader)
1456   (make-local-variable 'message-mailer)
1457   (make-local-variable 'message-post-method)
1458   (set (make-local-variable 'message-sent-message-via) nil)
1459   (set (make-local-variable 'message-checksum) nil)
1460   (set (make-local-variable 'message-mime-part) 0)
1461   ;;(when (fboundp 'mail-hist-define-keys)
1462   ;;  (mail-hist-define-keys))
1463   (when (string-match "XEmacs\\|Lucid" emacs-version)
1464     (message-setup-toolbar))
1465   (easy-menu-add message-mode-menu message-mode-map)
1466   (easy-menu-add message-mode-field-menu message-mode-map)
1467   ;; Allow mail alias things.
1468   (when (eq message-mail-alias-type 'abbrev)
1469     (if (fboundp 'mail-abbrevs-setup)
1470         (mail-abbrevs-setup)
1471       (mail-aliases-setup)))
1472   (message-set-auto-save-file-name)
1473   (unless (string-match "XEmacs" emacs-version)
1474     (set (make-local-variable 'font-lock-defaults)
1475          '(message-font-lock-keywords t)))
1476   (make-local-variable 'adaptive-fill-regexp)
1477   (setq adaptive-fill-regexp
1478         (concat "[ \t]*[-a-z0-9A-Z]*\\(>[ \t]*\\)+[ \t]*\\|" adaptive-fill-regexp))
1479   (unless (boundp 'adaptive-fill-first-line-regexp)
1480     (setq adaptive-fill-first-line-regexp nil))
1481   (make-local-variable 'adaptive-fill-first-line-regexp)
1482   (setq adaptive-fill-first-line-regexp
1483         (concat "[ \t]*[-a-z0-9A-Z]*\\(>[ \t]*\\)+[ \t]*\\|"
1484                 adaptive-fill-first-line-regexp))
1485   (make-local-variable 'auto-fill-inhibit-regexp)
1486   (setq auto-fill-inhibit-regexp "^[A-Z][^: \n\t]+:")
1487   (mm-enable-multibyte)
1488   (make-local-variable 'indent-tabs-mode) ;Turn off tabs for indentation.
1489   (setq indent-tabs-mode nil)
1490   (mml-mode)
1491   (run-hooks 'text-mode-hook 'message-mode-hook))
1492
1493 \f
1494
1495 ;;;
1496 ;;; Message mode commands
1497 ;;;
1498
1499 ;;; Movement commands
1500
1501 (defun message-goto-to ()
1502   "Move point to the To header."
1503   (interactive)
1504   (message-position-on-field "To"))
1505
1506 (defun message-goto-subject ()
1507   "Move point to the Subject header."
1508   (interactive)
1509   (message-position-on-field "Subject"))
1510
1511 (defun message-goto-cc ()
1512   "Move point to the Cc header."
1513   (interactive)
1514   (message-position-on-field "Cc" "To"))
1515
1516 (defun message-goto-bcc ()
1517   "Move point to the Bcc  header."
1518   (interactive)
1519   (message-position-on-field "Bcc" "Cc" "To"))
1520
1521 (defun message-goto-fcc ()
1522   "Move point to the Fcc header."
1523   (interactive)
1524   (message-position-on-field "Fcc" "To" "Newsgroups"))
1525
1526 (defun message-goto-reply-to ()
1527   "Move point to the Reply-To header."
1528   (interactive)
1529   (message-position-on-field "Reply-To" "Subject"))
1530
1531 (defun message-goto-newsgroups ()
1532   "Move point to the Newsgroups header."
1533   (interactive)
1534   (message-position-on-field "Newsgroups"))
1535
1536 (defun message-goto-distribution ()
1537   "Move point to the Distribution header."
1538   (interactive)
1539   (message-position-on-field "Distribution"))
1540
1541 (defun message-goto-followup-to ()
1542   "Move point to the Followup-To header."
1543   (interactive)
1544   (message-position-on-field "Followup-To" "Newsgroups"))
1545
1546 (defun message-goto-keywords ()
1547   "Move point to the Keywords header."
1548   (interactive)
1549   (message-position-on-field "Keywords" "Subject"))
1550
1551 (defun message-goto-summary ()
1552   "Move point to the Summary header."
1553   (interactive)
1554   (message-position-on-field "Summary" "Subject"))
1555
1556 (defun message-goto-body ()
1557   "Move point to the beginning of the message body."
1558   (interactive)
1559   (if (looking-at "[ \t]*\n") (expand-abbrev))
1560   (goto-char (point-min))
1561   (or (search-forward (concat "\n" mail-header-separator "\n") nil t)
1562       (search-forward "\n\n" nil t)))
1563
1564 (defun message-goto-eoh ()
1565   "Move point to the end of the headers."
1566   (interactive)
1567   (message-goto-body)
1568   (forward-line -1))
1569
1570 (defun message-goto-signature ()
1571   "Move point to the beginning of the message signature.
1572 If there is no signature in the article, go to the end and
1573 return nil."
1574   (interactive)
1575   (goto-char (point-min))
1576   (if (re-search-forward message-signature-separator nil t)
1577       (forward-line 1)
1578     (goto-char (point-max))
1579     nil))
1580
1581 \f
1582
1583 (defun message-insert-to (&optional force)
1584   "Insert a To header that points to the author of the article being replied to.
1585 If the original author requested not to be sent mail, the function signals
1586 an error.
1587 With the prefix argument FORCE, insert the header anyway."
1588   (interactive "P")
1589   (let ((co (message-fetch-reply-field "mail-copies-to")))
1590     (when (and (null force)
1591                co
1592                (or (equal (downcase co) "never")
1593                    (equal (downcase co) "nobody")))
1594       (error "The user has requested not to have copies sent via mail")))
1595   (when (and (message-position-on-field "To")
1596              (mail-fetch-field "to")
1597              (not (string-match "\\` *\\'" (mail-fetch-field "to"))))
1598     (insert ", "))
1599   (insert (or (message-fetch-reply-field "reply-to")
1600               (message-fetch-reply-field "from") "")))
1601
1602 (defun message-widen-reply ()
1603   "Widen the reply to include maximum recipients."
1604   (interactive)
1605   (let ((follow-to
1606          (and message-reply-buffer
1607               (buffer-name message-reply-buffer)
1608               (save-excursion
1609                 (set-buffer message-reply-buffer)
1610                 (message-get-reply-headers t)))))
1611     (save-excursion
1612       (save-restriction
1613         (message-narrow-to-headers)
1614         (dolist (elem follow-to)
1615           (message-remove-header (symbol-name (car elem)))
1616           (goto-char (point-min))
1617           (insert (symbol-name (car elem)) ": "
1618                   (cdr elem) "\n"))))))
1619
1620 (defun message-insert-newsgroups ()
1621   "Insert the Newsgroups header from the article being replied to."
1622   (interactive)
1623   (when (and (message-position-on-field "Newsgroups")
1624              (mail-fetch-field "newsgroups")
1625              (not (string-match "\\` *\\'" (mail-fetch-field "newsgroups"))))
1626     (insert ","))
1627   (insert (or (message-fetch-reply-field "newsgroups") "")))
1628
1629 \f
1630
1631 ;;; Various commands
1632
1633 (defun message-delete-not-region (beg end)
1634   "Delete everything in the body of the current message that is outside of the region."
1635   (interactive "r")
1636   (save-excursion
1637     (goto-char end)
1638     (delete-region (point) (if (not (message-goto-signature))
1639                                (point)
1640                              (forward-line -2)
1641                              (point)))
1642     (insert "\n")
1643     (goto-char beg)
1644     (delete-region beg (progn (message-goto-body)
1645                               (forward-line 2)
1646                               (point))))
1647   (when (message-goto-signature)
1648     (forward-line -2)))
1649
1650 (defun message-kill-to-signature ()
1651   "Deletes all text up to the signature."
1652   (interactive)
1653   (let ((point (point)))
1654     (message-goto-signature)
1655     (unless (eobp)
1656       (forward-line -2))
1657     (kill-region point (point))
1658     (unless (bolp)
1659       (insert "\n"))))
1660
1661 (defun message-newline-and-reformat ()
1662   "Insert four newlines, and then reformat if inside quoted text."
1663   (interactive)
1664   (let ((prefix "[]>»|:}+ \t]*")
1665         (supercite-thing "[-._a-zA-Z0-9]*[>]+[ \t]*")
1666         quoted point)
1667     (unless (bolp)
1668       (save-excursion
1669         (beginning-of-line)
1670         (when (looking-at (concat prefix
1671                                   supercite-thing))
1672           (setq quoted (match-string 0))))
1673       (insert "\n"))
1674     (setq point (point))
1675     (insert "\n\n\n")
1676     (delete-region (point) (re-search-forward "[ \t]*"))
1677     (when quoted
1678       (insert quoted))
1679     (fill-paragraph nil)
1680     (goto-char point)
1681     (forward-line 1)))
1682
1683 (defun message-insert-signature (&optional force)
1684   "Insert a signature.  See documentation for the `message-signature' variable."
1685   (interactive (list 0))
1686   (let* ((signature
1687           (cond
1688            ((and (null message-signature)
1689                  (eq force 0))
1690             (save-excursion
1691               (goto-char (point-max))
1692               (not (re-search-backward message-signature-separator nil t))))
1693            ((and (null message-signature)
1694                  force)
1695             t)
1696            ((message-functionp message-signature)
1697             (funcall message-signature))
1698            ((listp message-signature)
1699             (eval message-signature))
1700            (t message-signature)))
1701          (signature
1702           (cond ((stringp signature)
1703                  signature)
1704                 ((and (eq t signature)
1705                       message-signature-file
1706                       (file-exists-p message-signature-file))
1707                  signature))))
1708     (when signature
1709       (goto-char (point-max))
1710       ;; Insert the signature.
1711       (unless (bolp)
1712         (insert "\n"))
1713       (insert "\n-- \n")
1714       (if (eq signature t)
1715           (insert-file-contents message-signature-file)
1716         (insert signature))
1717       (goto-char (point-max))
1718       (or (bolp) (insert "\n")))))
1719
1720 (defun message-elide-region (b e)
1721   "Elide the text between point and mark.
1722 An ellipsis (from `message-elide-ellipsis') will be inserted where the
1723 text was killed."
1724   (interactive "r")
1725   (kill-region b e)
1726   (insert message-elide-ellipsis))
1727
1728 (defvar message-caesar-translation-table nil)
1729
1730 (defun message-caesar-region (b e &optional n)
1731   "Caesar rotation of region by N, default 13, for decrypting netnews."
1732   (interactive
1733    (list
1734     (min (point) (or (mark t) (point)))
1735     (max (point) (or (mark t) (point)))
1736     (when current-prefix-arg
1737       (prefix-numeric-value current-prefix-arg))))
1738
1739   (setq n (if (numberp n) (mod n 26) 13)) ;canonize N
1740   (unless (or (zerop n)                 ; no action needed for a rot of 0
1741               (= b e))                  ; no region to rotate
1742     ;; We build the table, if necessary.
1743     (when (or (not message-caesar-translation-table)
1744               (/= (aref message-caesar-translation-table ?a) (+ ?a n)))
1745       (setq message-caesar-translation-table
1746             (message-make-caesar-translation-table n)))
1747     (translate-region b e message-caesar-translation-table)))
1748
1749 (defun message-make-caesar-translation-table (n)
1750   "Create a rot table with offset N."
1751   (let ((i -1)
1752         (table (make-string 256 0)))
1753     (while (< (incf i) 256)
1754       (aset table i i))
1755     (concat
1756      (substring table 0 ?A)
1757      (substring table (+ ?A n) (+ ?A n (- 26 n)))
1758      (substring table ?A (+ ?A n))
1759      (substring table (+ ?A 26) ?a)
1760      (substring table (+ ?a n) (+ ?a n (- 26 n)))
1761      (substring table ?a (+ ?a n))
1762      (substring table (+ ?a 26) 255))))
1763
1764 (defun message-caesar-buffer-body (&optional rotnum)
1765   "Caesar rotates all letters in the current buffer by 13 places.
1766 Used to encode/decode possiblyun offensive messages (commonly in net.jokes).
1767 With prefix arg, specifies the number of places to rotate each letter forward.
1768 Mail and USENET news headers are not rotated."
1769   (interactive (if current-prefix-arg
1770                    (list (prefix-numeric-value current-prefix-arg))
1771                  (list nil)))
1772   (save-excursion
1773     (save-restriction
1774       (when (message-goto-body)
1775         (narrow-to-region (point) (point-max)))
1776       (message-caesar-region (point-min) (point-max) rotnum))))
1777
1778 (defun message-pipe-buffer-body (program)
1779   "Pipe the message body in the current buffer through PROGRAM."
1780   (save-excursion
1781     (save-restriction
1782       (when (message-goto-body)
1783         (narrow-to-region (point) (point-max)))
1784       (shell-command-on-region
1785        (point-min) (point-max) program nil t))))
1786
1787 (defun message-rename-buffer (&optional enter-string)
1788   "Rename the *message* buffer to \"*message* RECIPIENT\".
1789 If the function is run with a prefix, it will ask for a new buffer
1790 name, rather than giving an automatic name."
1791   (interactive "Pbuffer name: ")
1792   (save-excursion
1793     (save-restriction
1794       (goto-char (point-min))
1795       (narrow-to-region (point)
1796                         (search-forward mail-header-separator nil 'end))
1797       (let* ((mail-to (or
1798                        (if (message-news-p) (message-fetch-field "Newsgroups")
1799                          (message-fetch-field "To"))
1800                        ""))
1801              (mail-trimmed-to
1802               (if (string-match "," mail-to)
1803                   (concat (substring mail-to 0 (match-beginning 0)) ", ...")
1804                 mail-to))
1805              (name-default (concat "*message* " mail-trimmed-to))
1806              (name (if enter-string
1807                        (read-string "New buffer name: " name-default)
1808                      name-default)))
1809         (rename-buffer name t)))))
1810
1811 (defun message-fill-yanked-message (&optional justifyp)
1812   "Fill the paragraphs of a message yanked into this one.
1813 Numeric argument means justify as well."
1814   (interactive "P")
1815   (save-excursion
1816     (goto-char (point-min))
1817     (search-forward (concat "\n" mail-header-separator "\n") nil t)
1818     (let ((fill-prefix message-yank-prefix))
1819       (fill-individual-paragraphs (point) (point-max) justifyp))))
1820
1821 (defun message-indent-citation ()
1822   "Modify text just inserted from a message to be cited.
1823 The inserted text should be the region.
1824 When this function returns, the region is again around the modified text.
1825
1826 Normally, indent each nonblank line `message-indentation-spaces' spaces.
1827 However, if `message-yank-prefix' is non-nil, insert that prefix on each line."
1828   (let ((start (point)))
1829     ;; Remove unwanted headers.
1830     (when message-ignored-cited-headers
1831       (let (all-removed)
1832         (save-restriction
1833           (narrow-to-region
1834            (goto-char start)
1835            (if (search-forward "\n\n" nil t)
1836                (1- (point))
1837              (point)))
1838           (message-remove-header message-ignored-cited-headers t)
1839           (when (= (point-min) (point-max))
1840             (setq all-removed t))
1841           (goto-char (point-max)))
1842         (if all-removed
1843             (goto-char start)
1844           (forward-line 1))))
1845     ;; Delete blank lines at the start of the buffer.
1846     (while (and (point-min)
1847                 (eolp)
1848                 (not (eobp)))
1849       (message-delete-line))
1850     ;; Delete blank lines at the end of the buffer.
1851     (goto-char (point-max))
1852     (unless (eolp)
1853       (insert "\n"))
1854     (while (and (zerop (forward-line -1))
1855                 (looking-at "$"))
1856       (message-delete-line))
1857     ;; Do the indentation.
1858     (if (null message-yank-prefix)
1859         (indent-rigidly start (mark t) message-indentation-spaces)
1860       (save-excursion
1861         (goto-char start)
1862         (while (< (point) (mark t))
1863           (insert message-yank-prefix)
1864           (forward-line 1))))
1865     (goto-char start)))
1866
1867 (defun message-yank-original (&optional arg)
1868   "Insert the message being replied to, if any.
1869 Puts point before the text and mark after.
1870 Normally indents each nonblank line ARG spaces (default 3).  However,
1871 if `message-yank-prefix' is non-nil, insert that prefix on each line.
1872
1873 This function uses `message-cite-function' to do the actual citing.
1874
1875 Just \\[universal-argument] as argument means don't indent, insert no
1876 prefix, and don't delete any headers."
1877   (interactive "P")
1878   (let ((modified (buffer-modified-p)))
1879     (when (and message-reply-buffer
1880                message-cite-function)
1881       (delete-windows-on message-reply-buffer t)
1882       (insert-buffer message-reply-buffer)
1883       (funcall message-cite-function)
1884       (message-exchange-point-and-mark)
1885       (unless (bolp)
1886         (insert ?\n))
1887       (unless modified
1888         (setq message-checksum (message-checksum))))))
1889
1890 (defun message-yank-buffer (buffer)
1891   "Insert BUFFER into the current buffer and quote it."
1892   (interactive "bYank buffer: ")
1893   (let ((message-reply-buffer buffer))
1894     (save-window-excursion
1895       (message-yank-original))))
1896
1897 (defun message-buffers ()
1898   "Return a list of active message buffers."
1899   (let (buffers)
1900     (save-excursion
1901       (dolist (buffer (buffer-list t))
1902         (set-buffer buffer)
1903         (when (and (eq major-mode 'message-mode)
1904                    (null message-sent-message-via))
1905           (push (buffer-name buffer) buffers))))
1906     (nreverse buffers)))
1907
1908 (defun message-cite-original-without-signature ()
1909   "Cite function in the standard Message manner."
1910   (let ((start (point))
1911         (end (mark t))
1912         (functions
1913          (when message-indent-citation-function
1914            (if (listp message-indent-citation-function)
1915                message-indent-citation-function
1916              (list message-indent-citation-function)))))
1917     (mml-quote-region start end)
1918     ;; Allow undoing.
1919     (undo-boundary)
1920     (goto-char end)
1921     (when (re-search-backward message-signature-separator start t)
1922       ;; Also peel off any blank lines before the signature.
1923       (forward-line -1)
1924       (while (looking-at "^[ \t]*$")
1925         (forward-line -1))
1926       (forward-line 1)
1927       (delete-region (point) end))
1928     (goto-char start)
1929     (while functions
1930       (funcall (pop functions)))
1931     (when message-citation-line-function
1932       (unless (bolp)
1933         (insert "\n"))
1934       (funcall message-citation-line-function))))
1935
1936 (defvar mail-citation-hook)             ;Compiler directive
1937 (defun message-cite-original ()
1938   "Cite function in the standard Message manner."
1939   (if (and (boundp 'mail-citation-hook)
1940            mail-citation-hook)
1941       (run-hooks 'mail-citation-hook)
1942     (let ((start (point))
1943           (end (mark t))
1944           (functions
1945            (when message-indent-citation-function
1946              (if (listp message-indent-citation-function)
1947                  message-indent-citation-function
1948                (list message-indent-citation-function)))))
1949       (mml-quote-region start end)
1950       (goto-char start)
1951       (while functions
1952         (funcall (pop functions)))
1953       (when message-citation-line-function
1954         (unless (bolp)
1955           (insert "\n"))
1956         (funcall message-citation-line-function)))))
1957
1958 (defun message-insert-citation-line ()
1959   "Function that inserts a simple citation line."
1960   (when message-reply-headers
1961     (insert (mail-header-from message-reply-headers) " writes:\n\n")))
1962
1963 (defun message-position-on-field (header &rest afters)
1964   (let ((case-fold-search t))
1965     (save-restriction
1966       (narrow-to-region
1967        (goto-char (point-min))
1968        (progn
1969          (re-search-forward
1970           (concat "^" (regexp-quote mail-header-separator) "$"))
1971          (match-beginning 0)))
1972       (goto-char (point-min))
1973       (if (re-search-forward (concat "^" (regexp-quote header) ":") nil t)
1974           (progn
1975             (re-search-forward "^[^ \t]" nil 'move)
1976             (beginning-of-line)
1977             (skip-chars-backward "\n")
1978             t)
1979         (while (and afters
1980                     (not (re-search-forward
1981                           (concat "^" (regexp-quote (car afters)) ":")
1982                           nil t)))
1983           (pop afters))
1984         (when afters
1985           (re-search-forward "^[^ \t]" nil 'move)
1986           (beginning-of-line))
1987         (insert header ": \n")
1988         (forward-char -1)
1989         nil))))
1990
1991 (defun message-remove-signature ()
1992   "Remove the signature from the text between point and mark.
1993 The text will also be indented the normal way."
1994   (save-excursion
1995     (let ((start (point))
1996           mark)
1997       (if (not (re-search-forward message-signature-separator (mark t) t))
1998           ;; No signature here, so we just indent the cited text.
1999           (message-indent-citation)
2000         ;; Find the last non-empty line.
2001         (forward-line -1)
2002         (while (looking-at "[ \t]*$")
2003           (forward-line -1))
2004         (forward-line 1)
2005         (setq mark (set-marker (make-marker) (point)))
2006         (goto-char start)
2007         (message-indent-citation)
2008         ;; Enable undoing the deletion.
2009         (undo-boundary)
2010         (delete-region mark (mark t))
2011         (set-marker mark nil)))))
2012
2013 \f
2014
2015 ;;;
2016 ;;; Sending messages
2017 ;;;
2018
2019 (defun message-send-and-exit (&optional arg)
2020   "Send message like `message-send', then, if no errors, exit from mail buffer."
2021   (interactive "P")
2022   (let ((buf (current-buffer))
2023         (actions message-exit-actions))
2024     (when (and (message-send arg)
2025                (buffer-name buf))
2026       (if message-kill-buffer-on-exit
2027           (kill-buffer buf)
2028         (bury-buffer buf)
2029         (when (eq buf (current-buffer))
2030           (message-bury buf)))
2031       (message-do-actions actions)
2032       t)))
2033
2034 (defun message-dont-send ()
2035   "Don't send the message you have been editing."
2036   (interactive)
2037   (set-buffer-modified-p t)
2038   (save-buffer)
2039   (let ((actions message-postpone-actions))
2040     (message-bury (current-buffer))
2041     (message-do-actions actions)))
2042
2043 (defun message-kill-buffer ()
2044   "Kill the current buffer."
2045   (interactive)
2046   (when (or (not (buffer-modified-p))
2047             (yes-or-no-p "Message modified; kill anyway? "))
2048     (let ((actions message-kill-actions))
2049       (setq buffer-file-name nil)
2050       (kill-buffer (current-buffer))
2051       (message-do-actions actions))))
2052
2053 (defun message-bury (buffer)
2054   "Bury this mail buffer."
2055   (let ((newbuf (other-buffer buffer)))
2056     (bury-buffer buffer)
2057     (if (and (fboundp 'frame-parameters)
2058              (cdr (assq 'dedicated (frame-parameters)))
2059              (not (null (delq (selected-frame) (visible-frame-list)))))
2060         (delete-frame (selected-frame))
2061       (switch-to-buffer newbuf))))
2062
2063 (defun message-send (&optional arg)
2064   "Send the message in the current buffer.
2065 If `message-interactive' is non-nil, wait for success indication or
2066 error messages, and inform user.
2067 Otherwise any failure is reported in a message back to the user from
2068 the mailer.
2069 The usage of ARG is defined by the instance that called Message.
2070 It should typically alter the sending method in some way or other."
2071   (interactive "P")
2072   ;; Make it possible to undo the coming changes.
2073   (undo-boundary)
2074   (let ((inhibit-read-only t))
2075     (put-text-property (point-min) (point-max) 'read-only nil))
2076   (message-fix-before-sending)
2077   (run-hooks 'message-send-hook)
2078   (message "Sending...")
2079   (let ((alist message-send-method-alist)
2080         (success t)
2081         elem sent)
2082     (while (and success
2083                 (setq elem (pop alist)))
2084       (when (or (not (funcall (cadr elem)))
2085                 (and (or (not (memq (car elem)
2086                                     message-sent-message-via))
2087                          (y-or-n-p
2088                           (format
2089                            "Already sent message via %s; resend? "
2090                            (car elem))))
2091                      (setq success (funcall (caddr elem) arg))))
2092         (setq sent t)))
2093     (unless (or sent (not success))
2094       (error "No methods specified to send by"))
2095     (when (and success sent)
2096       (message-do-fcc)
2097       (save-excursion
2098         (run-hooks 'message-sent-hook))
2099       (message "Sending...done")
2100       ;; Mark the buffer as unmodified and delete auto-save.
2101       (set-buffer-modified-p nil)
2102       (delete-auto-save-file-if-necessary t)
2103       (message-disassociate-draft)
2104       ;; Delete other mail buffers and stuff.
2105       (message-do-send-housekeeping)
2106       (message-do-actions message-send-actions)
2107       ;; Return success.
2108       t)))
2109
2110 (defun message-send-via-mail (arg)
2111   "Send the current message via mail."
2112   (message-send-mail arg))
2113
2114 (defun message-send-via-news (arg)
2115   "Send the current message via news."
2116   (funcall message-send-news-function arg))
2117
2118 (defmacro message-check (type &rest forms)
2119   "Eval FORMS if TYPE is to be checked."
2120   `(or (message-check-element ,type)
2121        (save-excursion
2122          ,@forms)))
2123
2124 (put 'message-check 'lisp-indent-function 1)
2125 (put 'message-check 'edebug-form-spec '(form body))
2126
2127 (defun message-fix-before-sending ()
2128   "Do various things to make the message nice before sending it."
2129   ;; Make sure there's a newline at the end of the message.
2130   (goto-char (point-max))
2131   (unless (bolp)
2132     (insert "\n"))
2133   ;; Delete all invisible text.
2134   (message-check 'invisible-text
2135     (when (text-property-any (point-min) (point-max) 'invisible t)
2136       (put-text-property (point-min) (point-max) 'invisible nil)
2137       (unless (yes-or-no-p
2138                "Invisible text found and made visible; continue posting? ")
2139         (error "Invisible text found and made visible")))))
2140
2141 (defun message-add-action (action &rest types)
2142   "Add ACTION to be performed when doing an exit of type TYPES."
2143   (let (var)
2144     (while types
2145       (set (setq var (intern (format "message-%s-actions" (pop types))))
2146            (nconc (symbol-value var) (list action))))))
2147
2148 (defun message-do-actions (actions)
2149   "Perform all actions in ACTIONS."
2150   ;; Now perform actions on successful sending.
2151   (while actions
2152     (ignore-errors
2153       (cond
2154        ;; A simple function.
2155        ((message-functionp (car actions))
2156         (funcall (car actions)))
2157        ;; Something to be evaled.
2158        (t
2159         (eval (car actions)))))
2160     (pop actions)))
2161
2162 (defun message-send-mail-partially ()
2163   "Sendmail as message/partial."
2164   (let ((p (goto-char (point-min)))
2165         (tembuf (message-generate-new-buffer-clone-locals " message temp"))
2166         (curbuf (current-buffer))
2167         (id (message-make-message-id)) (n 1)
2168         plist total  header required-mail-headers)
2169     (while (not (eobp))
2170       (if (< (point-max) (+ p message-send-mail-partially-limit))
2171           (goto-char (point-max))
2172         (goto-char (+ p message-send-mail-partially-limit))
2173         (beginning-of-line)
2174         (if (<= (point) p) (end-of-line))) ;; In case of bad message.
2175       (push p plist)
2176       (setq p (point)))
2177     (setq total (length plist))
2178     (push (point-max) plist)
2179     (setq plist (nreverse plist))
2180     (unwind-protect
2181         (save-excursion
2182           (setq p (pop plist))
2183           (while plist
2184             (set-buffer curbuf)
2185             (copy-to-buffer tembuf p (car plist))
2186             (set-buffer tembuf)
2187             (goto-char (point-min))
2188             (if header
2189                 (progn
2190                   (goto-char (point-min))
2191                   (narrow-to-region (point) (point))
2192                   (insert header))
2193               (message-goto-eoh)
2194               (setq header (buffer-substring (point-min) (point)))
2195               (goto-char (point-min))
2196               (narrow-to-region (point) (point))
2197               (insert header)
2198               (message-remove-header "Mime-Version")
2199               (message-remove-header "Content-Type")
2200               (message-remove-header "Message-ID")
2201               (message-remove-header "Lines")
2202               (goto-char (point-max))
2203               (insert "Mime-Version: 1.0\n")
2204               (setq header (buffer-substring (point-min) (point-max))))
2205             (goto-char (point-max))
2206             (insert (format "Content-Type: message/partial; id=\"%s\"; number=%d; total=%d\n"
2207                             id n total))
2208             (let ((mail-header-separator ""))
2209               (when (memq 'Message-ID message-required-mail-headers)
2210                 (insert "Message-ID: " (message-make-message-id) "\n"))
2211               (when (memq 'Lines message-required-mail-headers)
2212                 (let ((mail-header-separator ""))
2213                   (insert "Lines: " (message-make-lines) "\n")))
2214               (message-goto-subject)
2215               (end-of-line)
2216               (insert (format " (%d/%d)" n total))
2217               (goto-char (point-max))
2218               (insert "\n")
2219               (widen)
2220               (funcall message-send-mail-function))
2221             (setq n (+ n 1))
2222             (setq p (pop plist))
2223             (erase-buffer)))
2224       (kill-buffer tembuf))))
2225
2226 (defun message-send-mail (&optional arg)
2227   (require 'mail-utils)
2228   (let* ((tembuf (message-generate-new-buffer-clone-locals " message temp"))
2229          (case-fold-search nil)
2230          (news (message-news-p))
2231          (mailbuf (current-buffer))
2232          (message-this-is-mail t)
2233          (message-posting-charset
2234           (if (fboundp 'gnus-setup-posting-charset)
2235               (gnus-setup-posting-charset nil)
2236             message-posting-charset)))
2237     (save-restriction
2238       (message-narrow-to-headers)
2239       ;; Insert some headers.
2240       (let ((message-deletable-headers
2241              (if news nil message-deletable-headers)))
2242         (message-generate-headers message-required-mail-headers))
2243       ;; Let the user do all of the above.
2244       (run-hooks 'message-header-hook))
2245     (unwind-protect
2246         (save-excursion
2247           (set-buffer tembuf)
2248           (erase-buffer)
2249           ;; Avoid copying text props.
2250           (insert (format
2251                    "%s" (save-excursion
2252                           (set-buffer mailbuf)
2253                           (buffer-string))))
2254           ;; Remove some headers.
2255           (message-encode-message-body)
2256           (save-restriction
2257             (message-narrow-to-headers)
2258             ;; We (re)generate the Lines header.
2259             (when (memq 'Lines message-required-mail-headers)
2260               (message-generate-headers '(Lines)))
2261             ;; Remove some headers.
2262             (message-remove-header message-ignored-mail-headers t)
2263             (mail-encode-encoded-word-buffer))
2264           (goto-char (point-max))
2265           ;; require one newline at the end.
2266           (or (= (preceding-char) ?\n)
2267               (insert ?\n))
2268           (when (and news
2269                      (or (message-fetch-field "cc")
2270                          (message-fetch-field "to")))
2271             (message-insert-courtesy-copy))
2272           (if (or (not message-send-mail-partially-limit)
2273                   (< (point-max) message-send-mail-partially-limit)
2274                   (not (y-or-n-p "The message size is too large, should it be sent partially?")))
2275               (funcall message-send-mail-function)
2276             (message-send-mail-partially)))
2277       (kill-buffer tembuf))
2278     (set-buffer mailbuf)
2279     (push 'mail message-sent-message-via)))
2280
2281 (defun message-send-mail-with-sendmail ()
2282   "Send off the prepared buffer with sendmail."
2283   (let ((errbuf (if message-interactive
2284                     (message-generate-new-buffer-clone-locals
2285                      " sendmail errors")
2286                   0))
2287         resend-to-addresses delimline)
2288     (let ((case-fold-search t))
2289       (save-restriction
2290         (message-narrow-to-headers)
2291         (setq resend-to-addresses (message-fetch-field "resent-to")))
2292       ;; Change header-delimiter to be what sendmail expects.
2293       (goto-char (point-min))
2294       (re-search-forward
2295        (concat "^" (regexp-quote mail-header-separator) "\n"))
2296       (replace-match "\n")
2297       (backward-char 1)
2298       (setq delimline (point-marker))
2299       (run-hooks 'message-send-mail-hook)
2300       ;; Insert an extra newline if we need it to work around
2301       ;; Sun's bug that swallows newlines.
2302       (goto-char (1+ delimline))
2303       (when (eval message-mailer-swallows-blank-line)
2304         (newline))
2305       (when message-interactive
2306         (save-excursion
2307           (set-buffer errbuf)
2308           (erase-buffer))))
2309     (let ((default-directory "/")
2310           (coding-system-for-write message-send-coding-system))
2311       (apply 'call-process-region
2312              (append (list (point-min) (point-max)
2313                            (if (boundp 'sendmail-program)
2314                                sendmail-program
2315                              "/usr/lib/sendmail")
2316                            nil errbuf nil "-oi")
2317                      ;; Always specify who from,
2318                      ;; since some systems have broken sendmails.
2319                      ;; But some systems are more broken with -f, so
2320                      ;; we'll let users override this.
2321                      (if (null message-sendmail-f-is-evil)
2322                          (list "-f" (message-make-address)))
2323                      ;; These mean "report errors by mail"
2324                      ;; and "deliver in background".
2325                      (if (null message-interactive) '("-oem" "-odb"))
2326                      ;; Get the addresses from the message
2327                      ;; unless this is a resend.
2328                      ;; We must not do that for a resend
2329                      ;; because we would find the original addresses.
2330                      ;; For a resend, include the specific addresses.
2331                      (if resend-to-addresses
2332                          (list resend-to-addresses)
2333                        '("-t")))))
2334     (when message-interactive
2335       (save-excursion
2336         (set-buffer errbuf)
2337         (goto-char (point-min))
2338         (while (re-search-forward "\n\n* *" nil t)
2339           (replace-match "; "))
2340         (if (not (zerop (buffer-size)))
2341             (error "Sending...failed to %s"
2342                    (buffer-substring (point-min) (point-max)))))
2343       (when (bufferp errbuf)
2344         (kill-buffer errbuf)))))
2345
2346 (defun message-send-mail-with-qmail ()
2347   "Pass the prepared message buffer to qmail-inject.
2348 Refer to the documentation for the variable `message-send-mail-function'
2349 to find out how to use this."
2350   ;; replace the header delimiter with a blank line
2351   (goto-char (point-min))
2352   (re-search-forward
2353    (concat "^" (regexp-quote mail-header-separator) "\n"))
2354   (replace-match "\n")
2355   (run-hooks 'message-send-mail-hook)
2356   ;; send the message
2357   (case
2358       (let ((coding-system-for-write message-send-coding-system))
2359         (apply
2360          'call-process-region 1 (point-max) message-qmail-inject-program
2361          nil nil nil
2362          ;; qmail-inject's default behaviour is to look for addresses on the
2363          ;; command line; if there're none, it scans the headers.
2364          ;; yes, it does The Right Thing w.r.t. Resent-To and it's kin.
2365          ;;
2366          ;; in general, ALL of qmail-inject's defaults are perfect for simply
2367          ;; reading a formatted (i. e., at least a To: or Resent-To header)
2368          ;; message from stdin.
2369          ;;
2370          ;; qmail also has the advantage of not having been raped by
2371          ;; various vendors, so we don't have to allow for that, either --
2372          ;; compare this with message-send-mail-with-sendmail and weep
2373          ;; for sendmail's lost innocence.
2374          ;;
2375          ;; all this is way cool coz it lets us keep the arguments entirely
2376          ;; free for -inject-arguments -- a big win for the user and for us
2377          ;; since we don't have to play that double-guessing game and the user
2378          ;; gets full control (no gestapo'ish -f's, for instance).  --sj
2379          message-qmail-inject-args))
2380     ;; qmail-inject doesn't say anything on it's stdout/stderr,
2381     ;; we have to look at the retval instead
2382     (0 nil)
2383     (1   (error "qmail-inject reported permanent failure"))
2384     (111 (error "qmail-inject reported transient failure"))
2385     ;; should never happen
2386     (t   (error "qmail-inject reported unknown failure"))))
2387
2388 (defun message-send-mail-with-mh ()
2389   "Send the prepared message buffer with mh."
2390   (let ((mh-previous-window-config nil)
2391         (name (mh-new-draft-name)))
2392     (setq buffer-file-name name)
2393     ;; MH wants to generate these headers itself.
2394     (when message-mh-deletable-headers
2395       (let ((headers message-mh-deletable-headers))
2396         (while headers
2397           (goto-char (point-min))
2398           (and (re-search-forward
2399                 (concat "^" (symbol-name (car headers)) ": *") nil t)
2400                (message-delete-line))
2401           (pop headers))))
2402     (run-hooks 'message-send-mail-hook)
2403     ;; Pass it on to mh.
2404     (mh-send-letter)))
2405
2406 (defun message-send-news (&optional arg)
2407   (let* ((tembuf (message-generate-new-buffer-clone-locals " *message temp*"))
2408          (case-fold-search nil)
2409          (method (if (message-functionp message-post-method)
2410                      (funcall message-post-method arg)
2411                    message-post-method))
2412          (messbuf (current-buffer))
2413          (message-syntax-checks
2414           (if arg
2415               (cons '(existing-newsgroups . disabled)
2416                     message-syntax-checks)
2417             message-syntax-checks))
2418          (message-this-is-news t)
2419          (message-posting-charset (gnus-setup-posting-charset 
2420                                    (message-fetch-field "Newsgroups")))
2421          result)
2422     (if (not (message-check-news-body-syntax))
2423         nil
2424       (save-restriction
2425         (message-narrow-to-headers)
2426         ;; Insert some headers.
2427         (message-generate-headers message-required-news-headers)
2428         ;; Let the user do all of the above.
2429         (run-hooks 'message-header-hook))
2430       (message-cleanup-headers)
2431       (if (not (message-check-news-syntax))
2432           nil
2433         (unwind-protect
2434             (save-excursion
2435               (set-buffer tembuf)
2436               (buffer-disable-undo)
2437               (erase-buffer)
2438               ;; Avoid copying text props.
2439               (insert (format
2440                        "%s" (save-excursion
2441                               (set-buffer messbuf)
2442                               (buffer-string))))
2443               (message-encode-message-body)
2444               ;; Remove some headers.
2445               (save-restriction
2446                 (message-narrow-to-headers)
2447                 ;; We (re)generate the Lines header.
2448                 (when (memq 'Lines message-required-mail-headers)
2449                   (message-generate-headers '(Lines)))
2450                 ;; Remove some headers.
2451                 (message-remove-header message-ignored-news-headers t)
2452                 (let ((mail-parse-charset (car message-posting-charset)))
2453                   (mail-encode-encoded-word-buffer)))
2454               (goto-char (point-max))
2455               ;; require one newline at the end.
2456               (or (= (preceding-char) ?\n)
2457                   (insert ?\n))
2458               (let ((case-fold-search t))
2459                 ;; Remove the delimiter.
2460                 (goto-char (point-min))
2461                 (re-search-forward
2462                  (concat "^" (regexp-quote mail-header-separator) "\n"))
2463                 (replace-match "\n")
2464                 (backward-char 1))
2465               (run-hooks 'message-send-news-hook)
2466               (gnus-open-server method)
2467               (setq result (let ((mail-header-separator ""))
2468                              (gnus-request-post method))))
2469           (kill-buffer tembuf))
2470         (set-buffer messbuf)
2471         (if result
2472             (push 'news message-sent-message-via)
2473           (message "Couldn't send message via news: %s"
2474                    (nnheader-get-report (car method)))
2475           nil)))))
2476
2477 ;;;
2478 ;;; Header generation & syntax checking.
2479 ;;;
2480
2481 (defun message-check-element (type)
2482   "Returns non-nil if this type is not to be checked."
2483   (if (eq message-syntax-checks 'dont-check-for-anything-just-trust-me)
2484       t
2485     (let ((able (assq type message-syntax-checks)))
2486       (and (consp able)
2487            (eq (cdr able) 'disabled)))))
2488
2489 (defun message-check-news-syntax ()
2490   "Check the syntax of the message."
2491   (save-excursion
2492     (save-restriction
2493       (widen)
2494       ;; We narrow to the headers and check them first.
2495       (save-excursion
2496         (save-restriction
2497           (message-narrow-to-headers)
2498           (message-check-news-header-syntax))))))
2499
2500 (defun message-check-news-header-syntax ()
2501   (and
2502    ;; Check Newsgroups header.
2503    (message-check 'newsgroups
2504      (let ((group (message-fetch-field "newsgroups")))
2505        (or
2506         (and group
2507              (not (string-match "\\`[ \t]*\\'" group)))
2508         (ignore
2509          (message
2510           "The newsgroups field is empty or missing.  Posting is denied.")))))
2511    ;; Check the Subject header.
2512    (message-check 'subject
2513      (let* ((case-fold-search t)
2514             (subject (message-fetch-field "subject")))
2515        (or
2516         (and subject
2517              (not (string-match "\\`[ \t]*\\'" subject)))
2518         (ignore
2519          (message
2520           "The subject field is empty or missing.  Posting is denied.")))))
2521    ;; Check for commands in Subject.
2522    (message-check 'subject-cmsg
2523      (if (string-match "^cmsg " (message-fetch-field "subject"))
2524          (y-or-n-p
2525           "The control code \"cmsg\" is in the subject.  Really post? ")
2526        t))
2527    ;; Check for multiple identical headers.
2528    (message-check 'multiple-headers
2529      (let (found)
2530        (while (and (not found)
2531                    (re-search-forward "^[^ \t:]+: " nil t))
2532          (save-excursion
2533            (or (re-search-forward
2534                 (concat "^"
2535                         (regexp-quote
2536                          (setq found
2537                                (buffer-substring
2538                                 (match-beginning 0) (- (match-end 0) 2))))
2539                         ":")
2540                 nil t)
2541                (setq found nil))))
2542        (if found
2543            (y-or-n-p (format "Multiple %s headers.  Really post? " found))
2544          t)))
2545    ;; Check for Version and Sendsys.
2546    (message-check 'sendsys
2547      (if (re-search-forward "^Sendsys:\\|^Version:" nil t)
2548          (y-or-n-p
2549           (format "The article contains a %s command.  Really post? "
2550                   (buffer-substring (match-beginning 0)
2551                                     (1- (match-end 0)))))
2552        t))
2553    ;; See whether we can shorten Followup-To.
2554    (message-check 'shorten-followup-to
2555      (let ((newsgroups (message-fetch-field "newsgroups"))
2556            (followup-to (message-fetch-field "followup-to"))
2557            to)
2558        (when (and newsgroups
2559                   (string-match "," newsgroups)
2560                   (not followup-to)
2561                   (not
2562                    (zerop
2563                     (length
2564                      (setq to (completing-read
2565                                "Followups to: (default all groups) "
2566                                (mapcar (lambda (g) (list g))
2567                                        (cons "poster"
2568                                              (message-tokenize-header
2569                                               newsgroups)))))))))
2570          (goto-char (point-min))
2571          (insert "Followup-To: " to "\n"))
2572        t))
2573    ;; Check "Shoot me".
2574    (message-check 'shoot
2575      (if (re-search-forward
2576           "Message-ID.*.i-did-not-set--mail-host-address--so-shoot-me" nil t)
2577          (y-or-n-p "You appear to have a misconfigured system.  Really post? ")
2578        t))
2579    ;; Check for Approved.
2580    (message-check 'approved
2581      (if (re-search-forward "^Approved:" nil t)
2582          (y-or-n-p "The article contains an Approved header.  Really post? ")
2583        t))
2584    ;; Check the Message-ID header.
2585    (message-check 'message-id
2586      (let* ((case-fold-search t)
2587             (message-id (message-fetch-field "message-id" t)))
2588        (or (not message-id)
2589            ;; Is there an @ in the ID?
2590            (and (string-match "@" message-id)
2591                 ;; Is there a dot in the ID?
2592                 (string-match "@[^.]*\\." message-id)
2593                 ;; Does the ID end with a dot?
2594                 (not (string-match "\\.>" message-id)))
2595            (y-or-n-p
2596             (format "The Message-ID looks strange: \"%s\".  Really post? "
2597                     message-id)))))
2598    ;; Check the Newsgroups & Followup-To headers.
2599    (message-check 'existing-newsgroups
2600      (let* ((case-fold-search t)
2601             (newsgroups (message-fetch-field "newsgroups"))
2602             (followup-to (message-fetch-field "followup-to"))
2603             (groups (message-tokenize-header
2604                      (if followup-to
2605                          (concat newsgroups "," followup-to)
2606                        newsgroups)))
2607             (hashtb (and (boundp 'gnus-active-hashtb)
2608                          gnus-active-hashtb))
2609             errors)
2610        (if (or (not hashtb)
2611                (not (boundp 'gnus-read-active-file))
2612                (not gnus-read-active-file)
2613                (eq gnus-read-active-file 'some))
2614            t
2615          (while groups
2616            (when (and (not (boundp (intern (car groups) hashtb)))
2617                       (not (equal (car groups) "poster")))
2618              (push (car groups) errors))
2619            (pop groups))
2620          (if (not errors)
2621              t
2622            (y-or-n-p
2623             (format
2624              "Really post to %s unknown group%s: %s "
2625              (if (= (length errors) 1) "this" "these")
2626              (if (= (length errors) 1) "" "s")
2627              (mapconcat 'identity errors ", ")))))))
2628    ;; Check the Newsgroups & Followup-To headers for syntax errors.
2629    (message-check 'valid-newsgroups
2630      (let ((case-fold-search t)
2631            (headers '("Newsgroups" "Followup-To"))
2632            header error)
2633        (while (and headers (not error))
2634          (when (setq header (mail-fetch-field (car headers)))
2635            (if (or
2636                 (not
2637                  (string-match
2638                   "\\`\\([-+_&.a-zA-Z0-9]+\\)?\\(,[-+_&.a-zA-Z0-9]+\\)*\\'"
2639                   header))
2640                 (memq
2641                  nil (mapcar
2642                       (lambda (g)
2643                         (not (string-match "\\.\\'\\|\\.\\." g)))
2644                       (message-tokenize-header header ","))))
2645                (setq error t)))
2646          (unless error
2647            (pop headers)))
2648        (if (not error)
2649            t
2650          (y-or-n-p
2651           (format "The %s header looks odd: \"%s\".  Really post? "
2652                   (car headers) header)))))
2653    (message-check 'repeated-newsgroups
2654      (let ((case-fold-search t)
2655            (headers '("Newsgroups" "Followup-To"))
2656            header error groups group)
2657        (while (and headers
2658                    (not error))
2659          (when (setq header (mail-fetch-field (pop headers)))
2660            (setq groups (message-tokenize-header header ","))
2661            (while (setq group (pop groups))
2662              (when (member group groups)
2663                (setq error group
2664                      groups nil)))))
2665        (if (not error)
2666            t
2667          (y-or-n-p
2668           (format "Group %s is repeated in headers.  Really post? " error)))))
2669    ;; Check the From header.
2670    (message-check 'from
2671      (let* ((case-fold-search t)
2672             (from (message-fetch-field "from"))
2673             ad)
2674        (cond
2675         ((not from)
2676          (message "There is no From line.  Posting is denied.")
2677          nil)
2678         ((or (not (string-match
2679                    "@[^\\.]*\\."
2680                    (setq ad (nth 1 (mail-extract-address-components
2681                                     from))))) ;larsi@ifi
2682              (string-match "\\.\\." ad) ;larsi@ifi..uio
2683              (string-match "@\\." ad)   ;larsi@.ifi.uio
2684              (string-match "\\.$" ad)   ;larsi@ifi.uio.
2685              (not (string-match "^[^@]+@[^@]+$" ad)) ;larsi.ifi.uio
2686              (string-match "(.*).*(.*)" from)) ;(lars) (lars)
2687          (message
2688           "Denied posting -- the From looks strange: \"%s\"." from)
2689          nil)
2690         (t t))))))
2691
2692 (defun message-check-news-body-syntax ()
2693   (and
2694    ;; Check for long lines.
2695    (message-check 'long-lines
2696      (goto-char (point-min))
2697      (re-search-forward
2698       (concat "^" (regexp-quote mail-header-separator) "$"))
2699      (while (and
2700              (progn
2701                (end-of-line)
2702                (< (current-column) 80))
2703              (zerop (forward-line 1))))
2704      (or (bolp)
2705          (eobp)
2706          (y-or-n-p
2707           "You have lines longer than 79 characters.  Really post? ")))
2708    ;; Check whether the article is empty.
2709    (message-check 'empty
2710      (goto-char (point-min))
2711      (re-search-forward
2712       (concat "^" (regexp-quote mail-header-separator) "$"))
2713      (forward-line 1)
2714      (let ((b (point)))
2715        (goto-char (point-max))
2716        (re-search-backward message-signature-separator nil t)
2717        (beginning-of-line)
2718        (or (re-search-backward "[^ \n\t]" b t)
2719            (y-or-n-p "Empty article.  Really post? "))))
2720    ;; Check for control characters.
2721    (message-check 'control-chars
2722      (if (re-search-forward "[\000-\007\013\015-\032\034-\037\200-\237]" nil t)
2723          (y-or-n-p
2724           "The article contains control characters.  Really post? ")
2725        t))
2726    ;; Check excessive size.
2727    (message-check 'size
2728      (if (> (buffer-size) 60000)
2729          (y-or-n-p
2730           (format "The article is %d octets long.  Really post? "
2731                   (buffer-size)))
2732        t))
2733    ;; Check whether any new text has been added.
2734    (message-check 'new-text
2735      (or
2736       (not message-checksum)
2737       (not (eq (message-checksum) message-checksum))
2738       (y-or-n-p
2739        "It looks like no new text has been added.  Really post? ")))
2740    ;; Check the length of the signature.
2741    (message-check 'signature
2742      (goto-char (point-max))
2743      (if (> (count-lines (point) (point-max)) 5)
2744          (y-or-n-p
2745           (format
2746            "Your .sig is %d lines; it should be max 4.  Really post? "
2747            (1- (count-lines (point) (point-max)))))
2748        t))))
2749
2750 (defun message-checksum ()
2751   "Return a \"checksum\" for the current buffer."
2752   (let ((sum 0))
2753     (save-excursion
2754       (goto-char (point-min))
2755       (re-search-forward
2756        (concat "^" (regexp-quote mail-header-separator) "$"))
2757       (while (not (eobp))
2758         (when (not (looking-at "[ \t\n]"))
2759           (setq sum (logxor (ash sum 1) (if (natnump sum) 0 1)
2760                             (char-after))))
2761         (forward-char 1)))
2762     sum))
2763
2764 (defun message-do-fcc ()
2765   "Process Fcc headers in the current buffer."
2766   (let ((case-fold-search t)
2767         (buf (current-buffer))
2768         list file)
2769     (save-excursion
2770       (set-buffer (get-buffer-create " *message temp*"))
2771       (erase-buffer)
2772       (insert-buffer-substring buf)
2773       (save-restriction
2774         (message-narrow-to-headers)
2775         (while (setq file (message-fetch-field "fcc"))
2776           (push file list)
2777           (message-remove-header "fcc" nil t)))
2778       (goto-char (point-min))
2779       (re-search-forward (concat "^" (regexp-quote mail-header-separator) "$"))
2780       (replace-match "" t t)
2781       ;; Process FCC operations.
2782       (while list
2783         (setq file (pop list))
2784         (if (string-match "^[ \t]*|[ \t]*\\(.*\\)[ \t]*$" file)
2785             ;; Pipe the article to the program in question.
2786             (call-process-region (point-min) (point-max) shell-file-name
2787                                  nil nil nil shell-command-switch
2788                                  (match-string 1 file))
2789           ;; Save the article.
2790           (setq file (expand-file-name file))
2791           (unless (file-exists-p (file-name-directory file))
2792             (make-directory (file-name-directory file) t))
2793           (if (and message-fcc-handler-function
2794                    (not (eq message-fcc-handler-function 'rmail-output)))
2795               (funcall message-fcc-handler-function file)
2796             (if (and (file-readable-p file) (mail-file-babyl-p file))
2797                 (rmail-output file 1 nil t)
2798               (let ((mail-use-rfc822 t))
2799                 (rmail-output file 1 t t))))))
2800
2801       (kill-buffer (current-buffer)))))
2802
2803 (defun message-output (filename)
2804   "Append this article to Unix/babyl mail file.."
2805   (if (and (file-readable-p filename)
2806            (mail-file-babyl-p filename))
2807       (rmail-output-to-rmail-file filename t)
2808     (gnus-output-to-mail filename t)))
2809
2810 (defun message-cleanup-headers ()
2811   "Do various automatic cleanups of the headers."
2812   ;; Remove empty lines in the header.
2813   (save-restriction
2814     (message-narrow-to-headers)
2815     ;; Remove blank lines.
2816     (while (re-search-forward "^[ \t]*\n" nil t)
2817       (replace-match "" t t))
2818
2819     ;; Correct Newsgroups and Followup-To headers:  Change sequence of
2820     ;; spaces to comma and eliminate spaces around commas.  Eliminate
2821     ;; embedded line breaks.
2822     (goto-char (point-min))
2823     (while (re-search-forward "^\\(Newsgroups\\|Followup-To\\): +" nil t)
2824       (save-restriction
2825         (narrow-to-region
2826          (point)
2827          (if (re-search-forward "^[^ \t]" nil t)
2828              (match-beginning 0)
2829            (forward-line 1)
2830            (point)))
2831         (goto-char (point-min))
2832         (while (re-search-forward "\n[ \t]+" nil t)
2833           (replace-match " " t t))      ;No line breaks (too confusing)
2834         (goto-char (point-min))
2835         (while (re-search-forward "[ \t\n]*,[ \t\n]*\\|[ \t]+" nil t)
2836           (replace-match "," t t))
2837         (goto-char (point-min))
2838         ;; Remove trailing commas.
2839         (when (re-search-forward ",+$" nil t)
2840           (replace-match "" t t))))))
2841
2842 (defun message-make-date (&optional now)
2843   "Make a valid data header.
2844 If NOW, use that time instead."
2845   (let* ((now (or now (current-time)))
2846          (zone (nth 8 (decode-time now)))
2847          (sign "+"))
2848     (when (< zone 0)
2849       (setq sign "-")
2850       (setq zone (- zone)))
2851     (concat
2852      (format-time-string "%d" now)
2853      ;; The month name of the %b spec is locale-specific.  Pfff.
2854      (format " %s "
2855              (capitalize (car (rassoc (nth 4 (decode-time now))
2856                                       parse-time-months))))
2857      (format-time-string "%Y %H:%M:%S " now)
2858      ;; We do all of this because XEmacs doesn't have the %z spec.
2859      (format "%s%02d%02d" sign (/ zone 3600) (/ (% zone 3600) 60)))))
2860
2861 (defun message-make-message-id ()
2862   "Make a unique Message-ID."
2863   (concat "<" (message-unique-id)
2864           (let ((psubject (save-excursion (message-fetch-field "subject")))
2865                 (psupersedes
2866                  (save-excursion (message-fetch-field "supersedes"))))
2867             (if (or
2868                  (and message-reply-headers
2869                       (mail-header-references message-reply-headers)
2870                       (mail-header-subject message-reply-headers)
2871                       psubject
2872                       (mail-header-subject message-reply-headers)
2873                       (not (string=
2874                             (message-strip-subject-re
2875                              (mail-header-subject message-reply-headers))
2876                             (message-strip-subject-re psubject))))
2877                  (and psupersedes
2878                       (string-match "_-_@" psupersedes)))
2879                 "_-_" ""))
2880           "@" (message-make-fqdn) ">"))
2881
2882 (defvar message-unique-id-char nil)
2883
2884 ;; If you ever change this function, make sure the new version
2885 ;; cannot generate IDs that the old version could.
2886 ;; You might for example insert a "." somewhere (not next to another dot
2887 ;; or string boundary), or modify the "fsf" string.
2888 (defun message-unique-id ()
2889   ;; Don't use microseconds from (current-time), they may be unsupported.
2890   ;; Instead we use this randomly inited counter.
2891   (setq message-unique-id-char
2892         (% (1+ (or message-unique-id-char (logand (random t) (1- (lsh 1 20)))))
2893            ;; (current-time) returns 16-bit ints,
2894            ;; and 2^16*25 just fits into 4 digits i base 36.
2895            (* 25 25)))
2896   (let ((tm (current-time)))
2897     (concat
2898      (if (memq system-type '(ms-dos emx vax-vms))
2899          (let ((user (downcase (user-login-name))))
2900            (while (string-match "[^a-z0-9_]" user)
2901              (aset user (match-beginning 0) ?_))
2902            user)
2903        (message-number-base36 (user-uid) -1))
2904      (message-number-base36 (+ (car   tm)
2905                                (lsh (% message-unique-id-char 25) 16)) 4)
2906      (message-number-base36 (+ (nth 1 tm)
2907                                (lsh (/ message-unique-id-char 25) 16)) 4)
2908      ;; Append the newsreader name, because while the generated
2909      ;; ID is unique to this newsreader, other newsreaders might
2910      ;; otherwise generate the same ID via another algorithm.
2911      ".fsf")))
2912
2913 (defun message-number-base36 (num len)
2914   (if (if (< len 0)
2915           (<= num 0)
2916         (= len 0))
2917       ""
2918     (concat (message-number-base36 (/ num 36) (1- len))
2919             (char-to-string (aref "zyxwvutsrqponmlkjihgfedcba9876543210"
2920                                   (% num 36))))))
2921
2922 (defun message-make-organization ()
2923   "Make an Organization header."
2924   (let* ((organization
2925           (when message-user-organization
2926             (if (message-functionp message-user-organization)
2927                 (funcall message-user-organization)
2928               message-user-organization))))
2929     (save-excursion
2930       (message-set-work-buffer)
2931       (cond ((stringp organization)
2932              (insert organization))
2933             ((and (eq t organization)
2934                   message-user-organization-file
2935                   (file-exists-p message-user-organization-file))
2936              (insert-file-contents message-user-organization-file)))
2937       (goto-char (point-min))
2938       (while (re-search-forward "[\t\n]+" nil t)
2939         (replace-match "" t t))
2940       (unless (zerop (buffer-size))
2941         (buffer-string)))))
2942
2943 (defun message-make-lines ()
2944   "Count the number of lines and return numeric string."
2945   (save-excursion
2946     (save-restriction
2947       (widen)
2948       (goto-char (point-min))
2949       (re-search-forward
2950        (concat "^" (regexp-quote mail-header-separator) "$"))
2951       (forward-line 1)
2952       (int-to-string (count-lines (point) (point-max))))))
2953
2954 (defun message-make-in-reply-to ()
2955   "Return the In-Reply-To header for this message."
2956   (when message-reply-headers
2957     (let ((from (mail-header-from message-reply-headers))
2958           (date (mail-header-date message-reply-headers)))
2959       (when from
2960         (let ((stop-pos
2961                (string-match "  *at \\|  *@ \\| *(\\| *<" from)))
2962           (concat (if (and stop-pos
2963                            (not (zerop stop-pos)))
2964                       (substring from 0 stop-pos) from)
2965                   "'s message of \""
2966                   (if (or (not date) (string= date ""))
2967                       "(unknown date)" date)
2968                   "\""))))))
2969
2970 (defun message-make-distribution ()
2971   "Make a Distribution header."
2972   (let ((orig-distribution (message-fetch-reply-field "distribution")))
2973     (cond ((message-functionp message-distribution-function)
2974            (funcall message-distribution-function))
2975           (t orig-distribution))))
2976
2977 (defun message-make-expires ()
2978   "Return an Expires header based on `message-expires'."
2979   (let ((current (current-time))
2980         (future (* 1.0 message-expires 60 60 24)))
2981     ;; Add the future to current.
2982     (setcar current (+ (car current) (round (/ future (expt 2 16)))))
2983     (setcar (cdr current) (+ (nth 1 current) (% (round future) (expt 2 16))))
2984     (message-make-date current)))
2985
2986 (defun message-make-path ()
2987   "Return uucp path."
2988   (let ((login-name (user-login-name)))
2989     (cond ((null message-user-path)
2990            (concat (system-name) "!" login-name))
2991           ((stringp message-user-path)
2992            ;; Support GENERICPATH.  Suggested by vixie@decwrl.dec.com.
2993            (concat message-user-path "!" login-name))
2994           (t login-name))))
2995
2996 (defun message-make-from ()
2997   "Make a From header."
2998   (let* ((style message-from-style)
2999          (login (message-make-address))
3000          (fullname
3001           (or (and (boundp 'user-full-name)
3002                    user-full-name)
3003               (user-full-name))))
3004     (when (string= fullname "&")
3005       (setq fullname (user-login-name)))
3006     (save-excursion
3007       (message-set-work-buffer)
3008       (cond
3009        ((or (null style)
3010             (equal fullname ""))
3011         (insert login))
3012        ((or (eq style 'angles)
3013             (and (not (eq style 'parens))
3014                  ;; Use angles if no quoting is needed, or if parens would
3015                  ;; need quoting too.
3016                  (or (not (string-match "[^- !#-'*+/-9=?A-Z^-~]" fullname))
3017                      (let ((tmp (concat fullname nil)))
3018                        (while (string-match "([^()]*)" tmp)
3019                          (aset tmp (match-beginning 0) ?-)
3020                          (aset tmp (1- (match-end 0)) ?-))
3021                        (string-match "[\\()]" tmp)))))
3022         (insert fullname)
3023         (goto-char (point-min))
3024         ;; Look for a character that cannot appear unquoted
3025         ;; according to RFC 822.
3026         (when (re-search-forward "[^- !#-'*+/-9=?A-Z^-~]" nil 1)
3027           ;; Quote fullname, escaping specials.
3028           (goto-char (point-min))
3029           (insert "\"")
3030           (while (re-search-forward "[\"\\]" nil 1)
3031             (replace-match "\\\\\\&" t))
3032           (insert "\""))
3033         (insert " <" login ">"))
3034        (t                               ; 'parens or default
3035         (insert login " (")
3036         (let ((fullname-start (point)))
3037           (insert fullname)
3038           (goto-char fullname-start)
3039           ;; RFC 822 says \ and nonmatching parentheses
3040           ;; must be escaped in comments.
3041           ;; Escape every instance of ()\ ...
3042           (while (re-search-forward "[()\\]" nil 1)
3043             (replace-match "\\\\\\&" t))
3044           ;; ... then undo escaping of matching parentheses,
3045           ;; including matching nested parentheses.
3046           (goto-char fullname-start)
3047           (while (re-search-forward
3048                   "\\(\\=\\|[^\\]\\(\\\\\\\\\\)*\\)\\\\(\\(\\([^\\]\\|\\\\\\\\\\)*\\)\\\\)"
3049                   nil 1)
3050             (replace-match "\\1(\\3)" t)
3051             (goto-char fullname-start)))
3052         (insert ")")))
3053       (buffer-string))))
3054
3055 (defun message-make-sender ()
3056   "Return the \"real\" user address.
3057 This function tries to ignore all user modifications, and
3058 give as trustworthy answer as possible."
3059   (concat (user-login-name) "@" (system-name)))
3060
3061 (defun message-make-address ()
3062   "Make the address of the user."
3063   (or (message-user-mail-address)
3064       (concat (user-login-name) "@" (message-make-domain))))
3065
3066 (defun message-user-mail-address ()
3067   "Return the pertinent part of `user-mail-address'."
3068   (when user-mail-address
3069     (if (string-match " " user-mail-address)
3070         (nth 1 (mail-extract-address-components user-mail-address))
3071       user-mail-address)))
3072
3073 (defun message-make-fqdn ()
3074   "Return user's fully qualified domain name."
3075   (let ((system-name (system-name))
3076         (user-mail (message-user-mail-address)))
3077     (cond
3078      ((string-match "[^.]\\.[^.]" system-name)
3079       ;; `system-name' returned the right result.
3080       system-name)
3081      ;; Try `mail-host-address'.
3082      ((and (boundp 'mail-host-address)
3083            (stringp mail-host-address)
3084            (string-match "\\." mail-host-address))
3085       mail-host-address)
3086      ;; We try `user-mail-address' as a backup.
3087      ((and user-mail
3088            (string-match "\\." user-mail)
3089            (string-match "@\\(.*\\)\\'" user-mail))
3090       (match-string 1 user-mail))
3091      ;; Default to this bogus thing.
3092      (t
3093       (concat system-name ".i-did-not-set--mail-host-address--so-shoot-me")))))
3094
3095 (defun message-make-host-name ()
3096   "Return the name of the host."
3097   (let ((fqdn (message-make-fqdn)))
3098     (string-match "^[^.]+\\." fqdn)
3099     (substring fqdn 0 (1- (match-end 0)))))
3100
3101 (defun message-make-domain ()
3102   "Return the domain name."
3103   (or mail-host-address
3104       (message-make-fqdn)))
3105
3106 (defun message-generate-headers (headers)
3107   "Prepare article HEADERS.
3108 Headers already prepared in the buffer are not modified."
3109   (save-restriction
3110     (message-narrow-to-headers)
3111     (let* ((Date (message-make-date))
3112            (Message-ID (message-make-message-id))
3113            (Organization (message-make-organization))
3114            (From (message-make-from))
3115            (Path (message-make-path))
3116            (Subject nil)
3117            (Newsgroups nil)
3118            (In-Reply-To (message-make-in-reply-to))
3119            (To nil)
3120            (Distribution (message-make-distribution))
3121            (Lines (message-make-lines))
3122            (User-Agent message-newsreader)
3123            (Expires (message-make-expires))
3124            (case-fold-search t)
3125            header value elem)
3126       ;; First we remove any old generated headers.
3127       (let ((headers message-deletable-headers))
3128         (unless (buffer-modified-p)
3129           (setq headers (delq 'Message-ID (copy-sequence headers))))
3130         (while headers
3131           (goto-char (point-min))
3132           (and (re-search-forward
3133                 (concat "^" (symbol-name (car headers)) ": *") nil t)
3134                (get-text-property (1+ (match-beginning 0)) 'message-deletable)
3135                (message-delete-line))
3136           (pop headers)))
3137       ;; Go through all the required headers and see if they are in the
3138       ;; articles already.  If they are not, or are empty, they are
3139       ;; inserted automatically - except for Subject, Newsgroups and
3140       ;; Distribution.
3141       (while headers
3142         (goto-char (point-min))
3143         (setq elem (pop headers))
3144         (if (consp elem)
3145             (if (eq (car elem) 'optional)
3146                 (setq header (cdr elem))
3147               (setq header (car elem)))
3148           (setq header elem))
3149         (when (or (not (re-search-forward
3150                         (concat "^"
3151                                 (regexp-quote
3152                                  (downcase
3153                                   (if (stringp header)
3154                                       header
3155                                     (symbol-name header))))
3156                                 ":")
3157                         nil t))
3158                   (progn
3159                     ;; The header was found.  We insert a space after the
3160                     ;; colon, if there is none.
3161                     (if (/= (char-after) ? ) (insert " ") (forward-char 1))
3162                     ;; Find out whether the header is empty...
3163                     (looking-at "[ \t]*\n[^ \t]")))
3164           ;; So we find out what value we should insert.
3165           (setq value
3166                 (cond
3167                  ((and (consp elem) (eq (car elem) 'optional))
3168                   ;; This is an optional header.  If the cdr of this
3169                   ;; is something that is nil, then we do not insert
3170                   ;; this header.
3171                   (setq header (cdr elem))
3172                   (or (and (fboundp (cdr elem)) (funcall (cdr elem)))
3173                       (and (boundp (cdr elem)) (symbol-value (cdr elem)))))
3174                  ((consp elem)
3175                   ;; The element is a cons.  Either the cdr is a
3176                   ;; string to be inserted verbatim, or it is a
3177                   ;; function, and we insert the value returned from
3178                   ;; this function.
3179                   (or (and (stringp (cdr elem)) (cdr elem))
3180                       (and (fboundp (cdr elem)) (funcall (cdr elem)))))
3181                  ((and (boundp header) (symbol-value header))
3182                   ;; The element is a symbol.  We insert the value
3183                   ;; of this symbol, if any.
3184                   (symbol-value header))
3185                  ((not (message-check-element header))
3186                   ;; We couldn't generate a value for this header,
3187                   ;; so we just ask the user.
3188                   (read-from-minibuffer
3189                    (format "Empty header for %s; enter value: " header)))))
3190           ;; Finally insert the header.
3191           (when (and value
3192                      (not (equal value "")))
3193             (save-excursion
3194               (if (bolp)
3195                   (progn
3196                     ;; This header didn't exist, so we insert it.
3197                     (goto-char (point-max))
3198                     (insert (if (stringp header) header (symbol-name header))
3199                             ": " value "\n")
3200                     (forward-line -1))
3201                 ;; The value of this header was empty, so we clear
3202                 ;; totally and insert the new value.
3203                 (delete-region (point) (gnus-point-at-eol))
3204                 (insert value))
3205               ;; Add the deletable property to the headers that require it.
3206               (and (memq header message-deletable-headers)
3207                    (progn (beginning-of-line) (looking-at "[^:]+: "))
3208                    (add-text-properties
3209                     (point) (match-end 0)
3210                     '(message-deletable t face italic) (current-buffer)))))))
3211       ;; Insert new Sender if the From is strange.
3212       (let ((from (message-fetch-field "from"))
3213             (sender (message-fetch-field "sender"))
3214             (secure-sender (message-make-sender)))
3215         (when (and from
3216                    (not (message-check-element 'sender))
3217                    (not (string=
3218                          (downcase
3219                           (cadr (mail-extract-address-components from)))
3220                          (downcase secure-sender)))
3221                    (or (null sender)
3222                        (not
3223                         (string=
3224                          (downcase
3225                           (cadr (mail-extract-address-components sender)))
3226                          (downcase secure-sender)))))
3227           (goto-char (point-min))
3228           ;; Rename any old Sender headers to Original-Sender.
3229           (when (re-search-forward "^\\(Original-\\)*Sender:" nil t)
3230             (beginning-of-line)
3231             (insert "Original-")
3232             (beginning-of-line))
3233           (when (or (message-news-p)
3234                     (string-match "@.+\\.." secure-sender))
3235             (insert "Sender: " secure-sender "\n")))))))
3236
3237 (defun message-insert-courtesy-copy ()
3238   "Insert a courtesy message in mail copies of combined messages."
3239   (let (newsgroups)
3240     (save-excursion
3241       (save-restriction
3242         (message-narrow-to-headers)
3243         (when (setq newsgroups (message-fetch-field "newsgroups"))
3244           (goto-char (point-max))
3245           (insert "Posted-To: " newsgroups "\n")))
3246       (forward-line 1)
3247       (when message-courtesy-message
3248         (cond
3249          ((string-match "%s" message-courtesy-message)
3250           (insert (format message-courtesy-message newsgroups)))
3251          (t
3252           (insert message-courtesy-message)))))))
3253
3254 ;;;
3255 ;;; Setting up a message buffer
3256 ;;;
3257
3258 (defun message-fill-address (header value)
3259   (save-restriction
3260     (narrow-to-region (point) (point))
3261     (insert (capitalize (symbol-name header))
3262             ": "
3263             (if (consp value) (car value) value)
3264             "\n")
3265     (narrow-to-region (point-min) (1- (point-max)))
3266     (let (quoted last)
3267       (goto-char (point-min))
3268       (while (not (eobp))
3269         (skip-chars-forward "^,\"" (point-max))
3270         (if (or (eq (char-after) ?,)
3271                 (eobp))
3272             (when (not quoted)
3273               (if (and (> (current-column) 78)
3274                        last)
3275                   (progn
3276                     (save-excursion
3277                       (goto-char last)
3278                       (insert "\n\t"))
3279                     (setq last (1+ (point))))
3280                 (setq last (1+ (point)))))
3281           (setq quoted (not quoted)))
3282         (unless (eobp)
3283           (forward-char 1))))
3284     (goto-char (point-max))
3285     (widen)
3286     (forward-line 1)))
3287
3288 (defun message-fill-header (header value)
3289   (let ((begin (point))
3290         (fill-column 78)
3291         (fill-prefix "\t"))
3292     (insert (capitalize (symbol-name header))
3293             ": "
3294             (if (consp value) (car value) value)
3295             "\n")
3296     (save-restriction
3297       (narrow-to-region begin (point))
3298       (fill-region-as-paragraph begin (point))
3299       ;; Tapdance around looong Message-IDs.
3300       (forward-line -1)
3301       (when (looking-at "[ \t]*$")
3302         (message-delete-line))
3303       (goto-char begin)
3304       (re-search-forward ":" nil t)
3305       (when (looking-at "\n[ \t]+")
3306         (replace-match " " t t))
3307       (goto-char (point-max)))))
3308
3309 (defun message-shorten-1 (list cut surplus)
3310   ;; Cut SURPLUS elements out of LIST, beginning with CUTth one.
3311   (setcdr (nthcdr (- cut 2) list)
3312           (nthcdr (+ (- cut 2) surplus 1) list)))
3313
3314 (defun message-shorten-references (header references)
3315   "Trim REFERENCES to be less than 31 Message-ID long, and fold them.
3316 If folding is disallowed, also check that the REFERENCES are less
3317 than 988 characters long, and if they are not, trim them until they are."
3318   (let ((maxcount 31)
3319         (count 0)
3320         (cut 6)
3321         refs)
3322     (with-temp-buffer
3323       (insert references)
3324       (goto-char (point-min))
3325       ;; Cons a list of valid references.
3326       (while (re-search-forward "<[^>]+>" nil t)
3327         (push (match-string 0) refs))
3328       (setq refs (nreverse refs)
3329             count (length refs)))
3330
3331     ;; If the list has more than MAXCOUNT elements, trim it by
3332     ;; removing the CUTth element and the required number of
3333     ;; elements that follow.
3334     (when (> count maxcount)
3335       (let ((surplus (- count maxcount)))
3336         (message-shorten-1 refs cut surplus)
3337         (decf count surplus)))
3338
3339     ;; If folding is disallowed, make sure the total length (including
3340     ;; the spaces between) will be less than MAXSIZE characters.
3341     ;;
3342     ;; Only disallow folding for News messages. At this point the headers
3343     ;; have not been generated, thus we use message-this-is-news directly.
3344     (when (and message-this-is-news message-cater-to-broken-inn)
3345       (let ((maxsize 988)
3346             (totalsize (+ (apply #'+ (mapcar #'length refs))
3347                           (1- count)))
3348             (surplus 0)
3349             (ptr (nthcdr (1- cut) refs)))
3350         ;; Decide how many elements to cut off...
3351         (while (> totalsize maxsize)
3352           (decf totalsize (1+ (length (car ptr))))
3353           (incf surplus)
3354           (setq ptr (cdr ptr)))
3355         ;; ...and do it.
3356         (when (> surplus 0)
3357           (message-shorten-1 refs cut surplus))))
3358
3359     ;; Finally, collect the references back into a string and insert
3360     ;; it into the buffer.
3361     (let ((refstring (mapconcat #'identity refs " ")))
3362       (if (and message-this-is-news message-cater-to-broken-inn)
3363           (insert (capitalize (symbol-name header)) ": "
3364                   refstring "\n")
3365         (message-fill-header header refstring)))))
3366
3367 (defun message-position-point ()
3368   "Move point to where the user probably wants to find it."
3369   (message-narrow-to-headers)
3370   (cond
3371    ((re-search-forward "^[^:]+:[ \t]*$" nil t)
3372     (search-backward ":" )
3373     (widen)
3374     (forward-char 1)
3375     (if (eq (char-after) ? )
3376         (forward-char 1)
3377       (insert " ")))
3378    (t
3379     (goto-char (point-max))
3380     (widen)
3381     (forward-line 1)
3382     (unless (looking-at "$")
3383       (forward-line 2)))
3384    (sit-for 0)))
3385
3386 (defun message-buffer-name (type &optional to group)
3387   "Return a new (unique) buffer name based on TYPE and TO."
3388   (cond
3389    ;; Generate a new buffer name The Message Way.
3390    ((eq message-generate-new-buffers 'unique)
3391     (generate-new-buffer-name
3392      (concat "*" type
3393              (if to
3394                  (concat " to "
3395                          (or (car (mail-extract-address-components to))
3396                              to) "")
3397                "")
3398              (if (and group (not (string= group ""))) (concat " on " group) "")
3399              "*")))
3400    ;; Check whether `message-generate-new-buffers' is a function,
3401    ;; and if so, call it.
3402    ((message-functionp message-generate-new-buffers)
3403     (funcall message-generate-new-buffers type to group))
3404    ((eq message-generate-new-buffers 'unsent)
3405     (generate-new-buffer-name
3406      (concat "*unsent " type
3407              (if to
3408                  (concat " to "
3409                          (or (car (mail-extract-address-components to))
3410                              to) "")
3411                "")
3412              (if (and group (not (string= group ""))) (concat " on " group) "")
3413              "*")))
3414    ;; Use standard name.
3415    (t
3416     (format "*%s message*" type))))
3417
3418 (defun message-pop-to-buffer (name)
3419   "Pop to buffer NAME, and warn if it already exists and is modified."
3420   (let ((buffer (get-buffer name)))
3421     (if (and buffer
3422              (buffer-name buffer))
3423         (progn
3424           (set-buffer (pop-to-buffer buffer))
3425           (when (and (buffer-modified-p)
3426                      (not (y-or-n-p
3427                            "Message already being composed; erase? ")))
3428             (error "Message being composed")))
3429       (set-buffer (pop-to-buffer name)))
3430     (erase-buffer)
3431     (message-mode)))
3432
3433 (defun message-do-send-housekeeping ()
3434   "Kill old message buffers."
3435   ;; We might have sent this buffer already.  Delete it from the
3436   ;; list of buffers.
3437   (setq message-buffer-list (delq (current-buffer) message-buffer-list))
3438   (while (and message-max-buffers
3439               message-buffer-list
3440               (>= (length message-buffer-list) message-max-buffers))
3441     ;; Kill the oldest buffer -- unless it has been changed.
3442     (let ((buffer (pop message-buffer-list)))
3443       (when (and (buffer-name buffer)
3444                  (not (buffer-modified-p buffer)))
3445         (kill-buffer buffer))))
3446   ;; Rename the buffer.
3447   (if message-send-rename-function
3448       (funcall message-send-rename-function)
3449     (when (string-match "\\`\\*\\(unsent \\)?" (buffer-name))
3450       (rename-buffer
3451        (concat "*sent " (substring (buffer-name) (match-end 0))) t)))
3452   ;; Push the current buffer onto the list.
3453   (when message-max-buffers
3454     (setq message-buffer-list
3455           (nconc message-buffer-list (list (current-buffer))))))
3456
3457 (defvar mc-modes-alist)
3458 (defun message-setup (headers &optional replybuffer actions)
3459   (when (and (boundp 'mc-modes-alist)
3460              (not (assq 'message-mode mc-modes-alist)))
3461     (push '(message-mode (encrypt . mc-encrypt-message)
3462                          (sign . mc-sign-message))
3463           mc-modes-alist))
3464   (when actions
3465     (setq message-send-actions actions))
3466   (setq message-reply-buffer replybuffer)
3467   (goto-char (point-min))
3468   ;; Insert all the headers.
3469   (mail-header-format
3470    (let ((h headers)
3471          (alist message-header-format-alist))
3472      (while h
3473        (unless (assq (caar h) message-header-format-alist)
3474          (push (list (caar h)) alist))
3475        (pop h))
3476      alist)
3477    headers)
3478   (delete-region (point) (progn (forward-line -1) (point)))
3479   (when message-default-headers
3480     (insert message-default-headers)
3481     (or (bolp) (insert ?\n)))
3482   (put-text-property
3483    (point)
3484    (progn
3485      (insert mail-header-separator "\n")
3486      (1- (point)))
3487    'read-only nil)
3488   (forward-line -1)
3489   (when (message-news-p)
3490     (when message-default-news-headers
3491       (insert message-default-news-headers)
3492       (or (bolp) (insert ?\n)))
3493     (when message-generate-headers-first
3494       (message-generate-headers
3495        (delq 'Lines
3496              (delq 'Subject
3497                    (copy-sequence message-required-news-headers))))))
3498   (when (message-mail-p)
3499     (when message-default-mail-headers
3500       (insert message-default-mail-headers)
3501       (or (bolp) (insert ?\n)))
3502     (when message-generate-headers-first
3503       (message-generate-headers
3504        (delq 'Lines
3505              (delq 'Subject
3506                    (copy-sequence message-required-mail-headers))))))
3507   (run-hooks 'message-signature-setup-hook)
3508   (message-insert-signature)
3509   (save-restriction
3510     (message-narrow-to-headers)
3511     (run-hooks 'message-header-setup-hook))
3512   (set-buffer-modified-p nil)
3513   (setq buffer-undo-list nil)
3514   (run-hooks 'message-setup-hook)
3515   (message-position-point)
3516   (undo-boundary))
3517
3518 (defun message-set-auto-save-file-name ()
3519   "Associate the message buffer with a file in the drafts directory."
3520   (when message-auto-save-directory
3521     (if (gnus-alive-p)
3522         (setq message-draft-article
3523               (nndraft-request-associate-buffer "drafts"))
3524       (setq buffer-file-name (expand-file-name "*message*"
3525                                                message-auto-save-directory))
3526       (setq buffer-auto-save-file-name (make-auto-save-file-name)))
3527     (clear-visited-file-modtime)
3528     (setq buffer-file-coding-system message-draft-coding-system)))
3529
3530 (defun message-disassociate-draft ()
3531   "Disassociate the message buffer from the drafts directory."
3532   (when message-draft-article
3533     (nndraft-request-expire-articles
3534      (list message-draft-article) "drafts" nil t)))
3535
3536 (defun message-insert-headers ()
3537   "Generate the headers for the article."
3538   (interactive)
3539   (save-excursion
3540     (save-restriction
3541       (message-narrow-to-headers)
3542       (when (message-news-p)
3543         (message-generate-headers
3544          (delq 'Lines
3545                (delq 'Subject
3546                      (copy-sequence message-required-news-headers)))))
3547       (when (message-mail-p)
3548         (message-generate-headers
3549          (delq 'Lines
3550                (delq 'Subject
3551                      (copy-sequence message-required-mail-headers))))))))
3552
3553 \f
3554
3555 ;;;
3556 ;;; Commands for interfacing with message
3557 ;;;
3558
3559 ;;;###autoload
3560 (defun message-mail (&optional to subject
3561                                other-headers continue switch-function
3562                                yank-action send-actions)
3563   "Start editing a mail message to be sent.
3564 OTHER-HEADERS is an alist of header/value pairs."
3565   (interactive)
3566   (let ((message-this-is-mail t))
3567     (message-pop-to-buffer (message-buffer-name "mail" to))
3568     (message-setup
3569      (nconc
3570       `((To . ,(or to "")) (Subject . ,(or subject "")))
3571       (when other-headers other-headers)))))
3572
3573 ;;;###autoload
3574 (defun message-news (&optional newsgroups subject)
3575   "Start editing a news article to be sent."
3576   (interactive)
3577   (let ((message-this-is-news t))
3578     (message-pop-to-buffer (message-buffer-name "news" nil newsgroups))
3579     (message-setup `((Newsgroups . ,(or newsgroups ""))
3580                      (Subject . ,(or subject ""))))))
3581
3582 (defun message-get-reply-headers (wide &optional to-address)
3583   (let (follow-to mct never-mct from to cc reply-to ccalist)
3584     ;; Find all relevant headers we need.
3585     (setq from (message-fetch-field "from")
3586           to (message-fetch-field "to")
3587           cc (message-fetch-field "cc")
3588           mct (message-fetch-field "mail-copies-to")
3589           reply-to (message-fetch-field "reply-to"))
3590
3591     ;; Handle special values of Mail-Copies-To.
3592     (when mct
3593       (cond ((or (equal (downcase mct) "never")
3594                  (equal (downcase mct) "nobody"))
3595              (setq never-mct t)
3596              (setq mct nil))
3597             ((or (equal (downcase mct) "always")
3598                  (equal (downcase mct) "poster"))
3599              (setq mct (or reply-to from)))))
3600
3601     (if (or (not wide)
3602             to-address)
3603         (progn
3604           (setq follow-to (list (cons 'To (or to-address reply-to from))))
3605           (when (and wide mct)
3606             (push (cons 'Cc mct) follow-to)))
3607       (let (ccalist)
3608         (save-excursion
3609           (message-set-work-buffer)
3610           (unless never-mct
3611             (insert (or reply-to from "")))
3612           (insert (if to (concat (if (bolp) "" ", ") to "") ""))
3613           (insert (if mct (concat (if (bolp) "" ", ") mct) ""))
3614           (insert (if cc (concat (if (bolp) "" ", ") cc) ""))
3615           (goto-char (point-min))
3616           (while (re-search-forward "[ \t]+" nil t)
3617             (replace-match " " t t))
3618           ;; Remove addresses that match `rmail-dont-reply-to-names'.
3619           (let ((rmail-dont-reply-to-names message-dont-reply-to-names))
3620             (insert (prog1 (rmail-dont-reply-to (buffer-string))
3621                       (erase-buffer))))
3622           (goto-char (point-min))
3623           ;; Perhaps "Mail-Copies-To: never" removed the only address?
3624           (when (eobp)
3625             (insert (or reply-to from "")))
3626           (setq ccalist
3627                 (mapcar
3628                  (lambda (addr)
3629                    (cons (mail-strip-quoted-names addr) addr))
3630                  (message-tokenize-header (buffer-string))))
3631           (let ((s ccalist))
3632             (while s
3633               (setq ccalist (delq (assoc (car (pop s)) s) ccalist)))))
3634         (setq follow-to (list (cons 'To (cdr (pop ccalist)))))
3635         (when ccalist
3636           (let ((ccs (cons 'Cc (mapconcat
3637                                 (lambda (addr) (cdr addr)) ccalist ", "))))
3638             (when (string-match "^ +" (cdr ccs))
3639               (setcdr ccs (substring (cdr ccs) (match-end 0))))
3640             (push ccs follow-to)))))
3641     follow-to))
3642
3643
3644 ;;;###autoload
3645 (defun message-reply (&optional to-address wide)
3646   "Start editing a reply to the article in the current buffer."
3647   (interactive)
3648   (let ((cur (current-buffer))
3649         from subject date reply-to to cc
3650         references message-id follow-to
3651         (inhibit-point-motion-hooks t)
3652         (message-this-is-mail t)
3653         gnus-warning)
3654     (save-restriction
3655       (message-narrow-to-head)
3656       ;; Allow customizations to have their say.
3657       (if (not wide)
3658           ;; This is a regular reply.
3659           (if (message-functionp message-reply-to-function)
3660               (setq follow-to (funcall message-reply-to-function)))
3661         ;; This is a followup.
3662         (if (message-functionp message-wide-reply-to-function)
3663             (save-excursion
3664               (setq follow-to
3665                     (funcall message-wide-reply-to-function)))))
3666       (setq message-id (message-fetch-field "message-id" t)
3667             references (message-fetch-field "references")
3668             date (message-fetch-field "date")
3669             from (message-fetch-field "from")
3670             subject (or (message-fetch-field "subject") "none"))
3671     ;; Remove any (buggy) Re:'s that are present and make a
3672     ;; proper one.
3673     (when (string-match message-subject-re-regexp subject)
3674       (setq subject (substring subject (match-end 0))))
3675     (setq subject (concat "Re: " subject))
3676
3677     (when (and (setq gnus-warning (message-fetch-field "gnus-warning"))
3678                (string-match "<[^>]+>" gnus-warning))
3679       (setq message-id (match-string 0 gnus-warning)))
3680
3681     (unless follow-to
3682       (setq follow-to (message-get-reply-headers wide to-address))))
3683
3684     (message-pop-to-buffer
3685      (message-buffer-name
3686       (if wide "wide reply" "reply") from
3687       (if wide to-address nil)))
3688
3689     (setq message-reply-headers
3690           (vector 0 subject from date message-id references 0 0 ""))
3691
3692     (message-setup
3693      `((Subject . ,subject)
3694        ,@follow-to
3695        ,@(if (or references message-id)
3696              `((References . ,(concat (or references "") (and references " ")
3697                                       (or message-id ""))))
3698            nil))
3699      cur)))
3700
3701 ;;;###autoload
3702 (defun message-wide-reply (&optional to-address)
3703   "Make a \"wide\" reply to the message in the current buffer."
3704   (interactive)
3705   (message-reply to-address t))
3706
3707 ;;;###autoload
3708 (defun message-followup (&optional to-newsgroups)
3709   "Follow up to the message in the current buffer.
3710 If TO-NEWSGROUPS, use that as the new Newsgroups line."
3711   (interactive)
3712   (let ((cur (current-buffer))
3713         from subject date reply-to mct
3714         references message-id follow-to
3715         (inhibit-point-motion-hooks t)
3716         (message-this-is-news t)
3717         followup-to distribution newsgroups gnus-warning posted-to)
3718     (save-restriction
3719       (narrow-to-region
3720        (goto-char (point-min))
3721        (if (search-forward "\n\n" nil t)
3722            (1- (point))
3723          (point-max)))
3724       (when (message-functionp message-followup-to-function)
3725         (setq follow-to
3726               (funcall message-followup-to-function)))
3727       (setq from (message-fetch-field "from")
3728             date (message-fetch-field "date")
3729             subject (or (message-fetch-field "subject") "none")
3730             references (message-fetch-field "references")
3731             message-id (message-fetch-field "message-id" t)
3732             followup-to (message-fetch-field "followup-to")
3733             newsgroups (message-fetch-field "newsgroups")
3734             posted-to (message-fetch-field "posted-to")
3735             reply-to (message-fetch-field "reply-to")
3736             distribution (message-fetch-field "distribution")
3737             mct (message-fetch-field "mail-copies-to"))
3738       (when (and (setq gnus-warning (message-fetch-field "gnus-warning"))
3739                  (string-match "<[^>]+>" gnus-warning))
3740         (setq message-id (match-string 0 gnus-warning)))
3741       ;; Remove bogus distribution.
3742       (when (and (stringp distribution)
3743                  (let ((case-fold-search t))
3744                    (string-match "world" distribution)))
3745         (setq distribution nil))
3746       ;; Remove any (buggy) Re:'s that are present and make a
3747       ;; proper one.
3748       (when (string-match message-subject-re-regexp subject)
3749         (setq subject (substring subject (match-end 0))))
3750       (setq subject (concat "Re: " subject))
3751       (widen))
3752
3753     (message-pop-to-buffer (message-buffer-name "followup" from newsgroups))
3754
3755     (message-setup
3756      `((Subject . ,subject)
3757        ,@(cond
3758           (to-newsgroups
3759            (list (cons 'Newsgroups to-newsgroups)))
3760           (follow-to follow-to)
3761           ((and followup-to message-use-followup-to)
3762            (list
3763             (cond
3764              ((equal (downcase followup-to) "poster")
3765               (if (or (eq message-use-followup-to 'use)
3766                       (message-y-or-n-p "Obey Followup-To: poster? " t "\
3767 You should normally obey the Followup-To: header.
3768
3769 `Followup-To: poster' sends your response via e-mail instead of news.
3770
3771 A typical situation where `Followup-To: poster' is used is when the poster
3772 does not read the newsgroup, so he wouldn't see any replies sent to it."))
3773                   (progn
3774                     (setq message-this-is-news nil)
3775                     (cons 'To (or reply-to from "")))
3776                 (cons 'Newsgroups newsgroups)))
3777              (t
3778               (if (or (equal followup-to newsgroups)
3779                       (not (eq message-use-followup-to 'ask))
3780                       (message-y-or-n-p
3781                        (concat "Obey Followup-To: " followup-to "? ") t "\
3782 You should normally obey the Followup-To: header.
3783
3784         `Followup-To: " followup-to "'
3785 directs your response to " (if (string-match "," followup-to)
3786                                "the specified newsgroups"
3787                              "that newsgroup only") ".
3788
3789 If a message is posted to several newsgroups, Followup-To is often
3790 used to direct the following discussion to one newsgroup only,
3791 because discussions that are spread over several newsgroup tend to
3792 be fragmented and very difficult to follow.
3793
3794 Also, some source/announcement newsgroups are not indented for discussion;
3795 responses here are directed to other newsgroups."))
3796                   (cons 'Newsgroups followup-to)
3797                 (cons 'Newsgroups newsgroups))))))
3798           (posted-to
3799            `((Newsgroups . ,posted-to)))
3800           (t
3801            `((Newsgroups . ,newsgroups))))
3802        ,@(and distribution (list (cons 'Distribution distribution)))
3803        ,@(if (or references message-id)
3804              `((References . ,(concat (or references "") (and references " ")
3805                                       (or message-id "")))))
3806        ,@(when (and mct
3807                     (not (or (equal (downcase mct) "never")
3808                              (equal (downcase mct) "nobody"))))
3809            (list (cons 'Cc (if (or (equal (downcase mct) "always")
3810                                    (equal (downcase mct) "poster"))
3811                                (or reply-to from "")
3812                              mct)))))
3813
3814      cur)
3815
3816     (setq message-reply-headers
3817           (vector 0 subject from date message-id references 0 0 ""))))
3818
3819
3820 ;;;###autoload
3821 (defun message-cancel-news (&optional arg)
3822   "Cancel an article you posted.
3823 If ARG, allow editing of the cancellation message."
3824   (interactive "P")
3825   (unless (message-news-p)
3826     (error "This is not a news article; canceling is impossible"))
3827   (when (yes-or-no-p "Do you really want to cancel this article? ")
3828     (let (from newsgroups message-id distribution buf sender)
3829       (save-excursion
3830         ;; Get header info from original article.
3831         (save-restriction
3832           (message-narrow-to-head)
3833           (setq from (message-fetch-field "from")
3834                 sender (message-fetch-field "sender")
3835                 newsgroups (message-fetch-field "newsgroups")
3836                 message-id (message-fetch-field "message-id" t)
3837                 distribution (message-fetch-field "distribution")))
3838         ;; Make sure that this article was written by the user.
3839         (unless (or (and sender
3840                          (string-equal
3841                           (downcase sender)
3842                           (downcase (message-make-sender))))
3843                     (string-equal
3844                      (downcase (cadr (mail-extract-address-components from)))
3845                      (downcase (cadr (mail-extract-address-components
3846                                       (message-make-from))))))
3847           (error "This article is not yours"))
3848         ;; Make control message.
3849         (if arg
3850             (message-news)
3851           (setq buf (set-buffer (get-buffer-create " *message cancel*"))))
3852         (erase-buffer)
3853         (insert "Newsgroups: " newsgroups "\n"
3854                 "From: " (message-make-from) "\n"
3855                 "Subject: cmsg cancel " message-id "\n"
3856                 "Control: cancel " message-id "\n"
3857                 (if distribution
3858                     (concat "Distribution: " distribution "\n")
3859                   "")
3860                 mail-header-separator "\n"
3861                 message-cancel-message)
3862         (run-hooks 'message-cancel-hook)
3863         (unless arg
3864           (message "Canceling your article...")
3865           (if (let ((message-syntax-checks
3866                      'dont-check-for-anything-just-trust-me))
3867                 (funcall message-send-news-function))
3868               (message "Canceling your article...done"))
3869           (kill-buffer buf))))))
3870
3871 ;;;###autoload
3872 (defun message-supersede ()
3873   "Start composing a message to supersede the current message.
3874 This is done simply by taking the old article and adding a Supersedes
3875 header line with the old Message-ID."
3876   (interactive)
3877   (let ((cur (current-buffer))
3878         (sender (message-fetch-field "sender"))
3879         (from (message-fetch-field "from")))
3880     ;; Check whether the user owns the article that is to be superseded.
3881     (unless (or (and sender
3882                      (string-equal
3883                       (downcase sender)
3884                       (downcase (message-make-sender))))
3885                 (string-equal
3886                  (downcase (cadr (mail-extract-address-components from)))
3887                  (downcase (cadr (mail-extract-address-components
3888                                   (message-make-from))))))
3889       (error "This article is not yours"))
3890     ;; Get a normal message buffer.
3891     (message-pop-to-buffer (message-buffer-name "supersede"))
3892     (insert-buffer-substring cur)
3893     (message-narrow-to-head)
3894     ;; Remove unwanted headers.
3895     (when message-ignored-supersedes-headers
3896       (message-remove-header message-ignored-supersedes-headers t))
3897     (goto-char (point-min))
3898     (if (not (re-search-forward "^Message-ID: " nil t))
3899         (error "No Message-ID in this article")
3900       (replace-match "Supersedes: " t t))
3901     (goto-char (point-max))
3902     (insert mail-header-separator)
3903     (widen)
3904     (forward-line 1)))
3905
3906 ;;;###autoload
3907 (defun message-recover ()
3908   "Reread contents of current buffer from its last auto-save file."
3909   (interactive)
3910   (let ((file-name (make-auto-save-file-name)))
3911     (cond ((save-window-excursion
3912              (if (not (eq system-type 'vax-vms))
3913                  (with-output-to-temp-buffer "*Directory*"
3914                    (with-current-buffer standard-output
3915                      (fundamental-mode)) ; for Emacs 20.4+
3916                    (buffer-disable-undo standard-output)
3917                    (let ((default-directory "/"))
3918                      (call-process
3919                       "ls" nil standard-output nil "-l" file-name))))
3920              (yes-or-no-p (format "Recover auto save file %s? " file-name)))
3921            (let ((buffer-read-only nil))
3922              (erase-buffer)
3923              (insert-file-contents file-name nil)))
3924           (t (error "message-recover cancelled")))))
3925
3926 ;;; Washing Subject:
3927
3928 (defun message-wash-subject (subject)
3929   "Remove junk like \"Re:\", \"(fwd)\", etc. that was added to the subject by previous forwarders, replyers, etc."
3930   (with-temp-buffer
3931     (insert-string subject)
3932     (goto-char (point-min))
3933     ;; strip Re/Fwd stuff off the beginning
3934     (while (re-search-forward
3935             "\\([Rr][Ee]:\\|[Ff][Ww][Dd]\\(\\[[0-9]*\\]\\)?:\\|[Ff][Ww]:\\)" nil t)
3936       (replace-match ""))
3937
3938     ;; and gnus-style forwards [foo@bar.com] subject
3939     (goto-char (point-min))
3940     (while (re-search-forward "\\[[^ \t]*\\(@\\|\\.\\)[^ \t]*\\]" nil t)
3941       (replace-match ""))
3942
3943     ;; and off the end
3944     (goto-char (point-max))
3945     (while (re-search-backward "([Ff][Ww][Dd])" nil t)
3946       (replace-match ""))
3947
3948     ;; and finally, any whitespace that was left-over
3949     (goto-char (point-min))
3950     (while (re-search-forward "^[ \t]+" nil t)
3951       (replace-match ""))
3952     (goto-char (point-max))
3953     (while (re-search-backward "[ \t]+$" nil t)
3954       (replace-match ""))
3955
3956     (buffer-string)))
3957
3958 ;;; Forwarding messages.
3959
3960 (defun message-forward-subject-author-subject (subject)
3961   "Generate a subject for a forwarded message.
3962 The form is: [Source] Subject, where if the original message was mail,
3963 Source is the sender, and if the original message was news, Source is
3964 the list of newsgroups is was posted to."
3965   (concat "["
3966           (or (message-fetch-field
3967                (if (message-news-p) "newsgroups" "from"))
3968               "(nowhere)")
3969           "] " subject))
3970
3971 (defun message-forward-subject-fwd (subject)
3972   "Generate a subject for a forwarded message.
3973 The form is: Fwd: Subject, where Subject is the original subject of
3974 the message."
3975   (concat "Fwd: " subject))
3976
3977 (defun message-make-forward-subject ()
3978   "Return a Subject header suitable for the message in the current buffer."
3979   (save-excursion
3980     (save-restriction
3981       (current-buffer)
3982       (message-narrow-to-head)
3983       (let ((funcs message-make-forward-subject-function)
3984             (subject (if message-wash-forwarded-subjects
3985                          (message-wash-subject
3986                           (or (message-fetch-field "Subject") ""))
3987                        (or (message-fetch-field "Subject") ""))))
3988         ;; Make sure funcs is a list.
3989         (and funcs
3990              (not (listp funcs))
3991              (setq funcs (list funcs)))
3992         ;; Apply funcs in order, passing subject generated by previous
3993         ;; func to the next one.
3994         (while funcs
3995           (when (message-functionp (car funcs))
3996             (setq subject (funcall (car funcs) subject)))
3997           (setq funcs (cdr funcs)))
3998         subject))))
3999
4000 ;;;###autoload
4001 (defun message-forward (&optional news)
4002   "Forward the current message via mail.
4003 Optional NEWS will use news to forward instead of mail."
4004   (interactive "P")
4005   (let* ((cur (current-buffer))
4006          (subject (if message-forward-show-mml
4007                       (message-make-forward-subject)
4008                     (mail-decode-encoded-word-string
4009                      (message-make-forward-subject))))
4010          art-beg)
4011     (if news
4012         (message-news nil subject)
4013       (message-mail nil subject))
4014     ;; Put point where we want it before inserting the forwarded
4015     ;; message.
4016     (if message-forward-before-signature
4017         (message-goto-body)
4018       (goto-char (point-max)))
4019     (if message-forward-as-mime
4020         (if message-forward-show-mml
4021             (insert "\n\n<#mml type=message/rfc822 disposition=inline>\n")
4022           (insert "\n\n<#part type=message/rfc822 disposition=inline"
4023                   " buffer=\"" (buffer-name cur) "\">\n"))
4024       (insert "\n-------------------- Start of forwarded message --------------------\n"))
4025     (let ((b (point))
4026           e)
4027       (if message-forward-show-mml
4028           (insert-buffer-substring cur)
4029         (unless message-forward-as-mime
4030           (mml-insert-buffer cur)))
4031       (setq e (point))
4032       (if message-forward-as-mime
4033           (if message-forward-show-mml
4034               (insert "<#/mml>\n")
4035             (insert "<#/part>\n"))
4036         (insert "\n-------------------- End of forwarded message --------------------\n"))
4037       (when (and (or message-forward-show-mml
4038                      (not message-forward-as-mime))
4039              (not current-prefix-arg)
4040              message-forward-ignored-headers)
4041         (save-restriction
4042           (narrow-to-region b e)
4043           (goto-char b)
4044           (narrow-to-region (point) (or (search-forward "\n\n" nil t) (point)))
4045           (message-remove-header message-forward-ignored-headers t))))
4046     (message-position-point)))
4047
4048 ;;;###autoload
4049 (defun message-resend (address)
4050   "Resend the current article to ADDRESS."
4051   (interactive
4052    (list (message-read-from-minibuffer "Resend message to: ")))
4053   (message "Resending message to %s..." address)
4054   (save-excursion
4055     (let ((cur (current-buffer))
4056           beg)
4057       ;; We first set up a normal mail buffer.
4058       (set-buffer (get-buffer-create " *message resend*"))
4059       (erase-buffer)
4060       (message-setup `((To . ,address)))
4061       ;; Insert our usual headers.
4062       (message-generate-headers '(From Date To))
4063       (message-narrow-to-headers)
4064       ;; Rename them all to "Resent-*".
4065       (while (re-search-forward "^[A-Za-z]" nil t)
4066         (forward-char -1)
4067         (insert "Resent-"))
4068       (widen)
4069       (forward-line)
4070       (delete-region (point) (point-max))
4071       (setq beg (point))
4072       ;; Insert the message to be resent.
4073       (insert-buffer-substring cur)
4074       (goto-char (point-min))
4075       (search-forward "\n\n")
4076       (forward-char -1)
4077       (save-restriction
4078         (narrow-to-region beg (point))
4079         (message-remove-header message-ignored-resent-headers t)
4080         (goto-char (point-max)))
4081       (insert mail-header-separator)
4082       ;; Rename all old ("Also-")Resent headers.
4083       (while (re-search-backward "^\\(Also-\\)*Resent-" beg t)
4084         (beginning-of-line)
4085         (insert "Also-"))
4086       ;; Quote any "From " lines at the beginning.
4087       (goto-char beg)
4088       (when (looking-at "From ")
4089         (replace-match "X-From-Line: "))
4090       ;; Send it.
4091       (let ((message-inhibit-body-encoding t)
4092             message-required-mail-headers)
4093         (message-send-mail))
4094       (kill-buffer (current-buffer)))
4095     (message "Resending message to %s...done" address)))
4096
4097 ;;;###autoload
4098 (defun message-bounce ()
4099   "Re-mail the current message.
4100 This only makes sense if the current message is a bounce message than
4101 contains some mail you have written which has been bounced back to
4102 you."
4103   (interactive)
4104   (let ((handles (mm-dissect-buffer t))
4105         boundary)
4106     (message-pop-to-buffer (message-buffer-name "bounce"))
4107     (if (stringp (car handles))
4108         ;; This is a MIME bounce.
4109         (mm-insert-part (car (last handles)))
4110       ;; This is a non-MIME bounce, so we try to remove things
4111       ;; manually.
4112       (mm-insert-part handles)
4113       (undo-boundary)
4114       (goto-char (point-min))
4115       (search-forward "\n\n" nil t)
4116       (or (and (re-search-forward message-unsent-separator nil t)
4117                (forward-line 1))
4118           (re-search-forward "^Return-Path:.*\n" nil t))
4119       ;; We remove everything before the bounced mail.
4120       (delete-region
4121        (point-min)
4122        (if (re-search-forward "^[^ \n\t]+:" nil t)
4123            (match-beginning 0)
4124          (point))))
4125     (save-restriction
4126       (message-narrow-to-head)
4127       (message-remove-header message-ignored-bounced-headers t)
4128       (goto-char (point-max))
4129       (insert mail-header-separator))
4130     (message-position-point)))
4131
4132 ;;;
4133 ;;; Interactive entry points for new message buffers.
4134 ;;;
4135
4136 ;;;###autoload
4137 (defun message-mail-other-window (&optional to subject)
4138   "Like `message-mail' command, but display mail buffer in another window."
4139   (interactive)
4140   (let ((pop-up-windows t)
4141         (special-display-buffer-names nil)
4142         (special-display-regexps nil)
4143         (same-window-buffer-names nil)
4144         (same-window-regexps nil))
4145     (message-pop-to-buffer (message-buffer-name "mail" to)))
4146   (let ((message-this-is-mail t))
4147     (message-setup `((To . ,(or to "")) (Subject . ,(or subject ""))))))
4148
4149 ;;;###autoload
4150 (defun message-mail-other-frame (&optional to subject)
4151   "Like `message-mail' command, but display mail buffer in another frame."
4152   (interactive)
4153   (let ((pop-up-frames t)
4154         (special-display-buffer-names nil)
4155         (special-display-regexps nil)
4156         (same-window-buffer-names nil)
4157         (same-window-regexps nil))
4158     (message-pop-to-buffer (message-buffer-name "mail" to)))
4159   (let ((message-this-is-mail t))
4160     (message-setup `((To . ,(or to "")) (Subject . ,(or subject ""))))))
4161
4162 ;;;###autoload
4163 (defun message-news-other-window (&optional newsgroups subject)
4164   "Start editing a news article to be sent."
4165   (interactive)
4166   (let ((pop-up-windows t)
4167         (special-display-buffer-names nil)
4168         (special-display-regexps nil)
4169         (same-window-buffer-names nil)
4170         (same-window-regexps nil))
4171     (message-pop-to-buffer (message-buffer-name "news" nil newsgroups)))
4172   (let ((message-this-is-news t))
4173     (message-setup `((Newsgroups . ,(or newsgroups ""))
4174                      (Subject . ,(or subject ""))))))
4175
4176 ;;;###autoload
4177 (defun message-news-other-frame (&optional newsgroups subject)
4178   "Start editing a news article to be sent."
4179   (interactive)
4180   (let ((pop-up-frames t)
4181         (special-display-buffer-names nil)
4182         (special-display-regexps nil)
4183         (same-window-buffer-names nil)
4184         (same-window-regexps nil))
4185     (message-pop-to-buffer (message-buffer-name "news" nil newsgroups)))
4186   (let ((message-this-is-news t))
4187     (message-setup `((Newsgroups . ,(or newsgroups ""))
4188                      (Subject . ,(or subject ""))))))
4189
4190 ;;; underline.el
4191
4192 ;; This code should be moved to underline.el (from which it is stolen).
4193
4194 ;;;###autoload
4195 (defun bold-region (start end)
4196   "Bold all nonblank characters in the region.
4197 Works by overstriking characters.
4198 Called from program, takes two arguments START and END
4199 which specify the range to operate on."
4200   (interactive "r")
4201   (save-excursion
4202     (let ((end1 (make-marker)))
4203       (move-marker end1 (max start end))
4204       (goto-char (min start end))
4205       (while (< (point) end1)
4206         (or (looking-at "[_\^@- ]")
4207             (insert (char-after) "\b"))
4208         (forward-char 1)))))
4209
4210 ;;;###autoload
4211 (defun unbold-region (start end)
4212   "Remove all boldness (overstruck characters) in the region.
4213 Called from program, takes two arguments START and END
4214 which specify the range to operate on."
4215   (interactive "r")
4216   (save-excursion
4217     (let ((end1 (make-marker)))
4218       (move-marker end1 (max start end))
4219       (goto-char (min start end))
4220       (while (re-search-forward "\b" end1 t)
4221         (if (eq (char-after) (char-after (- (point) 2)))
4222             (delete-char -2))))))
4223
4224 (defalias 'message-exchange-point-and-mark 'exchange-point-and-mark)
4225
4226 ;; Support for toolbar
4227 (when (string-match "XEmacs\\|Lucid" emacs-version)
4228   (require 'messagexmas))
4229
4230 ;;; Group name completion.
4231
4232 (defvar message-newgroups-header-regexp
4233   "^\\(Newsgroups\\|Followup-To\\|Posted-To\\|Gcc\\):"
4234   "Regexp that match headers that lists groups.")
4235
4236 (defun message-tab ()
4237   "Expand group names in Newsgroups and Followup-To headers.
4238 Do a `tab-to-tab-stop' if not in those headers."
4239   (interactive)
4240   (if (let ((mail-abbrev-mode-regexp message-newgroups-header-regexp))
4241         (mail-abbrev-in-expansion-header-p))
4242       (message-expand-group)
4243     (tab-to-tab-stop)))
4244
4245 (defvar gnus-active-hashtb)
4246 (defun message-expand-group ()
4247   "Expand the group name under point."
4248   (let* ((b (save-excursion
4249               (save-restriction
4250                 (narrow-to-region
4251                  (save-excursion
4252                    (beginning-of-line)
4253                    (skip-chars-forward "^:")
4254                    (1+ (point)))
4255                  (point))
4256                 (skip-chars-backward "^, \t\n") (point))))
4257          (completion-ignore-case t)
4258          (string (buffer-substring b (progn (skip-chars-forward "^,\t\n ")
4259                                             (point))))
4260          (hashtb (and (boundp 'gnus-active-hashtb) gnus-active-hashtb))
4261          (completions (all-completions string hashtb))
4262          comp)
4263     (delete-region b (point))
4264     (cond
4265      ((= (length completions) 1)
4266       (if (string= (car completions) string)
4267           (progn
4268             (insert string)
4269             (message "Only matching group"))
4270         (insert (car completions))))
4271      ((and (setq comp (try-completion string hashtb))
4272            (not (string= comp string)))
4273       (insert comp))
4274      (t
4275       (insert string)
4276       (if (not comp)
4277           (message "No matching groups")
4278         (save-selected-window
4279           (pop-to-buffer "*Completions*")
4280           (buffer-disable-undo)
4281           (let ((buffer-read-only nil))
4282             (erase-buffer)
4283             (let ((standard-output (current-buffer)))
4284               (display-completion-list (sort completions 'string<)))
4285             (goto-char (point-min))
4286             (delete-region (point) (progn (forward-line 3) (point))))))))))
4287
4288 ;;; Help stuff.
4289
4290 (defun message-talkative-question (ask question show &rest text)
4291   "Call FUNCTION with argument QUESTION; optionally display TEXT... args.
4292 If SHOW is non-nil, the arguments TEXT... are displayed in a temp buffer.
4293 The following arguments may contain lists of values."
4294   (if (and show
4295            (setq text (message-flatten-list text)))
4296       (save-window-excursion
4297         (save-excursion
4298           (with-output-to-temp-buffer " *MESSAGE information message*"
4299             (set-buffer " *MESSAGE information message*")
4300             (fundamental-mode)          ; for Emacs 20.4+
4301             (mapcar 'princ text)
4302             (goto-char (point-min))))
4303         (funcall ask question))
4304     (funcall ask question)))
4305
4306 (defun message-flatten-list (list)
4307   "Return a new, flat list that contains all elements of LIST.
4308
4309 \(message-flatten-list '(1 (2 3 (4 5 (6))) 7))
4310 => (1 2 3 4 5 6 7)"
4311   (cond ((consp list)
4312          (apply 'append (mapcar 'message-flatten-list list)))
4313         (list
4314          (list list))))
4315
4316 (defun message-generate-new-buffer-clone-locals (name &optional varstr)
4317   "Create and return a buffer with a name based on NAME using generate-new-buffer.
4318 Then clone the local variables and values from the old buffer to the
4319 new one, cloning only the locals having a substring matching the
4320 regexp varstr."
4321   (let ((oldbuf (current-buffer)))
4322     (save-excursion
4323       (set-buffer (generate-new-buffer name))
4324       (message-clone-locals oldbuf varstr)
4325       (current-buffer))))
4326
4327 (defun message-clone-locals (buffer &optional varstr)
4328   "Clone the local variables from BUFFER to the current buffer."
4329   (let ((locals (save-excursion
4330                   (set-buffer buffer)
4331                   (buffer-local-variables)))
4332         (regexp "^gnus\\|^nn\\|^message\\|^user-mail-address"))
4333     (mapcar
4334      (lambda (local)
4335        (when (and (consp local)
4336                   (car local)
4337                   (string-match regexp (symbol-name (car local)))
4338                   (or (null varstr)
4339                       (string-match varstr (symbol-name (car local)))))
4340          (ignore-errors
4341            (set (make-local-variable (car local))
4342                 (cdr local)))))
4343      locals)))
4344
4345 ;;; Miscellaneous functions
4346
4347 ;; stolen (and renamed) from nnheader.el
4348 (defun message-replace-chars-in-string (string from to)
4349   "Replace characters in STRING from FROM to TO."
4350   (let ((string (substring string 0))   ;Copy string.
4351         (len (length string))
4352         (idx 0))
4353     ;; Replace all occurrences of FROM with TO.
4354     (while (< idx len)
4355       (when (= (aref string idx) from)
4356         (aset string idx to))
4357       (setq idx (1+ idx)))
4358     string))
4359
4360 ;;;
4361 ;;; MIME functions
4362 ;;;
4363
4364 (defvar message-inhibit-body-encoding nil)
4365
4366 (defun message-encode-message-body ()
4367   (unless message-inhibit-body-encoding 
4368     (let ((mail-parse-charset (or mail-parse-charset
4369                                   message-default-charset))
4370           (case-fold-search t)
4371           lines content-type-p)
4372       (message-goto-body)
4373       (save-restriction
4374         (narrow-to-region (point) (point-max))
4375         (let ((new (mml-generate-mime)))
4376           (when new
4377             (delete-region (point-min) (point-max))
4378             (insert new)
4379             (goto-char (point-min))
4380             (if (eq (aref new 0) ?\n)
4381                 (delete-char 1)
4382               (search-forward "\n\n")
4383               (setq lines (buffer-substring (point-min) (1- (point))))
4384               (delete-region (point-min) (point))))))
4385       (save-restriction
4386         (message-narrow-to-headers-or-head)
4387         (message-remove-header "Mime-Version")
4388         (goto-char (point-max))
4389         (insert "MIME-Version: 1.0\n")
4390         (when lines
4391           (insert lines))
4392         (setq content-type-p
4393               (re-search-backward "^Content-Type:" nil t)))
4394       (save-restriction
4395         (message-narrow-to-headers-or-head)
4396         (message-remove-first-header "Content-Type")
4397         (message-remove-first-header "Content-Transfer-Encoding"))
4398       ;; We always make sure that the message has a Content-Type header.
4399       ;; This is because some broken MTAs and MUAs get awfully confused
4400       ;; when confronted with a message with a MIME-Version header and
4401       ;; without a Content-Type header.  For instance, Solaris'
4402       ;; /usr/bin/mail.
4403       (unless content-type-p
4404         (goto-char (point-min))
4405         (re-search-forward "^MIME-Version:")
4406         (forward-line 1)
4407         (insert "Content-Type: text/plain; charset=us-ascii\n")))))
4408
4409 (defun message-read-from-minibuffer (prompt)
4410   "Read from the minibuffer while providing abbrev expansion."
4411   (if (fboundp 'mail-abbrevs-setup)
4412       (let ((mail-abbrev-mode-regexp "")
4413             (minibuffer-setup-hook 'mail-abbrevs-setup))
4414         (read-from-minibuffer prompt)))
4415   (let ((minibuffer-setup-hook 'mail-abbrev-minibuffer-setup-hook))
4416     (read-string prompt)))
4417
4418 (provide 'message)
4419
4420 (run-hooks 'message-load-hook)
4421
4422 ;; Local Variables:
4423 ;; coding: iso-8859-1
4424 ;; End:
4425
4426 ;;; message.el ends here