Merge from trunk
[emacs.git] / lisp / mail / sendmail.el
blob42860fb43b90a9b210a4a645fe48a1e52686965c
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." "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 "Insert the \"From:\" field of a mail header.
945 The style of the field is determined by the variable `mail-from-style'.
946 This function does not perform RFC2047 encoding."
947 (let* ((login user-mail-address)
948 (fullname (user-full-name))
949 (quote-fullname nil))
950 (if (string-match "[^\0-\177]" fullname)
951 (setq quote-fullname t))
952 (cond ((null mail-from-style)
953 (insert "From: " login "\n"))
954 ;; This is deprecated.
955 ((eq mail-from-style 'system-default)
956 nil)
957 ((or (eq mail-from-style 'angles)
958 (and (not (eq mail-from-style 'parens))
959 ;; Use angles if no quoting is needed, or if
960 ;; parens would need quoting too.
961 (or (not (string-match "[^- !#-'*+/-9=?A-Z^-~]" fullname))
962 (let ((tmp (concat fullname nil)))
963 (while (string-match "([^()]*)" tmp)
964 (aset tmp (match-beginning 0) ?-)
965 (aset tmp (1- (match-end 0)) ?-))
966 (string-match "[\\()]" tmp)))))
967 (insert "From: " fullname)
968 (let ((fullname-start (+ (point-min) 6))
969 (fullname-end (point-marker)))
970 (goto-char fullname-start)
971 ;; Look for a character that cannot appear unquoted
972 ;; according to RFC 822.
973 (if (or (re-search-forward "[^- !#-'*+/-9=?A-Z^-~]"
974 fullname-end 1)
975 quote-fullname)
976 (progn
977 ;; Quote fullname, escaping specials.
978 (goto-char fullname-start)
979 (insert "\"")
980 (while (re-search-forward "[\"\\]"
981 fullname-end 1)
982 (replace-match "\\\\\\&" t))
983 (insert "\""))))
984 (insert " <" login ">\n"))
985 ;; 'parens or default
987 (insert "From: " login " (")
988 (let ((fullname-start (point)))
989 (if quote-fullname
990 (insert "\""))
991 (insert fullname)
992 (if quote-fullname
993 (insert "\""))
994 (let ((fullname-end (point-marker)))
995 (goto-char fullname-start)
996 ;; RFC 822 says \ and nonmatching parentheses
997 ;; must be escaped in comments.
998 ;; Escape every instance of ()\ ...
999 (while (re-search-forward "[()\\]" fullname-end 1)
1000 (replace-match "\\\\\\&" t))
1001 ;; ... then undo escaping of matching parentheses,
1002 ;; including matching nested parentheses.
1003 (goto-char fullname-start)
1004 (while (re-search-forward
1005 "\\(\\=\\|[^\\]\\(\\\\\\\\\\)*\\)\\\\(\\(\\([^\\]\\|\\\\\\\\\\)*\\)\\\\)"
1006 fullname-end 1)
1007 (replace-match "\\1(\\3)" t)
1008 (goto-char fullname-start))))
1009 (insert ")\n")))))
1011 (defun mail-encode-header (beg end)
1012 "Encode the mail header between BEG and END according to RFC2047.
1013 Return non-nil if and only if some part of the header is encoded."
1014 (save-restriction
1015 (narrow-to-region beg end)
1016 (let* ((selected (select-message-coding-system))
1017 (mm-coding-system-priorities
1018 (if (and selected (coding-system-get selected :mime-charset))
1019 (cons selected mm-coding-system-priorities)
1020 mm-coding-system-priorities))
1021 (tick (buffer-chars-modified-tick))
1022 (rfc2047-encode-encoded-words nil))
1023 (rfc2047-encode-message-header)
1024 (= tick (buffer-chars-modified-tick)))))
1026 ;; Normally you will not need to modify these options unless you are
1027 ;; using some non-genuine substitute for sendmail which does not
1028 ;; implement each and every option that the original supports.
1029 ;; E.g., ssmtp does not support "-odb", so, if your site uses it,
1030 ;; you will need to modify `sendmail-error-reporting-non-interactive'
1031 ;; in your site-init.el.
1032 (defvar sendmail-error-reporting-interactive
1033 ;; These mean "report errors to terminal" and "deliver interactively"
1034 '("-oep" "-odi"))
1035 (defvar sendmail-error-reporting-non-interactive
1036 ;; These mean "report errors by mail" and "deliver in background".
1037 '("-oem" "-odb"))
1039 (defun sendmail-send-it ()
1040 "Send the current mail buffer using the Sendmail package.
1041 This is a suitable value for `send-mail-function'. It sends using the
1042 external program defined by `sendmail-program'."
1043 (require 'mail-utils)
1044 (let ((errbuf (if mail-interactive
1045 (generate-new-buffer " sendmail errors")
1047 (tembuf (generate-new-buffer " sendmail temp"))
1048 (multibyte enable-multibyte-characters)
1049 (case-fold-search nil)
1050 (selected-coding (select-message-coding-system))
1051 resend-to-addresses
1052 delimline
1053 fcc-was-found
1054 (mailbuf (current-buffer))
1055 ;; Examine these variables now, so that
1056 ;; local binding in the mail buffer will take effect.
1057 (envelope-from
1058 (and mail-specify-envelope-from
1059 (or (mail-envelope-from) user-mail-address))))
1060 (unwind-protect
1061 (with-current-buffer tembuf
1062 (erase-buffer)
1063 (unless multibyte
1064 (set-buffer-multibyte nil))
1065 (insert-buffer-substring mailbuf)
1066 (set-buffer-file-coding-system selected-coding)
1067 (goto-char (point-max))
1068 ;; require one newline at the end.
1069 (or (= (preceding-char) ?\n)
1070 (insert ?\n))
1071 ;; Change header-delimiter to be what sendmail expects.
1072 (goto-char (mail-header-end))
1073 (delete-region (point) (progn (end-of-line) (point)))
1074 (setq delimline (point-marker))
1075 (sendmail-sync-aliases)
1076 (if mail-aliases
1077 (expand-mail-aliases (point-min) delimline))
1078 (goto-char (point-min))
1079 ;; Ignore any blank lines in the header
1080 (while (and (re-search-forward "\n\n\n*" delimline t)
1081 (< (point) delimline))
1082 (replace-match "\n"))
1083 (goto-char (point-min))
1084 ;; Look for Resent- headers. They require sending
1085 ;; the message specially.
1086 (let ((case-fold-search t))
1087 (goto-char (point-min))
1088 (while (re-search-forward "^Resent-\\(to\\|cc\\|bcc\\):" delimline t)
1089 ;; Put a list of such addresses in resend-to-addresses.
1090 (setq resend-to-addresses
1091 (save-restriction
1092 (narrow-to-region (point)
1093 (save-excursion
1094 (forward-line 1)
1095 (while (looking-at "^[ \t]")
1096 (forward-line 1))
1097 (point)))
1098 (append (mail-parse-comma-list)
1099 resend-to-addresses)))
1100 ;; Delete Resent-BCC ourselves
1101 (if (save-excursion (beginning-of-line)
1102 (looking-at "resent-bcc"))
1103 (delete-region (line-beginning-position)
1104 (line-beginning-position 2))))
1105 ;; Apparently this causes a duplicate Sender.
1106 ;; ;; If the From is different than current user, insert Sender.
1107 ;; (goto-char (point-min))
1108 ;; (and (re-search-forward "^From:" delimline t)
1109 ;; (progn
1110 ;; (require 'mail-utils)
1111 ;; (not (string-equal
1112 ;; (mail-strip-quoted-names
1113 ;; (save-restriction
1114 ;; (narrow-to-region (point-min) delimline)
1115 ;; (mail-fetch-field "From")))
1116 ;; (user-login-name))))
1117 ;; (progn
1118 ;; (forward-line 1)
1119 ;; (insert "Sender: " (user-login-name) "\n")))
1120 ;; Don't send out a blank subject line
1121 (goto-char (point-min))
1122 (if (re-search-forward "^Subject:\\([ \t]*\n\\)+\\b" delimline t)
1123 (replace-match "")
1124 ;; This one matches a Subject just before the header delimiter.
1125 (if (and (re-search-forward "^Subject:\\([ \t]*\n\\)+" delimline t)
1126 (= (match-end 0) delimline))
1127 (replace-match "")))
1128 ;; Put the "From:" field in unless for some odd reason
1129 ;; they put one in themselves.
1130 (goto-char (point-min))
1131 (if (not (re-search-forward "^From:" delimline t))
1132 (mail-insert-from-field))
1133 ;; Possibly add a MIME header for the current coding system
1134 (let (charset where-content-type)
1135 (goto-char (point-min))
1136 (setq where-content-type
1137 (re-search-forward "^Content-type:" delimline t))
1138 (goto-char (point-min))
1139 (and (eq mail-send-nonascii 'mime)
1140 (not (re-search-forward "^MIME-version:" delimline t))
1141 (progn (skip-chars-forward "\0-\177")
1142 (/= (point) (point-max)))
1143 selected-coding
1144 (setq charset
1145 (coding-system-get selected-coding :mime-charset))
1146 (progn
1147 (goto-char delimline)
1148 (insert "MIME-version: 1.0\n"
1149 "Content-type: text/plain; charset="
1150 (symbol-name charset)
1151 "\nContent-Transfer-Encoding: 8bit\n")
1152 ;; The character set we will actually use
1153 ;; should override any specified in the message itself.
1154 (when where-content-type
1155 (goto-char where-content-type)
1156 (delete-region (point-at-bol)
1157 (progn (forward-line 1) (point)))))))
1158 ;; Insert an extra newline if we need it to work around
1159 ;; Sun's bug that swallows newlines.
1160 (goto-char (1+ delimline))
1161 (if (eval mail-mailer-swallows-blank-line)
1162 (newline))
1163 ;; Find and handle any FCC fields.
1164 (goto-char (point-min))
1165 (if (re-search-forward "^FCC:" delimline t)
1166 (progn
1167 (setq fcc-was-found t)
1168 (mail-do-fcc delimline)))
1169 (if mail-interactive
1170 (with-current-buffer errbuf
1171 (erase-buffer))))
1172 ;; Encode the header according to RFC2047.
1173 (mail-encode-header (point-min) delimline)
1174 (goto-char (point-min))
1175 (if (let ((case-fold-search t))
1176 (or resend-to-addresses
1177 (re-search-forward "^To:\\|^cc:\\|^bcc:"
1178 delimline t)))
1179 (let* ((default-directory "/")
1180 (coding-system-for-write selected-coding)
1181 (args
1182 (append (list (point-min) (point-max)
1183 sendmail-program
1184 nil errbuf nil "-oi")
1185 (and envelope-from
1186 (list "-f" envelope-from))
1187 ;; ;; Don't say "from root" if running under su.
1188 ;; (and (equal (user-real-login-name) "root")
1189 ;; (list "-f" (user-login-name)))
1190 (and mail-alias-file
1191 (list (concat "-oA" mail-alias-file)))
1192 (if mail-interactive
1193 sendmail-error-reporting-interactive
1194 sendmail-error-reporting-non-interactive)
1195 ;; Get the addresses from the message
1196 ;; unless this is a resend.
1197 ;; We must not do that for a resend
1198 ;; because we would find the original addresses.
1199 ;; For a resend, include the specific addresses.
1200 (or resend-to-addresses
1201 '("-t")
1203 (if mail-use-dsn
1204 (list "-N" (mapconcat 'symbol-name
1205 mail-use-dsn ",")))
1208 (exit-value (apply 'call-process-region args)))
1209 (cond ((or (null exit-value) (eq 0 exit-value)))
1210 ((numberp exit-value)
1211 (error "Sending...failed with exit value %d" exit-value))
1212 ((stringp exit-value)
1213 (error "Sending...terminated by signal: %s" exit-value))
1215 (error "SENDMAIL-SEND-IT -- fall through: %S" exit-value))))
1216 (or fcc-was-found
1217 (error "No recipients")))
1218 (if mail-interactive
1219 (with-current-buffer errbuf
1220 (goto-char (point-min))
1221 (while (re-search-forward "\n\n* *" nil t)
1222 (replace-match "; "))
1223 (if (not (zerop (buffer-size)))
1224 (error "Sending...failed to %s"
1225 (buffer-substring (point-min) (point-max)))))))
1226 (kill-buffer tembuf)
1227 (if (bufferp errbuf)
1228 (kill-buffer errbuf)))))
1230 (autoload 'rmail-output-to-rmail-buffer "rmailout")
1232 (defun mail-do-fcc (header-end)
1233 "Find and act on any FCC: headers in the current message before HEADER-END.
1234 If a buffer is visiting the FCC file, append to it before
1235 offering to save it, if it was modified initially. If this is an
1236 Rmail buffer, update Rmail as needed. If there is no buffer,
1237 just append to the file, in Babyl format if necessary."
1238 (unless (markerp header-end)
1239 (error "Value of `header-end' must be a marker"))
1240 (let (fcc-list
1241 (mailbuf (current-buffer))
1242 (time (current-time)))
1243 (save-excursion
1244 (goto-char (point-min))
1245 (let ((case-fold-search t))
1246 (while (re-search-forward "^FCC:[ \t]*" header-end t)
1247 (push (buffer-substring (point)
1248 (progn
1249 (end-of-line)
1250 (skip-chars-backward " \t")
1251 (point)))
1252 fcc-list)
1253 (delete-region (match-beginning 0)
1254 (progn (forward-line 1) (point)))))
1255 (with-temp-buffer
1256 ;; This initial newline is not written out if we create a new
1257 ;; file (see below).
1258 (insert "\nFrom " (user-login-name) " " (current-time-string time) "\n")
1259 ;; Insert the time zone before the year.
1260 (forward-char -1)
1261 (forward-word -1)
1262 (require 'mail-utils)
1263 (insert (mail-rfc822-time-zone time) " ")
1264 (goto-char (point-max))
1265 (insert-buffer-substring mailbuf)
1266 ;; Make sure messages are separated.
1267 (goto-char (point-max))
1268 (insert ?\n)
1269 (goto-char 2)
1270 ;; ``Quote'' "^From " as ">From "
1271 ;; (note that this isn't really quoting, as there is no requirement
1272 ;; that "^[>]+From " be quoted in the same transparent way.)
1273 (let ((case-fold-search nil))
1274 (while (search-forward "\nFrom " nil t)
1275 (forward-char -5)
1276 (insert ?>)))
1277 (dolist (fcc fcc-list)
1278 (let* ((buffer (find-buffer-visiting fcc))
1279 (curbuf (current-buffer))
1280 dont-write-the-file
1281 buffer-matches-file
1282 (beg (point-min)) ; the initial blank line
1283 (end (point-max))
1284 ;; After the ^From line.
1285 (beg2 (save-excursion (goto-char (point-min))
1286 (forward-line 2) (point))))
1287 (if buffer
1288 ;; File is present in a buffer => append to that buffer.
1289 (with-current-buffer buffer
1290 (setq buffer-matches-file
1291 (and (not (buffer-modified-p))
1292 (verify-visited-file-modtime buffer)))
1293 (let ((msg (bound-and-true-p rmail-current-message))
1294 (buffer-read-only nil))
1295 ;; If MSG is non-nil, buffer is in Rmail mode.
1296 (if msg
1297 (let ((buff (generate-new-buffer " *mail-do-fcc")))
1298 (unwind-protect
1299 (progn
1300 (with-current-buffer buff
1301 (insert-buffer-substring curbuf (1+ beg) end))
1302 (rmail-output-to-rmail-buffer buff msg))
1303 (kill-buffer buff)))
1304 ;; Output file not in Rmail mode => just insert
1305 ;; at the end.
1306 (save-restriction
1307 (widen)
1308 (goto-char (point-max))
1309 (insert-buffer-substring curbuf beg end)))
1310 ;; Offer to save the buffer if it was modified
1311 ;; before we started.
1312 (unless buffer-matches-file
1313 (if (y-or-n-p (format "Save file %s? " fcc))
1314 (save-buffer))
1315 (setq dont-write-the-file t)))))
1316 ;; Append to the file directly, unless we've already taken
1317 ;; care of it.
1318 (unless dont-write-the-file
1319 (if (and (file-exists-p fcc)
1320 (mail-file-babyl-p fcc))
1321 ;; If the file is a Babyl file, convert the message to
1322 ;; Babyl format. Even though Rmail no longer uses
1323 ;; Babyl, this code can remain for the time being, on
1324 ;; the off-chance one FCCs to a Babyl file that has
1325 ;; not yet been converted to mbox.
1326 (let ((coding-system-for-write
1327 (or rmail-file-coding-system 'emacs-mule)))
1328 (with-temp-buffer
1329 (insert "\C-l\n0, unseen,,\n*** EOOH ***\nDate: "
1330 (mail-rfc822-date) "\n")
1331 (insert-buffer-substring curbuf beg2 end)
1332 (insert "\n\C-_")
1333 (write-region (point-min) (point-max) fcc t)))
1334 ;; Ensure there is a blank line between messages, but
1335 ;; not at the very start of the file.
1336 (write-region (if (file-exists-p fcc)
1337 (point-min)
1338 (1+ (point-min)))
1339 (point-max) fcc t)))
1340 (and buffer (not dont-write-the-file)
1341 (with-current-buffer buffer
1342 (set-visited-file-modtime)))))))))
1344 (defun mail-sent-via ()
1345 "Make a Sent-via header line from each To or CC header line."
1346 (interactive)
1347 (save-excursion
1348 ;; put a marker at the end of the header
1349 (let ((end (copy-marker (mail-header-end)))
1350 (case-fold-search t))
1351 (goto-char (point-min))
1352 ;; search for the To: lines and make Sent-via: lines from them
1353 ;; search for the next To: line
1354 (while (re-search-forward "^\\(to\\|cc\\):" end t)
1355 ;; Grab this line plus all its continuations, sans the `to:'.
1356 (let ((to-line
1357 (buffer-substring (point)
1358 (progn
1359 (if (re-search-forward "^[^ \t\n]" end t)
1360 (backward-char 1)
1361 (goto-char end))
1362 (point)))))
1363 ;; Insert a copy, with altered header field name.
1364 (insert-before-markers "Sent-via:" to-line))))))
1366 (make-obsolete 'mail-sent-via "nobody can remember what it is for." "24.1")
1369 (defun mail-to ()
1370 "Move point to end of To field, creating it if necessary."
1371 (interactive)
1372 (expand-abbrev)
1373 (mail-position-on-field "To"))
1375 (defun mail-subject ()
1376 "Move point to end of Subject field, creating it if necessary."
1377 (interactive)
1378 (expand-abbrev)
1379 (mail-position-on-field "Subject"))
1381 (defun mail-cc ()
1382 "Move point to end of CC field, creating it if necessary."
1383 (interactive)
1384 (expand-abbrev)
1385 (or (mail-position-on-field "cc" t)
1386 (progn (mail-position-on-field "to")
1387 (insert "\nCC: "))))
1389 (defun mail-bcc ()
1390 "Move point to end of BCC field, creating it if necessary."
1391 (interactive)
1392 (expand-abbrev)
1393 (or (mail-position-on-field "bcc" t)
1394 (progn (mail-position-on-field "to")
1395 (insert "\nBCC: "))))
1397 (defun mail-fcc (folder)
1398 "Add a new FCC field, with file name completion."
1399 (interactive "FFolder carbon copy: ")
1400 (expand-abbrev)
1401 (or (mail-position-on-field "fcc" t) ;Put new field after exiting FCC.
1402 (mail-position-on-field "to"))
1403 (insert "\nFCC: " folder))
1405 (defun mail-reply-to ()
1406 "Move point to end of Reply-To field, creating it if necessary."
1407 (interactive)
1408 (expand-abbrev)
1409 (mail-position-on-field "Reply-To"))
1411 (defun mail-mail-reply-to ()
1412 "Move point to end of Mail-Reply-To field, creating it if necessary."
1413 (interactive)
1414 (expand-abbrev)
1415 (or (mail-position-on-field "mail-reply-to" t)
1416 (progn (mail-position-on-field "to")
1417 (insert "\nMail-Reply-To: "))))
1419 (defun mail-mail-followup-to ()
1420 "Move point to end of Mail-Followup-To field, creating it if necessary."
1421 (interactive)
1422 (expand-abbrev)
1423 (or (mail-position-on-field "mail-followup-to" t)
1424 (progn (mail-position-on-field "to")
1425 (insert "\nMail-Followup-To: "))))
1427 (defun mail-position-on-field (field &optional soft)
1428 (let (end
1429 (case-fold-search t))
1430 (setq end (mail-header-end))
1431 (goto-char (point-min))
1432 (if (re-search-forward (concat "^" (regexp-quote field) ":") end t)
1433 (progn
1434 (re-search-forward "^[^ \t]" nil 'move)
1435 (beginning-of-line)
1436 (skip-chars-backward "\n")
1438 (or soft
1439 (progn (goto-char end)
1440 (insert field ": \n")
1441 (skip-chars-backward "\n")))
1442 nil)))
1444 (defun mail-text ()
1445 "Move point to beginning of text field."
1446 (interactive)
1447 (expand-abbrev)
1448 (goto-char (mail-text-start)))
1450 (defun mail-signature (&optional atpoint)
1451 "Sign letter with signature.
1452 If the variable `mail-signature' is a string, inserts it.
1453 If it is t or nil, inserts the contents of the file `mail-signature-file'.
1454 Otherwise, evals `mail-signature'.
1455 Prefix argument ATPOINT means insert at point rather than the end."
1456 (interactive "*P")
1457 ;; Test for an unreadable file here, before we delete trailing
1458 ;; whitespace, so that we don't modify the buffer needlessly.
1459 (if (and (memq mail-signature '(t nil))
1460 (not (file-readable-p mail-signature-file)))
1461 (if (called-interactively-p 'interactive)
1462 (message "The signature file `%s' could not be read"
1463 mail-signature-file))
1464 (save-excursion
1465 (unless atpoint
1466 (goto-char (point-max))
1467 ;; Delete trailing whitespace and blank lines.
1468 (skip-chars-backward " \t\n")
1469 (end-of-line)
1470 (delete-region (point) (point-max)))
1471 (cond ((stringp mail-signature)
1472 (insert mail-signature))
1473 ((memq mail-signature '(t nil))
1474 (insert "\n\n-- \n")
1475 (insert-file-contents (expand-file-name mail-signature-file)))
1477 ;; FIXME add condition-case error handling?
1478 (eval mail-signature))))))
1480 (defun mail-fill-yanked-message (&optional justifyp)
1481 "Fill the paragraphs of a message yanked into this one.
1482 Numeric argument means justify as well."
1483 (interactive "P")
1484 (save-excursion
1485 (goto-char (mail-text-start))
1486 (fill-individual-paragraphs (point)
1487 (point-max)
1488 justifyp
1489 mail-citation-prefix-regexp)))
1491 (defun mail-indent-citation ()
1492 "Modify text just inserted from a message to be cited.
1493 The inserted text should be the region.
1494 When this function returns, the region is again around the modified text.
1496 Normally, indent each nonblank line `mail-indentation-spaces' spaces.
1497 However, if `mail-yank-prefix' is non-nil, insert that prefix on each line."
1498 (mail-yank-clear-headers (region-beginning) (region-end))
1499 (if (null mail-yank-prefix)
1500 (indent-rigidly (region-beginning) (region-end)
1501 mail-indentation-spaces)
1502 (save-excursion
1503 (let ((end (set-marker (make-marker) (region-end))))
1504 (goto-char (region-beginning))
1505 (while (< (point) end)
1506 (insert mail-yank-prefix)
1507 (forward-line 1))))))
1509 (defun mail-yank-original (arg)
1510 "Insert the message being replied to, if any (in Rmail).
1511 Puts point after the text and mark before.
1512 Normally, indents each nonblank line ARG spaces (default 3).
1513 However, if `mail-yank-prefix' is non-nil, insert that prefix on each line.
1515 Just \\[universal-argument] as argument means don't indent, insert no prefix,
1516 and don't delete any header fields."
1517 (interactive "P")
1518 (if mail-reply-action
1519 (let ((start (point))
1520 (original mail-reply-action)
1521 (omark (mark t)))
1522 (and (consp original) (eq (car original) 'insert-buffer)
1523 (setq original (nth 1 original)))
1524 (if (consp original)
1525 (progn
1526 ;; Call yank function, and set the mark if it doesn't.
1527 (apply (car original) (cdr original))
1528 (if (eq omark (mark t))
1529 (push-mark (point))))
1530 ;; If the original message is in another window in the same
1531 ;; frame, delete that window to save space.
1532 (delete-windows-on original t)
1533 (with-no-warnings
1534 ;; We really want this to set mark.
1535 (insert-buffer original)
1536 ;; If they yank the original text, the encoding of the
1537 ;; original message is a better default than
1538 ;; the default buffer-file-coding-system.
1539 (and (coding-system-equal
1540 (default-value 'buffer-file-coding-system)
1541 buffer-file-coding-system)
1542 (setq buffer-file-coding-system
1543 (coding-system-change-text-conversion
1544 buffer-file-coding-system
1545 (coding-system-base
1546 (with-current-buffer original
1547 buffer-file-coding-system))))))
1548 (set-text-properties (point) (mark t) nil))
1549 (if (consp arg)
1551 (goto-char start)
1552 (let ((mail-indentation-spaces (if arg (prefix-numeric-value arg)
1553 mail-indentation-spaces))
1554 ;; Avoid error in Transient Mark mode
1555 ;; on account of mark's being inactive.
1556 (mark-even-if-inactive t))
1557 (cond (mail-citation-hook
1558 ;; Bind mail-citation-header to the inserted
1559 ;; message's header.
1560 (let ((mail-citation-header
1561 (buffer-substring-no-properties
1562 start
1563 (save-excursion
1564 (save-restriction
1565 (narrow-to-region start (point-max))
1566 (goto-char start)
1567 (rfc822-goto-eoh)
1568 (point))))))
1569 (run-hooks 'mail-citation-hook)))
1570 (mail-yank-hooks
1571 (run-hooks 'mail-yank-hooks))
1573 (mail-indent-citation)))))
1574 ;; This is like exchange-point-and-mark, but doesn't activate the mark.
1575 ;; It is cleaner to avoid activation, even though the command
1576 ;; loop would deactivate the mark because we inserted text.
1577 (goto-char (prog1 (mark t)
1578 (set-marker (mark-marker) (point) (current-buffer))))
1579 (if (not (eolp)) (insert ?\n)))))
1581 (defun mail-yank-clear-headers (start end)
1582 (if (< end start)
1583 (let (temp)
1584 (setq temp start start end end temp)))
1585 (if mail-yank-ignored-headers
1586 (save-excursion
1587 (goto-char start)
1588 (if (search-forward "\n\n" end t)
1589 (save-restriction
1590 (narrow-to-region start (point))
1591 (goto-char start)
1592 (while (let ((case-fold-search t))
1593 (re-search-forward mail-yank-ignored-headers nil t))
1594 (beginning-of-line)
1595 (delete-region (point)
1596 (progn (re-search-forward "\n[^ \t]")
1597 (forward-char -1)
1598 (point)))))))))
1600 (defun mail-yank-region (arg)
1601 "Insert the selected region from the message being replied to.
1602 Puts point after the text and mark before.
1603 Normally, indents each nonblank line ARG spaces (default 3).
1604 However, if `mail-yank-prefix' is non-nil, insert that prefix on each line.
1606 Just \\[universal-argument] as argument means don't indent, insert no prefix,
1607 and don't delete any header fields."
1608 (interactive "P")
1609 (and (consp mail-reply-action)
1610 (eq (car mail-reply-action) 'insert-buffer)
1611 (with-current-buffer (nth 1 mail-reply-action)
1612 (or (mark t)
1613 (error "No mark set: %S" (current-buffer))))
1614 (let ((buffer (nth 1 mail-reply-action))
1615 (start (point))
1616 ;; Avoid error in Transient Mark mode
1617 ;; on account of mark's being inactive.
1618 (mark-even-if-inactive t))
1619 ;; Insert the citation text.
1620 (insert (with-current-buffer buffer
1621 (buffer-substring-no-properties (point) (mark))))
1622 (push-mark start)
1623 ;; Indent or otherwise annotate the citation text.
1624 (if (consp arg)
1626 (let ((mail-indentation-spaces (if arg (prefix-numeric-value arg)
1627 mail-indentation-spaces)))
1628 (if mail-citation-hook
1629 ;; Bind mail-citation-header to the original message's header.
1630 (let ((mail-citation-header
1631 (with-current-buffer buffer
1632 (buffer-substring-no-properties
1633 (point-min)
1634 (save-excursion
1635 (goto-char (point-min))
1636 (rfc822-goto-eoh)
1637 (point))))))
1638 (run-hooks 'mail-citation-hook))
1639 (if mail-yank-hooks
1640 (run-hooks 'mail-yank-hooks)
1641 (mail-indent-citation))))))))
1643 (defun mail-split-line ()
1644 "Split current line, moving portion beyond point vertically down.
1645 If the current line has `mail-yank-prefix', insert it on the new line."
1646 (interactive "*")
1647 (split-line mail-yank-prefix))
1650 (defun mail-attach-file (&optional file)
1651 "Insert a file at the end of the buffer, with separator lines around it."
1652 (interactive "fAttach file: ")
1653 (save-excursion
1654 (goto-char (point-max))
1655 (or (bolp) (newline))
1656 (newline)
1657 (let ((start (point))
1658 middle)
1659 (insert (format "===File %s===" file))
1660 (insert-char ?= (max 0 (- 60 (current-column))))
1661 (newline)
1662 (setq middle (point))
1663 (insert "============================================================\n")
1664 (push-mark)
1665 (goto-char middle)
1666 (insert-file-contents file)
1667 (or (bolp) (newline))
1668 (goto-char start))))
1670 ;; Put these commands last, to reduce chance of lossage from quitting
1671 ;; in middle of loading the file.
1673 ;;;###autoload
1674 (defun mail (&optional noerase to subject in-reply-to cc replybuffer
1675 actions return-action)
1676 "Edit a message to be sent. Prefix arg means resume editing (don't erase).
1677 When this function returns, the buffer `*mail*' is selected.
1678 The value is t if the message was newly initialized; otherwise, nil.
1680 Optionally, the signature file `mail-signature-file' can be inserted at the
1681 end; see the variable `mail-signature'.
1683 \\<mail-mode-map>
1684 While editing message, type \\[mail-send-and-exit] to send the message and exit.
1686 Various special commands starting with C-c are available in sendmail mode
1687 to move to message header fields:
1688 \\{mail-mode-map}
1690 If `mail-self-blind' is non-nil, a BCC to yourself is inserted
1691 when the message is initialized.
1693 If `mail-default-reply-to' is non-nil, it should be an address (a string);
1694 a Reply-to: field with that address is inserted.
1696 If `mail-archive-file-name' is non-nil, an FCC field with that file name
1697 is inserted.
1699 The normal hook `mail-setup-hook' is run after the message is
1700 initialized. It can add more default fields to the message.
1702 The first argument, NOERASE, determines what to do when there is
1703 an existing modified `*mail*' buffer. If NOERASE is nil, the
1704 existing mail buffer is used, and the user is prompted whether to
1705 keep the old contents or to erase them. If NOERASE has the value
1706 `new', a new mail buffer will be created instead of using the old
1707 one. Any other non-nil value means to always select the old
1708 buffer without erasing the contents.
1710 The second through fifth arguments,
1711 TO, SUBJECT, IN-REPLY-TO and CC, specify if non-nil
1712 the initial contents of those header fields.
1713 These arguments should not have final newlines.
1714 The sixth argument REPLYBUFFER is a buffer which contains an
1715 original message being replied to, or else an action
1716 of the form (FUNCTION . ARGS) which says how to insert the original.
1717 Or it can be nil, if not replying to anything.
1718 The seventh argument ACTIONS is a list of actions to take
1719 if/when the message is sent. Each action looks like (FUNCTION . ARGS);
1720 when the message is sent, we apply FUNCTION to ARGS.
1721 This is how Rmail arranges to mark messages `answered'."
1722 (interactive "P")
1723 (if (eq noerase 'new)
1724 (pop-to-buffer-same-window (generate-new-buffer "*mail*"))
1725 (and noerase
1726 (not (get-buffer "*mail*"))
1727 (setq noerase nil))
1728 (pop-to-buffer-same-window "*mail*"))
1730 ;; Avoid danger that the auto-save file can't be written.
1731 (let ((dir (expand-file-name
1732 (file-name-as-directory mail-default-directory))))
1733 (if (file-exists-p dir)
1734 (setq default-directory dir)))
1735 ;; Only call auto-save-mode if necessary, to avoid changing auto-save file.
1736 (if (or (and auto-save-default (not buffer-auto-save-file-name))
1737 (and (not auto-save-default) buffer-auto-save-file-name))
1738 (auto-save-mode auto-save-default))
1739 (mail-mode)
1740 ;; Disconnect the buffer from its visited file
1741 ;; (in case the user has actually visited a file *mail*).
1742 ;; (set-visited-file-name nil)
1743 (let (initialized)
1744 (and (not (and noerase
1745 (not (eq noerase 'new))))
1746 (if buffer-file-name
1747 (if (buffer-modified-p)
1748 (when (y-or-n-p "Buffer has unsaved changes; reinitialize it and discard them? ")
1749 (if (y-or-n-p "Disconnect buffer from visited file? ")
1750 (set-visited-file-name nil))
1752 (when (y-or-n-p "Reinitialize buffer, and disconnect it from the visited file? ")
1753 (set-visited-file-name nil)
1755 ;; A non-file-visiting buffer.
1756 (if (buffer-modified-p)
1757 (y-or-n-p "Unsent message being composed; erase it? ")
1759 (let ((inhibit-read-only t))
1760 (erase-buffer)
1761 (mail-setup to subject in-reply-to cc replybuffer actions
1762 return-action)
1763 (setq initialized t)))
1764 (if (and buffer-auto-save-file-name
1765 (file-exists-p buffer-auto-save-file-name))
1766 (message "Auto save file for draft message exists; consider M-x mail-recover"))
1767 initialized))
1769 (declare-function dired-view-file "dired" ())
1770 (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
1772 (defun mail-recover-1 ()
1773 "Pop up a list of auto-saved draft messages so you can recover one of them."
1774 (interactive)
1775 (let ((file-name (make-auto-save-file-name))
1776 (ls-lisp-support-shell-wildcards t)
1777 non-random-len wildcard)
1778 ;; Remove the random part from the auto-save-file-name, and
1779 ;; create a wildcard which matches possible candidates.
1780 ;; Note: this knows that make-auto-save-file-name appends
1781 ;; "#<RANDOM-STUFF>#" to the buffer name, where RANDOM-STUFF
1782 ;; is the result of (make-temp-name "").
1783 (setq non-random-len
1784 (- (length file-name) (length (make-temp-name "")) 1))
1785 (setq wildcard (concat (substring file-name 0 non-random-len) "*"))
1786 (if (null (file-expand-wildcards wildcard))
1787 (message "There are no auto-saved drafts to recover")
1788 ;; Bind dired-trivial-filenames to t because all auto-save file
1789 ;; names are normally ``trivial'', so Dired will set point after
1790 ;; all the files, at buffer bottom. We want it on the first
1791 ;; file instead.
1792 ;; Require dired so that dired-trivial-filenames does not get
1793 ;; unbound on exit from the let.
1794 (require 'dired)
1795 (let ((dired-trivial-filenames t))
1796 (dired-other-window wildcard (concat dired-listing-switches "t")))
1797 (rename-buffer "*Auto-saved Drafts*" t)
1798 (save-excursion
1799 (goto-char (point-min))
1800 (or (looking-at " Move to the draft file you want to recover,")
1801 (let ((inhibit-read-only t))
1802 ;; Each line starts with a space so that Font Lock mode
1803 ;; won't highlight the first character.
1804 (insert "\
1805 Move to the draft file you want to recover, then type C-c C-c
1806 to recover text of message whose composition was interrupted.
1807 To browse text of a draft, type v on the draft file's line.
1809 You can also delete some of these files;
1810 type d on a line to mark that file for deletion.
1812 List of possible auto-save files for recovery:
1814 "))))
1815 (use-local-map
1816 (let ((map (make-sparse-keymap)))
1817 (set-keymap-parent map (current-local-map))
1818 map))
1819 (define-key (current-local-map) "v"
1820 (lambda ()
1821 (interactive)
1822 (let ((coding-system-for-read 'utf-8-emacs-unix))
1823 (dired-view-file))))
1824 (define-key (current-local-map) "\C-c\C-c"
1825 (lambda ()
1826 (interactive)
1827 (let ((fname (dired-get-filename))
1828 ;; Auto-saved files are written in the internal
1829 ;; representation, so they should be read accordingly.
1830 (coding-system-for-read 'utf-8-emacs-unix))
1831 (switch-to-buffer-other-window "*mail*")
1832 (let ((buffer-read-only nil))
1833 (erase-buffer)
1834 (insert-file-contents fname nil)
1835 ;; insert-file-contents will set buffer-file-coding-system
1836 ;; to utf-8-emacs, which is probably not what they want to
1837 ;; use for sending the message. But we don't know what
1838 ;; was its value before the buffer was killed or Emacs
1839 ;; crashed. We therefore reset buffer-file-coding-system
1840 ;; to the default value, so that either the default does
1841 ;; TRT, or the user will get prompted for the right
1842 ;; encoding when they send the message.
1843 (setq buffer-file-coding-system
1844 (default-value 'buffer-file-coding-system)))))))))
1846 (declare-function dired-move-to-filename "dired" (&optional raise-error eol))
1847 (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
1848 (declare-function dired-view-file "dired" ())
1850 (defun mail-recover ()
1851 "Recover interrupted mail composition from auto-save files.
1853 If the mail buffer has a current valid auto-save file,
1854 the command recovers that file. Otherwise, it displays a
1855 buffer showing the existing auto-saved draft messages;
1856 you can move to one of them and type C-c C-c to recover that one."
1857 (interactive)
1858 ;; In case they invoke us from some random buffer...
1859 (switch-to-buffer "*mail*")
1860 ;; If *mail* didn't exist, set its directory, so that auto-saved
1861 ;; drafts will be found.
1862 (let ((dir (expand-file-name
1863 (file-name-as-directory mail-default-directory))))
1864 (if (file-exists-p dir)
1865 (setq default-directory dir)))
1866 (or (eq major-mode 'mail-mode)
1867 (mail-mode))
1868 (let ((file-name buffer-auto-save-file-name))
1869 (cond ((and file-name (file-exists-p file-name))
1870 (let ((dispbuf
1871 ;; This used to invoke `ls' via call-process, but
1872 ;; dired-noselect is more portable to systems where
1873 ;; `ls' is not a standard program (it will use
1874 ;; ls-lisp instead).
1875 (dired-noselect file-name
1876 (concat dired-listing-switches "t"))))
1877 (save-selected-window
1878 (select-window (display-buffer dispbuf t))
1879 (goto-char (point-min))
1880 (forward-line 2)
1881 (dired-move-to-filename)
1882 (setq dispbuf (rename-buffer "*Directory*" t)))
1883 (if (not (yes-or-no-p
1884 (format "Recover mail draft from auto save file %s? "
1885 file-name)))
1886 (error "mail-recover cancelled")
1887 (let ((buffer-read-only nil)
1888 (buffer-coding buffer-file-coding-system)
1889 ;; Auto-save files are written in internal
1890 ;; representation of non-ASCII characters.
1891 (coding-system-for-read 'utf-8-emacs-unix))
1892 (erase-buffer)
1893 (insert-file-contents file-name nil)
1894 (setq buffer-file-coding-system buffer-coding)))))
1895 (t (mail-recover-1)))))
1897 ;;;###autoload
1898 (defun mail-other-window (&optional noerase to subject in-reply-to cc replybuffer sendactions)
1899 "Like `mail' command, but display mail buffer in another window."
1900 (interactive "P")
1901 (pop-to-buffer-other-window "*mail*")
1902 (mail noerase to subject in-reply-to cc replybuffer sendactions))
1904 ;;;###autoload
1905 (defun mail-other-frame (&optional noerase to subject in-reply-to cc replybuffer sendactions)
1906 "Like `mail' command, but display mail buffer in another frame."
1907 (interactive "P")
1908 (pop-to-buffer-other-frame "*mail*")
1909 (mail noerase to subject in-reply-to cc replybuffer sendactions))
1911 ;; Do not add anything but external entries on this page.
1913 (provide 'sendmail)
1915 ;;; sendmail.el ends here