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