(message-setup-1): Revert previous change, since it needs to bind the props to insert...
[gnus] / lisp / message.el
1 ;;; message.el --- composing mail and news messages
2
3 ;; Copyright (C) 1996-2011 Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: mail, news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
22
23 ;;; Commentary:
24
25 ;; This mode provides mail-sending facilities from within Emacs.  It
26 ;; consists mainly of large chunks of code from the sendmail.el,
27 ;; gnus-msg.el and rnewspost.el files.
28
29 ;;; Code:
30
31 ;; For Emacs <22.2 and XEmacs.
32 (eval-and-compile
33   (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
34 (eval-when-compile
35   (require 'cl))
36
37 (require 'mailheader)
38 (require 'gmm-utils)
39 (require 'mail-utils)
40 ;; Only for the trivial macros mail-header-from, mail-header-date
41 ;; mail-header-references, mail-header-subject, mail-header-id
42 (eval-when-compile (require 'nnheader))
43 ;; This is apparently necessary even though things are autoloaded.
44 ;; Because we dynamically bind mail-abbrev-mode-regexp, we'd better
45 ;; require mailabbrev here.
46 (if (featurep 'xemacs)
47     (require 'mail-abbrevs)
48   (require 'mailabbrev))
49 (require 'mail-parse)
50 (require 'mml)
51 (require 'rfc822)
52
53 (autoload 'mailclient-send-it "mailclient") ;; Emacs 22 or contrib/
54
55 (defvar gnus-message-group-art)
56 (defvar gnus-list-identifiers) ; gnus-sum is required where necessary
57 (defvar rmail-enable-mime-composing)
58
59 (defgroup message '((user-mail-address custom-variable)
60                     (user-full-name custom-variable))
61   "Mail and news message composing."
62   :link '(custom-manual "(message)Top")
63   :group 'mail
64   :group 'news)
65
66 (put 'user-mail-address 'custom-type 'string)
67 (put 'user-full-name 'custom-type 'string)
68
69 (defgroup message-various nil
70   "Various Message Variables."
71   :link '(custom-manual "(message)Various Message Variables")
72   :group 'message)
73
74 (defgroup message-buffers nil
75   "Message Buffers."
76   :link '(custom-manual "(message)Message Buffers")
77   :group 'message)
78
79 (defgroup message-sending nil
80   "Message Sending."
81   :link '(custom-manual "(message)Sending Variables")
82   :group 'message)
83
84 (defgroup message-interface nil
85   "Message Interface."
86   :link '(custom-manual "(message)Interface")
87   :group 'message)
88
89 (defgroup message-forwarding nil
90   "Message Forwarding."
91   :link '(custom-manual "(message)Forwarding")
92   :group 'message-interface)
93
94 (defgroup message-insertion nil
95   "Message Insertion."
96   :link '(custom-manual "(message)Insertion")
97   :group 'message)
98
99 (defgroup message-headers nil
100   "Message Headers."
101   :link '(custom-manual "(message)Message Headers")
102   :group 'message)
103
104 (defgroup message-news nil
105   "Composing News Messages."
106   :group 'message)
107
108 (defgroup message-mail nil
109   "Composing Mail Messages."
110   :group 'message)
111
112 (defgroup message-faces nil
113   "Faces used for message composing."
114   :group 'message
115   :group 'faces)
116
117 (defcustom message-directory "~/Mail/"
118   "*Directory from which all other mail file variables are derived."
119   :group 'message-various
120   :type 'directory)
121
122 (defcustom message-max-buffers 10
123   "*How many buffers to keep before starting to kill them off."
124   :group 'message-buffers
125   :type 'integer)
126
127 (defcustom message-send-rename-function nil
128   "Function called to rename the buffer after sending it."
129   :group 'message-buffers
130   :type '(choice function (const nil)))
131
132 (defcustom message-cite-style nil
133   "The overall style to be used when yanking cited text.
134 Values are either `traditional' (cited text first),
135 `top-post' (cited text at the bottom), or nil (don't override the
136 individual message variables)."
137   :version "24.1"
138   :group 'message-various
139   :type '(choice (const :tag "None" :value nil)
140                  (const :tag "Traditional" :value traditional)
141                  (const :tag "Top-post" :value top-post)))
142
143 (defcustom message-fcc-handler-function 'message-output
144   "*A function called to save outgoing articles.
145 This function will be called with the name of the file to store the
146 article in.  The default function is `message-output' which saves in Unix
147 mailbox format."
148   :type '(radio (function-item message-output)
149                 (function :tag "Other"))
150   :group 'message-sending)
151
152 (defcustom message-fcc-externalize-attachments nil
153   "If non-nil, attachments are included as external parts in Fcc copies."
154   :version "22.1"
155   :type 'boolean
156   :group 'message-sending)
157
158 (defcustom message-courtesy-message
159   "The following message is a courtesy copy of an article\nthat has been posted to %s as well.\n\n"
160   "*This is inserted at the start of a mailed copy of a posted message.
161 If the string contains the format spec \"%s\", the Newsgroups
162 the article has been posted to will be inserted there.
163 If this variable is nil, no such courtesy message will be added."
164   :group 'message-sending
165   :type '(radio string (const nil)))
166
167 (defcustom message-ignored-bounced-headers
168   "^\\(Received\\|Return-Path\\|Delivered-To\\):"
169   "*Regexp that matches headers to be removed in resent bounced mail."
170   :group 'message-interface
171   :type 'regexp)
172
173 (defcustom message-from-style 'default
174   ;; In Emacs 24.1 this defaults to the value of `mail-from-style'
175   ;; that defaults to:
176   ;; `angles' in Emacs 22.1~23.1, XEmacs 21.4, 21.5, and SXEmacs 22.1;
177   ;; `system-default' in Emacs 23.2, and 24.1
178   "Specifies how \"From\" headers look.
179
180 If nil, they contain just the return address like:
181         king@grassland.com
182 If `parens', they look like:
183         king@grassland.com (Elvis Parsley)
184 If `angles', they look like:
185         Elvis Parsley <king@grassland.com>
186
187 Otherwise, most addresses look like `angles', but they look like
188 `parens' if `angles' would need quoting and `parens' would not."
189   :version "23.2"
190   :type '(choice (const :tag "simple" nil)
191                  (const parens)
192                  (const angles)
193                  (const default))
194   :group 'message-headers)
195
196 (defcustom message-insert-canlock t
197   "Whether to insert a Cancel-Lock header in news postings."
198   :version "22.1"
199   :group 'message-headers
200   :type 'boolean)
201
202 (defcustom message-syntax-checks
203   (if message-insert-canlock '((sender . disabled)) nil)
204   ;; Guess this one shouldn't be easy to customize...
205   "*Controls what syntax checks should not be performed on outgoing posts.
206 To disable checking of long signatures, for instance, add
207  `(signature . disabled)' to this list.
208
209 Don't touch this variable unless you really know what you're doing.
210
211 Checks include `approved', `bogus-recipient', `continuation-headers',
212 `control-chars', `empty', `existing-newsgroups', `from', `illegible-text',
213 `invisible-text', `long-header-lines', `long-lines', `message-id',
214 `multiple-headers', `new-text', `newsgroups', `quoting-style',
215 `repeated-newsgroups', `reply-to', `sender', `sendsys', `shoot',
216 `shorten-followup-to', `signature', `size', `subject', `subject-cmsg'
217 and `valid-newsgroups'."
218   :group 'message-news
219   :type '(repeat sexp))                 ; Fixme: improve this
220
221 (defcustom message-required-headers '((optional . References)
222                                       From)
223   "*Headers to be generated or prompted for when sending a message.
224 Also see `message-required-news-headers' and
225 `message-required-mail-headers'."
226   :version "22.1"
227   :group 'message-news
228   :group 'message-headers
229   :link '(custom-manual "(message)Message Headers")
230   :type '(repeat sexp))
231
232 (defcustom message-draft-headers '(References From Date)
233   "*Headers to be generated when saving a draft message."
234   :version "22.1"
235   :group 'message-news
236   :group 'message-headers
237   :link '(custom-manual "(message)Message Headers")
238   :type '(repeat sexp))
239
240 (defcustom message-required-news-headers
241   '(From Newsgroups Subject Date Message-ID
242          (optional . Organization)
243          (optional . User-Agent))
244   "*Headers to be generated or prompted for when posting an article.
245 RFC977 and RFC1036 require From, Date, Newsgroups, Subject,
246 Message-ID.  Organization, Lines, In-Reply-To, Expires, and
247 User-Agent are optional.  If you don't want message to insert some
248 header, remove it from this list."
249   :group 'message-news
250   :group 'message-headers
251   :link '(custom-manual "(message)Message Headers")
252   :type '(repeat sexp))
253
254 (defcustom message-required-mail-headers
255   '(From Subject Date (optional . In-Reply-To) Message-ID
256          (optional . User-Agent))
257   "*Headers to be generated or prompted for when mailing a message.
258 It is recommended that From, Date, To, Subject and Message-ID be
259 included.  Organization and User-Agent are optional."
260   :group 'message-mail
261   :group 'message-headers
262   :link '(custom-manual "(message)Message Headers")
263   :type '(repeat sexp))
264
265 (defcustom message-prune-recipient-rules nil
266   "Rules for how to prune the list of recipients when doing wide replies.
267 This is a list of regexps and regexp matches."
268   :version "24.1"
269   :group 'message-mail
270   :group 'message-headers
271   :link '(custom-manual "(message)Wide Reply")
272   :type '(repeat regexp))
273
274 (defcustom message-deletable-headers '(Message-ID Date Lines)
275   "Headers to be deleted if they already exist and were generated by message previously."
276   :group 'message-headers
277   :link '(custom-manual "(message)Message Headers")
278   :type 'sexp)
279
280 (defcustom message-ignored-news-headers
281   "^NNTP-Posting-Host:\\|^Xref:\\|^[BGF]cc:\\|^Resent-Fcc:\\|^X-Draft-From:\\|^X-Gnus-Agent-Meta-Information:"
282   "*Regexp of headers to be removed unconditionally before posting."
283   :group 'message-news
284   :group 'message-headers
285   :link '(custom-manual "(message)Message Headers")
286   :type '(repeat :value-to-internal (lambda (widget value)
287                                       (custom-split-regexp-maybe value))
288                  :match (lambda (widget value)
289                           (or (stringp value)
290                               (widget-editable-list-match widget value)))
291                  regexp))
292
293 (defcustom message-ignored-mail-headers
294   "^\\([GF]cc\\|Resent-Fcc\\|Xref\\|X-Draft-From\\|X-Gnus-Agent-Meta-Information\\):"
295   "*Regexp of headers to be removed unconditionally before mailing."
296   :group 'message-mail
297   :group 'message-headers
298   :link '(custom-manual "(message)Mail Headers")
299   :type 'regexp)
300
301 (defcustom message-ignored-supersedes-headers "^Path:\\|^Date\\|^NNTP-Posting-Host:\\|^Xref:\\|^Lines:\\|^Received:\\|^X-From-Line:\\|^X-Trace:\\|^X-ID:\\|^X-Complaints-To:\\|Return-Path:\\|^Supersedes:\\|^NNTP-Posting-Date:\\|^X-Trace:\\|^X-Complaints-To:\\|^Cancel-Lock:\\|^Cancel-Key:\\|^X-Hashcash:\\|^X-Payment:\\|^Approved:\\|^Injection-Date:\\|^Injection-Info:"
302   "*Header lines matching this regexp will be deleted before posting.
303 It's best to delete old Path and Date headers before posting to avoid
304 any confusion."
305   :group 'message-interface
306   :link '(custom-manual "(message)Superseding")
307   :type '(repeat :value-to-internal (lambda (widget value)
308                                       (custom-split-regexp-maybe value))
309                  :match (lambda (widget value)
310                           (or (stringp value)
311                               (widget-editable-list-match widget value)))
312                  regexp))
313
314 (defcustom message-subject-re-regexp
315   "^[ \t]*\\([Rr][Ee]\\(\\[[0-9]*\\]\\)*:[ \t]*\\)*[ \t]*"
316   "*Regexp matching \"Re: \" in the subject line."
317   :group 'message-various
318   :link '(custom-manual "(message)Message Headers")
319   :type 'regexp)
320
321 ;;; Start of variables adopted from `message-utils.el'.
322
323 (defcustom message-subject-trailing-was-query t
324   "*What to do with trailing \"(was: <old subject>)\" in subject lines.
325 If nil, leave the subject unchanged.  If it is the symbol `ask', query
326 the user what do do.  In this case, the subject is matched against
327 `message-subject-trailing-was-ask-regexp'.  If
328 `message-subject-trailing-was-query' is t, always strip the trailing
329 old subject.  In this case, `message-subject-trailing-was-regexp' is
330 used."
331   :version "24.1"
332   :type '(choice (const :tag "never" nil)
333                  (const :tag "always strip" t)
334                  (const ask))
335   :link '(custom-manual "(message)Message Headers")
336   :group 'message-various)
337
338 (defcustom message-subject-trailing-was-ask-regexp
339   "[ \t]*\\([[(]+[Ww][Aa][Ss]:?[ \t]*.*[])]+\\)"
340   "*Regexp matching \"(was: <old subject>)\" in the subject line.
341
342 The function `message-strip-subject-trailing-was' uses this regexp if
343 `message-subject-trailing-was-query' is set to the symbol `ask'.  If
344 the variable is t instead of `ask', use
345 `message-subject-trailing-was-regexp' instead.
346
347 It is okay to create some false positives here, as the user is asked."
348   :version "22.1"
349   :group 'message-various
350   :link '(custom-manual "(message)Message Headers")
351   :type 'regexp)
352
353 (defcustom message-subject-trailing-was-regexp
354   "[ \t]*\\((*[Ww][Aa][Ss]:[ \t]*.*)\\)"
355   "*Regexp matching \"(was: <old subject>)\" in the subject line.
356
357 If `message-subject-trailing-was-query' is set to t, the subject is
358 matched against `message-subject-trailing-was-regexp' in
359 `message-strip-subject-trailing-was'.  You should use a regexp creating very
360 few false positives here."
361   :version "22.1"
362   :group 'message-various
363   :link '(custom-manual "(message)Message Headers")
364   :type 'regexp)
365
366 ;;; marking inserted text
367
368 (defcustom message-mark-insert-begin
369   "--8<---------------cut here---------------start------------->8---\n"
370   "How to mark the beginning of some inserted text."
371   :version "22.1"
372   :type 'string
373   :link '(custom-manual "(message)Insertion Variables")
374   :group 'message-various)
375
376 (defcustom message-mark-insert-end
377   "--8<---------------cut here---------------end--------------->8---\n"
378   "How to mark the end of some inserted text."
379   :version "22.1"
380   :type 'string
381   :link '(custom-manual "(message)Insertion Variables")
382   :group 'message-various)
383
384 (defcustom message-archive-header "X-No-Archive: Yes\n"
385   "Header to insert when you don't want your article to be archived.
386 Archives \(such as groups.google.com\) respect this header."
387   :version "22.1"
388   :type 'string
389   :link '(custom-manual "(message)Header Commands")
390   :group 'message-various)
391
392 (defcustom message-archive-note
393   "X-No-Archive: Yes - save http://groups.google.com/"
394   "Note to insert why you wouldn't want this posting archived.
395 If nil, don't insert any text in the body."
396   :version "22.1"
397   :type '(radio string (const nil))
398   :link '(custom-manual "(message)Header Commands")
399   :group 'message-various)
400
401 ;;; Crossposts and Followups
402 ;; inspired by JoH-followup-to by Jochem Huhman <joh  at gmx.de>
403 ;; new suggestions by R. Weikusat <rw at another.de>
404
405 (defvar message-cross-post-old-target nil
406   "Old target for cross-posts or follow-ups.")
407 (make-variable-buffer-local 'message-cross-post-old-target)
408
409 (defcustom message-cross-post-default t
410   "When non-nil `message-cross-post-followup-to' will perform a crosspost.
411 If nil, `message-cross-post-followup-to' will only do a followup.  Note that
412 you can explicitly override this setting by calling
413 `message-cross-post-followup-to' with a prefix."
414   :version "22.1"
415   :type 'boolean
416   :group 'message-various)
417
418 (defcustom message-cross-post-note "Crosspost & Followup-To: "
419   "Note to insert before signature to notify of cross-post and follow-up."
420   :version "22.1"
421   :type 'string
422   :group 'message-various)
423
424 (defcustom message-followup-to-note "Followup-To: "
425   "Note to insert before signature to notify of follow-up only."
426   :version "22.1"
427   :type 'string
428   :group 'message-various)
429
430 (defcustom message-cross-post-note-function 'message-cross-post-insert-note
431   "Function to use to insert note about Crosspost or Followup-To.
432 The function will be called with four arguments.  The function should not only
433 insert a note, but also ensure old notes are deleted.  See the documentation
434 for `message-cross-post-insert-note'."
435   :version "22.1"
436   :type 'function
437   :group 'message-various)
438
439 ;;; End of variables adopted from `message-utils.el'.
440
441 (defcustom message-signature-separator "^-- $"
442   "Regexp matching the signature separator.
443 This variable is used to strip off the signature from quoted text
444 when `message-cite-function' is
445 `message-cite-original-without-signature'.  Most useful values
446 are \"^-- $\" (strict) and \"^-- *$\" (loose; allow missing
447 whitespace)."
448   :type '(choice (const :tag "strict" "^-- $")
449                  (const :tag "loose" "^-- *$")
450                  regexp)
451   :version "22.3" ;; Gnus 5.10.12 (changed default)
452   :link '(custom-manual "(message)Various Message Variables")
453   :group 'message-various)
454
455 (defcustom message-elide-ellipsis "\n[...]\n\n"
456   "*The string which is inserted for elided text."
457   :type 'string
458   :link '(custom-manual "(message)Various Commands")
459   :group 'message-various)
460
461 (defcustom message-interactive t
462   ;; In Emacs 24.1 this defaults to the value of `mail-interactive'
463   ;; that defaults to:
464   ;; `nil' in Emacs 22.1~22.3, XEmacs 21.4, 21.5, and SXEmacs 22.1;
465   ;; `t' in Emacs 23.1~24.1
466   "Non-nil means when sending a message wait for and display errors.
467 A value of nil means let mailer mail back a message to report errors."
468   :version "23.2"
469   :group 'message-sending
470   :group 'message-mail
471   :link '(custom-manual "(message)Sending Variables")
472   :type 'boolean)
473
474 (defcustom message-confirm-send nil
475   "When non-nil, ask for confirmation when sending a message."
476   :group 'message-sending
477   :group 'message-mail
478   :version "23.1" ;; No Gnus
479   :link '(custom-manual "(message)Sending Variables")
480   :type 'boolean)
481
482 (defcustom message-generate-new-buffers 'unsent
483   "*Say whether to create a new message buffer to compose a message.
484 Valid values include:
485
486 nil
487   Generate the buffer name in the Message way (e.g., *mail*, *news*,
488   *mail to whom*, *news on group*, etc.) and continue editing in the
489   existing buffer of that name.  If there is no such buffer, it will
490   be newly created.
491
492 `unique' or t
493   Create the new buffer with the name generated in the Message way.
494
495 `unsent'
496   Similar to `unique' but the buffer name begins with \"*unsent \".
497
498 `standard'
499   Similar to nil but the buffer name is simpler like *mail message*.
500
501 function
502   If this is a function, call that function with three parameters:
503   The type, the To address and the group name (any of these may be nil).
504   The function should return the new buffer name."
505   :version "24.1"
506   :group 'message-buffers
507   :link '(custom-manual "(message)Message Buffers")
508   :type '(choice (const nil)
509                  (sexp :tag "unique" :format "unique\n" :value unique
510                        :match (lambda (widget value) (memq value '(unique t))))
511                  (const unsent)
512                  (const standard)
513                  (function :format "\n    %{%t%}: %v")))
514
515 (defcustom message-kill-buffer-on-exit nil
516   "*Non-nil means that the message buffer will be killed after sending a message."
517   :group 'message-buffers
518   :link '(custom-manual "(message)Message Buffers")
519   :type 'boolean)
520
521 (defcustom message-kill-buffer-query t
522   "*Non-nil means that killing a modified message buffer has to be confirmed.
523 This is used by `message-kill-buffer'."
524   :version "23.1" ;; No Gnus
525   :group 'message-buffers
526   :type 'boolean)
527
528 (defcustom message-user-organization
529   (or (getenv "ORGANIZATION") t)
530   "String to be used as an Organization header.
531 If t, use `message-user-organization-file'."
532   :group 'message-headers
533   :type '(choice string
534                  (const :tag "consult file" t)))
535
536 (defcustom message-user-organization-file
537   (let (orgfile)
538     (dolist (f (list "/etc/organization"
539                      "/etc/news/organization"
540                      "/usr/lib/news/organization"))
541       (when (file-readable-p f)
542         (setq orgfile f)))
543     orgfile)
544   "*Local news organization file."
545   :type 'file
546   :link '(custom-manual "(message)News Headers")
547   :group 'message-headers)
548
549 (defcustom message-make-forward-subject-function
550   #'message-forward-subject-name-subject
551   "*List of functions called to generate subject headers for forwarded messages.
552 The subject generated by the previous function is passed into each
553 successive function.
554
555 The provided functions are:
556
557 * `message-forward-subject-author-subject' Source of article (author or
558       newsgroup), in brackets followed by the subject
559 * `message-forward-subject-name-subject' Source of article (name of author
560       or newsgroup), in brackets followed by the subject
561 * `message-forward-subject-fwd' Subject of article with 'Fwd:' prepended
562       to it."
563   :group 'message-forwarding
564   :link '(custom-manual "(message)Forwarding")
565   :type '(radio (function-item message-forward-subject-author-subject)
566                 (function-item message-forward-subject-fwd)
567                 (function-item message-forward-subject-name-subject)
568                 (repeat :tag "List of functions" function)))
569
570 (defcustom message-forward-as-mime t
571   "*Non-nil means forward messages as an inline/rfc822 MIME section.
572 Otherwise, directly inline the old message in the forwarded message."
573   :version "21.1"
574   :group 'message-forwarding
575   :link '(custom-manual "(message)Forwarding")
576   :type 'boolean)
577
578 (defcustom message-forward-show-mml 'best
579   "*Non-nil means show forwarded messages as MML (decoded from MIME).
580 Otherwise, forwarded messages are unchanged.
581 Can also be the symbol `best' to indicate that MML should be
582 used, except when it is a bad idea to use MML.  One example where
583 it is a bad idea is when forwarding a signed or encrypted
584 message, because converting MIME to MML would invalidate the
585 digital signature."
586   :version "21.1"
587   :group 'message-forwarding
588   :type '(choice (const :tag "use MML" t)
589                  (const :tag "don't use MML " nil)
590                  (const :tag "use MML when appropriate" best)))
591
592 (defcustom message-forward-before-signature t
593   "*Non-nil means put forwarded message before signature, else after."
594   :group 'message-forwarding
595   :type 'boolean)
596
597 (defcustom message-wash-forwarded-subjects nil
598   "*Non-nil means try to remove as much cruft as possible from the subject.
599 Done before generating the new subject of a forward."
600   :group 'message-forwarding
601   :link '(custom-manual "(message)Forwarding")
602   :type 'boolean)
603
604 (defcustom message-ignored-resent-headers
605   ;; `Delivered-To' needs to be removed because some mailers use it to
606   ;; detect loops, so if you resend a message to an address that ultimately
607   ;; comes back to you (e.g. a mailing-list to which you subscribe, in which
608   ;; case you may be removed from the list on the grounds that mail to you
609   ;; bounced with a "mailing loop" error).
610   "^Return-receipt\\|^X-Gnus\\|^Gnus-Warning:\\|^>?From \\|^Delivered-To:"
611   "*All headers that match this regexp will be deleted when resending a message."
612   :group 'message-interface
613   :link '(custom-manual "(message)Resending")
614   :type '(repeat :value-to-internal (lambda (widget value)
615                                       (custom-split-regexp-maybe value))
616                  :match (lambda (widget value)
617                           (or (stringp value)
618                               (widget-editable-list-match widget value)))
619                  regexp))
620
621 (defcustom message-forward-ignored-headers "^Content-Transfer-Encoding:\\|^X-Gnus"
622   "*All headers that match this regexp will be deleted when forwarding a message."
623   :version "21.1"
624   :group 'message-forwarding
625   :type '(repeat :value-to-internal (lambda (widget value)
626                                       (custom-split-regexp-maybe value))
627                  :match (lambda (widget value)
628                           (or (stringp value)
629                               (widget-editable-list-match widget value)))
630                  regexp))
631
632 (defcustom message-ignored-cited-headers "."
633   "*Delete these headers from the messages you yank."
634   :group 'message-insertion
635   :link '(custom-manual "(message)Insertion Variables")
636   :type 'regexp)
637
638 (defcustom message-cite-prefix-regexp
639   ;; In Emacs 24.1 this defaults to the value of
640   ;; `mail-citation-prefix-regexp'; the default value varies according
641   ;; to the Emacs version.  In XEmacs 21.4 and 21.5, sendmail.el
642   ;; provides it.
643   (if (string-match "[[:digit:]]" "1")
644       ;; Support POSIX?  XEmacs 21.5.27 doesn't.
645       "\\([ \t]*[_.[:word:]]+>+\\|[ \t]*[]>|]\\)+"
646     ;; ?-, ?_ or ?. MUST NOT be in syntax entry w.
647     (let (non-word-constituents)
648       (with-syntax-table text-mode-syntax-table
649         (setq non-word-constituents
650               (concat
651                (if (string-match "\\w" "_")  "" "_")
652                (if (string-match "\\w" ".")  "" "."))))
653       (if (equal non-word-constituents "")
654           "\\([ \t]*\\(\\w\\)+>+\\|[ \t]*[]>|]\\)+"
655         (concat "\\([ \t]*\\(\\w\\|["
656                 non-word-constituents
657                 "]\\)+>+\\|[ \t]*[]>|]\\)+"))))
658   "*Regexp matching the longest possible citation prefix on a line."
659   :version "24.1"
660   :group 'message-insertion
661   :link '(custom-manual "(message)Insertion Variables")
662   :type 'regexp
663   :set (lambda (symbol value)
664          (prog1
665              (custom-set-default symbol value)
666            (if (boundp 'gnus-message-cite-prefix-regexp)
667                (setq gnus-message-cite-prefix-regexp
668                      (concat "^\\(?:" value "\\)"))))))
669
670 (defcustom message-cancel-message "I am canceling my own article.\n"
671   "Message to be inserted in the cancel message."
672   :group 'message-interface
673   :link '(custom-manual "(message)Canceling News")
674   :type 'string)
675
676 (defun message-send-mail-function ()
677   "Return suitable value for the variable `message-send-mail-function'."
678   (cond ((and (require 'sendmail)
679               (boundp 'sendmail-program)
680               sendmail-program
681               (executable-find sendmail-program))
682          'message-send-mail-with-sendmail)
683         ((and (locate-library "smtpmail")
684               (boundp 'smtpmail-default-smtp-server)
685               smtpmail-default-smtp-server)
686          'message-smtpmail-send-it)
687         ((locate-library "mailclient")
688          'message-send-mail-with-mailclient)
689         (t
690          (error "Don't know how to send mail.  Please customize `message-send-mail-function'"))))
691
692 ;; Useful to set in site-init.el
693 (defcustom message-send-mail-function
694   (cond ((eq send-mail-function 'smtpmail-send-it) 'message-smtpmail-send-it)
695         ((eq send-mail-function 'feedmail-send-it) 'feedmail-send-it)
696         ((eq send-mail-function 'mailclient-send-it)
697          'message-send-mail-with-mailclient)
698         (t (message-send-mail-function)))
699   "Function to call to send the current buffer as mail.
700 The headers should be delimited by a line whose contents match the
701 variable `mail-header-separator'.
702
703 Valid values include `message-send-mail-with-sendmail'
704 `message-send-mail-with-mh', `message-send-mail-with-qmail',
705 `message-smtpmail-send-it', `smtpmail-send-it',
706 `feedmail-send-it' and `message-send-mail-with-mailclient'.  The
707 default is system dependent and determined by the function
708 `message-send-mail-function'.
709
710 See also `send-mail-function'."
711   :type '(radio (function-item message-send-mail-with-sendmail)
712                 (function-item message-send-mail-with-mh)
713                 (function-item message-send-mail-with-qmail)
714                 (function-item message-smtpmail-send-it)
715                 (function-item smtpmail-send-it)
716                 (function-item feedmail-send-it)
717                 (function-item message-send-mail-with-mailclient
718                                :tag "Use Mailclient package")
719                 (function :tag "Other"))
720   :group 'message-sending
721   :version "23.2"
722   :initialize 'custom-initialize-default
723   :link '(custom-manual "(message)Mail Variables")
724   :group 'message-mail)
725
726 (defcustom message-send-news-function 'message-send-news
727   "Function to call to send the current buffer as news.
728 The headers should be delimited by a line whose contents match the
729 variable `mail-header-separator'."
730   :group 'message-sending
731   :group 'message-news
732   :link '(custom-manual "(message)News Variables")
733   :type 'function)
734
735 (defcustom message-reply-to-function nil
736   "If non-nil, function that should return a list of headers.
737 This function should pick out addresses from the To, Cc, and From headers
738 and respond with new To and Cc headers."
739   :group 'message-interface
740   :link '(custom-manual "(message)Reply")
741   :type '(choice function (const nil)))
742
743 (defcustom message-wide-reply-to-function nil
744   "If non-nil, function that should return a list of headers.
745 This function should pick out addresses from the To, Cc, and From headers
746 and respond with new To and Cc headers."
747   :group 'message-interface
748   :link '(custom-manual "(message)Wide Reply")
749   :type '(choice function (const nil)))
750
751 (defcustom message-followup-to-function nil
752   "If non-nil, function that should return a list of headers.
753 This function should pick out addresses from the To, Cc, and From headers
754 and respond with new To and Cc headers."
755   :group 'message-interface
756   :link '(custom-manual "(message)Followup")
757   :type '(choice function (const nil)))
758
759 (defcustom message-extra-wide-headers nil
760   "If non-nil, a list of additional address headers.
761 These are used when composing a wide reply."
762   :group 'message-sending
763   :type '(repeat string))
764
765 (defcustom message-use-followup-to 'ask
766   "*Specifies what to do with Followup-To header.
767 If nil, always ignore the header.  If it is t, use its value, but
768 query before using the \"poster\" value.  If it is the symbol `ask',
769 always query the user whether to use the value.  If it is the symbol
770 `use', always use the value."
771   :group 'message-interface
772   :link '(custom-manual "(message)Followup")
773   :type '(choice (const :tag "ignore" nil)
774                  (const :tag "use & query" t)
775                  (const use)
776                  (const ask)))
777
778 (defcustom message-use-mail-followup-to 'use
779   "*Specifies what to do with Mail-Followup-To header.
780 If nil, always ignore the header.  If it is the symbol `ask', always
781 query the user whether to use the value.  If it is the symbol `use',
782 always use the value."
783   :version "22.1"
784   :group 'message-interface
785   :link '(custom-manual "(message)Mailing Lists")
786   :type '(choice (const :tag "ignore" nil)
787                  (const use)
788                  (const ask)))
789
790 (defcustom message-subscribed-address-functions nil
791   "*Specifies functions for determining list subscription.
792 If nil, do not attempt to determine list subscription with functions.
793 If non-nil, this variable contains a list of functions which return
794 regular expressions to match lists.  These functions can be used in
795 conjunction with `message-subscribed-regexps' and
796 `message-subscribed-addresses'."
797   :version "22.1"
798   :group 'message-interface
799   :link '(custom-manual "(message)Mailing Lists")
800   :type '(repeat sexp))
801
802 (defcustom message-subscribed-address-file nil
803   "*A file containing addresses the user is subscribed to.
804 If nil, do not look at any files to determine list subscriptions.  If
805 non-nil, each line of this file should be a mailing list address."
806   :version "22.1"
807   :group 'message-interface
808   :link '(custom-manual "(message)Mailing Lists")
809   :type '(radio file (const nil)))
810
811 (defcustom message-subscribed-addresses nil
812   "*Specifies a list of addresses the user is subscribed to.
813 If nil, do not use any predefined list subscriptions.  This list of
814 addresses can be used in conjunction with
815 `message-subscribed-address-functions' and `message-subscribed-regexps'."
816   :version "22.1"
817   :group 'message-interface
818   :link '(custom-manual "(message)Mailing Lists")
819   :type '(repeat string))
820
821 (defcustom message-subscribed-regexps nil
822   "*Specifies a list of addresses the user is subscribed to.
823 If nil, do not use any predefined list subscriptions.  This list of
824 regular expressions can be used in conjunction with
825 `message-subscribed-address-functions' and `message-subscribed-addresses'."
826   :version "22.1"
827   :group 'message-interface
828   :link '(custom-manual "(message)Mailing Lists")
829   :type '(repeat regexp))
830
831 (defcustom message-allow-no-recipients 'ask
832   "Specifies what to do when there are no recipients other than Gcc/Fcc.
833 If it is the symbol `always', the posting is allowed.  If it is the
834 symbol `never', the posting is not allowed.  If it is the symbol
835 `ask', you are prompted."
836   :version "22.1"
837   :group 'message-interface
838   :link '(custom-manual "(message)Message Headers")
839   :type '(choice (const always)
840                  (const never)
841                  (const ask)))
842
843 (defcustom message-sendmail-f-is-evil nil
844   "*Non-nil means don't add \"-f username\" to the sendmail command line.
845 Doing so would be even more evil than leaving it out."
846   :group 'message-sending
847   :link '(custom-manual "(message)Mail Variables")
848   :type 'boolean)
849
850 (defcustom message-sendmail-envelope-from nil
851   ;; In Emacs 24.1 this defaults to the value of `mail-envelope-from'
852   ;; if it is available, or defaults to nil.  sendmail.el provides it;
853   ;; the default value is nil in all (X)Emacsen that Gnus supports.
854   "*Envelope-from when sending mail with sendmail.
855 If this is nil, use `user-mail-address'.  If it is the symbol
856 `header', use the From: header of the message."
857   :version "23.2"
858   :type '(choice (string :tag "From name")
859                  (const :tag "Use From: header from message" header)
860                  (const :tag "Use `user-mail-address'" nil))
861   :link '(custom-manual "(message)Mail Variables")
862   :group 'message-sending)
863
864 (defcustom message-sendmail-extra-arguments nil
865   "Additional arguments to `sendmail-program'."
866   ;; E.g. '("-a" "account") for msmtp
867   :version "23.1" ;; No Gnus
868   :type '(repeat string)
869   ;; :link '(custom-manual "(message)Mail Variables")
870   :group 'message-sending)
871
872 ;; qmail-related stuff
873 (defcustom message-qmail-inject-program "/var/qmail/bin/qmail-inject"
874   "Location of the qmail-inject program."
875   :group 'message-sending
876   :link '(custom-manual "(message)Mail Variables")
877   :type 'file)
878
879 (defcustom message-qmail-inject-args nil
880   "Arguments passed to qmail-inject programs.
881 This should be a list of strings, one string for each argument.
882 It may also be a function.
883
884 For e.g., if you wish to set the envelope sender address so that bounces
885 go to the right place or to deal with listserv's usage of that address, you
886 might set this variable to '(\"-f\" \"you@some.where\")."
887   :group 'message-sending
888   :link '(custom-manual "(message)Mail Variables")
889   :type '(choice (function)
890                  (repeat string)))
891
892 (defvar gnus-post-method)
893 (defvar gnus-select-method)
894 (defcustom message-post-method
895   (cond ((and (boundp 'gnus-post-method)
896               (listp gnus-post-method)
897               gnus-post-method)
898          gnus-post-method)
899         ((boundp 'gnus-select-method)
900          gnus-select-method)
901         (t '(nnspool "")))
902   "*Method used to post news.
903 Note that when posting from inside Gnus, for instance, this
904 variable isn't used."
905   :group 'message-news
906   :group 'message-sending
907   ;; This should be the `gnus-select-method' widget, but that might
908   ;; create a dependence to `gnus.el'.
909   :type 'sexp)
910
911 (defcustom message-generate-headers-first nil
912   "Which headers should be generated before starting to compose a message.
913 If t, generate all required headers.  This can also be a list of headers to
914 generate.  The variables `message-required-news-headers' and
915 `message-required-mail-headers' specify which headers to generate.
916
917 Note that the variable `message-deletable-headers' specifies headers which
918 are to be deleted and then re-generated before sending, so this variable
919 will not have a visible effect for those headers."
920   :group 'message-headers
921   :link '(custom-manual "(message)Message Headers")
922   :type '(choice (const :tag "None" nil)
923                  (const :tag "All" t)
924                  (repeat (sexp :tag "Header"))))
925
926 (defcustom message-fill-column 72
927   "Column beyond which automatic line-wrapping should happen.
928 Local value for message buffers.  If non-nil, also turn on
929 auto-fill in message buffers."
930   :group 'message-various
931   ;; :link '(custom-manual "(message)Message Headers")
932   :type '(choice (const :tag "Don't turn on auto fill" nil)
933                  (integer)))
934
935 (defcustom message-setup-hook nil
936   "Normal hook, run each time a new outgoing message is initialized.
937 The function `message-setup' runs this hook."
938   :group 'message-various
939   :link '(custom-manual "(message)Various Message Variables")
940   :type 'hook)
941
942 (defcustom message-cancel-hook nil
943   "Hook run when cancelling articles."
944   :group 'message-various
945   :link '(custom-manual "(message)Various Message Variables")
946   :type 'hook)
947
948 (defcustom message-signature-setup-hook nil
949   "Normal hook, run each time a new outgoing message is initialized.
950 It is run after the headers have been inserted and before
951 the signature is inserted."
952   :group 'message-various
953   :link '(custom-manual "(message)Various Message Variables")
954   :type 'hook)
955
956 (defcustom message-mode-hook nil
957   "Hook run in message mode buffers."
958   :group 'message-various
959   :type 'hook)
960
961 (defcustom message-header-hook nil
962   "Hook run in a message mode buffer narrowed to the headers."
963   :group 'message-various
964   :type 'hook)
965
966 (defcustom message-header-setup-hook nil
967   "Hook called narrowed to the headers when setting up a message buffer."
968   :group 'message-various
969   :link '(custom-manual "(message)Various Message Variables")
970   :type 'hook)
971
972 (defcustom message-minibuffer-local-map
973   (let ((map (make-sparse-keymap 'message-minibuffer-local-map)))
974     (set-keymap-parent map minibuffer-local-map)
975     map)
976   "Keymap for `message-read-from-minibuffer'."
977   :version "22.1"
978   :group 'message-various)
979
980 (defcustom message-citation-line-function 'message-insert-citation-line
981   "*Function called to insert the \"Whomever writes:\" line.
982
983 Predefined functions include `message-insert-citation-line' and
984 `message-insert-formatted-citation-line' (see the variable
985 `message-citation-line-format').
986
987 Note that Gnus provides a feature where the reader can click on
988 `writes:' to hide the cited text.  If you change this line too much,
989 people who read your message will have to change their Gnus
990 configuration.  See the variable `gnus-cite-attribution-suffix'."
991   :type '(choice
992           (function-item :tag "plain" message-insert-citation-line)
993           (function-item :tag "formatted" message-insert-formatted-citation-line)
994           (function :tag "Other"))
995   :link '(custom-manual "(message)Insertion Variables")
996   :group 'message-insertion)
997
998 (defcustom message-citation-line-format "On %a, %b %d %Y, %N wrote:\n"
999   "Format of the \"Whomever writes:\" line.
1000
1001 The string is formatted using `format-spec'.  The following
1002 constructs are replaced:
1003
1004   %f   The full From, e.g. \"John Doe <john.doe@example.invalid>\".
1005   %n   The mail address, e.g. \"john.doe@example.invalid\".
1006   %N   The real name if present, e.g.: \"John Doe\", else fall
1007        back to the mail address.
1008   %F   The first name if present, e.g.: \"John\".
1009   %L   The last name if present, e.g.: \"Doe\".
1010
1011 All other format specifiers are passed to `format-time-string'
1012 which is called using the date from the article your replying to.
1013 Extracting the first (%F) and last name (%L) is done
1014 heuristically, so you should always check it yourself.
1015
1016 Please also read the note in the documentation of
1017 `message-citation-line-function'."
1018   :type '(choice (const :tag "Plain" "%f writes:")
1019                  (const :tag "Include date" "On %a, %b %d %Y, %n wrote:")
1020                  string)
1021   :link '(custom-manual "(message)Insertion Variables")
1022   :version "23.1" ;; No Gnus
1023   :group 'message-insertion)
1024
1025 (defcustom message-yank-prefix "> "
1026   ;; In Emacs 24.1 this defaults to the value of `mail-yank-prefix'
1027   ;; that defaults to:
1028   ;; `nil' in Emacs 22.1~23.1;
1029   ;; "> " in Emacs 23.2, 24.1, XEmacs 21.4, 21.5, and SXEmacs 22.1
1030   "*Prefix inserted on the lines of yanked messages.
1031 Fix `message-cite-prefix-regexp' if it is set to an abnormal value.
1032 See also `message-yank-cited-prefix' and `message-yank-empty-prefix'."
1033   :version "23.2"
1034   :type 'string
1035   :link '(custom-manual "(message)Insertion Variables")
1036   :group 'message-insertion)
1037
1038 (defcustom message-yank-cited-prefix ">"
1039   "*Prefix inserted on cited lines of yanked messages.
1040 Fix `message-cite-prefix-regexp' if it is set to an abnormal value.
1041 See also `message-yank-prefix' and `message-yank-empty-prefix'."
1042   :version "22.1"
1043   :type 'string
1044   :link '(custom-manual "(message)Insertion Variables")
1045   :group 'message-insertion)
1046
1047 (defcustom message-yank-empty-prefix ">"
1048   "*Prefix inserted on empty lines of yanked messages.
1049 See also `message-yank-prefix' and `message-yank-cited-prefix'."
1050   :version "22.1"
1051   :type 'string
1052   :link '(custom-manual "(message)Insertion Variables")
1053   :group 'message-insertion)
1054
1055 (defcustom message-indentation-spaces 3
1056   ;; In Emacs 24.1 this defaults to the value of
1057   ;; `mail-indentation-spaces' that defaults to `3' in Emacs 22.1~24.1,
1058   ;; and SXEmacs 22.1.  In XEmacs 21.4 and 21.5, sendmail.el provides
1059   ;; it; the defalut value is `3'.
1060   "*Number of spaces to insert at the beginning of each cited line.
1061 Used by `message-yank-original' via `message-yank-cite'."
1062   :version "23.2"
1063   :group 'message-insertion
1064   :link '(custom-manual "(message)Insertion Variables")
1065   :type 'integer)
1066
1067 (defcustom message-cite-function 'message-cite-original-without-signature
1068   "*Function for citing an original message.
1069 Predefined functions include `message-cite-original' and
1070 `message-cite-original-without-signature'.
1071 Note that these functions use `mail-citation-hook' if that is non-nil."
1072   :type '(radio (function-item message-cite-original)
1073                 (function-item message-cite-original-without-signature)
1074                 (function-item sc-cite-original)
1075                 (function :tag "Other"))
1076   :link '(custom-manual "(message)Insertion Variables")
1077   :version "22.3" ;; Gnus 5.10.12 (changed default)
1078   :group 'message-insertion)
1079
1080 (defcustom message-indent-citation-function 'message-indent-citation
1081   "*Function for modifying a citation just inserted in the mail buffer.
1082 This can also be a list of functions.  Each function can find the
1083 citation between (point) and (mark t).  And each function should leave
1084 point and mark around the citation text as modified."
1085   :type 'function
1086   :link '(custom-manual "(message)Insertion Variables")
1087   :group 'message-insertion)
1088
1089 (defcustom message-signature t
1090   ;; In Emacs 24.1 this defaults to the value of `mail-signature' that
1091   ;; defaults to:
1092   ;; `nil' in Emacs 22.1~23.1, XEmacs 21.4, 21.5, and SXEmacs 22.1;
1093   ;; `t' in Emacs 23.2, and 24.1
1094   "*String to be inserted at the end of the message buffer.
1095 If t, the `message-signature-file' file will be inserted instead.
1096 If a function, the result from the function will be used instead.
1097 If a form, the result from the form will be used instead."
1098   :version "23.2"
1099   :type 'sexp
1100   :link '(custom-manual "(message)Insertion Variables")
1101   :group 'message-insertion)
1102
1103 (defcustom message-signature-file "~/.signature"
1104   ;; In Emacs 24.1 this defaults to the value of `mail-signature-file'
1105   ;; that defaults to "~/.signature" in Emacs 22.1~24.1, and SXEmacs
1106   ;; 22.1.  In XEmacs 21.4 and 21.5, sendmail.el provides it;
1107   ;; the defalut value is "~/.signature".
1108   "*Name of file containing the text inserted at end of message buffer.
1109 Ignored if the named file doesn't exist.
1110 If nil, don't insert a signature.
1111 If a path is specified, the value of `message-signature-directory' is ignored,
1112 even if set."
1113   :version "23.2"
1114   :type '(choice file (const :tags "None" nil))
1115   :link '(custom-manual "(message)Insertion Variables")
1116   :group 'message-insertion)
1117
1118 (defcustom message-signature-directory nil
1119   "*Name of directory containing signature files.
1120 Comes in handy if you have many such files, handled via posting styles for
1121 instance.
1122 If nil, `message-signature-file' is expected to specify the directory if
1123 needed."
1124   :type '(choice string (const :tags "None" nil))
1125   :link '(custom-manual "(message)Insertion Variables")
1126   :group 'message-insertion)
1127
1128 (defcustom message-signature-insert-empty-line t
1129   "*If non-nil, insert an empty line before the signature separator."
1130   :version "22.1"
1131   :type 'boolean
1132   :link '(custom-manual "(message)Insertion Variables")
1133   :group 'message-insertion)
1134
1135 (defcustom message-distribution-function nil
1136   "*Function called to return a Distribution header."
1137   :group 'message-news
1138   :group 'message-headers
1139   :link '(custom-manual "(message)News Headers")
1140   :type '(choice function (const nil)))
1141
1142 (defcustom message-expires 14
1143   "Number of days before your article expires."
1144   :group 'message-news
1145   :group 'message-headers
1146   :link '(custom-manual "(message)News Headers")
1147   :type 'integer)
1148
1149 (defcustom message-user-path nil
1150   "If nil, use the NNTP server name in the Path header.
1151 If stringp, use this; if non-nil, use no host name (user name only)."
1152   :group 'message-news
1153   :group 'message-headers
1154   :link '(custom-manual "(message)News Headers")
1155   :type '(choice (const :tag "nntp" nil)
1156                  (string :tag "name")
1157                  (sexp :tag "none" :format "%t" t)))
1158
1159 ;; This can be the name of a buffer, or a cons cell (FUNCTION . ARGS)
1160 ;; for yanking the original buffer.
1161 (defvar message-reply-buffer nil)
1162 (defvar message-reply-headers nil
1163   "The headers of the current replied article.
1164 It is a vector of the following headers:
1165 \[number subject from date id references chars lines xref extra].")
1166 (defvar message-newsreader nil)
1167 (defvar message-mailer nil)
1168 (defvar message-sent-message-via nil)
1169 (defvar message-checksum nil)
1170 (defvar message-send-actions nil
1171   "A list of actions to be performed upon successful sending of a message.")
1172 (defvar message-return-action nil
1173   "Action to return to the caller after sending or postphoning a message.")
1174 (defvar message-exit-actions nil
1175   "A list of actions to be performed upon exiting after sending a message.")
1176 (defvar message-kill-actions nil
1177   "A list of actions to be performed before killing a message buffer.")
1178 (defvar message-postpone-actions nil
1179   "A list of actions to be performed after postponing a message.")
1180
1181 (define-widget 'message-header-lines 'text
1182   "All header lines must be LFD terminated."
1183   :format "%{%t%}:%n%v"
1184   :valid-regexp "^\\'"
1185   :error "All header lines must be newline terminated")
1186
1187 (defcustom message-default-headers ""
1188   "Header lines to be inserted in outgoing messages.
1189 This can be set to a string containing or a function returning
1190 header lines to be inserted before you edit the message, so you
1191 can edit or delete these lines.  If set to a function, it is
1192 called and its result is inserted."
1193   :version "23.2"
1194   :group 'message-headers
1195   :link '(custom-manual "(message)Message Headers")
1196   :type '(choice
1197           (message-header-lines :tag "String")
1198           (function :tag "Function")))
1199
1200 (defcustom message-default-mail-headers
1201   ;; Ease the transition from mail-mode to message-mode.  See bugs#4431, 5555.
1202   (concat (if (and (boundp 'mail-default-reply-to)
1203                    (stringp mail-default-reply-to))
1204               (format "Reply-to: %s\n" mail-default-reply-to))
1205           (if (and (boundp 'mail-self-blind)
1206                    mail-self-blind)
1207               (format "BCC: %s\n" user-mail-address))
1208           (if (and (boundp 'mail-archive-file-name)
1209                    (stringp mail-archive-file-name))
1210               (format "FCC: %s\n" mail-archive-file-name))
1211           ;; Use the value of `mail-default-headers' if available.
1212           ;; Note: as for XEmacs 21.4 and 21.5, it is unavailable
1213           ;; unless sendmail.el is loaded.
1214           (if (boundp 'mail-default-headers)
1215               mail-default-headers))
1216   "*A string of header lines to be inserted in outgoing mails."
1217   :version "23.2"
1218   :group 'message-headers
1219   :group 'message-mail
1220   :link '(custom-manual "(message)Mail Headers")
1221   :type 'message-header-lines)
1222
1223 (defcustom message-default-news-headers ""
1224   "*A string of header lines to be inserted in outgoing news articles."
1225   :group 'message-headers
1226   :group 'message-news
1227   :link '(custom-manual "(message)News Headers")
1228   :type 'message-header-lines)
1229
1230 ;; Note: could use /usr/ucb/mail instead of sendmail;
1231 ;; options -t, and -v if not interactive.
1232 (defcustom message-mailer-swallows-blank-line
1233   (if (and (string-match "sparc-sun-sunos\\(\\'\\|[^5]\\)"
1234                          system-configuration)
1235            (file-readable-p "/etc/sendmail.cf")
1236            (with-temp-buffer
1237              (insert-file-contents "/etc/sendmail.cf")
1238              (goto-char (point-min))
1239              (let ((case-fold-search nil))
1240                (re-search-forward "^OR\\>" nil t))))
1241       ;; According to RFC822, "The field-name must be composed of printable
1242       ;; ASCII characters (i. e., characters that have decimal values between
1243       ;; 33 and 126, except colon)", i. e., any chars except ctl chars,
1244       ;; space, or colon.
1245       '(looking-at "[ \t]\\|[][!\"#$%&'()*+,-./0-9;<=>?@A-Z\\\\^_`a-z{|}~]+:"))
1246   "*Set this non-nil if the system's mailer runs the header and body together.
1247 \(This problem exists on Sunos 4 when sendmail is run in remote mode.)
1248 The value should be an expression to test whether the problem will
1249 actually occur."
1250   :group 'message-sending
1251   :link '(custom-manual "(message)Mail Variables")
1252   :type 'sexp)
1253
1254 ;;;###autoload
1255 (define-mail-user-agent 'message-user-agent
1256   'message-mail 'message-send-and-exit
1257   'message-kill-buffer 'message-send-hook)
1258
1259 (defvar message-mh-deletable-headers '(Message-ID Date Lines Sender)
1260   "If non-nil, delete the deletable headers before feeding to mh.")
1261
1262 (defvar message-send-method-alist
1263   '((news message-news-p message-send-via-news)
1264     (mail message-mail-p message-send-via-mail))
1265   "Alist of ways to send outgoing messages.
1266 Each element has the form
1267
1268   \(TYPE PREDICATE FUNCTION)
1269
1270 where TYPE is a symbol that names the method; PREDICATE is a function
1271 called without any parameters to determine whether the message is
1272 a message of type TYPE; and FUNCTION is a function to be called if
1273 PREDICATE returns non-nil.  FUNCTION is called with one parameter --
1274 the prefix.")
1275
1276 (defcustom message-mail-alias-type 'abbrev
1277   "*What alias expansion type to use in Message buffers.
1278 The default is `abbrev', which uses mailabbrev.  `ecomplete' uses
1279 an electric completion mode.  nil switches mail aliases off.
1280 This can also be a list of values."
1281   :group 'message
1282   :link '(custom-manual "(message)Mail Aliases")
1283   :type '(choice (const :tag "Use Mailabbrev" abbrev)
1284                  (const :tag "Use ecomplete" ecomplete)
1285                  (const :tag "No expansion" nil)))
1286
1287 (defcustom message-self-insert-commands '(self-insert-command)
1288   "List of `self-insert-command's used to trigger ecomplete.
1289 When one of those commands is invoked to enter a character in To or Cc
1290 header, ecomplete will suggest the candidates of recipients (see also
1291 `message-mail-alias-type').  If you use some tool to enter non-ASCII
1292 text and it replaces `self-insert-command' with the other command, e.g.
1293 `egg-self-insert-command', you may want to add it to this list."
1294   :group 'message-various
1295   :type '(repeat function))
1296
1297 (defcustom message-auto-save-directory
1298   (file-name-as-directory (expand-file-name "drafts" message-directory))
1299   "*Directory where Message auto-saves buffers if Gnus isn't running.
1300 If nil, Message won't auto-save."
1301   :group 'message-buffers
1302   :link '(custom-manual "(message)Various Message Variables")
1303   :type '(choice directory (const :tag "Don't auto-save" nil)))
1304
1305 (defcustom message-default-charset
1306   (and (not (mm-multibyte-p)) 'iso-8859-1)
1307   "Default charset used in non-MULE Emacsen.
1308 If nil, you might be asked to input the charset."
1309   :version "21.1"
1310   :group 'message
1311   :link '(custom-manual "(message)Various Message Variables")
1312   :type 'symbol)
1313
1314 (defcustom message-dont-reply-to-names
1315   (and (boundp 'rmail-dont-reply-to-names) rmail-dont-reply-to-names)
1316   "*Addresses to prune when doing wide replies.
1317 This can be a regexp or a list of regexps.  Also, a value of nil means
1318 exclude your own user name only."
1319   :version "21.1"
1320   :group 'message
1321   :link '(custom-manual "(message)Wide Reply")
1322   :type '(choice (const :tag "Yourself" nil)
1323                  regexp
1324                  (repeat :tag "Regexp List" regexp)))
1325
1326 (defsubst message-dont-reply-to-names ()
1327   (gmm-regexp-concat message-dont-reply-to-names))
1328
1329 (defvar message-shoot-gnksa-feet nil
1330   "*A list of GNKSA feet you are allowed to shoot.
1331 Gnus gives you all the opportunity you could possibly want for
1332 shooting yourself in the foot.  Also, Gnus allows you to shoot the
1333 feet of Good Net-Keeping Seal of Approval.  The following are foot
1334 candidates:
1335 `empty-article'     Allow you to post an empty article;
1336 `quoted-text-only'  Allow you to post quoted text only;
1337 `multiple-copies'   Allow you to post multiple copies;
1338 `cancel-messages'   Allow you to cancel or supersede messages from
1339                     your other email addresses.")
1340
1341 (defsubst message-gnksa-enable-p (feature)
1342   (or (not (listp message-shoot-gnksa-feet))
1343       (memq feature message-shoot-gnksa-feet)))
1344
1345 (defcustom message-hidden-headers '("^References:" "^Face:" "^X-Face:"
1346                                     "^X-Draft-From:")
1347   "Regexp of headers to be hidden when composing new messages.
1348 This can also be a list of regexps to match headers.  Or a list
1349 starting with `not' and followed by regexps."
1350   :version "22.1"
1351   :group 'message
1352   :link '(custom-manual "(message)Message Headers")
1353   :type '(choice
1354           :format "%{%t%}: %[Value Type%] %v"
1355           (regexp :menu-tag "regexp" :format "regexp\n%t: %v")
1356           (repeat :menu-tag "(regexp ...)" :format "(regexp ...)\n%v%i"
1357                   (regexp :format "%t: %v"))
1358           (cons :menu-tag "(not regexp ...)" :format "(not regexp ...)\n%v"
1359                 (const not)
1360                 (repeat :format "%v%i"
1361                         (regexp :format "%t: %v")))))
1362
1363 (defcustom message-cite-articles-with-x-no-archive t
1364   "If non-nil, cite text from articles that has X-No-Archive set."
1365   :group 'message
1366   :type 'boolean)
1367
1368 ;;; Internal variables.
1369 ;;; Well, not really internal.
1370
1371 (defvar message-mode-syntax-table
1372   (let ((table (copy-syntax-table text-mode-syntax-table)))
1373     (modify-syntax-entry ?% ". " table)
1374     (modify-syntax-entry ?> ". " table)
1375     (modify-syntax-entry ?< ". " table)
1376     table)
1377   "Syntax table used while in Message mode.")
1378
1379 (defface message-header-to
1380   '((((class color)
1381       (background dark))
1382      (:foreground "DarkOliveGreen1" :bold t))
1383     (((class color)
1384       (background light))
1385      (:foreground "MidnightBlue" :bold t))
1386     (t
1387      (:bold t :italic t)))
1388   "Face used for displaying From headers."
1389   :group 'message-faces)
1390 ;; backward-compatibility alias
1391 (put 'message-header-to-face 'face-alias 'message-header-to)
1392 (put 'message-header-to-face 'obsolete-face "22.1")
1393
1394 (defface message-header-cc
1395   '((((class color)
1396       (background dark))
1397      (:foreground "chartreuse1" :bold t))
1398     (((class color)
1399       (background light))
1400      (:foreground "MidnightBlue"))
1401     (t
1402      (:bold t)))
1403   "Face used for displaying Cc headers."
1404   :group 'message-faces)
1405 ;; backward-compatibility alias
1406 (put 'message-header-cc-face 'face-alias 'message-header-cc)
1407 (put 'message-header-cc-face 'obsolete-face "22.1")
1408
1409 (defface message-header-subject
1410   '((((class color)
1411       (background dark))
1412      (:foreground "OliveDrab1"))
1413     (((class color)
1414       (background light))
1415      (:foreground "navy blue" :bold t))
1416     (t
1417      (:bold t)))
1418   "Face used for displaying subject headers."
1419   :group 'message-faces)
1420 ;; backward-compatibility alias
1421 (put 'message-header-subject-face 'face-alias 'message-header-subject)
1422 (put 'message-header-subject-face 'obsolete-face "22.1")
1423
1424 (defface message-header-newsgroups
1425   '((((class color)
1426       (background dark))
1427      (:foreground "yellow" :bold t :italic t))
1428     (((class color)
1429       (background light))
1430      (:foreground "blue4" :bold t :italic t))
1431     (t
1432      (:bold t :italic t)))
1433   "Face used for displaying newsgroups headers."
1434   :group 'message-faces)
1435 ;; backward-compatibility alias
1436 (put 'message-header-newsgroups-face 'face-alias 'message-header-newsgroups)
1437 (put 'message-header-newsgroups-face 'obsolete-face "22.1")
1438
1439 (defface message-header-other
1440   '((((class color)
1441       (background dark))
1442      (:foreground "VioletRed1"))
1443     (((class color)
1444       (background light))
1445      (:foreground "steel blue"))
1446     (t
1447      (:bold t :italic t)))
1448   "Face used for displaying newsgroups headers."
1449   :group 'message-faces)
1450 ;; backward-compatibility alias
1451 (put 'message-header-other-face 'face-alias 'message-header-other)
1452 (put 'message-header-other-face 'obsolete-face "22.1")
1453
1454 (defface message-header-name
1455   '((((class color)
1456       (background dark))
1457      (:foreground "green"))
1458     (((class color)
1459       (background light))
1460      (:foreground "cornflower blue"))
1461     (t
1462      (:bold t)))
1463   "Face used for displaying header names."
1464   :group 'message-faces)
1465 ;; backward-compatibility alias
1466 (put 'message-header-name-face 'face-alias 'message-header-name)
1467 (put 'message-header-name-face 'obsolete-face "22.1")
1468
1469 (defface message-header-xheader
1470   '((((class color)
1471       (background dark))
1472      (:foreground "DeepSkyBlue1"))
1473     (((class color)
1474       (background light))
1475      (:foreground "blue"))
1476     (t
1477      (:bold t)))
1478   "Face used for displaying X-Header headers."
1479   :group 'message-faces)
1480 ;; backward-compatibility alias
1481 (put 'message-header-xheader-face 'face-alias 'message-header-xheader)
1482 (put 'message-header-xheader-face 'obsolete-face "22.1")
1483
1484 (defface message-separator
1485   '((((class color)
1486       (background dark))
1487      (:foreground "LightSkyBlue1"))
1488     (((class color)
1489       (background light))
1490      (:foreground "brown"))
1491     (t
1492      (:bold t)))
1493   "Face used for displaying the separator."
1494   :group 'message-faces)
1495 ;; backward-compatibility alias
1496 (put 'message-separator-face 'face-alias 'message-separator)
1497 (put 'message-separator-face 'obsolete-face "22.1")
1498
1499 (defface message-cited-text
1500   '((((class color)
1501       (background dark))
1502      (:foreground "LightPink1"))
1503     (((class color)
1504       (background light))
1505      (:foreground "red"))
1506     (t
1507      (:bold t)))
1508   "Face used for displaying cited text names."
1509   :group 'message-faces)
1510 ;; backward-compatibility alias
1511 (put 'message-cited-text-face 'face-alias 'message-cited-text)
1512 (put 'message-cited-text-face 'obsolete-face "22.1")
1513
1514 (defface message-mml
1515   '((((class color)
1516       (background dark))
1517      (:foreground "MediumSpringGreen"))
1518     (((class color)
1519       (background light))
1520      (:foreground "ForestGreen"))
1521     (t
1522      (:bold t)))
1523   "Face used for displaying MML."
1524   :group 'message-faces)
1525 ;; backward-compatibility alias
1526 (put 'message-mml-face 'face-alias 'message-mml)
1527 (put 'message-mml-face 'obsolete-face "22.1")
1528
1529 (defun message-font-lock-make-header-matcher (regexp)
1530   (let ((form
1531          `(lambda (limit)
1532             (let ((start (point)))
1533               (save-restriction
1534                 (widen)
1535                 (goto-char (point-min))
1536                 (if (re-search-forward
1537                      (concat "^" (regexp-quote mail-header-separator) "$")
1538                      nil t)
1539                     (setq limit (min limit (match-beginning 0))))
1540                 (goto-char start))
1541               (and (< start limit)
1542                    (re-search-forward ,regexp limit t))))))
1543     (if (featurep 'bytecomp)
1544         (byte-compile form)
1545       form)))
1546
1547 (defvar message-font-lock-keywords
1548   (let ((content "[ \t]*\\(.+\\(\n[ \t].*\\)*\\)\n?"))
1549     `((,(message-font-lock-make-header-matcher
1550          (concat "^\\([Tt]o:\\)" content))
1551        (1 'message-header-name)
1552        (2 'message-header-to nil t))
1553       (,(message-font-lock-make-header-matcher
1554          (concat "^\\(^[GBF]?[Cc][Cc]:\\|^[Rr]eply-[Tt]o:\\)" content))
1555        (1 'message-header-name)
1556        (2 'message-header-cc nil t))
1557       (,(message-font-lock-make-header-matcher
1558          (concat "^\\([Ss]ubject:\\)" content))
1559        (1 'message-header-name)
1560        (2 'message-header-subject nil t))
1561       (,(message-font-lock-make-header-matcher
1562          (concat "^\\([Nn]ewsgroups:\\|Followup-[Tt]o:\\)" content))
1563        (1 'message-header-name)
1564        (2 'message-header-newsgroups nil t))
1565       (,(message-font-lock-make-header-matcher
1566          (concat "^\\(X-[A-Za-z0-9-]+:\\|In-Reply-To:\\)" content))
1567        (1 'message-header-name)
1568        (2 'message-header-xheader))
1569       (,(message-font-lock-make-header-matcher
1570          (concat "^\\([A-Z][^: \n\t]+:\\)" content))
1571        (1 'message-header-name)
1572        (2 'message-header-other nil t))
1573       ,@(if (and mail-header-separator
1574                  (not (equal mail-header-separator "")))
1575             `((,(concat "^\\(" (regexp-quote mail-header-separator) "\\)$")
1576                1 'message-separator))
1577           nil)
1578       ((lambda (limit)
1579          (re-search-forward (concat "^\\("
1580                                     message-cite-prefix-regexp
1581                                     "\\).*")
1582                             limit t))
1583        (0 'message-cited-text))
1584       ("<#/?\\(multipart\\|part\\|external\\|mml\\|secure\\)[^>]*>"
1585        (0 'message-mml))))
1586   "Additional expressions to highlight in Message mode.")
1587
1588
1589 ;; XEmacs does it like this.  For Emacs, we have to set the
1590 ;; `font-lock-defaults' buffer-local variable.
1591 (put 'message-mode 'font-lock-defaults '(message-font-lock-keywords t))
1592
1593 (defvar message-face-alist
1594   '((bold . message-bold-region)
1595     (underline . underline-region)
1596     (default . (lambda (b e)
1597                  (message-unbold-region b e)
1598                  (ununderline-region b e))))
1599   "Alist of mail and news faces for facemenu.
1600 The cdr of each entry is a function for applying the face to a region.")
1601
1602 (defcustom message-send-hook nil
1603   "Hook run before sending messages.
1604 This hook is run quite early when sending."
1605   :group 'message-various
1606   :options '(ispell-message)
1607   :link '(custom-manual "(message)Various Message Variables")
1608   :type 'hook)
1609
1610 (defcustom message-send-mail-hook nil
1611   "Hook run before sending mail messages.
1612 This hook is run very late -- just before the message is sent as
1613 mail."
1614   :group 'message-various
1615   :link '(custom-manual "(message)Various Message Variables")
1616   :type 'hook)
1617
1618 (defcustom message-send-news-hook nil
1619   "Hook run before sending news messages.
1620 This hook is run very late -- just before the message is sent as
1621 news."
1622   :group 'message-various
1623   :link '(custom-manual "(message)Various Message Variables")
1624   :type 'hook)
1625
1626 (defcustom message-sent-hook nil
1627   "Hook run after sending messages."
1628   :group 'message-various
1629   :type 'hook)
1630
1631 (defvar message-send-coding-system 'binary
1632   "Coding system to encode outgoing mail.")
1633
1634 (defvar message-draft-coding-system
1635   mm-auto-save-coding-system
1636   "*Coding system to compose mail.
1637 If you'd like to make it possible to share draft files between XEmacs
1638 and Emacs, you may use `iso-2022-7bit' for this value at your own risk.
1639 Note that the coding-system `iso-2022-7bit' isn't suitable to all data.")
1640
1641 (defcustom message-send-mail-partially-limit nil
1642   "The limitation of messages sent as message/partial.
1643 The lower bound of message size in characters, beyond which the message
1644 should be sent in several parts.  If it is nil, the size is unlimited."
1645   :version "24.1"
1646   :group 'message-buffers
1647   :link '(custom-manual "(message)Mail Variables")
1648   :type '(choice (const :tag "unlimited" nil)
1649                  (integer 1000000)))
1650
1651 (defcustom message-alternative-emails nil
1652   "*Regexp matching alternative email addresses.
1653 The first address in the To, Cc or From headers of the original
1654 article matching this variable is used as the From field of
1655 outgoing messages.
1656
1657 This variable has precedence over posting styles and anything that runs
1658 off `message-setup-hook'."
1659   :group 'message-headers
1660   :link '(custom-manual "(message)Message Headers")
1661   :type '(choice (const :tag "Always use primary" nil)
1662                  regexp))
1663
1664 (defcustom message-hierarchical-addresses nil
1665   "A list of hierarchical mail address definitions.
1666
1667 Inside each entry, the first address is the \"top\" address, and
1668 subsequent addresses are subaddresses; this is used to indicate that
1669 mail sent to the first address will automatically be delivered to the
1670 subaddresses.  So if the first address appears in the recipient list
1671 for a message, the subaddresses will be removed (if present) before
1672 the mail is sent.  All addresses in this structure should be
1673 downcased."
1674   :version "22.1"
1675   :group 'message-headers
1676   :type '(repeat (repeat string)))
1677
1678 (defcustom message-mail-user-agent nil
1679   "Like `mail-user-agent'.
1680 Except if it is nil, use Gnus native MUA; if it is t, use
1681 `mail-user-agent'."
1682   :version "22.1"
1683   :type '(radio (const :tag "Gnus native"
1684                        :format "%t\n"
1685                        nil)
1686                 (const :tag "`mail-user-agent'"
1687                        :format "%t\n"
1688                        t)
1689                 (function-item :tag "Default Emacs mail"
1690                                :format "%t\n"
1691                                sendmail-user-agent)
1692                 (function-item :tag "Emacs interface to MH"
1693                                :format "%t\n"
1694                                mh-e-user-agent)
1695                 (function :tag "Other"))
1696   :version "21.1"
1697   :group 'message)
1698
1699 (defcustom message-wide-reply-confirm-recipients nil
1700   "Whether to confirm a wide reply to multiple email recipients.
1701 If this variable is nil, don't ask whether to reply to all recipients.
1702 If this variable is non-nil, pose the question \"Reply to all
1703 recipients?\" before a wide reply to multiple recipients.  If the user
1704 answers yes, reply to all recipients as usual.  If the user answers
1705 no, only reply back to the author."
1706   :version "22.1"
1707   :group 'message-headers
1708   :link '(custom-manual "(message)Wide Reply")
1709   :type 'boolean)
1710
1711 (defcustom message-user-fqdn nil
1712   "*Domain part of Message-Ids."
1713   :version "22.1"
1714   :group 'message-headers
1715   :link '(custom-manual "(message)News Headers")
1716   :type '(radio (const :format "%v  " nil)
1717                 (string :format "FQDN: %v")))
1718
1719 (defcustom message-use-idna (and (condition-case nil (require 'idna)
1720                                    (file-error))
1721                                  (mm-coding-system-p 'utf-8)
1722                                  (executable-find idna-program)
1723                                  (string= (idna-to-ascii "räksmörgÃ¥s")
1724                                           "xn--rksmrgs-5wao1o")
1725                                  t)
1726   "Whether to encode non-ASCII in domain names into ASCII according to IDNA.
1727 GNU Libidn, and in particular the elisp package \"idna.el\" and
1728 the external program \"idn\", must be installed for this
1729 functionality to work."
1730   :version "22.1"
1731   :group 'message-headers
1732   :link '(custom-manual "(message)IDNA")
1733   :type '(choice (const :tag "Ask" ask)
1734                  (const :tag "Never" nil)
1735                  (const :tag "Always" t)))
1736
1737 (defcustom message-generate-hashcash (if (executable-find "hashcash") 'opportunistic)
1738   "*Whether to generate X-Hashcash: headers.
1739 If t, always generate hashcash headers.  If `opportunistic',
1740 only generate hashcash headers if it can be done without the user
1741 waiting (i.e., only asynchronously).
1742
1743 You must have the \"hashcash\" binary installed, see `hashcash-path'."
1744   :version "24.1"
1745   :group 'message-headers
1746   :link '(custom-manual "(message)Mail Headers")
1747   :type '(choice (const :tag "Always" t)
1748                  (const :tag "Never" nil)
1749                  (const :tag "Opportunistic" opportunistic)))
1750
1751 ;;; Internal variables.
1752
1753 (defvar message-sending-message "Sending...")
1754 (defvar message-buffer-list nil)
1755 (defvar message-this-is-news nil)
1756 (defvar message-this-is-mail nil)
1757 (defvar message-draft-article nil)
1758 (defvar message-mime-part nil)
1759 (defvar message-posting-charset nil)
1760 (defvar message-inserted-headers nil)
1761 (defvar message-inhibit-ecomplete nil)
1762
1763 ;; Byte-compiler warning
1764 (defvar gnus-active-hashtb)
1765 (defvar gnus-read-active-file)
1766
1767 ;;; Regexp matching the delimiter of messages in UNIX mail format
1768 ;;; (UNIX From lines), minus the initial ^.  It should be a copy
1769 ;;; of rmail.el's rmail-unix-mail-delimiter.
1770 (defvar message-unix-mail-delimiter
1771   (let ((time-zone-regexp
1772          (concat "\\([A-Z]?[A-Z]?[A-Z][A-Z]\\( DST\\)?"
1773                  "\\|[-+]?[0-9][0-9][0-9][0-9]"
1774                  "\\|"
1775                  "\\) *")))
1776     (concat
1777      "From "
1778
1779      ;; Many things can happen to an RFC 822 mailbox before it is put into
1780      ;; a `From' line.  The leading phrase can be stripped, e.g.
1781      ;; `Joe <@w.x:joe@y.z>' -> `<@w.x:joe@y.z>'.  The <> can be stripped, e.g.
1782      ;; `<@x.y:joe@y.z>' -> `@x.y:joe@y.z'.  Everything starting with a CRLF
1783      ;; can be removed, e.g.
1784      ;;         From: joe@y.z (Joe      K
1785      ;;                 User)
1786      ;; can yield `From joe@y.z (Joe    K Fri Mar 22 08:11:15 1996', and
1787      ;;         From: Joe User
1788      ;;                 <joe@y.z>
1789      ;; can yield `From Joe User Fri Mar 22 08:11:15 1996'.
1790      ;; The mailbox can be removed or be replaced by white space, e.g.
1791      ;;         From: "Joe User"{space}{tab}
1792      ;;                 <joe@y.z>
1793      ;; can yield `From {space}{tab} Fri Mar 22 08:11:15 1996',
1794      ;; where {space} and {tab} represent the Ascii space and tab characters.
1795      ;; We want to match the results of any of these manglings.
1796      ;; The following regexp rejects names whose first characters are
1797      ;; obviously bogus, but after that anything goes.
1798      "\\([^\0-\b\n-\r\^?].*\\)?"
1799
1800      ;; The time the message was sent.
1801      "\\([^\0-\r \^?]+\\) +"            ; day of the week
1802      "\\([^\0-\r \^?]+\\) +"            ; month
1803      "\\([0-3]?[0-9]\\) +"              ; day of month
1804      "\\([0-2][0-9]:[0-5][0-9]\\(:[0-6][0-9]\\)?\\) *" ; time of day
1805
1806      ;; Perhaps a time zone, specified by an abbreviation, or by a
1807      ;; numeric offset.
1808      time-zone-regexp
1809
1810      ;; The year.
1811      " \\([0-9][0-9]+\\) *"
1812
1813      ;; On some systems the time zone can appear after the year, too.
1814      time-zone-regexp
1815
1816      ;; Old uucp cruft.
1817      "\\(remote from .*\\)?"
1818
1819      "\n"))
1820   "Regexp matching the delimiter of messages in UNIX mail format.")
1821
1822 (defvar message-unsent-separator
1823   (concat "^ *---+ +Unsent message follows +---+ *$\\|"
1824           "^ *---+ +Returned message +---+ *$\\|"
1825           "^Start of returned message$\\|"
1826           "^ *---+ +Original message +---+ *$\\|"
1827           "^ *--+ +begin message +--+ *$\\|"
1828           "^ *---+ +Original message follows +---+ *$\\|"
1829           "^ *---+ +Undelivered message follows +---+ *$\\|"
1830           "^------ This is a copy of the message, including all the headers. ------ *$\\|"
1831           "^|? *---+ +Message text follows: +---+ *|?$")
1832   "A regexp that matches the separator before the text of a failed message.")
1833
1834 (defvar message-field-fillers
1835   '((To message-fill-field-address)
1836     (Cc message-fill-field-address)
1837     (From message-fill-field-address))
1838   "Alist of header names/filler functions.")
1839
1840 (defvar message-header-format-alist
1841   `((From)
1842     (Newsgroups)
1843     (To)
1844     (Cc)
1845     (Subject)
1846     (In-Reply-To)
1847     (Fcc)
1848     (Bcc)
1849     (Date)
1850     (Organization)
1851     (Distribution)
1852     (Lines)
1853     (Expires)
1854     (Message-ID)
1855     (References . message-shorten-references)
1856     (User-Agent))
1857   "Alist used for formatting headers.")
1858
1859 (defvar message-options nil
1860   "Some saved answers when sending message.")
1861
1862 (defvar message-send-mail-real-function nil
1863   "Internal send mail function.")
1864
1865 (defvar message-bogus-system-names "^localhost\\.\\|\\.local$"
1866   "The regexp of bogus system names.")
1867
1868 (defcustom message-valid-fqdn-regexp
1869   (concat "[a-z0-9][-.a-z0-9]+\\." ;; [hostname.subdomain.]domain.
1870           ;; valid TLDs:
1871           "\\([a-z][a-z]\\|" ;; two letter country TDLs
1872           "aero\\|arpa\\|bitnet\\|biz\\|bofh\\|"
1873           "cat\\|com\\|coop\\|edu\\|gov\\|"
1874           "info\\|int\\|jobs\\|"
1875           "mil\\|mobi\\|museum\\|name\\|net\\|"
1876           "org\\|pro\\|travel\\|uucp\\)")
1877   ;; http://en.wikipedia.org/wiki/List_of_Internet_top-level_domains
1878   ;; http://en.wikipedia.org/wiki/GTLD
1879   ;; `in the process of being approved': .asia .post .tel .sex
1880   ;; "dead" nato bitnet uucp
1881   "Regular expression that matches a valid FQDN."
1882   ;; see also: gnus-button-valid-fqdn-regexp
1883   :version "22.1"
1884   :group 'message-headers
1885   :type 'regexp)
1886
1887 (autoload 'gnus-alive-p "gnus-util")
1888 (autoload 'gnus-delay-article "gnus-delay")
1889 (autoload 'gnus-extract-address-components "gnus-util")
1890 (autoload 'gnus-find-method-for-group "gnus")
1891 (autoload 'gnus-group-decoded-name "gnus-group")
1892 (autoload 'gnus-group-name-charset "gnus-group")
1893 (autoload 'gnus-group-name-decode "gnus-group")
1894 (autoload 'gnus-groups-from-server "gnus")
1895 (autoload 'gnus-make-local-hook "gnus-util")
1896 (autoload 'gnus-open-server "gnus-int")
1897 (autoload 'gnus-output-to-mail "gnus-util")
1898 (autoload 'gnus-output-to-rmail "gnus-util")
1899 (autoload 'gnus-request-post "gnus-int")
1900 (autoload 'gnus-select-frame-set-input-focus "gnus-util")
1901 (autoload 'gnus-server-string "gnus")
1902 (autoload 'idna-to-ascii "idna")
1903 (autoload 'message-setup-toolbar "messagexmas")
1904 (autoload 'mh-new-draft-name "mh-comp")
1905 (autoload 'mh-send-letter "mh-comp")
1906 (autoload 'nndraft-request-associate-buffer "nndraft")
1907 (autoload 'nndraft-request-expire-articles "nndraft")
1908 (autoload 'nnvirtual-find-group-art "nnvirtual")
1909 (autoload 'rmail-dont-reply-to "mail-utils")
1910 (autoload 'rmail-msg-is-pruned "rmail")
1911 (autoload 'rmail-output "rmailout")
1912
1913 \f
1914
1915 ;;;
1916 ;;; Utility functions.
1917 ;;;
1918
1919 (defmacro message-y-or-n-p (question show &rest text)
1920   "Ask QUESTION, displaying remaining args in a temporary buffer if SHOW."
1921   `(message-talkative-question 'y-or-n-p ,question ,show ,@text))
1922
1923 (defmacro message-delete-line (&optional n)
1924   "Delete the current line (and the next N lines)."
1925   `(delete-region (progn (beginning-of-line) (point))
1926                   (progn (forward-line ,(or n 1)) (point))))
1927
1928 (defun message-mark-active-p ()
1929   "Non-nil means the mark and region are currently active in this buffer."
1930   mark-active)
1931
1932 (defun message-unquote-tokens (elems)
1933   "Remove double quotes (\") from strings in list ELEMS."
1934   (mapcar (lambda (item)
1935             (while (string-match "^\\(.*\\)\"\\(.*\\)$" item)
1936               (setq item (concat (match-string 1 item)
1937                                  (match-string 2 item))))
1938             item)
1939           elems))
1940
1941 (defun message-tokenize-header (header &optional separator)
1942   "Split HEADER into a list of header elements.
1943 SEPARATOR is a string of characters to be used as separators.  \",\"
1944 is used by default."
1945   (if (not header)
1946       nil
1947     (let ((regexp (format "[%s]+" (or separator ",")))
1948           (first t)
1949           beg quoted elems paren)
1950       (with-temp-buffer
1951         (mm-enable-multibyte)
1952         (setq beg (point-min))
1953         (insert header)
1954         (goto-char (point-min))
1955         (while (not (eobp))
1956           (if first
1957               (setq first nil)
1958             (forward-char 1))
1959           (cond ((and (> (point) beg)
1960                       (or (eobp)
1961                           (and (looking-at regexp)
1962                                (not quoted)
1963                                (not paren))))
1964                  (push (buffer-substring beg (point)) elems)
1965                  (setq beg (match-end 0)))
1966                 ((eq (char-after) ?\")
1967                  (setq quoted (not quoted)))
1968                 ((and (eq (char-after) ?\()
1969                       (not quoted))
1970                  (setq paren t))
1971                 ((and (eq (char-after) ?\))
1972                       (not quoted))
1973                  (setq paren nil))))
1974         (nreverse elems)))))
1975
1976 (autoload 'nnheader-insert-file-contents "nnheader")
1977
1978 (defun message-mail-file-mbox-p (file)
1979   "Say whether FILE looks like a Unix mbox file."
1980   (when (and (file-exists-p file)
1981              (file-readable-p file)
1982              (file-regular-p file))
1983     (with-temp-buffer
1984       (nnheader-insert-file-contents file)
1985       (goto-char (point-min))
1986       (looking-at message-unix-mail-delimiter))))
1987
1988 (defun message-fetch-field (header &optional not-all)
1989   "The same as `mail-fetch-field', only remove all newlines.
1990 The buffer is expected to be narrowed to just the header of the message;
1991 see `message-narrow-to-headers-or-head'."
1992   (let* ((inhibit-point-motion-hooks t)
1993          (value (mail-fetch-field header nil (not not-all))))
1994     (when value
1995       (while (string-match "\n[\t ]+" value)
1996         (setq value (replace-match " " t t value)))
1997       value)))
1998
1999 (defun message-field-value (header &optional not-all)
2000   "The same as `message-fetch-field', only narrow to the headers first."
2001   (save-excursion
2002     (save-restriction
2003       (message-narrow-to-headers-or-head)
2004       (message-fetch-field header not-all))))
2005
2006 (defun message-narrow-to-field ()
2007   "Narrow the buffer to the header on the current line."
2008   (beginning-of-line)
2009   (while (looking-at "[ \t]")
2010     (forward-line -1))
2011   (narrow-to-region
2012    (point)
2013    (progn
2014      (forward-line 1)
2015      (if (re-search-forward "^[^ \n\t]" nil t)
2016          (point-at-bol)
2017        (point-max))))
2018   (goto-char (point-min)))
2019
2020 (defun message-add-header (&rest headers)
2021   "Add the HEADERS to the message header, skipping those already present."
2022   (while headers
2023     (let (hclean)
2024       (unless (string-match "^\\([^:]+\\):[ \t]*[^ \t]" (car headers))
2025         (error "Invalid header `%s'" (car headers)))
2026       (setq hclean (match-string 1 (car headers)))
2027       (save-restriction
2028         (message-narrow-to-headers)
2029         (unless (re-search-forward (concat "^" (regexp-quote hclean) ":") nil t)
2030           (goto-char (point-max))
2031           (if (string-match "\n$" (car headers))
2032               (insert (car headers))
2033             (insert (car headers) ?\n)))))
2034     (setq headers (cdr headers))))
2035
2036 (defmacro message-with-reply-buffer (&rest forms)
2037   "Evaluate FORMS in the reply buffer, if it exists."
2038   `(when (and (bufferp message-reply-buffer)
2039               (buffer-name message-reply-buffer))
2040      (with-current-buffer message-reply-buffer
2041        ,@forms)))
2042
2043 (put 'message-with-reply-buffer 'lisp-indent-function 0)
2044 (put 'message-with-reply-buffer 'edebug-form-spec '(body))
2045
2046 (defun message-fetch-reply-field (header)
2047   "Fetch field HEADER from the message we're replying to."
2048   (message-with-reply-buffer
2049     (save-restriction
2050       (mail-narrow-to-head)
2051       (message-fetch-field header))))
2052
2053 (defun message-strip-list-identifiers (subject)
2054   "Remove list identifiers in `gnus-list-identifiers' from string SUBJECT."
2055   (require 'gnus-sum)                   ; for gnus-list-identifiers
2056   (let ((regexp (if (stringp gnus-list-identifiers)
2057                     gnus-list-identifiers
2058                   (mapconcat 'identity gnus-list-identifiers " *\\|"))))
2059     (if (string-match (concat "\\(\\(\\(Re: +\\)?\\(" regexp
2060                               " *\\)\\)+\\(Re: +\\)?\\)") subject)
2061         (concat (substring subject 0 (match-beginning 1))
2062                 (or (match-string 3 subject)
2063                     (match-string 5 subject))
2064                 (substring subject
2065                            (match-end 1)))
2066       subject)))
2067
2068 (defun message-strip-subject-re (subject)
2069   "Remove \"Re:\" from subject lines in string SUBJECT."
2070   (if (string-match message-subject-re-regexp subject)
2071       (substring subject (match-end 0))
2072     subject))
2073
2074 (defcustom message-replacement-char "."
2075   "Replacement character used instead of unprintable or not decodable chars."
2076   :group 'message-various
2077   :version "22.1" ;; Gnus 5.10.9
2078   :type '(choice string
2079                  (const ".")
2080                  (const "?")))
2081
2082 ;; FIXME: We also should call `message-strip-subject-encoded-words'
2083 ;; when forwarding.  Probably in `message-make-forward-subject' and
2084 ;; `message-forward-make-body'.
2085
2086 (defun message-strip-subject-encoded-words (subject)
2087   "Fix non-decodable words in SUBJECT."
2088   ;; Cf. `gnus-simplify-subject-fully'.
2089   (let* ((case-fold-search t)
2090          (replacement-chars (format "[%s%s%s]"
2091                                     message-replacement-char
2092                                     message-replacement-char
2093                                     message-replacement-char))
2094          (enc-word-re "=\\?\\([^?]+\\)\\?\\([QB]\\)\\?\\([^?]+\\)\\(\\?=\\)")
2095          cs-string
2096          (have-marker
2097           (with-temp-buffer
2098             (insert subject)
2099             (goto-char (point-min))
2100             (when (re-search-forward enc-word-re nil t)
2101               (setq cs-string (match-string 1)))))
2102          cs-coding q-or-b word-beg word-end)
2103     (if (or (not have-marker) ;; No encoded word found...
2104             ;; ... or double encoding was correct:
2105             (and (stringp cs-string)
2106                  (setq cs-string (downcase cs-string))
2107                  (mm-coding-system-p (intern cs-string))
2108                  (not (prog1
2109                           (y-or-n-p
2110                            (format "\
2111 Decoded Subject \"%s\"
2112 contains a valid encoded word.  Decode again? "
2113                                    subject))
2114                         (setq cs-coding (intern cs-string))))))
2115         subject
2116       (with-temp-buffer
2117         (insert subject)
2118         (goto-char (point-min))
2119         (while (re-search-forward enc-word-re nil t)
2120           (setq cs-string (downcase (match-string 1))
2121                 q-or-b    (match-string 2)
2122                 word-beg (match-beginning 0)
2123                 word-end (match-end 0))
2124           (setq cs-coding
2125                 (if (mm-coding-system-p (intern cs-string))
2126                     (setq cs-coding (intern cs-string))
2127                   nil))
2128           ;; No double encoded subject? => bogus charset.
2129           (unless cs-coding
2130             (setq cs-coding
2131                   (mm-read-coding-system
2132                    (format "\
2133 Decoded Subject \"%s\"
2134 contains an encoded word.  The charset `%s' is unknown or invalid.
2135 Hit RET to replace non-decodable characters with \"%s\" or enter replacement
2136 charset: "
2137                            subject cs-string message-replacement-char)))
2138             (if cs-coding
2139                 (replace-match (concat "=?" (symbol-name cs-coding)
2140                                        "?\\2?\\3\\4\\5"))
2141               (save-excursion
2142                 (goto-char word-beg)
2143                 (re-search-forward "=\\?\\([^?]+\\)\\?\\([QB]\\)\\?" word-end t)
2144                 (replace-match "")
2145                 ;; QP or base64
2146                 (if (string-match "\\`Q\\'" q-or-b)
2147                     ;; QP
2148                     (progn
2149                       (message "Replacing non-decodable characters with \"%s\"."
2150                                message-replacement-char)
2151                       (while (re-search-forward "\\(=[A-F0-9][A-F0-9]\\)+"
2152                                                 word-end t)
2153                         (replace-match message-replacement-char)))
2154                   ;; base64
2155                   (message "Replacing non-decodable characters with \"%s\"."
2156                            replacement-chars)
2157                   (re-search-forward "[^?]+" word-end t)
2158                   (replace-match replacement-chars))
2159                 (re-search-forward "\\?=")
2160                 (replace-match "")))))
2161         (rfc2047-decode-region (point-min) (point-max))
2162         (buffer-string)))))
2163
2164 ;;; Start of functions adopted from `message-utils.el'.
2165
2166 (defun message-strip-subject-trailing-was (subject)
2167   "Remove trailing \"(was: <old subject>)\" from SUBJECT lines.
2168 Leading \"Re: \" is not stripped by this function.  Use the function
2169 `message-strip-subject-re' for this."
2170   (let* ((query message-subject-trailing-was-query)
2171          (new) (found))
2172     (setq found
2173           (string-match
2174            (if (eq query 'ask)
2175                message-subject-trailing-was-ask-regexp
2176              message-subject-trailing-was-regexp)
2177            subject))
2178     (if found
2179         (setq new (substring subject 0 (match-beginning 0))))
2180     (if (or (not found) (eq query nil))
2181         subject
2182       (if (eq query 'ask)
2183           (if (message-y-or-n-p
2184                "Strip `(was: <old subject>)' in subject? " t
2185                (concat
2186                 "Strip `(was: <old subject>)' in subject "
2187                 "and use the new one instead?\n\n"
2188                 "Current subject is:   \""
2189                 subject "\"\n\n"
2190                 "New subject would be: \""
2191                 new "\"\n\n"
2192                 "See the variable `message-subject-trailing-was-query' "
2193                 "to get rid of this query."
2194                 ))
2195               new subject)
2196         new))))
2197
2198 ;;; Suggested by Jonas Steverud  @  www.dtek.chalmers.se/~d4jonas/
2199
2200 (defun message-change-subject (new-subject)
2201   "Ask for NEW-SUBJECT header, append (was: <Old Subject>)."
2202   (interactive
2203    (list
2204     (read-from-minibuffer "New subject: ")))
2205   (cond ((and (not (or (null new-subject) ; new subject not empty
2206                        (zerop (string-width new-subject))
2207                        (string-match "^[ \t]*$" new-subject))))
2208          (save-excursion
2209            (let ((old-subject
2210                   (save-restriction
2211                     (message-narrow-to-headers)
2212                     (message-fetch-field "Subject"))))
2213              (cond ((not old-subject)
2214                     (error "No current subject"))
2215                    ((not (string-match
2216                           (concat "^[ \t]*"
2217                                   (regexp-quote new-subject)
2218                                   " \t]*$")
2219                           old-subject))  ; yes, it really is a new subject
2220                     ;; delete eventual Re: prefix
2221                     (setq old-subject
2222                           (message-strip-subject-re old-subject))
2223                     (message-goto-subject)
2224                     (message-delete-line)
2225                     (insert (concat "Subject: "
2226                                     new-subject
2227                                     " (was: "
2228                                     old-subject ")\n")))))))))
2229
2230 (defun message-mark-inserted-region (beg end &optional verbatim)
2231   "Mark some region in the current article with enclosing tags.
2232 See `message-mark-insert-begin' and `message-mark-insert-end'.
2233 If VERBATIM, use slrn style verbatim marks (\"#v+\" and \"#v-\")."
2234   (interactive "r\nP")
2235   (save-excursion
2236     ;; add to the end of the region first, otherwise end would be invalid
2237     (goto-char end)
2238     (insert (if verbatim "#v-\n" message-mark-insert-end))
2239     (goto-char beg)
2240     (insert (if verbatim "#v+\n" message-mark-insert-begin))))
2241
2242 (defun message-mark-insert-file (file &optional verbatim)
2243   "Insert FILE at point, marking it with enclosing tags.
2244 See `message-mark-insert-begin' and `message-mark-insert-end'.
2245 If VERBATIM, use slrn style verbatim marks (\"#v+\" and \"#v-\")."
2246   (interactive "fFile to insert: \nP")
2247     ;; reverse insertion to get correct result.
2248   (let ((p (point)))
2249     (insert (if verbatim "#v-\n" message-mark-insert-end))
2250     (goto-char p)
2251     (insert-file-contents file)
2252     (goto-char p)
2253     (insert (if verbatim "#v+\n" message-mark-insert-begin))))
2254
2255 (defun message-add-archive-header ()
2256   "Insert \"X-No-Archive: Yes\" in the header and a note in the body.
2257 The note can be customized using `message-archive-note'.  When called with a
2258 prefix argument, ask for a text to insert.  If you don't want the note in the
2259 body, set  `message-archive-note' to nil."
2260   (interactive)
2261   (if current-prefix-arg
2262       (setq message-archive-note
2263             (read-from-minibuffer "Reason for No-Archive: "
2264                                   (cons message-archive-note 0))))
2265     (save-excursion
2266       (if (message-goto-signature)
2267           (re-search-backward message-signature-separator))
2268       (when message-archive-note
2269         (insert message-archive-note)
2270         (newline))
2271       (message-add-header message-archive-header)
2272       (message-sort-headers)))
2273
2274 (defun message-cross-post-followup-to-header (target-group)
2275   "Mangles FollowUp-To and Newsgroups header to point to TARGET-GROUP.
2276 With prefix-argument just set Follow-Up, don't cross-post."
2277   (interactive
2278    (list ; Completion based on Gnus
2279     (completing-read "Followup To: "
2280                      (if (boundp 'gnus-newsrc-alist)
2281                          gnus-newsrc-alist)
2282                      nil nil '("poster" . 0)
2283                      (if (boundp 'gnus-group-history)
2284                          'gnus-group-history))))
2285   (message-remove-header "Follow[Uu]p-[Tt]o" t)
2286   (message-goto-newsgroups)
2287   (beginning-of-line)
2288   ;; if we already did a crosspost before, kill old target
2289   (if (and message-cross-post-old-target
2290            (re-search-forward
2291             (regexp-quote (concat "," message-cross-post-old-target))
2292             nil t))
2293       (replace-match ""))
2294   ;; unless (followup is to poster or user explicitly asked not
2295   ;; to cross-post, or target-group is already in Newsgroups)
2296   ;; add target-group to Newsgroups line.
2297   (cond ((and (or
2298                ;; def: cross-post, req:no
2299                (and message-cross-post-default (not current-prefix-arg))
2300                ;; def: no-cross-post, req:yes
2301                (and (not message-cross-post-default) current-prefix-arg))
2302               (not (string-match "poster" target-group))
2303               (not (string-match (regexp-quote target-group)
2304                                  (message-fetch-field "Newsgroups"))))
2305          (end-of-line)
2306          (insert (concat "," target-group))))
2307   (end-of-line) ; ensure Followup: comes after Newsgroups:
2308   ;; unless new followup would be identical to Newsgroups line
2309   ;; make a new Followup-To line
2310   (if (not (string-match (concat "^[ \t]*"
2311                                  target-group
2312                                  "[ \t]*$")
2313                          (message-fetch-field "Newsgroups")))
2314       (insert (concat "\nFollowup-To: " target-group)))
2315   (setq message-cross-post-old-target target-group))
2316
2317 (defun message-cross-post-insert-note (target-group cross-post in-old
2318                                                     old-groups)
2319   "Insert a in message body note about a set Followup or Crosspost.
2320 If there have been previous notes, delete them.  TARGET-GROUP specifies the
2321 group to Followup-To.  When CROSS-POST is t, insert note about
2322 crossposting.  IN-OLD specifies whether TARGET-GROUP is a member of
2323 OLD-GROUPS.  OLD-GROUPS lists the old-groups the posting would have
2324 been made to before the user asked for a Crosspost."
2325   ;; start scanning body for previous uses
2326   (message-goto-signature)
2327   (let ((head (re-search-backward
2328                (concat "^" mail-header-separator)
2329                nil t))) ; just search in body
2330     (message-goto-signature)
2331     (while (re-search-backward
2332             (concat "^" (regexp-quote message-cross-post-note) ".*")
2333             head t)
2334       (message-delete-line))
2335     (message-goto-signature)
2336     (while (re-search-backward
2337             (concat "^" (regexp-quote message-followup-to-note) ".*")
2338             head t)
2339       (message-delete-line))
2340     ;; insert new note
2341     (if (message-goto-signature)
2342         (re-search-backward message-signature-separator))
2343     (if (or in-old
2344             (not cross-post)
2345             (string-match "^[ \t]*poster[ \t]*$" target-group))
2346         (insert (concat message-followup-to-note target-group "\n"))
2347       (insert (concat message-cross-post-note target-group "\n")))))
2348
2349 (defun message-cross-post-followup-to (target-group)
2350   "Crossposts message and set Followup-To to TARGET-GROUP.
2351 With prefix-argument just set Follow-Up, don't cross-post."
2352   (interactive
2353    (list ; Completion based on Gnus
2354     (completing-read "Followup To: "
2355                      (if (boundp 'gnus-newsrc-alist)
2356                          gnus-newsrc-alist)
2357                      nil nil '("poster" . 0)
2358                      (if (boundp 'gnus-group-history)
2359                          'gnus-group-history))))
2360   (cond ((not (or (null target-group) ; new subject not empty
2361                   (zerop (string-width target-group))
2362                   (string-match "^[ \t]*$" target-group)))
2363          (save-excursion
2364            (let* ((old-groups (message-fetch-field "Newsgroups"))
2365                   (in-old (string-match
2366                            (regexp-quote target-group)
2367                            (or old-groups ""))))
2368              ;; check whether target exactly matches old Newsgroups
2369              (cond ((not old-groups)
2370                     (error "No current newsgroup"))
2371                    ((or (not in-old)
2372                         (not (string-match
2373                               (concat "^[ \t]*"
2374                                       (regexp-quote target-group)
2375                                       "[ \t]*$")
2376                               old-groups)))
2377                     ;; yes, Newsgroups line must change
2378                     (message-cross-post-followup-to-header target-group)
2379                     ;; insert note whether we do cross-post or followup-to
2380                     (funcall message-cross-post-note-function
2381                              target-group
2382                              (if (or (and message-cross-post-default
2383                                           (not current-prefix-arg))
2384                                      (and (not message-cross-post-default)
2385                                           current-prefix-arg)) t)
2386                              in-old old-groups))))))))
2387
2388 ;;; Reduce To: to Cc: or Bcc: header
2389
2390 (defun message-reduce-to-to-cc ()
2391  "Replace contents of To: header with contents of Cc: or Bcc: header."
2392  (interactive)
2393  (let ((cc-content
2394         (save-restriction (message-narrow-to-headers)
2395                           (message-fetch-field "cc")))
2396        (bcc nil))
2397    (if (and (not cc-content)
2398             (setq cc-content
2399                   (save-restriction
2400                     (message-narrow-to-headers)
2401                     (message-fetch-field "bcc"))))
2402        (setq bcc t))
2403    (cond (cc-content
2404           (save-excursion
2405             (message-goto-to)
2406             (message-delete-line)
2407             (insert (concat "To: " cc-content "\n"))
2408             (save-restriction
2409               (message-narrow-to-headers)
2410               (message-remove-header (if bcc
2411                                          "bcc"
2412                                        "cc"))))))))
2413
2414 ;;; End of functions adopted from `message-utils.el'.
2415
2416 (defun message-remove-header (header &optional is-regexp first reverse)
2417   "Remove HEADER in the narrowed buffer.
2418 If IS-REGEXP, HEADER is a regular expression.
2419 If FIRST, only remove the first instance of the header.
2420 Return the number of headers removed."
2421   (goto-char (point-min))
2422   (let ((regexp (if is-regexp header (concat "^" (regexp-quote header) ":")))
2423         (number 0)
2424         (case-fold-search t)
2425         last)
2426     (while (and (not (eobp))
2427                 (not last))
2428       (if (if reverse
2429               (not (looking-at regexp))
2430             (looking-at regexp))
2431           (progn
2432             (incf number)
2433             (when first
2434               (setq last t))
2435             (delete-region
2436              (point)
2437              ;; There might be a continuation header, so we have to search
2438              ;; until we find a new non-continuation line.
2439              (progn
2440                (forward-line 1)
2441                (if (re-search-forward "^[^ \t]" nil t)
2442                    (goto-char (match-beginning 0))
2443                  (point-max)))))
2444         (forward-line 1)
2445         (if (re-search-forward "^[^ \t]" nil t)
2446             (goto-char (match-beginning 0))
2447           (goto-char (point-max)))))
2448     number))
2449
2450 (defun message-remove-first-header (header)
2451   "Remove the first instance of HEADER if there is more than one."
2452   (let ((count 0)
2453         (regexp (concat "^" (regexp-quote header) ":")))
2454     (save-excursion
2455       (goto-char (point-min))
2456       (while (re-search-forward regexp nil t)
2457         (incf count)))
2458     (while (> count 1)
2459       (message-remove-header header nil t)
2460       (decf count))))
2461
2462 (defun message-narrow-to-headers ()
2463   "Narrow the buffer to the head of the message."
2464   (widen)
2465   (narrow-to-region
2466    (goto-char (point-min))
2467    (if (re-search-forward
2468         (concat "^" (regexp-quote mail-header-separator) "\n") nil t)
2469        (match-beginning 0)
2470      (point-max)))
2471   (goto-char (point-min)))
2472
2473 (defun message-narrow-to-head-1 ()
2474   "Like `message-narrow-to-head'.  Don't widen."
2475   (narrow-to-region
2476    (goto-char (point-min))
2477    (if (search-forward "\n\n" nil 1)
2478        (1- (point))
2479      (point-max)))
2480   (goto-char (point-min)))
2481
2482 ;; FIXME: clarify diffference: message-narrow-to-head,
2483 ;; message-narrow-to-headers-or-head, message-narrow-to-headers
2484 (defun message-narrow-to-head ()
2485   "Narrow the buffer to the head of the message.
2486 Point is left at the beginning of the narrowed-to region."
2487   (widen)
2488   (message-narrow-to-head-1))
2489
2490 (defun message-narrow-to-headers-or-head ()
2491   "Narrow the buffer to the head of the message."
2492   (widen)
2493   (narrow-to-region
2494    (goto-char (point-min))
2495    (if (re-search-forward (concat "\\(\n\\)\n\\|^\\("
2496                                   (regexp-quote mail-header-separator)
2497                                   "\n\\)")
2498                           nil t)
2499        (or (match-end 1) (match-beginning 2))
2500      (point-max)))
2501   (goto-char (point-min)))
2502
2503 (defun message-news-p ()
2504   "Say whether the current buffer contains a news message."
2505   (and (not message-this-is-mail)
2506        (or message-this-is-news
2507            (save-excursion
2508              (save-restriction
2509                (message-narrow-to-headers)
2510                (and (message-fetch-field "newsgroups")
2511                     (not (message-fetch-field "posted-to"))))))))
2512
2513 (defun message-mail-p ()
2514   "Say whether the current buffer contains a mail message."
2515   (and (not message-this-is-news)
2516        (or message-this-is-mail
2517            (save-excursion
2518              (save-restriction
2519                (message-narrow-to-headers)
2520                (or (message-fetch-field "to")
2521                    (message-fetch-field "cc")
2522                    (message-fetch-field "bcc")))))))
2523
2524 (defun message-subscribed-p ()
2525   "Say whether we need to insert a MFT header."
2526   (or message-subscribed-regexps
2527       message-subscribed-addresses
2528       message-subscribed-address-file
2529       message-subscribed-address-functions))
2530
2531 (defun message-next-header ()
2532   "Go to the beginning of the next header."
2533   (beginning-of-line)
2534   (or (eobp) (forward-char 1))
2535   (not (if (re-search-forward "^[^ \t]" nil t)
2536            (beginning-of-line)
2537          (goto-char (point-max)))))
2538
2539 (defun message-sort-headers-1 ()
2540   "Sort the buffer as headers using `message-rank' text props."
2541   (goto-char (point-min))
2542   (require 'sort)
2543   (sort-subr
2544    nil 'message-next-header
2545    (lambda ()
2546      (message-next-header)
2547      (unless (bobp)
2548        (forward-char -1)))
2549    (lambda ()
2550      (or (get-text-property (point) 'message-rank)
2551          10000))))
2552
2553 (defun message-sort-headers ()
2554   "Sort the headers of the current message according to `message-header-format-alist'."
2555   (interactive)
2556   (save-excursion
2557     (save-restriction
2558       (let ((max (1+ (length message-header-format-alist)))
2559             rank)
2560         (message-narrow-to-headers)
2561         (while (re-search-forward "^[^ \n]+:" nil t)
2562           (put-text-property
2563            (match-beginning 0) (1+ (match-beginning 0))
2564            'message-rank
2565            (if (setq rank (length (memq (assq (intern (buffer-substring
2566                                                        (match-beginning 0)
2567                                                        (1- (match-end 0))))
2568                                               message-header-format-alist)
2569                                         message-header-format-alist)))
2570                (- max rank)
2571              (1+ max)))))
2572       (message-sort-headers-1))))
2573
2574 (defun message-kill-address ()
2575   "Kill the address under point."
2576   (interactive)
2577   (let ((start (point)))
2578     (message-skip-to-next-address)
2579     (kill-region start (point))))
2580
2581
2582 (autoload 'Info-goto-node "info")
2583 (defvar mml2015-use)
2584
2585 (defun message-info (&optional arg)
2586   "Display the Message manual.
2587
2588 Prefixed with one \\[universal-argument], display the Emacs MIME
2589 manual.  With two \\[universal-argument]'s, display the EasyPG or
2590 PGG manual, depending on the value of `mml2015-use'."
2591   (interactive "p")
2592   ;; Don't use `info' because support for `(filename)nodename' is not
2593   ;; available in XEmacs < 21.5.12.
2594   (Info-goto-node (format "(%s)Top"
2595                           (cond ((eq arg 16)
2596                                  (require 'mml2015)
2597                                  mml2015-use)
2598                                 ((eq arg  4) 'emacs-mime)
2599                                 ;; `booleanp' only available in Emacs 22+
2600                                 ((and (not (memq arg '(nil t)))
2601                                       (symbolp arg))
2602                                  arg)
2603                                 (t
2604                                  'message)))))
2605
2606 \f
2607
2608 ;;;
2609 ;;; Message mode
2610 ;;;
2611
2612 ;;; Set up keymap.
2613
2614 (defvar message-mode-map nil)
2615
2616 (unless message-mode-map
2617   (setq message-mode-map (make-keymap))
2618   (set-keymap-parent message-mode-map text-mode-map)
2619   (define-key message-mode-map "\C-c?" 'describe-mode)
2620
2621   (define-key message-mode-map "\C-c\C-f\C-t" 'message-goto-to)
2622   (define-key message-mode-map "\C-c\C-f\C-o" 'message-goto-from)
2623   (define-key message-mode-map "\C-c\C-f\C-b" 'message-goto-bcc)
2624   (define-key message-mode-map "\C-c\C-f\C-w" 'message-goto-fcc)
2625   (define-key message-mode-map "\C-c\C-f\C-c" 'message-goto-cc)
2626   (define-key message-mode-map "\C-c\C-f\C-s" 'message-goto-subject)
2627   (define-key message-mode-map "\C-c\C-f\C-r" 'message-goto-reply-to)
2628   (define-key message-mode-map "\C-c\C-f\C-n" 'message-goto-newsgroups)
2629   (define-key message-mode-map "\C-c\C-f\C-d" 'message-goto-distribution)
2630   (define-key message-mode-map "\C-c\C-f\C-f" 'message-goto-followup-to)
2631   (define-key message-mode-map "\C-c\C-f\C-m" 'message-goto-mail-followup-to)
2632   (define-key message-mode-map "\C-c\C-f\C-k" 'message-goto-keywords)
2633   (define-key message-mode-map "\C-c\C-f\C-u" 'message-goto-summary)
2634   (define-key message-mode-map "\C-c\C-f\C-i"
2635     'message-insert-or-toggle-importance)
2636   (define-key message-mode-map "\C-c\C-f\C-a"
2637     'message-generate-unsubscribed-mail-followup-to)
2638
2639   ;; modify headers (and insert notes in body)
2640   (define-key message-mode-map "\C-c\C-fs"    'message-change-subject)
2641   ;;
2642   (define-key message-mode-map "\C-c\C-fx"    'message-cross-post-followup-to)
2643   ;; prefix+message-cross-post-followup-to = same w/o cross-post
2644   (define-key message-mode-map "\C-c\C-ft"    'message-reduce-to-to-cc)
2645   (define-key message-mode-map "\C-c\C-fa"    'message-add-archive-header)
2646   ;; mark inserted text
2647   (define-key message-mode-map "\C-c\M-m" 'message-mark-inserted-region)
2648   (define-key message-mode-map "\C-c\M-f" 'message-mark-insert-file)
2649
2650   (define-key message-mode-map "\C-c\C-b" 'message-goto-body)
2651   (define-key message-mode-map "\C-c\C-i" 'message-goto-signature)
2652
2653   (define-key message-mode-map "\C-c\C-t" 'message-insert-to)
2654   (define-key message-mode-map "\C-c\C-fw" 'message-insert-wide-reply)
2655   (define-key message-mode-map "\C-c\C-n" 'message-insert-newsgroups)
2656   (define-key message-mode-map "\C-c\C-l" 'message-to-list-only)
2657   (define-key message-mode-map "\C-c\C-f\C-e" 'message-insert-expires)
2658
2659   (define-key message-mode-map "\C-c\C-u" 'message-insert-or-toggle-importance)
2660   (define-key message-mode-map "\C-c\M-n"
2661     'message-insert-disposition-notification-to)
2662
2663   (define-key message-mode-map "\C-c\C-y" 'message-yank-original)
2664   (define-key message-mode-map "\C-c\M-\C-y" 'message-yank-buffer)
2665   (define-key message-mode-map "\C-c\C-q" 'message-fill-yanked-message)
2666   (define-key message-mode-map "\C-c\C-w" 'message-insert-signature)
2667   (define-key message-mode-map "\C-c\M-h" 'message-insert-headers)
2668   (define-key message-mode-map "\C-c\C-r" 'message-caesar-buffer-body)
2669   (define-key message-mode-map "\C-c\C-o" 'message-sort-headers)
2670   (define-key message-mode-map "\C-c\M-r" 'message-rename-buffer)
2671
2672   (define-key message-mode-map "\C-c\C-c" 'message-send-and-exit)
2673   (define-key message-mode-map "\C-c\C-s" 'message-send)
2674   (define-key message-mode-map "\C-c\C-k" 'message-kill-buffer)
2675   (define-key message-mode-map "\C-c\C-d" 'message-dont-send)
2676   (define-key message-mode-map "\C-c\n" 'gnus-delay-article)
2677
2678   (define-key message-mode-map "\C-c\M-k" 'message-kill-address)
2679   (define-key message-mode-map "\C-c\C-e" 'message-elide-region)
2680   (define-key message-mode-map "\C-c\C-v" 'message-delete-not-region)
2681   (define-key message-mode-map "\C-c\C-z" 'message-kill-to-signature)
2682   (define-key message-mode-map "\M-\r" 'message-newline-and-reformat)
2683   (define-key message-mode-map [remap split-line]  'message-split-line)
2684
2685   (define-key message-mode-map "\C-c\C-a" 'mml-attach-file)
2686
2687   (define-key message-mode-map "\C-a" 'message-beginning-of-line)
2688   (define-key message-mode-map "\t" 'message-tab)
2689
2690   (define-key message-mode-map "\M-n" 'message-display-abbrev))
2691
2692 (easy-menu-define
2693   message-mode-menu message-mode-map "Message Menu."
2694   `("Message"
2695     ["Yank Original" message-yank-original message-reply-buffer]
2696     ["Fill Yanked Message" message-fill-yanked-message t]
2697     ["Insert Signature" message-insert-signature t]
2698     ["Caesar (rot13) Message" message-caesar-buffer-body t]
2699     ["Caesar (rot13) Region" message-caesar-region (message-mark-active-p)]
2700     ["Elide Region" message-elide-region
2701      :active (message-mark-active-p)
2702      ,@(if (featurep 'xemacs) nil
2703          '(:help "Replace text in region with an ellipsis"))]
2704     ["Delete Outside Region" message-delete-not-region
2705      :active (message-mark-active-p)
2706      ,@(if (featurep 'xemacs) nil
2707          '(:help "Delete all quoted text outside region"))]
2708     ["Kill To Signature" message-kill-to-signature t]
2709     ["Newline and Reformat" message-newline-and-reformat t]
2710     ["Rename buffer" message-rename-buffer t]
2711     ["Spellcheck" ispell-message
2712      ,@(if (featurep 'xemacs) '(t)
2713          '(:help "Spellcheck this message"))]
2714     "----"
2715     ["Insert Region Marked" message-mark-inserted-region
2716      :active (message-mark-active-p)
2717      ,@(if (featurep 'xemacs) nil
2718          '(:help "Mark region with enclosing tags"))]
2719     ["Insert File Marked..." message-mark-insert-file
2720      ,@(if (featurep 'xemacs) '(t)
2721          '(:help "Insert file at point marked with enclosing tags"))]
2722     "----"
2723     ["Send Message" message-send-and-exit
2724      ,@(if (featurep 'xemacs) '(t)
2725          '(:help "Send this message"))]
2726     ["Postpone Message" message-dont-send
2727      ,@(if (featurep 'xemacs) '(t)
2728          '(:help "File this draft message and exit"))]
2729     ["Send at Specific Time..." gnus-delay-article
2730      ,@(if (featurep 'xemacs) '(t)
2731          '(:help "Ask, then arrange to send message at that time"))]
2732     ["Kill Message" message-kill-buffer
2733      ,@(if (featurep 'xemacs) '(t)
2734          '(:help "Delete this message without sending"))]
2735     "----"
2736     ["Message manual" message-info
2737      ,@(if (featurep 'xemacs) '(t)
2738          '(:help "Display the Message manual"))]))
2739
2740 (easy-menu-define
2741   message-mode-field-menu message-mode-map ""
2742   `("Field"
2743     ["To" message-goto-to t]
2744     ["From" message-goto-from t]
2745     ["Subject" message-goto-subject t]
2746     ["Change subject..." message-change-subject t]
2747     ["Cc" message-goto-cc t]
2748     ["Bcc" message-goto-bcc t]
2749     ["Fcc" message-goto-fcc t]
2750     ["Reply-To" message-goto-reply-to t]
2751     ["Flag As Important" message-insert-importance-high
2752      ,@(if (featurep 'xemacs) '(t)
2753          '(:help "Mark this message as important"))]
2754     ["Flag As Unimportant" message-insert-importance-low
2755      ,@(if (featurep 'xemacs) '(t)
2756          '(:help "Mark this message as unimportant"))]
2757     ["Request Receipt"
2758      message-insert-disposition-notification-to
2759      ,@(if (featurep 'xemacs) '(t)
2760          '(:help "Request a receipt notification"))]
2761     "----"
2762     ;; (typical) news stuff
2763     ["Summary" message-goto-summary t]
2764     ["Keywords" message-goto-keywords t]
2765     ["Newsgroups" message-goto-newsgroups t]
2766     ["Fetch Newsgroups" message-insert-newsgroups t]
2767     ["Followup-To" message-goto-followup-to t]
2768     ;; ["Followup-To (with note in body)" message-cross-post-followup-to t]
2769     ["Crosspost / Followup-To..." message-cross-post-followup-to t]
2770     ["Distribution" message-goto-distribution t]
2771     ["Expires" message-insert-expires t ]
2772     ["X-No-Archive" message-add-archive-header t ]
2773     "----"
2774     ;; (typical) mailing-lists stuff
2775     ["Fetch To" message-insert-to
2776      ,@(if (featurep 'xemacs) '(t)
2777          '(:help "Insert a To header that points to the author."))]
2778     ["Fetch To and Cc" message-insert-wide-reply
2779      ,@(if (featurep 'xemacs) '(t)
2780          '(:help
2781            "Insert To and Cc headers as if you were doing a wide reply."))]
2782     "----"
2783     ["Send to list only" message-to-list-only t]
2784     ["Mail-Followup-To" message-goto-mail-followup-to t]
2785     ["Unsubscribed list post" message-generate-unsubscribed-mail-followup-to
2786      ,@(if (featurep 'xemacs) '(t)
2787          '(:help "Insert a reasonable `Mail-Followup-To:' header."))]
2788     ["Reduce To: to Cc:" message-reduce-to-to-cc t]
2789     "----"
2790     ["Sort Headers" message-sort-headers t]
2791     ["Encode non-ASCII domain names" message-idna-to-ascii-rhs t]
2792     ;; We hide `message-hidden-headers' by narrowing the buffer.
2793     ["Show Hidden Headers" widen t]
2794     ["Goto Body" message-goto-body t]
2795     ["Goto Signature" message-goto-signature t]))
2796
2797 (defvar message-tool-bar-map nil)
2798
2799 (defvar facemenu-add-face-function)
2800 (defvar facemenu-remove-face-function)
2801
2802 ;;; Forbidden properties
2803 ;;
2804 ;; We use `after-change-functions' to keep special text properties
2805 ;; that interfere with the normal function of message mode out of the
2806 ;; buffer.
2807
2808 (defcustom message-strip-special-text-properties t
2809   "Strip special properties from the message buffer.
2810
2811 Emacs has a number of special text properties which can break message
2812 composing in various ways.  If this option is set, message will strip
2813 these properties from the message composition buffer.  However, some
2814 packages requires these properties to be present in order to work.
2815 If you use one of these packages, turn this option off, and hope the
2816 message composition doesn't break too bad."
2817   :version "22.1"
2818   :group 'message-various
2819   :link '(custom-manual "(message)Various Message Variables")
2820   :type 'boolean)
2821
2822 (defvar message-forbidden-properties
2823   ;; No reason this should be clutter up customize.  We make it a
2824   ;; property list (rather than a list of property symbols), to be
2825   ;; directly useful for `remove-text-properties'.
2826   '(field nil read-only nil invisible nil intangible nil
2827           mouse-face nil modification-hooks nil insert-in-front-hooks nil
2828           insert-behind-hooks nil point-entered nil point-left nil)
2829   ;; Other special properties:
2830   ;; category, face, display: probably doesn't do any harm.
2831   ;; fontified: is used by font-lock.
2832   ;; syntax-table, local-map: I dunno.
2833   ;; We need to add XEmacs names to the list.
2834   "Property list of with properties forbidden in message buffers.
2835 The values of the properties are ignored, only the property names are used.")
2836
2837 (defun message-tamago-not-in-use-p (pos)
2838   "Return t when tamago version 4 is not in use at the cursor position.
2839 Tamago version 4 is a popular input method for writing Japanese text.
2840 It uses the properties `intangible', `invisible', `modification-hooks'
2841 and `read-only' when translating ascii or kana text to kanji text.
2842 These properties are essential to work, so we should never strip them."
2843   (not (and (boundp 'egg-modefull-mode)
2844             (symbol-value 'egg-modefull-mode)
2845             (or (memq (get-text-property pos 'intangible)
2846                       '(its-part-1 its-part-2))
2847                 (get-text-property pos 'egg-end)
2848                 (get-text-property pos 'egg-lang)
2849                 (get-text-property pos 'egg-start)))))
2850
2851 (defsubst message-mail-alias-type-p (type)
2852   (if (atom message-mail-alias-type)
2853       (eq message-mail-alias-type type)
2854     (memq type message-mail-alias-type)))
2855
2856 (defun message-strip-forbidden-properties (begin end &optional old-length)
2857   "Strip forbidden properties between BEGIN and END, ignoring the third arg.
2858 This function is intended to be called from `after-change-functions'.
2859 See also `message-forbidden-properties'."
2860   (when (and (message-mail-alias-type-p 'ecomplete)
2861              (memq this-command message-self-insert-commands))
2862     (message-display-abbrev))
2863   (when (and message-strip-special-text-properties
2864              (message-tamago-not-in-use-p begin))
2865     (let ((buffer-read-only nil)
2866           (inhibit-read-only t))
2867       (remove-text-properties begin end message-forbidden-properties))))
2868
2869 (autoload 'ecomplete-setup "ecomplete") ;; for Emacs <23.
2870
2871 ;;;###autoload
2872 (define-derived-mode message-mode text-mode "Message"
2873   "Major mode for editing mail and news to be sent.
2874 Like Text Mode but with these additional commands:\\<message-mode-map>
2875 C-c C-s  `message-send' (send the message)  C-c C-c  `message-send-and-exit'
2876 C-c C-d  Postpone sending the message       C-c C-k  Kill the message
2877 C-c C-f  move to a header field (and create it if there isn't):
2878          C-c C-f C-t  move to To        C-c C-f C-s  move to Subject
2879          C-c C-f C-c  move to Cc        C-c C-f C-b  move to Bcc
2880          C-c C-f C-w  move to Fcc       C-c C-f C-r  move to Reply-To
2881          C-c C-f C-u  move to Summary   C-c C-f C-n  move to Newsgroups
2882          C-c C-f C-k  move to Keywords  C-c C-f C-d  move to Distribution
2883          C-c C-f C-o  move to From (\"Originator\")
2884          C-c C-f C-f  move to Followup-To
2885          C-c C-f C-m  move to Mail-Followup-To
2886          C-c C-f C-e  move to Expires
2887          C-c C-f C-i  cycle through Importance values
2888          C-c C-f s    change subject and append \"(was: <Old Subject>)\"
2889          C-c C-f x    crossposting with FollowUp-To header and note in body
2890          C-c C-f t    replace To: header with contents of Cc: or Bcc:
2891          C-c C-f a    Insert X-No-Archive: header and a note in the body
2892 C-c C-t  `message-insert-to' (add a To header to a news followup)
2893 C-c C-l  `message-to-list-only' (removes all but list address in to/cc)
2894 C-c C-n  `message-insert-newsgroups' (add a Newsgroup header to a news reply)
2895 C-c C-b  `message-goto-body' (move to beginning of message text).
2896 C-c C-i  `message-goto-signature' (move to the beginning of the signature).
2897 C-c C-w  `message-insert-signature' (insert `message-signature-file' file).
2898 C-c C-y  `message-yank-original' (insert current message, if any).
2899 C-c C-q  `message-fill-yanked-message' (fill what was yanked).
2900 C-c C-e  `message-elide-region' (elide the text between point and mark).
2901 C-c C-v  `message-delete-not-region' (remove the text outside the region).
2902 C-c C-z  `message-kill-to-signature' (kill the text up to the signature).
2903 C-c C-r  `message-caesar-buffer-body' (rot13 the message body).
2904 C-c C-a  `mml-attach-file' (attach a file as MIME).
2905 C-c C-u  `message-insert-or-toggle-importance'  (insert or cycle importance).
2906 C-c M-n  `message-insert-disposition-notification-to'  (request receipt).
2907 C-c M-m  `message-mark-inserted-region' (mark region with enclosing tags).
2908 C-c M-f  `message-mark-insert-file' (insert file marked with enclosing tags).
2909 M-RET    `message-newline-and-reformat' (break the line and reformat)."
2910   (setq local-abbrev-table text-mode-abbrev-table)
2911   (set (make-local-variable 'message-reply-buffer) nil)
2912   (set (make-local-variable 'message-inserted-headers) nil)
2913   (set (make-local-variable 'message-send-actions) nil)
2914   (set (make-local-variable 'message-return-action) nil)
2915   (set (make-local-variable 'message-exit-actions) nil)
2916   (set (make-local-variable 'message-kill-actions) nil)
2917   (set (make-local-variable 'message-postpone-actions) nil)
2918   (set (make-local-variable 'message-draft-article) nil)
2919   (setq buffer-offer-save t)
2920   (set (make-local-variable 'facemenu-add-face-function)
2921        (lambda (face end)
2922          (let ((face-fun (cdr (assq face message-face-alist))))
2923            (if face-fun
2924                (funcall face-fun (point) end)
2925              (error "Face %s not configured for %s mode" face mode-name)))
2926          ""))
2927   (set (make-local-variable 'facemenu-remove-face-function) t)
2928   (set (make-local-variable 'message-reply-headers) nil)
2929   (make-local-variable 'message-newsreader)
2930   (make-local-variable 'message-mailer)
2931   (make-local-variable 'message-post-method)
2932   (set (make-local-variable 'message-sent-message-via) nil)
2933   (set (make-local-variable 'message-checksum) nil)
2934   (set (make-local-variable 'message-mime-part) 0)
2935   (message-setup-fill-variables)
2936   (when message-fill-column
2937     (setq fill-column message-fill-column)
2938     (turn-on-auto-fill))
2939   ;; Allow using comment commands to add/remove quoting.
2940   ;; (set (make-local-variable 'comment-start) message-yank-prefix)
2941   (when message-yank-prefix
2942     (set (make-local-variable 'comment-start) message-yank-prefix)
2943     (set (make-local-variable 'comment-start-skip)
2944          (concat "^" (regexp-quote message-yank-prefix) "[ \t]*")))
2945   (if (featurep 'xemacs)
2946       (message-setup-toolbar)
2947     (set (make-local-variable 'font-lock-defaults)
2948          '(message-font-lock-keywords t))
2949     (if (boundp 'tool-bar-map)
2950         (set (make-local-variable 'tool-bar-map) (message-make-tool-bar))))
2951   (easy-menu-add message-mode-menu message-mode-map)
2952   (easy-menu-add message-mode-field-menu message-mode-map)
2953   (gnus-make-local-hook 'after-change-functions)
2954   ;; Mmmm... Forbidden properties...
2955   (add-hook 'after-change-functions 'message-strip-forbidden-properties
2956             nil 'local)
2957   ;; Allow mail alias things.
2958   (cond
2959    ((message-mail-alias-type-p 'abbrev)
2960     (if (fboundp 'mail-abbrevs-setup)
2961         (mail-abbrevs-setup)
2962       (if (fboundp 'mail-aliases-setup) ; warning avoidance
2963           (mail-aliases-setup))))
2964    ((message-mail-alias-type-p 'ecomplete)
2965     (ecomplete-setup)))
2966   (add-hook 'completion-at-point-functions 'message-completion-function nil t)
2967   (unless buffer-file-name
2968     (message-set-auto-save-file-name))
2969   (unless (buffer-base-buffer)
2970     ;; Don't enable multibyte on an indirect buffer.  Maybe enabling
2971     ;; multibyte is not necessary at all. -- zsh
2972     (mm-enable-multibyte))
2973   (set (make-local-variable 'indent-tabs-mode) nil) ;No tabs for indentation.
2974   (mml-mode))
2975
2976 (defun message-setup-fill-variables ()
2977   "Setup message fill variables."
2978   (set (make-local-variable 'fill-paragraph-function)
2979        'message-fill-paragraph)
2980   (make-local-variable 'paragraph-separate)
2981   (make-local-variable 'paragraph-start)
2982   (make-local-variable 'adaptive-fill-regexp)
2983   (unless (boundp 'adaptive-fill-first-line-regexp)
2984     (setq adaptive-fill-first-line-regexp nil))
2985   (make-local-variable 'adaptive-fill-first-line-regexp)
2986   (let ((quote-prefix-regexp
2987          ;; User should change message-cite-prefix-regexp if
2988          ;; message-yank-prefix is set to an abnormal value.
2989          (concat "\\(" message-cite-prefix-regexp "\\)[ \t]*")))
2990     (setq paragraph-start
2991           (concat
2992            (regexp-quote mail-header-separator) "$\\|"
2993            "[ \t]*$\\|"                 ; blank lines
2994            "-- $\\|"                    ; signature delimiter
2995            "---+$\\|"              ; delimiters for forwarded messages
2996            page-delimiter "$\\|"        ; spoiler warnings
2997            ".*wrote:$\\|"               ; attribution lines
2998            quote-prefix-regexp "$\\|"   ; empty lines in quoted text
2999                                         ; mml tags
3000            "<#!*/?\\(multipart\\|part\\|external\\|mml\\|secure\\)"))
3001     (setq paragraph-separate paragraph-start)
3002     (setq adaptive-fill-regexp
3003           (concat quote-prefix-regexp "\\|" adaptive-fill-regexp))
3004     (setq adaptive-fill-first-line-regexp
3005           (concat quote-prefix-regexp "\\|"
3006                   adaptive-fill-first-line-regexp)))
3007   (make-local-variable 'auto-fill-inhibit-regexp)
3008   ;;(setq auto-fill-inhibit-regexp "^[A-Z][^: \n\t]+:")
3009   (setq auto-fill-inhibit-regexp nil)
3010   (make-local-variable 'normal-auto-fill-function)
3011   (setq normal-auto-fill-function 'message-do-auto-fill)
3012   ;; KLUDGE: auto fill might already be turned on in `text-mode-hook'.
3013   ;; In that case, ensure that it uses the right function.  The real
3014   ;; solution would be not to use `define-derived-mode', and run
3015   ;; `text-mode-hook' ourself at the end of the mode.
3016   ;; -- Per Abrahamsen <abraham@dina.kvl.dk> Date: 2001-10-19.
3017   ;; This kludge is unneeded in Emacs>=21 since define-derived-mode is
3018   ;; now careful to run parent hooks after the body.  --Stef
3019   (when auto-fill-function
3020     (setq auto-fill-function normal-auto-fill-function)))
3021
3022 \f
3023
3024 ;;;
3025 ;;; Message mode commands
3026 ;;;
3027
3028 ;;; Movement commands
3029
3030 (defun message-goto-to ()
3031   "Move point to the To header."
3032   (interactive)
3033   (message-position-on-field "To"))
3034
3035 (defun message-goto-from ()
3036   "Move point to the From header."
3037   (interactive)
3038   (message-position-on-field "From"))
3039
3040 (defun message-goto-subject ()
3041   "Move point to the Subject header."
3042   (interactive)
3043   (message-position-on-field "Subject"))
3044
3045 (defun message-goto-cc ()
3046   "Move point to the Cc header."
3047   (interactive)
3048   (message-position-on-field "Cc" "To"))
3049
3050 (defun message-goto-bcc ()
3051   "Move point to the Bcc  header."
3052   (interactive)
3053   (message-position-on-field "Bcc" "Cc" "To"))
3054
3055 (defun message-goto-fcc ()
3056   "Move point to the Fcc header."
3057   (interactive)
3058   (message-position-on-field "Fcc" "To" "Newsgroups"))
3059
3060 (defun message-goto-reply-to ()
3061   "Move point to the Reply-To header."
3062   (interactive)
3063   (message-position-on-field "Reply-To" "Subject"))
3064
3065 (defun message-goto-newsgroups ()
3066   "Move point to the Newsgroups header."
3067   (interactive)
3068   (message-position-on-field "Newsgroups"))
3069
3070 (defun message-goto-distribution ()
3071   "Move point to the Distribution header."
3072   (interactive)
3073   (message-position-on-field "Distribution"))
3074
3075 (defun message-goto-followup-to ()
3076   "Move point to the Followup-To header."
3077   (interactive)
3078   (message-position-on-field "Followup-To" "Newsgroups"))
3079
3080 (defun message-goto-mail-followup-to ()
3081   "Move point to the Mail-Followup-To header."
3082   (interactive)
3083   (message-position-on-field "Mail-Followup-To" "To"))
3084
3085 (defun message-goto-keywords ()
3086   "Move point to the Keywords header."
3087   (interactive)
3088   (message-position-on-field "Keywords" "Subject"))
3089
3090 (defun message-goto-summary ()
3091   "Move point to the Summary header."
3092   (interactive)
3093   (message-position-on-field "Summary" "Subject"))
3094
3095 (eval-when-compile
3096   (defmacro message-called-interactively-p (kind)
3097     (condition-case nil
3098         (progn
3099           (eval '(called-interactively-p 'any))
3100           ;; Emacs >=23.2
3101           `(called-interactively-p ,kind))
3102       ;; Emacs <23.2
3103       (wrong-number-of-arguments '(called-interactively-p))
3104       ;; XEmacs
3105       (void-function '(interactive-p)))))
3106
3107 (defun message-goto-body ()
3108   "Move point to the beginning of the message body."
3109   (interactive)
3110   (when (and (message-called-interactively-p 'any)
3111              (looking-at "[ \t]*\n"))
3112     (expand-abbrev))
3113   (goto-char (point-min))
3114   (or (search-forward (concat "\n" mail-header-separator "\n") nil t)
3115       (search-forward-regexp "[^:]+:\\([^\n]\\|\n[ \t]\\)+\n\n" nil t)))
3116
3117 (defun message-in-body-p ()
3118   "Return t if point is in the message body."
3119   (let ((body (save-excursion (message-goto-body))))
3120     (>= (point) body)))
3121
3122 (defun message-goto-eoh ()
3123   "Move point to the end of the headers."
3124   (interactive)
3125   (message-goto-body)
3126   (forward-line -1))
3127
3128 (defun message-goto-signature ()
3129   "Move point to the beginning of the message signature.
3130 If there is no signature in the article, go to the end and
3131 return nil."
3132   (interactive)
3133   (goto-char (point-min))
3134   (if (re-search-forward message-signature-separator nil t)
3135       (forward-line 1)
3136     (goto-char (point-max))
3137     nil))
3138
3139 (defun message-generate-unsubscribed-mail-followup-to (&optional include-cc)
3140   "Insert a reasonable MFT header in a post to an unsubscribed list.
3141 When making original posts to a mailing list you are not subscribed to,
3142 you have to type in a MFT header by hand.  The contents, usually, are
3143 the addresses of the list and your own address.  This function inserts
3144 such a header automatically.  It fetches the contents of the To: header
3145 in the current mail buffer, and appends the current `user-mail-address'.
3146
3147 If the optional argument INCLUDE-CC is non-nil, the addresses in the
3148 Cc: header are also put into the MFT."
3149
3150   (interactive "P")
3151   (let* (cc tos)
3152     (save-restriction
3153       (message-narrow-to-headers)
3154       (message-remove-header "Mail-Followup-To")
3155       (setq cc (and include-cc (message-fetch-field "Cc")))
3156       (setq tos (if cc
3157                     (concat (message-fetch-field "To") "," cc)
3158                   (message-fetch-field "To"))))
3159     (message-goto-mail-followup-to)
3160     (insert (concat tos ", " user-mail-address))))
3161
3162 \f
3163
3164 (defun message-insert-to (&optional force)
3165   "Insert a To header that points to the author of the article being replied to.
3166 If the original author requested not to be sent mail, don't insert unless the
3167 prefix FORCE is given."
3168   (interactive "P")
3169   (let* ((mct (message-fetch-reply-field "mail-copies-to"))
3170          (dont (and mct (or (equal (downcase mct) "never")
3171                             (equal (downcase mct) "nobody"))))
3172          (to (or (message-fetch-reply-field "mail-reply-to")
3173                  (message-fetch-reply-field "reply-to")
3174                  (message-fetch-reply-field "from"))))
3175     (when (and dont to)
3176       (message
3177        (if force
3178            "Ignoring the user request not to have copies sent via mail"
3179          "Complying with the user request not to have copies sent via mail")))
3180     (when (and force (not to))
3181       (error "No mail address in the article"))
3182     (when (and to (or force (not dont)))
3183       (message-carefully-insert-headers (list (cons 'To to))))))
3184
3185 (defun message-insert-wide-reply ()
3186   "Insert To and Cc headers as if you were doing a wide reply."
3187   (interactive)
3188   (let ((headers (message-with-reply-buffer
3189                    (message-get-reply-headers t))))
3190     (message-carefully-insert-headers headers)))
3191
3192 (defcustom message-header-synonyms
3193   '((To Cc Bcc)
3194     (Original-To))
3195   "List of lists of header synonyms.
3196 E.g., if this list contains a member list with elements `Cc' and `To',
3197 then `message-carefully-insert-headers' will not insert a `To' header
3198 when the message is already `Cc'ed to the recipient."
3199   :version "22.1"
3200   :group 'message-headers
3201   :link '(custom-manual "(message)Message Headers")
3202   :type '(repeat sexp))
3203
3204 (defun message-carefully-insert-headers (headers)
3205   "Insert the HEADERS, an alist, into the message buffer.
3206 Does not insert the headers when they are already present there
3207 or in the synonym headers, defined by `message-header-synonyms'."
3208   ;; FIXME: Should compare only the address and not the full name.  Comparison
3209   ;; should be done case-folded (and with `string=' rather than
3210   ;; `string-match').
3211   ;; (mail-strip-quoted-names "Foo Bar <foo@bar>, bla@fasel (Bla Fasel)")
3212   (dolist (header headers)
3213     (let* ((header-name (symbol-name (car header)))
3214            (new-header (cdr header))
3215            (synonyms (loop for synonym in message-header-synonyms
3216                            when (memq (car header) synonym) return synonym))
3217            (old-header
3218             (loop for synonym in synonyms
3219                   for old-header = (mail-fetch-field (symbol-name synonym))
3220                   when (and old-header (string-match new-header old-header))
3221                   return synonym)))
3222       (if old-header
3223           (message "already have `%s' in `%s'" new-header old-header)
3224         (when (and (message-position-on-field header-name)
3225                    (setq old-header (mail-fetch-field header-name))
3226                    (not (string-match "\\` *\\'" old-header)))
3227           (insert ", "))
3228         (insert new-header)))))
3229
3230 (defun message-widen-reply ()
3231   "Widen the reply to include maximum recipients."
3232   (interactive)
3233   (let ((follow-to
3234          (and (bufferp message-reply-buffer)
3235               (buffer-name message-reply-buffer)
3236               (with-current-buffer message-reply-buffer
3237                 (message-get-reply-headers t)))))
3238     (save-excursion
3239       (save-restriction
3240         (message-narrow-to-headers)
3241         (dolist (elem follow-to)
3242           (message-remove-header (symbol-name (car elem)))
3243           (goto-char (point-min))
3244           (insert (symbol-name (car elem)) ": "
3245                   (cdr elem) "\n"))))))
3246
3247 (defun message-insert-newsgroups ()
3248   "Insert the Newsgroups header from the article being replied to."
3249   (interactive)
3250   (when (and (message-position-on-field "Newsgroups")
3251              (mail-fetch-field "newsgroups")
3252              (not (string-match "\\` *\\'" (mail-fetch-field "newsgroups"))))
3253     (insert ","))
3254   (insert (or (message-fetch-reply-field "newsgroups") "")))
3255
3256 \f
3257
3258 ;;; Various commands
3259
3260 (defun message-delete-not-region (beg end)
3261   "Delete everything in the body of the current message outside of the region."
3262   (interactive "r")
3263   (let (citeprefix)
3264     (save-excursion
3265       (goto-char beg)
3266       ;; snarf citation prefix, if appropriate
3267       (unless (eq (point) (progn (beginning-of-line) (point)))
3268         (when (looking-at message-cite-prefix-regexp)
3269           (setq citeprefix (match-string 0))))
3270       (goto-char end)
3271       (delete-region (point) (if (not (message-goto-signature))
3272                                  (point)
3273                                (forward-line -2)
3274                                (point)))
3275       (insert "\n")
3276       (goto-char beg)
3277       (delete-region beg (progn (message-goto-body)
3278                                 (forward-line 2)
3279                                 (point)))
3280       (when citeprefix
3281         (insert citeprefix))))
3282   (when (message-goto-signature)
3283     (forward-line -2)))
3284
3285 (defun message-kill-to-signature (&optional arg)
3286   "Kill all text up to the signature.
3287 If a numeric argument or prefix arg is given, leave that number
3288 of lines before the signature intact."
3289   (interactive "P")
3290   (save-excursion
3291     (save-restriction
3292       (let ((point (point)))
3293         (narrow-to-region point (point-max))
3294         (message-goto-signature)
3295         (unless (eobp)
3296           (if (and arg (numberp arg))
3297               (forward-line (- -1 arg))
3298             (end-of-line -1)))
3299         (unless (= point (point))
3300           (kill-region point (point))
3301           (unless (bolp)
3302             (insert "\n")))))))
3303
3304 (defun message-newline-and-reformat (&optional arg not-break)
3305   "Insert four newlines, and then reformat if inside quoted text.
3306 Prefix arg means justify as well."
3307   (interactive (list (if current-prefix-arg 'full)))
3308   (let (quoted point beg end leading-space bolp fill-paragraph-function)
3309     (setq point (point))
3310     (beginning-of-line)
3311     (setq beg (point))
3312     (setq bolp (= beg point))
3313     ;; Find first line of the paragraph.
3314     (if not-break
3315         (while (and (not (eobp))
3316                     (not (looking-at message-cite-prefix-regexp))
3317                     (looking-at paragraph-start))
3318           (forward-line 1)))
3319     ;; Find the prefix
3320     (when (looking-at message-cite-prefix-regexp)
3321       (setq quoted (match-string 0))
3322       (goto-char (match-end 0))
3323       (looking-at "[ \t]*")
3324       (setq leading-space (match-string 0)))
3325     (if (and quoted
3326              (not not-break)
3327              (not bolp)
3328              (< (- point beg) (length quoted)))
3329         ;; break inside the cite prefix.
3330         (setq quoted nil
3331               end nil))
3332     (if quoted
3333         (progn
3334           (forward-line 1)
3335           (while (and (not (eobp))
3336                       (not (looking-at paragraph-separate))
3337                       (looking-at message-cite-prefix-regexp)
3338                       (equal quoted (match-string 0)))
3339             (goto-char (match-end 0))
3340             (looking-at "[ \t]*")
3341             (if (> (length leading-space) (length (match-string 0)))
3342                 (setq leading-space (match-string 0)))
3343             (forward-line 1))
3344           (setq end (point))
3345           (goto-char beg)
3346           (while (and (if (bobp) nil (forward-line -1) t)
3347                       (not (looking-at paragraph-start))
3348                       (looking-at message-cite-prefix-regexp)
3349                       (equal quoted (match-string 0)))
3350             (setq beg (point))
3351             (goto-char (match-end 0))
3352             (looking-at "[ \t]*")
3353             (if (> (length leading-space) (length (match-string 0)))
3354                 (setq leading-space (match-string 0)))))
3355       (while (and (not (eobp))
3356                   (not (looking-at paragraph-separate))
3357                   (not (looking-at message-cite-prefix-regexp)))
3358         (forward-line 1))
3359       (setq end (point))
3360       (goto-char beg)
3361       (while (and (if (bobp) nil (forward-line -1) t)
3362                   (not (looking-at paragraph-start))
3363                   (not (looking-at message-cite-prefix-regexp)))
3364         (setq beg (point))))
3365     (goto-char point)
3366     (save-restriction
3367       (narrow-to-region beg end)
3368       (if not-break
3369           (setq point nil)
3370         (if bolp
3371             (newline)
3372           (newline)
3373           (newline))
3374         (setq point (point))
3375         ;; (newline 2) doesn't mark both newline's as hard, so call
3376         ;; newline twice. -jas
3377         (newline)
3378         (newline)
3379         (delete-region (point) (re-search-forward "[ \t]*"))
3380         (when (and quoted (not bolp))
3381           (insert quoted leading-space)))
3382       (undo-boundary)
3383       (if quoted
3384           (let* ((adaptive-fill-regexp
3385                   (regexp-quote (concat quoted leading-space)))
3386                  (adaptive-fill-first-line-regexp
3387                   adaptive-fill-regexp ))
3388             (fill-paragraph arg))
3389         (fill-paragraph arg))
3390       (if point (goto-char point)))))
3391
3392 (defun message-fill-paragraph (&optional arg)
3393   "Message specific function to fill a paragraph.
3394 This function is used as the value of `fill-paragraph-function' in
3395 Message buffers and is not meant to be called directly."
3396   (interactive (list (if current-prefix-arg 'full)))
3397   (if (if (boundp 'filladapt-mode) filladapt-mode)
3398       nil
3399     (if (message-point-in-header-p)
3400         (message-fill-field)
3401       (message-newline-and-reformat arg t))
3402     t))
3403
3404 (defun message-point-in-header-p ()
3405   "Return t if point is in the header."
3406   (save-excursion
3407     (not (re-search-backward
3408           (concat "^" (regexp-quote mail-header-separator) "\n") nil t))))
3409
3410 (defun message-do-auto-fill ()
3411   "Like `do-auto-fill', but don't fill in message header."
3412   (unless (message-point-in-header-p)
3413     (do-auto-fill)))
3414
3415 (defun message-insert-signature (&optional force)
3416   "Insert a signature.  See documentation for variable `message-signature'."
3417   (interactive (list 0))
3418   (let* ((signature
3419           (cond
3420            ((and (null message-signature)
3421                  (eq force 0))
3422             (save-excursion
3423               (goto-char (point-max))
3424               (not (re-search-backward message-signature-separator nil t))))
3425            ((and (null message-signature)
3426                  force)
3427             t)
3428            ((functionp message-signature)
3429             (funcall message-signature))
3430            ((listp message-signature)
3431             (eval message-signature))
3432            (t message-signature)))
3433          signature-file)
3434     (setq signature
3435           (cond ((stringp signature)
3436                  signature)
3437                 ((and (eq t signature) message-signature-file)
3438                  (setq signature-file
3439                        (if (and message-signature-directory
3440                                 ;; don't actually use the signature directory
3441                                 ;; if message-signature-file contains a path.
3442                                 (not (file-name-directory
3443                                       message-signature-file)))
3444                            (expand-file-name message-signature-file
3445                                              message-signature-directory)
3446                          message-signature-file))
3447                  (file-exists-p signature-file))))
3448     (when signature
3449       (goto-char (point-max))
3450       ;; Insert the signature.
3451       (unless (bolp)
3452         (insert "\n"))
3453       (when message-signature-insert-empty-line
3454         (insert "\n"))
3455       (insert "-- \n")
3456       (if (eq signature t)
3457           (insert-file-contents signature-file)
3458         (insert signature))
3459       (goto-char (point-max))
3460       (or (bolp) (insert "\n")))))
3461
3462 (defun message-insert-importance-high ()
3463   "Insert header to mark message as important."
3464   (interactive)
3465   (save-excursion
3466     (save-restriction
3467       (message-narrow-to-headers)
3468       (message-remove-header "Importance"))
3469     (message-goto-eoh)
3470     (insert "Importance: high\n")))
3471
3472 (defun message-insert-importance-low ()
3473   "Insert header to mark message as unimportant."
3474   (interactive)
3475   (save-excursion
3476     (save-restriction
3477       (message-narrow-to-headers)
3478       (message-remove-header "Importance"))
3479     (message-goto-eoh)
3480     (insert "Importance: low\n")))
3481
3482 (defun message-insert-or-toggle-importance ()
3483   "Insert a \"Importance: high\" header, or cycle through the header values.
3484 The three allowed values according to RFC 1327 are `high', `normal'
3485 and `low'."
3486   (interactive)
3487   (save-excursion
3488     (let ((new "high")
3489           cur)
3490       (save-restriction
3491         (message-narrow-to-headers)
3492         (when (setq cur (message-fetch-field "Importance"))
3493           (message-remove-header "Importance")
3494           (setq new (cond ((string= cur "high")
3495                            "low")
3496                           ((string= cur "low")
3497                            "normal")
3498                           (t
3499                            "high")))))
3500       (message-goto-eoh)
3501       (insert (format "Importance: %s\n" new)))))
3502
3503 (defun message-insert-disposition-notification-to ()
3504   "Request a disposition notification (return receipt) to this message.
3505 Note that this should not be used in newsgroups."
3506   (interactive)
3507   (save-excursion
3508     (save-restriction
3509       (message-narrow-to-headers)
3510       (message-remove-header "Disposition-Notification-To"))
3511     (message-goto-eoh)
3512     (insert (format "Disposition-Notification-To: %s\n"
3513                     (or (message-field-value "Reply-to")
3514                         (message-field-value "From")
3515                         (message-make-from))))))
3516
3517 (defun message-elide-region (b e)
3518   "Elide the text in the region.
3519 An ellipsis (from `message-elide-ellipsis') will be inserted where the
3520 text was killed."
3521   (interactive "r")
3522   (kill-region b e)
3523   (insert message-elide-ellipsis))
3524
3525 (defvar message-caesar-translation-table nil)
3526
3527 (defun message-caesar-region (b e &optional n)
3528   "Caesar rotate region B to E by N, default 13, for decrypting netnews."
3529   (interactive
3530    (list
3531     (min (point) (or (mark t) (point)))
3532     (max (point) (or (mark t) (point)))
3533     (when current-prefix-arg
3534       (prefix-numeric-value current-prefix-arg))))
3535
3536   (setq n (if (numberp n) (mod n 26) 13)) ;canonize N
3537   (unless (or (zerop n)                 ; no action needed for a rot of 0
3538               (= b e))                  ; no region to rotate
3539     ;; We build the table, if necessary.
3540     (when (or (not message-caesar-translation-table)
3541               (/= (aref message-caesar-translation-table ?a) (+ ?a n)))
3542       (setq message-caesar-translation-table
3543             (message-make-caesar-translation-table n)))
3544     (translate-region b e message-caesar-translation-table)))
3545
3546 (defun message-make-caesar-translation-table (n)
3547   "Create a rot table with offset N."
3548   (let ((i -1)
3549         (table (make-string 256 0)))
3550     (while (< (incf i) 256)
3551       (aset table i i))
3552     (concat
3553      (substring table 0 ?A)
3554      (substring table (+ ?A n) (+ ?A n (- 26 n)))
3555      (substring table ?A (+ ?A n))
3556      (substring table (+ ?A 26) ?a)
3557      (substring table (+ ?a n) (+ ?a n (- 26 n)))
3558      (substring table ?a (+ ?a n))
3559      (substring table (+ ?a 26) 255))))
3560
3561 (defun message-caesar-buffer-body (&optional rotnum wide)
3562   "Caesar rotate all letters in the current buffer by 13 places.
3563 Used to encode/decode possibly offensive messages (commonly in rec.humor).
3564 With prefix arg, specifies the number of places to rotate each letter forward.
3565 Mail and USENET news headers are not rotated unless WIDE is non-nil."
3566   (interactive (if current-prefix-arg
3567                    (list (prefix-numeric-value current-prefix-arg))
3568                  (list nil)))
3569   (save-excursion
3570     (save-restriction
3571       (when (and (not wide) (message-goto-body))
3572         (narrow-to-region (point) (point-max)))
3573       (message-caesar-region (point-min) (point-max) rotnum))))
3574
3575 (defun message-pipe-buffer-body (program)
3576   "Pipe the message body in the current buffer through PROGRAM."
3577   (save-excursion
3578     (save-restriction
3579       (when (message-goto-body)
3580         (narrow-to-region (point) (point-max)))
3581       (shell-command-on-region
3582        (point-min) (point-max) program nil t))))
3583
3584 (defun message-rename-buffer (&optional enter-string)
3585   "Rename the *message* buffer to \"*message* RECIPIENT\".
3586 If the function is run with a prefix, it will ask for a new buffer
3587 name, rather than giving an automatic name."
3588   (interactive "Pbuffer name: ")
3589   (save-excursion
3590     (save-restriction
3591       (goto-char (point-min))
3592       (narrow-to-region (point)
3593                         (search-forward mail-header-separator nil 'end))
3594       (let* ((mail-to (or
3595                        (if (message-news-p) (message-fetch-field "Newsgroups")
3596                          (message-fetch-field "To"))
3597                        ""))
3598              (mail-trimmed-to
3599               (if (string-match "," mail-to)
3600                   (concat (substring mail-to 0 (match-beginning 0)) ", ...")
3601                 mail-to))
3602              (name-default (concat "*message* " mail-trimmed-to))
3603              (name (if enter-string
3604                        (read-string "New buffer name: " name-default)
3605                      name-default)))
3606         (rename-buffer name t)))))
3607
3608 (defun message-fill-yanked-message (&optional justifyp)
3609   "Fill the paragraphs of a message yanked into this one.
3610 Numeric argument means justify as well."
3611   (interactive "P")
3612   (save-excursion
3613     (goto-char (point-min))
3614     (search-forward (concat "\n" mail-header-separator "\n") nil t)
3615     (let ((fill-prefix message-yank-prefix))
3616       (fill-individual-paragraphs (point) (point-max) justifyp))))
3617
3618 (defun message-indent-citation (&optional start end yank-only)
3619   "Modify text just inserted from a message to be cited.
3620 The inserted text should be the region.
3621 When this function returns, the region is again around the modified text.
3622
3623 Normally, indent each nonblank line `message-indentation-spaces' spaces.
3624 However, if `message-yank-prefix' is non-nil, insert that prefix on each line."
3625   (unless start (setq start (point)))
3626   (unless yank-only
3627     ;; Remove unwanted headers.
3628     (when message-ignored-cited-headers
3629       (let (all-removed)
3630         (save-restriction
3631           (narrow-to-region
3632            (goto-char start)
3633            (if (search-forward "\n\n" nil t)
3634                (1- (point))
3635              (point)))
3636           (message-remove-header message-ignored-cited-headers t)
3637           (when (= (point-min) (point-max))
3638             (setq all-removed t))
3639           (goto-char (point-max)))
3640         (if all-removed
3641             (goto-char start)
3642           (forward-line 1))))
3643     ;; Delete blank lines at the start of the buffer.
3644     (while (and (point-min)
3645                 (eolp)
3646                 (not (eobp)))
3647       (message-delete-line))
3648     ;; Delete blank lines at the end of the buffer.
3649     (goto-char (point-max))
3650     (unless (eolp)
3651       (insert "\n"))
3652     (while (and (zerop (forward-line -1))
3653                 (looking-at "$"))
3654       (message-delete-line)))
3655   ;; Do the indentation.
3656   (if (null message-yank-prefix)
3657       (indent-rigidly start (or end (mark t)) message-indentation-spaces)
3658     (save-excursion
3659       (goto-char start)
3660       (while (< (point) (or end (mark t)))
3661         (cond ((looking-at ">")
3662                (insert message-yank-cited-prefix))
3663               ((looking-at "^$")
3664                (insert message-yank-empty-prefix))
3665               (t
3666                (insert message-yank-prefix)))
3667         (forward-line 1))))
3668   (goto-char start))
3669
3670 (defun message-remove-blank-cited-lines (&optional remove)
3671   "Remove cited lines containing only blanks.
3672 If REMOVE is non-nil, remove newlines, too.
3673
3674 To use this automatically, you may add this function to
3675 `gnus-message-setup-hook'."
3676   (interactive "P")
3677   (let ((citexp
3678          (concat
3679           "^\\("
3680           (when (boundp 'message-yank-cited-prefix)
3681             (concat message-yank-cited-prefix "\\|"))
3682           message-yank-prefix
3683           "\\)+ *\n"
3684           )))
3685     (gnus-message 8 "removing `%s'" citexp)
3686     (save-excursion
3687       (message-goto-body)
3688       (while (re-search-forward citexp nil t)
3689         (replace-match (if remove "" "\n"))))))
3690
3691 (defvar message-cite-reply-above nil
3692   "If non-nil, start own text above the quote.
3693
3694 Note: Top posting is bad netiquette.  Don't use it unless you
3695 really must.  You probably want to set variable only for specific
3696 groups, e.g. using `gnus-posting-styles':
3697
3698   (eval (set (make-local-variable 'message-cite-reply-above) t))
3699
3700 This variable has no effect in news postings.")
3701
3702 (defun message-yank-original (&optional arg)
3703   "Insert the message being replied to, if any.
3704 Puts point before the text and mark after.
3705 Normally indents each nonblank line ARG spaces (default 3).  However,
3706 if `message-yank-prefix' is non-nil, insert that prefix on each line.
3707
3708 This function uses `message-cite-function' to do the actual citing.
3709
3710 Just \\[universal-argument] as argument means don't indent, insert no
3711 prefix, and don't delete any headers."
3712   (interactive "P")
3713   (let ((modified (buffer-modified-p))
3714         body-text)
3715     (when (and message-reply-buffer
3716                message-cite-function)
3717       (when message-cite-reply-above
3718         (if (and (not (message-news-p))
3719                  (or (eq message-cite-reply-above 'is-evil)
3720                      (y-or-n-p "\
3721 Top posting is bad netiquette.  Please don't top post unless you really must.
3722 Really top post? ")))
3723             (save-excursion
3724               (setq body-text
3725                     (buffer-substring (message-goto-body)
3726                                       (point-max)))
3727               (delete-region (message-goto-body) (point-max)))
3728           (set (make-local-variable 'message-cite-reply-above) nil)))
3729       (if (bufferp message-reply-buffer)
3730           (delete-windows-on message-reply-buffer t))
3731       (push-mark (save-excursion
3732                    (cond
3733                     ((bufferp message-reply-buffer)
3734                      (insert-buffer-substring message-reply-buffer))
3735                     ((and (consp message-reply-buffer)
3736                           (functionp (car message-reply-buffer)))
3737                      (apply (car message-reply-buffer)
3738                             (cdr message-reply-buffer))))
3739                    (unless (bolp)
3740                      (insert ?\n))
3741                    (point)))
3742       (unless arg
3743         (funcall message-cite-function)
3744         (unless (eq (char-before (mark t)) ?\n)
3745           (let ((pt (point)))
3746             (goto-char (mark t))
3747             (insert-before-markers ?\n)
3748             (goto-char pt))))
3749       (when message-cite-reply-above
3750         (message-goto-body)
3751         (insert body-text)
3752         (insert (if (bolp) "\n" "\n\n"))
3753         (message-goto-body))
3754       ;; Add a `message-setup-very-last-hook' here?
3755       ;; Add `gnus-article-highlight-citation' here?
3756       (unless modified
3757         (setq message-checksum (message-checksum))))))
3758
3759 (defun message-yank-buffer (buffer)
3760   "Insert BUFFER into the current buffer and quote it."
3761   (interactive "bYank buffer: ")
3762   (let ((message-reply-buffer (get-buffer buffer)))
3763     (save-window-excursion
3764       (message-yank-original))))
3765
3766 (defun message-buffers ()
3767   "Return a list of active message buffers."
3768   (let (buffers)
3769     (save-current-buffer
3770       (dolist (buffer (buffer-list t))
3771         (set-buffer buffer)
3772         (when (and (eq major-mode 'message-mode)
3773                    (null message-sent-message-via))
3774           (push (buffer-name buffer) buffers))))
3775     (nreverse buffers)))
3776
3777 (defun message-cite-original-1 (strip-signature)
3778   "Cite an original message.
3779 If STRIP-SIGNATURE is non-nil, strips off the signature from the
3780 original message.
3781
3782 This function uses `mail-citation-hook' if that is non-nil."
3783   (if (and (boundp 'mail-citation-hook)
3784            mail-citation-hook)
3785       (run-hooks 'mail-citation-hook)
3786     (let* ((start (point))
3787            (end (mark t))
3788            (x-no-archive nil)
3789            (functions
3790             (when message-indent-citation-function
3791               (if (listp message-indent-citation-function)
3792                   message-indent-citation-function
3793                 (list message-indent-citation-function))))
3794            ;; This function may be called by `gnus-summary-yank-message' and
3795            ;; may insert a different article from the original.  So, we will
3796            ;; modify the value of `message-reply-headers' with that article.
3797            (message-reply-headers
3798             (save-restriction
3799               (narrow-to-region start end)
3800               (message-narrow-to-head-1)
3801               (setq x-no-archive (message-fetch-field "x-no-archive"))
3802               (vector 0
3803                       (or (message-fetch-field "subject") "none")
3804                       (or (message-fetch-field "from") "nobody")
3805                       (message-fetch-field "date")
3806                       (message-fetch-field "message-id" t)
3807                       (message-fetch-field "references")
3808                       0 0 ""))))
3809       (mml-quote-region start end)
3810       (when strip-signature
3811         ;; Allow undoing.
3812         (undo-boundary)
3813         (goto-char end)
3814         (when (re-search-backward message-signature-separator start t)
3815           ;; Also peel off any blank lines before the signature.
3816           (forward-line -1)
3817           (while (looking-at "^[ \t]*$")
3818             (forward-line -1))
3819           (forward-line 1)
3820           (delete-region (point) end)
3821           (unless (search-backward "\n\n" start t)
3822             ;; Insert a blank line if it is peeled off.
3823             (insert "\n"))))
3824       (goto-char start)
3825       (mapc 'funcall functions)
3826       (when message-citation-line-function
3827         (unless (bolp)
3828           (insert "\n"))
3829         (funcall message-citation-line-function))
3830       (when (and x-no-archive
3831                  (not message-cite-articles-with-x-no-archive)
3832                  (string-match "yes" x-no-archive))
3833         (undo-boundary)
3834         (delete-region (point) (mark t))
3835         (insert "> [Quoted text removed due to X-No-Archive]\n")
3836         (push-mark)
3837         (forward-line -1)))))
3838
3839 (defun message-cite-original ()
3840   "Cite function in the standard Message manner."
3841   (message-cite-original-1 nil))
3842
3843 (defvar gnus-extract-address-components)
3844
3845 (autoload 'format-spec "format-spec")
3846
3847 (defun message-insert-formatted-citation-line (&optional from date)
3848   "Function that inserts a formatted citation line.
3849
3850 See `message-citation-line-format'."
3851   ;; The optional args are for testing/debugging.  They will disappear later.
3852   ;; Example:
3853   ;; (with-temp-buffer
3854   ;;   (message-insert-formatted-citation-line
3855   ;;    "John Doe <john.doe@example.invalid>"
3856   ;;    (current-time))
3857   ;;   (buffer-string))
3858   (when (or message-reply-headers (and from date))
3859     (unless from
3860       (setq from (mail-header-from message-reply-headers)))
3861     (let* ((data (condition-case ()
3862                      (funcall (if (boundp gnus-extract-address-components)
3863                                   gnus-extract-address-components
3864                                 'mail-extract-address-components)
3865                               from)
3866                    (error nil)))
3867            (name (car data))
3868            (fname name)
3869            (lname name)
3870            (net (car (cdr data)))
3871            (name-or-net (or (car data)
3872                             (car (cdr data)) from))
3873            (replydate
3874             (or
3875              date
3876              ;; We need Gnus functionality if the user wants date or time from
3877              ;; the original article:
3878              (when (string-match "%[^fnNFL]" message-citation-line-format)
3879                (autoload 'gnus-date-get-time "gnus-util")
3880                (gnus-date-get-time (mail-header-date message-reply-headers)))))
3881            (flist
3882             (let ((i ?A) lst)
3883               (when (stringp name)
3884                 ;; Guess first name and last name:
3885                 (cond ((string-match
3886                         "\\`\\(\\w\\|[-.]\\)+ \\(\\w\\|[-.]\\)+\\'" name)
3887                        (setq fname (nth 0 (split-string name "[ \t]+"))
3888                              lname (nth 1 (split-string name "[ \t]+"))))
3889                       ((string-match
3890                         "\\`\\(\\w\\|[-.]\\)+, \\(\\w\\|[-.]\\)+\\'" name)
3891                        (setq fname (nth 1 (split-string name "[ \t,]+"))
3892                              lname (nth 0 (split-string name "[ \t,]+"))))
3893                       ((string-match
3894                         "\\`\\(\\w\\|[-.]\\)+\\'" name)
3895                        (setq fname name
3896                              lname ""))))
3897               ;; The following letters are not used in `format-time-string':
3898               (push ?E lst) (push "<E>" lst)
3899               (push ?F lst) (push fname lst)
3900               ;; We might want to use "" instead of "<X>" later.
3901               (push ?J lst) (push "<J>" lst)
3902               (push ?K lst) (push "<K>" lst)
3903               (push ?L lst) (push lname lst)
3904               (push ?N lst) (push name-or-net lst)
3905               (push ?O lst) (push "<O>" lst)
3906               (push ?P lst) (push "<P>" lst)
3907               (push ?Q lst) (push "<Q>" lst)
3908               (push ?f lst) (push from lst)
3909               (push ?i lst) (push "<i>" lst)
3910               (push ?n lst) (push net lst)
3911               (push ?o lst) (push "<o>" lst)
3912               (push ?q lst) (push "<q>" lst)
3913               (push ?t lst) (push "<t>" lst)
3914               (push ?v lst) (push "<v>" lst)
3915               ;; Delegate the rest to `format-time-string':
3916               (while (<= i ?z)
3917                 (when (and (not (memq i lst))
3918                            ;; Skip (Z,a)
3919                            (or (<= i ?Z)
3920                                (>= i ?a)))
3921                   (push i lst)
3922                   (push (condition-case nil
3923                             (format-time-string (format "%%%c" i) replydate)
3924                           (error (format ">%c<" i)))
3925                         lst))
3926                 (setq i (1+ i)))
3927               (reverse lst)))
3928            (spec (apply 'format-spec-make flist)))
3929       (insert (format-spec message-citation-line-format spec)))
3930     (newline)))
3931
3932 (defun message-cite-original-without-signature ()
3933   "Cite function in the standard Message manner.
3934 This function strips off the signature from the original message."
3935   (message-cite-original-1 t))
3936
3937 (defun message-insert-citation-line ()
3938   "Insert a simple citation line."
3939   (when message-reply-headers
3940     (insert (mail-header-from message-reply-headers) " writes:")
3941     (newline)
3942     (newline)))
3943
3944 (defun message-position-on-field (header &rest afters)
3945   (let ((case-fold-search t))
3946     (save-restriction
3947       (narrow-to-region
3948        (goto-char (point-min))
3949        (progn
3950          (re-search-forward
3951           (concat "^" (regexp-quote mail-header-separator) "$"))
3952          (match-beginning 0)))
3953       (goto-char (point-min))
3954       (if (re-search-forward (concat "^" (regexp-quote header) ":") nil t)
3955           (progn
3956             (re-search-forward "^[^ \t]" nil 'move)
3957             (beginning-of-line)
3958             (skip-chars-backward "\n")
3959             t)
3960         (while (and afters
3961                     (not (re-search-forward
3962                           (concat "^" (regexp-quote (car afters)) ":")
3963                           nil t)))
3964           (pop afters))
3965         (when afters
3966           (re-search-forward "^[^ \t]" nil 'move)
3967           (beginning-of-line))
3968         (insert header ": \n")
3969         (forward-char -1)
3970         nil))))
3971
3972 (defun message-remove-signature ()
3973   "Remove the signature from the text between point and mark.
3974 The text will also be indented the normal way."
3975   (save-excursion
3976     (let ((start (point))
3977           mark)
3978       (if (not (re-search-forward message-signature-separator (mark t) t))
3979           ;; No signature here, so we just indent the cited text.
3980           (message-indent-citation)
3981         ;; Find the last non-empty line.
3982         (forward-line -1)
3983         (while (looking-at "[ \t]*$")
3984           (forward-line -1))
3985         (forward-line 1)
3986         (setq mark (set-marker (make-marker) (point)))
3987         (goto-char start)
3988         (message-indent-citation)
3989         ;; Enable undoing the deletion.
3990         (undo-boundary)
3991         (delete-region mark (mark t))
3992         (set-marker mark nil)))))
3993
3994 \f
3995
3996 ;;;
3997 ;;; Sending messages
3998 ;;;
3999
4000 (defun message-send-and-exit (&optional arg)
4001   "Send message like `message-send', then, if no errors, exit from mail buffer."
4002   (interactive "P")
4003   (let ((buf (current-buffer))
4004         (actions message-exit-actions))
4005     (when (and (message-send arg)
4006                (buffer-name buf))
4007       (message-bury buf)
4008       (if message-kill-buffer-on-exit
4009           (kill-buffer buf))
4010       (message-do-actions actions)
4011       t)))
4012
4013 (defun message-dont-send ()
4014   "Don't send the message you have been editing.
4015 Instead, just auto-save the buffer and then bury it."
4016   (interactive)
4017   (set-buffer-modified-p t)
4018   (save-buffer)
4019   (let ((actions message-postpone-actions))
4020     (message-bury (current-buffer))
4021     (message-do-actions actions)))
4022
4023 (defun message-kill-buffer ()
4024   "Kill the current buffer."
4025   (interactive)
4026   (when (or (not (buffer-modified-p))
4027             (not message-kill-buffer-query)
4028             (yes-or-no-p "Message modified; kill anyway? "))
4029     (let ((actions message-kill-actions)
4030           (draft-article message-draft-article)
4031           (auto-save-file-name buffer-auto-save-file-name)
4032           (file-name buffer-file-name)
4033           (modified (buffer-modified-p)))
4034       (setq buffer-file-name nil)
4035       (kill-buffer (current-buffer))
4036       (when (and (or (and auto-save-file-name
4037                           (file-exists-p auto-save-file-name))
4038                      (and file-name
4039                           (file-exists-p file-name)))
4040                  (progn
4041                    ;; If the message buffer has lived in a dedicated window,
4042                    ;; `kill-buffer' has killed the frame.  Thus the
4043                    ;; `yes-or-no-p' may show up in a lowered frame.  Make sure
4044                    ;; that the user can see the question by raising the
4045                    ;; current frame:
4046                    (raise-frame)
4047                    (yes-or-no-p (format "Remove the backup file%s? "
4048                                         (if modified " too" "")))))
4049         (ignore-errors
4050           (delete-file auto-save-file-name))
4051         (let ((message-draft-article draft-article))
4052           (message-disassociate-draft)))
4053       (message-do-actions actions))))
4054
4055 (defun message-bury (buffer)
4056   "Bury this mail BUFFER."
4057   (let ((newbuf (other-buffer buffer)))
4058     (bury-buffer buffer)
4059     (if message-return-action
4060         (apply (car message-return-action) (cdr message-return-action))
4061       (switch-to-buffer newbuf))))
4062
4063 (defun message-send (&optional arg)
4064   "Send the message in the current buffer.
4065 If `message-interactive' is non-nil, wait for success indication or
4066 error messages, and inform user.
4067 Otherwise any failure is reported in a message back to the user from
4068 the mailer.
4069 The usage of ARG is defined by the instance that called Message.
4070 It should typically alter the sending method in some way or other."
4071   (interactive "P")
4072   ;; Make it possible to undo the coming changes.
4073   (undo-boundary)
4074   (let ((inhibit-read-only t))
4075     (put-text-property (point-min) (point-max) 'read-only nil))
4076   (message-fix-before-sending)
4077   (run-hooks 'message-send-hook)
4078   (when message-confirm-send
4079     (or (y-or-n-p "Send message? ")
4080         (keyboard-quit)))
4081   (message message-sending-message)
4082   (let ((alist message-send-method-alist)
4083         (success t)
4084         elem sent dont-barf-on-no-method
4085         (message-options message-options))
4086     (message-options-set-recipient)
4087     (while (and success
4088                 (setq elem (pop alist)))
4089       (when (funcall (cadr elem))
4090         (when (and (or (not (memq (car elem)
4091                                   message-sent-message-via))
4092                        (message-fetch-field "supersedes")
4093                        (if (or (message-gnksa-enable-p 'multiple-copies)
4094                                (not (eq (car elem) 'news)))
4095                            (y-or-n-p
4096                             (format
4097                              "Already sent message via %s; resend? "
4098                              (car elem)))
4099                          (error "Denied posting -- multiple copies")))
4100                    (setq success (funcall (caddr elem) arg)))
4101           (setq sent t))))
4102     (unless (or sent
4103                 (not success)
4104                 (let ((fcc (message-fetch-field "Fcc"))
4105                       (gcc (message-fetch-field "Gcc")))
4106                   (when (or fcc gcc)
4107                     (or (eq message-allow-no-recipients 'always)
4108                         (and (not (eq message-allow-no-recipients 'never))
4109                              (setq dont-barf-on-no-method
4110                                    (gnus-y-or-n-p
4111                                     (format "No receiver, perform %s anyway? "
4112                                             (cond ((and fcc gcc) "Fcc and Gcc")
4113                                                   (fcc "Fcc")
4114                                                   (t "Gcc"))))))))))
4115       (error "No methods specified to send by"))
4116     (when (or dont-barf-on-no-method
4117               (and success sent))
4118       (message-do-fcc)
4119       (save-excursion
4120         (run-hooks 'message-sent-hook))
4121       (message "Sending...done")
4122       ;; Do ecomplete address snarfing.
4123       (when (and (message-mail-alias-type-p 'ecomplete)
4124                  (not message-inhibit-ecomplete))
4125         (message-put-addresses-in-ecomplete))
4126       ;; Mark the buffer as unmodified and delete auto-save.
4127       (set-buffer-modified-p nil)
4128       (delete-auto-save-file-if-necessary t)
4129       (message-disassociate-draft)
4130       ;; Delete other mail buffers and stuff.
4131       (message-do-send-housekeeping)
4132       (message-do-actions message-send-actions)
4133       ;; Return success.
4134       t)))
4135
4136 (defun message-send-via-mail (arg)
4137   "Send the current message via mail."
4138   (message-send-mail arg))
4139
4140 (defun message-send-via-news (arg)
4141   "Send the current message via news."
4142   (funcall message-send-news-function arg))
4143
4144 (defmacro message-check (type &rest forms)
4145   "Eval FORMS if TYPE is to be checked."
4146   `(or (message-check-element ,type)
4147        (save-excursion
4148          ,@forms)))
4149
4150 (put 'message-check 'lisp-indent-function 1)
4151 (put 'message-check 'edebug-form-spec '(form body))
4152
4153 (defun message-text-with-property (prop &optional start end reverse)
4154   "Return a list of start and end positions where the text has PROP.
4155 START and END bound the search, they default to `point-min' and
4156 `point-max' respectively.  If REVERSE is non-nil, find text which does
4157 not have PROP."
4158   (unless start
4159     (setq start (point-min)))
4160   (unless end
4161     (setq end (point-max)))
4162   (let (next regions)
4163     (if reverse
4164         (while (and start
4165                     (setq start (text-property-any start end prop nil)))
4166           (setq next (next-single-property-change start prop nil end))
4167           (push (cons start (or next end)) regions)
4168           (setq start next))
4169       (while (and start
4170                   (or (get-text-property start prop)
4171                       (and (setq start (next-single-property-change
4172                                         start prop nil end))
4173                            (get-text-property start prop))))
4174         (setq next (text-property-any start end prop nil))
4175         (push (cons start (or next end)) regions)
4176         (setq start next)))
4177     (nreverse regions)))
4178
4179 (defcustom message-bogus-addresses
4180   '("noreply" "nospam" "invalid" "@@" "[^[:ascii:]].*@" "[ \t]")
4181   "List of regexps of potentially bogus mail addresses.
4182 See `message-check-recipients' how to setup checking.
4183
4184 This list should make it possible to catch typos or warn about
4185 spam-trap addresses.  It doesn't aim to verify strict RFC
4186 conformance."
4187   :version "23.1" ;; No Gnus
4188   :group 'message-headers
4189   :type '(choice
4190           (const :tag "None" nil)
4191           (list
4192            (set :inline t
4193                 (const "noreply")
4194                 (const "nospam")
4195                 (const "invalid")
4196                 (const :tag "duplicate @" "@@")
4197                 (const :tag "non-ascii local part" "[^[:ascii:]].*@")
4198                 ;; Already caught by `message-valid-fqdn-regexp'
4199                 ;; (const :tag "`_' in domain part" "@.*_")
4200                 (const :tag "whitespace" "[ \t]"))
4201            (repeat :inline t
4202                    :tag "Other"
4203                    (regexp)))))
4204
4205 (defun message-fix-before-sending ()
4206   "Do various things to make the message nice before sending it."
4207   ;; Make sure there's a newline at the end of the message.
4208   (goto-char (point-max))
4209   (unless (bolp)
4210     (insert "\n"))
4211   ;; Make the hidden headers visible.
4212   (widen)
4213   ;; Sort headers before sending the message.
4214   (message-sort-headers)
4215   ;; Make invisible text visible.
4216   ;; It doesn't seem as if this is useful, since the invisible property
4217   ;; is clobbered by an after-change hook anyhow.
4218   (message-check 'invisible-text
4219     (let ((regions (message-text-with-property 'invisible))
4220           from to)
4221       (when regions
4222         (while regions
4223           (setq from (caar regions)
4224                 to (cdar regions)
4225                 regions (cdr regions))
4226           (put-text-property from to 'invisible nil)
4227           (message-overlay-put (message-make-overlay from to)
4228                                'face 'highlight))
4229         (unless (yes-or-no-p
4230                  "Invisible text found and made visible; continue sending? ")
4231           (error "Invisible text found and made visible")))))
4232   (message-check 'illegible-text
4233     (let (char found choice)
4234       (message-goto-body)
4235       (while (progn
4236                (skip-chars-forward mm-7bit-chars)
4237                (when (get-text-property (point) 'no-illegible-text)
4238                  ;; There is a signed or encrypted raw message part
4239                  ;; that is considered to be safe.
4240                  (goto-char (or (next-single-property-change
4241                                  (point) 'no-illegible-text)
4242                                 (point-max))))
4243                (setq char (char-after)))
4244         (when (or (< (mm-char-int char) 128)
4245                   (and (mm-multibyte-p)
4246                        (memq (char-charset char)
4247                              '(eight-bit-control eight-bit-graphic
4248                                                  ;; Emacs 23, Bug#1770:
4249                                                  eight-bit
4250                                                  control-1))
4251                        (not (get-text-property
4252                              (point) 'untranslated-utf-8))))
4253           (message-overlay-put (message-make-overlay (point) (1+ (point)))
4254                                'face 'highlight)
4255           (setq found t))
4256         (forward-char))
4257       (when found
4258         (setq choice
4259               (gnus-multiple-choice
4260                "Non-printable characters found.  Continue sending?"
4261                `((?d "Remove non-printable characters and send")
4262                  (?r ,(format
4263                        "Replace non-printable characters with \"%s\" and send"
4264                        message-replacement-char))
4265                  (?s "Send as is without removing anything")
4266                  (?e "Continue editing"))))
4267         (if (eq choice ?e)
4268           (error "Non-printable characters"))
4269         (message-goto-body)
4270         (skip-chars-forward mm-7bit-chars)
4271         (while (not (eobp))
4272           (when (let ((char (char-after)))
4273                   (or (< (mm-char-int char) 128)
4274                       (and (mm-multibyte-p)
4275                            ;; FIXME: Wrong for Emacs 23 (unicode) and for
4276                            ;; things like undecodable utf-8 (in Emacs 21?).
4277                            ;; Should at least use find-coding-systems-region.
4278                            ;; -- fx
4279                            (memq (char-charset char)
4280                                  '(eight-bit-control eight-bit-graphic
4281                                                      ;; Emacs 23, Bug#1770:
4282                                                      eight-bit
4283                                                      control-1))
4284                            (not (get-text-property
4285                                  (point) 'untranslated-utf-8)))))
4286             (if (eq choice ?i)
4287                 (message-kill-all-overlays)
4288               (delete-char 1)
4289               (when (eq choice ?r)
4290                 (insert message-replacement-char))))
4291           (forward-char)
4292           (skip-chars-forward mm-7bit-chars)))))
4293   (message-check 'bogus-recipient
4294     ;; Warn before sending a mail to an invalid address.
4295     (message-check-recipients)))
4296
4297 (defun message-bogus-recipient-p (recipients)
4298   "Check if a mail address in RECIPIENTS looks bogus.
4299
4300 RECIPIENTS is a mail header.  Return a list of potentially bogus
4301 addresses.  If none is found, return nil.
4302
4303 An address might be bogus if the domain part is not fully
4304 qualified, see `message-valid-fqdn-regexp', or if there's a
4305 matching entry in `message-bogus-addresses'."
4306   ;; FIXME: How about "foo@subdomain", when the MTA adds ".domain.tld"?
4307   (let (found)
4308     (mapc (lambda (address)
4309             (setq address (or (cadr address) ""))
4310             (when
4311                 (or (string= "" address)
4312                     (not
4313                      (or
4314                       (not (string-match "@" address))
4315                       (string-match
4316                        (concat ".@.*\\("
4317                                message-valid-fqdn-regexp "\\)\\'") address)))
4318                     (and message-bogus-addresses
4319                          (let ((re
4320                                 (if (listp message-bogus-addresses)
4321                                     (mapconcat 'identity
4322                                                message-bogus-addresses
4323                                                "\\|")
4324                                   message-bogus-addresses)))
4325                            (string-match re address))))
4326               (push address found)))
4327           ;;
4328           (mail-extract-address-components recipients t))
4329     found))
4330
4331 (defun message-check-recipients ()
4332   "Warn before composing or sending a mail to an invalid address.
4333
4334 This function could be useful in `message-setup-hook'."
4335   (interactive)
4336   (save-restriction
4337     (message-narrow-to-headers)
4338     (dolist (hdr '("To" "Cc" "Bcc"))
4339       (let ((addr (message-fetch-field hdr)))
4340         (when (stringp addr)
4341           (dolist (bog (message-bogus-recipient-p addr))
4342             (and bog
4343                  (not (y-or-n-p
4344                        (format
4345                         "Address `%s'%s might be bogus.  Continue? "
4346                         bog
4347                         ;; If the encoded version of the email address
4348                         ;; is different from the unencoded version,
4349                         ;; then we likely have invisible characters or
4350                         ;; the like.  Display the encoded version,
4351                         ;; too.
4352                         (let ((encoded (rfc2047-encode-string bog)))
4353                           (if (string= encoded bog)
4354                               ""
4355                             (format " (%s)" encoded))))))
4356                  (error "Bogus address"))))))))
4357
4358 (custom-add-option 'message-setup-hook 'message-check-recipients)
4359
4360 (defun message-add-action (action &rest types)
4361   "Add ACTION to be performed when doing an exit of type TYPES."
4362   (while types
4363     (add-to-list (intern (format "message-%s-actions" (pop types)))
4364                  action)))
4365
4366 (defun message-delete-action (action &rest types)
4367   "Delete ACTION from lists of actions performed when doing an exit of type TYPES."
4368   (let (var)
4369     (while types
4370       (set (setq var (intern (format "message-%s-actions" (pop types))))
4371            (delq action (symbol-value var))))))
4372
4373 (defun message-do-actions (actions)
4374   "Perform all actions in ACTIONS."
4375   ;; Now perform actions on successful sending.
4376   (dolist (action actions)
4377     (ignore-errors
4378       (cond
4379        ;; A simple function.
4380        ((functionp action)
4381         (funcall action))
4382        ;; Something to be evaled.
4383        (t
4384         (eval action))))))
4385
4386 (defun message-send-mail-partially ()
4387   "Send mail as message/partial."
4388   ;; replace the header delimiter with a blank line
4389   (goto-char (point-min))
4390   (re-search-forward
4391    (concat "^" (regexp-quote mail-header-separator) "\n"))
4392   (replace-match "\n")
4393   (run-hooks 'message-send-mail-hook)
4394   (let ((p (goto-char (point-min)))
4395         (tembuf (message-generate-new-buffer-clone-locals " message temp"))
4396         (curbuf (current-buffer))
4397         (id (message-make-message-id)) (n 1)
4398         plist total header)
4399     (while (not (eobp))
4400       (if (< (point-max) (+ p message-send-mail-partially-limit))
4401           (goto-char (point-max))
4402         (goto-char (+ p message-send-mail-partially-limit))
4403         (beginning-of-line)
4404         (if (<= (point) p) (forward-line 1))) ;; In case of bad message.
4405       (push p plist)
4406       (setq p (point)))
4407     (setq total (length plist))
4408     (push (point-max) plist)
4409     (setq plist (nreverse plist))
4410     (unwind-protect
4411         (save-excursion
4412           (setq p (pop plist))
4413           (while plist
4414             (set-buffer curbuf)
4415             (copy-to-buffer tembuf p (car plist))
4416             (set-buffer tembuf)
4417             (goto-char (point-min))
4418             (if header
4419                 (progn
4420                   (goto-char (point-min))
4421                   (narrow-to-region (point) (point))
4422                   (insert header))
4423               (message-goto-eoh)
4424               (setq header (buffer-substring (point-min) (point)))
4425               (goto-char (point-min))
4426               (narrow-to-region (point) (point))
4427               (insert header)
4428               (message-remove-header "Mime-Version")
4429               (message-remove-header "Content-Type")
4430               (message-remove-header "Content-Transfer-Encoding")
4431               (message-remove-header "Message-ID")
4432               (message-remove-header "Lines")
4433               (goto-char (point-max))
4434               (insert "Mime-Version: 1.0\n")
4435               (setq header (buffer-string)))
4436             (goto-char (point-max))
4437             (insert (format "Content-Type: message/partial; id=\"%s\"; number=%d; total=%d\n\n"
4438                             id n total))
4439             (forward-char -1)
4440             (let ((mail-header-separator ""))
4441               (when (memq 'Message-ID message-required-mail-headers)
4442                 (insert "Message-ID: " (message-make-message-id) "\n"))
4443               (when (memq 'Lines message-required-mail-headers)
4444                 (insert "Lines: " (message-make-lines) "\n"))
4445               (message-goto-subject)
4446               (end-of-line)
4447               (insert (format " (%d/%d)" n total))
4448               (widen)
4449               (funcall (or message-send-mail-real-function
4450                            message-send-mail-function)))
4451             (setq n (+ n 1))
4452             (setq p (pop plist))
4453             (erase-buffer)))
4454       (kill-buffer tembuf))))
4455
4456 (declare-function hashcash-wait-async "hashcash" (&optional buffer))
4457
4458 (defun message-send-mail (&optional arg)
4459   (require 'mail-utils)
4460   (let* ((tembuf (message-generate-new-buffer-clone-locals " message temp"))
4461          (case-fold-search nil)
4462          (news (message-news-p))
4463          (mailbuf (current-buffer))
4464          (message-this-is-mail t)
4465          ;; gnus-setup-posting-charset is autoloaded in mml.el (FIXME
4466          ;; maybe it should not be), which this file requires.  Hence
4467          ;; the fboundp test is always true.  Loading it from gnus-msg
4468          ;; loads many Gnus files (Bug#5642).  If
4469          ;; gnus-group-posting-charset-alist hasn't been customized,
4470          ;; this is just going to return nil anyway.  FIXME it would
4471          ;; be good to improve this further, because even if g-g-p-c-a
4472          ;; has been customized, that is likely to just be for news.
4473          ;; Eg either move the definition from gnus-msg, or separate out
4474          ;; the mail and news parts.
4475          (message-posting-charset
4476           (if (and (fboundp 'gnus-setup-posting-charset)
4477                    (boundp 'gnus-group-posting-charset-alist))
4478               (gnus-setup-posting-charset nil)
4479             message-posting-charset))
4480          (headers message-required-mail-headers))
4481     (when (and message-generate-hashcash
4482                (not (eq message-generate-hashcash 'opportunistic)))
4483       (message "Generating hashcash...")
4484       (require 'hashcash)
4485       ;; Wait for calculations already started to finish...
4486       (hashcash-wait-async)
4487       ;; ...and do calculations not already done.  mail-add-payment
4488       ;; will leave existing X-Hashcash headers alone.
4489       (mail-add-payment)
4490       (message "Generating hashcash...done"))
4491     (save-restriction
4492       (message-narrow-to-headers)
4493       ;; Generate the Mail-Followup-To header if the header is not there...
4494       (if (and (message-subscribed-p)
4495                (not (mail-fetch-field "mail-followup-to")))
4496           (setq headers
4497                 (cons
4498                  (cons "Mail-Followup-To" (message-make-mail-followup-to))
4499                  message-required-mail-headers))
4500         ;; otherwise, delete the MFT header if the field is empty
4501         (when (equal "" (mail-fetch-field "mail-followup-to"))
4502           (message-remove-header "^Mail-Followup-To:")))
4503       ;; Insert some headers.
4504       (let ((message-deletable-headers
4505              (if news nil message-deletable-headers)))
4506         (message-generate-headers headers))
4507       ;; Check continuation headers.
4508       (message-check 'continuation-headers
4509         (goto-char (point-min))
4510         (while (re-search-forward "^[^ \t\n][^ \t\n:]*[ \t\n]" nil t)
4511           (goto-char (match-beginning 0))
4512           (if (y-or-n-p "Fix continuation lines? ")
4513               (insert " ")
4514             (forward-line 1)
4515             (unless (y-or-n-p "Send anyway? ")
4516               (error "Failed to send the message")))))
4517       ;; Let the user do all of the above.
4518       (run-hooks 'message-header-hook))
4519     (unwind-protect
4520         (with-current-buffer tembuf
4521           (erase-buffer)
4522           ;; Avoid copying text props (except hard newlines).
4523           (insert (with-current-buffer mailbuf
4524                     (mml-buffer-substring-no-properties-except-hard-newlines
4525                      (point-min) (point-max))))
4526           ;; Remove some headers.
4527           (message-encode-message-body)
4528           (save-restriction
4529             (message-narrow-to-headers)
4530             ;; We (re)generate the Lines header.
4531             (when (memq 'Lines message-required-mail-headers)
4532               (message-generate-headers '(Lines)))
4533             ;; Remove some headers.
4534             (message-remove-header message-ignored-mail-headers t)
4535             (let ((mail-parse-charset message-default-charset))
4536               (mail-encode-encoded-word-buffer)))
4537           (goto-char (point-max))
4538           ;; require one newline at the end.
4539           (or (= (preceding-char) ?\n)
4540               (insert ?\n))
4541           (message-cleanup-headers)
4542           ;; FIXME: we're inserting the courtesy copy after encoding.
4543           ;; This is wrong if the courtesy copy string contains
4544           ;; non-ASCII characters. -- jh
4545           (when
4546               (save-restriction
4547                 (message-narrow-to-headers)
4548                 (and news
4549                      (not (message-fetch-field "List-Post"))
4550                      (not (message-fetch-field "List-ID"))
4551                      (or (message-fetch-field "cc")
4552                          (message-fetch-field "bcc")
4553                          (message-fetch-field "to"))
4554                      (let ((content-type (message-fetch-field
4555                                           "content-type")))
4556                        (and
4557                         (or
4558                          (not content-type)
4559                          (string= "text/plain"
4560                                   (car
4561                                    (mail-header-parse-content-type
4562                                     content-type))))
4563                         (not
4564                          (string= "base64"
4565                                   (message-fetch-field
4566                                    "content-transfer-encoding")))))))
4567             (message-insert-courtesy-copy
4568              (with-current-buffer mailbuf
4569                message-courtesy-message)))
4570           ;; Let's make sure we encoded all the body.
4571           (assert (save-excursion
4572                     (goto-char (point-min))
4573                     (not (re-search-forward "[^\000-\377]" nil t))))
4574           (mm-disable-multibyte)
4575           (if (or (not message-send-mail-partially-limit)
4576                   (< (buffer-size) message-send-mail-partially-limit)
4577                   (not (message-y-or-n-p
4578                         "The message size is too large, split? "
4579                         t
4580                         "\
4581 The message size, "
4582                         (/ (buffer-size) 1000) "KB, is too large.
4583
4584 Some mail gateways (MTA's) bounce large messages.  To avoid the
4585 problem, answer `y', and the message will be split into several
4586 smaller pieces, the size of each is about "
4587                         (/ message-send-mail-partially-limit 1000)
4588                         "KB except the last
4589 one.
4590
4591 However, some mail readers (MUA's) can't read split messages, i.e.,
4592 mails in message/partially format. Answer `n', and the message will be
4593 sent in one piece.
4594
4595 The size limit is controlled by `message-send-mail-partially-limit'.
4596 If you always want Gnus to send messages in one piece, set
4597 `message-send-mail-partially-limit' to nil.
4598 ")))
4599               (progn
4600                 (message "Sending via mail...")
4601                 (funcall (or message-send-mail-real-function
4602                              message-send-mail-function)))
4603             (message-send-mail-partially)))
4604       (kill-buffer tembuf))
4605     (set-buffer mailbuf)
4606     (push 'mail message-sent-message-via)))
4607
4608 (defun message-send-mail-with-sendmail ()
4609   "Send off the prepared buffer with sendmail."
4610   (require 'sendmail)
4611   (let ((errbuf (if message-interactive
4612                     (message-generate-new-buffer-clone-locals
4613                      " sendmail errors")
4614                   0))
4615         resend-to-addresses delimline)
4616     (unwind-protect
4617         (progn
4618           (let ((case-fold-search t))
4619             (save-restriction
4620               (message-narrow-to-headers)
4621               (setq resend-to-addresses (message-fetch-field "resent-to")))
4622             ;; Change header-delimiter to be what sendmail expects.
4623             (goto-char (point-min))
4624             (re-search-forward
4625              (concat "^" (regexp-quote mail-header-separator) "\n"))
4626             (replace-match "\n")
4627             (backward-char 1)
4628             (setq delimline (point-marker))
4629             (run-hooks 'message-send-mail-hook)
4630             ;; Insert an extra newline if we need it to work around
4631             ;; Sun's bug that swallows newlines.
4632             (goto-char (1+ delimline))
4633             (when (eval message-mailer-swallows-blank-line)
4634               (newline))
4635             (when message-interactive
4636               (with-current-buffer errbuf
4637                 (erase-buffer))))
4638           (let* ((default-directory "/")
4639                  (coding-system-for-write message-send-coding-system)
4640                  (cpr (apply
4641                        'call-process-region
4642                        (append
4643                         (list (point-min) (point-max)
4644                               (cond ((boundp 'sendmail-program)
4645                                      sendmail-program)
4646                                     ((file-exists-p "/usr/sbin/sendmail")
4647                                      "/usr/sbin/sendmail")
4648                                     ((file-exists-p "/usr/lib/sendmail")
4649                                      "/usr/lib/sendmail")
4650                                     ((file-exists-p "/usr/ucblib/sendmail")
4651                                      "/usr/ucblib/sendmail")
4652                                     (t "fakemail"))
4653                               nil errbuf nil "-oi")
4654                         message-sendmail-extra-arguments
4655                         ;; Always specify who from,
4656                         ;; since some systems have broken sendmails.
4657                         ;; But some systems are more broken with -f, so
4658                         ;; we'll let users override this.
4659                         (and (null message-sendmail-f-is-evil)
4660                              (list "-f" (message-sendmail-envelope-from)))
4661                         ;; These mean "report errors by mail"
4662                         ;; and "deliver in background".
4663                         (if (null message-interactive) '("-oem" "-odb"))
4664                         ;; Get the addresses from the message
4665                         ;; unless this is a resend.
4666                         ;; We must not do that for a resend
4667                         ;; because we would find the original addresses.
4668                         ;; For a resend, include the specific addresses.
4669                         (if resend-to-addresses
4670                             (list resend-to-addresses)
4671                           '("-t"))))))
4672             (unless (or (null cpr) (and (numberp cpr) (zerop cpr)))
4673               (if errbuf (pop-to-buffer errbuf))
4674               (error "Sending...failed with exit value %d" cpr)))
4675           (when message-interactive
4676             (with-current-buffer errbuf
4677               (goto-char (point-min))
4678               (while (re-search-forward "\n+ *" nil t)
4679                 (replace-match "; "))
4680               (if (not (zerop (buffer-size)))
4681                   (error "Sending...failed to %s"
4682                          (buffer-string))))))
4683       (when (bufferp errbuf)
4684         (kill-buffer errbuf)))))
4685
4686 (defun message-send-mail-with-qmail ()
4687   "Pass the prepared message buffer to qmail-inject.
4688 Refer to the documentation for the variable `message-send-mail-function'
4689 to find out how to use this."
4690   ;; replace the header delimiter with a blank line
4691   (goto-char (point-min))
4692   (re-search-forward
4693    (concat "^" (regexp-quote mail-header-separator) "\n"))
4694   (replace-match "\n")
4695   (run-hooks 'message-send-mail-hook)
4696   ;; send the message
4697   (case
4698       (let ((coding-system-for-write message-send-coding-system))
4699         (apply
4700          'call-process-region (point-min) (point-max)
4701          message-qmail-inject-program nil nil nil
4702          ;; qmail-inject's default behavior is to look for addresses on the
4703          ;; command line; if there're none, it scans the headers.
4704          ;; yes, it does The Right Thing w.r.t. Resent-To and it's kin.
4705          ;;
4706          ;; in general, ALL of qmail-inject's defaults are perfect for simply
4707          ;; reading a formatted (i. e., at least a To: or Resent-To header)
4708          ;; message from stdin.
4709          ;;
4710          ;; qmail also has the advantage of not having been raped by
4711          ;; various vendors, so we don't have to allow for that, either --
4712          ;; compare this with message-send-mail-with-sendmail and weep
4713          ;; for sendmail's lost innocence.
4714          ;;
4715          ;; all this is way cool coz it lets us keep the arguments entirely
4716          ;; free for -inject-arguments -- a big win for the user and for us
4717          ;; since we don't have to play that double-guessing game and the user
4718          ;; gets full control (no gestapo'ish -f's, for instance).  --sj
4719          (if (functionp message-qmail-inject-args)
4720              (funcall message-qmail-inject-args)
4721            message-qmail-inject-args)))
4722     ;; qmail-inject doesn't say anything on it's stdout/stderr,
4723     ;; we have to look at the retval instead
4724     (0 nil)
4725     (100 (error "qmail-inject reported permanent failure"))
4726     (111 (error "qmail-inject reported transient failure"))
4727     ;; should never happen
4728     (t   (error "qmail-inject reported unknown failure"))))
4729
4730 (defvar mh-previous-window-config)
4731
4732 (defun message-send-mail-with-mh ()
4733   "Send the prepared message buffer with mh."
4734   (let ((mh-previous-window-config nil)
4735         (name (mh-new-draft-name)))
4736     (setq buffer-file-name name)
4737     ;; MH wants to generate these headers itself.
4738     (when message-mh-deletable-headers
4739       (let ((headers message-mh-deletable-headers))
4740         (while headers
4741           (goto-char (point-min))
4742           (and (re-search-forward
4743                 (concat "^" (symbol-name (car headers)) ": *") nil t)
4744                (message-delete-line))
4745           (pop headers))))
4746     (run-hooks 'message-send-mail-hook)
4747     ;; Pass it on to mh.
4748     (mh-send-letter)))
4749
4750 (defun message-smtpmail-send-it ()
4751   "Send the prepared message buffer with `smtpmail-send-it'.
4752 The only difference from `smtpmail-send-it' is that this command
4753 evaluates `message-send-mail-hook' just before sending a message.
4754 It is useful if your ISP requires the POP-before-SMTP
4755 authentication.  See the Gnus manual for details."
4756   (run-hooks 'message-send-mail-hook)
4757   (smtpmail-send-it))
4758
4759 (defun message-send-mail-with-mailclient ()
4760   "Send the prepared message buffer with `mailclient-send-it'.
4761 The only difference from `mailclient-send-it' is that this
4762 command evaluates `message-send-mail-hook' just before sending a message."
4763   (run-hooks 'message-send-mail-hook)
4764   (mailclient-send-it))
4765
4766 (defun message-canlock-generate ()
4767   "Return a string that is non-trivial to guess.
4768 Do not use this for anything important, it is cryptographically weak."
4769   (require 'sha1)
4770   (let (sha1-maximum-internal-length)
4771     (sha1 (concat (message-unique-id)
4772                   (format "%x%x%x" (random) (random t) (random))
4773                   (prin1-to-string (recent-keys))
4774                   (prin1-to-string (garbage-collect))))))
4775
4776 (defvar canlock-password)
4777 (defvar canlock-password-for-verify)
4778
4779 (defun message-canlock-password ()
4780   "The password used by message for cancel locks.
4781 This is the value of `canlock-password', if that option is non-nil.
4782 Otherwise, generate and save a value for `canlock-password' first."
4783   (require 'canlock)
4784   (unless canlock-password
4785     (customize-save-variable 'canlock-password (message-canlock-generate))
4786     (setq canlock-password-for-verify canlock-password))
4787   canlock-password)
4788
4789 (defun message-insert-canlock ()
4790   (when message-insert-canlock
4791     (message-canlock-password)
4792     (canlock-insert-header)))
4793
4794 (autoload 'nnheader-get-report "nnheader")
4795
4796 (declare-function gnus-setup-posting-charset "gnus-msg" (group))
4797
4798 (defun message-send-news (&optional arg)
4799   (require 'gnus-msg)
4800   (let* ((tembuf (message-generate-new-buffer-clone-locals " *message temp*"))
4801          (case-fold-search nil)
4802          (method (if (functionp message-post-method)
4803                      (funcall message-post-method arg)
4804                    message-post-method))
4805          (newsgroups-field (save-restriction
4806                             (message-narrow-to-headers-or-head)
4807                             (message-fetch-field "Newsgroups")))
4808          (followup-field (save-restriction
4809                            (message-narrow-to-headers-or-head)
4810                            (message-fetch-field "Followup-To")))
4811          ;; BUG: We really need to get the charset for each name in the
4812          ;; Newsgroups and Followup-To lines to allow crossposting
4813          ;; between group namess with incompatible character sets.
4814          ;; -- Per Abrahamsen <abraham@dina.kvl.dk> 2001-10-08.
4815          (group-field-charset
4816           (gnus-group-name-charset method newsgroups-field))
4817          (followup-field-charset
4818           (gnus-group-name-charset method (or followup-field "")))
4819          (rfc2047-header-encoding-alist
4820           (append (when group-field-charset
4821                     (list (cons "Newsgroups" group-field-charset)))
4822                   (when followup-field-charset
4823                     (list (cons "Followup-To" followup-field-charset)))
4824                   rfc2047-header-encoding-alist))
4825          (messbuf (current-buffer))
4826          (message-syntax-checks
4827           (if (and arg
4828                    (listp message-syntax-checks))
4829               (cons '(existing-newsgroups . disabled)
4830                     message-syntax-checks)
4831             message-syntax-checks))
4832          (message-this-is-news t)
4833          (message-posting-charset
4834           (gnus-setup-posting-charset newsgroups-field))
4835          result)
4836     (if (not (message-check-news-body-syntax))
4837         nil
4838       (save-restriction
4839         (message-narrow-to-headers)
4840         ;; Insert some headers.
4841         (message-generate-headers message-required-news-headers)
4842         (message-insert-canlock)
4843         ;; Let the user do all of the above.
4844         (run-hooks 'message-header-hook))
4845       ;; Note: This check will be disabled by the ".*" default value for
4846       ;; gnus-group-name-charset-group-alist. -- Pa 2001-10-07.
4847       (when (and group-field-charset
4848                  (listp message-syntax-checks))
4849         (setq message-syntax-checks
4850               (cons '(valid-newsgroups . disabled)
4851                     message-syntax-checks)))
4852       (message-cleanup-headers)
4853       (if (not (let ((message-post-method method))
4854                  (message-check-news-syntax)))
4855           nil
4856         (unwind-protect
4857             (with-current-buffer tembuf
4858               (buffer-disable-undo)
4859               (erase-buffer)
4860               ;; Avoid copying text props (except hard newlines).
4861               (insert
4862                (with-current-buffer messbuf
4863                  (mml-buffer-substring-no-properties-except-hard-newlines
4864                   (point-min) (point-max))))
4865               (message-encode-message-body)
4866               ;; Remove some headers.
4867               (save-restriction
4868                 (message-narrow-to-headers)
4869                 ;; We (re)generate the Lines header.
4870                 (when (memq 'Lines message-required-mail-headers)
4871                   (message-generate-headers '(Lines)))
4872                 ;; Remove some headers.
4873                 (message-remove-header message-ignored-news-headers t)
4874                 (let ((mail-parse-charset message-default-charset))
4875                   (mail-encode-encoded-word-buffer)))
4876               (goto-char (point-max))
4877               ;; require one newline at the end.
4878               (or (= (preceding-char) ?\n)
4879                   (insert ?\n))
4880               (let ((case-fold-search t))
4881                 ;; Remove the delimiter.
4882                 (goto-char (point-min))
4883                 (re-search-forward
4884                  (concat "^" (regexp-quote mail-header-separator) "\n"))
4885                 (replace-match "\n")
4886                 (backward-char 1))
4887               (run-hooks 'message-send-news-hook)
4888               (gnus-open-server method)
4889               (message "Sending news via %s..." (gnus-server-string method))
4890               (setq result (let ((mail-header-separator ""))
4891                              (gnus-request-post method))))
4892           (kill-buffer tembuf))
4893         (set-buffer messbuf)
4894         (if result
4895             (push 'news message-sent-message-via)
4896           (message "Couldn't send message via news: %s"
4897                    (nnheader-get-report (car method)))
4898           nil)))))
4899
4900 ;;;
4901 ;;; Header generation & syntax checking.
4902 ;;;
4903
4904 (defun message-check-element (type)
4905   "Return non-nil if this TYPE is not to be checked."
4906   (if (eq message-syntax-checks 'dont-check-for-anything-just-trust-me)
4907       t
4908     (let ((able (assq type message-syntax-checks)))
4909       (and (consp able)
4910            (eq (cdr able) 'disabled)))))
4911
4912 (defun message-check-news-syntax ()
4913   "Check the syntax of the message."
4914   (save-excursion
4915     (save-restriction
4916       (widen)
4917       ;; We narrow to the headers and check them first.
4918       (save-excursion
4919         (save-restriction
4920           (message-narrow-to-headers)
4921           (message-check-news-header-syntax))))))
4922
4923 (defun message-check-news-header-syntax ()
4924   (and
4925    ;; Check Newsgroups header.
4926    (message-check 'newsgroups
4927      (let ((group (message-fetch-field "newsgroups")))
4928        (or
4929         (and group
4930              (not (string-match "\\`[ \t]*\\'" group)))
4931         (ignore
4932          (message
4933           "The newsgroups field is empty or missing.  Posting is denied.")))))
4934    ;; Check the Subject header.
4935    (message-check 'subject
4936      (let* ((case-fold-search t)
4937             (subject (message-fetch-field "subject")))
4938        (or
4939         (and subject
4940              (not (string-match "\\`[ \t]*\\'" subject)))
4941         (ignore
4942          (message
4943           "The subject field is empty or missing.  Posting is denied.")))))
4944    ;; Check for commands in Subject.
4945    (message-check 'subject-cmsg
4946      (if (string-match "^cmsg " (message-fetch-field "subject"))
4947          (y-or-n-p
4948           "The control code \"cmsg\" is in the subject.  Really post? ")
4949        t))
4950    ;; Check long header lines.
4951    (message-check 'long-header-lines
4952      (let ((header nil)
4953            (length 0)
4954            found)
4955        (while (and (not found)
4956                    (re-search-forward "^\\([^ \t:]+\\): " nil t))
4957          (if (> (- (point) (match-beginning 0)) 998)
4958              (setq found t
4959                    length (- (point) (match-beginning 0)))
4960            (setq header (match-string-no-properties 1)))
4961          (forward-line 1))
4962        (if found
4963            (y-or-n-p (format "Your %s header is too long (%d).  Really post? "
4964                              header length))
4965          t)))
4966    ;; Check for multiple identical headers.
4967    (message-check 'multiple-headers
4968      (let (found)
4969        (while (and (not found)
4970                    (re-search-forward "^[^ \t:]+: " nil t))
4971          (save-excursion
4972            (or (re-search-forward
4973                 (concat "^"
4974                         (regexp-quote
4975                          (setq found
4976                                (buffer-substring
4977                                 (match-beginning 0) (- (match-end 0) 2))))
4978                         ":")
4979                 nil t)
4980                (setq found nil))))
4981        (if found
4982            (y-or-n-p (format "Multiple %s headers.  Really post? " found))
4983          t)))
4984    ;; Check for Version and Sendsys.
4985    (message-check 'sendsys
4986      (if (re-search-forward "^Sendsys:\\|^Version:" nil t)
4987          (y-or-n-p
4988           (format "The article contains a %s command.  Really post? "
4989                   (buffer-substring (match-beginning 0)
4990                                     (1- (match-end 0)))))
4991        t))
4992    ;; See whether we can shorten Followup-To.
4993    (message-check 'shorten-followup-to
4994      (let ((newsgroups (message-fetch-field "newsgroups"))
4995            (followup-to (message-fetch-field "followup-to"))
4996            to)
4997        (when (and newsgroups
4998                   (string-match "," newsgroups)
4999                   (not followup-to)
5000                   (not
5001                    (zerop
5002                     (length
5003                      (setq to (completing-read
5004                                "Followups to (default no Followup-To header): "
5005                                (mapcar #'list
5006                                        (cons "poster"
5007                                              (message-tokenize-header
5008                                               newsgroups)))))))))
5009          (goto-char (point-min))
5010          (insert "Followup-To: " to "\n"))
5011        t))
5012    ;; Check "Shoot me".
5013    (message-check 'shoot
5014      (if (re-search-forward
5015           "Message-ID.*.i-did-not-set--mail-host-address--so-tickle-me" nil t)
5016          (y-or-n-p "You appear to have a misconfigured system.  Really post? ")
5017        t))
5018    ;; Check for Approved.
5019    (message-check 'approved
5020      (if (re-search-forward "^Approved:" nil t)
5021          (y-or-n-p "The article contains an Approved header.  Really post? ")
5022        t))
5023    ;; Check the Message-ID header.
5024    (message-check 'message-id
5025      (let* ((case-fold-search t)
5026             (message-id (message-fetch-field "message-id" t)))
5027        (or (not message-id)
5028            ;; Is there an @ in the ID?
5029            (and (string-match "@" message-id)
5030                 ;; Is there a dot in the ID?
5031                 (string-match "@[^.]*\\." message-id)
5032                 ;; Does the ID end with a dot?
5033                 (not (string-match "\\.>" message-id)))
5034            (y-or-n-p
5035             (format "The Message-ID looks strange: \"%s\".  Really post? "
5036                     message-id)))))
5037    ;; Check the Newsgroups & Followup-To headers.
5038    (message-check 'existing-newsgroups
5039      (let* ((case-fold-search t)
5040             (newsgroups (message-fetch-field "newsgroups"))
5041             (followup-to (message-fetch-field "followup-to"))
5042             (groups (message-tokenize-header
5043                      (if followup-to
5044                          (concat newsgroups "," followup-to)
5045                        newsgroups)))
5046             (post-method (if (functionp message-post-method)
5047                              (funcall message-post-method)
5048                            message-post-method))
5049             ;; KLUDGE to handle nnvirtual groups.  Doing this right
5050             ;; would probably involve a new nnoo function.
5051             ;; -- Per Abrahamsen <abraham@dina.kvl.dk>, 2001-10-17.
5052             (method (if (and (consp post-method)
5053                              (eq (car post-method) 'nnvirtual)
5054                              gnus-message-group-art)
5055                         (let ((group (car (nnvirtual-find-group-art
5056                                            (car gnus-message-group-art)
5057                                            (cdr gnus-message-group-art)))))
5058                           (gnus-find-method-for-group group))
5059                       post-method))
5060             (known-groups
5061              (mapcar (lambda (n)
5062                        (gnus-group-name-decode
5063                         (gnus-group-real-name n)
5064                         (gnus-group-name-charset method n)))
5065                      (gnus-groups-from-server method)))
5066             errors)
5067        (while groups
5068          (when (and (not (equal (car groups) "poster"))
5069                     (not (member (car groups) known-groups))
5070                     (not (member (car groups) errors)))
5071            (push (car groups) errors))
5072          (pop groups))
5073        (cond
5074         ;; Gnus is not running.
5075         ((or (not (and (boundp 'gnus-active-hashtb)
5076                        gnus-active-hashtb))
5077              (not (boundp 'gnus-read-active-file)))
5078          t)
5079         ;; We don't have all the group names.
5080         ((and (or (not gnus-read-active-file)
5081                   (eq gnus-read-active-file 'some))
5082               errors)
5083          (y-or-n-p
5084           (format
5085            "Really use %s possibly unknown group%s: %s? "
5086            (if (= (length errors) 1) "this" "these")
5087            (if (= (length errors) 1) "" "s")
5088            (mapconcat 'identity errors ", "))))
5089         ;; There were no errors.
5090         ((not errors)
5091          t)
5092         ;; There are unknown groups.
5093         (t
5094          (y-or-n-p
5095           (format
5096            "Really post to %s unknown group%s: %s? "
5097            (if (= (length errors) 1) "this" "these")
5098            (if (= (length errors) 1) "" "s")
5099            (mapconcat 'identity errors ", ")))))))
5100    ;; Check continuation headers.
5101    (message-check 'continuation-headers
5102      (goto-char (point-min))
5103      (let ((do-posting t))
5104        (while (re-search-forward "^[^ \t\n][^ \t\n:]*[ \t\n]" nil t)
5105          (goto-char (match-beginning 0))
5106          (if (y-or-n-p "Fix continuation lines? ")
5107              (insert " ")
5108            (forward-line 1)
5109            (unless (y-or-n-p "Send anyway? ")
5110              (setq do-posting nil))))
5111        do-posting))
5112    ;; Check the Newsgroups & Followup-To headers for syntax errors.
5113    (message-check 'valid-newsgroups
5114      (let ((case-fold-search t)
5115            (headers '("Newsgroups" "Followup-To"))
5116            header error)
5117        (while (and headers (not error))
5118          (when (setq header (mail-fetch-field (car headers)))
5119            (if (or
5120                 (not
5121                  (string-match
5122                   "\\`\\([-+_&.a-zA-Z0-9]+\\)?\\(,[-+_&.a-zA-Z0-9]+\\)*\\'"
5123                   header))
5124                 (memq
5125                  nil (mapcar
5126                       (lambda (g)
5127                         (not (string-match "\\.\\'\\|\\.\\." g)))
5128                       (message-tokenize-header header ","))))
5129                (setq error t)))
5130          (unless error
5131            (pop headers)))
5132        (if (not error)
5133            t
5134          (y-or-n-p
5135           (format "The %s header looks odd: \"%s\".  Really post? "
5136                   (car headers) header)))))
5137    (message-check 'repeated-newsgroups
5138      (let ((case-fold-search t)
5139            (headers '("Newsgroups" "Followup-To"))
5140            header error groups group)
5141        (while (and headers
5142                    (not error))
5143          (when (setq header (mail-fetch-field (pop headers)))
5144            (setq groups (message-tokenize-header header ","))
5145            (while (setq group (pop groups))
5146              (when (member group groups)
5147                (setq error group
5148                      groups nil)))))
5149        (if (not error)
5150            t
5151          (y-or-n-p
5152           (format "Group %s is repeated in headers.  Really post? " error)))))
5153    ;; Check the From header.
5154    (message-check 'from
5155      (let* ((case-fold-search t)
5156             (from (message-fetch-field "from"))
5157             ad)
5158        (cond
5159         ((not from)
5160          (message "There is no From line.  Posting is denied.")
5161          nil)
5162         ((or (not (string-match
5163                    "@[^\\.]*\\."
5164                    (setq ad (nth 1 (mail-extract-address-components
5165                                     from))))) ;larsi@ifi
5166              (string-match "\\.\\." ad) ;larsi@ifi..uio
5167              (string-match "@\\." ad)   ;larsi@.ifi.uio
5168              (string-match "\\.$" ad)   ;larsi@ifi.uio.
5169              (not (string-match "^[^@]+@[^@]+$" ad)) ;larsi.ifi.uio
5170              (string-match "(.*).*(.*)" from)) ;(lars) (lars)
5171          (message
5172           "Denied posting -- the From looks strange: \"%s\"." from)
5173          nil)
5174         ((let ((addresses (rfc822-addresses from)))
5175            ;; `rfc822-addresses' returns a string if parsing fails.
5176            (while (and (consp addresses)
5177                        (not (eq (string-to-char (car addresses)) ?\()))
5178              (setq addresses (cdr addresses)))
5179            addresses)
5180          (message
5181           "Denied posting -- bad From address: \"%s\"." from)
5182          nil)
5183         (t t))))
5184    ;; Check the Reply-To header.
5185    (message-check 'reply-to
5186      (let* ((case-fold-search t)
5187             (reply-to (message-fetch-field "reply-to"))
5188             ad)
5189        (cond
5190         ((not reply-to)
5191          t)
5192         ((string-match "," reply-to)
5193          (y-or-n-p
5194           (format "Multiple Reply-To addresses: \"%s\". Really post? "
5195                   reply-to)))
5196         ((or (not (string-match
5197                    "@[^\\.]*\\."
5198                    (setq ad (nth 1 (mail-extract-address-components
5199                                     reply-to))))) ;larsi@ifi
5200              (string-match "\\.\\." ad) ;larsi@ifi..uio
5201              (string-match "@\\." ad)   ;larsi@.ifi.uio
5202              (string-match "\\.$" ad)   ;larsi@ifi.uio.
5203              (not (string-match "^[^@]+@[^@]+$" ad)) ;larsi.ifi.uio
5204              (string-match "(.*).*(.*)" reply-to)) ;(lars) (lars)
5205          (y-or-n-p
5206           (format
5207            "The Reply-To looks strange: \"%s\". Really post? "
5208            reply-to)))
5209         (t t))))))
5210
5211 (defun message-check-news-body-syntax ()
5212   (and
5213    ;; Check for long lines.
5214    (message-check 'long-lines
5215      (goto-char (point-min))
5216      (re-search-forward
5217       (concat "^" (regexp-quote mail-header-separator) "$"))
5218      (forward-line 1)
5219      (while (and
5220              (or (looking-at
5221                   "<#\\(/\\)?\\(multipart\\|part\\|external\\|mml\\)")
5222                  (let ((p (point)))
5223                    (end-of-line)
5224                    (< (- (point) p) 80)))
5225              (zerop (forward-line 1))))
5226      (or (bolp)
5227          (eobp)
5228          (y-or-n-p
5229           "You have lines longer than 79 characters.  Really post? ")))
5230    ;; Check whether the article is empty.
5231    (message-check 'empty
5232      (goto-char (point-min))
5233      (re-search-forward
5234       (concat "^" (regexp-quote mail-header-separator) "$"))
5235      (forward-line 1)
5236      (let ((b (point)))
5237        (goto-char (point-max))
5238        (re-search-backward message-signature-separator nil t)
5239        (beginning-of-line)
5240        (or (re-search-backward "[^ \n\t]" b t)
5241            (if (message-gnksa-enable-p 'empty-article)
5242                (y-or-n-p "Empty article.  Really post? ")
5243              (message "Denied posting -- Empty article.")
5244              nil))))
5245    ;; Check for control characters.
5246    (message-check 'control-chars
5247      (if (re-search-forward
5248           (mm-string-to-multibyte "[\000-\007\013\015-\032\034-\037\200-\237]")
5249           nil t)
5250          (y-or-n-p
5251           "The article contains control characters.  Really post? ")
5252        t))
5253    ;; Check excessive size.
5254    (message-check 'size
5255      (if (> (buffer-size) 60000)
5256          (y-or-n-p
5257           (format "The article is %d octets long.  Really post? "
5258                   (buffer-size)))
5259        t))
5260    ;; Check whether any new text has been added.
5261    (message-check 'new-text
5262      (or
5263       (not message-checksum)
5264       (not (eq (message-checksum) message-checksum))
5265       (if (message-gnksa-enable-p 'quoted-text-only)
5266           (y-or-n-p
5267            "It looks like no new text has been added.  Really post? ")
5268         (message "Denied posting -- no new text has been added.")
5269         nil)))
5270    ;; Check the length of the signature.
5271    (message-check 'signature
5272      (let (sig-start sig-end)
5273        (goto-char (point-max))
5274        (if (not (re-search-backward message-signature-separator nil t))
5275            t
5276          (setq sig-start (1+ (point-at-eol)))
5277          (setq sig-end
5278                (if (re-search-forward
5279                     "<#/?\\(multipart\\|part\\|external\\|mml\\)" nil t)
5280                    (- (point-at-bol) 1)
5281                  (point-max)))
5282          (if (>= (count-lines sig-start sig-end) 5)
5283              (if (message-gnksa-enable-p 'signature)
5284                  (y-or-n-p
5285                   (format "Signature is excessively long (%d lines).  Really post? "
5286                           (count-lines sig-start sig-end)))
5287                (message "Denied posting -- Excessive signature.")
5288                nil)
5289            t))))
5290    ;; Ensure that text follows last quoted portion.
5291    (message-check 'quoting-style
5292      (goto-char (point-max))
5293      (let ((no-problem t))
5294        (when (search-backward-regexp "^>[^\n]*\n" nil t)
5295          (setq no-problem (search-forward-regexp "^[ \t]*[^>\n]" nil t)))
5296        (if no-problem
5297            t
5298          (if (message-gnksa-enable-p 'quoted-text-only)
5299              (y-or-n-p "Your text should follow quoted text.  Really post? ")
5300            ;; Ensure that
5301            (goto-char (point-min))
5302            (re-search-forward
5303             (concat "^" (regexp-quote mail-header-separator) "$"))
5304            (if (search-forward-regexp "^[ \t]*[^>\n]" nil t)
5305                (y-or-n-p "Your text should follow quoted text.  Really post? ")
5306              (message "Denied posting -- only quoted text.")
5307              nil)))))))
5308
5309 (defun message-checksum ()
5310   "Return a \"checksum\" for the current buffer."
5311   (let ((sum 0))
5312     (save-excursion
5313       (goto-char (point-min))
5314       (re-search-forward
5315        (concat "^" (regexp-quote mail-header-separator) "$"))
5316       (while (not (eobp))
5317         (when (not (looking-at "[ \t\n]"))
5318           (setq sum (logxor (ash sum 1) (if (natnump sum) 0 1)
5319                             (char-after))))
5320         (forward-char 1)))
5321     sum))
5322
5323 (defun message-do-fcc ()
5324   "Process Fcc headers in the current buffer."
5325   (let ((case-fold-search t)
5326         (buf (current-buffer))
5327         list file
5328         (mml-externalize-attachments message-fcc-externalize-attachments))
5329     (save-excursion
5330       (save-restriction
5331         (message-narrow-to-headers)
5332         (setq file (message-fetch-field "fcc" t)))
5333       (when file
5334         (set-buffer (get-buffer-create " *message temp*"))
5335         (erase-buffer)
5336         (insert-buffer-substring buf)
5337         (message-encode-message-body)
5338         (save-restriction
5339           (message-narrow-to-headers)
5340           (while (setq file (message-fetch-field "fcc" t))
5341             (push file list)
5342             (message-remove-header "fcc" nil t))
5343           (let ((mail-parse-charset message-default-charset)
5344                 (rfc2047-header-encoding-alist
5345                  (cons '("Newsgroups" . default)
5346                        rfc2047-header-encoding-alist)))
5347             (mail-encode-encoded-word-buffer)))
5348         (goto-char (point-min))
5349         (when (re-search-forward
5350                (concat "^" (regexp-quote mail-header-separator) "$")
5351                nil t)
5352           (replace-match "" t t ))
5353         ;; Process FCC operations.
5354         (while list
5355           (setq file (pop list))
5356           (if (string-match "^[ \t]*|[ \t]*\\(.*\\)[ \t]*$" file)
5357               ;; Pipe the article to the program in question.
5358               (call-process-region (point-min) (point-max) shell-file-name
5359                                    nil nil nil shell-command-switch
5360                                    (match-string 1 file))
5361             ;; Save the article.
5362             (setq file (expand-file-name file))
5363             (unless (file-exists-p (file-name-directory file))
5364               (make-directory (file-name-directory file) t))
5365             (if (and message-fcc-handler-function
5366                      (not (eq message-fcc-handler-function 'rmail-output)))
5367                 (funcall message-fcc-handler-function file)
5368               ;; FIXME this option, rmail-output (also used if
5369               ;; message-fcc-handler-function is nil) is not
5370               ;; documented anywhere AFAICS.  It should work in Emacs
5371               ;; 23; I suspect it does not work in Emacs 22.
5372               ;; FIXME I don't see the need for the two different cases here.
5373               ;; mail-use-rfc822 makes no difference (in Emacs 23),and
5374               ;; the third argument just controls \"Wrote file\" message.
5375               (if (and (file-readable-p file) (mail-file-babyl-p file))
5376                   (rmail-output file 1 nil t)
5377                 (let ((mail-use-rfc822 t))
5378                   (rmail-output file 1 t t))))))
5379         (kill-buffer (current-buffer))))))
5380
5381 (defun message-output (filename)
5382   "Append this article to Unix/babyl mail file FILENAME."
5383   (if (or (and (file-readable-p filename)
5384                (mail-file-babyl-p filename))
5385           ;; gnus-output-to-mail does the wrong thing with live, mbox
5386           ;; Rmail buffers in Emacs 23.
5387           ;; http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=597255
5388           (let ((buff (find-buffer-visiting filename)))
5389             (and buff (with-current-buffer buff
5390                         (eq major-mode 'rmail-mode)))))
5391       (gnus-output-to-rmail filename t)
5392     (gnus-output-to-mail filename t)))
5393
5394 (defun message-cleanup-headers ()
5395   "Do various automatic cleanups of the headers."
5396   ;; Remove empty lines in the header.
5397   (save-restriction
5398     (message-narrow-to-headers)
5399     ;; Remove blank lines.
5400     (while (re-search-forward "^[ \t]*\n" nil t)
5401       (replace-match "" t t))
5402
5403     ;; Correct Newsgroups and Followup-To headers:  Change sequence of
5404     ;; spaces to comma and eliminate spaces around commas.  Eliminate
5405     ;; embedded line breaks.
5406     (goto-char (point-min))
5407     (while (re-search-forward "^\\(Newsgroups\\|Followup-To\\): +" nil t)
5408       (save-restriction
5409         (narrow-to-region
5410          (point)
5411          (if (re-search-forward "^[^ \t]" nil t)
5412              (match-beginning 0)
5413            (forward-line 1)
5414            (point)))
5415         (goto-char (point-min))
5416         (while (re-search-forward "\n[ \t]+" nil t)
5417           (replace-match " " t t))     ;No line breaks (too confusing)
5418         (goto-char (point-min))
5419         (while (re-search-forward "[ \t\n]*,[ \t\n]*\\|[ \t]+" nil t)
5420           (replace-match "," t t))
5421         (goto-char (point-min))
5422         ;; Remove trailing commas.
5423         (when (re-search-forward ",+$" nil t)
5424           (replace-match "" t t))))))
5425
5426 (defun message-make-date (&optional now)
5427   "Make a valid data header.
5428 If NOW, use that time instead."
5429   (let ((system-time-locale "C"))
5430     (format-time-string "%a, %d %b %Y %T %z" now)))
5431
5432 (defun message-insert-expires (days)
5433   "Insert the Expires header.  Expiry in DAYS days."
5434   (interactive "NExpire article in how many days? ")
5435   (save-excursion
5436     (message-position-on-field "Expires" "X-Draft-From")
5437     (insert (message-make-expires-date days))))
5438
5439 (defun message-make-expires-date (days)
5440   "Make date string for the Expires header.  Expiry in DAYS days.
5441
5442 In posting styles use `(\"Expires\" (make-expires-date 30))'."
5443   (let* ((cur (decode-time (current-time)))
5444          (nday (+ days (nth 3 cur))))
5445     (setf (nth 3 cur) nday)
5446     (message-make-date (apply 'encode-time cur))))
5447
5448 (defun message-make-message-id ()
5449   "Make a unique Message-ID."
5450   (concat "<" (message-unique-id)
5451           (let ((psubject (save-excursion (message-fetch-field "subject")))
5452                 (psupersedes
5453                  (save-excursion (message-fetch-field "supersedes"))))
5454             (if (or
5455                  (and message-reply-headers
5456                       (mail-header-references message-reply-headers)
5457                       (mail-header-subject message-reply-headers)
5458                       psubject
5459                       (not (string=
5460                             (message-strip-subject-re
5461                              (mail-header-subject message-reply-headers))
5462                             (message-strip-subject-re psubject))))
5463                  (and psupersedes
5464                       (string-match "_-_@" psupersedes)))
5465                 "_-_" ""))
5466           "@" (message-make-fqdn) ">"))
5467
5468 (defvar message-unique-id-char nil)
5469
5470 ;; If you ever change this function, make sure the new version
5471 ;; cannot generate IDs that the old version could.
5472 ;; You might for example insert a "." somewhere (not next to another dot
5473 ;; or string boundary), or modify the "fsf" string.
5474 (defun message-unique-id ()
5475   ;; Don't use microseconds from (current-time), they may be unsupported.
5476   ;; Instead we use this randomly inited counter.
5477   (setq message-unique-id-char
5478         (% (1+ (or message-unique-id-char (logand (random t) (1- (lsh 1 20)))))
5479            ;; (current-time) returns 16-bit ints,
5480            ;; and 2^16*25 just fits into 4 digits i base 36.
5481            (* 25 25)))
5482   (let ((tm (current-time)))
5483     (concat
5484      (if (or (eq system-type 'ms-dos)
5485              ;; message-number-base36 doesn't handle bigints.
5486              (floatp (user-uid)))
5487          (let ((user (downcase (user-login-name))))
5488            (while (string-match "[^a-z0-9_]" user)
5489              (aset user (match-beginning 0) ?_))
5490            user)
5491        (message-number-base36 (user-uid) -1))
5492      (message-number-base36 (+ (car tm)
5493                                (lsh (% message-unique-id-char 25) 16)) 4)
5494      (message-number-base36 (+ (nth 1 tm)
5495                                (lsh (/ message-unique-id-char 25) 16)) 4)
5496      ;; Append a given name, because while the generated ID is unique
5497      ;; to this newsreader, other newsreaders might otherwise generate
5498      ;; the same ID via another algorithm.
5499      ".fsf")))
5500
5501 (defun message-number-base36 (num len)
5502   (if (if (< len 0)
5503           (<= num 0)
5504         (= len 0))
5505       ""
5506     (concat (message-number-base36 (/ num 36) (1- len))
5507             (char-to-string (aref "zyxwvutsrqponmlkjihgfedcba9876543210"
5508                                   (% num 36))))))
5509
5510 (defun message-make-organization ()
5511   "Make an Organization header."
5512   (let* ((organization
5513           (when message-user-organization
5514             (if (functionp message-user-organization)
5515                 (funcall message-user-organization)
5516               message-user-organization))))
5517     (with-temp-buffer
5518       (mm-enable-multibyte)
5519       (cond ((stringp organization)
5520              (insert organization))
5521             ((and (eq t organization)
5522                   message-user-organization-file
5523                   (file-exists-p message-user-organization-file))
5524              (insert-file-contents message-user-organization-file)))
5525       (goto-char (point-min))
5526       (while (re-search-forward "[\t\n]+" nil t)
5527         (replace-match "" t t))
5528       (unless (zerop (buffer-size))
5529         (buffer-string)))))
5530
5531 (defun message-make-lines ()
5532   "Count the number of lines and return numeric string."
5533   (save-excursion
5534     (save-restriction
5535       (widen)
5536       (message-goto-body)
5537       (int-to-string (count-lines (point) (point-max))))))
5538
5539 (defun message-make-references ()
5540   "Return the References header for this message."
5541   (when message-reply-headers
5542     (let ((message-id (mail-header-id message-reply-headers))
5543           (references (mail-header-references message-reply-headers)))
5544       (if (or references message-id)
5545           (concat (or references "") (and references " ")
5546                   (or message-id ""))
5547         nil))))
5548
5549 (defun message-make-in-reply-to ()
5550   "Return the In-Reply-To header for this message."
5551   (when message-reply-headers
5552     (let ((from (mail-header-from message-reply-headers))
5553           (date (mail-header-date message-reply-headers))
5554           (msg-id (mail-header-id message-reply-headers)))
5555       (when from
5556         (let ((name (mail-extract-address-components from)))
5557           (concat
5558            msg-id (if msg-id " (")
5559            (if (car name)
5560                (if (string-match "[^\000-\177]" (car name))
5561                    ;; Quote a string containing non-ASCII characters.
5562                    ;; It will make the RFC2047 encoder cause an error
5563                    ;; if there are special characters.
5564                    (mm-with-multibyte-buffer
5565                      (insert (car name))
5566                      (goto-char (point-min))
5567                      (while (search-forward "\"" nil t)
5568                        (when (prog2
5569                                  (backward-char)
5570                                  (zerop (% (skip-chars-backward "\\\\") 2))
5571                                (goto-char (match-beginning 0)))
5572                          (insert "\\"))
5573                        (forward-char))
5574                      ;; Those quotes will be removed by the RFC2047 encoder.
5575                      (concat "\"" (buffer-string) "\""))
5576                  (car name))
5577              (nth 1 name))
5578            "'s message of \""
5579            (if (or (not date) (string= date ""))
5580                "(unknown date)" date)
5581            "\"" (if msg-id ")")))))))
5582
5583 (defun message-make-distribution ()
5584   "Make a Distribution header."
5585   (let ((orig-distribution (message-fetch-reply-field "distribution")))
5586     (cond ((functionp message-distribution-function)
5587            (funcall message-distribution-function))
5588           (t orig-distribution))))
5589
5590 (defun message-make-expires ()
5591   "Return an Expires header based on `message-expires'."
5592   (let ((current (current-time))
5593         (future (* 1.0 message-expires 60 60 24)))
5594     ;; Add the future to current.
5595     (setcar current (+ (car current) (round (/ future (expt 2 16)))))
5596     (setcar (cdr current) (+ (nth 1 current) (% (round future) (expt 2 16))))
5597     (message-make-date current)))
5598
5599 (defun message-make-path ()
5600   "Return uucp path."
5601   (let ((login-name (user-login-name)))
5602     (cond ((null message-user-path)
5603            (concat (system-name) "!" login-name))
5604           ((stringp message-user-path)
5605            ;; Support GENERICPATH.  Suggested by vixie@decwrl.dec.com.
5606            (concat message-user-path "!" login-name))
5607           (t login-name))))
5608
5609 (defun message-make-from (&optional name address)
5610   "Make a From header."
5611   (let* ((style message-from-style)
5612          (login (or address (message-make-address)))
5613          (fullname (or name
5614                        (and (boundp 'user-full-name)
5615                             user-full-name)
5616                        (user-full-name))))
5617     (when (string= fullname "&")
5618       (setq fullname (user-login-name)))
5619     (with-temp-buffer
5620       (mm-enable-multibyte)
5621       (cond
5622        ((or (null style)
5623             (equal fullname ""))
5624         (insert login))
5625        ((or (eq style 'angles)
5626             (and (not (eq style 'parens))
5627                  ;; Use angles if no quoting is needed, or if parens would
5628                  ;; need quoting too.
5629                  (or (not (string-match "[^- !#-'*+/-9=?A-Z^-~]" fullname))
5630                      (let ((tmp (concat fullname nil)))
5631                        (while (string-match "([^()]*)" tmp)
5632                          (aset tmp (match-beginning 0) ?-)
5633                          (aset tmp (1- (match-end 0)) ?-))
5634                        (string-match "[\\()]" tmp)))))
5635         (insert fullname)
5636         (goto-char (point-min))
5637         ;; Look for a character that cannot appear unquoted
5638         ;; according to RFC 822.
5639         (when (re-search-forward "[^- !#-'*+/-9=?A-Z^-~]" nil 1)
5640           ;; Quote fullname, escaping specials.
5641           (goto-char (point-min))
5642           (insert "\"")
5643           (while (re-search-forward "[\"\\]" nil 1)
5644             (replace-match "\\\\\\&" t))
5645           (insert "\""))
5646         (insert " <" login ">"))
5647        (t                               ; 'parens or default
5648         (insert login " (")
5649         (let ((fullname-start (point)))
5650           (insert fullname)
5651           (goto-char fullname-start)
5652           ;; RFC 822 says \ and nonmatching parentheses
5653           ;; must be escaped in comments.
5654           ;; Escape every instance of ()\ ...
5655           (while (re-search-forward "[()\\]" nil 1)
5656             (replace-match "\\\\\\&" t))
5657           ;; ... then undo escaping of matching parentheses,
5658           ;; including matching nested parentheses.
5659           (goto-char fullname-start)
5660           (while (re-search-forward
5661                   "\\(\\=\\|[^\\]\\(\\\\\\\\\\)*\\)\\\\(\\(\\([^\\]\\|\\\\\\\\\\)*\\)\\\\)"
5662                   nil 1)
5663             (replace-match "\\1(\\3)" t)
5664             (goto-char fullname-start)))
5665         (insert ")")))
5666       (buffer-string))))
5667
5668 (defun message-make-sender ()
5669   "Return the \"real\" user address.
5670 This function tries to ignore all user modifications, and
5671 give as trustworthy answer as possible."
5672   (concat (user-login-name) "@" (system-name)))
5673
5674 (defun message-make-address ()
5675   "Make the address of the user."
5676   (or (message-user-mail-address)
5677       (concat (user-login-name) "@" (message-make-domain))))
5678
5679 (defun message-user-mail-address ()
5680   "Return the pertinent part of `user-mail-address'."
5681   (when (and user-mail-address
5682              (string-match "@.*\\." user-mail-address))
5683     (if (string-match " " user-mail-address)
5684         (nth 1 (mail-extract-address-components user-mail-address))
5685       user-mail-address)))
5686
5687 (defun message-sendmail-envelope-from ()
5688   "Return the envelope from."
5689   (cond ((eq message-sendmail-envelope-from 'header)
5690          (nth 1 (mail-extract-address-components
5691                  (message-fetch-field "from"))))
5692         ((stringp message-sendmail-envelope-from)
5693          message-sendmail-envelope-from)
5694         (t
5695          (message-make-address))))
5696
5697 (defun message-make-fqdn ()
5698   "Return user's fully qualified domain name."
5699   (let* ((system-name (system-name))
5700          (user-mail (message-user-mail-address))
5701          (user-domain
5702           (if (and user-mail
5703                    (string-match "@\\(.*\\)\\'" user-mail))
5704               (match-string 1 user-mail)))
5705          (case-fold-search t))
5706     (cond
5707      ((and message-user-fqdn
5708            (stringp message-user-fqdn)
5709            (string-match message-valid-fqdn-regexp message-user-fqdn)
5710            (not (string-match message-bogus-system-names message-user-fqdn)))
5711       ;; `message-user-fqdn' seems to be valid
5712       message-user-fqdn)
5713      ((and (string-match message-valid-fqdn-regexp system-name)
5714            (not (string-match message-bogus-system-names system-name)))
5715       ;; `system-name' returned the right result.
5716       system-name)
5717      ;; Try `mail-host-address'.
5718      ((and (boundp 'mail-host-address)
5719            (stringp mail-host-address)
5720            (string-match message-valid-fqdn-regexp mail-host-address)
5721            (not (string-match message-bogus-system-names mail-host-address)))
5722       mail-host-address)
5723      ;; We try `user-mail-address' as a backup.
5724      ((and user-domain
5725            (stringp user-domain)
5726            (string-match message-valid-fqdn-regexp user-domain)
5727            (not (string-match message-bogus-system-names user-domain)))
5728       user-domain)
5729      ;; Default to this bogus thing.
5730      (t
5731       (concat system-name
5732               ".i-did-not-set--mail-host-address--so-tickle-me")))))
5733
5734 (defun message-make-host-name ()
5735   "Return the name of the host."
5736   (let ((fqdn (message-make-fqdn)))
5737     (string-match "^[^.]+\\." fqdn)
5738     (substring fqdn 0 (1- (match-end 0)))))
5739
5740 (defun message-make-domain ()
5741   "Return the domain name."
5742   (or mail-host-address
5743       (message-make-fqdn)))
5744
5745 (defun message-to-list-only ()
5746   "Send a message to the list only.
5747 Remove all addresses but the list address from To and Cc headers."
5748   (interactive)
5749   (let ((listaddr (message-make-mail-followup-to t)))
5750     (when listaddr
5751       (save-excursion
5752         (message-remove-header "to")
5753         (message-remove-header "cc")
5754         (message-position-on-field "To" "X-Draft-From")
5755         (insert listaddr)))))
5756
5757 (defun message-make-mail-followup-to (&optional only-show-subscribed)
5758   "Return the Mail-Followup-To header.
5759 If passed the optional argument ONLY-SHOW-SUBSCRIBED only return the
5760 subscribed address (and not the additional To and Cc header contents)."
5761   (let* ((case-fold-search t)
5762          (to (message-fetch-field "To"))
5763          (cc (message-fetch-field "cc"))
5764          (msg-recipients (concat to (and to cc ", ") cc))
5765          (recipients
5766           (mapcar 'mail-strip-quoted-names
5767                   (message-tokenize-header msg-recipients)))
5768          (file-regexps
5769           (if message-subscribed-address-file
5770               (let (begin end item re)
5771                 (save-excursion
5772                   (with-temp-buffer
5773                     (insert-file-contents message-subscribed-address-file)
5774                     (while (not (eobp))
5775                       (setq begin (point))
5776                       (forward-line 1)
5777                       (setq end (point))
5778                       (if (bolp) (setq end (1- end)))
5779                       (setq item (regexp-quote (buffer-substring begin end)))
5780                       (if re (setq re (concat re "\\|" item))
5781                         (setq re (concat "\\`\\(" item))))
5782                     (and re (list (concat re "\\)\\'"))))))))
5783          (mft-regexps (apply 'append message-subscribed-regexps
5784                              (mapcar 'regexp-quote
5785                                      message-subscribed-addresses)
5786                              file-regexps
5787                              (mapcar 'funcall
5788                                      message-subscribed-address-functions))))
5789     (save-match-data
5790       (let ((list
5791              (loop for recipient in recipients
5792                when (loop for regexp in mft-regexps
5793                       when (string-match regexp recipient) return t)
5794                return recipient)))
5795         (when list
5796           (if only-show-subscribed
5797               list
5798             msg-recipients))))))
5799
5800 (defun message-idna-to-ascii-rhs-1 (header)
5801   "Interactively potentially IDNA encode domain names in HEADER."
5802   (let ((field (message-fetch-field header))
5803         ace)
5804     (when field
5805       (dolist (rhs
5806                (mm-delete-duplicates
5807                 (mapcar (lambda (rhs) (or (cadr (split-string rhs "@")) ""))
5808                         (mapcar 'downcase
5809                                 (mapcar
5810                                  (lambda (elem)
5811                                    (or (cadr elem)
5812                                        ""))
5813                                  (mail-extract-address-components field t))))))
5814         ;; Note that `rhs' will be "" if the address does not have
5815         ;; the domain part, i.e., if it is a local user's address.
5816         (setq ace (if (string-match "\\`[[:ascii:]]*\\'" rhs)
5817                       rhs
5818                     (downcase (idna-to-ascii rhs))))
5819         (when (and (not (equal rhs ace))
5820                    (or (not (eq message-use-idna 'ask))
5821                        (y-or-n-p (format "Replace %s with %s in %s:? "
5822                                          rhs ace header))))
5823           (goto-char (point-min))
5824           (while (re-search-forward (concat "^" header ":") nil t)
5825             (message-narrow-to-field)
5826             (while (search-forward (concat "@" rhs) nil t)
5827               (replace-match (concat "@" ace) t t))
5828             (goto-char (point-max))
5829             (widen)))))))
5830
5831 (defun message-idna-to-ascii-rhs ()
5832   "Possibly IDNA encode non-ASCII domain names in From:, To: and Cc: headers.
5833 See `message-idna-encode'."
5834   (interactive)
5835   (when message-use-idna
5836     (save-excursion
5837       (save-restriction
5838         ;; `message-narrow-to-head' that recognizes only the first empty
5839         ;; line as the message header separator used to be used here.
5840         ;; However, since there is the "--text follows this line--" line
5841         ;; normally, it failed in narrowing to the headers and potentially
5842         ;; caused the IDNA encoding on lines that look like headers in
5843         ;; the message body.
5844         (message-narrow-to-headers-or-head)
5845         (message-idna-to-ascii-rhs-1 "From")
5846         (message-idna-to-ascii-rhs-1 "To")
5847         (message-idna-to-ascii-rhs-1 "Reply-To")
5848         (message-idna-to-ascii-rhs-1 "Mail-Reply-To")
5849         (message-idna-to-ascii-rhs-1 "Mail-Followup-To")
5850         (message-idna-to-ascii-rhs-1 "Cc")))))
5851
5852 (defvar Date)
5853 (defvar Message-ID)
5854 (defvar Organization)
5855 (defvar From)
5856 (defvar Path)
5857 (defvar Subject)
5858 (defvar Newsgroups)
5859 (defvar In-Reply-To)
5860 (defvar References)
5861 (defvar To)
5862 (defvar Distribution)
5863 (defvar Lines)
5864 (defvar User-Agent)
5865 (defvar Expires)
5866
5867 (defun message-generate-headers (headers)
5868   "Prepare article HEADERS.
5869 Headers already prepared in the buffer are not modified."
5870   (setq headers (append headers message-required-headers))
5871   (save-restriction
5872     (message-narrow-to-headers)
5873     (let* ((Date (message-make-date))
5874            (Message-ID (message-make-message-id))
5875            (Organization (message-make-organization))
5876            (From (message-make-from))
5877            (Path (message-make-path))
5878            (Subject nil)
5879            (Newsgroups nil)
5880            (In-Reply-To (message-make-in-reply-to))
5881            (References (message-make-references))
5882            (To nil)
5883            (Distribution (message-make-distribution))
5884            (Lines (message-make-lines))
5885            (User-Agent message-newsreader)
5886            (Expires (message-make-expires))
5887            (case-fold-search t)
5888            (optionalp nil)
5889            header value elem header-string)
5890       ;; First we remove any old generated headers.
5891       (let ((headers message-deletable-headers))
5892         (unless (buffer-modified-p)
5893           (setq headers (delq 'Message-ID (copy-sequence headers))))
5894         (while headers
5895           (goto-char (point-min))
5896           (and (re-search-forward
5897                 (concat "^" (symbol-name (car headers)) ": *") nil t)
5898                (get-text-property (1+ (match-beginning 0)) 'message-deletable)
5899                (message-delete-line))
5900           (pop headers)))
5901       ;; Go through all the required headers and see if they are in the
5902       ;; articles already.  If they are not, or are empty, they are
5903       ;; inserted automatically - except for Subject, Newsgroups and
5904       ;; Distribution.
5905       (while headers
5906         (goto-char (point-min))
5907         (setq elem (pop headers))
5908         (if (consp elem)
5909             (if (eq (car elem) 'optional)
5910                 (setq header (cdr elem)
5911                       optionalp t)
5912               (setq header (car elem)))
5913           (setq header elem))
5914         (setq header-string  (if (stringp header)
5915                                  header
5916                                (symbol-name header)))
5917         (when (or (not (re-search-forward
5918                         (concat "^"
5919                                 (regexp-quote (downcase header-string))
5920                                 ":")
5921                         nil t))
5922                   (progn
5923                     ;; The header was found.  We insert a space after the
5924                     ;; colon, if there is none.
5925                     (if (/= (char-after) ? ) (insert " ") (forward-char 1))
5926                     ;; Find out whether the header is empty.
5927                     (looking-at "[ \t]*\n[^ \t]")))
5928           ;; So we find out what value we should insert.
5929           (setq value
5930                 (cond
5931                  ((and (consp elem)
5932                        (eq (car elem) 'optional)
5933                        (not (member header-string message-inserted-headers)))
5934                   ;; This is an optional header.  If the cdr of this
5935                   ;; is something that is nil, then we do not insert
5936                   ;; this header.
5937                   (setq header (cdr elem))
5938                   (or (and (functionp (cdr elem))
5939                            (funcall (cdr elem)))
5940                       (and (boundp (cdr elem))
5941                            (symbol-value (cdr elem)))))
5942                  ((consp elem)
5943                   ;; The element is a cons.  Either the cdr is a
5944                   ;; string to be inserted verbatim, or it is a
5945                   ;; function, and we insert the value returned from
5946                   ;; this function.
5947                   (or (and (stringp (cdr elem))
5948                            (cdr elem))
5949                       (and (functionp (cdr elem))
5950                            (funcall (cdr elem)))))
5951                  ((and (boundp header)
5952                        (symbol-value header))
5953                   ;; The element is a symbol.  We insert the value
5954                   ;; of this symbol, if any.
5955                   (symbol-value header))
5956                  ((not (message-check-element
5957                         (intern (downcase (symbol-name header)))))
5958                   ;; We couldn't generate a value for this header,
5959                   ;; so we just ask the user.
5960                   (read-from-minibuffer
5961                    (format "Empty header for %s; enter value: " header)))))
5962           ;; Finally insert the header.
5963           (when (and value
5964                      (not (equal value "")))
5965             (save-excursion
5966               (if (bolp)
5967                   (progn
5968                     ;; This header didn't exist, so we insert it.
5969                     (goto-char (point-max))
5970                     (let ((formatter
5971                            (cdr (assq header message-header-format-alist))))
5972                       (if formatter
5973                           (funcall formatter header value)
5974                         (insert header-string ": " value))
5975                       (push header-string message-inserted-headers)
5976                       (goto-char (message-fill-field))
5977                       ;; We check whether the value was ended by a
5978                       ;; newline.  If not, we insert one.
5979                       (unless (bolp)
5980                         (insert "\n"))
5981                       (forward-line -1)))
5982                 ;; The value of this header was empty, so we clear
5983                 ;; totally and insert the new value.
5984                 (delete-region (point) (point-at-eol))
5985                 ;; If the header is optional, and the header was
5986                 ;; empty, we can't insert it anyway.
5987                 (unless optionalp
5988                   (push header-string message-inserted-headers)
5989                   (insert value)
5990                   (message-fill-field)))
5991               ;; Add the deletable property to the headers that require it.
5992               (and (memq header message-deletable-headers)
5993                    (progn (beginning-of-line) (looking-at "[^:]+: "))
5994                    (add-text-properties
5995                     (point) (match-end 0)
5996                     '(message-deletable t face italic) (current-buffer)))))))
5997       ;; Insert new Sender if the From is strange.
5998       (let ((from (message-fetch-field "from"))
5999             (sender (message-fetch-field "sender"))
6000             (secure-sender (message-make-sender)))
6001         (when (and from
6002                    (not (message-check-element 'sender))
6003                    (not (string=
6004                          (downcase
6005                           (cadr (mail-extract-address-components from)))
6006                          (downcase secure-sender)))
6007                    (or (null sender)
6008                        (not
6009                         (string=
6010                          (downcase
6011                           (cadr (mail-extract-address-components sender)))
6012                          (downcase secure-sender)))))
6013           (goto-char (point-min))
6014           ;; Rename any old Sender headers to Original-Sender.
6015           (when (re-search-forward "^\\(Original-\\)*Sender:" nil t)
6016             (beginning-of-line)
6017             (insert "Original-")
6018             (beginning-of-line))
6019           (when (or (message-news-p)
6020                     (string-match "@.+\\.." secure-sender))
6021             (insert "Sender: " secure-sender "\n"))))
6022       ;; Check for IDNA
6023       (message-idna-to-ascii-rhs))))
6024
6025 (defun message-insert-courtesy-copy (message)
6026   "Insert a courtesy message in mail copies of combined messages."
6027   (let (newsgroups)
6028     (save-excursion
6029       (save-restriction
6030         (message-narrow-to-headers)
6031         (when (setq newsgroups (message-fetch-field "newsgroups"))
6032           (goto-char (point-max))
6033           (insert "Posted-To: " newsgroups "\n")))
6034       (forward-line 1)
6035       (when message
6036         (cond
6037          ((string-match "%s" message)
6038           (insert (format message newsgroups)))
6039          (t
6040           (insert message)))))))
6041
6042 ;;;
6043 ;;; Setting up a message buffer
6044 ;;;
6045
6046 (defun message-skip-to-next-address ()
6047   (let ((end (save-excursion
6048                (message-next-header)
6049                (point)))
6050         quoted char)
6051     (when (looking-at ",")
6052       (forward-char 1))
6053     (while (and (not (= (point) end))
6054                 (or (not (eq char ?,))
6055                     quoted))
6056       (skip-chars-forward "^,\"" (point-max))
6057       (when (eq (setq char (following-char)) ?\")
6058         (setq quoted (not quoted)))
6059       (unless (= (point) end)
6060         (forward-char 1)))
6061     (skip-chars-forward " \t\n")))
6062
6063 (defun message-fill-address (header value)
6064   (insert (capitalize (symbol-name header))
6065           ": "
6066           (if (consp value) (car value) value)
6067           "\n")
6068   (message-fill-field-address))
6069
6070 (defun message-split-line ()
6071   "Split current line, moving portion beyond point vertically down.
6072 If the current line has `message-yank-prefix', insert it on the new line."
6073   (interactive "*")
6074   (condition-case nil
6075       (split-line message-yank-prefix) ;; Emacs 22.1+ supports arg.
6076     (error
6077      (split-line))))
6078
6079 (defun message-insert-header (header value)
6080   (insert (capitalize (symbol-name header))
6081           ": "
6082           (if (consp value) (car value) value)))
6083
6084 (defun message-field-name ()
6085   (save-excursion
6086     (goto-char (point-min))
6087     (when (looking-at "\\([^:]+\\):")
6088       (intern (capitalize (match-string 1))))))
6089
6090 (defun message-fill-field ()
6091   (save-excursion
6092     (save-restriction
6093       (message-narrow-to-field)
6094       (let ((field-name (message-field-name)))
6095         (funcall (or (cadr (assq field-name message-field-fillers))
6096                      'message-fill-field-general)))
6097       (point-max))))
6098
6099 (defun message-fill-field-address ()
6100   (while (not (eobp))
6101     (message-skip-to-next-address)
6102     (let (last)
6103       (if (and (> (current-column) 78)
6104                last)
6105           (progn
6106             (save-excursion
6107               (goto-char last)
6108               (insert "\n\t"))
6109             (setq last (1+ (point))))
6110         (setq last (1+ (point)))))))
6111
6112 (defun message-fill-field-general ()
6113   (let ((begin (point))
6114         (fill-column 78)
6115         (fill-prefix "\t"))
6116     (while (and (search-forward "\n" nil t)
6117                 (not (eobp)))
6118       (replace-match " " t t))
6119     (fill-region-as-paragraph begin (point-max))
6120     ;; Tapdance around looong Message-IDs.
6121     (forward-line -1)
6122     (when (looking-at "[ \t]*$")
6123       (message-delete-line))
6124     (goto-char begin)
6125     (search-forward ":" nil t)
6126     (when (looking-at "\n[ \t]+")
6127       (replace-match " " t t))
6128     (goto-char (point-max))))
6129
6130 (defun message-shorten-1 (list cut surplus)
6131   "Cut SURPLUS elements out of LIST, beginning with CUTth one."
6132   (setcdr (nthcdr (- cut 2) list)
6133           (nthcdr (+ (- cut 2) surplus 1) list)))
6134
6135 (defun message-shorten-references (header references)
6136   "Trim REFERENCES to be 21 Message-ID long or less, and fold them.
6137 When sending via news, also check that the REFERENCES are less
6138 than 988 characters long, and if they are not, trim them until
6139 they are."
6140   ;; 21 is the number suggested by USEAGE.
6141   (let ((maxcount 21)
6142         (count 0)
6143         (cut 2)
6144         refs)
6145     (with-temp-buffer
6146       (insert references)
6147       (goto-char (point-min))
6148       ;; Cons a list of valid references.  GNKSA says we must not include MIDs
6149       ;; with whitespace or missing brackets (7.a "Does not propagate broken
6150       ;; Message-IDs in original References").
6151       (while (re-search-forward "<[^ <]+@[^ <]+>" nil t)
6152         (push (match-string 0) refs))
6153       (setq refs (nreverse refs)
6154             count (length refs)))
6155
6156     ;; If the list has more than MAXCOUNT elements, trim it by
6157     ;; removing the CUTth element and the required number of
6158     ;; elements that follow.
6159     (when (> count maxcount)
6160       (let ((surplus (- count maxcount)))
6161         (message-shorten-1 refs cut surplus)
6162         (decf count surplus)))
6163
6164     ;; When sending via news, make sure the total folded length will
6165     ;; be less than 998 characters.  This is to cater to broken INN
6166     ;; 2.3 which counts the total number of characters in a header
6167     ;; rather than the physical line length of each line, as it should.
6168     ;;
6169     ;; This hack should be removed when it's believed than INN 2.3 is
6170     ;; no longer widely used.
6171     ;;
6172     ;; At this point the headers have not been generated, thus we use
6173     ;; message-this-is-news directly.
6174     (when message-this-is-news
6175       (while (< 998
6176                 (with-temp-buffer
6177                   (message-insert-header
6178                    header (mapconcat #'identity refs " "))
6179                   (buffer-size)))
6180         (message-shorten-1 refs cut 1)))
6181     ;; Finally, collect the references back into a string and insert
6182     ;; it into the buffer.
6183     (message-insert-header header (mapconcat #'identity refs " "))))
6184
6185 (defun message-position-point ()
6186   "Move point to where the user probably wants to find it."
6187   (message-narrow-to-headers)
6188   (cond
6189    ((re-search-forward "^[^:]+:[ \t]*$" nil t)
6190     (search-backward ":" )
6191     (widen)
6192     (forward-char 1)
6193     (if (eq (char-after) ? )
6194         (forward-char 1)
6195       (insert " ")))
6196    (t
6197     (goto-char (point-max))
6198     (widen)
6199     (forward-line 1)
6200     (unless (looking-at "$")
6201       (forward-line 2)))
6202    (sit-for 0)))
6203
6204 (defcustom message-beginning-of-line t
6205   "Whether \\<message-mode-map>\\[message-beginning-of-line]\
6206  goes to beginning of header values."
6207   :version "22.1"
6208   :group 'message-buffers
6209   :link '(custom-manual "(message)Movement")
6210   :type 'boolean)
6211
6212 (defun message-beginning-of-line (&optional n)
6213   "Move point to beginning of header value or to beginning of line.
6214 The prefix argument N is passed directly to `beginning-of-line'.
6215
6216 This command is identical to `beginning-of-line' if point is
6217 outside the message header or if the option `message-beginning-of-line'
6218 is nil.
6219
6220 If point is in the message header and on a (non-continued) header
6221 line, move point to the beginning of the header value or the beginning of line,
6222 whichever is closer.  If point is already at beginning of line, move point to
6223 beginning of header value.  Therefore, repeated calls will toggle point
6224 between beginning of field and beginning of line."
6225   (interactive "p")
6226   (let ((zrs 'zmacs-region-stays))
6227     (when (and (featurep 'xemacs) (interactive-p) (boundp zrs))
6228       (set zrs t)))
6229   (if (and message-beginning-of-line
6230            (message-point-in-header-p))
6231       (let* ((here (point))
6232              (bol (progn (beginning-of-line n) (point)))
6233              (eol (point-at-eol))
6234              (eoh (re-search-forward ": *" eol t)))
6235         (goto-char
6236          (if (and eoh (or (< eoh here) (= bol here)))
6237              eoh bol)))
6238     (beginning-of-line n)))
6239
6240 (defun message-buffer-name (type &optional to group)
6241   "Return a new (unique) buffer name based on TYPE and TO."
6242   (cond
6243    ;; Generate a new buffer name The Message Way.
6244    ((memq message-generate-new-buffers '(unique t))
6245     (generate-new-buffer-name
6246      (concat "*" type
6247              (if to
6248                  (concat " to "
6249                          (or (car (mail-extract-address-components to))
6250                              to) "")
6251                "")
6252              (if (and group (not (string= group ""))) (concat " on " group) "")
6253              "*")))
6254    ;; Check whether `message-generate-new-buffers' is a function,
6255    ;; and if so, call it.
6256    ((functionp message-generate-new-buffers)
6257     (funcall message-generate-new-buffers type to group))
6258    ((eq message-generate-new-buffers 'unsent)
6259     (generate-new-buffer-name
6260      (concat "*unsent " type
6261              (if to
6262                  (concat " to "
6263                          (or (car (mail-extract-address-components to))
6264                              to) "")
6265                "")
6266              (if (and group (not (string= group ""))) (concat " on " group) "")
6267              "*")))
6268    ;; Search for the existing message buffer with the specified name.
6269    (t
6270     (let* ((new (if (eq message-generate-new-buffers 'standard)
6271                     (generate-new-buffer-name (concat "*" type " message*"))
6272                   (let ((message-generate-new-buffers 'unique))
6273                     (message-buffer-name type to group))))
6274            (regexp (concat "\\`"
6275                            (regexp-quote
6276                             (if (string-match "<[0-9]+>\\'" new)
6277                                 (substring new 0 (match-beginning 0))
6278                               new))
6279                            "\\(?:<\\([0-9]+\\)>\\)?\\'"))
6280            (case-fold-search nil))
6281       (or (cdar
6282            (last
6283             (sort
6284              (delq nil
6285                    (mapcar
6286                     (lambda (b)
6287                       (when (and (string-match regexp (setq b (buffer-name b)))
6288                                  (eq (with-current-buffer b major-mode)
6289                                      'message-mode))
6290                         (cons (string-to-number (or (match-string 1 b) "1"))
6291                               b)))
6292                     (buffer-list)))
6293              'car-less-than-car)))
6294           new)))))
6295
6296 (defun message-pop-to-buffer (name &optional switch-function)
6297   "Pop to buffer NAME, and warn if it already exists and is modified."
6298   (let ((buffer (get-buffer name)))
6299     (if (and buffer
6300              (buffer-name buffer))
6301         (let ((window (get-buffer-window buffer 0)))
6302           (if window
6303               ;; Raise the frame already displaying the message buffer.
6304               (progn
6305                 (gnus-select-frame-set-input-focus (window-frame window))
6306                 (select-window window))
6307             (funcall (or switch-function 'pop-to-buffer) buffer)
6308             (set-buffer buffer))
6309           (when (and (buffer-modified-p)
6310                      (not (prog1
6311                               (y-or-n-p
6312                                "Message already being composed; erase? ")
6313                             (message nil))))
6314             (error "Message being composed")))
6315       (funcall (or switch-function 'pop-to-buffer) name)
6316       (set-buffer name))
6317     (erase-buffer)
6318     (message-mode)))
6319
6320 (defun message-do-send-housekeeping ()
6321   "Kill old message buffers."
6322   ;; We might have sent this buffer already.  Delete it from the
6323   ;; list of buffers.
6324   (setq message-buffer-list (delq (current-buffer) message-buffer-list))
6325   (while (and message-max-buffers
6326               message-buffer-list
6327               (>= (length message-buffer-list) message-max-buffers))
6328     ;; Kill the oldest buffer -- unless it has been changed.
6329     (let ((buffer (pop message-buffer-list)))
6330       (when (and (buffer-name buffer)
6331                  (not (buffer-modified-p buffer)))
6332         (kill-buffer buffer))))
6333   ;; Rename the buffer.
6334   (if message-send-rename-function
6335       (funcall message-send-rename-function)
6336     ;; Note: mail-abbrevs of XEmacs renames buffer name behind Gnus.
6337     (when (string-match
6338            "\\`\\*\\(sent \\|unsent \\)?\\(.+\\)\\*[^\\*]*\\|\\`mail to "
6339            (buffer-name))
6340       (let ((name (match-string 2 (buffer-name)))
6341             to group)
6342         (if (not (or (null name)
6343                      (string-equal name "mail")
6344                      (string-equal name "posting")))
6345             (setq name (concat "*sent " name "*"))
6346           (message-narrow-to-headers)
6347           (setq to (message-fetch-field "to"))
6348           (setq group (message-fetch-field "newsgroups"))
6349           (widen)
6350           (setq name
6351                 (cond
6352                  (to (concat "*sent mail to "
6353                              (or (car (mail-extract-address-components to))
6354                                  to) "*"))
6355                  ((and group (not (string= group "")))
6356                   (concat "*sent posting on " group "*"))
6357                  (t "*sent mail*"))))
6358         (unless (string-equal name (buffer-name))
6359           (rename-buffer name t)))))
6360   ;; Push the current buffer onto the list.
6361   (when message-max-buffers
6362     (setq message-buffer-list
6363           (nconc message-buffer-list (list (current-buffer))))))
6364
6365 (defun message-mail-user-agent ()
6366   (let ((mua (cond
6367               ((not message-mail-user-agent) nil)
6368               ((eq message-mail-user-agent t) mail-user-agent)
6369               (t message-mail-user-agent))))
6370     (if (memq mua '(message-user-agent gnus-user-agent))
6371         nil
6372       mua)))
6373
6374 ;; YANK-ACTION, if non-nil, can be a buffer or a yank action of the
6375 ;; form (FUNCTION . ARGS).
6376 (defun message-setup (headers &optional yank-action actions
6377                               continue switch-function return-action)
6378   (let ((mua (message-mail-user-agent))
6379         subject to field)
6380     (if (not (and message-this-is-mail mua))
6381         (message-setup-1 headers yank-action actions return-action)
6382       (setq headers (copy-sequence headers))
6383       (setq field (assq 'Subject headers))
6384       (when field
6385         (setq subject (cdr field))
6386         (setq headers (delq field headers)))
6387       (setq field (assq 'To headers))
6388       (when field
6389         (setq to (cdr field))
6390         (setq headers (delq field headers)))
6391       (let ((mail-user-agent mua))
6392         (compose-mail to subject
6393                       (mapcar (lambda (item)
6394                                 (cons
6395                                  (format "%s" (car item))
6396                                  (cdr item)))
6397                               headers)
6398                       continue switch-function
6399                       (if (bufferp yank-action)
6400                           (list 'insert-buffer yank-action)
6401                         yank-action)
6402                       actions)))))
6403
6404 (defun message-headers-to-generate (headers included-headers excluded-headers)
6405   "Return a list that includes all headers from HEADERS.
6406 If INCLUDED-HEADERS is a list, just include those headers.  If it is
6407 t, include all headers.  In any case, headers from EXCLUDED-HEADERS
6408 are not included."
6409   (let ((result nil)
6410         header-name)
6411     (dolist (header headers)
6412       (setq header-name (cond
6413                          ((and (consp header)
6414                                (eq (car header) 'optional))
6415                           ;; On the form (optional . Header)
6416                           (cdr header))
6417                          ((consp header)
6418                           ;; On the form (Header . function)
6419                           (car header))
6420                          (t
6421                           ;; Just a Header.
6422                           header)))
6423       (when (and (not (memq header-name excluded-headers))
6424                  (or (eq included-headers t)
6425                      (memq header-name included-headers)))
6426         (push header result)))
6427     (nreverse result)))
6428
6429 (defun message-setup-1 (headers &optional yank-action actions return-action)
6430   (dolist (action actions)
6431     (condition-case nil
6432         (add-to-list 'message-send-actions
6433                      `(apply ',(car action) ',(cdr action)))))
6434   (setq message-return-action return-action)
6435   (setq message-reply-buffer
6436         (if (and (consp yank-action)
6437                  (eq (car yank-action) 'insert-buffer))
6438             (nth 1 yank-action)
6439           yank-action))
6440   (goto-char (point-min))
6441   ;; Insert all the headers.
6442   (mail-header-format
6443    (let ((h headers)
6444          (alist message-header-format-alist))
6445      (while h
6446        (unless (assq (caar h) message-header-format-alist)
6447          (push (list (caar h)) alist))
6448        (pop h))
6449      alist)
6450    headers)
6451   (delete-region (point) (progn (forward-line -1) (point)))
6452   (when message-default-headers
6453     (insert
6454      (if (functionp message-default-headers)
6455          (funcall message-default-headers)
6456        message-default-headers))
6457     (or (bolp) (insert ?\n)))
6458   (let ((message-forbidden-properties nil))
6459     (insert (propertize (concat mail-header-separator "\n")
6460                         'read-only t 'rear-nonsticky t 'intangible t)))
6461   (forward-line -1)
6462   ;; If a crash happens while replying, the auto-save file would *not* have a
6463   ;; `References:' header if `message-generate-headers-first' was nil.
6464   ;; Therefore, always generate it first.
6465   (let ((message-generate-headers-first
6466          (append message-generate-headers-first '(References))))
6467     (when (message-news-p)
6468       (when message-default-news-headers
6469         (insert message-default-news-headers)
6470         (or (bolp) (insert ?\n)))
6471       (message-generate-headers
6472        (message-headers-to-generate
6473         (append message-required-news-headers
6474                 message-required-headers)
6475         message-generate-headers-first
6476         '(Lines Subject))))
6477     (when (message-mail-p)
6478       (when message-default-mail-headers
6479         (insert message-default-mail-headers)
6480         (or (bolp) (insert ?\n)))
6481       (message-generate-headers
6482        (message-headers-to-generate
6483         (append message-required-mail-headers
6484                 message-required-headers)
6485         message-generate-headers-first
6486         '(Lines Subject)))))
6487   (run-hooks 'message-signature-setup-hook)
6488   (message-insert-signature)
6489   (save-restriction
6490     (message-narrow-to-headers)
6491     (run-hooks 'message-header-setup-hook))
6492   (setq buffer-undo-list nil)
6493   (when message-generate-hashcash
6494     ;; Generate hashcash headers for recipients already known
6495     (mail-add-payment-async))
6496   ;; Gnus posting styles are applied via buffer-local `message-setup-hook'
6497   ;; values.
6498   (run-hooks 'message-setup-hook)
6499   ;; Do this last to give it precedence over posting styles, etc.
6500   (when (message-mail-p)
6501     (save-restriction
6502       (message-narrow-to-headers)
6503       (if message-alternative-emails
6504           (message-use-alternative-email-as-from))))
6505   (message-position-point)
6506   ;; Allow correct handling of `message-checksum' in `message-yank-original':
6507   (set-buffer-modified-p nil)
6508   (undo-boundary))
6509
6510 (defun message-set-auto-save-file-name ()
6511   "Associate the message buffer with a file in the drafts directory."
6512   (when message-auto-save-directory
6513     (unless (file-directory-p
6514              (directory-file-name message-auto-save-directory))
6515       (make-directory message-auto-save-directory t))
6516     (if (gnus-alive-p)
6517         (setq message-draft-article
6518               (nndraft-request-associate-buffer "drafts"))
6519
6520       ;; If Gnus were alive, draft messages would be saved in the drafts folder.
6521       ;; But Gnus is not alive, so arrange to save the draft message in a
6522       ;; regular file in message-auto-save-directory.  Append a unique
6523       ;; time-based suffix to the filename to allow multiple drafts to be saved
6524       ;; simultaneously without overwriting each other (which mimics the
6525       ;; functionality of the Gnus drafts folder).
6526       (setq buffer-file-name (expand-file-name
6527                               (concat
6528                               (if (memq system-type
6529                                         '(ms-dos windows-nt cygwin))
6530                                   "message"
6531                                 "*message*")
6532                                (format-time-string "-%Y%m%d-%H%M%S"))
6533                               message-auto-save-directory))
6534       (setq buffer-auto-save-file-name (make-auto-save-file-name)))
6535     (clear-visited-file-modtime)
6536     (setq buffer-file-coding-system message-draft-coding-system)))
6537
6538 (defun message-disassociate-draft ()
6539   "Disassociate the message buffer from the drafts directory."
6540   (when message-draft-article
6541     (nndraft-request-expire-articles
6542      (list message-draft-article) "drafts" nil t)))
6543
6544 (defun message-insert-headers ()
6545   "Generate the headers for the article."
6546   (interactive)
6547   (save-excursion
6548     (save-restriction
6549       (message-narrow-to-headers)
6550       (when (message-news-p)
6551         (message-generate-headers
6552          (delq 'Lines
6553                (delq 'Subject
6554                      (copy-sequence message-required-news-headers)))))
6555       (when (message-mail-p)
6556         (message-generate-headers
6557          (delq 'Lines
6558                (delq 'Subject
6559                      (copy-sequence message-required-mail-headers))))))))
6560
6561 \f
6562
6563 ;;;
6564 ;;; Commands for interfacing with message
6565 ;;;
6566
6567 ;;;###autoload
6568 (defun message-mail (&optional to subject other-headers continue
6569                                switch-function yank-action send-actions
6570                                return-action &rest ignored)
6571   "Start editing a mail message to be sent.
6572 OTHER-HEADERS is an alist of header/value pairs.  CONTINUE says whether
6573 to continue editing a message already being composed.  SWITCH-FUNCTION
6574 is a function used to switch to and display the mail buffer."
6575   (interactive)
6576   (let ((message-this-is-mail t))
6577     (unless (message-mail-user-agent)
6578       (message-pop-to-buffer
6579        ;; Search for the existing message buffer if `continue' is non-nil.
6580        (let ((message-generate-new-buffers
6581               (when (or (not continue)
6582                         (eq message-generate-new-buffers 'standard)
6583                         (functionp message-generate-new-buffers))
6584                 message-generate-new-buffers)))
6585          (message-buffer-name "mail" to))
6586        switch-function))
6587     (message-setup
6588      (nconc
6589       `((To . ,(or to "")) (Subject . ,(or subject "")))
6590       ;; C-h f compose-mail says that headers should be specified as
6591       ;; (string . value); however all the rest of message expects
6592       ;; headers to be symbols, not strings (eg message-header-format-alist).
6593       ;; http://lists.gnu.org/archive/html/emacs-devel/2011-01/msg00337.html
6594       ;; We need to convert any string input, eg from rmail-start-mail.
6595       (dolist (h other-headers other-headers)
6596         (if (stringp (car h)) (setcar h (intern (capitalize (car h)))))))
6597      yank-action send-actions continue switch-function
6598      return-action)))
6599
6600 ;;;###autoload
6601 (defun message-news (&optional newsgroups subject)
6602   "Start editing a news article to be sent."
6603   (interactive)
6604   (let ((message-this-is-news t))
6605     (message-pop-to-buffer (message-buffer-name "posting" nil newsgroups))
6606     (message-setup `((Newsgroups . ,(or newsgroups ""))
6607                      (Subject . ,(or subject ""))))))
6608
6609 (defun message-alter-recipients-discard-bogus-full-name (addrcell)
6610   "Discard mail address in full names.
6611 When the full name in reply headers contains the mail
6612 address (e.g. \"foo@bar <foo@bar>\"), discard full name.
6613 ADDRCELL is a cons cell where the car is the mail address and the
6614 cdr is the complete address (full name and mail address)."
6615   (if (string-match (concat (regexp-quote (car addrcell)) ".*"
6616                             (regexp-quote (car addrcell)))
6617                     (cdr addrcell))
6618       (cons (car addrcell) (car addrcell))
6619     addrcell))
6620
6621 (defcustom message-alter-recipients-function nil
6622   "Function called to allow alteration of reply header structures.
6623 It is called in `message-get-reply-headers' for each recipient.
6624 The function is called with one parameter, a cons cell ..."
6625   :type '(choice (const :tag "None" nil)
6626                  (const :tag "Discard bogus full name"
6627                         message-alter-recipients-discard-bogus-full-name)
6628                  function)
6629   :version "23.1" ;; No Gnus
6630   :group 'message-headers)
6631
6632 (defun message-get-reply-headers (wide &optional to-address address-headers)
6633   (let (follow-to mct never-mct to cc author mft recipients extra)
6634     ;; Find all relevant headers we need.
6635     (save-restriction
6636       (message-narrow-to-headers-or-head)
6637       ;; Gmane renames "To".  Look at "Original-To", too, if it is present in
6638       ;; message-header-synonyms.
6639       (setq to (or (message-fetch-field "to")
6640                    (and (loop for synonym in message-header-synonyms
6641                               when (memq 'Original-To synonym)
6642                               return t)
6643                         (message-fetch-field "original-to")))
6644             cc (message-fetch-field "cc")
6645             extra (when message-extra-wide-headers
6646                     (mapconcat 'identity
6647                                (mapcar 'message-fetch-field
6648                                        message-extra-wide-headers)
6649                                ", "))
6650             mct (message-fetch-field "mail-copies-to")
6651             author (or (message-fetch-field "mail-reply-to")
6652                        (message-fetch-field "reply-to")
6653                        (message-fetch-field "from")
6654                        "")
6655             mft (and message-use-mail-followup-to
6656                      (message-fetch-field "mail-followup-to"))))
6657
6658     ;; Handle special values of Mail-Copies-To.
6659     (when mct
6660       (cond ((or (equal (downcase mct) "never")
6661                  (equal (downcase mct) "nobody"))
6662              (setq never-mct t)
6663              (setq mct nil))
6664             ((or (equal (downcase mct) "always")
6665                  (equal (downcase mct) "poster"))
6666              (setq mct author))))
6667
6668     (save-match-data
6669       ;; Build (textual) list of new recipient addresses.
6670       (cond
6671        (to-address
6672         (setq recipients (concat ", " to-address))
6673         ;; If the author explicitly asked for a copy, we don't deny it to them.
6674         (if mct (setq recipients (concat recipients ", " mct))))
6675        ((not wide)
6676         (setq recipients (concat ", " author)))
6677        (address-headers
6678         (dolist (header address-headers)
6679           (let ((value (message-fetch-field header)))
6680             (when value
6681               (setq recipients (concat recipients ", " value))))))
6682        ((and mft
6683              (string-match "[^ \t,]" mft)
6684              (or (not (eq message-use-mail-followup-to 'ask))
6685                  (message-y-or-n-p "Obey Mail-Followup-To? " t "\
6686 You should normally obey the Mail-Followup-To: header.  In this
6687 article, it has the value of
6688
6689 " mft "
6690
6691 which directs your response to " (if (string-match "," mft)
6692                                      "the specified addresses"
6693                                    "that address only") ".
6694
6695 Most commonly, Mail-Followup-To is used by a mailing list poster to
6696 express that responses should be sent to just the list, and not the
6697 poster as well.
6698
6699 If a message is posted to several mailing lists, Mail-Followup-To may
6700 also be used to direct the following discussion to one list only,
6701 because discussions that are spread over several lists tend to be
6702 fragmented and very difficult to follow.
6703
6704 Also, some source/announcement lists are not intended for discussion;
6705 responses here are directed to other addresses.
6706
6707 You may customize the variable `message-use-mail-followup-to', if you
6708 want to get rid of this query permanently.")))
6709         (setq recipients (concat ", " mft)))
6710        (t
6711         (setq recipients (if never-mct "" (concat ", " author)))
6712         (if to (setq recipients (concat recipients ", " to)))
6713         (if cc (setq recipients (concat recipients ", " cc)))
6714         (if extra (setq recipients (concat recipients ", " extra)))
6715         (if mct (setq recipients (concat recipients ", " mct)))))
6716       (if (>= (length recipients) 2)
6717           ;; Strip the leading ", ".
6718           (setq recipients (substring recipients 2)))
6719       ;; Squeeze whitespace.
6720       (while (string-match "[ \t][ \t]+" recipients)
6721         (setq recipients (replace-match " " t t recipients)))
6722       ;; Remove addresses that match `rmail-dont-reply-to-names'.
6723       (let ((rmail-dont-reply-to-names (message-dont-reply-to-names)))
6724         (setq recipients (rmail-dont-reply-to recipients)))
6725       ;; Perhaps "Mail-Copies-To: never" removed the only address?
6726       (if (string-equal recipients "")
6727           (setq recipients author))
6728       ;; Convert string to a list of (("foo@bar" . "Name <Foo@BAR>") ...).
6729       (setq recipients
6730             (mapcar
6731              (lambda (addr)
6732                (if message-alter-recipients-function
6733                    (funcall message-alter-recipients-function
6734                             (cons (downcase (mail-strip-quoted-names addr))
6735                                   addr))
6736                  (cons (downcase (mail-strip-quoted-names addr)) addr)))
6737              (message-tokenize-header recipients)))
6738       ;; Remove first duplicates.  (Why not all duplicates?  Is this a bug?)
6739       (let ((s recipients))
6740         (while s
6741           (setq recipients (delq (assoc (car (pop s)) s) recipients))))
6742
6743       ;; Remove hierarchical lists that are contained within each other,
6744       ;; if message-hierarchical-addresses is defined.
6745       (when message-hierarchical-addresses
6746         (let ((plain-addrs (mapcar 'car recipients))
6747               subaddrs recip)
6748           (while plain-addrs
6749             (setq subaddrs (assoc (car plain-addrs)
6750                                   message-hierarchical-addresses)
6751                   plain-addrs (cdr plain-addrs))
6752             (when subaddrs
6753               (setq subaddrs (cdr subaddrs))
6754               (while subaddrs
6755                 (setq recip (assoc (car subaddrs) recipients)
6756                       subaddrs (cdr subaddrs))
6757                 (if recip
6758                     (setq recipients (delq recip recipients))))))))
6759
6760       (setq recipients (message-prune-recipients recipients))
6761
6762       ;; Build the header alist.  Allow the user to be asked whether
6763       ;; or not to reply to all recipients in a wide reply.
6764       (setq follow-to (list (cons 'To (cdr (pop recipients)))))
6765       (when (and recipients
6766                  (or (not message-wide-reply-confirm-recipients)
6767                      (y-or-n-p "Reply to all recipients? ")))
6768         (setq recipients (mapconcat
6769                           (lambda (addr) (cdr addr)) recipients ", "))
6770         (if (string-match "^ +" recipients)
6771             (setq recipients (substring recipients (match-end 0))))
6772         (push (cons 'Cc recipients) follow-to)))
6773     follow-to))
6774
6775 (defun message-prune-recipients (recipients)
6776   (dolist (rule message-prune-recipient-rules)
6777     (let ((match (car rule))
6778           dup-match
6779           address)
6780       (dolist (recipient recipients)
6781         (setq address (car recipient))
6782         (when (string-match match address)
6783           (setq dup-match (replace-match (cadr rule) nil nil address))
6784           (dolist (recipient recipients)
6785             ;; Don't delete the address that triggered this.
6786             (when (and (not (eq address (car recipient)))
6787                        (string-match dup-match (car recipient)))
6788               (setq recipients (delq recipient recipients))))))))
6789   recipients)
6790
6791 (defcustom message-simplify-subject-functions
6792   '(message-strip-list-identifiers
6793     message-strip-subject-re
6794     message-strip-subject-trailing-was
6795     message-strip-subject-encoded-words)
6796   "List of functions taking a string argument that simplify subjects.
6797 The functions are applied when replying to a message.
6798
6799 Useful functions to put in this list include:
6800 `message-strip-list-identifiers', `message-strip-subject-re',
6801 `message-strip-subject-trailing-was', and
6802 `message-strip-subject-encoded-words'."
6803   :version "22.1" ;; Gnus 5.10.9
6804   :group 'message-various
6805   :type '(repeat function))
6806
6807 (defun message-simplify-subject (subject &optional functions)
6808   "Return simplified SUBJECT."
6809   (unless functions
6810     ;; Simplify fully:
6811     (setq functions message-simplify-subject-functions))
6812   (when (and (memq 'message-strip-list-identifiers functions)
6813              gnus-list-identifiers)
6814     (setq subject (message-strip-list-identifiers subject)))
6815   (when (memq 'message-strip-subject-re functions)
6816     (setq subject (concat "Re: " (message-strip-subject-re subject))))
6817   (when (and (memq 'message-strip-subject-trailing-was functions)
6818              message-subject-trailing-was-query)
6819     (setq subject (message-strip-subject-trailing-was subject)))
6820   (when (memq 'message-strip-subject-encoded-words functions)
6821     (setq subject (message-strip-subject-encoded-words subject)))
6822   subject)
6823
6824 ;;;###autoload
6825 (defun message-reply (&optional to-address wide)
6826   "Start editing a reply to the article in the current buffer."
6827   (interactive)
6828   (require 'gnus-sum)                   ; for gnus-list-identifiers
6829   (let ((cur (current-buffer))
6830         from subject date
6831         references message-id follow-to
6832         (inhibit-point-motion-hooks t)
6833         (message-this-is-mail t)
6834         gnus-warning)
6835     (save-restriction
6836       (message-narrow-to-head-1)
6837       ;; Allow customizations to have their say.
6838       (if (not wide)
6839           ;; This is a regular reply.
6840           (when (functionp message-reply-to-function)
6841             (save-excursion
6842               (setq follow-to (funcall message-reply-to-function))))
6843         ;; This is a followup.
6844         (when (functionp message-wide-reply-to-function)
6845           (save-excursion
6846             (setq follow-to
6847                   (funcall message-wide-reply-to-function)))))
6848       (setq message-id (message-fetch-field "message-id" t)
6849             references (message-fetch-field "references")
6850             date (message-fetch-field "date")
6851             from (or (message-fetch-field "from") "nobody")
6852             subject (or (message-fetch-field "subject") "none"))
6853
6854       ;; Strip list identifiers, "Re: ", and "was:"
6855       (setq subject (message-simplify-subject subject))
6856
6857       (when (and (setq gnus-warning (message-fetch-field "gnus-warning"))
6858                  (string-match "<[^>]+>" gnus-warning))
6859         (setq message-id (match-string 0 gnus-warning)))
6860
6861       (unless follow-to
6862         (setq follow-to (message-get-reply-headers wide to-address))))
6863
6864     (unless (message-mail-user-agent)
6865       (message-pop-to-buffer
6866        (message-buffer-name
6867         (if wide "wide reply" "reply") from
6868         (if wide to-address nil))))
6869
6870     (setq message-reply-headers
6871           (vector 0 subject from date message-id references 0 0 ""))
6872
6873     (message-setup
6874      `((Subject . ,subject)
6875        ,@follow-to)
6876      cur)))
6877
6878 ;;;###autoload
6879 (defun message-wide-reply (&optional to-address)
6880   "Make a \"wide\" reply to the message in the current buffer."
6881   (interactive)
6882   (message-reply to-address t))
6883
6884 ;;;###autoload
6885 (defun message-followup (&optional to-newsgroups)
6886   "Follow up to the message in the current buffer.
6887 If TO-NEWSGROUPS, use that as the new Newsgroups line."
6888   (interactive)
6889   (require 'gnus-sum)                   ; for gnus-list-identifiers
6890   (let ((cur (current-buffer))
6891         from subject date reply-to mrt mct
6892         references message-id follow-to
6893         (inhibit-point-motion-hooks t)
6894         (message-this-is-news t)
6895         followup-to distribution newsgroups gnus-warning posted-to)
6896     (save-restriction
6897       (narrow-to-region
6898        (goto-char (point-min))
6899        (if (search-forward "\n\n" nil t)
6900            (1- (point))
6901          (point-max)))
6902       (when (functionp message-followup-to-function)
6903         (setq follow-to
6904               (funcall message-followup-to-function)))
6905       (setq from (message-fetch-field "from")
6906             date (message-fetch-field "date")
6907             subject (or (message-fetch-field "subject") "none")
6908             references (message-fetch-field "references")
6909             message-id (message-fetch-field "message-id" t)
6910             followup-to (message-fetch-field "followup-to")
6911             newsgroups (message-fetch-field "newsgroups")
6912             posted-to (message-fetch-field "posted-to")
6913             reply-to (message-fetch-field "reply-to")
6914             mrt (message-fetch-field "mail-reply-to")
6915             distribution (message-fetch-field "distribution")
6916             mct (message-fetch-field "mail-copies-to"))
6917       (when (and (setq gnus-warning (message-fetch-field "gnus-warning"))
6918                  (string-match "<[^>]+>" gnus-warning))
6919         (setq message-id (match-string 0 gnus-warning)))
6920       ;; Remove bogus distribution.
6921       (when (and (stringp distribution)
6922                  (let ((case-fold-search t))
6923                    (string-match "world" distribution)))
6924         (setq distribution nil))
6925       ;; Strip list identifiers, "Re: ", and "was:"
6926       (setq subject (message-simplify-subject subject))
6927       (widen))
6928
6929     (message-pop-to-buffer (message-buffer-name "followup" from newsgroups))
6930
6931     (setq message-reply-headers
6932           (vector 0 subject from date message-id references 0 0 ""))
6933
6934     (message-setup
6935      `((Subject . ,subject)
6936        ,@(cond
6937           (to-newsgroups
6938            (list (cons 'Newsgroups to-newsgroups)))
6939           (follow-to follow-to)
6940           ((and followup-to message-use-followup-to)
6941            (list
6942             (cond
6943              ((equal (downcase followup-to) "poster")
6944               (if (or (eq message-use-followup-to 'use)
6945                       (message-y-or-n-p "Obey Followup-To: poster? " t "\
6946 You should normally obey the Followup-To: header.
6947
6948 `Followup-To: poster' sends your response via e-mail instead of news.
6949
6950 A typical situation where `Followup-To: poster' is used is when the poster
6951 does not read the newsgroup, so he wouldn't see any replies sent to it.
6952
6953 You may customize the variable `message-use-followup-to', if you
6954 want to get rid of this query permanently."))
6955                   (progn
6956                     (setq message-this-is-news nil)
6957                     (cons 'To (or mrt reply-to from "")))
6958                 (cons 'Newsgroups newsgroups)))
6959              (t
6960               (if (or (equal followup-to newsgroups)
6961                       (not (eq message-use-followup-to 'ask))
6962                       (message-y-or-n-p
6963                        (concat "Obey Followup-To: " followup-to "? ") t "\
6964 You should normally obey the Followup-To: header.
6965
6966         `Followup-To: " followup-to "'
6967 directs your response to " (if (string-match "," followup-to)
6968                                "the specified newsgroups"
6969                              "that newsgroup only") ".
6970
6971 If a message is posted to several newsgroups, Followup-To is often
6972 used to direct the following discussion to one newsgroup only,
6973 because discussions that are spread over several newsgroup tend to
6974 be fragmented and very difficult to follow.
6975
6976 Also, some source/announcement newsgroups are not intended for discussion;
6977 responses here are directed to other newsgroups.
6978
6979 You may customize the variable `message-use-followup-to', if you
6980 want to get rid of this query permanently."))
6981                   (cons 'Newsgroups followup-to)
6982                 (cons 'Newsgroups newsgroups))))))
6983           (posted-to
6984            `((Newsgroups . ,posted-to)))
6985           (t
6986            `((Newsgroups . ,newsgroups))))
6987        ,@(and distribution (list (cons 'Distribution distribution)))
6988        ,@(when (and mct
6989                     (not (or (equal (downcase mct) "never")
6990                              (equal (downcase mct) "nobody"))))
6991            (list (cons 'Cc (if (or (equal (downcase mct) "always")
6992                                    (equal (downcase mct) "poster"))
6993                                (or mrt reply-to from "")
6994                              mct)))))
6995
6996      cur)))
6997
6998 (defun message-is-yours-p ()
6999   "Non-nil means current article is yours.
7000 If you have added 'cancel-messages to `message-shoot-gnksa-feet', all articles
7001 are yours except those that have Cancel-Lock header not belonging to you.
7002 Instead of shooting GNKSA feet, you should modify `message-alternative-emails'
7003 regexp to match all of yours addresses."
7004   ;; Canlock-logic as suggested by Per Abrahamsen
7005   ;; <abraham@dina.kvl.dk>
7006   ;;
7007   ;; IF article has cancel-lock THEN
7008   ;;   IF we can verify it THEN
7009   ;;     issue cancel
7010   ;;   ELSE
7011   ;;     error: cancellock: article is not yours
7012   ;; ELSE
7013   ;;   Use old rules, comparing sender...
7014   (save-excursion
7015     (save-restriction
7016       (message-narrow-to-head-1)
7017       (if (message-fetch-field "Cancel-Lock")
7018           (if (null (canlock-verify))
7019               t
7020             (error "Failed to verify Cancel-lock: This article is not yours"))
7021         (let (sender from)
7022           (or
7023            (message-gnksa-enable-p 'cancel-messages)
7024            (and (setq sender (message-fetch-field "sender"))
7025                 (string-equal (downcase sender)
7026                               (downcase (message-make-sender))))
7027            ;; Email address in From field equals to our address
7028            (and (setq from (message-fetch-field "from"))
7029                 (string-equal
7030                  (downcase (car (mail-header-parse-address from)))
7031                  (downcase (car (mail-header-parse-address
7032                                  (message-make-from))))))
7033            ;; Email address in From field matches
7034            ;; 'message-alternative-emails' regexp
7035            (and from
7036                 message-alternative-emails
7037                 (string-match
7038                  message-alternative-emails
7039                  (car (mail-header-parse-address from))))))))))
7040
7041 ;;;###autoload
7042 (defun message-cancel-news (&optional arg)
7043   "Cancel an article you posted.
7044 If ARG, allow editing of the cancellation message."
7045   (interactive "P")
7046   (unless (message-news-p)
7047     (error "This is not a news article; canceling is impossible"))
7048   (let (from newsgroups message-id distribution buf)
7049     (save-excursion
7050       ;; Get header info from original article.
7051       (save-restriction
7052         (message-narrow-to-head-1)
7053         (setq from (message-fetch-field "from")
7054               newsgroups (message-fetch-field "newsgroups")
7055               message-id (message-fetch-field "message-id" t)
7056               distribution (message-fetch-field "distribution")))
7057       ;; Make sure that this article was written by the user.
7058       (unless (message-is-yours-p)
7059         (error "This article is not yours"))
7060       (when (yes-or-no-p "Do you really want to cancel this article? ")
7061         ;; Make control message.
7062         (if arg
7063             (message-news)
7064           (setq buf (set-buffer (get-buffer-create " *message cancel*"))))
7065         (erase-buffer)
7066         (insert "Newsgroups: " newsgroups "\n"
7067                 "From: " from "\n"
7068                 "Subject: cmsg cancel " message-id "\n"
7069                 "Control: cancel " message-id "\n"
7070                 (if distribution
7071                     (concat "Distribution: " distribution "\n")
7072                   "")
7073                 mail-header-separator "\n"
7074                 message-cancel-message)
7075         (run-hooks 'message-cancel-hook)
7076         (unless arg
7077           (message "Canceling your article...")
7078           (if (let ((message-syntax-checks
7079                      'dont-check-for-anything-just-trust-me))
7080                 (funcall message-send-news-function))
7081               (message "Canceling your article...done"))
7082           (kill-buffer buf))))))
7083
7084 ;;;###autoload
7085 (defun message-supersede ()
7086   "Start composing a message to supersede the current message.
7087 This is done simply by taking the old article and adding a Supersedes
7088 header line with the old Message-ID."
7089   (interactive)
7090   (let ((cur (current-buffer)))
7091     ;; Check whether the user owns the article that is to be superseded.
7092     (unless (message-is-yours-p)
7093       (error "This article is not yours"))
7094     ;; Get a normal message buffer.
7095     (message-pop-to-buffer (message-buffer-name "supersede"))
7096     (insert-buffer-substring cur)
7097     (mime-to-mml)
7098     (message-narrow-to-head-1)
7099     ;; Remove unwanted headers.
7100     (when message-ignored-supersedes-headers
7101       (message-remove-header message-ignored-supersedes-headers t))
7102     (goto-char (point-min))
7103     (if (not (re-search-forward "^Message-ID: " nil t))
7104         (error "No Message-ID in this article")
7105       (replace-match "Supersedes: " t t))
7106     (goto-char (point-max))
7107     (insert mail-header-separator)
7108     (widen)
7109     (forward-line 1)))
7110
7111 ;;;###autoload
7112 (defun message-recover ()
7113   "Reread contents of current buffer from its last auto-save file."
7114   (interactive)
7115   (let ((file-name (make-auto-save-file-name)))
7116     (cond ((save-window-excursion
7117              (with-output-to-temp-buffer "*Directory*"
7118                (with-current-buffer standard-output
7119                  (fundamental-mode))    ; for Emacs 20.4+
7120                (buffer-disable-undo standard-output)
7121                (let ((default-directory "/"))
7122                  (call-process
7123                   "ls" nil standard-output nil "-l" file-name)))
7124              (yes-or-no-p (format "Recover auto save file %s? " file-name)))
7125            (let ((buffer-read-only nil))
7126              (erase-buffer)
7127              (insert-file-contents file-name nil)))
7128           (t (error "message-recover cancelled")))))
7129
7130 ;;; Washing Subject:
7131
7132 (defun message-wash-subject (subject)
7133   "Remove junk like \"Re:\", \"(fwd)\", etc. added to subject string SUBJECT.
7134 Previous forwarders, replyers, etc. may add it."
7135   (with-temp-buffer
7136     (insert subject)
7137     (goto-char (point-min))
7138     ;; strip Re/Fwd stuff off the beginning
7139     (while (re-search-forward
7140             "\\([Rr][Ee]:\\|[Ff][Ww][Dd]\\(\\[[0-9]*\\]\\)?:\\|[Ff][Ww]:\\)" nil t)
7141       (replace-match ""))
7142
7143     ;; and gnus-style forwards [foo@bar.com] subject
7144     (goto-char (point-min))
7145     (while (re-search-forward "\\[[^ \t]*\\(@\\|\\.\\)[^ \t]*\\]" nil t)
7146       (replace-match ""))
7147
7148     ;; and off the end
7149     (goto-char (point-max))
7150     (while (re-search-backward "([Ff][Ww][Dd])" nil t)
7151       (replace-match ""))
7152
7153     ;; and finally, any whitespace that was left-over
7154     (goto-char (point-min))
7155     (while (re-search-forward "^[ \t]+" nil t)
7156       (replace-match ""))
7157     (goto-char (point-max))
7158     (while (re-search-backward "[ \t]+$" nil t)
7159       (replace-match ""))
7160
7161     (buffer-string)))
7162
7163 ;;; Forwarding messages.
7164
7165 (defvar message-forward-decoded-p nil
7166   "Non-nil means the original message is decoded.")
7167
7168 (defun message-forward-subject-name-subject (subject)
7169   "Generate a SUBJECT for a forwarded message.
7170 The form is: [Source] Subject, where if the original message was mail,
7171 Source is the name of the sender, and if the original message was
7172 news, Source is the list of newsgroups is was posted to."
7173   (let* ((group (message-fetch-field "newsgroups"))
7174          (from (message-fetch-field "from"))
7175          (prefix
7176           (if group
7177               (gnus-group-decoded-name group)
7178             (or (and from (or
7179                            (car (gnus-extract-address-components from))
7180                            (cadr (gnus-extract-address-components from))))
7181                 "(nowhere)"))))
7182     (concat "["
7183             (if message-forward-decoded-p
7184                 prefix
7185               (mail-decode-encoded-word-string prefix))
7186             "] " subject)))
7187
7188 (defun message-forward-subject-author-subject (subject)
7189   "Generate a SUBJECT for a forwarded message.
7190 The form is: [Source] Subject, where if the original message was mail,
7191 Source is the sender, and if the original message was news, Source is
7192 the list of newsgroups is was posted to."
7193   (let* ((group (message-fetch-field "newsgroups"))
7194          (prefix
7195           (if group
7196               (gnus-group-decoded-name group)
7197             (or (message-fetch-field "from")
7198                 "(nowhere)"))))
7199     (concat "["
7200             (if message-forward-decoded-p
7201                 prefix
7202               (mail-decode-encoded-word-string prefix))
7203             "] " subject)))
7204
7205 (defun message-forward-subject-fwd (subject)
7206   "Generate a SUBJECT for a forwarded message.
7207 The form is: Fwd: Subject, where Subject is the original subject of
7208 the message."
7209   (if (string-match "^Fwd: " subject)
7210       subject
7211     (concat "Fwd: " subject)))
7212
7213 (defun message-make-forward-subject ()
7214   "Return a Subject header suitable for the message in the current buffer."
7215   (save-excursion
7216     (save-restriction
7217       (message-narrow-to-head-1)
7218       (let ((funcs message-make-forward-subject-function)
7219             (subject (message-fetch-field "Subject")))
7220         (setq subject
7221               (if subject
7222                   (if message-forward-decoded-p
7223                       subject
7224                     (mail-decode-encoded-word-string subject))
7225                 ""))
7226         (when message-wash-forwarded-subjects
7227           (setq subject (message-wash-subject subject)))
7228         ;; Make sure funcs is a list.
7229         (and funcs
7230              (not (listp funcs))
7231              (setq funcs (list funcs)))
7232         ;; Apply funcs in order, passing subject generated by previous
7233         ;; func to the next one.
7234         (dolist (func funcs)
7235           (when (functionp func)
7236             (setq subject (funcall func subject))))
7237         subject))))
7238
7239 (defvar gnus-article-decoded-p)
7240
7241
7242 ;;;###autoload
7243 (defun message-forward (&optional news digest)
7244   "Forward the current message via mail.
7245 Optional NEWS will use news to forward instead of mail.
7246 Optional DIGEST will use digest to forward."
7247   (interactive "P")
7248   (let* ((cur (current-buffer))
7249          (message-forward-decoded-p
7250           (if (local-variable-p 'gnus-article-decoded-p (current-buffer))
7251               gnus-article-decoded-p ;; In an article buffer.
7252             message-forward-decoded-p))
7253          (subject (message-make-forward-subject)))
7254     (if news
7255         (message-news nil subject)
7256       (message-mail nil subject))
7257     (message-forward-make-body cur digest)))
7258
7259 (defun message-forward-make-body-plain (forward-buffer)
7260   (insert
7261    "\n-------------------- Start of forwarded message --------------------\n")
7262   (let ((b (point))
7263         (contents (with-current-buffer forward-buffer (buffer-string)))
7264         e)
7265     (unless (featurep 'xemacs)
7266       (unless (mm-multibyte-string-p contents)
7267         (error "Attempt to insert unibyte string from the buffer \"%s\"\
7268  to the multibyte buffer \"%s\""
7269                (if (bufferp forward-buffer)
7270                    (buffer-name forward-buffer)
7271                  forward-buffer)
7272                (buffer-name))))
7273     (insert (mm-with-multibyte-buffer
7274               (insert contents)
7275               (mime-to-mml)
7276               (goto-char (point-min))
7277               (when (looking-at "From ")
7278                 (replace-match "X-From-Line: "))
7279               (buffer-string)))
7280     (unless (bolp) (insert "\n"))
7281     (setq e (point))
7282     (insert
7283      "-------------------- End of forwarded message --------------------\n")
7284     (message-remove-ignored-headers b e)))
7285
7286 (defun message-remove-ignored-headers (b e)
7287   (when message-forward-ignored-headers
7288     (save-restriction
7289       (narrow-to-region b e)
7290       (goto-char b)
7291       (narrow-to-region (point)
7292                         (or (search-forward "\n\n" nil t) (point)))
7293       (let ((ignored (if (stringp message-forward-ignored-headers)
7294                          (list message-forward-ignored-headers)
7295                        message-forward-ignored-headers)))
7296         (dolist (elem ignored)
7297           (message-remove-header elem t))))))
7298
7299 (defun message-forward-make-body-mime (forward-buffer)
7300   (let ((b (point)))
7301     (insert "\n\n<#part type=message/rfc822 disposition=inline raw=t>\n")
7302     (save-restriction
7303       (narrow-to-region (point) (point))
7304       (mml-insert-buffer forward-buffer)
7305       (goto-char (point-min))
7306       (when (looking-at "From ")
7307         (replace-match "X-From-Line: "))
7308       (goto-char (point-max)))
7309     (insert "<#/part>\n")
7310     ;; Consider there is no illegible text.
7311     (add-text-properties
7312      b (point)
7313      `(no-illegible-text t rear-nonsticky t start-open t))))
7314
7315 (defun message-forward-make-body-mml (forward-buffer)
7316   (insert "\n\n<#mml type=message/rfc822 disposition=inline>\n")
7317   (let ((b (point)) e)
7318     (if (not message-forward-decoded-p)
7319         (let ((contents (with-current-buffer forward-buffer (buffer-string))))
7320           (unless (featurep 'xemacs)
7321             (unless (mm-multibyte-string-p contents)
7322               (error "Attempt to insert unibyte string from the buffer \"%s\"\
7323  to the multibyte buffer \"%s\""
7324                      (if (bufferp forward-buffer)
7325                          (buffer-name forward-buffer)
7326                        forward-buffer)
7327                      (buffer-name))))
7328           (insert (mm-with-multibyte-buffer
7329                     (insert contents)
7330                     (mime-to-mml)
7331                     (goto-char (point-min))
7332                     (when (looking-at "From ")
7333                       (replace-match "X-From-Line: "))
7334                     (buffer-string))))
7335       (save-restriction
7336         (narrow-to-region (point) (point))
7337         (mml-insert-buffer forward-buffer)
7338         (goto-char (point-min))
7339         (when (looking-at "From ")
7340           (replace-match "X-From-Line: "))
7341         (goto-char (point-max))))
7342     (setq e (point))
7343     (insert "<#/mml>\n")
7344     (when (and (not message-forward-decoded-p)
7345                message-forward-ignored-headers)
7346       (message-remove-ignored-headers b e))))
7347
7348 (defun message-forward-make-body-digest-plain (forward-buffer)
7349   (insert
7350    "\n-------------------- Start of forwarded message --------------------\n")
7351   (mml-insert-buffer forward-buffer)
7352   (insert
7353    "\n-------------------- End of forwarded message --------------------\n"))
7354
7355 (defun message-forward-make-body-digest-mime (forward-buffer)
7356   (insert "\n<#multipart type=digest>\n")
7357   (let ((b (point)) e)
7358     (insert-buffer-substring forward-buffer)
7359     (setq e (point))
7360     (insert "<#/multipart>\n")
7361     (save-restriction
7362       (narrow-to-region b e)
7363       (goto-char b)
7364       (narrow-to-region (point)
7365                         (or (search-forward "\n\n" nil t) (point)))
7366       (delete-region (point-min) (point-max)))))
7367
7368 (defun message-forward-make-body-digest (forward-buffer)
7369   (if message-forward-as-mime
7370       (message-forward-make-body-digest-mime forward-buffer)
7371     (message-forward-make-body-digest-plain forward-buffer)))
7372
7373 (autoload 'mm-uu-dissect-text-parts "mm-uu")
7374 (autoload 'mm-uu-dissect "mm-uu")
7375
7376 (defun message-signed-or-encrypted-p (&optional dont-emulate-mime handles)
7377   "Say whether the current buffer contains signed or encrypted message.
7378 If DONT-EMULATE-MIME is nil, this function does the MIME emulation on
7379 messages that don't conform to PGP/MIME described in RFC2015.  HANDLES
7380 is for the internal use."
7381   (unless handles
7382     (let ((mm-decrypt-option 'never)
7383           (mm-verify-option 'never))
7384       (if (setq handles (mm-dissect-buffer nil t))
7385           (unless dont-emulate-mime
7386             (mm-uu-dissect-text-parts handles))
7387         (unless dont-emulate-mime
7388           (setq handles (mm-uu-dissect))))))
7389   ;; Check text/plain message in which there is a signed or encrypted
7390   ;; body that has been encoded by B or Q.
7391   (unless (or handles dont-emulate-mime)
7392     (let ((cur (current-buffer))
7393           (mm-decrypt-option 'never)
7394           (mm-verify-option 'never))
7395       (with-temp-buffer
7396         (insert-buffer-substring cur)
7397         (when (setq handles (mm-dissect-buffer t t))
7398           (if (and (prog1
7399                        (bufferp (car handles))
7400                      (mm-destroy-parts handles))
7401                    (equal (mm-handle-media-type handles) "text/plain"))
7402               (progn
7403                 (mm-decode-content-transfer-encoding
7404                  (mm-handle-encoding handles))
7405                 (setq handles (mm-uu-dissect)))
7406             (setq handles nil))))))
7407   (when handles
7408     (prog1
7409         (catch 'found
7410           (dolist (handle (if (stringp (car handles))
7411                               (if (member (car handles)
7412                                           '("multipart/signed"
7413                                             "multipart/encrypted"))
7414                                   (throw 'found t)
7415                                 (cdr handles))
7416                             (list handles)))
7417             (if (stringp (car handle))
7418                 (when (message-signed-or-encrypted-p dont-emulate-mime handle)
7419                   (throw 'found t))
7420               (when (and (bufferp (car handle))
7421                          (equal (mm-handle-media-type handle)
7422                                 "message/rfc822"))
7423                 (with-current-buffer (mm-handle-buffer handle)
7424                   (when (message-signed-or-encrypted-p dont-emulate-mime)
7425                     (throw 'found t)))))))
7426       (mm-destroy-parts handles))))
7427
7428 ;;;###autoload
7429 (defun message-forward-make-body (forward-buffer &optional digest)
7430   ;; Put point where we want it before inserting the forwarded
7431   ;; message.
7432   (if message-forward-before-signature
7433       (message-goto-body)
7434     (goto-char (point-max)))
7435   (if digest
7436       (message-forward-make-body-digest forward-buffer)
7437     (if message-forward-as-mime
7438         (if (and message-forward-show-mml
7439                  (not (and (eq message-forward-show-mml 'best)
7440                            ;; Use the raw form in the body if it contains
7441                            ;; signed or encrypted message so as not to be
7442                            ;; destroyed by re-encoding.
7443                            (with-current-buffer forward-buffer
7444                              (condition-case nil
7445                                  (message-signed-or-encrypted-p)
7446                                (error t))))))
7447             (message-forward-make-body-mml forward-buffer)
7448           (message-forward-make-body-mime forward-buffer))
7449       (message-forward-make-body-plain forward-buffer)))
7450   (message-position-point))
7451
7452 (declare-function rmail-toggle-header "rmail" (&optional arg))
7453
7454 ;;;###autoload
7455 (defun message-forward-rmail-make-body (forward-buffer)
7456   (save-window-excursion
7457     (set-buffer forward-buffer)
7458     (if (rmail-msg-is-pruned)
7459         (if (fboundp 'rmail-msg-restore-non-pruned-header)
7460             (rmail-msg-restore-non-pruned-header) ; Emacs 22
7461           (rmail-toggle-header 0))))              ; Emacs 23
7462   (message-forward-make-body forward-buffer))
7463
7464 ;; Fixme: Should have defcustom.
7465 ;;;###autoload
7466 (defun message-insinuate-rmail ()
7467   "Let RMAIL use message to forward."
7468   (interactive)
7469   (setq rmail-enable-mime-composing t)
7470   (setq rmail-insert-mime-forwarded-message-function
7471         'message-forward-rmail-make-body))
7472
7473 (defvar message-inhibit-body-encoding nil)
7474
7475 ;;;###autoload
7476 (defun message-resend (address)
7477   "Resend the current article to ADDRESS."
7478   (interactive
7479    (list (message-read-from-minibuffer "Resend message to: ")))
7480   (message "Resending message to %s..." address)
7481   (save-excursion
7482     (let ((cur (current-buffer))
7483           beg)
7484       ;; We first set up a normal mail buffer.
7485       (unless (message-mail-user-agent)
7486         (set-buffer (get-buffer-create " *message resend*"))
7487         (erase-buffer))
7488       (let ((message-this-is-mail t)
7489             message-generate-hashcash
7490             message-setup-hook)
7491         (message-setup `((To . ,address))))
7492       ;; Insert our usual headers.
7493       (message-generate-headers '(From Date To Message-ID))
7494       (message-narrow-to-headers)
7495       ;; Remove X-Draft-From header etc.
7496       (message-remove-header message-ignored-mail-headers t)
7497       ;; Rename them all to "Resent-*".
7498       (goto-char (point-min))
7499       (while (re-search-forward "^[A-Za-z]" nil t)
7500         (forward-char -1)
7501         (insert "Resent-"))
7502       (widen)
7503       (forward-line)
7504       (delete-region (point) (point-max))
7505       (setq beg (point))
7506       ;; Insert the message to be resent.
7507       (insert-buffer-substring cur)
7508       (goto-char (point-min))
7509       (search-forward "\n\n")
7510       (forward-char -1)
7511       (save-restriction
7512         (narrow-to-region beg (point))
7513         (message-remove-header message-ignored-resent-headers t)
7514         (goto-char (point-max)))
7515       (insert mail-header-separator)
7516       ;; Rename all old ("Also-")Resent headers.
7517       (while (re-search-backward "^\\(Also-\\)*Resent-" beg t)
7518         (beginning-of-line)
7519         (insert "Also-"))
7520       ;; Quote any "From " lines at the beginning.
7521       (goto-char beg)
7522       (when (looking-at "From ")
7523         (replace-match "X-From-Line: "))
7524       ;; Send it.
7525       (let ((message-inhibit-body-encoding
7526              ;; Don't do any further encoding if it looks like the
7527              ;; message has already been encoded.
7528              (let ((case-fold-search t))
7529                (re-search-forward "^mime-version:" nil t)))
7530             (message-inhibit-ecomplete t)
7531             message-required-mail-headers
7532             message-generate-hashcash
7533             rfc2047-encode-encoded-words)
7534         (message-send-mail))
7535       (kill-buffer (current-buffer)))
7536     (message "Resending message to %s...done" address)))
7537
7538 ;;;###autoload
7539 (defun message-bounce ()
7540   "Re-mail the current message.
7541 This only makes sense if the current message is a bounce message that
7542 contains some mail you have written which has been bounced back to
7543 you."
7544   (interactive)
7545   (let ((handles (mm-dissect-buffer t))
7546         boundary)
7547     (message-pop-to-buffer (message-buffer-name "bounce"))
7548     (if (stringp (car handles))
7549         ;; This is a MIME bounce.
7550         (mm-insert-part (car (last handles)))
7551       ;; This is a non-MIME bounce, so we try to remove things
7552       ;; manually.
7553       (mm-insert-part handles)
7554       (undo-boundary)
7555       (goto-char (point-min))
7556       (re-search-forward "\n\n+" nil t)
7557       (setq boundary (point))
7558       ;; We remove everything before the bounced mail.
7559       (if (or (re-search-forward message-unsent-separator nil t)
7560               (progn
7561                 (search-forward "\n\n" nil 'move)
7562                 (re-search-backward "^Return-Path:.*\n" boundary t)))
7563           (progn
7564             (forward-line 1)
7565             (delete-region (point-min)
7566                            (if (re-search-forward "^[^ \n\t]+:" nil t)
7567                                (match-beginning 0)
7568                              (point))))
7569         (goto-char boundary)
7570         (when (re-search-backward "^.?From .*\n" nil t)
7571           (delete-region (match-beginning 0) (match-end 0)))))
7572     (mime-to-mml)
7573     (save-restriction
7574       (message-narrow-to-head-1)
7575       (message-remove-header message-ignored-bounced-headers t)
7576       (goto-char (point-max))
7577       (insert mail-header-separator))
7578     (message-position-point)))
7579
7580 ;;;
7581 ;;; Interactive entry points for new message buffers.
7582 ;;;
7583
7584 ;;;###autoload
7585 (defun message-mail-other-window (&optional to subject)
7586   "Like `message-mail' command, but display mail buffer in another window."
7587   (interactive)
7588   (unless (message-mail-user-agent)
7589     (let ((pop-up-windows t)
7590           (special-display-buffer-names nil)
7591           (special-display-regexps nil)
7592           (same-window-buffer-names nil)
7593           (same-window-regexps nil))
7594       (message-pop-to-buffer (message-buffer-name "mail" to))))
7595   (let ((message-this-is-mail t))
7596     (message-setup `((To . ,(or to "")) (Subject . ,(or subject "")))
7597                    nil nil nil 'switch-to-buffer-other-window)))
7598
7599 ;;;###autoload
7600 (defun message-mail-other-frame (&optional to subject)
7601   "Like `message-mail' command, but display mail buffer in another frame."
7602   (interactive)
7603   (unless (message-mail-user-agent)
7604     (let ((pop-up-frames t)
7605           (special-display-buffer-names nil)
7606           (special-display-regexps nil)
7607           (same-window-buffer-names nil)
7608           (same-window-regexps nil))
7609       (message-pop-to-buffer (message-buffer-name "mail" to))))
7610   (let ((message-this-is-mail t))
7611     (message-setup `((To . ,(or to "")) (Subject . ,(or subject "")))
7612                    nil nil nil 'switch-to-buffer-other-frame)))
7613
7614 ;;;###autoload
7615 (defun message-news-other-window (&optional newsgroups subject)
7616   "Start editing a news article to be sent."
7617   (interactive)
7618   (let ((pop-up-windows t)
7619         (special-display-buffer-names nil)
7620         (special-display-regexps nil)
7621         (same-window-buffer-names nil)
7622         (same-window-regexps nil))
7623     (message-pop-to-buffer (message-buffer-name "posting" nil newsgroups)))
7624   (let ((message-this-is-news t))
7625     (message-setup `((Newsgroups . ,(or newsgroups ""))
7626                      (Subject . ,(or subject ""))))))
7627
7628 ;;;###autoload
7629 (defun message-news-other-frame (&optional newsgroups subject)
7630   "Start editing a news article to be sent."
7631   (interactive)
7632   (let ((pop-up-frames t)
7633         (special-display-buffer-names nil)
7634         (special-display-regexps nil)
7635         (same-window-buffer-names nil)
7636         (same-window-regexps nil))
7637     (message-pop-to-buffer (message-buffer-name "posting" nil newsgroups)))
7638   (let ((message-this-is-news t))
7639     (message-setup `((Newsgroups . ,(or newsgroups ""))
7640                      (Subject . ,(or subject ""))))))
7641
7642 ;;; underline.el
7643
7644 ;; This code should be moved to underline.el (from which it is stolen).
7645
7646 ;;;###autoload
7647 (defun message-bold-region (start end)
7648   "Bold all nonblank characters in the region.
7649 Works by overstriking characters.
7650 Called from program, takes two arguments START and END
7651 which specify the range to operate on."
7652   (interactive "r")
7653   (save-excursion
7654     (let ((end1 (make-marker)))
7655       (move-marker end1 (max start end))
7656       (goto-char (min start end))
7657       (while (< (point) end1)
7658         (or (looking-at "[_\^@- ]")
7659             (insert (char-after) "\b"))
7660         (forward-char 1)))))
7661
7662 ;;;###autoload
7663 (defun message-unbold-region (start end)
7664   "Remove all boldness (overstruck characters) in the region.
7665 Called from program, takes two arguments START and END
7666 which specify the range to operate on."
7667   (interactive "r")
7668   (save-excursion
7669     (let ((end1 (make-marker)))
7670       (move-marker end1 (max start end))
7671       (goto-char (min start end))
7672       (while (search-forward "\b" end1 t)
7673         (if (eq (char-after) (char-after (- (point) 2)))
7674             (delete-char -2))))))
7675
7676 (defun message-exchange-point-and-mark ()
7677   "Exchange point and mark, but don't activate region if it was inactive."
7678   (goto-char (prog1 (mark t)
7679                (set-marker (mark-marker) (point)))))
7680
7681 (defalias 'message-make-overlay 'make-overlay)
7682 (defalias 'message-delete-overlay 'delete-overlay)
7683 (defalias 'message-overlay-put 'overlay-put)
7684 (defun message-kill-all-overlays ()
7685   (if (featurep 'xemacs)
7686       (map-extents (lambda (extent ignore) (delete-extent extent)))
7687     (mapcar #'delete-overlay (overlays-in (point-min) (point-max)))))
7688
7689 ;; Support for toolbar
7690 (defvar tool-bar-mode)
7691
7692 ;; Note: The :set function in the `message-tool-bar*' variables will only
7693 ;; affect _new_ message buffers.  We might add a function that walks thru all
7694 ;; message-mode buffers and force the update.
7695 (defun message-tool-bar-update (&optional symbol value)
7696   "Update message mode toolbar.
7697 Setter function for custom variables."
7698   (setq-default message-tool-bar-map nil)
7699   (when symbol
7700     ;; When used as ":set" function:
7701     (set-default symbol value)))
7702
7703 (defcustom message-tool-bar (if (eq gmm-tool-bar-style 'gnome)
7704                                 'message-tool-bar-gnome
7705                               'message-tool-bar-retro)
7706   "Specifies the message mode tool bar.
7707
7708 It can be either a list or a symbol refering to a list.  See
7709 `gmm-tool-bar-from-list' for the format of the list.  The
7710 default key map is `message-mode-map'.
7711
7712 Pre-defined symbols include `message-tool-bar-gnome' and
7713 `message-tool-bar-retro'."
7714   :type '(repeat gmm-tool-bar-list-item)
7715   :type '(choice (const :tag "GNOME style" message-tool-bar-gnome)
7716                  (const :tag "Retro look"  message-tool-bar-retro)
7717                  (repeat :tag "User defined list" gmm-tool-bar-item)
7718                  (symbol))
7719   :version "23.1" ;; No Gnus
7720   :initialize 'custom-initialize-default
7721   :set 'message-tool-bar-update
7722   :group 'message)
7723
7724 (defcustom message-tool-bar-gnome
7725   '((ispell-message "spell" nil
7726                     :vert-only t
7727                     :visible (or (not (boundp 'flyspell-mode))
7728                                  (not flyspell-mode)))
7729     (flyspell-buffer "spell" t
7730                      :vert-only t
7731                      :visible (and (boundp 'flyspell-mode)
7732                                    flyspell-mode)
7733                      :help "Flyspell whole buffer")
7734     (message-send-and-exit "mail/send" t :label "Send")
7735     (message-dont-send "mail/save-draft")
7736     (mml-attach-file "attach" mml-mode-map :vert-only t)
7737     (mml-preview "mail/preview" mml-mode-map)
7738     (mml-secure-message-sign-encrypt "lock" mml-mode-map :visible nil)
7739     (message-insert-importance-high "important" nil :visible nil)
7740     (message-insert-importance-low "unimportant" nil :visible nil)
7741     (message-insert-disposition-notification-to "receipt" nil :visible nil))
7742   "List of items for the message tool bar (GNOME style).
7743
7744 See `gmm-tool-bar-from-list' for details on the format of the list."
7745   :type '(repeat gmm-tool-bar-item)
7746   :version "23.1" ;; No Gnus
7747   :initialize 'custom-initialize-default
7748   :set 'message-tool-bar-update
7749   :group 'message)
7750
7751 (defcustom message-tool-bar-retro
7752   '(;; Old Emacs 21 icon for consistency.
7753     (message-send-and-exit "gnus/mail-send")
7754     (message-kill-buffer "close")
7755     (message-dont-send "cancel")
7756     (mml-attach-file "attach" mml-mode-map)
7757     (ispell-message "spell")
7758     (mml-preview "preview" mml-mode-map)
7759     (message-insert-importance-high "gnus/important")
7760     (message-insert-importance-low "gnus/unimportant")
7761     (message-insert-disposition-notification-to "gnus/receipt"))
7762   "List of items for the message tool bar (retro style).
7763
7764 See `gmm-tool-bar-from-list' for details on the format of the list."
7765   :type '(repeat gmm-tool-bar-item)
7766   :version "23.1" ;; No Gnus
7767   :initialize 'custom-initialize-default
7768   :set 'message-tool-bar-update
7769   :group 'message)
7770
7771 (defcustom message-tool-bar-zap-list
7772   '(new-file open-file dired kill-buffer write-file
7773              print-buffer customize help)
7774   "List of icon items from the global tool bar.
7775 These items are not displayed on the message mode tool bar.
7776
7777 See `gmm-tool-bar-from-list' for the format of the list."
7778   :type 'gmm-tool-bar-zap-list
7779   :version "23.1" ;; No Gnus
7780   :initialize 'custom-initialize-default
7781   :set 'message-tool-bar-update
7782   :group 'message)
7783
7784 (defvar image-load-path)
7785
7786 (defun message-make-tool-bar (&optional force)
7787   "Make a message mode tool bar from `message-tool-bar-list'.
7788 When FORCE, rebuild the tool bar."
7789   (when (and (not (featurep 'xemacs))
7790              (boundp 'tool-bar-mode)
7791              tool-bar-mode
7792              (or (not message-tool-bar-map) force))
7793     (setq message-tool-bar-map
7794           (let* ((load-path
7795                   (gmm-image-load-path-for-library "message"
7796                                                    "mail/save-draft.xpm"
7797                                                    nil t))
7798                  (image-load-path (cons (car load-path)
7799                                         (when (boundp 'image-load-path)
7800                                           image-load-path))))
7801             (gmm-tool-bar-from-list message-tool-bar
7802                                     message-tool-bar-zap-list
7803                                     'message-mode-map))))
7804   message-tool-bar-map)
7805
7806 ;;; Group name completion.
7807
7808 (defcustom message-newgroups-header-regexp
7809   "^\\(Newsgroups\\|Followup-To\\|Posted-To\\|Gcc\\):"
7810   "Regexp that match headers that lists groups."
7811   :group 'message
7812   :type 'regexp)
7813
7814 (defcustom message-completion-alist
7815   (list (cons message-newgroups-header-regexp 'message-expand-group)
7816         '("^\\(Resent-\\)?\\(To\\|B?Cc\\):" . message-expand-name)
7817         '("^\\(Reply-To\\|From\\|Mail-Followup-To\\|Mail-Copies-To\\):"
7818           . message-expand-name)
7819         '("^\\(Disposition-Notification-To\\|Return-Receipt-To\\):"
7820           . message-expand-name))
7821   "Alist of (RE . FUN).  Use FUN for completion on header lines matching RE."
7822   :version "22.1"
7823   :group 'message
7824   :type '(alist :key-type regexp :value-type function))
7825
7826 (defcustom message-expand-name-databases
7827   '(bbdb eudc)
7828   "List of databases to try for name completion (`message-expand-name').
7829 Each element is a symbol and can be `bbdb' or `eudc'."
7830   :group 'message
7831   :type '(set (const bbdb) (const eudc)))
7832
7833 (defcustom message-tab-body-function nil
7834   "*Function to execute when `message-tab' (TAB) is executed in the body.
7835 If nil, the function bound in `text-mode-map' or `global-map' is executed."
7836   :version "22.1"
7837   :group 'message
7838   :link '(custom-manual "(message)Various Commands")
7839   :type '(choice (const nil)
7840                  function))
7841
7842 (declare-function mail-abbrev-in-expansion-header-p "mailabbrev" ())
7843
7844 (defun message-tab ()
7845   "Complete names according to `message-completion-alist'.
7846 Execute function specified by `message-tab-body-function' when not in
7847 those headers."
7848   (interactive)
7849   (cond
7850    ((if (and (boundp 'completion-fail-discreetly)
7851              (fboundp 'completion-at-point))
7852         (let ((completion-fail-discreetly t)) (completion-at-point))
7853       (funcall (or (message-completion-function) #'ignore)))
7854     ;; Completion was performed; nothing else to do.
7855     nil)
7856    (message-tab-body-function (funcall message-tab-body-function))
7857    (t (funcall (or (lookup-key text-mode-map "\t")
7858                    (lookup-key global-map "\t")
7859                    'indent-relative)))))
7860
7861 (defvar mail-abbrev-mode-regexp)
7862
7863 (defun message-completion-function ()
7864   (let ((alist message-completion-alist))
7865     (while (and alist
7866                 (let ((mail-abbrev-mode-regexp (caar alist)))
7867                   (not (mail-abbrev-in-expansion-header-p))))
7868       (setq alist (cdr alist)))
7869     (cdar alist)))
7870
7871 (eval-and-compile
7872   (condition-case nil
7873       (with-temp-buffer
7874         (let ((standard-output (current-buffer)))
7875           (eval '(display-completion-list nil "")))
7876         (defalias 'message-display-completion-list 'display-completion-list))
7877     (error ;; Don't use `wrong-number-of-arguments' here because of XEmacs.
7878      (defun message-display-completion-list (completions &optional ignore)
7879        "Display the list of completions, COMPLETIONS, using `standard-output'."
7880        (display-completion-list completions)))))
7881
7882 (defun message-expand-group ()
7883   "Expand the group name under point."
7884   (let* ((b (save-excursion
7885               (save-restriction
7886                 (narrow-to-region
7887                  (save-excursion
7888                    (beginning-of-line)
7889                    (skip-chars-forward "^:")
7890                    (1+ (point)))
7891                  (point))
7892                 (skip-chars-backward "^, \t\n") (point))))
7893          (completion-ignore-case t)
7894          (e (progn (skip-chars-forward "^,\t\n ") (point)))
7895          (hashtb (and (boundp 'gnus-active-hashtb) gnus-active-hashtb)))
7896     (message-completion-in-region e b hashtb)))
7897
7898 (defalias 'message-completion-in-region
7899   (if (fboundp 'completion-in-region)
7900       'completion-in-region
7901     (lambda (e b hashtb)
7902       (let* ((string (buffer-substring b e))
7903              (completions (all-completions string hashtb))
7904              comp)
7905         (delete-region b (point))
7906         (cond
7907          ((= (length completions) 1)
7908           (if (string= (car completions) string)
7909               (progn
7910                 (insert string)
7911                 (message "Only matching group"))
7912             (insert (car completions))))
7913          ((and (setq comp (try-completion string hashtb))
7914                (not (string= comp string)))
7915           (insert comp))
7916          (t
7917           (insert string)
7918           (if (not comp)
7919               (message "No matching groups")
7920             (save-selected-window
7921               (pop-to-buffer "*Completions*")
7922               (buffer-disable-undo)
7923               (let ((buffer-read-only nil))
7924                 (erase-buffer)
7925                 (let ((standard-output (current-buffer)))
7926                   (message-display-completion-list (sort completions 'string<)
7927                                                    string))
7928                 (setq buffer-read-only nil)
7929                 (goto-char (point-min))
7930                 (delete-region (point)
7931                                (progn (forward-line 3) (point))))))))))))
7932
7933 (defun message-expand-name ()
7934   (cond ((and (memq 'eudc message-expand-name-databases)
7935                     (boundp 'eudc-protocol)
7936                     eudc-protocol)
7937          (eudc-expand-inline))
7938         ((and (memq 'bbdb message-expand-name-databases)
7939               (fboundp 'bbdb-complete-name))
7940          (let ((starttick (buffer-modified-tick)))
7941            (or (bbdb-complete-name)
7942                ;; Apparently, bbdb-complete-name can return nil even when
7943                ;; completion took place.  So let's double check the buffer was
7944                ;; not modified.
7945                (/= starttick (buffer-modified-tick)))))
7946         (t
7947          (expand-abbrev))))
7948
7949 ;;; Help stuff.
7950
7951 (defun message-talkative-question (ask question show &rest text)
7952   "Call FUNCTION with argument QUESTION; optionally display TEXT... args.
7953 If SHOW is non-nil, the arguments TEXT... are displayed in a temp buffer.
7954 The following arguments may contain lists of values."
7955   (if (and show
7956            (setq text (message-flatten-list text)))
7957       (save-window-excursion
7958         (with-output-to-temp-buffer " *MESSAGE information message*"
7959           (with-current-buffer " *MESSAGE information message*"
7960             (fundamental-mode)          ; for Emacs 20.4+
7961             (mapc 'princ text)
7962             (goto-char (point-min))))
7963         (funcall ask question))
7964     (funcall ask question)))
7965
7966 (defun message-flatten-list (list)
7967   "Return a new, flat list that contains all elements of LIST.
7968
7969 \(message-flatten-list '(1 (2 3 (4 5 (6))) 7))
7970 => (1 2 3 4 5 6 7)"
7971   (cond ((consp list)
7972          (apply 'append (mapcar 'message-flatten-list list)))
7973         (list
7974          (list list))))
7975
7976 (defun message-generate-new-buffer-clone-locals (name &optional varstr)
7977   "Create and return a buffer with name based on NAME using `generate-new-buffer'.
7978 Then clone the local variables and values from the old buffer to the
7979 new one, cloning only the locals having a substring matching the
7980 regexp VARSTR."
7981   (let ((oldbuf (current-buffer)))
7982     (with-current-buffer (generate-new-buffer name)
7983       (message-clone-locals oldbuf varstr)
7984       (current-buffer))))
7985
7986 (defun message-clone-locals (buffer &optional varstr)
7987   "Clone the local variables from BUFFER to the current buffer."
7988   (let ((locals (with-current-buffer buffer (buffer-local-variables)))
7989         (regexp "^gnus\\|^nn\\|^message\\|^sendmail\\|^smtp\\|^user-mail-address"))
7990     (mapcar
7991      (lambda (local)
7992        (when (and (consp local)
7993                   (car local)
7994                   (string-match regexp (symbol-name (car local)))
7995                   (or (null varstr)
7996                       (string-match varstr (symbol-name (car local)))))
7997          (ignore-errors
7998            (set (make-local-variable (car local))
7999                 (cdr local)))))
8000      locals)))
8001
8002 ;;;
8003 ;;; MIME functions
8004 ;;;
8005
8006 (defun message-encode-message-body ()
8007   (unless message-inhibit-body-encoding
8008     (let ((mail-parse-charset (or mail-parse-charset
8009                                   message-default-charset))
8010           (case-fold-search t)
8011           lines content-type-p)
8012       (message-goto-body)
8013       (save-restriction
8014         (narrow-to-region (point) (point-max))
8015         (let ((new (mml-generate-mime)))
8016           (when new
8017             (delete-region (point-min) (point-max))
8018             (insert new)
8019             (goto-char (point-min))
8020             (if (eq (aref new 0) ?\n)
8021                 (delete-char 1)
8022               (search-forward "\n\n")
8023               (setq lines (buffer-substring (point-min) (1- (point))))
8024               (delete-region (point-min) (point))))))
8025       (save-restriction
8026         (message-narrow-to-headers-or-head)
8027         (message-remove-header "Mime-Version")
8028         (goto-char (point-max))
8029         (insert "MIME-Version: 1.0\n")
8030         (when lines
8031           (insert lines))
8032         (setq content-type-p
8033               (or mml-boundary
8034                   (re-search-backward "^Content-Type:" nil t))))
8035       (save-restriction
8036         (message-narrow-to-headers-or-head)
8037         (message-remove-first-header "Content-Type")
8038         (message-remove-first-header "Content-Transfer-Encoding"))
8039       ;; We always make sure that the message has a Content-Type
8040       ;; header.  This is because some broken MTAs and MUAs get
8041       ;; awfully confused when confronted with a message with a
8042       ;; MIME-Version header and without a Content-Type header.  For
8043       ;; instance, Solaris' /usr/bin/mail.
8044       (unless content-type-p
8045         (goto-char (point-min))
8046         ;; For unknown reason, MIME-Version doesn't exist.
8047         (when (re-search-forward "^MIME-Version:" nil t)
8048           (forward-line 1)
8049           (insert "Content-Type: text/plain; charset=us-ascii\n"))))))
8050
8051 (defun message-read-from-minibuffer (prompt &optional initial-contents)
8052   "Read from the minibuffer while providing abbrev expansion."
8053   (if (fboundp 'mail-abbrevs-setup)
8054       (let ((mail-abbrev-mode-regexp "")
8055             (minibuffer-setup-hook 'mail-abbrevs-setup)
8056             (minibuffer-local-map message-minibuffer-local-map))
8057         (read-from-minibuffer prompt initial-contents))
8058     (let ((minibuffer-setup-hook 'mail-abbrev-minibuffer-setup-hook)
8059           (minibuffer-local-map message-minibuffer-local-map))
8060       (read-string prompt initial-contents))))
8061
8062 (defun message-use-alternative-email-as-from ()
8063   "Set From field of the outgoing message to the first matching
8064 address in `message-alternative-emails', looking at To, Cc and
8065 From headers in the original article."
8066   (require 'mail-utils)
8067   (let* ((fields '("To" "Cc" "From"))
8068          (emails
8069           (split-string
8070            (mail-strip-quoted-names
8071             (mapconcat 'message-fetch-reply-field fields ","))
8072            "[ \f\t\n\r\v,]+"))
8073          email)
8074     (while emails
8075       (if (string-match message-alternative-emails (car emails))
8076           (setq email (car emails)
8077                 emails nil))
8078       (pop emails))
8079     (unless (or (not email) (equal email user-mail-address))
8080       (message-remove-header "From")
8081       (goto-char (point-max))
8082       (insert "From: " (let ((user-mail-address email)) (message-make-from))
8083               "\n"))))
8084
8085 (defun message-options-get (symbol)
8086   (cdr (assq symbol message-options)))
8087
8088 (defun message-options-set (symbol value)
8089   (let ((the-cons (assq symbol message-options)))
8090     (if the-cons
8091         (if value
8092             (setcdr the-cons value)
8093           (setq message-options (delq the-cons message-options)))
8094       (and value
8095            (push (cons symbol value) message-options))))
8096   value)
8097
8098 (defun message-options-set-recipient ()
8099   (save-restriction
8100     (message-narrow-to-headers-or-head)
8101     (message-options-set 'message-sender
8102                          (mail-strip-quoted-names
8103                           (message-fetch-field "from")))
8104     (message-options-set 'message-recipients
8105                          (mail-strip-quoted-names
8106                           (let ((to (message-fetch-field "to"))
8107                                 (cc (message-fetch-field "cc"))
8108                                 (bcc (message-fetch-field "bcc")))
8109                             (concat
8110                              (or to "")
8111                              (if (and to cc) ", ")
8112                              (or cc "")
8113                              (if (and (or to cc) bcc) ", ")
8114                              (or bcc "")))))))
8115
8116 (defun message-hide-headers ()
8117   "Hide headers based on the `message-hidden-headers' variable."
8118   (let ((regexps (if (stringp message-hidden-headers)
8119                      (list message-hidden-headers)
8120                    message-hidden-headers))
8121         (inhibit-point-motion-hooks t)
8122         (after-change-functions nil)
8123         (end-of-headers (point-min)))
8124     (when regexps
8125       (save-excursion
8126         (save-restriction
8127           (message-narrow-to-headers)
8128           (goto-char (point-min))
8129           (while (not (eobp))
8130             (if (not (message-hide-header-p regexps))
8131                 (message-next-header)
8132               (let ((begin (point))
8133                     header header-len)
8134                 (message-next-header)
8135                 (setq header (buffer-substring begin (point))
8136                       header-len (- (point) begin))
8137                 (delete-region begin (point))
8138                 (goto-char end-of-headers)
8139                 (insert header)
8140                 (setq end-of-headers
8141                       (+ end-of-headers header-len))))))))
8142     (narrow-to-region end-of-headers (point-max))))
8143
8144 (defun message-hide-header-p (regexps)
8145   (let ((result nil)
8146         (reverse nil))
8147     (when (eq (car regexps) 'not)
8148       (setq reverse t)
8149       (pop regexps))
8150     (dolist (regexp regexps)
8151       (setq result (or result (looking-at regexp))))
8152     (if reverse
8153         (not result)
8154       result)))
8155
8156 (declare-function ecomplete-add-item "ecomplete" (type key text))
8157 (declare-function ecomplete-save "ecomplete" ())
8158
8159 (defun message-put-addresses-in-ecomplete ()
8160   (require 'ecomplete)
8161   (dolist (header '("to" "cc" "from" "reply-to"))
8162     (let ((value (message-field-value header)))
8163       (dolist (string (mail-header-parse-addresses value 'raw))
8164         (setq string
8165               (gnus-replace-in-string
8166                (gnus-replace-in-string string "^ +\\| +$" "") "\n" ""))
8167         (ecomplete-add-item 'mail (car (mail-header-parse-address string))
8168                             string))))
8169   (ecomplete-save))
8170
8171 (autoload 'ecomplete-display-matches "ecomplete")
8172
8173 (defun message-display-abbrev (&optional choose)
8174   "Display the next possible abbrev for the text before point."
8175   (interactive (list t))
8176   (when (and (memq (char-after (point-at-bol)) '(?C ?T ?\t ? ))
8177              (message-point-in-header-p)
8178              (save-excursion
8179                (beginning-of-line)
8180                (while (and (memq (char-after) '(?\t ? ))
8181                            (zerop (forward-line -1))))
8182                (looking-at "To:\\|Cc:")))
8183     (let* ((end (point))
8184            (start (save-excursion
8185                     (and (re-search-backward "[\n\t ]" nil t)
8186                          (1+ (point)))))
8187            (word (when start (buffer-substring start end)))
8188            (match (when (and word
8189                              (not (zerop (length word))))
8190                     (ecomplete-display-matches 'mail word choose))))
8191       (when (and choose match)
8192         (delete-region start end)
8193         (insert match)))))
8194
8195 ;; To send pre-formatted letters like the example below, you can use
8196 ;; `message-send-form-letter':
8197 ;; --8<---------------cut here---------------start------------->8---
8198 ;; To: alice@invalid.invalid
8199 ;; Subject: Verification of your contact information
8200 ;; From: Contact verification <admin@foo.invalid>
8201 ;; --text follows this line--
8202 ;; Hi Alice,
8203 ;; please verify that your contact information is still valid:
8204 ;; Alice A, A avenue 11, 1111 A town, Austria
8205 ;; ----------next form letter message follows this line----------
8206 ;; To: bob@invalid.invalid
8207 ;; Subject: Verification of your contact information
8208 ;; From: Contact verification <admin@foo.invalid>
8209 ;; --text follows this line--
8210 ;; Hi Bob,
8211 ;; please verify that your contact information is still valid:
8212 ;; Bob, B street 22, 22222 Be town, Belgium
8213 ;; ----------next form letter message follows this line----------
8214 ;; To: charlie@invalid.invalid
8215 ;; Subject: Verification of your contact information
8216 ;; From: Contact verification <admin@foo.invalid>
8217 ;; --text follows this line--
8218 ;; Hi Charlie,
8219 ;; please verify that your contact information is still valid:
8220 ;; Charlie Chaplin, C plaza 33, 33333 C town, Chile
8221 ;; --8<---------------cut here---------------end--------------->8---
8222
8223 ;; FIXME: What is the most common term (circular letter, form letter, serial
8224 ;; letter, standard letter) for such kind of letter?  See also
8225 ;; <http://en.wikipedia.org/wiki/Form_letter>
8226
8227 ;; FIXME: Maybe extent message-mode's font-lock support to recognize
8228 ;; `message-form-letter-separator', i.e. highlight each message like a single
8229 ;; message.
8230
8231 (defcustom message-form-letter-separator
8232   "\n----------next form letter message follows this line----------\n"
8233   "Separator for `message-send-form-letter'."
8234   ;; :group 'message-form-letter
8235   :group 'message-various
8236   :version "23.1" ;; No Gnus
8237   :type 'string)
8238
8239 (defcustom message-send-form-letter-delay 1
8240   "Delay in seconds when sending a message with `message-send-form-letter'.
8241 Only used when `message-send-form-letter' is called with non-nil
8242 argument `force'."
8243   ;; :group 'message-form-letter
8244   :group 'message-various
8245   :version "23.1" ;; No Gnus
8246   :type 'integer)
8247
8248 (defun message-send-form-letter (&optional force)
8249   "Sent all form letter messages from current buffer.
8250 Unless FORCE, prompt before sending.
8251
8252 The messages are separated by `message-form-letter-separator'.
8253 Header and body are separated by `mail-header-separator'."
8254   (interactive "P")
8255   (let ((sent 0) (skipped 0)
8256         start end text
8257         buff
8258         to done)
8259     (goto-char (point-min))
8260     (while (not done)
8261       (setq start (point)
8262             end (if (search-forward message-form-letter-separator nil t)
8263                     (- (point) (length message-form-letter-separator) -1)
8264                   (setq done t)
8265                   (point-max)))
8266       (setq text
8267             (buffer-substring-no-properties start end))
8268       (setq buff (generate-new-buffer "*mail - form letter*"))
8269       (with-current-buffer buff
8270         (insert text)
8271         (message-mode)
8272         (setq to (message-fetch-field "To"))
8273         (switch-to-buffer buff)
8274         (when force
8275           (sit-for message-send-form-letter-delay))
8276         (if (or force
8277                   (y-or-n-p (format "Send message to `%s'? " to)))
8278             (progn
8279               (setq sent (1+ sent))
8280               (message-send-and-exit))
8281           (message (format "Message to `%s' skipped." to))
8282           (setq skipped (1+ skipped)))
8283         (when (buffer-live-p buff)
8284           (kill-buffer buff))))
8285     (message "%s message(s) sent, %s skipped." sent skipped)))
8286
8287 (defun message-replace-header (header new-value &optional after force)
8288   "Remove HEADER and insert the NEW-VALUE.
8289 If AFTER, insert after this header.  If FORCE, insert new field
8290 even if NEW-VALUE is empty."
8291   ;; Similar to `nnheader-replace-header' but for message buffers.
8292   (save-excursion
8293     (save-restriction
8294       (message-narrow-to-headers)
8295       (message-remove-header header))
8296     (when (or force (> (length new-value) 0))
8297       (if after
8298           (message-position-on-field header after)
8299         (message-position-on-field header))
8300       (insert new-value))))
8301
8302 (defcustom message-recipients-without-full-name
8303   (list "ding@gnus.org"
8304         "bugs@gnus.org"
8305         "emacs-devel@gnu.org"
8306         "emacs-pretest-bug@gnu.org"
8307         "bug-gnu-emacs@gnu.org")
8308   "Mail addresses that have no full name.
8309 Used in `message-simplify-recipients'."
8310   ;; Maybe the addresses could be extracted from
8311   ;; `gnus-parameter-to-list-alist'?
8312   :type '(choice (const :tag "None" nil)
8313                  (repeat string))
8314   :version "23.1" ;; No Gnus
8315   :group 'message-headers)
8316
8317 (defun message-simplify-recipients ()
8318   (interactive)
8319   (dolist (hdr '("Cc" "To"))
8320     (message-replace-header
8321      hdr
8322      (mapconcat
8323       (lambda (addrcomp)
8324         (if (and message-recipients-without-full-name
8325                  (string-match
8326                   (regexp-opt message-recipients-without-full-name)
8327                   (cadr addrcomp)))
8328             (cadr addrcomp)
8329           (if (car addrcomp)
8330               (message-make-from (car addrcomp) (cadr addrcomp))
8331             (cadr addrcomp))))
8332       (when (message-fetch-field hdr)
8333         (mail-extract-address-components
8334          (message-fetch-field hdr) t))
8335       ", "))))
8336
8337 (when (featurep 'xemacs)
8338   (require 'messagexmas)
8339   (message-xmas-redefine))
8340
8341 (provide 'message)
8342
8343 (run-hooks 'message-load-hook)
8344
8345 ;; Local Variables:
8346 ;; coding: iso-8859-1
8347 ;; End:
8348
8349 ;;; message.el ends here