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