]> code.delx.au - gnu-emacs/blob - lisp/mail/sendmail.el
(mh-quit): Undo 3/3 change (and the subsequent fix).
[gnu-emacs] / lisp / mail / sendmail.el
1 ;;; sendmail.el --- mail sending commands for Emacs.
2
3 ;; Copyright (C) 1985, 86, 92, 93, 94, 95, 96, 1998 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 the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;; This mode provides mail-sending facilities from within Emacs. It is
28 ;; documented in the Emacs user's manual.
29
30 ;;; Code:
31 (defgroup sendmail nil
32 "Mail sending commands for Emacs."
33 :prefix "mail-"
34 :group 'mail)
35
36 ;;;###autoload
37 (defcustom mail-from-style 'angles "\
38 *Specifies how \"From:\" fields look.
39
40 If `nil', they contain just the return address like:
41 king@grassland.com
42 If `parens', they look like:
43 king@grassland.com (Elvis Parsley)
44 If `angles', they look like:
45 Elvis Parsley <king@grassland.com>"
46 :type '(choice (const nil) (const parens) (const angles))
47 :group 'sendmail)
48
49 ;;;###autoload
50 (defcustom mail-self-blind nil "\
51 *Non-nil means insert BCC to self in messages to be sent.
52 This is done when the message is initialized,
53 so you can remove or alter the BCC field to override the default."
54 :type 'boolean
55 :group 'sendmail)
56
57 ;;;###autoload
58 (defcustom mail-interactive nil "\
59 *Non-nil means when sending a message wait for and display errors.
60 nil means let mailer mail back a message to report errors."
61 :type 'boolean
62 :group 'sendmail)
63
64 ;;;###autoload
65 (defcustom mail-yank-ignored-headers "^via:\\|^mail-from:\\|^origin:\\|^status:\\|^remailed\\|^received:\\|^message-id:\\|^summary-line:\\|^to:\\|^subject:\\|^in-reply-to:\\|^return-path:" "\
66 *Delete these headers from old message when it's inserted in a reply."
67 :type 'regexp
68 :group 'sendmail)
69
70 ;; Useful to set in site-init.el
71 ;;;###autoload
72 (defvar send-mail-function 'sendmail-send-it "\
73 Function to call to send the current buffer as mail.
74 The headers should be delimited by a line which is
75 not a valid RFC822 header or continuation line.")
76
77 ;;;###autoload
78 (defcustom mail-header-separator "--text follows this line--" "\
79 *Line used to separate headers from text in messages being composed."
80 :type 'string
81 :group 'sendmail)
82
83 ;; Set up mail-header-separator for use as a category text property.
84 (put 'mail-header-separator 'rear-nonsticky '(category))
85 ;;; This was a nice idea, for preventing accidental modification of
86 ;;; the separator. But I found it also prevented or obstructed
87 ;;; certain deliberate operations, such as copying the separator line
88 ;;; up to the top to send myself a copy of an already sent outgoing message
89 ;;; and other things. So I turned it off. --rms.
90 ;;;(put 'mail-header-separator 'read-only t)
91
92 ;;;###autoload
93 (defcustom mail-archive-file-name nil "\
94 *Name of file to write all outgoing messages in, or nil for none.
95 This can be an inbox file or an Rmail file."
96 :type '(choice file (const nil))
97 :group 'sendmail)
98
99 ;;;###autoload
100 (defcustom mail-default-reply-to nil
101 "*Address to insert as default Reply-to field of outgoing messages.
102 If nil, it will be initialized from the REPLYTO environment variable
103 when you first send mail."
104 :type '(choice (const nil) string)
105 :group 'sendmail)
106
107 ;;;###autoload
108 (defcustom mail-alias-file nil
109 "*If non-nil, the name of a file to use instead of `/usr/lib/aliases'.
110 This file defines aliases to be expanded by the mailer; this is a different
111 feature from that of defining aliases in `.mailrc' to be expanded in Emacs.
112 This variable has no effect unless your system uses sendmail as its mailer."
113 :type '(choice (const nil) file)
114 :group 'sendmail)
115
116 ;;;###autoload
117 (defcustom mail-personal-alias-file "~/.mailrc"
118 "*If non-nil, the name of the user's personal mail alias file.
119 This file typically should be in same format as the `.mailrc' file used by
120 the `Mail' or `mailx' program.
121 This file need not actually exist."
122 :type '(choice (const nil) file)
123 :group 'sendmail)
124
125 (defcustom mail-setup-hook nil
126 "Normal hook, run each time a new outgoing mail message is initialized.
127 The function `mail-setup' runs this hook."
128 :type 'hook
129 :group 'sendmail)
130
131 (defvar mail-aliases t
132 "Alist of mail address aliases,
133 or t meaning should be initialized from your mail aliases file.
134 \(The file's name is normally `~/.mailrc', but your MAILRC environment
135 variable can override that name.)
136 The alias definitions in the file have this form:
137 alias ALIAS MEANING")
138
139 (defvar mail-alias-modtime nil
140 "The modification time of your mail alias file when it was last examined.")
141
142 (defcustom mail-yank-prefix nil
143 "*Prefix insert on lines of yanked message being replied to.
144 nil means use indentation."
145 :type '(choice (const nil) string)
146 :group 'sendmail)
147
148 (defcustom mail-indentation-spaces 3
149 "*Number of spaces to insert at the beginning of each cited line.
150 Used by `mail-yank-original' via `mail-indent-citation'."
151 :type 'integer
152 :group 'sendmail)
153 (defvar mail-yank-hooks nil
154 "Obsolete hook for modifying a citation just inserted in the mail buffer.
155 Each hook function can find the citation between (point) and (mark t).
156 And each hook function should leave point and mark around the citation
157 text as modified.
158
159 This is a normal hook, misnamed for historical reasons.
160 It is semi-obsolete and mail agents should no longer use it.")
161
162 (defcustom mail-citation-hook nil
163 "*Hook for modifying a citation just inserted in the mail buffer.
164 Each hook function can find the citation between (point) and (mark t).
165 And each hook function should leave point and mark around the citation
166 text as modified.
167
168 If this hook is entirely empty (nil), a default action is taken
169 instead of no action."
170 :type 'hook
171 :group 'sendmail)
172
173 (defvar mail-abbrevs-loaded nil)
174 (defvar mail-mode-map nil)
175
176 (autoload 'build-mail-aliases "mailalias"
177 "Read mail aliases from user's personal aliases file and set `mail-aliases'."
178 nil)
179
180 (autoload 'expand-mail-aliases "mailalias"
181 "Expand all mail aliases in suitable header fields found between BEG and END.
182 Suitable header fields are `To', `Cc' and `Bcc' and their `Resent-' variants.
183 Optional second arg EXCLUDE may be a regular expression defining text to be
184 removed from alias expansions."
185 nil)
186
187 ;;;###autoload
188 (defcustom mail-signature nil
189 "*Text inserted at end of mail buffer when a message is initialized.
190 If t, it means to insert the contents of the file `mail-signature-file'."
191 :type '(choice (const nil) (const t) string)
192 :group 'sendmail)
193
194 (defcustom mail-signature-file "~/.signature"
195 "*File containing the text inserted at end of mail buffer."
196 :type 'file
197 :group 'sendmail)
198
199 (defvar mail-reply-action nil)
200 (defvar mail-send-actions nil
201 "A list of actions to be performed upon successful sending of a message.")
202 (put 'mail-reply-action 'permanent-local t)
203 (put 'mail-send-actions 'permanent-local t)
204
205 (defcustom mail-default-headers nil
206 "*A string containing header lines, to be inserted in outgoing messages.
207 It is inserted before you edit the message,
208 so you can edit or delete these lines."
209 :type '(choice (const nil) string)
210 :group 'sendmail)
211
212 (defcustom mail-bury-selects-summary t
213 "*If non-nil, try to show RMAIL summary buffer after returning from mail.
214 The functions \\[mail-send-on-exit] or \\[mail-dont-send] select
215 the RMAIL summary buffer before returning, if it exists and this variable
216 is non-nil."
217 :type 'boolean
218 :group 'sendmail)
219
220 ;; I find that this happens so often, for innocent reasons,
221 ;; that it is not acceptable to bother the user about it -- rms.
222 (defcustom mail-send-nonascii t
223 "*Specify whether to allow sending non-ASCII characters in mail.
224 If t, that means do allow it. nil means don't allow it.
225 `query' means ask the user each time.
226 Including non-ASCII characters in a mail message can be problematical
227 for the recipient, who may not know how to decode them properly."
228 :type '(choice (const t) (const nil) (const query))
229 :group 'sendmail)
230
231 ;; Note: could use /usr/ucb/mail instead of sendmail;
232 ;; options -t, and -v if not interactive.
233 (defvar mail-mailer-swallows-blank-line
234 (if (and (string-match "sparc-sun-sunos\\(\\'\\|[^5]\\)" system-configuration)
235 (file-readable-p "/etc/sendmail.cf")
236 (let ((buffer (get-buffer-create " *temp*")))
237 (unwind-protect
238 (save-excursion
239 (set-buffer buffer)
240 (insert-file-contents "/etc/sendmail.cf")
241 (goto-char (point-min))
242 (let ((case-fold-search nil))
243 (re-search-forward "^OR\\>" nil t)))
244 (kill-buffer buffer))))
245 ;; According to RFC822, "The field-name must be composed of printable
246 ;; ASCII characters (i.e. characters that have decimal values between
247 ;; 33 and 126, except colon)", i.e. any chars except ctl chars,
248 ;; space, or colon.
249 '(looking-at "[ \t]\\|[][!\"#$%&'()*+,-./0-9;<=>?@A-Z\\\\^_`a-z{|}~]+:"))
250 "Set this non-nil if the system's mailer runs the header and body together.
251 \(This problem exists on Sunos 4 when sendmail is run in remote mode.)
252 The value should be an expression to test whether the problem will
253 actually occur.")
254
255 (defvar mail-mode-syntax-table nil
256 "Syntax table used while in mail mode.")
257
258 (if (not mail-mode-syntax-table)
259 (progn
260 (setq mail-mode-syntax-table (copy-syntax-table text-mode-syntax-table))
261 (modify-syntax-entry ?% ". " mail-mode-syntax-table)))
262
263 (defvar mail-font-lock-keywords
264 (eval-when-compile
265 (let* ((cite-chars "[>|}]")
266 (cite-prefix "A-Za-z")
267 (cite-suffix (concat cite-prefix "0-9_.@-`'\"")))
268 (list '("^\\(To\\|Newsgroups\\):" . font-lock-function-name-face)
269 '("^\\(B?CC\\|Reply-to\\):" . font-lock-keyword-face)
270 '("^\\(Subject:\\)[ \t]*\\(.+\\)?"
271 (1 font-lock-comment-face) (2 font-lock-type-face nil t))
272 ;; Use EVAL to delay in case `mail-header-separator' gets changed.
273 '(eval .
274 (let ((separator (if (zerop (length mail-header-separator))
275 " \\`\\' "
276 (regexp-quote mail-header-separator))))
277 (cons (concat "^" separator "$") 'font-lock-warning-face)))
278 ;; Use MATCH-ANCHORED to effectively anchor the regexp left side.
279 `(,cite-chars
280 (,(concat "\\=[ \t]*"
281 "\\(\\([" cite-prefix "]+[" cite-suffix "]*\\)?"
282 "\\(" cite-chars "[ \t]*\\)\\)+"
283 "\\(.*\\)")
284 (beginning-of-line) (end-of-line)
285 (2 font-lock-constant-face nil t)
286 (4 font-lock-comment-face nil t)))
287 '("^\\(X-[A-Za-z0-9-]+\\|In-reply-to\\):.*"
288 . font-lock-string-face))))
289 "Additional expressions to highlight in Mail mode.")
290
291 (defcustom mail-send-hook nil
292 "Normal hook run before sending mail, in Mail mode."
293 :type 'hook
294 :group 'sendmail)
295 \f
296 (defun sendmail-sync-aliases ()
297 (let ((modtime (nth 5 (file-attributes mail-personal-alias-file))))
298 (or (equal mail-alias-modtime modtime)
299 (setq mail-alias-modtime modtime
300 mail-aliases t))))
301
302 (defun mail-setup (to subject in-reply-to cc replybuffer actions)
303 (or mail-default-reply-to
304 (setq mail-default-reply-to (getenv "REPLYTO")))
305 (sendmail-sync-aliases)
306 (if (eq mail-aliases t)
307 (progn
308 (setq mail-aliases nil)
309 (if (file-exists-p mail-personal-alias-file)
310 (build-mail-aliases))))
311 ;; Don't leave this around from a previous message.
312 (kill-local-variable 'buffer-file-coding-system)
313 (kill-local-variable 'enable-multibyte-characters)
314 (if current-input-method
315 (inactivate-input-method))
316 (setq mail-send-actions actions)
317 (setq mail-reply-action replybuffer)
318 (goto-char (point-min))
319 (insert "To: ")
320 (save-excursion
321 (if to
322 ;; Here removed code to extract names from within <...>
323 ;; on the assumption that mail-strip-quoted-names
324 ;; has been called and has done so.
325 (let ((fill-prefix "\t")
326 (address-start (point)))
327 (insert to "\n")
328 (fill-region-as-paragraph address-start (point-max)))
329 (newline))
330 (if cc
331 (let ((fill-prefix "\t")
332 (address-start (progn (insert "CC: ") (point))))
333 (insert cc "\n")
334 (fill-region-as-paragraph address-start (point-max))))
335 (if in-reply-to
336 (let ((fill-prefix "\t")
337 (fill-column 78)
338 (address-start (point)))
339 (insert "In-reply-to: " in-reply-to "\n")
340 (fill-region-as-paragraph address-start (point-max))))
341 (insert "Subject: " (or subject "") "\n")
342 (if mail-default-headers
343 (insert mail-default-headers))
344 (if mail-default-reply-to
345 (insert "Reply-to: " mail-default-reply-to "\n"))
346 (if mail-self-blind
347 (insert "BCC: " user-mail-address "\n"))
348 (if mail-archive-file-name
349 (insert "FCC: " mail-archive-file-name "\n"))
350 (put-text-property (point)
351 (progn
352 (insert mail-header-separator "\n")
353 (1- (point)))
354 'category 'mail-header-separator)
355 ;; Insert the signature. But remember the beginning of the message.
356 (if to (setq to (point)))
357 (cond ((eq mail-signature t)
358 (if (file-exists-p mail-signature-file)
359 (progn
360 (insert "\n\n-- \n")
361 (insert-file-contents mail-signature-file))))
362 (mail-signature
363 (insert mail-signature)))
364 (goto-char (point-max))
365 (or (bolp) (newline)))
366 (if to (goto-char to))
367 (or to subject in-reply-to
368 (set-buffer-modified-p nil))
369 (run-hooks 'mail-setup-hook))
370 \f
371 ;;;###autoload
372 (defun mail-mode ()
373 "Major mode for editing mail to be sent.
374 Like Text Mode but with these additional commands:
375 \\[mail-send] mail-send (send the message) \\[mail-send-and-exit] mail-send-and-exit
376 Here are commands that move to a header field (and create it if there isn't):
377 \\[mail-to] move to To: \\[mail-subject] move to Subject:
378 \\[mail-cc] move to CC: \\[mail-bcc] move to BCC:
379 \\[mail-fcc] move to FCC:
380 \\[mail-text] mail-text (move to beginning of message text).
381 \\[mail-signature] mail-signature (insert `mail-signature-file' file).
382 \\[mail-yank-original] mail-yank-original (insert current message, in Rmail).
383 \\[mail-fill-yanked-message] mail-fill-yanked-message (fill what was yanked).
384 \\[mail-sent-via] mail-sent-via (add a Sent-via field for each To or CC)."
385 (interactive)
386 (kill-all-local-variables)
387 (make-local-variable 'mail-reply-action)
388 (make-local-variable 'mail-send-actions)
389 (set-syntax-table mail-mode-syntax-table)
390 (use-local-map mail-mode-map)
391 (setq local-abbrev-table text-mode-abbrev-table)
392 (setq major-mode 'mail-mode)
393 (setq mode-name "Mail")
394 (setq buffer-offer-save t)
395 (make-local-variable 'font-lock-defaults)
396 (setq font-lock-defaults '(mail-font-lock-keywords t))
397 (make-local-variable 'paragraph-separate)
398 (make-local-variable 'paragraph-start)
399 (make-local-variable 'normal-auto-fill-function)
400 (setq normal-auto-fill-function 'mail-mode-auto-fill)
401 (make-local-variable 'fill-paragraph-function)
402 (setq fill-paragraph-function 'mail-mode-fill-paragraph)
403 (make-local-variable 'adaptive-fill-regexp)
404 (setq adaptive-fill-regexp
405 (concat "[ \t]*[-a-z0-9A-Z]*>+[ \t]*\\|" adaptive-fill-regexp))
406 (make-local-variable 'adaptive-fill-first-line-regexp)
407 (setq adaptive-fill-first-line-regexp
408 (concat "[ \t]*[-a-z0-9A-Z]*>+[ \t]*\\|" adaptive-fill-first-line-regexp))
409 ;; `-- ' precedes the signature. `-----' appears at the start of the
410 ;; lines that delimit forwarded messages.
411 ;; Lines containing just >= 3 dashes, perhaps after whitespace,
412 ;; are also sometimes used and should be separators.
413 (setq paragraph-start (concat (regexp-quote mail-header-separator)
414 "$\\|[ \t]*[a-z0-9A-Z]*>+[ \t]*$\\|[ \t]*$\\|"
415 page-delimiter))
416 (setq paragraph-separate paragraph-start)
417 (run-hooks 'text-mode-hook 'mail-mode-hook))
418
419
420 (defun mail-header-end ()
421 "Return the buffer location of the end of headers, as a number."
422 (save-excursion
423 (rfc822-goto-eoh)
424 (point)))
425
426 (defun mail-text-start ()
427 "Return the buffer location of the start of text, as a number."
428 (save-excursion
429 (rfc822-goto-eoh)
430 (forward-line 1)
431 (point)))
432
433 (defun mail-sendmail-delimit-header ()
434 "Set up whatever header delimiter convention sendmail will use.
435 Concretely: replace the first blank line in the header with the separator."
436 (rfc822-goto-eoh)
437 (insert mail-header-separator)
438 (point))
439
440 (defun mail-sendmail-undelimit-header ()
441 "Remove header separator to put the message in correct form for sendmail.
442 Leave point at the start of the delimiter line."
443 (rfc822-goto-eoh)
444 (delete-region (point) (progn (end-of-line) (point))))
445
446 (defun mail-mode-auto-fill ()
447 "Carry out Auto Fill for Mail mode.
448 If within the headers, this makes the new lines into continuation lines."
449 (if (< (point) (mail-header-end))
450 (let ((old-line-start (save-excursion (beginning-of-line) (point))))
451 (if (do-auto-fill)
452 (save-excursion
453 (beginning-of-line)
454 (while (not (eq (point) old-line-start))
455 ;; Use insert-before-markers in case we're inserting
456 ;; before the saved value of point (which is common).
457 (insert-before-markers " ")
458 (forward-line -1))
459 t)))
460 (do-auto-fill)))
461
462 (defun mail-mode-fill-paragraph (arg)
463 ;; Do something special only if within the headers.
464 (if (< (point) (mail-header-end))
465 (let (beg end fieldname)
466 (re-search-backward "^[-a-zA-Z]+:" nil 'yes)
467 (setq beg (point))
468 (setq fieldname
469 (downcase (buffer-substring beg (1- (match-end 0)))))
470 (forward-line 1)
471 ;; Find continuation lines and get rid of their continuation markers.
472 (while (looking-at "[ \t]")
473 (delete-horizontal-space)
474 (forward-line 1))
475 (setq end (point-marker))
476 (goto-char beg)
477 ;; If this field contains addresses,
478 ;; make sure we can fill after each address.
479 (if (member fieldname
480 '("to" "cc" "bcc" "from" "reply-to"
481 "resent-to" "resent-cc" "resent-bcc"
482 "resent-from" "resent-reply-to"))
483 (while (search-forward "," end t)
484 (or (looking-at "[ \t]")
485 (insert " "))))
486 (fill-region-as-paragraph beg end)
487 ;; Mark all lines except the first as continuations.
488 (goto-char beg)
489 (forward-line 1)
490 (while (< (point) end)
491 (insert " ")
492 (forward-line 1))
493 (move-marker end nil)
494 t)))
495 \f
496 ;;; Set up keymap.
497
498 (if mail-mode-map
499 nil
500 (setq mail-mode-map (nconc (make-sparse-keymap) text-mode-map))
501 (define-key mail-mode-map "\M-\t" 'mail-complete)
502 (define-key mail-mode-map "\C-c?" 'describe-mode)
503 (define-key mail-mode-map "\C-c\C-f\C-t" 'mail-to)
504 (define-key mail-mode-map "\C-c\C-f\C-b" 'mail-bcc)
505 (define-key mail-mode-map "\C-c\C-f\C-f" 'mail-fcc)
506 (define-key mail-mode-map "\C-c\C-f\C-c" 'mail-cc)
507 (define-key mail-mode-map "\C-c\C-f\C-s" 'mail-subject)
508 (define-key mail-mode-map "\C-c\C-f\C-r" 'mail-reply-to)
509 (define-key mail-mode-map "\C-c\C-t" 'mail-text)
510 (define-key mail-mode-map "\C-c\C-y" 'mail-yank-original)
511 (define-key mail-mode-map "\C-c\C-r" 'mail-yank-region)
512 (define-key mail-mode-map "\C-c\C-q" 'mail-fill-yanked-message)
513 (define-key mail-mode-map "\C-c\C-w" 'mail-signature)
514 (define-key mail-mode-map "\C-c\C-v" 'mail-sent-via)
515 (define-key mail-mode-map "\C-c\C-c" 'mail-send-and-exit)
516 (define-key mail-mode-map "\C-c\C-s" 'mail-send)
517 (define-key mail-mode-map "\C-c\C-i" 'mail-attach-file))
518
519 (define-key mail-mode-map [menu-bar mail]
520 (cons "Mail" (make-sparse-keymap "Mail")))
521
522 (define-key mail-mode-map [menu-bar mail fill]
523 '("Fill Citation" . mail-fill-yanked-message))
524
525 (define-key mail-mode-map [menu-bar mail yank]
526 '("Cite Original" . mail-yank-original))
527
528 (define-key mail-mode-map [menu-bar mail signature]
529 '("Insert Signature" . mail-signature))
530
531 (define-key mail-mode-map [menu-bar mail mail-sep]
532 '("--"))
533
534 (define-key mail-mode-map [menu-bar mail cancel]
535 '("Cancel" . mail-dont-send))
536
537 (define-key mail-mode-map [menu-bar mail send-stay]
538 '("Send, Keep Editing" . mail-send))
539
540 (define-key mail-mode-map [menu-bar mail send]
541 '("Send Message" . mail-send-and-exit))
542
543 (define-key mail-mode-map [menu-bar headers]
544 (cons "Headers" (make-sparse-keymap "Move to Header")))
545
546 (define-key mail-mode-map [menu-bar headers text]
547 '("Text" . mail-text))
548
549 (define-key mail-mode-map [menu-bar headers expand-aliases]
550 '("Expand Aliases" . expand-mail-aliases))
551
552 (define-key mail-mode-map [menu-bar headers sent-via]
553 '("Sent Via" . mail-sent-via))
554
555 (define-key mail-mode-map [menu-bar headers reply-to]
556 '("Reply-To" . mail-reply-to))
557
558 (define-key mail-mode-map [menu-bar headers bcc]
559 '("Bcc" . mail-bcc))
560
561 (define-key mail-mode-map [menu-bar headers fcc]
562 '("Fcc" . mail-fcc))
563
564 (define-key mail-mode-map [menu-bar headers cc]
565 '("Cc" . mail-cc))
566
567 (define-key mail-mode-map [menu-bar headers subject]
568 '("Subject" . mail-subject))
569
570 (define-key mail-mode-map [menu-bar headers to]
571 '("To" . mail-to))
572 \f
573 ;; User-level commands for sending.
574
575 (defun mail-send-and-exit (arg)
576 "Send message like `mail-send', then, if no errors, exit from mail buffer.
577 Prefix arg means don't delete this window."
578 (interactive "P")
579 (mail-send)
580 (mail-bury arg))
581
582 (defun mail-dont-send (arg)
583 "Don't send the message you have been editing.
584 Prefix arg means don't delete this window."
585 (interactive "P")
586 (mail-bury arg))
587
588 (defun mail-bury (arg)
589 "Bury this mail buffer."
590 (let ((newbuf (other-buffer (current-buffer))))
591 (bury-buffer (current-buffer))
592 (if (and (or (window-dedicated-p (frame-selected-window))
593 (cdr (assq 'mail-dedicated-frame (frame-parameters))))
594 (not (null (delq (selected-frame) (visible-frame-list)))))
595 (delete-frame (selected-frame))
596 (let (rmail-flag summary-buffer)
597 (and (not arg)
598 (not (one-window-p))
599 (save-excursion
600 (set-buffer (window-buffer (next-window (selected-window) 'not)))
601 (setq rmail-flag (eq major-mode 'rmail-mode))
602 (setq summary-buffer
603 (and mail-bury-selects-summary
604 (boundp 'rmail-summary-buffer)
605 rmail-summary-buffer
606 (buffer-name rmail-summary-buffer)
607 (not (get-buffer-window rmail-summary-buffer))
608 rmail-summary-buffer))))
609 (if rmail-flag
610 ;; If the Rmail buffer has a summary, show that.
611 (if summary-buffer (switch-to-buffer summary-buffer)
612 (delete-window))
613 (switch-to-buffer newbuf))))))
614
615 (defun mail-send ()
616 "Send the message in the current buffer.
617 If `mail-interactive' is non-nil, wait for success indication
618 or error messages, and inform user.
619 Otherwise any failure is reported in a message back to
620 the user from the mailer."
621 (interactive)
622 (if (if buffer-file-name
623 (y-or-n-p "Send buffer contents as mail message? ")
624 (or (buffer-modified-p)
625 (y-or-n-p "Message already sent; resend? ")))
626 (let ((inhibit-read-only t)
627 (opoint (point)))
628 (when (and enable-multibyte-characters
629 (not (eq mail-send-nonascii t)))
630 (goto-char (point-min))
631 (skip-chars-forward "\0-\177")
632 (or (= (point) (point-max))
633 (if (eq mail-send-nonascii 'query)
634 (or (y-or-n-p "Message contains non-ASCII characters; send anyway? ")
635 (error "Aborted"))
636 (error "Message contains non-ASCII characters"))))
637 ;; Complain about any invalid line.
638 (goto-char (point-min))
639 (while (< (point) (mail-header-end))
640 (unless (looking-at "[ \t]\\|.*:\\|$")
641 (push-mark opoint)
642 (error "Invalid header line (maybe a continuation line lacks initial whitespace)"))
643 (forward-line 1))
644 (goto-char opoint)
645 (run-hooks 'mail-send-hook)
646 (message "Sending...")
647 (funcall send-mail-function)
648 ;; Now perform actions on successful sending.
649 (while mail-send-actions
650 (condition-case nil
651 (apply (car (car mail-send-actions))
652 (cdr (car mail-send-actions)))
653 (error))
654 (setq mail-send-actions (cdr mail-send-actions)))
655 (message "Sending...done")
656 ;; If buffer has no file, mark it as unmodified and delete autosave.
657 (if (not buffer-file-name)
658 (progn
659 (set-buffer-modified-p nil)
660 (delete-auto-save-file-if-necessary t))))))
661 \f
662 ;; This does the real work of sending a message via sendmail.
663 ;; It is called via the variable send-mail-function.
664
665 ;;;###autoload
666 (defvar sendmail-coding-system nil
667 "Coding system to encode the outgoing mail.")
668
669 (defun sendmail-send-it ()
670 (require 'mail-utils)
671 (let ((errbuf (if mail-interactive
672 (generate-new-buffer " sendmail errors")
673 0))
674 (tembuf (generate-new-buffer " sendmail temp"))
675 (case-fold-search nil)
676 resend-to-addresses
677 delimline
678 fcc-was-found
679 (mailbuf (current-buffer))
680 (sendmail-coding-system
681 (if (local-variable-p 'buffer-file-coding-system)
682 buffer-file-coding-system
683 (or sendmail-coding-system
684 default-buffer-file-coding-system
685 'iso-latin-1))))
686 (if (fboundp select-safe-coding-system-function)
687 (setq sendmail-coding-system
688 (funcall select-safe-coding-system-function
689 (point-min) (point-max) sendmail-coding-system)))
690 (unwind-protect
691 (save-excursion
692 (set-buffer tembuf)
693 (erase-buffer)
694 (insert-buffer-substring mailbuf)
695 (goto-char (point-max))
696 ;; require one newline at the end.
697 (or (= (preceding-char) ?\n)
698 (insert ?\n))
699 ;; Change header-delimiter to be what sendmail expects.
700 (goto-char (mail-header-end))
701 (delete-region (point) (progn (end-of-line) (point)))
702 (setq delimline (point-marker))
703 (sendmail-sync-aliases)
704 (if mail-aliases
705 (expand-mail-aliases (point-min) delimline))
706 (goto-char (point-min))
707 ;; Ignore any blank lines in the header
708 (while (and (re-search-forward "\n\n\n*" delimline t)
709 (< (point) delimline))
710 (replace-match "\n"))
711 (goto-char (point-min))
712 (let ((case-fold-search t))
713 (goto-char (point-min))
714 (while (re-search-forward "^Resent-\\(to\\|cc\\|bcc\\):" delimline t)
715 (setq resend-to-addresses
716 (save-restriction
717 (narrow-to-region (point)
718 (save-excursion
719 (forward-line 1)
720 (while (looking-at "^[ \t]")
721 (forward-line 1))
722 (point)))
723 (append (mail-parse-comma-list)
724 resend-to-addresses)))
725 ;; Delete Resent-BCC ourselves
726 (if (save-excursion (beginning-of-line)
727 (looking-at "resent-bcc"))
728 (delete-region (save-excursion (beginning-of-line) (point))
729 (save-excursion (end-of-line) (1+ (point))))))
730 ;;; Apparently this causes a duplicate Sender.
731 ;;; ;; If the From is different than current user, insert Sender.
732 ;;; (goto-char (point-min))
733 ;;; (and (re-search-forward "^From:" delimline t)
734 ;;; (progn
735 ;;; (require 'mail-utils)
736 ;;; (not (string-equal
737 ;;; (mail-strip-quoted-names
738 ;;; (save-restriction
739 ;;; (narrow-to-region (point-min) delimline)
740 ;;; (mail-fetch-field "From")))
741 ;;; (user-login-name))))
742 ;;; (progn
743 ;;; (forward-line 1)
744 ;;; (insert "Sender: " (user-login-name) "\n")))
745 ;; Don't send out a blank subject line
746 (goto-char (point-min))
747 (if (re-search-forward "^Subject:\\([ \t]*\n\\)+\\b" delimline t)
748 (replace-match "")
749 ;; This one matches a Subject just before the header delimiter.
750 (if (and (re-search-forward "^Subject:\\([ \t]*\n\\)+" delimline t)
751 (= (match-end 0) delimline))
752 (replace-match "")))
753 ;; Put the "From:" field in unless for some odd reason
754 ;; they put one in themselves.
755 (goto-char (point-min))
756 (if (not (re-search-forward "^From:" delimline t))
757 (let* ((login user-mail-address)
758 (fullname (user-full-name))
759 (quote-fullname nil))
760 (if (string-match "[\200-\377]" fullname)
761 (setq fullname (mail-quote-printable fullname t)
762 quote-fullname t))
763 (cond ((eq mail-from-style 'angles)
764 (insert "From: " fullname)
765 (let ((fullname-start (+ (point-min) 6))
766 (fullname-end (point-marker)))
767 (goto-char fullname-start)
768 ;; Look for a character that cannot appear unquoted
769 ;; according to RFC 822.
770 (if (or (re-search-forward "[^- !#-'*+/-9=?A-Z^-~]"
771 fullname-end 1)
772 quote-fullname)
773 (progn
774 ;; Quote fullname, escaping specials.
775 (goto-char fullname-start)
776 (insert "\"")
777 (while (re-search-forward "[\"\\]"
778 fullname-end 1)
779 (replace-match "\\\\\\&" t))
780 (insert "\""))))
781 (insert " <" login ">\n"))
782 ((eq mail-from-style 'parens)
783 (insert "From: " login " (")
784 (let ((fullname-start (point)))
785 (if quote-fullname
786 (insert "\""))
787 (insert fullname)
788 (if quote-fullname
789 (insert "\""))
790 (let ((fullname-end (point-marker)))
791 (goto-char fullname-start)
792 ;; RFC 822 says \ and nonmatching parentheses
793 ;; must be escaped in comments.
794 ;; Escape every instance of ()\ ...
795 (while (re-search-forward "[()\\]" fullname-end 1)
796 (replace-match "\\\\\\&" t))
797 ;; ... then undo escaping of matching parentheses,
798 ;; including matching nested parentheses.
799 (goto-char fullname-start)
800 (while (re-search-forward
801 "\\(\\=\\|[^\\]\\(\\\\\\\\\\)*\\)\\\\(\\(\\([^\\]\\|\\\\\\\\\\)*\\)\\\\)"
802 fullname-end 1)
803 (replace-match "\\1(\\3)" t)
804 (goto-char fullname-start))))
805 (insert ")\n"))
806 ((null mail-from-style)
807 (insert "From: " login "\n")))))
808 ;; Insert an extra newline if we need it to work around
809 ;; Sun's bug that swallows newlines.
810 (goto-char (1+ delimline))
811 (if (eval mail-mailer-swallows-blank-line)
812 (newline))
813 ;; Find and handle any FCC fields.
814 (goto-char (point-min))
815 (if (re-search-forward "^FCC:" delimline t)
816 (progn
817 (setq fcc-was-found t)
818 (mail-do-fcc delimline)))
819 (if mail-interactive
820 (save-excursion
821 (set-buffer errbuf)
822 (erase-buffer))))
823 (goto-char (point-min))
824 (if (let ((case-fold-search t))
825 (re-search-forward "^To:\\|^cc:\\|^bcc:\\|^resent-to:\
826 \\|^resent-cc:\\|^resent-bcc:"
827 delimline t))
828 (let ((default-directory "/")
829 (coding-system-for-write sendmail-coding-system))
830 (apply 'call-process-region
831 (append (list (point-min) (point-max)
832 (if (boundp 'sendmail-program)
833 sendmail-program
834 "/usr/lib/sendmail")
835 nil errbuf nil "-oi")
836 ;; Always specify who from,
837 ;; since some systems have broken sendmails.
838 (list "-f" (user-login-name))
839 ;;; ;; Don't say "from root" if running under su.
840 ;;; (and (equal (user-real-login-name) "root")
841 ;;; (list "-f" (user-login-name)))
842 (and mail-alias-file
843 (list (concat "-oA" mail-alias-file)))
844 (if mail-interactive
845 ;; These mean "report errors to terminal"
846 ;; and "deliver interactively"
847 '("-oep" "-odi")
848 ;; These mean "report errors by mail"
849 ;; and "deliver in background".
850 '("-oem" "-odb"))
851 ;; Get the addresses from the message
852 ;; unless this is a resend.
853 ;; We must not do that for a resend
854 ;; because we would find the original addresses.
855 ;; For a resend, include the specific addresses.
856 (or resend-to-addresses
857 '("-t")))))
858 (or fcc-was-found
859 (error "No recipients")))
860 (if mail-interactive
861 (save-excursion
862 (set-buffer errbuf)
863 (goto-char (point-min))
864 (while (re-search-forward "\n\n* *" nil t)
865 (replace-match "; "))
866 (if (not (zerop (buffer-size)))
867 (error "Sending...failed to %s"
868 (buffer-substring (point-min) (point-max)))))))
869 (kill-buffer tembuf)
870 (if (bufferp errbuf)
871 (kill-buffer errbuf)))))
872
873 (defun mail-do-fcc (header-end)
874 (let (fcc-list
875 (rmailbuf (current-buffer))
876 (time (current-time))
877 (tembuf (generate-new-buffer " rmail output"))
878 (case-fold-search t))
879 (save-excursion
880 (goto-char (point-min))
881 (while (re-search-forward "^FCC:[ \t]*" header-end t)
882 (setq fcc-list (cons (buffer-substring (point)
883 (progn
884 (end-of-line)
885 (skip-chars-backward " \t")
886 (point)))
887 fcc-list))
888 (delete-region (match-beginning 0)
889 (progn (forward-line 1) (point))))
890 (set-buffer tembuf)
891 (erase-buffer)
892 ;; This initial newline is written out if the fcc file already exists.
893 (insert "\nFrom " (user-login-name) " "
894 (current-time-string time) "\n")
895 ;; Insert the time zone before the year.
896 (forward-char -1)
897 (forward-word -1)
898 (require 'mail-utils)
899 (insert (mail-rfc822-time-zone time) " ")
900 (goto-char (point-max))
901 (insert-buffer-substring rmailbuf)
902 ;; Make sure messages are separated.
903 (goto-char (point-max))
904 (insert ?\n)
905 (goto-char 2)
906 ;; ``Quote'' "^From " as ">From "
907 ;; (note that this isn't really quoting, as there is no requirement
908 ;; that "^[>]+From " be quoted in the same transparent way.)
909 (let ((case-fold-search nil))
910 (while (search-forward "\nFrom " nil t)
911 (forward-char -5)
912 (insert ?>)))
913 (while fcc-list
914 (let* ((buffer (find-buffer-visiting (car fcc-list)))
915 (curbuf (current-buffer))
916 dont-write-the-file
917 buffer-matches-file
918 (beg (point-min)) (end (point-max))
919 (beg2 (save-excursion (goto-char (point-min))
920 (forward-line 2) (point))))
921 (if buffer
922 ;; File is present in a buffer => append to that buffer.
923 (save-excursion
924 (set-buffer buffer)
925 (setq buffer-matches-file
926 (and (not (buffer-modified-p))
927 (verify-visited-file-modtime buffer)))
928 ;; Keep the end of the accessible portion at the same place
929 ;; unless it is the end of the buffer.
930 (let ((max (if (/= (1+ (buffer-size)) (point-max))
931 (point-max))))
932 (unwind-protect
933 ;; Code below lifted from rmailout.el
934 ;; function rmail-output-to-rmail-file:
935 (let ((buffer-read-only nil)
936 (msg (and (boundp 'rmail-current-message)
937 rmail-current-message)))
938 ;; If MSG is non-nil, buffer is in RMAIL mode.
939 (if msg
940 (progn
941 (rmail-maybe-set-message-counters)
942 (widen)
943 (narrow-to-region (point-max) (point-max))
944 (insert "\C-l\n0, unseen,,\n*** EOOH ***\n"
945 "Date: " (mail-rfc822-date) "\n")
946 (insert-buffer-substring curbuf beg2 end)
947 (insert "\n\C-_")
948 (goto-char (point-min))
949 (widen)
950 (search-backward "\n\^_")
951 (narrow-to-region (point) (point-max))
952 (rmail-count-new-messages t)
953 (rmail-show-message msg)
954 (setq max nil))
955 ;; Output file not in rmail mode
956 ;; => just insert at the end.
957 (narrow-to-region (point-min) (1+ (buffer-size)))
958 (goto-char (point-max))
959 (insert-buffer-substring curbuf beg end))
960 (or buffer-matches-file
961 (progn
962 (if (y-or-n-p (format "Save file %s? "
963 (car fcc-list)))
964 (save-buffer))
965 (setq dont-write-the-file t))))
966 (if max (narrow-to-region (point-min) max))))))
967 ;; Append to the file directly,
968 ;; unless we've already taken care of it.
969 (unless dont-write-the-file
970 (if (and (file-exists-p (car fcc-list))
971 (mail-file-babyl-p (car fcc-list)))
972 ;; If the file is a Babyl file,
973 ;; convert the message to Babyl format.
974 (let ((coding-system-for-write
975 (or rmail-file-coding-system
976 'emacs-mule)))
977 (save-excursion
978 (set-buffer (get-buffer-create " mail-temp"))
979 (setq buffer-read-only nil)
980 (erase-buffer)
981 (insert "\C-l\n0, unseen,,\n*** EOOH ***\n"
982 "Date: " (mail-rfc822-date) "\n")
983 (insert-buffer-substring curbuf beg2 end)
984 (insert "\n\C-_")
985 (write-region (point-min) (point-max) (car fcc-list) t)
986 (erase-buffer)))
987 (write-region
988 (1+ (point-min)) (point-max) (car fcc-list) t)))
989 (and buffer (not dont-write-the-file)
990 (with-current-buffer buffer
991 (set-visited-file-modtime))))
992 (setq fcc-list (cdr fcc-list))))
993 (kill-buffer tembuf)))
994
995 (defun mail-sent-via ()
996 "Make a Sent-via header line from each To or CC header line."
997 (interactive)
998 (save-excursion
999 ;; put a marker at the end of the header
1000 (let ((end (make-marker (mail-header-end)))
1001 (case-fold-search t)
1002 to-line)
1003 (goto-char (point-min))
1004 ;; search for the To: lines and make Sent-via: lines from them
1005 ;; search for the next To: line
1006 (while (re-search-forward "^\\(to\\|cc\\):" end t)
1007 ;; Grab this line plus all its continuations, sans the `to:'.
1008 (let ((to-line
1009 (buffer-substring (point)
1010 (progn
1011 (if (re-search-forward "^[^ \t\n]" end t)
1012 (backward-char 1)
1013 (goto-char end))
1014 (point)))))
1015 ;; Insert a copy, with altered header field name.
1016 (insert-before-markers "Sent-via:" to-line))))))
1017 \f
1018 (defun mail-to ()
1019 "Move point to end of To-field."
1020 (interactive)
1021 (expand-abbrev)
1022 (mail-position-on-field "To"))
1023
1024 (defun mail-subject ()
1025 "Move point to end of Subject-field."
1026 (interactive)
1027 (expand-abbrev)
1028 (mail-position-on-field "Subject"))
1029
1030 (defun mail-cc ()
1031 "Move point to end of CC-field. Create a CC field if none."
1032 (interactive)
1033 (expand-abbrev)
1034 (or (mail-position-on-field "cc" t)
1035 (progn (mail-position-on-field "to")
1036 (insert "\nCC: "))))
1037
1038 (defun mail-bcc ()
1039 "Move point to end of BCC-field. Create a BCC field if none."
1040 (interactive)
1041 (expand-abbrev)
1042 (or (mail-position-on-field "bcc" t)
1043 (progn (mail-position-on-field "to")
1044 (insert "\nBCC: "))))
1045
1046 (defun mail-fcc (folder)
1047 "Add a new FCC field, with file name completion."
1048 (interactive "FFolder carbon copy: ")
1049 (expand-abbrev)
1050 (or (mail-position-on-field "fcc" t) ;Put new field after exiting FCC.
1051 (mail-position-on-field "to"))
1052 (insert "\nFCC: " folder))
1053
1054 (defun mail-reply-to ()
1055 "Move point to end of Reply-To-field."
1056 (interactive)
1057 (expand-abbrev)
1058 (mail-position-on-field "Reply-To"))
1059
1060 (defun mail-position-on-field (field &optional soft)
1061 (let (end
1062 (case-fold-search t))
1063 (setq end (mail-header-end))
1064 (goto-char (point-min))
1065 (if (re-search-forward (concat "^" (regexp-quote field) ":") end t)
1066 (progn
1067 (re-search-forward "^[^ \t]" nil 'move)
1068 (beginning-of-line)
1069 (skip-chars-backward "\n")
1070 t)
1071 (or soft
1072 (progn (goto-char end)
1073 (insert field ": \n")
1074 (skip-chars-backward "\n")))
1075 nil)))
1076
1077 (defun mail-text ()
1078 "Move point to beginning of message text."
1079 (interactive)
1080 (expand-abbrev)
1081 (goto-char (mail-text-start)))
1082 \f
1083 (defun mail-signature (atpoint)
1084 "Sign letter with contents of the file `mail-signature-file'.
1085 Prefix arg means put contents at point."
1086 (interactive "P")
1087 (save-excursion
1088 (or atpoint
1089 (goto-char (point-max)))
1090 (skip-chars-backward " \t\n")
1091 (end-of-line)
1092 (or atpoint
1093 (delete-region (point) (point-max)))
1094 (insert "\n\n-- \n")
1095 (insert-file-contents (expand-file-name mail-signature-file))))
1096
1097 (defun mail-fill-yanked-message (&optional justifyp)
1098 "Fill the paragraphs of a message yanked into this one.
1099 Numeric argument means justify as well."
1100 (interactive "P")
1101 (save-excursion
1102 (goto-char (mail-text-start))
1103 (fill-individual-paragraphs (point)
1104 (point-max)
1105 justifyp
1106 t)))
1107
1108 (defun mail-indent-citation ()
1109 "Modify text just inserted from a message to be cited.
1110 The inserted text should be the region.
1111 When this function returns, the region is again around the modified text.
1112
1113 Normally, indent each nonblank line `mail-indentation-spaces' spaces.
1114 However, if `mail-yank-prefix' is non-nil, insert that prefix on each line."
1115 (mail-yank-clear-headers (region-beginning) (region-end))
1116 (if (null mail-yank-prefix)
1117 (indent-rigidly (region-beginning) (region-end)
1118 mail-indentation-spaces)
1119 (save-excursion
1120 (let ((end (set-marker (make-marker) (region-end))))
1121 (goto-char (region-beginning))
1122 (while (< (point) end)
1123 (insert mail-yank-prefix)
1124 (forward-line 1))))))
1125
1126 (defun mail-yank-original (arg)
1127 "Insert the message being replied to, if any (in rmail).
1128 Puts point after the text and mark before.
1129 Normally, indents each nonblank line ARG spaces (default 3).
1130 However, if `mail-yank-prefix' is non-nil, insert that prefix on each line.
1131
1132 Just \\[universal-argument] as argument means don't indent, insert no prefix,
1133 and don't delete any header fields."
1134 (interactive "P")
1135 (if mail-reply-action
1136 (let ((start (point))
1137 (original mail-reply-action))
1138 (and (consp original) (eq (car original) 'insert-buffer)
1139 (setq original (nth 1 original)))
1140 (if (consp original)
1141 (apply (car original) (cdr original))
1142 ;; If the original message is in another window in the same frame,
1143 ;; delete that window to save screen space.
1144 ;; t means don't alter other frames.
1145 (delete-windows-on original t)
1146 (insert-buffer original))
1147 (if (consp arg)
1148 nil
1149 (goto-char start)
1150 (let ((mail-indentation-spaces (if arg (prefix-numeric-value arg)
1151 mail-indentation-spaces))
1152 (mark-even-if-inactive t))
1153 (if mail-citation-hook
1154 (run-hooks 'mail-citation-hook)
1155 (if mail-yank-hooks
1156 (run-hooks 'mail-yank-hooks)
1157 (mail-indent-citation)))))
1158 ;; This is like exchange-point-and-mark, but doesn't activate the mark.
1159 ;; It is cleaner to avoid activation, even though the command
1160 ;; loop would deactivate the mark because we inserted text.
1161 (goto-char (prog1 (mark t)
1162 (set-marker (mark-marker) (point) (current-buffer))))
1163 (if (not (eolp)) (insert ?\n)))))
1164
1165 (defun mail-yank-clear-headers (start end)
1166 (if (< end start)
1167 (let (temp)
1168 (setq temp start start end end temp)))
1169 (if mail-yank-ignored-headers
1170 (save-excursion
1171 (goto-char start)
1172 (if (search-forward "\n\n" end t)
1173 (save-restriction
1174 (narrow-to-region start (point))
1175 (goto-char start)
1176 (while (let ((case-fold-search t))
1177 (re-search-forward mail-yank-ignored-headers nil t))
1178 (beginning-of-line)
1179 (delete-region (point)
1180 (progn (re-search-forward "\n[^ \t]")
1181 (forward-char -1)
1182 (point)))))))))
1183
1184 (defun mail-yank-region (arg)
1185 "Insert the selected region from the message being replied to.
1186 Puts point after the text and mark before.
1187 Normally, indents each nonblank line ARG spaces (default 3).
1188 However, if `mail-yank-prefix' is non-nil, insert that prefix on each line.
1189
1190 Just \\[universal-argument] as argument means don't indent, insert no prefix,
1191 and don't delete any header fields."
1192 (interactive "P")
1193 (and (consp mail-reply-action)
1194 (eq (car mail-reply-action) 'insert-buffer)
1195 (let ((buffer (nth 1 mail-reply-action))
1196 (start (point)))
1197 ;; Insert the citation text.
1198 (insert (with-current-buffer buffer
1199 (buffer-substring (point) (mark))))
1200 (push-mark start)
1201 ;; Indent or otherwise annotate the citation text.
1202 (if (consp arg)
1203 nil
1204 (let ((mail-indentation-spaces (if arg (prefix-numeric-value arg)
1205 mail-indentation-spaces)))
1206 (if mail-citation-hook
1207 (run-hooks 'mail-citation-hook)
1208 (if mail-yank-hooks
1209 (run-hooks 'mail-yank-hooks)
1210 (mail-indent-citation))))))))
1211 \f
1212 (defun mail-attach-file (&optional file)
1213 "Insert a file at the end of the buffer, with separator lines around it."
1214 (interactive "fAttach file: ")
1215 (save-excursion
1216 (goto-char (point-max))
1217 (or (bolp) (newline))
1218 (newline)
1219 (let ((start (point))
1220 middle)
1221 (insert (format "===File %s===" file))
1222 (insert-char ?= (max 0 (- 60 (current-column))))
1223 (newline)
1224 (setq middle (point))
1225 (insert "============================================================\n")
1226 (push-mark)
1227 (goto-char middle)
1228 (insert-file-contents file)
1229 (or (bolp) (newline))
1230 (goto-char start))))
1231 \f
1232 ;; Put these commands last, to reduce chance of lossage from quitting
1233 ;; in middle of loading the file.
1234
1235 ;;;###autoload (add-hook 'same-window-buffer-names "*mail*")
1236
1237 ;;;###autoload
1238 (defun mail (&optional noerase to subject in-reply-to cc replybuffer actions)
1239 "Edit a message to be sent. Prefix arg means resume editing (don't erase).
1240 When this function returns, the buffer `*mail*' is selected.
1241 The value is t if the message was newly initialized; otherwise, nil.
1242
1243 Optionally, the signature file `mail-signature-file' can be inserted at the
1244 end; see the variable `mail-signature'.
1245
1246 \\<mail-mode-map>
1247 While editing message, type \\[mail-send-and-exit] to send the message and exit.
1248
1249 Various special commands starting with C-c are available in sendmail mode
1250 to move to message header fields:
1251 \\{mail-mode-map}
1252
1253 If `mail-self-blind' is non-nil, a BCC to yourself is inserted
1254 when the message is initialized.
1255
1256 If `mail-default-reply-to' is non-nil, it should be an address (a string);
1257 a Reply-to: field with that address is inserted.
1258
1259 If `mail-archive-file-name' is non-nil, an FCC field with that file name
1260 is inserted.
1261
1262 The normal hook `mail-setup-hook' is run after the message is
1263 initialized. It can add more default fields to the message.
1264
1265 When calling from a program, the first argument if non-nil says
1266 not to erase the existing contents of the `*mail*' buffer.
1267
1268 The second through fifth arguments,
1269 TO, SUBJECT, IN-REPLY-TO and CC, specify if non-nil
1270 the initial contents of those header fields.
1271 These arguments should not have final newlines.
1272 The sixth argument REPLYBUFFER is a buffer which contains an
1273 original message being replied to, or else an action
1274 of the form (FUNCTION . ARGS) which says how to insert the original.
1275 Or it can be nil, if not replying to anything.
1276 The seventh argument ACTIONS is a list of actions to take
1277 if/when the message is sent. Each action looks like (FUNCTION . ARGS);
1278 when the message is sent, we apply FUNCTION to ARGS.
1279 This is how Rmail arranges to mark messages `answered'."
1280 (interactive "P")
1281 ;;; This is commented out because I found it was confusing in practice.
1282 ;;; It is easy enough to rename *mail* by hand with rename-buffer
1283 ;;; if you want to have multiple mail buffers.
1284 ;;; And then you can control which messages to save. --rms.
1285 ;;; (let ((index 1)
1286 ;;; buffer)
1287 ;;; ;; If requested, look for a mail buffer that is modified and go to it.
1288 ;;; (if noerase
1289 ;;; (progn
1290 ;;; (while (and (setq buffer
1291 ;;; (get-buffer (if (= 1 index) "*mail*"
1292 ;;; (format "*mail*<%d>" index))))
1293 ;;; (not (buffer-modified-p buffer)))
1294 ;;; (setq index (1+ index)))
1295 ;;; (if buffer (switch-to-buffer buffer)
1296 ;;; ;; If none exists, start a new message.
1297 ;;; ;; This will never re-use an existing unmodified mail buffer
1298 ;;; ;; (since index is not 1 anymore). Perhaps it should.
1299 ;;; (setq noerase nil))))
1300 ;;; ;; Unless we found a modified message and are happy, start a new message.
1301 ;;; (if (not noerase)
1302 ;;; (progn
1303 ;;; ;; Look for existing unmodified mail buffer.
1304 ;;; (while (and (setq buffer
1305 ;;; (get-buffer (if (= 1 index) "*mail*"
1306 ;;; (format "*mail*<%d>" index))))
1307 ;;; (buffer-modified-p buffer))
1308 ;;; (setq index (1+ index)))
1309 ;;; ;; If none, make a new one.
1310 ;;; (or buffer
1311 ;;; (setq buffer (generate-new-buffer "*mail*")))
1312 ;;; ;; Go there and initialize it.
1313 ;;; (switch-to-buffer buffer)
1314 ;;; (erase-buffer)
1315 ;;; (setq default-directory (expand-file-name "~/"))
1316 ;;; (auto-save-mode auto-save-default)
1317 ;;; (mail-mode)
1318 ;;; (mail-setup to subject in-reply-to cc replybuffer actions)
1319 ;;; (if (and buffer-auto-save-file-name
1320 ;;; (file-exists-p buffer-auto-save-file-name))
1321 ;;; (message "Auto save file for draft message exists; consider M-x mail-recover"))
1322 ;;; t))
1323 (pop-to-buffer "*mail*")
1324 ;; Put the auto-save file in the home dir
1325 ;; to avoid any danger that it can't be written.
1326 (if (file-exists-p (expand-file-name "~/"))
1327 (setq default-directory (expand-file-name "~/")))
1328 (auto-save-mode auto-save-default)
1329 (mail-mode)
1330 ;; Disconnect the buffer from its visited file
1331 ;; (in case the user has actually visited a file *mail*).
1332 ; (set-visited-file-name nil)
1333 (let (initialized)
1334 (and (not noerase)
1335 (if buffer-file-name
1336 (if (buffer-modified-p)
1337 (when (y-or-n-p "Buffer has unsaved changes; reinitialize it and discard them? ")
1338 (if (y-or-n-p "Disconnect buffer from visited file? ")
1339 (set-visited-file-name nil))
1340 t)
1341 (when (y-or-n-p "Reinitialize buffer, and disconnect it from the visited file? ")
1342 (set-visited-file-name nil)
1343 t))
1344 ;; A non-file-visiting buffer.
1345 (if (buffer-modified-p)
1346 (y-or-n-p "Unsent message being composed; erase it? ")
1347 t))
1348 (let ((inhibit-read-only t))
1349 (erase-buffer)
1350 (mail-setup to subject in-reply-to cc replybuffer actions)
1351 (setq initialized t)))
1352 (if (and buffer-auto-save-file-name
1353 (file-exists-p buffer-auto-save-file-name))
1354 (message "Auto save file for draft message exists; consider M-x mail-recover"))
1355 initialized))
1356
1357 (defun mail-recover ()
1358 "Reread contents of current buffer from its last auto-save file."
1359 (interactive)
1360 (let ((file-name (make-auto-save-file-name)))
1361 (cond ((save-window-excursion
1362 (if (not (eq system-type 'vax-vms))
1363 (with-output-to-temp-buffer "*Directory*"
1364 (buffer-disable-undo standard-output)
1365 (let ((default-directory "/"))
1366 (call-process
1367 "ls" nil standard-output nil "-l" file-name))))
1368 (yes-or-no-p (format "Recover auto save file %s? " file-name)))
1369 (let ((buffer-read-only nil))
1370 (erase-buffer)
1371 (insert-file-contents file-name nil)))
1372 (t (error "mail-recover cancelled")))))
1373
1374 ;;;###autoload
1375 (defun mail-other-window (&optional noerase to subject in-reply-to cc replybuffer sendactions)
1376 "Like `mail' command, but display mail buffer in another window."
1377 (interactive "P")
1378 (let ((pop-up-windows t)
1379 (special-display-buffer-names nil)
1380 (special-display-regexps nil)
1381 (same-window-buffer-names nil)
1382 (same-window-regexps nil))
1383 (pop-to-buffer "*mail*"))
1384 (mail noerase to subject in-reply-to cc replybuffer sendactions))
1385
1386 ;;;###autoload
1387 (defun mail-other-frame (&optional noerase to subject in-reply-to cc replybuffer sendactions)
1388 "Like `mail' command, but display mail buffer in another frame."
1389 (interactive "P")
1390 (let ((pop-up-frames t)
1391 (special-display-buffer-names nil)
1392 (special-display-regexps nil)
1393 (same-window-buffer-names nil)
1394 (same-window-regexps nil))
1395 (pop-to-buffer "*mail*"))
1396 (mail noerase to subject in-reply-to cc replybuffer sendactions))
1397
1398 ;;; Do not add anything but external entries on this page.
1399
1400 (provide 'sendmail)
1401
1402 ;;; sendmail.el ends here