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