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