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