Indent.
[gnus] / lisp / message.el
1 ;;; message.el --- composing mail and news messages
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: mail, news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;; This mode provides mail-sending facilities from within Emacs.  It
28 ;; consists mainly of large chunks of code from the sendmail.el,
29 ;; gnus-msg.el and rnewspost.el files.
30
31 ;;; Code:
32
33 (eval-when-compile
34   (require 'cl)
35   (defvar gnus-list-identifiers)) ; gnus-sum is required where necessary
36 (require 'canlock)
37 (require 'mailheader)
38 (require 'nnheader)
39 ;; This is apparently necessary even though things are autoloaded.
40 ;; Because we dynamically bind mail-abbrev-mode-regexp, we'd better
41 ;; require mailabbrev here.
42 (if (featurep 'xemacs)
43     (require 'mail-abbrevs)
44   (require 'mailabbrev))
45 (require 'mail-parse)
46 (require 'mml)
47 (require 'rfc822)
48 (eval-and-compile
49   (autoload 'sha1 "sha1-el"))
50
51 (defgroup message '((user-mail-address custom-variable)
52                     (user-full-name custom-variable))
53   "Mail and news message composing."
54   :link '(custom-manual "(message)Top")
55   :group 'mail
56   :group 'news)
57
58 (put 'user-mail-address 'custom-type 'string)
59 (put 'user-full-name 'custom-type 'string)
60
61 (defgroup message-various nil
62   "Various Message Variables"
63   :link '(custom-manual "(message)Various Message Variables")
64   :group 'message)
65
66 (defgroup message-buffers nil
67   "Message Buffers"
68   :link '(custom-manual "(message)Message Buffers")
69   :group 'message)
70
71 (defgroup message-sending nil
72   "Message Sending"
73   :link '(custom-manual "(message)Sending Variables")
74   :group 'message)
75
76 (defgroup message-interface nil
77   "Message Interface"
78   :link '(custom-manual "(message)Interface")
79   :group 'message)
80
81 (defgroup message-forwarding nil
82   "Message Forwarding"
83   :link '(custom-manual "(message)Forwarding")
84   :group 'message-interface)
85
86 (defgroup message-insertion nil
87   "Message Insertion"
88   :link '(custom-manual "(message)Insertion")
89   :group 'message)
90
91 (defgroup message-headers nil
92   "Message Headers"
93   :link '(custom-manual "(message)Message Headers")
94   :group 'message)
95
96 (defgroup message-news nil
97   "Composing News Messages"
98   :group 'message)
99
100 (defgroup message-mail nil
101   "Composing Mail Messages"
102   :group 'message)
103
104 (defgroup message-faces nil
105   "Faces used for message composing."
106   :group 'message
107   :group 'faces)
108
109 (defcustom message-directory "~/Mail/"
110   "*Directory from which all other mail file variables are derived."
111   :group 'message-various
112   :type 'directory)
113
114 (defcustom message-max-buffers 10
115   "*How many buffers to keep before starting to kill them off."
116   :group 'message-buffers
117   :type 'integer)
118
119 (defcustom message-send-rename-function nil
120   "Function called to rename the buffer after sending it."
121   :group 'message-buffers
122   :type '(choice function (const nil)))
123
124 (defcustom message-fcc-handler-function 'message-output
125   "*A function called to save outgoing articles.
126 This function will be called with the name of the file to store the
127 article in.  The default function is `message-output' which saves in Unix
128 mailbox format."
129   :type '(radio (function-item message-output)
130                 (function :tag "Other"))
131   :group 'message-sending)
132
133 (defcustom message-fcc-externalize-attachments nil
134   "If non-nil, attachments are included as external parts in Fcc copies."
135   :type 'boolean
136   :group 'message-sending)
137
138 (defcustom message-courtesy-message
139   "The following message is a courtesy copy of an article\nthat has been posted to %s as well.\n\n"
140   "*This is inserted at the start of a mailed copy of a posted message.
141 If the string contains the format spec \"%s\", the Newsgroups
142 the article has been posted to will be inserted there.
143 If this variable is nil, no such courtesy message will be added."
144   :group 'message-sending
145   :type 'string)
146
147 (defcustom message-ignored-bounced-headers
148   "^\\(Received\\|Return-Path\\|Delivered-To\\):"
149   "*Regexp that matches headers to be removed in resent bounced mail."
150   :group 'message-interface
151   :type 'regexp)
152
153 ;;;###autoload
154 (defcustom message-from-style 'default
155   "*Specifies how \"From\" headers look.
156
157 If nil, they contain just the return address like:
158         king@grassland.com
159 If `parens', they look like:
160         king@grassland.com (Elvis Parsley)
161 If `angles', they look like:
162         Elvis Parsley <king@grassland.com>
163
164 Otherwise, most addresses look like `angles', but they look like
165 `parens' if `angles' would need quoting and `parens' would not."
166   :type '(choice (const :tag "simple" nil)
167                  (const parens)
168                  (const angles)
169                  (const default))
170   :group 'message-headers)
171
172 (defcustom message-insert-canlock t
173   "Whether to insert a Cancel-Lock header in news postings."
174   :version "21.3"
175   :group 'message-headers
176   :type 'boolean)
177
178 (defcustom message-syntax-checks
179   (if message-insert-canlock '((sender . disabled)) nil)
180   ;; Guess this one shouldn't be easy to customize...
181   "*Controls what syntax checks should not be performed on outgoing posts.
182 To disable checking of long signatures, for instance, add
183  `(signature . disabled)' to this list.
184
185 Don't touch this variable unless you really know what you're doing.
186
187 Checks include `subject-cmsg', `multiple-headers', `sendsys',
188 `message-id', `from', `long-lines', `control-chars', `size',
189 `new-text', `quoting-style', `redirected-followup', `signature',
190 `approved', `sender', `empty', `empty-headers', `message-id', `from',
191 `subject', `shorten-followup-to', `existing-newsgroups',
192 `buffer-file-name', `unchanged', `newsgroups', `reply-to',
193 'continuation-headers', and `long-header-lines'."
194   :group 'message-news
195   :type '(repeat sexp))                 ; Fixme: improve this
196
197 (defcustom message-required-headers '((optional . References)
198                                       From)
199   "*Headers to be generated or prompted for when sending a message.
200 Also see `message-required-news-headers' and
201 `message-required-mail-headers'."
202   :group 'message-news
203   :group 'message-headers
204   :type '(repeat sexp))
205
206 (defcustom message-draft-headers '(References From)
207   "*Headers to be generated when saving a draft message."
208   :group 'message-news
209   :group 'message-headers
210   :type '(repeat sexp))
211
212 (defcustom message-required-news-headers
213   '(From Newsgroups Subject Date Message-ID
214          (optional . Organization)
215          (optional . User-Agent))
216   "*Headers to be generated or prompted for when posting an article.
217 RFC977 and RFC1036 require From, Date, Newsgroups, Subject,
218 Message-ID.  Organization, Lines, In-Reply-To, Expires, and
219 User-Agent are optional.  If don't you want message to insert some
220 header, remove it from this list."
221   :group 'message-news
222   :group 'message-headers
223   :type '(repeat sexp))
224
225 (defcustom message-required-mail-headers
226   '(From Subject Date (optional . In-Reply-To) Message-ID
227          (optional . User-Agent))
228   "*Headers to be generated or prompted for when mailing a message.
229 It is recommended that From, Date, To, Subject and Message-ID be
230 included.  Organization and User-Agent are optional."
231   :group 'message-mail
232   :group 'message-headers
233   :type '(repeat sexp))
234
235 (defcustom message-deletable-headers '(Message-ID Date Lines)
236   "Headers to be deleted if they already exist and were generated by message previously."
237   :group 'message-headers
238   :type 'sexp)
239
240 (defcustom message-ignored-news-headers
241   "^NNTP-Posting-Host:\\|^Xref:\\|^[BGF]cc:\\|^Resent-Fcc:\\|^X-Draft-From:\\|^X-Gnus-Agent-Meta-Information:"
242   "*Regexp of headers to be removed unconditionally before posting."
243   :group 'message-news
244   :group 'message-headers
245   :type 'regexp)
246
247 (defcustom message-ignored-mail-headers
248   "^[GF]cc:\\|^Resent-Fcc:\\|^Xref:\\|^X-Draft-From:\\|^X-Gnus-Agent-Meta-Information:"
249   "*Regexp of headers to be removed unconditionally before mailing."
250   :group 'message-mail
251   :group 'message-headers
252   :type 'regexp)
253
254 (defcustom message-ignored-supersedes-headers "^Path:\\|^Date\\|^NNTP-Posting-Host:\\|^Xref:\\|^Lines:\\|^Received:\\|^X-From-Line:\\|^X-Trace:\\|^X-Complaints-To:\\|Return-Path:\\|^Supersedes:\\|^NNTP-Posting-Date:\\|^X-Trace:\\|^X-Complaints-To:\\|^Cancel-Lock:\\|^Cancel-Key:\\|^X-Hashcash:"
255   "*Header lines matching this regexp will be deleted before posting.
256 It's best to delete old Path and Date headers before posting to avoid
257 any confusion."
258   :group 'message-interface
259   :type 'regexp)
260
261 (defcustom message-subject-re-regexp
262   "^[ \t]*\\([Rr][Ee]\\(\\[[0-9]*\\]\\)*:[ \t]*\\)*[ \t]*"
263   "*Regexp matching \"Re: \" in the subject line."
264   :group 'message-various
265   :type 'regexp)
266
267 ;;; Start of variables adopted from `message-utils.el'.
268
269 (defcustom message-subject-trailing-was-query 'ask
270   ;; should it default to nil or ask?
271   "*What to do with trailing \"(was: <old subject>)\" in subject lines.
272 If nil, leave the subject unchanged.  If it is the symbol `ask', query
273 the user what do do.  In this case, the subject is matched against
274 `message-subject-trailing-was-ask-regexp'.  If
275 `message-subject-trailing-was-query' is t, always strip the trailing
276 old subject.  In this case, `message-subject-trailing-was-regexp' is
277 used."
278   :type '(choice (const :tag "never" nil)
279                  (const :tag "always strip" t)
280                  (const ask))
281   :group 'message-various)
282
283 (defcustom message-subject-trailing-was-ask-regexp
284   "[ \t]*\\([[(]+[Ww][Aa][Ss][ \t]*.*[\])]+\\)"
285   "*Regexp matching \"(was: <old subject>)\" in the subject line.
286
287 The function `message-strip-subject-trailing-was' uses this regexp if
288 `message-subject-trailing-was-query' is set to the symbol `ask'.  If
289 the variable is t instead of `ask', use
290 `message-subject-trailing-was-regexp' instead.
291
292 It is okay to create some false positives here, as the user is asked."
293   :group 'message-various
294   :type 'regexp)
295
296 (defcustom message-subject-trailing-was-regexp
297   "[ \t]*\\((*[Ww][Aa][Ss]:[ \t]*.*)\\)"
298   "*Regexp matching \"(was: <old subject>)\" in the subject line.
299
300 If `message-subject-trailing-was-query' is set to t, the subject is
301 matched against `message-subject-trailing-was-regexp' in
302 `message-strip-subject-trailing-was'.  You should use a regexp creating very
303 few false positives here."
304   :group 'message-various
305   :type 'regexp)
306
307 ;; Fixme: Why are all these things autoloaded?
308
309 ;;; marking inserted text
310
311 ;;;###autoload
312 (defcustom message-mark-insert-begin
313   "--8<---------------cut here---------------start------------->8---\n"
314   "How to mark the beginning of some inserted text."
315   :type 'string
316   :group 'message-various)
317
318 ;;;###autoload
319 (defcustom message-mark-insert-end
320   "--8<---------------cut here---------------end--------------->8---\n"
321   "How to mark the end of some inserted text."
322   :type 'string
323   :group 'message-various)
324
325 ;;;###autoload
326 (defcustom message-archive-header
327   "X-No-Archive: Yes\n"
328   "Header to insert when you don't want your article to be archived.
329 Archives \(such as groups.googgle.com\) respect this header."
330   :type 'string
331   :group 'message-various)
332
333 ;;;###autoload
334 (defcustom message-archive-note
335   "X-No-Archive: Yes - save http://groups.google.com/"
336   "Note to insert why you wouldn't want this posting archived.
337 If nil, don't insert any text in the body."
338   :type 'string
339   :group 'message-various)
340
341 ;;; Crossposts and Followups
342 ;; inspired by JoH-followup-to by Jochem Huhman <joh  at gmx.de>
343 ;; new suggestions by R. Weikusat <rw at another.de>
344
345 (defvar message-cross-post-old-target nil
346   "Old target for cross-posts or follow-ups.")
347 (make-variable-buffer-local 'message-cross-post-old-target)
348
349 ;;;###autoload
350 (defcustom message-cross-post-default t
351   "When non-nil `message-cross-post-followup-to' will perform a crosspost.
352 If nil, `message-cross-post-followup-to' will only do a followup.  Note that
353 you can explicitly override this setting by calling
354 `message-cross-post-followup-to' with a prefix."
355   :type 'boolean
356   :group 'message-various)
357
358 ;;;###autoload
359 (defcustom message-cross-post-note
360   "Crosspost & Followup-To: "
361   "Note to insert before signature to notify of cross-post and follow-up."
362   :type 'string
363   :group 'message-various)
364
365 ;;;###autoload
366 (defcustom message-followup-to-note
367   "Followup-To: "
368   "Note to insert before signature to notify of follow-up only."
369   :type 'string
370   :group 'message-various)
371
372 ;;;###autoload
373 (defcustom message-cross-post-note-function
374   'message-cross-post-insert-note
375   "Function to use to insert note about Crosspost or Followup-To.
376 The function will be called with four arguments.  The function should not only
377 insert a note, but also ensure old notes are deleted.  See the documentation
378 for `message-cross-post-insert-note'."
379   :type 'function
380   :group 'message-various)
381
382 ;;; End of variables adopted from `message-utils.el'.
383
384 ;;;###autoload
385 (defcustom message-signature-separator "^-- *$"
386   "Regexp matching the signature separator."
387   :type 'regexp
388   :group 'message-various)
389
390 (defcustom message-elide-ellipsis "\n[...]\n\n"
391   "*The string which is inserted for elided text."
392   :type 'string
393   :group 'message-various)
394
395 (defcustom message-interactive t
396   "Non-nil means when sending a message wait for and display errors.
397 nil means let mailer mail back a message to report errors."
398   :group 'message-sending
399   :group 'message-mail
400   :type 'boolean)
401
402 (defcustom message-generate-new-buffers 'unique
403   "*Non-nil means create a new message buffer whenever `message-setup' is called.
404 If this is a function, call that function with three parameters:  The type,
405 the to address and the group name.  (Any of these may be nil.)  The function
406 should return the new buffer name."
407   :group 'message-buffers
408   :type '(choice (const :tag "off" nil)
409                  (const :tag "unique" unique)
410                  (const :tag "unsent" unsent)
411                  (function fun)))
412
413 (defcustom message-kill-buffer-on-exit nil
414   "*Non-nil means that the message buffer will be killed after sending a message."
415   :group 'message-buffers
416   :type 'boolean)
417
418 (eval-when-compile
419   (defvar gnus-local-organization))
420 (defcustom message-user-organization
421   (or (and (boundp 'gnus-local-organization)
422            (stringp gnus-local-organization)
423            gnus-local-organization)
424       (getenv "ORGANIZATION")
425       t)
426   "*String to be used as an Organization header.
427 If t, use `message-user-organization-file'."
428   :group 'message-headers
429   :type '(choice string
430                  (const :tag "consult file" t)))
431
432 ;;;###autoload
433 (defcustom message-user-organization-file "/usr/lib/news/organization"
434   "*Local news organization file."
435   :type 'file
436   :group 'message-headers)
437
438 (defcustom message-make-forward-subject-function
439   'message-forward-subject-name-subject
440   "*List of functions called to generate subject headers for forwarded messages.
441 The subject generated by the previous function is passed into each
442 successive function.
443
444 The provided functions are:
445
446 * `message-forward-subject-author-subject' (Source of article (author or
447       newsgroup)), in brackets followed by the subject
448 * `message-forward-subject-name-subject' (Source of article (name of author
449       or newsgroup)), in brackets followed by the subject
450 * `message-forward-subject-fwd' (Subject of article with 'Fwd:' prepended
451       to it."
452   :group 'message-forwarding
453   :type '(radio (function-item message-forward-subject-author-subject)
454                 (function-item message-forward-subject-fwd)
455                 (repeat :tag "List of functions" function)))
456
457 (defcustom message-forward-as-mime t
458   "*If non-nil, forward messages as an inline/rfc822 MIME section.  Otherwise, directly inline the old message in the forwarded message."
459   :version "21.1"
460   :group 'message-forwarding
461   :type 'boolean)
462
463 (defcustom message-forward-show-mml t
464   "*If non-nil, forward messages are shown as mml.  Otherwise, forward messages are unchanged."
465   :version "21.1"
466   :group 'message-forwarding
467   :type 'boolean)
468
469 (defcustom message-forward-before-signature t
470   "*If non-nil, put forwarded message before signature, else after."
471   :group 'message-forwarding
472   :type 'boolean)
473
474 (defcustom message-wash-forwarded-subjects nil
475   "*If non-nil, try to remove as much old cruft as possible from the subject of messages before generating the new subject of a forward."
476   :group 'message-forwarding
477   :type 'boolean)
478
479 (defcustom message-ignored-resent-headers "^Return-receipt\\|^X-Gnus\\|^Gnus-Warning:\\|^>?From "
480   "*All headers that match this regexp will be deleted when resending a message."
481   :group 'message-interface
482   :type 'regexp)
483
484 (defcustom message-forward-ignored-headers "^Content-Transfer-Encoding:\\|^X-Gnus"
485   "*All headers that match this regexp will be deleted when forwarding a message."
486   :version "21.1"
487   :group 'message-forwarding
488   :type '(choice (const :tag "None" nil)
489                  regexp))
490
491 (defcustom message-ignored-cited-headers "."
492   "*Delete these headers from the messages you yank."
493   :group 'message-insertion
494   :type 'regexp)
495
496 (defcustom message-cite-prefix-regexp
497   (if (string-match "[[:digit:]]" "1") ;; support POSIX?
498       "\\([ \t]*[-_.[:word:]]+>+\\|[ \t]*[]>|}+]\\)+"
499     ;; ?-, ?_ or ?. MUST NOT be in syntax entry w.
500     (let ((old-table (syntax-table))
501           non-word-constituents)
502       (set-syntax-table text-mode-syntax-table)
503       (setq non-word-constituents
504             (concat
505              (if (string-match "\\w" "-")  "" "-")
506              (if (string-match "\\w" "_")  "" "_")
507              (if (string-match "\\w" ".")  "" ".")))
508       (set-syntax-table old-table)
509       (if (equal non-word-constituents "")
510           "\\([ \t]*\\(\\w\\)+>+\\|[ \t]*[]>|}+]\\)+"
511         (concat "\\([ \t]*\\(\\w\\|["
512                 non-word-constituents
513                 "]\\)+>+\\|[ \t]*[]>|}+]\\)+"))))
514   "*Regexp matching the longest possible citation prefix on a line."
515   :group 'message-insertion
516   :type 'regexp)
517
518 (defcustom message-cancel-message "I am canceling my own article.\n"
519   "Message to be inserted in the cancel message."
520   :group 'message-interface
521   :type 'string)
522
523 ;; Useful to set in site-init.el
524 ;;;###autoload
525 (defcustom message-send-mail-function 'message-send-mail-with-sendmail
526   "Function to call to send the current buffer as mail.
527 The headers should be delimited by a line whose contents match the
528 variable `mail-header-separator'.
529
530 Valid values include `message-send-mail-with-sendmail' (the default),
531 `message-send-mail-with-mh', `message-send-mail-with-qmail',
532 `message-smtpmail-send-it', `smtpmail-send-it' and `feedmail-send-it'.
533
534 See also `send-mail-function'."
535   :type '(radio (function-item message-send-mail-with-sendmail)
536                 (function-item message-send-mail-with-mh)
537                 (function-item message-send-mail-with-qmail)
538                 (function-item message-smtpmail-send-it)
539                 (function-item smtpmail-send-it)
540                 (function-item feedmail-send-it)
541                 (function :tag "Other"))
542   :group 'message-sending
543   :group 'message-mail)
544
545 (defcustom message-send-news-function 'message-send-news
546   "Function to call to send the current buffer as news.
547 The headers should be delimited by a line whose contents match the
548 variable `mail-header-separator'."
549   :group 'message-sending
550   :group 'message-news
551   :type 'function)
552
553 (defcustom message-reply-to-function nil
554   "If non-nil, function that should return a list of headers.
555 This function should pick out addresses from the To, Cc, and From headers
556 and respond with new To and Cc headers."
557   :group 'message-interface
558   :type '(choice function (const nil)))
559
560 (defcustom message-wide-reply-to-function nil
561   "If non-nil, function that should return a list of headers.
562 This function should pick out addresses from the To, Cc, and From headers
563 and respond with new To and Cc headers."
564   :group 'message-interface
565   :type '(choice function (const nil)))
566
567 (defcustom message-followup-to-function nil
568   "If non-nil, function that should return a list of headers.
569 This function should pick out addresses from the To, Cc, and From headers
570 and respond with new To and Cc headers."
571   :group 'message-interface
572   :type '(choice function (const nil)))
573
574 (defcustom message-use-followup-to 'ask
575   "*Specifies what to do with Followup-To header.
576 If nil, always ignore the header.  If it is t, use its value, but
577 query before using the \"poster\" value.  If it is the symbol `ask',
578 always query the user whether to use the value.  If it is the symbol
579 `use', always use the value."
580   :group 'message-interface
581   :type '(choice (const :tag "ignore" nil)
582                  (const :tag "use & query" t)
583                  (const use)
584                  (const ask)))
585
586 (defcustom message-use-mail-followup-to 'use
587   "*Specifies what to do with Mail-Followup-To header.
588 If nil, always ignore the header.  If it is the symbol `ask', always
589 query the user whether to use the value.  If it is the symbol `use',
590 always use the value."
591   :group 'message-interface
592   :type '(choice (const :tag "ignore" nil)
593                  (const use)
594                  (const ask)))
595
596 (defcustom message-subscribed-address-functions nil
597   "*Specifies functions for determining list subscription.
598 If nil, do not attempt to determine list subscribtion with functions.
599 If non-nil, this variable contains a list of functions which return
600 regular expressions to match lists.  These functions can be used in
601 conjunction with `message-subscribed-regexps' and
602 `message-subscribed-addresses'."
603   :group 'message-interface
604   :type '(repeat sexp))
605
606 (defcustom message-subscribed-address-file nil
607   "*A file containing addresses the user is subscribed to.
608 If nil, do not look at any files to determine list subscriptions.  If
609 non-nil, each line of this file should be a mailing list address."
610   :group 'message-interface
611   :type 'string)
612
613 (defcustom message-subscribed-addresses nil
614   "*Specifies a list of addresses the user is subscribed to.
615 If nil, do not use any predefined list subscriptions.  This list of
616 addresses can be used in conjuction with
617 `message-subscribed-address-functions' and `message-subscribed-regexps'."
618   :group 'message-interface
619   :type '(repeat string))
620
621 (defcustom message-subscribed-regexps nil
622   "*Specifies a list of addresses the user is subscribed to.
623 If nil, do not use any predefined list subscriptions.  This list of
624 regular expressions can be used in conjuction with
625 `message-subscribed-address-functions' and `message-subscribed-addresses'."
626   :group 'message-interface
627   :type '(repeat regexp))
628
629 (defcustom message-allow-no-recipients 'ask
630   "Specifies what to do when there are no recipients other than Gcc/Fcc.
631 If it is the symbol `always', the posting is allowed.  If it is the
632 symbol `never', the posting is not allowed.  If it is the symbol
633 `ask', you are prompted."
634   :group 'message-interface
635   :type '(choice (const always)
636                  (const never)
637                  (const ask)))
638
639 (defcustom message-sendmail-f-is-evil nil
640   "*Non-nil means don't add \"-f username\" to the sendmail command line.
641 Doing so would be even more evil than leaving it out."
642   :group 'message-sending
643   :type 'boolean)
644
645 (defcustom message-sendmail-envelope-from nil
646   "*Envelope-from when sending mail with sendmail.
647 If this is nil, use `user-mail-address'.  If it is the symbol
648 `header', use the From: header of the message."
649   :type '(choice (string :tag "From name")
650                  (const :tag "Use From: header from message" header)
651                  (const :tag "Use `user-mail-address'" nil))
652   :group 'message-sending)
653
654 ;; qmail-related stuff
655 (defcustom message-qmail-inject-program "/var/qmail/bin/qmail-inject"
656   "Location of the qmail-inject program."
657   :group 'message-sending
658   :type 'file)
659
660 (defcustom message-qmail-inject-args nil
661   "Arguments passed to qmail-inject programs.
662 This should be a list of strings, one string for each argument.  It
663 may also be a function.
664
665 For e.g., if you wish to set the envelope sender address so that bounces
666 go to the right place or to deal with listserv's usage of that address, you
667 might set this variable to '(\"-f\" \"you@some.where\")."
668   :group 'message-sending
669   :type '(choice (function)
670                  (repeat string)))
671
672 (defvar message-cater-to-broken-inn t
673   "Non-nil means Gnus should not fold the `References' header.
674 Folding `References' makes ancient versions of INN create incorrect
675 NOV lines.")
676
677 (eval-when-compile
678   (defvar gnus-post-method)
679   (defvar gnus-select-method))
680 (defcustom message-post-method
681   (cond ((and (boundp 'gnus-post-method)
682               (listp gnus-post-method)
683               gnus-post-method)
684          gnus-post-method)
685         ((boundp 'gnus-select-method)
686          gnus-select-method)
687         (t '(nnspool "")))
688   "*Method used to post news.
689 Note that when posting from inside Gnus, for instance, this
690 variable isn't used."
691   :group 'message-news
692   :group 'message-sending
693   ;; This should be the `gnus-select-method' widget, but that might
694   ;; create a dependence to `gnus.el'.
695   :type 'sexp)
696
697 ;; FIXME: This should be a temporary workaround until someone implements a
698 ;; proper solution.  If a crash happens while replying, the auto-save file
699 ;; will *not* have a `References:' header if `message-generate-headers-first'
700 ;; is nil.  See: http://article.gmane.org/gmane.emacs.gnus.general/51138
701 (defcustom message-generate-headers-first '(references)
702   "*If non-nil, generate all required headers before composing.
703 The variables `message-required-news-headers' and
704 `message-required-mail-headers' specify which headers to generate.
705 This can also be a list of headers that should be generated before
706 composing.
707
708 Note that the variable `message-deletable-headers' specifies headers which
709 are to be deleted and then re-generated before sending, so this variable
710 will not have a visible effect for those headers."
711   :group 'message-headers
712   :type '(choice (const :tag "None" nil)
713                  (const :tag "References" '(references))
714                  (const :tag "All" t)
715                  (repeat (sexp :tag "Header"))))
716
717 (defcustom message-setup-hook nil
718   "Normal hook, run each time a new outgoing message is initialized.
719 The function `message-setup' runs this hook."
720   :group 'message-various
721   :type 'hook)
722
723 (defcustom message-cancel-hook nil
724   "Hook run when cancelling articles."
725   :group 'message-various
726   :type 'hook)
727
728 (defcustom message-signature-setup-hook nil
729   "Normal hook, run each time a new outgoing message is initialized.
730 It is run after the headers have been inserted and before
731 the signature is inserted."
732   :group 'message-various
733   :type 'hook)
734
735 (defcustom message-mode-hook nil
736   "Hook run in message mode buffers."
737   :group 'message-various
738   :type 'hook)
739
740 (defcustom message-header-hook nil
741   "Hook run in a message mode buffer narrowed to the headers."
742   :group 'message-various
743   :type 'hook)
744
745 (defcustom message-header-setup-hook nil
746   "Hook called narrowed to the headers when setting up a message buffer."
747   :group 'message-various
748   :type 'hook)
749
750 (defcustom message-minibuffer-local-map
751   (let ((map (make-sparse-keymap 'message-minibuffer-local-map)))
752     (set-keymap-parent map minibuffer-local-map)
753     map)
754   "Keymap for `message-read-from-minibuffer'.")
755
756 ;;;###autoload
757 (defcustom message-citation-line-function 'message-insert-citation-line
758   "*Function called to insert the \"Whomever writes:\" line.
759
760 Note that Gnus provides a feature where the reader can click on
761 `writes:' to hide the cited text.  If you change this line too much,
762 people who read your message will have to change their Gnus
763 configuration.  See the variable `gnus-cite-attribution-suffix'."
764   :type 'function
765   :group 'message-insertion)
766
767 ;;;###autoload
768 (defcustom message-yank-prefix "> "
769   "*Prefix inserted on the lines of yanked messages.
770 Fix `message-cite-prefix-regexp' if it is set to an abnormal value.
771 See also `message-yank-cited-prefix'."
772   :type 'string
773   :group 'message-insertion)
774
775 (defcustom message-yank-cited-prefix ">"
776   "*Prefix inserted on cited or empty lines of yanked messages.
777 Fix `message-cite-prefix-regexp' if it is set to an abnormal value.
778 See also `message-yank-prefix'."
779   :type 'string
780   :group 'message-insertion)
781
782 (defcustom message-indentation-spaces 3
783   "*Number of spaces to insert at the beginning of each cited line.
784 Used by `message-yank-original' via `message-yank-cite'."
785   :group 'message-insertion
786   :type 'integer)
787
788 ;;;###autoload
789 (defcustom message-cite-function 'message-cite-original
790   "*Function for citing an original message.
791 Predefined functions include `message-cite-original' and
792 `message-cite-original-without-signature'.
793 Note that `message-cite-original' uses `mail-citation-hook' if that is non-nil."
794   :type '(radio (function-item message-cite-original)
795                 (function-item message-cite-original-without-signature)
796                 (function-item sc-cite-original)
797                 (function :tag "Other"))
798   :group 'message-insertion)
799
800 ;;;###autoload
801 (defcustom message-indent-citation-function 'message-indent-citation
802   "*Function for modifying a citation just inserted in the mail buffer.
803 This can also be a list of functions.  Each function can find the
804 citation between (point) and (mark t).  And each function should leave
805 point and mark around the citation text as modified."
806   :type 'function
807   :group 'message-insertion)
808
809 ;;;###autoload
810 (defcustom message-signature t
811   "*String to be inserted at the end of the message buffer.
812 If t, the `message-signature-file' file will be inserted instead.
813 If a function, the result from the function will be used instead.
814 If a form, the result from the form will be used instead."
815   :type 'sexp
816   :group 'message-insertion)
817
818 ;;;###autoload
819 (defcustom message-signature-file "~/.signature"
820   "*Name of file containing the text inserted at end of message buffer.
821 Ignored if the named file doesn't exist.
822 If nil, don't insert a signature."
823   :type '(choice file (const :tags "None" nil))
824   :group 'message-insertion)
825
826 ;;;###autoload
827 (defcustom message-signature-insert-empty-line t
828   "*If non-nil, insert an empty line before the signature separator."
829   :type 'boolean
830   :group 'message-insertion)
831
832 (defcustom message-distribution-function nil
833   "*Function called to return a Distribution header."
834   :group 'message-news
835   :group 'message-headers
836   :type '(choice function (const nil)))
837
838 (defcustom message-expires 14
839   "Number of days before your article expires."
840   :group 'message-news
841   :group 'message-headers
842   :link '(custom-manual "(message)News Headers")
843   :type 'integer)
844
845 (defcustom message-user-path nil
846   "If nil, use the NNTP server name in the Path header.
847 If stringp, use this; if non-nil, use no host name (user name only)."
848   :group 'message-news
849   :group 'message-headers
850   :link '(custom-manual "(message)News Headers")
851   :type '(choice (const :tag "nntp" nil)
852                  (string :tag "name")
853                  (sexp :tag "none" :format "%t" t)))
854
855 (defvar message-reply-buffer nil)
856 (defvar message-reply-headers nil
857   "The headers of the current replied article.
858 It is a vector of the following headers:
859 \[number subject from date id references chars lines xref extra].")
860 (defvar message-newsreader nil)
861 (defvar message-mailer nil)
862 (defvar message-sent-message-via nil)
863 (defvar message-checksum nil)
864 (defvar message-send-actions nil
865   "A list of actions to be performed upon successful sending of a message.")
866 (defvar message-exit-actions nil
867   "A list of actions to be performed upon exiting after sending a message.")
868 (defvar message-kill-actions nil
869   "A list of actions to be performed before killing a message buffer.")
870 (defvar message-postpone-actions nil
871   "A list of actions to be performed after postponing a message.")
872
873 (define-widget 'message-header-lines 'text
874   "All header lines must be LFD terminated."
875   :format "%{%t%}:%n%v"
876   :valid-regexp "^\\'"
877   :error "All header lines must be newline terminated")
878
879 (defcustom message-default-headers ""
880   "*A string containing header lines to be inserted in outgoing messages.
881 It is inserted before you edit the message, so you can edit or delete
882 these lines."
883   :group 'message-headers
884   :type 'message-header-lines)
885
886 (defcustom message-default-mail-headers ""
887   "*A string of header lines to be inserted in outgoing mails."
888   :group 'message-headers
889   :group 'message-mail
890   :type 'message-header-lines)
891
892 (defcustom message-default-news-headers ""
893   "*A string of header lines to be inserted in outgoing news articles."
894   :group 'message-headers
895   :group 'message-news
896   :type 'message-header-lines)
897
898 ;; Note: could use /usr/ucb/mail instead of sendmail;
899 ;; options -t, and -v if not interactive.
900 (defcustom message-mailer-swallows-blank-line
901   (if (and (string-match "sparc-sun-sunos\\(\\'\\|[^5]\\)"
902                          system-configuration)
903            (file-readable-p "/etc/sendmail.cf")
904            (let ((buffer (get-buffer-create " *temp*")))
905              (unwind-protect
906                  (save-excursion
907                    (set-buffer buffer)
908                    (insert-file-contents "/etc/sendmail.cf")
909                    (goto-char (point-min))
910                    (let ((case-fold-search nil))
911                      (re-search-forward "^OR\\>" nil t)))
912                (kill-buffer buffer))))
913       ;; According to RFC822, "The field-name must be composed of printable
914       ;; ASCII characters (i. e., characters that have decimal values between
915       ;; 33 and 126, except colon)", i. e., any chars except ctl chars,
916       ;; space, or colon.
917       '(looking-at "[ \t]\\|[][!\"#$%&'()*+,-./0-9;<=>?@A-Z\\\\^_`a-z{|}~]+:"))
918   "*Set this non-nil if the system's mailer runs the header and body together.
919 \(This problem exists on Sunos 4 when sendmail is run in remote mode.)
920 The value should be an expression to test whether the problem will
921 actually occur."
922   :group 'message-sending
923   :type 'sexp)
924
925 ;;;###autoload
926 (define-mail-user-agent 'message-user-agent
927   'message-mail 'message-send-and-exit
928   'message-kill-buffer 'message-send-hook)
929
930 (defvar message-mh-deletable-headers '(Message-ID Date Lines Sender)
931   "If non-nil, delete the deletable headers before feeding to mh.")
932
933 (defvar message-send-method-alist
934   '((news message-news-p message-send-via-news)
935     (mail message-mail-p message-send-via-mail))
936   "Alist of ways to send outgoing messages.
937 Each element has the form
938
939   \(TYPE PREDICATE FUNCTION)
940
941 where TYPE is a symbol that names the method; PREDICATE is a function
942 called without any parameters to determine whether the message is
943 a message of type TYPE; and FUNCTION is a function to be called if
944 PREDICATE returns non-nil.  FUNCTION is called with one parameter --
945 the prefix.")
946
947 (defcustom message-mail-alias-type 'abbrev
948   "*What alias expansion type to use in Message buffers.
949 The default is `abbrev', which uses mailabbrev.  nil switches
950 mail aliases off."
951   :group 'message
952   :link '(custom-manual "(message)Mail Aliases")
953   :type '(choice (const :tag "Use Mailabbrev" abbrev)
954                  (const :tag "No expansion" nil)))
955
956 (defcustom message-auto-save-directory
957   (file-name-as-directory (nnheader-concat message-directory "drafts"))
958   "*Directory where Message auto-saves buffers if Gnus isn't running.
959 If nil, Message won't auto-save."
960   :group 'message-buffers
961   :type '(choice directory (const :tag "Don't auto-save" nil)))
962
963 (defcustom message-default-charset
964   (and (not (mm-multibyte-p)) 'iso-8859-1)
965   "Default charset used in non-MULE Emacsen.
966 If nil, you might be asked to input the charset."
967   :version "21.1"
968   :group 'message
969   :type 'symbol)
970
971 (defcustom message-dont-reply-to-names
972   (and (boundp 'rmail-dont-reply-to-names) rmail-dont-reply-to-names)
973   "*A regexp specifying addresses to prune when doing wide replies.
974 A value of nil means exclude your own user name only."
975   :version "21.1"
976   :group 'message
977   :type '(choice (const :tag "Yourself" nil)
978                  regexp))
979
980 (defvar message-shoot-gnksa-feet nil
981   "*A list of GNKSA feet you are allowed to shoot.
982 Gnus gives you all the opportunity you could possibly want for
983 shooting yourself in the foot.  Also, Gnus allows you to shoot the
984 feet of Good Net-Keeping Seal of Approval.  The following are foot
985 candidates:
986 `empty-article'     Allow you to post an empty article;
987 `quoted-text-only'  Allow you to post quoted text only;
988 `multiple-copies'   Allow you to post multiple copies;
989 `cancel-messages'   Allow you to cancel or supersede messages from
990                     your other email addresses.")
991
992 (defsubst message-gnksa-enable-p (feature)
993   (or (not (listp message-shoot-gnksa-feet))
994       (memq feature message-shoot-gnksa-feet)))
995
996 (defcustom message-hidden-headers nil
997   "Regexp of headers to be hidden when composing new messages.
998 This can also be a list of regexps to match headers.  Or a list
999 starting with `not' and followed by regexps.."
1000   :group 'message
1001   :type '(repeat regexp))
1002
1003 ;;; Internal variables.
1004 ;;; Well, not really internal.
1005
1006 (defvar message-mode-syntax-table
1007   (let ((table (copy-syntax-table text-mode-syntax-table)))
1008     (modify-syntax-entry ?% ". " table)
1009     (modify-syntax-entry ?> ". " table)
1010     (modify-syntax-entry ?< ". " table)
1011     table)
1012   "Syntax table used while in Message mode.")
1013
1014 (defface message-header-to-face
1015   '((((class color)
1016       (background dark))
1017      (:foreground "green2" :bold t))
1018     (((class color)
1019       (background light))
1020      (:foreground "MidnightBlue" :bold t))
1021     (t
1022      (:bold t :italic t)))
1023   "Face used for displaying From headers."
1024   :group 'message-faces)
1025
1026 (defface message-header-cc-face
1027   '((((class color)
1028       (background dark))
1029      (:foreground "green4" :bold t))
1030     (((class color)
1031       (background light))
1032      (:foreground "MidnightBlue"))
1033     (t
1034      (:bold t)))
1035   "Face used for displaying Cc headers."
1036   :group 'message-faces)
1037
1038 (defface message-header-subject-face
1039   '((((class color)
1040       (background dark))
1041      (:foreground "green3"))
1042     (((class color)
1043       (background light))
1044      (:foreground "navy blue" :bold t))
1045     (t
1046      (:bold t)))
1047   "Face used for displaying subject headers."
1048   :group 'message-faces)
1049
1050 (defface message-header-newsgroups-face
1051   '((((class color)
1052       (background dark))
1053      (:foreground "yellow" :bold t :italic t))
1054     (((class color)
1055       (background light))
1056      (:foreground "blue4" :bold t :italic t))
1057     (t
1058      (:bold t :italic t)))
1059   "Face used for displaying newsgroups headers."
1060   :group 'message-faces)
1061
1062 (defface message-header-other-face
1063   '((((class color)
1064       (background dark))
1065      (:foreground "#b00000"))
1066     (((class color)
1067       (background light))
1068      (:foreground "steel blue"))
1069     (t
1070      (:bold t :italic t)))
1071   "Face used for displaying newsgroups headers."
1072   :group 'message-faces)
1073
1074 (defface message-header-name-face
1075   '((((class color)
1076       (background dark))
1077      (:foreground "DarkGreen"))
1078     (((class color)
1079       (background light))
1080      (:foreground "cornflower blue"))
1081     (t
1082      (:bold t)))
1083   "Face used for displaying header names."
1084   :group 'message-faces)
1085
1086 (defface message-header-xheader-face
1087   '((((class color)
1088       (background dark))
1089      (:foreground "blue"))
1090     (((class color)
1091       (background light))
1092      (:foreground "blue"))
1093     (t
1094      (:bold t)))
1095   "Face used for displaying X-Header headers."
1096   :group 'message-faces)
1097
1098 (defface message-separator-face
1099   '((((class color)
1100       (background dark))
1101      (:foreground "blue3"))
1102     (((class color)
1103       (background light))
1104      (:foreground "brown"))
1105     (t
1106      (:bold t)))
1107   "Face used for displaying the separator."
1108   :group 'message-faces)
1109
1110 (defface message-cited-text-face
1111   '((((class color)
1112       (background dark))
1113      (:foreground "red"))
1114     (((class color)
1115       (background light))
1116      (:foreground "red"))
1117     (t
1118      (:bold t)))
1119   "Face used for displaying cited text names."
1120   :group 'message-faces)
1121
1122 (defface message-mml-face
1123   '((((class color)
1124       (background dark))
1125      (:foreground "ForestGreen"))
1126     (((class color)
1127       (background light))
1128      (:foreground "ForestGreen"))
1129     (t
1130      (:bold t)))
1131   "Face used for displaying MML."
1132   :group 'message-faces)
1133
1134 (defun message-font-lock-make-header-matcher (regexp)
1135   (let ((form
1136          `(lambda (limit)
1137             (let ((start (point)))
1138               (save-restriction
1139                 (widen)
1140                 (goto-char (point-min))
1141                 (if (re-search-forward
1142                      (concat "^" (regexp-quote mail-header-separator) "$")
1143                      nil t)
1144                     (setq limit (min limit (match-beginning 0))))
1145                 (goto-char start))
1146               (and (< start limit)
1147                    (re-search-forward ,regexp limit t))))))
1148     (if (featurep 'bytecomp)
1149         (byte-compile form)
1150       form)))
1151
1152 (defvar message-font-lock-keywords
1153   (let ((content "[ \t]*\\(.+\\(\n[ \t].*\\)*\\)\n?"))
1154     `((,(message-font-lock-make-header-matcher
1155          (concat "^\\([Tt]o:\\)" content))
1156        (1 'message-header-name-face)
1157        (2 'message-header-to-face nil t))
1158       (,(message-font-lock-make-header-matcher
1159          (concat "^\\(^[GBF]?[Cc][Cc]:\\|^[Rr]eply-[Tt]o:\\)" content))
1160        (1 'message-header-name-face)
1161        (2 'message-header-cc-face nil t))
1162       (,(message-font-lock-make-header-matcher
1163          (concat "^\\([Ss]ubject:\\)" content))
1164        (1 'message-header-name-face)
1165        (2 'message-header-subject-face nil t))
1166       (,(message-font-lock-make-header-matcher
1167          (concat "^\\([Nn]ewsgroups:\\|Followup-[Tt]o:\\)" content))
1168        (1 'message-header-name-face)
1169        (2 'message-header-newsgroups-face nil t))
1170       (,(message-font-lock-make-header-matcher
1171          (concat "^\\([A-Z][^: \n\t]+:\\)" content))
1172        (1 'message-header-name-face)
1173        (2 'message-header-other-face nil t))
1174       (,(message-font-lock-make-header-matcher
1175          (concat "^\\(X-[A-Za-z0-9-]+:\\|In-Reply-To:\\)" content))
1176        (1 'message-header-name-face)
1177        (2 'message-header-name-face))
1178       ,@(if (and mail-header-separator
1179                  (not (equal mail-header-separator "")))
1180             `((,(concat "^\\(" (regexp-quote mail-header-separator) "\\)$")
1181                1 'message-separator-face))
1182           nil)
1183       ((lambda (limit)
1184          (re-search-forward (concat "^\\("
1185                                     message-cite-prefix-regexp
1186                                     "\\).*")
1187                             limit t))
1188        (0 'message-cited-text-face))
1189       ("<#/?\\(multipart\\|part\\|external\\|mml\\|secure\\)[^>]*>"
1190        (0 'message-mml-face))))
1191   "Additional expressions to highlight in Message mode.")
1192
1193
1194 ;; XEmacs does it like this.  For Emacs, we have to set the
1195 ;; `font-lock-defaults' buffer-local variable.
1196 (put 'message-mode 'font-lock-defaults '(message-font-lock-keywords t))
1197
1198 (defvar message-face-alist
1199   '((bold . bold-region)
1200     (underline . underline-region)
1201     (default . (lambda (b e)
1202                  (unbold-region b e)
1203                  (ununderline-region b e))))
1204   "Alist of mail and news faces for facemenu.
1205 The cdr of each entry is a function for applying the face to a region.")
1206
1207 (defcustom message-send-hook nil
1208   "Hook run before sending messages.
1209 This hook is run quite early when sending."
1210   :group 'message-various
1211   :options '(ispell-message)
1212   :type 'hook)
1213
1214 (defcustom message-send-mail-hook nil
1215   "Hook run before sending mail messages.
1216 This hook is run very late -- just before the message is sent as
1217 mail."
1218   :group 'message-various
1219   :type 'hook)
1220
1221 (defcustom message-send-news-hook nil
1222   "Hook run before sending news messages.
1223 This hook is run very late -- just before the message is sent as
1224 news."
1225   :group 'message-various
1226   :type 'hook)
1227
1228 (defcustom message-sent-hook nil
1229   "Hook run after sending messages."
1230   :group 'message-various
1231   :type 'hook)
1232
1233 (defvar message-send-coding-system 'binary
1234   "Coding system to encode outgoing mail.")
1235
1236 (defvar message-draft-coding-system
1237   mm-auto-save-coding-system
1238   "*Coding system to compose mail.
1239 If you'd like to make it possible to share draft files between XEmacs
1240 and Emacs, you may use `iso-2022-7bit' for this value at your own risk.
1241 Note that the coding-system `iso-2022-7bit' isn't suitable to all data.")
1242
1243 (defcustom message-send-mail-partially-limit 1000000
1244   "The limitation of messages sent as message/partial.
1245 The lower bound of message size in characters, beyond which the message
1246 should be sent in several parts.  If it is nil, the size is unlimited."
1247   :version "21.1"
1248   :group 'message-buffers
1249   :type '(choice (const :tag "unlimited" nil)
1250                  (integer 1000000)))
1251
1252 (defcustom message-alternative-emails nil
1253   "A regexp to match the alternative email addresses.
1254 The first matched address (not primary one) is used in the From field."
1255   :group 'message-headers
1256   :type '(choice (const :tag "Always use primary" nil)
1257                  regexp))
1258
1259 (defcustom message-hierarchical-addresses nil
1260   "A list of hierarchical mail address definitions.
1261
1262 Inside each entry, the first address is the \"top\" address, and
1263 subsequent addresses are subaddresses; this is used to indicate that
1264 mail sent to the first address will automatically be delivered to the
1265 subaddresses.  So if the first address appears in the recipient list
1266 for a message, the subaddresses will be removed (if present) before
1267 the mail is sent.  All addresses in this structure should be
1268 downcased."
1269   :group 'message-headers
1270   :type '(repeat (repeat string)))
1271
1272 (defcustom message-mail-user-agent nil
1273   "Like `mail-user-agent'.
1274 Except if it is nil, use Gnus native MUA; if it is t, use
1275 `mail-user-agent'."
1276   :type '(radio (const :tag "Gnus native"
1277                        :format "%t\n"
1278                        nil)
1279                 (const :tag "`mail-user-agent'"
1280                        :format "%t\n"
1281                        t)
1282                 (function-item :tag "Default Emacs mail"
1283                                :format "%t\n"
1284                                sendmail-user-agent)
1285                 (function-item :tag "Emacs interface to MH"
1286                                :format "%t\n"
1287                                mh-e-user-agent)
1288                 (function :tag "Other"))
1289   :version "21.1"
1290   :group 'message)
1291
1292 (defcustom message-wide-reply-confirm-recipients nil
1293   "Whether to confirm a wide reply to multiple email recipients.
1294 If this variable is nil, don't ask whether to reply to all recipients.
1295 If this variable is non-nil, pose the question \"Reply to all
1296 recipients?\" before a wide reply to multiple recipients.  If the user
1297 answers yes, reply to all recipients as usual.  If the user answers
1298 no, only reply back to the author."
1299   :version "21.3"
1300   :group 'message-headers
1301   :type 'boolean)
1302
1303 (defcustom message-user-fqdn nil
1304   "*Domain part of Messsage-Ids."
1305   :group 'message-headers
1306   :link '(custom-manual "(message)News Headers")
1307   :type 'string)
1308
1309 (defcustom message-use-idna (and (condition-case nil (require 'idna)
1310                                    (file-error))
1311                                  (mm-coding-system-p 'utf-8)
1312                                  (executable-find idna-program)
1313                                  'ask)
1314   "Whether to encode non-ASCII in domain names into ASCII according to IDNA."
1315   :group 'message-headers
1316   :type '(choice (const :tag "Ask" ask)
1317                  (const :tag "Never" nil)
1318                  (const :tag "Always" t)))
1319
1320 ;;; Internal variables.
1321
1322 (defvar message-sending-message "Sending...")
1323 (defvar message-buffer-list nil)
1324 (defvar message-this-is-news nil)
1325 (defvar message-this-is-mail nil)
1326 (defvar message-draft-article nil)
1327 (defvar message-mime-part nil)
1328 (defvar message-posting-charset nil)
1329
1330 ;; Byte-compiler warning
1331 (eval-when-compile
1332   (defvar gnus-active-hashtb)
1333   (defvar gnus-read-active-file))
1334
1335 ;;; Regexp matching the delimiter of messages in UNIX mail format
1336 ;;; (UNIX From lines), minus the initial ^.  It should be a copy
1337 ;;; of rmail.el's rmail-unix-mail-delimiter.
1338 (defvar message-unix-mail-delimiter
1339   (let ((time-zone-regexp
1340          (concat "\\([A-Z]?[A-Z]?[A-Z][A-Z]\\( DST\\)?"
1341                  "\\|[-+]?[0-9][0-9][0-9][0-9]"
1342                  "\\|"
1343                  "\\) *")))
1344     (concat
1345      "From "
1346
1347      ;; Many things can happen to an RFC 822 mailbox before it is put into
1348      ;; a `From' line.  The leading phrase can be stripped, e.g.
1349      ;; `Joe <@w.x:joe@y.z>' -> `<@w.x:joe@y.z>'.  The <> can be stripped, e.g.
1350      ;; `<@x.y:joe@y.z>' -> `@x.y:joe@y.z'.  Everything starting with a CRLF
1351      ;; can be removed, e.g.
1352      ;;         From: joe@y.z (Joe      K
1353      ;;                 User)
1354      ;; can yield `From joe@y.z (Joe    K Fri Mar 22 08:11:15 1996', and
1355      ;;         From: Joe User
1356      ;;                 <joe@y.z>
1357      ;; can yield `From Joe User Fri Mar 22 08:11:15 1996'.
1358      ;; The mailbox can be removed or be replaced by white space, e.g.
1359      ;;         From: "Joe User"{space}{tab}
1360      ;;                 <joe@y.z>
1361      ;; can yield `From {space}{tab} Fri Mar 22 08:11:15 1996',
1362      ;; where {space} and {tab} represent the Ascii space and tab characters.
1363      ;; We want to match the results of any of these manglings.
1364      ;; The following regexp rejects names whose first characters are
1365      ;; obviously bogus, but after that anything goes.
1366      "\\([^\0-\b\n-\r\^?].*\\)?"
1367
1368      ;; The time the message was sent.
1369      "\\([^\0-\r \^?]+\\) +"            ; day of the week
1370      "\\([^\0-\r \^?]+\\) +"            ; month
1371      "\\([0-3]?[0-9]\\) +"              ; day of month
1372      "\\([0-2][0-9]:[0-5][0-9]\\(:[0-6][0-9]\\)?\\) *" ; time of day
1373
1374      ;; Perhaps a time zone, specified by an abbreviation, or by a
1375      ;; numeric offset.
1376      time-zone-regexp
1377
1378      ;; The year.
1379      " \\([0-9][0-9]+\\) *"
1380
1381      ;; On some systems the time zone can appear after the year, too.
1382      time-zone-regexp
1383
1384      ;; Old uucp cruft.
1385      "\\(remote from .*\\)?"
1386
1387      "\n"))
1388   "Regexp matching the delimiter of messages in UNIX mail format.")
1389
1390 (defvar message-unsent-separator
1391   (concat "^ *---+ +Unsent message follows +---+ *$\\|"
1392           "^ *---+ +Returned message +---+ *$\\|"
1393           "^Start of returned message$\\|"
1394           "^ *---+ +Original message +---+ *$\\|"
1395           "^ *--+ +begin message +--+ *$\\|"
1396           "^ *---+ +Original message follows +---+ *$\\|"
1397           "^ *---+ +Undelivered message follows +---+ *$\\|"
1398           "^|? *---+ +Message text follows: +---+ *|?$")
1399   "A regexp that matches the separator before the text of a failed message.")
1400
1401 (defvar message-header-format-alist
1402   `((Newsgroups)
1403     (To . message-fill-address)
1404     (Cc . message-fill-address)
1405     (Subject)
1406     (In-Reply-To)
1407     (Fcc)
1408     (Bcc)
1409     (Date)
1410     (Organization)
1411     (Distribution)
1412     (Lines)
1413     (Expires)
1414     (Message-ID)
1415     (References . message-shorten-references)
1416     (User-Agent))
1417   "Alist used for formatting headers.")
1418
1419 (defvar message-options nil
1420   "Some saved answers when sending message.")
1421
1422 (defvar message-send-mail-real-function nil
1423   "Internal send mail function.")
1424
1425 (defvar message-bogus-system-names "^localhost\\."
1426   "The regexp of bogus system names.")
1427
1428 (defcustom message-valid-fqdn-regexp
1429   (concat "[a-z0-9][-.a-z0-9]+\\." ;; [hostname.subdomain.]domain.
1430           ;; valid TLDs:
1431           "\\([a-z][a-z]" ;; two letter country TDLs
1432           "\\|biz\\|com\\|edu\\|gov\\|int\\|mil\\|net\\|org"
1433           "\\|aero\\|coop\\|info\\|name\\|museum"
1434           "\\|arpa\\|pro\\|uucp\\|bitnet\\|bofh" ;; old style?
1435           "\\)")
1436   "Regular expression that matches a valid FQDN."
1437   ;; see also: gnus-button-valid-fqdn-regexp
1438   :group 'message-headers
1439   :type 'regexp)
1440
1441 (eval-and-compile
1442   (autoload 'message-setup-toolbar "messagexmas")
1443   (autoload 'mh-new-draft-name "mh-comp")
1444   (autoload 'mh-send-letter "mh-comp")
1445   (autoload 'gnus-point-at-eol "gnus-util")
1446   (autoload 'gnus-point-at-bol "gnus-util")
1447   (autoload 'gnus-output-to-rmail "gnus-util")
1448   (autoload 'gnus-output-to-mail "gnus-util")
1449   (autoload 'nndraft-request-associate-buffer "nndraft")
1450   (autoload 'nndraft-request-expire-articles "nndraft")
1451   (autoload 'gnus-open-server "gnus-int")
1452   (autoload 'gnus-request-post "gnus-int")
1453   (autoload 'gnus-alive-p "gnus-util")
1454   (autoload 'gnus-server-string "gnus")
1455   (autoload 'gnus-group-name-charset "gnus-group")
1456   (autoload 'gnus-group-name-decode "gnus-group")
1457   (autoload 'gnus-groups-from-server "gnus")
1458   (autoload 'rmail-output "rmailout")
1459   (autoload 'gnus-delay-article "gnus-delay")
1460   (autoload 'gnus-make-local-hook "gnus-util"))
1461
1462 \f
1463
1464 ;;;
1465 ;;; Utility functions.
1466 ;;;
1467
1468 (defmacro message-y-or-n-p (question show &rest text)
1469   "Ask QUESTION, displaying remaining args in a temporary buffer if SHOW."
1470   `(message-talkative-question 'y-or-n-p ,question ,show ,@text))
1471
1472 (defmacro message-delete-line (&optional n)
1473   "Delete the current line (and the next N lines)."
1474   `(delete-region (progn (beginning-of-line) (point))
1475                   (progn (forward-line ,(or n 1)) (point))))
1476
1477 (defun message-mark-active-p ()
1478   "Non-nil means the mark and region are currently active in this buffer."
1479   mark-active)
1480
1481 (defun message-unquote-tokens (elems)
1482   "Remove double quotes (\") from strings in list ELEMS."
1483   (mapcar (lambda (item)
1484             (while (string-match "^\\(.*\\)\"\\(.*\\)$" item)
1485               (setq item (concat (match-string 1 item)
1486                                  (match-string 2 item))))
1487             item)
1488           elems))
1489
1490 (defun message-tokenize-header (header &optional separator)
1491   "Split HEADER into a list of header elements.
1492 SEPARATOR is a string of characters to be used as separators.  \",\"
1493 is used by default."
1494   (if (not header)
1495       nil
1496     (let ((regexp (format "[%s]+" (or separator ",")))
1497           (beg 1)
1498           (first t)
1499           quoted elems paren)
1500       (with-temp-buffer
1501         (mm-enable-multibyte)
1502         (insert header)
1503         (goto-char (point-min))
1504         (while (not (eobp))
1505           (if first
1506               (setq first nil)
1507             (forward-char 1))
1508           (cond ((and (> (point) beg)
1509                       (or (eobp)
1510                           (and (looking-at regexp)
1511                                (not quoted)
1512                                (not paren))))
1513                  (push (buffer-substring beg (point)) elems)
1514                  (setq beg (match-end 0)))
1515                 ((eq (char-after) ?\")
1516                  (setq quoted (not quoted)))
1517                 ((and (eq (char-after) ?\()
1518                       (not quoted))
1519                  (setq paren t))
1520                 ((and (eq (char-after) ?\))
1521                       (not quoted))
1522                  (setq paren nil))))
1523         (nreverse elems)))))
1524
1525 (defun message-mail-file-mbox-p (file)
1526   "Say whether FILE looks like a Unix mbox file."
1527   (when (and (file-exists-p file)
1528              (file-readable-p file)
1529              (file-regular-p file))
1530     (with-temp-buffer
1531       (nnheader-insert-file-contents file)
1532       (goto-char (point-min))
1533       (looking-at message-unix-mail-delimiter))))
1534
1535 (defun message-fetch-field (header &optional not-all)
1536   "The same as `mail-fetch-field', only remove all newlines."
1537   (let* ((inhibit-point-motion-hooks t)
1538          (case-fold-search t)
1539          (value (mail-fetch-field header nil (not not-all))))
1540     (when value
1541       (while (string-match "\n[\t ]+" value)
1542         (setq value (replace-match " " t t value)))
1543       (set-text-properties 0 (length value) nil value)
1544       value)))
1545
1546 (defun message-narrow-to-field ()
1547   "Narrow the buffer to the header on the current line."
1548   (beginning-of-line)
1549   (narrow-to-region
1550    (point)
1551    (progn
1552      (forward-line 1)
1553      (if (re-search-forward "^[^ \n\t]" nil t)
1554          (progn
1555            (beginning-of-line)
1556            (point))
1557        (point-max))))
1558   (goto-char (point-min)))
1559
1560 (defun message-add-header (&rest headers)
1561   "Add the HEADERS to the message header, skipping those already present."
1562   (while headers
1563     (let (hclean)
1564       (unless (string-match "^\\([^:]+\\):[ \t]*[^ \t]" (car headers))
1565         (error "Invalid header `%s'" (car headers)))
1566       (setq hclean (match-string 1 (car headers)))
1567       (save-restriction
1568         (message-narrow-to-headers)
1569         (unless (re-search-forward (concat "^" (regexp-quote hclean) ":") nil t)
1570           (goto-char (point-max))
1571           (if (string-match "\n$" (car headers))
1572               (insert (car headers))
1573             (insert (car headers) ?\n)))))
1574     (setq headers (cdr headers))))
1575
1576 (defmacro message-with-reply-buffer (&rest forms)
1577   "Evaluate FORMS in the reply buffer, if it exists."
1578   `(when (and message-reply-buffer
1579               (buffer-name message-reply-buffer))
1580      (save-excursion
1581        (set-buffer message-reply-buffer)
1582        ,@forms)))
1583
1584 (put 'message-with-reply-buffer 'lisp-indent-function 0)
1585 (put 'message-with-reply-buffer 'edebug-form-spec '(body))
1586
1587 (defun message-fetch-reply-field (header)
1588   "Fetch field HEADER from the message we're replying to."
1589   (message-with-reply-buffer
1590     (save-restriction
1591       (mail-narrow-to-head)
1592       (message-fetch-field header))))
1593
1594 (defun message-functionp (form)
1595   "Return non-nil if FORM is funcallable."
1596   (or (and (symbolp form) (fboundp form))
1597       (and (listp form) (eq (car form) 'lambda))
1598       (byte-code-function-p form)))
1599
1600 (defun message-strip-list-identifiers (subject)
1601   "Remove list identifiers in `gnus-list-identifiers' from string SUBJECT."
1602   (require 'gnus-sum)                   ; for gnus-list-identifiers
1603   (let ((regexp (if (stringp gnus-list-identifiers)
1604                     gnus-list-identifiers
1605                   (mapconcat 'identity gnus-list-identifiers " *\\|"))))
1606     (if (string-match (concat "\\(\\(\\(Re: +\\)?\\(" regexp
1607                               " *\\)\\)+\\(Re: +\\)?\\)") subject)
1608         (concat (substring subject 0 (match-beginning 1))
1609                 (or (match-string 3 subject)
1610                     (match-string 5 subject))
1611                 (substring subject
1612                            (match-end 1)))
1613       subject)))
1614
1615 (defun message-strip-subject-re (subject)
1616   "Remove \"Re:\" from subject lines in string SUBJECT."
1617   (if (string-match message-subject-re-regexp subject)
1618       (substring subject (match-end 0))
1619     subject))
1620
1621 ;;; Start of functions adopted from `message-utils.el'.
1622
1623 (defun message-strip-subject-trailing-was (subject)
1624   "Remove trailing \"(Was: <old subject>)\" from SUBJECT lines.
1625 Leading \"Re: \" is not stripped by this function.  Use the function
1626 `message-strip-subject-re' for this."
1627   (let* ((query message-subject-trailing-was-query)
1628          (new) (found))
1629     (setq found
1630           (string-match
1631            (if (eq query 'ask)
1632                message-subject-trailing-was-ask-regexp
1633              message-subject-trailing-was-regexp)
1634            subject))
1635     (if found
1636         (setq new (substring subject 0 (match-beginning 0))))
1637     (if (or (not found) (eq query nil))
1638         subject
1639       (if (eq query 'ask)
1640           (if (message-y-or-n-p
1641                "Strip `(was: <old subject>)' in subject? " t
1642                (concat
1643                 "Strip `(was: <old subject>)' in subject "
1644                 "and use the new one instead?\n\n"
1645                 "Current subject is:   \""
1646                 subject "\"\n\n"
1647                 "New subject would be: \""
1648                 new "\"\n\n"
1649                 "See the variable `message-subject-trailing-was-query' "
1650                 "to get rid of this query."
1651                 ))
1652               new subject)
1653         new))))
1654
1655 ;;; Suggested by Jonas Steverud  @  www.dtek.chalmers.se/~d4jonas/
1656
1657 ;;;###autoload
1658 (defun message-change-subject (new-subject)
1659   "Ask for NEW-SUBJECT header, append (was: <Old Subject>)."
1660   (interactive
1661    (list
1662     (read-from-minibuffer "New subject: ")))
1663   (cond ((and (not (or (null new-subject) ; new subject not empty
1664                        (zerop (string-width new-subject))
1665                        (string-match "^[ \t]*$" new-subject))))
1666          (save-excursion
1667            (let ((old-subject (message-fetch-field "Subject")))
1668              (cond ((not old-subject)
1669                     (error "No current subject"))
1670                    ((not (string-match
1671                           (concat "^[ \t]*"
1672                                   (regexp-quote new-subject)
1673                                   " \t]*$")
1674                           old-subject))  ; yes, it really is a new subject
1675                     ;; delete eventual Re: prefix
1676                     (setq old-subject
1677                           (message-strip-subject-re old-subject))
1678                     (message-goto-subject)
1679                     (message-delete-line)
1680                     (insert (concat "Subject: "
1681                                     new-subject
1682                                     " (was: "
1683                                     old-subject ")\n")))))))))
1684
1685 ;;;###autoload
1686 (defun message-mark-inserted-region (beg end)
1687   "Mark some region in the current article with enclosing tags.
1688 See `message-mark-insert-begin' and `message-mark-insert-end'."
1689   (interactive "r")
1690   (save-excursion
1691     ; add to the end of the region first, otherwise end would be invalid
1692     (goto-char end)
1693     (insert message-mark-insert-end)
1694     (goto-char beg)
1695     (insert message-mark-insert-begin)))
1696
1697 ;;;###autoload
1698 (defun message-mark-insert-file (file)
1699   "Insert FILE at point, marking it with enclosing tags.
1700 See `message-mark-insert-begin' and `message-mark-insert-end'."
1701   (interactive "fFile to insert: ")
1702     ;; reverse insertion to get correct result.
1703   (let ((p (point)))
1704     (insert message-mark-insert-end)
1705     (goto-char p)
1706     (insert-file-contents file)
1707     (goto-char p)
1708     (insert message-mark-insert-begin)))
1709
1710 ;;;###autoload
1711 (defun message-add-archive-header ()
1712   "Insert \"X-No-Archive: Yes\" in the header and a note in the body.
1713 The note can be customized using `message-archive-note'.  When called with a
1714 prefix argument, ask for a text to insert.  If you don't want the note in the
1715 body, set  `message-archive-note' to nil."
1716   (interactive)
1717   (if current-prefix-arg
1718       (setq message-archive-note
1719             (read-from-minibuffer "Reason for No-Archive: "
1720                                   (cons message-archive-note 0))))
1721     (save-excursion
1722       (if (message-goto-signature)
1723           (re-search-backward message-signature-separator))
1724       (when message-archive-note
1725         (insert message-archive-note)
1726         (newline))
1727       (message-add-header message-archive-header)
1728       (message-sort-headers)))
1729
1730 ;;;###autoload
1731 (defun message-cross-post-followup-to-header (target-group)
1732   "Mangles FollowUp-To and Newsgroups header to point to TARGET-GROUP.
1733 With prefix-argument just set Follow-Up, don't cross-post."
1734   (interactive
1735    (list ; Completion based on Gnus
1736     (completing-read "Followup To: "
1737                      (if (boundp 'gnus-newsrc-alist)
1738                          gnus-newsrc-alist)
1739                      nil nil '("poster" . 0)
1740                      (if (boundp 'gnus-group-history)
1741                          'gnus-group-history))))
1742   (message-remove-header "Follow[Uu]p-[Tt]o" t)
1743   (message-goto-newsgroups)
1744   (beginning-of-line)
1745   ;; if we already did a crosspost before, kill old target
1746   (if (and message-cross-post-old-target
1747            (re-search-forward
1748             (regexp-quote (concat "," message-cross-post-old-target))
1749             nil t))
1750       (replace-match ""))
1751   ;; unless (followup is to poster or user explicitly asked not
1752   ;; to cross-post, or target-group is already in Newsgroups)
1753   ;; add target-group to Newsgroups line.
1754   (cond ((and (or
1755                ;; def: cross-post, req:no
1756                (and message-cross-post-default (not current-prefix-arg))
1757                ;; def: no-cross-post, req:yes
1758                (and (not message-cross-post-default) current-prefix-arg))
1759               (not (string-match "poster" target-group))
1760               (not (string-match (regexp-quote target-group)
1761                                  (message-fetch-field "Newsgroups"))))
1762          (end-of-line)
1763          (insert (concat "," target-group))))
1764   (end-of-line) ; ensure Followup: comes after Newsgroups:
1765   ;; unless new followup would be identical to Newsgroups line
1766   ;; make a new Followup-To line
1767   (if (not (string-match (concat "^[ \t]*"
1768                                  target-group
1769                                  "[ \t]*$")
1770                          (message-fetch-field "Newsgroups")))
1771       (insert (concat "\nFollowup-To: " target-group)))
1772   (setq message-cross-post-old-target target-group))
1773
1774 ;;;###autoload
1775 (defun message-cross-post-insert-note (target-group cross-post in-old
1776                                                     old-groups)
1777   "Insert a in message body note about a set Followup or Crosspost.
1778 If there have been previous notes, delete them.  TARGET-GROUP specifies the
1779 group to Followup-To.  When CROSS-POST is t, insert note about
1780 crossposting.  IN-OLD specifies whether TARGET-GROUP is a member of
1781 OLD-GROUPS.  OLD-GROUPS lists the old-groups the posting would have
1782 been made to before the user asked for a Crosspost."
1783   ;; start scanning body for previous uses
1784   (message-goto-signature)
1785   (let ((head (re-search-backward
1786                (concat "^" mail-header-separator)
1787                nil t))) ; just search in body
1788     (message-goto-signature)
1789     (while (re-search-backward
1790             (concat "^" (regexp-quote message-cross-post-note) ".*")
1791             head t)
1792       (message-delete-line))
1793     (message-goto-signature)
1794     (while (re-search-backward
1795             (concat "^" (regexp-quote message-followup-to-note) ".*")
1796             head t)
1797       (message-delete-line))
1798     ;; insert new note
1799     (if (message-goto-signature)
1800         (re-search-backward message-signature-separator))
1801     (if (or in-old
1802             (not cross-post)
1803             (string-match "^[ \t]*poster[ \t]*$" target-group))
1804         (insert (concat message-followup-to-note target-group "\n"))
1805       (insert (concat message-cross-post-note target-group "\n")))))
1806
1807 ;;;###autoload
1808 (defun message-cross-post-followup-to (target-group)
1809   "Crossposts message and set Followup-To to TARGET-GROUP.
1810 With prefix-argument just set Follow-Up, don't cross-post."
1811   (interactive
1812    (list ; Completion based on Gnus
1813     (completing-read "Followup To: "
1814                      (if (boundp 'gnus-newsrc-alist)
1815                          gnus-newsrc-alist)
1816                      nil nil '("poster" . 0)
1817                      (if (boundp 'gnus-group-history)
1818                          'gnus-group-history))))
1819   (cond ((not (or (null target-group) ; new subject not empty
1820                   (zerop (string-width target-group))
1821                   (string-match "^[ \t]*$" target-group)))
1822          (save-excursion
1823            (let* ((old-groups (message-fetch-field "Newsgroups"))
1824                   (in-old (string-match
1825                            (regexp-quote target-group)
1826                            (or old-groups ""))))
1827              ;; check whether target exactly matches old Newsgroups
1828              (cond ((not old-groups)
1829                     (error "No current newsgroup"))
1830                    ((or (not in-old)
1831                         (not (string-match
1832                               (concat "^[ \t]*"
1833                                       (regexp-quote target-group)
1834                                       "[ \t]*$")
1835                               old-groups)))
1836                     ;; yes, Newsgroups line must change
1837                     (message-cross-post-followup-to-header target-group)
1838                     ;; insert note whether we do cross-post or followup-to
1839                     (funcall message-cross-post-note-function
1840                              target-group
1841                              (if (or (and message-cross-post-default
1842                                           (not current-prefix-arg))
1843                                      (and (not message-cross-post-default)
1844                                           current-prefix-arg)) t)
1845                              in-old old-groups))))))))
1846
1847 ;;; Reduce To: to Cc: or Bcc: header
1848
1849 ;;;###autoload
1850 (defun message-reduce-to-to-cc ()
1851  "Replace contents of To: header with contents of Cc: or Bcc: header."
1852  (interactive)
1853  (let ((cc-content (message-fetch-field "cc"))
1854        (bcc nil))
1855    (if (and (not cc-content)
1856             (setq cc-content (message-fetch-field "bcc")))
1857        (setq bcc t))
1858    (cond (cc-content
1859           (save-excursion
1860             (message-goto-to)
1861             (message-delete-line)
1862             (insert (concat "To: " cc-content "\n"))
1863             (message-remove-header (if bcc
1864                                        "bcc"
1865                                      "cc")))))))
1866
1867 ;;; End of functions adopted from `message-utils.el'.
1868
1869 (defun message-remove-header (header &optional is-regexp first reverse)
1870   "Remove HEADER in the narrowed buffer.
1871 If IS-REGEXP, HEADER is a regular expression.
1872 If FIRST, only remove the first instance of the header.
1873 Return the number of headers removed."
1874   (goto-char (point-min))
1875   (let ((regexp (if is-regexp header (concat "^" (regexp-quote header) ":")))
1876         (number 0)
1877         (case-fold-search t)
1878         last)
1879     (while (and (not (eobp))
1880                 (not last))
1881       (if (if reverse
1882               (not (looking-at regexp))
1883             (looking-at regexp))
1884           (progn
1885             (incf number)
1886             (when first
1887               (setq last t))
1888             (delete-region
1889              (point)
1890              ;; There might be a continuation header, so we have to search
1891              ;; until we find a new non-continuation line.
1892              (progn
1893                (forward-line 1)
1894                (if (re-search-forward "^[^ \t]" nil t)
1895                    (goto-char (match-beginning 0))
1896                  (point-max)))))
1897         (forward-line 1)
1898         (if (re-search-forward "^[^ \t]" nil t)
1899             (goto-char (match-beginning 0))
1900           (goto-char (point-max)))))
1901     number))
1902
1903 (defun message-remove-first-header (header)
1904   "Remove the first instance of HEADER if there is more than one."
1905   (let ((count 0)
1906         (regexp (concat "^" (regexp-quote header) ":")))
1907     (save-excursion
1908       (goto-char (point-min))
1909       (while (re-search-forward regexp nil t)
1910         (incf count)))
1911     (while (> count 1)
1912       (message-remove-header header nil t)
1913       (decf count))))
1914
1915 (defun message-narrow-to-headers ()
1916   "Narrow the buffer to the head of the message."
1917   (widen)
1918   (narrow-to-region
1919    (goto-char (point-min))
1920    (if (re-search-forward
1921         (concat "^" (regexp-quote mail-header-separator) "\n") nil t)
1922        (match-beginning 0)
1923      (point-max)))
1924   (goto-char (point-min)))
1925
1926 (defun message-narrow-to-head-1 ()
1927   "Like `message-narrow-to-head'.  Don't widen."
1928   (narrow-to-region
1929    (goto-char (point-min))
1930    (if (search-forward "\n\n" nil 1)
1931        (1- (point))
1932      (point-max)))
1933   (goto-char (point-min)))
1934
1935 (defun message-narrow-to-head ()
1936   "Narrow the buffer to the head of the message.
1937 Point is left at the beginning of the narrowed-to region."
1938   (widen)
1939   (message-narrow-to-head-1))
1940
1941 (defun message-narrow-to-headers-or-head ()
1942   "Narrow the buffer to the head of the message."
1943   (widen)
1944   (narrow-to-region
1945    (goto-char (point-min))
1946    (cond
1947     ((re-search-forward
1948       (concat "^" (regexp-quote mail-header-separator) "\n") nil t)
1949      (match-beginning 0))
1950     ((search-forward "\n\n" nil t)
1951      (1- (point)))
1952     (t
1953      (point-max))))
1954   (goto-char (point-min)))
1955
1956 (defun message-news-p ()
1957   "Say whether the current buffer contains a news message."
1958   (and (not message-this-is-mail)
1959        (or message-this-is-news
1960            (save-excursion
1961              (save-restriction
1962                (message-narrow-to-headers)
1963                (and (message-fetch-field "newsgroups")
1964                     (not (message-fetch-field "posted-to"))))))))
1965
1966 (defun message-mail-p ()
1967   "Say whether the current buffer contains a mail message."
1968   (and (not message-this-is-news)
1969        (or message-this-is-mail
1970            (save-excursion
1971              (save-restriction
1972                (message-narrow-to-headers)
1973                (or (message-fetch-field "to")
1974                    (message-fetch-field "cc")
1975                    (message-fetch-field "bcc")))))))
1976
1977 (defun message-subscribed-p ()
1978   "Say whether we need to insert a MFT header."
1979   (or message-subscribed-regexps
1980       message-subscribed-addresses
1981       message-subscribed-address-file
1982       message-subscribed-address-functions))
1983
1984 (defun message-next-header ()
1985   "Go to the beginning of the next header."
1986   (beginning-of-line)
1987   (or (eobp) (forward-char 1))
1988   (not (if (re-search-forward "^[^ \t]" nil t)
1989            (beginning-of-line)
1990          (goto-char (point-max)))))
1991
1992 (defun message-sort-headers-1 ()
1993   "Sort the buffer as headers using `message-rank' text props."
1994   (goto-char (point-min))
1995   (require 'sort)
1996   (sort-subr
1997    nil 'message-next-header
1998    (lambda ()
1999      (message-next-header)
2000      (unless (bobp)
2001        (forward-char -1)))
2002    (lambda ()
2003      (or (get-text-property (point) 'message-rank)
2004          10000))))
2005
2006 (defun message-sort-headers ()
2007   "Sort the headers of the current message according to `message-header-format-alist'."
2008   (interactive)
2009   (save-excursion
2010     (save-restriction
2011       (let ((max (1+ (length message-header-format-alist)))
2012             rank)
2013         (message-narrow-to-headers)
2014         (while (re-search-forward "^[^ \n]+:" nil t)
2015           (put-text-property
2016            (match-beginning 0) (1+ (match-beginning 0))
2017            'message-rank
2018            (if (setq rank (length (memq (assq (intern (buffer-substring
2019                                                        (match-beginning 0)
2020                                                        (1- (match-end 0))))
2021                                               message-header-format-alist)
2022                                         message-header-format-alist)))
2023                (- max rank)
2024              (1+ max)))))
2025       (message-sort-headers-1))))
2026
2027
2028 \f
2029
2030 ;;;
2031 ;;; Message mode
2032 ;;;
2033
2034 ;;; Set up keymap.
2035
2036 (defvar message-mode-map nil)
2037
2038 (unless message-mode-map
2039   (setq message-mode-map (make-keymap))
2040   (set-keymap-parent message-mode-map text-mode-map)
2041   (define-key message-mode-map "\C-c?" 'describe-mode)
2042
2043   (define-key message-mode-map "\C-c\C-f\C-t" 'message-goto-to)
2044   (define-key message-mode-map "\C-c\C-f\C-o" 'message-goto-from)
2045   (define-key message-mode-map "\C-c\C-f\C-b" 'message-goto-bcc)
2046   (define-key message-mode-map "\C-c\C-f\C-w" 'message-goto-fcc)
2047   (define-key message-mode-map "\C-c\C-f\C-c" 'message-goto-cc)
2048   (define-key message-mode-map "\C-c\C-f\C-s" 'message-goto-subject)
2049   (define-key message-mode-map "\C-c\C-f\C-r" 'message-goto-reply-to)
2050   (define-key message-mode-map "\C-c\C-f\C-n" 'message-goto-newsgroups)
2051   (define-key message-mode-map "\C-c\C-f\C-d" 'message-goto-distribution)
2052   (define-key message-mode-map "\C-c\C-f\C-f" 'message-goto-followup-to)
2053   (define-key message-mode-map "\C-c\C-f\C-m" 'message-goto-mail-followup-to)
2054   (define-key message-mode-map "\C-c\C-f\C-k" 'message-goto-keywords)
2055   (define-key message-mode-map "\C-c\C-f\C-u" 'message-goto-summary)
2056   (define-key message-mode-map "\C-c\C-f\C-i"
2057     'message-insert-or-toggle-importance)
2058   (define-key message-mode-map "\C-c\C-f\C-a"
2059     'message-generate-unsubscribed-mail-followup-to)
2060
2061   ;; modify headers (and insert notes in body)
2062   (define-key message-mode-map "\C-c\C-fs"    'message-change-subject)
2063   ;;
2064   (define-key message-mode-map "\C-c\C-fx"    'message-cross-post-followup-to)
2065   ;; prefix+message-cross-post-followup-to = same w/o cross-post
2066   (define-key message-mode-map "\C-c\C-ft"    'message-reduce-to-to-cc)
2067   (define-key message-mode-map "\C-c\C-fa"    'message-add-archive-header)
2068   ;; mark inserted text
2069   (define-key message-mode-map "\C-c\M-m" 'message-mark-inserted-region)
2070   (define-key message-mode-map "\C-c\M-f" 'message-mark-insert-file)
2071
2072   (define-key message-mode-map "\C-c\C-b" 'message-goto-body)
2073   (define-key message-mode-map "\C-c\C-i" 'message-goto-signature)
2074
2075   (define-key message-mode-map "\C-c\C-t" 'message-insert-to)
2076   (define-key message-mode-map "\C-c\C-fw" 'message-insert-wide-reply)
2077   (define-key message-mode-map "\C-c\C-n" 'message-insert-newsgroups)
2078   (define-key message-mode-map "\C-c\C-l" 'message-to-list-only)
2079
2080   (define-key message-mode-map "\C-c\C-u" 'message-insert-or-toggle-importance)
2081   (define-key message-mode-map "\C-c\M-n"
2082     'message-insert-disposition-notification-to)
2083
2084   (define-key message-mode-map "\C-c\C-y" 'message-yank-original)
2085   (define-key message-mode-map "\C-c\M-\C-y" 'message-yank-buffer)
2086   (define-key message-mode-map "\C-c\C-q" 'message-fill-yanked-message)
2087   (define-key message-mode-map "\C-c\C-w" 'message-insert-signature)
2088   (define-key message-mode-map "\C-c\M-h" 'message-insert-headers)
2089   (define-key message-mode-map "\C-c\C-r" 'message-caesar-buffer-body)
2090   (define-key message-mode-map "\C-c\C-o" 'message-sort-headers)
2091   (define-key message-mode-map "\C-c\M-r" 'message-rename-buffer)
2092
2093   (define-key message-mode-map "\C-c\C-c" 'message-send-and-exit)
2094   (define-key message-mode-map "\C-c\C-s" 'message-send)
2095   (define-key message-mode-map "\C-c\C-k" 'message-kill-buffer)
2096   (define-key message-mode-map "\C-c\C-d" 'message-dont-send)
2097   (define-key message-mode-map "\C-c\n" 'gnus-delay-article)
2098
2099   (define-key message-mode-map "\C-c\C-e" 'message-elide-region)
2100   (define-key message-mode-map "\C-c\C-v" 'message-delete-not-region)
2101   (define-key message-mode-map "\C-c\C-z" 'message-kill-to-signature)
2102   (define-key message-mode-map "\M-\r" 'message-newline-and-reformat)
2103   ;;(define-key message-mode-map "\M-q" 'message-fill-paragraph)
2104   (define-key message-mode-map [remap split-line]  'message-split-line)
2105
2106   (define-key message-mode-map "\C-c\C-a" 'mml-attach-file)
2107
2108   (define-key message-mode-map "\C-a" 'message-beginning-of-line)
2109   (define-key message-mode-map "\t" 'message-tab)
2110   (define-key message-mode-map "\M-;" 'comment-region))
2111
2112 (easy-menu-define
2113   message-mode-menu message-mode-map "Message Menu."
2114   `("Message"
2115     ["Yank Original" message-yank-original message-reply-buffer]
2116     ["Fill Yanked Message" message-fill-yanked-message t]
2117     ["Insert Signature" message-insert-signature t]
2118     ["Caesar (rot13) Message" message-caesar-buffer-body t]
2119     ["Caesar (rot13) Region" message-caesar-region (message-mark-active-p)]
2120     ["Elide Region" message-elide-region 
2121      :active (message-mark-active-p)
2122      ,@(if (featurep 'xemacs) nil
2123          '(:help "Replace text in region with an ellipsis"))]
2124     ["Delete Outside Region" message-delete-not-region 
2125      :active (message-mark-active-p)
2126      ,@(if (featurep 'xemacs) nil
2127          '(:help "Delete all quoted text outside region"))]
2128     ["Kill To Signature" message-kill-to-signature t]
2129     ["Newline and Reformat" message-newline-and-reformat t]
2130     ["Rename buffer" message-rename-buffer t]
2131     ["Spellcheck" ispell-message
2132      ,@(if (featurep 'xemacs) '(t)
2133          '(:help "Spellcheck this message"))]
2134     "----"
2135     ["Insert Region Marked" message-mark-inserted-region
2136      :active (message-mark-active-p)
2137      ,@(if (featurep 'xemacs) nil
2138          '(:help "Mark region with enclosing tags"))]
2139     ["Insert File Marked..." message-mark-insert-file
2140      ,@(if (featurep 'xemacs) '(t)
2141          '(:help "Insert file at point marked with enclosing tags"))]
2142     "----"
2143     ["Send Message" message-send-and-exit
2144      ,@(if (featurep 'xemacs) '(t)
2145          '(:help "Send this message"))]
2146     ["Postpone Message" message-dont-send
2147      ,@(if (featurep 'xemacs) '(t)
2148          '(:help "File this draft message and exit"))]
2149     ["Send at Specific Time..." gnus-delay-article
2150      ,@(if (featurep 'xemacs) '(t)
2151          '(:help "Ask, then arrange to send message at that time"))]
2152     ["Kill Message" message-kill-buffer
2153      ,@(if (featurep 'xemacs) '(t)
2154          '(:help "Delete this message without sending"))]))
2155
2156 (easy-menu-define
2157   message-mode-field-menu message-mode-map ""
2158   `("Field"
2159     ["Fetch To" message-insert-to t]
2160     ["Fetch Newsgroups" message-insert-newsgroups t]
2161     "----"
2162     ["To" message-goto-to t]
2163     ["From" message-goto-from t]
2164     ["Subject" message-goto-subject t]
2165     ["Change subject..." message-change-subject t]
2166     ["Cc" message-goto-cc t]
2167     ["Bcc" message-goto-bcc t]
2168     ["Fcc" message-goto-fcc t]
2169     ["Reply-To" message-goto-reply-to t]
2170     ["Flag As Important" message-insert-importance-high
2171      ,@(if (featurep 'xemacs) '(t)
2172          '(:help "Mark this message as important"))]
2173     ["Flag As Unimportant" message-insert-importance-low
2174      ,@(if (featurep 'xemacs) '(t)
2175          '(:help "Mark this message as unimportant"))]
2176     ["Request Receipt"
2177      message-insert-disposition-notification-to
2178      ,@(if (featurep 'xemacs) '(t)
2179          '(:help "Request a receipt notification"))]
2180     "----"
2181     ;; (typical) news stuff
2182     ["Summary" message-goto-summary t]
2183     ["Keywords" message-goto-keywords t]
2184     ["Newsgroups" message-goto-newsgroups t]
2185     ["Followup-To" message-goto-followup-to t]
2186     ;; ["Followup-To (with note in body)" message-cross-post-followup-to t]
2187     ["Crosspost / Followup-To..." message-cross-post-followup-to t]
2188     ["Distribution" message-goto-distribution t]
2189     ["X-No-Archive:" message-add-archive-header t ]
2190     "----"
2191     ;; (typical) mailing-lists stuff
2192     ["Send to list only" message-to-list-only t]
2193     ["Mail-Followup-To" message-goto-mail-followup-to t]
2194     ["Reduce To: to Cc:" message-reduce-to-to-cc t]
2195     "----"
2196     ["Sort Headers" message-sort-headers t]
2197     ["Encode non-ASCII domain names" message-idna-to-ascii-rhs t]
2198     ["Goto Body" message-goto-body t]
2199     ["Goto Signature" message-goto-signature t]))
2200
2201 (defvar message-tool-bar-map nil)
2202
2203 (eval-when-compile
2204   (defvar facemenu-add-face-function)
2205   (defvar facemenu-remove-face-function))
2206
2207 ;;; Forbidden properties
2208 ;;
2209 ;; We use `after-change-functions' to keep special text properties
2210 ;; that interfer with the normal function of message mode out of the
2211 ;; buffer.
2212
2213 (defcustom message-strip-special-text-properties t
2214   "Strip special properties from the message buffer.
2215
2216 Emacs has a number of special text properties which can break message
2217 composing in various ways.  If this option is set, message will strip
2218 these properties from the message composition buffer.  However, some
2219 packages requires these properties to be present in order to work.
2220 If you use one of these packages, turn this option off, and hope the
2221 message composition doesn't break too bad."
2222   :group 'message-various
2223   :type 'boolean)
2224
2225 (defconst message-forbidden-properties
2226   ;; No reason this should be clutter up customize.  We make it a
2227   ;; property list (rather than a list of property symbols), to be
2228   ;; directly useful for `remove-text-properties'.
2229   '(field nil read-only nil invisible nil intangible nil
2230           mouse-face nil modification-hooks nil insert-in-front-hooks nil
2231           insert-behind-hooks nil point-entered nil point-left nil)
2232   ;; Other special properties:
2233   ;; category, face, display: probably doesn't do any harm.
2234   ;; fontified: is used by font-lock.
2235   ;; syntax-table, local-map: I dunno.
2236   ;; We need to add XEmacs names to the list.
2237   "Property list of with properties.forbidden in message buffers.
2238 The values of the properties are ignored, only the property names are used.")
2239
2240 (defun message-tamago-not-in-use-p (pos)
2241   "Return t when tamago version 4 is not in use at the cursor position.
2242 Tamago version 4 is a popular input method for writing Japanese text.
2243 It uses the properties `intangible', `invisible', `modification-hooks'
2244 and `read-only' when translating ascii or kana text to kanji text.
2245 These properties are essential to work, so we should never strip them."
2246   (not (and (boundp 'egg-modefull-mode)
2247             (symbol-value 'egg-modefull-mode)
2248             (or (memq (get-text-property pos 'intangible)
2249                       '(its-part-1 its-part-2))
2250                 (get-text-property pos 'egg-end)
2251                 (get-text-property pos 'egg-lang)
2252                 (get-text-property pos 'egg-start)))))
2253
2254 (defun message-strip-forbidden-properties (begin end &optional old-length)
2255   "Strip forbidden properties between BEGIN and END, ignoring the third arg.
2256 This function is intended to be called from `after-change-functions'.
2257 See also `message-forbidden-properties'."
2258   (when (and message-strip-special-text-properties
2259              (message-tamago-not-in-use-p begin))
2260     (while (not (= begin end))
2261       (when (not (get-text-property begin 'message-hidden))
2262         (remove-text-properties begin (1+ begin)
2263                                 message-forbidden-properties))
2264       (incf begin))))
2265
2266 ;;;###autoload
2267 (define-derived-mode message-mode text-mode "Message"
2268   "Major mode for editing mail and news to be sent.
2269 Like Text Mode but with these additional commands:\\<message-mode-map>
2270 C-c C-s  `message-send' (send the message)  C-c C-c  `message-send-and-exit'
2271 C-c C-d  Postpone sending the message       C-c C-k  Kill the message
2272 C-c C-f  move to a header field (and create it if there isn't):
2273          C-c C-f C-t  move to To        C-c C-f C-s  move to Subject
2274          C-c C-f C-c  move to Cc        C-c C-f C-b  move to Bcc
2275          C-c C-f C-w  move to Fcc       C-c C-f C-r  move to Reply-To
2276          C-c C-f C-u  move to Summary   C-c C-f C-n  move to Newsgroups
2277          C-c C-f C-k  move to Keywords  C-c C-f C-d  move to Distribution
2278          C-c C-f C-o  move to From (\"Originator\")
2279          C-c C-f C-f  move to Followup-To
2280          C-c C-f C-m  move to Mail-Followup-To
2281          C-c C-f C-i  cycle through Importance values
2282          C-c C-f s    change subject and append \"(was: <Old Subject>)\"
2283          C-c C-f x    crossposting with FollowUp-To header and note in body
2284          C-c C-f t    replace To: header with contents of Cc: or Bcc:
2285          C-c C-f a    Insert X-No-Archive: header and a note in the body
2286 C-c C-t  `message-insert-to' (add a To header to a news followup)
2287 C-c C-l  `message-to-list-only' (removes all but list address in to/cc)
2288 C-c C-n  `message-insert-newsgroups' (add a Newsgroup header to a news reply)
2289 C-c C-b  `message-goto-body' (move to beginning of message text).
2290 C-c C-i  `message-goto-signature' (move to the beginning of the signature).
2291 C-c C-w  `message-insert-signature' (insert `message-signature-file' file).
2292 C-c C-y  `message-yank-original' (insert current message, if any).
2293 C-c C-q  `message-fill-yanked-message' (fill what was yanked).
2294 C-c C-e  `message-elide-region' (elide the text between point and mark).
2295 C-c C-v  `message-delete-not-region' (remove the text outside the region).
2296 C-c C-z  `message-kill-to-signature' (kill the text up to the signature).
2297 C-c C-r  `message-caesar-buffer-body' (rot13 the message body).
2298 C-c C-a  `mml-attach-file' (attach a file as MIME).
2299 C-c C-u  `message-insert-or-toggle-importance'  (insert or cycle importance).
2300 C-c M-n  `message-insert-disposition-notification-to'  (request receipt).
2301 C-c M-m  `message-mark-inserted-region' (mark region with enclosing tags).
2302 C-c M-f  `message-mark-insert-file' (insert file marked with enclosing tags).
2303 M-RET    `message-newline-and-reformat' (break the line and reformat)."
2304   (setq local-abbrev-table text-mode-abbrev-table)
2305   (set (make-local-variable 'message-reply-buffer) nil)
2306   (make-local-variable 'message-send-actions)
2307   (make-local-variable 'message-exit-actions)
2308   (make-local-variable 'message-kill-actions)
2309   (make-local-variable 'message-postpone-actions)
2310   (make-local-variable 'message-draft-article)
2311   (setq buffer-offer-save t)
2312   (set (make-local-variable 'facemenu-add-face-function)
2313        (lambda (face end)
2314          (let ((face-fun (cdr (assq face message-face-alist))))
2315            (if face-fun
2316                (funcall face-fun (point) end)
2317              (error "Face %s not configured for %s mode" face mode-name)))
2318          ""))
2319   (set (make-local-variable 'facemenu-remove-face-function) t)
2320   (set (make-local-variable 'message-reply-headers) nil)
2321   (make-local-variable 'message-newsreader)
2322   (make-local-variable 'message-mailer)
2323   (make-local-variable 'message-post-method)
2324   (set (make-local-variable 'message-sent-message-via) nil)
2325   (set (make-local-variable 'message-checksum) nil)
2326   (set (make-local-variable 'message-mime-part) 0)
2327   (message-setup-fill-variables)
2328   (set
2329    (make-local-variable 'paragraph-separate)
2330    (format "\\(%s\\)\\|\\(%s\\)"
2331            paragraph-separate
2332            "<#!*/?\\(multipart\\|part\\|external\\|mml\\|secure\\)"))
2333   ;; Allow using comment commands to add/remove quoting.
2334   (set (make-local-variable 'comment-start) message-yank-prefix)
2335   (if (featurep 'xemacs)
2336       (message-setup-toolbar)
2337     (set (make-local-variable 'font-lock-defaults)
2338          '(message-font-lock-keywords t))
2339     (if (boundp 'tool-bar-map)
2340         (set (make-local-variable 'tool-bar-map) (message-tool-bar-map))))
2341   (easy-menu-add message-mode-menu message-mode-map)
2342   (easy-menu-add message-mode-field-menu message-mode-map)
2343   (gnus-make-local-hook 'after-change-functions)
2344   ;; Mmmm... Forbidden properties...
2345   (add-hook 'after-change-functions 'message-strip-forbidden-properties
2346             nil 'local)
2347   ;; Allow mail alias things.
2348   (when (eq message-mail-alias-type 'abbrev)
2349     (if (fboundp 'mail-abbrevs-setup)
2350         (mail-abbrevs-setup)
2351       (mail-aliases-setup)))
2352   (unless buffer-file-name
2353     (message-set-auto-save-file-name))
2354   (unless (buffer-base-buffer)
2355     ;; Don't enable multibyte on an indirect buffer.  Maybe enabling
2356     ;; multibyte is not necessary at all. -- zsh
2357     (mm-enable-multibyte))
2358   (set (make-local-variable 'indent-tabs-mode) nil) ;No tabs for indentation.
2359   (mml-mode))
2360
2361 (defun message-setup-fill-variables ()
2362   "Setup message fill variables."
2363   (set (make-local-variable 'fill-paragraph-function)
2364        'message-fill-paragraph)
2365   (make-local-variable 'paragraph-separate)
2366   (make-local-variable 'paragraph-start)
2367   (make-local-variable 'adaptive-fill-regexp)
2368   (unless (boundp 'adaptive-fill-first-line-regexp)
2369     (setq adaptive-fill-first-line-regexp nil))
2370   (make-local-variable 'adaptive-fill-first-line-regexp)
2371   (let ((quote-prefix-regexp
2372          ;; User should change message-cite-prefix-regexp if
2373          ;; message-yank-prefix is set to an abnormal value.
2374          (concat "\\(" message-cite-prefix-regexp "\\)[ \t]*")))
2375     (setq paragraph-start
2376           (concat
2377            (regexp-quote mail-header-separator) "$\\|"
2378            "[ \t]*$\\|"                 ; blank lines
2379            "-- $\\|"                    ; signature delimiter
2380            "---+$\\|"              ; delimiters for forwarded messages
2381            page-delimiter "$\\|"        ; spoiler warnings
2382            ".*wrote:$\\|"               ; attribution lines
2383            quote-prefix-regexp "$"))    ; empty lines in quoted text
2384     (setq paragraph-separate paragraph-start)
2385     (setq adaptive-fill-regexp
2386           (concat quote-prefix-regexp "\\|" adaptive-fill-regexp))
2387     (setq adaptive-fill-first-line-regexp
2388           (concat quote-prefix-regexp "\\|"
2389                   adaptive-fill-first-line-regexp)))
2390   (make-local-variable 'auto-fill-inhibit-regexp)
2391   ;;(setq auto-fill-inhibit-regexp "^[A-Z][^: \n\t]+:")
2392   (setq auto-fill-inhibit-regexp nil)
2393   (make-local-variable 'normal-auto-fill-function)
2394   (setq normal-auto-fill-function 'message-do-auto-fill)
2395   ;; KLUDGE: auto fill might already be turned on in `text-mode-hook'.
2396   ;; In that case, ensure that it uses the right function.  The real
2397   ;; solution would be not to use `define-derived-mode', and run
2398   ;; `text-mode-hook' ourself at the end of the mode.
2399   ;; -- Per Abrahamsen <abraham@dina.kvl.dk> Date: 2001-10-19.
2400   (when auto-fill-function
2401     (setq auto-fill-function normal-auto-fill-function)))
2402
2403 \f
2404
2405 ;;;
2406 ;;; Message mode commands
2407 ;;;
2408
2409 ;;; Movement commands
2410
2411 (defun message-goto-to ()
2412   "Move point to the To header."
2413   (interactive)
2414   (message-position-on-field "To"))
2415
2416 (defun message-goto-from ()
2417   "Move point to the From header."
2418   (interactive)
2419   (message-position-on-field "From"))
2420
2421 (defun message-goto-subject ()
2422   "Move point to the Subject header."
2423   (interactive)
2424   (message-position-on-field "Subject"))
2425
2426 (defun message-goto-cc ()
2427   "Move point to the Cc header."
2428   (interactive)
2429   (message-position-on-field "Cc" "To"))
2430
2431 (defun message-goto-bcc ()
2432   "Move point to the Bcc  header."
2433   (interactive)
2434   (message-position-on-field "Bcc" "Cc" "To"))
2435
2436 (defun message-goto-fcc ()
2437   "Move point to the Fcc header."
2438   (interactive)
2439   (message-position-on-field "Fcc" "To" "Newsgroups"))
2440
2441 (defun message-goto-reply-to ()
2442   "Move point to the Reply-To header."
2443   (interactive)
2444   (message-position-on-field "Reply-To" "Subject"))
2445
2446 (defun message-goto-newsgroups ()
2447   "Move point to the Newsgroups header."
2448   (interactive)
2449   (message-position-on-field "Newsgroups"))
2450
2451 (defun message-goto-distribution ()
2452   "Move point to the Distribution header."
2453   (interactive)
2454   (message-position-on-field "Distribution"))
2455
2456 (defun message-goto-followup-to ()
2457   "Move point to the Followup-To header."
2458   (interactive)
2459   (message-position-on-field "Followup-To" "Newsgroups"))
2460
2461 (defun message-goto-mail-followup-to ()
2462   "Move point to the Mail-Followup-To header."
2463   (interactive)
2464   (message-position-on-field "Mail-Followup-To" "From"))
2465
2466 (defun message-goto-keywords ()
2467   "Move point to the Keywords header."
2468   (interactive)
2469   (message-position-on-field "Keywords" "Subject"))
2470
2471 (defun message-goto-summary ()
2472   "Move point to the Summary header."
2473   (interactive)
2474   (message-position-on-field "Summary" "Subject"))
2475
2476 (defun message-goto-body (&optional interactivep)
2477   "Move point to the beginning of the message body."
2478   (interactive (list t))
2479   (when (and interactivep
2480              (looking-at "[ \t]*\n"))
2481     (expand-abbrev))
2482   (goto-char (point-min))
2483   (or (search-forward (concat "\n" mail-header-separator "\n") nil t)
2484       (search-forward-regexp "[^:]+:\\([^\n]\\|\n[ \t]\\)+\n\n" nil t)))
2485
2486 (defun message-goto-eoh ()
2487   "Move point to the end of the headers."
2488   (interactive)
2489   (message-goto-body)
2490   (forward-line -1))
2491
2492 (defun message-goto-signature ()
2493   "Move point to the beginning of the message signature.
2494 If there is no signature in the article, go to the end and
2495 return nil."
2496   (interactive)
2497   (goto-char (point-min))
2498   (if (re-search-forward message-signature-separator nil t)
2499       (forward-line 1)
2500     (goto-char (point-max))
2501     nil))
2502
2503 (defun message-generate-unsubscribed-mail-followup-to (&optional include-cc)
2504   "Insert a reasonable MFT header in a post to an unsubscribed list.
2505 When making original posts to a mailing list you are not subscribed to,
2506 you have to type in a MFT header by hand.  The contents, usually, are
2507 the addresses of the list and your own address.  This function inserts
2508 such a header automatically.  It fetches the contents of the To: header
2509 in the current mail buffer, and appends the current `user-mail-address'.
2510
2511 If the optional argument INCLUDE-CC is non-nil, the addresses in the
2512 Cc: header are also put into the MFT."
2513
2514   (interactive "P")
2515   (message-remove-header "Mail-Followup-To")
2516   (let* ((cc (and include-cc (message-fetch-field "Cc")))
2517          (tos (if cc
2518                   (concat (message-fetch-field "To") "," cc)
2519                 (message-fetch-field "To"))))
2520     (message-goto-mail-followup-to)
2521     (insert (concat tos ", " user-mail-address))))
2522
2523 \f
2524
2525 (defun message-insert-to (&optional force)
2526   "Insert a To header that points to the author of the article being replied to.
2527 If the original author requested not to be sent mail, the function signals
2528 an error.
2529 With the prefix argument FORCE, insert the header anyway."
2530   (interactive "P")
2531   (let ((co (message-fetch-reply-field "mail-copies-to")))
2532     (when (and (null force)
2533                co
2534                (or (equal (downcase co) "never")
2535                    (equal (downcase co) "nobody")))
2536       (error "The user has requested not to have copies sent via mail")))
2537   (message-carefully-insert-headers
2538    (list (cons 'To
2539                (or (message-fetch-reply-field "mail-reply-to")
2540                    (message-fetch-reply-field "reply-to")
2541                    (message-fetch-reply-field "from")
2542                    "")))))
2543
2544 (defun message-insert-wide-reply ()
2545   "Insert To and Cc headers as if you were doing a wide reply."
2546   (interactive)
2547   (let ((headers (message-with-reply-buffer
2548                    (message-get-reply-headers t))))
2549     (message-carefully-insert-headers headers)))
2550
2551 (defun message-carefully-insert-headers (headers)
2552   (dolist (header headers)
2553     (let ((header-name (symbol-name (car header))))
2554       (when (and (message-position-on-field header-name)
2555                  (mail-fetch-field header-name)
2556                  (not (string-match "\\` *\\'"
2557                                     (mail-fetch-field header-name))))
2558         (insert ", "))
2559       (insert (cdr header)))))
2560
2561 (defun message-widen-reply ()
2562   "Widen the reply to include maximum recipients."
2563   (interactive)
2564   (let ((follow-to
2565          (and message-reply-buffer
2566               (buffer-name message-reply-buffer)
2567               (save-excursion
2568                 (set-buffer message-reply-buffer)
2569                 (message-get-reply-headers t)))))
2570     (save-excursion
2571       (save-restriction
2572         (message-narrow-to-headers)
2573         (dolist (elem follow-to)
2574           (message-remove-header (symbol-name (car elem)))
2575           (goto-char (point-min))
2576           (insert (symbol-name (car elem)) ": "
2577                   (cdr elem) "\n"))))))
2578
2579 (defun message-insert-newsgroups ()
2580   "Insert the Newsgroups header from the article being replied to."
2581   (interactive)
2582   (when (and (message-position-on-field "Newsgroups")
2583              (mail-fetch-field "newsgroups")
2584              (not (string-match "\\` *\\'" (mail-fetch-field "newsgroups"))))
2585     (insert ","))
2586   (insert (or (message-fetch-reply-field "newsgroups") "")))
2587
2588 \f
2589
2590 ;;; Various commands
2591
2592 (defun message-delete-not-region (beg end)
2593   "Delete everything in the body of the current message outside of the region."
2594   (interactive "r")
2595   (let (citeprefix)
2596     (save-excursion
2597       (goto-char beg)
2598       ;; snarf citation prefix, if appropriate
2599       (unless (eq (point) (progn (beginning-of-line) (point)))
2600         (when (looking-at message-cite-prefix-regexp)
2601           (setq citeprefix (match-string 0))))
2602       (goto-char end)
2603       (delete-region (point) (if (not (message-goto-signature))
2604                                  (point)
2605                                (forward-line -2)
2606                                (point)))
2607       (insert "\n")
2608       (goto-char beg)
2609       (delete-region beg (progn (message-goto-body)
2610                                 (forward-line 2)
2611                                 (point)))
2612       (when citeprefix
2613         (insert citeprefix))))
2614   (when (message-goto-signature)
2615     (forward-line -2)))
2616
2617 (defun message-kill-to-signature ()
2618   "Deletes all text up to the signature."
2619   (interactive)
2620   (let ((point (point)))
2621     (message-goto-signature)
2622     (unless (eobp)
2623       (end-of-line -1))
2624     (kill-region point (point))
2625     (unless (bolp)
2626       (insert "\n"))))
2627
2628 (defun message-newline-and-reformat (&optional arg not-break)
2629   "Insert four newlines, and then reformat if inside quoted text.
2630 Prefix arg means justify as well."
2631   (interactive (list (if current-prefix-arg 'full)))
2632   (let (quoted point beg end leading-space bolp)
2633     (setq point (point))
2634     (beginning-of-line)
2635     (setq beg (point))
2636     (setq bolp (= beg point))
2637     ;; Find first line of the paragraph.
2638     (if not-break
2639         (while (and (not (eobp))
2640                     (not (looking-at message-cite-prefix-regexp))
2641                     (looking-at paragraph-start))
2642           (forward-line 1)))
2643     ;; Find the prefix
2644     (when (looking-at message-cite-prefix-regexp)
2645       (setq quoted (match-string 0))
2646       (goto-char (match-end 0))
2647       (looking-at "[ \t]*")
2648       (setq leading-space (match-string 0)))
2649     (if (and quoted
2650              (not not-break)
2651              (not bolp)
2652              (< (- point beg) (length quoted)))
2653         ;; break inside the cite prefix.
2654         (setq quoted nil
2655               end nil))
2656     (if quoted
2657         (progn
2658           (forward-line 1)
2659           (while (and (not (eobp))
2660                       (not (looking-at paragraph-separate))
2661                       (looking-at message-cite-prefix-regexp)
2662                       (equal quoted (match-string 0)))
2663             (goto-char (match-end 0))
2664             (looking-at "[ \t]*")
2665             (if (> (length leading-space) (length (match-string 0)))
2666                 (setq leading-space (match-string 0)))
2667             (forward-line 1))
2668           (setq end (point))
2669           (goto-char beg)
2670           (while (and (if (bobp) nil (forward-line -1) t)
2671                       (not (looking-at paragraph-start))
2672                       (looking-at message-cite-prefix-regexp)
2673                       (equal quoted (match-string 0)))
2674             (setq beg (point))
2675             (goto-char (match-end 0))
2676             (looking-at "[ \t]*")
2677             (if (> (length leading-space) (length (match-string 0)))
2678                 (setq leading-space (match-string 0)))))
2679       (while (and (not (eobp))
2680                   (not (looking-at paragraph-separate))
2681                   (not (looking-at message-cite-prefix-regexp)))
2682         (forward-line 1))
2683       (setq end (point))
2684       (goto-char beg)
2685       (while (and (if (bobp) nil (forward-line -1) t)
2686                   (not (looking-at paragraph-start))
2687                   (not (looking-at message-cite-prefix-regexp)))
2688         (setq beg (point))))
2689     (goto-char point)
2690     (save-restriction
2691       (narrow-to-region beg end)
2692       (if not-break
2693           (setq point nil)
2694         (if bolp
2695             (newline)
2696           (newline)
2697           (newline))
2698         (setq point (point))
2699         ;; (newline 2) doesn't mark both newline's as hard, so call
2700         ;; newline twice. -jas
2701         (newline)
2702         (newline)
2703         (delete-region (point) (re-search-forward "[ \t]*"))
2704         (when (and quoted (not bolp))
2705           (insert quoted leading-space)))
2706       (undo-boundary)
2707       (if quoted
2708           (let* ((adaptive-fill-regexp
2709                   (regexp-quote (concat quoted leading-space)))
2710                  (adaptive-fill-first-line-regexp
2711                   adaptive-fill-regexp ))
2712             (fill-paragraph arg))
2713         (fill-paragraph arg))
2714       (if point (goto-char point)))))
2715
2716 (defun message-fill-paragraph (&optional arg)
2717   "Like `fill-paragraph'."
2718   (interactive (list (if current-prefix-arg 'full)))
2719   (if (if (boundp 'filladapt-mode) filladapt-mode)
2720       nil
2721     (message-newline-and-reformat arg t)
2722     t))
2723
2724 ;; Is it better to use `mail-header-end'?
2725 (defun message-point-in-header-p ()
2726   "Return t if point is in the header."
2727   (save-excursion
2728     (let ((p (point)))
2729       (goto-char (point-min))
2730       (not (re-search-forward
2731             (concat "^" (regexp-quote mail-header-separator) "\n")
2732             p t)))))
2733
2734 (defun message-do-auto-fill ()
2735   "Like `do-auto-fill', but don't fill in message header."
2736   (unless (message-point-in-header-p)
2737     (do-auto-fill)))
2738
2739 (defun message-insert-signature (&optional force)
2740   "Insert a signature.  See documentation for variable `message-signature'."
2741   (interactive (list 0))
2742   (let* ((signature
2743           (cond
2744            ((and (null message-signature)
2745                  (eq force 0))
2746             (save-excursion
2747               (goto-char (point-max))
2748               (not (re-search-backward message-signature-separator nil t))))
2749            ((and (null message-signature)
2750                  force)
2751             t)
2752            ((message-functionp message-signature)
2753             (funcall message-signature))
2754            ((listp message-signature)
2755             (eval message-signature))
2756            (t message-signature)))
2757          (signature
2758           (cond ((stringp signature)
2759                  signature)
2760                 ((and (eq t signature)
2761                       message-signature-file
2762                       (file-exists-p message-signature-file))
2763                  signature))))
2764     (when signature
2765       (goto-char (point-max))
2766       ;; Insert the signature.
2767       (unless (bolp)
2768         (insert "\n"))
2769       (when message-signature-insert-empty-line
2770         (insert "\n"))
2771       (insert "-- \n")
2772       (if (eq signature t)
2773           (insert-file-contents message-signature-file)
2774         (insert signature))
2775       (goto-char (point-max))
2776       (or (bolp) (insert "\n")))))
2777
2778 (defun message-insert-importance-high ()
2779   "Insert header to mark message as important."
2780   (interactive)
2781   (save-excursion
2782     (message-remove-header "Importance")
2783     (message-goto-eoh)
2784     (insert "Importance: high\n")))
2785
2786 (defun message-insert-importance-low ()
2787   "Insert header to mark message as unimportant."
2788   (interactive)
2789   (save-excursion
2790     (message-remove-header "Importance")
2791     (message-goto-eoh)
2792     (insert "Importance: low\n")))
2793
2794 (defun message-insert-or-toggle-importance ()
2795   "Insert a \"Importance: high\" header, or cycle through the header values.
2796 The three allowed values according to RFC 1327 are `high', `normal'
2797 and `low'."
2798   (interactive)
2799   (save-excursion
2800     (let ((valid '("high" "normal" "low"))
2801           (new "high")
2802           cur)
2803       (when (setq cur (message-fetch-field "Importance"))
2804         (message-remove-header "Importance")
2805         (setq new (cond ((string= cur "high")
2806                          "low")
2807                         ((string= cur "low")
2808                          "normal")
2809                         (t
2810                          "high"))))
2811       (message-goto-eoh)
2812       (insert (format "Importance: %s\n" new)))))
2813
2814 (defun message-insert-disposition-notification-to ()
2815   "Request a disposition notification (return receipt) to this message.
2816 Note that this should not be used in newsgroups."
2817   (interactive)
2818   (save-excursion
2819     (message-remove-header "Disposition-Notification-To")
2820     (message-goto-eoh)
2821     (insert (format "Disposition-Notification-To: %s\n"
2822                     (or (message-fetch-field "From") (message-make-from))))))
2823
2824 (defun message-elide-region (b e)
2825   "Elide the text in the region.
2826 An ellipsis (from `message-elide-ellipsis') will be inserted where the
2827 text was killed."
2828   (interactive "r")
2829   (kill-region b e)
2830   (insert message-elide-ellipsis))
2831
2832 (defvar message-caesar-translation-table nil)
2833
2834 (defun message-caesar-region (b e &optional n)
2835   "Caesar rotate region B to E by N, default 13, for decrypting netnews."
2836   (interactive
2837    (list
2838     (min (point) (or (mark t) (point)))
2839     (max (point) (or (mark t) (point)))
2840     (when current-prefix-arg
2841       (prefix-numeric-value current-prefix-arg))))
2842
2843   (setq n (if (numberp n) (mod n 26) 13)) ;canonize N
2844   (unless (or (zerop n)                 ; no action needed for a rot of 0
2845               (= b e))                  ; no region to rotate
2846     ;; We build the table, if necessary.
2847     (when (or (not message-caesar-translation-table)
2848               (/= (aref message-caesar-translation-table ?a) (+ ?a n)))
2849       (setq message-caesar-translation-table
2850             (message-make-caesar-translation-table n)))
2851     (translate-region b e message-caesar-translation-table)))
2852
2853 (defun message-make-caesar-translation-table (n)
2854   "Create a rot table with offset N."
2855   (let ((i -1)
2856         (table (make-string 256 0)))
2857     (while (< (incf i) 256)
2858       (aset table i i))
2859     (concat
2860      (substring table 0 ?A)
2861      (substring table (+ ?A n) (+ ?A n (- 26 n)))
2862      (substring table ?A (+ ?A n))
2863      (substring table (+ ?A 26) ?a)
2864      (substring table (+ ?a n) (+ ?a n (- 26 n)))
2865      (substring table ?a (+ ?a n))
2866      (substring table (+ ?a 26) 255))))
2867
2868 (defun message-caesar-buffer-body (&optional rotnum)
2869   "Caesar rotate all letters in the current buffer by 13 places.
2870 Used to encode/decode possibly offensive messages (commonly in rec.humor).
2871 With prefix arg, specifies the number of places to rotate each letter forward.
2872 Mail and USENET news headers are not rotated."
2873   (interactive (if current-prefix-arg
2874                    (list (prefix-numeric-value current-prefix-arg))
2875                  (list nil)))
2876   (save-excursion
2877     (save-restriction
2878       (when (message-goto-body)
2879         (narrow-to-region (point) (point-max)))
2880       (message-caesar-region (point-min) (point-max) rotnum))))
2881
2882 (defun message-pipe-buffer-body (program)
2883   "Pipe the message body in the current buffer through PROGRAM."
2884   (save-excursion
2885     (save-restriction
2886       (when (message-goto-body)
2887         (narrow-to-region (point) (point-max)))
2888       (shell-command-on-region
2889        (point-min) (point-max) program nil t))))
2890
2891 (defun message-rename-buffer (&optional enter-string)
2892   "Rename the *message* buffer to \"*message* RECIPIENT\".
2893 If the function is run with a prefix, it will ask for a new buffer
2894 name, rather than giving an automatic name."
2895   (interactive "Pbuffer name: ")
2896   (save-excursion
2897     (save-restriction
2898       (goto-char (point-min))
2899       (narrow-to-region (point)
2900                         (search-forward mail-header-separator nil 'end))
2901       (let* ((mail-to (or
2902                        (if (message-news-p) (message-fetch-field "Newsgroups")
2903                          (message-fetch-field "To"))
2904                        ""))
2905              (mail-trimmed-to
2906               (if (string-match "," mail-to)
2907                   (concat (substring mail-to 0 (match-beginning 0)) ", ...")
2908                 mail-to))
2909              (name-default (concat "*message* " mail-trimmed-to))
2910              (name (if enter-string
2911                        (read-string "New buffer name: " name-default)
2912                      name-default)))
2913         (rename-buffer name t)))))
2914
2915 (defun message-fill-yanked-message (&optional justifyp)
2916   "Fill the paragraphs of a message yanked into this one.
2917 Numeric argument means justify as well."
2918   (interactive "P")
2919   (save-excursion
2920     (goto-char (point-min))
2921     (search-forward (concat "\n" mail-header-separator "\n") nil t)
2922     (let ((fill-prefix message-yank-prefix))
2923       (fill-individual-paragraphs (point) (point-max) justifyp))))
2924
2925 (defun message-indent-citation ()
2926   "Modify text just inserted from a message to be cited.
2927 The inserted text should be the region.
2928 When this function returns, the region is again around the modified text.
2929
2930 Normally, indent each nonblank line `message-indentation-spaces' spaces.
2931 However, if `message-yank-prefix' is non-nil, insert that prefix on each line."
2932   (let ((start (point)))
2933     ;; Remove unwanted headers.
2934     (when message-ignored-cited-headers
2935       (let (all-removed)
2936         (save-restriction
2937           (narrow-to-region
2938            (goto-char start)
2939            (if (search-forward "\n\n" nil t)
2940                (1- (point))
2941              (point)))
2942           (message-remove-header message-ignored-cited-headers t)
2943           (when (= (point-min) (point-max))
2944             (setq all-removed t))
2945           (goto-char (point-max)))
2946         (if all-removed
2947             (goto-char start)
2948           (forward-line 1))))
2949     ;; Delete blank lines at the start of the buffer.
2950     (while (and (point-min)
2951                 (eolp)
2952                 (not (eobp)))
2953       (message-delete-line))
2954     ;; Delete blank lines at the end of the buffer.
2955     (goto-char (point-max))
2956     (unless (eolp)
2957       (insert "\n"))
2958     (while (and (zerop (forward-line -1))
2959                 (looking-at "$"))
2960       (message-delete-line))
2961     ;; Do the indentation.
2962     (if (null message-yank-prefix)
2963         (indent-rigidly start (mark t) message-indentation-spaces)
2964       (save-excursion
2965         (goto-char start)
2966         (while (< (point) (mark t))
2967           (if (or (looking-at ">") (looking-at "^$"))
2968               (insert message-yank-cited-prefix)
2969             (insert message-yank-prefix))
2970           (forward-line 1))))
2971     (goto-char start)))
2972
2973 (defun message-yank-original (&optional arg)
2974   "Insert the message being replied to, if any.
2975 Puts point before the text and mark after.
2976 Normally indents each nonblank line ARG spaces (default 3).  However,
2977 if `message-yank-prefix' is non-nil, insert that prefix on each line.
2978
2979 This function uses `message-cite-function' to do the actual citing.
2980
2981 Just \\[universal-argument] as argument means don't indent, insert no
2982 prefix, and don't delete any headers."
2983   (interactive "P")
2984   (let ((modified (buffer-modified-p)))
2985     (when (and message-reply-buffer
2986                message-cite-function)
2987       (delete-windows-on message-reply-buffer t)
2988       (insert-buffer message-reply-buffer)
2989       (unless arg
2990         (funcall message-cite-function))
2991       (message-exchange-point-and-mark)
2992       (unless (bolp)
2993         (insert ?\n))
2994       (unless modified
2995         (setq message-checksum (message-checksum))))))
2996
2997 (defun message-yank-buffer (buffer)
2998   "Insert BUFFER into the current buffer and quote it."
2999   (interactive "bYank buffer: ")
3000   (let ((message-reply-buffer buffer))
3001     (save-window-excursion
3002       (message-yank-original))))
3003
3004 (defun message-buffers ()
3005   "Return a list of active message buffers."
3006   (let (buffers)
3007     (save-excursion
3008       (dolist (buffer (buffer-list t))
3009         (set-buffer buffer)
3010         (when (and (eq major-mode 'message-mode)
3011                    (null message-sent-message-via))
3012           (push (buffer-name buffer) buffers))))
3013     (nreverse buffers)))
3014
3015 (defun message-cite-original-without-signature ()
3016   "Cite function in the standard Message manner."
3017   (let ((start (point))
3018         (end (mark t))
3019         (functions
3020          (when message-indent-citation-function
3021            (if (listp message-indent-citation-function)
3022                message-indent-citation-function
3023              (list message-indent-citation-function)))))
3024     (mml-quote-region start end)
3025     ;; Allow undoing.
3026     (undo-boundary)
3027     (goto-char end)
3028     (when (re-search-backward message-signature-separator start t)
3029       ;; Also peel off any blank lines before the signature.
3030       (forward-line -1)
3031       (while (looking-at "^[ \t]*$")
3032         (forward-line -1))
3033       (forward-line 1)
3034       (delete-region (point) end)
3035       (unless (search-backward "\n\n" start t)
3036         ;; Insert a blank line if it is peeled off.
3037         (insert "\n")))
3038     (goto-char start)
3039     (while functions
3040       (funcall (pop functions)))
3041     (when message-citation-line-function
3042       (unless (bolp)
3043         (insert "\n"))
3044       (funcall message-citation-line-function))))
3045
3046 (eval-when-compile (defvar mail-citation-hook)) ;Compiler directive
3047 (defun message-cite-original ()
3048   "Cite function in the standard Message manner."
3049   (if (and (boundp 'mail-citation-hook)
3050            mail-citation-hook)
3051       (run-hooks 'mail-citation-hook)
3052     (let ((start (point))
3053           (end (mark t))
3054           (functions
3055            (when message-indent-citation-function
3056              (if (listp message-indent-citation-function)
3057                  message-indent-citation-function
3058                (list message-indent-citation-function)))))
3059       (mml-quote-region start end)
3060       (goto-char start)
3061       (while functions
3062         (funcall (pop functions)))
3063       (when message-citation-line-function
3064         (unless (bolp)
3065           (insert "\n"))
3066         (funcall message-citation-line-function)))))
3067
3068 (defun message-insert-citation-line ()
3069   "Insert a simple citation line."
3070   (when message-reply-headers
3071     (insert (mail-header-from message-reply-headers) " writes:\n\n")))
3072
3073 (defun message-position-on-field (header &rest afters)
3074   (let ((case-fold-search t))
3075     (save-restriction
3076       (narrow-to-region
3077        (goto-char (point-min))
3078        (progn
3079          (re-search-forward
3080           (concat "^" (regexp-quote mail-header-separator) "$"))
3081          (match-beginning 0)))
3082       (goto-char (point-min))
3083       (if (re-search-forward (concat "^" (regexp-quote header) ":") nil t)
3084           (progn
3085             (re-search-forward "^[^ \t]" nil 'move)
3086             (beginning-of-line)
3087             (skip-chars-backward "\n")
3088             t)
3089         (while (and afters
3090                     (not (re-search-forward
3091                           (concat "^" (regexp-quote (car afters)) ":")
3092                           nil t)))
3093           (pop afters))
3094         (when afters
3095           (re-search-forward "^[^ \t]" nil 'move)
3096           (beginning-of-line))
3097         (insert header ": \n")
3098         (forward-char -1)
3099         nil))))
3100
3101 (defun message-remove-signature ()
3102   "Remove the signature from the text between point and mark.
3103 The text will also be indented the normal way."
3104   (save-excursion
3105     (let ((start (point))
3106           mark)
3107       (if (not (re-search-forward message-signature-separator (mark t) t))
3108           ;; No signature here, so we just indent the cited text.
3109           (message-indent-citation)
3110         ;; Find the last non-empty line.
3111         (forward-line -1)
3112         (while (looking-at "[ \t]*$")
3113           (forward-line -1))
3114         (forward-line 1)
3115         (setq mark (set-marker (make-marker) (point)))
3116         (goto-char start)
3117         (message-indent-citation)
3118         ;; Enable undoing the deletion.
3119         (undo-boundary)
3120         (delete-region mark (mark t))
3121         (set-marker mark nil)))))
3122
3123 \f
3124
3125 ;;;
3126 ;;; Sending messages
3127 ;;;
3128
3129 (defun message-send-and-exit (&optional arg)
3130   "Send message like `message-send', then, if no errors, exit from mail buffer."
3131   (interactive "P")
3132   (let ((buf (current-buffer))
3133         (actions message-exit-actions))
3134     (when (and (message-send arg)
3135                (buffer-name buf))
3136       (if message-kill-buffer-on-exit
3137           (kill-buffer buf)
3138         (bury-buffer buf)
3139         (when (eq buf (current-buffer))
3140           (message-bury buf)))
3141       (message-do-actions actions)
3142       t)))
3143
3144 (defun message-dont-send ()
3145   "Don't send the message you have been editing.
3146 Instead, just auto-save the buffer and then bury it."
3147   (interactive)
3148   (set-buffer-modified-p t)
3149   (save-buffer)
3150   (let ((actions message-postpone-actions))
3151     (message-bury (current-buffer))
3152     (message-do-actions actions)))
3153
3154 (defun message-kill-buffer ()
3155   "Kill the current buffer."
3156   (interactive)
3157   (when (or (not (buffer-modified-p))
3158             (yes-or-no-p "Message modified; kill anyway? "))
3159     (let ((actions message-kill-actions)
3160           (draft-article message-draft-article)
3161           (auto-save-file-name buffer-auto-save-file-name)
3162           (file-name buffer-file-name)
3163           (modified (buffer-modified-p)))
3164       (setq buffer-file-name nil)
3165       (kill-buffer (current-buffer))
3166       (when (and (or (and auto-save-file-name
3167                           (file-exists-p auto-save-file-name))
3168                      (and file-name
3169                           (file-exists-p file-name)))
3170                (yes-or-no-p (format "Remove the backup file%s? "
3171                                     (if modified " too" ""))))
3172         (ignore-errors
3173           (delete-file auto-save-file-name))
3174         (let ((message-draft-article draft-article))
3175           (message-disassociate-draft)))
3176       (message-do-actions actions))))
3177
3178 (defun message-bury (buffer)
3179   "Bury this mail BUFFER."
3180   (let ((newbuf (other-buffer buffer)))
3181     (bury-buffer buffer)
3182     (if (and (fboundp 'frame-parameters)
3183              (cdr (assq 'dedicated (frame-parameters)))
3184              (not (null (delq (selected-frame) (visible-frame-list)))))
3185         (delete-frame (selected-frame))
3186       (switch-to-buffer newbuf))))
3187
3188 (defun message-send (&optional arg)
3189   "Send the message in the current buffer.
3190 If `message-interactive' is non-nil, wait for success indication or
3191 error messages, and inform user.
3192 Otherwise any failure is reported in a message back to the user from
3193 the mailer.
3194 The usage of ARG is defined by the instance that called Message.
3195 It should typically alter the sending method in some way or other."
3196   (interactive "P")
3197   ;; Make it possible to undo the coming changes.
3198   (undo-boundary)
3199   (let ((inhibit-read-only t))
3200     (put-text-property (point-min) (point-max) 'read-only nil))
3201   (message-fix-before-sending)
3202   (run-hooks 'message-send-hook)
3203   (message message-sending-message)
3204   (let ((alist message-send-method-alist)
3205         (success t)
3206         elem sent dont-barf-on-no-method
3207         (message-options message-options))
3208     (message-options-set-recipient)
3209     (while (and success
3210                 (setq elem (pop alist)))
3211       (when (funcall (cadr elem))
3212         (when (and (or (not (memq (car elem)
3213                                   message-sent-message-via))
3214                        (not (message-fetch-field "supersedes"))
3215                        (if (or (message-gnksa-enable-p 'multiple-copies)
3216                                (not (eq (car elem) 'news)))
3217                            (y-or-n-p
3218                             (format
3219                              "Already sent message via %s; resend? "
3220                              (car elem)))
3221                          (error "Denied posting -- multiple copies")))
3222                    (setq success (funcall (caddr elem) arg)))
3223           (setq sent t))))
3224     (unless (or sent
3225                 (not success)
3226                 (let ((fcc (message-fetch-field "Fcc"))
3227                       (gcc (message-fetch-field "Gcc")))
3228                   (when (or fcc gcc)
3229                     (or (eq message-allow-no-recipients 'always)
3230                         (and (not (eq message-allow-no-recipients 'never))
3231                              (setq dont-barf-on-no-method
3232                                    (gnus-y-or-n-p
3233                                     (format "No receiver, perform %s anyway? "
3234                                             (cond ((and fcc gcc) "Fcc and Gcc")
3235                                                   (fcc "Fcc")
3236                                                   (t "Gcc"))))))))))
3237       (error "No methods specified to send by"))
3238     (when (or dont-barf-on-no-method
3239               (and success sent))
3240       (message-do-fcc)
3241       (save-excursion
3242         (run-hooks 'message-sent-hook))
3243       (message "Sending...done")
3244       ;; Mark the buffer as unmodified and delete auto-save.
3245       (set-buffer-modified-p nil)
3246       (delete-auto-save-file-if-necessary t)
3247       (message-disassociate-draft)
3248       ;; Delete other mail buffers and stuff.
3249       (message-do-send-housekeeping)
3250       (message-do-actions message-send-actions)
3251       ;; Return success.
3252       t)))
3253
3254 (defun message-send-via-mail (arg)
3255   "Send the current message via mail."
3256   (message-send-mail arg))
3257
3258 (defun message-send-via-news (arg)
3259   "Send the current message via news."
3260   (funcall message-send-news-function arg))
3261
3262 (defmacro message-check (type &rest forms)
3263   "Eval FORMS if TYPE is to be checked."
3264   `(or (message-check-element ,type)
3265        (save-excursion
3266          ,@forms)))
3267
3268 (put 'message-check 'lisp-indent-function 1)
3269 (put 'message-check 'edebug-form-spec '(form body))
3270
3271 (defun message-text-with-property (prop)
3272   "Return a list of all points where the text has PROP."
3273   (let ((points nil)
3274         (point (point-min)))
3275     (save-excursion
3276       (while (< point (point-max))
3277         (when (get-text-property point prop)
3278           (push point points))
3279         (incf point)))
3280     (nreverse points)))
3281
3282 (defun message-fix-before-sending ()
3283   "Do various things to make the message nice before sending it."
3284   ;; Make sure there's a newline at the end of the message.
3285   (goto-char (point-max))
3286   (unless (bolp)
3287     (insert "\n"))
3288   ;; Make the hidden headers visible.
3289   (let ((points (message-text-with-property 'message-hidden)))
3290     (when points
3291       (goto-char (car points))
3292       (dolist (point points)
3293         (add-text-properties point (1+ point)
3294                              '(invisible nil intangible nil)))))
3295   ;; Make invisible text visible.
3296   (message-check 'invisible-text
3297     (let ((points (message-text-with-property 'invisible)))
3298       (when points
3299         (goto-char (car points))
3300         (dolist (point points)
3301           (add-text-properties point (1+ point)
3302                                '(invisible nil face highlight
3303                                            font-lock-face highlight)))
3304         (unless (yes-or-no-p
3305                  "Invisible text found and made visible; continue posting? ")
3306           (error "Invisible text found and made visible")))))
3307   (message-check 'illegible-text
3308     (let (found choice)
3309       (message-goto-body)
3310       (skip-chars-forward mm-7bit-chars)
3311       (while (not (eobp))
3312         (when (let ((char (char-after)))
3313                 (or (< (mm-char-int char) 128)
3314                     (and (mm-multibyte-p)
3315                          (memq (char-charset char)
3316                                '(eight-bit-control eight-bit-graphic
3317                                                    control-1)))))
3318           (add-text-properties (point) (1+ (point))
3319                                '(font-lock-face highlight face highlight))
3320           (setq found t))
3321         (forward-char)
3322         (skip-chars-forward mm-7bit-chars))
3323       (when found
3324         (setq choice
3325               (gnus-multiple-choice
3326                "Illegible text found.  Continue posting?"
3327                '((?d "Remove and continue posting")
3328                  (?r "Replace with dots and continue posting")
3329                  (?i "Ignore and continue posting")
3330                  (?e "Continue editing"))))
3331         (if (eq choice ?e)
3332           (error "Illegible text found"))
3333         (message-goto-body)
3334         (skip-chars-forward mm-7bit-chars)
3335         (while (not (eobp))
3336           (when (let ((char (char-after)))
3337                   (or (< (mm-char-int char) 128)
3338                       (and (mm-multibyte-p)
3339                            (memq (char-charset char)
3340                                  '(eight-bit-control eight-bit-graphic
3341                                                      control-1)))))
3342             (if (eq choice ?i)
3343                 (remove-text-properties (point) (1+ (point))
3344                                         '(font-lock-face highlight face highlight))
3345               (delete-char 1)
3346               (when (eq choice ?r)
3347                 (insert "."))))
3348           (forward-char)
3349           (skip-chars-forward mm-7bit-chars))))))
3350
3351 (defun message-add-action (action &rest types)
3352   "Add ACTION to be performed when doing an exit of type TYPES."
3353   (while types
3354     (add-to-list (intern (format "message-%s-actions" (pop types)))
3355                  action)))
3356
3357 (defun message-delete-action (action &rest types)
3358   "Delete ACTION from lists of actions performed when doing an exit of type TYPES."
3359   (let (var)
3360     (while types
3361       (set (setq var (intern (format "message-%s-actions" (pop types))))
3362            (delq action (symbol-value var))))))
3363
3364 (defun message-do-actions (actions)
3365   "Perform all actions in ACTIONS."
3366   ;; Now perform actions on successful sending.
3367   (while actions
3368     (ignore-errors
3369       (cond
3370        ;; A simple function.
3371        ((message-functionp (car actions))
3372         (funcall (car actions)))
3373        ;; Something to be evaled.
3374        (t
3375         (eval (car actions)))))
3376     (pop actions)))
3377
3378 (defun message-send-mail-partially ()
3379   "Send mail as message/partial."
3380   ;; replace the header delimiter with a blank line
3381   (goto-char (point-min))
3382   (re-search-forward
3383    (concat "^" (regexp-quote mail-header-separator) "\n"))
3384   (replace-match "\n")
3385   (run-hooks 'message-send-mail-hook)
3386   (let ((p (goto-char (point-min)))
3387         (tembuf (message-generate-new-buffer-clone-locals " message temp"))
3388         (curbuf (current-buffer))
3389         (id (message-make-message-id)) (n 1)
3390         plist total  header required-mail-headers)
3391     (while (not (eobp))
3392       (if (< (point-max) (+ p message-send-mail-partially-limit))
3393           (goto-char (point-max))
3394         (goto-char (+ p message-send-mail-partially-limit))
3395         (beginning-of-line)
3396         (if (<= (point) p) (forward-line 1))) ;; In case of bad message.
3397       (push p plist)
3398       (setq p (point)))
3399     (setq total (length plist))
3400     (push (point-max) plist)
3401     (setq plist (nreverse plist))
3402     (unwind-protect
3403         (save-excursion
3404           (setq p (pop plist))
3405           (while plist
3406             (set-buffer curbuf)
3407             (copy-to-buffer tembuf p (car plist))
3408             (set-buffer tembuf)
3409             (goto-char (point-min))
3410             (if header
3411                 (progn
3412                   (goto-char (point-min))
3413                   (narrow-to-region (point) (point))
3414                   (insert header))
3415               (message-goto-eoh)
3416               (setq header (buffer-substring (point-min) (point)))
3417               (goto-char (point-min))
3418               (narrow-to-region (point) (point))
3419               (insert header)
3420               (message-remove-header "Mime-Version")
3421               (message-remove-header "Content-Type")
3422               (message-remove-header "Content-Transfer-Encoding")
3423               (message-remove-header "Message-ID")
3424               (message-remove-header "Lines")
3425               (goto-char (point-max))
3426               (insert "Mime-Version: 1.0\n")
3427               (setq header (buffer-string)))
3428             (goto-char (point-max))
3429             (insert (format "Content-Type: message/partial; id=\"%s\"; number=%d; total=%d\n\n"
3430                             id n total))
3431             (forward-char -1)
3432             (let ((mail-header-separator ""))
3433               (when (memq 'Message-ID message-required-mail-headers)
3434                 (insert "Message-ID: " (message-make-message-id) "\n"))
3435               (when (memq 'Lines message-required-mail-headers)
3436                 (insert "Lines: " (message-make-lines) "\n"))
3437               (message-goto-subject)
3438               (end-of-line)
3439               (insert (format " (%d/%d)" n total))
3440               (widen)
3441               (mm-with-unibyte-current-buffer
3442                 (funcall (or message-send-mail-real-function
3443                              message-send-mail-function))))
3444             (setq n (+ n 1))
3445             (setq p (pop plist))
3446             (erase-buffer)))
3447       (kill-buffer tembuf))))
3448
3449 (defun message-send-mail (&optional arg)
3450   (require 'mail-utils)
3451   (let* ((tembuf (message-generate-new-buffer-clone-locals " message temp"))
3452          (case-fold-search nil)
3453          (news (message-news-p))
3454          (mailbuf (current-buffer))
3455          (message-this-is-mail t)
3456          (message-posting-charset
3457           (if (fboundp 'gnus-setup-posting-charset)
3458               (gnus-setup-posting-charset nil)
3459             message-posting-charset))
3460          (headers message-required-mail-headers))
3461     (save-restriction
3462       (message-narrow-to-headers)
3463       ;; Generate the Mail-Followup-To header if the header is not there...
3464       (if (and (message-subscribed-p)
3465                (not (mail-fetch-field "mail-followup-to")))
3466           (setq headers
3467                 (cons
3468                  (cons "Mail-Followup-To" (message-make-mail-followup-to))
3469                  message-required-mail-headers))
3470         ;; otherwise, delete the MFT header if the field is empty
3471         (when (equal "" (mail-fetch-field "mail-followup-to"))
3472           (message-remove-header "^Mail-Followup-To:")))
3473       ;; Insert some headers.
3474       (let ((message-deletable-headers
3475              (if news nil message-deletable-headers)))
3476         (message-generate-headers headers))
3477       ;; Let the user do all of the above.
3478       (run-hooks 'message-header-hook))
3479     (unwind-protect
3480         (save-excursion
3481           (set-buffer tembuf)
3482           (erase-buffer)
3483           ;; Avoid copying text props (except hard newlines).
3484           (insert (with-current-buffer mailbuf
3485                     (mml-buffer-substring-no-properties-except-hard-newlines
3486                      (point-min) (point-max))))
3487           ;; Remove some headers.
3488           (message-encode-message-body)
3489           (save-restriction
3490             (message-narrow-to-headers)
3491             ;; We (re)generate the Lines header.
3492             (when (memq 'Lines message-required-mail-headers)
3493               (message-generate-headers '(Lines)))
3494             ;; Remove some headers.
3495             (message-remove-header message-ignored-mail-headers t)
3496             (let ((mail-parse-charset message-default-charset))
3497               (mail-encode-encoded-word-buffer)))
3498           (goto-char (point-max))
3499           ;; require one newline at the end.
3500           (or (= (preceding-char) ?\n)
3501               (insert ?\n))
3502           (message-cleanup-headers)
3503           ;; FIXME: we're inserting the courtesy copy after encoding.
3504           ;; This is wrong if the courtesy copy string contains
3505           ;; non-ASCII characters. -- jh
3506           (when
3507               (save-restriction
3508                 (message-narrow-to-headers)
3509                 (and news
3510                      (or (message-fetch-field "cc")
3511                          (message-fetch-field "bcc")
3512                          (message-fetch-field "to"))
3513                      (let ((content-type (message-fetch-field
3514                                           "content-type")))
3515                        (and
3516                         (or
3517                          (not content-type)
3518                          (string= "text/plain"
3519                                   (car 
3520                                    (mail-header-parse-content-type
3521                                     content-type))))
3522                         (not
3523                          (string= "base64"
3524                                   (message-fetch-field
3525                                    "content-transfer-encoding")))))))
3526             (message-insert-courtesy-copy))
3527           (if (or (not message-send-mail-partially-limit)
3528                   (< (point-max) message-send-mail-partially-limit)
3529                   (not (message-y-or-n-p
3530                         "The message size is too large, split? "
3531                         t
3532                         "\
3533 The message size, "
3534                         (/ (point-max) 1000) "KB, is too large.
3535
3536 Some mail gateways (MTA's) bounce large messages.  To avoid the
3537 problem, answer `y', and the message will be split into several
3538 smaller pieces, the size of each is about "
3539                         (/ message-send-mail-partially-limit 1000)
3540                         "KB except the last
3541 one.
3542
3543 However, some mail readers (MUA's) can't read split messages, i.e.,
3544 mails in message/partially format. Answer `n', and the message will be
3545 sent in one piece.
3546
3547 The size limit is controlled by `message-send-mail-partially-limit'.
3548 If you always want Gnus to send messages in one piece, set
3549 `message-send-mail-partially-limit' to nil.
3550 ")))
3551               (mm-with-unibyte-current-buffer
3552                 (message "Sending via mail...")
3553                 (funcall (or message-send-mail-real-function
3554                              message-send-mail-function)))
3555             (message-send-mail-partially)))
3556       (kill-buffer tembuf))
3557     (set-buffer mailbuf)
3558     (push 'mail message-sent-message-via)))
3559
3560 (defun message-send-mail-with-sendmail ()
3561   "Send off the prepared buffer with sendmail."
3562   (let ((errbuf (if message-interactive
3563                     (message-generate-new-buffer-clone-locals
3564                      " sendmail errors")
3565                   0))
3566         resend-to-addresses delimline)
3567     (unwind-protect
3568         (progn
3569           (let ((case-fold-search t))
3570             (save-restriction
3571               (message-narrow-to-headers)
3572               (setq resend-to-addresses (message-fetch-field "resent-to")))
3573             ;; Change header-delimiter to be what sendmail expects.
3574             (goto-char (point-min))
3575             (re-search-forward
3576              (concat "^" (regexp-quote mail-header-separator) "\n"))
3577             (replace-match "\n")
3578             (backward-char 1)
3579             (setq delimline (point-marker))
3580             (run-hooks 'message-send-mail-hook)
3581             ;; Insert an extra newline if we need it to work around
3582             ;; Sun's bug that swallows newlines.
3583             (goto-char (1+ delimline))
3584             (when (eval message-mailer-swallows-blank-line)
3585               (newline))
3586             (when message-interactive
3587               (save-excursion
3588                 (set-buffer errbuf)
3589                 (erase-buffer))))
3590           (let* ((default-directory "/")
3591                  (coding-system-for-write message-send-coding-system)
3592                  (cpr (apply
3593                        'call-process-region
3594                        (append
3595                         (list (point-min) (point-max)
3596                               (if (boundp 'sendmail-program)
3597                                   sendmail-program
3598                                 "/usr/lib/sendmail")
3599                               nil errbuf nil "-oi")
3600                         ;; Always specify who from,
3601                         ;; since some systems have broken sendmails.
3602                         ;; But some systems are more broken with -f, so
3603                         ;; we'll let users override this.
3604                         (if (null message-sendmail-f-is-evil)
3605                             (list "-f" (message-sendmail-envelope-from)))
3606                         ;; These mean "report errors by mail"
3607                         ;; and "deliver in background".
3608                         (if (null message-interactive) '("-oem" "-odb"))
3609                         ;; Get the addresses from the message
3610                         ;; unless this is a resend.
3611                         ;; We must not do that for a resend
3612                         ;; because we would find the original addresses.
3613                         ;; For a resend, include the specific addresses.
3614                         (if resend-to-addresses
3615                             (list resend-to-addresses)
3616                           '("-t"))))))
3617             (unless (or (null cpr) (zerop cpr))
3618               (error "Sending...failed with exit value %d" cpr)))
3619           (when message-interactive
3620             (save-excursion
3621               (set-buffer errbuf)
3622               (goto-char (point-min))
3623               (while (re-search-forward "\n\n* *" nil t)
3624                 (replace-match "; "))
3625               (if (not (zerop (buffer-size)))
3626                   (error "Sending...failed to %s"
3627                          (buffer-string))))))
3628       (when (bufferp errbuf)
3629         (kill-buffer errbuf)))))
3630
3631 (defun message-send-mail-with-qmail ()
3632   "Pass the prepared message buffer to qmail-inject.
3633 Refer to the documentation for the variable `message-send-mail-function'
3634 to find out how to use this."
3635   ;; replace the header delimiter with a blank line
3636   (goto-char (point-min))
3637   (re-search-forward
3638    (concat "^" (regexp-quote mail-header-separator) "\n"))
3639   (replace-match "\n")
3640   (run-hooks 'message-send-mail-hook)
3641   ;; send the message
3642   (case
3643       (let ((coding-system-for-write message-send-coding-system))
3644         (apply
3645          'call-process-region 1 (point-max) message-qmail-inject-program
3646          nil nil nil
3647          ;; qmail-inject's default behaviour is to look for addresses on the
3648          ;; command line; if there're none, it scans the headers.
3649          ;; yes, it does The Right Thing w.r.t. Resent-To and it's kin.
3650          ;;
3651          ;; in general, ALL of qmail-inject's defaults are perfect for simply
3652          ;; reading a formatted (i. e., at least a To: or Resent-To header)
3653          ;; message from stdin.
3654          ;;
3655          ;; qmail also has the advantage of not having been raped by
3656          ;; various vendors, so we don't have to allow for that, either --
3657          ;; compare this with message-send-mail-with-sendmail and weep
3658          ;; for sendmail's lost innocence.
3659          ;;
3660          ;; all this is way cool coz it lets us keep the arguments entirely
3661          ;; free for -inject-arguments -- a big win for the user and for us
3662          ;; since we don't have to play that double-guessing game and the user
3663          ;; gets full control (no gestapo'ish -f's, for instance).  --sj
3664          (if (message-functionp message-qmail-inject-args)
3665              (funcall message-qmail-inject-args)
3666            message-qmail-inject-args)))
3667     ;; qmail-inject doesn't say anything on it's stdout/stderr,
3668     ;; we have to look at the retval instead
3669     (0 nil)
3670     (100 (error "qmail-inject reported permanent failure"))
3671     (111 (error "qmail-inject reported transient failure"))
3672     ;; should never happen
3673     (t   (error "qmail-inject reported unknown failure"))))
3674
3675 (defun message-send-mail-with-mh ()
3676   "Send the prepared message buffer with mh."
3677   (let ((mh-previous-window-config nil)
3678         (name (mh-new-draft-name)))
3679     (setq buffer-file-name name)
3680     ;; MH wants to generate these headers itself.
3681     (when message-mh-deletable-headers
3682       (let ((headers message-mh-deletable-headers))
3683         (while headers
3684           (goto-char (point-min))
3685           (and (re-search-forward
3686                 (concat "^" (symbol-name (car headers)) ": *") nil t)
3687                (message-delete-line))
3688           (pop headers))))
3689     (run-hooks 'message-send-mail-hook)
3690     ;; Pass it on to mh.
3691     (mh-send-letter)))
3692
3693 (defun message-smtpmail-send-it ()
3694   "Send the prepared message buffer with `smtpmail-send-it'.
3695 This only differs from `smtpmail-send-it' that this command evaluates
3696 `message-send-mail-hook' just before sending a message.  It is useful
3697 if your ISP requires the POP-before-SMTP authentication.  See the
3698 documentation for the function `mail-source-touch-pop'."
3699   (run-hooks 'message-send-mail-hook)
3700   (smtpmail-send-it))
3701
3702 (defun message-canlock-generate ()
3703   "Return a string that is non-trival to guess.
3704 Do not use this for anything important, it is cryptographically weak."
3705   (let (sha1-maximum-internal-length)
3706     (sha1 (concat (message-unique-id)
3707                   (format "%x%x%x" (random) (random t) (random))
3708                   (prin1-to-string (recent-keys))
3709                   (prin1-to-string (garbage-collect))))))
3710
3711 (defun message-canlock-password ()
3712   "The password used by message for cancel locks.
3713 This is the value of `canlock-password', if that option is non-nil.
3714 Otherwise, generate and save a value for `canlock-password' first."
3715   (unless canlock-password
3716     (customize-save-variable 'canlock-password (message-canlock-generate))
3717     (setq canlock-password-for-verify canlock-password))
3718   canlock-password)
3719
3720 (defun message-insert-canlock ()
3721   (when message-insert-canlock
3722     (message-canlock-password)
3723     (canlock-insert-header)))
3724
3725 (defun message-send-news (&optional arg)
3726   (let* ((tembuf (message-generate-new-buffer-clone-locals " *message temp*"))
3727          (case-fold-search nil)
3728          (method (if (message-functionp message-post-method)
3729                      (funcall message-post-method arg)
3730                    message-post-method))
3731          (newsgroups-field (save-restriction
3732                             (message-narrow-to-headers-or-head)
3733                             (message-fetch-field "Newsgroups")))
3734          (followup-field (save-restriction
3735                            (message-narrow-to-headers-or-head)
3736                            (message-fetch-field "Followup-To")))
3737          ;; BUG: We really need to get the charset for each name in the
3738          ;; Newsgroups and Followup-To lines to allow crossposting
3739          ;; between group namess with incompatible character sets.
3740          ;; -- Per Abrahamsen <abraham@dina.kvl.dk> 2001-10-08.
3741          (group-field-charset
3742           (gnus-group-name-charset method newsgroups-field))
3743          (followup-field-charset
3744           (gnus-group-name-charset method (or followup-field "")))
3745          (rfc2047-header-encoding-alist
3746           (append (when group-field-charset
3747                     (list (cons "Newsgroups