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