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