1 ;;; sendmail.el --- mail sending commands for Emacs. -*- byte-compile-dynamic: t -*-
3 ;; Copyright (C) 1985, 1986, 1992, 1993, 1994, 1995, 1996, 1998, 2000,
4 ;; 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
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)
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., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
28 ;; This mode provides mail-sending facilities from within Emacs. It is
29 ;; documented in the Emacs user's manual.
33 ;; Necessary to avoid recursive `require's.
38 (autoload 'rfc2047-encode-string
"rfc2047")
40 (defgroup sendmail nil
41 "Mail sending commands for Emacs."
45 (defcustom mail-setup-with-from t
46 "Non-nil means insert `From:' field when setting up the message."
52 (defcustom mail-from-style
'angles
"\
53 *Specifies how \"From:\" fields look.
55 If `nil', they contain just the return address like:
57 If `parens', they look like:
58 king@grassland.com (Elvis Parsley)
59 If `angles', they look like:
60 Elvis Parsley <king@grassland.com>
61 If `system-default', allows the mailer to insert its default From field
62 derived from the envelope-from address.
64 In old versions of Emacs, the `system-default' setting also caused
65 Emacs to pass the proper email address from `user-mail-address'
66 to the mailer to specify the envelope-from address. But that is now
67 controlled by a separate variable, `mail-specify-envelope-from'."
68 :type
'(choice (const nil
) (const parens
) (const angles
)
69 (const system-default
))
74 (defcustom mail-specify-envelope-from nil
75 "*If non-nil, specify the envelope-from address when sending mail.
76 The value used to specify it is whatever is found in
77 the variable `mail-envelope-from', with `user-mail-address' as fallback.
79 On most systems, specifying the envelope-from address is a
80 privileged operation. This variable affects sendmail and
81 smtpmail -- if you use feedmail to send mail, see instead the
82 variable `feedmail-deduce-envelope-from'."
87 (defcustom mail-envelope-from nil
88 "*If non-nil, designate the envelope-from address when sending mail.
89 This only has an effect if `mail-specify-envelope-from' is non-nil.
90 The value should be either a string, or the symbol `header' (in
91 which case the contents of the \"From\" header of the message
92 being sent is used), or nil (in which case the value of
93 `user-mail-address' is used)."
95 :type
'(choice (string :tag
"From-name")
96 (const :tag
"Use From: header from message" header
)
97 (const :tag
"Use `user-mail-address'" nil
))
101 (defcustom mail-self-blind nil
"\
102 *Non-nil means insert BCC to self in messages to be sent.
103 This is done when the message is initialized,
104 so you can remove or alter the BCC field to override the default."
109 (defcustom mail-interactive nil
"\
110 *Non-nil means when sending a message wait for and display errors.
111 nil means let mailer mail back a message to report errors."
116 (defcustom mail-yank-ignored-headers
"^via:\\|^mail-from:\\|^origin:\\|^status:\\|^remailed\\|^received:\\|^message-id:\\|^summary-line:\\|^to:\\|^subject:\\|^in-reply-to:\\|^return-path:\\|^mail-reply-to:\\|^mail-followup-to:" "\
117 *Delete these headers from old message when it's inserted in a reply."
121 ;; Useful to set in site-init.el
123 (defcustom send-mail-function
'sendmail-send-it
124 "Function to call to send the current buffer as mail.
125 The headers should be delimited by a line which is
126 not a valid RFC822 header or continuation line,
127 that matches the variable `mail-header-separator'.
128 This is used by the default mail-sending commands. See also
129 `message-send-mail-function' for use with the Message package."
130 :type
'(radio (function-item sendmail-send-it
:tag
"Use Sendmail package")
131 (function-item smtpmail-send-it
:tag
"Use SMTPmail package")
132 (function-item feedmail-send-it
:tag
"Use Feedmail package")
133 (function-item mailclient-send-it
:tag
"Use Mailclient package")
138 (defcustom mail-header-separator
"--text follows this line--" "\
139 *Line used to separate headers from text in messages being composed."
143 ;; Set up mail-header-separator for use as a category text property.
144 (put 'mail-header-separator
'rear-nonsticky
'(category))
145 ;;; This was a nice idea, for preventing accidental modification of
146 ;;; the separator. But I found it also prevented or obstructed
147 ;;; certain deliberate operations, such as copying the separator line
148 ;;; up to the top to send myself a copy of an already sent outgoing message
149 ;;; and other things. So I turned it off. --rms.
150 ;;;(put 'mail-header-separator 'read-only t)
153 (defcustom mail-archive-file-name nil
"\
154 *Name of file to write all outgoing messages in, or nil for none.
155 This can be an inbox file or an Rmail file."
156 :type
'(choice file
(const nil
))
160 (defcustom mail-default-reply-to nil
161 "*Address to insert as default Reply-to field of outgoing messages.
162 If nil, it will be initialized from the REPLYTO environment variable
163 when you first send mail."
164 :type
'(choice (const nil
) string
)
168 (defcustom mail-alias-file nil
169 "*If non-nil, the name of a file to use instead of `/usr/lib/aliases'.
170 This file defines aliases to be expanded by the mailer; this is a different
171 feature from that of defining aliases in `.mailrc' to be expanded in Emacs.
172 This variable has no effect unless your system uses sendmail as its mailer."
173 :type
'(choice (const nil
) file
)
177 (defcustom mail-personal-alias-file
"~/.mailrc"
178 "*If non-nil, the name of the user's personal mail alias file.
179 This file typically should be in same format as the `.mailrc' file used by
180 the `Mail' or `mailx' program.
181 This file need not actually exist."
182 :type
'(choice (const nil
) file
)
185 (defcustom mail-setup-hook nil
186 "Normal hook, run each time a new outgoing mail message is initialized.
187 The function `mail-setup' runs this hook."
189 :options
'(fortune-to-signature spook mail-abbrevs-setup
)
192 (defvar mail-aliases t
193 "Alist of mail address aliases,
194 or t meaning should be initialized from your mail aliases file.
195 \(The file's name is normally `~/.mailrc', but `mail-personal-alias-file'
196 can specify a different file name.)
197 The alias definitions in the file have this form:
198 alias ALIAS MEANING")
200 (defvar mail-alias-modtime nil
201 "The modification time of your mail alias file when it was last examined.")
203 (defcustom mail-yank-prefix nil
204 "*Prefix insert on lines of yanked message being replied to.
205 nil means use indentation."
206 :type
'(choice (const nil
) string
)
209 (defcustom mail-indentation-spaces
3
210 "*Number of spaces to insert at the beginning of each cited line.
211 Used by `mail-yank-original' via `mail-indent-citation'."
214 (defvar mail-yank-hooks nil
215 "Obsolete hook for modifying a citation just inserted in the mail buffer.
216 Each hook function can find the citation between (point) and (mark t).
217 And each hook function should leave point and mark around the citation
220 This is a normal hook, misnamed for historical reasons.
221 It is semi-obsolete and mail agents should no longer use it.")
224 (defcustom mail-citation-hook nil
225 "*Hook for modifying a citation just inserted in the mail buffer.
226 Each hook function can find the citation between (point) and (mark t),
227 and should leave point and mark around the citation text as modified.
228 The hook functions can find the header of the cited message
229 in the variable `mail-citation-header', whether or not this is included
230 in the cited portion of the message.
232 If this hook is entirely empty (nil), a default action is taken
233 instead of no action."
237 (defvar mail-citation-header nil
238 "While running `mail-citation-hook', this variable holds the message header.
239 This enables the hook functions to see the whole message header
240 regardless of what part of it (if any) is included in the cited text.")
242 (defcustom mail-citation-prefix-regexp
"[ \t]*[-a-z0-9A-Z]*>+[ \t]*\\|[ \t]*"
243 "*Regular expression to match a citation prefix plus whitespace.
244 It should match whatever sort of citation prefixes you want to handle,
245 with whitespace before and after; it should also match just whitespace.
246 The default value matches citations like `foo-bar>' plus whitespace."
251 (defvar mail-abbrevs-loaded nil
)
252 (defvar mail-mode-map nil
)
254 (autoload 'build-mail-aliases
"mailalias"
255 "Read mail aliases from user's personal aliases file and set `mail-aliases'."
258 (autoload 'expand-mail-aliases
"mailalias"
259 "Expand all mail aliases in suitable header fields found between BEG and END.
260 Suitable header fields are `To', `Cc' and `Bcc' and their `Resent-' variants.
261 Optional second arg EXCLUDE may be a regular expression defining text to be
262 removed from alias expansions."
266 (defcustom mail-signature nil
267 "*Text inserted at end of mail buffer when a message is initialized.
268 If t, it means to insert the contents of the file `mail-signature-file'.
269 If a string, that string is inserted.
270 (To make a proper signature, the string should begin with \\n\\n-- \\n,
271 which is the standard way to delimit a signature in a message.)
272 Otherwise, it should be an expression; it is evaluated
273 and should insert whatever you want to insert."
274 :type
'(choice (const :tag
"None" nil
)
275 (const :tag
"Use `.signature' file" t
)
276 (string :tag
"String to insert")
277 (sexp :tag
"Expression to evaluate"))
279 (put 'mail-signature
'risky-local-variable t
)
281 (defcustom mail-signature-file
"~/.signature"
282 "*File containing the text inserted at end of mail buffer."
287 (defcustom mail-default-directory
"~/"
288 "*Directory for mail buffers.
289 Value of `default-directory' for mail buffers.
290 This directory is used for auto-save files of mail buffers."
291 :type
'(directory :tag
"Directory")
295 (defvar mail-reply-action nil
)
296 (defvar mail-send-actions nil
297 "A list of actions to be performed upon successful sending of a message.")
298 (put 'mail-reply-action
'permanent-local t
)
299 (put 'mail-send-actions
'permanent-local t
)
301 (defcustom mail-default-headers nil
302 "*A string containing header lines, to be inserted in outgoing messages.
303 It is inserted before you edit the message,
304 so you can edit or delete these lines."
305 :type
'(choice (const nil
) string
)
308 (defcustom mail-bury-selects-summary t
309 "*If non-nil, try to show RMAIL summary buffer after returning from mail.
310 The functions \\[mail-send-on-exit] or \\[mail-dont-send] select
311 the RMAIL summary buffer before returning, if it exists and this variable
316 (defcustom mail-send-nonascii
'mime
317 "*Specify whether to allow sending non-ASCII characters in mail.
318 If t, that means do allow it. nil means don't allow it.
319 `query' means ask the user each time.
320 `mime' means add an appropriate MIME header if none already present.
321 The default is `mime'.
322 Including non-ASCII characters in a mail message can be problematical
323 for the recipient, who may not know how to decode them properly."
324 :type
'(choice (const t
) (const nil
) (const query
) (const mime
))
327 (defcustom mail-use-dsn nil
328 "*Ask MTA for notification of failed, delayed or successful delivery.
329 Note that only some MTAs (currently only recent versions of Sendmail)
330 support Delivery Status Notification."
332 :type
'(repeat (radio (const :tag
"Failure" failure
)
333 (const :tag
"Delay" delay
)
334 (const :tag
"Success" success
)))
337 ;; Note: could use /usr/ucb/mail instead of sendmail;
338 ;; options -t, and -v if not interactive.
339 (defvar mail-mailer-swallows-blank-line
340 (if (and (string-match "sparc-sun-sunos\\(\\'\\|[^5]\\)" system-configuration
)
341 (file-readable-p "/etc/sendmail.cf")
342 (let ((buffer (get-buffer-create " *temp*")))
346 (insert-file-contents "/etc/sendmail.cf")
347 (goto-char (point-min))
348 (let ((case-fold-search nil
))
349 (re-search-forward "^OR\\>" nil t
)))
350 (kill-buffer buffer
))))
351 ;; According to RFC822, "The field-name must be composed of printable
352 ;; ASCII characters (i.e. characters that have decimal values between
353 ;; 33 and 126, except colon)", i.e. any chars except ctl chars,
355 '(looking-at "[ \t]\\|[][!\"#$%&'()*+,-./0-9;<=>?@A-Z\\\\^_`a-z{|}~]+:"))
356 "Set this non-nil if the system's mailer runs the header and body together.
357 \(This problem exists on Sunos 4 when sendmail is run in remote mode.)
358 The value should be an expression to test whether the problem will
361 (defvar mail-mode-syntax-table
362 (let ((st (make-syntax-table)))
363 ;; define-derived-mode will make it inherit from text-mode-syntax-table.
364 (modify-syntax-entry ?%
". " st
)
366 "Syntax table used while in `mail-mode'.")
368 (defvar mail-font-lock-keywords
370 (let* ((cite-chars "[>|}]")
371 (cite-prefix "[:alpha:]")
372 (cite-suffix (concat cite-prefix
"0-9_.@-`'\"")))
373 (list '("^\\(To\\|Newsgroups\\):" . font-lock-function-name-face
)
374 '("^\\(B?CC\\|Reply-to\\|Mail-\\(reply\\|followup\\)-to\\):" . font-lock-keyword-face
)
375 '("^\\(Subject:\\)[ \t]*\\(.+\\)?"
376 (1 font-lock-comment-face
)
377 ;; (2 font-lock-type-face nil t)
379 ;; Use EVAL to delay in case `mail-header-separator' gets changed.
381 (let ((separator (if (zerop (length mail-header-separator
))
383 (regexp-quote mail-header-separator
))))
384 (cons (concat "^" separator
"$") 'font-lock-warning-face
)))
385 ;; Use MATCH-ANCHORED to effectively anchor the regexp left side.
387 (,(concat "\\=[ \t]*"
388 "\\(\\(\\([" cite-prefix
"]+[" cite-suffix
"]*\\)?"
389 "\\(" cite-chars
"[ \t]*\\)\\)+\\)"
391 (beginning-of-line) (end-of-line)
392 (1 font-lock-comment-delimiter-face nil t
)
393 (5 font-lock-comment-face nil t
)))
394 '("^\\(X-[A-Za-z0-9-]+\\|In-reply-to\\):.*\\(\n[ \t]+.*\\)*$"
395 . font-lock-string-face
))))
396 "Additional expressions to highlight in Mail mode.")
399 (defun sendmail-sync-aliases ()
400 (when mail-personal-alias-file
401 (let ((modtime (nth 5 (file-attributes mail-personal-alias-file
))))
402 (or (equal mail-alias-modtime modtime
)
403 (setq mail-alias-modtime modtime
406 (defun mail-setup (to subject in-reply-to cc replybuffer actions
)
407 (or mail-default-reply-to
408 (setq mail-default-reply-to
(getenv "REPLYTO")))
409 (sendmail-sync-aliases)
410 (if (eq mail-aliases t
)
412 (setq mail-aliases nil
)
413 (when mail-personal-alias-file
414 (if (file-exists-p mail-personal-alias-file
)
415 (build-mail-aliases)))))
416 ;; Don't leave this around from a previous message.
417 (kill-local-variable 'buffer-file-coding-system
)
418 ;; This doesn't work for enable-multibyte-characters.
419 ;; (kill-local-variable 'enable-multibyte-characters)
420 (set-buffer-multibyte default-enable-multibyte-characters
)
421 (if current-input-method
422 (inactivate-input-method))
423 (setq mail-send-actions actions
)
424 (setq mail-reply-action replybuffer
)
425 (goto-char (point-min))
426 (if mail-setup-with-from
427 (mail-insert-from-field))
431 ;; Here removed code to extract names from within <...>
432 ;; on the assumption that mail-strip-quoted-names
433 ;; has been called and has done so.
434 (let ((fill-prefix "\t")
435 (address-start (point)))
437 (fill-region-as-paragraph address-start
(point-max))
438 (goto-char (point-max))
443 (let ((fill-prefix "\t")
444 (address-start (progn (insert "CC: ") (point))))
446 (fill-region-as-paragraph address-start
(point-max))
447 (goto-char (point-max))
451 (let ((fill-prefix "\t")
453 (address-start (point)))
454 (insert "In-reply-to: " in-reply-to
"\n")
455 (fill-region-as-paragraph address-start
(point-max))
456 (goto-char (point-max))
459 (insert "Subject: " (or subject
"") "\n")
460 (if mail-default-headers
461 (insert mail-default-headers
))
462 (if mail-default-reply-to
463 (insert "Reply-to: " mail-default-reply-to
"\n"))
465 (insert "BCC: " user-mail-address
"\n"))
466 (if mail-archive-file-name
467 (insert "FCC: " mail-archive-file-name
"\n"))
468 (put-text-property (point)
470 (insert mail-header-separator
"\n")
472 'category
'mail-header-separator
)
473 ;; Insert the signature. But remember the beginning of the message.
474 (if to
(setq to
(point)))
475 (cond ((eq mail-signature t
)
476 (if (file-exists-p mail-signature-file
)
479 (insert-file-contents mail-signature-file
))))
480 ((stringp mail-signature
)
481 (insert mail-signature
))
483 (eval mail-signature
)))
484 (goto-char (point-max))
485 (or (bolp) (newline)))
486 (if to
(goto-char to
))
487 (or to subject in-reply-to
488 (set-buffer-modified-p nil
))
489 (run-hooks 'mail-setup-hook
))
491 (defcustom mail-mode-hook nil
492 "Hook run by Mail mode."
495 :options
'(footnote-mode))
497 (defvar mail-mode-abbrev-table text-mode-abbrev-table
)
499 (define-derived-mode mail-mode text-mode
"Mail"
500 "Major mode for editing mail to be sent.
501 Like Text Mode but with these additional commands:
502 \\[mail-send] mail-send (send the message) \\[mail-send-and-exit] mail-send-and-exit
503 Here are commands that move to a header field (and create it if there isn't):
504 \\[mail-to] move to To: \\[mail-subject] move to Subject:
505 \\[mail-cc] move to CC: \\[mail-bcc] move to BCC:
506 \\[mail-fcc] move to FCC: \\[mail-reply-to] move to Reply-To:
507 \\[mail-mail-reply-to] move to Mail-Reply-To:
508 \\[mail-mail-followup-to] move to Mail-Followup-To:
509 \\[mail-text] mail-text (move to beginning of message text).
510 \\[mail-signature] mail-signature (insert `mail-signature-file' file).
511 \\[mail-yank-original] mail-yank-original (insert current message, in Rmail).
512 \\[mail-fill-yanked-message] mail-fill-yanked-message (fill what was yanked).
513 \\[mail-sent-via] mail-sent-via (add a Sent-via field for each To or CC).
514 Turning on Mail mode runs the normal hooks `text-mode-hook' and
515 `mail-mode-hook' (in that order)."
516 (make-local-variable 'mail-reply-action
)
517 (make-local-variable 'mail-send-actions
)
518 (setq buffer-offer-save t
)
519 (make-local-variable 'font-lock-defaults
)
520 (setq font-lock-defaults
'(mail-font-lock-keywords t t
))
521 (make-local-variable 'paragraph-separate
)
522 (make-local-variable 'normal-auto-fill-function
)
523 (setq normal-auto-fill-function
'mail-mode-auto-fill
)
524 (make-local-variable 'fill-paragraph-function
)
525 (setq fill-paragraph-function
'mail-mode-fill-paragraph
)
526 ;; Allow using comment commands to add/remove quoting (this only does
527 ;; anything if mail-yank-prefix is set to a non-nil value).
528 (set (make-local-variable 'comment-start
) mail-yank-prefix
)
530 (set (make-local-variable 'comment-start-skip
)
531 (concat "^" (regexp-quote mail-yank-prefix
) "[ \t]*")))
532 (make-local-variable 'adaptive-fill-regexp
)
533 (setq adaptive-fill-regexp
534 (concat "[ \t]*[-[:alnum:]]+>+[ \t]*\\|"
535 adaptive-fill-regexp
))
536 (make-local-variable 'adaptive-fill-first-line-regexp
)
537 (setq adaptive-fill-first-line-regexp
538 (concat "[ \t]*[-[:alnum:]]*>+[ \t]*\\|"
539 adaptive-fill-first-line-regexp
))
540 ;; `-- ' precedes the signature. `-----' appears at the start of the
541 ;; lines that delimit forwarded messages.
542 ;; Lines containing just >= 3 dashes, perhaps after whitespace,
543 ;; are also sometimes used and should be separators.
544 (setq paragraph-separate
(concat (regexp-quote mail-header-separator
)
545 "$\\|\t*\\([-|#;>* ]\\|(?[0-9]+[.)]\\)+$"
546 "\\|[ \t]*[[:alnum:]]*>+[ \t]*$\\|[ \t]*$\\|"
551 (defun mail-header-end ()
552 "Return the buffer location of the end of headers, as a number."
559 (defun mail-text-start ()
560 "Return the buffer location of the start of text, as a number."
568 (defun mail-sendmail-delimit-header ()
569 "Set up whatever header delimiter convention sendmail will use.
570 Concretely: replace the first blank line in the header with the separator."
572 (insert mail-header-separator
)
575 (defun mail-sendmail-undelimit-header ()
576 "Remove header separator to put the message in correct form for sendmail.
577 Leave point at the start of the delimiter line."
579 (delete-region (point) (progn (end-of-line) (point))))
581 (defun mail-mode-auto-fill ()
582 "Carry out Auto Fill for Mail mode.
583 If within the headers, this makes the new lines into continuation lines."
584 (if (< (point) (mail-header-end))
585 (let ((old-line-start (save-excursion (beginning-of-line) (point))))
589 (while (not (eq (point) old-line-start
))
590 ;; Use insert-before-markers in case we're inserting
591 ;; before the saved value of point (which is common).
592 (insert-before-markers " ")
597 (defun mail-mode-fill-paragraph (arg)
598 ;; Do something special only if within the headers.
599 (if (< (point) (mail-header-end))
600 (let (beg end fieldname
)
601 (when (prog1 (re-search-backward "^[-a-zA-Z]+:" nil
'yes
)
604 (downcase (buffer-substring beg
(1- (match-end 0))))))
606 ;; Find continuation lines and get rid of their continuation markers.
607 (while (looking-at "[ \t]")
608 (delete-horizontal-space)
610 (setq end
(point-marker))
612 ;; If this field contains addresses,
613 ;; make sure we can fill after each address.
614 (if (member fieldname
615 '("to" "cc" "bcc" "from" "reply-to"
616 "mail-reply-to" "mail-followup-to"
617 "resent-to" "resent-cc" "resent-bcc"
618 "resent-from" "resent-reply-to"))
619 (while (search-forward "," end t
)
620 (or (looking-at "[ \t]")
622 (fill-region-as-paragraph beg end
)
623 ;; Mark all lines except the first as continuations.
626 (while (< (point) end
)
629 (move-marker end nil
)
636 (setq mail-mode-map
(make-sparse-keymap))
637 (define-key mail-mode-map
"\M-\t" 'mail-complete
)
638 (define-key mail-mode-map
"\C-c?" 'describe-mode
)
639 (define-key mail-mode-map
"\C-c\C-f\C-t" 'mail-to
)
640 (define-key mail-mode-map
"\C-c\C-f\C-b" 'mail-bcc
)
641 (define-key mail-mode-map
"\C-c\C-f\C-f" 'mail-fcc
)
642 (define-key mail-mode-map
"\C-c\C-f\C-c" 'mail-cc
)
643 (define-key mail-mode-map
"\C-c\C-f\C-s" 'mail-subject
)
644 (define-key mail-mode-map
"\C-c\C-f\C-r" 'mail-reply-to
)
645 (define-key mail-mode-map
"\C-c\C-f\C-a" 'mail-mail-reply-to
) ; author
646 (define-key mail-mode-map
"\C-c\C-f\C-l" 'mail-mail-followup-to
) ; list
647 (define-key mail-mode-map
"\C-c\C-t" 'mail-text
)
648 (define-key mail-mode-map
"\C-c\C-y" 'mail-yank-original
)
649 (define-key mail-mode-map
"\C-c\C-r" 'mail-yank-region
)
650 (define-key mail-mode-map
[remap split-line
] 'mail-split-line
)
651 (define-key mail-mode-map
"\C-c\C-q" 'mail-fill-yanked-message
)
652 (define-key mail-mode-map
"\C-c\C-w" 'mail-signature
)
653 (define-key mail-mode-map
"\C-c\C-v" 'mail-sent-via
)
654 (define-key mail-mode-map
"\C-c\C-c" 'mail-send-and-exit
)
655 (define-key mail-mode-map
"\C-c\C-s" 'mail-send
)
656 (define-key mail-mode-map
"\C-c\C-i" 'mail-attach-file
))
658 (define-key mail-mode-map
[menu-bar mail
]
659 (cons "Mail" (make-sparse-keymap "Mail")))
661 (define-key mail-mode-map
[menu-bar mail fill
]
662 '("Fill Citation" . mail-fill-yanked-message
))
664 (define-key mail-mode-map
[menu-bar mail yank
]
665 '("Cite Original" . mail-yank-original
))
667 (define-key mail-mode-map
[menu-bar mail signature
]
668 '("Insert Signature" . mail-signature
))
670 (define-key mail-mode-map
[menu-bar mail mail-sep
]
673 (define-key mail-mode-map
[menu-bar mail cancel
]
674 '("Cancel" . mail-dont-send
))
676 (define-key mail-mode-map
[menu-bar mail send-stay
]
677 '("Send, Keep Editing" . mail-send
))
679 (define-key mail-mode-map
[menu-bar mail send
]
680 '("Send Message" . mail-send-and-exit
))
682 (define-key mail-mode-map
[menu-bar headers
]
683 (cons "Headers" (make-sparse-keymap "Move to Header")))
685 (define-key mail-mode-map
[menu-bar headers text
]
686 '("Text" . mail-text
))
688 (define-key mail-mode-map
[menu-bar headers expand-aliases
]
689 '("Expand Aliases" . expand-mail-aliases
))
691 (define-key mail-mode-map
[menu-bar headers sent-via
]
692 '("Sent Via" . mail-sent-via
))
694 (define-key mail-mode-map
[menu-bar headers mail-reply-to
]
695 '("Mail Reply To" . mail-mail-reply-to
))
697 (define-key mail-mode-map
[menu-bar headers mail-followup-to
]
698 '("Mail Followup To" . mail-mail-followup-to
))
700 (define-key mail-mode-map
[menu-bar headers reply-to
]
701 '("Reply-To" . mail-reply-to
))
703 (define-key mail-mode-map
[menu-bar headers bcc
]
706 (define-key mail-mode-map
[menu-bar headers fcc
]
709 (define-key mail-mode-map
[menu-bar headers cc
]
712 (define-key mail-mode-map
[menu-bar headers subject
]
713 '("Subject" . mail-subject
))
715 (define-key mail-mode-map
[menu-bar headers to
]
718 ;; User-level commands for sending.
720 (defun mail-send-and-exit (&optional arg
)
721 "Send message like `mail-send', then, if no errors, exit from mail buffer.
722 Prefix arg means don't delete this window."
727 (defun mail-dont-send (&optional arg
)
728 "Don't send the message you have been editing.
729 Prefix arg means don't delete this window."
733 (defun mail-bury (&optional arg
)
734 "Bury this mail buffer."
735 (let ((newbuf (other-buffer (current-buffer))))
736 (bury-buffer (current-buffer))
737 (if (and (or (window-dedicated-p (frame-selected-window))
738 (cdr (assq 'mail-dedicated-frame
(frame-parameters))))
739 (not (null (delq (selected-frame) (visible-frame-list)))))
741 (if (display-multi-frame-p)
742 (delete-frame (selected-frame))
743 ;; The previous frame is where normally they have the
744 ;; RMAIL buffer displayed.
746 (let (rmail-flag summary-buffer
)
750 (set-buffer (window-buffer (next-window (selected-window) 'not
)))
751 (setq rmail-flag
(eq major-mode
'rmail-mode
))
753 (and mail-bury-selects-summary
754 (boundp 'rmail-summary-buffer
)
756 (buffer-name rmail-summary-buffer
)
757 (not (get-buffer-window rmail-summary-buffer
))
758 rmail-summary-buffer
))))
760 ;; If the Rmail buffer has a summary, show that.
761 (if summary-buffer
(switch-to-buffer summary-buffer
)
763 (switch-to-buffer newbuf
))))))
765 (defcustom mail-send-hook nil
766 "Hook run just before sending mail with `mail-send'."
768 :options
'(flyspell-mode-off)
772 (defcustom mail-mailing-lists nil
"\
773 *List of mailing list addresses the user is subscribed to.
775 The variable is used to trigger insertion of the \"Mail-Followup-To\"
776 header when sending a message to a mailing list."
777 :type
'(repeat string
)
782 "Send the message in the current buffer.
783 If `mail-interactive' is non-nil, wait for success indication
784 or error messages, and inform user.
785 Otherwise any failure is reported in a message back to
786 the user from the mailer."
788 (if (if buffer-file-name
789 (y-or-n-p "Send buffer contents as mail message? ")
790 (or (buffer-modified-p)
791 (y-or-n-p "Message already sent; resend? ")))
792 (let ((inhibit-read-only t
)
794 (ml (when mail-mailing-lists
795 ;; The surrounding regexp assumes the use of
796 ;; `mail-strip-quoted-names' on addresses before matching
797 ;; Cannot deal with full RFC 822 freedom, but that is
798 ;; unlikely to be problematic.
799 (concat "\\(?:[[:space:];,]\\|\\`\\)"
800 (regexp-opt mail-mailing-lists t
)
801 "\\(?:[[:space:];,]\\|\\'\\)"))))
802 ;; If there are mailing lists defined
805 (let* ((to (mail-fetch-field "to" nil t
))
806 (cc (mail-fetch-field "cc" nil t
))
807 (new-header-values ; To: and Cc:
808 (mail-strip-quoted-names
809 (concat to
(when cc
(concat ", " cc
))))))
810 ;; If message goes to known mailing list ...
811 (when (string-match ml new-header-values
)
812 ;; Add Mail-Followup-To if none yet
813 (unless (mail-fetch-field "mail-followup-to")
814 (goto-char (mail-header-end))
815 (insert "Mail-Followup-To: "
822 (split-string new-header-values
824 (mapconcat 'identity l
", "))
826 ;; Add Mail-Reply-To if none yet
827 (unless (mail-fetch-field "mail-reply-to")
828 (goto-char (mail-header-end))
829 (insert "Mail-Reply-To: "
830 (or (mail-fetch-field "reply-to")
833 (unless (memq mail-send-nonascii
'(t mime
))
834 (goto-char (point-min))
835 (skip-chars-forward "\0-\177")
836 (or (= (point) (point-max))
837 (if (eq mail-send-nonascii
'query
)
838 (or (y-or-n-p "Message contains non-ASCII characters; send anyway? ")
840 (error "Message contains non-ASCII characters"))))
841 ;; Complain about any invalid line.
842 (goto-char (point-min))
843 (while (< (point) (mail-header-end))
844 (unless (looking-at "[ \t]\\|.*:\\|$")
846 (error "Invalid header line (maybe a continuation line lacks initial whitespace)"))
849 (run-hooks 'mail-send-hook
)
850 (message "Sending...")
851 (funcall send-mail-function
)
852 ;; Now perform actions on successful sending.
853 (while mail-send-actions
855 (apply (car (car mail-send-actions
))
856 (cdr (car mail-send-actions
)))
858 (setq mail-send-actions
(cdr mail-send-actions
)))
859 (message "Sending...done")
860 ;; If buffer has no file, mark it as unmodified and delete auto-save.
861 (if (not buffer-file-name
)
863 (set-buffer-modified-p nil
)
864 (delete-auto-save-file-if-necessary t
))))))
866 (defun mail-envelope-from ()
867 "Return the envelope mail address to use when sending mail.
868 This function uses `mail-envelope-from'."
869 (if (eq mail-envelope-from
'header
)
870 (nth 1 (mail-extract-address-components
871 (mail-fetch-field "From")))
874 ;; This does the real work of sending a message via sendmail.
875 ;; It is called via the variable send-mail-function.
878 (defvar sendmail-coding-system nil
879 "*Coding system for encoding the outgoing mail.
880 This has higher priority than `default-buffer-file-coding-system'
881 and `default-sendmail-coding-system',
882 but lower priority than the local value of `buffer-file-coding-system'.
883 See also the function `select-message-coding-system'.")
886 (defvar default-sendmail-coding-system
'iso-latin-1
887 "Default coding system for encoding the outgoing mail.
888 This variable is used only when `sendmail-coding-system' is nil.
890 This variable is set/changed by the command `set-language-environment'.
891 User should not set this variable manually,
892 instead use `sendmail-coding-system' to get a constant encoding
893 of outgoing mails regardless of the current language environment.
894 See also the function `select-message-coding-system'.")
896 (defun mail-insert-from-field ()
897 (let* ((login user-mail-address
)
898 (fullname (user-full-name))
899 (quote-fullname nil
))
900 (if (string-match "[^\0-\177]" fullname
)
901 (setq fullname
(rfc2047-encode-string fullname
)
903 (cond ((eq mail-from-style
'angles
)
904 (insert "From: " fullname
)
905 (let ((fullname-start (+ (point-min) 6))
906 (fullname-end (point-marker)))
907 (goto-char fullname-start
)
908 ;; Look for a character that cannot appear unquoted
909 ;; according to RFC 822.
910 (if (or (re-search-forward "[^- !#-'*+/-9=?A-Z^-~]"
914 ;; Quote fullname, escaping specials.
915 (goto-char fullname-start
)
917 (while (re-search-forward "[\"\\]"
919 (replace-match "\\\\\\&" t
))
921 (insert " <" login
">\n"))
922 ((eq mail-from-style
'parens
)
923 (insert "From: " login
" (")
924 (let ((fullname-start (point)))
930 (let ((fullname-end (point-marker)))
931 (goto-char fullname-start
)
932 ;; RFC 822 says \ and nonmatching parentheses
933 ;; must be escaped in comments.
934 ;; Escape every instance of ()\ ...
935 (while (re-search-forward "[()\\]" fullname-end
1)
936 (replace-match "\\\\\\&" t
))
937 ;; ... then undo escaping of matching parentheses,
938 ;; including matching nested parentheses.
939 (goto-char fullname-start
)
940 (while (re-search-forward
941 "\\(\\=\\|[^\\]\\(\\\\\\\\\\)*\\)\\\\(\\(\\([^\\]\\|\\\\\\\\\\)*\\)\\\\)"
943 (replace-match "\\1(\\3)" t
)
944 (goto-char fullname-start
))))
946 ((null mail-from-style
)
947 (insert "From: " login
"\n"))
948 ((eq mail-from-style
'system-default
)
950 (t (error "Invalid value for `mail-from-style'")))))
952 (defun sendmail-send-it ()
953 "Send the current mail buffer using the Sendmail package.
954 This is a suitable value for `send-mail-function'. It sends using the
955 external program defined by `sendmail-program'."
956 (require 'mail-utils
)
957 (let ((errbuf (if mail-interactive
958 (generate-new-buffer " sendmail errors")
960 (tembuf (generate-new-buffer " sendmail temp"))
961 (multibyte enable-multibyte-characters
)
962 (case-fold-search nil
)
963 (selected-coding (select-message-coding-system))
967 (mailbuf (current-buffer))
968 (program (if (boundp 'sendmail-program
)
970 "/usr/lib/sendmail"))
971 ;; Examine these variables now, so that
972 ;; local binding in the mail buffer will take effect.
974 (and mail-specify-envelope-from
975 (or (mail-envelope-from) user-mail-address
))))
981 (set-buffer-multibyte nil
))
982 (insert-buffer-substring mailbuf
)
983 (goto-char (point-max))
984 ;; require one newline at the end.
985 (or (= (preceding-char) ?
\n)
987 ;; Change header-delimiter to be what sendmail expects.
988 (goto-char (mail-header-end))
989 (delete-region (point) (progn (end-of-line) (point)))
990 (setq delimline
(point-marker))
991 (sendmail-sync-aliases)
993 (expand-mail-aliases (point-min) delimline
))
994 (goto-char (point-min))
995 ;; Ignore any blank lines in the header
996 (while (and (re-search-forward "\n\n\n*" delimline t
)
997 (< (point) delimline
))
998 (replace-match "\n"))
999 (goto-char (point-min))
1000 ;; Look for Resent- headers. They require sending
1001 ;; the message specially.
1002 (let ((case-fold-search t
))
1003 (goto-char (point-min))
1004 (while (re-search-forward "^Resent-\\(to\\|cc\\|bcc\\):" delimline t
)
1005 ;; Put a list of such addresses in resend-to-addresses.
1006 (setq resend-to-addresses
1008 (narrow-to-region (point)
1011 (while (looking-at "^[ \t]")
1014 (append (mail-parse-comma-list)
1015 resend-to-addresses
)))
1016 ;; Delete Resent-BCC ourselves
1017 (if (save-excursion (beginning-of-line)
1018 (looking-at "resent-bcc"))
1019 (delete-region (save-excursion (beginning-of-line) (point))
1020 (save-excursion (end-of-line) (1+ (point))))))
1021 ;;; Apparently this causes a duplicate Sender.
1022 ;;; ;; If the From is different than current user, insert Sender.
1023 ;;; (goto-char (point-min))
1024 ;;; (and (re-search-forward "^From:" delimline t)
1026 ;;; (require 'mail-utils)
1027 ;;; (not (string-equal
1028 ;;; (mail-strip-quoted-names
1029 ;;; (save-restriction
1030 ;;; (narrow-to-region (point-min) delimline)
1031 ;;; (mail-fetch-field "From")))
1032 ;;; (user-login-name))))
1034 ;;; (forward-line 1)
1035 ;;; (insert "Sender: " (user-login-name) "\n")))
1036 ;; Don't send out a blank subject line
1037 (goto-char (point-min))
1038 (if (re-search-forward "^Subject:\\([ \t]*\n\\)+\\b" delimline t
)
1040 ;; This one matches a Subject just before the header delimiter.
1041 (if (and (re-search-forward "^Subject:\\([ \t]*\n\\)+" delimline t
)
1042 (= (match-end 0) delimline
))
1043 (replace-match "")))
1044 ;; Put the "From:" field in unless for some odd reason
1045 ;; they put one in themselves.
1046 (goto-char (point-min))
1047 (if (not (re-search-forward "^From:" delimline t
))
1048 (mail-insert-from-field))
1049 ;; Possibly add a MIME header for the current coding system
1051 (goto-char (point-min))
1052 (and (eq mail-send-nonascii
'mime
)
1053 (not (re-search-forward "^MIME-version:" delimline t
))
1054 (progn (skip-chars-forward "\0-\177")
1055 (/= (point) (point-max)))
1058 (coding-system-get selected-coding
:mime-charset
))
1059 (goto-char delimline
)
1060 (insert "MIME-version: 1.0\n"
1061 "Content-type: text/plain; charset="
1062 (symbol-name charset
) "\n"
1063 "Content-Transfer-Encoding: 8bit\n")))
1064 ;; Insert an extra newline if we need it to work around
1065 ;; Sun's bug that swallows newlines.
1066 (goto-char (1+ delimline
))
1067 (if (eval mail-mailer-swallows-blank-line
)
1069 ;; Find and handle any FCC fields.
1070 (goto-char (point-min))
1071 (if (re-search-forward "^FCC:" delimline t
)
1073 (setq fcc-was-found t
)
1074 (mail-do-fcc delimline
)))
1075 (if mail-interactive
1079 (goto-char (point-min))
1080 (if (let ((case-fold-search t
))
1081 (or resend-to-addresses
1082 (re-search-forward "^To:\\|^cc:\\|^bcc:"
1084 (let* ((default-directory "/")
1085 (coding-system-for-write selected-coding
)
1087 (append (list (point-min) (point-max)
1089 nil errbuf nil
"-oi")
1091 (list "-f" envelope-from
))
1092 ;;; ;; Don't say "from root" if running under su.
1093 ;;; (and (equal (user-real-login-name) "root")
1094 ;;; (list "-f" (user-login-name)))
1095 (and mail-alias-file
1096 (list (concat "-oA" mail-alias-file
)))
1097 (if mail-interactive
1098 ;; These mean "report errors to terminal"
1099 ;; and "deliver interactively"
1101 ;; These mean "report errors by mail"
1102 ;; and "deliver in background".
1104 ;; Get the addresses from the message
1105 ;; unless this is a resend.
1106 ;; We must not do that for a resend
1107 ;; because we would find the original addresses.
1108 ;; For a resend, include the specific addresses.
1109 (or resend-to-addresses
1113 (list "-N" (mapconcat 'symbol-name
1117 (exit-value (apply 'call-process-region args
)))
1118 (or (null exit-value
) (eq 0 exit-value
)
1119 (error "Sending...failed with exit value %d" exit-value
)))
1121 (error "No recipients")))
1122 (if mail-interactive
1125 (goto-char (point-min))
1126 (while (re-search-forward "\n\n* *" nil t
)
1127 (replace-match "; "))
1128 (if (not (zerop (buffer-size)))
1129 (error "Sending...failed to %s"
1130 (buffer-substring (point-min) (point-max)))))))
1131 (kill-buffer tembuf
)
1132 (if (bufferp errbuf
)
1133 (kill-buffer errbuf
)))))
1135 (defun mail-do-fcc (header-end)
1137 (rmailbuf (current-buffer))
1138 (time (current-time))
1139 (tembuf (generate-new-buffer " rmail output"))
1140 (case-fold-search t
))
1141 (unless (markerp header-end
)
1142 (error "Value of `header-end' must be a marker"))
1144 (goto-char (point-min))
1145 (while (re-search-forward "^FCC:[ \t]*" header-end t
)
1146 (setq fcc-list
(cons (buffer-substring (point)
1149 (skip-chars-backward " \t")
1152 (delete-region (match-beginning 0)
1153 (progn (forward-line 1) (point))))
1156 ;; This initial newline is written out if the fcc file already exists.
1157 (insert "\nFrom " (user-login-name) " "
1158 (current-time-string time
) "\n")
1159 ;; Insert the time zone before the year.
1162 (require 'mail-utils
)
1163 (insert (mail-rfc822-time-zone time
) " ")
1164 (goto-char (point-max))
1165 (insert-buffer-substring rmailbuf
)
1166 ;; Make sure messages are separated.
1167 (goto-char (point-max))
1170 ;; ``Quote'' "^From " as ">From "
1171 ;; (note that this isn't really quoting, as there is no requirement
1172 ;; that "^[>]+From " be quoted in the same transparent way.)
1173 (let ((case-fold-search nil
))
1174 (while (search-forward "\nFrom " nil t
)
1178 (let* ((buffer (find-buffer-visiting (car fcc-list
)))
1179 (curbuf (current-buffer))
1182 (beg (point-min)) (end (point-max))
1183 (beg2 (save-excursion (goto-char (point-min))
1184 (forward-line 2) (point))))
1186 ;; File is present in a buffer => append to that buffer.
1189 (setq buffer-matches-file
1190 (and (not (buffer-modified-p))
1191 (verify-visited-file-modtime buffer
)))
1192 ;; Keep the end of the accessible portion at the same place
1193 ;; unless it is the end of the buffer.
1194 (let ((max (if (/= (1+ (buffer-size)) (point-max))
1197 ;; Code below lifted from rmailout.el
1198 ;; function rmail-output-to-rmail-file:
1199 (let ((buffer-read-only nil
)
1200 (msg (and (boundp 'rmail-current-message
)
1201 rmail-current-message
)))
1202 ;; If MSG is non-nil, buffer is in RMAIL mode.
1205 ;; Append to an ordinary buffer as a
1206 ;; Unix mail message.
1207 (rmail-maybe-set-message-counters)
1209 (narrow-to-region (point-max) (point-max))
1210 (insert "\C-l\n0, unseen,,\n*** EOOH ***\n"
1211 "Date: " (mail-rfc822-date) "\n")
1212 (insert-buffer-substring curbuf beg2 end
)
1214 (goto-char (point-min))
1216 (search-backward "\n\^_")
1217 (narrow-to-region (point) (point-max))
1218 (rmail-count-new-messages t
)
1219 (rmail-show-message msg
)
1221 ;; Output file not in rmail mode
1222 ;; => just insert at the end.
1223 (narrow-to-region (point-min) (1+ (buffer-size)))
1224 (goto-char (point-max))
1225 (insert-buffer-substring curbuf beg end
))
1226 (or buffer-matches-file
1228 (if (y-or-n-p (format "Save file %s? "
1231 (setq dont-write-the-file t
))))
1232 (if max
(narrow-to-region (point-min) max
))))))
1233 ;; Append to the file directly,
1234 ;; unless we've already taken care of it.
1235 (unless dont-write-the-file
1236 (if (and (file-exists-p (car fcc-list
))
1237 ;; Check that the file isn't empty. We don't
1238 ;; want to insert a newline at the start of an
1240 (not (zerop (nth 7 (file-attributes (car fcc-list
)))))
1241 (mail-file-babyl-p (car fcc-list
)))
1242 ;; If the file is a Babyl file,
1243 ;; convert the message to Babyl format.
1244 (let ((coding-system-for-write
1245 (or rmail-file-coding-system
1248 (set-buffer (get-buffer-create " mail-temp"))
1249 (setq buffer-read-only nil
)
1251 (insert "\C-l\n0, unseen,,\n*** EOOH ***\n"
1252 "Date: " (mail-rfc822-date) "\n")
1253 (insert-buffer-substring curbuf beg2 end
)
1255 (write-region (point-min) (point-max) (car fcc-list
) t
)
1258 (1+ (point-min)) (point-max) (car fcc-list
) t
)))
1259 (and buffer
(not dont-write-the-file
)
1260 (with-current-buffer buffer
1261 (set-visited-file-modtime))))
1262 (setq fcc-list
(cdr fcc-list
))))
1263 (kill-buffer tembuf
)))
1265 (defun mail-sent-via ()
1266 "Make a Sent-via header line from each To or CC header line."
1269 ;; put a marker at the end of the header
1270 (let ((end (copy-marker (mail-header-end)))
1271 (case-fold-search t
)
1273 (goto-char (point-min))
1274 ;; search for the To: lines and make Sent-via: lines from them
1275 ;; search for the next To: line
1276 (while (re-search-forward "^\\(to\\|cc\\):" end t
)
1277 ;; Grab this line plus all its continuations, sans the `to:'.
1279 (buffer-substring (point)
1281 (if (re-search-forward "^[^ \t\n]" end t
)
1285 ;; Insert a copy, with altered header field name.
1286 (insert-before-markers "Sent-via:" to-line
))))))
1289 "Move point to end of To-field."
1292 (mail-position-on-field "To"))
1294 (defun mail-subject ()
1295 "Move point to end of Subject-field."
1298 (mail-position-on-field "Subject"))
1301 "Move point to end of CC-field. Create a CC field if none."
1304 (or (mail-position-on-field "cc" t
)
1305 (progn (mail-position-on-field "to")
1306 (insert "\nCC: "))))
1309 "Move point to end of BCC-field. Create a BCC field if none."
1312 (or (mail-position-on-field "bcc" t
)
1313 (progn (mail-position-on-field "to")
1314 (insert "\nBCC: "))))
1316 (defun mail-fcc (folder)
1317 "Add a new FCC field, with file name completion."
1318 (interactive "FFolder carbon copy: ")
1320 (or (mail-position-on-field "fcc" t
) ;Put new field after exiting FCC.
1321 (mail-position-on-field "to"))
1322 (insert "\nFCC: " folder
))
1324 (defun mail-reply-to ()
1325 "Move point to end of Reply-To-field. Create a Reply-To field if none."
1328 (mail-position-on-field "Reply-To"))
1330 (defun mail-mail-reply-to ()
1331 "Move point to end of Mail-Reply-To field.
1332 Create a Mail-Reply-To field if none."
1335 (or (mail-position-on-field "mail-reply-to" t
)
1336 (progn (mail-position-on-field "to")
1337 (insert "\nMail-Reply-To: "))))
1339 (defun mail-mail-followup-to ()
1340 "Move point to end of Mail-Followup-To field.
1341 Create a Mail-Followup-To field if none."
1344 (or (mail-position-on-field "mail-followup-to" t
)
1345 (progn (mail-position-on-field "to")
1346 (insert "\nMail-Followup-To: "))))
1348 (defun mail-position-on-field (field &optional soft
)
1350 (case-fold-search t
))
1351 (setq end
(mail-header-end))
1352 (goto-char (point-min))
1353 (if (re-search-forward (concat "^" (regexp-quote field
) ":") end t
)
1355 (re-search-forward "^[^ \t]" nil
'move
)
1357 (skip-chars-backward "\n")
1360 (progn (goto-char end
)
1361 (insert field
": \n")
1362 (skip-chars-backward "\n")))
1366 "Move point to beginning of message text."
1369 (goto-char (mail-text-start)))
1371 (defun mail-signature (&optional atpoint
)
1372 "Sign letter with signature based on `mail-signature-file'.
1373 Prefix arg means put contents at point."
1377 (goto-char (point-max)))
1378 (skip-chars-backward " \t\n")
1381 (delete-region (point) (point-max)))
1382 (if (stringp mail-signature
)
1383 (insert mail-signature
)
1384 (insert "\n\n-- \n")
1385 (insert-file-contents (expand-file-name mail-signature-file
)))))
1387 (defun mail-fill-yanked-message (&optional justifyp
)
1388 "Fill the paragraphs of a message yanked into this one.
1389 Numeric argument means justify as well."
1392 (goto-char (mail-text-start))
1393 (fill-individual-paragraphs (point)
1396 mail-citation-prefix-regexp
)))
1398 (defun mail-indent-citation ()
1399 "Modify text just inserted from a message to be cited.
1400 The inserted text should be the region.
1401 When this function returns, the region is again around the modified text.
1403 Normally, indent each nonblank line `mail-indentation-spaces' spaces.
1404 However, if `mail-yank-prefix' is non-nil, insert that prefix on each line."
1405 (mail-yank-clear-headers (region-beginning) (region-end))
1406 (if (null mail-yank-prefix
)
1407 (indent-rigidly (region-beginning) (region-end)
1408 mail-indentation-spaces
)
1410 (let ((end (set-marker (make-marker) (region-end))))
1411 (goto-char (region-beginning))
1412 (while (< (point) end
)
1413 (insert mail-yank-prefix
)
1414 (forward-line 1))))))
1416 (defun mail-yank-original (arg)
1417 "Insert the message being replied to, if any (in rmail).
1418 Puts point after the text and mark before.
1419 Normally, indents each nonblank line ARG spaces (default 3).
1420 However, if `mail-yank-prefix' is non-nil, insert that prefix on each line.
1422 Just \\[universal-argument] as argument means don't indent, insert no prefix,
1423 and don't delete any header fields."
1425 (if mail-reply-action
1426 (let ((start (point))
1427 (original mail-reply-action
))
1428 (and (consp original
) (eq (car original
) 'insert-buffer
)
1429 (setq original
(nth 1 original
)))
1430 (if (consp original
)
1431 (apply (car original
) (cdr original
))
1432 ;; If the original message is in another window in the same frame,
1433 ;; delete that window to save screen space.
1434 ;; t means don't alter other frames.
1435 (delete-windows-on original t
)
1437 ;; We really want this to set mark.
1438 (insert-buffer original
))
1439 (set-text-properties (point) (mark t
) nil
))
1443 (let ((mail-indentation-spaces (if arg
(prefix-numeric-value arg
)
1444 mail-indentation-spaces
))
1445 ;; Avoid error in Transient Mark mode
1446 ;; on account of mark's being inactive.
1447 (mark-even-if-inactive t
))
1448 (cond (mail-citation-hook
1449 ;; Bind mail-citation-header to the inserted
1450 ;; message's header.
1451 (let ((mail-citation-header
1452 (buffer-substring-no-properties
1456 (narrow-to-region start
(point-max))
1460 (run-hooks 'mail-citation-hook
)))
1462 (run-hooks 'mail-yank-hooks
))
1464 (mail-indent-citation)))))
1465 ;; This is like exchange-point-and-mark, but doesn't activate the mark.
1466 ;; It is cleaner to avoid activation, even though the command
1467 ;; loop would deactivate the mark because we inserted text.
1468 (goto-char (prog1 (mark t
)
1469 (set-marker (mark-marker) (point) (current-buffer))))
1470 (if (not (eolp)) (insert ?
\n)))))
1472 (defun mail-yank-clear-headers (start end
)
1475 (setq temp start start end end temp
)))
1476 (if mail-yank-ignored-headers
1479 (if (search-forward "\n\n" end t
)
1481 (narrow-to-region start
(point))
1483 (while (let ((case-fold-search t
))
1484 (re-search-forward mail-yank-ignored-headers nil t
))
1486 (delete-region (point)
1487 (progn (re-search-forward "\n[^ \t]")
1491 (defun mail-yank-region (arg)
1492 "Insert the selected region from the message being replied to.
1493 Puts point after the text and mark before.
1494 Normally, indents each nonblank line ARG spaces (default 3).
1495 However, if `mail-yank-prefix' is non-nil, insert that prefix on each line.
1497 Just \\[universal-argument] as argument means don't indent, insert no prefix,
1498 and don't delete any header fields."
1500 (and (consp mail-reply-action
)
1501 (eq (car mail-reply-action
) 'insert-buffer
)
1502 (with-current-buffer (nth 1 mail-reply-action
)
1504 (error "No mark set: %S" (current-buffer))))
1505 (let ((buffer (nth 1 mail-reply-action
))
1507 ;; Avoid error in Transient Mark mode
1508 ;; on account of mark's being inactive.
1509 (mark-even-if-inactive t
))
1510 ;; Insert the citation text.
1511 (insert (with-current-buffer buffer
1512 (buffer-substring-no-properties (point) (mark))))
1514 ;; Indent or otherwise annotate the citation text.
1517 (let ((mail-indentation-spaces (if arg
(prefix-numeric-value arg
)
1518 mail-indentation-spaces
)))
1519 (if mail-citation-hook
1520 ;; Bind mail-citation-hook to the original message's header.
1521 (let ((mail-citation-header
1522 (with-current-buffer buffer
1523 (buffer-substring-no-properties
1526 (goto-char (point-min))
1529 (run-hooks 'mail-citation-hook
))
1531 (run-hooks 'mail-yank-hooks
)
1532 (mail-indent-citation))))))))
1534 (defun mail-split-line ()
1535 "Split current line, moving portion beyond point vertically down.
1536 If the current line has `mail-yank-prefix', insert it on the new line."
1538 (split-line mail-yank-prefix
))
1541 (defun mail-attach-file (&optional file
)
1542 "Insert a file at the end of the buffer, with separator lines around it."
1543 (interactive "fAttach file: ")
1545 (goto-char (point-max))
1546 (or (bolp) (newline))
1548 (let ((start (point))
1550 (insert (format "===File %s===" file
))
1551 (insert-char ?
= (max 0 (- 60 (current-column))))
1553 (setq middle
(point))
1554 (insert "============================================================\n")
1557 (insert-file-contents file
)
1558 (or (bolp) (newline))
1559 (goto-char start
))))
1561 ;; Put these commands last, to reduce chance of lossage from quitting
1562 ;; in middle of loading the file.
1564 ;;;###autoload (add-hook 'same-window-buffer-names "*mail*")
1567 (defun mail (&optional noerase to subject in-reply-to cc replybuffer actions
)
1568 "Edit a message to be sent. Prefix arg means resume editing (don't erase).
1569 When this function returns, the buffer `*mail*' is selected.
1570 The value is t if the message was newly initialized; otherwise, nil.
1572 Optionally, the signature file `mail-signature-file' can be inserted at the
1573 end; see the variable `mail-signature'.
1576 While editing message, type \\[mail-send-and-exit] to send the message and exit.
1578 Various special commands starting with C-c are available in sendmail mode
1579 to move to message header fields:
1582 If `mail-self-blind' is non-nil, a BCC to yourself is inserted
1583 when the message is initialized.
1585 If `mail-default-reply-to' is non-nil, it should be an address (a string);
1586 a Reply-to: field with that address is inserted.
1588 If `mail-archive-file-name' is non-nil, an FCC field with that file name
1591 The normal hook `mail-setup-hook' is run after the message is
1592 initialized. It can add more default fields to the message.
1594 The first argument, NOERASE, determines what to do when there is
1595 an existing modified `*mail*' buffer. If NOERASE is nil, the
1596 existing mail buffer is used, and the user is prompted whether to
1597 keep the old contents or to erase them. If NOERASE has the value
1598 `new', a new mail buffer will be created instead of using the old
1599 one. Any other non-nil value means to always select the old
1600 buffer without erasing the contents.
1602 The second through fifth arguments,
1603 TO, SUBJECT, IN-REPLY-TO and CC, specify if non-nil
1604 the initial contents of those header fields.
1605 These arguments should not have final newlines.
1606 The sixth argument REPLYBUFFER is a buffer which contains an
1607 original message being replied to, or else an action
1608 of the form (FUNCTION . ARGS) which says how to insert the original.
1609 Or it can be nil, if not replying to anything.
1610 The seventh argument ACTIONS is a list of actions to take
1611 if/when the message is sent. Each action looks like (FUNCTION . ARGS);
1612 when the message is sent, we apply FUNCTION to ARGS.
1613 This is how Rmail arranges to mark messages `answered'."
1615 ;;; This is commented out because I found it was confusing in practice.
1616 ;;; It is easy enough to rename *mail* by hand with rename-buffer
1617 ;;; if you want to have multiple mail buffers.
1618 ;;; And then you can control which messages to save. --rms.
1621 ;;; ;; If requested, look for a mail buffer that is modified and go to it.
1624 ;;; (while (and (setq buffer
1625 ;;; (get-buffer (if (= 1 index) "*mail*"
1626 ;;; (format "*mail*<%d>" index))))
1627 ;;; (not (buffer-modified-p buffer)))
1628 ;;; (setq index (1+ index)))
1629 ;;; (if buffer (switch-to-buffer buffer)
1630 ;;; ;; If none exists, start a new message.
1631 ;;; ;; This will never re-use an existing unmodified mail buffer
1632 ;;; ;; (since index is not 1 anymore). Perhaps it should.
1633 ;;; (setq noerase nil))))
1634 ;;; ;; Unless we found a modified message and are happy, start a new message.
1635 ;;; (if (not noerase)
1637 ;;; ;; Look for existing unmodified mail buffer.
1638 ;;; (while (and (setq buffer
1639 ;;; (get-buffer (if (= 1 index) "*mail*"
1640 ;;; (format "*mail*<%d>" index))))
1641 ;;; (buffer-modified-p buffer))
1642 ;;; (setq index (1+ index)))
1643 ;;; ;; If none, make a new one.
1645 ;;; (setq buffer (generate-new-buffer "*mail*")))
1646 ;;; ;; Go there and initialize it.
1647 ;;; (switch-to-buffer buffer)
1649 ;;; (setq default-directory (expand-file-name "~/"))
1650 ;;; (auto-save-mode auto-save-default)
1652 ;;; (mail-setup to subject in-reply-to cc replybuffer actions)
1653 ;;; (if (and buffer-auto-save-file-name
1654 ;;; (file-exists-p buffer-auto-save-file-name))
1655 ;;; (message "Auto save file for draft message exists; consider M-x mail-recover"))
1658 (if (eq noerase
'new
)
1659 (pop-to-buffer (generate-new-buffer "*mail*"))
1661 (not (get-buffer "*mail*"))
1663 (pop-to-buffer "*mail*"))
1665 ;; Avoid danger that the auto-save file can't be written.
1666 (let ((dir (expand-file-name
1667 (file-name-as-directory mail-default-directory
))))
1668 (if (file-exists-p dir
)
1669 (setq default-directory dir
)))
1670 ;; Only call auto-save-mode if necessary, to avoid changing auto-save file.
1671 (if (or (and auto-save-default
(not buffer-auto-save-file-name
))
1672 (and (not auto-save-default
) buffer-auto-save-file-name
))
1673 (auto-save-mode auto-save-default
))
1675 ;; Disconnect the buffer from its visited file
1676 ;; (in case the user has actually visited a file *mail*).
1677 ; (set-visited-file-name nil)
1679 (and (not (and noerase
1680 (not (eq noerase
'new
))))
1681 (if buffer-file-name
1682 (if (buffer-modified-p)
1683 (when (y-or-n-p "Buffer has unsaved changes; reinitialize it and discard them? ")
1684 (if (y-or-n-p "Disconnect buffer from visited file? ")
1685 (set-visited-file-name nil
))
1687 (when (y-or-n-p "Reinitialize buffer, and disconnect it from the visited file? ")
1688 (set-visited-file-name nil
)
1690 ;; A non-file-visiting buffer.
1691 (if (buffer-modified-p)
1692 (y-or-n-p "Unsent message being composed; erase it? ")
1694 (let ((inhibit-read-only t
))
1696 (mail-setup to subject in-reply-to cc replybuffer actions
)
1697 (setq initialized t
)))
1698 (if (and buffer-auto-save-file-name
1699 (file-exists-p buffer-auto-save-file-name
))
1700 (message "Auto save file for draft message exists; consider M-x mail-recover"))
1703 (defun mail-recover-1 ()
1704 "Pop up a list of auto-saved draft messages so you can recover one of them."
1706 (let ((file-name (make-auto-save-file-name))
1707 (ls-lisp-support-shell-wildcards t
)
1708 non-random-len wildcard
)
1709 ;; Remove the random part from the auto-save-file-name, and
1710 ;; create a wildcard which matches possible candidates.
1711 ;; Note: this knows that make-auto-save-file-name appends
1712 ;; "#<RANDOM-STUFF>#" to the buffer name, where RANDOM-STUFF
1713 ;; is the result of (make-temp-name "").
1714 (setq non-random-len
1715 (- (length file-name
) (length (make-temp-name "")) 1))
1716 (setq wildcard
(concat (substring file-name
0 non-random-len
) "*"))
1717 (if (null (file-expand-wildcards wildcard
))
1718 (message "There are no auto-saved drafts to recover")
1719 ;; Bind dired-trivial-filenames to t because all auto-save file
1720 ;; names are normally ``trivial'', so Dired will set point after
1721 ;; all the files, at buffer bottom. We want it on the first
1723 (let ((dired-trivial-filenames t
))
1724 (dired-other-window wildcard
(concat dired-listing-switches
"t")))
1725 (rename-buffer "*Auto-saved Drafts*" t
)
1727 (goto-char (point-min))
1728 (or (looking-at " Move to the draft file you want to recover,")
1729 (let ((inhibit-read-only t
))
1730 ;; Each line starts with a space so that Font Lock mode
1731 ;; won't highlight the first character.
1733 Move to the draft file you want to recover, then type C-c C-c
1734 to recover text of message whose composition was interrupted.
1735 To browse text of a draft, type v on the draft file's line.
1737 You can also delete some of these files;
1738 type d on a line to mark that file for deletion.
1740 List of possible auto-save files for recovery:
1744 (let ((map (make-sparse-keymap)))
1745 (set-keymap-parent map
(current-local-map))
1747 (define-key (current-local-map) "v"
1750 (let ((coding-system-for-read 'utf-8-emacs-unix
))
1751 (dired-view-file))))
1752 (define-key (current-local-map) "\C-c\C-c"
1755 (let ((fname (dired-get-filename))
1756 ;; Auto-saved files are written in the internal
1757 ;; representation, so they should be read accordingly.
1758 (coding-system-for-read 'utf-8-emacs-unix
))
1759 (switch-to-buffer-other-window "*mail*")
1760 (let ((buffer-read-only nil
))
1762 (insert-file-contents fname nil
)
1763 ;; insert-file-contents will set buffer-file-coding-system
1764 ;; to utf-8-emacs, which is probably not what they want to
1765 ;; use for sending the message. But we don't know what
1766 ;; was its value before the buffer was killed or Emacs
1767 ;; crashed. We therefore reset buffer-file-coding-system
1768 ;; to the default value, so that either the default does
1769 ;; TRT, or the user will get prompted for the right
1770 ;; encoding when they send the message.
1771 (setq buffer-file-coding-system
1772 default-buffer-file-coding-system
))))))))
1774 (defun mail-recover ()
1775 "Recover interrupted mail composition from auto-save files.
1777 If the mail buffer has a current valid auto-save file,
1778 the command recovers that file. Otherwise, it displays a
1779 buffer showing the existing auto-saved draft messages;
1780 you can move to one of them and type C-c C-c to recover that one."
1782 ;; In case they invoke us from some random buffer...
1783 (switch-to-buffer "*mail*")
1784 ;; If *mail* didn't exist, set its directory, so that auto-saved
1785 ;; drafts will be found.
1786 (let ((dir (expand-file-name
1787 (file-name-as-directory mail-default-directory
))))
1788 (if (file-exists-p dir
)
1789 (setq default-directory dir
)))
1790 (or (eq major-mode
'mail-mode
)
1792 (let ((file-name buffer-auto-save-file-name
))
1793 (cond ((and file-name
(file-exists-p file-name
))
1795 ;; This used to invoke `ls' via call-process, but
1796 ;; dired-noselect is more portable to systems where
1797 ;; `ls' is not a standard program (it will use
1798 ;; ls-lisp instead).
1799 (dired-noselect file-name
1800 (concat dired-listing-switches
"t"))))
1801 (save-selected-window
1802 (select-window (display-buffer dispbuf t
))
1803 (goto-char (point-min))
1805 (dired-move-to-filename)
1806 (setq dispbuf
(rename-buffer "*Directory*" t
)))
1807 (if (not (yes-or-no-p
1808 (format "Recover mail draft from auto save file %s? "
1810 (error "mail-recover cancelled")
1811 (let ((buffer-read-only nil
)
1812 (buffer-coding buffer-file-coding-system
)
1813 ;; Auto-save files are written in internal
1814 ;; representation of non-ASCII characters.
1815 (coding-system-for-read 'utf-8-emacs-unix
))
1817 (insert-file-contents file-name nil
)
1818 (setq buffer-file-coding-system buffer-coding
)))))
1819 (t (mail-recover-1)))))
1822 (defun mail-other-window (&optional noerase to subject in-reply-to cc replybuffer sendactions
)
1823 "Like `mail' command, but display mail buffer in another window."
1825 (let ((pop-up-windows t
)
1826 (special-display-buffer-names nil
)
1827 (special-display-regexps nil
)
1828 (same-window-buffer-names nil
)
1829 (same-window-regexps nil
))
1830 (pop-to-buffer "*mail*"))
1831 (mail noerase to subject in-reply-to cc replybuffer sendactions
))
1834 (defun mail-other-frame (&optional noerase to subject in-reply-to cc replybuffer sendactions
)
1835 "Like `mail' command, but display mail buffer in another frame."
1837 (let ((pop-up-frames t
)
1838 (special-display-buffer-names nil
)
1839 (special-display-regexps nil
)
1840 (same-window-buffer-names nil
)
1841 (same-window-regexps nil
))
1842 (pop-to-buffer "*mail*"))
1843 (mail noerase to subject in-reply-to cc replybuffer sendactions
))
1845 ;;; Do not add anything but external entries on this page.
1849 ;;; arch-tag: 48bc1025-d993-4d31-8d81-2a29491f0626
1850 ;;; sendmail.el ends here