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