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