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