Fix info and customize *-other-window commands.
[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
568 (let ((special-display-buffer-names nil)
569 (special-display-regexps nil)
570 (same-window-buffer-names nil)
571 (same-window-regexps nil))
572 (funcall switch-function "*mail*")))
573 (let ((cc (cdr (assoc-string "cc" other-headers t)))
574 (in-reply-to (cdr (assoc-string "in-reply-to" other-headers t)))
575 (body (cdr (assoc-string "body" other-headers t))))
576 (or (mail continue to subject in-reply-to cc yank-action
577 send-actions return-action)
578 continue
579 (error "Message aborted"))
580 (save-excursion
581 (rfc822-goto-eoh)
582 (while other-headers
583 (unless (member-ignore-case (car (car other-headers))
584 '("in-reply-to" "cc" "body"))
585 (insert (car (car other-headers)) ": "
586 (cdr (car other-headers))
587 (if use-hard-newlines hard-newline "\n")))
588 (setq other-headers (cdr other-headers)))
589 (when body
590 (forward-line 1)
591 (insert body))
592 t)))
593
594(defun mail-setup (to subject in-reply-to cc replybuffer
595 actions return-action)
235c2d35 596 (or mail-default-reply-to
810163a3 597 (setq mail-default-reply-to (getenv "REPLYTO")))
1498db18 598 (sendmail-sync-aliases)
e758216c
GM
599 (when (eq mail-aliases t)
600 (setq mail-aliases nil)
601 (and mail-personal-alias-file
602 (file-exists-p mail-personal-alias-file)
603 (build-mail-aliases)))
2813dcb9 604 ;; Don't leave this around from a previous message.
f8081291 605 (kill-local-variable 'buffer-file-coding-system)
9be82109
KH
606 ;; This doesn't work for enable-multibyte-characters.
607 ;; (kill-local-variable 'enable-multibyte-characters)
597e2240 608 (set-buffer-multibyte (default-value 'enable-multibyte-characters))
8881c1ab
KH
609 (if current-input-method
610 (inactivate-input-method))
25ca2e61
CY
611
612 ;; Local variables for Mail mode.
20a8832d 613 (setq mail-send-actions actions)
c8553837 614 (setq mail-reply-action replybuffer)
25ca2e61
CY
615 (setq mail-return-action return-action)
616
20a8832d 617 (goto-char (point-min))
1938b566
RS
618 (if mail-setup-with-from
619 (mail-insert-from-field))
20a8832d
RS
620 (insert "To: ")
621 (save-excursion
622 (if to
70ca75ae
RS
623 ;; Here removed code to extract names from within <...>
624 ;; on the assumption that mail-strip-quoted-names
625 ;; has been called and has done so.
626 (let ((fill-prefix "\t")
627 (address-start (point)))
20a8832d 628 (insert to "\n")
118a3c04
RS
629 (fill-region-as-paragraph address-start (point-max))
630 (goto-char (point-max))
631 (unless (bolp)
632 (newline)))
20a8832d
RS
633 (newline))
634 (if cc
70ca75ae
RS
635 (let ((fill-prefix "\t")
636 (address-start (progn (insert "CC: ") (point))))
637 (insert cc "\n")
118a3c04
RS
638 (fill-region-as-paragraph address-start (point-max))
639 (goto-char (point-max))
640 (unless (bolp)
641 (newline))))
20a8832d 642 (if in-reply-to
118a3c04 643 (let ((fill-prefix "\t")
58cabff0 644 (fill-column 78)
34046d66
RS
645 (address-start (point)))
646 (insert "In-reply-to: " in-reply-to "\n")
118a3c04
RS
647 (fill-region-as-paragraph address-start (point-max))
648 (goto-char (point-max))
649 (unless (bolp)
650 (newline))))
20a8832d
RS
651 (insert "Subject: " (or subject "") "\n")
652 (if mail-default-headers
653 (insert mail-default-headers))
654 (if mail-default-reply-to
655 (insert "Reply-to: " mail-default-reply-to "\n"))
656 (if mail-self-blind
22c03d39 657 (insert "BCC: " user-mail-address "\n"))
20a8832d
RS
658 (if mail-archive-file-name
659 (insert "FCC: " mail-archive-file-name "\n"))
6737ae74
RS
660 (put-text-property (point)
661 (progn
662 (insert mail-header-separator "\n")
663 (1- (point)))
664 'category 'mail-header-separator)
874bdb5a
RS
665 ;; Insert the signature. But remember the beginning of the message.
666 (if to (setq to (point)))
87ede499 667 (if mail-signature (mail-signature t))
20a8832d
RS
668 (goto-char (point-max))
669 (or (bolp) (newline)))
874bdb5a 670 (if to (goto-char to))
20a8832d
RS
671 (or to subject in-reply-to
672 (set-buffer-modified-p nil))
673 (run-hooks 'mail-setup-hook))
5b6575b7 674\f
be0b8d69 675(defcustom mail-mode-hook nil
b769f3da
GM
676 "Hook run by Mail mode.
677When composing a mail, this runs immediately after creating, or
678switching to, the `*mail*' buffer. See also `mail-setup-hook'."
be0b8d69
DL
679 :group 'sendmail
680 :type 'hook
681 :options '(footnote-mode))
682
567bb3dd 683(defvar mail-mode-abbrev-table text-mode-abbrev-table)
fd59d131 684(defvar mail-encode-mml)
20a8832d 685;;;###autoload
f81b71c7 686(define-derived-mode mail-mode text-mode "Mail"
20a8832d
RS
687 "Major mode for editing mail to be sent.
688Like Text Mode but with these additional commands:
e8f4db18
RS
689
690\\[mail-send] mail-send (send the message)
691\\[mail-send-and-exit] mail-send-and-exit (send the message and exit)
692
d3db616e 693Here are commands that move to a header field (and create it if there isn't):
5d838629
CY
694 \\[mail-to] move to To: \\[mail-subject] move to Subj:
695 \\[mail-bcc] move to BCC: \\[mail-cc] move to CC:
0678d779 696 \\[mail-fcc] move to FCC: \\[mail-reply-to] move to Reply-To:
3ca0be33
RS
697 \\[mail-mail-reply-to] move to Mail-Reply-To:
698 \\[mail-mail-followup-to] move to Mail-Followup-To:
5d838629 699\\[mail-text] move to message text.
d3db616e
KH
700\\[mail-signature] mail-signature (insert `mail-signature-file' file).
701\\[mail-yank-original] mail-yank-original (insert current message, in Rmail).
702\\[mail-fill-yanked-message] mail-fill-yanked-message (fill what was yanked).
c3760c17
EZ
703\\[mail-insert-file] insert a text file into the message.
704\\[mail-add-attachment] attach to the message a file as binary attachment.
4deb3ba9
EZ
705Turning on Mail mode runs the normal hooks `text-mode-hook' and
706`mail-mode-hook' (in that order)."
c8553837 707 (make-local-variable 'mail-reply-action)
20a8832d 708 (make-local-variable 'mail-send-actions)
25ca2e61 709 (make-local-variable 'mail-return-action)
fd59d131
EZ
710 (make-local-variable 'mail-encode-mml)
711 (setq mail-encode-mml nil)
20a8832d 712 (setq buffer-offer-save t)
2667df42 713 (make-local-variable 'font-lock-defaults)
117e4b0c 714 (setq font-lock-defaults '(mail-font-lock-keywords t t))
20a8832d 715 (make-local-variable 'paragraph-separate)
5b6575b7
RS
716 (make-local-variable 'normal-auto-fill-function)
717 (setq normal-auto-fill-function 'mail-mode-auto-fill)
b5682230 718 (make-local-variable 'fill-paragraph-function)
c8553837 719 (setq fill-paragraph-function 'mail-mode-fill-paragraph)
eba52a1a
MB
720 ;; Allow using comment commands to add/remove quoting (this only does
721 ;; anything if mail-yank-prefix is set to a non-nil value).
722 (set (make-local-variable 'comment-start) mail-yank-prefix)
6b61353c
KH
723 (if mail-yank-prefix
724 (set (make-local-variable 'comment-start-skip)
725 (concat "^" (regexp-quote mail-yank-prefix) "[ \t]*")))
1b8def58
RS
726 (make-local-variable 'adaptive-fill-regexp)
727 (setq adaptive-fill-regexp
567a1c29 728 (concat "[ \t]*[-[:alnum:]]+>+[ \t]*\\|"
f81b71c7 729 adaptive-fill-regexp))
d78f466f 730 (make-local-variable 'adaptive-fill-first-line-regexp)
f03ca5f5 731 (setq adaptive-fill-first-line-regexp
40035487 732 (concat "[ \t]*[-[:alnum:]]*>+[ \t]*\\|"
f81b71c7 733 adaptive-fill-first-line-regexp))
daca8ba5
SM
734 (add-hook 'completion-at-point-functions #'mail-completion-at-point-function
735 nil 'local)
df0d89b1
RS
736 ;; `-- ' precedes the signature. `-----' appears at the start of the
737 ;; lines that delimit forwarded messages.
738 ;; Lines containing just >= 3 dashes, perhaps after whitespace,
739 ;; are also sometimes used and should be separators.
f3e95b0d 740 (setq paragraph-separate (concat (regexp-quote mail-header-separator)
75e4db34 741 "$\\|\t*\\([-|#;>* ]\\|(?[0-9]+[.)]\\)+$"
0eafc06c 742 "\\|[ \t]*[[:alnum:]]*>+[ \t]*$\\|[ \t]*$\\|"
f3e95b0d
SM
743 "--\\( \\|-+\\)$\\|"
744 page-delimiter)))
5b6575b7 745
51d541d4
RS
746
747(defun mail-header-end ()
748 "Return the buffer location of the end of headers, as a number."
66191b7e
RS
749 (save-restriction
750 (widen)
751 (save-excursion
752 (rfc822-goto-eoh)
753 (point))))
51d541d4
RS
754
755(defun mail-text-start ()
756 "Return the buffer location of the start of text, as a number."
66191b7e
RS
757 (save-restriction
758 (widen)
759 (save-excursion
760 (rfc822-goto-eoh)
761 (forward-line 1)
762 (point))))
51d541d4
RS
763
764(defun mail-sendmail-delimit-header ()
765 "Set up whatever header delimiter convention sendmail will use.
766Concretely: replace the first blank line in the header with the separator."
767 (rfc822-goto-eoh)
768 (insert mail-header-separator)
769 (point))
770
771(defun mail-sendmail-undelimit-header ()
772 "Remove header separator to put the message in correct form for sendmail.
773Leave point at the start of the delimiter line."
774 (rfc822-goto-eoh)
775 (delete-region (point) (progn (end-of-line) (point))))
776
5b6575b7
RS
777(defun mail-mode-auto-fill ()
778 "Carry out Auto Fill for Mail mode.
779If within the headers, this makes the new lines into continuation lines."
51d541d4 780 (if (< (point) (mail-header-end))
5ed619e0 781 (let ((old-line-start (line-beginning-position)))
5b6575b7
RS
782 (if (do-auto-fill)
783 (save-excursion
784 (beginning-of-line)
785 (while (not (eq (point) old-line-start))
28afe199
RS
786 ;; Use insert-before-markers in case we're inserting
787 ;; before the saved value of point (which is common).
788 (insert-before-markers " ")
5b6575b7
RS
789 (forward-line -1))
790 t)))
791 (do-auto-fill)))
c8553837
RS
792
793(defun mail-mode-fill-paragraph (arg)
794 ;; Do something special only if within the headers.
51d541d4 795 (if (< (point) (mail-header-end))
a1506d29 796 (let (beg end fieldname)
8fcbbf7d
GM
797 (when (prog1 (re-search-backward "^[-a-zA-Z]+:" nil 'yes)
798 (setq beg (point)))
c8553837 799 (setq fieldname
8fcbbf7d 800 (downcase (buffer-substring beg (1- (match-end 0))))))
c8553837
RS
801 (forward-line 1)
802 ;; Find continuation lines and get rid of their continuation markers.
803 (while (looking-at "[ \t]")
804 (delete-horizontal-space)
805 (forward-line 1))
806 (setq end (point-marker))
807 (goto-char beg)
808 ;; If this field contains addresses,
809 ;; make sure we can fill after each address.
810 (if (member fieldname
811 '("to" "cc" "bcc" "from" "reply-to"
3ca0be33 812 "mail-reply-to" "mail-followup-to"
c8553837
RS
813 "resent-to" "resent-cc" "resent-bcc"
814 "resent-from" "resent-reply-to"))
815 (while (search-forward "," end t)
816 (or (looking-at "[ \t]")
817 (insert " "))))
95fe848e 818 (fill-region-as-paragraph beg end arg)
c8553837
RS
819 ;; Mark all lines except the first as continuations.
820 (goto-char beg)
821 (forward-line 1)
822 (while (< (point) end)
823 (insert " ")
824 (forward-line 1))
825 (move-marker end nil)
826 t)))
299fdd8d 827\f
5b6575b7
RS
828;; User-level commands for sending.
829
2c1e2995 830(defun mail-send-and-exit (&optional arg)
33b4a308 831 "Send message like `mail-send', then, if no errors, exit from mail buffer.
20a8832d
RS
832Prefix arg means don't delete this window."
833 (interactive "P")
834 (mail-send)
1b6bb250
RS
835 (mail-bury arg))
836
8451b5e9 837(defun mail-dont-send (&optional arg)
1b6bb250
RS
838 "Don't send the message you have been editing.
839Prefix arg means don't delete this window."
840 (interactive "P")
841 (mail-bury arg))
842
8451b5e9 843(defun mail-bury (&optional arg)
1b6bb250 844 "Bury this mail buffer."
50718fc2
RS
845 (let ((newbuf (other-buffer (current-buffer)))
846 (return-action mail-return-action)
847 some-rmail)
e49ad9a5 848 (bury-buffer (current-buffer))
50718fc2
RS
849 ;; If there is an Rmail buffer, return to it nicely
850 ;; even if this message was not started by an Rmail command.
851 (unless return-action
852 (dolist (buffer (buffer-list))
853 (if (eq (buffer-local-value 'major-mode buffer) 'rmail-mode)
854 (setq return-action `(rmail-mail-return ,newbuf)))))
855 (if (and (null arg) return-action)
856 (apply (car return-action) (cdr return-action))
25ca2e61 857 (switch-to-buffer newbuf))))
20a8832d 858
be0b8d69 859(defcustom mail-send-hook nil
28f57f7e 860 "Hook run just before sending a message."
be0b8d69
DL
861 :type 'hook
862 :options '(flyspell-mode-off)
863 :group 'sendmail)
864
3ca0be33 865;;;###autoload
0462d019
GM
866(defcustom mail-mailing-lists nil
867"List of mailing list addresses the user is subscribed to.
3ca0be33
RS
868The variable is used to trigger insertion of the \"Mail-Followup-To\"
869header when sending a message to a mailing list."
870 :type '(repeat string)
871 :group 'sendmail)
872
c3760c17 873(declare-function mml-to-mime "mml" ())
3ca0be33 874
20a8832d
RS
875(defun mail-send ()
876 "Send the message in the current buffer.
877If `mail-interactive' is non-nil, wait for success indication
878or error messages, and inform user.
879Otherwise any failure is reported in a message back to
880the user from the mailer."
881 (interactive)
e11094e6
RS
882 (if (if buffer-file-name
883 (y-or-n-p "Send buffer contents as mail message? ")
884 (or (buffer-modified-p)
885 (y-or-n-p "Message already sent; resend? ")))
34153d76 886 (let ((inhibit-read-only t)
3ca0be33
RS
887 (opoint (point))
888 (ml (when mail-mailing-lists
889 ;; The surrounding regexp assumes the use of
890 ;; `mail-strip-quoted-names' on addresses before matching
891 ;; Cannot deal with full RFC 822 freedom, but that is
892 ;; unlikely to be problematic.
893 (concat "\\(?:[[:space:];,]\\|\\`\\)"
894 (regexp-opt mail-mailing-lists t)
895 "\\(?:[[:space:];,]\\|\\'\\)"))))
896 ;; If there are mailing lists defined
897 (when ml
898 (save-excursion
899 (let* ((to (mail-fetch-field "to" nil t))
900 (cc (mail-fetch-field "cc" nil t))
901 (new-header-values ; To: and Cc:
902 (mail-strip-quoted-names
903 (concat to (when cc (concat ", " cc))))))
904 ;; If message goes to known mailing list ...
905 (when (string-match ml new-header-values)
906 ;; Add Mail-Followup-To if none yet
907 (unless (mail-fetch-field "mail-followup-to")
908 (goto-char (mail-header-end))
909 (insert "Mail-Followup-To: "
910 (let ((l))
911 (mapc
912 ;; remove duplicates
4f91a816
SM
913 (lambda (e)
914 (unless (member e l)
915 (push e l)))
6f0de615
EZ
916 (split-string new-header-values
917 ",[[:space:]]+" t))
3ca0be33
RS
918 (mapconcat 'identity l ", "))
919 "\n"))
920 ;; Add Mail-Reply-To if none yet
921 (unless (mail-fetch-field "mail-reply-to")
922 (goto-char (mail-header-end))
923 (insert "Mail-Reply-To: "
924 (or (mail-fetch-field "reply-to")
925 user-mail-address)
926 "\n"))))))
42d305db 927 (unless (memq mail-send-nonascii '(t mime))
34153d76
RS
928 (goto-char (point-min))
929 (skip-chars-forward "\0-\177")
930 (or (= (point) (point-max))
931 (if (eq mail-send-nonascii 'query)
932 (or (y-or-n-p "Message contains non-ASCII characters; send anyway? ")
933 (error "Aborted"))
934 (error "Message contains non-ASCII characters"))))
413d1aa2
RS
935 ;; Complain about any invalid line.
936 (goto-char (point-min))
c43d7539
CY
937 (re-search-forward (regexp-quote mail-header-separator) (point-max) t)
938 (let ((header-end (or (match-beginning 0) (point-max))))
939 (goto-char (point-min))
940 (while (< (point) header-end)
941 (unless (looking-at "[ \t]\\|.*:\\|$")
942 (push-mark opoint)
943 (error "Invalid header line (maybe a continuation line lacks initial whitespace)"))
944 (forward-line 1)))
34153d76 945 (goto-char opoint)
fd59d131
EZ
946 (when mail-encode-mml
947 (mml-to-mime)
948 (setq mail-encode-mml nil))
20a8832d 949 (run-hooks 'mail-send-hook)
da5667c6 950 (message "Sending...")
20a8832d
RS
951 (funcall send-mail-function)
952 ;; Now perform actions on successful sending.
953 (while mail-send-actions
954 (condition-case nil
d2561f34
RS
955 (apply (car (car mail-send-actions))
956 (cdr (car mail-send-actions)))
20a8832d
RS
957 (error))
958 (setq mail-send-actions (cdr mail-send-actions)))
d2561f34 959 (message "Sending...done")
4e0c8650 960 ;; If buffer has no file, mark it as unmodified and delete auto-save.
d2561f34
RS
961 (if (not buffer-file-name)
962 (progn
963 (set-buffer-modified-p nil)
964 (delete-auto-save-file-if-necessary t))))))
56e5818f
SJ
965
966(defun mail-envelope-from ()
967 "Return the envelope mail address to use when sending mail.
968This function uses `mail-envelope-from'."
969 (if (eq mail-envelope-from 'header)
970 (nth 1 (mail-extract-address-components
971 (mail-fetch-field "From")))
972 mail-envelope-from))
5b6575b7
RS
973\f
974;; This does the real work of sending a message via sendmail.
975;; It is called via the variable send-mail-function.
20a8832d 976
ae54d287
KH
977;;;###autoload
978(defvar sendmail-coding-system nil
da241b5c 979 "*Coding system for encoding the outgoing mail.
b56a5ae0 980This has higher priority than the default `buffer-file-coding-system'
da241b5c
KH
981and `default-sendmail-coding-system',
982but lower priority than the local value of `buffer-file-coding-system'.
0b64a3da 983See also the function `select-message-coding-system'.")
da241b5c
KH
984
985;;;###autoload
986(defvar default-sendmail-coding-system 'iso-latin-1
265f03bc 987 "Default coding system for encoding the outgoing mail.
da241b5c
KH
988This variable is used only when `sendmail-coding-system' is nil.
989
27f667c0 990This variable is set/changed by the command `set-language-environment'.
da241b5c 991User should not set this variable manually,
27f667c0 992instead use `sendmail-coding-system' to get a constant encoding
da241b5c 993of outgoing mails regardless of the current language environment.
0b64a3da 994See also the function `select-message-coding-system'.")
ee0a4f29 995
1938b566 996(defun mail-insert-from-field ()
b8d747b9
KH
997 "Insert the \"From:\" field of a mail header.
998The style of the field is determined by the variable `mail-from-style'.
999This function does not perform RFC2047 encoding."
1938b566
RS
1000 (let* ((login user-mail-address)
1001 (fullname (user-full-name))
1002 (quote-fullname nil))
1003 (if (string-match "[^\0-\177]" fullname)
b8d747b9 1004 (setq quote-fullname t))
0b8ee421
CY
1005 (cond ((null mail-from-style)
1006 (insert "From: " login "\n"))
1007 ;; This is deprecated.
1008 ((eq mail-from-style 'system-default)
1009 nil)
1010 ((or (eq mail-from-style 'angles)
1011 (and (not (eq mail-from-style 'parens))
1012 ;; Use angles if no quoting is needed, or if
1013 ;; parens would need quoting too.
1014 (or (not (string-match "[^- !#-'*+/-9=?A-Z^-~]" fullname))
1015 (let ((tmp (concat fullname nil)))
1016 (while (string-match "([^()]*)" tmp)
1017 (aset tmp (match-beginning 0) ?-)
1018 (aset tmp (1- (match-end 0)) ?-))
1019 (string-match "[\\()]" tmp)))))
1938b566
RS
1020 (insert "From: " fullname)
1021 (let ((fullname-start (+ (point-min) 6))
1022 (fullname-end (point-marker)))
1023 (goto-char fullname-start)
1024 ;; Look for a character that cannot appear unquoted
1025 ;; according to RFC 822.
1026 (if (or (re-search-forward "[^- !#-'*+/-9=?A-Z^-~]"
1027 fullname-end 1)
1028 quote-fullname)
1029 (progn
1030 ;; Quote fullname, escaping specials.
1031 (goto-char fullname-start)
1032 (insert "\"")
1033 (while (re-search-forward "[\"\\]"
1034 fullname-end 1)
1035 (replace-match "\\\\\\&" t))
1036 (insert "\""))))
1037 (insert " <" login ">\n"))
0b8ee421
CY
1038 ;; 'parens or default
1039 (t
1938b566
RS
1040 (insert "From: " login " (")
1041 (let ((fullname-start (point)))
1042 (if quote-fullname
1043 (insert "\""))
1044 (insert fullname)
1045 (if quote-fullname
1046 (insert "\""))
1047 (let ((fullname-end (point-marker)))
1048 (goto-char fullname-start)
1049 ;; RFC 822 says \ and nonmatching parentheses
1050 ;; must be escaped in comments.
1051 ;; Escape every instance of ()\ ...
1052 (while (re-search-forward "[()\\]" fullname-end 1)
1053 (replace-match "\\\\\\&" t))
1054 ;; ... then undo escaping of matching parentheses,
1055 ;; including matching nested parentheses.
1056 (goto-char fullname-start)
1057 (while (re-search-forward
1058 "\\(\\=\\|[^\\]\\(\\\\\\\\\\)*\\)\\\\(\\(\\([^\\]\\|\\\\\\\\\\)*\\)\\\\)"
1059 fullname-end 1)
1060 (replace-match "\\1(\\3)" t)
1061 (goto-char fullname-start))))
0b8ee421 1062 (insert ")\n")))))
1938b566 1063
b8d747b9
KH
1064(defun mail-encode-header (beg end)
1065 "Encode the mail header between BEG and END according to RFC2047.
1066Return non-nil if and only if some part of the header is encoded."
1067 (save-restriction
1068 (narrow-to-region beg end)
1069 (let* ((selected (select-message-coding-system))
1070 (mm-coding-system-priorities
1071 (if (and selected (coding-system-get selected :mime-charset))
1072 (cons selected mm-coding-system-priorities)
1073 mm-coding-system-priorities))
c92a1e54
KH
1074 (tick (buffer-chars-modified-tick))
1075 (rfc2047-encode-encoded-words nil))
b8d747b9
KH
1076 (rfc2047-encode-message-header)
1077 (= tick (buffer-chars-modified-tick)))))
1078
78205dab
SS
1079;; Normally you will not need to modify these options unless you are
1080;; using some non-genuine substitute for sendmail which does not
1081;; implement each and every option that the original supports.
1082;; E.g., ssmtp does not support "-odb", so, if your site uses it,
1083;; you will need to modify `sendmail-error-reporting-non-interactive'
1084;; in your site-init.el.
1085(defvar sendmail-error-reporting-interactive
1086 ;; These mean "report errors to terminal" and "deliver interactively"
1087 '("-oep" "-odi"))
1088(defvar sendmail-error-reporting-non-interactive
1089 ;; These mean "report errors by mail" and "deliver in background".
1090 '("-oem" "-odb"))
1091
20a8832d 1092(defun sendmail-send-it ()
67ad9438
DL
1093 "Send the current mail buffer using the Sendmail package.
1094This is a suitable value for `send-mail-function'. It sends using the
1095external program defined by `sendmail-program'."
8e183d5b 1096 (require 'mail-utils)
20a8832d
RS
1097 (let ((errbuf (if mail-interactive
1098 (generate-new-buffer " sendmail errors")
538b9847 1099 0))
20a8832d 1100 (tembuf (generate-new-buffer " sendmail temp"))
c86237b2 1101 (multibyte enable-multibyte-characters)
20a8832d 1102 (case-fold-search nil)
a80d7290 1103 (selected-coding (select-message-coding-system))
3ca0be33 1104 resend-to-addresses
20a8832d 1105 delimline
00365e98 1106 fcc-was-found
3ff0518e 1107 (mailbuf (current-buffer))
00417fac
RS
1108 ;; Examine these variables now, so that
1109 ;; local binding in the mail buffer will take effect.
1110 (envelope-from
1111 (and mail-specify-envelope-from
56e5818f 1112 (or (mail-envelope-from) user-mail-address))))
20a8832d 1113 (unwind-protect
95fe848e 1114 (with-current-buffer tembuf
20a8832d 1115 (erase-buffer)
c86237b2
RS
1116 (unless multibyte
1117 (set-buffer-multibyte nil))
20a8832d 1118 (insert-buffer-substring mailbuf)
b8d747b9 1119 (set-buffer-file-coding-system selected-coding)
20a8832d
RS
1120 (goto-char (point-max))
1121 ;; require one newline at the end.
1122 (or (= (preceding-char) ?\n)
1123 (insert ?\n))
1124 ;; Change header-delimiter to be what sendmail expects.
51d541d4
RS
1125 (goto-char (mail-header-end))
1126 (delete-region (point) (progn (end-of-line) (point)))
20a8832d 1127 (setq delimline (point-marker))
1498db18 1128 (sendmail-sync-aliases)
5ef9d627
RS
1129 (if mail-aliases
1130 (expand-mail-aliases (point-min) delimline))
20a8832d 1131 (goto-char (point-min))
413d1aa2 1132 ;; Ignore any blank lines in the header
20a8832d
RS
1133 (while (and (re-search-forward "\n\n\n*" delimline t)
1134 (< (point) delimline))
1135 (replace-match "\n"))
413d1aa2 1136 (goto-char (point-min))
3ca0be33
RS
1137 ;; Look for Resent- headers. They require sending
1138 ;; the message specially.
20a8832d 1139 (let ((case-fold-search t))
3ca0be33
RS
1140 (goto-char (point-min))
1141 (while (re-search-forward "^Resent-\\(to\\|cc\\|bcc\\):" delimline t)
1142 ;; Put a list of such addresses in resend-to-addresses.
1143 (setq resend-to-addresses
1144 (save-restriction
1145 (narrow-to-region (point)
1146 (save-excursion
1147 (forward-line 1)
1148 (while (looking-at "^[ \t]")
1149 (forward-line 1))
1150 (point)))
1151 (append (mail-parse-comma-list)
1152 resend-to-addresses)))
1153 ;; Delete Resent-BCC ourselves
1154 (if (save-excursion (beginning-of-line)
1155 (looking-at "resent-bcc"))
1f7f45a5
SM
1156 (delete-region (line-beginning-position)
1157 (line-beginning-position 2))))
1158 ;; Apparently this causes a duplicate Sender.
1159 ;; ;; If the From is different than current user, insert Sender.
1160 ;; (goto-char (point-min))
1161 ;; (and (re-search-forward "^From:" delimline t)
1162 ;; (progn
1163 ;; (require 'mail-utils)
1164 ;; (not (string-equal
1165 ;; (mail-strip-quoted-names
1166 ;; (save-restriction
1167 ;; (narrow-to-region (point-min) delimline)
1168 ;; (mail-fetch-field "From")))
1169 ;; (user-login-name))))
1170 ;; (progn
1171 ;; (forward-line 1)
1172 ;; (insert "Sender: " (user-login-name) "\n")))
20a8832d
RS
1173 ;; Don't send out a blank subject line
1174 (goto-char (point-min))
66961910 1175 (if (re-search-forward "^Subject:\\([ \t]*\n\\)+\\b" delimline t)
54115447
RS
1176 (replace-match "")
1177 ;; This one matches a Subject just before the header delimiter.
1178 (if (and (re-search-forward "^Subject:\\([ \t]*\n\\)+" delimline t)
1179 (= (match-end 0) delimline))
1180 (replace-match "")))
ea9ceb34
KH
1181 ;; Put the "From:" field in unless for some odd reason
1182 ;; they put one in themselves.
1183 (goto-char (point-min))
1184 (if (not (re-search-forward "^From:" delimline t))
1938b566 1185 (mail-insert-from-field))
baaa1f19 1186 ;; Possibly add a MIME header for the current coding system
caf9510c
RS
1187 (let (charset where-content-type)
1188 (goto-char (point-min))
1189 (setq where-content-type
1190 (re-search-forward "^Content-type:" delimline t))
baaa1f19
GM
1191 (goto-char (point-min))
1192 (and (eq mail-send-nonascii 'mime)
1193 (not (re-search-forward "^MIME-version:" delimline t))
1194 (progn (skip-chars-forward "\0-\177")
1195 (/= (point) (point-max)))
a80d7290 1196 selected-coding
baaa1f19 1197 (setq charset
3724c600 1198 (coding-system-get selected-coding :mime-charset))
caf9510c
RS
1199 (progn
1200 (goto-char delimline)
1201 (insert "MIME-version: 1.0\n"
1202 "Content-type: text/plain; charset="
1203 (symbol-name charset)
1204 "\nContent-Transfer-Encoding: 8bit\n")
1205 ;; The character set we will actually use
1206 ;; should override any specified in the message itself.
1207 (when where-content-type
1208 (goto-char where-content-type)
9b026d9f 1209 (delete-region (point-at-bol)
caf9510c 1210 (progn (forward-line 1) (point)))))))
7c203051
RS
1211 ;; Insert an extra newline if we need it to work around
1212 ;; Sun's bug that swallows newlines.
1213 (goto-char (1+ delimline))
1214 (if (eval mail-mailer-swallows-blank-line)
1215 (newline))
d6e000df
RS
1216 ;; Find and handle any FCC fields.
1217 (goto-char (point-min))
1218 (if (re-search-forward "^FCC:" delimline t)
00365e98
RS
1219 (progn
1220 (setq fcc-was-found t)
1221 (mail-do-fcc delimline)))
20a8832d 1222 (if mail-interactive
95fe848e 1223 (with-current-buffer errbuf
20a8832d 1224 (erase-buffer))))
b8d747b9
KH
1225 ;; Encode the header according to RFC2047.
1226 (mail-encode-header (point-min) delimline)
00365e98
RS
1227 (goto-char (point-min))
1228 (if (let ((case-fold-search t))
3ca0be33
RS
1229 (or resend-to-addresses
1230 (re-search-forward "^To:\\|^cc:\\|^bcc:"
1231 delimline t)))
63b89616 1232 (let* ((default-directory "/")
a80d7290 1233 (coding-system-for-write selected-coding)
a1506d29 1234 (args
63b89616 1235 (append (list (point-min) (point-max)
8340026c 1236 sendmail-program
63b89616 1237 nil errbuf nil "-oi")
00417fac
RS
1238 (and envelope-from
1239 (list "-f" envelope-from))
1f7f45a5
SM
1240 ;; ;; Don't say "from root" if running under su.
1241 ;; (and (equal (user-real-login-name) "root")
1242 ;; (list "-f" (user-login-name)))
63b89616
KH
1243 (and mail-alias-file
1244 (list (concat "-oA" mail-alias-file)))
1245 (if mail-interactive
78205dab
SS
1246 sendmail-error-reporting-interactive
1247 sendmail-error-reporting-non-interactive)
3ca0be33
RS
1248 ;; Get the addresses from the message
1249 ;; unless this is a resend.
1250 ;; We must not do that for a resend
1251 ;; because we would find the original addresses.
1252 ;; For a resend, include the specific addresses.
1253 (or resend-to-addresses
180d5509 1254 '("-t")
3ca0be33 1255 )
0b0b4ce7
RS
1256 (if mail-use-dsn
1257 (list "-N" (mapconcat 'symbol-name
1258 mail-use-dsn ",")))
180d5509
GM
1259 )
1260 )
63b89616 1261 (exit-value (apply 'call-process-region args)))
0d0ff7bc
GM
1262 (cond ((or (null exit-value) (eq 0 exit-value)))
1263 ((numberp exit-value)
1264 (error "Sending...failed with exit value %d" exit-value))
1265 ((stringp exit-value)
1266 (error "Sending...terminated by signal: %s" exit-value))
1267 (t
1268 (error "SENDMAIL-SEND-IT -- fall through: %S" exit-value))))
00365e98
RS
1269 (or fcc-was-found
1270 (error "No recipients")))
20a8832d 1271 (if mail-interactive
95fe848e 1272 (with-current-buffer errbuf
20a8832d
RS
1273 (goto-char (point-min))
1274 (while (re-search-forward "\n\n* *" nil t)
1275 (replace-match "; "))
1276 (if (not (zerop (buffer-size)))
1277 (error "Sending...failed to %s"
1278 (buffer-substring (point-min) (point-max)))))))
1279 (kill-buffer tembuf)
1280 (if (bufferp errbuf)
1281 (kill-buffer errbuf)))))
1282
04ca017c
GM
1283(autoload 'rmail-output-to-rmail-buffer "rmailout")
1284
20a8832d 1285(defun mail-do-fcc (header-end)
04ca017c
GM
1286 "Find and act on any FCC: headers in the current message before HEADER-END.
1287If a buffer is visiting the FCC file, append to it before
1288offering to save it, if it was modified initially. If this is an
1289Rmail buffer, update Rmail as needed. If there is no buffer,
1290just append to the file, in Babyl format if necessary."
95fe848e
SM
1291 (unless (markerp header-end)
1292 (error "Value of `header-end' must be a marker"))
20a8832d 1293 (let (fcc-list
04ca017c
GM
1294 (mailbuf (current-buffer))
1295 (time (current-time)))
20a8832d
RS
1296 (save-excursion
1297 (goto-char (point-min))
04ca017c
GM
1298 (let ((case-fold-search t))
1299 (while (re-search-forward "^FCC:[ \t]*" header-end t)
1300 (push (buffer-substring (point)
1301 (progn
1302 (end-of-line)
1303 (skip-chars-backward " \t")
1304 (point)))
1305 fcc-list)
1306 (delete-region (match-beginning 0)
1307 (progn (forward-line 1) (point)))))
1308 (with-temp-buffer
1309 ;; This initial newline is not written out if we create a new
1310 ;; file (see below).
1311 (insert "\nFrom " (user-login-name) " " (current-time-string time) "\n")
1312 ;; Insert the time zone before the year.
1313 (forward-char -1)
1314 (forward-word -1)
1315 (require 'mail-utils)
1316 (insert (mail-rfc822-time-zone time) " ")
1317 (goto-char (point-max))
1318 (insert-buffer-substring mailbuf)
1319 ;; Make sure messages are separated.
1320 (goto-char (point-max))
1321 (insert ?\n)
1322 (goto-char 2)
1323 ;; ``Quote'' "^From " as ">From "
1324 ;; (note that this isn't really quoting, as there is no requirement
1325 ;; that "^[>]+From " be quoted in the same transparent way.)
1326 (let ((case-fold-search nil))
1327 (while (search-forward "\nFrom " nil t)
1328 (forward-char -5)
1329 (insert ?>)))
1330 (dolist (fcc fcc-list)
1331 (let* ((buffer (find-buffer-visiting fcc))
1332 (curbuf (current-buffer))
1333 dont-write-the-file
1334 buffer-matches-file
1335 (beg (point-min)) ; the initial blank line
1336 (end (point-max))
1337 ;; After the ^From line.
1338 (beg2 (save-excursion (goto-char (point-min))
1339 (forward-line 2) (point))))
1340 (if buffer
1341 ;; File is present in a buffer => append to that buffer.
1342 (with-current-buffer buffer
1343 (setq buffer-matches-file
1344 (and (not (buffer-modified-p))
1345 (verify-visited-file-modtime buffer)))
1346 (let ((msg (bound-and-true-p rmail-current-message))
1347 (buffer-read-only nil))
1348 ;; If MSG is non-nil, buffer is in Rmail mode.
1349 (if msg
1350 (let ((buff (generate-new-buffer " *mail-do-fcc")))
1351 (unwind-protect
1352 (progn
1353 (with-current-buffer buff
1354 (insert-buffer-substring curbuf (1+ beg) end))
1355 (rmail-output-to-rmail-buffer buff msg))
1356 (kill-buffer buff)))
1357 ;; Output file not in Rmail mode => just insert
1358 ;; at the end.
1359 (save-restriction
1360 (widen)
1361 (goto-char (point-max))
1362 (insert-buffer-substring curbuf beg end)))
1363 ;; Offer to save the buffer if it was modified
1364 ;; before we started.
1365 (unless buffer-matches-file
1366 (if (y-or-n-p (format "Save file %s? " fcc))
1367 (save-buffer))
1368 (setq dont-write-the-file t)))))
1369 ;; Append to the file directly, unless we've already taken
1370 ;; care of it.
1371 (unless dont-write-the-file
1372 (if (and (file-exists-p fcc)
1373 (mail-file-babyl-p fcc))
1374 ;; If the file is a Babyl file, convert the message to
1375 ;; Babyl format. Even though Rmail no longer uses
1376 ;; Babyl, this code can remain for the time being, on
1377 ;; the off-chance one FCCs to a Babyl file that has
1378 ;; not yet been converted to mbox.
1379 (let ((coding-system-for-write
1380 (or rmail-file-coding-system 'emacs-mule)))
1381 (with-temp-buffer
1382 (insert "\C-l\n0, unseen,,\n*** EOOH ***\nDate: "
1383 (mail-rfc822-date) "\n")
1384 (insert-buffer-substring curbuf beg2 end)
1385 (insert "\n\C-_")
1386 (write-region (point-min) (point-max) fcc t)))
1387 ;; Ensure there is a blank line between messages, but
1388 ;; not at the very start of the file.
1389 (write-region (if (file-exists-p fcc)
1390 (point-min)
1391 (1+ (point-min)))
1392 (point-max) fcc t)))
1393 (and buffer (not dont-write-the-file)
1394 (with-current-buffer buffer
1395 (set-visited-file-modtime)))))))))
20a8832d
RS
1396
1397(defun mail-sent-via ()
1398 "Make a Sent-via header line from each To or CC header line."
1399 (interactive)
1400 (save-excursion
20a8832d 1401 ;; put a marker at the end of the header
81f5ed32 1402 (let ((end (copy-marker (mail-header-end)))
95fe848e 1403 (case-fold-search t))
20a8832d
RS
1404 (goto-char (point-min))
1405 ;; search for the To: lines and make Sent-via: lines from them
1406 ;; search for the next To: line
1407 (while (re-search-forward "^\\(to\\|cc\\):" end t)
1408 ;; Grab this line plus all its continuations, sans the `to:'.
1409 (let ((to-line
1410 (buffer-substring (point)
1411 (progn
1412 (if (re-search-forward "^[^ \t\n]" end t)
1413 (backward-char 1)
1414 (goto-char end))
1415 (point)))))
1416 ;; Insert a copy, with altered header field name.
1417 (insert-before-markers "Sent-via:" to-line))))))
fb532fe8
GM
1418
1419(make-obsolete 'mail-sent-via "nobody can remember what it is for." "24.1")
1420
20a8832d
RS
1421\f
1422(defun mail-to ()
87ede499 1423 "Move point to end of To field, creating it if necessary."
20a8832d
RS
1424 (interactive)
1425 (expand-abbrev)
1426 (mail-position-on-field "To"))
1427
1428(defun mail-subject ()
87ede499 1429 "Move point to end of Subject field, creating it if necessary."
20a8832d
RS
1430 (interactive)
1431 (expand-abbrev)
1432 (mail-position-on-field "Subject"))
1433
1434(defun mail-cc ()
87ede499 1435 "Move point to end of CC field, creating it if necessary."
20a8832d
RS
1436 (interactive)
1437 (expand-abbrev)
1438 (or (mail-position-on-field "cc" t)
1439 (progn (mail-position-on-field "to")
1440 (insert "\nCC: "))))
1441
1442(defun mail-bcc ()
87ede499 1443 "Move point to end of BCC field, creating it if necessary."
20a8832d
RS
1444 (interactive)
1445 (expand-abbrev)
1446 (or (mail-position-on-field "bcc" t)
1447 (progn (mail-position-on-field "to")
1448 (insert "\nBCC: "))))
1449
5a31c2d2 1450(defun mail-fcc (folder)
70ee42f7 1451 "Add a new FCC field, with file name completion."
5a31c2d2 1452 (interactive "FFolder carbon copy: ")
70ee42f7
JB
1453 (expand-abbrev)
1454 (or (mail-position-on-field "fcc" t) ;Put new field after exiting FCC.
1455 (mail-position-on-field "to"))
5a31c2d2 1456 (insert "\nFCC: " folder))
70ee42f7 1457
a1506d29 1458(defun mail-reply-to ()
87ede499 1459 "Move point to end of Reply-To field, creating it if necessary."
3a3ffba4
RS
1460 (interactive)
1461 (expand-abbrev)
1462 (mail-position-on-field "Reply-To"))
1463
3ca0be33 1464(defun mail-mail-reply-to ()
87ede499 1465 "Move point to end of Mail-Reply-To field, creating it if necessary."
3ca0be33
RS
1466 (interactive)
1467 (expand-abbrev)
1468 (or (mail-position-on-field "mail-reply-to" t)
1469 (progn (mail-position-on-field "to")
1470 (insert "\nMail-Reply-To: "))))
1471
1472(defun mail-mail-followup-to ()
87ede499 1473 "Move point to end of Mail-Followup-To field, creating it if necessary."
3ca0be33
RS
1474 (interactive)
1475 (expand-abbrev)
1476 (or (mail-position-on-field "mail-followup-to" t)
1477 (progn (mail-position-on-field "to")
1478 (insert "\nMail-Followup-To: "))))
1479
20a8832d
RS
1480(defun mail-position-on-field (field &optional soft)
1481 (let (end
1482 (case-fold-search t))
51d541d4 1483 (setq end (mail-header-end))
20a8832d
RS
1484 (goto-char (point-min))
1485 (if (re-search-forward (concat "^" (regexp-quote field) ":") end t)
1486 (progn
1487 (re-search-forward "^[^ \t]" nil 'move)
1488 (beginning-of-line)
1489 (skip-chars-backward "\n")
1490 t)
1491 (or soft
1492 (progn (goto-char end)
08564963
JB
1493 (insert field ": \n")
1494 (skip-chars-backward "\n")))
20a8832d
RS
1495 nil)))
1496
1497(defun mail-text ()
5d838629 1498 "Move point to beginning of text field."
20a8832d 1499 (interactive)
e3b671fc 1500 (expand-abbrev)
51d541d4 1501 (goto-char (mail-text-start)))
20a8832d 1502\f
e04233aa 1503(defun mail-signature (&optional atpoint)
87ede499
GM
1504 "Sign letter with signature.
1505If the variable `mail-signature' is a string, inserts it.
1506If it is t or nil, inserts the contents of the file `mail-signature-file'.
1507Otherwise, evals `mail-signature'.
1508Prefix argument ATPOINT means insert at point rather than the end."
1509 (interactive "*P")
1510 ;; Test for an unreadable file here, before we delete trailing
1511 ;; whitespace, so that we don't modify the buffer needlessly.
1512 (if (and (memq mail-signature '(t nil))
1513 (not (file-readable-p mail-signature-file)))
32226619 1514 (if (called-interactively-p 'interactive)
87ede499
GM
1515 (message "The signature file `%s' could not be read"
1516 mail-signature-file))
1517 (save-excursion
1518 (unless atpoint
1519 (goto-char (point-max))
1520 ;; Delete trailing whitespace and blank lines.
1521 (skip-chars-backward " \t\n")
1522 (end-of-line)
0d021ccf 1523 (delete-region (point) (point-max)))
87ede499
GM
1524 (cond ((stringp mail-signature)
1525 (insert mail-signature))
1526 ((memq mail-signature '(t nil))
1527 (insert "\n\n-- \n")
1528 (insert-file-contents (expand-file-name mail-signature-file)))
1529 (t
1530 ;; FIXME add condition-case error handling?
1531 (eval mail-signature))))))
20a8832d
RS
1532
1533(defun mail-fill-yanked-message (&optional justifyp)
1534 "Fill the paragraphs of a message yanked into this one.
1535Numeric argument means justify as well."
1536 (interactive "P")
1537 (save-excursion
51d541d4 1538 (goto-char (mail-text-start))
20a8832d
RS
1539 (fill-individual-paragraphs (point)
1540 (point-max)
1541 justifyp
706e3d85 1542 mail-citation-prefix-regexp)))
20a8832d 1543
374bda6b 1544(defun mail-indent-citation ()
1c24b04a
RS
1545 "Modify text just inserted from a message to be cited.
1546The inserted text should be the region.
1547When this function returns, the region is again around the modified text.
1548
1549Normally, indent each nonblank line `mail-indentation-spaces' spaces.
1550However, if `mail-yank-prefix' is non-nil, insert that prefix on each line."
374bda6b 1551 (mail-yank-clear-headers (region-beginning) (region-end))
e39e74c0 1552 (if (null mail-yank-prefix)
374bda6b
RS
1553 (indent-rigidly (region-beginning) (region-end)
1554 mail-indentation-spaces)
e39e74c0 1555 (save-excursion
374bda6b
RS
1556 (let ((end (set-marker (make-marker) (region-end))))
1557 (goto-char (region-beginning))
1558 (while (< (point) end)
1559 (insert mail-yank-prefix)
1560 (forward-line 1))))))
1c24b04a 1561
20a8832d 1562(defun mail-yank-original (arg)
04ca017c 1563 "Insert the message being replied to, if any (in Rmail).
26adca1b 1564Puts point after the text and mark before.
20a8832d
RS
1565Normally, indents each nonblank line ARG spaces (default 3).
1566However, if `mail-yank-prefix' is non-nil, insert that prefix on each line.
1567
1568Just \\[universal-argument] as argument means don't indent, insert no prefix,
1569and don't delete any header fields."
1570 (interactive "P")
c8553837
RS
1571 (if mail-reply-action
1572 (let ((start (point))
9295137d
CY
1573 (original mail-reply-action)
1574 (omark (mark t)))
c8553837
RS
1575 (and (consp original) (eq (car original) 'insert-buffer)
1576 (setq original (nth 1 original)))
1577 (if (consp original)
9295137d
CY
1578 (progn
1579 ;; Call yank function, and set the mark if it doesn't.
1580 (apply (car original) (cdr original))
1581 (if (eq omark (mark t))
1582 (push-mark (point))))
1583 ;; If the original message is in another window in the same
1584 ;; frame, delete that window to save space.
c8553837 1585 (delete-windows-on original t)
c7c93bd9
RS
1586 (with-no-warnings
1587 ;; We really want this to set mark.
60c2df40
EZ
1588 (insert-buffer original)
1589 ;; If they yank the original text, the encoding of the
1590 ;; original message is a better default than
b56a5ae0 1591 ;; the default buffer-file-coding-system.
60c2df40
EZ
1592 (and (coding-system-equal
1593 (default-value 'buffer-file-coding-system)
1594 buffer-file-coding-system)
1595 (setq buffer-file-coding-system
1596 (coding-system-change-text-conversion
1597 buffer-file-coding-system
1598 (coding-system-base
1599 (with-current-buffer original
1600 buffer-file-coding-system))))))
75e4db34 1601 (set-text-properties (point) (mark t) nil))
20a8832d
RS
1602 (if (consp arg)
1603 nil
1c24b04a
RS
1604 (goto-char start)
1605 (let ((mail-indentation-spaces (if arg (prefix-numeric-value arg)
ac9f0310 1606 mail-indentation-spaces))
8761c2c4
RS
1607 ;; Avoid error in Transient Mark mode
1608 ;; on account of mark's being inactive.
ac9f0310 1609 (mark-even-if-inactive t))
e04233aa
GM
1610 (cond (mail-citation-hook
1611 ;; Bind mail-citation-header to the inserted
1612 ;; message's header.
1613 (let ((mail-citation-header
1614 (buffer-substring-no-properties
1615 start
1616 (save-excursion
1617 (save-restriction
1618 (narrow-to-region start (point-max))
1619 (goto-char start)
1620 (rfc822-goto-eoh)
1621 (point))))))
1622 (run-hooks 'mail-citation-hook)))
1623 (mail-yank-hooks
1624 (run-hooks 'mail-yank-hooks))
1625 (t
1626 (mail-indent-citation)))))
3c280f48
RS
1627 ;; This is like exchange-point-and-mark, but doesn't activate the mark.
1628 ;; It is cleaner to avoid activation, even though the command
1629 ;; loop would deactivate the mark because we inserted text.
1630 (goto-char (prog1 (mark t)
1631 (set-marker (mark-marker) (point) (current-buffer))))
20a8832d
RS
1632 (if (not (eolp)) (insert ?\n)))))
1633
1634(defun mail-yank-clear-headers (start end)
e39e74c0
RS
1635 (if (< end start)
1636 (let (temp)
1637 (setq temp start start end end temp)))
2b3a206d
RS
1638 (if mail-yank-ignored-headers
1639 (save-excursion
1640 (goto-char start)
1641 (if (search-forward "\n\n" end t)
1642 (save-restriction
1643 (narrow-to-region start (point))
1644 (goto-char start)
1645 (while (let ((case-fold-search t))
1646 (re-search-forward mail-yank-ignored-headers nil t))
1647 (beginning-of-line)
1648 (delete-region (point)
1649 (progn (re-search-forward "\n[^ \t]")
1650 (forward-char -1)
1651 (point)))))))))
e39e74c0
RS
1652
1653(defun mail-yank-region (arg)
1654 "Insert the selected region from the message being replied to.
1655Puts point after the text and mark before.
1656Normally, indents each nonblank line ARG spaces (default 3).
1657However, if `mail-yank-prefix' is non-nil, insert that prefix on each line.
1658
1659Just \\[universal-argument] as argument means don't indent, insert no prefix,
1660and don't delete any header fields."
1661 (interactive "P")
1662 (and (consp mail-reply-action)
1663 (eq (car mail-reply-action) 'insert-buffer)
75e4db34
RS
1664 (with-current-buffer (nth 1 mail-reply-action)
1665 (or (mark t)
1666 (error "No mark set: %S" (current-buffer))))
e39e74c0 1667 (let ((buffer (nth 1 mail-reply-action))
8761c2c4
RS
1668 (start (point))
1669 ;; Avoid error in Transient Mark mode
1670 ;; on account of mark's being inactive.
1671 (mark-even-if-inactive t))
e39e74c0
RS
1672 ;; Insert the citation text.
1673 (insert (with-current-buffer buffer
d6bccf09 1674 (buffer-substring-no-properties (point) (mark))))
e39e74c0
RS
1675 (push-mark start)
1676 ;; Indent or otherwise annotate the citation text.
1677 (if (consp arg)
1678 nil
1679 (let ((mail-indentation-spaces (if arg (prefix-numeric-value arg)
1680 mail-indentation-spaces)))
1681 (if mail-citation-hook
d746203c 1682 ;; Bind mail-citation-header to the original message's header.
d6bccf09
RS
1683 (let ((mail-citation-header
1684 (with-current-buffer buffer
1685 (buffer-substring-no-properties
1686 (point-min)
1687 (save-excursion
1688 (goto-char (point-min))
1689 (rfc822-goto-eoh)
1690 (point))))))
1691 (run-hooks 'mail-citation-hook))
e39e74c0
RS
1692 (if mail-yank-hooks
1693 (run-hooks 'mail-yank-hooks)
374bda6b 1694 (mail-indent-citation))))))))
8ba7cd40
KS
1695
1696(defun mail-split-line ()
1697 "Split current line, moving portion beyond point vertically down.
1698If the current line has `mail-yank-prefix', insert it on the new line."
1699 (interactive "*")
1700 (split-line mail-yank-prefix))
1701
20a8832d 1702\f
c3760c17 1703(defun mail-insert-file (&optional file)
f0aef6bf
RS
1704 "Insert a file at the end of the buffer, with separator lines around it."
1705 (interactive "fAttach file: ")
1706 (save-excursion
1707 (goto-char (point-max))
1708 (or (bolp) (newline))
1709 (newline)
1710 (let ((start (point))
1711 middle)
1712 (insert (format "===File %s===" file))
1713 (insert-char ?= (max 0 (- 60 (current-column))))
1714 (newline)
1715 (setq middle (point))
1716 (insert "============================================================\n")
1717 (push-mark)
1718 (goto-char middle)
1719 (insert-file-contents file)
1720 (or (bolp) (newline))
1721 (goto-char start))))
767d420a 1722
c3760c17
EZ
1723(define-obsolete-function-alias 'mail-attach-file 'mail-insert-file "24.1")
1724
1725(declare-function mml-attach-file "mml"
1726 (file &optional type description disposition))
1727(declare-function mm-default-file-encoding "mm-encode" (file))
1728
767d420a 1729(defun mail-add-attachment (file)
c3760c17 1730 "Add FILE as a MIME attachment to the end of the mail message being composed."
767d420a
EZ
1731 (interactive "fAttach file: ")
1732 (mml-attach-file file
1733 (or (mm-default-file-encoding file)
1734 "application/octet-stream") nil)
1735 (setq mail-encode-mml t))
1736
f0aef6bf 1737\f
699adcb2
RS
1738;; Put these commands last, to reduce chance of lossage from quitting
1739;; in middle of loading the file.
1740
20a8832d 1741;;;###autoload
25ca2e61
CY
1742(defun mail (&optional noerase to subject in-reply-to cc replybuffer
1743 actions return-action)
e635fdf0
RS
1744 "Edit a message to be sent. Prefix arg means resume editing (don't erase).
1745When this function returns, the buffer `*mail*' is selected.
1746The value is t if the message was newly initialized; otherwise, nil.
20a8832d 1747
4ac2e032
RS
1748Optionally, the signature file `mail-signature-file' can be inserted at the
1749end; see the variable `mail-signature'.
20a8832d
RS
1750
1751\\<mail-mode-map>
1752While editing message, type \\[mail-send-and-exit] to send the message and exit.
1753
1754Various special commands starting with C-c are available in sendmail mode
1755to move to message header fields:
1756\\{mail-mode-map}
1757
1758If `mail-self-blind' is non-nil, a BCC to yourself is inserted
1759when the message is initialized.
1760
1761If `mail-default-reply-to' is non-nil, it should be an address (a string);
1762a Reply-to: field with that address is inserted.
1763
1764If `mail-archive-file-name' is non-nil, an FCC field with that file name
1765is inserted.
1766
8b262cbc
KH
1767The normal hook `mail-setup-hook' is run after the message is
1768initialized. It can add more default fields to the message.
20a8832d 1769
1db2d450
CY
1770The first argument, NOERASE, determines what to do when there is
1771an existing modified `*mail*' buffer. If NOERASE is nil, the
1772existing mail buffer is used, and the user is prompted whether to
1773keep the old contents or to erase them. If NOERASE has the value
1774`new', a new mail buffer will be created instead of using the old
1775one. Any other non-nil value means to always select the old
1776buffer without erasing the contents.
dfdac213
RS
1777
1778The second through fifth arguments,
1779 TO, SUBJECT, IN-REPLY-TO and CC, specify if non-nil
20a8832d
RS
1780 the initial contents of those header fields.
1781 These arguments should not have final newlines.
c8553837
RS
1782The sixth argument REPLYBUFFER is a buffer which contains an
1783 original message being replied to, or else an action
1784 of the form (FUNCTION . ARGS) which says how to insert the original.
1785 Or it can be nil, if not replying to anything.
20a8832d
RS
1786The seventh argument ACTIONS is a list of actions to take
1787 if/when the message is sent. Each action looks like (FUNCTION . ARGS);
1788 when the message is sent, we apply FUNCTION to ARGS.
1789 This is how Rmail arranges to mark messages `answered'."
1790 (interactive "P")
1db2d450
CY
1791 (if (eq noerase 'new)
1792 (pop-to-buffer (generate-new-buffer "*mail*"))
09c9e6bb
CY
1793 (and noerase
1794 (not (get-buffer "*mail*"))
1795 (setq noerase nil))
1db2d450
CY
1796 (pop-to-buffer "*mail*"))
1797
eb1213c1
MR
1798 ;; Avoid danger that the auto-save file can't be written.
1799 (let ((dir (expand-file-name
1800 (file-name-as-directory mail-default-directory))))
1801 (if (file-exists-p dir)
1802 (setq default-directory dir)))
2e78575e
RS
1803 ;; Only call auto-save-mode if necessary, to avoid changing auto-save file.
1804 (if (or (and auto-save-default (not buffer-auto-save-file-name))
1805 (and (not auto-save-default) buffer-auto-save-file-name))
1806 (auto-save-mode auto-save-default))
e635fdf0 1807 (mail-mode)
3b112b5e
MB
1808 ;; Disconnect the buffer from its visited file
1809 ;; (in case the user has actually visited a file *mail*).
1f7f45a5 1810 ;; (set-visited-file-name nil)
e635fdf0 1811 (let (initialized)
09c9e6bb
CY
1812 (and (not (and noerase
1813 (not (eq noerase 'new))))
5e32e119
RS
1814 (if buffer-file-name
1815 (if (buffer-modified-p)
1816 (when (y-or-n-p "Buffer has unsaved changes; reinitialize it and discard them? ")
1817 (if (y-or-n-p "Disconnect buffer from visited file? ")
1818 (set-visited-file-name nil))
1819 t)
1820 (when (y-or-n-p "Reinitialize buffer, and disconnect it from the visited file? ")
1821 (set-visited-file-name nil)
1822 t))
1823 ;; A non-file-visiting buffer.
1824 (if (buffer-modified-p)
1825 (y-or-n-p "Unsent message being composed; erase it? ")
1826 t))
6737ae74
RS
1827 (let ((inhibit-read-only t))
1828 (erase-buffer)
25ca2e61
CY
1829 (mail-setup to subject in-reply-to cc replybuffer actions
1830 return-action)
6737ae74 1831 (setq initialized t)))
e635fdf0
RS
1832 (if (and buffer-auto-save-file-name
1833 (file-exists-p buffer-auto-save-file-name))
1834 (message "Auto save file for draft message exists; consider M-x mail-recover"))
1835 initialized))
20a8832d 1836
73e72da4
DN
1837(declare-function dired-view-file "dired" ())
1838(declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
1839
6cd4cfc5 1840(defun mail-recover-1 ()
918c7db7 1841 "Pop up a list of auto-saved draft messages so you can recover one of them."
6cd4cfc5
EZ
1842 (interactive)
1843 (let ((file-name (make-auto-save-file-name))
1844 (ls-lisp-support-shell-wildcards t)
1845 non-random-len wildcard)
1846 ;; Remove the random part from the auto-save-file-name, and
1847 ;; create a wildcard which matches possible candidates.
1848 ;; Note: this knows that make-auto-save-file-name appends
1849 ;; "#<RANDOM-STUFF>#" to the buffer name, where RANDOM-STUFF
1850 ;; is the result of (make-temp-name "").
1851 (setq non-random-len
d41eb0aa 1852 (- (length file-name) (length (make-temp-name "")) 1))
6cd4cfc5
EZ
1853 (setq wildcard (concat (substring file-name 0 non-random-len) "*"))
1854 (if (null (file-expand-wildcards wildcard))
1855 (message "There are no auto-saved drafts to recover")
1856 ;; Bind dired-trivial-filenames to t because all auto-save file
1857 ;; names are normally ``trivial'', so Dired will set point after
1858 ;; all the files, at buffer bottom. We want it on the first
1859 ;; file instead.
aae4b48c
GM
1860 ;; Require dired so that dired-trivial-filenames does not get
1861 ;; unbound on exit from the let.
1862 (require 'dired)
6cd4cfc5 1863 (let ((dired-trivial-filenames t))
254c37a5 1864 (dired-other-window wildcard (concat dired-listing-switches " -t")))
6cd4cfc5
EZ
1865 (rename-buffer "*Auto-saved Drafts*" t)
1866 (save-excursion
1867 (goto-char (point-min))
1868 (or (looking-at " Move to the draft file you want to recover,")
1869 (let ((inhibit-read-only t))
1870 ;; Each line starts with a space so that Font Lock mode
1871 ;; won't highlight the first character.
1872 (insert "\
1873 Move to the draft file you want to recover, then type C-c C-c
1874 to recover text of message whose composition was interrupted.
1875 To browse text of a draft, type v on the draft file's line.
1876
1877 You can also delete some of these files;
1878 type d on a line to mark that file for deletion.
1879
1880 List of possible auto-save files for recovery:
1881
1882"))))
1883 (use-local-map
1884 (let ((map (make-sparse-keymap)))
1885 (set-keymap-parent map (current-local-map))
1886 map))
1887 (define-key (current-local-map) "v"
1888 (lambda ()
1889 (interactive)
7f04a67e 1890 (let ((coding-system-for-read 'utf-8-emacs-unix))
6cd4cfc5
EZ
1891 (dired-view-file))))
1892 (define-key (current-local-map) "\C-c\C-c"
1893 (lambda ()
1894 (interactive)
1895 (let ((fname (dired-get-filename))
1896 ;; Auto-saved files are written in the internal
1897 ;; representation, so they should be read accordingly.
7f04a67e 1898 (coding-system-for-read 'utf-8-emacs-unix))
6cd4cfc5
EZ
1899 (switch-to-buffer-other-window "*mail*")
1900 (let ((buffer-read-only nil))
1901 (erase-buffer)
1902 (insert-file-contents fname nil)
1903 ;; insert-file-contents will set buffer-file-coding-system
7f04a67e 1904 ;; to utf-8-emacs, which is probably not what they want to
6cd4cfc5
EZ
1905 ;; use for sending the message. But we don't know what
1906 ;; was its value before the buffer was killed or Emacs
1907 ;; crashed. We therefore reset buffer-file-coding-system
1908 ;; to the default value, so that either the default does
1909 ;; TRT, or the user will get prompted for the right
1910 ;; encoding when they send the message.
1911 (setq buffer-file-coding-system
b56a5ae0 1912 (default-value 'buffer-file-coding-system)))))))))
6cd4cfc5 1913
2b54af74
DN
1914(declare-function dired-move-to-filename "dired" (&optional raise-error eol))
1915(declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
1916(declare-function dired-view-file "dired" ())
1917
20a8832d 1918(defun mail-recover ()
918c7db7
EZ
1919 "Recover interrupted mail composition from auto-save files.
1920
1921If the mail buffer has a current valid auto-save file,
1922the command recovers that file. Otherwise, it displays a
1923buffer showing the existing auto-saved draft messages;
1924you can move to one of them and type C-c C-c to recover that one."
20a8832d 1925 (interactive)
6cd4cfc5
EZ
1926 ;; In case they invoke us from some random buffer...
1927 (switch-to-buffer "*mail*")
1928 ;; If *mail* didn't exist, set its directory, so that auto-saved
1929 ;; drafts will be found.
eb1213c1
MR
1930 (let ((dir (expand-file-name
1931 (file-name-as-directory mail-default-directory))))
1932 (if (file-exists-p dir)
1933 (setq default-directory dir)))
6cd4cfc5
EZ
1934 (or (eq major-mode 'mail-mode)
1935 (mail-mode))
1936 (let ((file-name buffer-auto-save-file-name))
1937 (cond ((and file-name (file-exists-p file-name))
1938 (let ((dispbuf
1939 ;; This used to invoke `ls' via call-process, but
1940 ;; dired-noselect is more portable to systems where
1941 ;; `ls' is not a standard program (it will use
1942 ;; ls-lisp instead).
1943 (dired-noselect file-name
254c37a5 1944 (concat dired-listing-switches " -t"))))
8366cfb5
RS
1945 (save-selected-window
1946 (select-window (display-buffer dispbuf t))
1947 (goto-char (point-min))
1948 (forward-line 2)
1949 (dired-move-to-filename)
1950 (setq dispbuf (rename-buffer "*Directory*" t)))
6cd4cfc5
EZ
1951 (if (not (yes-or-no-p
1952 (format "Recover mail draft from auto save file %s? "
1953 file-name)))
1954 (error "mail-recover cancelled")
1955 (let ((buffer-read-only nil)
1956 (buffer-coding buffer-file-coding-system)
1957 ;; Auto-save files are written in internal
1958 ;; representation of non-ASCII characters.
7f04a67e 1959 (coding-system-for-read 'utf-8-emacs-unix))
6cd4cfc5
EZ
1960 (erase-buffer)
1961 (insert-file-contents file-name nil)
1962 (setq buffer-file-coding-system buffer-coding)))))
1963 (t (mail-recover-1)))))
20a8832d
RS
1964
1965;;;###autoload
1966(defun mail-other-window (&optional noerase to subject in-reply-to cc replybuffer sendactions)
1967 "Like `mail' command, but display mail buffer in another window."
1968 (interactive "P")
67a6988b
RS
1969 (let ((pop-up-windows t)
1970 (special-display-buffer-names nil)
1971 (special-display-regexps nil)
1972 (same-window-buffer-names nil)
1973 (same-window-regexps nil))
20a8832d
RS
1974 (pop-to-buffer "*mail*"))
1975 (mail noerase to subject in-reply-to cc replybuffer sendactions))
1976
1977;;;###autoload
0cc89026
JB
1978(defun mail-other-frame (&optional noerase to subject in-reply-to cc replybuffer sendactions)
1979 "Like `mail' command, but display mail buffer in another frame."
20a8832d 1980 (interactive "P")
67a6988b
RS
1981 (let ((pop-up-frames t)
1982 (special-display-buffer-names nil)
1983 (special-display-regexps nil)
1984 (same-window-buffer-names nil)
1985 (same-window-regexps nil))
20a8832d
RS
1986 (pop-to-buffer "*mail*"))
1987 (mail noerase to subject in-reply-to cc replybuffer sendactions))
1988
95fe848e 1989;; Do not add anything but external entries on this page.
49116ac0
JB
1990
1991(provide 'sendmail)
1992
c88ab9ce 1993;;; sendmail.el ends here