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