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