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