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