(Frename_buffer): Rename arg NAME to NEWNAME.
[bpt/emacs.git] / lisp / mail / sendmail.el
1 ;;; sendmail.el --- mail sending commands for Emacs.
2
3 ;; Copyright (C) 1985, 1986, 1992, 1993, 1994 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 ;;; Commentary:
25
26 ;; This mode provides mail-sending facilities from within Emacs. It is
27 ;; documented in the Emacs user's manual.
28
29 ;;; Code:
30
31 ;;;###autoload
32 (defvar mail-self-blind nil "\
33 Non-nil means insert BCC to self in messages to be sent.
34 This is done when the message is initialized,
35 so you can remove or alter the BCC field to override the default.")
36
37 ;;;###autoload
38 (defvar mail-interactive nil "\
39 Non-nil means when sending a message wait for and display errors.
40 nil means let mailer mail back a message to report errors.")
41
42 ;;;###autoload
43 (defvar mail-yank-ignored-headers "^via:\\|^mail-from:\\|^origin:\\|^status:\\|^remailed\\|^received:\\|^message-id:\\|^summary-line:\\|^to:\\|^subject:\\|^in-reply-to:\\|^return-path:" "\
44 Delete these headers from old message when it's inserted in a reply.")
45
46 ;; Useful to set in site-init.el
47 ;;;###autoload
48 (defvar send-mail-function 'sendmail-send-it "\
49 Function to call to send the current buffer as mail.
50 The headers are be delimited by a line which is `mail-header-separator'.")
51
52 ;;;###autoload
53 (defvar mail-header-separator "--text follows this line--" "\
54 *Line used to separate headers from text in messages being composed.")
55
56 ;;;###autoload
57 (defvar mail-archive-file-name nil "\
58 *Name of file to write all outgoing messages in, or nil for none.
59 This can be an inbox file or an Rmail file.")
60
61 ;;;###autoload
62 (defvar mail-default-reply-to t
63 "*Address to insert as default Reply-to field of outgoing messages.")
64
65 ;;;###autoload
66 (defvar mail-alias-file nil
67 "*If non-nil, the name of a file to use instead of `/usr/lib/aliases'.
68 This file defines aliases to be expanded by the mailer; this is a different
69 feature from that of defining aliases in `.mailrc' to be expanded in Emacs.
70 This variable has no effect unless your system uses sendmail as its mailer.")
71
72 (defvar mail-aliases t
73 "Alist of mail address aliases,
74 or t meaning should be initialized from your mail aliases file.
75 \(The file's name is normally `~/.mailrc', but your MAILRC environment
76 variable can override that name.)
77 The alias definitions in the file have this form:
78 alias ALIAS MEANING")
79
80 (defvar mail-alias-modtime nil
81 "The modification time of your mail alias file when it was last examined.")
82
83 (defvar mail-yank-prefix nil
84 "*Prefix insert on lines of yanked message being replied to.
85 nil means use indentation.")
86 (defvar mail-indentation-spaces 3
87 "*Number of spaces to insert at the beginning of each cited line.
88 Used by `mail-yank-original' via `mail-yank-cite'.")
89 (defvar mail-yank-hooks nil
90 "Obsolete hook for modifying a citation just inserted in the mail buffer.
91 Each hook function can find the citation between (point) and (mark t).
92 And each hook function should leave point and mark around the citation
93 text as modified.
94
95 This is a normal hook, misnamed for historical reasons.
96 It is semi-obsolete and mail agents should no longer use it.")
97
98 (defvar mail-citation-hook nil
99 "*Hook for modifying a citation just inserted in the mail buffer.
100 Each hook function can find the citation between (point) and (mark t).
101 And each hook function should leave point and mark around the citation
102 text as modified.
103
104 If this hook is entirely empty (nil), a default action is taken
105 instead of no action.")
106
107 (defvar mail-abbrevs-loaded nil)
108 (defvar mail-mode-map nil)
109
110 (autoload 'build-mail-aliases "mailalias"
111 "Read mail aliases from `~/.mailrc' and set `mail-aliases'."
112 nil)
113
114 (autoload 'expand-mail-aliases "mailalias"
115 "Expand all mail aliases in suitable header fields found between BEG and END.
116 Suitable header fields are `To', `Cc' and `Bcc' and their `Resent-' variants.
117 Optional second arg EXCLUDE may be a regular expression defining text to be
118 removed from alias expansions."
119 nil)
120
121 ;;;###autoload
122 (defvar mail-signature nil
123 "*Text inserted at end of mail buffer when a message is initialized.
124 If t, it means to insert the contents of the file `~/.signature'.")
125
126 (defvar mail-reply-buffer nil)
127 (defvar mail-send-actions nil
128 "A list of actions to be performed upon successful sending of a message.")
129
130 (defvar mail-default-headers nil
131 "*A string containing header lines, to be inserted in outgoing messages.
132 It is inserted before you edit the message,
133 so you can edit or delete these lines.")
134
135 (defvar mail-bury-selects-summary t
136 "*If non-nil, try to show RMAIL summary buffer after returning from mail.
137 The functions \\[mail-send-on-exit] or \\[mail-dont-send] select
138 the RMAIL summary buffer before returning, if it exists and this variable
139 is non-nil.")
140
141 ;; Note: could use /usr/ucb/mail instead of sendmail;
142 ;; options -t, and -v if not interactive.
143 (defvar mail-mailer-swallows-blank-line
144 (if (and (string-match "sparc-sun-sunos\\(\\'\\|[^5]\\)" system-configuration)
145 (file-readable-p "/etc/sendmail.cf")
146 (let ((buffer (get-buffer-create " *temp*")))
147 (unwind-protect
148 (save-excursion
149 (set-buffer buffer)
150 (insert-file-contents "/etc/sendmail.cf")
151 (goto-char (point-min))
152 (let ((case-fold-search nil))
153 (re-search-forward "^OR\\>" nil t)))
154 (kill-buffer buffer))))
155 '(looking-at "[ \t]\\|[-a-zA-Z]+:"))
156 "Set this non-nil if the system's mailer runs the header and body together.
157 \(This problem exists on Sunos 4 when sendmail is run in remote mode.)
158 The value should be an expression to test whether the problem will
159 actually occur.")
160
161 (defvar mail-mode-syntax-table nil
162 "Syntax table used while in mail mode.")
163
164 (if (not mail-mode-syntax-table)
165 (progn
166 (setq mail-mode-syntax-table (copy-syntax-table text-mode-syntax-table))
167 (modify-syntax-entry ?% ". " mail-mode-syntax-table)))
168
169 (defvar mail-font-lock-keywords
170 (list '("^To:" . font-lock-function-name-face)
171 '("^B?CC:\\|^Reply-To:" . font-lock-keyword-face)
172 '("^Subject:" . font-lock-comment-face)
173 '("^Subject:\\s *\\(.+\\)$" 1 font-lock-type-face)
174 (list (concat "^\\(" mail-header-separator "\\)$") 1
175 'font-lock-comment-face)
176 '("^[ \t]*\\sw*[>|}].*$" . font-lock-reference-face) ; Citation.
177 '("^\\(X-[A-Za-z0-9-]+\\|In-reply-to\\):.*$" . font-lock-string-face))
178 "Additional expressions to highlight in Mail mode.")
179
180 (defvar mail-send-hook nil
181 "Normal hook run before sending mail, in Mail mode.")
182
183 (defun sendmail-synch-aliases ()
184 (let ((modtime (nth 5 (file-attributes (or (getenv "MAILRC") "~/.mailrc")))))
185 (or (equal mail-alias-modtime modtime)
186 (setq mail-alias-modtime modtime
187 mail-aliases t))))
188
189 (defun mail-setup (to subject in-reply-to cc replybuffer actions)
190 (if (eq mail-default-reply-to t)
191 (setq mail-default-reply-to (getenv "REPLYTO")))
192 (sendmail-synch-aliases)
193 (if (eq mail-aliases t)
194 (progn
195 (setq mail-aliases nil)
196 (if (file-exists-p "~/.mailrc")
197 (build-mail-aliases))))
198 (setq mail-send-actions actions)
199 (setq mail-reply-buffer replybuffer)
200 (goto-char (point-min))
201 (insert "To: ")
202 (save-excursion
203 (if to
204 ;; Here removed code to extract names from within <...>
205 ;; on the assumption that mail-strip-quoted-names
206 ;; has been called and has done so.
207 (let ((fill-prefix "\t")
208 (address-start (point)))
209 (insert to "\n")
210 (fill-region-as-paragraph address-start (point-max)))
211 (newline))
212 (if cc
213 (let ((fill-prefix "\t")
214 (address-start (progn (insert "CC: ") (point))))
215 (insert cc "\n")
216 (fill-region-as-paragraph address-start (point-max))))
217 (if in-reply-to
218 (let ((fill-prefix "\t")
219 (address-start (point)))
220 (insert "In-reply-to: " in-reply-to "\n")
221 (fill-region-as-paragraph address-start (point-max))))
222 (insert "Subject: " (or subject "") "\n")
223 (if mail-default-headers
224 (insert mail-default-headers))
225 (if mail-default-reply-to
226 (insert "Reply-to: " mail-default-reply-to "\n"))
227 (if mail-self-blind
228 (insert "BCC: " (user-login-name) "\n"))
229 (if mail-archive-file-name
230 (insert "FCC: " mail-archive-file-name "\n"))
231 (insert mail-header-separator "\n")
232 ;; Insert the signature. But remember the beginning of the message.
233 (if to (setq to (point)))
234 (cond ((eq mail-signature t)
235 (if (file-exists-p "~/.signature")
236 (progn
237 (insert "\n\n-- \n")
238 (insert-file-contents "~/.signature"))))
239 (mail-signature
240 (insert mail-signature)))
241 (goto-char (point-max))
242 (or (bolp) (newline)))
243 (if to (goto-char to))
244 (or to subject in-reply-to
245 (set-buffer-modified-p nil))
246 (run-hooks 'mail-setup-hook))
247
248 ;;;###autoload
249 (defun mail-mode ()
250 "Major mode for editing mail to be sent.
251 Like Text Mode but with these additional commands:
252 C-c C-s mail-send (send the message) C-c C-c mail-send-and-exit
253 C-c C-f move to a header field (and create it if there isn't):
254 C-c C-f C-t move to To: C-c C-f C-s move to Subj:
255 C-c C-f C-b move to BCC: C-c C-f C-c move to CC:
256 C-c C-f C-f move to FCC:
257 C-c C-t move to message text.
258 C-c C-y mail-yank-original (insert current message, in Rmail).
259 C-c C-q mail-fill-yanked-message (fill what was yanked).
260 C-c C-v mail-sent-via (add a sent-via field for each To or CC)."
261 (interactive)
262 (kill-all-local-variables)
263 (make-local-variable 'mail-reply-buffer)
264 (setq mail-reply-buffer nil)
265 (make-local-variable 'mail-send-actions)
266 (set-syntax-table mail-mode-syntax-table)
267 (use-local-map mail-mode-map)
268 (setq local-abbrev-table text-mode-abbrev-table)
269 (setq major-mode 'mail-mode)
270 (setq mode-name "Mail")
271 (setq buffer-offer-save t)
272 (make-local-variable 'font-lock-defaults)
273 (setq font-lock-defaults '(mail-font-lock-keywords t))
274 (make-local-variable 'paragraph-separate)
275 (make-local-variable 'paragraph-start)
276 (setq paragraph-start (concat "^" mail-header-separator
277 "$\\|^[ \t]*[-_][-_][-_]+$\\|"
278 paragraph-start))
279 (setq paragraph-separate (concat "^" mail-header-separator
280 "$\\|^[ \t]*[-_][-_][-_]+$\\|"
281 paragraph-separate))
282 (run-hooks 'text-mode-hook 'mail-mode-hook))
283 \f
284 ;;; Set up keymap.
285
286 (if mail-mode-map
287 nil
288 (setq mail-mode-map (nconc (make-sparse-keymap) text-mode-map))
289 (define-key mail-mode-map "\C-c?" 'describe-mode)
290 (define-key mail-mode-map "\C-c\C-f\C-t" 'mail-to)
291 (define-key mail-mode-map "\C-c\C-f\C-b" 'mail-bcc)
292 (define-key mail-mode-map "\C-c\C-f\C-f" 'mail-fcc)
293 (define-key mail-mode-map "\C-c\C-f\C-c" 'mail-cc)
294 (define-key mail-mode-map "\C-c\C-f\C-s" 'mail-subject)
295 (define-key mail-mode-map "\C-c\C-t" 'mail-text)
296 (define-key mail-mode-map "\C-c\C-y" 'mail-yank-original)
297 (define-key mail-mode-map "\C-c\C-q" 'mail-fill-yanked-message)
298 (define-key mail-mode-map "\C-c\C-w" 'mail-signature)
299 (define-key mail-mode-map "\C-c\C-v" 'mail-sent-via)
300 (define-key mail-mode-map "\C-c\C-c" 'mail-send-and-exit)
301 (define-key mail-mode-map "\C-c\C-s" 'mail-send))
302
303 (define-key mail-mode-map [menu-bar mail]
304 (cons "Mail" (make-sparse-keymap "Mail")))
305
306 (define-key mail-mode-map [menu-bar mail fill]
307 '("Fill Citation" . mail-fill-yanked-message))
308
309 (define-key mail-mode-map [menu-bar mail yank]
310 '("Cite Original" . mail-yank-original))
311
312 (define-key mail-mode-map [menu-bar mail signature]
313 '("Insert Signature" . mail-signature))
314
315 (define-key mail-mode-map [menu-bar mail cancel]
316 '("Cancel" . mail-dont-send))
317
318 (define-key mail-mode-map [menu-bar mail send-stay]
319 '("Send, Keep Editing" . mail-send))
320
321 (define-key mail-mode-map [menu-bar mail send]
322 '("Send Message" . mail-send-and-exit))
323
324 (define-key mail-mode-map [menu-bar headers]
325 (cons "Headers" (make-sparse-keymap "Headers")))
326
327 (define-key mail-mode-map [menu-bar headers sent-via]
328 '("Sent Via" . mail-sent-via))
329
330 (define-key mail-mode-map [menu-bar headers text]
331 '("Text" . mail-text))
332
333 (define-key mail-mode-map [menu-bar headers bcc]
334 '("Bcc" . mail-bcc))
335
336 (define-key mail-mode-map [menu-bar headers fcc]
337 '("Fcc" . mail-fcc))
338
339 (define-key mail-mode-map [menu-bar headers cc]
340 '("Cc" . mail-cc))
341
342 (define-key mail-mode-map [menu-bar headers subject]
343 '("Subject" . mail-subject))
344
345 (define-key mail-mode-map [menu-bar headers to]
346 '("To" . mail-to))
347 \f
348 (defun mail-send-and-exit (arg)
349 "Send message like `mail-send', then, if no errors, exit from mail buffer.
350 Prefix arg means don't delete this window."
351 (interactive "P")
352 (mail-send)
353 (mail-bury arg))
354
355 (defun mail-dont-send (arg)
356 "Don't send the message you have been editing.
357 Prefix arg means don't delete this window."
358 (interactive "P")
359 (mail-bury arg))
360
361 (defun mail-bury (arg)
362 "Bury this mail buffer."
363 (let ((newbuf (other-buffer (current-buffer))))
364 (bury-buffer (current-buffer))
365 (if (and (fboundp 'frame-parameters)
366 (cdr (assq 'dedicated (frame-parameters)))
367 (not (null (delq (selected-frame) (visible-frame-list)))))
368 (delete-frame (selected-frame))
369 (let (rmail-flag summary-buffer)
370 (and (not arg)
371 (not (one-window-p))
372 (save-excursion
373 (set-buffer (window-buffer (next-window (selected-window) 'not)))
374 (setq rmail-flag (eq major-mode 'rmail-mode))
375 (setq summary-buffer
376 (and mail-bury-selects-summary
377 (boundp 'rmail-summary-buffer)
378 rmail-summary-buffer
379 (buffer-name rmail-summary-buffer)
380 (not (get-buffer-window rmail-summary-buffer))
381 rmail-summary-buffer))))
382 (if rmail-flag
383 ;; If the Rmail buffer has a summary, show that.
384 (if summary-buffer (switch-to-buffer summary-buffer)
385 (delete-window))
386 (switch-to-buffer newbuf))))))
387
388 (defun mail-send ()
389 "Send the message in the current buffer.
390 If `mail-interactive' is non-nil, wait for success indication
391 or error messages, and inform user.
392 Otherwise any failure is reported in a message back to
393 the user from the mailer."
394 (interactive)
395 (if (if buffer-file-name
396 (y-or-n-p "Send buffer contents as mail message? ")
397 (or (buffer-modified-p)
398 (y-or-n-p "Message already sent; resend? ")))
399 (progn
400 (run-hooks 'mail-send-hook)
401 (message "Sending...")
402 (funcall send-mail-function)
403 ;; Now perform actions on successful sending.
404 (while mail-send-actions
405 (condition-case nil
406 (apply (car (car mail-send-actions))
407 (cdr (car mail-send-actions)))
408 (error))
409 (setq mail-send-actions (cdr mail-send-actions)))
410 (message "Sending...done")
411 ;; If buffer has no file, mark it as unmodified and delete autosave.
412 (if (not buffer-file-name)
413 (progn
414 (set-buffer-modified-p nil)
415 (delete-auto-save-file-if-necessary t))))))
416
417 (defun sendmail-send-it ()
418 (let ((errbuf (if mail-interactive
419 (generate-new-buffer " sendmail errors")
420 0))
421 (tembuf (generate-new-buffer " sendmail temp"))
422 (case-fold-search nil)
423 resend-to-addresses
424 delimline
425 (mailbuf (current-buffer)))
426 (unwind-protect
427 (save-excursion
428 (set-buffer tembuf)
429 (erase-buffer)
430 (insert-buffer-substring mailbuf)
431 (goto-char (point-max))
432 ;; require one newline at the end.
433 (or (= (preceding-char) ?\n)
434 (insert ?\n))
435 ;; Change header-delimiter to be what sendmail expects.
436 (goto-char (point-min))
437 (re-search-forward
438 (concat "^" (regexp-quote mail-header-separator) "\n"))
439 (replace-match "\n")
440 (backward-char 1)
441 (setq delimline (point-marker))
442 (sendmail-synch-aliases)
443 (if mail-aliases
444 (expand-mail-aliases (point-min) delimline))
445 (goto-char (point-min))
446 ;; ignore any blank lines in the header
447 (while (and (re-search-forward "\n\n\n*" delimline t)
448 (< (point) delimline))
449 (replace-match "\n"))
450 (let ((case-fold-search t))
451 (goto-char (point-min))
452 ;; Find and handle any FCC fields.
453 (goto-char (point-min))
454 (if (re-search-forward "^FCC:" delimline t)
455 (mail-do-fcc delimline))
456 (goto-char (point-min))
457 (require 'mail-utils)
458 (while (re-search-forward "^Resent-to:" delimline t)
459 (setq resend-to-addresses
460 (save-restriction
461 (narrow-to-region (point)
462 (save-excursion
463 (end-of-line)
464 (point)))
465 (append (mail-parse-comma-list)
466 resend-to-addresses))))
467 ;;; Apparently this causes a duplicate Sender.
468 ;;; ;; If the From is different than current user, insert Sender.
469 ;;; (goto-char (point-min))
470 ;;; (and (re-search-forward "^From:" delimline t)
471 ;;; (progn
472 ;;; (require 'mail-utils)
473 ;;; (not (string-equal
474 ;;; (mail-strip-quoted-names
475 ;;; (save-restriction
476 ;;; (narrow-to-region (point-min) delimline)
477 ;;; (mail-fetch-field "From")))
478 ;;; (user-login-name))))
479 ;;; (progn
480 ;;; (forward-line 1)
481 ;;; (insert "Sender: " (user-login-name) "\n")))
482 ;; "S:" is an abbreviation for "Subject:".
483 (goto-char (point-min))
484 (if (re-search-forward "^S:" delimline t)
485 (replace-match "Subject:"))
486 ;; Don't send out a blank subject line
487 (goto-char (point-min))
488 (if (re-search-forward "^Subject:[ \t]*\n" delimline t)
489 (replace-match ""))
490 ;; Insert an extra newline if we need it to work around
491 ;; Sun's bug that swallows newlines.
492 (goto-char (1+ delimline))
493 (if (eval mail-mailer-swallows-blank-line)
494 (newline))
495 (if mail-interactive
496 (save-excursion
497 (set-buffer errbuf)
498 (erase-buffer))))
499 (apply 'call-process-region
500 (append (list (point-min) (point-max)
501 (if (boundp 'sendmail-program)
502 sendmail-program
503 "/usr/lib/sendmail")
504 nil errbuf nil "-oi")
505 ;; Always specify who from,
506 ;; since some systems have broken sendmails.
507 (list "-f" (user-login-name))
508 ;;; ;; Don't say "from root" if running under su.
509 ;;; (and (equal (user-real-login-name) "root")
510 ;;; (list "-f" (user-login-name)))
511 (and mail-alias-file
512 (list (concat "-oA" mail-alias-file)))
513 ;; These mean "report errors by mail"
514 ;; and "deliver in background".
515 (if (null mail-interactive) '("-oem" "-odb"))
516 ;; Get the addresses from the message
517 ;; unless this is a resend.
518 ;; We must not do that for a resend
519 ;; because we would find the original addresses.
520 ;; For a resend, include the specific addresses.
521 (or resend-to-addresses
522 '("-t"))))
523 (if mail-interactive
524 (save-excursion
525 (set-buffer errbuf)
526 (goto-char (point-min))
527 (while (re-search-forward "\n\n* *" nil t)
528 (replace-match "; "))
529 (if (not (zerop (buffer-size)))
530 (error "Sending...failed to %s"
531 (buffer-substring (point-min) (point-max)))))))
532 (kill-buffer tembuf)
533 (if (bufferp errbuf)
534 (kill-buffer errbuf)))))
535
536 ;; Return non-nil if file FILE is an Rmail file.
537 (defun mail-file-babyl-p (file)
538 (unwind-protect
539 (save-excursion
540 (set-buffer (get-buffer-create " mail-temp"))
541 (erase-buffer)
542 (insert-file-contents file nil 0 20)
543 (looking-at "BABYL OPTIONS:"))
544 (kill-buffer " mail-temp")))
545
546 (defun mail-do-fcc (header-end)
547 (let (fcc-list
548 (rmailbuf (current-buffer))
549 (time (current-time))
550 (tembuf (generate-new-buffer " rmail output"))
551 (case-fold-search t))
552 (save-excursion
553 (goto-char (point-min))
554 (while (re-search-forward "^FCC:[ \t]*" header-end t)
555 (setq fcc-list (cons (buffer-substring (point)
556 (progn
557 (end-of-line)
558 (skip-chars-backward " \t")
559 (point)))
560 fcc-list))
561 (delete-region (match-beginning 0)
562 (progn (forward-line 1) (point))))
563 (set-buffer tembuf)
564 (erase-buffer)
565 ;; This initial newline is written out if the fcc file already exists.
566 (insert "\nFrom " (user-login-name) " "
567 (current-time-string time) "\n")
568 ;; Insert the time zone before the year.
569 (forward-char -1)
570 (forward-word -1)
571 (require 'mail-utils)
572 (insert (mail-rfc822-time-zone time) " ")
573 (goto-char (point-max))
574 (insert-buffer-substring rmailbuf)
575 ;; Make sure messages are separated.
576 (goto-char (point-max))
577 (insert ?\n)
578 (goto-char 2)
579 ;; ``Quote'' "^From " as ">From "
580 ;; (note that this isn't really quoting, as there is no requirement
581 ;; that "^[>]+From " be quoted in the same transparent way.)
582 (let ((case-fold-search nil))
583 (while (search-forward "\nFrom " nil t)
584 (forward-char -5)
585 (insert ?>)))
586 (while fcc-list
587 (let ((buffer (get-file-buffer (car fcc-list)))
588 (curbuf (current-buffer))
589 (beg (point-min)) (end (point-max))
590 (beg2 (save-excursion (goto-char (point-min))
591 (forward-line 2) (point))))
592 (if buffer
593 ;; File is present in a buffer => append to that buffer.
594 (save-excursion
595 (set-buffer buffer)
596 ;; Keep the end of the accessible portion at the same place
597 ;; unless it is the end of the buffer.
598 (let ((max (if (/= (1+ (buffer-size)) (point-max))
599 (point-max))))
600 (unwind-protect
601 ;; Code below lifted from rmailout.el
602 ;; function rmail-output-to-rmail-file:
603 (let ((buffer-read-only nil)
604 (msg (and (boundp 'rmail-current-message)
605 rmail-current-message)))
606 ;; If MSG is non-nil, buffer is in RMAIL mode.
607 (if msg
608 (progn
609 (rmail-maybe-set-message-counters)
610 (widen)
611 (narrow-to-region (point-max) (point-max))
612 (insert "\C-l\n0, unseen,,\n*** EOOH ***\n"
613 "From: " (user-login-name) "\n"
614 "Date: " (mail-rfc822-date) "\n")
615 (insert-buffer-substring curbuf beg2 end)
616 (insert "\n\C-_")
617 (goto-char (point-min))
618 (widen)
619 (search-backward "\n\^_")
620 (narrow-to-region (point) (point-max))
621 (rmail-count-new-messages t)
622 (rmail-show-message msg)
623 (setq max nil))
624 ;; Output file not in rmail mode
625 ;; => just insert at the end.
626 (narrow-to-region (point-min) (1+ (buffer-size)))
627 (goto-char (point-max))
628 (insert-buffer-substring curbuf beg end)))
629 (if max (narrow-to-region (point-min) max)))))
630 ;; Else append to the file directly.
631 (if (and (file-exists-p (car fcc-list))
632 (mail-file-babyl-p (car fcc-list)))
633 ;; If the file is a Babyl file,
634 ;; convert the message to Babyl format.
635 (save-excursion
636 (set-buffer (get-buffer-create " mail-temp"))
637 (insert "\C-l\n0, unseen,,\n*** EOOH ***\n"
638 "From: " (user-login-name) "\n"
639 "Date: " (mail-rfc822-date) "\n")
640 (insert-buffer-substring curbuf beg2 end)
641 (insert "\n\C-_")
642 (write-region (point-min) (point-max) (car fcc-list) t))
643 (write-region
644 (1+ (point-min)) (point-max) (car fcc-list) t))))
645 (setq fcc-list (cdr fcc-list))))
646 (kill-buffer tembuf)))
647
648 (defun mail-sent-via ()
649 "Make a Sent-via header line from each To or CC header line."
650 (interactive)
651 (save-excursion
652 (goto-char (point-min))
653 ;; find the header-separator
654 (search-forward (concat "\n" mail-header-separator "\n"))
655 (forward-line -1)
656 ;; put a marker at the end of the header
657 (let ((end (point-marker))
658 (case-fold-search t)
659 to-line)
660 (goto-char (point-min))
661 ;; search for the To: lines and make Sent-via: lines from them
662 ;; search for the next To: line
663 (while (re-search-forward "^\\(to\\|cc\\):" end t)
664 ;; Grab this line plus all its continuations, sans the `to:'.
665 (let ((to-line
666 (buffer-substring (point)
667 (progn
668 (if (re-search-forward "^[^ \t\n]" end t)
669 (backward-char 1)
670 (goto-char end))
671 (point)))))
672 ;; Insert a copy, with altered header field name.
673 (insert-before-markers "Sent-via:" to-line))))))
674 \f
675 (defun mail-to ()
676 "Move point to end of To-field."
677 (interactive)
678 (expand-abbrev)
679 (mail-position-on-field "To"))
680
681 (defun mail-subject ()
682 "Move point to end of Subject-field."
683 (interactive)
684 (expand-abbrev)
685 (mail-position-on-field "Subject"))
686
687 (defun mail-cc ()
688 "Move point to end of CC-field. Create a CC field if none."
689 (interactive)
690 (expand-abbrev)
691 (or (mail-position-on-field "cc" t)
692 (progn (mail-position-on-field "to")
693 (insert "\nCC: "))))
694
695 (defun mail-bcc ()
696 "Move point to end of BCC-field. Create a BCC field if none."
697 (interactive)
698 (expand-abbrev)
699 (or (mail-position-on-field "bcc" t)
700 (progn (mail-position-on-field "to")
701 (insert "\nBCC: "))))
702
703 (defun mail-fcc (folder)
704 "Add a new FCC field, with file name completion."
705 (interactive "FFolder carbon copy: ")
706 (expand-abbrev)
707 (or (mail-position-on-field "fcc" t) ;Put new field after exiting FCC.
708 (mail-position-on-field "to"))
709 (insert "\nFCC: " folder))
710
711 (defun mail-position-on-field (field &optional soft)
712 (let (end
713 (case-fold-search t))
714 (goto-char (point-min))
715 (re-search-forward (concat "^" (regexp-quote mail-header-separator) "\n"))
716 (setq end (match-beginning 0))
717 (goto-char (point-min))
718 (if (re-search-forward (concat "^" (regexp-quote field) ":") end t)
719 (progn
720 (re-search-forward "^[^ \t]" nil 'move)
721 (beginning-of-line)
722 (skip-chars-backward "\n")
723 t)
724 (or soft
725 (progn (goto-char end)
726 (insert field ": \n")
727 (skip-chars-backward "\n")))
728 nil)))
729
730 (defun mail-text ()
731 "Move point to beginning of text field."
732 (interactive)
733 (goto-char (point-min))
734 (search-forward (concat "\n" mail-header-separator "\n")))
735 \f
736 (defun mail-signature (atpoint)
737 "Sign letter with contents of the file `~/.signature'.
738 Prefix arg means put contents at point."
739 (interactive "P")
740 (save-excursion
741 (or atpoint
742 (goto-char (point-max)))
743 (skip-chars-backward " \t\n")
744 (end-of-line)
745 (or atpoint
746 (delete-region (point) (point-max)))
747 (insert "\n\n-- \n")
748 (insert-file-contents (expand-file-name "~/.signature"))))
749
750 (defun mail-fill-yanked-message (&optional justifyp)
751 "Fill the paragraphs of a message yanked into this one.
752 Numeric argument means justify as well."
753 (interactive "P")
754 (save-excursion
755 (goto-char (point-min))
756 (search-forward (concat "\n" mail-header-separator "\n") nil t)
757 (fill-individual-paragraphs (point)
758 (point-max)
759 justifyp
760 t)))
761
762 (defun mail-indent-citation ()
763 "Modify text just inserted from a message to be cited.
764 The inserted text should be the region.
765 When this function returns, the region is again around the modified text.
766
767 Normally, indent each nonblank line `mail-indentation-spaces' spaces.
768 However, if `mail-yank-prefix' is non-nil, insert that prefix on each line."
769 (let ((start (point)))
770 (mail-yank-clear-headers start (mark t))
771 (if (null mail-yank-prefix)
772 (indent-rigidly start (mark t) mail-indentation-spaces)
773 (save-excursion
774 (goto-char start)
775 (while (< (point) (mark t))
776 (insert mail-yank-prefix)
777 (forward-line 1))))))
778
779 (defun mail-yank-original (arg)
780 "Insert the message being replied to, if any (in rmail).
781 Puts point before the text and mark after.
782 Normally, indents each nonblank line ARG spaces (default 3).
783 However, if `mail-yank-prefix' is non-nil, insert that prefix on each line.
784
785 Just \\[universal-argument] as argument means don't indent, insert no prefix,
786 and don't delete any header fields."
787 (interactive "P")
788 (if mail-reply-buffer
789 (let ((start (point)))
790 ;; If the original message is in another window in the same frame,
791 ;; delete that window to save screen space.
792 ;; t means don't alter other frames.
793 (delete-windows-on mail-reply-buffer t)
794 (insert-buffer mail-reply-buffer)
795 (if (consp arg)
796 nil
797 (goto-char start)
798 (let ((mail-indentation-spaces (if arg (prefix-numeric-value arg)
799 mail-indentation-spaces)))
800 (if mail-citation-hook
801 (run-hooks 'mail-citation-hook)
802 (if mail-yank-hooks
803 (run-hooks 'mail-yank-hooks)
804 (mail-indent-citation)))))
805 ;; This is like exchange-point-and-mark, but doesn't activate the mark.
806 ;; It is cleaner to avoid activation, even though the command
807 ;; loop would deactivate the mark because we inserted text.
808 (goto-char (prog1 (mark t)
809 (set-marker (mark-marker) (point) (current-buffer))))
810 (if (not (eolp)) (insert ?\n)))))
811
812 (defun mail-yank-clear-headers (start end)
813 (save-excursion
814 (goto-char start)
815 (if (search-forward "\n\n" end t)
816 (save-restriction
817 (narrow-to-region start (point))
818 (goto-char start)
819 (while (let ((case-fold-search t))
820 (re-search-forward mail-yank-ignored-headers nil t))
821 (beginning-of-line)
822 (delete-region (point)
823 (progn (re-search-forward "\n[^ \t]")
824 (forward-char -1)
825 (point))))))))
826 \f
827 ;; Put these last, to reduce chance of lossage from quitting in middle of loading the file.
828
829 ;;;###autoload
830 (defun mail (&optional noerase to subject in-reply-to cc replybuffer actions)
831 "Edit a message to be sent. Prefix arg means resume editing (don't erase).
832 When this function returns, the buffer `*mail*' is selected.
833 The value is t if the message was newly initialized; otherwise, nil.
834
835 By default, the signature file `~/.signature' is inserted at the end;
836 see the variable `mail-signature'.
837
838 \\<mail-mode-map>
839 While editing message, type \\[mail-send-and-exit] to send the message and exit.
840
841 Various special commands starting with C-c are available in sendmail mode
842 to move to message header fields:
843 \\{mail-mode-map}
844
845 If `mail-self-blind' is non-nil, a BCC to yourself is inserted
846 when the message is initialized.
847
848 If `mail-default-reply-to' is non-nil, it should be an address (a string);
849 a Reply-to: field with that address is inserted.
850
851 If `mail-archive-file-name' is non-nil, an FCC field with that file name
852 is inserted.
853
854 If `mail-setup-hook' is bound, its value is called with no arguments
855 after the message is initialized. It can add more default fields.
856
857 When calling from a program, the first argument if non-nil says
858 not to erase the existing contents of the `*mail*' buffer.
859
860 The second through fifth arguments,
861 TO, SUBJECT, IN-REPLY-TO and CC, specify if non-nil
862 the initial contents of those header fields.
863 These arguments should not have final newlines.
864 The sixth argument REPLYBUFFER is a buffer whose contents
865 should be yanked if the user types C-c C-y.
866 The seventh argument ACTIONS is a list of actions to take
867 if/when the message is sent. Each action looks like (FUNCTION . ARGS);
868 when the message is sent, we apply FUNCTION to ARGS.
869 This is how Rmail arranges to mark messages `answered'."
870 (interactive "P")
871 ;;; This is commented out because I found it was confusing in practice.
872 ;;; It is easy enough to rename *mail* by hand with rename-buffer
873 ;;; if you want to have multiple mail buffers.
874 ;;; And then you can control which messages to save. --rms.
875 ;;; (let ((index 1)
876 ;;; buffer)
877 ;;; ;; If requested, look for a mail buffer that is modified and go to it.
878 ;;; (if noerase
879 ;;; (progn
880 ;;; (while (and (setq buffer
881 ;;; (get-buffer (if (= 1 index) "*mail*"
882 ;;; (format "*mail*<%d>" index))))
883 ;;; (not (buffer-modified-p buffer)))
884 ;;; (setq index (1+ index)))
885 ;;; (if buffer (switch-to-buffer buffer)
886 ;;; ;; If none exists, start a new message.
887 ;;; ;; This will never re-use an existing unmodified mail buffer
888 ;;; ;; (since index is not 1 anymore). Perhaps it should.
889 ;;; (setq noerase nil))))
890 ;;; ;; Unless we found a modified message and are happy, start a new message.
891 ;;; (if (not noerase)
892 ;;; (progn
893 ;;; ;; Look for existing unmodified mail buffer.
894 ;;; (while (and (setq buffer
895 ;;; (get-buffer (if (= 1 index) "*mail*"
896 ;;; (format "*mail*<%d>" index))))
897 ;;; (buffer-modified-p buffer))
898 ;;; (setq index (1+ index)))
899 ;;; ;; If none, make a new one.
900 ;;; (or buffer
901 ;;; (setq buffer (generate-new-buffer "*mail*")))
902 ;;; ;; Go there and initialize it.
903 ;;; (switch-to-buffer buffer)
904 ;;; (erase-buffer)
905 ;;; (setq default-directory (expand-file-name "~/"))
906 ;;; (auto-save-mode auto-save-default)
907 ;;; (mail-mode)
908 ;;; (mail-setup to subject in-reply-to cc replybuffer actions)
909 ;;; (if (and buffer-auto-save-file-name
910 ;;; (file-exists-p buffer-auto-save-file-name))
911 ;;; (message "Auto save file for draft message exists; consider M-x mail-recover"))
912 ;;; t))
913 (switch-to-buffer "*mail*")
914 (if (file-exists-p (expand-file-name "~/"))
915 (setq default-directory (expand-file-name "~/")))
916 (auto-save-mode auto-save-default)
917 (mail-mode)
918 ;; Disconnect the buffer from its visited file
919 ;; (in case the user has actually visited a file *mail*).
920 ; (set-visited-file-name nil)
921 (let (initialized)
922 (and (not noerase)
923 (or (not (buffer-modified-p))
924 (y-or-n-p "Unsent message being composed; erase it? "))
925 (progn (erase-buffer)
926 (mail-setup to subject in-reply-to cc replybuffer actions)
927 (setq initialized t)))
928 (if (and buffer-auto-save-file-name
929 (file-exists-p buffer-auto-save-file-name))
930 (message "Auto save file for draft message exists; consider M-x mail-recover"))
931 initialized))
932
933 (defun mail-recover ()
934 "Reread contents of current buffer from its last auto-save file."
935 (interactive)
936 (let ((file-name (make-auto-save-file-name)))
937 (cond ((save-window-excursion
938 (if (not (eq system-type 'vax-vms))
939 (with-output-to-temp-buffer "*Directory*"
940 (buffer-disable-undo standard-output)
941 (call-process "ls" nil standard-output nil "-l" file-name)))
942 (yes-or-no-p (format "Recover auto save file %s? " file-name)))
943 (let ((buffer-read-only nil))
944 (erase-buffer)
945 (insert-file-contents file-name nil)))
946 (t (error "mail-recover cancelled")))))
947
948 ;;;###autoload
949 (defun mail-other-window (&optional noerase to subject in-reply-to cc replybuffer sendactions)
950 "Like `mail' command, but display mail buffer in another window."
951 (interactive "P")
952 (let ((pop-up-windows t))
953 (pop-to-buffer "*mail*"))
954 (mail noerase to subject in-reply-to cc replybuffer sendactions))
955
956 ;;;###autoload
957 (defun mail-other-frame (&optional noerase to subject in-reply-to cc replybuffer sendactions)
958 "Like `mail' command, but display mail buffer in another frame."
959 (interactive "P")
960 (let ((pop-up-frames t))
961 (pop-to-buffer "*mail*"))
962 (mail noerase to subject in-reply-to cc replybuffer sendactions))
963
964 ;;; Do not execute these when sendmail.el is loaded,
965 ;;; only in loaddefs.el.
966 ;;;###autoload (define-key ctl-x-map "m" 'mail)
967 ;;;###autoload (define-key ctl-x-4-map "m" 'mail-other-window)
968 ;;;###autoload (define-key ctl-x-5-map "m" 'mail-other-frame)
969
970 ;;; Do not add anything but external entries on this page.
971
972 (provide 'sendmail)
973
974 ;;; sendmail.el ends here