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