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