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