]> code.delx.au - gnu-emacs/blob - lisp/mail/sendmail.el
Require sendmail.
[gnu-emacs] / lisp / mail / sendmail.el
1 ;;; sendmail.el --- mail sending commands for Emacs.
2
3 ;; Copyright (C) 1985, 86, 92, 93, 94, 95, 96 Free Software Foundation, Inc.
4
5 ;; Maintainer: FSF
6 ;; Keywords: mail
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;; This mode provides mail-sending facilities from within Emacs. It is
28 ;; documented in the Emacs user's manual.
29
30 ;;; Code:
31 (defgroup sendmail nil
32 "Mail sending commands for Emacs."
33 :prefix "mail-"
34 :group 'mail)
35
36 ;;;###autoload
37 (defcustom mail-from-style 'angles "\
38 *Specifies how \"From:\" fields look.
39
40 If `nil', they contain just the return address like:
41 king@grassland.com
42 If `parens', they look like:
43 king@grassland.com (Elvis Parsley)
44 If `angles', they look like:
45 Elvis Parsley <king@grassland.com>"
46 :type '(choice (const nil) (const parens) (const angles))
47 :group 'sendmail)
48
49 ;;;###autoload
50 (defcustom mail-self-blind nil "\
51 *Non-nil means insert BCC to self in messages to be sent.
52 This is done when the message is initialized,
53 so you can remove or alter the BCC field to override the default."
54 :type 'boolean
55 :group 'sendmail)
56
57 ;;;###autoload
58 (defcustom mail-interactive nil "\
59 *Non-nil means when sending a message wait for and display errors.
60 nil means let mailer mail back a message to report errors."
61 :type 'boolean
62 :group 'sendmail)
63
64 ;;;###autoload
65 (defcustom mail-yank-ignored-headers "^via:\\|^mail-from:\\|^origin:\\|^status:\\|^remailed\\|^received:\\|^message-id:\\|^summary-line:\\|^to:\\|^subject:\\|^in-reply-to:\\|^return-path:" "\
66 *Delete these headers from old message when it's inserted in a reply."
67 :type 'regexp
68 :group 'sendmail)
69
70 ;; Useful to set in site-init.el
71 ;;;###autoload
72 (defvar send-mail-function 'sendmail-send-it "\
73 Function to call to send the current buffer as mail.
74 The headers should be delimited by a line whose contents
75 match the variable `mail-header-separator'.")
76
77 ;;;###autoload
78 (defcustom mail-header-separator "--text follows this line--" "\
79 *Line used to separate headers from text in messages being composed."
80 :type 'string
81 :group 'sendmail)
82
83 ;; Set up mail-header-separator for use as a category text property.
84 (put 'mail-header-separator 'rear-nonsticky '(category))
85 ;;; This was a nice idea, for preventing accidental modification of
86 ;;; the separator. But I found it also prevented or obstructed
87 ;;; certain deliberate operations, such as copying the separator line
88 ;;; up to the top to send myself a copy of an already sent outgoing message
89 ;;; and other things. So I turned it off. --rms.
90 ;;;(put 'mail-header-separator 'read-only t)
91
92 ;;;###autoload
93 (defcustom mail-archive-file-name nil "\
94 *Name of file to write all outgoing messages in, or nil for none.
95 This can be an inbox file or an Rmail file."
96 :type '(choice file (const nil))
97 :group 'sendmail)
98
99 ;;;###autoload
100 (defcustom mail-default-reply-to nil
101 "*Address to insert as default Reply-to field of outgoing messages.
102 If nil, it will be initialized from the REPLYTO environment variable
103 when you first send mail."
104 :type '(choice (const nil) string)
105 :group 'sendmail)
106
107 ;;;###autoload
108 (defcustom mail-alias-file nil
109 "*If non-nil, the name of a file to use instead of `/usr/lib/aliases'.
110 This file defines aliases to be expanded by the mailer; this is a different
111 feature from that of defining aliases in `.mailrc' to be expanded in Emacs.
112 This variable has no effect unless your system uses sendmail as its mailer."
113 :type '(choice (const nil) file)
114 :group 'sendmail)
115
116 ;;;###autoload
117 (defcustom mail-personal-alias-file "~/.mailrc"
118 "*If non-nil, the name of the user's personal mail alias file.
119 This file typically should be in same format as the `.mailrc' file used by
120 the `Mail' or `mailx' program.
121 This file need not actually exist."
122 :type '(choice (const nil) file)
123 :group 'sendmail)
124
125 (defcustom mail-setup-hook nil
126 "Normal hook, run each time a new outgoing mail message is initialized.
127 The function `mail-setup' runs this hook."
128 :type 'hook
129 :group 'sendmail)
130
131 (defvar mail-aliases t
132 "Alist of mail address aliases,
133 or t meaning should be initialized from your mail aliases file.
134 \(The file's name is normally `~/.mailrc', but your MAILRC environment
135 variable can override that name.)
136 The alias definitions in the file have this form:
137 alias ALIAS MEANING")
138
139 (defvar mail-alias-modtime nil
140 "The modification time of your mail alias file when it was last examined.")
141
142 (defcustom mail-yank-prefix nil
143 "*Prefix insert on lines of yanked message being replied to.
144 nil means use indentation."
145 :type '(choice (const nil) string)
146 :group 'sendmail)
147
148 (defcustom mail-indentation-spaces 3
149 "*Number of spaces to insert at the beginning of each cited line.
150 Used by `mail-yank-original' via `mail-indent-citation'."
151 :type 'integer
152 :group 'sendmail)
153 (defvar mail-yank-hooks nil
154 "Obsolete hook for modifying a citation just inserted in the mail buffer.
155 Each hook function can find the citation between (point) and (mark t).
156 And each hook function should leave point and mark around the citation
157 text as modified.
158
159 This is a normal hook, misnamed for historical reasons.
160 It is semi-obsolete and mail agents should no longer use it.")
161
162 (defcustom mail-citation-hook nil
163 "*Hook for modifying a citation just inserted in the mail buffer.
164 Each hook function can find the citation between (point) and (mark t).
165 And each hook function should leave point and mark around the citation
166 text as modified.
167
168 If this hook is entirely empty (nil), a default action is taken
169 instead of no action."
170 :type 'hook
171 :group 'sendmail)
172
173 (defvar mail-abbrevs-loaded nil)
174 (defvar mail-mode-map nil)
175
176 (autoload 'build-mail-aliases "mailalias"
177 "Read mail aliases from user's personal aliases file and set `mail-aliases'."
178 nil)
179
180 (autoload 'expand-mail-aliases "mailalias"
181 "Expand all mail aliases in suitable header fields found between BEG and END.
182 Suitable header fields are `To', `Cc' and `Bcc' and their `Resent-' variants.
183 Optional second arg EXCLUDE may be a regular expression defining text to be
184 removed from alias expansions."
185 nil)
186
187 ;;;###autoload
188 (defcustom mail-signature nil
189 "*Text inserted at end of mail buffer when a message is initialized.
190 If t, it means to insert the contents of the file `mail-signature-file'."
191 :type '(choice (const nil) (const t) string)
192 :group 'sendmail)
193
194 (defcustom mail-signature-file "~/.signature"
195 "*File containing the text inserted at end of mail buffer."
196 :type 'file
197 :group 'sendmail)
198
199 (defvar mail-reply-action nil)
200 (defvar mail-send-actions nil
201 "A list of actions to be performed upon successful sending of a message.")
202 (put 'mail-reply-action 'permanent-local t)
203 (put 'mail-send-actions 'permanent-local t)
204
205 (defcustom mail-default-headers nil
206 "*A string containing header lines, to be inserted in outgoing messages.
207 It is inserted before you edit the message,
208 so you can edit or delete these lines."
209 :type '(choice (const nil) string)
210 :group 'sendmail)
211
212 (defcustom mail-bury-selects-summary t
213 "*If non-nil, try to show RMAIL summary buffer after returning from mail.
214 The functions \\[mail-send-on-exit] or \\[mail-dont-send] select
215 the RMAIL summary buffer before returning, if it exists and this variable
216 is non-nil."
217 :type 'boolean
218 :group 'sendmail)
219
220 ;; I find that this happens so often, for innocent reasons,
221 ;; that it is not acceptable to bother the user about it -- rms.
222 (defcustom mail-send-nonascii t
223 "*Specify whether to allow sending non-ASCII characters in mail.
224 If t, that means do allow it. nil means don't allow it.
225 `query' means ask the user each time.
226 Including non-ASCII characters in a mail message can be problematical
227 for the recipient, who may not know how to decode them properly."
228 :type '(choice (const t) (const nil) (const query))
229 :group 'sendmail)
230
231 ;; Note: could use /usr/ucb/mail instead of sendmail;
232 ;; options -t, and -v if not interactive.
233 (defvar mail-mailer-swallows-blank-line
234 (if (and (string-match "sparc-sun-sunos\\(\\'\\|[^5]\\)" system-configuration)
235 (file-readable-p "/etc/sendmail.cf")
236 (let ((buffer (get-buffer-create " *temp*")))
237 (unwind-protect
238 (save-excursion
239 (set-buffer buffer)
240 (insert-file-contents "/etc/sendmail.cf")
241 (goto-char (point-min))
242 (let ((case-fold-search nil))
243 (re-search-forward "^OR\\>" nil t)))
244 (kill-buffer buffer))))
245 ;; According to RFC822, "The field-name must be composed of printable
246 ;; ASCII characters (i.e. characters that have decimal values between
247 ;; 33 and 126, except colon)", i.e. any chars except ctl chars,
248 ;; space, or colon.
249 '(looking-at "[ \t]\\|[][!\"#$%&'()*+,-./0-9;<=>?@A-Z\\\\^_`a-z{|}~]+:"))
250 "Set this non-nil if the system's mailer runs the header and body together.
251 \(This problem exists on Sunos 4 when sendmail is run in remote mode.)
252 The value should be an expression to test whether the problem will
253 actually occur.")
254
255 (defvar mail-mode-syntax-table nil
256 "Syntax table used while in mail mode.")
257
258 (if (not mail-mode-syntax-table)
259 (progn
260 (setq mail-mode-syntax-table (copy-syntax-table text-mode-syntax-table))
261 (modify-syntax-entry ?% ". " mail-mode-syntax-table)))
262
263 (defvar mail-font-lock-keywords
264 (eval-when-compile
265 (let* ((cite-chars "[>|}]")
266 (cite-prefix "A-Za-z")
267 (cite-suffix (concat cite-prefix "0-9_.@-`'\"")))
268 (list '("^\\(To\\|Newsgroups\\):" . font-lock-function-name-face)
269 '("^\\(B?CC\\|Reply-to\\):" . font-lock-keyword-face)
270 '("^\\(Subject:\\)[ \t]*\\(.+\\)?"
271 (1 font-lock-comment-face) (2 font-lock-type-face nil t))
272 ;; Use EVAL to delay in case `mail-header-separator' gets changed.
273 '(eval .
274 (let ((separator (if (zerop (length mail-header-separator))
275 " \\`\\' "
276 (regexp-quote mail-header-separator))))
277 (cons (concat "^" separator "$") 'font-lock-warning-face)))
278 ;; Use MATCH-ANCHORED to effectively anchor the regexp left side.
279 `(,cite-chars
280 (,(concat "\\=[ \t]*"
281 "\\(\\([" cite-prefix "]+[" cite-suffix "]*\\)?"
282 "\\(" cite-chars "[ \t]*\\)\\)+"
283 "\\(.*\\)")
284 (beginning-of-line) (end-of-line)
285 (2 font-lock-constant-face nil t)
286 (4 font-lock-comment-face nil t)))
287 '("^\\(X-[A-Za-z0-9-]+\\|In-reply-to\\):.*"
288 . font-lock-string-face))))
289 "Additional expressions to highlight in Mail mode.")
290
291 (defcustom mail-send-hook nil
292 "Normal hook run before sending mail, in Mail mode."
293 :type 'hook
294 :group 'sendmail)
295 \f
296 (defun sendmail-sync-aliases ()
297 (let ((modtime (nth 5 (file-attributes mail-personal-alias-file))))
298 (or (equal mail-alias-modtime modtime)
299 (setq mail-alias-modtime modtime
300 mail-aliases t))))
301
302 (defun mail-setup (to subject in-reply-to cc replybuffer actions)
303 (or mail-default-reply-to
304 (setq mail-default-reply-to (getenv "REPLYTO")))
305 (sendmail-sync-aliases)
306 (if (eq mail-aliases t)
307 (progn
308 (setq mail-aliases nil)
309 (if (file-exists-p mail-personal-alias-file)
310 (build-mail-aliases))))
311 ;; Don't leave this around from a previous message.
312 (kill-local-variable 'buffer-file-coding-system)
313 (kill-local-variable 'enable-multibyte-characters)
314 (if current-input-method
315 (inactivate-input-method))
316 (setq mail-send-actions actions)
317 (setq mail-reply-action replybuffer)
318 (goto-char (point-min))
319 (insert "To: ")
320 (save-excursion
321 (if to
322 ;; Here removed code to extract names from within <...>
323 ;; on the assumption that mail-strip-quoted-names
324 ;; has been called and has done so.
325 (let ((fill-prefix "\t")
326 (address-start (point)))
327 (insert to "\n")
328 (fill-region-as-paragraph address-start (point-max)))
329 (newline))
330 (if cc
331 (let ((fill-prefix "\t")
332 (address-start (progn (insert "CC: ") (point))))
333 (insert cc "\n")
334 (fill-region-as-paragraph address-start (point-max))))
335 (if in-reply-to
336 (let ((fill-prefix "\t")
337 (fill-column 78)
338 (address-start (point)))
339 (insert "In-reply-to: " in-reply-to "\n")
340 (fill-region-as-paragraph address-start (point-max))))
341 (insert "Subject: " (or subject "") "\n")
342 (if mail-default-headers
343 (insert mail-default-headers))
344 (if mail-default-reply-to
345 (insert "Reply-to: " mail-default-reply-to "\n"))
346 (if mail-self-blind
347 (insert "BCC: " user-mail-address "\n"))
348 (if mail-archive-file-name
349 (insert "FCC: " mail-archive-file-name "\n"))
350 (put-text-property (point)
351 (progn
352 (insert mail-header-separator "\n")
353 (1- (point)))
354 'category 'mail-header-separator)
355 ;; Insert the signature. But remember the beginning of the message.
356 (if to (setq to (point)))
357 (cond ((eq mail-signature t)
358 (if (file-exists-p mail-signature-file)
359 (progn
360 (insert "\n\n-- \n")
361 (insert-file-contents mail-signature-file))))
362 (mail-signature
363 (insert mail-signature)))
364 (goto-char (point-max))
365 (or (bolp) (newline)))
366 (if to (goto-char to))
367 (or to subject in-reply-to
368 (set-buffer-modified-p nil))
369 (run-hooks 'mail-setup-hook))
370 \f
371 ;;;###autoload
372 (defun mail-mode ()
373 "Major mode for editing mail to be sent.
374 Like Text Mode but with these additional commands:
375 \\[mail-send] mail-send (send the message) \\[mail-send-and-exit] mail-send-and-exit
376 Here are commands that move to a header field (and create it if there isn't):
377 \\[mail-to] move to To: \\[mail-subject] move to Subject:
378 \\[mail-cc] move to CC: \\[mail-bcc] move to BCC:
379 \\[mail-fcc] move to FCC:
380 \\[mail-text] mail-text (move to beginning of message text).
381 \\[mail-signature] mail-signature (insert `mail-signature-file' file).
382 \\[mail-yank-original] mail-yank-original (insert current message, in Rmail).
383 \\[mail-fill-yanked-message] mail-fill-yanked-message (fill what was yanked).
384 \\[mail-sent-via] mail-sent-via (add a Sent-via field for each To or CC)."
385 (interactive)
386 (kill-all-local-variables)
387 (make-local-variable 'mail-reply-action)
388 (make-local-variable 'mail-send-actions)
389 (set-syntax-table mail-mode-syntax-table)
390 (use-local-map mail-mode-map)
391 (setq local-abbrev-table text-mode-abbrev-table)
392 (setq major-mode 'mail-mode)
393 (setq mode-name "Mail")
394 (setq buffer-offer-save t)
395 (make-local-variable 'font-lock-defaults)
396 (setq font-lock-defaults '(mail-font-lock-keywords t))
397 (make-local-variable 'paragraph-separate)
398 (make-local-variable 'paragraph-start)
399 (make-local-variable 'normal-auto-fill-function)
400 (setq normal-auto-fill-function 'mail-mode-auto-fill)
401 (make-local-variable 'fill-paragraph-function)
402 (setq fill-paragraph-function 'mail-mode-fill-paragraph)
403 (make-local-variable 'adaptive-fill-regexp)
404 (setq adaptive-fill-regexp
405 (concat "[ \t]*[-a-z0-9A-Z]*>+[ \t]*\\|" adaptive-fill-regexp))
406 (make-local-variable 'adaptive-fill-first-line-regexp)
407 (setq adaptive-fill-first-line-regexp
408 (concat "[ \t]*[-a-z0-9A-Z]*>+[ \t]*\\|" adaptive-fill-first-line-regexp))
409 ;; `-- ' precedes the signature. `-----' appears at the start of the
410 ;; lines that delimit forwarded messages.
411 ;; Lines containing just >= 3 dashes, perhaps after whitespace,
412 ;; are also sometimes used and should be separators.
413 (setq paragraph-start (concat (regexp-quote mail-header-separator)
414 "$\\|[ \t]*[a-z0-9A-Z]*>+[ \t]*$\\|[ \t]*$\\|"
415 page-delimiter))
416 (setq paragraph-separate paragraph-start)
417 (run-hooks 'text-mode-hook 'mail-mode-hook))
418
419 (defun mail-mode-auto-fill ()
420 "Carry out Auto Fill for Mail mode.
421 If within the headers, this makes the new lines into continuation lines."
422 (if (< (point)
423 (save-excursion
424 (goto-char (point-min))
425 (if (re-search-forward
426 (concat "^" (regexp-quote mail-header-separator) "$")
427 nil t)
428 (point)
429 0)))
430 (let ((old-line-start (save-excursion (beginning-of-line) (point))))
431 (if (do-auto-fill)
432 (save-excursion
433 (beginning-of-line)
434 (while (not (eq (point) old-line-start))
435 ;; Use insert-before-markers in case we're inserting
436 ;; before the saved value of point (which is common).
437 (insert-before-markers " ")
438 (forward-line -1))
439 t)))
440 (do-auto-fill)))
441
442 (defun mail-mode-fill-paragraph (arg)
443 ;; Do something special only if within the headers.
444 (if (< (point)
445 (save-excursion
446 (goto-char (point-min))
447 (if (re-search-forward
448 (concat "^" (regexp-quote mail-header-separator) "$")
449 nil t)
450 (point)
451 0)))
452 (let (beg end fieldname)
453 (re-search-backward "^[-a-zA-Z]+:" nil 'yes)
454 (setq beg (point))
455 (setq fieldname
456 (downcase (buffer-substring beg (1- (match-end 0)))))
457 (forward-line 1)
458 ;; Find continuation lines and get rid of their continuation markers.
459 (while (looking-at "[ \t]")
460 (delete-horizontal-space)
461 (forward-line 1))
462 (setq end (point-marker))
463 (goto-char beg)
464 ;; If this field contains addresses,
465 ;; make sure we can fill after each address.
466 (if (member fieldname
467 '("to" "cc" "bcc" "from" "reply-to"
468 "resent-to" "resent-cc" "resent-bcc"
469 "resent-from" "resent-reply-to"))
470 (while (search-forward "," end t)
471 (or (looking-at "[ \t]")
472 (insert " "))))
473 (fill-region-as-paragraph beg end)
474 ;; Mark all lines except the first as continuations.
475 (goto-char beg)
476 (forward-line 1)
477 (while (< (point) end)
478 (insert " ")
479 (forward-line 1))
480 (move-marker end nil)
481 t)))
482 \f
483 ;;; Set up keymap.
484
485 (if mail-mode-map
486 nil
487 (setq mail-mode-map (nconc (make-sparse-keymap) text-mode-map))
488 (define-key mail-mode-map "\M-\t" 'mail-complete)
489 (define-key mail-mode-map "\C-c?" 'describe-mode)
490 (define-key mail-mode-map "\C-c\C-f\C-t" 'mail-to)
491 (define-key mail-mode-map "\C-c\C-f\C-b" 'mail-bcc)
492 (define-key mail-mode-map "\C-c\C-f\C-f" 'mail-fcc)
493 (define-key mail-mode-map "\C-c\C-f\C-c" 'mail-cc)
494 (define-key mail-mode-map "\C-c\C-f\C-s" 'mail-subject)
495 (define-key mail-mode-map "\C-c\C-f\C-r" 'mail-reply-to)
496 (define-key mail-mode-map "\C-c\C-t" 'mail-text)
497 (define-key mail-mode-map "\C-c\C-y" 'mail-yank-original)
498 (define-key mail-mode-map "\C-c\C-r" 'mail-yank-region)
499 (define-key mail-mode-map "\C-c\C-q" 'mail-fill-yanked-message)
500 (define-key mail-mode-map "\C-c\C-w" 'mail-signature)
501 (define-key mail-mode-map "\C-c\C-v" 'mail-sent-via)
502 (define-key mail-mode-map "\C-c\C-c" 'mail-send-and-exit)
503 (define-key mail-mode-map "\C-c\C-s" 'mail-send)
504 (define-key mail-mode-map "\C-c\C-i" 'mail-attach-file))
505
506 (define-key mail-mode-map [menu-bar mail]
507 (cons "Mail" (make-sparse-keymap "Mail")))
508
509 (define-key mail-mode-map [menu-bar mail fill]
510 '("Fill Citation" . mail-fill-yanked-message))
511
512 (define-key mail-mode-map [menu-bar mail yank]
513 '("Cite Original" . mail-yank-original))
514
515 (define-key mail-mode-map [menu-bar mail signature]
516 '("Insert Signature" . mail-signature))
517
518 (define-key mail-mode-map [menu-bar mail mail-sep]
519 '("--"))
520
521 (define-key mail-mode-map [menu-bar mail cancel]
522 '("Cancel" . mail-dont-send))
523
524 (define-key mail-mode-map [menu-bar mail send-stay]
525 '("Send, Keep Editing" . mail-send))
526
527 (define-key mail-mode-map [menu-bar mail send]
528 '("Send Message" . mail-send-and-exit))
529
530 (define-key mail-mode-map [menu-bar headers]
531 (cons "Headers" (make-sparse-keymap "Move to Header")))
532
533 (define-key mail-mode-map [menu-bar headers text]
534 '("Text" . mail-text))
535
536 (define-key mail-mode-map [menu-bar headers expand-aliases]
537 '("Expand Aliases" . expand-mail-aliases))
538
539 (define-key mail-mode-map [menu-bar headers sent-via]
540 '("Sent Via" . mail-sent-via))
541
542 (define-key mail-mode-map [menu-bar headers reply-to]
543 '("Reply-To" . mail-reply-to))
544
545 (define-key mail-mode-map [menu-bar headers bcc]
546 '("Bcc" . mail-bcc))
547
548 (define-key mail-mode-map [menu-bar headers fcc]
549 '("Fcc" . mail-fcc))
550
551 (define-key mail-mode-map [menu-bar headers cc]
552 '("Cc" . mail-cc))
553
554 (define-key mail-mode-map [menu-bar headers subject]
555 '("Subject" . mail-subject))
556
557 (define-key mail-mode-map [menu-bar headers to]
558 '("To" . mail-to))
559 \f
560 ;; User-level commands for sending.
561
562 (defun mail-send-and-exit (arg)
563 "Send message like `mail-send', then, if no errors, exit from mail buffer.
564 Prefix arg means don't delete this window."
565 (interactive "P")
566 (mail-send)
567 (mail-bury arg))
568
569 (defun mail-dont-send (arg)
570 "Don't send the message you have been editing.
571 Prefix arg means don't delete this window."
572 (interactive "P")
573 (mail-bury arg))
574
575 (defun mail-bury (arg)
576 "Bury this mail buffer."
577 (let ((newbuf (other-buffer (current-buffer))))
578 (bury-buffer (current-buffer))
579 (if (and (or (window-dedicated-p (frame-selected-window))
580 (cdr (assq 'mail-dedicated-frame (frame-parameters))))
581 (not (null (delq (selected-frame) (visible-frame-list)))))
582 (delete-frame (selected-frame))
583 (let (rmail-flag summary-buffer)
584 (and (not arg)
585 (not (one-window-p))
586 (save-excursion
587 (set-buffer (window-buffer (next-window (selected-window) 'not)))
588 (setq rmail-flag (eq major-mode 'rmail-mode))
589 (setq summary-buffer
590 (and mail-bury-selects-summary
591 (boundp 'rmail-summary-buffer)
592 rmail-summary-buffer
593 (buffer-name rmail-summary-buffer)
594 (not (get-buffer-window rmail-summary-buffer))
595 rmail-summary-buffer))))
596 (if rmail-flag
597 ;; If the Rmail buffer has a summary, show that.
598 (if summary-buffer (switch-to-buffer summary-buffer)
599 (delete-window))
600 (switch-to-buffer newbuf))))))
601
602 (defun mail-send ()
603 "Send the message in the current buffer.
604 If `mail-interactive' is non-nil, wait for success indication
605 or error messages, and inform user.
606 Otherwise any failure is reported in a message back to
607 the user from the mailer."
608 (interactive)
609 (if (if buffer-file-name
610 (y-or-n-p "Send buffer contents as mail message? ")
611 (or (buffer-modified-p)
612 (y-or-n-p "Message already sent; resend? ")))
613 (let ((inhibit-read-only t)
614 (opoint (point)))
615 (when (and enable-multibyte-characters
616 (not (eq mail-send-nonascii t)))
617 (goto-char (point-min))
618 (skip-chars-forward "\0-\177")
619 (or (= (point) (point-max))
620 (if (eq mail-send-nonascii 'query)
621 (or (y-or-n-p "Message contains non-ASCII characters; send anyway? ")
622 (error "Aborted"))
623 (error "Message contains non-ASCII characters"))))
624 ;; Complain about any invalid line.
625 (goto-char (point-min))
626 (while (not (looking-at (regexp-quote mail-header-separator)))
627 (unless (looking-at "[ \t]\\|.*:\\|$")
628 (push-mark opoint)
629 (error "Invalid header line (maybe a continuation line lacks initial whitespace)"))
630 (forward-line 1))
631 (goto-char opoint)
632 (run-hooks 'mail-send-hook)
633 (message "Sending...")
634 (funcall send-mail-function)
635 ;; Now perform actions on successful sending.
636 (while mail-send-actions
637 (condition-case nil
638 (apply (car (car mail-send-actions))
639 (cdr (car mail-send-actions)))
640 (error))
641 (setq mail-send-actions (cdr mail-send-actions)))
642 (message "Sending...done")
643 ;; If buffer has no file, mark it as unmodified and delete autosave.
644 (if (not buffer-file-name)
645 (progn
646 (set-buffer-modified-p nil)
647 (delete-auto-save-file-if-necessary t))))))
648 \f
649 ;; This does the real work of sending a message via sendmail.
650 ;; It is called via the variable send-mail-function.
651
652 ;;;###autoload
653 (defvar sendmail-coding-system nil
654 "Coding system to encode the outgoing mail.")
655
656 (defun sendmail-send-it ()
657 (require 'mail-utils)
658 (let ((errbuf (if mail-interactive
659 (generate-new-buffer " sendmail errors")
660 0))
661 (tembuf (generate-new-buffer " sendmail temp"))
662 (case-fold-search nil)
663 resend-to-addresses
664 delimline
665 fcc-was-found
666 (mailbuf (current-buffer))
667 (sendmail-coding-system
668 (if (local-variable-p 'buffer-file-coding-system)
669 buffer-file-coding-system
670 (or sendmail-coding-system
671 default-buffer-file-coding-system
672 'iso-latin-1))))
673 (if (fboundp select-safe-coding-system-function)
674 (setq sendmail-coding-system
675 (funcall select-safe-coding-system-function
676 (point-min) (point-max) sendmail-coding-system)))
677 (unwind-protect
678 (save-excursion
679 (set-buffer tembuf)
680 (erase-buffer)
681 (insert-buffer-substring mailbuf)
682 (goto-char (point-max))
683 ;; require one newline at the end.
684 (or (= (preceding-char) ?\n)
685 (insert ?\n))
686 ;; Change header-delimiter to be what sendmail expects.
687 (goto-char (point-min))
688 (re-search-forward
689 (concat "^" (regexp-quote mail-header-separator) "\n"))
690 (replace-match "\n")
691 (backward-char 1)
692 (setq delimline (point-marker))
693 (sendmail-sync-aliases)
694 (if mail-aliases
695 (expand-mail-aliases (point-min) delimline))
696 (goto-char (point-min))
697 ;; Ignore any blank lines in the header
698 (while (and (re-search-forward "\n\n\n*" delimline t)
699 (< (point) delimline))
700 (replace-match "\n"))
701 (goto-char (point-min))
702 (let ((case-fold-search t))
703 (goto-char (point-min))
704 (while (re-search-forward "^Resent-\\(to\\|cc\\|bcc\\):" delimline t)
705 (setq resend-to-addresses
706 (save-restriction
707 (narrow-to-region (point)
708 (save-excursion
709 (forward-line 1)
710 (while (looking-at "^[ \t]")
711 (forward-line 1))
712 (point)))
713 (append (mail-parse-comma-list)
714 resend-to-addresses)))
715 ;; Delete Resent-BCC ourselves
716 (if (save-excursion (beginning-of-line)
717 (looking-at "resent-bcc"))
718 (delete-region (save-excursion (beginning-of-line) (point))
719 (save-excursion (end-of-line) (1+ (point))))))
720 ;;; Apparently this causes a duplicate Sender.
721 ;;; ;; If the From is different than current user, insert Sender.
722 ;;; (goto-char (point-min))
723 ;;; (and (re-search-forward "^From:" delimline t)
724 ;;; (progn
725 ;;; (require 'mail-utils)
726 ;;; (not (string-equal
727 ;;; (mail-strip-quoted-names
728 ;;; (save-restriction
729 ;;; (narrow-to-region (point-min) delimline)
730 ;;; (mail-fetch-field "From")))
731 ;;; (user-login-name))))
732 ;;; (progn
733 ;;; (forward-line 1)
734 ;;; (insert "Sender: " (user-login-name) "\n")))
735 ;; Don't send out a blank subject line
736 (goto-char (point-min))
737 (if (re-search-forward "^Subject:\\([ \t]*\n\\)+\\b" delimline t)
738 (replace-match "")
739 ;; This one matches a Subject just before the header delimiter.
740 (if (and (re-search-forward "^Subject:\\([ \t]*\n\\)+" delimline t)
741 (= (match-end 0) delimline))
742 (replace-match "")))
743 ;; Put the "From:" field in unless for some odd reason
744 ;; they put one in themselves.
745 (goto-char (point-min))
746 (if (not (re-search-forward "^From:" delimline t))
747 (let* ((login user-mail-address)
748 (fullname (user-full-name))
749 (quote-fullname nil))
750 (if (string-match "[\200-\377]" fullname)
751 (setq fullname (mail-quote-printable fullname t)
752 quote-fullname t))
753 (cond ((eq mail-from-style 'angles)
754 (insert "From: " fullname)
755 (let ((fullname-start (+ (point-min) 6))
756 (fullname-end (point-marker)))
757 (goto-char fullname-start)
758 ;; Look for a character that cannot appear unquoted
759 ;; according to RFC 822.
760 (if (or (re-search-forward "[^- !#-'*+/-9=?A-Z^-~]"
761 fullname-end 1)
762 quote-fullname)
763 (progn
764 ;; Quote fullname, escaping specials.
765 (goto-char fullname-start)
766 (insert "\"")
767 (while (re-search-forward "[\"\\]"
768 fullname-end 1)
769 (replace-match "\\\\\\&" t))
770 (insert "\""))))
771 (insert " <" login ">\n"))
772 ((eq mail-from-style 'parens)
773 (insert "From: " login " (")
774 (let ((fullname-start (point)))
775 (if quote-fullname
776 (insert "\""))
777 (insert fullname)
778 (if quote-fullname
779 (insert "\""))
780 (let ((fullname-end (point-marker)))
781 (goto-char fullname-start)
782 ;; RFC 822 says \ and nonmatching parentheses
783 ;; must be escaped in comments.
784 ;; Escape every instance of ()\ ...
785 (while (re-search-forward "[()\\]" fullname-end 1)
786 (replace-match "\\\\\\&" t))
787 ;; ... then undo escaping of matching parentheses,
788 ;; including matching nested parentheses.
789 (goto-char fullname-start)
790 (while (re-search-forward
791 "\\(\\=\\|[^\\]\\(\\\\\\\\\\)*\\)\\\\(\\(\\([^\\]\\|\\\\\\\\\\)*\\)\\\\)"
792 fullname-end 1)
793 (replace-match "\\1(\\3)" t)
794 (goto-char fullname-start))))
795 (insert ")\n"))
796 ((null mail-from-style)
797 (insert "From: " login "\n")))))
798 ;; Insert an extra newline if we need it to work around
799 ;; Sun's bug that swallows newlines.
800 (goto-char (1+ delimline))
801 (if (eval mail-mailer-swallows-blank-line)
802 (newline))
803 ;; Find and handle any FCC fields.
804 (goto-char (point-min))
805 (if (re-search-forward "^FCC:" delimline t)
806 (progn
807 (setq fcc-was-found t)
808 (mail-do-fcc delimline)))
809 (if mail-interactive
810 (save-excursion
811 (set-buffer errbuf)
812 (erase-buffer))))
813 (goto-char (point-min))
814 (if (let ((case-fold-search t))
815 (re-search-forward "^To:\\|^cc:\\|^bcc:\\|^resent-to:\
816 \\|^resent-cc:\\|^resent-bcc:"
817 delimline t))
818 (let ((default-directory "/")
819 (coding-system-for-write sendmail-coding-system))
820 (apply 'call-process-region
821 (append (list (point-min) (point-max)
822 (if (boundp 'sendmail-program)
823 sendmail-program
824 "/usr/lib/sendmail")
825 nil errbuf nil "-oi")
826 ;; Always specify who from,
827 ;; since some systems have broken sendmails.
828 (list "-f" (user-login-name))
829 ;;; ;; Don't say "from root" if running under su.
830 ;;; (and (equal (user-real-login-name) "root")
831 ;;; (list "-f" (user-login-name)))
832 (and mail-alias-file
833 (list (concat "-oA" mail-alias-file)))
834 (if mail-interactive
835 ;; These mean "report errors to terminal"
836 ;; and "deliver interactively"
837 '("-oep" "-odi")
838 ;; These mean "report errors by mail"
839 ;; and "deliver in background".
840 '("-oem" "-odb"))
841 ;; Get the addresses from the message
842 ;; unless this is a resend.
843 ;; We must not do that for a resend
844 ;; because we would find the original addresses.
845 ;; For a resend, include the specific addresses.
846 (or resend-to-addresses
847 '("-t")))))
848 (or fcc-was-found
849 (error "No recipients")))
850 (if mail-interactive
851 (save-excursion
852 (set-buffer errbuf)
853 (goto-char (point-min))
854 (while (re-search-forward "\n\n* *" nil t)
855 (replace-match "; "))
856 (if (not (zerop (buffer-size)))
857 (error "Sending...failed to %s"
858 (buffer-substring (point-min) (point-max)))))))
859 (kill-buffer tembuf)
860 (if (bufferp errbuf)
861 (kill-buffer errbuf)))))
862
863 (defun mail-do-fcc (header-end)
864 (let (fcc-list
865 (rmailbuf (current-buffer))
866 (time (current-time))
867 (tembuf (generate-new-buffer " rmail output"))
868 (case-fold-search t))
869 (save-excursion
870 (goto-char (point-min))
871 (while (re-search-forward "^FCC:[ \t]*" header-end t)
872 (setq fcc-list (cons (buffer-substring (point)
873 (progn
874 (end-of-line)
875 (skip-chars-backward " \t")
876 (point)))
877 fcc-list))
878 (delete-region (match-beginning 0)
879 (progn (forward-line 1) (point))))
880 (set-buffer tembuf)
881 (erase-buffer)
882 ;; This initial newline is written out if the fcc file already exists.
883 (insert "\nFrom " (user-login-name) " "
884 (current-time-string time) "\n")
885 ;; Insert the time zone before the year.
886 (forward-char -1)
887 (forward-word -1)
888 (require 'mail-utils)
889 (insert (mail-rfc822-time-zone time) " ")
890 (goto-char (point-max))
891 (insert-buffer-substring rmailbuf)
892 ;; Make sure messages are separated.
893 (goto-char (point-max))
894 (insert ?\n)
895 (goto-char 2)
896 ;; ``Quote'' "^From " as ">From "
897 ;; (note that this isn't really quoting, as there is no requirement
898 ;; that "^[>]+From " be quoted in the same transparent way.)
899 (let ((case-fold-search nil))
900 (while (search-forward "\nFrom " nil t)
901 (forward-char -5)
902 (insert ?>)))
903 (while fcc-list
904 (let* ((buffer (find-buffer-visiting (car fcc-list)))
905 (curbuf (current-buffer))
906 dont-write-the-file
907 buffer-matches-file
908 (beg (point-min)) (end (point-max))
909 (beg2 (save-excursion (goto-char (point-min))
910 (forward-line 2) (point))))
911 (if buffer
912 ;; File is present in a buffer => append to that buffer.
913 (save-excursion
914 (set-buffer buffer)
915 (setq buffer-matches-file
916 (and (not (buffer-modified-p))
917 (verify-visited-file-modtime buffer)))
918 ;; Keep the end of the accessible portion at the same place
919 ;; unless it is the end of the buffer.
920 (let ((max (if (/= (1+ (buffer-size)) (point-max))
921 (point-max))))
922 (unwind-protect
923 ;; Code below lifted from rmailout.el
924 ;; function rmail-output-to-rmail-file:
925 (let ((buffer-read-only nil)
926 (msg (and (boundp 'rmail-current-message)
927 rmail-current-message)))
928 ;; If MSG is non-nil, buffer is in RMAIL mode.
929 (if msg
930 (progn
931 (rmail-maybe-set-message-counters)
932 (widen)
933 (narrow-to-region (point-max) (point-max))
934 (insert "\C-l\n0, unseen,,\n*** EOOH ***\n"
935 "Date: " (mail-rfc822-date) "\n")
936 (insert-buffer-substring curbuf beg2 end)
937 (insert "\n\C-_")
938 (goto-char (point-min))
939 (widen)
940 (search-backward "\n\^_")
941 (narrow-to-region (point) (point-max))
942 (rmail-count-new-messages t)
943 (rmail-show-message msg)
944 (setq max nil))
945 ;; Output file not in rmail mode
946 ;; => just insert at the end.
947 (narrow-to-region (point-min) (1+ (buffer-size)))
948 (goto-char (point-max))
949 (insert-buffer-substring curbuf beg end))
950 (or buffer-matches-file
951 (progn
952 (if (y-or-n-p (format "Save file %s? "
953 (car fcc-list)))
954 (save-buffer))
955 (setq dont-write-the-file t))))
956 (if max (narrow-to-region (point-min) max))))))
957 ;; Append to the file directly,
958 ;; unless we've already taken care of it.
959 (unless dont-write-the-file
960 (if (and (file-exists-p (car fcc-list))
961 (mail-file-babyl-p (car fcc-list)))
962 ;; If the file is a Babyl file,
963 ;; convert the message to Babyl format.
964 (let ((coding-system-for-write
965 (or rmail-file-coding-system
966 'emacs-mule)))
967 (save-excursion
968 (set-buffer (get-buffer-create " mail-temp"))
969 (setq buffer-read-only nil)
970 (erase-buffer)
971 (insert "\C-l\n0, unseen,,\n*** EOOH ***\n"
972 "Date: " (mail-rfc822-date) "\n")
973 (insert-buffer-substring curbuf beg2 end)
974 (insert "\n\C-_")
975 (write-region (point-min) (point-max) (car fcc-list) t)
976 (erase-buffer)))
977 (write-region
978 (1+ (point-min)) (point-max) (car fcc-list) t)))
979 (and buffer (not dont-write-the-file)
980 (with-current-buffer buffer
981 (set-visited-file-modtime))))
982 (setq fcc-list (cdr fcc-list))))
983 (kill-buffer tembuf)))
984
985 (defun mail-sent-via ()
986 "Make a Sent-via header line from each To or CC header line."
987 (interactive)
988 (save-excursion
989 (goto-char (point-min))
990 ;; find the header-separator
991 (search-forward (concat "\n" mail-header-separator "\n"))
992 (forward-line -1)
993 ;; put a marker at the end of the header
994 (let ((end (point-marker))
995 (case-fold-search t)
996 to-line)
997 (goto-char (point-min))
998 ;; search for the To: lines and make Sent-via: lines from them
999 ;; search for the next To: line
1000 (while (re-search-forward "^\\(to\\|cc\\):" end t)
1001 ;; Grab this line plus all its continuations, sans the `to:'.
1002 (let ((to-line
1003 (buffer-substring (point)
1004 (progn
1005 (if (re-search-forward "^[^ \t\n]" end t)
1006 (backward-char 1)
1007 (goto-char end))
1008 (point)))))
1009 ;; Insert a copy, with altered header field name.
1010 (insert-before-markers "Sent-via:" to-line))))))
1011 \f
1012 (defun mail-to ()
1013 "Move point to end of To-field."
1014 (interactive)
1015 (expand-abbrev)
1016 (mail-position-on-field "To"))
1017
1018 (defun mail-subject ()
1019 "Move point to end of Subject-field."
1020 (interactive)
1021 (expand-abbrev)
1022 (mail-position-on-field "Subject"))
1023
1024 (defun mail-cc ()
1025 "Move point to end of CC-field. Create a CC field if none."
1026 (interactive)
1027 (expand-abbrev)
1028 (or (mail-position-on-field "cc" t)
1029 (progn (mail-position-on-field "to")
1030 (insert "\nCC: "))))
1031
1032 (defun mail-bcc ()
1033 "Move point to end of BCC-field. Create a BCC field if none."
1034 (interactive)
1035 (expand-abbrev)
1036 (or (mail-position-on-field "bcc" t)
1037 (progn (mail-position-on-field "to")
1038 (insert "\nBCC: "))))
1039
1040 (defun mail-fcc (folder)
1041 "Add a new FCC field, with file name completion."
1042 (interactive "FFolder carbon copy: ")
1043 (expand-abbrev)
1044 (or (mail-position-on-field "fcc" t) ;Put new field after exiting FCC.
1045 (mail-position-on-field "to"))
1046 (insert "\nFCC: " folder))
1047
1048 (defun mail-reply-to ()
1049 "Move point to end of Reply-To-field."
1050 (interactive)
1051 (expand-abbrev)
1052 (mail-position-on-field "Reply-To"))
1053
1054 (defun mail-position-on-field (field &optional soft)
1055 (let (end
1056 (case-fold-search t))
1057 (goto-char (point-min))
1058 (re-search-forward (concat "^" (regexp-quote mail-header-separator) "$"))
1059 (setq end (match-beginning 0))
1060 (goto-char (point-min))
1061 (if (re-search-forward (concat "^" (regexp-quote field) ":") end t)
1062 (progn
1063 (re-search-forward "^[^ \t]" nil 'move)
1064 (beginning-of-line)
1065 (skip-chars-backward "\n")
1066 t)
1067 (or soft
1068 (progn (goto-char end)
1069 (insert field ": \n")
1070 (skip-chars-backward "\n")))
1071 nil)))
1072
1073 (defun mail-text ()
1074 "Move point to beginning of message text."
1075 (interactive)
1076 (expand-abbrev)
1077 (goto-char (point-min))
1078 (search-forward (concat "\n" mail-header-separator "\n")))
1079 \f
1080 (defun mail-signature (atpoint)
1081 "Sign letter with contents of the file `mail-signature-file'.
1082 Prefix arg means put contents at point."
1083 (interactive "P")
1084 (save-excursion
1085 (or atpoint
1086 (goto-char (point-max)))
1087 (skip-chars-backward " \t\n")
1088 (end-of-line)
1089 (or atpoint
1090 (delete-region (point) (point-max)))
1091 (insert "\n\n-- \n")
1092 (insert-file-contents (expand-file-name mail-signature-file))))
1093
1094 (defun mail-fill-yanked-message (&optional justifyp)
1095 "Fill the paragraphs of a message yanked into this one.
1096 Numeric argument means justify as well."
1097 (interactive "P")
1098 (save-excursion
1099 (goto-char (point-min))
1100 (search-forward (concat "\n" mail-header-separator "\n") nil t)
1101 (fill-individual-paragraphs (point)
1102 (point-max)
1103 justifyp
1104 t)))
1105
1106 (defun mail-indent-citation ()
1107 "Modify text just inserted from a message to be cited.
1108 The inserted text should be the region.
1109 When this function returns, the region is again around the modified text.
1110
1111 Normally, indent each nonblank line `mail-indentation-spaces' spaces.
1112 However, if `mail-yank-prefix' is non-nil, insert that prefix on each line."
1113 (mail-yank-clear-headers (region-beginning) (region-end))
1114 (if (null mail-yank-prefix)
1115 (indent-rigidly (region-beginning) (region-end)
1116 mail-indentation-spaces)
1117 (save-excursion
1118 (let ((end (set-marker (make-marker) (region-end))))
1119 (goto-char (region-beginning))
1120 (while (< (point) end)
1121 (insert mail-yank-prefix)
1122 (forward-line 1))))))
1123
1124 (defun mail-yank-original (arg)
1125 "Insert the message being replied to, if any (in rmail).
1126 Puts point after the text and mark before.
1127 Normally, indents each nonblank line ARG spaces (default 3).
1128 However, if `mail-yank-prefix' is non-nil, insert that prefix on each line.
1129
1130 Just \\[universal-argument] as argument means don't indent, insert no prefix,
1131 and don't delete any header fields."
1132 (interactive "P")
1133 (if mail-reply-action
1134 (let ((start (point))
1135 (original mail-reply-action))
1136 (and (consp original) (eq (car original) 'insert-buffer)
1137 (setq original (nth 1 original)))
1138 (if (consp original)
1139 (apply (car original) (cdr original))
1140 ;; If the original message is in another window in the same frame,
1141 ;; delete that window to save screen space.
1142 ;; t means don't alter other frames.
1143 (delete-windows-on original t)
1144 (insert-buffer original))
1145 (if (consp arg)
1146 nil
1147 (goto-char start)
1148 (let ((mail-indentation-spaces (if arg (prefix-numeric-value arg)
1149 mail-indentation-spaces))
1150 (mark-even-if-inactive t))
1151 (if mail-citation-hook
1152 (run-hooks 'mail-citation-hook)
1153 (if mail-yank-hooks
1154 (run-hooks 'mail-yank-hooks)
1155 (mail-indent-citation)))))
1156 ;; This is like exchange-point-and-mark, but doesn't activate the mark.
1157 ;; It is cleaner to avoid activation, even though the command
1158 ;; loop would deactivate the mark because we inserted text.
1159 (goto-char (prog1 (mark t)
1160 (set-marker (mark-marker) (point) (current-buffer))))
1161 (if (not (eolp)) (insert ?\n)))))
1162
1163 (defun mail-yank-clear-headers (start end)
1164 (if (< end start)
1165 (let (temp)
1166 (setq temp start start end end temp)))
1167 (if mail-yank-ignored-headers
1168 (save-excursion
1169 (goto-char start)
1170 (if (search-forward "\n\n" end t)
1171 (save-restriction
1172 (narrow-to-region start (point))
1173 (goto-char start)
1174 (while (let ((case-fold-search t))
1175 (re-search-forward mail-yank-ignored-headers nil t))
1176 (beginning-of-line)
1177 (delete-region (point)
1178 (progn (re-search-forward "\n[^ \t]")
1179 (forward-char -1)
1180 (point)))))))))
1181
1182 (defun mail-yank-region (arg)
1183 "Insert the selected region from the message being replied to.
1184 Puts point after the text and mark before.
1185 Normally, indents each nonblank line ARG spaces (default 3).
1186 However, if `mail-yank-prefix' is non-nil, insert that prefix on each line.
1187
1188 Just \\[universal-argument] as argument means don't indent, insert no prefix,
1189 and don't delete any header fields."
1190 (interactive "P")
1191 (and (consp mail-reply-action)
1192 (eq (car mail-reply-action) 'insert-buffer)
1193 (let ((buffer (nth 1 mail-reply-action))
1194 (start (point)))
1195 ;; Insert the citation text.
1196 (insert (with-current-buffer buffer
1197 (buffer-substring (point) (mark))))
1198 (push-mark start)
1199 ;; Indent or otherwise annotate the citation text.
1200 (if (consp arg)
1201 nil
1202 (let ((mail-indentation-spaces (if arg (prefix-numeric-value arg)
1203 mail-indentation-spaces)))
1204 (if mail-citation-hook
1205 (run-hooks 'mail-citation-hook)
1206 (if mail-yank-hooks
1207 (run-hooks 'mail-yank-hooks)
1208 (mail-indent-citation))))))))
1209 \f
1210 (defun mail-attach-file (&optional file)
1211 "Insert a file at the end of the buffer, with separator lines around it."
1212 (interactive "fAttach file: ")
1213 (save-excursion
1214 (goto-char (point-max))
1215 (or (bolp) (newline))
1216 (newline)
1217 (let ((start (point))
1218 middle)
1219 (insert (format "===File %s===" file))
1220 (insert-char ?= (max 0 (- 60 (current-column))))
1221 (newline)
1222 (setq middle (point))
1223 (insert "============================================================\n")
1224 (push-mark)
1225 (goto-char middle)
1226 (insert-file-contents file)
1227 (or (bolp) (newline))
1228 (goto-char start))))
1229 \f
1230 ;; Put these commands last, to reduce chance of lossage from quitting
1231 ;; in middle of loading the file.
1232
1233 ;;;###autoload (add-hook 'same-window-buffer-names "*mail*")
1234
1235 ;;;###autoload
1236 (defun mail (&optional noerase to subject in-reply-to cc replybuffer actions)
1237 "Edit a message to be sent. Prefix arg means resume editing (don't erase).
1238 When this function returns, the buffer `*mail*' is selected.
1239 The value is t if the message was newly initialized; otherwise, nil.
1240
1241 Optionally, the signature file `mail-signature-file' can be inserted at the
1242 end; see the variable `mail-signature'.
1243
1244 \\<mail-mode-map>
1245 While editing message, type \\[mail-send-and-exit] to send the message and exit.
1246
1247 Various special commands starting with C-c are available in sendmail mode
1248 to move to message header fields:
1249 \\{mail-mode-map}
1250
1251 If `mail-self-blind' is non-nil, a BCC to yourself is inserted
1252 when the message is initialized.
1253
1254 If `mail-default-reply-to' is non-nil, it should be an address (a string);
1255 a Reply-to: field with that address is inserted.
1256
1257 If `mail-archive-file-name' is non-nil, an FCC field with that file name
1258 is inserted.
1259
1260 The normal hook `mail-setup-hook' is run after the message is
1261 initialized. It can add more default fields to the message.
1262
1263 When calling from a program, the first argument if non-nil says
1264 not to erase the existing contents of the `*mail*' buffer.
1265
1266 The second through fifth arguments,
1267 TO, SUBJECT, IN-REPLY-TO and CC, specify if non-nil
1268 the initial contents of those header fields.
1269 These arguments should not have final newlines.
1270 The sixth argument REPLYBUFFER is a buffer which contains an
1271 original message being replied to, or else an action
1272 of the form (FUNCTION . ARGS) which says how to insert the original.
1273 Or it can be nil, if not replying to anything.
1274 The seventh argument ACTIONS is a list of actions to take
1275 if/when the message is sent. Each action looks like (FUNCTION . ARGS);
1276 when the message is sent, we apply FUNCTION to ARGS.
1277 This is how Rmail arranges to mark messages `answered'."
1278 (interactive "P")
1279 ;;; This is commented out because I found it was confusing in practice.
1280 ;;; It is easy enough to rename *mail* by hand with rename-buffer
1281 ;;; if you want to have multiple mail buffers.
1282 ;;; And then you can control which messages to save. --rms.
1283 ;;; (let ((index 1)
1284 ;;; buffer)
1285 ;;; ;; If requested, look for a mail buffer that is modified and go to it.
1286 ;;; (if noerase
1287 ;;; (progn
1288 ;;; (while (and (setq buffer
1289 ;;; (get-buffer (if (= 1 index) "*mail*"
1290 ;;; (format "*mail*<%d>" index))))
1291 ;;; (not (buffer-modified-p buffer)))
1292 ;;; (setq index (1+ index)))
1293 ;;; (if buffer (switch-to-buffer buffer)
1294 ;;; ;; If none exists, start a new message.
1295 ;;; ;; This will never re-use an existing unmodified mail buffer
1296 ;;; ;; (since index is not 1 anymore). Perhaps it should.
1297 ;;; (setq noerase nil))))
1298 ;;; ;; Unless we found a modified message and are happy, start a new message.
1299 ;;; (if (not noerase)
1300 ;;; (progn
1301 ;;; ;; Look for existing unmodified mail buffer.
1302 ;;; (while (and (setq buffer
1303 ;;; (get-buffer (if (= 1 index) "*mail*"
1304 ;;; (format "*mail*<%d>" index))))
1305 ;;; (buffer-modified-p buffer))
1306 ;;; (setq index (1+ index)))
1307 ;;; ;; If none, make a new one.
1308 ;;; (or buffer
1309 ;;; (setq buffer (generate-new-buffer "*mail*")))
1310 ;;; ;; Go there and initialize it.
1311 ;;; (switch-to-buffer buffer)
1312 ;;; (erase-buffer)
1313 ;;; (setq default-directory (expand-file-name "~/"))
1314 ;;; (auto-save-mode auto-save-default)
1315 ;;; (mail-mode)
1316 ;;; (mail-setup to subject in-reply-to cc replybuffer actions)
1317 ;;; (if (and buffer-auto-save-file-name
1318 ;;; (file-exists-p buffer-auto-save-file-name))
1319 ;;; (message "Auto save file for draft message exists; consider M-x mail-recover"))
1320 ;;; t))
1321 (pop-to-buffer "*mail*")
1322 ;; Put the auto-save file in the home dir
1323 ;; to avoid any danger that it can't be written.
1324 (if (file-exists-p (expand-file-name "~/"))
1325 (setq default-directory (expand-file-name "~/")))
1326 (auto-save-mode auto-save-default)
1327 (mail-mode)
1328 ;; Disconnect the buffer from its visited file
1329 ;; (in case the user has actually visited a file *mail*).
1330 ; (set-visited-file-name nil)
1331 (let (initialized)
1332 (and (not noerase)
1333 (if buffer-file-name
1334 (if (buffer-modified-p)
1335 (when (y-or-n-p "Buffer has unsaved changes; reinitialize it and discard them? ")
1336 (if (y-or-n-p "Disconnect buffer from visited file? ")
1337 (set-visited-file-name nil))
1338 t)
1339 (when (y-or-n-p "Reinitialize buffer, and disconnect it from the visited file? ")
1340 (set-visited-file-name nil)
1341 t))
1342 ;; A non-file-visiting buffer.
1343 (if (buffer-modified-p)
1344 (y-or-n-p "Unsent message being composed; erase it? ")
1345 t))
1346 (let ((inhibit-read-only t))
1347 (erase-buffer)
1348 (mail-setup to subject in-reply-to cc replybuffer actions)
1349 (setq initialized t)))
1350 (if (and buffer-auto-save-file-name
1351 (file-exists-p buffer-auto-save-file-name))
1352 (message "Auto save file for draft message exists; consider M-x mail-recover"))
1353 initialized))
1354
1355 (defun mail-recover ()
1356 "Reread contents of current buffer from its last auto-save file."
1357 (interactive)
1358 (let ((file-name (make-auto-save-file-name)))
1359 (cond ((save-window-excursion
1360 (if (not (eq system-type 'vax-vms))
1361 (with-output-to-temp-buffer "*Directory*"
1362 (buffer-disable-undo standard-output)
1363 (let ((default-directory "/"))
1364 (call-process
1365 "ls" nil standard-output nil "-l" file-name))))
1366 (yes-or-no-p (format "Recover auto save file %s? " file-name)))
1367 (let ((buffer-read-only nil))
1368 (erase-buffer)
1369 (insert-file-contents file-name nil)))
1370 (t (error "mail-recover cancelled")))))
1371
1372 ;;;###autoload
1373 (defun mail-other-window (&optional noerase to subject in-reply-to cc replybuffer sendactions)
1374 "Like `mail' command, but display mail buffer in another window."
1375 (interactive "P")
1376 (let ((pop-up-windows t)
1377 (special-display-buffer-names nil)
1378 (special-display-regexps nil)
1379 (same-window-buffer-names nil)
1380 (same-window-regexps nil))
1381 (pop-to-buffer "*mail*"))
1382 (mail noerase to subject in-reply-to cc replybuffer sendactions))
1383
1384 ;;;###autoload
1385 (defun mail-other-frame (&optional noerase to subject in-reply-to cc replybuffer sendactions)
1386 "Like `mail' command, but display mail buffer in another frame."
1387 (interactive "P")
1388 (let ((pop-up-frames t)
1389 (special-display-buffer-names nil)
1390 (special-display-regexps nil)
1391 (same-window-buffer-names nil)
1392 (same-window-regexps nil))
1393 (pop-to-buffer "*mail*"))
1394 (mail noerase to subject in-reply-to cc replybuffer sendactions))
1395
1396 ;;; Do not add anything but external entries on this page.
1397
1398 (provide 'sendmail)
1399
1400 ;;; sendmail.el ends here