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