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