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