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