(mail-mode-auto-fill): Use insert-before-markers.
[bpt/emacs.git] / 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
32 ;;;###autoload
33 (defvar mail-from-style 'angles "\
34 *Specifies how \"From:\" fields look.
35
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>")
42
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.")
48
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.")
53
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.")
57
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'.")
64
65 ;;;###autoload
66 (defvar mail-header-separator "--text follows this line--" "\
67 *Line used to separate headers from text in messages being composed.")
68
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)
77
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.")
82
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.")
88
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.")
95
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.")
102
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.")
106
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")
114
115 (defvar mail-alias-modtime nil
116 "The modification time of your mail alias file when it was last examined.")
117
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.
129
130 This is a normal hook, misnamed for historical reasons.
131 It is semi-obsolete and mail agents should no longer use it.")
132
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.
138
139 If this hook is entirely empty (nil), a default action is taken
140 instead of no action.")
141
142 (defvar mail-abbrevs-loaded nil)
143 (defvar mail-mode-map nil)
144
145 (autoload 'build-mail-aliases "mailalias"
146 "Read mail aliases from user's personal aliases file and set `mail-aliases'."
147 nil)
148
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)
155
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'.")
160
161 (defvar mail-signature-file "~/.signature"
162 "*File containing the text inserted at end of mail buffer.")
163
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)
169
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.")
174
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.")
180
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.")
204
205 (defvar mail-mode-syntax-table nil
206 "Syntax table used while in mail mode.")
207
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)))
212
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.")
238
239 (defvar mail-send-hook nil
240 "Normal hook run before sending mail, in Mail mode.")
241 \f
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))))
247
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))
316 \f
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 ;; `-- ' precedes the signature. `-----' appears at the start of the
350 ;; lines that delimit forwarded messages.
351 ;; Lines containing just >= 3 dashes, perhaps after whitespace,
352 ;; are also sometimes used and should be separators.
353 (setq paragraph-start (concat (regexp-quote mail-header-separator)
354 "$\\|[ \t]*$\\|" page-delimiter))
355 (setq paragraph-separate paragraph-start)
356 (run-hooks 'text-mode-hook 'mail-mode-hook))
357
358 (defun mail-mode-auto-fill ()
359 "Carry out Auto Fill for Mail mode.
360 If within the headers, this makes the new lines into continuation lines."
361 (if (< (point)
362 (save-excursion
363 (goto-char (point-min))
364 (if (re-search-forward
365 (concat "^" (regexp-quote mail-header-separator) "$")
366 nil t)
367 (point)
368 0)))
369 (let ((old-line-start (save-excursion (beginning-of-line) (point))))
370 (if (do-auto-fill)
371 (save-excursion
372 (beginning-of-line)
373 (while (not (eq (point) old-line-start))
374 ;; Use insert-before-markers in case we're inserting
375 ;; before the saved value of point (which is common).
376 (insert-before-markers " ")
377 (forward-line -1))
378 t)))
379 (do-auto-fill)))
380
381 (defun mail-mode-fill-paragraph (arg)
382 ;; Do something special only if within the headers.
383 (if (< (point)
384 (save-excursion
385 (goto-char (point-min))
386 (if (re-search-forward
387 (concat "^" (regexp-quote mail-header-separator) "$")
388 nil t)
389 (point)
390 0)))
391 (let (beg end fieldname)
392 (re-search-backward "^[-a-zA-Z]+:" nil 'yes)
393 (setq beg (point))
394 (setq fieldname
395 (downcase (buffer-substring beg (1- (match-end 0)))))
396 (forward-line 1)
397 ;; Find continuation lines and get rid of their continuation markers.
398 (while (looking-at "[ \t]")
399 (delete-horizontal-space)
400 (forward-line 1))
401 (setq end (point-marker))
402 (goto-char beg)
403 ;; If this field contains addresses,
404 ;; make sure we can fill after each address.
405 (if (member fieldname
406 '("to" "cc" "bcc" "from" "reply-to"
407 "resent-to" "resent-cc" "resent-bcc"
408 "resent-from" "resent-reply-to"))
409 (while (search-forward "," end t)
410 (or (looking-at "[ \t]")
411 (insert " "))))
412 (fill-region-as-paragraph beg end)
413 ;; Mark all lines except the first as continuations.
414 (goto-char beg)
415 (forward-line 1)
416 (while (< (point) end)
417 (insert " ")
418 (forward-line 1))
419 (move-marker end nil)
420 t)))
421 \f
422 ;;; Set up keymap.
423
424 (if mail-mode-map
425 nil
426 (setq mail-mode-map (nconc (make-sparse-keymap) text-mode-map))
427 (define-key mail-mode-map "\M-\t" 'mail-complete)
428 (define-key mail-mode-map "\C-c?" 'describe-mode)
429 (define-key mail-mode-map "\C-c\C-f\C-t" 'mail-to)
430 (define-key mail-mode-map "\C-c\C-f\C-b" 'mail-bcc)
431 (define-key mail-mode-map "\C-c\C-f\C-f" 'mail-fcc)
432 (define-key mail-mode-map "\C-c\C-f\C-c" 'mail-cc)
433 (define-key mail-mode-map "\C-c\C-f\C-s" 'mail-subject)
434 (define-key mail-mode-map "\C-c\C-f\C-r" 'mail-reply-to)
435 (define-key mail-mode-map "\C-c\C-t" 'mail-text)
436 (define-key mail-mode-map "\C-c\C-y" 'mail-yank-original)
437 (define-key mail-mode-map "\C-c\C-r" 'mail-yank-region)
438 (define-key mail-mode-map "\C-c\C-q" 'mail-fill-yanked-message)
439 (define-key mail-mode-map "\C-c\C-w" 'mail-signature)
440 (define-key mail-mode-map "\C-c\C-v" 'mail-sent-via)
441 (define-key mail-mode-map "\C-c\C-c" 'mail-send-and-exit)
442 (define-key mail-mode-map "\C-c\C-s" 'mail-send)
443 (define-key mail-mode-map "\C-c\C-i" 'mail-attach-file))
444
445 (define-key mail-mode-map [menu-bar mail]
446 (cons "Mail" (make-sparse-keymap "Mail")))
447
448 (define-key mail-mode-map [menu-bar mail fill]
449 '("Fill Citation" . mail-fill-yanked-message))
450
451 (define-key mail-mode-map [menu-bar mail yank]
452 '("Cite Original" . mail-yank-original))
453
454 (define-key mail-mode-map [menu-bar mail signature]
455 '("Insert Signature" . mail-signature))
456
457 (define-key mail-mode-map [menu-bar mail mail-sep]
458 '("--"))
459
460 (define-key mail-mode-map [menu-bar mail cancel]
461 '("Cancel" . mail-dont-send))
462
463 (define-key mail-mode-map [menu-bar mail send-stay]
464 '("Send, Keep Editing" . mail-send))
465
466 (define-key mail-mode-map [menu-bar mail send]
467 '("Send Message" . mail-send-and-exit))
468
469 (define-key mail-mode-map [menu-bar headers]
470 (cons "Headers" (make-sparse-keymap "Move to Header")))
471
472 (define-key mail-mode-map [menu-bar headers text]
473 '("Text" . mail-text))
474
475 (define-key mail-mode-map [menu-bar headers expand-aliases]
476 '("Expand Aliases" . expand-mail-aliases))
477
478 (define-key mail-mode-map [menu-bar headers sent-via]
479 '("Sent Via" . mail-sent-via))
480
481 (define-key mail-mode-map [menu-bar headers reply-to]
482 '("Reply-To" . mail-reply-to))
483
484 (define-key mail-mode-map [menu-bar headers bcc]
485 '("Bcc" . mail-bcc))
486
487 (define-key mail-mode-map [menu-bar headers fcc]
488 '("Fcc" . mail-fcc))
489
490 (define-key mail-mode-map [menu-bar headers cc]
491 '("Cc" . mail-cc))
492
493 (define-key mail-mode-map [menu-bar headers subject]
494 '("Subject" . mail-subject))
495
496 (define-key mail-mode-map [menu-bar headers to]
497 '("To" . mail-to))
498 \f
499 ;; User-level commands for sending.
500
501 (defun mail-send-and-exit (arg)
502 "Send message like `mail-send', then, if no errors, exit from mail buffer.
503 Prefix arg means don't delete this window."
504 (interactive "P")
505 (mail-send)
506 (mail-bury arg))
507
508 (defun mail-dont-send (arg)
509 "Don't send the message you have been editing.
510 Prefix arg means don't delete this window."
511 (interactive "P")
512 (mail-bury arg))
513
514 (defun mail-bury (arg)
515 "Bury this mail buffer."
516 (let ((newbuf (other-buffer (current-buffer))))
517 (bury-buffer (current-buffer))
518 (if (and (or (window-dedicated-p (frame-selected-window))
519 (assq 'mail-dedicated-frame (frame-parameters)))
520 (not (null (delq (selected-frame) (visible-frame-list)))))
521 (delete-frame (selected-frame))
522 (let (rmail-flag summary-buffer)
523 (and (not arg)
524 (not (one-window-p))
525 (save-excursion
526 (set-buffer (window-buffer (next-window (selected-window) 'not)))
527 (setq rmail-flag (eq major-mode 'rmail-mode))
528 (setq summary-buffer
529 (and mail-bury-selects-summary
530 (boundp 'rmail-summary-buffer)
531 rmail-summary-buffer
532 (buffer-name rmail-summary-buffer)
533 (not (get-buffer-window rmail-summary-buffer))
534 rmail-summary-buffer))))
535 (if rmail-flag
536 ;; If the Rmail buffer has a summary, show that.
537 (if summary-buffer (switch-to-buffer summary-buffer)
538 (delete-window))
539 (switch-to-buffer newbuf))))))
540
541 (defun mail-send ()
542 "Send the message in the current buffer.
543 If `mail-interactive' is non-nil, wait for success indication
544 or error messages, and inform user.
545 Otherwise any failure is reported in a message back to
546 the user from the mailer."
547 (interactive)
548 (if (if buffer-file-name
549 (y-or-n-p "Send buffer contents as mail message? ")
550 (or (buffer-modified-p)
551 (y-or-n-p "Message already sent; resend? ")))
552 (let ((inhibit-read-only t))
553 (run-hooks 'mail-send-hook)
554 (message "Sending...")
555 (funcall send-mail-function)
556 ;; Now perform actions on successful sending.
557 (while mail-send-actions
558 (condition-case nil
559 (apply (car (car mail-send-actions))
560 (cdr (car mail-send-actions)))
561 (error))
562 (setq mail-send-actions (cdr mail-send-actions)))
563 (message "Sending...done")
564 ;; If buffer has no file, mark it as unmodified and delete autosave.
565 (if (not buffer-file-name)
566 (progn
567 (set-buffer-modified-p nil)
568 (delete-auto-save-file-if-necessary t))))))
569 \f
570 ;; This does the real work of sending a message via sendmail.
571 ;; It is called via the variable send-mail-function.
572
573 ;;;###autoload
574 (defvar sendmail-coding-system nil
575 "Coding system to which to encode the mail.")
576
577 (defun sendmail-send-it ()
578 (require 'mail-utils)
579 (let ((errbuf (if mail-interactive
580 (generate-new-buffer " sendmail errors")
581 0))
582 (tembuf (generate-new-buffer " sendmail temp"))
583 (case-fold-search nil)
584 resend-to-addresses
585 delimline
586 fcc-was-found
587 (mailbuf (current-buffer)))
588 (unwind-protect
589 (save-excursion
590 (set-buffer tembuf)
591 (erase-buffer)
592 (insert-buffer-substring mailbuf)
593 (goto-char (point-max))
594 ;; require one newline at the end.
595 (or (= (preceding-char) ?\n)
596 (insert ?\n))
597 ;; Change header-delimiter to be what sendmail expects.
598 (goto-char (point-min))
599 (re-search-forward
600 (concat "^" (regexp-quote mail-header-separator) "\n"))
601 (replace-match "\n")
602 (backward-char 1)
603 (setq delimline (point-marker))
604 (sendmail-sync-aliases)
605 (if mail-aliases
606 (expand-mail-aliases (point-min) delimline))
607 (goto-char (point-min))
608 ;; ignore any blank lines in the header
609 (while (and (re-search-forward "\n\n\n*" delimline t)
610 (< (point) delimline))
611 (replace-match "\n"))
612 (let ((case-fold-search t))
613 (goto-char (point-min))
614 (while (re-search-forward "^Resent-\\(to\\|cc\\|bcc\\):" delimline t)
615 (setq resend-to-addresses
616 (save-restriction
617 (narrow-to-region (point)
618 (save-excursion
619 (end-of-line)
620 (point)))
621 (append (mail-parse-comma-list)
622 resend-to-addresses)))
623 ;; Delete Resent-BCC ourselves
624 (if (save-excursion (beginning-of-line)
625 (looking-at "resent-bcc"))
626 (delete-region (save-excursion (beginning-of-line) (point))
627 (save-excursion (end-of-line) (1+ (point))))))
628 ;;; Apparently this causes a duplicate Sender.
629 ;;; ;; If the From is different than current user, insert Sender.
630 ;;; (goto-char (point-min))
631 ;;; (and (re-search-forward "^From:" delimline t)
632 ;;; (progn
633 ;;; (require 'mail-utils)
634 ;;; (not (string-equal
635 ;;; (mail-strip-quoted-names
636 ;;; (save-restriction
637 ;;; (narrow-to-region (point-min) delimline)
638 ;;; (mail-fetch-field "From")))
639 ;;; (user-login-name))))
640 ;;; (progn
641 ;;; (forward-line 1)
642 ;;; (insert "Sender: " (user-login-name) "\n")))
643 ;; Don't send out a blank subject line
644 (goto-char (point-min))
645 (if (re-search-forward "^Subject:\\([ \t]*\n\\)+\\b" delimline t)
646 (replace-match "")
647 ;; This one matches a Subject just before the header delimiter.
648 (if (and (re-search-forward "^Subject:\\([ \t]*\n\\)+" delimline t)
649 (= (match-end 0) delimline))
650 (replace-match "")))
651 ;; Put the "From:" field in unless for some odd reason
652 ;; they put one in themselves.
653 (goto-char (point-min))
654 (if (not (re-search-forward "^From:" delimline t))
655 (let* ((login user-mail-address)
656 (fullname (user-full-name))
657 (quote-fullname nil))
658 (if (string-match "[\200-\377]" fullname)
659 (setq fullname (mail-quote-printable fullname t)
660 quote-fullname t))
661 (cond ((eq mail-from-style 'angles)
662 (insert "From: " fullname)
663 (let ((fullname-start (+ (point-min) 6))
664 (fullname-end (point-marker)))
665 (goto-char fullname-start)
666 ;; Look for a character that cannot appear unquoted
667 ;; according to RFC 822.
668 (if (or (re-search-forward "[^- !#-'*+/-9=?A-Z^-~]"
669 fullname-end 1)
670 quote-fullname)
671 (progn
672 ;; Quote fullname, escaping specials.
673 (goto-char fullname-start)
674 (insert "\"")
675 (while (re-search-forward "[\"\\]"
676 fullname-end 1)
677 (replace-match "\\\\\\&" t))
678 (insert "\""))))
679 (insert " <" login ">\n"))
680 ((eq mail-from-style 'parens)
681 (insert "From: " login " (")
682 (let ((fullname-start (point)))
683 (if quote-fullname
684 (insert "\""))
685 (insert fullname)
686 (if quote-fullname
687 (insert "\""))
688 (let ((fullname-end (point-marker)))
689 (goto-char fullname-start)
690 ;; RFC 822 says \ and nonmatching parentheses
691 ;; must be escaped in comments.
692 ;; Escape every instance of ()\ ...
693 (while (re-search-forward "[()\\]" fullname-end 1)
694 (replace-match "\\\\\\&" t))
695 ;; ... then undo escaping of matching parentheses,
696 ;; including matching nested parentheses.
697 (goto-char fullname-start)
698 (while (re-search-forward
699 "\\(\\=\\|[^\\]\\(\\\\\\\\\\)*\\)\\\\(\\(\\([^\\]\\|\\\\\\\\\\)*\\)\\\\)"
700 fullname-end 1)
701 (replace-match "\\1(\\3)" t)
702 (goto-char fullname-start))))
703 (insert ")\n"))
704 ((null mail-from-style)
705 (insert "From: " login "\n")))))
706 ;; Insert an extra newline if we need it to work around
707 ;; Sun's bug that swallows newlines.
708 (goto-char (1+ delimline))
709 (if (eval mail-mailer-swallows-blank-line)
710 (newline))
711 ;; Find and handle any FCC fields.
712 (goto-char (point-min))
713 (if (re-search-forward "^FCC:" delimline t)
714 (progn
715 (setq fcc-was-found t)
716 (mail-do-fcc delimline)))
717 (if mail-interactive
718 (save-excursion
719 (set-buffer errbuf)
720 (erase-buffer))))
721 (goto-char (point-min))
722 (if (let ((case-fold-search t))
723 (re-search-forward "^To:\\|^cc:\\|^bcc:\\|^resent-to:\
724 \\|^resent-cc:\\|^resent-bcc:"
725 delimline t))
726 (let ((default-directory "/")
727 (coding-system-for-write sendmail-coding-system))
728 (apply 'call-process-region
729 (append (list (point-min) (point-max)
730 (if (boundp 'sendmail-program)
731 sendmail-program
732 "/usr/lib/sendmail")
733 nil errbuf nil "-oi")
734 ;; Always specify who from,
735 ;; since some systems have broken sendmails.
736 (list "-f" (user-login-name))
737 ;;; ;; Don't say "from root" if running under su.
738 ;;; (and (equal (user-real-login-name) "root")
739 ;;; (list "-f" (user-login-name)))
740 (and mail-alias-file
741 (list (concat "-oA" mail-alias-file)))
742 (if mail-interactive
743 ;; These mean "report errors to terminal"
744 ;; and "deliver interactively"
745 '("-oep" "-odi")
746 ;; These mean "report errors by mail"
747 ;; and "deliver in background".
748 '("-oem" "-odb"))
749 ;; Get the addresses from the message
750 ;; unless this is a resend.
751 ;; We must not do that for a resend
752 ;; because we would find the original addresses.
753 ;; For a resend, include the specific addresses.
754 (or resend-to-addresses
755 '("-t")))))
756 (or fcc-was-found
757 (error "No recipients")))
758 (if mail-interactive
759 (save-excursion
760 (set-buffer errbuf)
761 (goto-char (point-min))
762 (while (re-search-forward "\n\n* *" nil t)
763 (replace-match "; "))
764 (if (not (zerop (buffer-size)))
765 (error "Sending...failed to %s"
766 (buffer-substring (point-min) (point-max)))))))
767 (kill-buffer tembuf)
768 (if (bufferp errbuf)
769 (kill-buffer errbuf)))))
770
771 (defun mail-do-fcc (header-end)
772 (let (fcc-list
773 (rmailbuf (current-buffer))
774 (time (current-time))
775 (tembuf (generate-new-buffer " rmail output"))
776 (case-fold-search t))
777 (save-excursion
778 (goto-char (point-min))
779 (while (re-search-forward "^FCC:[ \t]*" header-end t)
780 (setq fcc-list (cons (buffer-substring (point)
781 (progn
782 (end-of-line)
783 (skip-chars-backward " \t")
784 (point)))
785 fcc-list))
786 (delete-region (match-beginning 0)
787 (progn (forward-line 1) (point))))
788 (set-buffer tembuf)
789 (erase-buffer)
790 ;; This initial newline is written out if the fcc file already exists.
791 (insert "\nFrom " (user-login-name) " "
792 (current-time-string time) "\n")
793 ;; Insert the time zone before the year.
794 (forward-char -1)
795 (forward-word -1)
796 (require 'mail-utils)
797 (insert (mail-rfc822-time-zone time) " ")
798 (goto-char (point-max))
799 (insert-buffer-substring rmailbuf)
800 ;; Make sure messages are separated.
801 (goto-char (point-max))
802 (insert ?\n)
803 (goto-char 2)
804 ;; ``Quote'' "^From " as ">From "
805 ;; (note that this isn't really quoting, as there is no requirement
806 ;; that "^[>]+From " be quoted in the same transparent way.)
807 (let ((case-fold-search nil))
808 (while (search-forward "\nFrom " nil t)
809 (forward-char -5)
810 (insert ?>)))
811 (while fcc-list
812 (let* ((buffer (find-buffer-visiting (car fcc-list)))
813 (curbuf (current-buffer))
814 dont-write-the-file
815 buffer-matches-file
816 (beg (point-min)) (end (point-max))
817 (beg2 (save-excursion (goto-char (point-min))
818 (forward-line 2) (point))))
819 (if buffer
820 ;; File is present in a buffer => append to that buffer.
821 (save-excursion
822 (set-buffer buffer)
823 (setq buffer-matches-file
824 (and (not (buffer-modified-p))
825 (verify-visited-file-modtime buffer)))
826 ;; Keep the end of the accessible portion at the same place
827 ;; unless it is the end of the buffer.
828 (let ((max (if (/= (1+ (buffer-size)) (point-max))
829 (point-max))))
830 (unwind-protect
831 ;; Code below lifted from rmailout.el
832 ;; function rmail-output-to-rmail-file:
833 (let ((buffer-read-only nil)
834 (msg (and (boundp 'rmail-current-message)
835 rmail-current-message)))
836 ;; If MSG is non-nil, buffer is in RMAIL mode.
837 (if msg
838 (progn
839 (rmail-maybe-set-message-counters)
840 (widen)
841 (narrow-to-region (point-max) (point-max))
842 (insert "\C-l\n0, unseen,,\n*** EOOH ***\n"
843 "Date: " (mail-rfc822-date) "\n")
844 (insert-buffer-substring curbuf beg2 end)
845 (insert "\n\C-_")
846 (goto-char (point-min))
847 (widen)
848 (search-backward "\n\^_")
849 (narrow-to-region (point) (point-max))
850 (rmail-count-new-messages t)
851 (rmail-show-message msg)
852 (setq max nil))
853 ;; Output file not in rmail mode
854 ;; => just insert at the end.
855 (narrow-to-region (point-min) (1+ (buffer-size)))
856 (goto-char (point-max))
857 (insert-buffer-substring curbuf beg end))
858 (or buffer-matches-file
859 (progn
860 (if (y-or-n-p (format "Save file %s? "
861 (car fcc-list)))
862 (save-buffer))
863 (setq dont-write-the-file t))))
864 (if max (narrow-to-region (point-min) max))))))
865 ;; Append to the file directly,
866 ;; unless we've already taken care of it.
867 (if (and (not dont-write-the-file)
868 (file-exists-p (car fcc-list))
869 (mail-file-babyl-p (car fcc-list)))
870 ;; If the file is a Babyl file,
871 ;; convert the message to Babyl format.
872 (save-excursion
873 (set-buffer (get-buffer-create " mail-temp"))
874 (setq buffer-read-only nil)
875 (erase-buffer)
876 (insert "\C-l\n0, unseen,,\n*** EOOH ***\n"
877 "Date: " (mail-rfc822-date) "\n")
878 (insert-buffer-substring curbuf beg2 end)
879 (insert "\n\C-_")
880 (write-region (point-min) (point-max) (car fcc-list) t)
881 (erase-buffer))
882 (write-region
883 (1+ (point-min)) (point-max) (car fcc-list) t))
884 (and buffer (not dont-write-the-file)
885 (with-current-buffer buffer
886 (set-visited-file-modtime))))
887 (setq fcc-list (cdr fcc-list))))
888 (kill-buffer tembuf)))
889
890 (defun mail-sent-via ()
891 "Make a Sent-via header line from each To or CC header line."
892 (interactive)
893 (save-excursion
894 (goto-char (point-min))
895 ;; find the header-separator
896 (search-forward (concat "\n" mail-header-separator "\n"))
897 (forward-line -1)
898 ;; put a marker at the end of the header
899 (let ((end (point-marker))
900 (case-fold-search t)
901 to-line)
902 (goto-char (point-min))
903 ;; search for the To: lines and make Sent-via: lines from them
904 ;; search for the next To: line
905 (while (re-search-forward "^\\(to\\|cc\\):" end t)
906 ;; Grab this line plus all its continuations, sans the `to:'.
907 (let ((to-line
908 (buffer-substring (point)
909 (progn
910 (if (re-search-forward "^[^ \t\n]" end t)
911 (backward-char 1)
912 (goto-char end))
913 (point)))))
914 ;; Insert a copy, with altered header field name.
915 (insert-before-markers "Sent-via:" to-line))))))
916 \f
917 (defun mail-to ()
918 "Move point to end of To-field."
919 (interactive)
920 (expand-abbrev)
921 (mail-position-on-field "To"))
922
923 (defun mail-subject ()
924 "Move point to end of Subject-field."
925 (interactive)
926 (expand-abbrev)
927 (mail-position-on-field "Subject"))
928
929 (defun mail-cc ()
930 "Move point to end of CC-field. Create a CC field if none."
931 (interactive)
932 (expand-abbrev)
933 (or (mail-position-on-field "cc" t)
934 (progn (mail-position-on-field "to")
935 (insert "\nCC: "))))
936
937 (defun mail-bcc ()
938 "Move point to end of BCC-field. Create a BCC field if none."
939 (interactive)
940 (expand-abbrev)
941 (or (mail-position-on-field "bcc" t)
942 (progn (mail-position-on-field "to")
943 (insert "\nBCC: "))))
944
945 (defun mail-fcc (folder)
946 "Add a new FCC field, with file name completion."
947 (interactive "FFolder carbon copy: ")
948 (expand-abbrev)
949 (or (mail-position-on-field "fcc" t) ;Put new field after exiting FCC.
950 (mail-position-on-field "to"))
951 (insert "\nFCC: " folder))
952
953 (defun mail-reply-to ()
954 "Move point to end of Reply-To-field."
955 (interactive)
956 (expand-abbrev)
957 (mail-position-on-field "Reply-To"))
958
959 (defun mail-position-on-field (field &optional soft)
960 (let (end
961 (case-fold-search t))
962 (goto-char (point-min))
963 (re-search-forward (concat "^" (regexp-quote mail-header-separator) "$"))
964 (setq end (match-beginning 0))
965 (goto-char (point-min))
966 (if (re-search-forward (concat "^" (regexp-quote field) ":") end t)
967 (progn
968 (re-search-forward "^[^ \t]" nil 'move)
969 (beginning-of-line)
970 (skip-chars-backward "\n")
971 t)
972 (or soft
973 (progn (goto-char end)
974 (insert field ": \n")
975 (skip-chars-backward "\n")))
976 nil)))
977
978 (defun mail-text ()
979 "Move point to beginning of message text."
980 (interactive)
981 (expand-abbrev)
982 (goto-char (point-min))
983 (search-forward (concat "\n" mail-header-separator "\n")))
984 \f
985 (defun mail-signature (atpoint)
986 "Sign letter with contents of the file `mail-signature-file'.
987 Prefix arg means put contents at point."
988 (interactive "P")
989 (save-excursion
990 (or atpoint
991 (goto-char (point-max)))
992 (skip-chars-backward " \t\n")
993 (end-of-line)
994 (or atpoint
995 (delete-region (point) (point-max)))
996 (insert "\n\n-- \n")
997 (insert-file-contents (expand-file-name mail-signature-file))))
998
999 (defun mail-fill-yanked-message (&optional justifyp)
1000 "Fill the paragraphs of a message yanked into this one.
1001 Numeric argument means justify as well."
1002 (interactive "P")
1003 (save-excursion
1004 (goto-char (point-min))
1005 (search-forward (concat "\n" mail-header-separator "\n") nil t)
1006 (fill-individual-paragraphs (point)
1007 (point-max)
1008 justifyp
1009 t)))
1010
1011 (defun mail-indent-citation ()
1012 "Modify text just inserted from a message to be cited.
1013 The inserted text should be the region.
1014 When this function returns, the region is again around the modified text.
1015
1016 Normally, indent each nonblank line `mail-indentation-spaces' spaces.
1017 However, if `mail-yank-prefix' is non-nil, insert that prefix on each line."
1018 (mail-yank-clear-headers (region-beginning) (region-end))
1019 (if (null mail-yank-prefix)
1020 (indent-rigidly (region-beginning) (region-end)
1021 mail-indentation-spaces)
1022 (save-excursion
1023 (goto-char (region-beginning))
1024 (while (< (point) (region-end))
1025 (insert mail-yank-prefix)
1026 (forward-line 1)))))
1027
1028 (defun mail-yank-original (arg)
1029 "Insert the message being replied to, if any (in rmail).
1030 Puts point after the text and mark before.
1031 Normally, indents each nonblank line ARG spaces (default 3).
1032 However, if `mail-yank-prefix' is non-nil, insert that prefix on each line.
1033
1034 Just \\[universal-argument] as argument means don't indent, insert no prefix,
1035 and don't delete any header fields."
1036 (interactive "P")
1037 (if mail-reply-action
1038 (let ((start (point))
1039 (original mail-reply-action))
1040 (and (consp original) (eq (car original) 'insert-buffer)
1041 (setq original (nth 1 original)))
1042 (if (consp original)
1043 (apply (car original) (cdr original))
1044 ;; If the original message is in another window in the same frame,
1045 ;; delete that window to save screen space.
1046 ;; t means don't alter other frames.
1047 (delete-windows-on original t)
1048 (insert-buffer original))
1049 (if (consp arg)
1050 nil
1051 (goto-char start)
1052 (let ((mail-indentation-spaces (if arg (prefix-numeric-value arg)
1053 mail-indentation-spaces)))
1054 (if mail-citation-hook
1055 (run-hooks 'mail-citation-hook)
1056 (if mail-yank-hooks
1057 (run-hooks 'mail-yank-hooks)
1058 (mail-indent-citation)))))
1059 ;; This is like exchange-point-and-mark, but doesn't activate the mark.
1060 ;; It is cleaner to avoid activation, even though the command
1061 ;; loop would deactivate the mark because we inserted text.
1062 (goto-char (prog1 (mark t)
1063 (set-marker (mark-marker) (point) (current-buffer))))
1064 (if (not (eolp)) (insert ?\n)))))
1065
1066 (defun mail-yank-clear-headers (start end)
1067 (if (< end start)
1068 (let (temp)
1069 (setq temp start start end end temp)))
1070 (if mail-yank-ignored-headers
1071 (save-excursion
1072 (goto-char start)
1073 (if (search-forward "\n\n" end t)
1074 (save-restriction
1075 (narrow-to-region start (point))
1076 (goto-char start)
1077 (while (let ((case-fold-search t))
1078 (re-search-forward mail-yank-ignored-headers nil t))
1079 (beginning-of-line)
1080 (delete-region (point)
1081 (progn (re-search-forward "\n[^ \t]")
1082 (forward-char -1)
1083 (point)))))))))
1084
1085 (defun mail-yank-region (arg)
1086 "Insert the selected region from the message being replied to.
1087 Puts point after the text and mark before.
1088 Normally, indents each nonblank line ARG spaces (default 3).
1089 However, if `mail-yank-prefix' is non-nil, insert that prefix on each line.
1090
1091 Just \\[universal-argument] as argument means don't indent, insert no prefix,
1092 and don't delete any header fields."
1093 (interactive "P")
1094 (and (consp mail-reply-action)
1095 (eq (car mail-reply-action) 'insert-buffer)
1096 (let ((buffer (nth 1 mail-reply-action))
1097 (start (point)))
1098 ;; Insert the citation text.
1099 (insert (with-current-buffer buffer
1100 (buffer-substring (point) (mark))))
1101 (push-mark start)
1102 ;; Indent or otherwise annotate the citation text.
1103 (if (consp arg)
1104 nil
1105 (let ((mail-indentation-spaces (if arg (prefix-numeric-value arg)
1106 mail-indentation-spaces)))
1107 (if mail-citation-hook
1108 (run-hooks 'mail-citation-hook)
1109 (if mail-yank-hooks
1110 (run-hooks 'mail-yank-hooks)
1111 (mail-indent-citation))))))))
1112 \f
1113 (defun mail-attach-file (&optional file)
1114 "Insert a file at the end of the buffer, with separator lines around it."
1115 (interactive "fAttach file: ")
1116 (save-excursion
1117 (goto-char (point-max))
1118 (or (bolp) (newline))
1119 (newline)
1120 (let ((start (point))
1121 middle)
1122 (insert (format "===File %s===" file))
1123 (insert-char ?= (max 0 (- 60 (current-column))))
1124 (newline)
1125 (setq middle (point))
1126 (insert "============================================================\n")
1127 (push-mark)
1128 (goto-char middle)
1129 (insert-file-contents file)
1130 (or (bolp) (newline))
1131 (goto-char start))))
1132 \f
1133 ;; Put these commands last, to reduce chance of lossage from quitting
1134 ;; in middle of loading the file.
1135
1136 ;;;###autoload (add-hook 'same-window-buffer-names "*mail*")
1137
1138 ;;;###autoload
1139 (defun mail (&optional noerase to subject in-reply-to cc replybuffer actions)
1140 "Edit a message to be sent. Prefix arg means resume editing (don't erase).
1141 When this function returns, the buffer `*mail*' is selected.
1142 The value is t if the message was newly initialized; otherwise, nil.
1143
1144 Optionally, the signature file `mail-signature-file' can be inserted at the
1145 end; see the variable `mail-signature'.
1146
1147 \\<mail-mode-map>
1148 While editing message, type \\[mail-send-and-exit] to send the message and exit.
1149
1150 Various special commands starting with C-c are available in sendmail mode
1151 to move to message header fields:
1152 \\{mail-mode-map}
1153
1154 If `mail-self-blind' is non-nil, a BCC to yourself is inserted
1155 when the message is initialized.
1156
1157 If `mail-default-reply-to' is non-nil, it should be an address (a string);
1158 a Reply-to: field with that address is inserted.
1159
1160 If `mail-archive-file-name' is non-nil, an FCC field with that file name
1161 is inserted.
1162
1163 The normal hook `mail-setup-hook' is run after the message is
1164 initialized. It can add more default fields to the message.
1165
1166 When calling from a program, the first argument if non-nil says
1167 not to erase the existing contents of the `*mail*' buffer.
1168
1169 The second through fifth arguments,
1170 TO, SUBJECT, IN-REPLY-TO and CC, specify if non-nil
1171 the initial contents of those header fields.
1172 These arguments should not have final newlines.
1173 The sixth argument REPLYBUFFER is a buffer which contains an
1174 original message being replied to, or else an action
1175 of the form (FUNCTION . ARGS) which says how to insert the original.
1176 Or it can be nil, if not replying to anything.
1177 The seventh argument ACTIONS is a list of actions to take
1178 if/when the message is sent. Each action looks like (FUNCTION . ARGS);
1179 when the message is sent, we apply FUNCTION to ARGS.
1180 This is how Rmail arranges to mark messages `answered'."
1181 (interactive "P")
1182 ;;; This is commented out because I found it was confusing in practice.
1183 ;;; It is easy enough to rename *mail* by hand with rename-buffer
1184 ;;; if you want to have multiple mail buffers.
1185 ;;; And then you can control which messages to save. --rms.
1186 ;;; (let ((index 1)
1187 ;;; buffer)
1188 ;;; ;; If requested, look for a mail buffer that is modified and go to it.
1189 ;;; (if noerase
1190 ;;; (progn
1191 ;;; (while (and (setq buffer
1192 ;;; (get-buffer (if (= 1 index) "*mail*"
1193 ;;; (format "*mail*<%d>" index))))
1194 ;;; (not (buffer-modified-p buffer)))
1195 ;;; (setq index (1+ index)))
1196 ;;; (if buffer (switch-to-buffer buffer)
1197 ;;; ;; If none exists, start a new message.
1198 ;;; ;; This will never re-use an existing unmodified mail buffer
1199 ;;; ;; (since index is not 1 anymore). Perhaps it should.
1200 ;;; (setq noerase nil))))
1201 ;;; ;; Unless we found a modified message and are happy, start a new message.
1202 ;;; (if (not noerase)
1203 ;;; (progn
1204 ;;; ;; Look for existing unmodified mail buffer.
1205 ;;; (while (and (setq buffer
1206 ;;; (get-buffer (if (= 1 index) "*mail*"
1207 ;;; (format "*mail*<%d>" index))))
1208 ;;; (buffer-modified-p buffer))
1209 ;;; (setq index (1+ index)))
1210 ;;; ;; If none, make a new one.
1211 ;;; (or buffer
1212 ;;; (setq buffer (generate-new-buffer "*mail*")))
1213 ;;; ;; Go there and initialize it.
1214 ;;; (switch-to-buffer buffer)
1215 ;;; (erase-buffer)
1216 ;;; (setq default-directory (expand-file-name "~/"))
1217 ;;; (auto-save-mode auto-save-default)
1218 ;;; (mail-mode)
1219 ;;; (mail-setup to subject in-reply-to cc replybuffer actions)
1220 ;;; (if (and buffer-auto-save-file-name
1221 ;;; (file-exists-p buffer-auto-save-file-name))
1222 ;;; (message "Auto save file for draft message exists; consider M-x mail-recover"))
1223 ;;; t))
1224 (pop-to-buffer "*mail*")
1225 ;; Put the auto-save file in the home dir
1226 ;; to avoid any danger that it can't be written.
1227 (if (file-exists-p (expand-file-name "~/"))
1228 (setq default-directory (expand-file-name "~/")))
1229 (auto-save-mode auto-save-default)
1230 (mail-mode)
1231 ;; Disconnect the buffer from its visited file
1232 ;; (in case the user has actually visited a file *mail*).
1233 ; (set-visited-file-name nil)
1234 (let (initialized)
1235 (and (not noerase)
1236 (or (not (buffer-modified-p))
1237 (y-or-n-p "Unsent message being composed; erase it? "))
1238 (let ((inhibit-read-only t))
1239 (erase-buffer)
1240 (mail-setup to subject in-reply-to cc replybuffer actions)
1241 (setq initialized t)))
1242 (if (and buffer-auto-save-file-name
1243 (file-exists-p buffer-auto-save-file-name))
1244 (message "Auto save file for draft message exists; consider M-x mail-recover"))
1245 initialized))
1246
1247 (defun mail-recover ()
1248 "Reread contents of current buffer from its last auto-save file."
1249 (interactive)
1250 (let ((file-name (make-auto-save-file-name)))
1251 (cond ((save-window-excursion
1252 (if (not (eq system-type 'vax-vms))
1253 (with-output-to-temp-buffer "*Directory*"
1254 (buffer-disable-undo standard-output)
1255 (let ((default-directory "/"))
1256 (call-process
1257 "ls" nil standard-output nil "-l" file-name))))
1258 (yes-or-no-p (format "Recover auto save file %s? " file-name)))
1259 (let ((buffer-read-only nil))
1260 (erase-buffer)
1261 (insert-file-contents file-name nil)))
1262 (t (error "mail-recover cancelled")))))
1263
1264 ;;;###autoload
1265 (defun mail-other-window (&optional noerase to subject in-reply-to cc replybuffer sendactions)
1266 "Like `mail' command, but display mail buffer in another window."
1267 (interactive "P")
1268 (let ((pop-up-windows t)
1269 (special-display-buffer-names nil)
1270 (special-display-regexps nil)
1271 (same-window-buffer-names nil)
1272 (same-window-regexps nil))
1273 (pop-to-buffer "*mail*"))
1274 (mail noerase to subject in-reply-to cc replybuffer sendactions))
1275
1276 ;;;###autoload
1277 (defun mail-other-frame (&optional noerase to subject in-reply-to cc replybuffer sendactions)
1278 "Like `mail' command, but display mail buffer in another frame."
1279 (interactive "P")
1280 (let ((pop-up-frames t)
1281 (special-display-buffer-names nil)
1282 (special-display-regexps nil)
1283 (same-window-buffer-names nil)
1284 (same-window-regexps nil))
1285 (pop-to-buffer "*mail*"))
1286 (mail noerase to subject in-reply-to cc replybuffer sendactions))
1287
1288 ;;; Do not add anything but external entries on this page.
1289
1290 (provide 'sendmail)
1291
1292 ;;; sendmail.el ends here