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