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