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