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