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