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