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