Replace `string-to-int' by `string-to-number'.
[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 368 '("^\\(Subject:\\)[ \t]*\\(.+\\)?"
b795df2b
RS
369 (1 font-lock-comment-face)
370;; (2 font-lock-type-face nil t)
371 )
f1052d2e 372 ;; Use EVAL to delay in case `mail-header-separator' gets changed.
3d51068d 373 '(eval .
e69d810c 374 (let ((separator (if (zerop (length mail-header-separator))
8646fe22 375 " \\`\\' "
e69d810c
SM
376 (regexp-quote mail-header-separator))))
377 (cons (concat "^" separator "$") 'font-lock-warning-face)))
f1052d2e
SM
378 ;; Use MATCH-ANCHORED to effectively anchor the regexp left side.
379 `(,cite-chars
380 (,(concat "\\=[ \t]*"
d2f29015
RS
381 "\\(\\(\\([" cite-prefix "]+[" cite-suffix "]*\\)?"
382 "\\(" cite-chars "[ \t]*\\)\\)+\\)"
3d51068d 383 "\\(.*\\)")
f1052d2e 384 (beginning-of-line) (end-of-line)
d2f29015
RS
385 (1 font-lock-comment-delimiter-face nil t)
386 (5 font-lock-comment-face nil t)))
c034acdf 387 '("^\\(X-[A-Za-z0-9-]+\\|In-reply-to\\):.*\\(\n[ \t]+.*\\)*$"
70a302f8 388 . font-lock-string-face))))
8b1f1a0c
RS
389 "Additional expressions to highlight in Mail mode.")
390
5b6575b7 391\f
1498db18 392(defun sendmail-sync-aliases ()
74f8a333
RS
393 (when mail-personal-alias-file
394 (let ((modtime (nth 5 (file-attributes mail-personal-alias-file))))
395 (or (equal mail-alias-modtime modtime)
396 (setq mail-alias-modtime modtime
397 mail-aliases t)))))
9cc75191 398
20a8832d 399(defun mail-setup (to subject in-reply-to cc replybuffer actions)
235c2d35 400 (or mail-default-reply-to
810163a3 401 (setq mail-default-reply-to (getenv "REPLYTO")))
1498db18 402 (sendmail-sync-aliases)
5ef9d627
RS
403 (if (eq mail-aliases t)
404 (progn
405 (setq mail-aliases nil)
74f8a333
RS
406 (when mail-personal-alias-file
407 (if (file-exists-p mail-personal-alias-file)
408 (build-mail-aliases)))))
2813dcb9 409 ;; Don't leave this around from a previous message.
f8081291 410 (kill-local-variable 'buffer-file-coding-system)
9be82109
KH
411 ;; This doesn't work for enable-multibyte-characters.
412 ;; (kill-local-variable 'enable-multibyte-characters)
413 (set-buffer-multibyte default-enable-multibyte-characters)
8881c1ab
KH
414 (if current-input-method
415 (inactivate-input-method))
20a8832d 416 (setq mail-send-actions actions)
c8553837 417 (setq mail-reply-action replybuffer)
20a8832d
RS
418 (goto-char (point-min))
419 (insert "To: ")
420 (save-excursion
421 (if to
70ca75ae
RS
422 ;; Here removed code to extract names from within <...>
423 ;; on the assumption that mail-strip-quoted-names
424 ;; has been called and has done so.
425 (let ((fill-prefix "\t")
426 (address-start (point)))
20a8832d 427 (insert to "\n")
118a3c04
RS
428 (fill-region-as-paragraph address-start (point-max))
429 (goto-char (point-max))
430 (unless (bolp)
431 (newline)))
20a8832d
RS
432 (newline))
433 (if cc
70ca75ae
RS
434 (let ((fill-prefix "\t")
435 (address-start (progn (insert "CC: ") (point))))
436 (insert cc "\n")
118a3c04
RS
437 (fill-region-as-paragraph address-start (point-max))
438 (goto-char (point-max))
439 (unless (bolp)
440 (newline))))
20a8832d 441 (if in-reply-to
118a3c04 442 (let ((fill-prefix "\t")
58cabff0 443 (fill-column 78)
34046d66
RS
444 (address-start (point)))
445 (insert "In-reply-to: " in-reply-to "\n")
118a3c04
RS
446 (fill-region-as-paragraph address-start (point-max))
447 (goto-char (point-max))
448 (unless (bolp)
449 (newline))))
20a8832d
RS
450 (insert "Subject: " (or subject "") "\n")
451 (if mail-default-headers
452 (insert mail-default-headers))
453 (if mail-default-reply-to
454 (insert "Reply-to: " mail-default-reply-to "\n"))
455 (if mail-self-blind
22c03d39 456 (insert "BCC: " user-mail-address "\n"))
20a8832d
RS
457 (if mail-archive-file-name
458 (insert "FCC: " mail-archive-file-name "\n"))
6737ae74
RS
459 (put-text-property (point)
460 (progn
461 (insert mail-header-separator "\n")
462 (1- (point)))
463 'category 'mail-header-separator)
874bdb5a
RS
464 ;; Insert the signature. But remember the beginning of the message.
465 (if to (setq to (point)))
547075d4 466 (cond ((eq mail-signature t)
4ac2e032 467 (if (file-exists-p mail-signature-file)
a5282b8f
RS
468 (progn
469 (insert "\n\n-- \n")
4ac2e032 470 (insert-file-contents mail-signature-file))))
f13c1dae
RS
471 ((stringp mail-signature)
472 (insert mail-signature))
473 (t
474 (eval mail-signature)))
20a8832d
RS
475 (goto-char (point-max))
476 (or (bolp) (newline)))
874bdb5a 477 (if to (goto-char to))
20a8832d
RS
478 (or to subject in-reply-to
479 (set-buffer-modified-p nil))
480 (run-hooks 'mail-setup-hook))
5b6575b7 481\f
be0b8d69
DL
482(defcustom mail-mode-hook nil
483 "Hook run by Mail mode."
484 :group 'sendmail
485 :type 'hook
486 :options '(footnote-mode))
487
567bb3dd 488(defvar mail-mode-abbrev-table text-mode-abbrev-table)
20a8832d 489;;;###autoload
f81b71c7 490(define-derived-mode mail-mode text-mode "Mail"
20a8832d
RS
491 "Major mode for editing mail to be sent.
492Like Text Mode but with these additional commands:
d3db616e
KH
493\\[mail-send] mail-send (send the message) \\[mail-send-and-exit] mail-send-and-exit
494Here are commands that move to a header field (and create it if there isn't):
495 \\[mail-to] move to To: \\[mail-subject] move to Subject:
496 \\[mail-cc] move to CC: \\[mail-bcc] move to BCC:
0678d779 497 \\[mail-fcc] move to FCC: \\[mail-reply-to] move to Reply-To:
3ca0be33
RS
498 \\[mail-mail-reply-to] move to Mail-Reply-To:
499 \\[mail-mail-followup-to] move to Mail-Followup-To:
d3db616e
KH
500\\[mail-text] mail-text (move to beginning of message text).
501\\[mail-signature] mail-signature (insert `mail-signature-file' file).
502\\[mail-yank-original] mail-yank-original (insert current message, in Rmail).
503\\[mail-fill-yanked-message] mail-fill-yanked-message (fill what was yanked).
4deb3ba9
EZ
504\\[mail-sent-via] mail-sent-via (add a Sent-via field for each To or CC).
505Turning on Mail mode runs the normal hooks `text-mode-hook' and
506`mail-mode-hook' (in that order)."
c8553837 507 (make-local-variable 'mail-reply-action)
20a8832d 508 (make-local-variable 'mail-send-actions)
20a8832d 509 (setq buffer-offer-save t)
2667df42 510 (make-local-variable 'font-lock-defaults)
117e4b0c 511 (setq font-lock-defaults '(mail-font-lock-keywords t t))
20a8832d 512 (make-local-variable 'paragraph-separate)
5b6575b7
RS
513 (make-local-variable 'normal-auto-fill-function)
514 (setq normal-auto-fill-function 'mail-mode-auto-fill)
b5682230 515 (make-local-variable 'fill-paragraph-function)
c8553837 516 (setq fill-paragraph-function 'mail-mode-fill-paragraph)
eba52a1a
MB
517 ;; Allow using comment commands to add/remove quoting (this only does
518 ;; anything if mail-yank-prefix is set to a non-nil value).
519 (set (make-local-variable 'comment-start) mail-yank-prefix)
fc2a68b5
SM
520 (if mail-yank-prefix
521 (set (make-local-variable 'comment-start-skip)
522 (concat "^" (regexp-quote mail-yank-prefix) "[ \t]*")))
1b8def58
RS
523 (make-local-variable 'adaptive-fill-regexp)
524 (setq adaptive-fill-regexp
567a1c29 525 (concat "[ \t]*[-[:alnum:]]+>+[ \t]*\\|"
f81b71c7 526 adaptive-fill-regexp))
d78f466f 527 (make-local-variable 'adaptive-fill-first-line-regexp)
f03ca5f5 528 (setq adaptive-fill-first-line-regexp
40035487 529 (concat "[ \t]*[-[:alnum:]]*>+[ \t]*\\|"
f81b71c7 530 adaptive-fill-first-line-regexp))
df0d89b1
RS
531 ;; `-- ' precedes the signature. `-----' appears at the start of the
532 ;; lines that delimit forwarded messages.
533 ;; Lines containing just >= 3 dashes, perhaps after whitespace,
534 ;; are also sometimes used and should be separators.
f3e95b0d 535 (setq paragraph-separate (concat (regexp-quote mail-header-separator)
75e4db34 536 "$\\|\t*\\([-|#;>* ]\\|(?[0-9]+[.)]\\)+$"
0eafc06c 537 "\\|[ \t]*[[:alnum:]]*>+[ \t]*$\\|[ \t]*$\\|"
f3e95b0d
SM
538 "--\\( \\|-+\\)$\\|"
539 page-delimiter)))
5b6575b7 540
51d541d4
RS
541
542(defun mail-header-end ()
543 "Return the buffer location of the end of headers, as a number."
66191b7e
RS
544 (save-restriction
545 (widen)
546 (save-excursion
547 (rfc822-goto-eoh)
548 (point))))
51d541d4
RS
549
550(defun mail-text-start ()
551 "Return the buffer location of the start of text, as a number."
66191b7e
RS
552 (save-restriction
553 (widen)
554 (save-excursion
555 (rfc822-goto-eoh)
556 (forward-line 1)
557 (point))))
51d541d4
RS
558
559(defun mail-sendmail-delimit-header ()
560 "Set up whatever header delimiter convention sendmail will use.
561Concretely: replace the first blank line in the header with the separator."
562 (rfc822-goto-eoh)
563 (insert mail-header-separator)
564 (point))
565
566(defun mail-sendmail-undelimit-header ()
567 "Remove header separator to put the message in correct form for sendmail.
568Leave point at the start of the delimiter line."
569 (rfc822-goto-eoh)
570 (delete-region (point) (progn (end-of-line) (point))))
571
5b6575b7
RS
572(defun mail-mode-auto-fill ()
573 "Carry out Auto Fill for Mail mode.
574If within the headers, this makes the new lines into continuation lines."
51d541d4 575 (if (< (point) (mail-header-end))
5b6575b7
RS
576 (let ((old-line-start (save-excursion (beginning-of-line) (point))))
577 (if (do-auto-fill)
578 (save-excursion
579 (beginning-of-line)
580 (while (not (eq (point) old-line-start))
28afe199
RS
581 ;; Use insert-before-markers in case we're inserting
582 ;; before the saved value of point (which is common).
583 (insert-before-markers " ")
5b6575b7
RS
584 (forward-line -1))
585 t)))
586 (do-auto-fill)))
c8553837
RS
587
588(defun mail-mode-fill-paragraph (arg)
589 ;; Do something special only if within the headers.
51d541d4 590 (if (< (point) (mail-header-end))
a1506d29 591 (let (beg end fieldname)
8fcbbf7d
GM
592 (when (prog1 (re-search-backward "^[-a-zA-Z]+:" nil 'yes)
593 (setq beg (point)))
c8553837 594 (setq fieldname
8fcbbf7d 595 (downcase (buffer-substring beg (1- (match-end 0))))))
c8553837
RS
596 (forward-line 1)
597 ;; Find continuation lines and get rid of their continuation markers.
598 (while (looking-at "[ \t]")
599 (delete-horizontal-space)
600 (forward-line 1))
601 (setq end (point-marker))
602 (goto-char beg)
603 ;; If this field contains addresses,
604 ;; make sure we can fill after each address.
605 (if (member fieldname
606 '("to" "cc" "bcc" "from" "reply-to"
3ca0be33 607 "mail-reply-to" "mail-followup-to"
c8553837
RS
608 "resent-to" "resent-cc" "resent-bcc"
609 "resent-from" "resent-reply-to"))
610 (while (search-forward "," end t)
611 (or (looking-at "[ \t]")
612 (insert " "))))
613 (fill-region-as-paragraph beg end)
614 ;; Mark all lines except the first as continuations.
615 (goto-char beg)
616 (forward-line 1)
617 (while (< (point) end)
618 (insert " ")
619 (forward-line 1))
620 (move-marker end nil)
621 t)))
299fdd8d
RS
622\f
623;;; Set up keymap.
20a8832d
RS
624
625(if mail-mode-map
626 nil
f81b71c7 627 (setq mail-mode-map (make-sparse-keymap))
c21b5781 628 (define-key mail-mode-map "\M-\t" 'mail-complete)
20a8832d
RS
629 (define-key mail-mode-map "\C-c?" 'describe-mode)
630 (define-key mail-mode-map "\C-c\C-f\C-t" 'mail-to)
631 (define-key mail-mode-map "\C-c\C-f\C-b" 'mail-bcc)
70ee42f7 632 (define-key mail-mode-map "\C-c\C-f\C-f" 'mail-fcc)
20a8832d
RS
633 (define-key mail-mode-map "\C-c\C-f\C-c" 'mail-cc)
634 (define-key mail-mode-map "\C-c\C-f\C-s" 'mail-subject)
3a3ffba4 635 (define-key mail-mode-map "\C-c\C-f\C-r" 'mail-reply-to)
3ca0be33
RS
636 (define-key mail-mode-map "\C-c\C-f\C-a" 'mail-mail-reply-to) ; author
637 (define-key mail-mode-map "\C-c\C-f\C-l" 'mail-mail-followup-to) ; list
20a8832d
RS
638 (define-key mail-mode-map "\C-c\C-t" 'mail-text)
639 (define-key mail-mode-map "\C-c\C-y" 'mail-yank-original)
e39e74c0 640 (define-key mail-mode-map "\C-c\C-r" 'mail-yank-region)
8ba7cd40 641 (define-key mail-mode-map [remap split-line] 'mail-split-line)
20a8832d
RS
642 (define-key mail-mode-map "\C-c\C-q" 'mail-fill-yanked-message)
643 (define-key mail-mode-map "\C-c\C-w" 'mail-signature)
644 (define-key mail-mode-map "\C-c\C-v" 'mail-sent-via)
645 (define-key mail-mode-map "\C-c\C-c" 'mail-send-and-exit)
f0aef6bf
RS
646 (define-key mail-mode-map "\C-c\C-s" 'mail-send)
647 (define-key mail-mode-map "\C-c\C-i" 'mail-attach-file))
299fdd8d
RS
648
649(define-key mail-mode-map [menu-bar mail]
650 (cons "Mail" (make-sparse-keymap "Mail")))
651
652(define-key mail-mode-map [menu-bar mail fill]
653 '("Fill Citation" . mail-fill-yanked-message))
654
655(define-key mail-mode-map [menu-bar mail yank]
656 '("Cite Original" . mail-yank-original))
657
658(define-key mail-mode-map [menu-bar mail signature]
659 '("Insert Signature" . mail-signature))
660
1abf89b8
SM
661(define-key mail-mode-map [menu-bar mail mail-sep]
662 '("--"))
663
1b6bb250
RS
664(define-key mail-mode-map [menu-bar mail cancel]
665 '("Cancel" . mail-dont-send))
666
299fdd8d
RS
667(define-key mail-mode-map [menu-bar mail send-stay]
668 '("Send, Keep Editing" . mail-send))
669
670(define-key mail-mode-map [menu-bar mail send]
671 '("Send Message" . mail-send-and-exit))
672
673(define-key mail-mode-map [menu-bar headers]
fa8afe42 674 (cons "Headers" (make-sparse-keymap "Move to Header")))
299fdd8d 675
1abf89b8
SM
676(define-key mail-mode-map [menu-bar headers text]
677 '("Text" . mail-text))
678
679(define-key mail-mode-map [menu-bar headers expand-aliases]
680 '("Expand Aliases" . expand-mail-aliases))
3a3ffba4 681
299fdd8d
RS
682(define-key mail-mode-map [menu-bar headers sent-via]
683 '("Sent Via" . mail-sent-via))
684
3ca0be33
RS
685(define-key mail-mode-map [menu-bar headers mail-reply-to]
686 '("Mail Reply To" . mail-mail-reply-to))
687
688(define-key mail-mode-map [menu-bar headers mail-followup-to]
689 '("Mail Followup To" . mail-mail-followup-to))
690
1abf89b8
SM
691(define-key mail-mode-map [menu-bar headers reply-to]
692 '("Reply-To" . mail-reply-to))
299fdd8d
RS
693
694(define-key mail-mode-map [menu-bar headers bcc]
695 '("Bcc" . mail-bcc))
696
697(define-key mail-mode-map [menu-bar headers fcc]
698 '("Fcc" . mail-fcc))
699
700(define-key mail-mode-map [menu-bar headers cc]
701 '("Cc" . mail-cc))
702
703(define-key mail-mode-map [menu-bar headers subject]
704 '("Subject" . mail-subject))
705
706(define-key mail-mode-map [menu-bar headers to]
707 '("To" . mail-to))
20a8832d 708\f
5b6575b7
RS
709;; User-level commands for sending.
710
2c1e2995 711(defun mail-send-and-exit (&optional arg)
33b4a308 712 "Send message like `mail-send', then, if no errors, exit from mail buffer.
20a8832d
RS
713Prefix arg means don't delete this window."
714 (interactive "P")
715 (mail-send)
1b6bb250
RS
716 (mail-bury arg))
717
8451b5e9 718(defun mail-dont-send (&optional arg)
1b6bb250
RS
719 "Don't send the message you have been editing.
720Prefix arg means don't delete this window."
721 (interactive "P")
722 (mail-bury arg))
723
8451b5e9 724(defun mail-bury (&optional arg)
1b6bb250 725 "Bury this mail buffer."
e49ad9a5
RS
726 (let ((newbuf (other-buffer (current-buffer))))
727 (bury-buffer (current-buffer))
54feedaa 728 (if (and (or (window-dedicated-p (frame-selected-window))
b03a1621 729 (cdr (assq 'mail-dedicated-frame (frame-parameters))))
9320d421 730 (not (null (delq (selected-frame) (visible-frame-list)))))
6f60853a
EZ
731 (progn
732 (if (display-multi-frame-p)
733 (delete-frame (selected-frame))
734 ;; The previous frame is where normally they have the
735 ;; RMAIL buffer displayed.
736 (other-frame -1)))
fdb90184
RS
737 (let (rmail-flag summary-buffer)
738 (and (not arg)
739 (not (one-window-p))
740 (save-excursion
741 (set-buffer (window-buffer (next-window (selected-window) 'not)))
742 (setq rmail-flag (eq major-mode 'rmail-mode))
743 (setq summary-buffer
1f7850dd
RS
744 (and mail-bury-selects-summary
745 (boundp 'rmail-summary-buffer)
6cfc977b 746 rmail-summary-buffer
fdb90184
RS
747 (buffer-name rmail-summary-buffer)
748 (not (get-buffer-window rmail-summary-buffer))
749 rmail-summary-buffer))))
750 (if rmail-flag
751 ;; If the Rmail buffer has a summary, show that.
752 (if summary-buffer (switch-to-buffer summary-buffer)
753 (delete-window))
754 (switch-to-buffer newbuf))))))
20a8832d 755
be0b8d69
DL
756(defcustom mail-send-hook nil
757 "Hook run just before sending mail with `mail-send'."
758 :type 'hook
759 :options '(flyspell-mode-off)
760 :group 'sendmail)
761
3ca0be33
RS
762;;;###autoload
763(defcustom mail-mailing-lists nil "\
764*List of mailing list addresses the user is subscribed to.
765
766The variable is used to trigger insertion of the \"Mail-Followup-To\"
767header when sending a message to a mailing list."
768 :type '(repeat string)
769 :group 'sendmail)
770
771
20a8832d
RS
772(defun mail-send ()
773 "Send the message in the current buffer.
774If `mail-interactive' is non-nil, wait for success indication
775or error messages, and inform user.
776Otherwise any failure is reported in a message back to
777the user from the mailer."
778 (interactive)
e11094e6
RS
779 (if (if buffer-file-name
780 (y-or-n-p "Send buffer contents as mail message? ")
781 (or (buffer-modified-p)
782 (y-or-n-p "Message already sent; resend? ")))
34153d76 783 (let ((inhibit-read-only t)
3ca0be33
RS
784 (opoint (point))
785 (ml (when mail-mailing-lists
786 ;; The surrounding regexp assumes the use of
787 ;; `mail-strip-quoted-names' on addresses before matching
788 ;; Cannot deal with full RFC 822 freedom, but that is
789 ;; unlikely to be problematic.
790 (concat "\\(?:[[:space:];,]\\|\\`\\)"
791 (regexp-opt mail-mailing-lists t)
792 "\\(?:[[:space:];,]\\|\\'\\)"))))
793 ;; If there are mailing lists defined
794 (when ml
795 (save-excursion
796 (let* ((to (mail-fetch-field "to" nil t))
797 (cc (mail-fetch-field "cc" nil t))
798 (new-header-values ; To: and Cc:
799 (mail-strip-quoted-names
800 (concat to (when cc (concat ", " cc))))))
801 ;; If message goes to known mailing list ...
802 (when (string-match ml new-header-values)
803 ;; Add Mail-Followup-To if none yet
804 (unless (mail-fetch-field "mail-followup-to")
805 (goto-char (mail-header-end))
806 (insert "Mail-Followup-To: "
807 (let ((l))
808 (mapc
809 ;; remove duplicates
810 '(lambda (e)
811 (unless (member e l)
812 (push e l)))
813 (split-string new-header-values ", +" t))
814 (mapconcat 'identity l ", "))
815 "\n"))
816 ;; Add Mail-Reply-To if none yet
817 (unless (mail-fetch-field "mail-reply-to")
818 (goto-char (mail-header-end))
819 (insert "Mail-Reply-To: "
820 (or (mail-fetch-field "reply-to")
821 user-mail-address)
822 "\n"))))))
42d305db 823 (unless (memq mail-send-nonascii '(t mime))
34153d76
RS
824 (goto-char (point-min))
825 (skip-chars-forward "\0-\177")
826 (or (= (point) (point-max))
827 (if (eq mail-send-nonascii 'query)
828 (or (y-or-n-p "Message contains non-ASCII characters; send anyway? ")
829 (error "Aborted"))
830 (error "Message contains non-ASCII characters"))))
413d1aa2
RS
831 ;; Complain about any invalid line.
832 (goto-char (point-min))
51d541d4 833 (while (< (point) (mail-header-end))
413d1aa2
RS
834 (unless (looking-at "[ \t]\\|.*:\\|$")
835 (push-mark opoint)
836 (error "Invalid header line (maybe a continuation line lacks initial whitespace)"))
837 (forward-line 1))
34153d76 838 (goto-char opoint)
20a8832d 839 (run-hooks 'mail-send-hook)
da5667c6 840 (message "Sending...")
20a8832d
RS
841 (funcall send-mail-function)
842 ;; Now perform actions on successful sending.
843 (while mail-send-actions
844 (condition-case nil
d2561f34
RS
845 (apply (car (car mail-send-actions))
846 (cdr (car mail-send-actions)))
20a8832d
RS
847 (error))
848 (setq mail-send-actions (cdr mail-send-actions)))
d2561f34 849 (message "Sending...done")
4e0c8650 850 ;; If buffer has no file, mark it as unmodified and delete auto-save.
d2561f34
RS
851 (if (not buffer-file-name)
852 (progn
853 (set-buffer-modified-p nil)
854 (delete-auto-save-file-if-necessary t))))))
56e5818f
SJ
855
856(defun mail-envelope-from ()
857 "Return the envelope mail address to use when sending mail.
858This function uses `mail-envelope-from'."
859 (if (eq mail-envelope-from 'header)
860 (nth 1 (mail-extract-address-components
861 (mail-fetch-field "From")))
862 mail-envelope-from))
5b6575b7
RS
863\f
864;; This does the real work of sending a message via sendmail.
865;; It is called via the variable send-mail-function.
20a8832d 866
ae54d287
KH
867;;;###autoload
868(defvar sendmail-coding-system nil
da241b5c 869 "*Coding system for encoding the outgoing mail.
265f03bc 870This has higher priority than `default-buffer-file-coding-system'
da241b5c
KH
871and `default-sendmail-coding-system',
872but lower priority than the local value of `buffer-file-coding-system'.
0b64a3da 873See also the function `select-message-coding-system'.")
da241b5c
KH
874
875;;;###autoload
876(defvar default-sendmail-coding-system 'iso-latin-1
265f03bc 877 "Default coding system for encoding the outgoing mail.
da241b5c
KH
878This variable is used only when `sendmail-coding-system' is nil.
879
880This variable is set/changed by the command set-language-environment.
881User should not set this variable manually,
882instead use sendmail-coding-system to get a constant encoding
883of outgoing mails regardless of the current language environment.
0b64a3da 884See also the function `select-message-coding-system'.")
ee0a4f29 885
20a8832d 886(defun sendmail-send-it ()
67ad9438
DL
887 "Send the current mail buffer using the Sendmail package.
888This is a suitable value for `send-mail-function'. It sends using the
889external program defined by `sendmail-program'."
8e183d5b 890 (require 'mail-utils)
20a8832d
RS
891 (let ((errbuf (if mail-interactive
892 (generate-new-buffer " sendmail errors")
538b9847 893 0))
20a8832d 894 (tembuf (generate-new-buffer " sendmail temp"))
c86237b2 895 (multibyte enable-multibyte-characters)
20a8832d 896 (case-fold-search nil)
a80d7290 897 (selected-coding (select-message-coding-system))
3ca0be33 898 resend-to-addresses
20a8832d 899 delimline
00365e98 900 fcc-was-found
3ff0518e
GM
901 (mailbuf (current-buffer))
902 (program (if (boundp 'sendmail-program)
903 sendmail-program
31bc27f6 904 "/usr/lib/sendmail"))
00417fac
RS
905 ;; Examine these variables now, so that
906 ;; local binding in the mail buffer will take effect.
907 (envelope-from
908 (and mail-specify-envelope-from
56e5818f 909 (or (mail-envelope-from) user-mail-address))))
20a8832d
RS
910 (unwind-protect
911 (save-excursion
912 (set-buffer tembuf)
913 (erase-buffer)
c86237b2
RS
914 (unless multibyte
915 (set-buffer-multibyte nil))
20a8832d
RS
916 (insert-buffer-substring mailbuf)
917 (goto-char (point-max))
918 ;; require one newline at the end.
919 (or (= (preceding-char) ?\n)
920 (insert ?\n))
921 ;; Change header-delimiter to be what sendmail expects.
51d541d4
RS
922 (goto-char (mail-header-end))
923 (delete-region (point) (progn (end-of-line) (point)))
20a8832d 924 (setq delimline (point-marker))
1498db18 925 (sendmail-sync-aliases)
5ef9d627
RS
926 (if mail-aliases
927 (expand-mail-aliases (point-min) delimline))
20a8832d 928 (goto-char (point-min))
413d1aa2 929 ;; Ignore any blank lines in the header
20a8832d
RS
930 (while (and (re-search-forward "\n\n\n*" delimline t)
931 (< (point) delimline))
932 (replace-match "\n"))
413d1aa2 933 (goto-char (point-min))
3ca0be33
RS
934 ;; Look for Resent- headers. They require sending
935 ;; the message specially.
20a8832d 936 (let ((case-fold-search t))
3ca0be33
RS
937 (goto-char (point-min))
938 (while (re-search-forward "^Resent-\\(to\\|cc\\|bcc\\):" delimline t)
939 ;; Put a list of such addresses in resend-to-addresses.
940 (setq resend-to-addresses
941 (save-restriction
942 (narrow-to-region (point)
943 (save-excursion
944 (forward-line 1)
945 (while (looking-at "^[ \t]")
946 (forward-line 1))
947 (point)))
948 (append (mail-parse-comma-list)
949 resend-to-addresses)))
950 ;; Delete Resent-BCC ourselves
951 (if (save-excursion (beginning-of-line)
952 (looking-at "resent-bcc"))
953 (delete-region (save-excursion (beginning-of-line) (point))
954 (save-excursion (end-of-line) (1+ (point))))))
955;;; Apparently this causes a duplicate Sender.
956;;; ;; If the From is different than current user, insert Sender.
957;;; (goto-char (point-min))
958;;; (and (re-search-forward "^From:" delimline t)
959;;; (progn
960;;; (require 'mail-utils)
961;;; (not (string-equal
962;;; (mail-strip-quoted-names
963;;; (save-restriction
964;;; (narrow-to-region (point-min) delimline)
965;;; (mail-fetch-field "From")))
966;;; (user-login-name))))
967;;; (progn
968;;; (forward-line 1)
969;;; (insert "Sender: " (user-login-name) "\n")))
20a8832d
RS
970 ;; Don't send out a blank subject line
971 (goto-char (point-min))
66961910 972 (if (re-search-forward "^Subject:\\([ \t]*\n\\)+\\b" delimline t)
54115447
RS
973 (replace-match "")
974 ;; This one matches a Subject just before the header delimiter.
975 (if (and (re-search-forward "^Subject:\\([ \t]*\n\\)+" delimline t)
976 (= (match-end 0) delimline))
977 (replace-match "")))
ea9ceb34
KH
978 ;; Put the "From:" field in unless for some odd reason
979 ;; they put one in themselves.
980 (goto-char (point-min))
981 (if (not (re-search-forward "^From:" delimline t))
cbc9f037 982 (let* ((login user-mail-address)
0d15bc31
RS
983 (fullname (user-full-name))
984 (quote-fullname nil))
67ad9438
DL
985 (if (string-match "[^\0-\177]" fullname)
986 (setq fullname (rfc2047-encode-string fullname)
0d15bc31 987 quote-fullname t))
ea9ceb34 988 (cond ((eq mail-from-style 'angles)
7f1d5de2
RS
989 (insert "From: " fullname)
990 (let ((fullname-start (+ (point-min) 6))
991 (fullname-end (point-marker)))
992 (goto-char fullname-start)
993 ;; Look for a character that cannot appear unquoted
994 ;; according to RFC 822.
0d15bc31
RS
995 (if (or (re-search-forward "[^- !#-'*+/-9=?A-Z^-~]"
996 fullname-end 1)
997 quote-fullname)
7f1d5de2
RS
998 (progn
999 ;; Quote fullname, escaping specials.
1000 (goto-char fullname-start)
1001 (insert "\"")
1002 (while (re-search-forward "[\"\\]"
1003 fullname-end 1)
1004 (replace-match "\\\\\\&" t))
1005 (insert "\""))))
1006 (insert " <" login ">\n"))
ea9ceb34 1007 ((eq mail-from-style 'parens)
7dad528a
RS
1008 (insert "From: " login " (")
1009 (let ((fullname-start (point)))
0d15bc31
RS
1010 (if quote-fullname
1011 (insert "\""))
7dad528a 1012 (insert fullname)
0d15bc31
RS
1013 (if quote-fullname
1014 (insert "\""))
7dad528a
RS
1015 (let ((fullname-end (point-marker)))
1016 (goto-char fullname-start)
1017 ;; RFC 822 says \ and nonmatching parentheses
1018 ;; must be escaped in comments.
1019 ;; Escape every instance of ()\ ...
1020 (while (re-search-forward "[()\\]" fullname-end 1)
1021 (replace-match "\\\\\\&" t))
1022 ;; ... then undo escaping of matching parentheses,
1023 ;; including matching nested parentheses.
1024 (goto-char fullname-start)
a1506d29 1025 (while (re-search-forward
7dad528a
RS
1026 "\\(\\=\\|[^\\]\\(\\\\\\\\\\)*\\)\\\\(\\(\\([^\\]\\|\\\\\\\\\\)*\\)\\\\)"
1027 fullname-end 1)
1028 (replace-match "\\1(\\3)" t)
1029 (goto-char fullname-start))))
7f1d5de2 1030 (insert ")\n"))
ea9ceb34 1031 ((null mail-from-style)
55c3534b
RS
1032 (insert "From: " login "\n"))
1033 ((eq mail-from-style 'system-default)
1034 nil)
e186b069 1035 (t (error "Invalid value for `mail-from-style'")))))
baaa1f19 1036 ;; Possibly add a MIME header for the current coding system
5ddcadb8 1037 (let (charset)
baaa1f19
GM
1038 (goto-char (point-min))
1039 (and (eq mail-send-nonascii 'mime)
1040 (not (re-search-forward "^MIME-version:" delimline t))
1041 (progn (skip-chars-forward "\0-\177")
1042 (/= (point) (point-max)))
a80d7290 1043 selected-coding
baaa1f19 1044 (setq charset
5ddcadb8 1045 (coding-system-get selected-coding 'mime-charset))
baaa1f19
GM
1046 (goto-char delimline)
1047 (insert "MIME-version: 1.0\n"
1048 "Content-type: text/plain; charset="
1049 (symbol-name charset) "\n"
1050 "Content-Transfer-Encoding: 8bit\n")))
7c203051
RS
1051 ;; Insert an extra newline if we need it to work around
1052 ;; Sun's bug that swallows newlines.
1053 (goto-char (1+ delimline))
1054 (if (eval mail-mailer-swallows-blank-line)
1055 (newline))
d6e000df
RS
1056 ;; Find and handle any FCC fields.
1057 (goto-char (point-min))
1058 (if (re-search-forward "^FCC:" delimline t)
00365e98
RS
1059 (progn
1060 (setq fcc-was-found t)
1061 (mail-do-fcc delimline)))
20a8832d
RS
1062 (if mail-interactive
1063 (save-excursion
1064 (set-buffer errbuf)
1065 (erase-buffer))))
00365e98
RS
1066 (goto-char (point-min))
1067 (if (let ((case-fold-search t))
3ca0be33
RS
1068 (or resend-to-addresses
1069 (re-search-forward "^To:\\|^cc:\\|^bcc:"
1070 delimline t)))
63b89616 1071 (let* ((default-directory "/")
a80d7290 1072 (coding-system-for-write selected-coding)
a1506d29 1073 (args
63b89616 1074 (append (list (point-min) (point-max)
3ff0518e 1075 program
63b89616 1076 nil errbuf nil "-oi")
00417fac
RS
1077 (and envelope-from
1078 (list "-f" envelope-from))
63b89616
KH
1079;;; ;; Don't say "from root" if running under su.
1080;;; (and (equal (user-real-login-name) "root")
1081;;; (list "-f" (user-login-name)))
1082 (and mail-alias-file
1083 (list (concat "-oA" mail-alias-file)))
1084 (if mail-interactive
1085 ;; These mean "report errors to terminal"
1086 ;; and "deliver interactively"
1087 '("-oep" "-odi")
1088 ;; These mean "report errors by mail"
1089 ;; and "deliver in background".
1090 '("-oem" "-odb"))
3ca0be33
RS
1091 ;; Get the addresses from the message
1092 ;; unless this is a resend.
1093 ;; We must not do that for a resend
1094 ;; because we would find the original addresses.
1095 ;; For a resend, include the specific addresses.
1096 (or resend-to-addresses
180d5509 1097 '("-t")
3ca0be33 1098 )
0b0b4ce7
RS
1099 (if mail-use-dsn
1100 (list "-N" (mapconcat 'symbol-name
1101 mail-use-dsn ",")))
180d5509
GM
1102 )
1103 )
63b89616 1104 (exit-value (apply 'call-process-region args)))
15502042 1105 (or (null exit-value) (eq 0 exit-value)
63b89616 1106 (error "Sending...failed with exit value %d" exit-value)))
00365e98
RS
1107 (or fcc-was-found
1108 (error "No recipients")))
20a8832d
RS
1109 (if mail-interactive
1110 (save-excursion
1111 (set-buffer errbuf)
1112 (goto-char (point-min))
1113 (while (re-search-forward "\n\n* *" nil t)
1114 (replace-match "; "))
1115 (if (not (zerop (buffer-size)))
1116 (error "Sending...failed to %s"
1117 (buffer-substring (point-min) (point-max)))))))
1118 (kill-buffer tembuf)
1119 (if (bufferp errbuf)
1120 (kill-buffer errbuf)))))
1121
1122(defun mail-do-fcc (header-end)
1123 (let (fcc-list
1124 (rmailbuf (current-buffer))
facb137b 1125 (time (current-time))
20a8832d
RS
1126 (tembuf (generate-new-buffer " rmail output"))
1127 (case-fold-search t))
e04233aa
GM
1128 (unless (markerp header-end)
1129 (error "Value of `header-end' must be a marker"))
20a8832d
RS
1130 (save-excursion
1131 (goto-char (point-min))
1132 (while (re-search-forward "^FCC:[ \t]*" header-end t)
1133 (setq fcc-list (cons (buffer-substring (point)
1134 (progn
1135 (end-of-line)
1136 (skip-chars-backward " \t")
1137 (point)))
1138 fcc-list))
1139 (delete-region (match-beginning 0)
1140 (progn (forward-line 1) (point))))
1141 (set-buffer tembuf)
1142 (erase-buffer)
fc23ca96
RS
1143 ;; This initial newline is written out if the fcc file already exists.
1144 (insert "\nFrom " (user-login-name) " "
facb137b 1145 (current-time-string time) "\n")
847f73fd
RS
1146 ;; Insert the time zone before the year.
1147 (forward-char -1)
1148 (forward-word -1)
f3098917
RS
1149 (require 'mail-utils)
1150 (insert (mail-rfc822-time-zone time) " ")
847f73fd 1151 (goto-char (point-max))
20a8832d
RS
1152 (insert-buffer-substring rmailbuf)
1153 ;; Make sure messages are separated.
1154 (goto-char (point-max))
1155 (insert ?\n)
1156 (goto-char 2)
1157 ;; ``Quote'' "^From " as ">From "
1158 ;; (note that this isn't really quoting, as there is no requirement
1159 ;; that "^[>]+From " be quoted in the same transparent way.)
1160 (let ((case-fold-search nil))
1161 (while (search-forward "\nFrom " nil t)
1162 (forward-char -5)
1163 (insert ?>)))
1164 (while fcc-list
89901917 1165 (let* ((buffer (find-buffer-visiting (car fcc-list)))
3a3ffba4 1166 (curbuf (current-buffer))
0d15bc31
RS
1167 dont-write-the-file
1168 buffer-matches-file
3a3ffba4
RS
1169 (beg (point-min)) (end (point-max))
1170 (beg2 (save-excursion (goto-char (point-min))
1171 (forward-line 2) (point))))
20a8832d
RS
1172 (if buffer
1173 ;; File is present in a buffer => append to that buffer.
d6642dff
RS
1174 (save-excursion
1175 (set-buffer buffer)
0d15bc31
RS
1176 (setq buffer-matches-file
1177 (and (not (buffer-modified-p))
205d354f 1178 (verify-visited-file-modtime buffer)))
d6642dff
RS
1179 ;; Keep the end of the accessible portion at the same place
1180 ;; unless it is the end of the buffer.
1181 (let ((max (if (/= (1+ (buffer-size)) (point-max))
1182 (point-max))))
1183 (unwind-protect
1184 ;; Code below lifted from rmailout.el
1185 ;; function rmail-output-to-rmail-file:
1186 (let ((buffer-read-only nil)
1187 (msg (and (boundp 'rmail-current-message)
1188 rmail-current-message)))
1189 ;; If MSG is non-nil, buffer is in RMAIL mode.
1190 (if msg
1191 (progn
e04233aa
GM
1192 ;; Append to an ordinary buffer as a
1193 ;; Unix mail message.
d6642dff
RS
1194 (rmail-maybe-set-message-counters)
1195 (widen)
1196 (narrow-to-region (point-max) (point-max))
1197 (insert "\C-l\n0, unseen,,\n*** EOOH ***\n"
d6642dff
RS
1198 "Date: " (mail-rfc822-date) "\n")
1199 (insert-buffer-substring curbuf beg2 end)
1200 (insert "\n\C-_")
1201 (goto-char (point-min))
1202 (widen)
1203 (search-backward "\n\^_")
1204 (narrow-to-region (point) (point-max))
1205 (rmail-count-new-messages t)
1206 (rmail-show-message msg)
1207 (setq max nil))
1208 ;; Output file not in rmail mode
1209 ;; => just insert at the end.
1210 (narrow-to-region (point-min) (1+ (buffer-size)))
1211 (goto-char (point-max))
0d15bc31
RS
1212 (insert-buffer-substring curbuf beg end))
1213 (or buffer-matches-file
1214 (progn
1215 (if (y-or-n-p (format "Save file %s? "
1216 (car fcc-list)))
1217 (save-buffer))
1218 (setq dont-write-the-file t))))
1219 (if max (narrow-to-region (point-min) max))))))
1220 ;; Append to the file directly,
1221 ;; unless we've already taken care of it.
f6f4d690
KH
1222 (unless dont-write-the-file
1223 (if (and (file-exists-p (car fcc-list))
faeb73d4
GM
1224 ;; Check that the file isn't empty. We don't
1225 ;; want to insert a newline at the start of an
1226 ;; empty file.
1227 (not (zerop (nth 7 (file-attributes (car fcc-list)))))
f6f4d690
KH
1228 (mail-file-babyl-p (car fcc-list)))
1229 ;; If the file is a Babyl file,
1230 ;; convert the message to Babyl format.
1231 (let ((coding-system-for-write
1232 (or rmail-file-coding-system
1233 'emacs-mule)))
1234 (save-excursion
1235 (set-buffer (get-buffer-create " mail-temp"))
1236 (setq buffer-read-only nil)
1237 (erase-buffer)
1238 (insert "\C-l\n0, unseen,,\n*** EOOH ***\n"
1239 "Date: " (mail-rfc822-date) "\n")
1240 (insert-buffer-substring curbuf beg2 end)
1241 (insert "\n\C-_")
1242 (write-region (point-min) (point-max) (car fcc-list) t)
1243 (erase-buffer)))
1244 (write-region
1245 (1+ (point-min)) (point-max) (car fcc-list) t)))
0d15bc31
RS
1246 (and buffer (not dont-write-the-file)
1247 (with-current-buffer buffer
1248 (set-visited-file-modtime))))
20a8832d
RS
1249 (setq fcc-list (cdr fcc-list))))
1250 (kill-buffer tembuf)))
1251
1252(defun mail-sent-via ()
1253 "Make a Sent-via header line from each To or CC header line."
1254 (interactive)
1255 (save-excursion
20a8832d 1256 ;; put a marker at the end of the header
81f5ed32 1257 (let ((end (copy-marker (mail-header-end)))
20a8832d
RS
1258 (case-fold-search t)
1259 to-line)
1260 (goto-char (point-min))
1261 ;; search for the To: lines and make Sent-via: lines from them
1262 ;; search for the next To: line
1263 (while (re-search-forward "^\\(to\\|cc\\):" end t)
1264 ;; Grab this line plus all its continuations, sans the `to:'.
1265 (let ((to-line
1266 (buffer-substring (point)
1267 (progn
1268 (if (re-search-forward "^[^ \t\n]" end t)
1269 (backward-char 1)
1270 (goto-char end))
1271 (point)))))
1272 ;; Insert a copy, with altered header field name.
1273 (insert-before-markers "Sent-via:" to-line))))))
1274\f
1275(defun mail-to ()
1276 "Move point to end of To-field."
1277 (interactive)
1278 (expand-abbrev)
1279 (mail-position-on-field "To"))
1280
1281(defun mail-subject ()
1282 "Move point to end of Subject-field."
1283 (interactive)
1284 (expand-abbrev)
1285 (mail-position-on-field "Subject"))
1286
1287(defun mail-cc ()
1288 "Move point to end of CC-field. Create a CC field if none."
1289 (interactive)
1290 (expand-abbrev)
1291 (or (mail-position-on-field "cc" t)
1292 (progn (mail-position-on-field "to")
1293 (insert "\nCC: "))))
1294
1295(defun mail-bcc ()
1296 "Move point to end of BCC-field. Create a BCC field if none."
1297 (interactive)
1298 (expand-abbrev)
1299 (or (mail-position-on-field "bcc" t)
1300 (progn (mail-position-on-field "to")
1301 (insert "\nBCC: "))))
1302
5a31c2d2 1303(defun mail-fcc (folder)
70ee42f7 1304 "Add a new FCC field, with file name completion."
5a31c2d2 1305 (interactive "FFolder carbon copy: ")
70ee42f7
JB
1306 (expand-abbrev)
1307 (or (mail-position-on-field "fcc" t) ;Put new field after exiting FCC.
1308 (mail-position-on-field "to"))
5a31c2d2 1309 (insert "\nFCC: " folder))
70ee42f7 1310
a1506d29 1311(defun mail-reply-to ()
e04233aa 1312 "Move point to end of Reply-To-field. Create a Reply-To field if none."
3a3ffba4
RS
1313 (interactive)
1314 (expand-abbrev)
1315 (mail-position-on-field "Reply-To"))
1316
3ca0be33
RS
1317(defun mail-mail-reply-to ()
1318 "Move point to end of Mail-Reply-To field.
1319Create a Mail-Reply-To field if none."
1320 (interactive)
1321 (expand-abbrev)
1322 (or (mail-position-on-field "mail-reply-to" t)
1323 (progn (mail-position-on-field "to")
1324 (insert "\nMail-Reply-To: "))))
1325
1326(defun mail-mail-followup-to ()
1327 "Move point to end of Mail-Followup-To field.
1328Create a Mail-Followup-To field if none."
1329 (interactive)
1330 (expand-abbrev)
1331 (or (mail-position-on-field "mail-followup-to" t)
1332 (progn (mail-position-on-field "to")
1333 (insert "\nMail-Followup-To: "))))
1334
20a8832d
RS
1335(defun mail-position-on-field (field &optional soft)
1336 (let (end
1337 (case-fold-search t))
51d541d4 1338 (setq end (mail-header-end))
20a8832d
RS
1339 (goto-char (point-min))
1340 (if (re-search-forward (concat "^" (regexp-quote field) ":") end t)
1341 (progn
1342 (re-search-forward "^[^ \t]" nil 'move)
1343 (beginning-of-line)
1344 (skip-chars-backward "\n")
1345 t)
1346 (or soft
1347 (progn (goto-char end)
08564963
JB
1348 (insert field ": \n")
1349 (skip-chars-backward "\n")))
20a8832d
RS
1350 nil)))
1351
1352(defun mail-text ()
330e9e11 1353 "Move point to beginning of message text."
20a8832d 1354 (interactive)
e3b671fc 1355 (expand-abbrev)
51d541d4 1356 (goto-char (mail-text-start)))
20a8832d 1357\f
e04233aa 1358(defun mail-signature (&optional atpoint)
287ef00f 1359 "Sign letter with signature based on `mail-signature-file'.
e4f794ed 1360Prefix arg means put contents at point."
0d021ccf 1361 (interactive "P")
20a8832d 1362 (save-excursion
0d021ccf
ER
1363 (or atpoint
1364 (goto-char (point-max)))
20a8832d
RS
1365 (skip-chars-backward " \t\n")
1366 (end-of-line)
0d021ccf
ER
1367 (or atpoint
1368 (delete-region (point) (point-max)))
287ef00f
RS
1369 (if (stringp mail-signature)
1370 (insert mail-signature)
1371 (insert "\n\n-- \n")
1372 (insert-file-contents (expand-file-name mail-signature-file)))))
20a8832d
RS
1373
1374(defun mail-fill-yanked-message (&optional justifyp)
1375 "Fill the paragraphs of a message yanked into this one.
1376Numeric argument means justify as well."
1377 (interactive "P")
1378 (save-excursion
51d541d4 1379 (goto-char (mail-text-start))
20a8832d
RS
1380 (fill-individual-paragraphs (point)
1381 (point-max)
1382 justifyp
706e3d85 1383 mail-citation-prefix-regexp)))
20a8832d 1384
374bda6b 1385(defun mail-indent-citation ()
1c24b04a
RS
1386 "Modify text just inserted from a message to be cited.
1387The inserted text should be the region.
1388When this function returns, the region is again around the modified text.
1389
1390Normally, indent each nonblank line `mail-indentation-spaces' spaces.
1391However, if `mail-yank-prefix' is non-nil, insert that prefix on each line."
374bda6b 1392 (mail-yank-clear-headers (region-beginning) (region-end))
e39e74c0 1393 (if (null mail-yank-prefix)
374bda6b
RS
1394 (indent-rigidly (region-beginning) (region-end)
1395 mail-indentation-spaces)
e39e74c0 1396 (save-excursion
374bda6b
RS
1397 (let ((end (set-marker (make-marker) (region-end))))
1398 (goto-char (region-beginning))
1399 (while (< (point) end)
1400 (insert mail-yank-prefix)
1401 (forward-line 1))))))
1c24b04a 1402
20a8832d
RS
1403(defun mail-yank-original (arg)
1404 "Insert the message being replied to, if any (in rmail).
26adca1b 1405Puts point after the text and mark before.
20a8832d
RS
1406Normally, indents each nonblank line ARG spaces (default 3).
1407However, if `mail-yank-prefix' is non-nil, insert that prefix on each line.
1408
1409Just \\[universal-argument] as argument means don't indent, insert no prefix,
1410and don't delete any header fields."
1411 (interactive "P")
c8553837
RS
1412 (if mail-reply-action
1413 (let ((start (point))
1414 (original mail-reply-action))
1415 (and (consp original) (eq (car original) 'insert-buffer)
1416 (setq original (nth 1 original)))
1417 (if (consp original)
1418 (apply (car original) (cdr original))
1419 ;; If the original message is in another window in the same frame,
1420 ;; delete that window to save screen space.
1421 ;; t means don't alter other frames.
1422 (delete-windows-on original t)
75e4db34
RS
1423 (insert-buffer original)
1424 (set-text-properties (point) (mark t) nil))
20a8832d
RS
1425 (if (consp arg)
1426 nil
1c24b04a
RS
1427 (goto-char start)
1428 (let ((mail-indentation-spaces (if arg (prefix-numeric-value arg)
ac9f0310 1429 mail-indentation-spaces))
8761c2c4
RS
1430 ;; Avoid error in Transient Mark mode
1431 ;; on account of mark's being inactive.
ac9f0310 1432 (mark-even-if-inactive t))
e04233aa
GM
1433 (cond (mail-citation-hook
1434 ;; Bind mail-citation-header to the inserted
1435 ;; message's header.
1436 (let ((mail-citation-header
1437 (buffer-substring-no-properties
1438 start
1439 (save-excursion
1440 (save-restriction
1441 (narrow-to-region start (point-max))
1442 (goto-char start)
1443 (rfc822-goto-eoh)
1444 (point))))))
1445 (run-hooks 'mail-citation-hook)))
1446 (mail-yank-hooks
1447 (run-hooks 'mail-yank-hooks))
1448 (t
1449 (mail-indent-citation)))))
3c280f48
RS
1450 ;; This is like exchange-point-and-mark, but doesn't activate the mark.
1451 ;; It is cleaner to avoid activation, even though the command
1452 ;; loop would deactivate the mark because we inserted text.
1453 (goto-char (prog1 (mark t)
1454 (set-marker (mark-marker) (point) (current-buffer))))
20a8832d
RS
1455 (if (not (eolp)) (insert ?\n)))))
1456
1457(defun mail-yank-clear-headers (start end)
e39e74c0
RS
1458 (if (< end start)
1459 (let (temp)
1460 (setq temp start start end end temp)))
2b3a206d
RS
1461 (if mail-yank-ignored-headers
1462 (save-excursion
1463 (goto-char start)
1464 (if (search-forward "\n\n" end t)
1465 (save-restriction
1466 (narrow-to-region start (point))
1467 (goto-char start)
1468 (while (let ((case-fold-search t))
1469 (re-search-forward mail-yank-ignored-headers nil t))
1470 (beginning-of-line)
1471 (delete-region (point)
1472 (progn (re-search-forward "\n[^ \t]")
1473 (forward-char -1)
1474 (point)))))))))
e39e74c0
RS
1475
1476(defun mail-yank-region (arg)
1477 "Insert the selected region from the message being replied to.
1478Puts point after the text and mark before.
1479Normally, indents each nonblank line ARG spaces (default 3).
1480However, if `mail-yank-prefix' is non-nil, insert that prefix on each line.
1481
1482Just \\[universal-argument] as argument means don't indent, insert no prefix,
1483and don't delete any header fields."
1484 (interactive "P")
1485 (and (consp mail-reply-action)
1486 (eq (car mail-reply-action) 'insert-buffer)
75e4db34
RS
1487 (with-current-buffer (nth 1 mail-reply-action)
1488 (or (mark t)
1489 (error "No mark set: %S" (current-buffer))))
e39e74c0 1490 (let ((buffer (nth 1 mail-reply-action))
8761c2c4
RS
1491 (start (point))
1492 ;; Avoid error in Transient Mark mode
1493 ;; on account of mark's being inactive.
1494 (mark-even-if-inactive t))
e39e74c0
RS
1495 ;; Insert the citation text.
1496 (insert (with-current-buffer buffer
d6bccf09 1497 (buffer-substring-no-properties (point) (mark))))
e39e74c0
RS
1498 (push-mark start)
1499 ;; Indent or otherwise annotate the citation text.
1500 (if (consp arg)
1501 nil
1502 (let ((mail-indentation-spaces (if arg (prefix-numeric-value arg)
1503 mail-indentation-spaces)))
1504 (if mail-citation-hook
d6bccf09
RS
1505 ;; Bind mail-citation-hook to the original message's header.
1506 (let ((mail-citation-header
1507 (with-current-buffer buffer
1508 (buffer-substring-no-properties
1509 (point-min)
1510 (save-excursion
1511 (goto-char (point-min))
1512 (rfc822-goto-eoh)
1513 (point))))))
1514 (run-hooks 'mail-citation-hook))
e39e74c0
RS
1515 (if mail-yank-hooks
1516 (run-hooks 'mail-yank-hooks)
374bda6b 1517 (mail-indent-citation))))))))
8ba7cd40
KS
1518
1519(defun mail-split-line ()
1520 "Split current line, moving portion beyond point vertically down.
1521If the current line has `mail-yank-prefix', insert it on the new line."
1522 (interactive "*")
1523 (split-line mail-yank-prefix))
1524
20a8832d 1525\f
f0aef6bf
RS
1526(defun mail-attach-file (&optional file)
1527 "Insert a file at the end of the buffer, with separator lines around it."
1528 (interactive "fAttach file: ")
1529 (save-excursion
1530 (goto-char (point-max))
1531 (or (bolp) (newline))
1532 (newline)
1533 (let ((start (point))
1534 middle)
1535 (insert (format "===File %s===" file))
1536 (insert-char ?= (max 0 (- 60 (current-column))))
1537 (newline)
1538 (setq middle (point))
1539 (insert "============================================================\n")
1540 (push-mark)
1541 (goto-char middle)
1542 (insert-file-contents file)
1543 (or (bolp) (newline))
1544 (goto-char start))))
1545\f
699adcb2
RS
1546;; Put these commands last, to reduce chance of lossage from quitting
1547;; in middle of loading the file.
1548
1549;;;###autoload (add-hook 'same-window-buffer-names "*mail*")
20a8832d
RS
1550
1551;;;###autoload
1552(defun mail (&optional noerase to subject in-reply-to cc replybuffer actions)
e635fdf0
RS
1553 "Edit a message to be sent. Prefix arg means resume editing (don't erase).
1554When this function returns, the buffer `*mail*' is selected.
1555The value is t if the message was newly initialized; otherwise, nil.
20a8832d 1556
4ac2e032
RS
1557Optionally, the signature file `mail-signature-file' can be inserted at the
1558end; see the variable `mail-signature'.
20a8832d
RS
1559
1560\\<mail-mode-map>
1561While editing message, type \\[mail-send-and-exit] to send the message and exit.
1562
1563Various special commands starting with C-c are available in sendmail mode
1564to move to message header fields:
1565\\{mail-mode-map}
1566
1567If `mail-self-blind' is non-nil, a BCC to yourself is inserted
1568when the message is initialized.
1569
1570If `mail-default-reply-to' is non-nil, it should be an address (a string);
1571a Reply-to: field with that address is inserted.
1572
1573If `mail-archive-file-name' is non-nil, an FCC field with that file name
1574is inserted.
1575
8b262cbc
KH
1576The normal hook `mail-setup-hook' is run after the message is
1577initialized. It can add more default fields to the message.
20a8832d 1578
dfdac213
RS
1579When calling from a program, the first argument if non-nil says
1580not to erase the existing contents of the `*mail*' buffer.
1581
1582The second through fifth arguments,
1583 TO, SUBJECT, IN-REPLY-TO and CC, specify if non-nil
20a8832d
RS
1584 the initial contents of those header fields.
1585 These arguments should not have final newlines.
c8553837
RS
1586The sixth argument REPLYBUFFER is a buffer which contains an
1587 original message being replied to, or else an action
1588 of the form (FUNCTION . ARGS) which says how to insert the original.
1589 Or it can be nil, if not replying to anything.
20a8832d
RS
1590The seventh argument ACTIONS is a list of actions to take
1591 if/when the message is sent. Each action looks like (FUNCTION . ARGS);
1592 when the message is sent, we apply FUNCTION to ARGS.
1593 This is how Rmail arranges to mark messages `answered'."
1594 (interactive "P")
e635fdf0
RS
1595;;; This is commented out because I found it was confusing in practice.
1596;;; It is easy enough to rename *mail* by hand with rename-buffer
1597;;; if you want to have multiple mail buffers.
1598;;; And then you can control which messages to save. --rms.
1599;;; (let ((index 1)
1600;;; buffer)
1601;;; ;; If requested, look for a mail buffer that is modified and go to it.
1602;;; (if noerase
1603;;; (progn
1604;;; (while (and (setq buffer
1605;;; (get-buffer (if (= 1 index) "*mail*"
1606;;; (format "*mail*<%d>" index))))
1607;;; (not (buffer-modified-p buffer)))
1608;;; (setq index (1+ index)))
1609;;; (if buffer (switch-to-buffer buffer)
1610;;; ;; If none exists, start a new message.
1611;;; ;; This will never re-use an existing unmodified mail buffer
1612;;; ;; (since index is not 1 anymore). Perhaps it should.
1613;;; (setq noerase nil))))
1614;;; ;; Unless we found a modified message and are happy, start a new message.
1615;;; (if (not noerase)
1616;;; (progn
1617;;; ;; Look for existing unmodified mail buffer.
1618;;; (while (and (setq buffer
1619;;; (get-buffer (if (= 1 index) "*mail*"
1620;;; (format "*mail*<%d>" index))))
1621;;; (buffer-modified-p buffer))
1622;;; (setq index (1+ index)))
1623;;; ;; If none, make a new one.
1624;;; (or buffer
1625;;; (setq buffer (generate-new-buffer "*mail*")))
1626;;; ;; Go there and initialize it.
1627;;; (switch-to-buffer buffer)
1628;;; (erase-buffer)
1629;;; (setq default-directory (expand-file-name "~/"))
1630;;; (auto-save-mode auto-save-default)
1631;;; (mail-mode)
1632;;; (mail-setup to subject in-reply-to cc replybuffer actions)
1633;;; (if (and buffer-auto-save-file-name
1634;;; (file-exists-p buffer-auto-save-file-name))
1635;;; (message "Auto save file for draft message exists; consider M-x mail-recover"))
1636;;; t))
67a6988b 1637 (pop-to-buffer "*mail*")
eb1213c1
MR
1638 ;; Avoid danger that the auto-save file can't be written.
1639 (let ((dir (expand-file-name
1640 (file-name-as-directory mail-default-directory))))
1641 (if (file-exists-p dir)
1642 (setq default-directory dir)))
2e78575e
RS
1643 ;; Only call auto-save-mode if necessary, to avoid changing auto-save file.
1644 (if (or (and auto-save-default (not buffer-auto-save-file-name))
1645 (and (not auto-save-default) buffer-auto-save-file-name))
1646 (auto-save-mode auto-save-default))
e635fdf0 1647 (mail-mode)
3b112b5e
MB
1648 ;; Disconnect the buffer from its visited file
1649 ;; (in case the user has actually visited a file *mail*).
e4fbad4b 1650; (set-visited-file-name nil)
e635fdf0
RS
1651 (let (initialized)
1652 (and (not noerase)
5e32e119
RS
1653 (if buffer-file-name
1654 (if (buffer-modified-p)
1655 (when (y-or-n-p "Buffer has unsaved changes; reinitialize it and discard them? ")
1656 (if (y-or-n-p "Disconnect buffer from visited file? ")
1657 (set-visited-file-name nil))
1658 t)
1659 (when (y-or-n-p "Reinitialize buffer, and disconnect it from the visited file? ")
1660 (set-visited-file-name nil)
1661 t))
1662 ;; A non-file-visiting buffer.
1663 (if (buffer-modified-p)
1664 (y-or-n-p "Unsent message being composed; erase it? ")
1665 t))
6737ae74
RS
1666 (let ((inhibit-read-only t))
1667 (erase-buffer)
1668 (mail-setup to subject in-reply-to cc replybuffer actions)
1669 (setq initialized t)))
e635fdf0
RS
1670 (if (and buffer-auto-save-file-name
1671 (file-exists-p buffer-auto-save-file-name))
1672 (message "Auto save file for draft message exists; consider M-x mail-recover"))
1673 initialized))
20a8832d 1674
6cd4cfc5 1675(defun mail-recover-1 ()
918c7db7 1676 "Pop up a list of auto-saved draft messages so you can recover one of them."
6cd4cfc5
EZ
1677 (interactive)
1678 (let ((file-name (make-auto-save-file-name))
1679 (ls-lisp-support-shell-wildcards t)
1680 non-random-len wildcard)
1681 ;; Remove the random part from the auto-save-file-name, and
1682 ;; create a wildcard which matches possible candidates.
1683 ;; Note: this knows that make-auto-save-file-name appends
1684 ;; "#<RANDOM-STUFF>#" to the buffer name, where RANDOM-STUFF
1685 ;; is the result of (make-temp-name "").
1686 (setq non-random-len
d41eb0aa 1687 (- (length file-name) (length (make-temp-name "")) 1))
6cd4cfc5
EZ
1688 (setq wildcard (concat (substring file-name 0 non-random-len) "*"))
1689 (if (null (file-expand-wildcards wildcard))
1690 (message "There are no auto-saved drafts to recover")
1691 ;; Bind dired-trivial-filenames to t because all auto-save file
1692 ;; names are normally ``trivial'', so Dired will set point after
1693 ;; all the files, at buffer bottom. We want it on the first
1694 ;; file instead.
1695 (let ((dired-trivial-filenames t))
1696 (dired-other-window wildcard (concat dired-listing-switches "t")))
1697 (rename-buffer "*Auto-saved Drafts*" t)
1698 (save-excursion
1699 (goto-char (point-min))
1700 (or (looking-at " Move to the draft file you want to recover,")
1701 (let ((inhibit-read-only t))
1702 ;; Each line starts with a space so that Font Lock mode
1703 ;; won't highlight the first character.
1704 (insert "\
1705 Move to the draft file you want to recover, then type C-c C-c
1706 to recover text of message whose composition was interrupted.
1707 To browse text of a draft, type v on the draft file's line.
1708
1709 You can also delete some of these files;
1710 type d on a line to mark that file for deletion.
1711
1712 List of possible auto-save files for recovery:
1713
1714"))))
1715 (use-local-map
1716 (let ((map (make-sparse-keymap)))
1717 (set-keymap-parent map (current-local-map))
1718 map))
1719 (define-key (current-local-map) "v"
1720 (lambda ()
1721 (interactive)
1722 (let ((coding-system-for-read 'emacs-mule-unix))
1723 (dired-view-file))))
1724 (define-key (current-local-map) "\C-c\C-c"
1725 (lambda ()
1726 (interactive)
1727 (let ((fname (dired-get-filename))
1728 ;; Auto-saved files are written in the internal
1729 ;; representation, so they should be read accordingly.
1730 (coding-system-for-read 'emacs-mule-unix))
1731 (switch-to-buffer-other-window "*mail*")
1732 (let ((buffer-read-only nil))
1733 (erase-buffer)
1734 (insert-file-contents fname nil)
1735 ;; insert-file-contents will set buffer-file-coding-system
1736 ;; to emacs-mule, which is probably not what they want to
1737 ;; use for sending the message. But we don't know what
1738 ;; was its value before the buffer was killed or Emacs
1739 ;; crashed. We therefore reset buffer-file-coding-system
1740 ;; to the default value, so that either the default does
1741 ;; TRT, or the user will get prompted for the right
1742 ;; encoding when they send the message.
1743 (setq buffer-file-coding-system
1744 default-buffer-file-coding-system))))))))
1745
20a8832d 1746(defun mail-recover ()
918c7db7
EZ
1747 "Recover interrupted mail composition from auto-save files.
1748
1749If the mail buffer has a current valid auto-save file,
1750the command recovers that file. Otherwise, it displays a
1751buffer showing the existing auto-saved draft messages;
1752you can move to one of them and type C-c C-c to recover that one."
20a8832d 1753 (interactive)
6cd4cfc5
EZ
1754 ;; In case they invoke us from some random buffer...
1755 (switch-to-buffer "*mail*")
1756 ;; If *mail* didn't exist, set its directory, so that auto-saved
1757 ;; drafts will be found.
eb1213c1
MR
1758 (let ((dir (expand-file-name
1759 (file-name-as-directory mail-default-directory))))
1760 (if (file-exists-p dir)
1761 (setq default-directory dir)))
6cd4cfc5
EZ
1762 (or (eq major-mode 'mail-mode)
1763 (mail-mode))
1764 (let ((file-name buffer-auto-save-file-name))
1765 (cond ((and file-name (file-exists-p file-name))
1766 (let ((dispbuf
1767 ;; This used to invoke `ls' via call-process, but
1768 ;; dired-noselect is more portable to systems where
1769 ;; `ls' is not a standard program (it will use
1770 ;; ls-lisp instead).
1771 (dired-noselect file-name
1772 (concat dired-listing-switches "t"))))
8366cfb5
RS
1773 (save-selected-window
1774 (select-window (display-buffer dispbuf t))
1775 (goto-char (point-min))
1776 (forward-line 2)
1777 (dired-move-to-filename)
1778 (setq dispbuf (rename-buffer "*Directory*" t)))
6cd4cfc5
EZ
1779 (if (not (yes-or-no-p
1780 (format "Recover mail draft from auto save file %s? "
1781 file-name)))
1782 (error "mail-recover cancelled")
1783 (let ((buffer-read-only nil)
1784 (buffer-coding buffer-file-coding-system)
1785 ;; Auto-save files are written in internal
1786 ;; representation of non-ASCII characters.
1787 (coding-system-for-read 'emacs-mule-unix))
1788 (erase-buffer)
1789 (insert-file-contents file-name nil)
1790 (setq buffer-file-coding-system buffer-coding)))))
1791 (t (mail-recover-1)))))
20a8832d
RS
1792
1793;;;###autoload
1794(defun mail-other-window (&optional noerase to subject in-reply-to cc replybuffer sendactions)
1795 "Like `mail' command, but display mail buffer in another window."
1796 (interactive "P")
67a6988b
RS
1797 (let ((pop-up-windows t)
1798 (special-display-buffer-names nil)
1799 (special-display-regexps nil)
1800 (same-window-buffer-names nil)
1801 (same-window-regexps nil))
20a8832d
RS
1802 (pop-to-buffer "*mail*"))
1803 (mail noerase to subject in-reply-to cc replybuffer sendactions))
1804
1805;;;###autoload
0cc89026
JB
1806(defun mail-other-frame (&optional noerase to subject in-reply-to cc replybuffer sendactions)
1807 "Like `mail' command, but display mail buffer in another frame."
20a8832d 1808 (interactive "P")
67a6988b
RS
1809 (let ((pop-up-frames t)
1810 (special-display-buffer-names nil)
1811 (special-display-regexps nil)
1812 (same-window-buffer-names nil)
1813 (same-window-regexps nil))
20a8832d
RS
1814 (pop-to-buffer "*mail*"))
1815 (mail noerase to subject in-reply-to cc replybuffer sendactions))
1816
20a8832d 1817;;; Do not add anything but external entries on this page.
49116ac0
JB
1818
1819(provide 'sendmail)
1820
ab5796a9 1821;;; arch-tag: 48bc1025-d993-4d31-8d81-2a29491f0626
c88ab9ce 1822;;; sendmail.el ends here