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