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