1 ;;; sendmail.el --- mail sending commands for Emacs. -*- byte-compile-dynamic: t -*-
3 ;; Copyright (C) 1985, 86, 92, 93, 94, 95, 96, 98, 2000, 2001, 2002, 2003
4 ;; Free Software Foundation, Inc.
9 ;; This file is part of GNU Emacs.
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 2, or (at your option)
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.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
28 ;; This mode provides mail-sending facilities from within Emacs. It is
29 ;; documented in the Emacs user's manual.
33 ;; Necessary to avoid recursive `require's.
38 (autoload 'rfc2047-encode-string "rfc2047")
40 (defgroup sendmail nil
41 "Mail sending commands for Emacs."
46 (defcustom mail-from-style 'angles "\
47 *Specifies how \"From:\" fields look.
49 If `nil', they contain just the return address like:
51 If `parens', they look like:
52 king@grassland.com (Elvis Parsley)
53 If `angles', they look like:
54 Elvis Parsley <king@grassland.com>
55 If `system-default', allows the mailer to insert its default From field
56 derived from the envelope-from address.
58 In old versions of Emacs, the `system-default' setting also caused
59 Emacs to pass the proper email address from `user-mail-address'
60 to the mailer to specify the envelope-from address. But that is now
61 controlled by a separate variable, `mail-specify-envelope-from'."
62 :type '(choice (const nil) (const parens) (const angles)
63 (const system-default))
68 (defcustom mail-specify-envelope-from nil
69 "*If non-nil, specify the envelope-from address when sending mail.
70 The value used to specify it is whatever is found in
71 `mail-envelope-from', with `user-mail-address' as fallback.
73 On most systems, specifying the envelope-from address
74 is a privileged operation."
79 (defcustom mail-envelope-from nil
80 "*If non-nil, designate the envelope-from address when sending mail.
81 If this is nil while `mail-specify-envelope-from' is non-nil, the
82 content of `user-mail-address' is used."
84 :type '(choice (string :tag "From-name")
85 (const :tag "Use From: header from message" header)
86 (const :tag "Use `user-mail-address'" nil))
90 (defcustom mail-self-blind nil "\
91 *Non-nil means insert BCC to self in messages to be sent.
92 This is done when the message is initialized,
93 so you can remove or alter the BCC field to override the default."
98 (defcustom mail-interactive nil "\
99 *Non-nil means when sending a message wait for and display errors.
100 nil means let mailer mail back a message to report errors."
105 (defcustom mail-yank-ignored-headers "^via:\\|^mail-from:\\|^origin:\\|^status:\\|^remailed\\|^received:\\|^message-id:\\|^summary-line:\\|^to:\\|^subject:\\|^in-reply-to:\\|^return-path:" "\
106 *Delete these headers from old message when it's inserted in a reply."
110 ;; Useful to set in site-init.el
112 (defcustom send-mail-function 'sendmail-send-it
113 "Function to call to send the current buffer as mail.
114 The headers should be delimited by a line which is
115 not a valid RFC822 header or continuation line,
116 that matches the variable `mail-header-separator'.
117 This is used by the default mail-sending commands. See also
118 `message-send-mail-function' for use with the Message package."
119 :type '(radio (function-item sendmail-send-it :tag "Use Sendmail package")
120 (function-item smtpmail-send-it :tag "Use SMTPmail package")
121 (function-item feedmail-send-it :tag "Use Feedmail package")
126 (defcustom mail-header-separator "--text follows this line--" "\
127 *Line used to separate headers from text in messages being composed."
131 ;; Set up mail-header-separator for use as a category text property.
132 (put 'mail-header-separator 'rear-nonsticky '(category))
133 ;;; This was a nice idea, for preventing accidental modification of
134 ;;; the separator. But I found it also prevented or obstructed
135 ;;; certain deliberate operations, such as copying the separator line
136 ;;; up to the top to send myself a copy of an already sent outgoing message
137 ;;; and other things. So I turned it off. --rms.
138 ;;;(put 'mail-header-separator 'read-only t)
141 (defcustom mail-archive-file-name nil "\
142 *Name of file to write all outgoing messages in, or nil for none.
143 This can be an inbox file or an Rmail file."
144 :type '(choice file (const nil))
148 (defcustom mail-default-reply-to nil
149 "*Address to insert as default Reply-to field of outgoing messages.
150 If nil, it will be initialized from the REPLYTO environment variable
151 when you first send mail."
152 :type '(choice (const nil) string)
156 (defcustom mail-alias-file nil
157 "*If non-nil, the name of a file to use instead of `/usr/lib/aliases'.
158 This file defines aliases to be expanded by the mailer; this is a different
159 feature from that of defining aliases in `.mailrc' to be expanded in Emacs.
160 This variable has no effect unless your system uses sendmail as its mailer."
161 :type '(choice (const nil) file)
165 (defcustom mail-personal-alias-file "~/.mailrc"
166 "*If non-nil, the name of the user's personal mail alias file.
167 This file typically should be in same format as the `.mailrc' file used by
168 the `Mail' or `mailx' program.
169 This file need not actually exist."
170 :type '(choice (const nil) file)
173 (defcustom mail-setup-hook nil
174 "Normal hook, run each time a new outgoing mail message is initialized.
175 The function `mail-setup' runs this hook."
177 :options '(fortune-to-signature spook mail-abbrevs-setup)
180 (defvar mail-aliases t
181 "Alist of mail address aliases,
182 or t meaning should be initialized from your mail aliases file.
183 \(The file's name is normally `~/.mailrc', but your MAILRC environment
184 variable can override that name.)
185 The alias definitions in the file have this form:
186 alias ALIAS MEANING")
188 (defvar mail-alias-modtime nil
189 "The modification time of your mail alias file when it was last examined.")
191 (defcustom mail-yank-prefix nil
192 "*Prefix insert on lines of yanked message being replied to.
193 nil means use indentation."
194 :type '(choice (const nil) string)
197 (defcustom mail-indentation-spaces 3
198 "*Number of spaces to insert at the beginning of each cited line.
199 Used by `mail-yank-original' via `mail-indent-citation'."
202 (defvar mail-yank-hooks nil
203 "Obsolete hook for modifying a citation just inserted in the mail buffer.
204 Each hook function can find the citation between (point) and (mark t).
205 And each hook function should leave point and mark around the citation
208 This is a normal hook, misnamed for historical reasons.
209 It is semi-obsolete and mail agents should no longer use it.")
211 (defcustom mail-citation-hook nil
212 "*Hook for modifying a citation just inserted in the mail buffer.
213 Each hook function can find the citation between (point) and (mark t),
214 and should leave point and mark around the citation text as modified.
215 The hook functions can find the header of the cited message
216 in the variable `mail-citation-header', whether or not this is included
217 in the cited portion of the message.
219 If this hook is entirely empty (nil), a default action is taken
220 instead of no action."
224 (defvar mail-citation-header nil
225 "While running `mail-citation-hook', this variable holds the message header.
226 This enables the hook functions to see the whole message header
227 regardless of what part of it (if any) is included in the cited text.")
229 (defcustom mail-citation-prefix-regexp "[ \t]*[-a-z0-9A-Z]*>+[ \t]*\\|[ \t]*"
230 "*Regular expression to match a citation prefix plus whitespace.
231 It should match whatever sort of citation prefixes you want to handle,
232 with whitespace before and after; it should also match just whitespace.
233 The default value matches citations like `foo-bar>' plus whitespace."
238 (defvar mail-abbrevs-loaded nil)
239 (defvar mail-mode-map nil)
241 (autoload 'build-mail-aliases "mailalias"
242 "Read mail aliases from user's personal aliases file and set `mail-aliases'."
245 (autoload 'expand-mail-aliases "mailalias"
246 "Expand all mail aliases in suitable header fields found between BEG and END.
247 Suitable header fields are `To', `Cc' and `Bcc' and their `Resent-' variants.
248 Optional second arg EXCLUDE may be a regular expression defining text to be
249 removed from alias expansions."
253 (defcustom mail-signature nil
254 "*Text inserted at end of mail buffer when a message is initialized.
255 If t, it means to insert the contents of the file `mail-signature-file'.
256 If a string, that string is inserted.
257 (To make a proper signature, the string should begin with \\n\\n-- \\n,
258 which is the standard way to delimit a signature in a message.)
259 Otherwise, it should be an expression; it is evaluated
260 and should insert whatever you want to insert."
261 :type '(choice (const :tag "None" nil)
262 (const :tag "Use `.signature' file" t)
263 (string :tag "String to insert")
264 (sexp :tag "Expression to evaluate"))
266 (put 'mail-signature 'risky-local-variable t)
268 (defcustom mail-signature-file "~/.signature"
269 "*File containing the text inserted at end of mail buffer."
274 (defcustom mail-default-directory "~/"
275 "*Directory for mail buffers.
276 Value of `default-directory' for mail buffers.
277 This directory is used for auto-save files of mail buffers."
278 :type '(directory :tag "Directory")
282 (defvar mail-reply-action nil)
283 (defvar mail-send-actions nil
284 "A list of actions to be performed upon successful sending of a message.")
285 (put 'mail-reply-action 'permanent-local t)
286 (put 'mail-send-actions 'permanent-local t)
288 (defcustom mail-default-headers nil
289 "*A string containing header lines, to be inserted in outgoing messages.
290 It is inserted before you edit the message,
291 so you can edit or delete these lines."
292 :type '(choice (const nil) string)
295 (defcustom mail-bury-selects-summary t
296 "*If non-nil, try to show RMAIL summary buffer after returning from mail.
297 The functions \\[mail-send-on-exit] or \\[mail-dont-send] select
298 the RMAIL summary buffer before returning, if it exists and this variable
303 (defcustom mail-send-nonascii 'mime
304 "*Specify whether to allow sending non-ASCII characters in mail.
305 If t, that means do allow it. nil means don't allow it.
306 `query' means ask the user each time.
307 `mime' means add an appropriate MIME header if none already present.
308 The default is `mime'.
309 Including non-ASCII characters in a mail message can be problematical
310 for the recipient, who may not know how to decode them properly."
311 :type '(choice (const t) (const nil) (const query) (const mime))
314 (defcustom mail-use-dsn nil
315 "*Ask MTA for notification of failed, delayed or successful delivery.
316 Note that only some MTAs (currently only recent versions of Sendmail)
317 support Delivery Status Notification."
319 :type '(repeat (radio (const :tag "Failure" failure)
320 (const :tag "Delay" delay)
321 (const :tag "Success" success)))
324 ;; Note: could use /usr/ucb/mail instead of sendmail;
325 ;; options -t, and -v if not interactive.
326 (defvar mail-mailer-swallows-blank-line
327 (if (and (string-match "sparc-sun-sunos\\(\\'\\|[^5]\\)" system-configuration)
328 (file-readable-p "/etc/sendmail.cf")
329 (let ((buffer (get-buffer-create " *temp*")))
333 (insert-file-contents "/etc/sendmail.cf")
334 (goto-char (point-min))
335 (let ((case-fold-search nil))
336 (re-search-forward "^OR\\>" nil t)))
337 (kill-buffer buffer))))
338 ;; According to RFC822, "The field-name must be composed of printable
339 ;; ASCII characters (i.e. characters that have decimal values between
340 ;; 33 and 126, except colon)", i.e. any chars except ctl chars,
342 '(looking-at "[ \t]\\|[][!\"#$%&'()*+,-./0-9;<=>?@A-Z\\\\^_`a-z{|}~]+:"))
343 "Set this non-nil if the system's mailer runs the header and body together.
344 \(This problem exists on Sunos 4 when sendmail is run in remote mode.)
345 The value should be an expression to test whether the problem will
348 (defvar mail-mode-syntax-table
349 (let ((st (make-syntax-table)))
350 ;; define-derived-mode will make it inherit from text-mode-syntax-table.
351 (modify-syntax-entry ?% ". " st)
353 "Syntax table used while in `mail-mode'.")
355 (defvar mail-font-lock-keywords
357 (let* ((cite-chars "[>|}]")
358 (cite-prefix "[:alpha:]")
359 (cite-suffix (concat cite-prefix "0-9_.@-`'\"")))
360 (list '("^\\(To\\|Newsgroups\\):" . font-lock-function-name-face)
361 '("^\\(B?CC\\|Reply-to\\):" . font-lock-keyword-face)
362 '("^\\(Subject:\\)[ \t]*\\(.+\\)?"
363 (1 font-lock-comment-face) (2 font-lock-type-face nil t))
364 ;; Use EVAL to delay in case `mail-header-separator' gets changed.
366 (let ((separator (if (zerop (length mail-header-separator))
368 (regexp-quote mail-header-separator))))
369 (cons (concat "^" separator "$") 'font-lock-warning-face)))
370 ;; Use MATCH-ANCHORED to effectively anchor the regexp left side.
372 (,(concat "\\=[ \t]*"
373 "\\(\\([" cite-prefix "]+[" cite-suffix "]*\\)?"
374 "\\(" cite-chars "[ \t]*\\)\\)+"
376 (beginning-of-line) (end-of-line)
377 (2 font-lock-constant-face nil t)
378 (4 font-lock-comment-face nil t)))
379 '("^\\(X-[A-Za-z0-9-]+\\|In-reply-to\\):.*\\(\n[ \t]+.*\\)*$"
380 . font-lock-string-face))))
381 "Additional expressions to highlight in Mail mode.")
384 (defun sendmail-sync-aliases ()
385 (let ((modtime (nth 5 (file-attributes mail-personal-alias-file))))
386 (or (equal mail-alias-modtime modtime)
387 (setq mail-alias-modtime modtime
390 (defun mail-setup (to subject in-reply-to cc replybuffer actions)
391 (or mail-default-reply-to
392 (setq mail-default-reply-to (getenv "REPLYTO")))
393 (sendmail-sync-aliases)
394 (if (eq mail-aliases t)
396 (setq mail-aliases nil)
397 (if (file-exists-p mail-personal-alias-file)
398 (build-mail-aliases))))
399 ;; Don't leave this around from a previous message.
400 (kill-local-variable 'buffer-file-coding-system)
401 ;; This doesn't work for enable-multibyte-characters.
402 ;; (kill-local-variable 'enable-multibyte-characters)
403 (set-buffer-multibyte default-enable-multibyte-characters)
404 (if current-input-method
405 (inactivate-input-method))
406 (setq mail-send-actions actions)
407 (setq mail-reply-action replybuffer)
408 (goto-char (point-min))
412 ;; Here removed code to extract names from within <...>
413 ;; on the assumption that mail-strip-quoted-names
414 ;; has been called and has done so.
415 (let ((fill-prefix "\t")
416 (address-start (point)))
418 (fill-region-as-paragraph address-start (point-max))
419 (goto-char (point-max))
424 (let ((fill-prefix "\t")
425 (address-start (progn (insert "CC: ") (point))))
427 (fill-region-as-paragraph address-start (point-max))
428 (goto-char (point-max))
432 (let ((fill-prefix "\t")
434 (address-start (point)))
435 (insert "In-reply-to: " in-reply-to "\n")
436 (fill-region-as-paragraph address-start (point-max))
437 (goto-char (point-max))
440 (insert "Subject: " (or subject "") "\n")
441 (if mail-default-headers
442 (insert mail-default-headers))
443 (if mail-default-reply-to
444 (insert "Reply-to: " mail-default-reply-to "\n"))
446 (insert "BCC: " user-mail-address "\n"))
447 (if mail-archive-file-name
448 (insert "FCC: " mail-archive-file-name "\n"))
449 (put-text-property (point)
451 (insert mail-header-separator "\n")
453 'category 'mail-header-separator)
454 ;; Insert the signature. But remember the beginning of the message.
455 (if to (setq to (point)))
456 (cond ((eq mail-signature t)
457 (if (file-exists-p mail-signature-file)
460 (insert-file-contents mail-signature-file))))
461 ((stringp mail-signature)
462 (insert mail-signature))
464 (eval mail-signature)))
465 (goto-char (point-max))
466 (or (bolp) (newline)))
467 (if to (goto-char to))
468 (or to subject in-reply-to
469 (set-buffer-modified-p nil))
470 (run-hooks 'mail-setup-hook))
472 (defcustom mail-mode-hook nil
473 "Hook run by Mail mode."
476 :options '(footnote-mode))
478 (defvar mail-mode-abbrev-table text-mode-abbrev-table)
480 (define-derived-mode mail-mode text-mode "Mail"
481 "Major mode for editing mail to be sent.
482 Like Text Mode but with these additional commands:
483 \\[mail-send] mail-send (send the message) \\[mail-send-and-exit] mail-send-and-exit
484 Here are commands that move to a header field (and create it if there isn't):
485 \\[mail-to] move to To: \\[mail-subject] move to Subject:
486 \\[mail-cc] move to CC: \\[mail-bcc] move to BCC:
487 \\[mail-fcc] move to FCC: \\[mail-reply-to] move to Reply-To:
488 \\[mail-text] mail-text (move to beginning of message text).
489 \\[mail-signature] mail-signature (insert `mail-signature-file' file).
490 \\[mail-yank-original] mail-yank-original (insert current message, in Rmail).
491 \\[mail-fill-yanked-message] mail-fill-yanked-message (fill what was yanked).
492 \\[mail-sent-via] mail-sent-via (add a Sent-via field for each To or CC).
493 Turning on Mail mode runs the normal hooks `text-mode-hook' and
494 `mail-mode-hook' (in that order)."
495 (make-local-variable 'mail-reply-action)
496 (make-local-variable 'mail-send-actions)
497 (setq buffer-offer-save t)
498 (make-local-variable 'font-lock-defaults)
499 (setq font-lock-defaults '(mail-font-lock-keywords t t))
500 (make-local-variable 'paragraph-separate)
501 (make-local-variable 'paragraph-start)
502 (make-local-variable 'normal-auto-fill-function)
503 (setq normal-auto-fill-function 'mail-mode-auto-fill)
504 (make-local-variable 'fill-paragraph-function)
505 (setq fill-paragraph-function 'mail-mode-fill-paragraph)
506 ;; Allow using comment commands to add/remove quoting (this only does
507 ;; anything if mail-yank-prefix is set to a non-nil value).
508 (set (make-local-variable 'comment-start) mail-yank-prefix)
509 (make-local-variable 'adaptive-fill-regexp)
510 (setq adaptive-fill-regexp
511 (concat "[ \t]*[-[:alnum:]]+>+[ \t]*\\|"
512 adaptive-fill-regexp))
513 (make-local-variable 'adaptive-fill-first-line-regexp)
514 (setq adaptive-fill-first-line-regexp
515 (concat "[ \t]*[-[:alnum:]]*>+[ \t]*\\|"
516 adaptive-fill-first-line-regexp))
517 ;; `-- ' precedes the signature. `-----' appears at the start of the
518 ;; lines that delimit forwarded messages.
519 ;; Lines containing just >= 3 dashes, perhaps after whitespace,
520 ;; are also sometimes used and should be separators.
521 (setq paragraph-start (concat (regexp-quote mail-header-separator)
522 "$\\|\t*\\([-|#;>* ]\\|(?[0-9]+[.)]\\)+$"
523 "\\|[ \t]*[[:alnum:]]*>+[ \t]*$\\|[ \t]*$\\|"
526 (setq paragraph-separate paragraph-start))
529 (defun mail-header-end ()
530 "Return the buffer location of the end of headers, as a number."
537 (defun mail-text-start ()
538 "Return the buffer location of the start of text, as a number."
546 (defun mail-sendmail-delimit-header ()
547 "Set up whatever header delimiter convention sendmail will use.
548 Concretely: replace the first blank line in the header with the separator."
550 (insert mail-header-separator)
553 (defun mail-sendmail-undelimit-header ()
554 "Remove header separator to put the message in correct form for sendmail.
555 Leave point at the start of the delimiter line."
557 (delete-region (point) (progn (end-of-line) (point))))
559 (defun mail-mode-auto-fill ()
560 "Carry out Auto Fill for Mail mode.
561 If within the headers, this makes the new lines into continuation lines."
562 (if (< (point) (mail-header-end))
563 (let ((old-line-start (save-excursion (beginning-of-line) (point))))
567 (while (not (eq (point) old-line-start))
568 ;; Use insert-before-markers in case we're inserting
569 ;; before the saved value of point (which is common).
570 (insert-before-markers " ")
575 (defun mail-mode-fill-paragraph (arg)
576 ;; Do something special only if within the headers.
577 (if (< (point) (mail-header-end))
578 (let (beg end fieldname)
579 (when (prog1 (re-search-backward "^[-a-zA-Z]+:" nil 'yes)
582 (downcase (buffer-substring beg (1- (match-end 0))))))
584 ;; Find continuation lines and get rid of their continuation markers.
585 (while (looking-at "[ \t]")
586 (delete-horizontal-space)
588 (setq end (point-marker))
590 ;; If this field contains addresses,
591 ;; make sure we can fill after each address.
592 (if (member fieldname
593 '("to" "cc" "bcc" "from" "reply-to"
594 "resent-to" "resent-cc" "resent-bcc"
595 "resent-from" "resent-reply-to"))
596 (while (search-forward "," end t)
597 (or (looking-at "[ \t]")
599 (fill-region-as-paragraph beg end)
600 ;; Mark all lines except the first as continuations.
603 (while (< (point) end)
606 (move-marker end nil)
613 (setq mail-mode-map (make-sparse-keymap))
614 (define-key mail-mode-map "\M-\t" 'mail-complete)
615 (define-key mail-mode-map "\C-c?" 'describe-mode)
616 (define-key mail-mode-map "\C-c\C-f\C-t" 'mail-to)
617 (define-key mail-mode-map "\C-c\C-f\C-b" 'mail-bcc)
618 (define-key mail-mode-map "\C-c\C-f\C-f" 'mail-fcc)
619 (define-key mail-mode-map "\C-c\C-f\C-c" 'mail-cc)
620 (define-key mail-mode-map "\C-c\C-f\C-s" 'mail-subject)
621 (define-key mail-mode-map "\C-c\C-f\C-r" 'mail-reply-to)
622 (define-key mail-mode-map "\C-c\C-t" 'mail-text)
623 (define-key mail-mode-map "\C-c\C-y" 'mail-yank-original)
624 (define-key mail-mode-map "\C-c\C-r" 'mail-yank-region)
625 (define-key mail-mode-map [remap split-line] 'mail-split-line)
626 (define-key mail-mode-map "\C-c\C-q" 'mail-fill-yanked-message)
627 (define-key mail-mode-map "\C-c\C-w" 'mail-signature)
628 (define-key mail-mode-map "\C-c\C-v" 'mail-sent-via)
629 (define-key mail-mode-map "\C-c\C-c" 'mail-send-and-exit)
630 (define-key mail-mode-map "\C-c\C-s" 'mail-send)
631 (define-key mail-mode-map "\C-c\C-i" 'mail-attach-file))
633 (define-key mail-mode-map [menu-bar mail]
634 (cons "Mail" (make-sparse-keymap "Mail")))
636 (define-key mail-mode-map [menu-bar mail fill]
637 '("Fill Citation" . mail-fill-yanked-message))
639 (define-key mail-mode-map [menu-bar mail yank]
640 '("Cite Original" . mail-yank-original))
642 (define-key mail-mode-map [menu-bar mail signature]
643 '("Insert Signature" . mail-signature))
645 (define-key mail-mode-map [menu-bar mail mail-sep]
648 (define-key mail-mode-map [menu-bar mail cancel]
649 '("Cancel" . mail-dont-send))
651 (define-key mail-mode-map [menu-bar mail send-stay]
652 '("Send, Keep Editing" . mail-send))
654 (define-key mail-mode-map [menu-bar mail send]
655 '("Send Message" . mail-send-and-exit))
657 (define-key mail-mode-map [menu-bar headers]
658 (cons "Headers" (make-sparse-keymap "Move to Header")))
660 (define-key mail-mode-map [menu-bar headers text]
661 '("Text" . mail-text))
663 (define-key mail-mode-map [menu-bar headers expand-aliases]
664 '("Expand Aliases" . expand-mail-aliases))
666 (define-key mail-mode-map [menu-bar headers sent-via]
667 '("Sent Via" . mail-sent-via))
669 (define-key mail-mode-map [menu-bar headers reply-to]
670 '("Reply-To" . mail-reply-to))
672 (define-key mail-mode-map [menu-bar headers bcc]
675 (define-key mail-mode-map [menu-bar headers fcc]
678 (define-key mail-mode-map [menu-bar headers cc]
681 (define-key mail-mode-map [menu-bar headers subject]
682 '("Subject" . mail-subject))
684 (define-key mail-mode-map [menu-bar headers to]
687 ;; User-level commands for sending.
689 (defun mail-send-and-exit (&optional arg)
690 "Send message like `mail-send', then, if no errors, exit from mail buffer.
691 Prefix arg means don't delete this window."
696 (defun mail-dont-send (&optional arg)
697 "Don't send the message you have been editing.
698 Prefix arg means don't delete this window."
702 (defun mail-bury (&optional arg)
703 "Bury this mail buffer."
704 (let ((newbuf (other-buffer (current-buffer))))
705 (bury-buffer (current-buffer))
706 (if (and (or (window-dedicated-p (frame-selected-window))
707 (cdr (assq 'mail-dedicated-frame (frame-parameters))))
708 (not (null (delq (selected-frame) (visible-frame-list)))))
709 (delete-frame (selected-frame))
710 (let (rmail-flag summary-buffer)
714 (set-buffer (window-buffer (next-window (selected-window) 'not)))
715 (setq rmail-flag (eq major-mode 'rmail-mode))
717 (and mail-bury-selects-summary
718 (boundp 'rmail-summary-buffer)
720 (buffer-name rmail-summary-buffer)
721 (not (get-buffer-window rmail-summary-buffer))
722 rmail-summary-buffer))))
724 ;; If the Rmail buffer has a summary, show that.
725 (if summary-buffer (switch-to-buffer summary-buffer)
727 (switch-to-buffer newbuf))))))
729 (defcustom mail-send-hook nil
730 "Hook run just before sending mail with `mail-send'."
732 :options '(flyspell-mode-off)
736 "Send the message in the current buffer.
737 If `mail-interactive' is non-nil, wait for success indication
738 or error messages, and inform user.
739 Otherwise any failure is reported in a message back to
740 the user from the mailer."
742 (if (if buffer-file-name
743 (y-or-n-p "Send buffer contents as mail message? ")
744 (or (buffer-modified-p)
745 (y-or-n-p "Message already sent; resend? ")))
746 (let ((inhibit-read-only t)
748 (unless (memq mail-send-nonascii '(t mime))
749 (goto-char (point-min))
750 (skip-chars-forward "\0-\177")
751 (or (= (point) (point-max))
752 (if (eq mail-send-nonascii 'query)
753 (or (y-or-n-p "Message contains non-ASCII characters; send anyway? ")
755 (error "Message contains non-ASCII characters"))))
756 ;; Complain about any invalid line.
757 (goto-char (point-min))
758 (while (< (point) (mail-header-end))
759 (unless (looking-at "[ \t]\\|.*:\\|$")
761 (error "Invalid header line (maybe a continuation line lacks initial whitespace)"))
764 (run-hooks 'mail-send-hook)
765 (message "Sending...")
766 (funcall send-mail-function)
767 ;; Now perform actions on successful sending.
768 (while mail-send-actions
770 (apply (car (car mail-send-actions))
771 (cdr (car mail-send-actions)))
773 (setq mail-send-actions (cdr mail-send-actions)))
774 (message "Sending...done")
775 ;; If buffer has no file, mark it as unmodified and delete auto-save.
776 (if (not buffer-file-name)
778 (set-buffer-modified-p nil)
779 (delete-auto-save-file-if-necessary t))))))
781 (defun mail-envelope-from ()
782 "Return the envelope mail address to use when sending mail.
783 This function uses `mail-envelope-from'."
784 (if (eq mail-envelope-from 'header)
785 (nth 1 (mail-extract-address-components
786 (mail-fetch-field "From")))
789 ;; This does the real work of sending a message via sendmail.
790 ;; It is called via the variable send-mail-function.
793 (defvar sendmail-coding-system nil
794 "*Coding system for encoding the outgoing mail.
795 This has higher priority than `default-buffer-file-coding-system'
796 and `default-sendmail-coding-system',
797 but lower priority than the local value of `buffer-file-coding-system'.
798 See also the function `select-message-coding-system'.")
801 (defvar default-sendmail-coding-system 'iso-latin-1
802 "Default coding system for encoding the outgoing mail.
803 This variable is used only when `sendmail-coding-system' is nil.
805 This variable is set/changed by the command set-language-environment.
806 User should not set this variable manually,
807 instead use sendmail-coding-system to get a constant encoding
808 of outgoing mails regardless of the current language environment.
809 See also the function `select-message-coding-system'.")
811 (defun sendmail-send-it ()
812 "Send the current mail buffer using the Sendmail package.
813 This is a suitable value for `send-mail-function'. It sends using the
814 external program defined by `sendmail-program'."
815 (require 'mail-utils)
816 (let ((errbuf (if mail-interactive
817 (generate-new-buffer " sendmail errors")
819 (tembuf (generate-new-buffer " sendmail temp"))
820 (multibyte enable-multibyte-characters)
821 (case-fold-search nil)
822 (selected-coding (select-message-coding-system))
823 ;;; resend-to-addresses
826 (mailbuf (current-buffer))
827 (program (if (boundp 'sendmail-program)
829 "/usr/lib/sendmail"))
830 ;; Examine these variables now, so that
831 ;; local binding in the mail buffer will take effect.
833 (and mail-specify-envelope-from
834 (or (mail-envelope-from) user-mail-address))))
840 (set-buffer-multibyte nil))
841 (insert-buffer-substring mailbuf)
842 (goto-char (point-max))
843 ;; require one newline at the end.
844 (or (= (preceding-char) ?\n)
846 ;; Change header-delimiter to be what sendmail expects.
847 (goto-char (mail-header-end))
848 (delete-region (point) (progn (end-of-line) (point)))
849 (setq delimline (point-marker))
850 (sendmail-sync-aliases)
852 (expand-mail-aliases (point-min) delimline))
853 (goto-char (point-min))
854 ;; Ignore any blank lines in the header
855 (while (and (re-search-forward "\n\n\n*" delimline t)
856 (< (point) delimline))
857 (replace-match "\n"))
858 (goto-char (point-min))
859 (let ((case-fold-search t))
860 ;;; (goto-char (point-min))
861 ;;; (while (re-search-forward "^Resent-\\(to\\|cc\\|bcc\\):" delimline t)
862 ;;; (setq resend-to-addresses
863 ;;; (save-restriction
864 ;;; (narrow-to-region (point)
867 ;;; (while (looking-at "^[ \t]")
868 ;;; (forward-line 1))
870 ;;; (append (mail-parse-comma-list)
871 ;;; resend-to-addresses)))
872 ;;; ;; Delete Resent-BCC ourselves
873 ;;; (if (save-excursion (beginning-of-line)
874 ;;; (looking-at "resent-bcc"))
875 ;;; (delete-region (save-excursion (beginning-of-line) (point))
876 ;;; (save-excursion (end-of-line) (1+ (point))))))
877 ;;; Apparently this causes a duplicate Sender.
878 ;;; ;; If the From is different than current user, insert Sender.
879 ;;; (goto-char (point-min))
880 ;;; (and (re-search-forward "^From:" delimline t)
882 ;;; (require 'mail-utils)
883 ;;; (not (string-equal
884 ;;; (mail-strip-quoted-names
885 ;;; (save-restriction
886 ;;; (narrow-to-region (point-min) delimline)
887 ;;; (mail-fetch-field "From")))
888 ;;; (user-login-name))))
891 ;;; (insert "Sender: " (user-login-name) "\n")))
892 ;; Don't send out a blank subject line
893 (goto-char (point-min))
894 (if (re-search-forward "^Subject:\\([ \t]*\n\\)+\\b" delimline t)
896 ;; This one matches a Subject just before the header delimiter.
897 (if (and (re-search-forward "^Subject:\\([ \t]*\n\\)+" delimline t)
898 (= (match-end 0) delimline))
900 ;; Put the "From:" field in unless for some odd reason
901 ;; they put one in themselves.
902 (goto-char (point-min))
903 (if (not (re-search-forward "^From:" delimline t))
904 (let* ((login user-mail-address)
905 (fullname (user-full-name))
906 (quote-fullname nil))
907 (if (string-match "[^\0-\177]" fullname)
908 (setq fullname (rfc2047-encode-string fullname)
910 (cond ((eq mail-from-style 'angles)
911 (insert "From: " fullname)
912 (let ((fullname-start (+ (point-min) 6))
913 (fullname-end (point-marker)))
914 (goto-char fullname-start)
915 ;; Look for a character that cannot appear unquoted
916 ;; according to RFC 822.
917 (if (or (re-search-forward "[^- !#-'*+/-9=?A-Z^-~]"
921 ;; Quote fullname, escaping specials.
922 (goto-char fullname-start)
924 (while (re-search-forward "[\"\\]"
926 (replace-match "\\\\\\&" t))
928 (insert " <" login ">\n"))
929 ((eq mail-from-style 'parens)
930 (insert "From: " login " (")
931 (let ((fullname-start (point)))
937 (let ((fullname-end (point-marker)))
938 (goto-char fullname-start)
939 ;; RFC 822 says \ and nonmatching parentheses
940 ;; must be escaped in comments.
941 ;; Escape every instance of ()\ ...
942 (while (re-search-forward "[()\\]" fullname-end 1)
943 (replace-match "\\\\\\&" t))
944 ;; ... then undo escaping of matching parentheses,
945 ;; including matching nested parentheses.
946 (goto-char fullname-start)
947 (while (re-search-forward
948 "\\(\\=\\|[^\\]\\(\\\\\\\\\\)*\\)\\\\(\\(\\([^\\]\\|\\\\\\\\\\)*\\)\\\\)"
950 (replace-match "\\1(\\3)" t)
951 (goto-char fullname-start))))
953 ((null mail-from-style)
954 (insert "From: " login "\n"))
955 ((eq mail-from-style 'system-default)
957 (t (error "Invalid value for `mail-from-style'")))))
958 ;; Possibly add a MIME header for the current coding system
960 (goto-char (point-min))
961 (and (eq mail-send-nonascii 'mime)
962 (not (re-search-forward "^MIME-version:" delimline t))
963 (progn (skip-chars-forward "\0-\177")
964 (/= (point) (point-max)))
967 (coding-system-get selected-coding 'mime-charset))
968 (goto-char delimline)
969 (insert "MIME-version: 1.0\n"
970 "Content-type: text/plain; charset="
971 (symbol-name charset) "\n"
972 "Content-Transfer-Encoding: 8bit\n")))
973 ;; Insert an extra newline if we need it to work around
974 ;; Sun's bug that swallows newlines.
975 (goto-char (1+ delimline))
976 (if (eval mail-mailer-swallows-blank-line)
978 ;; Find and handle any FCC fields.
979 (goto-char (point-min))
980 (if (re-search-forward "^FCC:" delimline t)
982 (setq fcc-was-found t)
983 (mail-do-fcc delimline)))
988 (goto-char (point-min))
989 (if (let ((case-fold-search t))
990 (re-search-forward "^To:\\|^cc:\\|^bcc:\\|^resent-to:\
991 \\|^resent-cc:\\|^resent-bcc:"
993 (let* ((default-directory "/")
994 (coding-system-for-write selected-coding)
996 (append (list (point-min) (point-max)
998 nil errbuf nil "-oi")
1000 (list "-f" envelope-from))
1001 ;;; ;; Don't say "from root" if running under su.
1002 ;;; (and (equal (user-real-login-name) "root")
1003 ;;; (list "-f" (user-login-name)))
1004 (and mail-alias-file
1005 (list (concat "-oA" mail-alias-file)))
1006 (if mail-interactive
1007 ;; These mean "report errors to terminal"
1008 ;; and "deliver interactively"
1010 ;; These mean "report errors by mail"
1011 ;; and "deliver in background".
1013 ;;; ;; Get the addresses from the message
1014 ;;; ;; unless this is a resend.
1015 ;;; ;; We must not do that for a resend
1016 ;;; ;; because we would find the original addresses.
1017 ;;; ;; For a resend, include the specific addresses.
1018 ;;; (or resend-to-addresses
1022 (list "-N" (mapconcat 'symbol-name
1026 (exit-value (apply 'call-process-region args)))
1027 (or (null exit-value) (zerop exit-value)
1028 (error "Sending...failed with exit value %d" exit-value)))
1030 (error "No recipients")))
1031 (if mail-interactive
1034 (goto-char (point-min))
1035 (while (re-search-forward "\n\n* *" nil t)
1036 (replace-match "; "))
1037 (if (not (zerop (buffer-size)))
1038 (error "Sending...failed to %s"
1039 (buffer-substring (point-min) (point-max)))))))
1040 (kill-buffer tembuf)
1041 (if (bufferp errbuf)
1042 (kill-buffer errbuf)))))
1044 (defun mail-do-fcc (header-end)
1046 (rmailbuf (current-buffer))
1047 (time (current-time))
1048 (tembuf (generate-new-buffer " rmail output"))
1049 (case-fold-search t))
1050 (unless (markerp header-end)
1051 (error "Value of `header-end' must be a marker"))
1053 (goto-char (point-min))
1054 (while (re-search-forward "^FCC:[ \t]*" header-end t)
1055 (setq fcc-list (cons (buffer-substring (point)
1058 (skip-chars-backward " \t")
1061 (delete-region (match-beginning 0)
1062 (progn (forward-line 1) (point))))
1065 ;; This initial newline is written out if the fcc file already exists.
1066 (insert "\nFrom " (user-login-name) " "
1067 (current-time-string time) "\n")
1068 ;; Insert the time zone before the year.
1071 (require 'mail-utils)
1072 (insert (mail-rfc822-time-zone time) " ")
1073 (goto-char (point-max))
1074 (insert-buffer-substring rmailbuf)
1075 ;; Make sure messages are separated.
1076 (goto-char (point-max))
1079 ;; ``Quote'' "^From " as ">From "
1080 ;; (note that this isn't really quoting, as there is no requirement
1081 ;; that "^[>]+From " be quoted in the same transparent way.)
1082 (let ((case-fold-search nil))
1083 (while (search-forward "\nFrom " nil t)
1087 (let* ((buffer (find-buffer-visiting (car fcc-list)))
1088 (curbuf (current-buffer))
1091 (beg (point-min)) (end (point-max))
1092 (beg2 (save-excursion (goto-char (point-min))
1093 (forward-line 2) (point))))
1095 ;; File is present in a buffer => append to that buffer.
1098 (setq buffer-matches-file
1099 (and (not (buffer-modified-p))
1100 (verify-visited-file-modtime buffer)))
1101 ;; Keep the end of the accessible portion at the same place
1102 ;; unless it is the end of the buffer.
1103 (let ((max (if (/= (1+ (buffer-size)) (point-max))
1106 ;; Code below lifted from rmailout.el
1107 ;; function rmail-output-to-rmail-file:
1108 (let ((buffer-read-only nil)
1109 (msg (and (boundp 'rmail-current-message)
1110 rmail-current-message)))
1111 ;; If MSG is non-nil, buffer is in RMAIL mode.
1114 ;; Append to an ordinary buffer as a
1115 ;; Unix mail message.
1116 (rmail-maybe-set-message-counters)
1118 (narrow-to-region (point-max) (point-max))
1119 (insert "\C-l\n0, unseen,,\n*** EOOH ***\n"
1120 "Date: " (mail-rfc822-date) "\n")
1121 (insert-buffer-substring curbuf beg2 end)
1123 (goto-char (point-min))
1125 (search-backward "\n\^_")
1126 (narrow-to-region (point) (point-max))
1127 (rmail-count-new-messages t)
1128 (rmail-show-message msg)
1130 ;; Output file not in rmail mode
1131 ;; => just insert at the end.
1132 (narrow-to-region (point-min) (1+ (buffer-size)))
1133 (goto-char (point-max))
1134 (insert-buffer-substring curbuf beg end))
1135 (or buffer-matches-file
1137 (if (y-or-n-p (format "Save file %s? "
1140 (setq dont-write-the-file t))))
1141 (if max (narrow-to-region (point-min) max))))))
1142 ;; Append to the file directly,
1143 ;; unless we've already taken care of it.
1144 (unless dont-write-the-file
1145 (if (and (file-exists-p (car fcc-list))
1146 ;; Check that the file isn't empty. We don't
1147 ;; want to insert a newline at the start of an
1149 (not (zerop (nth 7 (file-attributes (car fcc-list)))))
1150 (mail-file-babyl-p (car fcc-list)))
1151 ;; If the file is a Babyl file,
1152 ;; convert the message to Babyl format.
1153 (let ((coding-system-for-write
1154 (or rmail-file-coding-system
1157 (set-buffer (get-buffer-create " mail-temp"))
1158 (setq buffer-read-only nil)
1160 (insert "\C-l\n0, unseen,,\n*** EOOH ***\n"
1161 "Date: " (mail-rfc822-date) "\n")
1162 (insert-buffer-substring curbuf beg2 end)
1164 (write-region (point-min) (point-max) (car fcc-list) t)
1167 (1+ (point-min)) (point-max) (car fcc-list) t)))
1168 (and buffer (not dont-write-the-file)
1169 (with-current-buffer buffer
1170 (set-visited-file-modtime))))
1171 (setq fcc-list (cdr fcc-list))))
1172 (kill-buffer tembuf)))
1174 (defun mail-sent-via ()
1175 "Make a Sent-via header line from each To or CC header line."
1178 ;; put a marker at the end of the header
1179 (let ((end (copy-marker (mail-header-end)))
1180 (case-fold-search t)
1182 (goto-char (point-min))
1183 ;; search for the To: lines and make Sent-via: lines from them
1184 ;; search for the next To: line
1185 (while (re-search-forward "^\\(to\\|cc\\):" end t)
1186 ;; Grab this line plus all its continuations, sans the `to:'.
1188 (buffer-substring (point)
1190 (if (re-search-forward "^[^ \t\n]" end t)
1194 ;; Insert a copy, with altered header field name.
1195 (insert-before-markers "Sent-via:" to-line))))))
1198 "Move point to end of To-field."
1201 (mail-position-on-field "To"))
1203 (defun mail-subject ()
1204 "Move point to end of Subject-field."
1207 (mail-position-on-field "Subject"))
1210 "Move point to end of CC-field. Create a CC field if none."
1213 (or (mail-position-on-field "cc" t)
1214 (progn (mail-position-on-field "to")
1215 (insert "\nCC: "))))
1218 "Move point to end of BCC-field. Create a BCC field if none."
1221 (or (mail-position-on-field "bcc" t)
1222 (progn (mail-position-on-field "to")
1223 (insert "\nBCC: "))))
1225 (defun mail-fcc (folder)
1226 "Add a new FCC field, with file name completion."
1227 (interactive "FFolder carbon copy: ")
1229 (or (mail-position-on-field "fcc" t) ;Put new field after exiting FCC.
1230 (mail-position-on-field "to"))
1231 (insert "\nFCC: " folder))
1233 (defun mail-reply-to ()
1234 "Move point to end of Reply-To-field. Create a Reply-To field if none."
1237 (mail-position-on-field "Reply-To"))
1239 (defun mail-position-on-field (field &optional soft)
1241 (case-fold-search t))
1242 (setq end (mail-header-end))
1243 (goto-char (point-min))
1244 (if (re-search-forward (concat "^" (regexp-quote field) ":") end t)
1246 (re-search-forward "^[^ \t]" nil 'move)
1248 (skip-chars-backward "\n")
1251 (progn (goto-char end)
1252 (insert field ": \n")
1253 (skip-chars-backward "\n")))
1257 "Move point to beginning of message text."
1260 (goto-char (mail-text-start)))
1262 (defun mail-signature (&optional atpoint)
1263 "Sign letter with signature based on `mail-signature-file'.
1264 Prefix arg means put contents at point."
1268 (goto-char (point-max)))
1269 (skip-chars-backward " \t\n")
1272 (delete-region (point) (point-max)))
1273 (if (stringp mail-signature)
1274 (insert mail-signature)
1275 (insert "\n\n-- \n")
1276 (insert-file-contents (expand-file-name mail-signature-file)))))
1278 (defun mail-fill-yanked-message (&optional justifyp)
1279 "Fill the paragraphs of a message yanked into this one.
1280 Numeric argument means justify as well."
1283 (goto-char (mail-text-start))
1284 (fill-individual-paragraphs (point)
1287 mail-citation-prefix-regexp)))
1289 (defun mail-indent-citation ()
1290 "Modify text just inserted from a message to be cited.
1291 The inserted text should be the region.
1292 When this function returns, the region is again around the modified text.
1294 Normally, indent each nonblank line `mail-indentation-spaces' spaces.
1295 However, if `mail-yank-prefix' is non-nil, insert that prefix on each line."
1296 (mail-yank-clear-headers (region-beginning) (region-end))
1297 (if (null mail-yank-prefix)
1298 (indent-rigidly (region-beginning) (region-end)
1299 mail-indentation-spaces)
1301 (let ((end (set-marker (make-marker) (region-end))))
1302 (goto-char (region-beginning))
1303 (while (< (point) end)
1304 (insert mail-yank-prefix)
1305 (forward-line 1))))))
1307 (defun mail-yank-original (arg)
1308 "Insert the message being replied to, if any (in rmail).
1309 Puts point after the text and mark before.
1310 Normally, indents each nonblank line ARG spaces (default 3).
1311 However, if `mail-yank-prefix' is non-nil, insert that prefix on each line.
1313 Just \\[universal-argument] as argument means don't indent, insert no prefix,
1314 and don't delete any header fields."
1316 (if mail-reply-action
1317 (let ((start (point))
1318 (original mail-reply-action))
1319 (and (consp original) (eq (car original) 'insert-buffer)
1320 (setq original (nth 1 original)))
1321 (if (consp original)
1322 (apply (car original) (cdr original))
1323 ;; If the original message is in another window in the same frame,
1324 ;; delete that window to save screen space.
1325 ;; t means don't alter other frames.
1326 (delete-windows-on original t)
1327 (insert-buffer original)
1328 (set-text-properties (point) (mark t) nil))
1332 (let ((mail-indentation-spaces (if arg (prefix-numeric-value arg)
1333 mail-indentation-spaces))
1334 ;; Avoid error in Transient Mark mode
1335 ;; on account of mark's being inactive.
1336 (mark-even-if-inactive t))
1337 (cond (mail-citation-hook
1338 ;; Bind mail-citation-header to the inserted
1339 ;; message's header.
1340 (let ((mail-citation-header
1341 (buffer-substring-no-properties
1345 (narrow-to-region start (point-max))
1349 (run-hooks 'mail-citation-hook)))
1351 (run-hooks 'mail-yank-hooks))
1353 (mail-indent-citation)))))
1354 ;; This is like exchange-point-and-mark, but doesn't activate the mark.
1355 ;; It is cleaner to avoid activation, even though the command
1356 ;; loop would deactivate the mark because we inserted text.
1357 (goto-char (prog1 (mark t)
1358 (set-marker (mark-marker) (point) (current-buffer))))
1359 (if (not (eolp)) (insert ?\n)))))
1361 (defun mail-yank-clear-headers (start end)
1364 (setq temp start start end end temp)))
1365 (if mail-yank-ignored-headers
1368 (if (search-forward "\n\n" end t)
1370 (narrow-to-region start (point))
1372 (while (let ((case-fold-search t))
1373 (re-search-forward mail-yank-ignored-headers nil t))
1375 (delete-region (point)
1376 (progn (re-search-forward "\n[^ \t]")
1380 (defun mail-yank-region (arg)
1381 "Insert the selected region from the message being replied to.
1382 Puts point after the text and mark before.
1383 Normally, indents each nonblank line ARG spaces (default 3).
1384 However, if `mail-yank-prefix' is non-nil, insert that prefix on each line.
1386 Just \\[universal-argument] as argument means don't indent, insert no prefix,
1387 and don't delete any header fields."
1389 (and (consp mail-reply-action)
1390 (eq (car mail-reply-action) 'insert-buffer)
1391 (with-current-buffer (nth 1 mail-reply-action)
1393 (error "No mark set: %S" (current-buffer))))
1394 (let ((buffer (nth 1 mail-reply-action))
1396 ;; Avoid error in Transient Mark mode
1397 ;; on account of mark's being inactive.
1398 (mark-even-if-inactive t))
1399 ;; Insert the citation text.
1400 (insert (with-current-buffer buffer
1401 (buffer-substring-no-properties (point) (mark))))
1403 ;; Indent or otherwise annotate the citation text.
1406 (let ((mail-indentation-spaces (if arg (prefix-numeric-value arg)
1407 mail-indentation-spaces)))
1408 (if mail-citation-hook
1409 ;; Bind mail-citation-hook to the original message's header.
1410 (let ((mail-citation-header
1411 (with-current-buffer buffer
1412 (buffer-substring-no-properties
1415 (goto-char (point-min))
1418 (run-hooks 'mail-citation-hook))
1420 (run-hooks 'mail-yank-hooks)
1421 (mail-indent-citation))))))))
1423 (defun mail-split-line ()
1424 "Split current line, moving portion beyond point vertically down.
1425 If the current line has `mail-yank-prefix', insert it on the new line."
1427 (split-line mail-yank-prefix))
1430 (defun mail-attach-file (&optional file)
1431 "Insert a file at the end of the buffer, with separator lines around it."
1432 (interactive "fAttach file: ")
1434 (goto-char (point-max))
1435 (or (bolp) (newline))
1437 (let ((start (point))
1439 (insert (format "===File %s===" file))
1440 (insert-char ?= (max 0 (- 60 (current-column))))
1442 (setq middle (point))
1443 (insert "============================================================\n")
1446 (insert-file-contents file)
1447 (or (bolp) (newline))
1448 (goto-char start))))
1450 ;; Put these commands last, to reduce chance of lossage from quitting
1451 ;; in middle of loading the file.
1453 ;;;###autoload (add-hook 'same-window-buffer-names "*mail*")
1456 (defun mail (&optional noerase to subject in-reply-to cc replybuffer actions)
1457 "Edit a message to be sent. Prefix arg means resume editing (don't erase).
1458 When this function returns, the buffer `*mail*' is selected.
1459 The value is t if the message was newly initialized; otherwise, nil.
1461 Optionally, the signature file `mail-signature-file' can be inserted at the
1462 end; see the variable `mail-signature'.
1465 While editing message, type \\[mail-send-and-exit] to send the message and exit.
1467 Various special commands starting with C-c are available in sendmail mode
1468 to move to message header fields:
1471 If `mail-self-blind' is non-nil, a BCC to yourself is inserted
1472 when the message is initialized.
1474 If `mail-default-reply-to' is non-nil, it should be an address (a string);
1475 a Reply-to: field with that address is inserted.
1477 If `mail-archive-file-name' is non-nil, an FCC field with that file name
1480 The normal hook `mail-setup-hook' is run after the message is
1481 initialized. It can add more default fields to the message.
1483 When calling from a program, the first argument if non-nil says
1484 not to erase the existing contents of the `*mail*' buffer.
1486 The second through fifth arguments,
1487 TO, SUBJECT, IN-REPLY-TO and CC, specify if non-nil
1488 the initial contents of those header fields.
1489 These arguments should not have final newlines.
1490 The sixth argument REPLYBUFFER is a buffer which contains an
1491 original message being replied to, or else an action
1492 of the form (FUNCTION . ARGS) which says how to insert the original.
1493 Or it can be nil, if not replying to anything.
1494 The seventh argument ACTIONS is a list of actions to take
1495 if/when the message is sent. Each action looks like (FUNCTION . ARGS);
1496 when the message is sent, we apply FUNCTION to ARGS.
1497 This is how Rmail arranges to mark messages `answered'."
1499 ;;; This is commented out because I found it was confusing in practice.
1500 ;;; It is easy enough to rename *mail* by hand with rename-buffer
1501 ;;; if you want to have multiple mail buffers.
1502 ;;; And then you can control which messages to save. --rms.
1505 ;;; ;; If requested, look for a mail buffer that is modified and go to it.
1508 ;;; (while (and (setq buffer
1509 ;;; (get-buffer (if (= 1 index) "*mail*"
1510 ;;; (format "*mail*<%d>" index))))
1511 ;;; (not (buffer-modified-p buffer)))
1512 ;;; (setq index (1+ index)))
1513 ;;; (if buffer (switch-to-buffer buffer)
1514 ;;; ;; If none exists, start a new message.
1515 ;;; ;; This will never re-use an existing unmodified mail buffer
1516 ;;; ;; (since index is not 1 anymore). Perhaps it should.
1517 ;;; (setq noerase nil))))
1518 ;;; ;; Unless we found a modified message and are happy, start a new message.
1519 ;;; (if (not noerase)
1521 ;;; ;; Look for existing unmodified mail buffer.
1522 ;;; (while (and (setq buffer
1523 ;;; (get-buffer (if (= 1 index) "*mail*"
1524 ;;; (format "*mail*<%d>" index))))
1525 ;;; (buffer-modified-p buffer))
1526 ;;; (setq index (1+ index)))
1527 ;;; ;; If none, make a new one.
1529 ;;; (setq buffer (generate-new-buffer "*mail*")))
1530 ;;; ;; Go there and initialize it.
1531 ;;; (switch-to-buffer buffer)
1533 ;;; (setq default-directory (expand-file-name "~/"))
1534 ;;; (auto-save-mode auto-save-default)
1536 ;;; (mail-setup to subject in-reply-to cc replybuffer actions)
1537 ;;; (if (and buffer-auto-save-file-name
1538 ;;; (file-exists-p buffer-auto-save-file-name))
1539 ;;; (message "Auto save file for draft message exists; consider M-x mail-recover"))
1541 (pop-to-buffer "*mail*")
1542 ;; Avoid danger that the auto-save file can't be written.
1543 (let ((dir (expand-file-name
1544 (file-name-as-directory mail-default-directory))))
1545 (if (file-exists-p dir)
1546 (setq default-directory dir)))
1547 ;; Only call auto-save-mode if necessary, to avoid changing auto-save file.
1548 (if (or (and auto-save-default (not buffer-auto-save-file-name))
1549 (and (not auto-save-default) buffer-auto-save-file-name))
1550 (auto-save-mode auto-save-default))
1552 ;; Disconnect the buffer from its visited file
1553 ;; (in case the user has actually visited a file *mail*).
1554 ; (set-visited-file-name nil)
1557 (if buffer-file-name
1558 (if (buffer-modified-p)
1559 (when (y-or-n-p "Buffer has unsaved changes; reinitialize it and discard them? ")
1560 (if (y-or-n-p "Disconnect buffer from visited file? ")
1561 (set-visited-file-name nil))
1563 (when (y-or-n-p "Reinitialize buffer, and disconnect it from the visited file? ")
1564 (set-visited-file-name nil)
1566 ;; A non-file-visiting buffer.
1567 (if (buffer-modified-p)
1568 (y-or-n-p "Unsent message being composed; erase it? ")
1570 (let ((inhibit-read-only t))
1572 (mail-setup to subject in-reply-to cc replybuffer actions)
1573 (setq initialized t)))
1574 (if (and buffer-auto-save-file-name
1575 (file-exists-p buffer-auto-save-file-name))
1576 (message "Auto save file for draft message exists; consider M-x mail-recover"))
1579 (defun mail-recover-1 ()
1580 "Pop up a list of auto-saved draft messages so you can recover one of them."
1582 (let ((file-name (make-auto-save-file-name))
1583 (ls-lisp-support-shell-wildcards t)
1584 non-random-len wildcard)
1585 ;; Remove the random part from the auto-save-file-name, and
1586 ;; create a wildcard which matches possible candidates.
1587 ;; Note: this knows that make-auto-save-file-name appends
1588 ;; "#<RANDOM-STUFF>#" to the buffer name, where RANDOM-STUFF
1589 ;; is the result of (make-temp-name "").
1590 (setq non-random-len
1591 (- (length file-name) (length (make-temp-name "")) 1))
1592 (setq wildcard (concat (substring file-name 0 non-random-len) "*"))
1593 (if (null (file-expand-wildcards wildcard))
1594 (message "There are no auto-saved drafts to recover")
1595 ;; Bind dired-trivial-filenames to t because all auto-save file
1596 ;; names are normally ``trivial'', so Dired will set point after
1597 ;; all the files, at buffer bottom. We want it on the first
1599 (let ((dired-trivial-filenames t))
1600 (dired-other-window wildcard (concat dired-listing-switches "t")))
1601 (rename-buffer "*Auto-saved Drafts*" t)
1603 (goto-char (point-min))
1604 (or (looking-at " Move to the draft file you want to recover,")
1605 (let ((inhibit-read-only t))
1606 ;; Each line starts with a space so that Font Lock mode
1607 ;; won't highlight the first character.
1609 Move to the draft file you want to recover, then type C-c C-c
1610 to recover text of message whose composition was interrupted.
1611 To browse text of a draft, type v on the draft file's line.
1613 You can also delete some of these files;
1614 type d on a line to mark that file for deletion.
1616 List of possible auto-save files for recovery:
1620 (let ((map (make-sparse-keymap)))
1621 (set-keymap-parent map (current-local-map))
1623 (define-key (current-local-map) "v"
1626 (let ((coding-system-for-read 'emacs-mule-unix))
1627 (dired-view-file))))
1628 (define-key (current-local-map) "\C-c\C-c"
1631 (let ((fname (dired-get-filename))
1632 ;; Auto-saved files are written in the internal
1633 ;; representation, so they should be read accordingly.
1634 (coding-system-for-read 'emacs-mule-unix))
1635 (switch-to-buffer-other-window "*mail*")
1636 (let ((buffer-read-only nil))
1638 (insert-file-contents fname nil)
1639 ;; insert-file-contents will set buffer-file-coding-system
1640 ;; to emacs-mule, which is probably not what they want to
1641 ;; use for sending the message. But we don't know what
1642 ;; was its value before the buffer was killed or Emacs
1643 ;; crashed. We therefore reset buffer-file-coding-system
1644 ;; to the default value, so that either the default does
1645 ;; TRT, or the user will get prompted for the right
1646 ;; encoding when they send the message.
1647 (setq buffer-file-coding-system
1648 default-buffer-file-coding-system))))))))
1650 (defun mail-recover ()
1651 "Recover interrupted mail composition from auto-save files.
1653 If the mail buffer has a current valid auto-save file,
1654 the command recovers that file. Otherwise, it displays a
1655 buffer showing the existing auto-saved draft messages;
1656 you can move to one of them and type C-c C-c to recover that one."
1658 ;; In case they invoke us from some random buffer...
1659 (switch-to-buffer "*mail*")
1660 ;; If *mail* didn't exist, set its directory, so that auto-saved
1661 ;; drafts will be found.
1662 (let ((dir (expand-file-name
1663 (file-name-as-directory mail-default-directory))))
1664 (if (file-exists-p dir)
1665 (setq default-directory dir)))
1666 (or (eq major-mode 'mail-mode)
1668 (let ((file-name buffer-auto-save-file-name))
1669 (cond ((and file-name (file-exists-p file-name))
1671 ;; This used to invoke `ls' via call-process, but
1672 ;; dired-noselect is more portable to systems where
1673 ;; `ls' is not a standard program (it will use
1674 ;; ls-lisp instead).
1675 (dired-noselect file-name
1676 (concat dired-listing-switches "t"))))
1677 (save-selected-window
1678 (select-window (display-buffer dispbuf t))
1679 (goto-char (point-min))
1681 (dired-move-to-filename)
1682 (setq dispbuf (rename-buffer "*Directory*" t)))
1683 (if (not (yes-or-no-p
1684 (format "Recover mail draft from auto save file %s? "
1686 (error "mail-recover cancelled")
1687 (let ((buffer-read-only nil)
1688 (buffer-coding buffer-file-coding-system)
1689 ;; Auto-save files are written in internal
1690 ;; representation of non-ASCII characters.
1691 (coding-system-for-read 'emacs-mule-unix))
1693 (insert-file-contents file-name nil)
1694 (setq buffer-file-coding-system buffer-coding)))))
1695 (t (mail-recover-1)))))
1698 (defun mail-other-window (&optional noerase to subject in-reply-to cc replybuffer sendactions)
1699 "Like `mail' command, but display mail buffer in another window."
1701 (let ((pop-up-windows t)
1702 (special-display-buffer-names nil)
1703 (special-display-regexps nil)
1704 (same-window-buffer-names nil)
1705 (same-window-regexps nil))
1706 (pop-to-buffer "*mail*"))
1707 (mail noerase to subject in-reply-to cc replybuffer sendactions))
1710 (defun mail-other-frame (&optional noerase to subject in-reply-to cc replybuffer sendactions)
1711 "Like `mail' command, but display mail buffer in another frame."
1713 (let ((pop-up-frames t)
1714 (special-display-buffer-names nil)
1715 (special-display-regexps nil)
1716 (same-window-buffer-names nil)
1717 (same-window-regexps nil))
1718 (pop-to-buffer "*mail*"))
1719 (mail noerase to subject in-reply-to cc replybuffer sendactions))
1721 ;;; Do not add anything but external entries on this page.
1725 ;;; sendmail.el ends here