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