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