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