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