(sc-mail-field): Use assoc-string.
[bpt/emacs.git] / lisp / mail / sendmail.el
CommitLineData
f70de9d4 1;;; sendmail.el --- mail sending commands for Emacs. -*- byte-compile-dynamic: t -*-
c88ab9ce 2
aa83be2a 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
00164828 71the variable `mail-envelope-from', with `user-mail-address' as fallback.
32afb744 72
01bb608e 73On most systems, specifying the envelope-from address is a
00164828
GM
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
3ca0be33 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:" "\
0b5bb3ec
SE
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.
911ebffa
RS
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
3ca0be33 216;;;###autoload
0b5bb3ec 217(defcustom mail-citation-hook nil
1c24b04a 218 "*Hook for modifying a citation just inserted in the mail buffer.
d6bccf09
RS
219Each hook function can find the citation between (point) and (mark t),
220and should leave point and mark around the citation text as modified.
221The hook functions can find the header of the cited message
222in the variable `mail-citation-header', whether or not this is included
223in the cited portion of the message.
1c24b04a 224
015c5c55 225If this hook is entirely empty (nil), a default action is taken
0b5bb3ec
SE
226instead of no action."
227 :type 'hook
228 :group 'sendmail)
20a8832d 229
d6bccf09
RS
230(defvar mail-citation-header nil
231 "While running `mail-citation-hook', this variable holds the message header.
232This enables the hook functions to see the whole message header
233regardless of what part of it (if any) is included in the cited text.")
234
706e3d85
RS
235(defcustom mail-citation-prefix-regexp "[ \t]*[-a-z0-9A-Z]*>+[ \t]*\\|[ \t]*"
236 "*Regular expression to match a citation prefix plus whitespace.
237It should match whatever sort of citation prefixes you want to handle,
238with whitespace before and after; it should also match just whitespace.
239The default value matches citations like `foo-bar>' plus whitespace."
240 :type 'regexp
241 :group 'sendmail
242 :version "20.3")
243
20a8832d
RS
244(defvar mail-abbrevs-loaded nil)
245(defvar mail-mode-map nil)
246
5ef9d627 247(autoload 'build-mail-aliases "mailalias"
8790b698 248 "Read mail aliases from user's personal aliases file and set `mail-aliases'."
5ef9d627
RS
249 nil)
250
251(autoload 'expand-mail-aliases "mailalias"
252 "Expand all mail aliases in suitable header fields found between BEG and END.
253Suitable header fields are `To', `Cc' and `Bcc' and their `Resent-' variants.
254Optional second arg EXCLUDE may be a regular expression defining text to be
255removed from alias expansions."
256 nil)
257
ef15f270 258;;;###autoload
0b5bb3ec 259(defcustom mail-signature nil
547075d4 260 "*Text inserted at end of mail buffer when a message is initialized.
f13c1dae
RS
261If t, it means to insert the contents of the file `mail-signature-file'.
262If 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.)
265Otherwise, it should be an expression; it is evaluated
266and should insert whatever you want to insert."
eb1213c1 267 :type '(choice (const :tag "None" nil)
f13c1dae
RS
268 (const :tag "Use `.signature' file" t)
269 (string :tag "String to insert")
270 (sexp :tag "Expression to evaluate"))
0b5bb3ec 271 :group 'sendmail)
f13c1dae 272(put 'mail-signature 'risky-local-variable t)
4ac2e032 273
0b5bb3ec
SE
274(defcustom mail-signature-file "~/.signature"
275 "*File containing the text inserted at end of mail buffer."
276 :type 'file
277 :group 'sendmail)
20a8832d 278
eb1213c1
MR
279;;;###autoload
280(defcustom mail-default-directory "~/"
281 "*Directory for mail buffers.
282Value of `default-directory' for mail buffers.
283This directory is used for auto-save files of mail buffers."
284 :type '(directory :tag "Directory")
65280ff3 285 :group 'sendmail
bf247b6e 286 :version "22.1")
eb1213c1 287
c8553837 288(defvar mail-reply-action nil)
20a8832d
RS
289(defvar mail-send-actions nil
290 "A list of actions to be performed upon successful sending of a message.")
c8553837 291(put 'mail-reply-action 'permanent-local t)
98bd608d 292(put 'mail-send-actions 'permanent-local t)
20a8832d 293
0b5bb3ec 294(defcustom mail-default-headers nil
20a8832d
RS
295 "*A string containing header lines, to be inserted in outgoing messages.
296It is inserted before you edit the message,
0b5bb3ec
SE
297so you can edit or delete these lines."
298 :type '(choice (const nil) string)
299 :group 'sendmail)
20a8832d 300
0b5bb3ec 301(defcustom mail-bury-selects-summary t
1f7850dd
RS
302 "*If non-nil, try to show RMAIL summary buffer after returning from mail.
303The functions \\[mail-send-on-exit] or \\[mail-dont-send] select
304the RMAIL summary buffer before returning, if it exists and this variable
0b5bb3ec
SE
305is non-nil."
306 :type 'boolean
307 :group 'sendmail)
1f7850dd 308
baaa1f19 309(defcustom mail-send-nonascii 'mime
34153d76
RS
310 "*Specify whether to allow sending non-ASCII characters in mail.
311If t, that means do allow it. nil means don't allow it.
312`query' means ask the user each time.
baaa1f19
GM
313`mime' means add an appropriate MIME header if none already present.
314The default is `mime'.
34153d76 315Including non-ASCII characters in a mail message can be problematical
0b5bb3ec 316for the recipient, who may not know how to decode them properly."
baaa1f19 317 :type '(choice (const t) (const nil) (const query) (const mime))
0b5bb3ec 318 :group 'sendmail)
34153d76 319
0b0b4ce7
RS
320(defcustom mail-use-dsn nil
321 "*Ask MTA for notification of failed, delayed or successful delivery.
322Note that only some MTAs (currently only recent versions of Sendmail)
323support Delivery Status Notification."
324 :group 'sendmail
325 :type '(repeat (radio (const :tag "Failure" failure)
326 (const :tag "Delay" delay)
327 (const :tag "Success" success)))
bf247b6e 328 :version "22.1")
0b0b4ce7 329
7c203051
RS
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
01b5c04d 333 (if (and (string-match "sparc-sun-sunos\\(\\'\\|[^5]\\)" system-configuration)
47c5e807 334 (file-readable-p "/etc/sendmail.cf")
01b5c04d
RS
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))
5a853005 342 (re-search-forward "^OR\\>" nil t)))
01b5c04d 343 (kill-buffer buffer))))
9a41d08a
NF
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{|}~]+:"))
7c203051
RS
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.)
351The value should be an expression to test whether the problem will
352actually occur.")
353
f81b71c7
SM
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'.")
20a8832d 360
8b1f1a0c 361(defvar mail-font-lock-keywords
70a302f8 362 (eval-when-compile
f1052d2e 363 (let* ((cite-chars "[>|}]")
0eafc06c 364 (cite-prefix "[:alpha:]")
f1052d2e 365 (cite-suffix (concat cite-prefix "0-9_.@-`'\"")))
3d51068d 366 (list '("^\\(To\\|Newsgroups\\):" . font-lock-function-name-face)
3ca0be33 367 '("^\\(B?CC\\|Reply-to\\|Mail-\\(reply\\|followup\\)-to\\):" . font-lock-keyword-face)
70a302f8
SM
368 '("^\\(Subject:\\)[ \t]*\\(.+\\)?"
369 (1 font-lock-comment-face) (2 font-lock-type-face nil t))
f1052d2e 370 ;; Use EVAL to delay in case `mail-header-separator' gets changed.
3d51068d 371 '(eval .
e69d810c 372 (let ((separator (if (zerop (length mail-header-separator))
8646fe22 373 " \\`\\' "
e69d810c
SM
374 (regexp-quote mail-header-separator))))
375 (cons (concat "^" separator "$") 'font-lock-warning-face)))
f1052d2e
SM
376 ;; Use MATCH-ANCHORED to effectively anchor the regexp left side.
377 `(,cite-chars
378 (,(concat "\\=[ \t]*"
3d51068d
SM
379 "\\(\\([" cite-prefix "]+[" cite-suffix "]*\\)?"
380 "\\(" cite-chars "[ \t]*\\)\\)+"
381 "\\(.*\\)")
f1052d2e 382 (beginning-of-line) (end-of-line)
883212ce 383 (2 font-lock-constant-face nil t)
3d51068d 384 (4 font-lock-comment-face nil t)))
c034acdf 385 '("^\\(X-[A-Za-z0-9-]+\\|In-reply-to\\):.*\\(\n[ \t]+.*\\)*$"
70a302f8 386 . font-lock-string-face))))
8b1f1a0c
RS
387 "Additional expressions to highlight in Mail mode.")
388
5b6575b7 389\f
1498db18 390(defun sendmail-sync-aliases ()
74f8a333
RS
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)))))
9cc75191 396
20a8832d 397(defun mail-setup (to subject in-reply-to cc replybuffer actions)
235c2d35 398 (or mail-default-reply-to
810163a3 399 (setq mail-default-reply-to (getenv "REPLYTO")))
1498db18 400 (sendmail-sync-aliases)
5ef9d627
RS
401 (if (eq mail-aliases t)
402 (progn
403 (setq mail-aliases nil)
74f8a333
RS
404 (when mail-personal-alias-file
405 (if (file-exists-p mail-personal-alias-file)
406 (build-mail-aliases)))))
2813dcb9 407 ;; Don't leave this around from a previous message.
f8081291 408 (kill-local-variable 'buffer-file-coding-system)
9be82109
KH
409 ;; This doesn't work for enable-multibyte-characters.
410 ;; (kill-local-variable 'enable-multibyte-characters)
411 (set-buffer-multibyte default-enable-multibyte-characters)
8881c1ab
KH
412 (if current-input-method
413 (inactivate-input-method))
20a8832d 414 (setq mail-send-actions actions)
c8553837 415 (setq mail-reply-action replybuffer)
20a8832d
RS
416 (goto-char (point-min))
417 (insert "To: ")
418 (save-excursion
419 (if to
70ca75ae
RS
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)))
20a8832d 425 (insert to "\n")
118a3c04
RS
426 (fill-region-as-paragraph address-start (point-max))
427 (goto-char (point-max))
428 (unless (bolp)
429 (newline)))
20a8832d
RS
430 (newline))
431 (if cc
70ca75ae
RS
432 (let ((fill-prefix "\t")
433 (address-start (progn (insert "CC: ") (point))))
434 (insert cc "\n")
118a3c04
RS
435 (fill-region-as-paragraph address-start (point-max))
436 (goto-char (point-max))
437 (unless (bolp)
438 (newline))))
20a8832d 439 (if in-reply-to
118a3c04 440 (let ((fill-prefix "\t")
58cabff0 441 (fill-column 78)
34046d66
RS
442 (address-start (point)))
443 (insert "In-reply-to: " in-reply-to "\n")
118a3c04
RS
444 (fill-region-as-paragraph address-start (point-max))
445 (goto-char (point-max))
446 (unless (bolp)
447 (newline))))
20a8832d
RS
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
22c03d39 454 (insert "BCC: " user-mail-address "\n"))
20a8832d
RS
455 (if mail-archive-file-name
456 (insert "FCC: " mail-archive-file-name "\n"))
6737ae74
RS
457 (put-text-property (point)
458 (progn
459 (insert mail-header-separator "\n")
460 (1- (point)))
461 'category 'mail-header-separator)
874bdb5a
RS
462 ;; Insert the signature. But remember the beginning of the message.
463 (if to (setq to (point)))
547075d4 464 (cond ((eq mail-signature t)
4ac2e032 465 (if (file-exists-p mail-signature-file)
a5282b8f
RS
466 (progn
467 (insert "\n\n-- \n")
4ac2e032 468 (insert-file-contents mail-signature-file))))
f13c1dae
RS
469 ((stringp mail-signature)
470 (insert mail-signature))
471 (t
472 (eval mail-signature)))
20a8832d
RS
473 (goto-char (point-max))
474 (or (bolp) (newline)))
874bdb5a 475 (if to (goto-char to))
20a8832d
RS
476 (or to subject in-reply-to
477 (set-buffer-modified-p nil))
478 (run-hooks 'mail-setup-hook))
5b6575b7 479\f
be0b8d69
DL
480(defcustom mail-mode-hook nil
481 "Hook run by Mail mode."
482 :group 'sendmail
483 :type 'hook
484 :options '(footnote-mode))
485
567bb3dd 486(defvar mail-mode-abbrev-table text-mode-abbrev-table)
20a8832d 487;;;###autoload
f81b71c7 488(define-derived-mode mail-mode text-mode "Mail"
20a8832d
RS
489 "Major mode for editing mail to be sent.
490Like Text Mode but with these additional commands:
d3db616e
KH
491\\[mail-send] mail-send (send the message) \\[mail-send-and-exit] mail-send-and-exit
492Here 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:
0678d779 495 \\[mail-fcc] move to FCC: \\[mail-reply-to] move to Reply-To:
3ca0be33
RS
496 \\[mail-mail-reply-to] move to Mail-Reply-To:
497 \\[mail-mail-followup-to] move to Mail-Followup-To:
d3db616e
KH
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).
4deb3ba9
EZ
502\\[mail-sent-via] mail-sent-via (add a Sent-via field for each To or CC).
503Turning on Mail mode runs the normal hooks `text-mode-hook' and
504`mail-mode-hook' (in that order)."
c8553837 505 (make-local-variable 'mail-reply-action)
20a8832d 506 (make-local-variable 'mail-send-actions)
20a8832d 507 (setq buffer-offer-save t)
2667df42 508 (make-local-variable 'font-lock-defaults)
117e4b0c 509 (setq font-lock-defaults '(mail-font-lock-keywords t t))
20a8832d 510 (make-local-variable 'paragraph-separate)
5b6575b7
RS
511 (make-local-variable 'normal-auto-fill-function)
512 (setq normal-auto-fill-function 'mail-mode-auto-fill)
b5682230 513 (make-local-variable 'fill-paragraph-function)
c8553837 514 (setq fill-paragraph-function 'mail-mode-fill-paragraph)
eba52a1a
MB
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)
fc2a68b5
SM
518 (if mail-yank-prefix
519 (set (make-local-variable 'comment-start-skip)
520 (concat "^" (regexp-quote mail-yank-prefix) "[ \t]*")))
1b8def58
RS
521 (make-local-variable 'adaptive-fill-regexp)
522 (setq adaptive-fill-regexp
567a1c29 523 (concat "[ \t]*[-[:alnum:]]+>+[ \t]*\\|"
f81b71c7 524 adaptive-fill-regexp))
d78f466f 525 (make-local-variable 'adaptive-fill-first-line-regexp)
f03ca5f5 526 (setq adaptive-fill-first-line-regexp
40035487 527 (concat "[ \t]*[-[:alnum:]]*>+[ \t]*\\|"
f81b71c7 528 adaptive-fill-first-line-regexp))
df0d89b1
RS
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.
f3e95b0d 533 (setq paragraph-separate (concat (regexp-quote mail-header-separator)
75e4db34 534 "$\\|\t*\\([-|#;>* ]\\|(?[0-9]+[.)]\\)+$"
0eafc06c 535 "\\|[ \t]*[[:alnum:]]*>+[ \t]*$\\|[ \t]*$\\|"
f3e95b0d
SM
536 "--\\( \\|-+\\)$\\|"
537 page-delimiter)))
5b6575b7 538
51d541d4
RS
539
540(defun mail-header-end ()
541 "Return the buffer location of the end of headers, as a number."
66191b7e
RS
542 (save-restriction
543 (widen)
544 (save-excursion
545 (rfc822-goto-eoh)
546 (point))))
51d541d4
RS
547
548(defun mail-text-start ()
549 "Return the buffer location of the start of text, as a number."
66191b7e
RS
550 (save-restriction
551 (widen)
552 (save-excursion
553 (rfc822-goto-eoh)
554 (forward-line 1)
555 (point))))
51d541d4
RS
556
557(defun mail-sendmail-delimit-header ()
558 "Set up whatever header delimiter convention sendmail will use.
559Concretely: 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.
566Leave point at the start of the delimiter line."
567 (rfc822-goto-eoh)
568 (delete-region (point) (progn (end-of-line) (point))))
569
5b6575b7
RS
570(defun mail-mode-auto-fill ()
571 "Carry out Auto Fill for Mail mode.
572If within the headers, this makes the new lines into continuation lines."
51d541d4 573 (if (< (point) (mail-header-end))
5b6575b7
RS
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))
28afe199
RS
579 ;; Use insert-before-markers in case we're inserting
580 ;; before the saved value of point (which is common).
581 (insert-before-markers " ")
5b6575b7
RS
582 (forward-line -1))
583 t)))
584 (do-auto-fill)))
c8553837
RS
585
586(defun mail-mode-fill-paragraph (arg)
587 ;; Do something special only if within the headers.
51d541d4 588 (if (< (point) (mail-header-end))
a1506d29 589 (let (beg end fieldname)
8fcbbf7d
GM
590 (when (prog1 (re-search-backward "^[-a-zA-Z]+:" nil 'yes)
591 (setq beg (point)))
c8553837 592 (setq fieldname
8fcbbf7d 593 (downcase (buffer-substring beg (1- (match-end 0))))))
c8553837
RS
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"
3ca0be33 605 "mail-reply-to" "mail-followup-to"
c8553837
RS
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)))
299fdd8d
RS
620\f
621;;; Set up keymap.
20a8832d
RS
622
623(if mail-mode-map
624 nil
f81b71c7 625 (setq mail-mode-map (make-sparse-keymap))
c21b5781 626 (define-key mail-mode-map "\M-\t" 'mail-complete)
20a8832d
RS
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)
70ee42f7 630 (define-key mail-mode-map "\C-c\C-f\C-f" 'mail-fcc)
20a8832d
RS
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)
3a3ffba4 633 (define-key mail-mode-map "\C-c\C-f\C-r" 'mail-reply-to)
3ca0be33
RS
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
20a8832d
RS
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)
e39e74c0 638 (define-key mail-mode-map "\C-c\C-r" 'mail-yank-region)
8ba7cd40 639 (define-key mail-mode-map [remap split-line] 'mail-split-line)
20a8832d
RS
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)
f0aef6bf
RS
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))
299fdd8d
RS
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
1abf89b8
SM
659(define-key mail-mode-map [menu-bar mail mail-sep]
660 '("--"))
661
1b6bb250
RS
662(define-key mail-mode-map [menu-bar mail cancel]
663 '("Cancel" . mail-dont-send))
664
299fdd8d
RS
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]
fa8afe42 672 (cons "Headers" (make-sparse-keymap "Move to Header")))
299fdd8d 673
1abf89b8
SM
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))
3a3ffba4 679
299fdd8d
RS
680(define-key mail-mode-map [menu-bar headers sent-via]
681 '("Sent Via" . mail-sent-via))
682
3ca0be33
RS
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
1abf89b8
SM
689(define-key mail-mode-map [menu-bar headers reply-to]
690 '("Reply-To" . mail-reply-to))
299fdd8d
RS
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))
20a8832d 706\f
5b6575b7
RS
707;; User-level commands for sending.
708
2c1e2995 709(defun mail-send-and-exit (&optional arg)
33b4a308 710 "Send message like `mail-send', then, if no errors, exit from mail buffer.
20a8832d
RS
711Prefix arg means don't delete this window."
712 (interactive "P")
713 (mail-send)
1b6bb250
RS
714 (mail-bury arg))
715
8451b5e9 716(defun mail-dont-send (&optional arg)
1b6bb250
RS
717 "Don't send the message you have been editing.
718Prefix arg means don't delete this window."
719 (interactive "P")
720 (mail-bury arg))
721
8451b5e9 722(defun mail-bury (&optional arg)
1b6bb250 723 "Bury this mail buffer."
e49ad9a5
RS
724 (let ((newbuf (other-buffer (current-buffer))))
725 (bury-buffer (current-buffer))
54feedaa 726 (if (and (or (window-dedicated-p (frame-selected-window))
b03a1621 727 (cdr (assq 'mail-dedicated-frame (frame-parameters))))
9320d421 728 (not (null (delq (selected-frame) (visible-frame-list)))))
6f60853a
EZ
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)))
fdb90184
RS
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
1f7850dd
RS
742 (and mail-bury-selects-summary
743 (boundp 'rmail-summary-buffer)
6cfc977b 744 rmail-summary-buffer
fdb90184
RS
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))))))
20a8832d 753
be0b8d69
DL
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
3ca0be33
RS
760;;;###autoload
761(defcustom mail-mailing-lists nil "\
762*List of mailing list addresses the user is subscribed to.
763
764The variable is used to trigger insertion of the \"Mail-Followup-To\"
765header when sending a message to a mailing list."
766 :type '(repeat string)
767 :group 'sendmail)
768
769
20a8832d
RS
770(defun mail-send ()
771 "Send the message in the current buffer.
772If `mail-interactive' is non-nil, wait for success indication
773or error messages, and inform user.
774Otherwise any failure is reported in a message back to
775the user from the mailer."
776 (interactive)
e11094e6
RS
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? ")))
34153d76 781 (let ((inhibit-read-only t)
3ca0be33
RS
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"))))))
42d305db 821 (unless (memq mail-send-nonascii '(t mime))
34153d76
RS
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"))))
413d1aa2
RS
829 ;; Complain about any invalid line.
830 (goto-char (point-min))
51d541d4 831 (while (< (point) (mail-header-end))
413d1aa2
RS
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))
34153d76 836 (goto-char opoint)
20a8832d 837 (run-hooks 'mail-send-hook)
da5667c6 838 (message "Sending...")
20a8832d
RS
839 (funcall send-mail-function)
840 ;; Now perform actions on successful sending.
841 (while mail-send-actions
842 (condition-case nil
d2561f34
RS
843 (apply (car (car mail-send-actions))
844 (cdr (car mail-send-actions)))
20a8832d
RS
845 (error))
846 (setq mail-send-actions (cdr mail-send-actions)))
d2561f34 847 (message "Sending...done")
4e0c8650 848 ;; If buffer has no file, mark it as unmodified and delete auto-save.
d2561f34
RS
849 (if (not buffer-file-name)
850 (progn
851 (set-buffer-modified-p nil)
852 (delete-auto-save-file-if-necessary t))))))
56e5818f
SJ
853
854(defun mail-envelope-from ()
855 "Return the envelope mail address to use when sending mail.
856This 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))
5b6575b7
RS
861\f
862;; This does the real work of sending a message via sendmail.
863;; It is called via the variable send-mail-function.
20a8832d 864
ae54d287
KH
865;;;###autoload
866(defvar sendmail-coding-system nil
da241b5c 867 "*Coding system for encoding the outgoing mail.
265f03bc 868This has higher priority than `default-buffer-file-coding-system'
da241b5c
KH
869and `default-sendmail-coding-system',
870but lower priority than the local value of `buffer-file-coding-system'.
0b64a3da 871See also the function `select-message-coding-system'.")
da241b5c
KH
872
873;;;###autoload
874(defvar default-sendmail-coding-system 'iso-latin-1
265f03bc 875 "Default coding system for encoding the outgoing mail.
da241b5c
KH
876This variable is used only when `sendmail-coding-system' is nil.
877
878This variable is set/changed by the command set-language-environment.
879User should not set this variable manually,
880instead use sendmail-coding-system to get a constant encoding
881of outgoing mails regardless of the current language environment.
0b64a3da 882See also the function `select-message-coding-system'.")
ee0a4f29 883
20a8832d 884(defun sendmail-send-it ()
67ad9438
DL
885 "Send the current mail buffer using the Sendmail package.
886This is a suitable value for `send-mail-function'. It sends using the
887external program defined by `sendmail-program'."
8e183d5b 888 (require 'mail-utils)
20a8832d
RS
889 (let ((errbuf (if mail-interactive
890 (generate-new-buffer " sendmail errors")
538b9847 891 0))
20a8832d 892 (tembuf (generate-new-buffer " sendmail temp"))
c86237b2 893 (multibyte enable-multibyte-characters)
20a8832d 894 (case-fold-search nil)
a80d7290 895 (selected-coding (select-message-coding-system))
3ca0be33 896 resend-to-addresses
20a8832d 897 delimline
00365e98 898 fcc-was-found
3ff0518e
GM
899 (mailbuf (current-buffer))
900 (program (if (boundp 'sendmail-program)
901 sendmail-program
31bc27f6 902 "/usr/lib/sendmail"))
00417fac
RS
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
56e5818f 907 (or (mail-envelope-from) user-mail-address))))
20a8832d
RS
908 (unwind-protect
909 (save-excursion
910 (set-buffer tembuf)
911 (erase-buffer)
c86237b2
RS
912 (unless multibyte
913 (set-buffer-multibyte nil))
20a8832d
RS
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.
51d541d4
RS
920 (goto-char (mail-header-end))
921 (delete-region (point) (progn (end-of-line) (point)))
20a8832d 922 (setq delimline (point-marker))
1498db18 923 (sendmail-sync-aliases)
5ef9d627
RS
924 (if mail-aliases
925 (expand-mail-aliases (point-min) delimline))
20a8832d 926 (goto-char (point-min))
413d1aa2 927 ;; Ignore any blank lines in the header
20a8832d
RS
928 (while (and (re-search-forward "\n\n\n*" delimline t)
929 (< (point) delimline))
930 (replace-match "\n"))
413d1aa2 931 (goto-char (point-min))
3ca0be33
RS
932 ;; Look for Resent- headers. They require sending
933 ;; the message specially.
20a8832d 934 (let ((case-fold-search t))
3ca0be33
RS
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")))
20a8832d
RS
968 ;; Don't send out a blank subject line
969 (goto-char (point-min))
66961910 970 (if (re-search-forward "^Subject:\\([ \t]*\n\\)+\\b" delimline t)
54115447
RS
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 "")))
ea9ceb34
KH
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))
cbc9f037 980 (let* ((login user-mail-address)
0d15bc31
RS
981 (fullname (user-full-name))
982 (quote-fullname nil))
67ad9438
DL
983 (if (string-match "[^\0-\177]" fullname)
984 (setq fullname (rfc2047-encode-string fullname)
0d15bc31 985 quote-fullname t))
ea9ceb34 986 (cond ((eq mail-from-style 'angles)
7f1d5de2
RS
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.
0d15bc31
RS
993 (if (or (re-search-forward "[^- !#-'*+/-9=?A-Z^-~]"
994 fullname-end 1)
995 quote-fullname)
7f1d5de2
RS
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"))
ea9ceb34 1005 ((eq mail-from-style 'parens)
7dad528a
RS
1006 (insert "From: " login " (")
1007 (let ((fullname-start (point)))
0d15bc31
RS
1008 (if quote-fullname
1009 (insert "\""))
7dad528a 1010 (insert fullname)
0d15bc31
RS
1011 (if quote-fullname
1012 (insert "\""))
7dad528a
RS
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)
a1506d29 1023 (while (re-search-forward
7dad528a
RS
1024 "\\(\\=\\|[^\\]\\(\\\\\\\\\\)*\\)\\\\(\\(\\([^\\]\\|\\\\\\\\\\)*\\)\\\\)"
1025 fullname-end 1)
1026 (replace-match "\\1(\\3)" t)
1027 (goto-char fullname-start))))
7f1d5de2 1028 (insert ")\n"))
ea9ceb34 1029 ((null mail-from-style)
55c3534b
RS
1030 (insert "From: " login "\n"))
1031 ((eq mail-from-style 'system-default)
1032 nil)
e186b069 1033 (t (error "Invalid value for `mail-from-style'")))))
baaa1f19 1034 ;; Possibly add a MIME header for the current coding system
5ddcadb8 1035 (let (charset)
baaa1f19
GM
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)))
a80d7290 1041 selected-coding
baaa1f19 1042 (setq charset
5ddcadb8 1043 (coding-system-get selected-coding 'mime-charset))
baaa1f19
GM
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")))
7c203051
RS
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))
d6e000df
RS
1054 ;; Find and handle any FCC fields.
1055 (goto-char (point-min))
1056 (if (re-search-forward "^FCC:" delimline t)
00365e98
RS
1057 (progn
1058 (setq fcc-was-found t)
1059 (mail-do-fcc delimline)))
20a8832d
RS
1060 (if mail-interactive
1061 (save-excursion
1062 (set-buffer errbuf)
1063 (erase-buffer))))
00365e98
RS
1064 (goto-char (point-min))
1065 (if (let ((case-fold-search t))
3ca0be33
RS
1066 (or resend-to-addresses
1067 (re-search-forward "^To:\\|^cc:\\|^bcc:"
1068 delimline t)))
63b89616 1069 (let* ((default-directory "/")
a80d7290 1070 (coding-system-for-write selected-coding)
a1506d29 1071 (args
63b89616 1072 (append (list (point-min) (point-max)
3ff0518e 1073 program
63b89616 1074 nil errbuf nil "-oi")
00417fac
RS
1075 (and envelope-from
1076 (list "-f" envelope-from))
63b89616
KH
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"))
3ca0be33
RS
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
180d5509 1095 '("-t")
3ca0be33 1096 )
0b0b4ce7
RS
1097 (if mail-use-dsn
1098 (list "-N" (mapconcat 'symbol-name
1099 mail-use-dsn ",")))
180d5509
GM
1100 )
1101 )
63b89616 1102 (exit-value (apply 'call-process-region args)))
15502042 1103 (or (null exit-value) (eq 0 exit-value)
63b89616 1104 (error "Sending...failed with exit value %d" exit-value)))
00365e98
RS
1105 (or fcc-was-found
1106 (error "No recipients")))
20a8832d
RS
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))
facb137b 1123 (time (current-time))
20a8832d
RS
1124 (tembuf (generate-new-buffer " rmail output"))
1125 (case-fold-search t))
e04233aa
GM
1126 (unless (markerp header-end)
1127 (error "Value of `header-end' must be a marker"))
20a8832d
RS
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)
fc23ca96
RS
1141 ;; This initial newline is written out if the fcc file already exists.
1142 (insert "\nFrom " (user-login-name) " "
facb137b 1143 (current-time-string time) "\n")
847f73fd
RS
1144 ;; Insert the time zone before the year.
1145 (forward-char -1)
1146 (forward-word -1)
f3098917
RS
1147 (require 'mail-utils)
1148 (insert (mail-rfc822-time-zone time) " ")
847f73fd 1149 (goto-char (point-max))
20a8832d
RS
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
89901917 1163 (let* ((buffer (find-buffer-visiting (car fcc-list)))
3a3ffba4 1164 (curbuf (current-buffer))
0d15bc31
RS
1165 dont-write-the-file
1166 buffer-matches-file
3a3ffba4
RS
1167 (beg (point-min)) (end (point-max))
1168 (beg2 (save-excursion (goto-char (point-min))
1169 (forward-line 2) (point))))
20a8832d
RS
1170 (if buffer
1171 ;; File is present in a buffer => append to that buffer.
d6642dff
RS
1172 (save-excursion
1173 (set-buffer buffer)
0d15bc31
RS
1174 (setq buffer-matches-file
1175 (and (not (buffer-modified-p))
205d354f 1176 (verify-visited-file-modtime buffer)))
d6642dff
RS
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
e04233aa
GM
1190 ;; Append to an ordinary buffer as a
1191 ;; Unix mail message.
d6642dff
RS
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"
d6642dff
RS
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))
0d15bc31
RS
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.
f6f4d690
KH
1220 (unless dont-write-the-file
1221 (if (and (file-exists-p (car fcc-list))
faeb73d4
GM
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)))))
f6f4d690
KH
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)))
0d15bc31
RS
1244 (and buffer (not dont-write-the-file)
1245 (with-current-buffer buffer
1246 (set-visited-file-modtime))))
20a8832d
RS
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
20a8832d 1254 ;; put a marker at the end of the header
81f5ed32 1255 (let ((end (copy-marker (mail-header-end)))
20a8832d
RS
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
5a31c2d2 1301(defun mail-fcc (folder)
70ee42f7 1302 "Add a new FCC field, with file name completion."
5a31c2d2 1303 (interactive "FFolder carbon copy: ")
70ee42f7
JB
1304 (expand-abbrev)
1305 (or (mail-position-on-field "fcc" t) ;Put new field after exiting FCC.
1306 (mail-position-on-field "to"))
5a31c2d2 1307 (insert "\nFCC: " folder))
70ee42f7 1308
a1506d29 1309(defun mail-reply-to ()
e04233aa 1310 "Move point to end of Reply-To-field. Create a Reply-To field if none."
3a3ffba4
RS
1311 (interactive)
1312 (expand-abbrev)
1313 (mail-position-on-field "Reply-To"))
1314
3ca0be33
RS
1315(defun mail-mail-reply-to ()
1316 "Move point to end of Mail-Reply-To field.
1317Create 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.
1326Create 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
20a8832d
RS
1333(defun mail-position-on-field (field &optional soft)
1334 (let (end
1335 (case-fold-search t))
51d541d4 1336 (setq end (mail-header-end))
20a8832d
RS
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)
08564963
JB
1346 (insert field ": \n")
1347 (skip-chars-backward "\n")))
20a8832d
RS
1348 nil)))
1349
1350(defun mail-text ()
330e9e11 1351 "Move point to beginning of message text."
20a8832d 1352 (interactive)
e3b671fc 1353 (expand-abbrev)
51d541d4 1354 (goto-char (mail-text-start)))
20a8832d 1355\f
e04233aa 1356(defun mail-signature (&optional atpoint)
287ef00f 1357 "Sign letter with signature based on `mail-signature-file'.
e4f794ed 1358Prefix arg means put contents at point."
0d021ccf 1359 (interactive "P")
20a8832d 1360 (save-excursion
0d021ccf
ER
1361 (or atpoint
1362 (goto-char (point-max)))
20a8832d
RS
1363 (skip-chars-backward " \t\n")
1364 (end-of-line)
0d021ccf
ER
1365 (or atpoint
1366 (delete-region (point) (point-max)))
287ef00f
RS
1367 (if (stringp mail-signature)
1368 (insert mail-signature)
1369 (insert "\n\n-- \n")
1370 (insert-file-contents (expand-file-name mail-signature-file)))))
20a8832d
RS
1371
1372(defun mail-fill-yanked-message (&optional justifyp)
1373 "Fill the paragraphs of a message yanked into this one.
1374Numeric argument means justify as well."
1375 (interactive "P")
1376 (save-excursion
51d541d4 1377 (goto-char (mail-text-start))
20a8832d
RS
1378 (fill-individual-paragraphs (point)
1379 (point-max)
1380 justifyp
706e3d85 1381 mail-citation-prefix-regexp)))
20a8832d 1382
374bda6b 1383(defun mail-indent-citation ()
1c24b04a
RS
1384 "Modify text just inserted from a message to be cited.
1385The inserted text should be the region.
1386When this function returns, the region is again around the modified text.
1387
1388Normally, indent each nonblank line `mail-indentation-spaces' spaces.
1389However, if `mail-yank-prefix' is non-nil, insert that prefix on each line."
374bda6b 1390 (mail-yank-clear-headers (region-beginning) (region-end))
e39e74c0 1391 (if (null mail-yank-prefix)
374bda6b
RS
1392 (indent-rigidly (region-beginning) (region-end)
1393 mail-indentation-spaces)
e39e74c0 1394 (save-excursion
374bda6b
RS
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))))))
1c24b04a 1400
20a8832d
RS
1401(defun mail-yank-original (arg)
1402 "Insert the message being replied to, if any (in rmail).
26adca1b 1403Puts point after the text and mark before.
20a8832d
RS
1404Normally, indents each nonblank line ARG spaces (default 3).
1405However, if `mail-yank-prefix' is non-nil, insert that prefix on each line.
1406
1407Just \\[universal-argument] as argument means don't indent, insert no prefix,
1408and don't delete any header fields."
1409 (interactive "P")
c8553837
RS
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)
75e4db34
RS
1421 (insert-buffer original)
1422 (set-text-properties (point) (mark t) nil))
20a8832d
RS
1423 (if (consp arg)
1424 nil
1c24b04a
RS
1425 (goto-char start)
1426 (let ((mail-indentation-spaces (if arg (prefix-numeric-value arg)
ac9f0310 1427 mail-indentation-spaces))
8761c2c4
RS
1428 ;; Avoid error in Transient Mark mode
1429 ;; on account of mark's being inactive.
ac9f0310 1430 (mark-even-if-inactive t))
e04233aa
GM
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)))))
3c280f48
RS
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))))
20a8832d
RS
1453 (if (not (eolp)) (insert ?\n)))))
1454
1455(defun mail-yank-clear-headers (start end)
e39e74c0
RS
1456 (if (< end start)
1457 (let (temp)
1458 (setq temp start start end end temp)))
2b3a206d
RS
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)))))))))
e39e74c0
RS
1473
1474(defun mail-yank-region (arg)
1475 "Insert the selected region from the message being replied to.
1476Puts point after the text and mark before.
1477Normally, indents each nonblank line ARG spaces (default 3).
1478However, if `mail-yank-prefix' is non-nil, insert that prefix on each line.
1479
1480Just \\[universal-argument] as argument means don't indent, insert no prefix,
1481and don't delete any header fields."
1482 (interactive "P")
1483 (and (consp mail-reply-action)
1484 (eq (car mail-reply-action) 'insert-buffer)
75e4db34
RS
1485 (with-current-buffer (nth 1 mail-reply-action)
1486 (or (mark t)
1487 (error "No mark set: %S" (current-buffer))))
e39e74c0 1488 (let ((buffer (nth 1 mail-reply-action))
8761c2c4
RS
1489 (start (point))
1490 ;; Avoid error in Transient Mark mode
1491 ;; on account of mark's being inactive.
1492 (mark-even-if-inactive t))
e39e74c0
RS
1493 ;; Insert the citation text.
1494 (insert (with-current-buffer buffer
d6bccf09 1495 (buffer-substring-no-properties (point) (mark))))
e39e74c0
RS
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
d6bccf09
RS
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))
e39e74c0
RS
1513 (if mail-yank-hooks
1514 (run-hooks 'mail-yank-hooks)
374bda6b 1515 (mail-indent-citation))))))))
8ba7cd40
KS
1516
1517(defun mail-split-line ()
1518 "Split current line, moving portion beyond point vertically down.
1519If the current line has `mail-yank-prefix', insert it on the new line."
1520 (interactive "*")
1521 (split-line mail-yank-prefix))
1522
20a8832d 1523\f
f0aef6bf
RS
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
699adcb2
RS
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*")
20a8832d
RS
1548
1549;;;###autoload
1550(defun mail (&optional noerase to subject in-reply-to cc replybuffer actions)
e635fdf0
RS
1551 "Edit a message to be sent. Prefix arg means resume editing (don't erase).
1552When this function returns, the buffer `*mail*' is selected.
1553The value is t if the message was newly initialized; otherwise, nil.
20a8832d 1554
4ac2e032
RS
1555Optionally, the signature file `mail-signature-file' can be inserted at the
1556end; see the variable `mail-signature'.
20a8832d
RS
1557
1558\\<mail-mode-map>
1559While editing message, type \\[mail-send-and-exit] to send the message and exit.
1560
1561Various special commands starting with C-c are available in sendmail mode
1562to move to message header fields:
1563\\{mail-mode-map}
1564
1565If `mail-self-blind' is non-nil, a BCC to yourself is inserted
1566when the message is initialized.
1567
1568If `mail-default-reply-to' is non-nil, it should be an address (a string);
1569a Reply-to: field with that address is inserted.
1570
1571If `mail-archive-file-name' is non-nil, an FCC field with that file name
1572is inserted.
1573
8b262cbc
KH
1574The normal hook `mail-setup-hook' is run after the message is
1575initialized. It can add more default fields to the message.
20a8832d 1576
dfdac213
RS
1577When calling from a program, the first argument if non-nil says
1578not to erase the existing contents of the `*mail*' buffer.
1579
1580The second through fifth arguments,
1581 TO, SUBJECT, IN-REPLY-TO and CC, specify if non-nil
20a8832d
RS
1582 the initial contents of those header fields.
1583 These arguments should not have final newlines.
c8553837
RS
1584The 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.
20a8832d
RS
1588The 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")
e635fdf0
RS
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))
67a6988b 1635 (pop-to-buffer "*mail*")
eb1213c1
MR
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)))
2e78575e
RS
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))
e635fdf0 1645 (mail-mode)
3b112b5e
MB
1646 ;; Disconnect the buffer from its visited file
1647 ;; (in case the user has actually visited a file *mail*).
e4fbad4b 1648; (set-visited-file-name nil)
e635fdf0
RS
1649 (let (initialized)
1650 (and (not noerase)
5e32e119
RS
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))
6737ae74
RS
1664 (let ((inhibit-read-only t))
1665 (erase-buffer)
1666 (mail-setup to subject in-reply-to cc replybuffer actions)
1667 (setq initialized t)))
e635fdf0
RS
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))
20a8832d 1672
6cd4cfc5 1673(defun mail-recover-1 ()
918c7db7 1674 "Pop up a list of auto-saved draft messages so you can recover one of them."
6cd4cfc5
EZ
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
d41eb0aa 1685 (- (length file-name) (length (make-temp-name "")) 1))
6cd4cfc5
EZ
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 'emacs-mule-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 'emacs-mule-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 emacs-mule, 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
20a8832d 1744(defun mail-recover ()
918c7db7
EZ
1745 "Recover interrupted mail composition from auto-save files.
1746
1747If the mail buffer has a current valid auto-save file,
1748the command recovers that file. Otherwise, it displays a
1749buffer showing the existing auto-saved draft messages;
1750you can move to one of them and type C-c C-c to recover that one."
20a8832d 1751 (interactive)
6cd4cfc5
EZ
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.
eb1213c1
MR
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)))
6cd4cfc5
EZ
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"))))
8366cfb5
RS
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)))
6cd4cfc5
EZ
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 'emacs-mule-unix))
1786 (erase-buffer)
1787 (insert-file-contents file-name nil)
1788 (setq buffer-file-coding-system buffer-coding)))))
1789 (t (mail-recover-1)))))
20a8832d
RS
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")
67a6988b
RS
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))
20a8832d
RS
1800 (pop-to-buffer "*mail*"))
1801 (mail noerase to subject in-reply-to cc replybuffer sendactions))
1802
1803;;;###autoload
0cc89026
JB
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."
20a8832d 1806 (interactive "P")
67a6988b
RS
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))
20a8832d
RS
1812 (pop-to-buffer "*mail*"))
1813 (mail noerase to subject in-reply-to cc replybuffer sendactions))
1814
20a8832d 1815;;; Do not add anything but external entries on this page.
49116ac0
JB
1816
1817(provide 'sendmail)
1818
ab5796a9 1819;;; arch-tag: 48bc1025-d993-4d31-8d81-2a29491f0626
c88ab9ce 1820;;; sendmail.el ends here