]> code.delx.au - gnu-emacs/blob - lisp/mail/sendmail.el
(sendmail-send-it): Make deletion of empty subject line work reliably.
[gnu-emacs] / lisp / mail / sendmail.el
1 ;;; sendmail.el --- mail sending commands for Emacs.
2
3 ;; Copyright (C) 1985, 1986, 1992, 1993, 1994, 1995 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-from-style 'angles "\
33 *Specifies how \"From:\" fields look.
34
35 If `nil', they contain just the return address like:
36 king@grassland.com
37 If `parens', they look like:
38 king@grassland.com (Elvis Parsley)
39 If `angles', they look like:
40 Elvis Parsley <king@grassland.com>")
41
42 ;;;###autoload
43 (defvar mail-self-blind nil "\
44 Non-nil means insert BCC to self in messages to be sent.
45 This is done when the message is initialized,
46 so you can remove or alter the BCC field to override the default.")
47
48 ;;;###autoload
49 (defvar mail-interactive nil "\
50 Non-nil means when sending a message wait for and display errors.
51 nil means let mailer mail back a message to report errors.")
52
53 ;;;###autoload
54 (defvar mail-yank-ignored-headers "^via:\\|^mail-from:\\|^origin:\\|^status:\\|^remailed\\|^received:\\|^message-id:\\|^summary-line:\\|^to:\\|^subject:\\|^in-reply-to:\\|^return-path:" "\
55 Delete these headers from old message when it's inserted in a reply.")
56
57 ;; Useful to set in site-init.el
58 ;;;###autoload
59 (defvar send-mail-function 'sendmail-send-it "\
60 Function to call to send the current buffer as mail.
61 The headers should be delimited by a line whose contents
62 match the variable `mail-header-separator'.")
63
64 ;;;###autoload
65 (defvar mail-header-separator "--text follows this line--" "\
66 *Line used to separate headers from text in messages being composed.")
67
68 ;; Set up mail-header-separator for use as a category text property.
69 (put 'mail-header-separator 'rear-nonsticky '(category))
70 (put 'mail-header-separator 'read-only t)
71
72 ;;;###autoload
73 (defvar mail-archive-file-name nil "\
74 *Name of file to write all outgoing messages in, or nil for none.
75 This can be an inbox file or an Rmail file.")
76
77 ;;;###autoload
78 (defvar mail-default-reply-to nil
79 "*Address to insert as default Reply-to field of outgoing messages.
80 If nil, it will be initialized from the REPLYTO environment variable
81 when you first send mail.")
82
83 ;;;###autoload
84 (defvar mail-alias-file nil
85 "*If non-nil, the name of a file to use instead of `/usr/lib/aliases'.
86 This file defines aliases to be expanded by the mailer; this is a different
87 feature from that of defining aliases in `.mailrc' to be expanded in Emacs.
88 This variable has no effect unless your system uses sendmail as its mailer.")
89
90 ;;;###autoload
91 (defvar mail-personal-alias-file "~/.mailrc"
92 "*If non-nil, the name of the user's personal mail alias file.
93 This file typically should be in same format as the `.mailrc' file used by
94 the `Mail' or `mailx' program.
95 This file need not actually exist.")
96
97 (defvar mail-setup-hook nil
98 "Normal hook, run each time a new outgoing mail message is initialized.
99 The function `mail-setup' runs this hook.")
100
101 (defvar mail-aliases t
102 "Alist of mail address aliases,
103 or t meaning should be initialized from your mail aliases file.
104 \(The file's name is normally `~/.mailrc', but your MAILRC environment
105 variable can override that name.)
106 The alias definitions in the file have this form:
107 alias ALIAS MEANING")
108
109 (defvar mail-alias-modtime nil
110 "The modification time of your mail alias file when it was last examined.")
111
112 (defvar mail-yank-prefix nil
113 "*Prefix insert on lines of yanked message being replied to.
114 nil means use indentation.")
115 (defvar mail-indentation-spaces 3
116 "*Number of spaces to insert at the beginning of each cited line.
117 Used by `mail-yank-original' via `mail-yank-cite'.")
118 (defvar mail-yank-hooks nil
119 "Obsolete hook for modifying a citation just inserted in the mail buffer.
120 Each hook function can find the citation between (point) and (mark t).
121 And each hook function should leave point and mark around the citation
122 text as modified.
123
124 This is a normal hook, misnamed for historical reasons.
125 It is semi-obsolete and mail agents should no longer use it.")
126
127 (defvar mail-citation-hook nil
128 "*Hook for modifying a citation just inserted in the mail buffer.
129 Each hook function can find the citation between (point) and (mark t).
130 And each hook function should leave point and mark around the citation
131 text as modified.
132
133 If this hook is entirely empty (nil), a default action is taken
134 instead of no action.")
135
136 (defvar mail-abbrevs-loaded nil)
137 (defvar mail-mode-map nil)
138
139 (autoload 'build-mail-aliases "mailalias"
140 "Read mail aliases from user's personal aliases file and set `mail-aliases'."
141 nil)
142
143 (autoload 'expand-mail-aliases "mailalias"
144 "Expand all mail aliases in suitable header fields found between BEG and END.
145 Suitable header fields are `To', `Cc' and `Bcc' and their `Resent-' variants.
146 Optional second arg EXCLUDE may be a regular expression defining text to be
147 removed from alias expansions."
148 nil)
149
150 ;;;###autoload
151 (defvar mail-signature nil
152 "*Text inserted at end of mail buffer when a message is initialized.
153 If t, it means to insert the contents of the file `mail-signature-file'.")
154
155 (defvar mail-signature-file "~/.signature"
156 "*File containing the text inserted at end of mail buffer.")
157
158 (defvar mail-reply-buffer nil)
159 (defvar mail-send-actions nil
160 "A list of actions to be performed upon successful sending of a message.")
161
162 (defvar mail-default-headers nil
163 "*A string containing header lines, to be inserted in outgoing messages.
164 It is inserted before you edit the message,
165 so you can edit or delete these lines.")
166
167 (defvar mail-bury-selects-summary t
168 "*If non-nil, try to show RMAIL summary buffer after returning from mail.
169 The functions \\[mail-send-on-exit] or \\[mail-dont-send] select
170 the RMAIL summary buffer before returning, if it exists and this variable
171 is non-nil.")
172
173 ;; Note: could use /usr/ucb/mail instead of sendmail;
174 ;; options -t, and -v if not interactive.
175 (defvar mail-mailer-swallows-blank-line
176 (if (and (string-match "sparc-sun-sunos\\(\\'\\|[^5]\\)" system-configuration)
177 (file-readable-p "/etc/sendmail.cf")
178 (let ((buffer (get-buffer-create " *temp*")))
179 (unwind-protect
180 (save-excursion
181 (set-buffer buffer)
182 (insert-file-contents "/etc/sendmail.cf")
183 (goto-char (point-min))
184 (let ((case-fold-search nil))
185 (re-search-forward "^OR\\>" nil t)))
186 (kill-buffer buffer))))
187 ;; According to RFC822, "The field-name must be composed of printable
188 ;; ASCII characters (i.e. characters that have decimal values between
189 ;; 33 and 126, except colon)", i.e. any chars except ctl chars,
190 ;; space, or colon.
191 '(looking-at "[ \t]\\|[][!\"#$%&'()*+,-./0-9;<=>?@A-Z\\\\^_`a-z{|}~]+:"))
192 "Set this non-nil if the system's mailer runs the header and body together.
193 \(This problem exists on Sunos 4 when sendmail is run in remote mode.)
194 The value should be an expression to test whether the problem will
195 actually occur.")
196
197 (defvar mail-mode-syntax-table nil
198 "Syntax table used while in mail mode.")
199
200 (if (not mail-mode-syntax-table)
201 (progn
202 (setq mail-mode-syntax-table (copy-syntax-table text-mode-syntax-table))
203 (modify-syntax-entry ?% ". " mail-mode-syntax-table)))
204
205 (defvar mail-font-lock-keywords
206 (let* ((cite-prefix "A-Za-z") (cite-suffix (concat cite-prefix "0-9_.@-")))
207 (list '("^To:" . font-lock-function-name-face)
208 '("^B?CC:\\|^Reply-To:" . font-lock-keyword-face)
209 '("^\\(Subject:\\)[ \t]*\\(.+\\)?"
210 (1 font-lock-comment-face) (2 font-lock-type-face nil t))
211 (list (concat "^\\(" (regexp-quote mail-header-separator) "\\)$")
212 1 'font-lock-comment-face)
213 (cons (concat "^[ \t]*"
214 "\\([" cite-prefix "]+[" cite-suffix "]*\\)?"
215 "[>|}].*")
216 'font-lock-reference-face)
217 '("^\\(X-[A-Za-z0-9-]+\\|In-reply-to\\):.*"
218 . font-lock-string-face)))
219 "Additional expressions to highlight in Mail mode.")
220
221 (defvar mail-send-hook nil
222 "Normal hook run before sending mail, in Mail mode.")
223
224 (defun sendmail-synch-aliases ()
225 (let ((modtime (nth 5 (file-attributes mail-personal-alias-file))))
226 (or (equal mail-alias-modtime modtime)
227 (setq mail-alias-modtime modtime
228 mail-aliases t))))
229
230 (defun mail-setup (to subject in-reply-to cc replybuffer actions)
231 (or mail-default-reply-to
232 (setq mail-default-reply-to (getenv "REPLYTO")))
233 (sendmail-synch-aliases)
234 (if (eq mail-aliases t)
235 (progn
236 (setq mail-aliases nil)
237 (if (file-exists-p mail-personal-alias-file)
238 (build-mail-aliases))))
239 (setq mail-send-actions actions)
240 (setq mail-reply-buffer replybuffer)
241 (goto-char (point-min))
242 (insert "To: ")
243 (save-excursion
244 (if to
245 ;; Here removed code to extract names from within <...>
246 ;; on the assumption that mail-strip-quoted-names
247 ;; has been called and has done so.
248 (let ((fill-prefix "\t")
249 (address-start (point)))
250 (insert to "\n")
251 (fill-region-as-paragraph address-start (point-max)))
252 (newline))
253 (if cc
254 (let ((fill-prefix "\t")
255 (address-start (progn (insert "CC: ") (point))))
256 (insert cc "\n")
257 (fill-region-as-paragraph address-start (point-max))))
258 (if in-reply-to
259 (let ((fill-prefix "\t")
260 (fill-column 78)
261 (address-start (point)))
262 (insert "In-reply-to: " in-reply-to "\n")
263 (fill-region-as-paragraph address-start (point-max))))
264 (insert "Subject: " (or subject "") "\n")
265 (if mail-default-headers
266 (insert mail-default-headers))
267 (if mail-default-reply-to
268 (insert "Reply-to: " mail-default-reply-to "\n"))
269 (if mail-self-blind
270 (insert "BCC: " (user-login-name) "\n"))
271 (if mail-archive-file-name
272 (insert "FCC: " mail-archive-file-name "\n"))
273 (put-text-property (point)
274 (progn
275 (insert mail-header-separator "\n")
276 (1- (point)))
277 'category 'mail-header-separator)
278 ;; Insert the signature. But remember the beginning of the message.
279 (if to (setq to (point)))
280 (cond ((eq mail-signature t)
281 (if (file-exists-p mail-signature-file)
282 (progn
283 (insert "\n\n-- \n")
284 (insert-file-contents mail-signature-file))))
285 (mail-signature
286 (insert mail-signature)))
287 (goto-char (point-max))
288 (or (bolp) (newline)))
289 (if to (goto-char to))
290 (or to subject in-reply-to
291 (set-buffer-modified-p nil))
292 (run-hooks 'mail-setup-hook))
293
294 ;;;###autoload
295 (defun mail-mode ()
296 "Major mode for editing mail to be sent.
297 Like Text Mode but with these additional commands:
298 C-c C-s mail-send (send the message) C-c C-c mail-send-and-exit
299 C-c C-f move to a header field (and create it if there isn't):
300 C-c C-f C-t move to To: C-c C-f C-s move to Subject:
301 C-c C-f C-c move to CC: C-c C-f C-b move to BCC:
302 C-c C-f C-f move to FCC:
303 C-c C-t mail-text (move to beginning of message text).
304 C-c C-w mail-signature (insert `mail-signature-file' file).
305 C-c C-y mail-yank-original (insert current message, in Rmail).
306 C-c C-q mail-fill-yanked-message (fill what was yanked).
307 C-c C-v mail-sent-via (add a Sent-via field for each To or CC)."
308 (interactive)
309 (kill-all-local-variables)
310 (make-local-variable 'mail-reply-buffer)
311 (setq mail-reply-buffer nil)
312 (make-local-variable 'mail-send-actions)
313 (set-syntax-table mail-mode-syntax-table)
314 (use-local-map mail-mode-map)
315 (setq local-abbrev-table text-mode-abbrev-table)
316 (setq major-mode 'mail-mode)
317 (setq mode-name "Mail")
318 (setq buffer-offer-save t)
319 (make-local-variable 'font-lock-defaults)
320 (setq font-lock-defaults '(mail-font-lock-keywords t))
321 (make-local-variable 'paragraph-separate)
322 (make-local-variable 'paragraph-start)
323 (setq paragraph-start (concat (regexp-quote mail-header-separator)
324 "$\\|[ \t]*[-_][-_][-_]+$\\|"
325 paragraph-start))
326 (setq paragraph-separate (concat (regexp-quote mail-header-separator)
327 "$\\|[ \t]*[-_][-_][-_]+$\\|"
328 paragraph-separate))
329 (run-hooks 'text-mode-hook 'mail-mode-hook))
330 \f
331 ;;; Set up keymap.
332
333 (if mail-mode-map
334 nil
335 (setq mail-mode-map (nconc (make-sparse-keymap) text-mode-map))
336 (define-key mail-mode-map "\C-c?" 'describe-mode)
337 (define-key mail-mode-map "\C-c\C-f\C-t" 'mail-to)
338 (define-key mail-mode-map "\C-c\C-f\C-b" 'mail-bcc)
339 (define-key mail-mode-map "\C-c\C-f\C-f" 'mail-fcc)
340 (define-key mail-mode-map "\C-c\C-f\C-c" 'mail-cc)
341 (define-key mail-mode-map "\C-c\C-f\C-s" 'mail-subject)
342 (define-key mail-mode-map "\C-c\C-f\C-r" 'mail-reply-to)
343 (define-key mail-mode-map "\C-c\C-t" 'mail-text)
344 (define-key mail-mode-map "\C-c\C-y" 'mail-yank-original)
345 (define-key mail-mode-map "\C-c\C-q" 'mail-fill-yanked-message)
346 (define-key mail-mode-map "\C-c\C-w" 'mail-signature)
347 (define-key mail-mode-map "\C-c\C-v" 'mail-sent-via)
348 (define-key mail-mode-map "\C-c\C-c" 'mail-send-and-exit)
349 (define-key mail-mode-map "\C-c\C-s" 'mail-send))
350
351 (define-key mail-mode-map [menu-bar mail]
352 (cons "Mail" (make-sparse-keymap "Mail")))
353
354 (define-key mail-mode-map [menu-bar mail fill]
355 '("Fill Citation" . mail-fill-yanked-message))
356
357 (define-key mail-mode-map [menu-bar mail yank]
358 '("Cite Original" . mail-yank-original))
359
360 (define-key mail-mode-map [menu-bar mail signature]
361 '("Insert Signature" . mail-signature))
362
363 (define-key mail-mode-map [menu-bar mail cancel]
364 '("Cancel" . mail-dont-send))
365
366 (define-key mail-mode-map [menu-bar mail send-stay]
367 '("Send, Keep Editing" . mail-send))
368
369 (define-key mail-mode-map [menu-bar mail send]
370 '("Send Message" . mail-send-and-exit))
371
372 (define-key mail-mode-map [menu-bar headers]
373 (cons "Headers" (make-sparse-keymap "Move to Header")))
374
375 (define-key mail-mode-map [menu-bar headers reply-to]
376 '("Reply-To" . mail-reply-to))
377
378 (define-key mail-mode-map [menu-bar headers sent-via]
379 '("Sent Via" . mail-sent-via))
380
381 (define-key mail-mode-map [menu-bar headers text]
382 '("Text" . mail-text))
383
384 (define-key mail-mode-map [menu-bar headers bcc]
385 '("Bcc" . mail-bcc))
386
387 (define-key mail-mode-map [menu-bar headers fcc]
388 '("Fcc" . mail-fcc))
389
390 (define-key mail-mode-map [menu-bar headers cc]
391 '("Cc" . mail-cc))
392
393 (define-key mail-mode-map [menu-bar headers subject]
394 '("Subject" . mail-subject))
395
396 (define-key mail-mode-map [menu-bar headers to]
397 '("To" . mail-to))
398 \f
399 (defun mail-send-and-exit (arg)
400 "Send message like `mail-send', then, if no errors, exit from mail buffer.
401 Prefix arg means don't delete this window."
402 (interactive "P")
403 (mail-send)
404 (mail-bury arg))
405
406 (defun mail-dont-send (arg)
407 "Don't send the message you have been editing.
408 Prefix arg means don't delete this window."
409 (interactive "P")
410 (mail-bury arg))
411
412 (defun mail-bury (arg)
413 "Bury this mail buffer."
414 (let ((newbuf (other-buffer (current-buffer))))
415 (bury-buffer (current-buffer))
416 (if (and (window-dedicated-p (frame-selected-window))
417 (not (null (delq (selected-frame) (visible-frame-list)))))
418 (delete-frame (selected-frame))
419 (let (rmail-flag summary-buffer)
420 (and (not arg)
421 (not (one-window-p))
422 (save-excursion
423 (set-buffer (window-buffer (next-window (selected-window) 'not)))
424 (setq rmail-flag (eq major-mode 'rmail-mode))
425 (setq summary-buffer
426 (and mail-bury-selects-summary
427 (boundp 'rmail-summary-buffer)
428 rmail-summary-buffer
429 (buffer-name rmail-summary-buffer)
430 (not (get-buffer-window rmail-summary-buffer))
431 rmail-summary-buffer))))
432 (if rmail-flag
433 ;; If the Rmail buffer has a summary, show that.
434 (if summary-buffer (switch-to-buffer summary-buffer)
435 (delete-window))
436 (switch-to-buffer newbuf))))))
437
438 (defun mail-send ()
439 "Send the message in the current buffer.
440 If `mail-interactive' is non-nil, wait for success indication
441 or error messages, and inform user.
442 Otherwise any failure is reported in a message back to
443 the user from the mailer."
444 (interactive)
445 (if (if buffer-file-name
446 (y-or-n-p "Send buffer contents as mail message? ")
447 (or (buffer-modified-p)
448 (y-or-n-p "Message already sent; resend? ")))
449 (let ((inhibit-read-only t))
450 (run-hooks 'mail-send-hook)
451 (message "Sending...")
452 (funcall send-mail-function)
453 ;; Now perform actions on successful sending.
454 (while mail-send-actions
455 (condition-case nil
456 (apply (car (car mail-send-actions))
457 (cdr (car mail-send-actions)))
458 (error))
459 (setq mail-send-actions (cdr mail-send-actions)))
460 (message "Sending...done")
461 ;; If buffer has no file, mark it as unmodified and delete autosave.
462 (if (not buffer-file-name)
463 (progn
464 (set-buffer-modified-p nil)
465 (delete-auto-save-file-if-necessary t))))))
466
467 (defun sendmail-send-it ()
468 (require 'mail-utils)
469 (let ((errbuf (if mail-interactive
470 (generate-new-buffer " sendmail errors")
471 0))
472 (tembuf (generate-new-buffer " sendmail temp"))
473 (case-fold-search nil)
474 resend-to-addresses
475 delimline
476 (mailbuf (current-buffer)))
477 (unwind-protect
478 (save-excursion
479 (set-buffer tembuf)
480 (erase-buffer)
481 (insert-buffer-substring mailbuf)
482 (goto-char (point-max))
483 ;; require one newline at the end.
484 (or (= (preceding-char) ?\n)
485 (insert ?\n))
486 ;; Change header-delimiter to be what sendmail expects.
487 (goto-char (point-min))
488 (re-search-forward
489 (concat "^" (regexp-quote mail-header-separator) "\n"))
490 (replace-match "\n")
491 (backward-char 1)
492 (setq delimline (point-marker))
493 (sendmail-synch-aliases)
494 (if mail-aliases
495 (expand-mail-aliases (point-min) delimline))
496 (goto-char (point-min))
497 ;; ignore any blank lines in the header
498 (while (and (re-search-forward "\n\n\n*" delimline t)
499 (< (point) delimline))
500 (replace-match "\n"))
501 (let ((case-fold-search t))
502 (goto-char (point-min))
503 (while (re-search-forward "^Resent-\\(to\\|cc\\|bcc\\):" delimline t)
504 (setq resend-to-addresses
505 (save-restriction
506 (narrow-to-region (point)
507 (save-excursion
508 (end-of-line)
509 (point)))
510 (append (mail-parse-comma-list)
511 resend-to-addresses)))
512 ;; Delete Resent-BCC ourselves
513 (if (save-excursion (beginning-of-line)
514 (looking-at "resent-bcc"))
515 (delete-region (save-excursion (beginning-of-line) (point))
516 (save-excursion (end-of-line) (1+ (point))))))
517 ;;; Apparently this causes a duplicate Sender.
518 ;;; ;; If the From is different than current user, insert Sender.
519 ;;; (goto-char (point-min))
520 ;;; (and (re-search-forward "^From:" delimline t)
521 ;;; (progn
522 ;;; (require 'mail-utils)
523 ;;; (not (string-equal
524 ;;; (mail-strip-quoted-names
525 ;;; (save-restriction
526 ;;; (narrow-to-region (point-min) delimline)
527 ;;; (mail-fetch-field "From")))
528 ;;; (user-login-name))))
529 ;;; (progn
530 ;;; (forward-line 1)
531 ;;; (insert "Sender: " (user-login-name) "\n")))
532 ;; Don't send out a blank subject line
533 (goto-char (point-min))
534 (if (re-search-forward "^Subject:\\([ \t]*\n\\)+\\b" delimline t)
535 (replace-match "")
536 ;; This one matches a Subject just before the header delimiter.
537 (if (and (re-search-forward "^Subject:\\([ \t]*\n\\)+" delimline t)
538 (= (match-end 0) delimline))
539 (replace-match "")))
540 ;; Put the "From:" field in unless for some odd reason
541 ;; they put one in themselves.
542 (goto-char (point-min))
543 (if (not (re-search-forward "^From:" delimline t))
544 (let* ((login user-mail-address)
545 (fullname (user-full-name)))
546 (cond ((eq mail-from-style 'angles)
547 (insert "From: " fullname)
548 (let ((fullname-start (+ (point-min) 6))
549 (fullname-end (point-marker)))
550 (goto-char fullname-start)
551 ;; Look for a character that cannot appear unquoted
552 ;; according to RFC 822.
553 (if (re-search-forward "[^- !#-'*+/-9=?A-Z^-~]"
554 fullname-end 1)
555 (progn
556 ;; Quote fullname, escaping specials.
557 (goto-char fullname-start)
558 (insert "\"")
559 (while (re-search-forward "[\"\\]"
560 fullname-end 1)
561 (replace-match "\\\\\\&" t))
562 (insert "\""))))
563 (insert " <" login ">\n"))
564 ((eq mail-from-style 'parens)
565 (insert "From: " login " (")
566 (let ((fullname-start (point)))
567 (insert fullname)
568 (let ((fullname-end (point-marker)))
569 (goto-char fullname-start)
570 ;; RFC 822 says \ and nonmatching parentheses
571 ;; must be escaped in comments.
572 ;; Escape every instance of ()\ ...
573 (while (re-search-forward "[()\\]" fullname-end 1)
574 (replace-match "\\\\\\&" t))
575 ;; ... then undo escaping of matching parentheses,
576 ;; including matching nested parentheses.
577 (goto-char fullname-start)
578 (while (re-search-forward
579 "\\(\\=\\|[^\\]\\(\\\\\\\\\\)*\\)\\\\(\\(\\([^\\]\\|\\\\\\\\\\)*\\)\\\\)"
580 fullname-end 1)
581 (replace-match "\\1(\\3)" t)
582 (goto-char fullname-start))))
583 (insert ")\n"))
584 ((null mail-from-style)
585 (insert "From: " login "\n")))))
586 ;; Insert an extra newline if we need it to work around
587 ;; Sun's bug that swallows newlines.
588 (goto-char (1+ delimline))
589 (if (eval mail-mailer-swallows-blank-line)
590 (newline))
591 ;; Find and handle any FCC fields.
592 (goto-char (point-min))
593 (if (re-search-forward "^FCC:" delimline t)
594 (mail-do-fcc delimline))
595 (if mail-interactive
596 (save-excursion
597 (set-buffer errbuf)
598 (erase-buffer))))
599 (let ((default-directory "/"))
600 (apply 'call-process-region
601 (append (list (point-min) (point-max)
602 (if (boundp 'sendmail-program)
603 sendmail-program
604 "/usr/lib/sendmail")
605 nil errbuf nil "-oi")
606 ;; Always specify who from,
607 ;; since some systems have broken sendmails.
608 (list "-f" (user-login-name))
609 ;;; ;; Don't say "from root" if running under su.
610 ;;; (and (equal (user-real-login-name) "root")
611 ;;; (list "-f" (user-login-name)))
612 (and mail-alias-file
613 (list (concat "-oA" mail-alias-file)))
614 ;; These mean "report errors by mail"
615 ;; and "deliver in background".
616 (if (null mail-interactive) '("-oem" "-odb"))
617 ;; Get the addresses from the message
618 ;; unless this is a resend.
619 ;; We must not do that for a resend
620 ;; because we would find the original addresses.
621 ;; For a resend, include the specific addresses.
622 (or resend-to-addresses
623 '("-t")))))
624 (if mail-interactive
625 (save-excursion
626 (set-buffer errbuf)
627 (goto-char (point-min))
628 (while (re-search-forward "\n\n* *" nil t)
629 (replace-match "; "))
630 (if (not (zerop (buffer-size)))
631 (error "Sending...failed to %s"
632 (buffer-substring (point-min) (point-max)))))))
633 (kill-buffer tembuf)
634 (if (bufferp errbuf)
635 (kill-buffer errbuf)))))
636
637 (defun mail-do-fcc (header-end)
638 (let (fcc-list
639 (rmailbuf (current-buffer))
640 (time (current-time))
641 (tembuf (generate-new-buffer " rmail output"))
642 (case-fold-search t))
643 (save-excursion
644 (goto-char (point-min))
645 (while (re-search-forward "^FCC:[ \t]*" header-end t)
646 (setq fcc-list (cons (buffer-substring (point)
647 (progn
648 (end-of-line)
649 (skip-chars-backward " \t")
650 (point)))
651 fcc-list))
652 (delete-region (match-beginning 0)
653 (progn (forward-line 1) (point))))
654 (set-buffer tembuf)
655 (erase-buffer)
656 ;; This initial newline is written out if the fcc file already exists.
657 (insert "\nFrom " (user-login-name) " "
658 (current-time-string time) "\n")
659 ;; Insert the time zone before the year.
660 (forward-char -1)
661 (forward-word -1)
662 (require 'mail-utils)
663 (insert (mail-rfc822-time-zone time) " ")
664 (goto-char (point-max))
665 (insert-buffer-substring rmailbuf)
666 ;; Make sure messages are separated.
667 (goto-char (point-max))
668 (insert ?\n)
669 (goto-char 2)
670 ;; ``Quote'' "^From " as ">From "
671 ;; (note that this isn't really quoting, as there is no requirement
672 ;; that "^[>]+From " be quoted in the same transparent way.)
673 (let ((case-fold-search nil))
674 (while (search-forward "\nFrom " nil t)
675 (forward-char -5)
676 (insert ?>)))
677 (while fcc-list
678 (let* ((buffer (find-buffer-visiting (car fcc-list)))
679 (curbuf (current-buffer))
680 (beg (point-min)) (end (point-max))
681 (beg2 (save-excursion (goto-char (point-min))
682 (forward-line 2) (point))))
683 (if buffer
684 ;; File is present in a buffer => append to that buffer.
685 (save-excursion
686 (set-buffer buffer)
687 ;; Keep the end of the accessible portion at the same place
688 ;; unless it is the end of the buffer.
689 (let ((max (if (/= (1+ (buffer-size)) (point-max))
690 (point-max))))
691 (unwind-protect
692 ;; Code below lifted from rmailout.el
693 ;; function rmail-output-to-rmail-file:
694 (let ((buffer-read-only nil)
695 (msg (and (boundp 'rmail-current-message)
696 rmail-current-message)))
697 ;; If MSG is non-nil, buffer is in RMAIL mode.
698 (if msg
699 (progn
700 (rmail-maybe-set-message-counters)
701 (widen)
702 (narrow-to-region (point-max) (point-max))
703 (insert "\C-l\n0, unseen,,\n*** EOOH ***\n"
704 "Date: " (mail-rfc822-date) "\n")
705 (insert-buffer-substring curbuf beg2 end)
706 (insert "\n\C-_")
707 (goto-char (point-min))
708 (widen)
709 (search-backward "\n\^_")
710 (narrow-to-region (point) (point-max))
711 (rmail-count-new-messages t)
712 (rmail-show-message msg)
713 (setq max nil))
714 ;; Output file not in rmail mode
715 ;; => just insert at the end.
716 (narrow-to-region (point-min) (1+ (buffer-size)))
717 (goto-char (point-max))
718 (insert-buffer-substring curbuf beg end)))
719 (if max (narrow-to-region (point-min) max)))))
720 ;; Else append to the file directly.
721 (if (and (file-exists-p (car fcc-list))
722 (mail-file-babyl-p (car fcc-list)))
723 ;; If the file is a Babyl file,
724 ;; convert the message to Babyl format.
725 (save-excursion
726 (set-buffer (get-buffer-create " mail-temp"))
727 (setq buffer-read-only nil)
728 (erase-buffer)
729 (insert "\C-l\n0, unseen,,\n*** EOOH ***\n"
730 "Date: " (mail-rfc822-date) "\n")
731 (insert-buffer-substring curbuf beg2 end)
732 (insert "\n\C-_")
733 (write-region (point-min) (point-max) (car fcc-list) t)
734 (erase-buffer))
735 (write-region
736 (1+ (point-min)) (point-max) (car fcc-list) t))))
737 (setq fcc-list (cdr fcc-list))))
738 (kill-buffer tembuf)))
739
740 (defun mail-sent-via ()
741 "Make a Sent-via header line from each To or CC header line."
742 (interactive)
743 (save-excursion
744 (goto-char (point-min))
745 ;; find the header-separator
746 (search-forward (concat "\n" mail-header-separator "\n"))
747 (forward-line -1)
748 ;; put a marker at the end of the header
749 (let ((end (point-marker))
750 (case-fold-search t)
751 to-line)
752 (goto-char (point-min))
753 ;; search for the To: lines and make Sent-via: lines from them
754 ;; search for the next To: line
755 (while (re-search-forward "^\\(to\\|cc\\):" end t)
756 ;; Grab this line plus all its continuations, sans the `to:'.
757 (let ((to-line
758 (buffer-substring (point)
759 (progn
760 (if (re-search-forward "^[^ \t\n]" end t)
761 (backward-char 1)
762 (goto-char end))
763 (point)))))
764 ;; Insert a copy, with altered header field name.
765 (insert-before-markers "Sent-via:" to-line))))))
766 \f
767 (defun mail-to ()
768 "Move point to end of To-field."
769 (interactive)
770 (expand-abbrev)
771 (mail-position-on-field "To"))
772
773 (defun mail-subject ()
774 "Move point to end of Subject-field."
775 (interactive)
776 (expand-abbrev)
777 (mail-position-on-field "Subject"))
778
779 (defun mail-cc ()
780 "Move point to end of CC-field. Create a CC field if none."
781 (interactive)
782 (expand-abbrev)
783 (or (mail-position-on-field "cc" t)
784 (progn (mail-position-on-field "to")
785 (insert "\nCC: "))))
786
787 (defun mail-bcc ()
788 "Move point to end of BCC-field. Create a BCC field if none."
789 (interactive)
790 (expand-abbrev)
791 (or (mail-position-on-field "bcc" t)
792 (progn (mail-position-on-field "to")
793 (insert "\nBCC: "))))
794
795 (defun mail-fcc (folder)
796 "Add a new FCC field, with file name completion."
797 (interactive "FFolder carbon copy: ")
798 (expand-abbrev)
799 (or (mail-position-on-field "fcc" t) ;Put new field after exiting FCC.
800 (mail-position-on-field "to"))
801 (insert "\nFCC: " folder))
802
803 (defun mail-reply-to ()
804 "Move point to end of Reply-To-field."
805 (interactive)
806 (expand-abbrev)
807 (mail-position-on-field "Reply-To"))
808
809 (defun mail-position-on-field (field &optional soft)
810 (let (end
811 (case-fold-search t))
812 (goto-char (point-min))
813 (re-search-forward (concat "^" (regexp-quote mail-header-separator) "$"))
814 (setq end (match-beginning 0))
815 (goto-char (point-min))
816 (if (re-search-forward (concat "^" (regexp-quote field) ":") end t)
817 (progn
818 (re-search-forward "^[^ \t]" nil 'move)
819 (beginning-of-line)
820 (skip-chars-backward "\n")
821 t)
822 (or soft
823 (progn (goto-char end)
824 (insert field ": \n")
825 (skip-chars-backward "\n")))
826 nil)))
827
828 (defun mail-text ()
829 "Move point to beginning of message text."
830 (interactive)
831 (expand-abbrev)
832 (goto-char (point-min))
833 (search-forward (concat "\n" mail-header-separator "\n")))
834 \f
835 (defun mail-signature (atpoint)
836 "Sign letter with contents of the file `mail-signature-file'.
837 Prefix arg means put contents at point."
838 (interactive "P")
839 (save-excursion
840 (or atpoint
841 (goto-char (point-max)))
842 (skip-chars-backward " \t\n")
843 (end-of-line)
844 (or atpoint
845 (delete-region (point) (point-max)))
846 (insert "\n\n-- \n")
847 (insert-file-contents (expand-file-name mail-signature-file))))
848
849 (defun mail-fill-yanked-message (&optional justifyp)
850 "Fill the paragraphs of a message yanked into this one.
851 Numeric argument means justify as well."
852 (interactive "P")
853 (save-excursion
854 (goto-char (point-min))
855 (search-forward (concat "\n" mail-header-separator "\n") nil t)
856 (fill-individual-paragraphs (point)
857 (point-max)
858 justifyp
859 t)))
860
861 (defun mail-indent-citation ()
862 "Modify text just inserted from a message to be cited.
863 The inserted text should be the region.
864 When this function returns, the region is again around the modified text.
865
866 Normally, indent each nonblank line `mail-indentation-spaces' spaces.
867 However, if `mail-yank-prefix' is non-nil, insert that prefix on each line."
868 (let ((start (point)))
869 (mail-yank-clear-headers start (mark t))
870 (if (null mail-yank-prefix)
871 (indent-rigidly start (mark t) mail-indentation-spaces)
872 (save-excursion
873 (goto-char start)
874 (while (< (point) (mark t))
875 (insert mail-yank-prefix)
876 (forward-line 1))))))
877
878 (defun mail-yank-original (arg)
879 "Insert the message being replied to, if any (in rmail).
880 Puts point before the text and mark after.
881 Normally, indents each nonblank line ARG spaces (default 3).
882 However, if `mail-yank-prefix' is non-nil, insert that prefix on each line.
883
884 Just \\[universal-argument] as argument means don't indent, insert no prefix,
885 and don't delete any header fields."
886 (interactive "P")
887 (if mail-reply-buffer
888 (let ((start (point)))
889 ;; If the original message is in another window in the same frame,
890 ;; delete that window to save screen space.
891 ;; t means don't alter other frames.
892 (delete-windows-on mail-reply-buffer t)
893 (insert-buffer mail-reply-buffer)
894 (if (consp arg)
895 nil
896 (goto-char start)
897 (let ((mail-indentation-spaces (if arg (prefix-numeric-value arg)
898 mail-indentation-spaces)))
899 (if mail-citation-hook
900 (run-hooks 'mail-citation-hook)
901 (if mail-yank-hooks
902 (run-hooks 'mail-yank-hooks)
903 (mail-indent-citation)))))
904 ;; This is like exchange-point-and-mark, but doesn't activate the mark.
905 ;; It is cleaner to avoid activation, even though the command
906 ;; loop would deactivate the mark because we inserted text.
907 (goto-char (prog1 (mark t)
908 (set-marker (mark-marker) (point) (current-buffer))))
909 (if (not (eolp)) (insert ?\n)))))
910
911 (defun mail-yank-clear-headers (start end)
912 (if mail-yank-ignored-headers
913 (save-excursion
914 (goto-char start)
915 (if (search-forward "\n\n" end t)
916 (save-restriction
917 (narrow-to-region start (point))
918 (goto-char start)
919 (while (let ((case-fold-search t))
920 (re-search-forward mail-yank-ignored-headers nil t))
921 (beginning-of-line)
922 (delete-region (point)
923 (progn (re-search-forward "\n[^ \t]")
924 (forward-char -1)
925 (point)))))))))
926 \f
927 ;; Put these last, to reduce chance of lossage from quitting in middle of loading the file.
928
929 ;;;###autoload
930 (defun mail (&optional noerase to subject in-reply-to cc replybuffer actions)
931 "Edit a message to be sent. Prefix arg means resume editing (don't erase).
932 When this function returns, the buffer `*mail*' is selected.
933 The value is t if the message was newly initialized; otherwise, nil.
934
935 Optionally, the signature file `mail-signature-file' can be inserted at the
936 end; see the variable `mail-signature'.
937
938 \\<mail-mode-map>
939 While editing message, type \\[mail-send-and-exit] to send the message and exit.
940
941 Various special commands starting with C-c are available in sendmail mode
942 to move to message header fields:
943 \\{mail-mode-map}
944
945 If `mail-self-blind' is non-nil, a BCC to yourself is inserted
946 when the message is initialized.
947
948 If `mail-default-reply-to' is non-nil, it should be an address (a string);
949 a Reply-to: field with that address is inserted.
950
951 If `mail-archive-file-name' is non-nil, an FCC field with that file name
952 is inserted.
953
954 The normal hook `mail-setup-hook' is run after the message is
955 initialized. It can add more default fields to the message.
956
957 When calling from a program, the first argument if non-nil says
958 not to erase the existing contents of the `*mail*' buffer.
959
960 The second through fifth arguments,
961 TO, SUBJECT, IN-REPLY-TO and CC, specify if non-nil
962 the initial contents of those header fields.
963 These arguments should not have final newlines.
964 The sixth argument REPLYBUFFER is a buffer whose contents
965 should be yanked if the user types C-c C-y.
966 The seventh argument ACTIONS is a list of actions to take
967 if/when the message is sent. Each action looks like (FUNCTION . ARGS);
968 when the message is sent, we apply FUNCTION to ARGS.
969 This is how Rmail arranges to mark messages `answered'."
970 (interactive "P")
971 ;;; This is commented out because I found it was confusing in practice.
972 ;;; It is easy enough to rename *mail* by hand with rename-buffer
973 ;;; if you want to have multiple mail buffers.
974 ;;; And then you can control which messages to save. --rms.
975 ;;; (let ((index 1)
976 ;;; buffer)
977 ;;; ;; If requested, look for a mail buffer that is modified and go to it.
978 ;;; (if noerase
979 ;;; (progn
980 ;;; (while (and (setq buffer
981 ;;; (get-buffer (if (= 1 index) "*mail*"
982 ;;; (format "*mail*<%d>" index))))
983 ;;; (not (buffer-modified-p buffer)))
984 ;;; (setq index (1+ index)))
985 ;;; (if buffer (switch-to-buffer buffer)
986 ;;; ;; If none exists, start a new message.
987 ;;; ;; This will never re-use an existing unmodified mail buffer
988 ;;; ;; (since index is not 1 anymore). Perhaps it should.
989 ;;; (setq noerase nil))))
990 ;;; ;; Unless we found a modified message and are happy, start a new message.
991 ;;; (if (not noerase)
992 ;;; (progn
993 ;;; ;; Look for existing unmodified mail buffer.
994 ;;; (while (and (setq buffer
995 ;;; (get-buffer (if (= 1 index) "*mail*"
996 ;;; (format "*mail*<%d>" index))))
997 ;;; (buffer-modified-p buffer))
998 ;;; (setq index (1+ index)))
999 ;;; ;; If none, make a new one.
1000 ;;; (or buffer
1001 ;;; (setq buffer (generate-new-buffer "*mail*")))
1002 ;;; ;; Go there and initialize it.
1003 ;;; (switch-to-buffer buffer)
1004 ;;; (erase-buffer)
1005 ;;; (setq default-directory (expand-file-name "~/"))
1006 ;;; (auto-save-mode auto-save-default)
1007 ;;; (mail-mode)
1008 ;;; (mail-setup to subject in-reply-to cc replybuffer actions)
1009 ;;; (if (and buffer-auto-save-file-name
1010 ;;; (file-exists-p buffer-auto-save-file-name))
1011 ;;; (message "Auto save file for draft message exists; consider M-x mail-recover"))
1012 ;;; t))
1013 (pop-to-buffer "*mail*")
1014 (auto-save-mode auto-save-default)
1015 (mail-mode)
1016 ;; Disconnect the buffer from its visited file
1017 ;; (in case the user has actually visited a file *mail*).
1018 ; (set-visited-file-name nil)
1019 (let (initialized)
1020 (and (not noerase)
1021 (or (not (buffer-modified-p))
1022 (y-or-n-p "Unsent message being composed; erase it? "))
1023 (let ((inhibit-read-only t))
1024 (erase-buffer)
1025 (mail-setup to subject in-reply-to cc replybuffer actions)
1026 (setq initialized t)))
1027 (if (and buffer-auto-save-file-name
1028 (file-exists-p buffer-auto-save-file-name))
1029 (message "Auto save file for draft message exists; consider M-x mail-recover"))
1030 initialized))
1031
1032 (defun mail-recover ()
1033 "Reread contents of current buffer from its last auto-save file."
1034 (interactive)
1035 (let ((file-name (make-auto-save-file-name)))
1036 (cond ((save-window-excursion
1037 (if (not (eq system-type 'vax-vms))
1038 (with-output-to-temp-buffer "*Directory*"
1039 (buffer-disable-undo standard-output)
1040 (let ((default-directory "/"))
1041 (call-process
1042 "ls" nil standard-output nil "-l" file-name))))
1043 (yes-or-no-p (format "Recover auto save file %s? " file-name)))
1044 (let ((buffer-read-only nil))
1045 (erase-buffer)
1046 (insert-file-contents file-name nil)))
1047 (t (error "mail-recover cancelled")))))
1048
1049 ;;;###autoload
1050 (defun mail-other-window (&optional noerase to subject in-reply-to cc replybuffer sendactions)
1051 "Like `mail' command, but display mail buffer in another window."
1052 (interactive "P")
1053 (let ((pop-up-windows t)
1054 (special-display-buffer-names nil)
1055 (special-display-regexps nil)
1056 (same-window-buffer-names nil)
1057 (same-window-regexps nil))
1058 (pop-to-buffer "*mail*"))
1059 (mail noerase to subject in-reply-to cc replybuffer sendactions))
1060
1061 ;;;###autoload
1062 (defun mail-other-frame (&optional noerase to subject in-reply-to cc replybuffer sendactions)
1063 "Like `mail' command, but display mail buffer in another frame."
1064 (interactive "P")
1065 (let ((pop-up-frames t)
1066 (special-display-buffer-names nil)
1067 (special-display-regexps nil)
1068 (same-window-buffer-names nil)
1069 (same-window-regexps nil))
1070 (pop-to-buffer "*mail*"))
1071 (mail noerase to subject in-reply-to cc replybuffer sendactions))
1072
1073 ;;; Do not execute these when sendmail.el is loaded,
1074 ;;; only in loaddefs.el.
1075 ;;;###autoload (define-key ctl-x-map "m" 'mail)
1076 ;;;###autoload (define-key ctl-x-4-map "m" 'mail-other-window)
1077 ;;;###autoload (define-key ctl-x-5-map "m" 'mail-other-frame)
1078
1079 ;;;###autoload (add-hook 'same-window-buffer-names "*mail*")
1080
1081 ;;; Do not add anything but external entries on this page.
1082
1083 (provide 'sendmail)
1084
1085 ;;; sendmail.el ends here