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