Small fixes for dired-listing-switches usage.
[emacs.git] / lisp / mail / sendmail.el
blobf4ef9b91903de18484701544960d80df6c188fc0
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 ;; 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."
474 "24.1" 'set)
476 (defvar mail-mode-syntax-table
477 ;; define-derived-mode will make it inherit from text-mode-syntax-table.
478 (let ((st (make-syntax-table)))
479 ;; FIXME this is probably very obsolete now ("percent hack").
480 ;; sending.texi used to say:
481 ;; Mail mode defines the character `%' as a word separator; this
482 ;; is helpful for using the word commands to edit mail addresses.
483 (modify-syntax-entry ?% ". " st)
485 "Syntax table used while in `mail-mode'.")
487 (defvar mail-font-lock-keywords
488 (eval-when-compile
489 (let* ((cite-chars "[>|}]")
490 (cite-prefix "[:alpha:]")
491 (cite-suffix (concat cite-prefix "0-9_.@-`'\"")))
492 (list '("^\\(To\\|Newsgroups\\):" . font-lock-function-name-face)
493 '("^\\(B?CC\\|Reply-to\\|Mail-\\(reply\\|followup\\)-to\\):" . font-lock-keyword-face)
494 '("^\\(Subject:\\)[ \t]*\\(.+\\)?"
495 (1 font-lock-comment-face)
496 ;; (2 font-lock-type-face nil t)
498 ;; Use EVAL to delay in case `mail-header-separator' gets changed.
499 '(eval .
500 (let ((separator (if (zerop (length mail-header-separator))
501 " \\`\\' "
502 (regexp-quote mail-header-separator))))
503 (cons (concat "^" separator "$") 'font-lock-warning-face)))
504 ;; Use MATCH-ANCHORED to effectively anchor the regexp left side.
505 `(,cite-chars
506 (,(concat "\\=[ \t]*"
507 "\\(\\(\\([" cite-prefix "]+[" cite-suffix "]*\\)?"
508 "\\(" cite-chars "[ \t]*\\)\\)+\\)"
509 "\\(.*\\)")
510 (beginning-of-line) (end-of-line)
511 (1 font-lock-comment-delimiter-face nil t)
512 (5 font-lock-comment-face nil t)))
513 '("^\\(X-[A-Za-z0-9-]+\\|In-reply-to\\):.*\\(\n[ \t]+.*\\)*$"
514 . font-lock-string-face))))
515 "Additional expressions to highlight in Mail mode.")
518 (defun sendmail-sync-aliases ()
519 (when mail-personal-alias-file
520 (let ((modtime (nth 5 (file-attributes mail-personal-alias-file))))
521 (or (equal mail-alias-modtime modtime)
522 (setq mail-alias-modtime modtime
523 mail-aliases t)))))
526 ;;;###autoload
527 (define-mail-user-agent 'sendmail-user-agent
528 'sendmail-user-agent-compose
529 'mail-send-and-exit)
531 ;;;###autoload
532 (defun sendmail-user-agent-compose (&optional to subject other-headers
533 continue switch-function yank-action
534 send-actions return-action
535 &rest ignored)
536 (if switch-function
537 (let ((special-display-buffer-names nil)
538 (special-display-regexps nil)
539 (same-window-buffer-names nil)
540 (same-window-regexps nil))
541 (funcall switch-function "*mail*")))
542 (let ((cc (cdr (assoc-string "cc" other-headers t)))
543 (in-reply-to (cdr (assoc-string "in-reply-to" other-headers t)))
544 (body (cdr (assoc-string "body" other-headers t))))
545 (or (mail continue to subject in-reply-to cc yank-action
546 send-actions return-action)
547 continue
548 (error "Message aborted"))
549 (save-excursion
550 (rfc822-goto-eoh)
551 (while other-headers
552 (unless (member-ignore-case (car (car other-headers))
553 '("in-reply-to" "cc" "body"))
554 (insert (car (car other-headers)) ": "
555 (cdr (car other-headers))
556 (if use-hard-newlines hard-newline "\n")))
557 (setq other-headers (cdr other-headers)))
558 (when body
559 (forward-line 1)
560 (insert body))
561 t)))
563 (defun mail-setup (to subject in-reply-to cc replybuffer
564 actions return-action)
565 (or mail-default-reply-to
566 (setq mail-default-reply-to (getenv "REPLYTO")))
567 (sendmail-sync-aliases)
568 (when (eq mail-aliases t)
569 (setq mail-aliases nil)
570 (and mail-personal-alias-file
571 (file-exists-p mail-personal-alias-file)
572 (build-mail-aliases)))
573 ;; Don't leave this around from a previous message.
574 (kill-local-variable 'buffer-file-coding-system)
575 ;; This doesn't work for enable-multibyte-characters.
576 ;; (kill-local-variable 'enable-multibyte-characters)
577 (set-buffer-multibyte (default-value 'enable-multibyte-characters))
578 (if current-input-method
579 (inactivate-input-method))
581 ;; Local variables for Mail mode.
582 (setq mail-send-actions actions)
583 (setq mail-reply-action replybuffer)
584 (setq mail-return-action return-action)
586 (goto-char (point-min))
587 (if mail-setup-with-from
588 (mail-insert-from-field))
589 (insert "To: ")
590 (save-excursion
591 (if to
592 ;; Here removed code to extract names from within <...>
593 ;; on the assumption that mail-strip-quoted-names
594 ;; has been called and has done so.
595 (let ((fill-prefix "\t")
596 (address-start (point)))
597 (insert to "\n")
598 (fill-region-as-paragraph address-start (point-max))
599 (goto-char (point-max))
600 (unless (bolp)
601 (newline)))
602 (newline))
603 (if cc
604 (let ((fill-prefix "\t")
605 (address-start (progn (insert "CC: ") (point))))
606 (insert cc "\n")
607 (fill-region-as-paragraph address-start (point-max))
608 (goto-char (point-max))
609 (unless (bolp)
610 (newline))))
611 (if in-reply-to
612 (let ((fill-prefix "\t")
613 (fill-column 78)
614 (address-start (point)))
615 (insert "In-reply-to: " in-reply-to "\n")
616 (fill-region-as-paragraph address-start (point-max))
617 (goto-char (point-max))
618 (unless (bolp)
619 (newline))))
620 (insert "Subject: " (or subject "") "\n")
621 (if mail-default-headers
622 (insert mail-default-headers))
623 (if mail-default-reply-to
624 (insert "Reply-to: " mail-default-reply-to "\n"))
625 (if mail-self-blind
626 (insert "BCC: " user-mail-address "\n"))
627 (if mail-archive-file-name
628 (insert "FCC: " mail-archive-file-name "\n"))
629 (put-text-property (point)
630 (progn
631 (insert mail-header-separator "\n")
632 (1- (point)))
633 'category 'mail-header-separator)
634 ;; Insert the signature. But remember the beginning of the message.
635 (if to (setq to (point)))
636 (if mail-signature (mail-signature t))
637 (goto-char (point-max))
638 (or (bolp) (newline)))
639 (if to (goto-char to))
640 (or to subject in-reply-to
641 (set-buffer-modified-p nil))
642 (run-hooks 'mail-setup-hook))
644 (defcustom mail-mode-hook nil
645 "Hook run by Mail mode.
646 When composing a mail, this runs immediately after creating, or
647 switching to, the `*mail*' buffer. See also `mail-setup-hook'."
648 :group 'sendmail
649 :type 'hook
650 :options '(footnote-mode))
652 (defvar mail-mode-abbrev-table text-mode-abbrev-table)
653 ;;;###autoload
654 (define-derived-mode mail-mode text-mode "Mail"
655 "Major mode for editing mail to be sent.
656 Like Text Mode but with these additional commands:
658 \\[mail-send] mail-send (send the message)
659 \\[mail-send-and-exit] mail-send-and-exit (send the message and exit)
661 Here are commands that move to a header field (and create it if there isn't):
662 \\[mail-to] move to To: \\[mail-subject] move to Subj:
663 \\[mail-bcc] move to BCC: \\[mail-cc] move to CC:
664 \\[mail-fcc] move to FCC: \\[mail-reply-to] move to Reply-To:
665 \\[mail-mail-reply-to] move to Mail-Reply-To:
666 \\[mail-mail-followup-to] move to Mail-Followup-To:
667 \\[mail-text] move to message text.
668 \\[mail-signature] mail-signature (insert `mail-signature-file' file).
669 \\[mail-yank-original] mail-yank-original (insert current message, in Rmail).
670 \\[mail-fill-yanked-message] mail-fill-yanked-message (fill what was yanked).
671 Turning on Mail mode runs the normal hooks `text-mode-hook' and
672 `mail-mode-hook' (in that order)."
673 (make-local-variable 'mail-reply-action)
674 (make-local-variable 'mail-send-actions)
675 (make-local-variable 'mail-return-action)
676 (setq buffer-offer-save t)
677 (make-local-variable 'font-lock-defaults)
678 (setq font-lock-defaults '(mail-font-lock-keywords t t))
679 (make-local-variable 'paragraph-separate)
680 (make-local-variable 'normal-auto-fill-function)
681 (setq normal-auto-fill-function 'mail-mode-auto-fill)
682 (make-local-variable 'fill-paragraph-function)
683 (setq fill-paragraph-function 'mail-mode-fill-paragraph)
684 ;; Allow using comment commands to add/remove quoting (this only does
685 ;; anything if mail-yank-prefix is set to a non-nil value).
686 (set (make-local-variable 'comment-start) mail-yank-prefix)
687 (if mail-yank-prefix
688 (set (make-local-variable 'comment-start-skip)
689 (concat "^" (regexp-quote mail-yank-prefix) "[ \t]*")))
690 (make-local-variable 'adaptive-fill-regexp)
691 (setq adaptive-fill-regexp
692 (concat "[ \t]*[-[:alnum:]]+>+[ \t]*\\|"
693 adaptive-fill-regexp))
694 (make-local-variable 'adaptive-fill-first-line-regexp)
695 (setq adaptive-fill-first-line-regexp
696 (concat "[ \t]*[-[:alnum:]]*>+[ \t]*\\|"
697 adaptive-fill-first-line-regexp))
698 (add-hook 'completion-at-point-functions #'mail-completion-at-point-function
699 nil 'local)
700 ;; `-- ' precedes the signature. `-----' appears at the start of the
701 ;; lines that delimit forwarded messages.
702 ;; Lines containing just >= 3 dashes, perhaps after whitespace,
703 ;; are also sometimes used and should be separators.
704 (setq paragraph-separate (concat (regexp-quote mail-header-separator)
705 "$\\|\t*\\([-|#;>* ]\\|(?[0-9]+[.)]\\)+$"
706 "\\|[ \t]*[[:alnum:]]*>+[ \t]*$\\|[ \t]*$\\|"
707 "--\\( \\|-+\\)$\\|"
708 page-delimiter)))
711 (defun mail-header-end ()
712 "Return the buffer location of the end of headers, as a number."
713 (save-restriction
714 (widen)
715 (save-excursion
716 (rfc822-goto-eoh)
717 (point))))
719 (defun mail-text-start ()
720 "Return the buffer location of the start of text, as a number."
721 (save-restriction
722 (widen)
723 (save-excursion
724 (rfc822-goto-eoh)
725 (forward-line 1)
726 (point))))
728 (defun mail-sendmail-delimit-header ()
729 "Set up whatever header delimiter convention sendmail will use.
730 Concretely: replace the first blank line in the header with the separator."
731 (rfc822-goto-eoh)
732 (insert mail-header-separator)
733 (point))
735 (defun mail-sendmail-undelimit-header ()
736 "Remove header separator to put the message in correct form for sendmail.
737 Leave point at the start of the delimiter line."
738 (rfc822-goto-eoh)
739 (delete-region (point) (progn (end-of-line) (point))))
741 (defun mail-mode-auto-fill ()
742 "Carry out Auto Fill for Mail mode.
743 If within the headers, this makes the new lines into continuation lines."
744 (if (< (point) (mail-header-end))
745 (let ((old-line-start (line-beginning-position)))
746 (if (do-auto-fill)
747 (save-excursion
748 (beginning-of-line)
749 (while (not (eq (point) old-line-start))
750 ;; Use insert-before-markers in case we're inserting
751 ;; before the saved value of point (which is common).
752 (insert-before-markers " ")
753 (forward-line -1))
754 t)))
755 (do-auto-fill)))
757 (defun mail-mode-fill-paragraph (arg)
758 ;; Do something special only if within the headers.
759 (if (< (point) (mail-header-end))
760 (let (beg end fieldname)
761 (when (prog1 (re-search-backward "^[-a-zA-Z]+:" nil 'yes)
762 (setq beg (point)))
763 (setq fieldname
764 (downcase (buffer-substring beg (1- (match-end 0))))))
765 (forward-line 1)
766 ;; Find continuation lines and get rid of their continuation markers.
767 (while (looking-at "[ \t]")
768 (delete-horizontal-space)
769 (forward-line 1))
770 (setq end (point-marker))
771 (goto-char beg)
772 ;; If this field contains addresses,
773 ;; make sure we can fill after each address.
774 (if (member fieldname
775 '("to" "cc" "bcc" "from" "reply-to"
776 "mail-reply-to" "mail-followup-to"
777 "resent-to" "resent-cc" "resent-bcc"
778 "resent-from" "resent-reply-to"))
779 (while (search-forward "," end t)
780 (or (looking-at "[ \t]")
781 (insert " "))))
782 (fill-region-as-paragraph beg end arg)
783 ;; Mark all lines except the first as continuations.
784 (goto-char beg)
785 (forward-line 1)
786 (while (< (point) end)
787 (insert " ")
788 (forward-line 1))
789 (move-marker end nil)
790 t)))
792 ;; User-level commands for sending.
794 (defun mail-send-and-exit (&optional arg)
795 "Send message like `mail-send', then, if no errors, exit from mail buffer.
796 Prefix arg means don't delete this window."
797 (interactive "P")
798 (mail-send)
799 (mail-bury arg))
801 (defun mail-dont-send (&optional arg)
802 "Don't send the message you have been editing.
803 Prefix arg means don't delete this window."
804 (interactive "P")
805 (mail-bury arg))
807 (defun mail-bury (&optional arg)
808 "Bury this mail buffer."
809 (let ((newbuf (other-buffer (current-buffer))))
810 (bury-buffer (current-buffer))
811 (if (and (null arg) mail-return-action)
812 (apply (car mail-return-action) (cdr mail-return-action))
813 (switch-to-buffer newbuf))))
815 (defcustom mail-send-hook nil
816 "Hook run just before sending a message."
817 :type 'hook
818 :options '(flyspell-mode-off)
819 :group 'sendmail)
821 ;;;###autoload
822 (defcustom mail-mailing-lists nil
823 "List of mailing list addresses the user is subscribed to.
824 The variable is used to trigger insertion of the \"Mail-Followup-To\"
825 header when sending a message to a mailing list."
826 :type '(repeat string)
827 :group 'sendmail)
830 (defun mail-send ()
831 "Send the message in the current buffer.
832 If `mail-interactive' is non-nil, wait for success indication
833 or error messages, and inform user.
834 Otherwise any failure is reported in a message back to
835 the user from the mailer."
836 (interactive)
837 (if (if buffer-file-name
838 (y-or-n-p "Send buffer contents as mail message? ")
839 (or (buffer-modified-p)
840 (y-or-n-p "Message already sent; resend? ")))
841 (let ((inhibit-read-only t)
842 (opoint (point))
843 (ml (when mail-mailing-lists
844 ;; The surrounding regexp assumes the use of
845 ;; `mail-strip-quoted-names' on addresses before matching
846 ;; Cannot deal with full RFC 822 freedom, but that is
847 ;; unlikely to be problematic.
848 (concat "\\(?:[[:space:];,]\\|\\`\\)"
849 (regexp-opt mail-mailing-lists t)
850 "\\(?:[[:space:];,]\\|\\'\\)"))))
851 ;; If there are mailing lists defined
852 (when ml
853 (save-excursion
854 (let* ((to (mail-fetch-field "to" nil t))
855 (cc (mail-fetch-field "cc" nil t))
856 (new-header-values ; To: and Cc:
857 (mail-strip-quoted-names
858 (concat to (when cc (concat ", " cc))))))
859 ;; If message goes to known mailing list ...
860 (when (string-match ml new-header-values)
861 ;; Add Mail-Followup-To if none yet
862 (unless (mail-fetch-field "mail-followup-to")
863 (goto-char (mail-header-end))
864 (insert "Mail-Followup-To: "
865 (let ((l))
866 (mapc
867 ;; remove duplicates
868 (lambda (e)
869 (unless (member e l)
870 (push e l)))
871 (split-string new-header-values
872 ",[[:space:]]+" t))
873 (mapconcat 'identity l ", "))
874 "\n"))
875 ;; Add Mail-Reply-To if none yet
876 (unless (mail-fetch-field "mail-reply-to")
877 (goto-char (mail-header-end))
878 (insert "Mail-Reply-To: "
879 (or (mail-fetch-field "reply-to")
880 user-mail-address)
881 "\n"))))))
882 (unless (memq mail-send-nonascii '(t mime))
883 (goto-char (point-min))
884 (skip-chars-forward "\0-\177")
885 (or (= (point) (point-max))
886 (if (eq mail-send-nonascii 'query)
887 (or (y-or-n-p "Message contains non-ASCII characters; send anyway? ")
888 (error "Aborted"))
889 (error "Message contains non-ASCII characters"))))
890 ;; Complain about any invalid line.
891 (goto-char (point-min))
892 (re-search-forward (regexp-quote mail-header-separator) (point-max) t)
893 (let ((header-end (or (match-beginning 0) (point-max))))
894 (goto-char (point-min))
895 (while (< (point) header-end)
896 (unless (looking-at "[ \t]\\|.*:\\|$")
897 (push-mark opoint)
898 (error "Invalid header line (maybe a continuation line lacks initial whitespace)"))
899 (forward-line 1)))
900 (goto-char opoint)
901 (run-hooks 'mail-send-hook)
902 (message "Sending...")
903 (funcall send-mail-function)
904 ;; Now perform actions on successful sending.
905 (while mail-send-actions
906 (condition-case nil
907 (apply (car (car mail-send-actions))
908 (cdr (car mail-send-actions)))
909 (error))
910 (setq mail-send-actions (cdr mail-send-actions)))
911 (message "Sending...done")
912 ;; If buffer has no file, mark it as unmodified and delete auto-save.
913 (if (not buffer-file-name)
914 (progn
915 (set-buffer-modified-p nil)
916 (delete-auto-save-file-if-necessary t))))))
918 (defun mail-envelope-from ()
919 "Return the envelope mail address to use when sending mail.
920 This function uses `mail-envelope-from'."
921 (if (eq mail-envelope-from 'header)
922 (nth 1 (mail-extract-address-components
923 (mail-fetch-field "From")))
924 mail-envelope-from))
926 ;; This does the real work of sending a message via sendmail.
927 ;; It is called via the variable send-mail-function.
929 ;;;###autoload
930 (defvar sendmail-coding-system nil
931 "*Coding system for encoding the outgoing mail.
932 This has higher priority than the default `buffer-file-coding-system'
933 and `default-sendmail-coding-system',
934 but lower priority than the local value of `buffer-file-coding-system'.
935 See also the function `select-message-coding-system'.")
937 ;;;###autoload
938 (defvar default-sendmail-coding-system 'iso-latin-1
939 "Default coding system for encoding the outgoing mail.
940 This variable is used only when `sendmail-coding-system' is nil.
942 This variable is set/changed by the command `set-language-environment'.
943 User should not set this variable manually,
944 instead use `sendmail-coding-system' to get a constant encoding
945 of outgoing mails regardless of the current language environment.
946 See also the function `select-message-coding-system'.")
948 (defun mail-insert-from-field ()
949 "Insert the \"From:\" field of a mail header.
950 The style of the field is determined by the variable `mail-from-style'.
951 This function does not perform RFC2047 encoding."
952 (let* ((login user-mail-address)
953 (fullname (user-full-name))
954 (quote-fullname nil))
955 (if (string-match "[^\0-\177]" fullname)
956 (setq quote-fullname t))
957 (cond ((null mail-from-style)
958 (insert "From: " login "\n"))
959 ;; This is deprecated.
960 ((eq mail-from-style 'system-default)
961 nil)
962 ((or (eq mail-from-style 'angles)
963 (and (not (eq mail-from-style 'parens))
964 ;; Use angles if no quoting is needed, or if
965 ;; parens would need quoting too.
966 (or (not (string-match "[^- !#-'*+/-9=?A-Z^-~]" fullname))
967 (let ((tmp (concat fullname nil)))
968 (while (string-match "([^()]*)" tmp)
969 (aset tmp (match-beginning 0) ?-)
970 (aset tmp (1- (match-end 0)) ?-))
971 (string-match "[\\()]" tmp)))))
972 (insert "From: " fullname)
973 (let ((fullname-start (+ (point-min) 6))
974 (fullname-end (point-marker)))
975 (goto-char fullname-start)
976 ;; Look for a character that cannot appear unquoted
977 ;; according to RFC 822.
978 (if (or (re-search-forward "[^- !#-'*+/-9=?A-Z^-~]"
979 fullname-end 1)
980 quote-fullname)
981 (progn
982 ;; Quote fullname, escaping specials.
983 (goto-char fullname-start)
984 (insert "\"")
985 (while (re-search-forward "[\"\\]"
986 fullname-end 1)
987 (replace-match "\\\\\\&" t))
988 (insert "\""))))
989 (insert " <" login ">\n"))
990 ;; 'parens or default
992 (insert "From: " login " (")
993 (let ((fullname-start (point)))
994 (if quote-fullname
995 (insert "\""))
996 (insert fullname)
997 (if quote-fullname
998 (insert "\""))
999 (let ((fullname-end (point-marker)))
1000 (goto-char fullname-start)
1001 ;; RFC 822 says \ and nonmatching parentheses
1002 ;; must be escaped in comments.
1003 ;; Escape every instance of ()\ ...
1004 (while (re-search-forward "[()\\]" fullname-end 1)
1005 (replace-match "\\\\\\&" t))
1006 ;; ... then undo escaping of matching parentheses,
1007 ;; including matching nested parentheses.
1008 (goto-char fullname-start)
1009 (while (re-search-forward
1010 "\\(\\=\\|[^\\]\\(\\\\\\\\\\)*\\)\\\\(\\(\\([^\\]\\|\\\\\\\\\\)*\\)\\\\)"
1011 fullname-end 1)
1012 (replace-match "\\1(\\3)" t)
1013 (goto-char fullname-start))))
1014 (insert ")\n")))))
1016 (defun mail-encode-header (beg end)
1017 "Encode the mail header between BEG and END according to RFC2047.
1018 Return non-nil if and only if some part of the header is encoded."
1019 (save-restriction
1020 (narrow-to-region beg end)
1021 (let* ((selected (select-message-coding-system))
1022 (mm-coding-system-priorities
1023 (if (and selected (coding-system-get selected :mime-charset))
1024 (cons selected mm-coding-system-priorities)
1025 mm-coding-system-priorities))
1026 (tick (buffer-chars-modified-tick))
1027 (rfc2047-encode-encoded-words nil))
1028 (rfc2047-encode-message-header)
1029 (= tick (buffer-chars-modified-tick)))))
1031 ;; Normally you will not need to modify these options unless you are
1032 ;; using some non-genuine substitute for sendmail which does not
1033 ;; implement each and every option that the original supports.
1034 ;; E.g., ssmtp does not support "-odb", so, if your site uses it,
1035 ;; you will need to modify `sendmail-error-reporting-non-interactive'
1036 ;; in your site-init.el.
1037 (defvar sendmail-error-reporting-interactive
1038 ;; These mean "report errors to terminal" and "deliver interactively"
1039 '("-oep" "-odi"))
1040 (defvar sendmail-error-reporting-non-interactive
1041 ;; These mean "report errors by mail" and "deliver in background".
1042 '("-oem" "-odb"))
1044 (defun sendmail-send-it ()
1045 "Send the current mail buffer using the Sendmail package.
1046 This is a suitable value for `send-mail-function'. It sends using the
1047 external program defined by `sendmail-program'."
1048 (require 'mail-utils)
1049 (let ((errbuf (if mail-interactive
1050 (generate-new-buffer " sendmail errors")
1052 (tembuf (generate-new-buffer " sendmail temp"))
1053 (multibyte enable-multibyte-characters)
1054 (case-fold-search nil)
1055 (selected-coding (select-message-coding-system))
1056 resend-to-addresses
1057 delimline
1058 fcc-was-found
1059 (mailbuf (current-buffer))
1060 ;; Examine these variables now, so that
1061 ;; local binding in the mail buffer will take effect.
1062 (envelope-from
1063 (and mail-specify-envelope-from
1064 (or (mail-envelope-from) user-mail-address))))
1065 (unwind-protect
1066 (with-current-buffer tembuf
1067 (erase-buffer)
1068 (unless multibyte
1069 (set-buffer-multibyte nil))
1070 (insert-buffer-substring mailbuf)
1071 (set-buffer-file-coding-system selected-coding)
1072 (goto-char (point-max))
1073 ;; require one newline at the end.
1074 (or (= (preceding-char) ?\n)
1075 (insert ?\n))
1076 ;; Change header-delimiter to be what sendmail expects.
1077 (goto-char (mail-header-end))
1078 (delete-region (point) (progn (end-of-line) (point)))
1079 (setq delimline (point-marker))
1080 (sendmail-sync-aliases)
1081 (if mail-aliases
1082 (expand-mail-aliases (point-min) delimline))
1083 (goto-char (point-min))
1084 ;; Ignore any blank lines in the header
1085 (while (and (re-search-forward "\n\n\n*" delimline t)
1086 (< (point) delimline))
1087 (replace-match "\n"))
1088 (goto-char (point-min))
1089 ;; Look for Resent- headers. They require sending
1090 ;; the message specially.
1091 (let ((case-fold-search t))
1092 (goto-char (point-min))
1093 (while (re-search-forward "^Resent-\\(to\\|cc\\|bcc\\):" delimline t)
1094 ;; Put a list of such addresses in resend-to-addresses.
1095 (setq resend-to-addresses
1096 (save-restriction
1097 (narrow-to-region (point)
1098 (save-excursion
1099 (forward-line 1)
1100 (while (looking-at "^[ \t]")
1101 (forward-line 1))
1102 (point)))
1103 (append (mail-parse-comma-list)
1104 resend-to-addresses)))
1105 ;; Delete Resent-BCC ourselves
1106 (if (save-excursion (beginning-of-line)
1107 (looking-at "resent-bcc"))
1108 (delete-region (line-beginning-position)
1109 (line-beginning-position 2))))
1110 ;; Apparently this causes a duplicate Sender.
1111 ;; ;; If the From is different than current user, insert Sender.
1112 ;; (goto-char (point-min))
1113 ;; (and (re-search-forward "^From:" delimline t)
1114 ;; (progn
1115 ;; (require 'mail-utils)
1116 ;; (not (string-equal
1117 ;; (mail-strip-quoted-names
1118 ;; (save-restriction
1119 ;; (narrow-to-region (point-min) delimline)
1120 ;; (mail-fetch-field "From")))
1121 ;; (user-login-name))))
1122 ;; (progn
1123 ;; (forward-line 1)
1124 ;; (insert "Sender: " (user-login-name) "\n")))
1125 ;; Don't send out a blank subject line
1126 (goto-char (point-min))
1127 (if (re-search-forward "^Subject:\\([ \t]*\n\\)+\\b" delimline t)
1128 (replace-match "")
1129 ;; This one matches a Subject just before the header delimiter.
1130 (if (and (re-search-forward "^Subject:\\([ \t]*\n\\)+" delimline t)
1131 (= (match-end 0) delimline))
1132 (replace-match "")))
1133 ;; Put the "From:" field in unless for some odd reason
1134 ;; they put one in themselves.
1135 (goto-char (point-min))
1136 (if (not (re-search-forward "^From:" delimline t))
1137 (mail-insert-from-field))
1138 ;; Possibly add a MIME header for the current coding system
1139 (let (charset where-content-type)
1140 (goto-char (point-min))
1141 (setq where-content-type
1142 (re-search-forward "^Content-type:" delimline t))
1143 (goto-char (point-min))
1144 (and (eq mail-send-nonascii 'mime)
1145 (not (re-search-forward "^MIME-version:" delimline t))
1146 (progn (skip-chars-forward "\0-\177")
1147 (/= (point) (point-max)))
1148 selected-coding
1149 (setq charset
1150 (coding-system-get selected-coding :mime-charset))
1151 (progn
1152 (goto-char delimline)
1153 (insert "MIME-version: 1.0\n"
1154 "Content-type: text/plain; charset="
1155 (symbol-name charset)
1156 "\nContent-Transfer-Encoding: 8bit\n")
1157 ;; The character set we will actually use
1158 ;; should override any specified in the message itself.
1159 (when where-content-type
1160 (goto-char where-content-type)
1161 (delete-region (point-at-bol)
1162 (progn (forward-line 1) (point)))))))
1163 ;; Insert an extra newline if we need it to work around
1164 ;; Sun's bug that swallows newlines.
1165 (goto-char (1+ delimline))
1166 (if (eval mail-mailer-swallows-blank-line)
1167 (newline))
1168 ;; Find and handle any FCC fields.
1169 (goto-char (point-min))
1170 (if (re-search-forward "^FCC:" delimline t)
1171 (progn
1172 (setq fcc-was-found t)
1173 (mail-do-fcc delimline)))
1174 (if mail-interactive
1175 (with-current-buffer errbuf
1176 (erase-buffer))))
1177 ;; Encode the header according to RFC2047.
1178 (mail-encode-header (point-min) delimline)
1179 (goto-char (point-min))
1180 (if (let ((case-fold-search t))
1181 (or resend-to-addresses
1182 (re-search-forward "^To:\\|^cc:\\|^bcc:"
1183 delimline t)))
1184 (let* ((default-directory "/")
1185 (coding-system-for-write selected-coding)
1186 (args
1187 (append (list (point-min) (point-max)
1188 sendmail-program
1189 nil errbuf nil "-oi")
1190 (and envelope-from
1191 (list "-f" envelope-from))
1192 ;; ;; Don't say "from root" if running under su.
1193 ;; (and (equal (user-real-login-name) "root")
1194 ;; (list "-f" (user-login-name)))
1195 (and mail-alias-file
1196 (list (concat "-oA" mail-alias-file)))
1197 (if mail-interactive
1198 sendmail-error-reporting-interactive
1199 sendmail-error-reporting-non-interactive)
1200 ;; Get the addresses from the message
1201 ;; unless this is a resend.
1202 ;; We must not do that for a resend
1203 ;; because we would find the original addresses.
1204 ;; For a resend, include the specific addresses.
1205 (or resend-to-addresses
1206 '("-t")
1208 (if mail-use-dsn
1209 (list "-N" (mapconcat 'symbol-name
1210 mail-use-dsn ",")))
1213 (exit-value (apply 'call-process-region args)))
1214 (cond ((or (null exit-value) (eq 0 exit-value)))
1215 ((numberp exit-value)
1216 (error "Sending...failed with exit value %d" exit-value))
1217 ((stringp exit-value)
1218 (error "Sending...terminated by signal: %s" exit-value))
1220 (error "SENDMAIL-SEND-IT -- fall through: %S" exit-value))))
1221 (or fcc-was-found
1222 (error "No recipients")))
1223 (if mail-interactive
1224 (with-current-buffer errbuf
1225 (goto-char (point-min))
1226 (while (re-search-forward "\n\n* *" nil t)
1227 (replace-match "; "))
1228 (if (not (zerop (buffer-size)))
1229 (error "Sending...failed to %s"
1230 (buffer-substring (point-min) (point-max)))))))
1231 (kill-buffer tembuf)
1232 (if (bufferp errbuf)
1233 (kill-buffer errbuf)))))
1235 (autoload 'rmail-output-to-rmail-buffer "rmailout")
1237 (defun mail-do-fcc (header-end)
1238 "Find and act on any FCC: headers in the current message before HEADER-END.
1239 If a buffer is visiting the FCC file, append to it before
1240 offering to save it, if it was modified initially. If this is an
1241 Rmail buffer, update Rmail as needed. If there is no buffer,
1242 just append to the file, in Babyl format if necessary."
1243 (unless (markerp header-end)
1244 (error "Value of `header-end' must be a marker"))
1245 (let (fcc-list
1246 (mailbuf (current-buffer))
1247 (time (current-time)))
1248 (save-excursion
1249 (goto-char (point-min))
1250 (let ((case-fold-search t))
1251 (while (re-search-forward "^FCC:[ \t]*" header-end t)
1252 (push (buffer-substring (point)
1253 (progn
1254 (end-of-line)
1255 (skip-chars-backward " \t")
1256 (point)))
1257 fcc-list)
1258 (delete-region (match-beginning 0)
1259 (progn (forward-line 1) (point)))))
1260 (with-temp-buffer
1261 ;; This initial newline is not written out if we create a new
1262 ;; file (see below).
1263 (insert "\nFrom " (user-login-name) " " (current-time-string time) "\n")
1264 ;; Insert the time zone before the year.
1265 (forward-char -1)
1266 (forward-word -1)
1267 (require 'mail-utils)
1268 (insert (mail-rfc822-time-zone time) " ")
1269 (goto-char (point-max))
1270 (insert-buffer-substring mailbuf)
1271 ;; Make sure messages are separated.
1272 (goto-char (point-max))
1273 (insert ?\n)
1274 (goto-char 2)
1275 ;; ``Quote'' "^From " as ">From "
1276 ;; (note that this isn't really quoting, as there is no requirement
1277 ;; that "^[>]+From " be quoted in the same transparent way.)
1278 (let ((case-fold-search nil))
1279 (while (search-forward "\nFrom " nil t)
1280 (forward-char -5)
1281 (insert ?>)))
1282 (dolist (fcc fcc-list)
1283 (let* ((buffer (find-buffer-visiting fcc))
1284 (curbuf (current-buffer))
1285 dont-write-the-file
1286 buffer-matches-file
1287 (beg (point-min)) ; the initial blank line
1288 (end (point-max))
1289 ;; After the ^From line.
1290 (beg2 (save-excursion (goto-char (point-min))
1291 (forward-line 2) (point))))
1292 (if buffer
1293 ;; File is present in a buffer => append to that buffer.
1294 (with-current-buffer buffer
1295 (setq buffer-matches-file
1296 (and (not (buffer-modified-p))
1297 (verify-visited-file-modtime buffer)))
1298 (let ((msg (bound-and-true-p rmail-current-message))
1299 (buffer-read-only nil))
1300 ;; If MSG is non-nil, buffer is in Rmail mode.
1301 (if msg
1302 (let ((buff (generate-new-buffer " *mail-do-fcc")))
1303 (unwind-protect
1304 (progn
1305 (with-current-buffer buff
1306 (insert-buffer-substring curbuf (1+ beg) end))
1307 (rmail-output-to-rmail-buffer buff msg))
1308 (kill-buffer buff)))
1309 ;; Output file not in Rmail mode => just insert
1310 ;; at the end.
1311 (save-restriction
1312 (widen)
1313 (goto-char (point-max))
1314 (insert-buffer-substring curbuf beg end)))
1315 ;; Offer to save the buffer if it was modified
1316 ;; before we started.
1317 (unless buffer-matches-file
1318 (if (y-or-n-p (format "Save file %s? " fcc))
1319 (save-buffer))
1320 (setq dont-write-the-file t)))))
1321 ;; Append to the file directly, unless we've already taken
1322 ;; care of it.
1323 (unless dont-write-the-file
1324 (if (and (file-exists-p fcc)
1325 (mail-file-babyl-p fcc))
1326 ;; If the file is a Babyl file, convert the message to
1327 ;; Babyl format. Even though Rmail no longer uses
1328 ;; Babyl, this code can remain for the time being, on
1329 ;; the off-chance one FCCs to a Babyl file that has
1330 ;; not yet been converted to mbox.
1331 (let ((coding-system-for-write
1332 (or rmail-file-coding-system 'emacs-mule)))
1333 (with-temp-buffer
1334 (insert "\C-l\n0, unseen,,\n*** EOOH ***\nDate: "
1335 (mail-rfc822-date) "\n")
1336 (insert-buffer-substring curbuf beg2 end)
1337 (insert "\n\C-_")
1338 (write-region (point-min) (point-max) fcc t)))
1339 ;; Ensure there is a blank line between messages, but
1340 ;; not at the very start of the file.
1341 (write-region (if (file-exists-p fcc)
1342 (point-min)
1343 (1+ (point-min)))
1344 (point-max) fcc t)))
1345 (and buffer (not dont-write-the-file)
1346 (with-current-buffer buffer
1347 (set-visited-file-modtime)))))))))
1349 (defun mail-sent-via ()
1350 "Make a Sent-via header line from each To or CC header line."
1351 (interactive)
1352 (save-excursion
1353 ;; put a marker at the end of the header
1354 (let ((end (copy-marker (mail-header-end)))
1355 (case-fold-search t))
1356 (goto-char (point-min))
1357 ;; search for the To: lines and make Sent-via: lines from them
1358 ;; search for the next To: line
1359 (while (re-search-forward "^\\(to\\|cc\\):" end t)
1360 ;; Grab this line plus all its continuations, sans the `to:'.
1361 (let ((to-line
1362 (buffer-substring (point)
1363 (progn
1364 (if (re-search-forward "^[^ \t\n]" end t)
1365 (backward-char 1)
1366 (goto-char end))
1367 (point)))))
1368 ;; Insert a copy, with altered header field name.
1369 (insert-before-markers "Sent-via:" to-line))))))
1371 (make-obsolete 'mail-sent-via "nobody can remember what it is for." "24.1")
1374 (defun mail-to ()
1375 "Move point to end of To field, creating it if necessary."
1376 (interactive)
1377 (expand-abbrev)
1378 (mail-position-on-field "To"))
1380 (defun mail-subject ()
1381 "Move point to end of Subject field, creating it if necessary."
1382 (interactive)
1383 (expand-abbrev)
1384 (mail-position-on-field "Subject"))
1386 (defun mail-cc ()
1387 "Move point to end of CC field, creating it if necessary."
1388 (interactive)
1389 (expand-abbrev)
1390 (or (mail-position-on-field "cc" t)
1391 (progn (mail-position-on-field "to")
1392 (insert "\nCC: "))))
1394 (defun mail-bcc ()
1395 "Move point to end of BCC field, creating it if necessary."
1396 (interactive)
1397 (expand-abbrev)
1398 (or (mail-position-on-field "bcc" t)
1399 (progn (mail-position-on-field "to")
1400 (insert "\nBCC: "))))
1402 (defun mail-fcc (folder)
1403 "Add a new FCC field, with file name completion."
1404 (interactive "FFolder carbon copy: ")
1405 (expand-abbrev)
1406 (or (mail-position-on-field "fcc" t) ;Put new field after exiting FCC.
1407 (mail-position-on-field "to"))
1408 (insert "\nFCC: " folder))
1410 (defun mail-reply-to ()
1411 "Move point to end of Reply-To field, creating it if necessary."
1412 (interactive)
1413 (expand-abbrev)
1414 (mail-position-on-field "Reply-To"))
1416 (defun mail-mail-reply-to ()
1417 "Move point to end of Mail-Reply-To field, creating it if necessary."
1418 (interactive)
1419 (expand-abbrev)
1420 (or (mail-position-on-field "mail-reply-to" t)
1421 (progn (mail-position-on-field "to")
1422 (insert "\nMail-Reply-To: "))))
1424 (defun mail-mail-followup-to ()
1425 "Move point to end of Mail-Followup-To field, creating it if necessary."
1426 (interactive)
1427 (expand-abbrev)
1428 (or (mail-position-on-field "mail-followup-to" t)
1429 (progn (mail-position-on-field "to")
1430 (insert "\nMail-Followup-To: "))))
1432 (defun mail-position-on-field (field &optional soft)
1433 (let (end
1434 (case-fold-search t))
1435 (setq end (mail-header-end))
1436 (goto-char (point-min))
1437 (if (re-search-forward (concat "^" (regexp-quote field) ":") end t)
1438 (progn
1439 (re-search-forward "^[^ \t]" nil 'move)
1440 (beginning-of-line)
1441 (skip-chars-backward "\n")
1443 (or soft
1444 (progn (goto-char end)
1445 (insert field ": \n")
1446 (skip-chars-backward "\n")))
1447 nil)))
1449 (defun mail-text ()
1450 "Move point to beginning of text field."
1451 (interactive)
1452 (expand-abbrev)
1453 (goto-char (mail-text-start)))
1455 (defun mail-signature (&optional atpoint)
1456 "Sign letter with signature.
1457 If the variable `mail-signature' is a string, inserts it.
1458 If it is t or nil, inserts the contents of the file `mail-signature-file'.
1459 Otherwise, evals `mail-signature'.
1460 Prefix argument ATPOINT means insert at point rather than the end."
1461 (interactive "*P")
1462 ;; Test for an unreadable file here, before we delete trailing
1463 ;; whitespace, so that we don't modify the buffer needlessly.
1464 (if (and (memq mail-signature '(t nil))
1465 (not (file-readable-p mail-signature-file)))
1466 (if (called-interactively-p 'interactive)
1467 (message "The signature file `%s' could not be read"
1468 mail-signature-file))
1469 (save-excursion
1470 (unless atpoint
1471 (goto-char (point-max))
1472 ;; Delete trailing whitespace and blank lines.
1473 (skip-chars-backward " \t\n")
1474 (end-of-line)
1475 (delete-region (point) (point-max)))
1476 (cond ((stringp mail-signature)
1477 (insert mail-signature))
1478 ((memq mail-signature '(t nil))
1479 (insert "\n\n-- \n")
1480 (insert-file-contents (expand-file-name mail-signature-file)))
1482 ;; FIXME add condition-case error handling?
1483 (eval mail-signature))))))
1485 (defun mail-fill-yanked-message (&optional justifyp)
1486 "Fill the paragraphs of a message yanked into this one.
1487 Numeric argument means justify as well."
1488 (interactive "P")
1489 (save-excursion
1490 (goto-char (mail-text-start))
1491 (fill-individual-paragraphs (point)
1492 (point-max)
1493 justifyp
1494 mail-citation-prefix-regexp)))
1496 (defun mail-indent-citation ()
1497 "Modify text just inserted from a message to be cited.
1498 The inserted text should be the region.
1499 When this function returns, the region is again around the modified text.
1501 Normally, indent each nonblank line `mail-indentation-spaces' spaces.
1502 However, if `mail-yank-prefix' is non-nil, insert that prefix on each line."
1503 (mail-yank-clear-headers (region-beginning) (region-end))
1504 (if (null mail-yank-prefix)
1505 (indent-rigidly (region-beginning) (region-end)
1506 mail-indentation-spaces)
1507 (save-excursion
1508 (let ((end (set-marker (make-marker) (region-end))))
1509 (goto-char (region-beginning))
1510 (while (< (point) end)
1511 (insert mail-yank-prefix)
1512 (forward-line 1))))))
1514 (defun mail-yank-original (arg)
1515 "Insert the message being replied to, if any (in Rmail).
1516 Puts point after the text and mark before.
1517 Normally, indents each nonblank line ARG spaces (default 3).
1518 However, if `mail-yank-prefix' is non-nil, insert that prefix on each line.
1520 Just \\[universal-argument] as argument means don't indent, insert no prefix,
1521 and don't delete any header fields."
1522 (interactive "P")
1523 (if mail-reply-action
1524 (let ((start (point))
1525 (original mail-reply-action)
1526 (omark (mark t)))
1527 (and (consp original) (eq (car original) 'insert-buffer)
1528 (setq original (nth 1 original)))
1529 (if (consp original)
1530 (progn
1531 ;; Call yank function, and set the mark if it doesn't.
1532 (apply (car original) (cdr original))
1533 (if (eq omark (mark t))
1534 (push-mark (point))))
1535 ;; If the original message is in another window in the same
1536 ;; frame, delete that window to save space.
1537 (delete-windows-on original t)
1538 (with-no-warnings
1539 ;; We really want this to set mark.
1540 (insert-buffer original)
1541 ;; If they yank the original text, the encoding of the
1542 ;; original message is a better default than
1543 ;; the default buffer-file-coding-system.
1544 (and (coding-system-equal
1545 (default-value 'buffer-file-coding-system)
1546 buffer-file-coding-system)
1547 (setq buffer-file-coding-system
1548 (coding-system-change-text-conversion
1549 buffer-file-coding-system
1550 (coding-system-base
1551 (with-current-buffer original
1552 buffer-file-coding-system))))))
1553 (set-text-properties (point) (mark t) nil))
1554 (if (consp arg)
1556 (goto-char start)
1557 (let ((mail-indentation-spaces (if arg (prefix-numeric-value arg)
1558 mail-indentation-spaces))
1559 ;; Avoid error in Transient Mark mode
1560 ;; on account of mark's being inactive.
1561 (mark-even-if-inactive t))
1562 (cond (mail-citation-hook
1563 ;; Bind mail-citation-header to the inserted
1564 ;; message's header.
1565 (let ((mail-citation-header
1566 (buffer-substring-no-properties
1567 start
1568 (save-excursion
1569 (save-restriction
1570 (narrow-to-region start (point-max))
1571 (goto-char start)
1572 (rfc822-goto-eoh)
1573 (point))))))
1574 (run-hooks 'mail-citation-hook)))
1575 (mail-yank-hooks
1576 (run-hooks 'mail-yank-hooks))
1578 (mail-indent-citation)))))
1579 ;; This is like exchange-point-and-mark, but doesn't activate the mark.
1580 ;; It is cleaner to avoid activation, even though the command
1581 ;; loop would deactivate the mark because we inserted text.
1582 (goto-char (prog1 (mark t)
1583 (set-marker (mark-marker) (point) (current-buffer))))
1584 (if (not (eolp)) (insert ?\n)))))
1586 (defun mail-yank-clear-headers (start end)
1587 (if (< end start)
1588 (let (temp)
1589 (setq temp start start end end temp)))
1590 (if mail-yank-ignored-headers
1591 (save-excursion
1592 (goto-char start)
1593 (if (search-forward "\n\n" end t)
1594 (save-restriction
1595 (narrow-to-region start (point))
1596 (goto-char start)
1597 (while (let ((case-fold-search t))
1598 (re-search-forward mail-yank-ignored-headers nil t))
1599 (beginning-of-line)
1600 (delete-region (point)
1601 (progn (re-search-forward "\n[^ \t]")
1602 (forward-char -1)
1603 (point)))))))))
1605 (defun mail-yank-region (arg)
1606 "Insert the selected region from the message being replied to.
1607 Puts point after the text and mark before.
1608 Normally, indents each nonblank line ARG spaces (default 3).
1609 However, if `mail-yank-prefix' is non-nil, insert that prefix on each line.
1611 Just \\[universal-argument] as argument means don't indent, insert no prefix,
1612 and don't delete any header fields."
1613 (interactive "P")
1614 (and (consp mail-reply-action)
1615 (eq (car mail-reply-action) 'insert-buffer)
1616 (with-current-buffer (nth 1 mail-reply-action)
1617 (or (mark t)
1618 (error "No mark set: %S" (current-buffer))))
1619 (let ((buffer (nth 1 mail-reply-action))
1620 (start (point))
1621 ;; Avoid error in Transient Mark mode
1622 ;; on account of mark's being inactive.
1623 (mark-even-if-inactive t))
1624 ;; Insert the citation text.
1625 (insert (with-current-buffer buffer
1626 (buffer-substring-no-properties (point) (mark))))
1627 (push-mark start)
1628 ;; Indent or otherwise annotate the citation text.
1629 (if (consp arg)
1631 (let ((mail-indentation-spaces (if arg (prefix-numeric-value arg)
1632 mail-indentation-spaces)))
1633 (if mail-citation-hook
1634 ;; Bind mail-citation-header to the original message's header.
1635 (let ((mail-citation-header
1636 (with-current-buffer buffer
1637 (buffer-substring-no-properties
1638 (point-min)
1639 (save-excursion
1640 (goto-char (point-min))
1641 (rfc822-goto-eoh)
1642 (point))))))
1643 (run-hooks 'mail-citation-hook))
1644 (if mail-yank-hooks
1645 (run-hooks 'mail-yank-hooks)
1646 (mail-indent-citation))))))))
1648 (defun mail-split-line ()
1649 "Split current line, moving portion beyond point vertically down.
1650 If the current line has `mail-yank-prefix', insert it on the new line."
1651 (interactive "*")
1652 (split-line mail-yank-prefix))
1655 (defun mail-attach-file (&optional file)
1656 "Insert a file at the end of the buffer, with separator lines around it."
1657 (interactive "fAttach file: ")
1658 (save-excursion
1659 (goto-char (point-max))
1660 (or (bolp) (newline))
1661 (newline)
1662 (let ((start (point))
1663 middle)
1664 (insert (format "===File %s===" file))
1665 (insert-char ?= (max 0 (- 60 (current-column))))
1666 (newline)
1667 (setq middle (point))
1668 (insert "============================================================\n")
1669 (push-mark)
1670 (goto-char middle)
1671 (insert-file-contents file)
1672 (or (bolp) (newline))
1673 (goto-char start))))
1675 ;; Put these commands last, to reduce chance of lossage from quitting
1676 ;; in middle of loading the file.
1678 ;;;###autoload (add-hook 'same-window-buffer-names (purecopy "*mail*"))
1679 ;;;###autoload (add-hook 'same-window-buffer-names (purecopy "*unsent mail*"))
1681 ;;;###autoload
1682 (defun mail (&optional noerase to subject in-reply-to cc replybuffer
1683 actions return-action)
1684 "Edit a message to be sent. Prefix arg means resume editing (don't erase).
1685 When this function returns, the buffer `*mail*' is selected.
1686 The value is t if the message was newly initialized; otherwise, nil.
1688 Optionally, the signature file `mail-signature-file' can be inserted at the
1689 end; see the variable `mail-signature'.
1691 \\<mail-mode-map>
1692 While editing message, type \\[mail-send-and-exit] to send the message and exit.
1694 Various special commands starting with C-c are available in sendmail mode
1695 to move to message header fields:
1696 \\{mail-mode-map}
1698 If `mail-self-blind' is non-nil, a BCC to yourself is inserted
1699 when the message is initialized.
1701 If `mail-default-reply-to' is non-nil, it should be an address (a string);
1702 a Reply-to: field with that address is inserted.
1704 If `mail-archive-file-name' is non-nil, an FCC field with that file name
1705 is inserted.
1707 The normal hook `mail-setup-hook' is run after the message is
1708 initialized. It can add more default fields to the message.
1710 The first argument, NOERASE, determines what to do when there is
1711 an existing modified `*mail*' buffer. If NOERASE is nil, the
1712 existing mail buffer is used, and the user is prompted whether to
1713 keep the old contents or to erase them. If NOERASE has the value
1714 `new', a new mail buffer will be created instead of using the old
1715 one. Any other non-nil value means to always select the old
1716 buffer without erasing the contents.
1718 The second through fifth arguments,
1719 TO, SUBJECT, IN-REPLY-TO and CC, specify if non-nil
1720 the initial contents of those header fields.
1721 These arguments should not have final newlines.
1722 The sixth argument REPLYBUFFER is a buffer which contains an
1723 original message being replied to, or else an action
1724 of the form (FUNCTION . ARGS) which says how to insert the original.
1725 Or it can be nil, if not replying to anything.
1726 The seventh argument ACTIONS is a list of actions to take
1727 if/when the message is sent. Each action looks like (FUNCTION . ARGS);
1728 when the message is sent, we apply FUNCTION to ARGS.
1729 This is how Rmail arranges to mark messages `answered'."
1730 (interactive "P")
1731 (if (eq noerase 'new)
1732 (pop-to-buffer (generate-new-buffer "*mail*"))
1733 (and noerase
1734 (not (get-buffer "*mail*"))
1735 (setq noerase nil))
1736 (pop-to-buffer "*mail*"))
1738 ;; Avoid danger that the auto-save file can't be written.
1739 (let ((dir (expand-file-name
1740 (file-name-as-directory mail-default-directory))))
1741 (if (file-exists-p dir)
1742 (setq default-directory dir)))
1743 ;; Only call auto-save-mode if necessary, to avoid changing auto-save file.
1744 (if (or (and auto-save-default (not buffer-auto-save-file-name))
1745 (and (not auto-save-default) buffer-auto-save-file-name))
1746 (auto-save-mode auto-save-default))
1747 (mail-mode)
1748 ;; Disconnect the buffer from its visited file
1749 ;; (in case the user has actually visited a file *mail*).
1750 ;; (set-visited-file-name nil)
1751 (let (initialized)
1752 (and (not (and noerase
1753 (not (eq noerase 'new))))
1754 (if buffer-file-name
1755 (if (buffer-modified-p)
1756 (when (y-or-n-p "Buffer has unsaved changes; reinitialize it and discard them? ")
1757 (if (y-or-n-p "Disconnect buffer from visited file? ")
1758 (set-visited-file-name nil))
1760 (when (y-or-n-p "Reinitialize buffer, and disconnect it from the visited file? ")
1761 (set-visited-file-name nil)
1763 ;; A non-file-visiting buffer.
1764 (if (buffer-modified-p)
1765 (y-or-n-p "Unsent message being composed; erase it? ")
1767 (let ((inhibit-read-only t))
1768 (erase-buffer)
1769 (mail-setup to subject in-reply-to cc replybuffer actions
1770 return-action)
1771 (setq initialized t)))
1772 (if (and buffer-auto-save-file-name
1773 (file-exists-p buffer-auto-save-file-name))
1774 (message "Auto save file for draft message exists; consider M-x mail-recover"))
1775 initialized))
1777 (declare-function dired-view-file "dired" ())
1778 (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
1780 (defun mail-recover-1 ()
1781 "Pop up a list of auto-saved draft messages so you can recover one of them."
1782 (interactive)
1783 (let ((file-name (make-auto-save-file-name))
1784 (ls-lisp-support-shell-wildcards t)
1785 non-random-len wildcard)
1786 ;; Remove the random part from the auto-save-file-name, and
1787 ;; create a wildcard which matches possible candidates.
1788 ;; Note: this knows that make-auto-save-file-name appends
1789 ;; "#<RANDOM-STUFF>#" to the buffer name, where RANDOM-STUFF
1790 ;; is the result of (make-temp-name "").
1791 (setq non-random-len
1792 (- (length file-name) (length (make-temp-name "")) 1))
1793 (setq wildcard (concat (substring file-name 0 non-random-len) "*"))
1794 (if (null (file-expand-wildcards wildcard))
1795 (message "There are no auto-saved drafts to recover")
1796 ;; Bind dired-trivial-filenames to t because all auto-save file
1797 ;; names are normally ``trivial'', so Dired will set point after
1798 ;; all the files, at buffer bottom. We want it on the first
1799 ;; file instead.
1800 ;; Require dired so that dired-trivial-filenames does not get
1801 ;; unbound on exit from the let.
1802 (require 'dired)
1803 (let ((dired-trivial-filenames t))
1804 (dired-other-window wildcard (concat dired-listing-switches " -t")))
1805 (rename-buffer "*Auto-saved Drafts*" t)
1806 (save-excursion
1807 (goto-char (point-min))
1808 (or (looking-at " Move to the draft file you want to recover,")
1809 (let ((inhibit-read-only t))
1810 ;; Each line starts with a space so that Font Lock mode
1811 ;; won't highlight the first character.
1812 (insert "\
1813 Move to the draft file you want to recover, then type C-c C-c
1814 to recover text of message whose composition was interrupted.
1815 To browse text of a draft, type v on the draft file's line.
1817 You can also delete some of these files;
1818 type d on a line to mark that file for deletion.
1820 List of possible auto-save files for recovery:
1822 "))))
1823 (use-local-map
1824 (let ((map (make-sparse-keymap)))
1825 (set-keymap-parent map (current-local-map))
1826 map))
1827 (define-key (current-local-map) "v"
1828 (lambda ()
1829 (interactive)
1830 (let ((coding-system-for-read 'utf-8-emacs-unix))
1831 (dired-view-file))))
1832 (define-key (current-local-map) "\C-c\C-c"
1833 (lambda ()
1834 (interactive)
1835 (let ((fname (dired-get-filename))
1836 ;; Auto-saved files are written in the internal
1837 ;; representation, so they should be read accordingly.
1838 (coding-system-for-read 'utf-8-emacs-unix))
1839 (switch-to-buffer-other-window "*mail*")
1840 (let ((buffer-read-only nil))
1841 (erase-buffer)
1842 (insert-file-contents fname nil)
1843 ;; insert-file-contents will set buffer-file-coding-system
1844 ;; to utf-8-emacs, which is probably not what they want to
1845 ;; use for sending the message. But we don't know what
1846 ;; was its value before the buffer was killed or Emacs
1847 ;; crashed. We therefore reset buffer-file-coding-system
1848 ;; to the default value, so that either the default does
1849 ;; TRT, or the user will get prompted for the right
1850 ;; encoding when they send the message.
1851 (setq buffer-file-coding-system
1852 (default-value 'buffer-file-coding-system)))))))))
1854 (declare-function dired-move-to-filename "dired" (&optional raise-error eol))
1855 (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
1856 (declare-function dired-view-file "dired" ())
1858 (defun mail-recover ()
1859 "Recover interrupted mail composition from auto-save files.
1861 If the mail buffer has a current valid auto-save file,
1862 the command recovers that file. Otherwise, it displays a
1863 buffer showing the existing auto-saved draft messages;
1864 you can move to one of them and type C-c C-c to recover that one."
1865 (interactive)
1866 ;; In case they invoke us from some random buffer...
1867 (switch-to-buffer "*mail*")
1868 ;; If *mail* didn't exist, set its directory, so that auto-saved
1869 ;; drafts will be found.
1870 (let ((dir (expand-file-name
1871 (file-name-as-directory mail-default-directory))))
1872 (if (file-exists-p dir)
1873 (setq default-directory dir)))
1874 (or (eq major-mode 'mail-mode)
1875 (mail-mode))
1876 (let ((file-name buffer-auto-save-file-name))
1877 (cond ((and file-name (file-exists-p file-name))
1878 (let ((dispbuf
1879 ;; This used to invoke `ls' via call-process, but
1880 ;; dired-noselect is more portable to systems where
1881 ;; `ls' is not a standard program (it will use
1882 ;; ls-lisp instead).
1883 (dired-noselect file-name
1884 (concat dired-listing-switches " -t"))))
1885 (save-selected-window
1886 (select-window (display-buffer dispbuf t))
1887 (goto-char (point-min))
1888 (forward-line 2)
1889 (dired-move-to-filename)
1890 (setq dispbuf (rename-buffer "*Directory*" t)))
1891 (if (not (yes-or-no-p
1892 (format "Recover mail draft from auto save file %s? "
1893 file-name)))
1894 (error "mail-recover cancelled")
1895 (let ((buffer-read-only nil)
1896 (buffer-coding buffer-file-coding-system)
1897 ;; Auto-save files are written in internal
1898 ;; representation of non-ASCII characters.
1899 (coding-system-for-read 'utf-8-emacs-unix))
1900 (erase-buffer)
1901 (insert-file-contents file-name nil)
1902 (setq buffer-file-coding-system buffer-coding)))))
1903 (t (mail-recover-1)))))
1905 ;;;###autoload
1906 (defun mail-other-window (&optional noerase to subject in-reply-to cc replybuffer sendactions)
1907 "Like `mail' command, but display mail buffer in another window."
1908 (interactive "P")
1909 (let ((pop-up-windows t)
1910 (special-display-buffer-names nil)
1911 (special-display-regexps nil)
1912 (same-window-buffer-names nil)
1913 (same-window-regexps nil))
1914 (pop-to-buffer "*mail*"))
1915 (mail noerase to subject in-reply-to cc replybuffer sendactions))
1917 ;;;###autoload
1918 (defun mail-other-frame (&optional noerase to subject in-reply-to cc replybuffer sendactions)
1919 "Like `mail' command, but display mail buffer in another frame."
1920 (interactive "P")
1921 (let ((pop-up-frames t)
1922 (special-display-buffer-names nil)
1923 (special-display-regexps nil)
1924 (same-window-buffer-names nil)
1925 (same-window-regexps nil))
1926 (pop-to-buffer "*mail*"))
1927 (mail noerase to subject in-reply-to cc replybuffer sendactions))
1929 ;; Do not add anything but external entries on this page.
1931 (provide 'sendmail)
1933 ;;; sendmail.el ends here