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