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