Remove [] from the list of bogus characters.
[gnus] / contrib / sendmail.el
1 ;;; sendmail.el --- mail sending commands for Emacs.  -*- byte-compile-dynamic: t -*-
2
3 ;; Copyright (C) 1985, 1986, 1992, 1993, 1994, 1995, 1996, 1998, 2000,
4 ;;   2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
5 ;;   Free Software Foundation, Inc.
6
7 ;; Maintainer: FSF
8 ;; Keywords: mail
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
24
25 ;;; Commentary:
26
27 ;; This mode provides mail-sending facilities from within Emacs.  It is
28 ;; documented in the Emacs user's manual.
29
30 ;;; Code:
31 (eval-when-compile
32   ;; Necessary to avoid recursive `require's.
33   (provide 'sendmail)
34   (require 'rmail)
35   (require 'mailalias))
36
37 (autoload 'rfc2047-encode-string "rfc2047")
38
39 (defgroup sendmail nil
40   "Mail sending commands for Emacs."
41   :prefix "mail-"
42   :group 'mail)
43
44 (defcustom mail-setup-with-from t
45   "Non-nil means insert `From:' field when setting up the message."
46   :type 'boolean
47   :group 'sendmail
48   :version "22.1")
49
50 (defcustom sendmail-program
51   (cond
52     ((file-exists-p "/usr/sbin/sendmail") "/usr/sbin/sendmail")
53     ((file-exists-p "/usr/lib/sendmail") "/usr/lib/sendmail")
54     ((file-exists-p "/usr/ucblib/sendmail") "/usr/ucblib/sendmail")
55     (t "fakemail"))                     ;In ../etc, to interface to /bin/mail.
56   "Program used to send messages."
57   :group 'mail
58   :type 'file)
59
60 ;;;###autoload
61 (defcustom mail-from-style 'angles
62   "Specifies how \"From:\" fields look.
63
64 If `nil', they contain just the return address like:
65         king@grassland.com
66 If `parens', they look like:
67         king@grassland.com (Elvis Parsley)
68 If `angles', they look like:
69         Elvis Parsley <king@grassland.com>
70 If `system-default', allows the mailer to insert its default From field
71 derived from the envelope-from address.
72
73 In old versions of Emacs, the `system-default' setting also caused
74 Emacs to pass the proper email address from `user-mail-address'
75 to the mailer to specify the envelope-from address.  But that is now
76 controlled by a separate variable, `mail-specify-envelope-from'."
77   :type '(choice (const nil) (const parens) (const angles)
78                  (const system-default))
79   :version "20.3"
80   :group 'sendmail)
81
82 ;;;###autoload
83 (defcustom mail-specify-envelope-from nil
84   "If non-nil, specify the envelope-from address when sending mail.
85 The value used to specify it is whatever is found in
86 the variable `mail-envelope-from', with `user-mail-address' as fallback.
87
88 On most systems, specifying the envelope-from address is a
89 privileged operation.  This variable affects sendmail and
90 smtpmail -- if you use feedmail to send mail, see instead the
91 variable `feedmail-deduce-envelope-from'."
92   :version "21.1"
93   :type 'boolean
94   :group 'sendmail)
95
96 (defcustom mail-envelope-from nil
97   "If non-nil, designate the envelope-from address when sending mail.
98 This only has an effect if `mail-specify-envelope-from' is non-nil.
99 The value should be either a string, or the symbol `header' (in
100 which case the contents of the \"From\" header of the message
101 being sent is used), or nil (in which case the value of
102 `user-mail-address' is used)."
103   :version "21.1"
104   :type '(choice (string :tag "From-name")
105                  (const :tag "Use From: header from message" header)
106                  (const :tag "Use `user-mail-address'" nil))
107   :group 'sendmail)
108
109 ;;;###autoload
110 (defcustom mail-self-blind nil
111   "Non-nil means insert BCC to self in messages to be sent.
112 This is done when the message is initialized,
113 so you can remove or alter the BCC field to override the default."
114   :type 'boolean
115   :group 'sendmail)
116
117 ;;;###autoload
118 (defcustom mail-interactive t
119   ;; We used to use a default of nil rather than t, but nowadays it is very
120   ;; common for sendmail to be misconfigured, so one cannot rely on the
121   ;; bounce message to be delivered anywhere, least of all to the
122   ;; user's mailbox.
123   "Non-nil means when sending a message wait for and display errors.
124 Otherwise, let mailer send back a message to report errors."
125   :type 'boolean
126   :group 'sendmail)
127
128 (defcustom mail-yank-ignored-headers
129   (concat "^"
130           (regexp-opt '("via" "mail-from" "origin" "status" "remailed"
131                         "received" "message-id" "summary-line" "to" "subject"
132                         "in-reply-to" "return-path" "mail-reply-to"
133                         ;; Should really be rmail-attribute-header and
134                         ;; rmail-keyword-header, but this file does not
135                         ;; require rmail (at run time).
136                         "x-rmail-attributes" "x-rmail-keywords"
137                         "mail-followup-to") "\\(?:")
138           ":")
139   "Delete these headers from old message when it's inserted in a reply."
140   :type 'regexp
141   :group 'sendmail
142   :version "23.1")
143
144 ;; Prevent problems with `window-system' not having the correct value
145 ;; when loaddefs.el is loaded. `custom-reevaluate-setting' needs the
146 ;; standard value.
147 ;;;###autoload
148 (put 'send-mail-function 'standard-value
149      '((if (and window-system (memq system-type '(darwin windows-nt)))
150            'mailclient-send-it
151          'sendmail-send-it)))
152
153 ;; Useful to set in site-init.el
154 ;;;###autoload
155 (defcustom send-mail-function
156   (if (and window-system (memq system-type '(darwin windows-nt)))
157       'mailclient-send-it
158     'sendmail-send-it)
159   "Function to call to send the current buffer as mail.
160 The headers should be delimited by a line which is
161 not a valid RFC822 header or continuation line,
162 that matches the variable `mail-header-separator'.
163 This is used by the default mail-sending commands.  See also
164 `message-send-mail-function' for use with the Message package."
165   :type '(radio (function-item sendmail-send-it :tag "Use Sendmail package")
166                 (function-item smtpmail-send-it :tag "Use SMTPmail package")
167                 (function-item feedmail-send-it :tag "Use Feedmail package")
168                 (function-item mailclient-send-it :tag "Use Mailclient package")
169                 function)
170   :group 'sendmail)
171
172 ;;;###autoload
173 (defcustom mail-header-separator "--text follows this line--"
174   "Line used to separate headers from text in messages being composed."
175   :type 'string
176   :group 'sendmail)
177
178 ;; Set up mail-header-separator for use as a category text property.
179 (put 'mail-header-separator 'rear-nonsticky '(category))
180 ;; This was a nice idea, for preventing accidental modification of
181 ;; the separator.   But I found it also prevented or obstructed
182 ;; certain deliberate operations, such as copying the separator line
183 ;; up to the top to send myself a copy of an already sent outgoing message
184 ;; and other things.  So I turned it off.  --rms.
185 ;;(put 'mail-header-separator 'read-only t)
186
187 ;;;###autoload
188 (defcustom mail-archive-file-name nil
189   "Name of file to write all outgoing messages in, or nil for none.
190 This is normally an mbox file, but for backwards compatibility may also
191 be a Babyl file."
192   :type '(choice file (const nil))
193   :group 'sendmail)
194
195 ;;;###autoload
196 (defcustom mail-default-reply-to nil
197   "Address to insert as default Reply-to field of outgoing messages.
198 If nil, it will be initialized from the REPLYTO environment variable
199 when you first send mail."
200   :type '(choice (const nil) string)
201   :group 'sendmail)
202
203 ;;;###autoload
204 (defcustom mail-alias-file nil
205   "If non-nil, the name of a file to use instead of `/usr/lib/aliases'.
206 This file defines aliases to be expanded by the mailer; this is a different
207 feature from that of defining aliases in `.mailrc' to be expanded in Emacs.
208 This variable has no effect unless your system uses sendmail as its mailer."
209   :type '(choice (const nil) file)
210   :group 'sendmail)
211
212 ;;;###autoload
213 (defcustom mail-personal-alias-file "~/.mailrc"
214   "If non-nil, the name of the user's personal mail alias file.
215 This file typically should be in same format as the `.mailrc' file used by
216 the `Mail' or `mailx' program.
217 This file need not actually exist."
218   :type '(choice (const nil) file)
219   :group 'sendmail)
220
221 ;;;###autoload
222 (defcustom mail-setup-hook nil
223   "Normal hook, run each time a new outgoing mail message is initialized.
224 The function `mail-setup' runs this hook."
225   :type 'hook
226   :options '(fortune-to-signature spook mail-abbrevs-setup)
227   :group 'sendmail)
228
229 ;;;###autoload
230 (defvar mail-aliases t
231   "Alist of mail address aliases,
232 or t meaning should be initialized from your mail aliases file.
233 \(The file's name is normally `~/.mailrc', but `mail-personal-alias-file'
234 can specify a different file name.)
235 The alias definitions in the file have this form:
236     alias ALIAS MEANING")
237
238 (defvar mail-alias-modtime nil
239   "The modification time of your mail alias file when it was last examined.")
240
241 ;;;###autoload
242 (defcustom mail-yank-prefix nil
243   "Prefix insert on lines of yanked message being replied to.
244 If this is nil, use indentation, as specified by `mail-indentation-spaces'."
245   :type '(choice (const nil) string)
246   :group 'sendmail)
247
248 ;;;###autoload
249 (defcustom mail-indentation-spaces 3
250   "Number of spaces to insert at the beginning of each cited line.
251 Used by `mail-yank-original' via `mail-indent-citation'."
252   :type 'integer
253   :group 'sendmail)
254
255 ;; FIXME make it really obsolete.
256 (defvar mail-yank-hooks nil
257   "Obsolete hook for modifying a citation just inserted in the mail buffer.
258 Each hook function can find the citation between (point) and (mark t).
259 And each hook function should leave point and mark around the citation
260 text as modified.
261
262 This is a normal hook, misnamed for historical reasons.
263 It is semi-obsolete and mail agents should no longer use it.")
264
265 ;;;###autoload
266 (defcustom mail-citation-hook nil
267   "Hook for modifying a citation just inserted in the mail buffer.
268 Each hook function can find the citation between (point) and (mark t),
269 and should leave point and mark around the citation text as modified.
270 The hook functions can find the header of the cited message
271 in the variable `mail-citation-header', whether or not this is included
272 in the cited portion of the message.
273
274 If this hook is entirely empty (nil), a default action is taken
275 instead of no action."
276   :type 'hook
277   :group 'sendmail)
278
279 (defvar mail-citation-header nil
280   "While running `mail-citation-hook', this variable holds the message header.
281 This enables the hook functions to see the whole message header
282 regardless of what part of it (if any) is included in the cited text.")
283
284 ;;;###autoload
285 (defcustom mail-citation-prefix-regexp "[ \t]*[-a-z0-9A-Z]*>+[ \t]*\\|[ \t]*"
286   "Regular expression to match a citation prefix plus whitespace.
287 It should match whatever sort of citation prefixes you want to handle,
288 with whitespace before and after; it should also match just whitespace.
289 The default value matches citations like `foo-bar>' plus whitespace."
290   :type 'regexp
291   :group 'sendmail
292   :version "20.3")
293
294 (defvar mail-abbrevs-loaded nil)
295 (defvar mail-mode-map
296   (let ((map (make-sparse-keymap)))
297     (define-key map "\M-\t" 'mail-complete)
298     (define-key map "\C-c?" 'describe-mode)
299     (define-key map "\C-c\C-f\C-t" 'mail-to)
300     (define-key map "\C-c\C-f\C-b" 'mail-bcc)
301     (define-key map "\C-c\C-f\C-f" 'mail-fcc)
302     (define-key map "\C-c\C-f\C-c" 'mail-cc)
303     (define-key map "\C-c\C-f\C-s" 'mail-subject)
304     (define-key map "\C-c\C-f\C-r" 'mail-reply-to)
305     (define-key map "\C-c\C-f\C-a" 'mail-mail-reply-to)    ; author
306     (define-key map "\C-c\C-f\C-l" 'mail-mail-followup-to) ; list
307     (define-key map "\C-c\C-t" 'mail-text)
308     (define-key map "\C-c\C-y" 'mail-yank-original)
309     (define-key map "\C-c\C-r" 'mail-yank-region)
310     (define-key map [remap split-line] 'mail-split-line)
311     (define-key map "\C-c\C-q" 'mail-fill-yanked-message)
312     (define-key map "\C-c\C-w" 'mail-signature)
313     (define-key map "\C-c\C-v" 'mail-sent-via)
314     (define-key map "\C-c\C-c" 'mail-send-and-exit)
315     (define-key map "\C-c\C-s" 'mail-send)
316     (define-key map "\C-c\C-i" 'mail-attach-file)
317     ;; FIXME add this? "b" = bury buffer.  It's in the menu-bar.
318 ;;;    (define-key map "\C-c\C-b" 'mail-dont-send)
319
320     (define-key map [menu-bar mail]
321       (cons "Mail" (make-sparse-keymap "Mail")))
322
323     (define-key map [menu-bar mail fill]
324       '("Fill Citation" . mail-fill-yanked-message))
325
326     (define-key map [menu-bar mail yank]
327       '(menu-item "Cite Original" mail-yank-original :enable mail-reply-action))
328
329     (define-key map [menu-bar mail signature]
330       '("Insert Signature" . mail-signature))
331
332     (define-key map [menu-bar mail mail-sep]
333       '("--"))
334
335     (define-key map [menu-bar mail cancel]
336       '("Cancel" . mail-dont-send))
337
338     (define-key map [menu-bar mail send-stay]
339       '("Send, Keep Editing" . mail-send))
340
341     (define-key map [menu-bar mail send]
342       '("Send Message" . mail-send-and-exit))
343
344     (define-key map [menu-bar headers]
345       (cons "Headers" (make-sparse-keymap "Move to Header")))
346
347     (define-key map [menu-bar headers text]
348       '("Text" . mail-text))
349
350     (define-key map [menu-bar headers expand-aliases]
351       '("Expand Aliases" . expand-mail-aliases))
352
353     (define-key map [menu-bar headers sent-via]
354       '("Sent-Via" . mail-sent-via))
355
356     (define-key map [menu-bar headers mail-reply-to]
357       '("Mail-Reply-To" . mail-mail-reply-to))
358
359     (define-key map [menu-bar headers mail-followup-to]
360       '("Mail-Followup-To" . mail-mail-followup-to))
361
362     (define-key map [menu-bar headers reply-to]
363       '("Reply-To" . mail-reply-to))
364
365     (define-key map [menu-bar headers bcc]
366       '("Bcc" . mail-bcc))
367
368     (define-key map [menu-bar headers fcc]
369       '("Fcc" . mail-fcc))
370
371     (define-key map [menu-bar headers cc]
372       '("Cc" . mail-cc))
373
374     (define-key map [menu-bar headers subject]
375       '("Subject" . mail-subject))
376
377     (define-key map [menu-bar headers to]
378       '("To" . mail-to))
379
380     map))
381
382 (autoload 'build-mail-aliases "mailalias"
383   "Read mail aliases from user's personal aliases file and set `mail-aliases'."
384   nil)
385
386 (autoload 'expand-mail-aliases "mailalias"
387   "Expand all mail aliases in suitable header fields found between BEG and END.
388 Suitable header fields are `To', `Cc' and `Bcc' and their `Resent-' variants.
389 Optional second arg EXCLUDE may be a regular expression defining text to be
390 removed from alias expansions."
391   nil)
392
393 ;;;###autoload
394 (defcustom mail-signature nil
395   "Text inserted at end of mail buffer when a message is initialized.
396 If t, it means to insert the contents of the file `mail-signature-file'.
397 If a string, that string is inserted.
398  (To make a proper signature, the string should begin with \\n\\n-- \\n,
399   which is the standard way to delimit a signature in a message.)
400 Otherwise, it should be an expression; it is evaluated
401 and should insert whatever you want to insert."
402   :type '(choice (const :tag "None" nil)
403                  (const :tag "Use `.signature' file" t)
404                  (string :tag "String to insert")
405                  (sexp :tag "Expression to evaluate"))
406   :group 'sendmail)
407 (put 'mail-signature 'risky-local-variable t)
408
409 ;;;###autoload
410 (defcustom mail-signature-file "~/.signature"
411   "File containing the text inserted at end of mail buffer."
412   :type 'file
413   :group 'sendmail)
414
415 ;;;###autoload
416 (defcustom mail-default-directory "~/"
417   "Directory for mail buffers.
418 Value of `default-directory' for mail buffers.
419 This directory is used for auto-save files of mail buffers."
420   :type '(directory :tag "Directory")
421   :group 'sendmail
422   :version "22.1")
423
424 (defvar mail-reply-action nil)
425 (defvar mail-send-actions nil
426   "A list of actions to be performed upon successful sending of a message.")
427 (put 'mail-reply-action 'permanent-local t)
428 (put 'mail-send-actions 'permanent-local t)
429
430 ;;;###autoload
431 (defcustom mail-default-headers nil
432   "A string containing header lines, to be inserted in outgoing messages.
433 It can contain newlines, and should end in one.  It is inserted
434 before you edit the message, so you can edit or delete the lines."
435   :type '(choice (const nil) string)
436   :group 'sendmail)
437
438 ;; FIXME no need for autoload
439 ;;;###autoload
440 (defcustom mail-bury-selects-summary t
441   "If non-nil, try to show Rmail summary buffer after returning from mail.
442 The functions \\[mail-send-on-exit] or \\[mail-dont-send] select
443 the Rmail summary buffer before returning, if it exists and this variable
444 is non-nil."
445   :type 'boolean
446   :group 'sendmail)
447
448 ;; FIXME no need for autoload
449 ;;;###autoload
450 (defcustom mail-send-nonascii 'mime
451   "Specify whether to allow sending non-ASCII characters in mail.
452 If t, that means do allow it.  nil means don't allow it.
453 `query' means ask the user each time.
454 `mime' means add an appropriate MIME header if none already present.
455 The default is `mime'.
456 Including non-ASCII characters in a mail message can be problematical
457 for the recipient, who may not know how to decode them properly."
458   :type '(choice (const t) (const nil) (const query) (const mime))
459   :group 'sendmail)
460
461 (defcustom mail-use-dsn nil
462   "Ask MTA for notification of failed, delayed or successful delivery.
463 Note that only some MTAs (currently only recent versions of Sendmail)
464 support Delivery Status Notification."
465   :group 'sendmail
466   :type '(repeat (radio (const :tag "Failure" failure)
467                         (const :tag "Delay" delay)
468                         (const :tag "Success" success)))
469   :version "22.1")
470
471 ;; Note: could use /usr/ucb/mail instead of sendmail;
472 ;; options -t, and -v if not interactive.
473 (defvar mail-mailer-swallows-blank-line
474   (if (and (string-match "sparc-sun-sunos\\(\\'\\|[^5]\\)" system-configuration)
475            (file-readable-p "/etc/sendmail.cf")
476            (with-temp-buffer
477              (insert-file-contents "/etc/sendmail.cf")
478              (goto-char (point-min))
479              (let ((case-fold-search nil))
480                (re-search-forward "^OR\\>" nil t))))
481       ;; According to RFC822, "The field-name must be composed of printable
482       ;; ASCII characters (i.e. characters that have decimal values between
483       ;; 33 and 126, except colon)", i.e. any chars except ctl chars,
484       ;; space, or colon.
485       '(looking-at "[ \t]\\|[][!\"#$%&'()*+,-./0-9;<=>?@A-Z\\\\^_`a-z{|}~]+:"))
486   "Set this non-nil if the system's mailer runs the header and body together.
487 \(This problem exists on Sunos 4 when sendmail is run in remote mode.)
488 The value should be an expression to test whether the problem will
489 actually occur.")
490
491 (defvar mail-mode-syntax-table
492   ;; define-derived-mode will make it inherit from text-mode-syntax-table.
493   (let ((st (make-syntax-table)))
494     ;; FIXME this is probably very obsolete now ("percent hack").
495     ;; sending.texi used to say:
496     ;;   Mail mode defines the character `%' as a word separator; this
497     ;;   is helpful for using the word commands to edit mail addresses.
498     (modify-syntax-entry ?% ". " st)
499     st)
500   "Syntax table used while in `mail-mode'.")
501
502 (defvar mail-font-lock-keywords
503   (eval-when-compile
504     (let* ((cite-chars "[>|}]")
505            (cite-prefix "[:alpha:]")
506            (cite-suffix (concat cite-prefix "0-9_.@-`'\"")))
507       (list '("^\\(To\\|Newsgroups\\):" . font-lock-function-name-face)
508             '("^\\(B?CC\\|Reply-to\\|Mail-\\(reply\\|followup\\)-to\\):" . font-lock-keyword-face)
509             '("^\\(Subject:\\)[ \t]*\\(.+\\)?"
510               (1 font-lock-comment-face)
511 ;;            (2 font-lock-type-face nil t)
512               )
513             ;; Use EVAL to delay in case `mail-header-separator' gets changed.
514             '(eval .
515               (let ((separator (if (zerop (length mail-header-separator))
516                                    " \\`\\' "
517                                  (regexp-quote mail-header-separator))))
518                 (cons (concat "^" separator "$") 'font-lock-warning-face)))
519             ;; Use MATCH-ANCHORED to effectively anchor the regexp left side.
520             `(,cite-chars
521               (,(concat "\\=[ \t]*"
522                         "\\(\\(\\([" cite-prefix "]+[" cite-suffix "]*\\)?"
523                         "\\(" cite-chars "[ \t]*\\)\\)+\\)"
524                         "\\(.*\\)")
525                (beginning-of-line) (end-of-line)
526                (1 font-lock-comment-delimiter-face nil t)
527                (5 font-lock-comment-face nil t)))
528             '("^\\(X-[A-Za-z0-9-]+\\|In-reply-to\\):.*\\(\n[ \t]+.*\\)*$"
529               . font-lock-string-face))))
530   "Additional expressions to highlight in Mail mode.")
531
532 \f
533 (defun sendmail-sync-aliases ()
534   (when mail-personal-alias-file
535     (let ((modtime (nth 5 (file-attributes mail-personal-alias-file))))
536       (or (equal mail-alias-modtime modtime)
537           (setq mail-alias-modtime modtime
538                 mail-aliases t)))))
539
540 (defun mail-setup (to subject in-reply-to cc replybuffer actions)
541   (or mail-default-reply-to
542       (setq mail-default-reply-to (getenv "REPLYTO")))
543   (sendmail-sync-aliases)
544   (if (eq mail-aliases t)
545       (progn
546         (setq mail-aliases nil)
547         (when mail-personal-alias-file
548           (if (file-exists-p mail-personal-alias-file)
549               (build-mail-aliases)))))
550   ;; Don't leave this around from a previous message.
551   (kill-local-variable 'buffer-file-coding-system)
552   ;; This doesn't work for enable-multibyte-characters.
553   ;; (kill-local-variable 'enable-multibyte-characters)
554   (set-buffer-multibyte default-enable-multibyte-characters)
555   (if current-input-method
556       (inactivate-input-method))
557   (setq mail-send-actions actions)
558   (setq mail-reply-action replybuffer)
559   (goto-char (point-min))
560   (if mail-setup-with-from
561       (mail-insert-from-field))
562   (insert "To: ")
563   (save-excursion
564     (if to
565         ;; Here removed code to extract names from within <...>
566         ;; on the assumption that mail-strip-quoted-names
567         ;; has been called and has done so.
568         (let ((fill-prefix "\t")
569               (address-start (point)))
570           (insert to "\n")
571           (fill-region-as-paragraph address-start (point-max))
572           (goto-char (point-max))
573           (unless (bolp)
574             (newline)))
575       (newline))
576     (if cc
577         (let ((fill-prefix "\t")
578               (address-start (progn (insert "CC: ") (point))))
579           (insert cc "\n")
580           (fill-region-as-paragraph address-start (point-max))
581           (goto-char (point-max))
582           (unless (bolp)
583             (newline))))
584     (if in-reply-to
585         (let ((fill-prefix "\t")
586               (fill-column 78)
587               (address-start (point)))
588           (insert "In-reply-to: " in-reply-to "\n")
589           (fill-region-as-paragraph address-start (point-max))
590           (goto-char (point-max))
591           (unless (bolp)
592             (newline))))
593     (insert "Subject: " (or subject "") "\n")
594     (if mail-default-headers
595         (insert mail-default-headers))
596     (if mail-default-reply-to
597         (insert "Reply-to: " mail-default-reply-to "\n"))
598     (if mail-self-blind
599         (insert "BCC: " user-mail-address "\n"))
600     (if mail-archive-file-name
601         (insert "FCC: " mail-archive-file-name "\n"))
602     (put-text-property (point)
603                        (progn
604                          (insert mail-header-separator "\n")
605                          (1- (point)))
606                        'category 'mail-header-separator)
607     ;; Insert the signature.  But remember the beginning of the message.
608     (if to (setq to (point)))
609     (if mail-signature (mail-signature t))
610     (goto-char (point-max))
611     (or (bolp) (newline)))
612   (if to (goto-char to))
613   (or to subject in-reply-to
614       (set-buffer-modified-p nil))
615   (run-hooks 'mail-setup-hook))
616 \f
617 (defcustom mail-mode-hook nil
618   "Hook run by Mail mode.
619 When composing a mail, this runs immediately after creating, or
620 switching to, the `*mail*' buffer.  See also `mail-setup-hook'."
621   :group 'sendmail
622   :type 'hook
623   :options '(footnote-mode))
624
625 (defvar mail-mode-abbrev-table text-mode-abbrev-table)
626 ;;;###autoload
627 (define-derived-mode mail-mode text-mode "Mail"
628   "Major mode for editing mail to be sent.
629 Like Text Mode but with these additional commands:
630
631 \\[mail-send]  mail-send (send the message)
632 \\[mail-send-and-exit]  mail-send-and-exit (send the message and exit)
633
634 Here are commands that move to a header field (and create it if there isn't):
635          \\[mail-to]  move to To:       \\[mail-subject]  move to Subj:
636          \\[mail-bcc]  move to BCC:     \\[mail-cc]  move to CC:
637          \\[mail-fcc]  move to FCC:     \\[mail-reply-to] move to Reply-To:
638          \\[mail-mail-reply-to]  move to Mail-Reply-To:
639          \\[mail-mail-followup-to] move to Mail-Followup-To:
640 \\[mail-text]  move to message text.
641 \\[mail-signature]  mail-signature (insert `mail-signature-file' file).
642 \\[mail-yank-original]  mail-yank-original (insert current message, in Rmail).
643 \\[mail-fill-yanked-message]  mail-fill-yanked-message (fill what was yanked).
644 \\[mail-sent-via]  mail-sent-via (add a sent-via field for each To or CC).
645 Turning on Mail mode runs the normal hooks `text-mode-hook' and
646 `mail-mode-hook' (in that order)."
647   (make-local-variable 'mail-reply-action)
648   (make-local-variable 'mail-send-actions)
649   (setq buffer-offer-save t)
650   (make-local-variable 'font-lock-defaults)
651   (setq font-lock-defaults '(mail-font-lock-keywords t t))
652   (make-local-variable 'paragraph-separate)
653   (make-local-variable 'normal-auto-fill-function)
654   (setq normal-auto-fill-function 'mail-mode-auto-fill)
655   (make-local-variable 'fill-paragraph-function)
656   (setq fill-paragraph-function 'mail-mode-fill-paragraph)
657   ;; Allow using comment commands to add/remove quoting (this only does
658   ;; anything if mail-yank-prefix is set to a non-nil value).
659   (set (make-local-variable 'comment-start) mail-yank-prefix)
660   (if mail-yank-prefix
661       (set (make-local-variable 'comment-start-skip)
662            (concat "^" (regexp-quote mail-yank-prefix) "[ \t]*")))
663   (make-local-variable 'adaptive-fill-regexp)
664   (setq adaptive-fill-regexp
665         (concat "[ \t]*[-[:alnum:]]+>+[ \t]*\\|"
666                 adaptive-fill-regexp))
667   (make-local-variable 'adaptive-fill-first-line-regexp)
668   (setq adaptive-fill-first-line-regexp
669         (concat "[ \t]*[-[:alnum:]]*>+[ \t]*\\|"
670                 adaptive-fill-first-line-regexp))
671   ;; `-- ' precedes the signature.  `-----' appears at the start of the
672   ;; lines that delimit forwarded messages.
673   ;; Lines containing just >= 3 dashes, perhaps after whitespace,
674   ;; are also sometimes used and should be separators.
675   (setq paragraph-separate (concat (regexp-quote mail-header-separator)
676                                 "$\\|\t*\\([-|#;>* ]\\|(?[0-9]+[.)]\\)+$"
677                                 "\\|[ \t]*[[:alnum:]]*>+[ \t]*$\\|[ \t]*$\\|"
678                                 "--\\( \\|-+\\)$\\|"
679                                 page-delimiter)))
680
681
682 (defun mail-header-end ()
683   "Return the buffer location of the end of headers, as a number."
684   (save-restriction
685     (widen)
686     (save-excursion
687       (rfc822-goto-eoh)
688       (point))))
689
690 (defun mail-text-start ()
691   "Return the buffer location of the start of text, as a number."
692   (save-restriction
693     (widen)
694     (save-excursion
695       (rfc822-goto-eoh)
696       (forward-line 1)
697       (point))))
698
699 (defun mail-sendmail-delimit-header ()
700   "Set up whatever header delimiter convention sendmail will use.
701 Concretely: replace the first blank line in the header with the separator."
702   (rfc822-goto-eoh)
703   (insert mail-header-separator)
704   (point))
705
706 (defun mail-sendmail-undelimit-header ()
707   "Remove header separator to put the message in correct form for sendmail.
708 Leave point at the start of the delimiter line."
709   (rfc822-goto-eoh)
710   (delete-region (point) (progn (end-of-line) (point))))
711
712 (defun mail-mode-auto-fill ()
713   "Carry out Auto Fill for Mail mode.
714 If within the headers, this makes the new lines into continuation lines."
715   (if (< (point) (mail-header-end))
716       (let ((old-line-start (save-excursion (beginning-of-line) (point))))
717         (if (do-auto-fill)
718             (save-excursion
719               (beginning-of-line)
720               (while (not (eq (point) old-line-start))
721                 ;; Use insert-before-markers in case we're inserting
722                 ;; before the saved value of point (which is common).
723                 (insert-before-markers "   ")
724                 (forward-line -1))
725               t)))
726     (do-auto-fill)))
727
728 (defun mail-mode-fill-paragraph (arg)
729   ;; Do something special only if within the headers.
730   (if (< (point) (mail-header-end))
731       (let (beg end fieldname)
732         (when (prog1 (re-search-backward "^[-a-zA-Z]+:" nil 'yes)
733                 (setq beg (point)))
734         (setq fieldname
735                 (downcase (buffer-substring beg (1- (match-end 0))))))
736         (forward-line 1)
737         ;; Find continuation lines and get rid of their continuation markers.
738         (while (looking-at "[ \t]")
739           (delete-horizontal-space)
740           (forward-line 1))
741         (setq end (point-marker))
742         (goto-char beg)
743         ;; If this field contains addresses,
744         ;; make sure we can fill after each address.
745         (if (member fieldname
746                     '("to" "cc" "bcc" "from" "reply-to"
747                       "mail-reply-to" "mail-followup-to"
748                       "resent-to" "resent-cc" "resent-bcc"
749                       "resent-from" "resent-reply-to"))
750             (while (search-forward "," end t)
751               (or (looking-at "[ \t]")
752                   (insert " "))))
753         (fill-region-as-paragraph beg end arg)
754         ;; Mark all lines except the first as continuations.
755         (goto-char beg)
756         (forward-line 1)
757         (while (< (point) end)
758           (insert "  ")
759           (forward-line 1))
760         (move-marker end nil)
761         t)))
762 \f
763 ;; User-level commands for sending.
764
765 (defun mail-send-and-exit (&optional arg)
766   "Send message like `mail-send', then, if no errors, exit from mail buffer.
767 Prefix arg means don't delete this window."
768   (interactive "P")
769   (mail-send)
770   (mail-bury arg))
771
772 (defun mail-dont-send (&optional arg)
773   "Don't send the message you have been editing.
774 Prefix arg means don't delete this window."
775   (interactive "P")
776   (mail-bury arg))
777
778 (defun mail-bury (&optional arg)
779   "Bury this mail buffer."
780   (let ((newbuf (other-buffer (current-buffer))))
781     (bury-buffer (current-buffer))
782     (if (and (or nil
783                  ;; In this case, we need to go to a different frame.
784                  (window-dedicated-p (frame-selected-window))
785                  ;; In this mode of operation, the frame was probably
786                  ;; made for this buffer, so the user probably wants
787                  ;; to delete it now.
788                  (and pop-up-frames (one-window-p))
789                  (cdr (assq 'mail-dedicated-frame (frame-parameters))))
790              (not (null (delq (selected-frame) (visible-frame-list)))))
791         (progn
792           (if (display-multi-frame-p)
793               (delete-frame (selected-frame))
794             ;; The previous frame is where normally they have the
795             ;; Rmail buffer displayed.
796             (other-frame -1)))
797       (let (rmail-flag summary-buffer)
798         (and (not arg)
799              (not (one-window-p))
800              (with-current-buffer
801                  (window-buffer (next-window (selected-window) 'not))
802                (setq rmail-flag (eq major-mode 'rmail-mode))
803                (setq summary-buffer
804                      (and mail-bury-selects-summary
805                           (boundp 'rmail-summary-buffer)
806                           rmail-summary-buffer
807                           (buffer-name rmail-summary-buffer)
808                           (not (get-buffer-window rmail-summary-buffer))
809                           rmail-summary-buffer))))
810         (if rmail-flag
811             ;; If the Rmail buffer has a summary, show that.
812             (if summary-buffer (switch-to-buffer summary-buffer)
813               (delete-window))
814           (switch-to-buffer newbuf))))))
815
816 (defcustom mail-send-hook nil
817   "Hook run just before sending mail with `mail-send'."
818   :type 'hook
819   :options '(flyspell-mode-off)
820   :group 'sendmail)
821
822 ;;;###autoload
823 (defcustom mail-mailing-lists nil
824 "List of mailing list addresses the user is subscribed to.
825 The variable is used to trigger insertion of the \"Mail-Followup-To\"
826 header when sending a message to a mailing list."
827   :type '(repeat string)
828   :group 'sendmail)
829
830
831 (defun mail-send ()
832   "Send the message in the current buffer.
833 If `mail-interactive' is non-nil, wait for success indication
834 or error messages, and inform user.
835 Otherwise any failure is reported in a message back to
836 the user from the mailer."
837   (interactive)
838   (if (if buffer-file-name
839           (y-or-n-p "Send buffer contents as mail message? ")
840         (or (buffer-modified-p)
841             (y-or-n-p "Message already sent; resend? ")))
842       (let ((inhibit-read-only t)
843             (opoint (point))
844             (ml (when mail-mailing-lists
845                 ;; The surrounding regexp assumes the use of