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