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