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