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