* gnus.el: Fix copyright statements.
[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-before-signature t
303   "*If non-nil, put forwarded message before signature, else after."
304   :group 'message-forwarding
305   :type 'boolean)
306
307 (defcustom message-wash-forwarded-subjects nil
308   "*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."
309   :group 'message-forwarding
310   :type 'boolean)
311
312 (defcustom message-ignored-resent-headers "^Return-receipt\\|^X-Gnus\\|^Gnus-Warning:"
313   "*All headers that match this regexp will be deleted when resending a message."
314   :group 'message-interface
315   :type 'regexp)
316
317 (defcustom message-forward-ignored-headers "Content-Transfer-Encoding"
318   "*All headers that match this regexp will be deleted when forwarding a message."
319   :group 'message-forwarding
320   :type '(choice (const :tag "None" nil)
321                  regexp))
322
323 (defcustom message-ignored-cited-headers "."
324   "*Delete these headers from the messages you yank."
325   :group 'message-insertion
326   :type 'regexp)
327
328 (defcustom message-cancel-message "I am canceling my own article.\n"
329   "Message to be inserted in the cancel message."
330   :group 'message-interface
331   :type 'string)
332
333 ;; Useful to set in site-init.el
334 ;;;###autoload
335 (defcustom message-send-mail-function 'message-send-mail-with-sendmail
336   "Function to call to send the current buffer as mail.
337 The headers should be delimited by a line whose contents match the
338 variable `mail-header-separator'.
339
340 Valid values include `message-send-mail-with-sendmail' (the default),
341 `message-send-mail-with-mh', `message-send-mail-with-qmail' and
342 `smtpmail-send-it'."
343   :type '(radio (function-item message-send-mail-with-sendmail)
344                 (function-item message-send-mail-with-mh)
345                 (function-item message-send-mail-with-qmail)
346                 (function-item smtpmail-send-it)
347                 (function :tag "Other"))
348   :group 'message-sending
349   :group 'message-mail)
350
351 (defcustom message-send-news-function 'message-send-news
352   "Function to call to send the current buffer as news.
353 The headers should be delimited by a line whose contents match the
354 variable `mail-header-separator'."
355   :group 'message-sending
356   :group 'message-news
357   :type 'function)
358
359 (defcustom message-reply-to-function nil
360   "Function that should return a list of headers.
361 This function should pick out addresses from the To, Cc, and From headers
362 and respond with new To and Cc headers."
363   :group 'message-interface
364   :type 'function)
365
366 (defcustom message-wide-reply-to-function nil
367   "Function that should return a list of headers.
368 This function should pick out addresses from the To, Cc, and From headers
369 and respond with new To and Cc headers."
370   :group 'message-interface
371   :type 'function)
372
373 (defcustom message-followup-to-function nil
374   "Function that should return a list of headers.
375 This function should pick out addresses from the To, Cc, and From headers
376 and respond with new To and Cc headers."
377   :group 'message-interface
378   :type 'function)
379
380 (defcustom message-use-followup-to 'ask
381   "*Specifies what to do with Followup-To header.
382 If nil, always ignore the header.  If it is t, use its value, but
383 query before using the \"poster\" value.  If it is the symbol `ask',
384 always query the user whether to use the value.  If it is the symbol
385 `use', always use the value."
386   :group 'message-interface
387   :type '(choice (const :tag "ignore" nil)
388                  (const use)
389                  (const ask)))
390
391 (defcustom message-sendmail-f-is-evil nil
392   "*Non-nil means that \"-f username\" should not be added to the sendmail command line.
393 Doing so would be even more evil than leaving it out."
394   :group 'message-sending
395   :type 'boolean)
396
397 ;; qmail-related stuff
398 (defcustom message-qmail-inject-program "/var/qmail/bin/qmail-inject"
399   "Location of the qmail-inject program."
400   :group 'message-sending
401   :type 'file)
402
403 (defcustom message-qmail-inject-args nil
404   "Arguments passed to qmail-inject programs.
405 This should be a list of strings, one string for each argument.
406
407 For e.g., if you wish to set the envelope sender address so that bounces
408 go to the right place or to deal with listserv's usage of that address, you
409 might set this variable to '(\"-f\" \"you@some.where\")."
410   :group 'message-sending
411   :type '(repeat string))
412
413 (defvar message-cater-to-broken-inn t
414   "Non-nil means Gnus should not fold the `References' header.
415 Folding `References' makes ancient versions of INN create incorrect
416 NOV lines.")
417
418 (defvar gnus-post-method)
419 (defvar gnus-select-method)
420 (defcustom message-post-method
421   (cond ((and (boundp 'gnus-post-method)
422               (listp gnus-post-method)
423               gnus-post-method)
424          gnus-post-method)
425         ((boundp 'gnus-select-method)
426          gnus-select-method)
427         (t '(nnspool "")))
428   "*Method used to post news.
429 Note that when posting from inside Gnus, for instance, this
430 variable isn't used."
431   :group 'message-news
432   :group 'message-sending
433   ;; This should be the `gnus-select-method' widget, but that might
434   ;; create a dependence to `gnus.el'.
435   :type 'sexp)
436
437 (defcustom message-generate-headers-first nil
438   "*If non-nil, generate all possible headers before composing."
439   :group 'message-headers
440   :type 'boolean)
441
442 (defcustom message-setup-hook nil
443   "Normal hook, run each time a new outgoing message is initialized.
444 The function `message-setup' runs this hook."
445   :group 'message-various
446   :type 'hook)
447
448 (defcustom message-cancel-hook nil
449   "Hook run when cancelling articles."
450   :group 'message-various
451   :type 'hook)
452
453 (defcustom message-signature-setup-hook nil
454   "Normal hook, run each time a new outgoing message is initialized.
455 It is run after the headers have been inserted and before
456 the signature is inserted."
457   :group 'message-various
458   :type 'hook)
459
460 (defcustom message-mode-hook nil
461   "Hook run in message mode buffers."
462   :group 'message-various
463   :type 'hook)
464
465 (defcustom message-header-hook nil
466   "Hook run in a message mode buffer narrowed to the headers."
467   :group 'message-various
468   :type 'hook)
469
470 (defcustom message-header-setup-hook nil
471   "Hook called narrowed to the headers when setting up a message buffer."
472   :group 'message-various
473   :type 'hook)
474
475 ;;;###autoload
476 (defcustom message-citation-line-function 'message-insert-citation-line
477   "*Function called to insert the \"Whomever writes:\" line."
478   :type 'function
479   :group 'message-insertion)
480
481 ;;;###autoload
482 (defcustom message-yank-prefix "> "
483   "*Prefix inserted on the lines of yanked messages."
484   :type 'string
485   :group 'message-insertion)
486
487 (defcustom message-indentation-spaces 3
488   "*Number of spaces to insert at the beginning of each cited line.
489 Used by `message-yank-original' via `message-yank-cite'."
490   :group 'message-insertion
491   :type 'integer)
492
493 ;;;###autoload
494 (defcustom message-cite-function 'message-cite-original
495   "*Function for citing an original message.
496 Predefined functions include `message-cite-original' and
497 `message-cite-original-without-signature'.
498 Note that `message-cite-original' uses `mail-citation-hook' if that is non-nil."
499   :type '(radio (function-item message-cite-original)
500                 (function-item message-cite-original-without-signature)
501                 (function-item sc-cite-original)
502                 (function :tag "Other"))
503   :group 'message-insertion)
504
505 ;;;###autoload
506 (defcustom message-indent-citation-function 'message-indent-citation
507   "*Function for modifying a citation just inserted in the mail buffer.
508 This can also be a list of functions.  Each function can find the
509 citation between (point) and (mark t).  And each function should leave
510 point and mark around the citation text as modified."
511   :type 'function
512   :group 'message-insertion)
513
514 (defvar message-abbrevs-loaded nil)
515
516 ;;;###autoload
517 (defcustom message-signature t
518   "*String to be inserted at the end of the message buffer.
519 If t, the `message-signature-file' file will be inserted instead.
520 If a function, the result from the function will be used instead.
521 If a form, the result from the form will be used instead."
522   :type 'sexp
523   :group 'message-insertion)
524
525 ;;;###autoload
526 (defcustom message-signature-file "~/.signature"
527   "*File containing the text inserted at end of message buffer."
528   :type 'file
529   :group 'message-insertion)
530
531 (defcustom message-distribution-function nil
532   "*Function called to return a Distribution header."
533   :group 'message-news
534   :group 'message-headers
535   :type 'function)
536
537 (defcustom message-expires 14
538   "Number of days before your article expires."
539   :group 'message-news
540   :group 'message-headers
541   :link '(custom-manual "(message)News Headers")
542   :type 'integer)
543
544 (defcustom message-user-path nil
545   "If nil, use the NNTP server name in the Path header.
546 If stringp, use this; if non-nil, use no host name (user name only)."
547   :group 'message-news
548   :group 'message-headers
549   :link '(custom-manual "(message)News Headers")
550   :type '(choice (const :tag "nntp" nil)
551                  (string :tag "name")
552                  (sexp :tag "none" :format "%t" t)))
553
554 (defvar message-reply-buffer nil)
555 (defvar message-reply-headers nil)
556 (defvar message-newsreader nil)
557 (defvar message-mailer nil)
558 (defvar message-sent-message-via nil)
559 (defvar message-checksum nil)
560 (defvar message-send-actions nil
561   "A list of actions to be performed upon successful sending of a message.")
562 (defvar message-exit-actions nil
563   "A list of actions to be performed upon exiting after sending a message.")
564 (defvar message-kill-actions nil
565   "A list of actions to be performed before killing a message buffer.")
566 (defvar message-postpone-actions nil
567   "A list of actions to be performed after postponing a message.")
568
569 (define-widget 'message-header-lines 'text
570   "All header lines must be LFD terminated."
571   :format "%t:%n%v"
572   :valid-regexp "^\\'"
573   :error "All header lines must be newline terminated")
574
575 (defcustom message-default-headers ""
576   "*A string containing header lines to be inserted in outgoing messages.
577 It is inserted before you edit the message, so you can edit or delete
578 these lines."
579   :group 'message-headers
580   :type 'message-header-lines)
581
582 (defcustom message-default-mail-headers ""
583   "*A string of header lines to be inserted in outgoing mails."
584   :group 'message-headers
585   :group 'message-mail
586   :type 'message-header-lines)
587
588 (defcustom message-default-news-headers ""
589   "*A string of header lines to be inserted in outgoing news articles."
590   :group 'message-headers
591   :group 'message-news
592   :type 'message-header-lines)
593
594 ;; Note: could use /usr/ucb/mail instead of sendmail;
595 ;; options -t, and -v if not interactive.
596 (defcustom message-mailer-swallows-blank-line
597   (if (and (string-match "sparc-sun-sunos\\(\\'\\|[^5]\\)"
598                          system-configuration)
599            (file-readable-p "/etc/sendmail.cf")
600            (let ((buffer (get-buffer-create " *temp*")))
601              (unwind-protect
602                  (save-excursion
603                    (set-buffer buffer)
604                    (insert-file-contents "/etc/sendmail.cf")
605                    (goto-char (point-min))
606                    (let ((case-fold-search nil))
607                      (re-search-forward "^OR\\>" nil t)))
608                (kill-buffer buffer))))
609       ;; According to RFC822, "The field-name must be composed of printable
610       ;; ASCII characters (i. e., characters that have decimal values between
611       ;; 33 and 126, except colon)", i. e., any chars except ctl chars,
612       ;; space, or colon.
613       '(looking-at "[ \t]\\|[][!\"#$%&'()*+,-./0-9;<=>?@A-Z\\\\^_`a-z{|}~]+:"))
614   "*Set this non-nil if the system's mailer runs the header and body together.
615 \(This problem exists on Sunos 4 when sendmail is run in remote mode.)
616 The value should be an expression to test whether the problem will
617 actually occur."
618   :group 'message-sending
619   :type 'sexp)
620
621 ;; Ignore errors in case this is used in Emacs 19.
622 ;; Don't use ignore-errors because this is copied into loaddefs.el.
623 ;;;###autoload
624 (ignore-errors
625   (define-mail-user-agent 'message-user-agent
626     'message-mail 'message-send-and-exit
627     'message-kill-buffer 'message-send-hook))
628
629 (defvar message-mh-deletable-headers '(Message-ID Date Lines Sender)
630   "If non-nil, delete the deletable headers before feeding to mh.")
631
632 (defvar message-send-method-alist
633   '((news message-news-p message-send-via-news)
634     (mail message-mail-p message-send-via-mail))
635   "Alist of ways to send outgoing messages.
636 Each element has the form
637
638   \(TYPE PREDICATE FUNCTION)
639
640 where TYPE is a symbol that names the method; PREDICATE is a function
641 called without any parameters to determine whether the message is
642 a message of type TYPE; and FUNCTION is a function to be called if
643 PREDICATE returns non-nil.  FUNCTION is called with one parameter --
644 the prefix.")
645
646 (defvar message-mail-alias-type 'abbrev
647   "*What alias expansion type to use in Message buffers.
648 The default is `abbrev', which uses mailabbrev.  nil switches
649 mail aliases off.")
650
651 (defcustom message-auto-save-directory
652   (nnheader-concat message-directory "drafts/")
653   "*Directory where Message auto-saves buffers if Gnus isn't running.
654 If nil, Message won't auto-save."
655   :group 'message-buffers
656   :type 'directory)
657
658 (defcustom message-buffer-naming-style 'unique
659   "*The way new message buffers are named.
660 Valid valued are `unique' and `unsent'."
661   :group 'message-buffers
662   :type '(choice (const :tag "unique" unique)
663                  (const :tag "unsent" unsent)))
664
665 (defcustom message-default-charset nil
666   "Default charset used in non-MULE XEmacsen."
667   :group 'message
668   :type 'symbol)
669
670 (defcustom message-dont-reply-to-names rmail-dont-reply-to-names
671   "*A regexp specifying names to prune when doing wide replies.
672 A value of nil means exclude your own name only."
673   :group 'message
674   :type '(choice (const :tag "Yourself" nil)
675                  regexp))
676
677 ;;; Internal variables.
678 ;;; Well, not really internal.
679
680 (defvar message-mode-syntax-table
681   (let ((table (copy-syntax-table text-mode-syntax-table)))
682     (modify-syntax-entry ?% ". " table)
683     (modify-syntax-entry ?> ". " table)
684     (modify-syntax-entry ?< ". " table)
685     table)
686   "Syntax table used while in Message mode.")
687
688 (defvar message-mode-abbrev-table text-mode-abbrev-table
689   "Abbrev table used in Message mode buffers.
690 Defaults to `text-mode-abbrev-table'.")
691 (defgroup message-headers nil
692   "Message headers."
693   :link '(custom-manual "(message)Variables")
694   :group 'message)
695
696 (defface message-header-to-face
697   '((((class color)
698       (background dark))
699      (:foreground "green2" :bold t))
700     (((class color)
701       (background light))
702      (:foreground "MidnightBlue" :bold t))
703     (t
704      (:bold t :italic t)))
705   "Face used for displaying From headers."
706   :group 'message-faces)
707
708 (defface message-header-cc-face
709   '((((class color)
710       (background dark))
711      (:foreground "green4" :bold t))
712     (((class color)
713       (background light))
714      (:foreground "MidnightBlue"))
715     (t
716      (:bold t)))
717   "Face used for displaying Cc headers."
718   :group 'message-faces)
719
720 (defface message-header-subject-face
721   '((((class color)
722       (background dark))
723      (:foreground "green3"))
724     (((class color)
725       (background light))
726      (:foreground "navy blue" :bold t))
727     (t
728      (:bold t)))
729   "Face used for displaying subject headers."
730   :group 'message-faces)
731
732 (defface message-header-newsgroups-face
733   '((((class color)
734       (background dark))
735      (:foreground "yellow" :bold t :italic t))
736     (((class color)
737       (background light))
738      (:foreground "blue4" :bold t :italic t))
739     (t
740      (:bold t :italic t)))
741   "Face used for displaying newsgroups headers."
742   :group 'message-faces)
743
744 (defface message-header-other-face
745   '((((class color)
746       (background dark))
747      (:foreground "#b00000"))
748     (((class color)
749       (background light))
750      (:foreground "steel blue"))
751     (t
752      (:bold t :italic t)))
753   "Face used for displaying newsgroups headers."
754   :group 'message-faces)
755
756 (defface message-header-name-face
757   '((((class color)
758       (background dark))
759      (:foreground "DarkGreen"))
760     (((class color)
761       (background light))
762      (:foreground "cornflower blue"))
763     (t
764      (:bold t)))
765   "Face used for displaying header names."
766   :group 'message-faces)
767
768 (defface message-header-xheader-face
769   '((((class color)
770       (background dark))
771      (:foreground "blue"))
772     (((class color)
773       (background light))
774      (:foreground "blue"))
775     (t
776      (:bold t)))
777   "Face used for displaying X-Header headers."
778   :group 'message-faces)
779
780 (defface message-separator-face
781   '((((class color)
782       (background dark))
783      (:foreground "blue3"))
784     (((class color)
785       (background light))
786      (:foreground "brown"))
787     (t
788      (:bold t)))
789   "Face used for displaying the separator."
790   :group 'message-faces)
791
792 (defface message-cited-text-face
793   '((((class color)
794       (background dark))
795      (:foreground "red"))
796     (((class color)
797       (background light))
798      (:foreground "red"))
799     (t
800      (:bold t)))
801   "Face used for displaying cited text names."
802   :group 'message-faces)
803
804 (defface message-mml-face
805   '((((class color)
806       (background dark))
807      (:foreground "ForestGreen"))
808     (((class color)
809       (background light))
810      (:foreground "ForestGreen"))
811     (t
812      (:bold t)))
813   "Face used for displaying MML."
814   :group 'message-faces)
815
816 (defvar message-font-lock-keywords
817   (let* ((cite-prefix "A-Za-z")
818          (cite-suffix (concat cite-prefix "0-9_.@-"))
819          (content "[ \t]*\\(.+\\(\n[ \t].*\\)*\\)"))
820     `((,(concat "^\\([Tt]o:\\)" content)
821        (1 'message-header-name-face)
822        (2 'message-header-to-face nil t))
823       (,(concat "^\\(^[GBF]?[Cc][Cc]:\\|^[Rr]eply-[Tt]o:\\)" content)
824        (1 'message-header-name-face)
825        (2 'message-header-cc-face nil t))
826       (,(concat "^\\([Ss]ubject:\\)" content)
827        (1 'message-header-name-face)
828        (2 'message-header-subject-face nil t))
829       (,(concat "^\\([Nn]ewsgroups:\\|Followup-[Tt]o:\\)" content)
830        (1 'message-header-name-face)
831        (2 'message-header-newsgroups-face nil t))
832       (,(concat "^\\([A-Z][^: \n\t]+:\\)" content)
833        (1 'message-header-name-face)
834        (2 'message-header-other-face nil t))
835       (,(concat "^\\(X-[A-Za-z0-9-]+\\|In-Reply-To\\):" content)
836        (1 'message-header-name-face)
837        (2 'message-header-name-face))
838       ,@(if (and mail-header-separator
839                  (not (equal mail-header-separator "")))
840             `((,(concat "^\\(" (regexp-quote mail-header-separator) "\\)$")
841                1 'message-separator-face))
842           nil)
843       (,(concat "^[ \t]*"
844                 "\\([" cite-prefix "]+[" cite-suffix "]*\\)?"
845                 "[:>|}].*")
846        (0 'message-cited-text-face))
847       ("<#/?\\(multipart\\|part\\|external\\).*>"
848        (0 'message-mml-face))))
849   "Additional expressions to highlight in Message mode.")
850
851 ;; XEmacs does it like this.  For Emacs, we have to set the
852 ;; `font-lock-defaults' buffer-local variable.
853 (put 'message-mode 'font-lock-defaults '(message-font-lock-keywords t))
854
855 (defvar message-face-alist
856   '((bold . bold-region)
857     (underline . underline-region)
858     (default . (lambda (b e)
859                  (unbold-region b e)
860                  (ununderline-region b e))))
861   "Alist of mail and news faces for facemenu.
862 The cdr of ech entry is a function for applying the face to a region.")
863
864 (defcustom message-send-hook nil
865   "Hook run before sending messages."
866   :group 'message-various
867   :options '(ispell-message)
868   :type 'hook)
869
870 (defcustom message-send-mail-hook nil
871   "Hook run before sending mail messages."
872   :group 'message-various
873   :type 'hook)
874
875 (defcustom message-send-news-hook nil
876   "Hook run before sending news messages."
877   :group 'message-various
878   :type 'hook)
879
880 (defcustom message-sent-hook nil
881   "Hook run after sending messages."
882   :group 'message-various
883   :type 'hook)
884
885 (defvar message-send-coding-system 'binary
886   "Coding system to encode outgoing mail.")
887
888 (defvar message-draft-coding-system
889   mm-auto-save-coding-system
890   "Coding system to compose mail.")
891
892 ;;; Internal variables.
893
894 (defvar message-buffer-list nil)
895 (defvar message-this-is-news nil)
896 (defvar message-this-is-mail nil)
897 (defvar message-draft-article nil)
898 (defvar message-mime-part nil)
899 (defvar message-posting-charset nil)
900
901 ;; Byte-compiler warning
902 (defvar gnus-active-hashtb)
903 (defvar gnus-read-active-file)
904
905 ;;; Regexp matching the delimiter of messages in UNIX mail format
906 ;;; (UNIX From lines), minus the initial ^.  It should be a copy
907 ;;; of rmail.el's rmail-unix-mail-delimiter.
908 (defvar message-unix-mail-delimiter
909   (let ((time-zone-regexp
910          (concat "\\([A-Z]?[A-Z]?[A-Z][A-Z]\\( DST\\)?"
911                  "\\|[-+]?[0-9][0-9][0-9][0-9]"
912                  "\\|"
913                  "\\) *")))
914     (concat
915      "From "
916
917      ;; Many things can happen to an RFC 822 mailbox before it is put into
918      ;; a `From' line.  The leading phrase can be stripped, e.g.
919      ;; `Joe <@w.x:joe@y.z>' -> `<@w.x:joe@y.z>'.  The <> can be stripped, e.g.
920      ;; `<@x.y:joe@y.z>' -> `@x.y:joe@y.z'.  Everything starting with a CRLF
921      ;; can be removed, e.g.
922      ;;         From: joe@y.z (Joe      K
923      ;;                 User)
924      ;; can yield `From joe@y.z (Joe    K Fri Mar 22 08:11:15 1996', and
925      ;;         From: Joe User
926      ;;                 <joe@y.z>
927      ;; can yield `From Joe User Fri Mar 22 08:11:15 1996'.
928      ;; The mailbox can be removed or be replaced by white space, e.g.
929      ;;         From: "Joe User"{space}{tab}
930      ;;                 <joe@y.z>
931      ;; can yield `From {space}{tab} Fri Mar 22 08:11:15 1996',
932      ;; where {space} and {tab} represent the Ascii space and tab characters.
933      ;; We want to match the results of any of these manglings.
934      ;; The following regexp rejects names whose first characters are
935      ;; obviously bogus, but after that anything goes.
936      "\\([^\0-\b\n-\r\^?].*\\)? "
937
938      ;; The time the message was sent.
939      "\\([^\0-\r \^?]+\\) +"            ; day of the week
940      "\\([^\0-\r \^?]+\\) +"            ; month
941      "\\([0-3]?[0-9]\\) +"              ; day of month
942      "\\([0-2][0-9]:[0-5][0-9]\\(:[0-6][0-9]\\)?\\) *" ; time of day
943
944      ;; Perhaps a time zone, specified by an abbreviation, or by a
945      ;; numeric offset.
946      time-zone-regexp
947
948      ;; The year.
949      " \\([0-9][0-9]+\\) *"
950
951      ;; On some systems the time zone can appear after the year, too.
952      time-zone-regexp
953
954      ;; Old uucp cruft.
955      "\\(remote from .*\\)?"
956
957      "\n"))
958   "Regexp matching the delimiter of messages in UNIX mail format.")
959
960 (defvar message-unsent-separator
961   (concat "^ *---+ +Unsent message follows +---+ *$\\|"
962           "^ *---+ +Returned message +---+ *$\\|"
963           "^Start of returned message$\\|"
964           "^ *---+ +Original message +---+ *$\\|"
965           "^ *--+ +begin message +--+ *$\\|"
966           "^ *---+ +Original message follows +---+ *$\\|"
967           "^ *---+ +Undelivered message follows +---+ *$\\|"
968           "^|? *---+ +Message text follows: +---+ *|?$")
969   "A regexp that matches the separator before the text of a failed message.")
970
971 (defvar message-header-format-alist
972   `((Newsgroups)
973     (To . message-fill-address)
974     (Cc . message-fill-address)
975     (Subject)
976     (In-Reply-To)
977     (Fcc)
978     (Bcc)
979     (Date)
980     (Organization)
981     (Distribution)
982     (Lines)
983     (Expires)
984     (Message-ID)
985     (References . message-shorten-references)
986     (User-Agent))
987   "Alist used for formatting headers.")
988
989 (eval-and-compile
990   (autoload 'message-setup-toolbar "messagexmas")
991   (autoload 'mh-new-draft-name "mh-comp")
992   (autoload 'mh-send-letter "mh-comp")
993   (autoload 'gnus-point-at-eol "gnus-util")
994   (autoload 'gnus-point-at-bol "gnus-util")
995   (autoload 'gnus-output-to-mail "gnus-util")
996   (autoload 'mail-abbrev-in-expansion-header-p "mailabbrev")
997   (autoload 'nndraft-request-associate-buffer "nndraft")
998   (autoload 'nndraft-request-expire-articles "nndraft")
999   (autoload 'gnus-open-server "gnus-int")
1000   (autoload 'gnus-request-post "gnus-int")
1001   (autoload 'gnus-alive-p "gnus-util")
1002   (autoload 'rmail-output "rmail"))
1003
1004 \f
1005
1006 ;;;
1007 ;;; Utility functions.
1008 ;;;
1009
1010 (defmacro message-y-or-n-p (question show &rest text)
1011   "Ask QUESTION, displaying the rest of the arguments in a temp. buffer if SHOW"
1012   `(message-talkative-question 'y-or-n-p ,question ,show ,@text))
1013
1014 ;; Delete the current line (and the next N lines.);
1015 (defmacro message-delete-line (&optional n)
1016   `(delete-region (progn (beginning-of-line) (point))
1017                   (progn (forward-line ,(or n 1)) (point))))
1018
1019 (defun message-tokenize-header (header &optional separator)
1020   "Split HEADER into a list of header elements.
1021 \",\" is used as the separator."
1022   (if (not header)
1023       nil
1024     (let ((regexp (format "[%s]+" (or separator ",")))
1025           (beg 1)
1026           (first t)
1027           quoted elems paren)
1028       (save-excursion
1029         (message-set-work-buffer)
1030         (insert header)
1031         (goto-char (point-min))
1032         (while (not (eobp))
1033           (if first
1034               (setq first nil)
1035             (forward-char 1))
1036           (cond ((and (> (point) beg)
1037                       (or (eobp)
1038                           (and (looking-at regexp)
1039                                (not quoted)
1040                                (not paren))))
1041                  (push (buffer-substring beg (point)) elems)
1042                  (setq beg (match-end 0)))
1043                 ((eq (char-after) ?\")
1044                  (setq quoted (not quoted)))
1045                 ((and (eq (char-after) ?\()
1046                       (not quoted))
1047                  (setq paren t))
1048                 ((and (eq (char-after) ?\))
1049                       (not quoted))
1050                  (setq paren nil))))
1051         (nreverse elems)))))
1052
1053 (defun message-mail-file-mbox-p (file)
1054   "Say whether FILE looks like a Unix mbox file."
1055   (when (and (file-exists-p file)
1056              (file-readable-p file)
1057              (file-regular-p file))
1058     (with-temp-buffer
1059       (nnheader-insert-file-contents file)
1060       (goto-char (point-min))
1061       (looking-at message-unix-mail-delimiter))))
1062
1063 (defun message-fetch-field (header &optional not-all)
1064   "The same as `mail-fetch-field', only remove all newlines."
1065   (let* ((inhibit-point-motion-hooks t)
1066          (case-fold-search t)
1067          (value (mail-fetch-field header nil (not not-all))))
1068     (when value
1069       (while (string-match "\n[\t ]+" value)
1070         (setq value (replace-match " " t t value)))
1071       ;; We remove all text props.
1072       (format "%s" value))))
1073
1074 (defun message-narrow-to-field ()
1075   "Narrow the buffer to the header on the current line."
1076   (beginning-of-line)
1077   (narrow-to-region
1078    (point)
1079    (progn
1080      (forward-line 1)
1081      (if (re-search-forward "^[^ \n\t]" nil t)
1082          (progn
1083            (beginning-of-line)
1084            (point))
1085        (point-max))))
1086   (goto-char (point-min)))
1087
1088 (defun message-add-header (&rest headers)
1089   "Add the HEADERS to the message header, skipping those already present."
1090   (while headers
1091     (let (hclean)
1092       (unless (string-match "^\\([^:]+\\):[ \t]*[^ \t]" (car headers))
1093         (error "Invalid header `%s'" (car headers)))
1094       (setq hclean (match-string 1 (car headers)))
1095       (save-restriction
1096         (message-narrow-to-headers)
1097         (unless (re-search-forward (concat "^" (regexp-quote hclean) ":") nil t)
1098           (insert (car headers) ?\n))))
1099     (setq headers (cdr headers))))
1100
1101
1102 (defun message-fetch-reply-field (header)
1103   "Fetch FIELD from the message we're replying to."
1104   (when (and message-reply-buffer
1105              (buffer-name message-reply-buffer))
1106     (save-excursion
1107       (set-buffer message-reply-buffer)
1108       (message-fetch-field header))))
1109
1110 (defun message-set-work-buffer ()
1111   (if (get-buffer " *message work*")
1112       (progn
1113         (set-buffer " *message work*")
1114         (erase-buffer))
1115     (set-buffer (get-buffer-create " *message work*"))
1116     (kill-all-local-variables)
1117     (mm-enable-multibyte)))
1118
1119 (defun message-functionp (form)
1120   "Return non-nil if FORM is funcallable."
1121   (or (and (symbolp form) (fboundp form))
1122       (and (listp form) (eq (car form) 'lambda))
1123       (byte-code-function-p form)))
1124
1125 (defun message-strip-subject-re (subject)
1126   "Remove \"Re:\" from subject lines."
1127   (if (string-match message-subject-re-regexp subject)
1128       (substring subject (match-end 0))
1129     subject))
1130
1131 (defun message-remove-header (header &optional is-regexp first reverse)
1132   "Remove HEADER in the narrowed buffer.
1133 If REGEXP, HEADER is a regular expression.
1134 If FIRST, only remove the first instance of the header.
1135 Return the number of headers removed."
1136   (goto-char (point-min))
1137   (let ((regexp (if is-regexp header (concat "^" (regexp-quote header) ":")))
1138         (number 0)
1139         (case-fold-search t)
1140         last)
1141     (while (and (not (eobp))
1142                 (not last))
1143       (if (if reverse
1144               (not (looking-at regexp))
1145             (looking-at regexp))
1146           (progn
1147             (incf number)
1148             (when first
1149               (setq last t))
1150             (delete-region
1151              (point)
1152              ;; There might be a continuation header, so we have to search
1153              ;; until we find a new non-continuation line.
1154              (progn
1155                (forward-line 1)
1156                (if (re-search-forward "^[^ \t]" nil t)
1157                    (goto-char (match-beginning 0))
1158                  (point-max)))))
1159         (forward-line 1)
1160         (if (re-search-forward "^[^ \t]" nil t)
1161             (goto-char (match-beginning 0))
1162           (goto-char (point-max)))))
1163     number))
1164
1165 (defun message-remove-first-header (header)
1166   "Remove the first instance of HEADER if there is more than one."
1167   (let ((count 0)
1168         (regexp (concat "^" (regexp-quote header) ":")))
1169     (save-excursion
1170       (goto-char (point-min))
1171       (while (re-search-forward regexp nil t)
1172         (incf count)))
1173     (while (> count 1)
1174       (message-remove-header header nil t)
1175       (decf count))))
1176
1177 (defun message-narrow-to-headers ()
1178   "Narrow the buffer to the head of the message."
1179   (widen)
1180   (narrow-to-region
1181    (goto-char (point-min))
1182    (if (re-search-forward
1183         (concat "^" (regexp-quote mail-header-separator) "\n") nil t)
1184        (match-beginning 0)
1185      (point-max)))
1186   (goto-char (point-min)))
1187
1188 (defun message-narrow-to-head ()
1189   "Narrow the buffer to the head of the message.
1190 Point is left at the beginning of the narrowed-to region."
1191   (widen)
1192   (narrow-to-region
1193    (goto-char (point-min))
1194    (if (search-forward "\n\n" nil 1)
1195        (1- (point))
1196      (point-max)))
1197   (goto-char (point-min)))
1198
1199 (defun message-narrow-to-headers-or-head ()
1200   "Narrow the buffer to the head of the message."
1201   (widen)
1202   (narrow-to-region
1203    (goto-char (point-min))
1204    (cond
1205     ((re-search-forward
1206       (concat "^" (regexp-quote mail-header-separator) "\n") nil t)
1207      (match-beginning 0))
1208     ((search-forward "\n\n" nil t)
1209      (1- (point)))
1210     (t
1211      (point-max))))
1212   (goto-char (point-min)))
1213
1214 (defun message-news-p ()
1215   "Say whether the current buffer contains a news message."
1216   (and (not message-this-is-mail)
1217        (or message-this-is-news
1218            (save-excursion
1219              (save-restriction
1220                (message-narrow-to-headers)
1221                (and (message-fetch-field "newsgroups")
1222                     (not (message-fetch-field "posted-to"))))))))
1223
1224 (defun message-mail-p ()
1225   "Say whether the current buffer contains a mail message."
1226   (and (not message-this-is-news)
1227        (or message-this-is-mail
1228            (save-excursion
1229              (save-restriction
1230                (message-narrow-to-headers)
1231                (or (message-fetch-field "to")
1232                    (message-fetch-field "cc")
1233                    (message-fetch-field "bcc")))))))
1234
1235 (defun message-next-header ()
1236   "Go to the beginning of the next header."
1237   (beginning-of-line)
1238   (or (eobp) (forward-char 1))
1239   (not (if (re-search-forward "^[^ \t]" nil t)
1240            (beginning-of-line)
1241          (goto-char (point-max)))))
1242
1243 (defun message-sort-headers-1 ()
1244   "Sort the buffer as headers using `message-rank' text props."
1245   (goto-char (point-min))
1246   (require 'sort)
1247   (sort-subr
1248    nil 'message-next-header
1249    (lambda ()
1250      (message-next-header)
1251      (unless (bobp)
1252        (forward-char -1)))
1253    (lambda ()
1254      (or (get-text-property (point) 'message-rank)
1255          10000))))
1256
1257 (defun message-sort-headers ()
1258   "Sort the headers of the current message according to `message-header-format-alist'."
1259   (interactive)
1260   (save-excursion
1261     (save-restriction
1262       (let ((max (1+ (length message-header-format-alist)))
1263             rank)
1264         (message-narrow-to-headers)
1265         (while (re-search-forward "^[^ \n]+:" nil t)
1266           (put-text-property
1267            (match-beginning 0) (1+ (match-beginning 0))
1268            'message-rank
1269            (if (setq rank (length (memq (assq (intern (buffer-substring
1270                                                        (match-beginning 0)
1271                                                        (1- (match-end 0))))
1272                                               message-header-format-alist)
1273                                         message-header-format-alist)))
1274                (- max rank)
1275              (1+ max)))))
1276       (message-sort-headers-1))))
1277
1278 \f
1279
1280 ;;;
1281 ;;; Message mode
1282 ;;;
1283
1284 ;;; Set up keymap.
1285
1286 (defvar message-mode-map nil)
1287
1288 (unless message-mode-map
1289   (setq message-mode-map (make-keymap))
1290   (set-keymap-parent message-mode-map text-mode-map)
1291   (define-key message-mode-map "\C-c?" 'describe-mode)
1292
1293   (define-key message-mode-map "\C-c\C-f\C-t" 'message-goto-to)
1294   (define-key message-mode-map "\C-c\C-f\C-b" 'message-goto-bcc)
1295   (define-key message-mode-map "\C-c\C-f\C-w" 'message-goto-fcc)
1296   (define-key message-mode-map "\C-c\C-f\C-c" 'message-goto-cc)
1297   (define-key message-mode-map "\C-c\C-f\C-s" 'message-goto-subject)
1298   (define-key message-mode-map "\C-c\C-f\C-r" 'message-goto-reply-to)
1299   (define-key message-mode-map "\C-c\C-f\C-n" 'message-goto-newsgroups)
1300   (define-key message-mode-map "\C-c\C-f\C-d" 'message-goto-distribution)
1301   (define-key message-mode-map "\C-c\C-f\C-f" 'message-goto-followup-to)
1302   (define-key message-mode-map "\C-c\C-f\C-k" 'message-goto-keywords)
1303   (define-key message-mode-map "\C-c\C-f\C-u" 'message-goto-summary)
1304   (define-key message-mode-map "\C-c\C-b" 'message-goto-body)
1305   (define-key message-mode-map "\C-c\C-i" 'message-goto-signature)
1306
1307   (define-key message-mode-map "\C-c\C-t" 'message-insert-to)
1308   (define-key message-mode-map "\C-c\C-n" 'message-insert-newsgroups)
1309
1310   (define-key message-mode-map "\C-c\C-y" 'message-yank-original)
1311   (define-key message-mode-map "\C-c\M-\C-y" 'message-yank-buffer)
1312   (define-key message-mode-map "\C-c\C-q" 'message-fill-yanked-message)
1313   (define-key message-mode-map "\C-c\C-w" 'message-insert-signature)
1314   (define-key message-mode-map "\C-c\M-h" 'message-insert-headers)
1315   (define-key message-mode-map "\C-c\C-r" 'message-caesar-buffer-body)
1316   (define-key message-mode-map "\C-c\C-o" 'message-sort-headers)
1317   (define-key message-mode-map "\C-c\M-r" 'message-rename-buffer)
1318
1319   (define-key message-mode-map "\C-c\C-c" 'message-send-and-exit)
1320   (define-key message-mode-map "\C-c\C-s" 'message-send)
1321   (define-key message-mode-map "\C-c\C-k" 'message-kill-buffer)
1322   (define-key message-mode-map "\C-c\C-d" 'message-dont-send)
1323
1324   (define-key message-mode-map "\C-c\C-e" 'message-elide-region)
1325   (define-key message-mode-map "\C-c\C-v" 'message-delete-not-region)
1326   (define-key message-mode-map "\C-c\C-z" 'message-kill-to-signature)
1327   (define-key message-mode-map "\M-\r" 'message-newline-and-reformat)
1328
1329   (define-key message-mode-map "\C-c\C-a" 'mml-attach-file)
1330
1331   (define-key message-mode-map "\t" 'message-tab))
1332
1333 (easy-menu-define
1334  message-mode-menu message-mode-map "Message Menu."
1335  '("Message"
1336    ["Sort Headers" message-sort-headers t]
1337    ["Yank Original" message-yank-original t]
1338    ["Fill Yanked Message" message-fill-yanked-message t]
1339    ["Insert Signature" message-insert-signature t]
1340    ["Caesar (rot13) Message" message-caesar-buffer-body t]
1341    ["Caesar (rot13) Region" message-caesar-region (mark t)]
1342    ["Elide Region" message-elide-region (mark t)]
1343    ["Delete Outside Region" message-delete-not-region (mark t)]
1344    ["Kill To Signature" message-kill-to-signature t]
1345    ["Newline and Reformat" message-newline-and-reformat t]
1346    ["Rename buffer" message-rename-buffer t]
1347    ["Spellcheck" ispell-message t]
1348    ["Attach file as MIME" mml-attach-file t]
1349    "----"
1350    ["Send Message" message-send-and-exit t]
1351    ["Abort Message" message-dont-send t]
1352    ["Kill Message" message-kill-buffer t]))
1353
1354 (easy-menu-define
1355  message-mode-field-menu message-mode-map ""
1356  '("Field"
1357    ["Fetch To" message-insert-to t]
1358    ["Fetch Newsgroups" message-insert-newsgroups t]
1359    "----"
1360    ["To" message-goto-to t]
1361    ["Subject" message-goto-subject t]
1362    ["Cc" message-goto-cc t]
1363    ["Reply-To" message-goto-reply-to t]
1364    ["Summary" message-goto-summary t]
1365    ["Keywords" message-goto-keywords t]
1366    ["Newsgroups" message-goto-newsgroups t]
1367    ["Followup-To" message-goto-followup-to t]
1368    ["Distribution" message-goto-distribution t]
1369    ["Body" message-goto-body t]
1370    ["Signature" message-goto-signature t]))
1371
1372 (defvar facemenu-add-face-function)
1373 (defvar facemenu-remove-face-function)
1374
1375 ;;;###autoload
1376 (defun message-mode ()
1377   "Major mode for editing mail and news to be sent.
1378 Like Text Mode but with these additional commands:
1379 C-c C-s  message-send (send the message)    C-c C-c  message-send-and-exit
1380 C-c C-d  Pospone sending the message        C-c C-k  Kill the message
1381 C-c C-f  move to a header field (and create it if there isn't):
1382          C-c C-f C-t  move to To        C-c C-f C-s  move to Subject
1383          C-c C-f C-c  move to Cc        C-c C-f C-b  move to Bcc
1384          C-c C-f C-w  move to Fcc       C-c C-f C-r  move to Reply-To
1385          C-c C-f C-u  move to Summary   C-c C-f C-n  move to Newsgroups
1386          C-c C-f C-k  move to Keywords  C-c C-f C-d  move to Distribution
1387          C-c C-f C-f  move to Followup-To
1388 C-c C-t  message-insert-to (add a To header to a news followup)
1389 C-c C-n  message-insert-newsgroups (add a Newsgroup header to a news reply)
1390 C-c C-b  message-goto-body (move to beginning of message text).
1391 C-c C-i  message-goto-signature (move to the beginning of the signature).
1392 C-c C-w  message-insert-signature (insert `message-signature-file' file).
1393 C-c C-y  message-yank-original (insert current message, if any).
1394 C-c C-q  message-fill-yanked-message (fill what was yanked).
1395 C-c C-e  message-elide-region (elide the text between point and mark).
1396 C-c C-v  message-delete-not-region (remove the text outside the region).
1397 C-c C-z  message-kill-to-signature (kill the text up to the signature).
1398 C-c C-r  message-caesar-buffer-body (rot13 the message body).
1399 C-c C-a  mml-attach-file (attach a file as MIME).
1400 M-RET    message-newline-and-reformat (break the line and reformat)."
1401   (interactive)
1402   (kill-all-local-variables)
1403   (set (make-local-variable 'message-reply-buffer) nil)
1404   (make-local-variable 'message-send-actions)
1405   (make-local-variable 'message-exit-actions)
1406   (make-local-variable 'message-kill-actions)
1407   (make-local-variable 'message-postpone-actions)
1408   (make-local-variable 'message-draft-article)
1409   (make-local-hook 'kill-buffer-hook)
1410   (set-syntax-table message-mode-syntax-table)
1411   (use-local-map message-mode-map)
1412   (setq local-abbrev-table message-mode-abbrev-table)
1413   (setq major-mode 'message-mode)
1414   (setq mode-name "Message")
1415   (setq buffer-offer-save t)
1416   (make-local-variable 'facemenu-add-face-function)
1417   (make-local-variable 'facemenu-remove-face-function)
1418   (setq facemenu-add-face-function
1419         (lambda (face end)
1420           (let ((face-fun (cdr (assq face message-face-alist))))
1421             (if face-fun
1422                 (funcall face-fun (point) end)
1423               (error "Face %s not configured for %s mode" face mode-name)))
1424           "")
1425         facemenu-remove-face-function t)
1426   (make-local-variable 'paragraph-separate)
1427   (make-local-variable 'paragraph-start)
1428   ;; `-- ' precedes the signature.  `-----' appears at the start of the
1429   ;; lines that delimit forwarded messages.
1430   ;; Lines containing just >= 3 dashes, perhaps after whitespace,
1431   ;; are also sometimes used and should be separators.
1432   (setq paragraph-start
1433         (concat (regexp-quote mail-header-separator)
1434                 "$\\|[ \t]*[a-z0-9A-Z]*>+[ \t]*$\\|[ \t]*$\\|"
1435                 "-- $\\|---+$\\|"
1436                 page-delimiter
1437                 ;;!!! Uhm... shurely this can't be right?
1438                 "[> " (regexp-quote message-yank-prefix) "]+$"))
1439   (setq paragraph-separate paragraph-start)
1440   (make-local-variable 'message-reply-headers)
1441   (setq message-reply-headers nil)
1442   (make-local-variable 'message-newsreader)
1443   (make-local-variable 'message-mailer)
1444   (make-local-variable 'message-post-method)
1445   (set (make-local-variable 'message-sent-message-via) nil)
1446   (set (make-local-variable 'message-checksum) nil)
1447   (set (make-local-variable 'message-mime-part) 0)
1448   ;;(when (fboundp 'mail-hist-define-keys)
1449   ;;  (mail-hist-define-keys))
1450   (when (string-match "XEmacs\\|Lucid" emacs-version)
1451     (message-setup-toolbar))
1452   (easy-menu-add message-mode-menu message-mode-map)
1453   (easy-menu-add message-mode-field-menu message-mode-map)
1454   ;; Allow mail alias things.
1455   (when (eq message-mail-alias-type 'abbrev)
1456     (if (fboundp 'mail-abbrevs-setup)
1457         (mail-abbrevs-setup)
1458       (mail-aliases-setup)))
1459   (message-set-auto-save-file-name)
1460   (unless (string-match "XEmacs" emacs-version)
1461     (set (make-local-variable 'font-lock-defaults)
1462          '(message-font-lock-keywords t)))
1463   (make-local-variable 'adaptive-fill-regexp)
1464   (setq adaptive-fill-regexp
1465         (concat "[ \t]*[-a-z0-9A-Z]*\\(>[ \t]*\\)+[ \t]*\\|" adaptive-fill-regexp))
1466   (unless (boundp 'adaptive-fill-first-line-regexp)
1467     (setq adaptive-fill-first-line-regexp nil))
1468   (make-local-variable 'adaptive-fill-first-line-regexp)
1469   (setq adaptive-fill-first-line-regexp
1470         (concat "[ \t]*[-a-z0-9A-Z]*\\(>[ \t]*\\)+[ \t]*\\|"
1471                 adaptive-fill-first-line-regexp))
1472   (make-local-variable 'auto-fill-inhibit-regexp)
1473   (setq auto-fill-inhibit-regexp "^[A-Z][^: \n\t]+:")
1474   (mm-enable-multibyte)
1475   (make-local-variable 'indent-tabs-mode) ;Turn off tabs for indentation.
1476   (setq indent-tabs-mode nil)
1477   (mml-mode)
1478   (run-hooks 'text-mode-hook 'message-mode-hook))
1479
1480 \f
1481
1482 ;;;
1483 ;;; Message mode commands
1484 ;;;
1485
1486 ;;; Movement commands
1487
1488 (defun message-goto-to ()
1489   "Move point to the To header."
1490   (interactive)
1491   (message-position-on-field "To"))
1492
1493 (defun message-goto-subject ()
1494   "Move point to the Subject header."
1495   (interactive)
1496   (message-position-on-field "Subject"))
1497
1498 (defun message-goto-cc ()
1499   "Move point to the Cc header."
1500   (interactive)
1501   (message-position-on-field "Cc" "To"))
1502
1503 (defun message-goto-bcc ()
1504   "Move point to the Bcc  header."
1505   (interactive)
1506   (message-position-on-field "Bcc" "Cc" "To"))
1507
1508 (defun message-goto-fcc ()
1509   "Move point to the Fcc header."
1510   (interactive)
1511   (message-position-on-field "Fcc" "To" "Newsgroups"))
1512
1513 (defun message-goto-reply-to ()
1514   "Move point to the Reply-To header."
1515   (interactive)
1516   (message-position-on-field "Reply-To" "Subject"))
1517
1518 (defun message-goto-newsgroups ()
1519   "Move point to the Newsgroups header."
1520   (interactive)
1521   (message-position-on-field "Newsgroups"))
1522
1523 (defun message-goto-distribution ()
1524   "Move point to the Distribution header."
1525   (interactive)
1526   (message-position-on-field "Distribution"))
1527
1528 (defun message-goto-followup-to ()
1529   "Move point to the Followup-To header."
1530   (interactive)
1531   (message-position-on-field "Followup-To" "Newsgroups"))
1532
1533 (defun message-goto-keywords ()
1534   "Move point to the Keywords header."
1535   (interactive)
1536   (message-position-on-field "Keywords" "Subject"))
1537
1538 (defun message-goto-summary ()
1539   "Move point to the Summary header."
1540   (interactive)
1541   (message-position-on-field "Summary" "Subject"))
1542
1543 (defun message-goto-body ()
1544   "Move point to the beginning of the message body."
1545   (interactive)
1546   (if (looking-at "[ \t]*\n") (expand-abbrev))
1547   (goto-char (point-min))
1548   (or (search-forward (concat "\n" mail-header-separator "\n") nil t)
1549       (search-forward "\n\n" nil t)))
1550
1551 (defun message-goto-eoh ()
1552   "Move point to the end of the headers."
1553   (interactive)
1554   (message-goto-body)
1555   (forward-line -1))
1556
1557 (defun message-goto-signature ()
1558   "Move point to the beginning of the message signature.
1559 If there is no signature in the article, go to the end and
1560 return nil."
1561   (interactive)
1562   (goto-char (point-min))
1563   (if (re-search-forward message-signature-separator nil t)
1564       (forward-line 1)
1565     (goto-char (point-max))
1566     nil))
1567
1568 \f
1569
1570 (defun message-insert-to (&optional force)
1571   "Insert a To header that points to the author of the article being replied to.
1572 If the original author requested not to be sent mail, the function signals
1573 an error.
1574 With the prefix argument FORCE, insert the header anyway."
1575   (interactive "P")
1576   (let ((co (message-fetch-reply-field "mail-copies-to")))
1577     (when (and (null force)
1578                co
1579                (or (equal (downcase co) "never")
1580                    (equal (downcase co) "nobody")))
1581       (error "The user has requested not to have copies sent via mail")))
1582   (when (and (message-position-on-field "To")
1583              (mail-fetch-field "to")
1584              (not (string-match "\\` *\\'" (mail-fetch-field "to"))))
1585     (insert ", "))
1586   (insert (or (message-fetch-reply-field "reply-to")
1587               (message-fetch-reply-field "from") "")))
1588
1589 (defun message-widen-reply ()
1590   "Widen the reply to include maximum recipients."
1591   (interactive)
1592   (let ((follow-to
1593          (and message-reply-buffer
1594               (buffer-name message-reply-buffer)
1595               (save-excursion
1596                 (set-buffer message-reply-buffer)
1597                 (message-get-reply-headers t)))))
1598     (save-excursion
1599       (save-restriction
1600         (message-narrow-to-headers)
1601         (dolist (elem follow-to)
1602           (message-remove-header (symbol-name (car elem)))
1603           (goto-char (point-min))
1604           (insert (symbol-name (car elem)) ": "
1605                   (cdr elem) "\n"))))))
1606
1607 (defun message-insert-newsgroups ()
1608   "Insert the Newsgroups header from the article being replied to."
1609   (interactive)
1610   (when (and (message-position-on-field "Newsgroups")
1611              (mail-fetch-field "newsgroups")
1612              (not (string-match "\\` *\\'" (mail-fetch-field "newsgroups"))))
1613     (insert ","))
1614   (insert (or (message-fetch-reply-field "newsgroups") "")))
1615
1616 \f
1617
1618 ;;; Various commands
1619
1620 (defun message-delete-not-region (beg end)
1621   "Delete everything in the body of the current message that is outside of the region."
1622   (interactive "r")
1623   (save-excursion
1624     (goto-char end)
1625     (delete-region (point) (if (not (message-goto-signature))
1626                                (point)
1627                              (forward-line -2)
1628                              (point)))
1629     (insert "\n")
1630     (goto-char beg)
1631     (delete-region beg (progn (message-goto-body)
1632                               (forward-line 2)
1633                               (point))))
1634   (when (message-goto-signature)
1635     (forward-line -2)))
1636
1637 (defun message-kill-to-signature ()
1638   "Deletes all text up to the signature."
1639   (interactive)
1640   (let ((point (point)))
1641     (message-goto-signature)
1642     (unless (eobp)
1643       (forward-line -2))
1644     (kill-region point (point))
1645     (unless (bolp)
1646       (insert "\n"))))
1647
1648 (defun message-newline-and-reformat ()
1649   "Insert four newlines, and then reformat if inside quoted text."
1650   (interactive)
1651   (let ((prefix "[]>ยป|:}+ \t]*")
1652         (supercite-thing "[-._a-zA-Z0-9]*[>]+[ \t]*")
1653         quoted point)
1654     (unless (bolp)
1655       (save-excursion
1656         (beginning-of-line)
1657         (when (looking-at (concat prefix
1658                                   supercite-thing))
1659           (setq quoted (match-string 0))))
1660       (insert "\n"))
1661     (setq point (point))
1662     (insert "\n\n\n")
1663     (delete-region (point) (re-search-forward "[ \t]*"))
1664     (when quoted
1665       (insert quoted))
1666     (fill-paragraph nil)
1667     (goto-char point)
1668     (forward-line 1)))
1669
1670 (defun message-insert-signature (&optional force)
1671   "Insert a signature.  See documentation for the `message-signature' variable."
1672   (interactive (list 0))
1673   (let* ((signature
1674           (cond
1675            ((and (null message-signature)
1676                  (eq force 0))
1677             (save-excursion
1678               (goto-char (point-max))
1679               (not (re-search-backward message-signature-separator nil t))))
1680            ((and (null message-signature)
1681                  force)
1682             t)
1683            ((message-functionp message-signature)
1684             (funcall message-signature))
1685            ((listp message-signature)
1686             (eval message-signature))
1687            (t message-signature)))
1688          (signature
1689           (cond ((stringp signature)
1690                  signature)
1691                 ((and (eq t signature)
1692                       message-signature-file
1693                       (file-exists-p message-signature-file))
1694                  signature))))
1695     (when signature
1696       (goto-char (point-max))
1697       ;; Insert the signature.
1698       (unless (bolp)
1699         (insert "\n"))
1700       (insert "\n-- \n")