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