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