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