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