1 ;;; sendmail.el --- mail sending commands for Emacs. -*- byte-compile-dynamic: t -*-
3 ;; Copyright (C) 1985, 1986, 1992, 1993, 1994, 1995, 1996, 1998, 2000,
4 ;; 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
5 ;; Free Software Foundation, Inc.
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 ;; This mode provides mail-sending facilities from within Emacs. It is
28 ;; documented in the Emacs user's manual.
33 (autoload 'rfc2047-encode-string "rfc2047")
35 (defgroup sendmail nil
36 "Mail sending commands for Emacs."
40 (defcustom mail-setup-with-from t
41 "Non-nil means insert `From:' field when setting up the message."
46 (defcustom sendmail-program
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 "fakemail")) ;In ../etc, to interface to /bin/mail.
52 "Program used to send messages."
57 (defcustom mail-from-style 'default
58 "Specifies how \"From:\" fields look.
60 If `nil', they contain just the return address like:
62 If `parens', they look like:
63 king@grassland.com (Elvis Parsley)
64 If `angles', they look like:
65 Elvis Parsley <king@grassland.com>
67 Otherwise, most addresses look like `angles', but they look like
68 `parens' if `angles' would need quoting and `parens' would not."
69 ;; The value `system-default' is now deprecated.
70 :type '(choice (const :tag "simple" nil)
78 (defcustom mail-specify-envelope-from nil
79 "If non-nil, specify the envelope-from address when sending mail.
80 The value used to specify it is whatever is found in
81 the variable `mail-envelope-from', with `user-mail-address' as fallback.
83 On most systems, specifying the envelope-from address is a
84 privileged operation. This variable affects sendmail and
85 smtpmail -- if you use feedmail to send mail, see instead the
86 variable `feedmail-deduce-envelope-from'."
91 (defcustom mail-envelope-from nil
92 "If non-nil, designate the envelope-from address when sending mail.
93 This only has an effect if `mail-specify-envelope-from' is non-nil.
94 The value should be either a string, or the symbol `header' (in
95 which case the contents of the \"From\" header of the message
96 being sent is used), or nil (in which case the value of
97 `user-mail-address' is used)."
99 :type '(choice (string :tag "From-name")
100 (const :tag "Use From: header from message" header)
101 (const :tag "Use `user-mail-address'" nil))
105 (defcustom mail-self-blind nil
106 "Non-nil means insert BCC to self in messages to be sent.
107 This is done when the message is initialized,
108 so you can remove or alter the BCC field to override the default."
113 (defcustom mail-interactive t
114 ;; We used to use a default of nil rather than t, but nowadays it is very
115 ;; common for sendmail to be misconfigured, so one cannot rely on the
116 ;; bounce message to be delivered anywhere, least of all to the
118 "Non-nil means when sending a message wait for and display errors.
119 Otherwise, let mailer send back a message to report errors."
121 :version "23.1" ; changed from nil to t
124 (defcustom mail-yank-ignored-headers
126 (regexp-opt '("via" "mail-from" "origin" "status" "remailed"
127 "received" "message-id" "summary-line" "to" "subject"
128 "in-reply-to" "return-path" "mail-reply-to"
129 ;; Should really be rmail-attribute-header and
130 ;; rmail-keyword-header, but this file does not
131 ;; require rmail (at run time).
132 "x-rmail-attributes" "x-rmail-keywords"
133 "mail-followup-to") "\\(?:")
135 "Delete these headers from old message when it's inserted in a reply."
140 ;; Prevent problems with `window-system' not having the correct value
141 ;; when loaddefs.el is loaded. `custom-reevaluate-setting' needs the
144 (put 'send-mail-function 'standard-value
145 '((if (and window-system (memq system-type '(darwin windows-nt)))
149 ;; Useful to set in site-init.el
151 (defcustom send-mail-function
152 (if (and window-system (memq system-type '(darwin windows-nt)))
155 "Function to call to send the current buffer as mail.
156 The headers should be delimited by a line which is
157 not a valid RFC822 header or continuation line,
158 that matches the variable `mail-header-separator'.
159 This is used by the default mail-sending commands. See also
160 `message-send-mail-function' for use with the Message package."
161 :type '(radio (function-item sendmail-send-it :tag "Use Sendmail package")
162 (function-item smtpmail-send-it :tag "Use SMTPmail package")
163 (function-item feedmail-send-it :tag "Use Feedmail package")
164 (function-item mailclient-send-it :tag "Use Mailclient package")
166 :initialize 'custom-initialize-delay
169 ;;;###autoload(custom-initialize-delay 'send-mail-function nil)
172 (defcustom mail-header-separator (purecopy "--text follows this line--")
173 "Line used to separate headers from text in messages being composed."
177 ;; Set up mail-header-separator for use as a category text property.
178 (put 'mail-header-separator 'rear-nonsticky '(category))
179 ;; This was a nice idea, for preventing accidental modification of
180 ;; the separator. But I found it also prevented or obstructed
181 ;; certain deliberate operations, such as copying the separator line
182 ;; up to the top to send myself a copy of an already sent outgoing message
183 ;; and other things. So I turned it off. --rms.
184 ;;(put 'mail-header-separator 'read-only t)
187 (defcustom mail-archive-file-name nil
188 "Name of file to write all outgoing messages in, or nil for none.
189 This is normally an mbox file, but for backwards compatibility may also
191 :type '(choice file (const nil))
195 (defcustom mail-default-reply-to nil
196 "Address to insert as default Reply-to field of outgoing messages.
197 If nil, it will be initialized from the REPLYTO environment variable
198 when you first send mail."
199 :type '(choice (const nil) string)
202 (defcustom mail-alias-file nil
203 "If non-nil, the name of a file to use instead of the sendmail default.
204 This file defines aliases to be expanded by the mailer; this is a different
205 feature from that of defining aliases in `.mailrc' to be expanded in Emacs.
206 This variable has no effect unless your system uses sendmail as its mailer.
207 The default file is defined in sendmail's configuration file, e.g.
209 :type '(choice (const :tag "Sendmail default" nil) file)
213 (defcustom mail-personal-alias-file (purecopy "~/.mailrc")
214 "If non-nil, the name of the user's personal mail alias file.
215 This file typically should be in same format as the `.mailrc' file used by
216 the `Mail' or `mailx' program.
217 This file need not actually exist."
218 :type '(choice (const nil) file)
222 (defcustom mail-setup-hook nil
223 "Normal hook, run each time a new outgoing message is initialized."
225 :options '(fortune-to-signature spook mail-abbrevs-setup)
229 (defvar mail-aliases t
230 "Alist of mail address aliases,
231 or t meaning should be initialized from your mail aliases file.
232 \(The file's name is normally `~/.mailrc', but `mail-personal-alias-file'
233 can specify a different file name.)
234 The alias definitions in the file have this form:
235 alias ALIAS MEANING")
237 (defvar mail-alias-modtime nil
238 "The modification time of your mail alias file when it was last examined.")
241 (defcustom mail-yank-prefix "> "
242 "Prefix insert on lines of yanked message being replied to.
243 If this is nil, use indentation, as specified by `mail-indentation-spaces'."
244 :type '(choice (const nil) string)
248 (defcustom mail-indentation-spaces 3
249 "Number of spaces to insert at the beginning of each cited line.
250 Used by `mail-yank-original' via `mail-indent-citation'."
254 ;; FIXME make it really obsolete.
255 (defvar mail-yank-hooks nil
256 "Obsolete hook for modifying a citation just inserted in the mail buffer.
257 Each hook function can find the citation between (point) and (mark t).
258 And each hook function should leave point and mark around the citation
261 This is a normal hook, misnamed for historical reasons.
262 It is semi-obsolete and mail agents should no longer use it.")
265 (defcustom mail-citation-hook nil
266 "Hook for modifying a citation just inserted in the mail buffer.
267 Each hook function can find the citation between (point) and (mark t),
268 and should leave point and mark around the citation text as modified.
269 The hook functions can find the header of the cited message
270 in the variable `mail-citation-header', whether or not this is included
271 in the cited portion of the message.
273 If this hook is entirely empty (nil), a default action is taken
274 instead of no action."
278 (defvar mail-citation-header nil
279 "While running `mail-citation-hook', this variable holds the message header.
280 This enables the hook functions to see the whole message header
281 regardless of what part of it (if any) is included in the cited text.")
284 (defcustom mail-citation-prefix-regexp
285 (purecopy "\\([ \t]*\\(\\w\\|[_.]\\)+>+\\|[ \t]*[]>|]\\)+")
286 "Regular expression to match a citation prefix plus whitespace.
287 It should match whatever sort of citation prefixes you want to handle,
288 with whitespace before and after; it should also match just whitespace.
289 The default value matches citations like `foo-bar>' plus whitespace."
294 (defvar mail-abbrevs-loaded nil)
295 (defvar mail-mode-map
296 (let ((map (make-sparse-keymap)))
297 (define-key map "\M-\t" 'mail-complete)
298 (define-key map "\C-c?" 'describe-mode)
299 (define-key map "\C-c\C-f\C-t" 'mail-to)
300 (define-key map "\C-c\C-f\C-b" 'mail-bcc)
301 (define-key map "\C-c\C-f\C-f" 'mail-fcc)
302 (define-key map "\C-c\C-f\C-c" 'mail-cc)
303 (define-key map "\C-c\C-f\C-s" 'mail-subject)
304 (define-key map "\C-c\C-f\C-r" 'mail-reply-to)
305 (define-key map "\C-c\C-f\C-a" 'mail-mail-reply-to) ; author
306 (define-key map "\C-c\C-f\C-l" 'mail-mail-followup-to) ; list
307 (define-key map "\C-c\C-t" 'mail-text)
308 (define-key map "\C-c\C-y" 'mail-yank-original)
309 (define-key map "\C-c\C-r" 'mail-yank-region)
310 (define-key map [remap split-line] 'mail-split-line)
311 (define-key map "\C-c\C-q" 'mail-fill-yanked-message)
312 (define-key map "\C-c\C-w" 'mail-signature)
313 (define-key map "\C-c\C-v" 'mail-sent-via)
314 (define-key map "\C-c\C-c" 'mail-send-and-exit)
315 (define-key map "\C-c\C-s" 'mail-send)
316 (define-key map "\C-c\C-i" 'mail-attach-file)
317 ;; FIXME add this? "b" = bury buffer. It's in the menu-bar.
318 ;;; (define-key map "\C-c\C-b" 'mail-dont-send)
320 (define-key map [menu-bar mail]
321 (cons "Mail" (make-sparse-keymap "Mail")))
323 (define-key map [menu-bar mail fill]
324 '("Fill Citation" . mail-fill-yanked-message))
326 (define-key map [menu-bar mail yank]
327 '(menu-item "Cite Original" mail-yank-original :enable mail-reply-action))
329 (define-key map [menu-bar mail signature]
330 '("Insert Signature" . mail-signature))
332 (define-key map [menu-bar mail mail-sep]
335 (define-key map [menu-bar mail cancel]
336 '("Cancel" . mail-dont-send))
338 (define-key map [menu-bar mail send-stay]
339 '("Send, Keep Editing" . mail-send))
341 (define-key map [menu-bar mail send]
342 '("Send Message" . mail-send-and-exit))
344 (define-key map [menu-bar headers]
345 (cons "Headers" (make-sparse-keymap "Move to Header")))
347 (define-key map [menu-bar headers text]
348 '("Text" . mail-text))
350 (define-key map [menu-bar headers expand-aliases]
351 '("Expand Aliases" . expand-mail-aliases))
353 (define-key map [menu-bar headers sent-via]
354 '("Sent-Via" . mail-sent-via))
356 (define-key map [menu-bar headers mail-reply-to]
357 '("Mail-Reply-To" . mail-mail-reply-to))
359 (define-key map [menu-bar headers mail-followup-to]
360 '("Mail-Followup-To" . mail-mail-followup-to))
362 (define-key map [menu-bar headers reply-to]
363 '("Reply-To" . mail-reply-to))
365 (define-key map [menu-bar headers bcc]
368 (define-key map [menu-bar headers fcc]
371 (define-key map [menu-bar headers cc]
374 (define-key map [menu-bar headers subject]
375 '("Subject" . mail-subject))
377 (define-key map [menu-bar headers to]
382 (autoload 'build-mail-aliases "mailalias"
383 "Read mail aliases from personal aliases file and set `mail-aliases'.
384 By default, this is the file specified by `mail-personal-alias-file'." t)
387 (defcustom mail-signature t
388 "Text inserted at end of mail buffer when a message is initialized.
389 If t, it means to insert the contents of the file `mail-signature-file'.
390 If a string, that string is inserted.
391 (To make a proper signature, the string should begin with \\n\\n-- \\n,
392 which is the standard way to delimit a signature in a message.)
393 Otherwise, it should be an expression; it is evaluated
394 and should insert whatever you want to insert."
395 :type '(choice (const :tag "None" nil)
396 (const :tag "Use `.signature' file" t)
397 (string :tag "String to insert")
398 (sexp :tag "Expression to evaluate"))
400 (put 'mail-signature 'risky-local-variable t)
403 (defcustom mail-signature-file (purecopy "~/.signature")
404 "File containing the text inserted at end of mail buffer."
409 (defcustom mail-default-directory (purecopy "~/")
410 "Value of `default-directory' for Mail mode buffers.
411 This directory is used for auto-save files of Mail mode buffers.
413 Note that Message mode does not use this variable; it auto-saves
414 in `message-auto-save-directory'."
415 :type '(directory :tag "Directory")
419 (defvar mail-reply-action nil)
420 (defvar mail-send-actions nil
421 "A list of actions to be performed upon successful sending of a message.")
422 (defvar mail-return-action nil)
425 (defcustom mail-default-headers nil
426 "A string containing header lines, to be inserted in outgoing messages.
427 It can contain newlines, and should end in one. It is inserted
428 before you edit the message, so you can edit or delete the lines."
429 :type '(choice (const nil) string)
432 (defcustom mail-bury-selects-summary t
433 "If non-nil, try to show Rmail summary buffer after returning from mail.
434 The functions \\[mail-send-on-exit] or \\[mail-dont-send] select
435 the Rmail summary buffer before returning, if it exists and this variable
440 (defcustom mail-send-nonascii 'mime
441 "Specify whether to allow sending non-ASCII characters in mail.
442 If t, that means do allow it. nil means don't allow it.
443 `query' means ask the user each time.
444 `mime' means add an appropriate MIME header if none already present.
445 The default is `mime'.
446 Including non-ASCII characters in a mail message can be problematical
447 for the recipient, who may not know how to decode them properly."
448 :type '(choice (const t) (const nil) (const query) (const mime))
451 (defcustom mail-use-dsn nil
452 "Ask MTA for notification of failed, delayed or successful delivery.
453 Note that only some MTAs (currently only recent versions of Sendmail)
454 support Delivery Status Notification."
456 :type '(repeat (radio (const :tag "Failure" failure)
457 (const :tag "Delay" delay)
458 (const :tag "Success" success)))
461 ;; Note: could use /usr/ucb/mail instead of sendmail;
462 ;; options -t, and -v if not interactive.
463 (defvar mail-mailer-swallows-blank-line nil
464 "Set this non-nil if the system's mailer runs the header and body together.
465 The actual value should be an expression to evaluate that returns
466 non-nil if the problem will actually occur.
467 \(As far as we know, this is not an issue on any system still supported
470 (put 'mail-mailer-swallows-blank-line 'risky-local-variable t) ; gets evalled
471 (make-obsolete-variable 'mail-mailer-swallows-blank-line
472 "no need to set this on any modern system." "24.1")
474 (defvar mail-mode-syntax-table
475 ;; define-derived-mode will make it inherit from text-mode-syntax-table.
476 (let ((st (make-syntax-table)))
477 ;; FIXME this is probably very obsolete now ("percent hack").
478 ;; sending.texi used to say:
479 ;; Mail mode defines the character `%' as a word separator; this
480 ;; is helpful for using the word commands to edit mail addresses.
481 (modify-syntax-entry ?% ". " st)
483 "Syntax table used while in `mail-mode'.")
485 (defvar mail-font-lock-keywords
487 (let* ((cite-chars "[>|}]")
488 (cite-prefix "[:alpha:]")
489 (cite-suffix (concat cite-prefix "0-9_.@-`'\"")))
490 (list '("^\\(To\\|Newsgroups\\):" . font-lock-function-name-face)
491 '("^\\(B?CC\\|Reply-to\\|Mail-\\(reply\\|followup\\)-to\\):" . font-lock-keyword-face)
492 '("^\\(Subject:\\)[ \t]*\\(.+\\)?"
493 (1 font-lock-comment-face)
494 ;; (2 font-lock-type-face nil t)
496 ;; Use EVAL to delay in case `mail-header-separator' gets changed.
498 (let ((separator (if (zerop (length mail-header-separator))
500 (regexp-quote mail-header-separator))))
501 (cons (concat "^" separator "$") 'font-lock-warning-face)))
502 ;; Use MATCH-ANCHORED to effectively anchor the regexp left side.
504 (,(concat "\\=[ \t]*"
505 "\\(\\(\\([" cite-prefix "]+[" cite-suffix "]*\\)?"
506 "\\(" cite-chars "[ \t]*\\)\\)+\\)"
508 (beginning-of-line) (end-of-line)
509 (1 font-lock-comment-delimiter-face nil t)
510 (5 font-lock-comment-face nil t)))
511 '("^\\(X-[A-Za-z0-9-]+\\|In-reply-to\\):.*\\(\n[ \t]+.*\\)*$"
512 . font-lock-string-face))))
513 "Additional expressions to highlight in Mail mode.")
516 (defun sendmail-sync-aliases ()
517 (when mail-personal-alias-file
518 (let ((modtime (nth 5 (file-attributes mail-personal-alias-file))))
519 (or (equal mail-alias-modtime modtime)
520 (setq mail-alias-modtime modtime
525 (define-mail-user-agent 'sendmail-user-agent
526 'sendmail-user-agent-compose
530 (defun sendmail-user-agent-compose (&optional to subject other-headers
531 continue switch-function yank-action
532 send-actions return-action
535 (let ((special-display-buffer-names nil)
536 (special-display-regexps nil)
537 (same-window-buffer-names nil)
538 (same-window-regexps nil))
539 (funcall switch-function "*mail*")))
540 (let ((cc (cdr (assoc-string "cc" other-headers t)))
541 (in-reply-to (cdr (assoc-string "in-reply-to" other-headers t)))
542 (body (cdr (assoc-string "body" other-headers t))))
543 (or (mail continue to subject in-reply-to cc yank-action
544 send-actions return-action)
546 (error "Message aborted"))
550 (unless (member-ignore-case (car (car other-headers))
551 '("in-reply-to" "cc" "body"))
552 (insert (car (car other-headers)) ": "
553 (cdr (car other-headers))
554 (if use-hard-newlines hard-newline "\n")))
555 (setq other-headers (cdr other-headers)))
561 (defun mail-setup (to subject in-reply-to cc replybuffer
562 actions return-action)
563 (or mail-default-reply-to
564 (setq mail-default-reply-to (getenv "REPLYTO")))
565 (sendmail-sync-aliases)
566 (when (eq mail-aliases t)
567 (setq mail-aliases nil)
568 (and mail-personal-alias-file
569 (file-exists-p mail-personal-alias-file)
570 (build-mail-aliases)))
571 ;; Don't leave this around from a previous message.
572 (kill-local-variable 'buffer-file-coding-system)
573 ;; This doesn't work for enable-multibyte-characters.
574 ;; (kill-local-variable 'enable-multibyte-characters)
575 (set-buffer-multibyte (default-value 'enable-multibyte-characters))
576 (if current-input-method
577 (inactivate-input-method))
579 ;; Local variables for Mail mode.
580 (setq mail-send-actions actions)
581 (setq mail-reply-action replybuffer)
582 (setq mail-return-action return-action)
584 (goto-char (point-min))
585 (if mail-setup-with-from
586 (mail-insert-from-field))
590 ;; Here removed code to extract names from within <...>
591 ;; on the assumption that mail-strip-quoted-names
592 ;; has been called and has done so.
593 (let ((fill-prefix "\t")
594 (address-start (point)))
596 (fill-region-as-paragraph address-start (point-max))
597 (goto-char (point-max))
602 (let ((fill-prefix "\t")
603 (address-start (progn (insert "CC: ") (point))))
605 (fill-region-as-paragraph address-start (point-max))
606 (goto-char (point-max))
610 (let ((fill-prefix "\t")
612 (address-start (point)))
613 (insert "In-reply-to: " in-reply-to "\n")
614 (fill-region-as-paragraph address-start (point-max))
615 (goto-char (point-max))
618 (insert "Subject: " (or subject "") "\n")
619 (if mail-default-headers
620 (insert mail-default-headers))
621 (if mail-default-reply-to
622 (insert "Reply-to: " mail-default-reply-to "\n"))
624 (insert "BCC: " user-mail-address "\n"))
625 (if mail-archive-file-name
626 (insert "FCC: " mail-archive-file-name "\n"))
627 (put-text-property (point)
629 (insert mail-header-separator "\n")
631 'category 'mail-header-separator)
632 ;; Insert the signature. But remember the beginning of the message.
633 (if to (setq to (point)))
634 (if mail-signature (mail-signature t))
635 (goto-char (point-max))
636 (or (bolp) (newline)))
637 (if to (goto-char to))
638 (or to subject in-reply-to
639 (set-buffer-modified-p nil))
640 (run-hooks 'mail-setup-hook))
642 (defcustom mail-mode-hook nil
643 "Hook run by Mail mode.
644 When composing a mail, this runs immediately after creating, or
645 switching to, the `*mail*' buffer. See also `mail-setup-hook'."
648 :options '(footnote-mode))
650 (defvar mail-mode-abbrev-table text-mode-abbrev-table)
652 (define-derived-mode mail-mode text-mode "Mail"
653 "Major mode for editing mail to be sent.
654 Like Text Mode but with these additional commands:
656 \\[mail-send] mail-send (send the message)
657 \\[mail-send-and-exit] mail-send-and-exit (send the message and exit)
659 Here are commands that move to a header field (and create it if there isn't):
660 \\[mail-to] move to To: \\[mail-subject] move to Subj:
661 \\[mail-bcc] move to BCC: \\[mail-cc] move to CC:
662 \\[mail-fcc] move to FCC: \\[mail-reply-to] move to Reply-To:
663 \\[mail-mail-reply-to] move to Mail-Reply-To:
664 \\[mail-mail-followup-to] move to Mail-Followup-To:
665 \\[mail-text] move to message text.
666 \\[mail-signature] mail-signature (insert `mail-signature-file' file).
667 \\[mail-yank-original] mail-yank-original (insert current message, in Rmail).
668 \\[mail-fill-yanked-message] mail-fill-yanked-message (fill what was yanked).
669 \\[mail-sent-via] mail-sent-via (add a sent-via field for each To or CC).
670 Turning on Mail mode runs the normal hooks `text-mode-hook' and
671 `mail-mode-hook' (in that order)."
672 (make-local-variable 'mail-reply-action)
673 (make-local-variable 'mail-send-actions)
674 (make-local-variable 'mail-return-action)
675 (setq buffer-offer-save t)
676 (make-local-variable 'font-lock-defaults)
677 (setq font-lock-defaults '(mail-font-lock-keywords t t))
678 (make-local-variable 'paragraph-separate)
679 (make-local-variable 'normal-auto-fill-function)
680 (setq normal-auto-fill-function 'mail-mode-auto-fill)
681 (make-local-variable 'fill-paragraph-function)
682 (setq fill-paragraph-function 'mail-mode-fill-paragraph)
683 ;; Allow using comment commands to add/remove quoting (this only does
684 ;; anything if mail-yank-prefix is set to a non-nil value).
685 (set (make-local-variable 'comment-start) mail-yank-prefix)
687 (set (make-local-variable 'comment-start-skip)
688 (concat "^" (regexp-quote mail-yank-prefix) "[ \t]*")))
689 (make-local-variable 'adaptive-fill-regexp)
690 (setq adaptive-fill-regexp
691 (concat "[ \t]*[-[:alnum:]]+>+[ \t]*\\|"
692 adaptive-fill-regexp))
693 (make-local-variable 'adaptive-fill-first-line-regexp)
694 (setq adaptive-fill-first-line-regexp
695 (concat "[ \t]*[-[:alnum:]]*>+[ \t]*\\|"
696 adaptive-fill-first-line-regexp))
697 ;; `-- ' precedes the signature. `-----' appears at the start of the
698 ;; lines that delimit forwarded messages.
699 ;; Lines containing just >= 3 dashes, perhaps after whitespace,
700 ;; are also sometimes used and should be separators.
701 (setq paragraph-separate (concat (regexp-quote mail-header-separator)
702 "$\\|\t*\\([-|#;>* ]\\|(?[0-9]+[.)]\\)+$"
703 "\\|[ \t]*[[:alnum:]]*>+[ \t]*$\\|[ \t]*$\\|"
708 (defun mail-header-end ()
709 "Return the buffer location of the end of headers, as a number."
716 (defun mail-text-start ()
717 "Return the buffer location of the start of text, as a number."
725 (defun mail-sendmail-delimit-header ()
726 "Set up whatever header delimiter convention sendmail will use.
727 Concretely: replace the first blank line in the header with the separator."
729 (insert mail-header-separator)
732 (defun mail-sendmail-undelimit-header ()
733 "Remove header separator to put the message in correct form for sendmail.
734 Leave point at the start of the delimiter line."
736 (delete-region (point) (progn (end-of-line) (point))))
738 (defun mail-mode-auto-fill ()
739 "Carry out Auto Fill for Mail mode.
740 If within the headers, this makes the new lines into continuation lines."
741 (if (< (point) (mail-header-end))
742 (let ((old-line-start (line-beginning-position)))
746 (while (not (eq (point) old-line-start))
747 ;; Use insert-before-markers in case we're inserting
748 ;; before the saved value of point (which is common).
749 (insert-before-markers " ")
754 (defun mail-mode-fill-paragraph (arg)
755 ;; Do something special only if within the headers.
756 (if (< (point) (mail-header-end))
757 (let (beg end fieldname)
758 (when (prog1 (re-search-backward "^[-a-zA-Z]+:" nil 'yes)
761 (downcase (buffer-substring beg (1- (match-end 0))))))
763 ;; Find continuation lines and get rid of their continuation markers.
764 (while (looking-at "[ \t]")
765 (delete-horizontal-space)
767 (setq end (point-marker))
769 ;; If this field contains addresses,
770 ;; make sure we can fill after each address.
771 (if (member fieldname
772 '("to" "cc" "bcc" "from" "reply-to"
773 "mail-reply-to" "mail-followup-to"
774 "resent-to" "resent-cc" "resent-bcc"
775 "resent-from" "resent-reply-to"))
776 (while (search-forward "," end t)
777 (or (looking-at "[ \t]")
779 (fill-region-as-paragraph beg end arg)
780 ;; Mark all lines except the first as continuations.
783 (while (< (point) end)
786 (move-marker end nil)
789 ;; User-level commands for sending.
791 (defun mail-send-and-exit (&optional arg)
792 "Send message like `mail-send', then, if no errors, exit from mail buffer.
793 Prefix arg means don't delete this window."
798 (defun mail-dont-send (&optional arg)
799 "Don't send the message you have been editing.
800 Prefix arg means don't delete this window."
804 (defun mail-bury (&optional arg)
805 "Bury this mail buffer."
806 (let ((newbuf (other-buffer (current-buffer))))
807 (bury-buffer (current-buffer))
808 (if (and (null arg) mail-return-action)
809 (apply (car mail-return-action) (cdr mail-return-action))
810 (switch-to-buffer newbuf))))
812 (defcustom mail-send-hook nil
813 "Hook run just before sending a message."
815 :options '(flyspell-mode-off)
819 (defcustom mail-mailing-lists nil
820 "List of mailing list addresses the user is subscribed to.
821 The variable is used to trigger insertion of the \"Mail-Followup-To\"
822 header when sending a message to a mailing list."
823 :type '(repeat string)
828 "Send the message in the current buffer.
829 If `mail-interactive' is non-nil, wait for success indication
830 or error messages, and inform user.
831 Otherwise any failure is reported in a message back to
832 the user from the mailer."
834 (if (if buffer-file-name
835 (y-or-n-p "Send buffer contents as mail message? ")
836 (or (buffer-modified-p)
837 (y-or-n-p "Message already sent; resend? ")))
838 (let ((inhibit-read-only t)
840 (ml (when mail-mailing-lists
841 ;; The surrounding regexp assumes the use of
842 ;; `mail-strip-quoted-names' on addresses before matching
843 ;; Cannot deal with full RFC 822 freedom, but that is
844 ;; unlikely to be problematic.
845 (concat "\\(?:[[:space:];,]\\|\\`\\)"
846 (regexp-opt mail-mailing-lists t)
847 "\\(?:[[:space:];,]\\|\\'\\)"))))
848 ;; If there are mailing lists defined
851 (let* ((to (mail-fetch-field "to" nil t))
852 (cc (mail-fetch-field "cc" nil t))
853 (new-header-values ; To: and Cc:
854 (mail-strip-quoted-names
855 (concat to (when cc (concat ", " cc))))))
856 ;; If message goes to known mailing list ...
857 (when (string-match ml new-header-values)
858 ;; Add Mail-Followup-To if none yet
859 (unless (mail-fetch-field "mail-followup-to")
860 (goto-char (mail-header-end))
861 (insert "Mail-Followup-To: "
868 (split-string new-header-values
870 (mapconcat 'identity l ", "))
872 ;; Add Mail-Reply-To if none yet
873 (unless (mail-fetch-field "mail-reply-to")
874 (goto-char (mail-header-end))
875 (insert "Mail-Reply-To: "
876 (or (mail-fetch-field "reply-to")
879 (unless (memq mail-send-nonascii '(t mime))
880 (goto-char (point-min))
881 (skip-chars-forward "\0-\177")
882 (or (= (point) (point-max))
883 (if (eq mail-send-nonascii 'query)
884 (or (y-or-n-p "Message contains non-ASCII characters; send anyway? ")
886 (error "Message contains non-ASCII characters"))))
887 ;; Complain about any invalid line.
888 (goto-char (point-min))
889 (re-search-forward (regexp-quote mail-header-separator) (point-max) t)
890 (let ((header-end (or (match-beginning 0) (point-max))))
891 (goto-char (point-min))
892 (while (< (point) header-end)
893 (unless (looking-at "[ \t]\\|.*:\\|$")
895 (error "Invalid header line (maybe a continuation line lacks initial whitespace)"))
898 (run-hooks 'mail-send-hook)
899 (message "Sending...")
900 (funcall send-mail-function)
901 ;; Now perform actions on successful sending.
902 (while mail-send-actions
904 (apply (car (car mail-send-actions))
905 (cdr (car mail-send-actions)))
907 (setq mail-send-actions (cdr mail-send-actions)))
908 (message "Sending...done")
909 ;; If buffer has no file, mark it as unmodified and delete auto-save.
910 (if (not buffer-file-name)
912 (set-buffer-modified-p nil)
913 (delete-auto-save-file-if-necessary t))))))
915 (defun mail-envelope-from ()
916 "Return the envelope mail address to use when sending mail.
917 This function uses `mail-envelope-from'."
918 (if (eq mail-envelope-from 'header)
919 (nth 1 (mail-extract-address-components
920 (mail-fetch-field "From")))
923 ;; This does the real work of sending a message via sendmail.
924 ;; It is called via the variable send-mail-function.
927 (defvar sendmail-coding-system nil
928 "*Coding system for encoding the outgoing mail.
929 This has higher priority than the default `buffer-file-coding-system'
930 and `default-sendmail-coding-system',
931 but lower priority than the local value of `buffer-file-coding-system'.
932 See also the function `select-message-coding-system'.")
935 (defvar default-sendmail-coding-system 'iso-latin-1
936 "Default coding system for encoding the outgoing mail.
937 This variable is used only when `sendmail-coding-system' is nil.
939 This variable is set/changed by the command `set-language-environment'.
940 User should not set this variable manually,
941 instead use `sendmail-coding-system' to get a constant encoding
942 of outgoing mails regardless of the current language environment.
943 See also the function `select-message-coding-system'.")
945 (defun mail-insert-from-field ()
946 (let* ((login user-mail-address)
947 (fullname (user-full-name))
948 (quote-fullname nil))
949 (if (string-match "[^\0-\177]" fullname)
950 (setq fullname (rfc2047-encode-string fullname)
952 (cond ((null mail-from-style)
953 (insert "From: " login "\n"))
954 ;; This is deprecated.
955 ((eq mail-from-style 'system-default)
957 ((or (eq mail-from-style 'angles)
958 (and (not (eq mail-from-style 'parens))
959 ;; Use angles if no quoting is needed, or if
960 ;; parens would need quoting too.
961 (or (not (string-match "[^- !#-'*+/-9=?A-Z^-~]" fullname))
962 (let ((tmp (concat fullname nil)))
963 (while (string-match "([^()]*)" tmp)
964 (aset tmp (match-beginning 0) ?-)
965 (aset tmp (1- (match-end 0)) ?-))
966 (string-match "[\\()]" tmp)))))
967 (insert "From: " fullname)
968 (let ((fullname-start (+ (point-min) 6))
969 (fullname-end (point-marker)))
970 (goto-char fullname-start)
971 ;; Look for a character that cannot appear unquoted
972 ;; according to RFC 822.
973 (if (or (re-search-forward "[^- !#-'*+/-9=?A-Z^-~]"
977 ;; Quote fullname, escaping specials.
978 (goto-char fullname-start)
980 (while (re-search-forward "[\"\\]"
982 (replace-match "\\\\\\&" t))
984 (insert " <" login ">\n"))
985 ;; 'parens or default
987 (insert "From: " login " (")
988 (let ((fullname-start (point)))
994 (let ((fullname-end (point-marker)))
995 (goto-char fullname-start)
996 ;; RFC 822 says \ and nonmatching parentheses
997 ;; must be escaped in comments.
998 ;; Escape every instance of ()\ ...
999 (while (re-search-forward "[()\\]" fullname-end 1)
1000 (replace-match "\\\\\\&" t))
1001 ;; ... then undo escaping of matching parentheses,
1002 ;; including matching nested parentheses.
1003 (goto-char fullname-start)
1004 (while (re-search-forward
1005 "\\(\\=\\|[^\\]\\(\\\\\\\\\\)*\\)\\\\(\\(\\([^\\]\\|\\\\\\\\\\)*\\)\\\\)"
1007 (replace-match "\\1(\\3)" t)
1008 (goto-char fullname-start))))
1011 ;; Normally you will not need to modify these options unless you are
1012 ;; using some non-genuine substitute for sendmail which does not
1013 ;; implement each and every option that the original supports.
1014 ;; E.g., ssmtp does not support "-odb", so, if your site uses it,
1015 ;; you will need to modify `sendmail-error-reporting-non-interactive'
1016 ;; in your site-init.el.
1017 (defvar sendmail-error-reporting-interactive
1018 ;; These mean "report errors to terminal" and "deliver interactively"
1020 (defvar sendmail-error-reporting-non-interactive
1021 ;; These mean "report errors by mail" and "deliver in background".
1024 (defun sendmail-send-it ()
1025 "Send the current mail buffer using the Sendmail package.
1026 This is a suitable value for `send-mail-function'. It sends using the
1027 external program defined by `sendmail-program'."
1028 (require 'mail-utils)
1029 (let ((errbuf (if mail-interactive
1030 (generate-new-buffer " sendmail errors")
1032 (tembuf (generate-new-buffer " sendmail temp"))
1033 (multibyte enable-multibyte-characters)
1034 (case-fold-search nil)
1035 (selected-coding (select-message-coding-system))
1039 (mailbuf (current-buffer))
1040 (program (if (boundp 'sendmail-program)
1042 "/usr/lib/sendmail"))
1043 ;; Examine these variables now, so that
1044 ;; local binding in the mail buffer will take effect.
1046 (and mail-specify-envelope-from
1047 (or (mail-envelope-from) user-mail-address))))
1049 (with-current-buffer tembuf
1052 (set-buffer-multibyte nil))
1053 (insert-buffer-substring mailbuf)
1054 (goto-char (point-max))
1055 ;; require one newline at the end.
1056 (or (= (preceding-char) ?\n)
1058 ;; Change header-delimiter to be what sendmail expects.
1059 (goto-char (mail-header-end))
1060 (delete-region (point) (progn (end-of-line) (point)))
1061 (setq delimline (point-marker))
1062 (sendmail-sync-aliases)
1064 (expand-mail-aliases (point-min) delimline))
1065 (goto-char (point-min))
1066 ;; Ignore any blank lines in the header
1067 (while (and (re-search-forward "\n\n\n*" delimline t)
1068 (< (point) delimline))
1069 (replace-match "\n"))
1070 (goto-char (point-min))
1071 ;; Look for Resent- headers. They require sending
1072 ;; the message specially.
1073 (let ((case-fold-search t))
1074 (goto-char (point-min))
1075 (while (re-search-forward "^Resent-\\(to\\|cc\\|bcc\\):" delimline t)
1076 ;; Put a list of such addresses in resend-to-addresses.
1077 (setq resend-to-addresses
1079 (narrow-to-region (point)
1082 (while (looking-at "^[ \t]")
1085 (append (mail-parse-comma-list)
1086 resend-to-addresses)))
1087 ;; Delete Resent-BCC ourselves
1088 (if (save-excursion (beginning-of-line)
1089 (looking-at "resent-bcc"))
1090 (delete-region (line-beginning-position)
1091 (line-beginning-position 2))))
1092 ;; Apparently this causes a duplicate Sender.
1093 ;; ;; If the From is different than current user, insert Sender.
1094 ;; (goto-char (point-min))
1095 ;; (and (re-search-forward "^From:" delimline t)
1097 ;; (require 'mail-utils)
1098 ;; (not (string-equal
1099 ;; (mail-strip-quoted-names
1100 ;; (save-restriction
1101 ;; (narrow-to-region (point-min) delimline)
1102 ;; (mail-fetch-field "From")))
1103 ;; (user-login-name))))
1106 ;; (insert "Sender: " (user-login-name) "\n")))
1107 ;; Don't send out a blank subject line
1108 (goto-char (point-min))
1109 (if (re-search-forward "^Subject:\\([ \t]*\n\\)+\\b" delimline t)
1111 ;; This one matches a Subject just before the header delimiter.
1112 (if (and (re-search-forward "^Subject:\\([ \t]*\n\\)+" delimline t)
1113 (= (match-end 0) delimline))
1114 (replace-match "")))
1115 ;; Put the "From:" field in unless for some odd reason
1116 ;; they put one in themselves.
1117 (goto-char (point-min))
1118 (if (not (re-search-forward "^From:" delimline t))
1119 (mail-insert-from-field))
1120 ;; Possibly add a MIME header for the current coding system
1121 (let (charset where-content-type)
1122 (goto-char (point-min))
1123 (setq where-content-type
1124 (re-search-forward "^Content-type:" delimline t))
1125 (goto-char (point-min))
1126 (and (eq mail-send-nonascii 'mime)
1127 (not (re-search-forward "^MIME-version:" delimline t))
1128 (progn (skip-chars-forward "\0-\177")
1129 (/= (point) (point-max)))
1132 (coding-system-get selected-coding :mime-charset))
1134 (goto-char delimline)
1135 (insert "MIME-version: 1.0\n"
1136 "Content-type: text/plain; charset="
1137 (symbol-name charset)
1138 "\nContent-Transfer-Encoding: 8bit\n")
1139 ;; The character set we will actually use
1140 ;; should override any specified in the message itself.
1141 (when where-content-type
1142 (goto-char where-content-type)
1143 (delete-region (point-at-bol)
1144 (progn (forward-line 1) (point)))))))
1145 ;; Insert an extra newline if we need it to work around
1146 ;; Sun's bug that swallows newlines.
1147 (goto-char (1+ delimline))
1148 (if (eval mail-mailer-swallows-blank-line)
1150 ;; Find and handle any FCC fields.
1151 (goto-char (point-min))
1152 (if (re-search-forward "^FCC:" delimline t)
1154 (setq fcc-was-found t)
1155 (mail-do-fcc delimline)))
1156 (if mail-interactive
1157 (with-current-buffer errbuf
1159 (goto-char (point-min))
1160 (if (let ((case-fold-search t))
1161 (or resend-to-addresses
1162 (re-search-forward "^To:\\|^cc:\\|^bcc:"
1164 (let* ((default-directory "/")
1165 (coding-system-for-write selected-coding)
1167 (append (list (point-min) (point-max)
1169 nil errbuf nil "-oi")
1171 (list "-f" envelope-from))
1172 ;; ;; Don't say "from root" if running under su.
1173 ;; (and (equal (user-real-login-name) "root")
1174 ;; (list "-f" (user-login-name)))
1175 (and mail-alias-file
1176 (list (concat "-oA" mail-alias-file)))
1177 (if mail-interactive
1178 sendmail-error-reporting-interactive
1179 sendmail-error-reporting-non-interactive)
1180 ;; Get the addresses from the message
1181 ;; unless this is a resend.
1182 ;; We must not do that for a resend
1183 ;; because we would find the original addresses.
1184 ;; For a resend, include the specific addresses.
1185 (or resend-to-addresses
1189 (list "-N" (mapconcat 'symbol-name
1193 (exit-value (apply 'call-process-region args)))
1194 (cond ((or (null exit-value) (eq 0 exit-value)))
1195 ((numberp exit-value)
1196 (error "Sending...failed with exit value %d" exit-value))
1197 ((stringp exit-value)
1198 (error "Sending...terminated by signal: %s" exit-value))
1200 (error "SENDMAIL-SEND-IT -- fall through: %S" exit-value))))
1202 (error "No recipients")))
1203 (if mail-interactive
1204 (with-current-buffer errbuf
1205 (goto-char (point-min))
1206 (while (re-search-forward "\n\n* *" nil t)
1207 (replace-match "; "))
1208 (if (not (zerop (buffer-size)))
1209 (error "Sending...failed to %s"
1210 (buffer-substring (point-min) (point-max)))))))
1211 (kill-buffer tembuf)
1212 (if (bufferp errbuf)
1213 (kill-buffer errbuf)))))
1215 (autoload 'rmail-output-to-rmail-buffer "rmailout")
1217 (defun mail-do-fcc (header-end)
1218 "Find and act on any FCC: headers in the current message before HEADER-END.
1219 If a buffer is visiting the FCC file, append to it before
1220 offering to save it, if it was modified initially. If this is an
1221 Rmail buffer, update Rmail as needed. If there is no buffer,
1222 just append to the file, in Babyl format if necessary."
1223 (unless (markerp header-end)
1224 (error "Value of `header-end' must be a marker"))
1226 (mailbuf (current-buffer))
1227 (time (current-time)))
1229 (goto-char (point-min))
1230 (let ((case-fold-search t))
1231 (while (re-search-forward "^FCC:[ \t]*" header-end t)
1232 (push (buffer-substring (point)
1235 (skip-chars-backward " \t")
1238 (delete-region (match-beginning 0)
1239 (progn (forward-line 1) (point)))))
1241 ;; This initial newline is not written out if we create a new
1242 ;; file (see below).
1243 (insert "\nFrom " (user-login-name) " " (current-time-string time) "\n")
1244 ;; Insert the time zone before the year.
1247 (require 'mail-utils)
1248 (insert (mail-rfc822-time-zone time) " ")
1249 (goto-char (point-max))
1250 (insert-buffer-substring mailbuf)
1251 ;; Make sure messages are separated.
1252 (goto-char (point-max))
1255 ;; ``Quote'' "^From " as ">From "
1256 ;; (note that this isn't really quoting, as there is no requirement
1257 ;; that "^[>]+From " be quoted in the same transparent way.)
1258 (let ((case-fold-search nil))
1259 (while (search-forward "\nFrom " nil t)
1262 (dolist (fcc fcc-list)
1263 (let* ((buffer (find-buffer-visiting fcc))
1264 (curbuf (current-buffer))
1267 (beg (point-min)) ; the initial blank line
1269 ;; After the ^From line.
1270 (beg2 (save-excursion (goto-char (point-min))
1271 (forward-line 2) (point))))
1273 ;; File is present in a buffer => append to that buffer.
1274 (with-current-buffer buffer
1275 (setq buffer-matches-file
1276 (and (not (buffer-modified-p))
1277 (verify-visited-file-modtime buffer)))
1278 (let ((msg (bound-and-true-p rmail-current-message))
1279 (buffer-read-only nil))
1280 ;; If MSG is non-nil, buffer is in Rmail mode.
1282 (let ((buff (generate-new-buffer " *mail-do-fcc")))
1285 (with-current-buffer buff
1286 (insert-buffer-substring curbuf (1+ beg) end))
1287 (rmail-output-to-rmail-buffer buff msg))
1288 (kill-buffer buff)))
1289 ;; Output file not in Rmail mode => just insert
1293 (goto-char (point-max))
1294 (insert-buffer-substring curbuf beg end)))
1295 ;; Offer to save the buffer if it was modified
1296 ;; before we started.
1297 (unless buffer-matches-file
1298 (if (y-or-n-p (format "Save file %s? " fcc))
1300 (setq dont-write-the-file t)))))
1301 ;; Append to the file directly, unless we've already taken
1303 (unless dont-write-the-file
1304 (if (and (file-exists-p fcc)
1305 (mail-file-babyl-p fcc))
1306 ;; If the file is a Babyl file, convert the message to
1307 ;; Babyl format. Even though Rmail no longer uses
1308 ;; Babyl, this code can remain for the time being, on
1309 ;; the off-chance one FCCs to a Babyl file that has
1310 ;; not yet been converted to mbox.
1311 (let ((coding-system-for-write
1312 (or rmail-file-coding-system 'emacs-mule)))
1314 (insert "\C-l\n0, unseen,,\n*** EOOH ***\nDate: "
1315 (mail-rfc822-date) "\n")
1316 (insert-buffer-substring curbuf beg2 end)
1318 (write-region (point-min) (point-max) fcc t)))
1319 ;; Ensure there is a blank line between messages, but
1320 ;; not at the very start of the file.
1321 (write-region (if (file-exists-p fcc)
1324 (point-max) fcc t)))
1325 (and buffer (not dont-write-the-file)
1326 (with-current-buffer buffer
1327 (set-visited-file-modtime)))))))))
1329 (defun mail-sent-via ()
1330 "Make a Sent-via header line from each To or CC header line."
1333 ;; put a marker at the end of the header
1334 (let ((end (copy-marker (mail-header-end)))
1335 (case-fold-search t))
1336 (goto-char (point-min))
1337 ;; search for the To: lines and make Sent-via: lines from them
1338 ;; search for the next To: line
1339 (while (re-search-forward "^\\(to\\|cc\\):" end t)
1340 ;; Grab this line plus all its continuations, sans the `to:'.
1342 (buffer-substring (point)
1344 (if (re-search-forward "^[^ \t\n]" end t)
1348 ;; Insert a copy, with altered header field name.
1349 (insert-before-markers "Sent-via:" to-line))))))
1352 "Move point to end of To field, creating it if necessary."
1355 (mail-position-on-field "To"))
1357 (defun mail-subject ()
1358 "Move point to end of Subject field, creating it if necessary."
1361 (mail-position-on-field "Subject"))
1364 "Move point to end of CC field, creating it if necessary."
1367 (or (mail-position-on-field "cc" t)
1368 (progn (mail-position-on-field "to")
1369 (insert "\nCC: "))))
1372 "Move point to end of BCC field, creating it if necessary."
1375 (or (mail-position-on-field "bcc" t)
1376 (progn (mail-position-on-field "to")
1377 (insert "\nBCC: "))))
1379 (defun mail-fcc (folder)
1380 "Add a new FCC field, with file name completion."
1381 (interactive "FFolder carbon copy: ")
1383 (or (mail-position-on-field "fcc" t) ;Put new field after exiting FCC.
1384 (mail-position-on-field "to"))
1385 (insert "\nFCC: " folder))
1387 (defun mail-reply-to ()
1388 "Move point to end of Reply-To field, creating it if necessary."
1391 (mail-position-on-field "Reply-To"))
1393 (defun mail-mail-reply-to ()
1394 "Move point to end of Mail-Reply-To field, creating it if necessary."
1397 (or (mail-position-on-field "mail-reply-to" t)
1398 (progn (mail-position-on-field "to")
1399 (insert "\nMail-Reply-To: "))))
1401 (defun mail-mail-followup-to ()
1402 "Move point to end of Mail-Followup-To field, creating it if necessary."
1405 (or (mail-position-on-field "mail-followup-to" t)
1406 (progn (mail-position-on-field "to")
1407 (insert "\nMail-Followup-To: "))))
1409 (defun mail-position-on-field (field &optional soft)
1411 (case-fold-search t))
1412 (setq end (mail-header-end))
1413 (goto-char (point-min))
1414 (if (re-search-forward (concat "^" (regexp-quote field) ":") end t)
1416 (re-search-forward "^[^ \t]" nil 'move)
1418 (skip-chars-backward "\n")
1421 (progn (goto-char end)
1422 (insert field ": \n")
1423 (skip-chars-backward "\n")))
1427 "Move point to beginning of text field."
1430 (goto-char (mail-text-start)))
1432 (defun mail-signature (&optional atpoint)
1433 "Sign letter with signature.
1434 If the variable `mail-signature' is a string, inserts it.
1435 If it is t or nil, inserts the contents of the file `mail-signature-file'.
1436 Otherwise, evals `mail-signature'.
1437 Prefix argument ATPOINT means insert at point rather than the end."
1439 ;; Test for an unreadable file here, before we delete trailing
1440 ;; whitespace, so that we don't modify the buffer needlessly.
1441 (if (and (memq mail-signature '(t nil))
1442 (not (file-readable-p mail-signature-file)))
1443 (if (called-interactively-p 'interactive)
1444 (message "The signature file `%s' could not be read"
1445 mail-signature-file))
1448 (goto-char (point-max))
1449 ;; Delete trailing whitespace and blank lines.
1450 (skip-chars-backward " \t\n")
1452 (delete-region (point) (point-max)))
1453 (cond ((stringp mail-signature)
1454 (insert mail-signature))
1455 ((memq mail-signature '(t nil))
1456 (insert "\n\n-- \n")
1457 (insert-file-contents (expand-file-name mail-signature-file)))
1459 ;; FIXME add condition-case error handling?
1460 (eval mail-signature))))))
1462 (defun mail-fill-yanked-message (&optional justifyp)
1463 "Fill the paragraphs of a message yanked into this one.
1464 Numeric argument means justify as well."
1467 (goto-char (mail-text-start))
1468 (fill-individual-paragraphs (point)
1471 mail-citation-prefix-regexp)))
1473 (defun mail-indent-citation ()
1474 "Modify text just inserted from a message to be cited.
1475 The inserted text should be the region.
1476 When this function returns, the region is again around the modified text.
1478 Normally, indent each nonblank line `mail-indentation-spaces' spaces.
1479 However, if `mail-yank-prefix' is non-nil, insert that prefix on each line."
1480 (mail-yank-clear-headers (region-beginning) (region-end))
1481 (if (null mail-yank-prefix)
1482 (indent-rigidly (region-beginning) (region-end)
1483 mail-indentation-spaces)
1485 (let ((end (set-marker (make-marker) (region-end))))
1486 (goto-char (region-beginning))
1487 (while (< (point) end)
1488 (insert mail-yank-prefix)
1489 (forward-line 1))))))
1491 (defun mail-yank-original (arg)
1492 "Insert the message being replied to, if any (in Rmail).
1493 Puts point after the text and mark before.
1494 Normally, indents each nonblank line ARG spaces (default 3).
1495 However, if `mail-yank-prefix' is non-nil, insert that prefix on each line.
1497 Just \\[universal-argument] as argument means don't indent, insert no prefix,
1498 and don't delete any header fields."
1500 (if mail-reply-action
1501 (let ((start (point))
1502 (original mail-reply-action)
1504 (and (consp original) (eq (car original) 'insert-buffer)
1505 (setq original (nth 1 original)))
1506 (if (consp original)
1508 ;; Call yank function, and set the mark if it doesn't.
1509 (apply (car original) (cdr original))
1510 (if (eq omark (mark t))
1511 (push-mark (point))))
1512 ;; If the original message is in another window in the same
1513 ;; frame, delete that window to save space.
1514 (delete-windows-on original t)
1516 ;; We really want this to set mark.
1517 (insert-buffer original)
1518 ;; If they yank the original text, the encoding of the
1519 ;; original message is a better default than
1520 ;; the default buffer-file-coding-system.
1521 (and (coding-system-equal
1522 (default-value 'buffer-file-coding-system)
1523 buffer-file-coding-system)
1524 (setq buffer-file-coding-system
1525 (coding-system-change-text-conversion
1526 buffer-file-coding-system
1528 (with-current-buffer original
1529 buffer-file-coding-system))))))
1530 (set-text-properties (point) (mark t) nil))
1534 (let ((mail-indentation-spaces (if arg (prefix-numeric-value arg)
1535 mail-indentation-spaces))
1536 ;; Avoid error in Transient Mark mode
1537 ;; on account of mark's being inactive.
1538 (mark-even-if-inactive t))
1539 (cond (mail-citation-hook
1540 ;; Bind mail-citation-header to the inserted
1541 ;; message's header.
1542 (let ((mail-citation-header
1543 (buffer-substring-no-properties
1547 (narrow-to-region start (point-max))
1551 (run-hooks 'mail-citation-hook)))
1553 (run-hooks 'mail-yank-hooks))
1555 (mail-indent-citation)))))
1556 ;; This is like exchange-point-and-mark, but doesn't activate the mark.
1557 ;; It is cleaner to avoid activation, even though the command
1558 ;; loop would deactivate the mark because we inserted text.
1559 (goto-char (prog1 (mark t)
1560 (set-marker (mark-marker) (point) (current-buffer))))
1561 (if (not (eolp)) (insert ?\n)))))
1563 (defun mail-yank-clear-headers (start end)
1566 (setq temp start start end end temp)))
1567 (if mail-yank-ignored-headers
1570 (if (search-forward "\n\n" end t)
1572 (narrow-to-region start (point))
1574 (while (let ((case-fold-search t))
1575 (re-search-forward mail-yank-ignored-headers nil t))
1577 (delete-region (point)
1578 (progn (re-search-forward "\n[^ \t]")
1582 (defun mail-yank-region (arg)
1583 "Insert the selected region from the message being replied to.
1584 Puts point after the text and mark before.
1585 Normally, indents each nonblank line ARG spaces (default 3).
1586 However, if `mail-yank-prefix' is non-nil, insert that prefix on each line.
1588 Just \\[universal-argument] as argument means don't indent, insert no prefix,
1589 and don't delete any header fields."
1591 (and (consp mail-reply-action)
1592 (eq (car mail-reply-action) 'insert-buffer)
1593 (with-current-buffer (nth 1 mail-reply-action)
1595 (error "No mark set: %S" (current-buffer))))
1596 (let ((buffer (nth 1 mail-reply-action))
1598 ;; Avoid error in Transient Mark mode
1599 ;; on account of mark's being inactive.
1600 (mark-even-if-inactive t))
1601 ;; Insert the citation text.
1602 (insert (with-current-buffer buffer
1603 (buffer-substring-no-properties (point) (mark))))
1605 ;; Indent or otherwise annotate the citation text.
1608 (let ((mail-indentation-spaces (if arg (prefix-numeric-value arg)
1609 mail-indentation-spaces)))
1610 (if mail-citation-hook
1611 ;; Bind mail-citation-header to the original message's header.
1612 (let ((mail-citation-header
1613 (with-current-buffer buffer
1614 (buffer-substring-no-properties
1617 (goto-char (point-min))
1620 (run-hooks 'mail-citation-hook))
1622 (run-hooks 'mail-yank-hooks)
1623 (mail-indent-citation))))))))
1625 (defun mail-split-line ()
1626 "Split current line, moving portion beyond point vertically down.
1627 If the current line has `mail-yank-prefix', insert it on the new line."
1629 (split-line mail-yank-prefix))
1632 (defun mail-attach-file (&optional file)
1633 "Insert a file at the end of the buffer, with separator lines around it."
1634 (interactive "fAttach file: ")
1636 (goto-char (point-max))
1637 (or (bolp) (newline))
1639 (let ((start (point))
1641 (insert (format "===File %s===" file))
1642 (insert-char ?= (max 0 (- 60 (current-column))))
1644 (setq middle (point))
1645 (insert "============================================================\n")
1648 (insert-file-contents file)
1649 (or (bolp) (newline))
1650 (goto-char start))))
1652 ;; Put these commands last, to reduce chance of lossage from quitting
1653 ;; in middle of loading the file.
1655 ;;;###autoload (add-hook 'same-window-buffer-names (purecopy "*mail*"))
1656 ;;;###autoload (add-hook 'same-window-buffer-names (purecopy "*unsent mail*"))
1659 (defun mail (&optional noerase to subject in-reply-to cc replybuffer
1660 actions return-action)
1661 "Edit a message to be sent. Prefix arg means resume editing (don't erase).
1662 When this function returns, the buffer `*mail*' is selected.
1663 The value is t if the message was newly initialized; otherwise, nil.
1665 Optionally, the signature file `mail-signature-file' can be inserted at the
1666 end; see the variable `mail-signature'.
1669 While editing message, type \\[mail-send-and-exit] to send the message and exit.
1671 Various special commands starting with C-c are available in sendmail mode
1672 to move to message header fields:
1675 If `mail-self-blind' is non-nil, a BCC to yourself is inserted
1676 when the message is initialized.
1678 If `mail-default-reply-to' is non-nil, it should be an address (a string);
1679 a Reply-to: field with that address is inserted.
1681 If `mail-archive-file-name' is non-nil, an FCC field with that file name
1684 The normal hook `mail-setup-hook' is run after the message is
1685 initialized. It can add more default fields to the message.
1687 The first argument, NOERASE, determines what to do when there is
1688 an existing modified `*mail*' buffer. If NOERASE is nil, the
1689 existing mail buffer is used, and the user is prompted whether to
1690 keep the old contents or to erase them. If NOERASE has the value
1691 `new', a new mail buffer will be created instead of using the old
1692 one. Any other non-nil value means to always select the old
1693 buffer without erasing the contents.
1695 The second through fifth arguments,
1696 TO, SUBJECT, IN-REPLY-TO and CC, specify if non-nil
1697 the initial contents of those header fields.
1698 These arguments should not have final newlines.
1699 The sixth argument REPLYBUFFER is a buffer which contains an
1700 original message being replied to, or else an action
1701 of the form (FUNCTION . ARGS) which says how to insert the original.
1702 Or it can be nil, if not replying to anything.
1703 The seventh argument ACTIONS is a list of actions to take
1704 if/when the message is sent. Each action looks like (FUNCTION . ARGS);
1705 when the message is sent, we apply FUNCTION to ARGS.
1706 This is how Rmail arranges to mark messages `answered'."
1708 (if (eq noerase 'new)
1709 (pop-to-buffer (generate-new-buffer "*mail*"))
1711 (not (get-buffer "*mail*"))
1713 (pop-to-buffer "*mail*"))
1715 ;; Avoid danger that the auto-save file can't be written.
1716 (let ((dir (expand-file-name
1717 (file-name-as-directory mail-default-directory))))
1718 (if (file-exists-p dir)
1719 (setq default-directory dir)))
1720 ;; Only call auto-save-mode if necessary, to avoid changing auto-save file.
1721 (if (or (and auto-save-default (not buffer-auto-save-file-name))
1722 (and (not auto-save-default) buffer-auto-save-file-name))
1723 (auto-save-mode auto-save-default))
1725 ;; Disconnect the buffer from its visited file
1726 ;; (in case the user has actually visited a file *mail*).
1727 ;; (set-visited-file-name nil)
1729 (and (not (and noerase
1730 (not (eq noerase 'new))))
1731 (if buffer-file-name
1732 (if (buffer-modified-p)
1733 (when (y-or-n-p "Buffer has unsaved changes; reinitialize it and discard them? ")
1734 (if (y-or-n-p "Disconnect buffer from visited file? ")
1735 (set-visited-file-name nil))
1737 (when (y-or-n-p "Reinitialize buffer, and disconnect it from the visited file? ")
1738 (set-visited-file-name nil)
1740 ;; A non-file-visiting buffer.
1741 (if (buffer-modified-p)
1742 (y-or-n-p "Unsent message being composed; erase it? ")
1744 (let ((inhibit-read-only t))
1746 (mail-setup to subject in-reply-to cc replybuffer actions
1748 (setq initialized t)))
1749 (if (and buffer-auto-save-file-name
1750 (file-exists-p buffer-auto-save-file-name))
1751 (message "Auto save file for draft message exists; consider M-x mail-recover"))
1754 (declare-function dired-view-file "dired" ())
1755 (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
1757 (defun mail-recover-1 ()
1758 "Pop up a list of auto-saved draft messages so you can recover one of them."
1760 (let ((file-name (make-auto-save-file-name))
1761 (ls-lisp-support-shell-wildcards t)
1762 non-random-len wildcard)
1763 ;; Remove the random part from the auto-save-file-name, and
1764 ;; create a wildcard which matches possible candidates.
1765 ;; Note: this knows that make-auto-save-file-name appends
1766 ;; "#<RANDOM-STUFF>#" to the buffer name, where RANDOM-STUFF
1767 ;; is the result of (make-temp-name "").
1768 (setq non-random-len
1769 (- (length file-name) (length (make-temp-name "")) 1))
1770 (setq wildcard (concat (substring file-name 0 non-random-len) "*"))
1771 (if (null (file-expand-wildcards wildcard))
1772 (message "There are no auto-saved drafts to recover")
1773 ;; Bind dired-trivial-filenames to t because all auto-save file
1774 ;; names are normally ``trivial'', so Dired will set point after
1775 ;; all the files, at buffer bottom. We want it on the first
1777 ;; Require dired so that dired-trivial-filenames does not get
1778 ;; unbound on exit from the let.
1780 (let ((dired-trivial-filenames t))
1781 (dired-other-window wildcard (concat dired-listing-switches "t")))
1782 (rename-buffer "*Auto-saved Drafts*" t)
1784 (goto-char (point-min))
1785 (or (looking-at " Move to the draft file you want to recover,")
1786 (let ((inhibit-read-only t))
1787 ;; Each line starts with a space so that Font Lock mode
1788 ;; won't highlight the first character.
1790 Move to the draft file you want to recover, then type C-c C-c
1791 to recover text of message whose composition was interrupted.
1792 To browse text of a draft, type v on the draft file's line.
1794 You can also delete some of these files;
1795 type d on a line to mark that file for deletion.
1797 List of possible auto-save files for recovery:
1801 (let ((map (make-sparse-keymap)))
1802 (set-keymap-parent map (current-local-map))
1804 (define-key (current-local-map) "v"
1807 (let ((coding-system-for-read 'utf-8-emacs-unix))
1808 (dired-view-file))))
1809 (define-key (current-local-map) "\C-c\C-c"
1812 (let ((fname (dired-get-filename))
1813 ;; Auto-saved files are written in the internal
1814 ;; representation, so they should be read accordingly.
1815 (coding-system-for-read 'utf-8-emacs-unix))
1816 (switch-to-buffer-other-window "*mail*")
1817 (let ((buffer-read-only nil))
1819 (insert-file-contents fname nil)
1820 ;; insert-file-contents will set buffer-file-coding-system
1821 ;; to utf-8-emacs, which is probably not what they want to
1822 ;; use for sending the message. But we don't know what
1823 ;; was its value before the buffer was killed or Emacs
1824 ;; crashed. We therefore reset buffer-file-coding-system
1825 ;; to the default value, so that either the default does
1826 ;; TRT, or the user will get prompted for the right
1827 ;; encoding when they send the message.
1828 (setq buffer-file-coding-system
1829 (default-value 'buffer-file-coding-system)))))))))
1831 (declare-function dired-move-to-filename "dired" (&optional raise-error eol))
1832 (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
1833 (declare-function dired-view-file "dired" ())
1835 (defun mail-recover ()
1836 "Recover interrupted mail composition from auto-save files.
1838 If the mail buffer has a current valid auto-save file,
1839 the command recovers that file. Otherwise, it displays a
1840 buffer showing the existing auto-saved draft messages;
1841 you can move to one of them and type C-c C-c to recover that one."
1843 ;; In case they invoke us from some random buffer...
1844 (switch-to-buffer "*mail*")
1845 ;; If *mail* didn't exist, set its directory, so that auto-saved
1846 ;; drafts will be found.
1847 (let ((dir (expand-file-name
1848 (file-name-as-directory mail-default-directory))))
1849 (if (file-exists-p dir)
1850 (setq default-directory dir)))
1851 (or (eq major-mode 'mail-mode)
1853 (let ((file-name buffer-auto-save-file-name))
1854 (cond ((and file-name (file-exists-p file-name))
1856 ;; This used to invoke `ls' via call-process, but
1857 ;; dired-noselect is more portable to systems where
1858 ;; `ls' is not a standard program (it will use
1859 ;; ls-lisp instead).
1860 (dired-noselect file-name
1861 (concat dired-listing-switches "t"))))
1862 (save-selected-window
1863 (select-window (display-buffer dispbuf t))
1864 (goto-char (point-min))
1866 (dired-move-to-filename)
1867 (setq dispbuf (rename-buffer "*Directory*" t)))
1868 (if (not (yes-or-no-p
1869 (format "Recover mail draft from auto save file %s? "
1871 (error "mail-recover cancelled")
1872 (let ((buffer-read-only nil)
1873 (buffer-coding buffer-file-coding-system)
1874 ;; Auto-save files are written in internal
1875 ;; representation of non-ASCII characters.
1876 (coding-system-for-read 'utf-8-emacs-unix))
1878 (insert-file-contents file-name nil)
1879 (setq buffer-file-coding-system buffer-coding)))))
1880 (t (mail-recover-1)))))
1883 (defun mail-other-window (&optional noerase to subject in-reply-to cc replybuffer sendactions)
1884 "Like `mail' command, but display mail buffer in another window."
1886 (let ((pop-up-windows t)
1887 (special-display-buffer-names nil)
1888 (special-display-regexps nil)
1889 (same-window-buffer-names nil)
1890 (same-window-regexps nil))
1891 (pop-to-buffer "*mail*"))
1892 (mail noerase to subject in-reply-to cc replybuffer sendactions))
1895 (defun mail-other-frame (&optional noerase to subject in-reply-to cc replybuffer sendactions)
1896 "Like `mail' command, but display mail buffer in another frame."
1898 (let ((pop-up-frames t)
1899 (special-display-buffer-names nil)
1900 (special-display-regexps nil)
1901 (same-window-buffer-names nil)
1902 (same-window-regexps nil))
1903 (pop-to-buffer "*mail*"))
1904 (mail noerase to subject in-reply-to cc replybuffer sendactions))
1906 ;; Do not add anything but external entries on this page.
1910 ;;; sendmail.el ends here