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