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