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