remove trailing slashes from srcdir
[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 Do not use an rmail file here! Instead, use its inbox file.")
60
61 ;;;###autoload
62 (defvar mail-default-reply-to nil
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 `~/.mailrc'.
75 The alias definitions in `~/.mailrc' have this form:
76 alias ALIAS MEANING")
77
78 (defvar mail-alias-modtime nil
79 "The modification time of ~/.mailrc when it was last examined.")
80
81 (defvar mail-yank-prefix nil
82 "*Prefix insert on lines of yanked message being replied to.
83 nil means use indentation.")
84 (defvar mail-indentation-spaces 3
85 "*Number of spaces to insert at the beginning of each cited line.
86 Used by `mail-yank-original' via `mail-yank-cite'.")
87 (defvar mail-yank-hooks nil
88 "Obsolete hook for modifying a citation just inserted in the mail buffer.
89 Each hook function can find the citation between (point) and (mark t).
90 And each hook function should leave point and mark around the citation
91 text as modified.
92
93 This is a normal hook, misnamed for historical reasons.
94 It is semi-obsolete and mail agents should no longer use it.")
95
96 (defvar mail-citation-hook nil
97 "*Hook for modifying a citation just inserted in the mail buffer.
98 Each hook function can find the citation between (point) and (mark t).
99 And each hook function should leave point and mark around the citation
100 text as modified.
101
102 If this hook is entirely empty (nil), a default action is taken
103 instead of no action.")
104
105 (defvar mail-abbrevs-loaded nil)
106 (defvar mail-mode-map nil)
107
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.
114 Suitable header fields are `To', `Cc' and `Bcc' and their `Resent-' variants.
115 Optional second arg EXCLUDE may be a regular expression defining text to be
116 removed from alias expansions."
117 nil)
118
119 ;;;###autoload
120 (defvar mail-signature nil
121 "*Text inserted at end of mail buffer when a message is initialized.
122 If t, it means to insert the contents of the file `~/.signature'.")
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.
130 It is inserted before you edit the message,
131 so you can edit or delete these lines.")
132
133 (defvar mail-bury-selects-summary t
134 "*If non-nil, try to show RMAIL summary buffer after returning from mail.
135 The functions \\[mail-send-on-exit] or \\[mail-dont-send] select
136 the RMAIL summary buffer before returning, if it exists and this variable
137 is non-nil.")
138
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
142 (if (and (string-match "sparc-sun-sunos\\(\\'\\|[^5]\\)" system-configuration)
143 (file-readable-p "/etc/sendmail.cf")
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))
151 (re-search-forward "^OR\\>" nil t)))
152 (kill-buffer buffer))))
153 '(looking-at "[ \t]\\|[-a-zA-Z]+:"))
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.)
156 The value should be an expression to test whether the problem will
157 actually occur.")
158
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
167 (defvar mail-send-hook nil
168 "Normal hook run before sending mail, in Mail mode.")
169
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
176 (defun mail-setup (to subject in-reply-to cc replybuffer actions)
177 (synch-mail-aliases)
178 (if (eq mail-aliases t)
179 (progn
180 (setq mail-aliases nil)
181 (if (file-exists-p "~/.mailrc")
182 (build-mail-aliases))))
183 (setq mail-send-actions actions)
184 (setq mail-reply-buffer replybuffer)
185 (goto-char (point-min))
186 (insert "To: ")
187 (save-excursion
188 (if to
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)))
194 (insert to "\n")
195 (fill-region-as-paragraph address-start (point-max)))
196 (newline))
197 (if cc
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))))
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"))
213 (insert mail-header-separator "\n")
214 ;; Insert the signature. But remember the beginning of the message.
215 (if to (setq to (point)))
216 (cond ((eq mail-signature t)
217 (if (file-exists-p "~/.signature")
218 (progn
219 (insert "\n\n-- \n")
220 (insert-file-contents "~/.signature"))))
221 (mail-signature
222 (insert mail-signature)))
223 (goto-char (point-max))
224 (or (bolp) (newline)))
225 (if to (goto-char to))
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.
233 Like Text Mode but with these additional commands:
234 C-c C-s mail-send (send the message) C-c C-c mail-send-and-exit
235 C-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:
238 C-c C-f C-f move to FCC:
239 C-c C-t move to message text.
240 C-c C-y mail-yank-original (insert current message, in Rmail).
241 C-c C-q mail-fill-yanked-message (fill what was yanked).
242 C-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))
263 \f
264 ;;; Set up keymap.
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)
272 (define-key mail-mode-map "\C-c\C-f\C-f" 'mail-fcc)
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))
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
295 (define-key mail-mode-map [menu-bar mail cancel]
296 '("Cancel" . mail-dont-send))
297
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))
327 \f
328 (defun mail-send-and-exit (arg)
329 "Send message like `mail-send', then, if no errors, exit from mail buffer.
330 Prefix arg means don't delete this window."
331 (interactive "P")
332 (mail-send)
333 (mail-bury arg))
334
335 (defun mail-dont-send (arg)
336 "Don't send the message you have been editing.
337 Prefix 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."
343 (let ((newbuf (other-buffer (current-buffer))))
344 (bury-buffer (current-buffer))
345 (if (and (fboundp 'frame-parameters)
346 (cdr (assq 'dedicated (frame-parameters)))
347 (not (null (delq (selected-frame) (visible-frame-list)))))
348 (delete-frame (selected-frame))
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
356 (and mail-bury-selects-summary
357 (boundp 'rmail-summary-buffer)
358 rmail-summary-buffer
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))))))
367
368 (defun mail-send ()
369 "Send the message in the current buffer.
370 If `mail-interactive' is non-nil, wait for success indication
371 or error messages, and inform user.
372 Otherwise any failure is reported in a message back to
373 the user from the mailer."
374 (interactive)
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? ")))
379 (progn
380 (run-hooks 'mail-send-hook)
381 (message "Sending...")
382 (funcall send-mail-function)
383 ;; Now perform actions on successful sending.
384 (while mail-send-actions
385 (condition-case nil
386 (apply (car (car mail-send-actions))
387 (cdr (car mail-send-actions)))
388 (error))
389 (setq mail-send-actions (cdr mail-send-actions)))
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))))))
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)
403 resend-to-addresses
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))
422 (synch-mail-aliases)
423 (if mail-aliases
424 (expand-mail-aliases (point-min) delimline))
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))
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))
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))))
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")))
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 ""))
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))
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")
484 nil errbuf nil "-oi")
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".
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"))))
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))
519 (time (current-time))
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)
535 ;; This initial newline is written out if the fcc file already exists.
536 (insert "\nFrom " (user-login-name) " "
537 (current-time-string time) "\n")
538 ;; Insert the time zone before the year.
539 (forward-char -1)
540 (forward-word -1)
541 (require 'mail-utils)
542 (insert (mail-rfc822-time-zone time) " ")
543 (goto-char (point-max))
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))
561 (beg (point-min)) (end (point-max))
562 (beg2 (save-excursion (goto-char (point-min))
563 (forward-line 2) (point))))
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
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))
582 (insert "\C-l\n0, unseen,,\n*** EOOH ***\n"
583 "From: " (user-login-name) "\n"
584 "Date: " (mail-rfc822-date) "\n")
585 (insert-buffer-substring curbuf beg2 end)
586 (insert "\n\C-_")
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))
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
602 (1+ (point-min)) (point-max) (car fcc-list) t)))
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
661 (defun mail-fcc (folder)
662 "Add a new FCC field, with file name completion."
663 (interactive "FFolder carbon copy: ")
664 (expand-abbrev)
665 (or (mail-position-on-field "fcc" t) ;Put new field after exiting FCC.
666 (mail-position-on-field "to"))
667 (insert "\nFCC: " folder))
668
669 (defun mail-position-on-field (field &optional soft)
670 (let (end
671 (case-fold-search t))
672 (goto-char (point-min))
673 (re-search-forward (concat "^" (regexp-quote mail-header-separator) "\n"))
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)
684 (insert field ": \n")
685 (skip-chars-backward "\n")))
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
694 (defun mail-signature (atpoint)
695 "Sign letter with contents of the file `~/.signature'.
696 Prefix arg means put contents at point."
697 (interactive "P")
698 (save-excursion
699 (or atpoint
700 (goto-char (point-max)))
701 (skip-chars-backward " \t\n")
702 (end-of-line)
703 (or atpoint
704 (delete-region (point) (point-max)))
705 (insert "\n\n-- \n")
706 (insert-file-contents (expand-file-name "~/.signature"))))
707
708 (defun mail-fill-yanked-message (&optional justifyp)
709 "Fill the paragraphs of a message yanked into this one.
710 Numeric 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
720 (defun mail-indent-citation ()
721 "Modify text just inserted from a message to be cited.
722 The inserted text should be the region.
723 When this function returns, the region is again around the modified text.
724
725 Normally, indent each nonblank line `mail-indentation-spaces' spaces.
726 However, 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
737 (defun mail-yank-original (arg)
738 "Insert the message being replied to, if any (in rmail).
739 Puts point before the text and mark after.
740 Normally, indents each nonblank line ARG spaces (default 3).
741 However, if `mail-yank-prefix' is non-nil, insert that prefix on each line.
742
743 Just \\[universal-argument] as argument means don't indent, insert no prefix,
744 and don't delete any header fields."
745 (interactive "P")
746 (if mail-reply-buffer
747 (let ((start (point)))
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)
752 (insert-buffer mail-reply-buffer)
753 (if (consp arg)
754 nil
755 (goto-char start)
756 (let ((mail-indentation-spaces (if arg (prefix-numeric-value arg)
757 mail-indentation-spaces)))
758 (if mail-citation-hook
759 (run-hooks 'mail-citation-hook)
760 (if mail-yank-hooks
761 (run-hooks 'mail-yank-hooks)
762 (mail-indent-citation)))))
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))))
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)
789 "Edit a message to be sent. Prefix arg means resume editing (don't erase).
790 When this function returns, the buffer `*mail*' is selected.
791 The value is t if the message was newly initialized; otherwise, nil.
792
793 By default, the signature file `~/.signature' is inserted at the end;
794 see the variable `mail-signature'.
795
796 \\<mail-mode-map>
797 While editing message, type \\[mail-send-and-exit] to send the message and exit.
798
799 Various special commands starting with C-c are available in sendmail mode
800 to move to message header fields:
801 \\{mail-mode-map}
802
803 If `mail-self-blind' is non-nil, a BCC to yourself is inserted
804 when the message is initialized.
805
806 If `mail-default-reply-to' is non-nil, it should be an address (a string);
807 a Reply-to: field with that address is inserted.
808
809 If `mail-archive-file-name' is non-nil, an FCC field with that file name
810 is inserted.
811
812 If `mail-setup-hook' is bound, its value is called with no arguments
813 after the message is initialized. It can add more default fields.
814
815 When 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.
819 The sixth argument REPLYBUFFER is a buffer whose contents
820 should be yanked if the user types C-c C-y.
821 The 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")
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*")
869 (if (file-exists-p (expand-file-name "~/"))
870 (setq default-directory (expand-file-name "~/")))
871 (auto-save-mode auto-save-default)
872 (mail-mode)
873 ;; Disconnect the buffer from its visited file
874 ;; (in case the user has actually visited a file *mail*).
875 ; (set-visited-file-name nil)
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))
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)))
901 (t (error "mail-recover cancelled")))))
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
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."
914 (interactive "P")
915 (let ((pop-up-frames t))
916 (pop-to-buffer "*mail*"))
917 (mail noerase to subject in-reply-to cc replybuffer sendactions))
918
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)
924
925 ;;; Do not add anything but external entries on this page.
926
927 (provide 'sendmail)
928
929 ;;; sendmail.el ends here