Import No Gnus v0.4.
[elisp/gnus.git-] / 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 Free Software Foundation, Inc.
5
6 ;; Maintainer: FSF
7 ;; Keywords: mail
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
25
26 ;;; Commentary:
27
28 ;; This mode provides mail-sending facilities from within Emacs.  It is
29 ;; documented in the Emacs user's manual.
30
31 ;;; Code:
32 (eval-when-compile
33   ;; Necessary to avoid recursive `require's.
34   (provide 'sendmail)
35   (require 'rmail)
36   (require 'mailalias))
37
38 (autoload 'rfc2047-encode-string "rfc2047")
39
40 (defgroup sendmail nil
41   "Mail sending commands for Emacs."
42   :prefix "mail-"
43   :group 'mail)
44
45 (defcustom mail-setup-with-from t
46   "Non-nil means insert `From:' field when setting up the message."
47   :type 'boolean
48   :group 'sendmail
49   :version "22.1")
50
51 ;;;###autoload
52 (defcustom mail-from-style 'angles
53   "Specifies how \"From:\" fields look.
54
55 If `nil', they contain just the return address like:
56         king@grassland.com
57 If `parens', they look like:
58         king@grassland.com (Elvis Parsley)
59 If `angles', they look like:
60         Elvis Parsley <king@grassland.com>
61 If `system-default', allows the mailer to insert its default From field
62 derived from the envelope-from address.
63
64 In old versions of Emacs, the `system-default' setting also caused
65 Emacs to pass the proper email address from `user-mail-address'
66 to the mailer to specify the envelope-from address.  But that is now
67 controlled by a separate variable, `mail-specify-envelope-from'."
68   :type '(choice (const nil) (const parens) (const angles)
69                  (const system-default))
70   :version "20.3"
71   :group 'sendmail)
72
73 ;;;###autoload
74 (defcustom mail-specify-envelope-from nil
75   "If non-nil, specify the envelope-from address when sending mail.
76 The value used to specify it is whatever is found in
77 the variable `mail-envelope-from', with `user-mail-address' as fallback.
78
79 On most systems, specifying the envelope-from address is a
80 privileged operation.  This variable affects sendmail and
81 smtpmail -- if you use feedmail to send mail, see instead the
82 variable `feedmail-deduce-envelope-from'."
83   :version "21.1"
84   :type 'boolean
85   :group 'sendmail)
86
87 (defcustom mail-envelope-from nil
88   "If non-nil, designate the envelope-from address when sending mail.
89 This only has an effect if `mail-specify-envelope-from' is non-nil.
90 The value should be either a string, or the symbol `header' (in
91 which case the contents of the \"From\" header of the message
92 being sent is used), or nil (in which case the value of
93 `user-mail-address' is used)."
94   :version "21.1"
95   :type '(choice (string :tag "From-name")
96                  (const :tag "Use From: header from message" header)
97                  (const :tag "Use `user-mail-address'" nil))
98   :group 'sendmail)
99
100 ;;;###autoload
101 (defcustom mail-self-blind nil
102   "Non-nil means insert BCC to self in messages to be sent.
103 This is done when the message is initialized,
104 so you can remove or alter the BCC field to override the default."
105   :type 'boolean
106   :group 'sendmail)
107
108 ;;;###autoload
109 (defcustom mail-interactive nil
110   "Non-nil means when sending a message wait for and display errors.
111 nil means let mailer mail back a message to report errors."
112   :type 'boolean
113   :group 'sendmail)
114
115 (defcustom mail-yank-ignored-headers
116   (concat "^"
117           (regexp-opt '("via" "mail-from" "origin" "status" "remailed"
118                         "received" "message-id" "summary-line" "to" "subject"
119                         "in-reply-to" "return-path" "mail-reply-to"
120                         "mail-followup-to") "\\(?:")
121           ":")
122   "Delete these headers from old message when it's inserted in a reply."
123   :type 'regexp
124   :group 'sendmail)
125
126 ;; Prevent problems with `window-system' not having the correct value
127 ;; when loaddefs.el is loaded. `custom-reevaluate-setting' needs the
128 ;; standard value.
129 ;;;###autoload
130 (put 'send-mail-function 'standard-value
131      '((if (and window-system (memq system-type '(darwin windows-nt)))
132            'mailclient-send-it
133          'sendmail-send-it)))
134
135 ;; Useful to set in site-init.el
136 ;;;###autoload
137 (defcustom send-mail-function
138   (if (and window-system (memq system-type '(darwin windows-nt)))
139       'mailclient-send-it
140     'sendmail-send-it)
141   "Function to call to send the current buffer as mail.
142 The headers should be delimited by a line which is
143 not a valid RFC822 header or continuation line,
144 that matches the variable `mail-header-separator'.
145 This is used by the default mail-sending commands.  See also
146 `message-send-mail-function' for use with the Message package."
147   :type '(radio (function-item sendmail-send-it :tag "Use Sendmail package")
148                 (function-item smtpmail-send-it :tag "Use SMTPmail package")
149                 (function-item feedmail-send-it :tag "Use Feedmail package")
150                 (function-item mailclient-send-it :tag "Use Mailclient package")
151                 function)
152   :group 'sendmail)
153
154 ;;;###autoload
155 (defcustom mail-header-separator "--text follows this line--"
156   "Line used to separate headers from text in messages being composed."
157   :type 'string
158   :group 'sendmail)
159
160 ;; Set up mail-header-separator for use as a category text property.
161 (put 'mail-header-separator 'rear-nonsticky '(category))
162 ;; This was a nice idea, for preventing accidental modification of
163 ;; the separator.   But I found it also prevented or obstructed
164 ;; certain deliberate operations, such as copying the separator line
165 ;; up to the top to send myself a copy of an already sent outgoing message
166 ;; and other things.  So I turned it off.  --rms.
167 ;;(put 'mail-header-separator 'read-only t)
168
169 ;;;###autoload
170 (defcustom mail-archive-file-name nil
171   "Name of file to write all outgoing messages in, or nil for none.
172 This can be an inbox file or an Rmail file."
173   :type '(choice file (const nil))
174   :group 'sendmail)
175
176 ;;;###autoload
177 (defcustom mail-default-reply-to nil
178   "Address to insert as default Reply-to field of outgoing messages.
179 If nil, it will be initialized from the REPLYTO environment variable
180 when you first send mail."
181   :type '(choice (const nil) string)
182   :group 'sendmail)
183
184 ;;;###autoload
185 (defcustom mail-alias-file nil
186   "If non-nil, the name of a file to use instead of `/usr/lib/aliases'.
187 This file defines aliases to be expanded by the mailer; this is a different
188 feature from that of defining aliases in `.mailrc' to be expanded in Emacs.
189 This variable has no effect unless your system uses sendmail as its mailer."
190   :type '(choice (const nil) file)
191   :group 'sendmail)
192
193 ;;;###autoload
194 (defcustom mail-personal-alias-file "~/.mailrc"
195   "If non-nil, the name of the user's personal mail alias file.
196 This file typically should be in same format as the `.mailrc' file used by
197 the `Mail' or `mailx' program.
198 This file need not actually exist."
199   :type '(choice (const nil) file)
200   :group 'sendmail)
201
202 ;;;###autoload
203 (defcustom mail-setup-hook nil
204   "Normal hook, run each time a new outgoing mail message is initialized.
205 The function `mail-setup' runs this hook."
206   :type 'hook
207   :options '(fortune-to-signature spook mail-abbrevs-setup)
208   :group 'sendmail)
209
210 ;;;###autoload
211 (defvar mail-aliases t
212   "Alist of mail address aliases,
213 or t meaning should be initialized from your mail aliases file.
214 \(The file's name is normally `~/.mailrc', but `mail-personal-alias-file'
215 can specify a different file name.)
216 The alias definitions in the file have this form:
217     alias ALIAS MEANING")
218
219 (defvar mail-alias-modtime nil
220   "The modification time of your mail alias file when it was last examined.")
221
222 ;;;###autoload
223 (defcustom mail-yank-prefix nil
224   "Prefix insert on lines of yanked message being replied to.
225 nil means use indentation."
226   :type '(choice (const nil) string)
227   :group 'sendmail)
228
229 ;;;###autoload
230 (defcustom mail-indentation-spaces 3
231   "Number of spaces to insert at the beginning of each cited line.
232 Used by `mail-yank-original' via `mail-indent-citation'."
233   :type 'integer
234   :group 'sendmail)
235
236 (defvar mail-yank-hooks nil
237   "Obsolete hook for modifying a citation just inserted in the mail buffer.
238 Each hook function can find the citation between (point) and (mark t).
239 And each hook function should leave point and mark around the citation
240 text as modified.
241
242 This is a normal hook, misnamed for historical reasons.
243 It is semi-obsolete and mail agents should no longer use it.")
244
245 ;;;###autoload
246 (defcustom mail-citation-hook nil
247   "Hook for modifying a citation just inserted in the mail buffer.
248 Each hook function can find the citation between (point) and (mark t),
249 and should leave point and mark around the citation text as modified.
250 The hook functions can find the header of the cited message
251 in the variable `mail-citation-header', whether or not this is included
252 in the cited portion of the message.
253
254 If this hook is entirely empty (nil), a default action is taken
255 instead of no action."
256   :type 'hook
257   :group 'sendmail)
258
259 (defvar mail-citation-header nil
260   "While running `mail-citation-hook', this variable holds the message header.
261 This enables the hook functions to see the whole message header
262 regardless of what part of it (if any) is included in the cited text.")
263
264 ;;;###autoload
265 (defcustom mail-citation-prefix-regexp "[ \t]*[-a-z0-9A-Z]*>+[ \t]*\\|[ \t]*"
266   "Regular expression to match a citation prefix plus whitespace.
267 It should match whatever sort of citation prefixes you want to handle,
268 with whitespace before and after; it should also match just whitespace.
269 The default value matches citations like `foo-bar>' plus whitespace."
270   :type 'regexp
271   :group 'sendmail
272   :version "20.3")
273
274 (defvar mail-abbrevs-loaded nil)
275 (defvar mail-mode-map
276   (let ((map (make-sparse-keymap)))
277     (define-key map "\M-\t" 'mail-complete)
278     (define-key map "\C-c?" 'describe-mode)
279     (define-key map "\C-c\C-f\C-t" 'mail-to)
280     (define-key map "\C-c\C-f\C-b" 'mail-bcc)
281     (define-key map "\C-c\C-f\C-f" 'mail-fcc)
282     (define-key map "\C-c\C-f\C-c" 'mail-cc)
283     (define-key map "\C-c\C-f\C-s" 'mail-subject)
284     (define-key map "\C-c\C-f\C-r" 'mail-reply-to)
285     (define-key map "\C-c\C-f\C-a" 'mail-mail-reply-to)    ; author
286     (define-key map "\C-c\C-f\C-l" 'mail-mail-followup-to) ; list
287     (define-key map "\C-c\C-t" 'mail-text)
288     (define-key map "\C-c\C-y" 'mail-yank-original)
289     (define-key map "\C-c\C-r" 'mail-yank-region)
290     (define-key map [remap split-line] 'mail-split-line)
291     (define-key map "\C-c\C-q" 'mail-fill-yanked-message)
292     (define-key map "\C-c\C-w" 'mail-signature)
293     (define-key map "\C-c\C-v" 'mail-sent-via)
294     (define-key map "\C-c\C-c" 'mail-send-and-exit)
295     (define-key map "\C-c\C-s" 'mail-send)
296     (define-key map "\C-c\C-i" 'mail-attach-file)
297
298     (define-key map [menu-bar mail]
299       (cons "Mail" (make-sparse-keymap "Mail")))
300
301     (define-key map [menu-bar mail fill]
302       '("Fill Citation" . mail-fill-yanked-message))
303
304     (define-key map [menu-bar mail yank]
305       '("Cite Original" . mail-yank-original))
306
307     (define-key map [menu-bar mail signature]
308       '("Insert Signature" . mail-signature))
309
310     (define-key map [menu-bar mail mail-sep]
311       '("--"))
312
313     (define-key map [menu-bar mail cancel]
314       '("Cancel" . mail-dont-send))
315
316     (define-key map [menu-bar mail send-stay]
317       '("Send, Keep Editing" . mail-send))
318
319     (define-key map [menu-bar mail send]
320       '("Send Message" . mail-send-and-exit))
321
322     (define-key map [menu-bar headers]
323       (cons "Headers" (make-sparse-keymap "Move to Header")))
324
325     (define-key map [menu-bar headers text]
326       '("Text" . mail-text))
327
328     (define-key map [menu-bar headers expand-aliases]
329       '("Expand Aliases" . expand-mail-aliases))
330
331     (define-key map [menu-bar headers sent-via]
332       '("Sent Via" . mail-sent-via))
333
334     (define-key map [menu-bar headers mail-reply-to]
335       '("Mail Reply To" . mail-mail-reply-to))
336
337     (define-key map [menu-bar headers mail-followup-to]
338       '("Mail Followup To" . mail-mail-followup-to))
339
340     (define-key map [menu-bar headers reply-to]
341       '("Reply-To" . mail-reply-to))
342
343     (define-key map [menu-bar headers bcc]
344       '("Bcc" . mail-bcc))
345
346     (define-key map [menu-bar headers fcc]
347       '("Fcc" . mail-fcc))
348
349     (define-key map [menu-bar headers cc]
350       '("Cc" . mail-cc))
351
352     (define-key map [menu-bar headers subject]
353       '("Subject" . mail-subject))
354
355     (define-key map [menu-bar headers to]
356       '("To" . mail-to))
357
358     map))
359
360 (autoload 'build-mail-aliases "mailalias"
361   "Read mail aliases from user's personal aliases file and set `mail-aliases'."
362   nil)
363
364 (autoload 'expand-mail-aliases "mailalias"
365   "Expand all mail aliases in suitable header fields found between BEG and END.
366 Suitable header fields are `To', `Cc' and `Bcc' and their `Resent-' variants.
367 Optional second arg EXCLUDE may be a regular expression defining text to be
368 removed from alias expansions."
369   nil)
370
371 ;;;###autoload
372 (defcustom mail-signature nil
373   "Text inserted at end of mail buffer when a message is initialized.
374 If t, it means to insert the contents of the file `mail-signature-file'.
375 If a string, that string is inserted.
376  (To make a proper signature, the string should begin with \\n\\n-- \\n,
377   which is the standard way to delimit a signature in a message.)
378 Otherwise, it should be an expression; it is evaluated
379 and should insert whatever you want to insert."
380   :type '(choice (const :tag "None" nil)
381                  (const :tag "Use `.signature' file" t)
382                  (string :tag "String to insert")
383                  (sexp :tag "Expression to evaluate"))
384   :group 'sendmail)
385 (put 'mail-signature 'risky-local-variable t)
386
387 ;;;###autoload
388 (defcustom mail-signature-file "~/.signature"
389   "File containing the text inserted at end of mail buffer."
390   :type 'file
391   :group 'sendmail)
392
393 ;;;###autoload
394 (defcustom mail-default-directory "~/"
395   "Directory for mail buffers.
396 Value of `default-directory' for mail buffers.
397 This directory is used for auto-save files of mail buffers."
398   :type '(directory :tag "Directory")
399   :group 'sendmail
400   :version "22.1")
401
402 (defvar mail-reply-action nil)
403 (defvar mail-send-actions nil
404   "A list of actions to be performed upon successful sending of a message.")
405 (put 'mail-reply-action 'permanent-local t)
406 (put 'mail-send-actions 'permanent-local t)
407
408 ;;;###autoload
409 (defcustom mail-default-headers nil
410   "A string containing header lines, to be inserted in outgoing messages.
411 It is inserted before you edit the message,
412 so you can edit or delete these lines."
413   :type '(choice (const nil) string)
414   :group 'sendmail)
415
416 ;;;###autoload
417 (defcustom mail-bury-selects-summary t
418   "If non-nil, try to show RMAIL summary buffer after returning from mail.
419 The functions \\[mail-send-on-exit] or \\[mail-dont-send] select
420 the RMAIL summary buffer before returning, if it exists and this variable
421 is non-nil."
422   :type 'boolean
423   :group 'sendmail)
424
425 ;;;###autoload
426 (defcustom mail-send-nonascii 'mime
427   "Specify whether to allow sending non-ASCII characters in mail.
428 If t, that means do allow it.  nil means don't allow it.
429 `query' means ask the user each time.
430 `mime' means add an appropriate MIME header if none already present.
431 The default is `mime'.
432 Including non-ASCII characters in a mail message can be problematical
433 for the recipient, who may not know how to decode them properly."
434   :type '(choice (const t) (const nil) (const query) (const mime))
435   :group 'sendmail)
436
437 (defcustom mail-use-dsn nil
438   "Ask MTA for notification of failed, delayed or successful delivery.
439 Note that only some MTAs (currently only recent versions of Sendmail)
440 support Delivery Status Notification."
441   :group 'sendmail
442   :type '(repeat (radio (const :tag "Failure" failure)
443                         (const :tag "Delay" delay)
444                         (const :tag "Success" success)))
445   :version "22.1")
446
447 ;; Note: could use /usr/ucb/mail instead of sendmail;
448 ;; options -t, and -v if not interactive.
449 (defvar mail-mailer-swallows-blank-line
450   (if (and (string-match "sparc-sun-sunos\\(\\'\\|[^5]\\)" system-configuration)
451            (file-readable-p "/etc/sendmail.cf")
452            (with-temp-buffer
453              (insert-file-contents "/etc/sendmail.cf")
454              (goto-char (point-min))
455              (let ((case-fold-search nil))
456                (re-search-forward "^OR\\>" nil t))))
457       ;; According to RFC822, "The field-name must be composed of printable
458       ;; ASCII characters (i.e. characters that have decimal values between
459       ;; 33 and 126, except colon)", i.e. any chars except ctl chars,
460       ;; space, or colon.
461       '(looking-at "[ \t]\\|[][!\"#$%&'()*+,-./0-9;<=>?@A-Z\\\\^_`a-z{|}~]+:"))
462   "Set this non-nil if the system's mailer runs the header and body together.
463 \(This problem exists on Sunos 4 when sendmail is run in remote mode.)
464 The value should be an expression to test whether the problem will
465 actually occur.")
466
467 (defvar mail-mode-syntax-table
468   (let ((st (make-syntax-table)))
469     ;; define-derived-mode will make it inherit from text-mode-syntax-table.
470     (modify-syntax-entry ?% ". " st)
471     st)
472   "Syntax table used while in `mail-mode'.")
473
474 (defvar mail-font-lock-keywords
475   (eval-when-compile
476     (let* ((cite-chars "[>|}]")
477            (cite-prefix "[:alpha:]")
478            (cite-suffix (concat cite-prefix "0-9_.@-`'\"")))
479       (list '("^\\(To\\|Newsgroups\\):" . font-lock-function-name-face)
480             '("^\\(B?CC\\|Reply-to\\|Mail-\\(reply\\|followup\\)-to\\):" . font-lock-keyword-face)
481             '("^\\(Subject:\\)[ \t]*\\(.+\\)?"
482               (1 font-lock-comment-face)
483 ;;            (2 font-lock-type-face nil t)
484               )
485             ;; Use EVAL to delay in case `mail-header-separator' gets changed.
486             '(eval .
487               (let ((separator (if (zerop (length mail-header-separator))
488                                    " \\`\\' "
489                                  (regexp-quote mail-header-separator))))
490                 (cons (concat "^" separator "$") 'font-lock-warning-face)))
491             ;; Use MATCH-ANCHORED to effectively anchor the regexp left side.
492             `(,cite-chars
493               (,(concat "\\=[ \t]*"
494                         "\\(\\(\\([" cite-prefix "]+[" cite-suffix "]*\\)?"
495                         "\\(" cite-chars "[ \t]*\\)\\)+\\)"
496                         "\\(.*\\)")
497                (beginning-of-line) (end-of-line)
498                (1 font-lock-comment-delimiter-face nil t)
499                (5 font-lock-comment-face nil t)))
500             '("^\\(X-[A-Za-z0-9-]+\\|In-reply-to\\):.*\\(\n[ \t]+.*\\)*$"
501               . font-lock-string-face))))
502   "Additional expressions to highlight in Mail mode.")
503
504 \f
505 (defun sendmail-sync-aliases ()
506   (when mail-personal-alias-file
507     (let ((modtime (nth 5 (file-attributes mail-personal-alias-file))))
508       (or (equal mail-alias-modtime modtime)
509           (setq mail-alias-modtime modtime
510                 mail-aliases t)))))
511
512 (defun mail-setup (to subject in-reply-to cc replybuffer actions)
513   (or mail-default-reply-to
514       (setq mail-default-reply-to (getenv "REPLYTO")))
515   (sendmail-sync-aliases)
516   (if (eq mail-aliases t)
517       (progn
518         (setq mail-aliases nil)
519         (when mail-personal-alias-file
520           (if (file-exists-p mail-personal-alias-file)
521               (build-mail-aliases)))))
522   ;; Don't leave this around from a previous message.
523   (kill-local-variable 'buffer-file-coding-system)
524   ;; This doesn't work for enable-multibyte-characters.
525   ;; (kill-local-variable 'enable-multibyte-characters)
526   (set-buffer-multibyte default-enable-multibyte-characters)
527   (if current-input-method
528       (inactivate-input-method))
529   (setq mail-send-actions actions)
530   (setq mail-reply-action replybuffer)
531   (goto-char (point-min))
532   (if mail-setup-with-from
533       (mail-insert-from-field))
534   (insert "To: ")
535   (save-excursion
536     (if to
537         ;; Here removed code to extract names from within <...>
538         ;; on the assumption that mail-strip-quoted-names
539         ;; has been called and has done so.
540         (let ((fill-prefix "\t")
541               (address-start (point)))
542           (insert to "\n")
543           (fill-region-as-paragraph address-start (point-max))
544           (goto-char (point-max))
545           (unless (bolp)
546             (newline)))
547       (newline))
548     (if cc
549         (let ((fill-prefix "\t")
550               (address-start (progn (insert "CC: ") (point))))
551           (insert cc "\n")
552           (fill-region-as-paragraph address-start (point-max))
553           (goto-char (point-max))
554           (unless (bolp)
555             (newline))))
556     (if in-reply-to
557         (let ((fill-prefix "\t")
558               (fill-column 78)
559               (address-start (point)))
560           (insert "In-reply-to: " in-reply-to "\n")
561           (fill-region-as-paragraph address-start (point-max))
562           (goto-char (point-max))
563           (unless (bolp)
564             (newline))))
565     (insert "Subject: " (or subject "") "\n")
566     (if mail-default-headers
567         (insert mail-default-headers))
568     (if mail-default-reply-to
569         (insert "Reply-to: " mail-default-reply-to "\n"))
570     (if mail-self-blind
571         (insert "BCC: " user-mail-address "\n"))
572     (if mail-archive-file-name
573         (insert "FCC: " mail-archive-file-name "\n"))
574     (put-text-property (point)
575                        (progn
576                          (insert mail-header-separator "\n")
577                          (1- (point)))
578                        'category 'mail-header-separator)
579     ;; Insert the signature.  But remember the beginning of the message.
580     (if to (setq to (point)))
581     (cond ((eq mail-signature t)
582            (if (file-exists-p mail-signature-file)
583                (progn
584                  (insert "\n\n-- \n")
585                  (insert-file-contents mail-signature-file))))
586           ((stringp mail-signature)
587            (insert mail-signature))
588           (t
589            (eval mail-signature)))
590     (goto-char (point-max))
591     (or (bolp) (newline)))
592   (if to (goto-char to))
593   (or to subject in-reply-to
594       (set-buffer-modified-p nil))
595   (run-hooks 'mail-setup-hook))
596 \f
597 (defcustom mail-mode-hook nil
598   "Hook run by Mail mode."
599   :group 'sendmail
600   :type 'hook
601   :options '(footnote-mode))
602
603 (defvar mail-mode-abbrev-table text-mode-abbrev-table)
604 ;;;###autoload
605 (define-derived-mode mail-mode text-mode "Mail"
606   "Major mode for editing mail to be sent.
607 Like Text Mode but with these additional commands:
608
609 \\[mail-send]  mail-send (send the message)
610 \\[mail-send-and-exit]  mail-send-and-exit (send the message and exit)
611
612 Here are commands that move to a header field (and create it if there isn't):
613          \\[mail-to]  move to To:       \\[mail-subject]  move to Subject:
614          \\[mail-cc]  move to CC:       \\[mail-bcc]  move to BCC:
615          \\[mail-fcc]  move to FCC:     \\[mail-reply-to] move to Reply-To:
616          \\[mail-mail-reply-to]  move to Mail-Reply-To:
617          \\[mail-mail-followup-to] move to Mail-Followup-To:
618 \\[mail-text]  mail-text (move to beginning of message text).
619 \\[mail-signature]  mail-signature (insert `mail-signature-file' file).
620 \\[mail-yank-original]  mail-yank-original (insert current message, in Rmail).
621 \\[mail-fill-yanked-message]  mail-fill-yanked-message (fill what was yanked).
622 \\[mail-sent-via]  mail-sent-via (add a Sent-via field for each To or CC).
623 Turning on Mail mode runs the normal hooks `text-mode-hook' and
624 `mail-mode-hook' (in that order)."
625   (make-local-variable 'mail-reply-action)
626   (make-local-variable 'mail-send-actions)
627   (setq buffer-offer-save t)
628   (make-local-variable 'font-lock-defaults)
629   (setq font-lock-defaults '(mail-font-lock-keywords t t))
630   (make-local-variable 'paragraph-separate)
631   (make-local-variable 'normal-auto-fill-function)
632   (setq normal-auto-fill-function 'mail-mode-auto-fill)
633   (make-local-variable 'fill-paragraph-function)
634   (setq fill-paragraph-function 'mail-mode-fill-paragraph)
635   ;; Allow using comment commands to add/remove quoting (this only does
636   ;; anything if mail-yank-prefix is set to a non-nil value).
637   (set (make-local-variable 'comment-start) mail-yank-prefix)
638   (if mail-yank-prefix
639       (set (make-local-variable 'comment-start-skip)
640            (concat "^" (regexp-quote mail-yank-prefix) "[ \t]*")))
641   (make-local-variable 'adaptive-fill-regexp)
642   (setq adaptive-fill-regexp
643         (concat "[ \t]*[-[:alnum:]]+>+[ \t]*\\|"
644                 adaptive-fill-regexp))
645   (make-local-variable 'adaptive-fill-first-line-regexp)
646   (setq adaptive-fill-first-line-regexp
647         (concat "[ \t]*[-[:alnum:]]*>+[ \t]*\\|"
648                 adaptive-fill-first-line-regexp))
649   ;; `-- ' precedes the signature.  `-----' appears at the start of the
650   ;; lines that delimit forwarded messages.
651   ;; Lines containing just >= 3 dashes, perhaps after whitespace,
652   ;; are also sometimes used and should be separators.
653   (setq paragraph-separate (concat (regexp-quote mail-header-separator)
654                                 "$\\|\t*\\([-|#;>* ]\\|(?[0-9]+[.)]\\)+$"
655                                 "\\|[ \t]*[[:alnum:]]*>+[ \t]*$\\|[ \t]*$\\|"
656                                 "--\\( \\|-+\\)$\\|"
657                                 page-delimiter)))
658
659
660 (defun mail-header-end ()
661   "Return the buffer location of the end of headers, as a number."
662   (save-restriction
663     (widen)
664     (save-excursion
665       (rfc822-goto-eoh)
666       (point))))
667
668 (defun mail-text-start ()
669   "Return the buffer location of the start of text, as a number."
670   (save-restriction
671     (widen)
672     (save-excursion
673       (rfc822-goto-eoh)
674       (forward-line 1)
675       (point))))
676
677 (defun mail-sendmail-delimit-header ()
678   "Set up whatever header delimiter convention sendmail will use.
679 Concretely: replace the first blank line in the header with the separator."
680   (rfc822-goto-eoh)
681   (insert mail-header-separator)
682   (point))
683
684 (defun mail-sendmail-undelimit-header ()
685   "Remove header separator to put the message in correct form for sendmail.
686 Leave point at the start of the delimiter line."
687   (rfc822-goto-eoh)
688   (delete-region (point) (progn (end-of-line) (point))))
689
690 (defun mail-mode-auto-fill ()
691   "Carry out Auto Fill for Mail mode.
692 If within the headers, this makes the new lines into continuation lines."
693   (if (< (point) (mail-header-end))
694       (let ((old-line-start (save-excursion (beginning-of-line) (point))))
695         (if (do-auto-fill)
696             (save-excursion
697               (beginning-of-line)
698               (while (not (eq (point) old-line-start))
699                 ;; Use insert-before-markers in case we're inserting
700                 ;; before the saved value of point (which is common).
701                 (insert-before-markers "   ")
702                 (forward-line -1))
703               t)))
704     (do-auto-fill)))
705
706 (defun mail-mode-fill-paragraph (arg)
707   ;; Do something special only if within the headers.
708   (if (< (point) (mail-header-end))
709       (let (beg end fieldname)
710         (when (prog1 (re-search-backward "^[-a-zA-Z]+:" nil 'yes)
711                 (setq beg (point)))
712         (setq fieldname
713                 (downcase (buffer-substring beg (1- (match-end 0))))))
714         (forward-line 1)
715         ;; Find continuation lines and get rid of their continuation markers.
716         (while (looking-at "[ \t]")
717           (delete-horizontal-space)
718           (forward-line 1))
719         (setq end (point-marker))
720         (goto-char beg)
721         ;; If this field contains addresses,
722         ;; make sure we can fill after each address.
723         (if (member fieldname
724                     '("to" "cc" "bcc" "from" "reply-to"
725                       "mail-reply-to" "mail-followup-to"
726                       "resent-to" "resent-cc" "resent-bcc"
727                       "resent-from" "resent-reply-to"))
728             (while (search-forward "," end t)
729               (or (looking-at "[ \t]")
730                   (insert " "))))
731         (fill-region-as-paragraph beg end arg)
732         ;; Mark all lines except the first as continuations.
733         (goto-char beg)
734         (forward-line 1)
735         (while (< (point) end)
736           (insert "  ")
737           (forward-line 1))
738         (move-marker end nil)
739         t)))
740 \f
741 ;; User-level commands for sending.
742
743 (defun mail-send-and-exit (&optional arg)
744   "Send message like `mail-send', then, if no errors, exit from mail buffer.
745 Prefix arg means don't delete this window."
746   (interactive "P")
747   (mail-send)
748   (mail-bury arg))
749
750 (defun mail-dont-send (&optional arg)
751   "Don't send the message you have been editing.
752 Prefix arg means don't delete this window."
753   (interactive "P")
754   (mail-bury arg))
755
756 (defun mail-bury (&optional arg)
757   "Bury this mail buffer."
758   (let ((newbuf (other-buffer (current-buffer))))
759     (bury-buffer (current-buffer))
760     (if (and (or (window-dedicated-p (frame-selected-window))
761                  (cdr (assq 'mail-dedicated-frame (frame-parameters))))
762              (not (null (delq (selected-frame) (visible-frame-list)))))
763         (progn
764           (if (display-multi-frame-p)
765               (delete-frame (selected-frame))
766             ;; The previous frame is where normally they have the
767             ;; RMAIL buffer displayed.
768             (other-frame -1)))
769       (let (rmail-flag summary-buffer)
770         (and (not arg)
771              (not (one-window-p))
772              (with-current-buffer
773                  (window-buffer (next-window (selected-window) 'not))
774                (setq rmail-flag (eq major-mode 'rmail-mode))
775                (setq summary-buffer
776                      (and mail-bury-selects-summary
777                           (boundp 'rmail-summary-buffer)
778                           rmail-summary-buffer
779                           (buffer-name rmail-summary-buffer)
780                           (not (get-buffer-window rmail-summary-buffer))
781                           rmail-summary-buffer))))
782         (if rmail-flag
783             ;; If the Rmail buffer has a summary, show that.
784             (if summary-buffer (switch-to-buffer summary-buffer)
785               (delete-window))
786           (switch-to-buffer newbuf))))))
787
788 (defcustom mail-send-hook nil
789   "Hook run just before sending mail with `mail-send'."
790   :type 'hook
791   :options '(flyspell-mode-off)
792   :group 'sendmail)
793
794 ;;;###autoload
795 (defcustom mail-mailing-lists nil "\
796 *List of mailing list addresses the user is subscribed to.
797
798 The variable is used to trigger insertion of the \"Mail-Followup-To\"
799 header when sending a message to a mailing list."
800   :type '(repeat string)
801   :group 'sendmail)
802
803
804 (defun mail-send ()
805   "Send the message in the current buffer.
806 If `mail-interactive' is non-nil, wait for success indication
807 or error messages, and inform user.
808 Otherwise any failure is reported in a message back to
809 the user from the mailer."
810   (interactive)
811   (if (if buffer-file-name
812           (y-or-n-p "Send buffer contents as mail message? ")
813         (or (buffer-modified-p)
814             (y-or-n-p "Message already sent; resend? ")))
815       (let ((inhibit-read-only t)
816             (opoint (point))
817             (ml (when mail-mailing-lists
818                 ;; The surrounding regexp assumes the use of
819                 ;; `mail-strip-quoted-names' on addresses before matching
820                 ;; Cannot deal with full RFC 822 freedom, but that is
821                 ;; unlikely to be problematic.
822                 (concat "\\(?:[[:space:];,]\\|\\`\\)"
823                         (regexp-opt mail-mailing-lists t)
824                         "\\(?:[[:space:];,]\\|\\'\\)"))))
825         ;; If there are mailing lists defined
826         (when ml
827           (save-excursion
828             (let* ((to (mail-fetch-field "to" nil t))
829                    (cc (mail-fetch-field "cc" nil t))
830                    (new-header-values   ; To: and Cc:
831                     (mail-strip-quoted-names
832                      (concat to (when cc (concat ", " cc))))))
833               ;; If message goes to known mailing list ...
834               (when (string-match ml new-header-values)
835                 ;; Add Mail-Followup-To if none yet
836                 (unless (mail-fetch-field "mail-followup-to")
837                   (goto-char (mail-header-end))
838                   (insert "Mail-Followup-To: "
839                           (let ((l))
840                             (mapc
841                              ;; remove duplicates
842                              '(lambda (e)
843                                 (unless (member e l)
844                                   (push e l)))
845                              (split-string new-header-values
846                                            ",[[:space:]]+" t))
847                             (mapconcat 'identity l ", "))
848                           "\n"))
849                 ;; Add Mail-Reply-To if none yet
850                 (unless (mail-fetch-field "mail-reply-to")
851                   (goto-char (mail-header-end))
852                   (insert "Mail-Reply-To: "
853                           (or (mail-fetch-field "reply-to")
854                               user-mail-address)
855                           "\n"))))))
856         (unless (memq mail-send-nonascii '(t mime))
857           (goto-char (point-min))
858           (skip-chars-forward "\0-\177")
859           (or (= (point) (point-max))
860               (if (eq mail-send-nonascii 'query)
861                   (or (y-or-n-p "Message contains non-ASCII characters; send anyway? ")
862                       (error "Aborted"))
863                 (error "Message contains non-ASCII characters"))))
864         ;; Complain about any invalid line.
865         (goto-char (point-min))
866         (while (< (point) (mail-header-end))
867           (unless (looking-at "[ \t]\\|.*:\\|$")
868             (push-mark opoint)
869             (error "Invalid header line (maybe a continuation line lacks initial whitespace)"))
870           (forward-line 1))
871         (goto-char opoint)
872         (run-hooks 'mail-send-hook)
873         (message "Sending...")
874         (funcall send-mail-function)
875         ;; Now perform actions on successful sending.
876         (while mail-send-actions
877           (condition-case nil
878               (apply (car (car mail-send-actions))
879                      (cdr (car mail-send-actions)))
880             (error))
881           (setq mail-send-actions (cdr mail-send-actions)))
882         (message "Sending...done")
883         ;; If buffer has no file, mark it as unmodified and delete auto-save.
884         (if (not buffer-file-name)
885             (progn
886               (set-buffer-modified-p nil)
887               (delete-auto-save-file-if-necessary t))))))
888
889 (defun mail-envelope-from ()
890   "Return the envelope mail address to use when sending mail.
891 This function uses `mail-envelope-from'."
892   (if (eq mail-envelope-from 'header)
893       (nth 1 (mail-extract-address-components
894               (mail-fetch-field "From")))
895     mail-envelope-from))
896 \f
897 ;; This does the real work of sending a message via sendmail.
898 ;; It is called via the variable send-mail-function.
899
900 ;;;###autoload
901 (defvar sendmail-coding-system nil
902   "*Coding system for encoding the outgoing mail.
903 This has higher priority than `default-buffer-file-coding-system'
904 and `default-sendmail-coding-system',
905 but lower priority than the local value of `buffer-file-coding-system'.
906 See also the function `select-message-coding-system'.")
907
908 ;;;###autoload
909 (defvar default-sendmail-coding-system 'iso-latin-1
910   "Default coding system for encoding the outgoing mail.
911 This variable is used only when `sendmail-coding-system' is nil.
912
913 This variable is set/changed by the command `set-language-environment'.
914 User should not set this variable manually,
915 instead use `sendmail-coding-system' to get a constant encoding
916 of outgoing mails regardless of the current language environment.
917 See also the function `select-message-coding-system'.")
918
919 (defun mail-insert-from-field ()
920   (let* ((login user-mail-address)
921          (fullname (user-full-name))
922          (quote-fullname nil))
923     (if (string-match "[^\0-\177]" fullname)
924         (setq fullname (rfc2047-encode-string fullname)
925               quote-fullname t))
926     (cond ((eq mail-from-style 'angles)
927            (insert "From: " fullname)
928            (let ((fullname-start (+ (point-min) 6))
929                  (fullname-end (point-marker)))
930              (goto-char fullname-start)
931              ;; Look for a character that cannot appear unquoted
932              ;; according to RFC 822.
933              (if (or (re-search-forward "[^- !#-'*+/-9=?A-Z^-~]"
934                                         fullname-end 1)
935                      quote-fullname)
936                  (progn
937                    ;; Quote fullname, escaping specials.
938                    (goto-char fullname-start)
939                    (insert "\"")
940                    (while (re-search-forward "[\"\\]"
941                                              fullname-end 1)
942                      (replace-match "\\\\\\&" t))
943                    (insert "\""))))
944            (insert " <" login ">\n"))
945           ((eq mail-from-style 'parens)
946            (insert "From: " login " (")
947            (let ((fullname-start (point)))
948              (if quote-fullname
949                  (insert "\""))
950              (insert fullname)
951              (if quote-fullname
952                  (insert "\""))
953              (let ((fullname-end (point-marker)))
954                (goto-char fullname-start)
955                ;; RFC 822 says \ and nonmatching parentheses
956                ;; must be escaped in comments.
957                ;; Escape every instance of ()\ ...
958                (while (re-search-forward "[()\\]" fullname-end 1)
959                  (replace-match "\\\\\\&" t))
960                ;; ... then undo escaping of matching parentheses,
961                ;; including matching nested parentheses.
962                (goto-char fullname-start)
963                (while (re-search-forward
964                        "\\(\\=\\|[^\\]\\(\\\\\\\\\\)*\\)\\\\(\\(\\([^\\]\\|\\\\\\\\\\)*\\)\\\\)"
965                        fullname-end 1)
966                  (replace-match "\\1(\\3)" t)
967                  (goto-char fullname-start))))
968            (insert ")\n"))
969           ((null mail-from-style)
970            (insert "From: " login "\n"))
971           ((eq mail-from-style 'system-default)
972            nil)
973           (t (error "Invalid value for `mail-from-style'")))))
974
975 (defun sendmail-send-it ()
976   "Send the current mail buffer using the Sendmail package.
977 This is a suitable value for `send-mail-function'.  It sends using the
978 external program defined by `sendmail-program'."
979   (require 'mail-utils)
980   (let ((errbuf (if mail-interactive
981                     (generate-new-buffer " sendmail errors")
982                   0))
983         (tembuf (generate-new-buffer " sendmail temp"))
984         (multibyte enable-multibyte-characters)
985         (case-fold-search nil)
986         (selected-coding (select-message-coding-system))
987         resend-to-addresses
988         delimline
989         fcc-was-found
990         (mailbuf (current-buffer))
991         (program (if (boundp 'sendmail-program)
992                      sendmail-program
993                    "/usr/lib/sendmail"))
994         ;; Examine these variables now, so that
995         ;; local binding in the mail buffer will take effect.
996         (envelope-from
997          (and mail-specify-envelope-from
998               (or (mail-envelope-from) user-mail-address))))
999     (unwind-protect
1000         (with-current-buffer tembuf
1001           (erase-buffer)
1002           (unless multibyte
1003             (set-buffer-multibyte nil))
1004           (insert-buffer-substring mailbuf)
1005           (goto-char (point-max))
1006           ;; require one newline at the end.
1007           (or (= (preceding-char) ?\n)
1008               (insert ?\n))
1009           ;; Change header-delimiter to be what sendmail expects.
1010           (goto-char (mail-header-end))
1011           (delete-region (point) (progn (end-of-line) (point)))
1012           (setq delimline (point-marker))
1013           (sendmail-sync-aliases)
1014           (if mail-aliases
1015               (expand-mail-aliases (point-min) delimline))
1016           (goto-char (point-min))
1017           ;; Ignore any blank lines in the header
1018           (while (and (re-search-forward "\n\n\n*" delimline t)
1019                       (< (point) delimline))
1020             (replace-match "\n"))
1021           (goto-char (point-min))
1022           ;; Look for Resent- headers.  They require sending
1023           ;; the message specially.
1024           (let ((case-fold-search t))
1025             (goto-char (point-min))
1026             (while (re-search-forward "^Resent-\\(to\\|cc\\|bcc\\):" delimline t)
1027               ;; Put a list of such addresses in resend-to-addresses.
1028               (setq resend-to-addresses
1029                     (save-restriction
1030                       (narrow-to-region (point)
1031                                         (save-excursion
1032                                           (forward-line 1)
1033                                           (while (looking-at "^[ \t]")
1034                                             (forward-line 1))
1035                                           (point)))
1036                       (append (mail-parse-comma-list)
1037                               resend-to-addresses)))
1038               ;; Delete Resent-BCC ourselves
1039               (if (save-excursion (beginning-of-line)
1040                                   (looking-at "resent-bcc"))
1041                   (delete-region (save-excursion (beginning-of-line) (point))
1042                                  (save-excursion (end-of-line) (1+ (point))))))
1043 ;;;  Apparently this causes a duplicate Sender.
1044 ;;;         ;; If the From is different than current user, insert Sender.
1045 ;;;         (goto-char (point-min))
1046 ;;;         (and (re-search-forward "^From:"  delimline t)
1047 ;;;              (progn
1048 ;;;                (require 'mail-utils)
1049 ;;;                (not (string-equal
1050 ;;;                      (mail-strip-quoted-names
1051 ;;;                       (save-restriction
1052 ;;;                         (narrow-to-region (point-min) delimline)
1053 ;;;                         (mail-fetch-field "From")))
1054 ;;;                      (user-login-name))))
1055 ;;;              (progn
1056 ;;;                (forward-line 1)
1057 ;;;                (insert "Sender: " (user-login-name) "\n")))
1058             ;; Don't send out a blank subject line
1059             (goto-char (point-min))
1060             (if (re-search-forward "^Subject:\\([ \t]*\n\\)+\\b" delimline t)
1061                 (replace-match "")
1062               ;; This one matches a Subject just before the header delimiter.
1063               (if (and (re-search-forward "^Subject:\\([ \t]*\n\\)+" delimline t)
1064                        (= (match-end 0) delimline))
1065                   (replace-match "")))
1066             ;; Put the "From:" field in unless for some odd reason
1067             ;; they put one in themselves.
1068             (goto-char (point-min))
1069             (if (not (re-search-forward "^From:" delimline t))
1070                 (mail-insert-from-field))
1071             ;; Possibly add a MIME header for the current coding system
1072             (let (charset)
1073               (goto-char (point-min))
1074               (and (eq mail-send-nonascii 'mime)
1075                    (not (re-search-forward "^MIME-version:" delimline t))
1076                    (progn (skip-chars-forward "\0-\177")
1077                           (/= (point) (point-max)))
1078                    selected-coding
1079                    (setq charset
1080                          (coding-system-get selected-coding 'mime-charset))
1081                    (goto-char delimline)
1082                    (insert "MIME-version: 1.0\n"
1083                            "Content-type: text/plain; charset="
1084                            (symbol-name charset)
1085                            "\nContent-Transfer-Encoding: 8bit\n")))
1086             ;; Insert an extra newline if we need it to work around
1087             ;; Sun's bug that swallows newlines.
1088             (goto-char (1+ delimline))
1089             (if (eval mail-mailer-swallows-blank-line)
1090                 (newline))
1091             ;; Find and handle any FCC fields.
1092             (goto-char (point-min))
1093             (if (re-search-forward "^FCC:" delimline t)
1094                 (progn
1095                   (setq fcc-was-found t)
1096                   (mail-do-fcc delimline)))
1097             (if mail-interactive
1098                 (with-current-buffer errbuf
1099                   (erase-buffer))))
1100           (goto-char (point-min))
1101           (if (let ((case-fold-search t))
1102                 (or resend-to-addresses
1103                     (re-search-forward "^To:\\|^cc:\\|^bcc:"
1104                                        delimline t)))
1105               (let* ((default-directory "/")
1106                      (coding-system-for-write selected-coding)
1107                      (args
1108                       (append (list (point-min) (point-max)
1109                                     program
1110                                     nil errbuf nil "-oi")
1111                               (and envelope-from
1112                                    (list "-f" envelope-from))
1113 ;;;                           ;; Don't say "from root" if running under su.
1114 ;;;                           (and (equal (user-real-login-name) "root")
1115 ;;;                                (list "-f" (user-login-name)))
1116                               (and mail-alias-file
1117                                    (list (concat "-oA" mail-alias-file)))
1118                               (if mail-interactive
1119                                   ;; These mean "report errors to terminal"
1120                                   ;; and "deliver interactively"
1121                                   '("-oep" "-odi")
1122                                 ;; These mean "report errors by mail"
1123                                 ;; and "deliver in background".
1124                                 '("-oem" "-odb"))
1125                               ;; Get the addresses from the message
1126                               ;; unless this is a resend.
1127                               ;; We must not do that for a resend
1128                               ;; because we would find the original addresses.
1129                               ;; For a resend, include the specific addresses.
1130                               (or resend-to-addresses
1131                                   '("-t")
1132                                   )
1133                               (if mail-use-dsn
1134                                   (list "-N" (mapconcat 'symbol-name
1135                                                         mail-use-dsn ",")))
1136                               )
1137                       )
1138                      (exit-value (apply 'call-process-region args)))
1139                 (or (null exit-value) (eq 0 exit-value)
1140                     (error "Sending...failed with exit value %d" exit-value)))
1141             (or fcc-was-found
1142                 (error "No recipients")))
1143           (if mail-interactive
1144               (with-current-buffer errbuf
1145                 (goto-char (point-min))
1146                 (while (re-search-forward "\n\n* *" nil t)
1147                   (replace-match "; "))
1148                 (if (not (zerop (buffer-size)))
1149                     (error "Sending...failed to %s"
1150                            (buffer-substring (point-min) (point-max)))))))
1151       (kill-buffer tembuf)
1152       (if (bufferp errbuf)
1153           (kill-buffer errbuf)))))
1154
1155 (defun mail-do-fcc (header-end)
1156   (unless (markerp header-end)
1157     (error "Value of `header-end' must be a marker"))
1158   (let (fcc-list
1159         (rmailbuf (current-buffer))
1160         (time (current-time))
1161         (tembuf (generate-new-buffer " rmail output"))
1162         (case-fold-search t))
1163     (save-excursion
1164       (goto-char (point-min))
1165       (while (re-search-forward "^FCC:[ \t]*" header-end t)
1166         (push (buffer-substring (point)
1167                                 (progn
1168                                   (end-of-line)
1169                                   (skip-chars-backward " \t")
1170                                   (point)))
1171               fcc-list)
1172         (delete-region (match-beginning 0)
1173                        (progn (forward-line 1) (point))))
1174       (set-buffer tembuf)
1175       (erase-buffer)
1176       ;; This initial newline is written out if the fcc file already exists.
1177       (insert "\nFrom " (user-login-name) " "
1178               (current-time-string time) "\n")
1179       ;; Insert the time zone before the year.
1180       (forward-char -1)
1181       (forward-word -1)
1182       (require 'mail-utils)
1183       (insert (mail-rfc822-time-zone time) " ")
1184       (goto-char (point-max))
1185       (insert-buffer-substring rmailbuf)
1186       ;; Make sure messages are separated.
1187       (goto-char (point-max))
1188       (insert ?\n)
1189       (goto-char 2)
1190       ;; ``Quote'' "^From " as ">From "
1191       ;;  (note that this isn't really quoting, as there is no requirement
1192       ;;   that "^[>]+From " be quoted in the same transparent way.)
1193       (let ((case-fold-search nil))
1194         (while (search-forward "\nFrom " nil t)
1195           (forward-char -5)
1196           (insert ?>)))
1197       (dolist (fcc fcc-list)
1198         (let* ((buffer (find-buffer-visiting fcc))
1199                (curbuf (current-buffer))
1200                dont-write-the-file
1201                buffer-matches-file
1202                (beg (point-min)) (end (point-max))
1203                (beg2 (save-excursion (goto-char (point-min))
1204                                      (forward-line 2) (point))))
1205           (if buffer
1206               ;; File is present in a buffer => append to that buffer.
1207               (with-current-buffer buffer
1208                 (setq buffer-matches-file
1209                       (and (not (buffer-modified-p))
1210                            (verify-visited-file-modtime buffer)))
1211                 ;; Keep the end of the accessible portion at the same place
1212                 ;; unless it is the end of the buffer.
1213                 (let ((max (if (/= (1+ (buffer-size)) (point-max))
1214                                (point-max))))
1215                   (unwind-protect
1216                       ;; Code below lifted from rmailout.el
1217                       ;; function rmail-output-to-rmail-file:
1218                       (let ((buffer-read-only nil)
1219                             (msg (and (boundp 'rmail-current-message)
1220                                       rmail-current-message)))
1221                         ;; If MSG is non-nil, buffer is in RMAIL mode.
1222                         (if msg
1223                             (progn
1224                               ;; Append to an ordinary buffer as a
1225                               ;; Unix mail message.
1226                               (rmail-maybe-set-message-counters)
1227                               (widen)
1228                               (narrow-to-region (point-max) (point-max))
1229                               (insert "\C-l\n0, unseen,,\n*** EOOH ***\n"
1230                                       "Date: " (mail-rfc822-date) "\n")
1231                               (insert-buffer-substring curbuf beg2 end)
1232                               (insert "\n\C-_")
1233                               (goto-char (point-min))
1234                               (widen)
1235                               (search-backward "\n\^_")
1236                               (narrow-to-region (point) (point-max))
1237                               (rmail-count-new-messages t)
1238                               (rmail-show-message msg)
1239                               (setq max nil))
1240                           ;; Output file not in rmail mode
1241                           ;; => just insert at the end.
1242                           (narrow-to-region (point-min) (1+ (buffer-size)))
1243                           (goto-char (point-max))
1244                           (insert-buffer-substring curbuf beg end))
1245                         (or buffer-matches-file
1246                             (progn
1247                               (if (y-or-n-p (format "Save file %s? "
1248                                                     fcc))
1249                                   (save-buffer))
1250                               (setq dont-write-the-file t))))
1251                     (if max (narrow-to-region (point-min) max))))))
1252           ;; Append to the file directly,
1253           ;; unless we've already taken care of it.
1254           (unless dont-write-the-file
1255             (if (and (file-exists-p fcc)
1256                      ;; Check that the file isn't empty.  We don't
1257                      ;; want to insert a newline at the start of an
1258                      ;; empty file.
1259                      (not (zerop (nth 7 (file-attributes fcc))))
1260                      (mail-file-babyl-p fcc))
1261                 ;; If the file is a Babyl file,
1262                 ;; convert the message to Babyl format.
1263                 (let ((coding-system-for-write
1264                        (or rmail-file-coding-system
1265                            'emacs-mule)))
1266                   (with-current-buffer (get-buffer-create " mail-temp")
1267                     (setq buffer-read-only nil)
1268                     (erase-buffer)
1269                     (insert "\C-l\n0, unseen,,\n*** EOOH ***\nDate: "
1270                             (mail-rfc822-date) "\n")
1271                     (insert-buffer-substring curbuf beg2 end)
1272                     (insert "\n\C-_")
1273                     (write-region (point-min) (point-max) fcc t)
1274                     (erase-buffer)))
1275               (write-region
1276                (1+ (point-min)) (point-max) fcc t)))
1277           (and buffer (not dont-write-the-file)
1278                (with-current-buffer buffer
1279                  (set-visited-file-modtime))))))
1280     (kill-buffer tembuf)))
1281
1282 (defun mail-sent-via ()
1283   "Make a Sent-via header line from each To or CC header line."
1284   (interactive)
1285   (save-excursion
1286     ;; put a marker at the end of the header
1287     (let ((end (copy-marker (mail-header-end)))
1288           (case-fold-search t))
1289       (goto-char (point-min))
1290       ;; search for the To: lines and make Sent-via: lines from them
1291       ;; search for the next To: line
1292       (while (re-search-forward "^\\(to\\|cc\\):" end t)
1293         ;; Grab this line plus all its continuations, sans the `to:'.
1294         (let ((to-line
1295                (buffer-substring (point)
1296                                  (progn
1297                                    (if (re-search-forward "^[^ \t\n]" end t)
1298                                        (backward-char 1)
1299                                      (goto-char end))
1300                                    (point)))))
1301           ;; Insert a copy, with altered header field name.
1302           (insert-before-markers "Sent-via:" to-line))))))
1303 \f
1304 (defun mail-to ()
1305   "Move point to end of To-field."
1306   (interactive)
1307   (expand-abbrev)
1308   (mail-position-on-field "To"))
1309
1310 (defun mail-subject ()
1311   "Move point to end of Subject-field."
1312   (interactive)
1313   (expand-abbrev)
1314   (mail-position-on-field "Subject"))
1315
1316 (defun mail-cc ()
1317   "Move point to end of CC-field.  Create a CC field if none."
1318   (interactive)
1319   (expand-abbrev)
1320   (or (mail-position-on-field "cc" t)
1321       (progn (mail-position-on-field "to")
1322              (insert "\nCC: "))))
1323
1324 (defun mail-bcc ()
1325   "Move point to end of BCC-field.  Create a BCC field if none."
1326   (interactive)
1327   (expand-abbrev)
1328   (or (mail-position-on-field "bcc" t)
1329       (progn (mail-position-on-field "to")
1330              (insert "\nBCC: "))))
1331
1332 (defun mail-fcc (folder)
1333   "Add a new FCC field, with file name completion."
1334   (interactive "FFolder carbon copy: ")
1335   (expand-abbrev)
1336   (or (mail-position-on-field "fcc" t)  ;Put new field after exiting FCC.
1337       (mail-position-on-field "to"))
1338   (insert "\nFCC: " folder))
1339
1340 (defun mail-reply-to ()
1341   "Move point to end of Reply-To-field.  Create a Reply-To field if none."
1342   (interactive)
1343   (expand-abbrev)
1344   (mail-position-on-field "Reply-To"))
1345
1346 (defun mail-mail-reply-to ()
1347   "Move point to end of Mail-Reply-To field.
1348 Create a Mail-Reply-To field if none."
1349   (interactive)
1350   (expand-abbrev)
1351   (or (mail-position-on-field "mail-reply-to" t)
1352       (progn (mail-position-on-field "to")
1353            (insert "\nMail-Reply-To: "))))
1354
1355 (defun mail-mail-followup-to ()
1356   "Move point to end of Mail-Followup-To field.
1357 Create a Mail-Followup-To field if none."
1358   (interactive)
1359   (expand-abbrev)
1360   (or (mail-position-on-field "mail-followup-to" t)
1361       (progn (mail-position-on-field "to")
1362            (insert "\nMail-Followup-To: "))))
1363
1364 (defun mail-position-on-field (field &optional soft)
1365   (let (end
1366         (case-fold-search t))
1367     (setq end (mail-header-end))
1368     (goto-char (point-min))
1369     (if (re-search-forward (concat "^" (regexp-quote field) ":") end t)
1370         (progn
1371           (re-search-forward "^[^ \t]" nil 'move)
1372           (beginning-of-line)
1373           (skip-chars-backward "\n")
1374           t)
1375       (or soft
1376           (progn (goto-char end)
1377                  (insert field ": \n")
1378                  (skip-chars-backward "\n")))
1379       nil)))
1380
1381 (defun mail-text ()
1382   "Move point to beginning of message text."
1383   (interactive)
1384   (expand-abbrev)
1385   (goto-char (mail-text-start)))
1386 \f
1387 (defun mail-signature (&optional atpoint)
1388   "Sign letter with signature based on `mail-signature-file'.
1389 Prefix arg means put contents at point."
1390   (interactive "P")
1391   (save-excursion
1392     (or atpoint
1393         (goto-char (point-max)))
1394     (skip-chars-backward " \t\n")
1395     (end-of-line)
1396     (or atpoint
1397         (delete-region (point) (point-max)))
1398     (if (stringp mail-signature)
1399         (insert mail-signature)
1400       (insert "\n\n-- \n")
1401       (insert-file-contents (expand-file-name mail-signature-file)))))
1402
1403 (defun mail-fill-yanked-message (&optional justifyp)
1404   "Fill the paragraphs of a message yanked into this one.
1405 Numeric argument means justify as well."
1406   (interactive "P")
1407   (save-excursion
1408     (goto-char (mail-text-start))
1409     (fill-individual-paragraphs (point)
1410                                 (point-max)
1411                                 justifyp
1412                                 mail-citation-prefix-regexp)))
1413
1414 (defun mail-indent-citation ()
1415   "Modify text just inserted from a message to be cited.
1416 The inserted text should be the region.
1417 When this function returns, the region is again around the modified text.
1418
1419 Normally, indent each nonblank line `mail-indentation-spaces' spaces.
1420 However, if `mail-yank-prefix' is non-nil, insert that prefix on each line."
1421   (mail-yank-clear-headers (region-beginning) (region-end))
1422   (if (null mail-yank-prefix)
1423       (indent-rigidly (region-beginning) (region-end)
1424                       mail-indentation-spaces)
1425     (save-excursion
1426       (let ((end (set-marker (make-marker) (region-end))))
1427         (goto-char (region-beginning))
1428         (while (< (point) end)
1429           (insert mail-yank-prefix)
1430           (forward-line 1))))))
1431
1432 (defun mail-yank-original (arg)
1433   "Insert the message being replied to, if any (in rmail).
1434 Puts point after the text and mark before.
1435 Normally, indents each nonblank line ARG spaces (default 3).
1436 However, if `mail-yank-prefix' is non-nil, insert that prefix on each line.
1437
1438 Just \\[universal-argument] as argument means don't indent, insert no prefix,
1439 and don't delete any header fields."
1440   (interactive "P")
1441   (if mail-reply-action
1442       (let ((start (point))
1443             (original mail-reply-action))
1444         (and (consp original) (eq (car original) 'insert-buffer)
1445              (setq original (nth 1 original)))
1446         (if (consp original)
1447             (apply (car original) (cdr original))
1448           ;; If the original message is in another window in the same frame,
1449           ;; delete that window to save screen space.
1450           ;; t means don't alter other frames.
1451           (delete-windows-on original t)
1452           (with-no-warnings
1453             ;; We really want this to set mark.
1454             (insert-buffer original))
1455           (set-text-properties (point) (mark t) nil))
1456         (if (consp arg)
1457             nil
1458           (goto-char start)
1459           (let ((mail-indentation-spaces (if arg (prefix-numeric-value arg)
1460                                            mail-indentation-spaces))
1461                 ;; Avoid error in Transient Mark mode
1462                 ;; on account of mark's being inactive.
1463                 (mark-even-if-inactive t))
1464             (cond (mail-citation-hook
1465                    ;; Bind mail-citation-header to the inserted
1466                    ;; message's header.
1467                    (let ((mail-citation-header
1468                           (buffer-substring-no-properties
1469                            start
1470                            (save-excursion
1471                              (save-restriction
1472                                (narrow-to-region start (point-max))
1473                                (goto-char start)
1474                                (rfc822-goto-eoh)
1475                                (point))))))
1476                      (run-hooks 'mail-citation-hook)))
1477                   (mail-yank-hooks
1478                    (run-hooks 'mail-yank-hooks))
1479                   (t
1480                    (mail-indent-citation)))))
1481         ;; This is like exchange-point-and-mark, but doesn't activate the mark.
1482         ;; It is cleaner to avoid activation, even though the command
1483         ;; loop would deactivate the mark because we inserted text.
1484         (goto-char (prog1 (mark t)
1485                      (set-marker (mark-marker) (point) (current-buffer))))
1486         (if (not (eolp)) (insert ?\n)))))
1487
1488 (defun mail-yank-clear-headers (start end)
1489   (if (< end start)
1490       (let (temp)
1491         (setq temp start start end end temp)))
1492   (if mail-yank-ignored-headers
1493       (save-excursion
1494         (goto-char start)
1495         (if (search-forward "\n\n" end t)
1496             (save-restriction
1497               (narrow-to-region start (point))
1498               (goto-char start)
1499               (while (let ((case-fold-search t))
1500                        (re-search-forward mail-yank-ignored-headers nil t))
1501                 (beginning-of-line)
1502                 (delete-region (point)
1503                                (progn (re-search-forward "\n[^ \t]")
1504                                       (forward-char -1)
1505                                       (point)))))))))
1506
1507 (defun mail-yank-region (arg)
1508   "Insert the selected region from the message being replied to.
1509 Puts point after the text and mark before.
1510 Normally, indents each nonblank line ARG spaces (default 3).
1511 However, if `mail-yank-prefix' is non-nil, insert that prefix on each line.
1512
1513 Just \\[universal-argument] as argument means don't indent, insert no prefix,
1514 and don't delete any header fields."
1515   (interactive "P")
1516   (and (consp mail-reply-action)
1517        (eq (car mail-reply-action) 'insert-buffer)
1518        (with-current-buffer (nth 1 mail-reply-action)
1519          (or (mark t)
1520              (error "No mark set: %S" (current-buffer))))
1521        (let ((buffer (nth 1 mail-reply-action))
1522              (start (point))
1523              ;; Avoid error in Transient Mark mode
1524              ;; on account of mark's being inactive.
1525              (mark-even-if-inactive t))
1526          ;; Insert the citation text.
1527          (insert (with-current-buffer buffer
1528                    (buffer-substring-no-properties (point) (mark))))
1529          (push-mark start)
1530          ;; Indent or otherwise annotate the citation text.
1531          (if (consp arg)
1532              nil
1533            (let ((mail-indentation-spaces (if arg (prefix-numeric-value arg)
1534                                             mail-indentation-spaces)))
1535              (if mail-citation-hook
1536                  ;; Bind mail-citation-hook to the original message's header.
1537                  (let ((mail-citation-header
1538                         (with-current-buffer buffer
1539                           (buffer-substring-no-properties
1540                            (point-min)
1541                            (save-excursion
1542                              (goto-char (point-min))
1543                              (rfc822-goto-eoh)
1544                              (point))))))
1545                    (run-hooks 'mail-citation-hook))
1546                (if mail-yank-hooks
1547                    (run-hooks 'mail-yank-hooks)
1548                  (mail-indent-citation))))))))
1549
1550 (defun mail-split-line ()
1551   "Split current line, moving portion beyond point vertically down.
1552 If the current line has `mail-yank-prefix', insert it on the new line."
1553   (interactive "*")
1554   (split-line mail-yank-prefix))
1555
1556 \f
1557 (defun mail-attach-file (&optional file)
1558   "Insert a file at the end of the buffer, with separator lines around it."
1559   (interactive "fAttach file: ")
1560   (save-excursion
1561     (goto-char (point-max))
1562     (or (bolp) (newline))
1563     (newline)
1564     (let ((start (point))
1565           middle)
1566       (insert (format "===File %s===" file))
1567       (insert-char ?= (max 0 (- 60 (current-column))))
1568       (newline)
1569       (setq middle (point))
1570       (insert "============================================================\n")
1571       (push-mark)
1572       (goto-char middle)
1573       (insert-file-contents file)
1574       (or (bolp) (newline))
1575       (goto-char start))))
1576 \f
1577 ;; Put these commands last, to reduce chance of lossage from quitting
1578 ;; in middle of loading the file.
1579
1580 ;;;###autoload (add-hook 'same-window-buffer-names "*mail*")
1581
1582 ;;;###autoload
1583 (defun mail (&optional noerase to subject in-reply-to cc replybuffer actions)
1584   "Edit a message to be sent.  Prefix arg means resume editing (don't erase).
1585 When this function returns, the buffer `*mail*' is selected.
1586 The value is t if the message was newly initialized; otherwise, nil.
1587
1588 Optionally, the signature file `mail-signature-file' can be inserted at the
1589 end; see the variable `mail-signature'.
1590
1591 \\<mail-mode-map>
1592 While editing message, type \\[mail-send-and-exit] to send the message and exit.
1593
1594 Various special commands starting with C-c are available in sendmail mode
1595 to move to message header fields:
1596 \\{mail-mode-map}
1597
1598 If `mail-self-blind' is non-nil, a BCC to yourself is inserted
1599 when the message is initialized.
1600
1601 If `mail-default-reply-to' is non-nil, it should be an address (a string);
1602 a Reply-to: field with that address is inserted.
1603
1604 If `mail-archive-file-name' is non-nil, an FCC field with that file name
1605 is inserted.
1606
1607 The normal hook `mail-setup-hook' is run after the message is
1608 initialized.  It can add more default fields to the message.
1609
1610 The first argument, NOERASE, determines what to do when there is
1611 an existing modified `*mail*' buffer.  If NOERASE is nil, the
1612 existing mail buffer is used, and the user is prompted whether to
1613 keep the old contents or to erase them.  If NOERASE has the value
1614 `new', a new mail buffer will be created instead of using the old
1615 one.  Any other non-nil value means to always select the old
1616 buffer without erasing the contents.
1617
1618 The second through fifth arguments,
1619  TO, SUBJECT, IN-REPLY-TO and CC, specify if non-nil
1620  the initial contents of those header fields.
1621  These arguments should not have final newlines.
1622 The sixth argument REPLYBUFFER is a buffer which contains an
1623  original message being replied to, or else an action
1624  of the form (FUNCTION . ARGS) which says how to insert the original.
1625  Or it can be nil, if not replying to anything.
1626 The seventh argument ACTIONS is a list of actions to take
1627  if/when the message is sent.  Each action looks like (FUNCTION . ARGS);
1628  when the message is sent, we apply FUNCTION to ARGS.
1629  This is how Rmail arranges to mark messages `answered'."
1630   (interactive "P")
1631 ;;;  This is commented out because I found it was confusing in practice.
1632 ;;;  It is easy enough to rename *mail* by hand with rename-buffer
1633 ;;;  if you want to have multiple mail buffers.
1634 ;;;  And then you can control which messages to save. --rms.
1635 ;;;  (let ((index 1)
1636 ;;;     buffer)
1637 ;;;    ;; If requested, look for a mail buffer that is modified and go to it.
1638 ;;;    (if noerase
1639 ;;;     (progn
1640 ;;;       (while (and (setq buffer
1641 ;;;                         (get-buffer (if (= 1 index) "*mail*"
1642 ;;;                                       (format "*mail*<%d>" index))))
1643 ;;;                   (not (buffer-modified-p buffer)))
1644 ;;;         (setq index (1+ index)))
1645 ;;;       (if buffer (switch-to-buffer buffer)
1646 ;;;         ;; If none exists, start a new message.
1647 ;;;         ;; This will never re-use an existing unmodified mail buffer
1648 ;;;         ;; (since index is not 1 anymore).  Perhaps it should.
1649 ;;;         (setq noerase nil))))
1650 ;;;    ;; Unless we found a modified message and are happy, start a new message.
1651 ;;;    (if (not noerase)
1652 ;;;     (progn
1653 ;;;       ;; Look for existing unmodified mail buffer.
1654 ;;;       (while (and (setq buffer
1655 ;;;                         (get-buffer (if (= 1 index) "*mail*"
1656 ;;;                                       (format "*mail*<%d>" index))))
1657 ;;;                   (buffer-modified-p buffer))
1658 ;;;         (setq index (1+ index)))
1659 ;;;       ;; If none, make a new one.
1660 ;;;       (or buffer
1661 ;;;           (setq buffer (generate-new-buffer "*mail*")))
1662 ;;;       ;; Go there and initialize it.
1663 ;;;       (switch-to-buffer buffer)
1664 ;;;       (erase-buffer)
1665 ;;;          (setq default-directory (expand-file-name "~/"))
1666 ;;;          (auto-save-mode auto-save-default)
1667 ;;;          (mail-mode)
1668 ;;;          (mail-setup to subject in-reply-to cc replybuffer actions)
1669 ;;;       (if (and buffer-auto-save-file-name
1670 ;;;                (file-exists-p buffer-auto-save-file-name))
1671 ;;;           (message "Auto save file for draft message exists; consider M-x mail-recover"))
1672 ;;;          t))
1673
1674   (if (eq noerase 'new)
1675       (pop-to-buffer (generate-new-buffer "*mail*"))
1676     (and noerase
1677          (not (get-buffer "*mail*"))
1678          (setq noerase nil))
1679     (pop-to-buffer "*mail*"))
1680
1681   ;; Avoid danger that the auto-save file can't be written.
1682   (let ((dir (expand-file-name
1683               (file-name-as-directory mail-default-directory))))
1684     (if (file-exists-p dir)
1685         (setq default-directory dir)))
1686   ;; Only call auto-save-mode if necessary, to avoid changing auto-save file.
1687   (if (or (and auto-save-default (not buffer-auto-save-file-name))
1688           (and (not auto-save-default) buffer-auto-save-file-name))
1689       (auto-save-mode auto-save-default))
1690   (mail-mode)
1691   ;; Disconnect the buffer from its visited file
1692   ;; (in case the user has actually visited a file *mail*).
1693 ;;;  (set-visited-file-name nil)
1694   (let (initialized)
1695     (and (not (and noerase
1696                    (not (eq noerase 'new))))
1697          (if buffer-file-name
1698              (if (buffer-modified-p)
1699                  (when (y-or-n-p "Buffer has unsaved changes; reinitialize it and discard them? ")
1700                    (if (y-or-n-p "Disconnect buffer from visited file? ")
1701                        (set-visited-file-name nil))
1702                    t)
1703                (when (y-or-n-p "Reinitialize buffer, and disconnect it from the visited file? ")
1704                  (set-visited-file-name nil)
1705                  t))
1706            ;; A non-file-visiting buffer.
1707            (if (buffer-modified-p)
1708                (y-or-n-p "Unsent message being composed; erase it? ")
1709              t))
1710          (let ((inhibit-read-only t))
1711            (erase-buffer)
1712            (mail-setup to subject in-reply-to cc replybuffer actions)
1713            (setq initialized t)))
1714     (if (and buffer-auto-save-file-name
1715              (file-exists-p buffer-auto-save-file-name))
1716         (message "Auto save file for draft message exists; consider M-x mail-recover"))
1717     initialized))
1718
1719 (defun mail-recover-1 ()
1720   "Pop up a list of auto-saved draft messages so you can recover one of them."
1721   (interactive)
1722   (let ((file-name (make-auto-save-file-name))
1723         (ls-lisp-support-shell-wildcards t)
1724         non-random-len wildcard)
1725     ;; Remove the random part from the auto-save-file-name, and
1726     ;; create a wildcard which matches possible candidates.
1727     ;; Note: this knows that make-auto-save-file-name appends
1728     ;; "#<RANDOM-STUFF>#" to the buffer name, where RANDOM-STUFF
1729     ;; is the result of (make-temp-name "").
1730     (setq non-random-len
1731           (- (length file-name) (length (make-temp-name "")) 1))
1732     (setq wildcard (concat (substring file-name 0 non-random-len) "*"))
1733     (if (null (file-expand-wildcards wildcard))
1734         (message "There are no auto-saved drafts to recover")
1735       ;; Bind dired-trivial-filenames to t because all auto-save file
1736       ;; names are normally ``trivial'', so Dired will set point after
1737       ;; all the files, at buffer bottom.  We want it on the first
1738       ;; file instead.
1739       (let ((dired-trivial-filenames t))
1740         (dired-other-window wildcard (concat dired-listing-switches "t")))
1741       (rename-buffer "*Auto-saved Drafts*" t)
1742       (save-excursion
1743         (goto-char (point-min))
1744         (or (looking-at " Move to the draft file you want to recover,")
1745             (let ((inhibit-read-only t))
1746               ;; Each line starts with a space so that Font Lock mode
1747               ;; won't highlight the first character.
1748               (insert "\
1749  Move to the draft file you want to recover, then type C-c C-c
1750  to recover text of message whose composition was interrupted.
1751  To browse text of a draft, type v on the draft file's line.
1752
1753  You can also delete some of these files;
1754  type d on a line to mark that file for deletion.
1755
1756  List of possible auto-save files for recovery:
1757
1758 "))))
1759       (use-local-map
1760        (let ((map (make-sparse-keymap)))
1761          (set-keymap-parent map (current-local-map))
1762          map))
1763       (define-key (current-local-map) "v"
1764         (lambda ()
1765           (interactive)
1766           (let ((coding-system-for-read 'emacs-mule-unix))
1767             (dired-view-file))))
1768       (define-key (current-local-map) "\C-c\C-c"
1769         (lambda ()
1770           (interactive)
1771           (let ((fname (dired-get-filename))
1772                 ;; Auto-saved files are written in the internal
1773                 ;; representation, so they should be read accordingly.
1774                 (coding-system-for-read 'emacs-mule-unix))
1775             (switch-to-buffer-other-window "*mail*")
1776             (let ((buffer-read-only nil))
1777               (erase-buffer)
1778               (insert-file-contents fname nil)
1779               ;; insert-file-contents will set buffer-file-coding-system
1780               ;; to emacs-mule, which is probably not what they want to
1781               ;; use for sending the message.  But we don't know what
1782               ;; was its value before the buffer was killed or Emacs
1783               ;; crashed.  We therefore reset buffer-file-coding-system
1784               ;; to the default value, so that either the default does
1785               ;; TRT, or the user will get prompted for the right
1786               ;; encoding when they send the message.
1787               (setq buffer-file-coding-system
1788                     default-buffer-file-coding-system))))))))
1789
1790 (defun mail-recover ()
1791   "Recover interrupted mail composition from auto-save files.
1792
1793 If the mail buffer has a current valid auto-save file,
1794 the command recovers that file.  Otherwise, it displays a
1795 buffer showing the existing auto-saved draft messages;
1796 you can move to one of them and type C-c C-c to recover that one."
1797   (interactive)
1798   ;; In case they invoke us from some random buffer...
1799   (switch-to-buffer "*mail*")
1800   ;; If *mail* didn't exist, set its directory, so that auto-saved
1801   ;; drafts will be found.
1802   (let ((dir (expand-file-name
1803               (file-name-as-directory mail-default-directory))))
1804     (if (file-exists-p dir)
1805         (setq default-directory dir)))
1806   (or (eq major-mode 'mail-mode)
1807       (mail-mode))
1808   (let ((file-name buffer-auto-save-file-name))
1809     (cond ((and file-name (file-exists-p file-name))
1810            (let ((dispbuf
1811                   ;; This used to invoke `ls' via call-process, but
1812                   ;; dired-noselect is more portable to systems where
1813                   ;; `ls' is not a standard program (it will use
1814                   ;; ls-lisp instead).
1815                   (dired-noselect file-name
1816                                   (concat dired-listing-switches "t"))))
1817              (save-selected-window
1818                (select-window (display-buffer dispbuf t))
1819                (goto-char (point-min))
1820                (forward-line 2)
1821                (dired-move-to-filename)
1822                (setq dispbuf (rename-buffer "*Directory*" t)))
1823              (if (not (yes-or-no-p
1824                        (format "Recover mail draft from auto save file %s? "
1825                                file-name)))
1826                  (error "mail-recover cancelled")
1827                (let ((buffer-read-only nil)
1828                      (buffer-coding buffer-file-coding-system)
1829                      ;; Auto-save files are written in internal
1830                      ;; representation of non-ASCII characters.
1831                      (coding-system-for-read 'emacs-mule-unix))
1832                  (erase-buffer)
1833                  (insert-file-contents file-name nil)
1834                  (setq buffer-file-coding-system buffer-coding)))))
1835           (t (mail-recover-1)))))
1836
1837 ;;;###autoload
1838 (defun mail-other-window (&optional noerase to subject in-reply-to cc replybuffer sendactions)
1839   "Like `mail' command, but display mail buffer in another window."
1840   (interactive "P")
1841   (let ((pop-up-windows t)
1842         (special-display-buffer-names nil)
1843         (special-display-regexps nil)
1844         (same-window-buffer-names nil)
1845         (same-window-regexps nil))
1846     (pop-to-buffer "*mail*"))
1847   (mail noerase to subject in-reply-to cc replybuffer sendactions))
1848
1849 ;;;###autoload
1850 (defun mail-other-frame (&optional noerase to subject in-reply-to cc replybuffer sendactions)
1851   "Like `mail' command, but display mail buffer in another frame."
1852   (interactive "P")
1853   (let ((pop-up-frames t)
1854         (special-display-buffer-names nil)
1855         (special-display-regexps nil)
1856         (same-window-buffer-names nil)
1857         (same-window-regexps nil))
1858     (pop-to-buffer "*mail*"))
1859   (mail noerase to subject in-reply-to cc replybuffer sendactions))
1860
1861 ;; Do not add anything but external entries on this page.
1862
1863 (provide 'sendmail)
1864
1865 ;; arch-tag: 48bc1025-d993-4d31-8d81-2a29491f0626
1866 ;;; sendmail.el ends here