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