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