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