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