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