(gnus-add-minor-mode): Make it an alias if
[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-rmail "gnus-util")
1009   (autoload 'gnus-output-to-mail "gnus-util")
1010   (autoload 'mail-abbrev-in-expansion-header-p "mailabbrev")
1011   (autoload 'nndraft-request-associate-buffer "nndraft")
1012   (autoload 'nndraft-request-expire-articles "nndraft")
1013   (autoload 'gnus-open-server "gnus-int")
1014   (autoload 'gnus-request-post "gnus-int")
1015   (autoload 'gnus-alive-p "gnus-util")
1016   (autoload 'gnus-list-identifiers "gnus-sum")
1017   (autoload 'rmail-output "rmail"))
1018
1019 \f
1020
1021 ;;;
1022 ;;; Utility functions.
1023 ;;;
1024
1025 (defmacro message-y-or-n-p (question show &rest text)
1026   "Ask QUESTION, displaying the rest of the arguments in a temp. buffer if SHOW"
1027   `(message-talkative-question 'y-or-n-p ,question ,show ,@text))
1028
1029 ;; Delete the current line (and the next N lines.);
1030 (defmacro message-delete-line (&optional n)
1031   `(delete-region (progn (beginning-of-line) (point))
1032                   (progn (forward-line ,(or n 1)) (point))))
1033
1034 (defun message-tokenize-header (header &optional separator)
1035   "Split HEADER into a list of header elements.
1036 \",\" is used as the separator."
1037   (if (not header)
1038       nil
1039     (let ((regexp (format "[%s]+" (or separator ",")))
1040           (beg 1)
1041           (first t)
1042           quoted elems paren)
1043       (save-excursion
1044         (message-set-work-buffer)
1045         (insert header)
1046         (goto-char (point-min))
1047         (while (not (eobp))
1048           (if first
1049               (setq first nil)
1050             (forward-char 1))
1051           (cond ((and (> (point) beg)
1052                       (or (eobp)
1053                           (and (looking-at regexp)
1054                                (not quoted)
1055                                (not paren))))
1056                  (push (buffer-substring beg (point)) elems)
1057                  (setq beg (match-end 0)))
1058                 ((eq (char-after) ?\")
1059                  (setq quoted (not quoted)))
1060                 ((and (eq (char-after) ?\()
1061                       (not quoted))
1062                  (setq paren t))
1063                 ((and (eq (char-after) ?\))
1064                       (not quoted))
1065                  (setq paren nil))))
1066         (nreverse elems)))))
1067
1068 (defun message-mail-file-mbox-p (file)
1069   "Say whether FILE looks like a Unix mbox file."
1070   (when (and (file-exists-p file)
1071              (file-readable-p file)
1072              (file-regular-p file))
1073     (with-temp-buffer
1074       (nnheader-insert-file-contents file)
1075       (goto-char (point-min))
1076       (looking-at message-unix-mail-delimiter))))
1077
1078 (defun message-fetch-field (header &optional not-all)
1079   "The same as `mail-fetch-field', only remove all newlines."
1080   (let* ((inhibit-point-motion-hooks t)
1081          (case-fold-search t)
1082          (value (mail-fetch-field header nil (not not-all))))
1083     (when value
1084       (while (string-match "\n[\t ]+" value)
1085         (setq value (replace-match " " t t value)))
1086       ;; We remove all text props.
1087       (format "%s" value))))
1088
1089 (defun message-narrow-to-field ()
1090   "Narrow the buffer to the header on the current line."
1091   (beginning-of-line)
1092   (narrow-to-region
1093    (point)
1094    (progn
1095      (forward-line 1)
1096      (if (re-search-forward "^[^ \n\t]" nil t)
1097          (progn
1098            (beginning-of-line)
1099            (point))
1100        (point-max))))
1101   (goto-char (point-min)))
1102
1103 (defun message-add-header (&rest headers)
1104   "Add the HEADERS to the message header, skipping those already present."
1105   (while headers
1106     (let (hclean)
1107       (unless (string-match "^\\([^:]+\\):[ \t]*[^ \t]" (car headers))
1108         (error "Invalid header `%s'" (car headers)))
1109       (setq hclean (match-string 1 (car headers)))
1110       (save-restriction
1111         (message-narrow-to-headers)
1112         (unless (re-search-forward (concat "^" (regexp-quote hclean) ":") nil t)
1113           (insert (car headers) ?\n))))
1114     (setq headers (cdr headers))))
1115
1116
1117 (defun message-fetch-reply-field (header)
1118   "Fetch FIELD from the message we're replying to."
1119   (when (and message-reply-buffer
1120              (buffer-name message-reply-buffer))
1121     (save-excursion
1122       (set-buffer message-reply-buffer)
1123       (message-fetch-field header))))
1124
1125 (defun message-set-work-buffer ()
1126   (if (get-buffer " *message work*")
1127       (progn
1128         (set-buffer " *message work*")
1129         (erase-buffer))
1130     (set-buffer (get-buffer-create " *message work*"))
1131     (kill-all-local-variables)
1132     (mm-enable-multibyte)))
1133
1134 (defun message-functionp (form)
1135   "Return non-nil if FORM is funcallable."
1136   (or (and (symbolp form) (fboundp form))
1137       (and (listp form) (eq (car form) 'lambda))
1138       (byte-code-function-p form)))
1139
1140 (defun message-strip-list-identifiers (subject)
1141   "Remove list identifiers in `gnus-list-identifiers'."
1142   (let ((regexp (if (stringp gnus-list-identifiers)
1143                     gnus-list-identifiers
1144                   (mapconcat 'identity gnus-list-identifiers " *\\|"))))
1145     (if (string-match (concat "\\(\\(\\(Re: +\\)?\\(" regexp 
1146                                 " *\\)\\)+\\(Re: +\\)?\\)") subject)
1147         (concat (substring subject 0 (match-beginning 1))
1148                 (or (match-string 3 subject)
1149                     (match-string 5 subject))
1150                 (substring subject
1151                            (match-end 1)))
1152       subject)))
1153
1154 (defun message-strip-subject-re (subject)
1155   "Remove \"Re:\" from subject lines."
1156   (if (string-match message-subject-re-regexp subject)
1157       (substring subject (match-end 0))
1158     subject))
1159
1160 (defun message-remove-header (header &optional is-regexp first reverse)
1161   "Remove HEADER in the narrowed buffer.
1162 If REGEXP, HEADER is a regular expression.
1163 If FIRST, only remove the first instance of the header.
1164 Return the number of headers removed."
1165   (goto-char (point-min))
1166   (let ((regexp (if is-regexp header (concat "^" (regexp-quote header) ":")))
1167         (number 0)
1168         (case-fold-search t)
1169         last)
1170     (while (and (not (eobp))
1171                 (not last))
1172       (if (if reverse
1173               (not (looking-at regexp))
1174             (looking-at regexp))
1175           (progn
1176             (incf number)
1177             (when first
1178               (setq last t))
1179             (delete-region
1180              (point)
1181              ;; There might be a continuation header, so we have to search
1182              ;; until we find a new non-continuation line.
1183              (progn
1184                (forward-line 1)
1185                (if (re-search-forward "^[^ \t]" nil t)
1186                    (goto-char (match-beginning 0))
1187                  (point-max)))))
1188         (forward-line 1)
1189         (if (re-search-forward "^[^ \t]" nil t)
1190             (goto-char (match-beginning 0))
1191           (goto-char (point-max)))))
1192     number))
1193
1194 (defun message-remove-first-header (header)
1195   "Remove the first instance of HEADER if there is more than one."
1196   (let ((count 0)
1197         (regexp (concat "^" (regexp-quote header) ":")))
1198     (save-excursion
1199       (goto-char (point-min))
1200       (while (re-search-forward regexp nil t)
1201         (incf count)))
1202     (while (> count 1)
1203       (message-remove-header header nil t)
1204       (decf count))))
1205
1206 (defun message-narrow-to-headers ()
1207   "Narrow the buffer to the head of the message."
1208   (widen)
1209   (narrow-to-region
1210    (goto-char (point-min))
1211    (if (re-search-forward
1212         (concat "^" (regexp-quote mail-header-separator) "\n") nil t)
1213        (match-beginning 0)
1214      (point-max)))
1215   (goto-char (point-min)))
1216
1217 (defun message-narrow-to-head ()
1218   "Narrow the buffer to the head of the message.
1219 Point is left at the beginning of the narrowed-to region."
1220   (widen)
1221   (narrow-to-region
1222    (goto-char (point-min))
1223    (if (search-forward "\n\n" nil 1)
1224        (1- (point))
1225      (point-max)))
1226   (goto-char (point-min)))
1227
1228 (defun message-narrow-to-headers-or-head ()
1229   "Narrow the buffer to the head of the message."
1230   (widen)
1231   (narrow-to-region
1232    (goto-char (point-min))
1233    (cond
1234     ((re-search-forward
1235       (concat "^" (regexp-quote mail-header-separator) "\n") nil t)
1236      (match-beginning 0))
1237     ((search-forward "\n\n" nil t)
1238      (1- (point)))
1239     (t
1240      (point-max))))
1241   (goto-char (point-min)))
1242
1243 (defun message-news-p ()
1244   "Say whether the current buffer contains a news message."
1245   (and (not message-this-is-mail)
1246        (or message-this-is-news
1247            (save-excursion
1248              (save-restriction
1249                (message-narrow-to-headers)
1250                (and (message-fetch-field "newsgroups")
1251                     (not (message-fetch-field "posted-to"))))))))
1252
1253 (defun message-mail-p ()
1254   "Say whether the current buffer contains a mail message."
1255   (and (not message-this-is-news)
1256        (or message-this-is-mail
1257            (save-excursion
1258              (save-restriction
1259                (message-narrow-to-headers)
1260                (or (message-fetch-field "to")
1261                    (message-fetch-field "cc")
1262                    (message-fetch-field "bcc")))))))
1263
1264 (defun message-next-header ()
1265   "Go to the beginning of the next header."
1266   (beginning-of-line)
1267   (or (eobp) (forward-char 1))
1268   (not (if (re-search-forward "^[^ \t]" nil t)
1269            (beginning-of-line)
1270          (goto-char (point-max)))))
1271
1272 (defun message-sort-headers-1 ()
1273   "Sort the buffer as headers using `message-rank' text props."
1274   (goto-char (point-min))
1275   (require 'sort)
1276   (sort-subr
1277    nil 'message-next-header
1278    (lambda ()
1279      (message-next-header)
1280      (unless (bobp)
1281        (forward-char -1)))
1282    (lambda ()
1283      (or (get-text-property (point) 'message-rank)
1284          10000))))
1285
1286 (defun message-sort-headers ()
1287   "Sort the headers of the current message according to `message-header-format-alist'."
1288   (interactive)
1289   (save-excursion
1290     (save-restriction
1291       (let ((max (1+ (length message-header-format-alist)))
1292             rank)
1293         (message-narrow-to-headers)
1294         (while (re-search-forward "^[^ \n]+:" nil t)
1295           (put-text-property
1296            (match-beginning 0) (1+ (match-beginning 0))
1297            'message-rank
1298            (if (setq rank (length (memq (assq (intern (buffer-substring
1299                                                        (match-beginning 0)
1300                                                        (1- (match-end 0))))
1301                                               message-header-format-alist)
1302                                         message-header-format-alist)))
1303                (- max rank)
1304              (1+ max)))))
1305       (message-sort-headers-1))))
1306
1307 \f
1308
1309 ;;;
1310 ;;; Message mode
1311 ;;;
1312
1313 ;;; Set up keymap.
1314
1315 (defvar message-mode-map nil)
1316
1317 (unless message-mode-map
1318   (setq message-mode-map (make-keymap))
1319   (set-keymap-parent message-mode-map text-mode-map)
1320   (define-key message-mode-map "\C-c?" 'describe-mode)
1321
1322   (define-key message-mode-map "\C-c\C-f\C-t" 'message-goto-to)
1323   (define-key message-mode-map "\C-c\C-f\C-b" 'message-goto-bcc)
1324   (define-key message-mode-map "\C-c\C-f\C-w" 'message-goto-fcc)
1325   (define-key message-mode-map "\C-c\C-f\C-c" 'message-goto-cc)
1326   (define-key message-mode-map "\C-c\C-f\C-s" 'message-goto-subject)
1327   (define-key message-mode-map "\C-c\C-f\C-r" 'message-goto-reply-to)
1328   (define-key message-mode-map "\C-c\C-f\C-n" 'message-goto-newsgroups)
1329   (define-key message-mode-map "\C-c\C-f\C-d" 'message-goto-distribution)
1330   (define-key message-mode-map "\C-c\C-f\C-f" 'message-goto-followup-to)
1331   (define-key message-mode-map "\C-c\C-f\C-k" 'message-goto-keywords)
1332   (define-key message-mode-map "\C-c\C-f\C-u" 'message-goto-summary)
1333   (define-key message-mode-map "\C-c\C-b" 'message-goto-body)
1334   (define-key message-mode-map "\C-c\C-i" 'message-goto-signature)
1335
1336   (define-key message-mode-map "\C-c\C-t" 'message-insert-to)
1337   (define-key message-mode-map "\C-c\C-n" 'message-insert-newsgroups)
1338
1339   (define-key message-mode-map "\C-c\C-y" 'message-yank-original)
1340   (define-key message-mode-map "\C-c\M-\C-y" 'message-yank-buffer)
1341   (define-key message-mode-map "\C-c\C-q" 'message-fill-yanked-message)
1342   (define-key message-mode-map "\C-c\C-w" 'message-insert-signature)
1343   (define-key message-mode-map "\C-c\M-h" 'message-insert-headers)
1344   (define-key message-mode-map "\C-c\C-r" 'message-caesar-buffer-body)
1345   (define-key message-mode-map "\C-c\C-o" 'message-sort-headers)
1346   (define-key message-mode-map "\C-c\M-r" 'message-rename-buffer)
1347
1348   (define-key message-mode-map "\C-c\C-c" 'message-send-and-exit)
1349   (define-key message-mode-map "\C-c\C-s" 'message-send)
1350   (define-key message-mode-map "\C-c\C-k" 'message-kill-buffer)
1351   (define-key message-mode-map "\C-c\C-d" 'message-dont-send)
1352
1353   (define-key message-mode-map "\C-c\C-e" 'message-elide-region)
1354   (define-key message-mode-map "\C-c\C-v" 'message-delete-not-region)
1355   (define-key message-mode-map "\C-c\C-z" 'message-kill-to-signature)
1356   (define-key message-mode-map "\M-\r" 'message-newline-and-reformat)
1357
1358   (define-key message-mode-map "\C-c\C-a" 'mml-attach-file)
1359
1360   (define-key message-mode-map "\t" 'message-tab))
1361
1362 (easy-menu-define
1363  message-mode-menu message-mode-map "Message Menu."
1364  '("Message"
1365    ["Sort Headers" message-sort-headers t]
1366    ["Yank Original" message-yank-original t]
1367    ["Fill Yanked Message" message-fill-yanked-message t]
1368    ["Insert Signature" message-insert-signature t]
1369    ["Caesar (rot13) Message" message-caesar-buffer-body t]
1370    ["Caesar (rot13) Region" message-caesar-region (mark t)]
1371    ["Elide Region" message-elide-region (mark t)]
1372    ["Delete Outside Region" message-delete-not-region (mark t)]
1373    ["Kill To Signature" message-kill-to-signature t]
1374    ["Newline and Reformat" message-newline-and-reformat t]
1375    ["Rename buffer" message-rename-buffer t]
1376    ["Spellcheck" ispell-message t]
1377    ["Attach file as MIME" mml-attach-file t]
1378    "----"
1379    ["Send Message" message-send-and-exit t]
1380    ["Abort Message" message-dont-send t]
1381    ["Kill Message" message-kill-buffer t]))
1382
1383 (easy-menu-define
1384  message-mode-field-menu message-mode-map ""
1385  '("Field"
1386    ["Fetch To" message-insert-to t]
1387    ["Fetch Newsgroups" message-insert-newsgroups t]
1388    "----"
1389    ["To" message-goto-to t]
1390    ["Subject" message-goto-subject t]
1391    ["Cc" message-goto-cc t]
1392    ["Reply-To" message-goto-reply-to t]
1393    ["Summary" message-goto-summary t]
1394    ["Keywords" message-goto-keywords t]
1395    ["Newsgroups" message-goto-newsgroups t]
1396    ["Followup-To" message-goto-followup-to t]
1397    ["Distribution" message-goto-distribution t]
1398    ["Body" message-goto-body t]
1399    ["Signature" message-goto-signature t]))
1400
1401 (defvar facemenu-add-face-function)
1402 (defvar facemenu-remove-face-function)
1403
1404 ;;;###autoload
1405 (defun message-mode ()
1406   "Major mode for editing mail and news to be sent.
1407 Like Text Mode but with these additional commands:
1408 C-c C-s  message-send (send the message)    C-c C-c  message-send-and-exit
1409 C-c C-d  Pospone sending the message        C-c C-k  Kill the message
1410 C-c C-f  move to a header field (and create it if there isn't):
1411          C-c C-f C-t  move to To        C-c C-f C-s  move to Subject
1412          C-c C-f C-c  move to Cc        C-c C-f C-b  move to Bcc
1413          C-c C-f C-w  move to Fcc       C-c C-f C-r  move to Reply-To
1414          C-c C-f C-u  move to Summary   C-c C-f C-n  move to Newsgroups
1415          C-c C-f C-k  move to Keywords  C-c C-f C-d  move to Distribution
1416          C-c C-f C-f  move to Followup-To
1417 C-c C-t  message-insert-to (add a To header to a news followup)
1418 C-c C-n  message-insert-newsgroups (add a Newsgroup header to a news reply)
1419 C-c C-b  message-goto-body (move to beginning of message text).
1420 C-c C-i  message-goto-signature (move to the beginning of the signature).
1421 C-c C-w  message-insert-signature (insert `message-signature-file' file).
1422 C-c C-y  message-yank-original (insert current message, if any).
1423 C-c C-q  message-fill-yanked-message (fill what was yanked).
1424 C-c C-e  message-elide-region (elide the text between point and mark).
1425 C-c C-v  message-delete-not-region (remove the text outside the region).
1426 C-c C-z  message-kill-to-signature (kill the text up to the signature).
1427 C-c C-r  message-caesar-buffer-body (rot13 the message body).
1428 C-c C-a  mml-attach-file (attach a file as MIME).
1429 M-RET    message-newline-and-reformat (break the line and reformat)."
1430   (interactive)
1431   (if (local-variable-p 'mml-buffer-list (current-buffer))
1432       (mml-destroy-buffers))
1433   (kill-all-local-variables)
1434   (set (make-local-variable 'message-reply-buffer) nil)
1435   (make-local-variable 'message-send-actions)
1436   (make-local-variable 'message-exit-actions)
1437   (make-local-variable 'message-kill-actions)
1438   (make-local-variable 'message-postpone-actions)
1439   (make-local-variable 'message-draft-article)
1440   (make-local-hook 'kill-buffer-hook)
1441   (set-syntax-table message-mode-syntax-table)
1442   (use-local-map message-mode-map)
1443   (setq local-abbrev-table message-mode-abbrev-table)
1444   (setq major-mode 'message-mode)
1445   (setq mode-name "Message")
1446   (setq buffer-offer-save t)
1447   (make-local-variable 'facemenu-add-face-function)
1448   (make-local-variable 'facemenu-remove-face-function)
1449   (setq facemenu-add-face-function
1450         (lambda (face end)
1451           (let ((face-fun (cdr (assq face message-face-alist))))
1452             (if face-fun
1453                 (funcall face-fun (point) end)
1454               (error "Face %s not configured for %s mode" face mode-name)))
1455           "")
1456         facemenu-remove-face-function t)
1457   (make-local-variable 'paragraph-separate)
1458   (make-local-variable 'paragraph-start)
1459   ;; `-- ' precedes the signature.  `-----' appears at the start of the
1460   ;; lines that delimit forwarded messages.
1461   ;; Lines containing just >= 3 dashes, perhaps after whitespace,
1462   ;; are also sometimes used and should be separators.
1463   (setq paragraph-start
1464         (concat (regexp-quote mail-header-separator)
1465                 "$\\|[ \t]*[a-z0-9A-Z]*>+[ \t]*$\\|[ \t]*$\\|"
1466                 "-- $\\|---+$\\|"
1467                 page-delimiter
1468                 ;;!!! Uhm... shurely this can't be right?
1469                 "[> " (regexp-quote message-yank-prefix) "]+$"))
1470   (setq paragraph-separate paragraph-start)
1471   (make-local-variable 'message-reply-headers)
1472   (setq message-reply-headers nil)
1473   (make-local-variable 'message-newsreader)
1474   (make-local-variable 'message-mailer)
1475   (make-local-variable 'message-post-method)
1476   (set (make-local-variable 'message-sent-message-via) nil)
1477   (set (make-local-variable 'message-checksum) nil)
1478   (set (make-local-variable 'message-mime-part) 0)
1479   ;;(when (fboundp 'mail-hist-define-keys)
1480   ;;  (mail-hist-define-keys))
1481   (when (string-match "XEmacs\\|Lucid" emacs-version)
1482     (message-setup-toolbar))
1483   (easy-menu-add message-mode-menu message-mode-map)
1484   (easy-menu-add message-mode-field-menu message-mode-map)
1485   ;; Allow mail alias things.
1486   (when (eq message-mail-alias-type 'abbrev)
1487     (if (fboundp 'mail-abbrevs-setup)
1488         (mail-abbrevs-setup)
1489       (mail-aliases-setup)))
1490   (message-set-auto-save-file-name)
1491   (unless (string-match "XEmacs" emacs-version)
1492     (set (make-local-variable 'font-lock-defaults)
1493          '(message-font-lock-keywords t)))
1494   (make-local-variable 'adaptive-fill-regexp)
1495   (setq adaptive-fill-regexp
1496         (concat "[ \t]*[-a-z0-9A-Z]*\\(>[ \t]*\\)+[ \t]*\\|" adaptive-fill-regexp))
1497   (unless (boundp 'adaptive-fill-first-line-regexp)
1498     (setq adaptive-fill-first-line-regexp nil))
1499   (make-local-variable 'adaptive-fill-first-line-regexp)
1500   (setq adaptive-fill-first-line-regexp
1501         (concat "[ \t]*[-a-z0-9A-Z]*\\(>[ \t]*\\)+[ \t]*\\|"
1502                 adaptive-fill-first-line-regexp))
1503   (make-local-variable 'auto-fill-inhibit-regexp)
1504   (setq auto-fill-inhibit-regexp "^[A-Z][^: \n\t]+:")
1505   (mm-enable-multibyte)
1506   (make-local-variable 'indent-tabs-mode) ;Turn off tabs for indentation.
1507   (setq indent-tabs-mode nil)
1508   (mml-mode)
1509   (run-hooks 'text-mode-hook 'message-mode-hook))
1510
1511 \f
1512
1513 ;;;
1514 ;;; Message mode commands
1515 ;;;
1516
1517 ;;; Movement commands
1518
1519 (defun message-goto-to ()
1520   "Move point to the To header."
1521   (interactive)
1522   (message-position-on-field "To"))
1523
1524 (defun message-goto-subject ()
1525   "Move point to the Subject header."
1526   (interactive)
1527   (message-position-on-field "Subject"))
1528
1529 (defun message-goto-cc ()
1530   "Move point to the Cc header."
1531   (interactive)
1532   (message-position-on-field "Cc" "To"))
1533
1534 (defun message-goto-bcc ()
1535   "Move point to the Bcc  header."
1536   (interactive)
1537   (message-position-on-field "Bcc" "Cc" "To"))
1538
1539 (defun message-goto-fcc ()
1540   "Move point to the Fcc header."
1541   (interactive)
1542   (message-position-on-field "Fcc" "To" "Newsgroups"))
1543
1544 (defun message-goto-reply-to ()
1545   "Move point to the Reply-To header."
1546   (interactive)
1547   (message-position-on-field "Reply-To" "Subject"))
1548
1549 (defun message-goto-newsgroups ()
1550   "Move point to the Newsgroups header."
1551   (interactive)
1552   (message-position-on-field "Newsgroups"))
1553
1554 (defun message-goto-distribution ()
1555   "Move point to the Distribution header."
1556   (interactive)
1557   (message-position-on-field "Distribution"))
1558
1559 (defun message-goto-followup-to ()
1560   "Move point to the Followup-To header."
1561   (interactive)
1562   (message-position-on-field "Followup-To" "Newsgroups"))
1563
1564 (defun message-goto-keywords ()
1565   "Move point to the Keywords header."
1566   (interactive)
1567   (message-position-on-field "Keywords" "Subject"))
1568
1569 (defun message-goto-summary ()
1570   "Move point to the Summary header."
1571   (interactive)
1572   (message-position-on-field "Summary" "Subject"))
1573
1574 (defun message-goto-body ()
1575   "Move point to the beginning of the message body."
1576   (interactive)
1577   (if (looking-at "[ \t]*\n") (expand-abbrev))
1578   (goto-char (point-min))
1579   (or (search-forward (concat "\n" mail-header-separator "\n") nil