]> code.delx.au - gnu-emacs/blob - lisp/mail/sendmail.el
(mail-send): Don't clear modified or delete autosave if visiting a file.
[gnu-emacs] / lisp / mail / sendmail.el
1 ;;; sendmail.el --- mail sending commands for Emacs.
2
3 ;; Copyright (C) 1985, 1986, 1992 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
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23
24 ;;; Code:
25
26 ;;;###autoload
27 (defvar mail-self-blind nil "\
28 Non-nil means insert BCC to self in messages to be sent.
29 This is done when the message is initialized,
30 so you can remove or alter the BCC field to override the default.")
31
32 ;;;###autoload
33 (defvar mail-interactive nil "\
34 Non-nil means when sending a message wait for and display errors.
35 nil means let mailer mail back a message to report errors.")
36
37 ;;;###autoload
38 (defvar mail-yank-ignored-headers "^via:\\|^mail-from:\\|^origin:\\|^status:\\|^remailed\\|^received:\\|^message-id:\\|^summary-line:\\|^to:\\|^subject:\\|^in-reply-to:\\|^return-path:" "\
39 Delete these headers from old message when it's inserted in a reply.")
40
41 ;; Useful to set in site-init.el
42 ;;;###autoload
43 (defconst send-mail-function 'sendmail-send-it "\
44 Function to call to send the current buffer as mail.
45 The headers are be delimited by a line which is `mail-header-separator'.")
46
47 ;;;###autoload
48 (defvar mail-header-separator "--text follows this line--" "\
49 *Line used to separate headers from text in messages being composed.")
50
51 ;;;###autoload
52 (defvar mail-archive-file-name nil "\
53 *Name of file to write all outgoing messages in, or nil for none.
54 Do not use an rmail file here! Instead, use its inbox file.")
55
56 (defvar mail-default-reply-to nil
57 "*Address to insert as default Reply-to field of outgoing messages.")
58
59 (defvar mail-alias-file nil
60 "*If non-nil, the name of a file to use instead of `/usr/lib/aliases'.
61 This file defines aliases to be expanded by the mailer; this is a different
62 feature from that of defining aliases in `.mailrc' to be expanded in Emacs.
63 This variable has no effect unless your system uses sendmail as its mailer.")
64
65 (defvar mail-aliases t
66 "Alist of mail address aliases,
67 or t meaning should be initialized from `~/.mailrc'.
68 The alias definitions in `~/.mailrc' have this form:
69 alias ALIAS MEANING")
70
71 (defvar mail-yank-prefix nil
72 "*Prefix insert on lines of yanked message being replied to.
73 nil means use indentation.")
74
75 (defvar mail-abbrevs-loaded nil)
76 (defvar mail-mode-map nil)
77
78 (autoload 'build-mail-aliases "mailalias"
79 "Read mail aliases from `~/.mailrc' and set `mail-aliases'."
80 nil)
81
82 (autoload 'expand-mail-aliases "mailalias"
83 "Expand all mail aliases in suitable header fields found between BEG and END.
84 Suitable header fields are `To', `Cc' and `Bcc' and their `Resent-' variants.
85 Optional second arg EXCLUDE may be a regular expression defining text to be
86 removed from alias expansions."
87 nil)
88
89 ;;;###autoload
90 (defvar mail-signature nil
91 "*Text inserted at end of mail buffer when a message is initialized.
92 If t, it means to insert the contents of the file `~/.signature'.")
93
94 (defvar mail-reply-buffer nil)
95 (defvar mail-send-actions nil
96 "A list of actions to be performed upon successful sending of a message.")
97
98 (defvar mail-default-headers nil
99 "*A string containing header lines, to be inserted in outgoing messages.
100 It is inserted before you edit the message,
101 so you can edit or delete these lines.")
102
103 (defvar mail-mode-syntax-table nil
104 "Syntax table used while in mail mode.")
105
106 (if (null mail-mode-syntax-table)
107 (progn
108 (setq mail-mode-syntax-table (copy-syntax-table text-mode-syntax-table))
109 (modify-syntax-entry ?% ". " mail-mode-syntax-table)))
110
111 (defun mail-setup (to subject in-reply-to cc replybuffer actions)
112 (if (eq mail-aliases t)
113 (progn
114 (setq mail-aliases nil)
115 (if (file-exists-p "~/.mailrc")
116 (build-mail-aliases))))
117 (setq mail-send-actions actions)
118 (setq mail-reply-buffer replybuffer)
119 (goto-char (point-min))
120 (insert "To: ")
121 (save-excursion
122 (if to
123 (progn
124 (insert to "\n")
125 ;;; Here removed code to extract names from within <...>
126 ;;; on the assumption that mail-strip-quoted-names
127 ;;; has been called and has done so.
128 (let ((fill-prefix "\t"))
129 (fill-region (point-min) (point-max))))
130 (newline))
131 (if cc
132 (let ((opos (point))
133 (fill-prefix "\t"))
134 (insert "CC: " cc "\n")
135 (fill-region-as-paragraph opos (point-max))))
136 (if in-reply-to
137 (insert "In-reply-to: " in-reply-to "\n"))
138 (insert "Subject: " (or subject "") "\n")
139 (if mail-default-headers
140 (insert mail-default-headers))
141 (if mail-default-reply-to
142 (insert "Reply-to: " mail-default-reply-to "\n"))
143 (if mail-self-blind
144 (insert "BCC: " (user-login-name) "\n"))
145 (if mail-archive-file-name
146 (insert "FCC: " mail-archive-file-name "\n"))
147 (insert mail-header-separator "\n")
148 ;; Insert the signature.
149 (cond ((eq mail-signature t)
150 (if (file-exists-p "~/.signature")
151 (progn
152 (insert "--\n")
153 (insert-file-contents "~/.signature"))))
154 (mail-signature
155 (insert mail-signature)))
156 (goto-char (point-max))
157 (or (bolp) (newline)))
158 (if to (goto-char (point-max)))
159 (or to subject in-reply-to
160 (set-buffer-modified-p nil))
161 (run-hooks 'mail-setup-hook))
162
163 ;;;###autoload
164 (defun mail-mode ()
165 "Major mode for editing mail to be sent.
166 Like Text Mode but with these additional commands:
167 C-c C-s mail-send (send the message) C-c C-c mail-send-and-exit
168 C-c C-f move to a header field (and create it if there isn't):
169 C-c C-f C-t move to To: C-c C-f C-s move to Subj:
170 C-c C-f C-b move to BCC: C-c C-f C-c move to CC:
171 C-c C-t move to message text.
172 C-c C-y mail-yank-original (insert current message, in Rmail).
173 C-c C-q mail-fill-yanked-message (fill what was yanked).
174 C-c C-v mail-sent-via (add a sent-via field for each To or CC)."
175 (interactive)
176 (kill-all-local-variables)
177 (make-local-variable 'mail-reply-buffer)
178 (setq mail-reply-buffer nil)
179 (make-local-variable 'mail-send-actions)
180 (set-syntax-table mail-mode-syntax-table)
181 (use-local-map mail-mode-map)
182 (setq local-abbrev-table text-mode-abbrev-table)
183 (setq major-mode 'mail-mode)
184 (setq mode-name "Mail")
185 (setq buffer-offer-save t)
186 (make-local-variable 'paragraph-separate)
187 (make-local-variable 'paragraph-start)
188 (setq paragraph-start (concat "^" mail-header-separator
189 "$\\|^[ \t]*[-_][-_][-_]+$\\|"
190 paragraph-start))
191 (setq paragraph-separate (concat "^" mail-header-separator
192 "$\\|^[ \t]*[-_][-_][-_]+$\\|"
193 paragraph-separate))
194 (run-hooks 'text-mode-hook 'mail-mode-hook))
195
196 (if mail-mode-map
197 nil
198 (setq mail-mode-map (nconc (make-sparse-keymap) text-mode-map))
199 (define-key mail-mode-map "\C-c?" 'describe-mode)
200 (define-key mail-mode-map "\C-c\C-f\C-t" 'mail-to)
201 (define-key mail-mode-map "\C-c\C-f\C-b" 'mail-bcc)
202 (define-key mail-mode-map "\C-c\C-f\C-f" 'mail-fcc)
203 (define-key mail-mode-map "\C-c\C-f\C-c" 'mail-cc)
204 (define-key mail-mode-map "\C-c\C-f\C-s" 'mail-subject)
205 (define-key mail-mode-map "\C-c\C-t" 'mail-text)
206 (define-key mail-mode-map "\C-c\C-y" 'mail-yank-original)
207 (define-key mail-mode-map "\C-c\C-q" 'mail-fill-yanked-message)
208 (define-key mail-mode-map "\C-c\C-w" 'mail-signature)
209 (define-key mail-mode-map "\C-c\C-v" 'mail-sent-via)
210 (define-key mail-mode-map "\C-c\C-c" 'mail-send-and-exit)
211 (define-key mail-mode-map "\C-c\C-s" 'mail-send))
212 \f
213 (defun mail-send-and-exit (arg)
214 "Send message like `mail-send', then, if no errors, exit from mail buffer.
215 Prefix arg means don't delete this window."
216 (interactive "P")
217 (mail-send)
218 (let ((newbuf (other-buffer (current-buffer))))
219 (bury-buffer (current-buffer))
220 (if (and (not arg)
221 (not (one-window-p))
222 (save-excursion
223 (set-buffer (window-buffer (next-window (selected-window) 'not)))
224 (eq major-mode 'rmail-mode)))
225 (delete-window)
226 (switch-to-buffer newbuf))))
227
228 (defun mail-send ()
229 "Send the message in the current buffer.
230 If `mail-interactive' is non-nil, wait for success indication
231 or error messages, and inform user.
232 Otherwise any failure is reported in a message back to
233 the user from the mailer."
234 (interactive)
235 (if (or (buffer-modified-p)
236 (y-or-n-p "Message already sent; resend? "))
237 (progn
238 (message "Sending...")
239 (run-hooks 'mail-send-hook)
240 (funcall send-mail-function)
241 ;; Now perform actions on successful sending.
242 (while mail-send-actions
243 (condition-case nil
244 (apply (car (car mail-send-actions))
245 (cdr (car mail-send-actions)))
246 (error))
247 (setq mail-send-actions (cdr mail-send-actions)))
248 (message "Sending...done")
249 ;; If buffer has no file, mark it as unmodified and delete autosave.
250 (if (not buffer-file-name)
251 (progn
252 (set-buffer-modified-p nil)
253 (delete-auto-save-file-if-necessary t))))))
254
255 (defun sendmail-send-it ()
256 (let ((errbuf (if mail-interactive
257 (generate-new-buffer " sendmail errors")
258 0))
259 (tembuf (generate-new-buffer " sendmail temp"))
260 (case-fold-search nil)
261 delimline
262 (mailbuf (current-buffer)))
263 (unwind-protect
264 (save-excursion
265 (set-buffer tembuf)
266 (erase-buffer)
267 (insert-buffer-substring mailbuf)
268 (goto-char (point-max))
269 ;; require one newline at the end.
270 (or (= (preceding-char) ?\n)
271 (insert ?\n))
272 ;; Change header-delimiter to be what sendmail expects.
273 (goto-char (point-min))
274 (re-search-forward
275 (concat "^" (regexp-quote mail-header-separator) "\n"))
276 (replace-match "\n")
277 (backward-char 1)
278 (setq delimline (point-marker))
279 (if mail-aliases
280 (expand-mail-aliases (point-min) delimline))
281 (goto-char (point-min))
282 ;; ignore any blank lines in the header
283 (while (and (re-search-forward "\n\n\n*" delimline t)
284 (< (point) delimline))
285 (replace-match "\n"))
286 (let ((case-fold-search t))
287 (goto-char (point-min))
288 (if (re-search-forward "^Sender:" delimline t)
289 (error "Sender may not be specified."))
290 ;; Find and handle any FCC fields.
291 (goto-char (point-min))
292 (if (re-search-forward "^FCC:" delimline t)
293 (mail-do-fcc delimline))
294 ;; If the From is different than current user, insert Sender.
295 (goto-char (point-min))
296 (and (re-search-forward "^From:" delimline t)
297 (progn
298 (require 'mail-utils)
299 (not (string-equal
300 (mail-strip-quoted-names
301 (save-restriction
302 (narrow-to-region (point-min) delimline)
303 (mail-fetch-field "From")))
304 (user-login-name))))
305 (progn
306 (forward-line 1)
307 (insert "Sender: " (user-login-name) "\n")))
308 ;; "S:" is an abbreviation for "Subject:".
309 (goto-char (point-min))
310 (if (re-search-forward "^S:" delimline t)
311 (replace-match "Subject:"))
312 ;; Don't send out a blank subject line
313 (goto-char (point-min))
314 (if (re-search-forward "^Subject:[ \t]*\n" delimline t)
315 (replace-match ""))
316 (if mail-interactive
317 (save-excursion
318 (set-buffer errbuf)
319 (erase-buffer))))
320 (apply 'call-process-region
321 (append (list (point-min) (point-max)
322 (if (boundp 'sendmail-program)
323 sendmail-program
324 "/usr/lib/sendmail")
325 nil errbuf nil
326 "-oi" "-t")
327 ;; Always specify who from,
328 ;; since some systems have broken sendmails.
329 (list "-f" (user-login-name))
330 ;;; ;; Don't say "from root" if running under su.
331 ;;; (and (equal (user-real-login-name) "root")
332 ;;; (list "-f" (user-login-name)))
333 (and mail-alias-file
334 (list (concat "-oA" mail-alias-file)))
335 ;; These mean "report errors by mail"
336 ;; and "deliver in background".
337 (if (null mail-interactive) '("-oem" "-odb"))))
338 (if mail-interactive
339 (save-excursion
340 (set-buffer errbuf)
341 (goto-char (point-min))
342 (while (re-search-forward "\n\n* *" nil t)
343 (replace-match "; "))
344 (if (not (zerop (buffer-size)))
345 (error "Sending...failed to %s"
346 (buffer-substring (point-min) (point-max)))))))
347 (kill-buffer tembuf)
348 (if (bufferp errbuf)
349 (kill-buffer errbuf)))))
350
351 (defun mail-do-fcc (header-end)
352 (let (fcc-list
353 (rmailbuf (current-buffer))
354 timezone
355 (tembuf (generate-new-buffer " rmail output"))
356 (case-fold-search t))
357 (save-excursion
358 (goto-char (point-min))
359 (while (re-search-forward "^FCC:[ \t]*" header-end t)
360 (setq fcc-list (cons (buffer-substring (point)
361 (progn
362 (end-of-line)
363 (skip-chars-backward " \t")
364 (point)))
365 fcc-list))
366 (delete-region (match-beginning 0)
367 (progn (forward-line 1) (point))))
368 (set-buffer tembuf)
369 (erase-buffer)
370 (call-process "date" nil t nil)
371 (goto-char (point-min))
372 (re-search-forward
373 "[0-9] \\([A-Za-z][A-Za-z ]*[A-Za-z]\\)[0-9 ]*$")
374 (setq timezone (buffer-substring (match-beginning 1) (match-end 1)))
375 (erase-buffer)
376 (insert "\nFrom " (user-login-name) " "
377 (current-time-string) "\n")
378 ;; Insert the time zone before the year.
379 (forward-char -1)
380 (forward-word -1)
381 (insert timezone " ")
382 (goto-char (point-max))
383 (insert-buffer-substring rmailbuf)
384 ;; Make sure messages are separated.
385 (goto-char (point-max))
386 (insert ?\n)
387 (goto-char 2)
388 ;; ``Quote'' "^From " as ">From "
389 ;; (note that this isn't really quoting, as there is no requirement
390 ;; that "^[>]+From " be quoted in the same transparent way.)
391 (let ((case-fold-search nil))
392 (while (search-forward "\nFrom " nil t)
393 (forward-char -5)
394 (insert ?>)))
395 (while fcc-list
396 (let ((buffer (get-file-buffer (car fcc-list))))
397 (if buffer
398 ;; File is present in a buffer => append to that buffer.
399 (let ((curbuf (current-buffer))
400 (beg (point-min)) (end (point-max)))
401 (save-excursion
402 (set-buffer buffer)
403 ;; Keep the end of the accessible portion at the same place
404 ;; unless it is the end of the buffer.
405 (let ((max (if (/= (1+ (buffer-size)) (point-max))
406 (point-max))))
407 (unwind-protect
408 (progn
409 (narrow-to-region (point-min) (1+ (buffer-size)))
410 (goto-char (point-max))
411 (if (eq major-mode 'rmail-mode)
412 ;; Append as a message to an RMAIL file
413 (let ((buffer-read-only nil))
414 ;; This forces RMAIL's message counters to be
415 ;; recomputed when the next RMAIL operation is
416 ;; done on the buffer.
417 ;; See rmail-maybe-set-message-counters.
418 (setq rmail-total-messages nil)
419 (insert "\C-l\n0, unseen,,\n*** EOOH ***\n"
420 "From: " (user-login-name) "\n"
421 "Date: " (current-time-string) "\n")
422 (insert-buffer-substring curbuf beg end)
423 (insert "\n\C-_")
424 (rmail-set-message-counters))
425 (insert-buffer-substring curbuf beg end)))
426 (if max (narrow-to-region (point-min) max))))))
427 ;; Else append to the file directly.
428 (write-region
429 ;; Include a blank line before if file already exists.
430 (if (file-exists-p (car fcc-list)) (point-min) (1+ (point-min)))
431 (point-max) (car fcc-list) t)))
432 (setq fcc-list (cdr fcc-list))))
433 (kill-buffer tembuf)))
434
435 (defun mail-sent-via ()
436 "Make a Sent-via header line from each To or CC header line."
437 (interactive)
438 (save-excursion
439 (goto-char (point-min))
440 ;; find the header-separator
441 (search-forward (concat "\n" mail-header-separator "\n"))
442 (forward-line -1)
443 ;; put a marker at the end of the header
444 (let ((end (point-marker))
445 (case-fold-search t)
446 to-line)
447 (goto-char (point-min))
448 ;; search for the To: lines and make Sent-via: lines from them
449 ;; search for the next To: line
450 (while (re-search-forward "^\\(to\\|cc\\):" end t)
451 ;; Grab this line plus all its continuations, sans the `to:'.
452 (let ((to-line
453 (buffer-substring (point)
454 (progn
455 (if (re-search-forward "^[^ \t\n]" end t)
456 (backward-char 1)
457 (goto-char end))
458 (point)))))
459 ;; Insert a copy, with altered header field name.
460 (insert-before-markers "Sent-via:" to-line))))))
461 \f
462 (defun mail-to ()
463 "Move point to end of To-field."
464 (interactive)
465 (expand-abbrev)
466 (mail-position-on-field "To"))
467
468 (defun mail-subject ()
469 "Move point to end of Subject-field."
470 (interactive)
471 (expand-abbrev)
472 (mail-position-on-field "Subject"))
473
474 (defun mail-cc ()
475 "Move point to end of CC-field. Create a CC field if none."
476 (interactive)
477 (expand-abbrev)
478 (or (mail-position-on-field "cc" t)
479 (progn (mail-position-on-field "to")
480 (insert "\nCC: "))))
481
482 (defun mail-bcc ()
483 "Move point to end of BCC-field. Create a BCC field if none."
484 (interactive)
485 (expand-abbrev)
486 (or (mail-position-on-field "bcc" t)
487 (progn (mail-position-on-field "to")
488 (insert "\nBCC: "))))
489
490 (defun mail-fcc ()
491 "Add a new FCC field, with file name completion."
492 (interactive)
493 (expand-abbrev)
494 (or (mail-position-on-field "fcc" t) ;Put new field after exiting FCC.
495 (mail-position-on-field "to"))
496 (insert "\nFCC: " (read-file-name "Folder carbon copy: ")))
497
498 (defun mail-position-on-field (field &optional soft)
499 (let (end
500 (case-fold-search t))
501 (goto-char (point-min))
502 (re-search-forward (concat "^" (regexp-quote mail-header-separator) "\n"))
503 (setq end (match-beginning 0))
504 (goto-char (point-min))
505 (if (re-search-forward (concat "^" (regexp-quote field) ":") end t)
506 (progn
507 (re-search-forward "^[^ \t]" nil 'move)
508 (beginning-of-line)
509 (skip-chars-backward "\n")
510 t)
511 (or soft
512 (progn (goto-char end)
513 (insert field ": \n")
514 (skip-chars-backward "\n")))
515 nil)))
516
517 (defun mail-text ()
518 "Move point to beginning of text field."
519 (interactive)
520 (goto-char (point-min))
521 (search-forward (concat "\n" mail-header-separator "\n")))
522 \f
523 (defun mail-signature (atpoint)
524 "Sign letter with contents of `mail-signature-file'."
525 (interactive "P")
526 (save-excursion
527 (or atpoint
528 (goto-char (point-max)))
529 (skip-chars-backward " \t\n")
530 (end-of-line)
531 (or atpoint
532 (delete-region (point) (point-max)))
533 (insert "\n\n--\n")
534 (insert-file-contents (expand-file-name "~/.signature"))))
535
536 (defun mail-fill-yanked-message (&optional justifyp)
537 "Fill the paragraphs of a message yanked into this one.
538 Numeric argument means justify as well."
539 (interactive "P")
540 (save-excursion
541 (goto-char (point-min))
542 (search-forward (concat "\n" mail-header-separator "\n") nil t)
543 (fill-individual-paragraphs (point)
544 (point-max)
545 justifyp
546 t)))
547
548 (defun mail-yank-original (arg)
549 "Insert the message being replied to, if any (in rmail).
550 Puts point before the text and mark after.
551 Normally, indents each nonblank line ARG spaces (default 3).
552 However, if `mail-yank-prefix' is non-nil, insert that prefix on each line.
553
554 Just \\[universal-argument] as argument means don't indent, insert no prefix,
555 and don't delete any header fields."
556 (interactive "P")
557 (if mail-reply-buffer
558 (let ((start (point)))
559 (delete-windows-on mail-reply-buffer)
560 (insert-buffer mail-reply-buffer)
561 (if (consp arg)
562 nil
563 (mail-yank-clear-headers start (mark))
564 (if (null mail-yank-prefix)
565 (indent-rigidly start (mark)
566 (if arg (prefix-numeric-value arg) 3))
567 (save-excursion
568 (goto-char start)
569 (while (< (point) (mark))
570 (insert mail-yank-prefix)
571 (forward-line 1)))))
572 (exchange-point-and-mark)
573 (if (not (eolp)) (insert ?\n)))))
574
575 (defun mail-yank-clear-headers (start end)
576 (save-excursion
577 (goto-char start)
578 (if (search-forward "\n\n" end t)
579 (save-restriction
580 (narrow-to-region start (point))
581 (goto-char start)
582 (while (let ((case-fold-search t))
583 (re-search-forward mail-yank-ignored-headers nil t))
584 (beginning-of-line)
585 (delete-region (point)
586 (progn (re-search-forward "\n[^ \t]")
587 (forward-char -1)
588 (point))))))))
589 \f
590 ;; Put these last, to reduce chance of lossage from quitting in middle of loading the file.
591
592 ;;;###autoload
593 (defun mail (&optional noerase to subject in-reply-to cc replybuffer actions)
594 "Edit a message to be sent. Prefix arg means resume editing (don't erase).
595 When this function returns, the buffer `*mail*' is selected.
596 The value is t if the message was newly initialized; otherwise, nil.
597
598 By default, the signature file `~/.signature' is inserted at the end;
599 see the variable `mail-signature'.
600
601 \\<mail-mode-map>
602 While editing message, type \\[mail-send-and-exit] to send the message and exit.
603
604 Various special commands starting with C-c are available in sendmail mode
605 to move to message header fields:
606 \\{mail-mode-map}
607
608 If `mail-self-blind' is non-nil, a BCC to yourself is inserted
609 when the message is initialized.
610
611 If `mail-default-reply-to' is non-nil, it should be an address (a string);
612 a Reply-to: field with that address is inserted.
613
614 If `mail-archive-file-name' is non-nil, an FCC field with that file name
615 is inserted.
616
617 If `mail-setup-hook' is bound, its value is called with no arguments
618 after the message is initialized. It can add more default fields.
619
620 When calling from a program, the second through fifth arguments
621 TO, SUBJECT, IN-REPLY-TO and CC specify if non-nil
622 the initial contents of those header fields.
623 These arguments should not have final newlines.
624 The sixth argument REPLYBUFFER is a buffer whose contents
625 should be yanked if the user types C-c C-y.
626 The seventh argument ACTIONS is a list of actions to take
627 if/when the message is sent. Each action looks like (FUNCTION . ARGS);
628 when the message is sent, we apply FUNCTION to ARGS.
629 This is how Rmail arranges to mark messages `answered'."
630 (interactive "P")
631 ;;; This is commented out because I found it was confusing in practice.
632 ;;; It is easy enough to rename *mail* by hand with rename-buffer
633 ;;; if you want to have multiple mail buffers.
634 ;;; And then you can control which messages to save. --rms.
635 ;;; (let ((index 1)
636 ;;; buffer)
637 ;;; ;; If requested, look for a mail buffer that is modified and go to it.
638 ;;; (if noerase
639 ;;; (progn
640 ;;; (while (and (setq buffer
641 ;;; (get-buffer (if (= 1 index) "*mail*"
642 ;;; (format "*mail*<%d>" index))))
643 ;;; (not (buffer-modified-p buffer)))
644 ;;; (setq index (1+ index)))
645 ;;; (if buffer (switch-to-buffer buffer)
646 ;;; ;; If none exists, start a new message.
647 ;;; ;; This will never re-use an existing unmodified mail buffer
648 ;;; ;; (since index is not 1 anymore). Perhaps it should.
649 ;;; (setq noerase nil))))
650 ;;; ;; Unless we found a modified message and are happy, start a new message.
651 ;;; (if (not noerase)
652 ;;; (progn
653 ;;; ;; Look for existing unmodified mail buffer.
654 ;;; (while (and (setq buffer
655 ;;; (get-buffer (if (= 1 index) "*mail*"
656 ;;; (format "*mail*<%d>" index))))
657 ;;; (buffer-modified-p buffer))
658 ;;; (setq index (1+ index)))
659 ;;; ;; If none, make a new one.
660 ;;; (or buffer
661 ;;; (setq buffer (generate-new-buffer "*mail*")))
662 ;;; ;; Go there and initialize it.
663 ;;; (switch-to-buffer buffer)
664 ;;; (erase-buffer)
665 ;;; (setq default-directory (expand-file-name "~/"))
666 ;;; (auto-save-mode auto-save-default)
667 ;;; (mail-mode)
668 ;;; (mail-setup to subject in-reply-to cc replybuffer actions)
669 ;;; (if (and buffer-auto-save-file-name
670 ;;; (file-exists-p buffer-auto-save-file-name))
671 ;;; (message "Auto save file for draft message exists; consider M-x mail-recover"))
672 ;;; t))
673 (switch-to-buffer "*mail*")
674 (setq default-directory (expand-file-name "~/"))
675 (auto-save-mode auto-save-default)
676 (mail-mode)
677 (let (initialized)
678 (and (not noerase)
679 (or (not (buffer-modified-p))
680 (y-or-n-p "Unsent message being composed; erase it? "))
681 (progn (erase-buffer)
682 (mail-setup to subject in-reply-to cc replybuffer actions)
683 (setq initialized t)))
684 (if (and buffer-auto-save-file-name
685 (file-exists-p buffer-auto-save-file-name))
686 (message "Auto save file for draft message exists; consider M-x mail-recover"))
687 initialized))
688
689 (defun mail-recover ()
690 "Reread contents of current buffer from its last auto-save file."
691 (interactive)
692 (let ((file-name (make-auto-save-file-name)))
693 (cond ((save-window-excursion
694 (if (not (eq system-type 'vax-vms))
695 (with-output-to-temp-buffer "*Directory*"
696 (buffer-disable-undo standard-output)
697 (call-process "ls" nil standard-output nil "-l" file-name)))
698 (yes-or-no-p (format "Recover auto save file %s? " file-name)))
699 (let ((buffer-read-only nil))
700 (erase-buffer)
701 (insert-file-contents file-name nil)))
702 (t (error "mail-recover cancelled.")))))
703
704 ;;;###autoload
705 (defun mail-other-window (&optional noerase to subject in-reply-to cc replybuffer sendactions)
706 "Like `mail' command, but display mail buffer in another window."
707 (interactive "P")
708 (let ((pop-up-windows t))
709 (pop-to-buffer "*mail*"))
710 (mail noerase to subject in-reply-to cc replybuffer sendactions))
711
712 ;;;###autoload
713 (defun mail-other-frame (&optional noerase to subject in-reply-to cc replybuffer sendactions)
714 "Like `mail' command, but display mail buffer in another frame."
715 (interactive "P")
716 (let ((pop-up-frames t))
717 (pop-to-buffer "*mail*"))
718 (mail noerase to subject in-reply-to cc replybuffer sendactions))
719
720
721 ;;;###autoload
722 (define-key ctl-x-map "m" 'mail)
723
724 ;;;###autoload
725 (define-key ctl-x-4-map "m" 'mail-other-window)
726
727 ;;;###autoload
728 (define-key ctl-x-5-map "m" 'mail-other-frame)
729
730
731 ;;; Do not add anything but external entries on this page.
732
733 (provide 'sendmail)
734
735 ;;; sendmail.el ends here