(message-draft-coding-system): Improve comment;
[gnus] / lisp / message.el
1 ;;; message.el --- composing mail and news messages
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002
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
34   (require 'cl)
35   (defvar gnus-list-identifiers)) ; gnus-sum is required where necessary
36 (require 'canlock)
37 (require 'mailheader)
38 (require 'nnheader)
39 ;; This is apparently necessary even though things are autoloaded.
40 ;; Because we dynamically bind mail-abbrev-mode-regexp, we'd better
41 ;; require mailabbrev here.
42 (if (featurep 'xemacs)
43     (require 'mail-abbrevs)
44   (require 'mailabbrev))
45 (require 'mail-parse)
46 (require 'mml)
47 (require 'rfc822)
48 (eval-and-compile
49   (autoload 'sha1 "sha1-el"))
50
51 (defgroup message '((user-mail-address custom-variable)
52                     (user-full-name custom-variable))
53   "Mail and news message composing."
54   :link '(custom-manual "(message)Top")
55   :group 'mail
56   :group 'news)
57
58 (put 'user-mail-address 'custom-type 'string)
59 (put 'user-full-name 'custom-type 'string)
60
61 (defgroup message-various nil
62   "Various Message Variables"
63   :link '(custom-manual "(message)Various Message Variables")
64   :group 'message)
65
66 (defgroup message-buffers nil
67   "Message Buffers"
68   :link '(custom-manual "(message)Message Buffers")
69   :group 'message)
70
71 (defgroup message-sending nil
72   "Message Sending"
73   :link '(custom-manual "(message)Sending Variables")
74   :group 'message)
75
76 (defgroup message-interface nil
77   "Message Interface"
78   :link '(custom-manual "(message)Interface")
79   :group 'message)
80
81 (defgroup message-forwarding nil
82   "Message Forwarding"
83   :link '(custom-manual "(message)Forwarding")
84   :group 'message-interface)
85
86 (defgroup message-insertion nil
87   "Message Insertion"
88   :link '(custom-manual "(message)Insertion")
89   :group 'message)
90
91 (defgroup message-headers nil
92   "Message Headers"
93   :link '(custom-manual "(message)Message Headers")
94   :group 'message)
95
96 (defgroup message-news nil
97   "Composing News Messages"
98   :group 'message)
99
100 (defgroup message-mail nil
101   "Composing Mail Messages"
102   :group 'message)
103
104 (defgroup message-faces nil
105   "Faces used for message composing."
106   :group 'message
107   :group 'faces)
108
109 (defcustom message-directory "~/Mail/"
110   "*Directory from which all other mail file variables are derived."
111   :group 'message-various
112   :type 'directory)
113
114 (defcustom message-max-buffers 10
115   "*How many buffers to keep before starting to kill them off."
116   :group 'message-buffers
117   :type 'integer)
118
119 (defcustom message-send-rename-function nil
120   "Function called to rename the buffer after sending it."
121   :group 'message-buffers
122   :type '(choice function (const nil)))
123
124 (defcustom message-fcc-handler-function 'message-output
125   "*A function called to save outgoing articles.
126 This function will be called with the name of the file to store the
127 article in.  The default function is `message-output' which saves in Unix
128 mailbox format."
129   :type '(radio (function-item message-output)
130                 (function :tag "Other"))
131   :group 'message-sending)
132
133 (defcustom message-fcc-externalize-attachments nil
134   "If non-nil, attachments are included as external parts in Fcc copies."
135   :type 'boolean
136   :group 'message-sending)
137
138 (defcustom message-courtesy-message
139   "The following message is a courtesy copy of an article\nthat has been posted to %s as well.\n\n"
140   "*This is inserted at the start of a mailed copy of a posted message.
141 If the string contains the format spec \"%s\", the Newsgroups
142 the article has been posted to will be inserted there.
143 If this variable is nil, no such courtesy message will be added."
144   :group 'message-sending
145   :type 'string)
146
147 (defcustom message-ignored-bounced-headers "^\\(Received\\|Return-Path\\):"
148   "*Regexp that matches headers to be removed in resent bounced mail."
149   :group 'message-interface
150   :type 'regexp)
151
152 ;;;###autoload
153 (defcustom message-from-style 'default
154   "*Specifies how \"From\" headers look.
155
156 If nil, they contain just the return address like:
157         king@grassland.com
158 If `parens', they look like:
159         king@grassland.com (Elvis Parsley)
160 If `angles', they look like:
161         Elvis Parsley <king@grassland.com>
162
163 Otherwise, most addresses look like `angles', but they look like
164 `parens' if `angles' would need quoting and `parens' would not."
165   :type '(choice (const :tag "simple" nil)
166                  (const parens)
167                  (const angles)
168                  (const default))
169   :group 'message-headers)
170
171 (defcustom message-insert-canlock t
172   "Whether to insert a Cancel-Lock header in news postings."
173   :version "21.3"
174   :group 'message-headers
175   :type 'boolean)
176
177 (defcustom message-syntax-checks 
178   (if message-insert-canlock '((sender . disabled)) nil)
179   ;; Guess this one shouldn't be easy to customize...
180   "*Controls what syntax checks should not be performed on outgoing posts.
181 To disable checking of long signatures, for instance, add
182  `(signature . disabled)' to this list.
183
184 Don't touch this variable unless you really know what you're doing.
185
186 Checks include `subject-cmsg', `multiple-headers', `sendsys',
187 `message-id', `from', `long-lines', `control-chars', `size',
188 `new-text', `quoting-style', `redirected-followup', `signature',
189 `approved', `sender', `empty', `empty-headers', `message-id', `from',
190 `subject', `shorten-followup-to', `existing-newsgroups',
191 `buffer-file-name', `unchanged', `newsgroups', `reply-to'."
192   :group 'message-news
193   :type '(repeat sexp))                 ; Fixme: improve this
194
195 (defcustom message-required-news-headers
196   '(From Newsgroups Subject Date Message-ID
197          (optional . Organization) Lines
198          (optional . User-Agent))
199   "*Headers to be generated or prompted for when posting an article.
200 RFC977 and RFC1036 require From, Date, Newsgroups, Subject,
201 Message-ID.  Organization, Lines, In-Reply-To, Expires, and
202 User-Agent are optional.  If don't you want message to insert some
203 header, remove it from this list."
204   :group 'message-news
205   :group 'message-headers
206   :type '(repeat sexp))
207
208 (defcustom message-required-mail-headers
209   '(From Subject Date (optional . In-Reply-To) Message-ID
210          (optional . User-Agent))
211   "*Headers to be generated or prompted for when mailing a message.
212 It is recommended that From, Date, To, Subject and Message-ID be
213 included.  Organization and User-Agent are optional."
214   :group 'message-mail
215   :group 'message-headers
216   :type '(repeat sexp))
217
218 (defcustom message-deletable-headers '(Message-ID Date Lines)
219   "Headers to be deleted if they already exist and were generated by message previously."
220   :group 'message-headers
221   :type 'sexp)
222
223 (defcustom message-ignored-news-headers
224   "^NNTP-Posting-Host:\\|^Xref:\\|^[BGF]cc:\\|^Resent-Fcc:\\|^X-Draft-From:\\|^X-Gnus-Agent-Meta-Information:"
225   "*Regexp of headers to be removed unconditionally before posting."
226   :group 'message-news
227   :group 'message-headers
228   :type 'regexp)
229
230 (defcustom message-ignored-mail-headers
231   "^[GF]cc:\\|^Resent-Fcc:\\|^Xref:\\|^X-Draft-From:\\|^X-Gnus-Agent-Meta-Information:"
232   "*Regexp of headers to be removed unconditionally before mailing."
233   :group 'message-mail
234   :group 'message-headers
235   :type 'regexp)
236
237 (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:\\|^Cancel-Lock:\\|^Cancel-Key:\\|^X-Hashcash:"
238   "*Header lines matching this regexp will be deleted before posting.
239 It's best to delete old Path and Date headers before posting to avoid
240 any confusion."
241   :group 'message-interface
242   :type 'regexp)
243
244 (defcustom message-subject-re-regexp
245   "^[ \t]*\\([Rr][Ee]\\(\\[[0-9]*\\]\\)*:[ \t]*\\)*[ \t]*"
246   "*Regexp matching \"Re: \" in the subject line."
247   :group 'message-various
248   :type 'regexp)
249
250 ;;; Start of variables adopted from `message-utils.el'.
251
252 (defcustom message-subject-trailing-was-query 'ask
253   ;; should it default to nil or ask?
254   "*What to do with trailing \"(was: <old subject>)\" in subject lines.
255 If nil, leave the subject unchanged.  If it is the symbol `ask', query
256 the user what do do.  In this case, the subject is matched against
257 `message-subject-trailing-was-ask-regexp'.  If
258 `message-subject-trailing-was-query' is t, always strip the trailing
259 old subject.  In this case, `message-subject-trailing-was-regexp' is
260 used."
261   :type '(choice (const :tag "never" nil)
262                  (const :tag "always strip" t)
263                  (const ask))
264   :group 'message-various)
265
266 (defcustom message-subject-trailing-was-ask-regexp
267   "[ \t]*\\([[(]+[Ww][Aa][Ss][ \t]*.*[\])]+\\)"
268   "*Regexp matching \"(was: <old subject>)\" in the subject line.
269
270 The function `message-strip-subject-trailing-was' uses this regexp if
271 `message-subject-trailing-was-query' is set to the symbol `ask'.  If
272 the variable is t instead of `ask', use
273 `message-subject-trailing-was-regexp' instead.
274
275 It is okay to create some false positives here, as the user is asked."
276   :group 'message-various
277   :type 'regexp)
278
279 (defcustom message-subject-trailing-was-regexp
280   "[ \t]*\\((*[Ww][Aa][Ss]:[ \t]*.*)\\)"
281   "*Regexp matching \"(was: <old subject>)\" in the subject line.
282
283 If `message-subject-trailing-was-query' is set to t, the subject is
284 matched against `message-subject-trailing-was-regexp' in
285 `message-strip-subject-trailing-was'.  You should use a regexp creating very
286 few false positives here."
287   :group 'message-various
288   :type 'regexp)
289
290 ;;; marking inserted text
291
292 ;;;###autoload
293 (defcustom message-mark-insert-begin
294   "--8<---------------cut here---------------start------------->8---\n"
295   "How to mark the beginning of some inserted text."
296   :type 'string
297   :group 'message-various)
298
299 ;;;###autoload
300 (defcustom message-mark-insert-end
301   "--8<---------------cut here---------------end--------------->8---\n"
302   "How to mark the end of some inserted text."
303   :type 'string
304   :group 'message-various)
305
306 ;;;###autoload
307 (defcustom message-archive-header
308   "X-No-Archive: Yes\n"
309   "Header to insert when you don't want your article to be archived.
310 Archives \(such as groups.googgle.com\) respect this header."
311   :type 'string
312   :group 'message-various)
313
314 ;;;###autoload
315 (defcustom message-archive-note
316   "X-No-Archive: Yes - save http://groups.google.com/"
317   "Note to insert why you wouldn't want this posting archived. 
318 If nil, don't insert any text in the body."
319   :type 'string
320   :group 'message-various)
321
322 ;;; Crossposts and Followups
323 ;; inspired by JoH-followup-to by Jochem Huhman <joh  at gmx.de>
324 ;; new suggestions by R. Weikusat <rw at another.de>
325
326 (defvar message-xpost-old-target nil
327   "Old target for cross-posts or follow-ups.")
328 (make-variable-buffer-local 'message-xpost-old-target)
329
330 ;;;###autoload
331 (defcustom message-xpost-default t
332   "When non-nil `message-xpost-fup2' will normally perform a crosspost.
333 If nil, `message-xpost-fup2' will only do a followup.  Note that you
334 can explicitly override this setting by calling `message-xpost-fup2'
335 with a prefix."
336   :type 'boolean
337   :group 'message-various)
338
339 ;;;###autoload
340 (defcustom message-xpost-note
341   "Crosspost & Followup-To: "
342   "Note to insert before signature to notify of xpost and follow-up."
343   :type 'string
344   :group 'message-various)
345
346 ;;;###autoload
347 (defcustom message-fup2-note
348   "Followup-To: "
349   "Note to insert before signature to notify of follow-up only."
350   :type 'string
351   :group 'message-various)
352
353 ;;;###autoload
354 (defcustom message-xpost-note-function
355   'message-xpost-insert-note
356   "Function to use to insert note about Crosspost or Followup-To.  
357 The function will be called with four arguments.  The function should not only
358 insert a note, but also ensure old notes are deleted.  See the documentation
359 for `message-xpost-insert-note'. "
360   :type 'function
361   :group 'message-various)
362
363 ;;; End of variables adopted from `message-utils.el'.
364
365 ;;;###autoload
366 (defcustom message-signature-separator "^-- *$"
367   "Regexp matching the signature separator."
368   :type 'regexp
369   :group 'message-various)
370
371 (defcustom message-elide-ellipsis "\n[...]\n\n"
372   "*The string which is inserted for elided text."
373   :type 'string
374   :group 'message-various)
375
376 (defcustom message-interactive t
377   "Non-nil means when sending a message wait for and display errors.
378 nil means let mailer mail back a message to report errors."
379   :group 'message-sending
380   :group 'message-mail
381   :type 'boolean)
382
383 (defcustom message-generate-new-buffers 'unique
384   "*Non-nil means create a new message buffer whenever `message-setup' is called.
385 If this is a function, call that function with three parameters:  The type,
386 the to address and the group name.  (Any of these may be nil.)  The function
387 should return the new buffer name."
388   :group 'message-buffers
389   :type '(choice (const :tag "off" nil)
390                  (const :tag "unique" unique)
391                  (const :tag "unsent" unsent)
392                  (function fun)))
393
394 (defcustom message-kill-buffer-on-exit nil
395   "*Non-nil means that the message buffer will be killed after sending a message."
396   :group 'message-buffers
397   :type 'boolean)
398
399 (eval-when-compile
400   (defvar gnus-local-organization))
401 (defcustom message-user-organization
402   (or (and (boundp 'gnus-local-organization)
403            (stringp gnus-local-organization)
404            gnus-local-organization)
405       (getenv "ORGANIZATION")
406       t)
407   "*String to be used as an Organization header.
408 If t, use `message-user-organization-file'."
409   :group 'message-headers
410   :type '(choice string
411                  (const :tag "consult file" t)))
412
413 ;;;###autoload
414 (defcustom message-user-organization-file "/usr/lib/news/organization"
415   "*Local news organization file."
416   :type 'file
417   :group 'message-headers)
418
419 (defcustom message-make-forward-subject-function
420   'message-forward-subject-author-subject
421   "*List of functions called to generate subject headers for forwarded messages.
422 The subject generated by the previous function is passed into each
423 successive function.
424
425 The provided functions are:
426
427 * `message-forward-subject-author-subject' (Source of article (author or
428       newsgroup)), in brackets followed by the subject
429 * `message-forward-subject-fwd' (Subject of article with 'Fwd:' prepended
430       to it."
431   :group 'message-forwarding
432   :type '(radio (function-item message-forward-subject-author-subject)
433                 (function-item message-forward-subject-fwd)
434                 (repeat :tag "List of functions" function)))
435
436 (defcustom message-forward-as-mime t
437   "*If non-nil, forward messages as an inline/rfc822 MIME section.  Otherwise, directly inline the old message in the forwarded message."
438   :version "21.1"
439   :group 'message-forwarding
440   :type 'boolean)
441
442 (defcustom message-forward-show-mml t
443   "*If non-nil, forward messages are shown as mml.  Otherwise, forward messages are unchanged."
444   :version "21.1"
445   :group 'message-forwarding
446   :type 'boolean)
447
448 (defcustom message-forward-before-signature t
449   "*If non-nil, put forwarded message before signature, else after."
450   :group 'message-forwarding
451   :type 'boolean)
452
453 (defcustom message-wash-forwarded-subjects nil
454   "*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."
455   :group 'message-forwarding
456   :type 'boolean)
457
458 (defcustom message-ignored-resent-headers "^Return-receipt\\|^X-Gnus\\|^Gnus-Warning:\\|^>?From "
459   "*All headers that match this regexp will be deleted when resending a message."
460   :group 'message-interface
461   :type 'regexp)
462
463 (defcustom message-forward-ignored-headers "^Content-Transfer-Encoding:\\|^X-Gnus"
464   "*All headers that match this regexp will be deleted when forwarding a message."
465   :version "21.1"
466   :group 'message-forwarding
467   :type '(choice (const :tag "None" nil)
468                  regexp))
469
470 (defcustom message-ignored-cited-headers "."
471   "*Delete these headers from the messages you yank."
472   :group 'message-insertion
473   :type 'regexp)
474
475 (defcustom message-cite-prefix-regexp
476   (if (string-match "[[:digit:]]" "1") ;; support POSIX?
477       "\\([ \t]*[-_.[:word:]]+>+\\|[ \t]*[]>|}+]\\)+"
478     ;; ?-, ?_ or ?. MUST NOT be in syntax entry w.
479     (let ((old-table (syntax-table))
480           non-word-constituents)
481       (set-syntax-table text-mode-syntax-table)
482       (setq non-word-constituents
483             (concat
484              (if (string-match "\\w" "-")  "" "-")
485              (if (string-match "\\w" "_")  "" "_")
486              (if (string-match "\\w" ".")  "" ".")))
487       (set-syntax-table old-table)
488       (if (equal non-word-constituents "")
489           "\\([ \t]*\\(\\w\\)+>+\\|[ \t]*[]>|}+]\\)+"
490         (concat "\\([ \t]*\\(\\w\\|["
491                 non-word-constituents
492                 "]\\)+>+\\|[ \t]*[]>|}+]\\)+"))))
493   "*Regexp matching the longest possible citation prefix on a line."
494   :group 'message-insertion
495   :type 'regexp)
496
497 (defcustom message-cancel-message "I am canceling my own article.\n"
498   "Message to be inserted in the cancel message."
499   :group 'message-interface
500   :type 'string)
501
502 ;; Useful to set in site-init.el
503 ;;;###autoload
504 (defcustom message-send-mail-function 'message-send-mail-with-sendmail
505   "Function to call to send the current buffer as mail.
506 The headers should be delimited by a line whose contents match the
507 variable `mail-header-separator'.
508
509 Valid values include `message-send-mail-with-sendmail' (the default),
510 `message-send-mail-with-mh', `message-send-mail-with-qmail',
511 `message-smtpmail-send-it', `smtpmail-send-it' and `feedmail-send-it'.
512
513 See also `send-mail-function'."
514   :type '(radio (function-item message-send-mail-with-sendmail)
515                 (function-item message-send-mail-with-mh)
516                 (function-item message-send-mail-with-qmail)
517                 (function-item message-smtpmail-send-it)
518                 (function-item smtpmail-send-it)
519                 (function-item feedmail-send-it)
520                 (function :tag "Other"))
521   :group 'message-sending
522   :group 'message-mail)
523
524 (defcustom message-send-news-function 'message-send-news
525   "Function to call to send the current buffer as news.
526 The headers should be delimited by a line whose contents match the
527 variable `mail-header-separator'."
528   :group 'message-sending
529   :group 'message-news
530   :type 'function)
531
532 (defcustom message-reply-to-function nil
533   "If non-nil, function that should return a list of headers.
534 This function should pick out addresses from the To, Cc, and From headers
535 and respond with new To and Cc headers."
536   :group 'message-interface
537   :type '(choice function (const nil)))
538
539 (defcustom message-wide-reply-to-function nil
540   "If non-nil, function that should return a list of headers.
541 This function should pick out addresses from the To, Cc, and From headers
542 and respond with new To and Cc headers."
543   :group 'message-interface
544   :type '(choice function (const nil)))
545
546 (defcustom message-followup-to-function nil
547   "If non-nil, function that should return a list of headers.
548 This function should pick out addresses from the To, Cc, and From headers
549 and respond with new To and Cc headers."
550   :group 'message-interface
551   :type '(choice function (const nil)))
552
553 (defcustom message-use-followup-to 'ask
554   "*Specifies what to do with Followup-To header.
555 If nil, always ignore the header.  If it is t, use its value, but
556 query before using the \"poster\" value.  If it is the symbol `ask',
557 always query the user whether to use the value.  If it is the symbol
558 `use', always use the value."
559   :group 'message-interface
560   :type '(choice (const :tag "ignore" nil)
561                  (const :tag "use & query" t)
562                  (const use)
563                  (const ask)))
564
565 (defcustom message-use-mail-followup-to 'use
566   "*Specifies what to do with Mail-Followup-To header.
567 If nil, always ignore the header.  If it is the symbol `ask', always
568 query the user whether to use the value.  If it is the symbol `use',
569 always use the value."
570   :group 'message-interface
571   :type '(choice (const :tag "ignore" nil)
572                  (const use)
573                  (const ask)))
574
575 (defcustom message-subscribed-address-functions nil
576   "*Specifies functions for determining list subscription.
577 If nil, do not attempt to determine list subscribtion with functions.
578 If non-nil, this variable contains a list of functions which return
579 regular expressions to match lists.  These functions can be used in
580 conjunction with `message-subscribed-regexps' and
581 `message-subscribed-addresses'."
582   :group 'message-interface
583   :type '(repeat sexp))
584
585 (defcustom message-subscribed-address-file nil
586   "*A file containing addresses the user is subscribed to.
587 If nil, do not look at any files to determine list subscriptions.  If
588 non-nil, each line of this file should be a mailing list address."
589   :group 'message-interface
590   :type 'string)
591
592 (defcustom message-subscribed-addresses nil
593   "*Specifies a list of addresses the user is subscribed to.
594 If nil, do not use any predefined list subscriptions.  This list of
595 addresses can be used in conjuction with
596 `message-subscribed-address-functions' and `message-subscribed-regexps'."
597   :group 'message-interface
598   :type '(repeat string))
599
600 (defcustom message-subscribed-regexps nil
601   "*Specifies a list of addresses the user is subscribed to.
602 If nil, do not use any predefined list subscriptions.  This list of
603 regular expressions can be used in conjuction with
604 `message-subscribed-address-functions' and `message-subscribed-addresses'."
605   :group 'message-interface
606   :type '(repeat regexp))
607
608 (defcustom message-allow-no-recipients 'ask
609   "Specifies what to do when there are no recipients other than Gcc/Fcc.
610 If it is the symbol `always', the posting is allowed.  If it is the
611 symbol `never', the posting is not allowed.  If it is the symbol
612 `ask', you are prompted."
613   :group 'message-interface
614   :type '(choice (const always)
615                  (const never)
616                  (const ask)))
617
618 (defcustom message-sendmail-f-is-evil nil
619   "*Non-nil means don't add \"-f username\" to the sendmail command line.
620 Doing so would be even more evil than leaving it out."
621   :group 'message-sending
622   :type 'boolean)
623
624 ;; qmail-related stuff
625 (defcustom message-qmail-inject-program "/var/qmail/bin/qmail-inject"
626   "Location of the qmail-inject program."
627   :group 'message-sending
628   :type 'file)
629
630 (defcustom message-qmail-inject-args nil
631   "Arguments passed to qmail-inject programs.
632 This should be a list of strings, one string for each argument.  It
633 may also be a function.
634
635 For e.g., if you wish to set the envelope sender address so that bounces
636 go to the right place or to deal with listserv's usage of that address, you
637 might set this variable to '(\"-f\" \"you@some.where\")."
638   :group 'message-sending
639   :type '(choice (function)
640                  (repeat string)))
641
642 (defvar message-cater-to-broken-inn t
643   "Non-nil means Gnus should not fold the `References' header.
644 Folding `References' makes ancient versions of INN create incorrect
645 NOV lines.")
646
647 (eval-when-compile
648   (defvar gnus-post-method)
649   (defvar gnus-select-method))
650 (defcustom message-post-method
651   (cond ((and (boundp 'gnus-post-method)
652               (listp gnus-post-method)
653               gnus-post-method)
654          gnus-post-method)
655         ((boundp 'gnus-select-method)
656          gnus-select-method)
657         (t '(nnspool "")))
658   "*Method used to post news.
659 Note that when posting from inside Gnus, for instance, this
660 variable isn't used."
661   :group 'message-news
662   :group 'message-sending
663   ;; This should be the `gnus-select-method' widget, but that might
664   ;; create a dependence to `gnus.el'.
665   :type 'sexp)
666
667 (defcustom message-generate-headers-first nil
668   "*If non-nil, generate all required headers before composing.
669 The variables `message-required-news-headers' and
670 `message-required-mail-headers' specify which headers to generate.
671
672 Note that the variable `message-deletable-headers' specifies headers which
673 are to be deleted and then re-generated before sending, so this variable
674 will not have a visible effect for those headers."
675   :group 'message-headers
676   :type 'boolean)
677
678 (defcustom message-setup-hook nil
679   "Normal hook, run each time a new outgoing message is initialized.
680 The function `message-setup' runs this hook."
681   :group 'message-various
682   :type 'hook)
683
684 (defcustom message-cancel-hook nil
685   "Hook run when cancelling articles."
686   :group 'message-various
687   :type 'hook)
688
689 (defcustom message-signature-setup-hook nil
690   "Normal hook, run each time a new outgoing message is initialized.
691 It is run after the headers have been inserted and before
692 the signature is inserted."
693   :group 'message-various
694   :type 'hook)
695
696 (defcustom message-mode-hook nil
697   "Hook run in message mode buffers."
698   :group 'message-various
699   :type 'hook)
700
701 (defcustom message-header-hook nil
702   "Hook run in a message mode buffer narrowed to the headers."
703   :group 'message-various
704   :type 'hook)
705
706 (defcustom message-header-setup-hook nil
707   "Hook called narrowed to the headers when setting up a message buffer."
708   :group 'message-various
709   :type 'hook)
710
711 (defcustom message-minibuffer-local-map
712   (let ((map (make-sparse-keymap 'message-minibuffer-local-map)))
713     (set-keymap-parent map minibuffer-local-map)
714     map)
715   "Keymap for `message-read-from-minibuffer'.")
716
717 ;;;###autoload
718 (defcustom message-citation-line-function 'message-insert-citation-line
719   "*Function called to insert the \"Whomever writes:\" line.
720
721 Note that Gnus provides a feature where the reader can click on
722 `writes:' to hide the cited text.  If you change this line too much,
723 people who read your message will have to change their Gnus
724 configuration.  See the variable `gnus-cite-attribution-suffix'."
725   :type 'function
726   :group 'message-insertion)
727
728 ;;;###autoload
729 (defcustom message-yank-prefix "> "
730   "*Prefix inserted on the lines of yanked messages.
731 Fix `message-cite-prefix-regexp' if it is set to an abnormal value.
732 See also `message-yank-cited-prefix'."
733   :type 'string
734   :group 'message-insertion)
735
736 (defcustom message-yank-cited-prefix ">"
737   "*Prefix inserted on cited or empty lines of yanked messages.
738 Fix `message-cite-prefix-regexp' if it is set to an abnormal value.
739 See also `message-yank-prefix'."
740   :type 'string
741   :group 'message-insertion)
742
743 (defcustom message-indentation-spaces 3
744   "*Number of spaces to insert at the beginning of each cited line.
745 Used by `message-yank-original' via `message-yank-cite'."
746   :group 'message-insertion
747   :type 'integer)
748
749 ;;;###autoload
750 (defcustom message-cite-function 'message-cite-original
751   "*Function for citing an original message.
752 Predefined functions include `message-cite-original' and
753 `message-cite-original-without-signature'.
754 Note that `message-cite-original' uses `mail-citation-hook' if that is non-nil."
755   :type '(radio (function-item message-cite-original)
756                 (function-item message-cite-original-without-signature)
757                 (function-item sc-cite-original)
758                 (function :tag "Other"))
759   :group 'message-insertion)
760
761 ;;;###autoload
762 (defcustom message-indent-citation-function 'message-indent-citation
763   "*Function for modifying a citation just inserted in the mail buffer.
764 This can also be a list of functions.  Each function can find the
765 citation between (point) and (mark t).  And each function should leave
766 point and mark around the citation text as modified."
767   :type 'function
768   :group 'message-insertion)
769
770 ;;;###autoload
771 (defcustom message-signature t
772   "*String to be inserted at the end of the message buffer.
773 If t, the `message-signature-file' file will be inserted instead.
774 If a function, the result from the function will be used instead.
775 If a form, the result from the form will be used instead."
776   :type 'sexp
777   :group 'message-insertion)
778
779 ;;;###autoload
780 (defcustom message-signature-file "~/.signature"
781   "*Name of file containing the text inserted at end of message buffer.
782 Ignored if the named file doesn't exist.
783 If nil, don't insert a signature."
784   :type '(choice file (const :tags "None" nil))
785   :group 'message-insertion)
786
787 (defcustom message-distribution-function nil
788   "*Function called to return a Distribution header."
789   :group 'message-news
790   :group 'message-headers
791   :type '(choice function (const nil)))
792
793 (defcustom message-expires 14
794   "Number of days before your article expires."
795   :group 'message-news
796   :group 'message-headers
797   :link '(custom-manual "(message)News Headers")
798   :type 'integer)
799
800 (defcustom message-user-path nil
801   "If nil, use the NNTP server name in the Path header.
802 If stringp, use this; if non-nil, use no host name (user name only)."
803   :group 'message-news
804   :group 'message-headers
805   :link '(custom-manual "(message)News Headers")
806   :type '(choice (const :tag "nntp" nil)
807                  (string :tag "name")
808                  (sexp :tag "none" :format "%t" t)))
809
810 (defvar message-reply-buffer nil)
811 (defvar message-reply-headers nil
812   "The headers of the current replied article.
813 It is a vector of the following headers:
814 \[number subject from date id references chars lines xref extra].")
815 (defvar message-newsreader nil)
816 (defvar message-mailer nil)
817 (defvar message-sent-message-via nil)
818 (defvar message-checksum nil)
819 (defvar message-send-actions nil
820   "A list of actions to be performed upon successful sending of a message.")
821 (defvar message-exit-actions nil
822   "A list of actions to be performed upon exiting after sending a message.")
823 (defvar message-kill-actions nil
824   "A list of actions to be performed before killing a message buffer.")
825 (defvar message-postpone-actions nil
826   "A list of actions to be performed after postponing a message.")
827
828 (define-widget 'message-header-lines 'text
829   "All header lines must be LFD terminated."
830   :format "%{%t%}:%n%v"
831   :valid-regexp "^\\'"
832   :error "All header lines must be newline terminated")
833
834 (defcustom message-default-headers ""
835   "*A string containing header lines to be inserted in outgoing messages.
836 It is inserted before you edit the message, so you can edit or delete
837 these lines."
838   :group 'message-headers
839   :type 'message-header-lines)
840
841 (defcustom message-default-mail-headers ""
842   "*A string of header lines to be inserted in outgoing mails."
843   :group 'message-headers
844   :group 'message-mail
845   :type 'message-header-lines)
846
847 (defcustom message-default-news-headers ""
848   "*A string of header lines to be inserted in outgoing news articles."
849   :group 'message-headers
850   :group 'message-news
851   :type 'message-header-lines)
852
853 ;; Note: could use /usr/ucb/mail instead of sendmail;
854 ;; options -t, and -v if not interactive.
855 (defcustom message-mailer-swallows-blank-line
856   (if (and (string-match "sparc-sun-sunos\\(\\'\\|[^5]\\)"
857                          system-configuration)
858            (file-readable-p "/etc/sendmail.cf")
859            (let ((buffer (get-buffer-create " *temp*")))
860              (unwind-protect
861                  (save-excursion
862                    (set-buffer buffer)
863                    (insert-file-contents "/etc/sendmail.cf")
864                    (goto-char (point-min))
865                    (let ((case-fold-search nil))
866                      (re-search-forward "^OR\\>" nil t)))
867                (kill-buffer buffer))))
868       ;; According to RFC822, "The field-name must be composed of printable
869       ;; ASCII characters (i. e., characters that have decimal values between
870       ;; 33 and 126, except colon)", i. e., any chars except ctl chars,
871       ;; space, or colon.
872       '(looking-at "[ \t]\\|[][!\"#$%&'()*+,-./0-9;<=>?@A-Z\\\\^_`a-z{|}~]+:"))
873   "*Set this non-nil if the system's mailer runs the header and body together.
874 \(This problem exists on Sunos 4 when sendmail is run in remote mode.)
875 The value should be an expression to test whether the problem will
876 actually occur."
877   :group 'message-sending
878   :type 'sexp)
879
880 ;;;###autoload
881 (define-mail-user-agent 'message-user-agent
882   'message-mail 'message-send-and-exit
883   'message-kill-buffer 'message-send-hook)
884
885 (defvar message-mh-deletable-headers '(Message-ID Date Lines Sender)
886   "If non-nil, delete the deletable headers before feeding to mh.")
887
888 (defvar message-send-method-alist
889   '((news message-news-p message-send-via-news)
890     (mail message-mail-p message-send-via-mail))
891   "Alist of ways to send outgoing messages.
892 Each element has the form
893
894   \(TYPE PREDICATE FUNCTION)
895
896 where TYPE is a symbol that names the method; PREDICATE is a function
897 called without any parameters to determine whether the message is
898 a message of type TYPE; and FUNCTION is a function to be called if
899 PREDICATE returns non-nil.  FUNCTION is called with one parameter --
900 the prefix.")
901
902 (defcustom message-mail-alias-type 'abbrev
903   "*What alias expansion type to use in Message buffers.
904 The default is `abbrev', which uses mailabbrev.  nil switches
905 mail aliases off."
906   :group 'message
907   :link '(custom-manual "(message)Mail Aliases")
908   :type '(choice (const :tag "Use Mailabbrev" abbrev)
909                  (const :tag "No expansion" nil)))
910
911 (defcustom message-auto-save-directory
912   (file-name-as-directory (nnheader-concat message-directory "drafts"))
913   "*Directory where Message auto-saves buffers if Gnus isn't running.
914 If nil, Message won't auto-save."
915   :group 'message-buffers
916   :type '(choice directory (const :tag "Don't auto-save" nil)))
917
918 (defcustom message-default-charset
919   (and (not (mm-multibyte-p)) 'iso-8859-1)
920   "Default charset used in non-MULE Emacsen.
921 If nil, you might be asked to input the charset."
922   :version "21.1"
923   :group 'message
924   :type 'symbol)
925
926 (defcustom message-dont-reply-to-names
927   (and (boundp 'rmail-dont-reply-to-names) rmail-dont-reply-to-names)
928   "*A regexp specifying addresses to prune when doing wide replies.
929 A value of nil means exclude your own user name only."
930   :version "21.1"
931   :group 'message
932   :type '(choice (const :tag "Yourself" nil)
933                  regexp))
934
935 (defvar message-shoot-gnksa-feet nil
936   "*A list of GNKSA feet you are allowed to shoot.
937 Gnus gives you all the opportunity you could possibly want for
938 shooting yourself in the foot.  Also, Gnus allows you to shoot the
939 feet of Good Net-Keeping Seal of Approval. The following are foot
940 candidates:
941 `empty-article'     Allow you to post an empty article;
942 `quoted-text-only'  Allow you to post quoted text only;
943 `multiple-copies'   Allow you to post multiple copies;
944 `cancel-messages'   Allow you to cancel or supersede messages from 
945                     your other email addresses.")
946
947 (defsubst message-gnksa-enable-p (feature)
948   (or (not (listp message-shoot-gnksa-feet))
949       (memq feature message-shoot-gnksa-feet)))
950
951 ;;; Internal variables.
952 ;;; Well, not really internal.
953
954 (defvar message-mode-syntax-table
955   (let ((table (copy-syntax-table text-mode-syntax-table)))
956     (modify-syntax-entry ?% ". " table)
957     (modify-syntax-entry ?> ". " table)
958     (modify-syntax-entry ?< ". " table)
959     table)
960   "Syntax table used while in Message mode.")
961
962 (defface message-header-to-face
963   '((((class color)
964       (background dark))
965      (:foreground "green2" :bold t))
966     (((class color)
967       (background light))
968      (:foreground "MidnightBlue" :bold t))
969     (t
970      (:bold t :italic t)))
971   "Face used for displaying From headers."
972   :group 'message-faces)
973
974 (defface message-header-cc-face
975   '((((class color)
976       (background dark))
977      (:foreground "green4" :bold t))
978     (((class color)
979       (background light))
980      (:foreground "MidnightBlue"))
981     (t
982      (:bold t)))
983   "Face used for displaying Cc headers."
984   :group 'message-faces)
985
986 (defface message-header-subject-face
987   '((((class color)
988       (background dark))
989      (:foreground "green3"))
990     (((class color)
991       (background light))
992      (:foreground "navy blue" :bold t))
993     (t
994      (:bold t)))
995   "Face used for displaying subject headers."
996   :group 'message-faces)
997
998 (defface message-header-newsgroups-face
999   '((((class color)
1000       (background dark))
1001      (:foreground "yellow" :bold t :italic t))
1002     (((class color)
1003       (background light))
1004      (:foreground "blue4" :bold t :italic t))
1005     (t
1006      (:bold t :italic t)))
1007   "Face used for displaying newsgroups headers."
1008   :group 'message-faces)
1009
1010 (defface message-header-other-face
1011   '((((class color)
1012       (background dark))
1013      (:foreground "#b00000"))
1014     (((class color)
1015       (background light))
1016      (:foreground "steel blue"))
1017     (t
1018      (:bold t :italic t)))
1019   "Face used for displaying newsgroups headers."
1020   :group 'message-faces)
1021
1022 (defface message-header-name-face
1023   '((((class color)
1024       (background dark))
1025      (:foreground "DarkGreen"))
1026     (((class color)
1027       (background light))
1028      (:foreground "cornflower blue"))
1029     (t
1030      (:bold t)))
1031   "Face used for displaying header names."
1032   :group 'message-faces)
1033
1034 (defface message-header-xheader-face
1035   '((((class color)
1036       (background dark))
1037      (:foreground "blue"))
1038     (((class color)
1039       (background light))
1040      (:foreground "blue"))
1041     (t
1042      (:bold t)))
1043   "Face used for displaying X-Header headers."
1044   :group 'message-faces)
1045
1046 (defface message-separator-face
1047   '((((class color)
1048       (background dark))
1049      (:foreground "blue3"))
1050     (((class color)
1051       (background light))
1052      (:foreground "brown"))
1053     (t
1054      (:bold t)))
1055   "Face used for displaying the separator."
1056   :group 'message-faces)
1057
1058 (defface message-cited-text-face
1059   '((((class color)
1060       (background dark))
1061      (:foreground "red"))
1062     (((class color)
1063       (background light))
1064      (:foreground "red"))
1065     (t
1066      (:bold t)))
1067   "Face used for displaying cited text names."
1068   :group 'message-faces)
1069
1070 (defface message-mml-face
1071   '((((class color)
1072       (background dark))
1073      (:foreground "ForestGreen"))
1074     (((class color)
1075       (background light))
1076      (:foreground "ForestGreen"))
1077     (t
1078      (:bold t)))
1079   "Face used for displaying MML."
1080   :group 'message-faces)
1081
1082 (defun message-font-lock-make-header-matcher (regexp)
1083   (let ((form
1084          `(lambda (limit)
1085             (let ((start (point)))
1086               (save-restriction
1087                 (widen)
1088                 (goto-char (point-min))
1089                 (if (re-search-forward
1090                      (concat "^" (regexp-quote mail-header-separator) "$")
1091                      nil t)
1092                     (setq limit (min limit (match-beginning 0))))
1093                 (goto-char start))
1094               (and (< start limit)
1095                    (re-search-forward ,regexp limit t))))))
1096     (if (featurep 'bytecomp)
1097         (byte-compile form)
1098       form)))
1099
1100 (defvar message-font-lock-keywords
1101   (let ((content "[ \t]*\\(.+\\(\n[ \t].*\\)*\\)\n?"))
1102     `((,(message-font-lock-make-header-matcher
1103          (concat "^\\([Tt]o:\\)" content))
1104        (1 'message-header-name-face)
1105        (2 'message-header-to-face nil t))
1106       (,(message-font-lock-make-header-matcher
1107          (concat "^\\(^[GBF]?[Cc][Cc]:\\|^[Rr]eply-[Tt]o:\\)" content))
1108        (1 'message-header-name-face)
1109        (2 'message-header-cc-face nil t))
1110       (,(message-font-lock-make-header-matcher
1111          (concat "^\\([Ss]ubject:\\)" content))
1112        (1 'message-header-name-face)
1113        (2 'message-header-subject-face nil t))
1114       (,(message-font-lock-make-header-matcher
1115          (concat "^\\([Nn]ewsgroups:\\|Followup-[Tt]o:\\)" content))
1116        (1 'message-header-name-face)
1117        (2 'message-header-newsgroups-face nil t))
1118       (,(message-font-lock-make-header-matcher
1119          (concat "^\\([A-Z][^: \n\t]+:\\)" content))
1120        (1 'message-header-name-face)
1121        (2 'message-header-other-face nil t))
1122       (,(message-font-lock-make-header-matcher
1123          (concat "^\\(X-[A-Za-z0-9-]+:\\|In-Reply-To:\\)" content))
1124        (1 'message-header-name-face)
1125        (2 'message-header-name-face))
1126       ,@(if (and mail-header-separator
1127                  (not (equal mail-header-separator "")))
1128             `((,(concat "^\\(" (regexp-quote mail-header-separator) "\\)$")
1129                1 'message-separator-face))
1130           nil)
1131       ((lambda (limit)
1132          (re-search-forward (concat "^\\("
1133                                     message-cite-prefix-regexp
1134                                     "\\).*")
1135                             limit t))
1136        (0 'message-cited-text-face))
1137       ("<#/?\\(multipart\\|part\\|external\\|mml\\|secure\\)[^>]*>"
1138        (0 'message-mml-face))))
1139   "Additional expressions to highlight in Message mode.")
1140
1141
1142 ;; XEmacs does it like this.  For Emacs, we have to set the
1143 ;; `font-lock-defaults' buffer-local variable.
1144 (put 'message-mode 'font-lock-defaults '(message-font-lock-keywords t))
1145
1146 (defvar message-face-alist
1147   '((bold . bold-region)
1148     (underline . underline-region)
1149     (default . (lambda (b e)
1150                  (unbold-region b e)
1151                  (ununderline-region b e))))
1152   "Alist of mail and news faces for facemenu.
1153 The cdr of ech entry is a function for applying the face to a region.")
1154
1155 (defcustom message-send-hook nil
1156   "Hook run before sending messages."
1157   :group 'message-various
1158   :options '(ispell-message)
1159   :type 'hook)
1160
1161 (defcustom message-send-mail-hook nil
1162   "Hook run before sending mail messages."
1163   :group 'message-various
1164   :type 'hook)
1165
1166 (defcustom message-send-news-hook nil
1167   "Hook run before sending news messages."
1168   :group 'message-various
1169   :type 'hook)
1170
1171 (defcustom message-sent-hook nil
1172   "Hook run after sending messages."
1173   :group 'message-various
1174   :type 'hook)
1175
1176 (defvar message-send-coding-system 'binary
1177   "Coding system to encode outgoing mail.")
1178
1179 (defvar message-draft-coding-system
1180   mm-auto-save-coding-system
1181   "*Coding system to compose mail.
1182 If you'd like to make it possible to share draft files between XEmacs
1183 and Emacs, you may use `iso-2022-7bit' for this value at your own risk.
1184 Note that the coding-system `iso-2022-7bit' isn't suitable to all data.")
1185
1186 (defcustom message-send-mail-partially-limit 1000000
1187   "The limitation of messages sent as message/partial.
1188 The lower bound of message size in characters, beyond which the message
1189 should be sent in several parts.  If it is nil, the size is unlimited."
1190   :version "21.1"
1191   :group 'message-buffers
1192   :type '(choice (const :tag "unlimited" nil)
1193                  (integer 1000000)))
1194
1195 (defcustom message-alternative-emails nil
1196   "A regexp to match the alternative email addresses.
1197 The first matched address (not primary one) is used in the From field."
1198   :group 'message-headers
1199   :type '(choice (const :tag "Always use primary" nil)
1200                  regexp))
1201
1202 (defcustom message-hierarchical-addresses nil
1203   "A list of hierarchical mail address definitions.
1204
1205 Inside each entry, the first address is the \"top\" address, and
1206 subsequent addresses are subaddresses; this is used to indicate that
1207 mail sent to the first address will automatically be delivered to the
1208 subaddresses.  So if the first address appears in the recipient list
1209 for a message, the subaddresses will be removed (if present) before
1210 the mail is sent.  All addresses in this structure should be
1211 downcased."
1212   :group 'message-headers
1213   :type '(repeat (repeat string)))
1214
1215 (defcustom message-mail-user-agent nil
1216   "Like `mail-user-agent'.
1217 Except if it is nil, use Gnus native MUA; if it is t, use
1218 `mail-user-agent'."
1219   :type '(radio (const :tag "Gnus native"
1220                        :format "%t\n"
1221                        nil)
1222                 (const :tag "`mail-user-agent'"
1223                        :format "%t\n"
1224                        t)
1225                 (function-item :tag "Default Emacs mail"
1226                                :format "%t\n"
1227                                sendmail-user-agent)
1228                 (function-item :tag "Emacs interface to MH"
1229                                :format "%t\n"
1230                                mh-e-user-agent)
1231                 (function :tag "Other"))
1232   :version "21.1"
1233   :group 'message)
1234
1235 (defcustom message-wide-reply-confirm-recipients nil
1236   "Whether to confirm a wide reply to multiple email recipients.
1237 If this variable is nil, don't ask whether to reply to all recipients.
1238 If this variable is non-nil, pose the question \"Reply to all
1239 recipients?\" before a wide reply to multiple recipients.  If the user
1240 answers yes, reply to all recipients as usual.  If the user answers
1241 no, only reply back to the author."
1242   :version "21.3"
1243   :group 'message-headers
1244   :type 'boolean)
1245
1246 ;;; Internal variables.
1247
1248 (defvar message-sending-message "Sending...")
1249 (defvar message-buffer-list nil)
1250 (defvar message-this-is-news nil)
1251 (defvar message-this-is-mail nil)
1252 (defvar message-draft-article nil)
1253 (defvar message-mime-part nil)
1254 (defvar message-posting-charset nil)
1255
1256 ;; Byte-compiler warning
1257 (eval-when-compile
1258   (defvar gnus-active-hashtb)
1259   (defvar gnus-read-active-file))
1260
1261 ;;; Regexp matching the delimiter of messages in UNIX mail format
1262 ;;; (UNIX From lines), minus the initial ^.  It should be a copy
1263 ;;; of rmail.el's rmail-unix-mail-delimiter.
1264 (defvar message-unix-mail-delimiter
1265   (let ((time-zone-regexp
1266          (concat "\\([A-Z]?[A-Z]?[A-Z][A-Z]\\( DST\\)?"
1267                  "\\|[-+]?[0-9][0-9][0-9][0-9]"
1268                  "\\|"
1269                  "\\) *")))
1270     (concat
1271      "From "
1272
1273      ;; Many things can happen to an RFC 822 mailbox before it is put into
1274      ;; a `From' line.  The leading phrase can be stripped, e.g.
1275      ;; `Joe <@w.x:joe@y.z>' -> `<@w.x:joe@y.z>'.  The <> can be stripped, e.g.
1276      ;; `<@x.y:joe@y.z>' -> `@x.y:joe@y.z'.  Everything starting with a CRLF
1277      ;; can be removed, e.g.
1278      ;;         From: joe@y.z (Joe      K
1279      ;;                 User)
1280      ;; can yield `From joe@y.z (Joe    K Fri Mar 22 08:11:15 1996', and
1281      ;;         From: Joe User
1282      ;;                 <joe@y.z>
1283      ;; can yield `From Joe User Fri Mar 22 08:11:15 1996'.
1284      ;; The mailbox can be removed or be replaced by white space, e.g.
1285      ;;         From: "Joe User"{space}{tab}
1286      ;;                 <joe@y.z>
1287      ;; can yield `From {space}{tab} Fri Mar 22 08:11:15 1996',
1288      ;; where {space} and {tab} represent the Ascii space and tab characters.
1289      ;; We want to match the results of any of these manglings.
1290      ;; The following regexp rejects names whose first characters are
1291      ;; obviously bogus, but after that anything goes.
1292      "\\([^\0-\b\n-\r\^?].*\\)? "
1293
1294      ;; The time the message was sent.
1295      "\\([^\0-\r \^?]+\\) +"            ; day of the week
1296      "\\([^\0-\r \^?]+\\) +"            ; month
1297      "\\([0-3]?[0-9]\\) +"              ; day of month
1298      "\\([0-2][0-9]:[0-5][0-9]\\(:[0-6][0-9]\\)?\\) *" ; time of day
1299
1300      ;; Perhaps a time zone, specified by an abbreviation, or by a
1301      ;; numeric offset.
1302      time-zone-regexp
1303
1304      ;; The year.
1305      " \\([0-9][0-9]+\\) *"
1306
1307      ;; On some systems the time zone can appear after the year, too.
1308      time-zone-regexp
1309
1310      ;; Old uucp cruft.
1311      "\\(remote from .*\\)?"
1312
1313      "\n"))
1314   "Regexp matching the delimiter of messages in UNIX mail format.")
1315
1316 (defvar message-unsent-separator
1317   (concat "^ *---+ +Unsent message follows +---+ *$\\|"
1318           "^ *---+ +Returned message +---+ *$\\|"
1319           "^Start of returned message$\\|"
1320           "^ *---+ +Original message +---+ *$\\|"
1321           "^ *--+ +begin message +--+ *$\\|"
1322           "^ *---+ +Original message follows +---+ *$\\|"
1323           "^ *---+ +Undelivered message follows +---+ *$\\|"
1324           "^|? *---+ +Message text follows: +---+ *|?$")
1325   "A regexp that matches the separator before the text of a failed message.")
1326
1327 (defvar message-header-format-alist
1328   `((Newsgroups)
1329     (To . message-fill-address)
1330     (Cc . message-fill-address)
1331     (Subject)
1332     (In-Reply-To)
1333     (Fcc)
1334     (Bcc)
1335     (Date)
1336     (Organization)
1337     (Distribution)
1338     (Lines)
1339     (Expires)
1340     (Message-ID)
1341     (References . message-shorten-references)
1342     (User-Agent))
1343   "Alist used for formatting headers.")
1344
1345 (defvar message-options nil
1346   "Some saved answers when sending message.")
1347
1348 (defvar message-send-mail-real-function nil
1349   "Internal send mail function.")
1350
1351 (defvar message-bogus-system-names "^localhost\\."
1352   "The regexp of bogus system names.")
1353
1354 (eval-and-compile
1355   (autoload 'message-setup-toolbar "messagexmas")
1356   (autoload 'mh-new-draft-name "mh-comp")
1357   (autoload 'mh-send-letter "mh-comp")
1358   (autoload 'gnus-point-at-eol "gnus-util")
1359   (autoload 'gnus-point-at-bol "gnus-util")
1360   (autoload 'gnus-output-to-rmail "gnus-util")
1361   (autoload 'gnus-output-to-mail "gnus-util")
1362   (autoload 'nndraft-request-associate-buffer "nndraft")
1363   (autoload 'nndraft-request-expire-articles "nndraft")
1364   (autoload 'gnus-open-server "gnus-int")
1365   (autoload 'gnus-request-post "gnus-int")
1366   (autoload 'gnus-alive-p "gnus-util")
1367   (autoload 'gnus-server-string "gnus")
1368   (autoload 'gnus-group-name-charset "gnus-group")
1369   (autoload 'gnus-group-name-decode "gnus-group")
1370   (autoload 'gnus-groups-from-server "gnus")
1371   (autoload 'rmail-output "rmailout")
1372   (autoload 'gnus-delay-article "gnus-delay"))
1373
1374 \f
1375
1376 ;;;
1377 ;;; Utility functions.
1378 ;;;
1379
1380 (defmacro message-y-or-n-p (question show &rest text)
1381   "Ask QUESTION, displaying remaining args in a temporary buffer if SHOW."
1382   `(message-talkative-question 'y-or-n-p ,question ,show ,@text))
1383
1384 (defmacro message-delete-line (&optional n)
1385   "Delete the current line (and the next N lines)."
1386   `(delete-region (progn (beginning-of-line) (point))
1387                   (progn (forward-line ,(or n 1)) (point))))
1388
1389 (defun message-unquote-tokens (elems)
1390   "Remove double quotes (\") from strings in list ELEMS."
1391   (mapcar (lambda (item)
1392             (while (string-match "^\\(.*\\)\"\\(.*\\)$" item)
1393               (setq item (concat (match-string 1 item)
1394                                  (match-string 2 item))))
1395             item)
1396           elems))
1397
1398 (defun message-tokenize-header (header &optional separator)
1399   "Split HEADER into a list of header elements.
1400 SEPARATOR is a string of characters to be used as separators.  \",\"
1401 is used by default."
1402   (if (not header)
1403       nil
1404     (let ((regexp (format "[%s]+" (or separator ",")))
1405           (beg 1)
1406           (first t)
1407           quoted elems paren)
1408       (save-excursion
1409         (message-set-work-buffer)
1410         (insert header)
1411         (goto-char (point-min))
1412         (while (not (eobp))
1413           (if first
1414               (setq first nil)
1415             (forward-char 1))
1416           (cond ((and (> (point) beg)
1417                       (or (eobp)
1418                           (and (looking-at regexp)
1419                                (not quoted)
1420                                (not paren))))
1421                  (push (buffer-substring beg (point)) elems)
1422                  (setq beg (match-end 0)))
1423                 ((eq (char-after) ?\")
1424                  (setq quoted (not quoted)))
1425                 ((and (eq (char-after) ?\()
1426                       (not quoted))
1427                  (setq paren t))
1428                 ((and (eq (char-after) ?\))
1429                       (not quoted))
1430                  (setq paren nil))))
1431         (nreverse elems)))))
1432
1433 (defun message-mail-file-mbox-p (file)
1434   "Say whether FILE looks like a Unix mbox file."
1435   (when (and (file-exists-p file)
1436              (file-readable-p file)
1437              (file-regular-p file))
1438     (with-temp-buffer
1439       (nnheader-insert-file-contents file)
1440       (goto-char (point-min))
1441       (looking-at message-unix-mail-delimiter))))
1442
1443 (defun message-fetch-field (header &optional not-all)
1444   "The same as `mail-fetch-field', only remove all newlines."
1445   (let* ((inhibit-point-motion-hooks t)
1446          (case-fold-search t)
1447          (value (mail-fetch-field header nil (not not-all))))
1448     (when value
1449       (while (string-match "\n[\t ]+" value)
1450         (setq value (replace-match " " t t value)))
1451       (set-text-properties 0 (length value) nil value)
1452       value)))
1453
1454 (defun message-narrow-to-field ()
1455   "Narrow the buffer to the header on the current line."
1456   (beginning-of-line)
1457   (narrow-to-region
1458    (point)
1459    (progn
1460      (forward-line 1)
1461      (if (re-search-forward "^[^ \n\t]" nil t)
1462          (progn
1463            (beginning-of-line)
1464            (point))
1465        (point-max))))
1466   (goto-char (point-min)))
1467
1468 (defun message-add-header (&rest headers)
1469   "Add the HEADERS to the message header, skipping those already present."
1470   (while headers
1471     (let (hclean)
1472       (unless (string-match "^\\([^:]+\\):[ \t]*[^ \t]" (car headers))
1473         (error "Invalid header `%s'" (car headers)))
1474       (setq hclean (match-string 1 (car headers)))
1475       (save-restriction
1476         (message-narrow-to-headers)
1477         (unless (re-search-forward (concat "^" (regexp-quote hclean) ":") nil t)
1478           (goto-char (point-max))
1479           (if (string-match "\n$" (car headers))
1480               (insert (car headers))
1481             (insert (car headers) ?\n)))))
1482     (setq headers (cdr headers))))
1483
1484
1485 (defun message-fetch-reply-field (header)
1486   "Fetch field HEADER from the message we're replying to."
1487   (when (and message-reply-buffer
1488              (buffer-name message-reply-buffer))
1489     (save-excursion
1490       (set-buffer message-reply-buffer)
1491       (message-fetch-field header))))
1492
1493 (defun message-set-work-buffer ()
1494   (if (get-buffer " *message work*")
1495       (progn
1496         (set-buffer " *message work*")
1497         (erase-buffer))
1498     (set-buffer (get-buffer-create " *message work*"))
1499     (kill-all-local-variables)
1500     (mm-enable-multibyte)))
1501
1502 (defun message-functionp (form)
1503   "Return non-nil if FORM is funcallable."
1504   (or (and (symbolp form) (fboundp form))
1505       (and (listp form) (eq (car form) 'lambda))
1506       (byte-code-function-p form)))
1507
1508 (defun message-strip-list-identifiers (subject)
1509   "Remove list identifiers in `gnus-list-identifiers' from string SUBJECT."
1510   (require 'gnus-sum)                   ; for gnus-list-identifiers
1511   (let ((regexp (if (stringp gnus-list-identifiers)
1512                     gnus-list-identifiers
1513                   (mapconcat 'identity gnus-list-identifiers " *\\|"))))
1514     (if (string-match (concat "\\(\\(\\(Re: +\\)?\\(" regexp
1515                               " *\\)\\)+\\(Re: +\\)?\\)") subject)
1516         (concat (substring subject 0 (match-beginning 1))
1517                 (or (match-string 3 subject)
1518                     (match-string 5 subject))
1519                 (substring subject
1520                            (match-end 1)))
1521       subject)))
1522
1523 (defun message-strip-subject-re (subject)
1524   "Remove \"Re:\" from subject lines in string SUBJECT."
1525   (if (string-match message-subject-re-regexp subject)
1526       (substring subject (match-end 0))
1527     subject))
1528
1529 ;;; Start of functions adopted from `message-utils.el'.
1530
1531 (defun message-strip-subject-trailing-was (subject)
1532   "Remove trailing \"(Was: <old subject>)\" from subject lines.   
1533 Leading \"Re: \" is not stripped by this function.  Use the function
1534 `message-strip-subject-re' for this."
1535   (let* ((query message-subject-trailing-was-query)
1536          (new) (found))
1537     (setq found
1538           (string-match 
1539            (if (eq query 'ask)
1540                message-subject-trailing-was-ask-regexp
1541              message-subject-trailing-was-regexp)
1542            subject))
1543     (if found
1544         (setq new (substring subject 0 (match-beginning 0))))
1545     (if (or (not found) (eq query nil))
1546         subject
1547       (if (eq query 'ask)
1548           (if (message-y-or-n-p
1549                "Strip `(was: <old subject>)' in subject? " t
1550                (concat 
1551                 "Strip `(was: <old subject>)' in subject "
1552                 "and use the new one instead?\n\n"
1553                 "Current subject is:   \""
1554                 subject "\"\n\n"
1555                 "New subject would be: \""
1556                 new "\"\n\n"
1557                 "See the variable `message-subject-trailing-was-query' "
1558                 "to get rid of this query."
1559                 ))
1560               new subject)
1561         new))))
1562
1563 ;;; Suggested by Jonas Steverud  @  www.dtek.chalmers.se/~d4jonas/
1564
1565 ;;;###autoload
1566 (defun message-change-subject (new-subject)
1567   "Ask for new Subject: header, append (was: <Old Subject>)."
1568   (interactive
1569    (list
1570     (read-from-minibuffer "New subject: ")))
1571   (cond ((and (not (or (null new-subject) ; new subject not empty
1572                        (zerop (string-width new-subject))
1573                        (string-match "^[ \t]*$" new-subject))))
1574          (save-excursion
1575            (let ((old-subject (message-fetch-field "Subject")))
1576              (cond ((not old-subject)
1577                     (error "No current subject."))
1578                    ((not (string-match
1579                           (concat "^[ \t]*"
1580                                   (regexp-quote new-subject)
1581                                   " \t]*$")
1582                           old-subject))  ; yes, it really is a new subject
1583                     ;; delete eventual Re: prefix
1584                     (setq old-subject
1585                           (message-strip-subject-re old-subject))
1586                     (message-goto-subject)
1587                     (message-delete-line)
1588                     (insert (concat "Subject: "
1589                                     new-subject
1590                                     " (was: "
1591                                     old-subject ")\n")))))))))
1592
1593 ;;;###autoload
1594 (defun message-mark-inserted-region (beg end)
1595   "Mark some region in the current article with enclosing tags.
1596 See `message-mark-insert-begin' and `message-mark-insert-end'."
1597   (interactive "r")
1598   (save-excursion
1599     ; add to the end of the region first, otherwise end would be invalid
1600     (goto-char end)
1601     (insert message-mark-insert-end)
1602     (goto-char beg)
1603     (insert message-mark-insert-begin)))
1604
1605 ;;;###autoload
1606 (defun message-mark-insert-file (file)
1607   "Inserts FILE at point, marking it with enclosing tags.
1608 See `message-mark-insert-begin' and `message-mark-insert-end'."
1609   (interactive "fFile to insert: ")
1610     ;; reverse insertion to get correct result.
1611   (let ((p (point)))
1612     (insert message-mark-insert-end)
1613     (goto-char p)
1614     (insert-file-contents file)
1615     (goto-char p)
1616     (insert message-mark-insert-begin)))
1617
1618 ;;;###autoload
1619 (defun message-add-archive-header ()
1620   "Insert \"X-No-Archive: Yes\" in the header and a note in the body.
1621 The note can be customized using `message-archive-note'.  When called with a
1622 prefix argument, ask for a text to insert.  If you don't want the note in the
1623 body, set  `message-archive-note' to nil."
1624   (interactive)
1625   (if current-prefix-arg
1626       (setq message-archive-note
1627             (read-from-minibuffer "Reason for No-Archive: "
1628                                   (cons message-archive-note 0))))
1629     (save-excursion
1630       (if (message-goto-signature)
1631           (re-search-backward message-signature-separator))
1632       (when message-archive-note
1633         (insert message-archive-note)
1634         (newline))
1635       (message-add-header message-archive-header)
1636       (message-sort-headers)))
1637
1638 ;;;###autoload
1639 (defun message-xpost-fup2-header (target-group)
1640   "Mangles FollowUp-To and Newsgroups header to point to TARGET-GROUP.
1641 With prefix-argument just set Follow-Up, don't cross-post."
1642   (interactive
1643    (list ; Completion based on Gnus
1644     (completing-read "Followup To: "
1645                      (if (boundp 'gnus-newsrc-alist)
1646                          gnus-newsrc-alist)
1647                      nil nil '("poster" . 0)
1648                      (if (boundp 'gnus-group-history)
1649                          'gnus-group-history))))
1650   (message-remove-header "Follow[Uu]p-[Tt]o" t)
1651   (message-goto-newsgroups)
1652   (beginning-of-line)
1653   ;; if we already did a crosspost before, kill old target
1654   (if (and message-xpost-old-target
1655            (re-search-forward
1656             (regexp-quote (concat "," message-xpost-old-target))
1657             nil t))
1658       (replace-match ""))
1659   ;; unless (followup is to poster or user explicitly asked not
1660   ;; to cross-post, or target-group is already in Newsgroups)
1661   ;; add target-group to Newsgroups line.
1662   (cond ((and (or
1663                ;; def: xpost, req:no
1664                (and message-xpost-default (not current-prefix-arg))  
1665                ;; def: no-xpost, req:yes
1666                (and (not message-xpost-default) current-prefix-arg))
1667               (not (string-match "poster" target-group))
1668               (not (string-match (regexp-quote target-group)
1669                                  (message-fetch-field "Newsgroups"))))
1670          (end-of-line)
1671          (insert-string (concat "," target-group))))
1672   (end-of-line) ; ensure Followup: comes after Newsgroups:
1673   ;; unless new followup would be identical to Newsgroups line
1674   ;; make a new Followup-To line
1675   (if (not (string-match (concat "^[ \t]*"
1676                                  target-group
1677                                  "[ \t]*$")
1678                          (message-fetch-field "Newsgroups")))
1679       (insert (concat "\nFollowup-To: " target-group)))
1680   (setq message-xpost-old-target target-group))
1681
1682 ;;;###autoload
1683 (defun message-xpost-insert-note (target-group xpost in-old old-groups)
1684   "Insert a in message body note about a set Followup or Crosspost.
1685 If there have been previous notes, delete them.  TARGET-GROUP specifies the
1686 group to Followup-To.  When XPOST is t, insert note about
1687 crossposting.  IN-OLD specifies whether TARGET-GROUP is a member of
1688 OLD-GROUPS.  OLD-GROUPS lists the old-groups the posting would have
1689 been made to before the user asked for a Crosspost."
1690   ;; start scanning body for previous uses
1691   (message-goto-signature)
1692   (let ((head (re-search-backward
1693                (concat "^" mail-header-separator)
1694                nil t))) ; just search in body
1695     (message-goto-signature)
1696     (while (re-search-backward
1697             (concat "^" (regexp-quote message-xpost-note) ".*")
1698             head t)
1699       (message-delete-line))
1700     (message-goto-signature)
1701     (while (re-search-backward
1702             (concat "^" (regexp-quote message-fup2-note) ".*")
1703             head t)
1704       (message-delete-line))
1705     ;; insert new note
1706     (if (message-goto-signature)
1707         (re-search-backward message-signature-separator))
1708     (if (or in-old
1709             (not xpost)
1710             (string-match "^[ \t]*poster[ \t]*$" target-group))
1711         (insert (concat message-fup2-note target-group "\n"))
1712       (insert (concat message-xpost-note target-group "\n")))))
1713
1714 ;;;###autoload
1715 (defun message-xpost-fup2 (target-group)
1716   "Crossposts message and sets Followup-To to TARGET-GROUP.
1717 With prefix-argument just set Follow-Up, don't cross-post."
1718   (interactive
1719    (list ; Completion based on Gnus
1720     (completing-read "Followup To: "
1721                      (if (boundp 'gnus-newsrc-alist)
1722                          gnus-newsrc-alist)
1723                      nil nil '("poster" . 0)
1724                      (if (boundp 'gnus-group-history)
1725                          'gnus-group-history))))
1726   (cond ((not (or (null target-group) ; new subject not empty
1727                   (zerop (string-width target-group))
1728                   (string-match "^[ \t]*$" target-group)))
1729          (save-excursion
1730            (let* ((old-groups (message-fetch-field "Newsgroups"))
1731                   (in-old (string-match
1732                            (regexp-quote target-group) 
1733                            (or old-groups ""))))
1734              ;; check whether target exactly matches old Newsgroups
1735              (cond ((not old-groups)
1736                     (error "No current newsgroup."))
1737                    ((or (not in-old)
1738                         (not (string-match
1739                               (concat "^[ \t]*"
1740                                       (regexp-quote target-group)
1741                                       "[ \t]*$")
1742                               old-groups)))
1743                     ;; yes, Newsgroups line must change
1744                     (message-xpost-fup2-header target-group)
1745                     ;; insert note whether we do xpost or fup2
1746                     (funcall message-xpost-note-function
1747                              target-group
1748                              (if (or (and message-xpost-default
1749                                           (not current-prefix-arg))
1750                                      (and (not message-xpost-default)
1751                                           current-prefix-arg)) t)
1752                              in-old old-groups))))))))
1753
1754 ;;; Reduce To: to Cc: or Bcc: header
1755
1756 ;;;###autoload
1757 (defun message-reduce-to-to-cc ()
1758  "Replace contents of To: header with contents of Cc: or Bcc: header."
1759  (interactive)
1760  (let ((cc-content (message-fetch-field "cc"))
1761        (bcc nil))
1762    (if (and (not cc-content)
1763             (setq cc-content (message-fetch-field "bcc")))
1764        (setq bcc t))
1765    (cond (cc-content
1766           (save-excursion
1767             (message-goto-to)
1768             (message-delete-line)
1769             (insert (concat "To: " cc-content "\n"))
1770             (message-remove-header (if bcc
1771                                        "bcc"
1772                                      "cc")))))))
1773
1774 ;;; End of functions adopted from `message-utils.el'.
1775
1776 (defun message-remove-header (header &optional is-regexp first reverse)
1777   "Remove HEADER in the narrowed buffer.
1778 If IS-REGEXP, HEADER is a regular expression.
1779 If FIRST, only remove the first instance of the header.
1780 Return the number of headers removed."
1781   (goto-char (point-min))
1782   (let ((regexp (if is-regexp header (concat "^" (regexp-quote header) ":")))
1783         (number 0)
1784         (case-fold-search t)
1785         last)
1786     (while (and (not (eobp))
1787                 (not last))
1788       (if (if reverse
1789               (not (looking-at regexp))
1790             (looking-at regexp))
1791           (progn
1792             (incf number)
1793             (when first
1794               (setq last t))
1795             (delete-region
1796              (point)
1797              ;; There might be a continuation header, so we have to search
1798              ;; until we find a new non-continuation line.
1799              (progn
1800                (forward-line 1)
1801                (if (re-search-forward "^[^ \t]" nil t)
1802                    (goto-char (match-beginning 0))
1803                  (point-max)))))
1804         (forward-line 1)
1805         (if (re-search-forward "^[^ \t]" nil t)
1806             (goto-char (match-beginning 0))
1807           (goto-char (point-max)))))
1808     number))
1809
1810 (defun message-remove-first-header (header)
1811   "Remove the first instance of HEADER if there is more than one."
1812   (let ((count 0)
1813         (regexp (concat "^" (regexp-quote header) ":")))
1814     (save-excursion
1815       (goto-char (point-min))
1816       (while (re-search-forward regexp nil t)
1817         (incf count)))
1818     (while (> count 1)
1819       (message-remove-header header nil t)
1820       (decf count))))
1821
1822 (defun message-narrow-to-headers ()
1823   "Narrow the buffer to the head of the message."
1824   (widen)
1825   (narrow-to-region
1826    (goto-char (point-min))
1827    (if (re-search-forward
1828         (concat "^" (regexp-quote mail-header-separator) "\n") nil t)
1829        (match-beginning 0)
1830      (point-max)))
1831   (goto-char (point-min)))
1832
1833 (defun message-narrow-to-head-1 ()
1834   "Like `message-narrow-to-head'.  Don't widen."
1835   (narrow-to-region
1836    (goto-char (point-min))
1837    (if (search-forward "\n\n" nil 1)
1838        (1- (point))
1839      (point-max)))
1840   (goto-char (point-min)))
1841
1842 (defun message-narrow-to-head ()
1843   "Narrow the buffer to the head of the message.
1844 Point is left at the beginning of the narrowed-to region."
1845   (widen)
1846   (message-narrow-to-head-1))
1847
1848 (defun message-narrow-to-headers-or-head ()
1849   "Narrow the buffer to the head of the message."
1850   (widen)
1851   (narrow-to-region
1852    (goto-char (point-min))
1853    (cond
1854     ((re-search-forward
1855       (concat "^" (regexp-quote mail-header-separator) "\n") nil t)
1856      (match-beginning 0))
1857     ((search-forward "\n\n" nil t)
1858      (1- (point)))
1859     (t
1860      (point-max))))
1861   (goto-char (point-min)))
1862
1863 (defun message-news-p ()
1864   "Say whether the current buffer contains a news message."
1865   (and (not message-this-is-mail)
1866        (or message-this-is-news
1867            (save-excursion
1868              (save-restriction
1869                (message-narrow-to-headers)
1870                (and (message-fetch-field "newsgroups")
1871                     (not (message-fetch-field "posted-to"))))))))
1872
1873 (defun message-mail-p ()
1874   "Say whether the current buffer contains a mail message."
1875   (and (not message-this-is-news)
1876        (or message-this-is-mail
1877            (save-excursion
1878              (save-restriction
1879                (message-narrow-to-headers)
1880                (or (message-fetch-field "to")
1881                    (message-fetch-field "cc")
1882                    (message-fetch-field "bcc")))))))
1883
1884 (defun message-next-header ()
1885   "Go to the beginning of the next header."
1886   (beginning-of-line)
1887   (or (eobp) (forward-char 1))
1888   (not (if (re-search-forward "^[^ \t]" nil t)
1889            (beginning-of-line)
1890          (goto-char (point-max)))))
1891
1892 (defun message-sort-headers-1 ()
1893   "Sort the buffer as headers using `message-rank' text props."
1894   (goto-char (point-min))
1895   (require 'sort)
1896   (sort-subr
1897    nil 'message-next-header
1898    (lambda ()
1899      (message-next-header)
1900      (unless (bobp)
1901        (forward-char -1)))
1902    (lambda ()
1903      (or (get-text-property (point) 'message-rank)
1904          10000))))
1905
1906 (defun message-sort-headers ()
1907   "Sort the headers of the current message according to `message-header-format-alist'."
1908   (interactive)
1909   (save-excursion
1910     (save-restriction
1911       (let ((max (1+ (length message-header-format-alist)))
1912             rank)
1913         (message-narrow-to-headers)
1914         (while (re-search-forward "^[^ \n]+:" nil t)
1915           (put-text-property
1916            (match-beginning 0) (1+ (match-beginning 0))
1917            'message-rank
1918            (if (setq rank (length (memq (assq (intern (buffer-substring
1919                                                        (match-beginning 0)
1920                                                        (1- (match-end 0))))
1921                                               message-header-format-alist)
1922                                         message-header-format-alist)))
1923                (- max rank)
1924              (1+ max)))))
1925       (message-sort-headers-1))))
1926
1927
1928 \f
1929
1930 ;;;
1931 ;;; Message mode
1932 ;;;
1933
1934 ;;; Set up keymap.
1935
1936 (defvar message-mode-map nil)
1937
1938 (unless message-mode-map
1939   (setq message-mode-map (make-keymap))
1940   (set-keymap-parent message-mode-map text-mode-map)
1941   (define-key message-mode-map "\C-c?" 'describe-mode)
1942
1943   (define-key message-mode-map "\C-c\C-f\C-t" 'message-goto-to)
1944   (define-key message-mode-map "\C-c\C-f\C-o" 'message-goto-from)
1945   (define-key message-mode-map "\C-c\C-f\C-b" 'message-goto-bcc)
1946   (define-key message-mode-map "\C-c\C-f\C-w" 'message-goto-fcc)
1947   (define-key message-mode-map "\C-c\C-f\C-c" 'message-goto-cc)
1948   (define-key message-mode-map "\C-c\C-f\C-s" 'message-goto-subject)
1949   (define-key message-mode-map "\C-c\C-f\C-r" 'message-goto-reply-to)
1950   (define-key message-mode-map "\C-c\C-f\C-n" 'message-goto-newsgroups)
1951   (define-key message-mode-map "\C-c\C-f\C-d" 'message-goto-distribution)
1952   (define-key message-mode-map "\C-c\C-f\C-f" 'message-goto-followup-to)
1953   (define-key message-mode-map "\C-c\C-f\C-m" 'message-goto-mail-followup-to)
1954   (define-key message-mode-map "\C-c\C-f\C-k" 'message-goto-keywords)
1955   (define-key message-mode-map "\C-c\C-f\C-u" 'message-goto-summary)
1956   (define-key message-mode-map "\C-c\C-f\C-i" 'message-insert-or-toggle-importance)
1957   (define-key message-mode-map "\C-c\C-f\C-a" 'message-gen-unsubscribed-mft)
1958
1959   ;; modify headers (and insert notes in body)
1960   (define-key message-mode-map "\C-c\C-fs"    'message-change-subject)
1961   ;;
1962   (define-key message-mode-map "\C-c\C-fx"    'message-xpost-fup2)
1963   ;; prefix+message-xpost-fup2 = same w/o xpost
1964   (define-key message-mode-map "\C-c\C-ft"    'message-reduce-to-to-cc)
1965   (define-key message-mode-map "\C-c\C-fa"    'message-add-archive-header)
1966   ;; mark inserted text
1967   (define-key message-mode-map "\C-c\M-m" 'message-mark-inserted-region)
1968   (define-key message-mode-map "\C-c\M-f" 'message-mark-insert-file)
1969   
1970   (define-key message-mode-map "\C-c\C-b" 'message-goto-body)
1971   (define-key message-mode-map "\C-c\C-i" 'message-goto-signature)
1972
1973   (define-key message-mode-map "\C-c\C-t" 'message-insert-to)
1974   (define-key message-mode-map "\C-c\C-n" 'message-insert-newsgroups)
1975   (define-key message-mode-map "\C-c\C-l" 'message-to-list-only)
1976
1977   (define-key message-mode-map "\C-c\C-u" 'message-insert-or-toggle-importance)
1978   (define-key message-mode-map "\C-c\M-n" 'message-insert-disposition-notification-to)
1979
1980   (define-key message-mode-map "\C-c\C-y" 'message-yank-original)
1981   (define-key message-mode-map "\C-c\M-\C-y" 'message-yank-buffer)
1982   (define-key message-mode-map "\C-c\C-q" 'message-fill-yanked-message)
1983   (define-key message-mode-map "\C-c\C-w" 'message-insert-signature)
1984   (define-key message-mode-map "\C-c\M-h" 'message-insert-headers)
1985   (define-key message-mode-map "\C-c\C-r" 'message-caesar-buffer-body)
1986   (define-key message-mode-map "\C-c\C-o" 'message-sort-headers)
1987   (define-key message-mode-map "\C-c\M-r" 'message-rename-buffer)
1988
1989   (define-key message-mode-map "\C-c\C-c" 'message-send-and-exit)
1990   (define-key message-mode-map "\C-c\C-s" 'message-send)
1991   (define-key message-mode-map "\C-c\C-k" 'message-kill-buffer)
1992   (define-key message-mode-map "\C-c\C-d" 'message-dont-send)
1993   (define-key message-mode-map "\C-c\n" 'gnus-delay-article)
1994
1995   (define-key message-mode-map "\C-c\C-e" 'message-elide-region)
1996   (define-key message-mode-map "\C-c\C-v" 'message-delete-not-region)
1997   (define-key message-mode-map "\C-c\C-z" 'message-kill-to-signature)
1998   (define-key message-mode-map "\M-\r" 'message-newline-and-reformat)
1999   ;;(define-key message-mode-map "\M-q" 'message-fill-paragraph)
2000
2001   (define-key message-mode-map "\C-c\C-a" 'mml-attach-file)
2002
2003   (define-key message-mode-map "\C-a" 'message-beginning-of-line)
2004   (define-key message-mode-map "\t" 'message-tab)
2005   (define-key message-mode-map "\M-;" 'comment-region))
2006
2007 (easy-menu-define
2008   message-mode-menu message-mode-map "Message Menu."
2009   `("Message"
2010     ["Sort Headers" message-sort-headers t]
2011     ["Yank Original" message-yank-original t]
2012     ["Fill Yanked Message" message-fill-yanked-message t]
2013     ["Insert Signature" message-insert-signature t]
2014     ["Caesar (rot13) Message" message-caesar-buffer-body t]
2015     ["Caesar (rot13) Region" message-caesar-region (mark t)]
2016     ["Elide Region" message-elide-region (mark t)]
2017     ["Delete Outside Region" message-delete-not-region (mark t)]
2018     ["Kill To Signature" message-kill-to-signature t]
2019     ["Newline and Reformat" message-newline-and-reformat t]
2020     ["Rename buffer" message-rename-buffer t]
2021     ["Flag As Important" message-insert-importance-high
2022      ,@(if (featurep 'xemacs) '(t)
2023          '(:help "Mark this message as important"))]
2024     ["Flag As Unimportant" message-insert-importance-low
2025      ,@(if (featurep 'xemacs) '(t)
2026          '(:help "Mark this message as unimportant"))]
2027     ["Request Receipt"
2028      message-insert-disposition-notification-to
2029      ,@(if (featurep 'xemacs) '(t)
2030          '(:help "Request a Disposition Notification of this article"))]
2031     ["Spellcheck" ispell-message
2032      ,@(if (featurep 'xemacs) '(t)
2033          '(:help "Spellcheck this message"))]
2034     "----"
2035     ["Insert Region Marked" message-mark-inserted-region 
2036      ,@(if (featurep 'xemacs) '(t)
2037          '(:help "Mark region with enclosing tags"))]
2038     ["Insert File Marked" message-mark-insert-file 
2039      ,@(if (featurep 'xemacs) '(t)
2040          '(:help "Insert file at point marked with enclosing tags"))]
2041     "----"
2042     ["Send Message" message-send-and-exit
2043      ,@(if (featurep 'xemacs) '(t)
2044          '(:help "Send this message"))]
2045     ["Postpone Message" message-dont-send
2046      ,@(if (featurep 'xemacs) '(t)
2047          '(:help "File this draft message and exit"))]
2048     ["Send at Specific Time" gnus-delay-article
2049      ,@(if (featurep 'xemacs) '(t)
2050          '(:help "Ask, then arrange to send message at that time"))]
2051     ["Kill Message" message-kill-buffer
2052      ,@(if (featurep 'xemacs) '(t)
2053          '(:help "Delete this message without sending"))]))
2054
2055 (easy-menu-define
2056   message-mode-field-menu message-mode-map ""
2057   '("Field"
2058     ["Fetch To" message-insert-to t]
2059     ["Fetch Newsgroups" message-insert-newsgroups t]
2060     "----"
2061     ["To" message-goto-to t]
2062     ["From" message-goto-from t]
2063     ["Subject" message-goto-subject t]
2064     ["Change subject" message-change-subject t]
2065     ["Cc" message-goto-cc t]
2066     ["Bcc" message-goto-bcc t]
2067     ["Fcc" message-goto-fcc t]
2068     ["Reply-To" message-goto-reply-to t]
2069     "----"
2070     ;; (typical) news stuff
2071     ["Summary" message-goto-summary t]
2072     ["Keywords" message-goto-keywords t]
2073     ["Newsgroups" message-goto-newsgroups t]
2074     ["Followup-To" message-goto-followup-to t]
2075     ;; ["Followup-To (with note in body)" message-xpost-fup2 t]
2076     ["Crosspost / Followup-To" message-xpost-fup2 t]
2077     ["Distribution" message-goto-distribution t]
2078     ["X-No-Archive:" message-add-archive-header t ]
2079     "----"
2080     ;; (typical) mailing-lists stuff
2081     ["Send to list only" message-to-list-only t]
2082     ["Mail-Followup-To" message-goto-mail-followup-to t]
2083     ["Reduce To: to Cc:" message-reduce-to-to-cc t]
2084     "----"
2085     ["Body" message-goto-body t]
2086     ["Signature" message-goto-signature t]))
2087
2088 (defvar message-tool-bar-map nil)
2089
2090 (eval-when-compile
2091   (defvar facemenu-add-face-function)
2092   (defvar facemenu-remove-face-function))
2093
2094 ;;; Forbidden properties
2095 ;;
2096 ;; We use `after-change-functions' to keep special text properties
2097 ;; that interfer with the normal function of message mode out of the
2098 ;; buffer.
2099
2100 (defcustom message-strip-special-text-properties t
2101   "Strip special properties from the message buffer.
2102
2103 Emacs has a number of special text properties which can break message
2104 composing in various ways.  If this option is set, message will strip
2105 these properties from the message composition buffer.  However, some
2106 packages requires these properties to be present in order to work.
2107 If you use one of these packages, turn this option off, and hope the
2108 message composition doesn't break too bad."
2109   :group 'message-various
2110   :type 'boolean)
2111
2112 (defconst message-forbidden-properties
2113   ;; No reason this should be clutter up customize.  We make it a
2114   ;; property list (rather than a list of property symbols), to be
2115   ;; directly useful for `remove-text-properties'.
2116   '(field nil read-only nil intangible nil invisible nil
2117           mouse-face nil modification-hooks nil insert-in-front-hooks nil
2118           insert-behind-hooks nil point-entered nil point-left nil)
2119   ;; Other special properties:
2120   ;; category, face, display: probably doesn't do any harm.
2121   ;; fontified: is used by font-lock.
2122   ;; syntax-table, local-map: I dunno.
2123   ;; We need to add XEmacs names to the list.
2124   "Property list of with properties.forbidden in message buffers.
2125 The values of the properties are ignored, only the property names are used.")
2126
2127 (defun message-tamago-not-in-use-p (pos)
2128   "Return t when tamago version 4 is not in use at the cursor position.
2129 Tamago version 4 is a popular input method for writing Japanese text.
2130 It uses the properties `intangible', `invisible', `modification-hooks'
2131 and `read-only' when translating ascii or kana text to kanji text.
2132 These properties are essential to work, so we should never strip them."
2133   (not (and (boundp 'egg-modefull-mode)
2134             (symbol-value 'egg-modefull-mode)
2135             (or (memq (get-text-property pos 'intangible)
2136                       '(its-part-1 its-part-2))
2137                 (get-text-property pos 'egg-end)
2138                 (get-text-property pos 'egg-lang)
2139                 (get-text-property pos 'egg-start)))))
2140
2141 (defun message-strip-forbidden-properties (begin end &optional old-length)
2142   "Strip forbidden properties between BEGIN and END, ignoring the third arg.
2143 This function is intended to be called from `after-change-functions'.
2144 See also `message-forbidden-properties'."
2145   (when (and message-strip-special-text-properties
2146              (message-tamago-not-in-use-p begin))
2147     (remove-text-properties begin end message-forbidden-properties)))
2148
2149 ;;;###autoload
2150 (define-derived-mode message-mode text-mode "Message"
2151   "Major mode for editing mail and news to be sent.
2152 Like Text Mode but with these additional commands:\\<message-mode-map>
2153 C-c C-s  `message-send' (send the message)  C-c C-c  `message-send-and-exit'
2154 C-c C-d  Postpone sending the message       C-c C-k  Kill the message
2155 C-c C-f  move to a header field (and create it if there isn't):
2156          C-c C-f C-t  move to To        C-c C-f C-s  move to Subject
2157          C-c C-f C-c  move to Cc        C-c C-f C-b  move to Bcc
2158          C-c C-f C-w  move to Fcc       C-c C-f C-r  move to Reply-To
2159          C-c C-f C-u  move to Summary   C-c C-f C-n  move to Newsgroups
2160          C-c C-f C-k  move to Keywords  C-c C-f C-d  move to Distribution
2161          C-c C-f C-o  move to From (\"Originator\")
2162          C-c C-f C-f  move to Followup-To
2163          C-c C-f C-m  move to Mail-Followup-To
2164          C-c C-f C-i  cycle through Importance values
2165          C-c C-f s    change subject and append \"(was: <Old Subject>)\"
2166          C-c C-f x    crossposting with FollowUp-To header and note in body
2167          C-c C-f t    replace To: header with contents of Cc: or Bcc:
2168          C-c C-f a    Insert X-No-Archive: header and a note in the body
2169 C-c C-t  `message-insert-to' (add a To header to a news followup)
2170 C-c C-l  `message-to-list-only' (removes all but list address in to/cc)
2171 C-c C-n  `message-insert-newsgroups' (add a Newsgroup header to a news reply)
2172 C-c C-b  `message-goto-body' (move to beginning of message text).
2173 C-c C-i  `message-goto-signature' (move to the beginning of the signature).
2174 C-c C-w  `message-insert-signature' (insert `message-signature-file' file).
2175 C-c C-y  `message-yank-original' (insert current message, if any).
2176 C-c C-q  `message-fill-yanked-message' (fill what was yanked).
2177 C-c C-e  `message-elide-region' (elide the text between point and mark).
2178 C-c C-v  `message-delete-not-region' (remove the text outside the region).
2179 C-c C-z  `message-kill-to-signature' (kill the text up to the signature).
2180 C-c C-r  `message-caesar-buffer-body' (rot13 the message body).
2181 C-c C-a  `mml-attach-file' (attach a file as MIME).
2182 C-c C-u  `message-insert-or-toggle-importance'  (insert or cycle importance).
2183 C-c M-n  `message-insert-disposition-notification-to'  (request receipt).
2184 C-c M-m  `message-mark-inserted-region' (mark region with enclosing tags).
2185 C-c M-f  `message-mark-insert-file' (insert file marked with enclosing tags).
2186 M-RET    `message-newline-and-reformat' (break the line and reformat)."
2187   (setq local-abbrev-table text-mode-abbrev-table)
2188   (set (make-local-variable 'message-reply-buffer) nil)
2189   (make-local-variable 'message-send-actions)
2190   (make-local-variable 'message-exit-actions)
2191   (make-local-variable 'message-kill-actions)
2192   (make-local-variable 'message-postpone-actions)
2193   (make-local-variable 'message-draft-article)
2194   (setq buffer-offer-save t)
2195   (set (make-local-variable 'facemenu-add-face-function)
2196        (lambda (face end)
2197          (let ((face-fun (cdr (assq face message-face-alist))))
2198            (if face-fun
2199                (funcall face-fun (point) end)
2200              (error "Face %s not configured for %s mode" face mode-name)))
2201          ""))
2202   (set (make-local-variable 'facemenu-remove-face-function) t)
2203   (set (make-local-variable 'message-reply-headers) nil)
2204   (make-local-variable 'message-newsreader)
2205   (make-local-variable 'message-mailer)
2206   (make-local-variable 'message-post-method)
2207   (set (make-local-variable 'message-sent-message-via) nil)
2208   (set (make-local-variable 'message-checksum) nil)
2209   (set (make-local-variable 'message-mime-part) 0)
2210   (message-setup-fill-variables)
2211   ;; Allow using comment commands to add/remove quoting.
2212   (set (make-local-variable 'comment-start) message-yank-prefix)
2213   (if (featurep 'xemacs)
2214       (message-setup-toolbar)
2215     (set (make-local-variable 'font-lock-defaults)
2216          '(message-font-lock-keywords t))
2217     (if (boundp 'tool-bar-map)
2218         (set (make-local-variable 'tool-bar-map) (message-tool-bar-map))))
2219   (easy-menu-add message-mode-menu message-mode-map)
2220   (easy-menu-add message-mode-field-menu message-mode-map)
2221   ;; make-local-hook is harmless though obsolete in Emacs 21.
2222   ;; Emacs 20 and XEmacs need make-local-hook.
2223   (make-local-hook 'after-change-functions)
2224   ;; Mmmm... Forbidden properties...
2225   (add-hook 'after-change-functions 'message-strip-forbidden-properties
2226             nil 'local)
2227   ;; Allow mail alias things.
2228   (when (eq message-mail-alias-type 'abbrev)
2229     (if (fboundp 'mail-abbrevs-setup)
2230         (mail-abbrevs-setup)
2231       (mail-aliases-setup)))
2232   (unless buffer-file-name
2233     (message-set-auto-save-file-name))
2234   (unless (buffer-base-buffer)
2235     ;; Don't enable multibyte on an indirect buffer.  Maybe enabling
2236     ;; multibyte is not necessary at all. -- zsh
2237     (mm-enable-multibyte))
2238   (set (make-local-variable 'indent-tabs-mode) nil) ;No tabs for indentation.
2239   (mml-mode))
2240
2241 (defun message-setup-fill-variables ()
2242   "Setup message fill variables."
2243   (set (make-local-variable 'fill-paragraph-function)
2244        'message-fill-paragraph)
2245   (make-local-variable 'paragraph-separate)
2246   (make-local-variable 'paragraph-start)
2247   (make-local-variable 'adaptive-fill-regexp)
2248   (unless (boundp 'adaptive-fill-first-line-regexp)
2249     (setq adaptive-fill-first-line-regexp nil))
2250   (make-local-variable 'adaptive-fill-first-line-regexp)
2251   (let ((quote-prefix-regexp
2252          ;; User should change message-cite-prefix-regexp if
2253          ;; message-yank-prefix is set to an abnormal value.
2254          (concat "\\(" message-cite-prefix-regexp "\\)[ \t]*")))
2255     (setq paragraph-start
2256           (concat
2257            (regexp-quote mail-header-separator) "$\\|"
2258            "[ \t]*$\\|"                 ; blank lines
2259            "-- $\\|"                    ; signature delimiter
2260            "---+$\\|"              ; delimiters for forwarded messages
2261            page-delimiter "$\\|"        ; spoiler warnings
2262            ".*wrote:$\\|"               ; attribution lines
2263            quote-prefix-regexp "$"))    ; empty lines in quoted text
2264     (setq paragraph-separate paragraph-start)
2265     (setq adaptive-fill-regexp
2266           (concat quote-prefix-regexp "\\|" adaptive-fill-regexp))
2267     (setq adaptive-fill-first-line-regexp
2268           (concat quote-prefix-regexp "\\|"
2269                   adaptive-fill-first-line-regexp)))
2270   (make-local-variable 'auto-fill-inhibit-regexp)
2271   ;;(setq auto-fill-inhibit-regexp "^[A-Z][^: \n\t]+:")
2272   (setq auto-fill-inhibit-regexp nil)
2273   (make-local-variable 'normal-auto-fill-function)
2274   (setq normal-auto-fill-function 'message-do-auto-fill)
2275   ;; KLUDGE: auto fill might already be turned on in `text-mode-hook'.
2276   ;; In that case, ensure that it uses the right function.  The real
2277   ;; solution would be not to use `define-derived-mode', and run
2278   ;; `text-mode-hook' ourself at the end of the mode.
2279   ;; -- Per Abrahamsen <abraham@dina.kvl.dk> Date: 2001-10-19.
2280   (when auto-fill-function
2281     (setq auto-fill-function normal-auto-fill-function)))
2282
2283 \f
2284
2285 ;;;
2286 ;;; Message mode commands
2287 ;;;
2288
2289 ;;; Movement commands
2290
2291 (defun message-goto-to ()
2292   "Move point to the To header."
2293   (interactive)
2294   (message-position-on-field "To"))
2295
2296 (defun message-goto-from ()
2297   "Move point to the From header."
2298   (interactive)
2299   (message-position-on-field "From"))
2300
2301 (defun message-goto-subject ()
2302   "Move point to the Subject header."
2303   (interactive)
2304   (message-position-on-field "Subject"))
2305
2306 (defun message-goto-cc ()
2307   "Move point to the Cc header."
2308   (interactive)
2309   (message-position-on-field "Cc" "To"))
2310
2311 (defun message-goto-bcc ()
2312   "Move point to the Bcc  header."
2313   (interactive)
2314   (message-position-on-field "Bcc" "Cc" "To"))
2315
2316 (defun message-goto-fcc ()
2317   "Move point to the Fcc header."
2318   (interactive)
2319   (message-position-on-field "Fcc" "To" "Newsgroups"))
2320
2321 (defun message-goto-reply-to ()
2322   "Move point to the Reply-To header."
2323   (interactive)
2324   (message-position-on-field "Reply-To" "Subject"))
2325
2326 (defun message-goto-newsgroups ()
2327   "Move point to the Newsgroups header."
2328   (interactive)
2329   (message-position-on-field "Newsgroups"))
2330
2331 (defun message-goto-distribution ()
2332   "Move point to the Distribution header."
2333   (interactive)
2334   (message-position-on-field "Distribution"))
2335
2336 (defun message-goto-followup-to ()
2337   "Move point to the Followup-To header."
2338   (interactive)
2339   (message-position-on-field "Followup-To" "Newsgroups"))
2340
2341 (defun message-goto-mail-followup-to ()
2342   "Move point to the Mail-Followup-To header."
2343   (interactive)
2344   (message-position-on-field "Mail-Followup-To" "From"))
2345
2346 (defun message-goto-keywords ()
2347   "Move point to the Keywords header."
2348   (interactive)
2349   (message-position-on-field "Keywords" "Subject"))
2350
2351 (defun message-goto-summary ()
2352   "Move point to the Summary header."
2353   (interactive)
2354   (message-position-on-field "Summary" "Subject"))
2355
2356 (defun message-goto-body (&optional interactivep)
2357   "Move point to the beginning of the message body."
2358   (interactive (list t))
2359   (when (and interactivep
2360              (looking-at "[ \t]*\n"))
2361     (expand-abbrev))
2362   (goto-char (point-min))
2363   (or (search-forward (concat "\n" mail-header-separator "\n") nil t)
2364       (search-forward-regexp "[^:]+:\\([^\n]\\|\n[ \t]\\)+\n\n" nil t)))
2365
2366 (defun message-goto-eoh ()
2367   "Move point to the end of the headers."
2368   (interactive)
2369   (message-goto-body)
2370   (forward-line -1))
2371
2372 (defun message-goto-signature ()
2373   "Move point to the beginning of the message signature.
2374 If there is no signature in the article, go to the end and
2375 return nil."
2376   (interactive)
2377   (goto-char (point-min))
2378   (if (re-search-forward message-signature-separator nil t)
2379       (forward-line 1)
2380     (goto-char (point-max))
2381     nil))
2382
2383 (defun message-gen-unsubscribed-mft (&optional include-cc)
2384   "Insert a reasonable MFT header in a post to an unsubscribed list.
2385 When making original posts to a mailing list you are not subscribed to,
2386 you have to type in a MFT header by hand.  The contents, usually, are
2387 the addresses of the list and your own address.  This function inserts
2388 such a header automatically.  It fetches the contents of the To: header
2389 in the current mail buffer, and appends the current user-mail-address.
2390
2391 If the optional argument `include-cc' is non-nil, the addresses in the
2392 Cc: header are also put into the MFT."
2393
2394   (interactive "P")
2395   (message-remove-header "Mail-Followup-To")
2396   (let* ((cc (and include-cc (message-fetch-field "Cc")))
2397          (tos (if cc
2398                   (concat (message-fetch-field "To") "," cc)
2399                 (message-fetch-field "To"))))
2400     (message-goto-mail-followup-to)
2401     (insert (concat tos ", " user-mail-address))))
2402
2403 \f
2404
2405 (defun message-insert-to (&optional force)
2406   "Insert a To header that points to the author of the article being replied to.
2407 If the original author requested not to be sent mail, the function signals
2408 an error.
2409 With the prefix argument FORCE, insert the header anyway."
2410   (interactive "P")
2411   (let ((co (message-fetch-reply-field "mail-copies-to")))
2412     (when (and (null force)
2413                co
2414                (or (equal (downcase co) "never")
2415                    (equal (downcase co) "nobody")))
2416       (error "The user has requested not to have copies sent via mail")))
2417   (when (and (message-position-on-field "To")
2418              (mail-fetch-field "to")
2419              (not (string-match "\\` *\\'" (mail-fetch-field "to"))))
2420     (insert ", "))
2421   (insert (or (message-fetch-reply-field "mail-reply-to")
2422               (message-fetch-reply-field "reply-to")
2423               (message-fetch-reply-field "from") "")))
2424
2425 (defun message-widen-reply ()
2426   "Widen the reply to include maximum recipients."
2427   (interactive)
2428   (let ((follow-to
2429          (and message-reply-buffer
2430               (buffer-name message-reply-buffer)
2431               (save-excursion
2432                 (set-buffer message-reply-buffer)
2433                 (message-get-reply-headers t)))))
2434     (save-excursion
2435       (save-restriction
2436         (message-narrow-to-headers)
2437         (dolist (elem follow-to)
2438           (message-remove-header (symbol-name (car elem)))
2439           (goto-char (point-min))
2440           (insert (symbol-name (car elem)) ": "
2441                   (cdr elem) "\n"))))))
2442
2443 (defun message-insert-newsgroups ()
2444   "Insert the Newsgroups header from the article being replied to."
2445   (interactive)
2446   (when (and (message-position-on-field "Newsgroups")
2447              (mail-fetch-field "newsgroups")
2448              (not (string-match "\\` *\\'" (mail-fetch-field "newsgroups"))))
2449     (insert ","))
2450   (insert (or (message-fetch-reply-field "newsgroups") "")))
2451
2452 \f
2453
2454 ;;; Various commands
2455
2456 (defun message-delete-not-region (beg end)
2457   "Delete everything in the body of the current message outside of the region."
2458   (interactive "r")
2459   (let (citeprefix)
2460     (save-excursion
2461       (goto-char beg)
2462       ;; snarf citation prefix, if appropriate
2463       (unless (eq (point) (progn (beginning-of-line) (point)))
2464         (when (looking-at message-cite-prefix-regexp)
2465           (setq citeprefix (match-string 0))))
2466       (goto-char end)
2467       (delete-region (point) (if (not (message-goto-signature))
2468                                  (point)
2469                                (forward-line -2)
2470                                (point)))
2471       (insert "\n")
2472       (goto-char beg)
2473       (delete-region beg (progn (message-goto-body)
2474                                 (forward-line 2)
2475                                 (point)))
2476       (when citeprefix
2477         (insert citeprefix))))
2478   (when (message-goto-signature)
2479     (forward-line -2)))
2480
2481 (defun message-kill-to-signature ()
2482   "Deletes all text up to the signature."
2483   (interactive)
2484   (let ((point (point)))
2485     (message-goto-signature)
2486     (unless (eobp)
2487       (forward-line -2))
2488     (kill-region point (point))
2489     (unless (bolp)
2490       (insert "\n"))))
2491
2492 (defun message-newline-and-reformat (&optional arg not-break)
2493   "Insert four newlines, and then reformat if inside quoted text.
2494 Prefix arg means justify as well."
2495   (interactive (list (if current-prefix-arg 'full)))
2496   (let (quoted point beg end leading-space bolp)
2497     (setq point (point))
2498     (beginning-of-line)
2499     (setq beg (point))
2500     (setq bolp (= beg point))
2501     ;; Find first line of the paragraph.
2502     (if not-break
2503         (while (and (not (eobp))
2504                     (not (looking-at message-cite-prefix-regexp))
2505                     (looking-at paragraph-start))
2506           (forward-line 1)))
2507     ;; Find the prefix
2508     (when (looking-at message-cite-prefix-regexp)
2509       (setq quoted (match-string 0))
2510       (goto-char (match-end 0))
2511       (looking-at "[ \t]*")
2512       (setq leading-space (match-string 0)))
2513     (if (and quoted
2514              (not not-break)
2515              (not bolp)
2516              (< (- point beg) (length quoted)))
2517         ;; break inside the cite prefix.
2518         (setq quoted nil
2519               end nil))
2520     (if quoted
2521         (progn
2522           (forward-line 1)
2523           (while (and (not (eobp))
2524                       (not (looking-at paragraph-separate))
2525                       (looking-at message-cite-prefix-regexp)
2526                       (equal quoted (match-string 0)))
2527             (goto-char (match-end 0))
2528             (looking-at "[ \t]*")
2529             (if (> (length leading-space) (length (match-string 0)))