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