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