Initial Commit
[packages] / xemacs-packages / mail-lib / sendmail.el
1 ;;; sendmail.el --- mail sending commands for Emacs.  -*- byte-compile-dynamic: t -*-
2
3 ;; Copyright (C) 1985, 86, 92, 93, 94, 95, 96, 98, 2000, 2001, 2002, 2003
4 ;;   Free Software Foundation, Inc.
5
6 ;; Maintainer: FSF
7 ;; Keywords: mail
8
9 ;; This file is part of XEmacs.
10
11 ;; XEmacs is free software; you can redistribute it and/or modify it
12 ;; under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; XEmacs is distributed in the hope that it will be useful, but
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19 ;; General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with XEmacs; see the file COPYING.  If not, write to the 
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26 ;;; Synched up with: Emacs 21.3
27
28 ;;; Commentary:
29
30 ;; This mode provides mail-sending facilities from within Emacs.  It is
31 ;; documented in the Emacs user's manual.
32
33 ;;; Code:
34
35 (require 'rfc822)
36
37 (defgroup sendmail nil
38   "Mail sending commands for Emacs."
39   :prefix "mail-"
40   :group 'mail)
41
42 ;;;###autoload
43 (defcustom mail-from-style 'angles "\
44 *Specifies how \"From:\" fields look.
45
46 If `nil', they contain just the return address like:
47         king@grassland.com
48 If `parens', they look like:
49         king@grassland.com (Elvis Parsley)
50 If `angles', they look like:
51         Elvis Parsley <king@grassland.com>
52 If `system-default', allows the mailer to insert its default From field
53 derived from the envelope-from address.
54
55 The email address passed to the mailer to specify the envelope-from
56 address is controlled by `mail-specify-envelope-from'."
57   :type '(choice (const nil) (const parens) (const angles)
58                  (const system-default))
59   :group 'sendmail)
60
61 ;;;###autoload
62 (defcustom mail-specify-envelope-from nil
63   "*If non-nil, specify the envelope-from address when sending mail.
64 The value used to specify it is whatever is found in
65 `mail-envelope-from', with `user-mail-address' as fallback.
66
67 On most systems, specifying the envelope-from address
68 is a privileged operation."
69   :type 'boolean
70   :group 'sendmail)
71
72 (defcustom mail-envelope-from nil
73   "*If non-nil, designate the envelope-from address when sending mail.
74 If this is nil while `mail-specify-envelope-from' is non-nil, the
75 content of `user-mail-address' is used."
76   :type '(choice (string :tag "From-name")
77                  (const :tag "Use From: header from message" header)
78                  (const :tag "Use `user-mail-address'" nil))
79   :group 'sendmail)
80
81 ;;;###autoload
82 (defcustom mail-self-blind nil "\
83 *Non-nil means insert BCC to self in messages to be sent.
84 This is done when the message is initialized,
85 so you can remove or alter the BCC field to override the default."
86   :type 'boolean
87   :group 'sendmail)
88
89 ;;;###autoload
90 (defcustom mail-interactive nil "\
91 *Non-nil means when sending a message wait for and display errors.
92 nil means let mailer mail back a message to report errors."
93   :type 'boolean
94   :group 'sendmail)
95
96 ;;; XEmacs change: moved rmail-ignored-headers here from rmail.el so that
97 ;;; the value of mail-yank-ignored-headers can default from it.  Both of
98 ;;; these end up in loaddefs.el, but "sendmail" comes before "rmail", so...
99 ;;;
100 ;;;###autoload
101 (defvar rmail-ignored-headers 
102   (purecopy
103    (concat
104     "^\\("
105     (mapconcat
106      'identity
107      '(;; RFC822
108        "Sender:" "References:" "Return-Path:" "Received:"
109        "[^: \t\n]*Message-ID:" "Errors-To:"
110        ;; RFC977 (NNTP)
111        "Path:" "Expires:" "Xref:" "Lines:" "Approved:" "Distribution:"
112        ;; SYSV mail:
113        "Content-Length:"
114        ;; MIME:
115        "Mime-Version:" "Content-Type:" "Content-Transfer-Encoding:"
116        ;; X400
117        "X400-Received:" "X400-Originator:" "X400-Mts-Identifier:"
118        "X400-Content-Type:" "Content-Identifier:"
119        ;; RMAIL and /usr/ucb/mail:
120        "Status:" "Summary-Line:"
121        ;; Supercite:
122        "X-Attribution:"
123        ;; Other random junk occasionally seen:
124        "Via:" "Sent-Via:" "Mail-From:" "Origin:" "Comments:" "Originator:"
125        "NF-ID:" "NF-From:" "Posting-Version:" "Posted:" "Posted-Date:"
126        "Date-Received:" "Relay-Version:" "Article-I\\.D\\.:" "NNTP-Version:"
127        "NNTP-Posting-Host:" "X-Mailer:" "X-Newsreader:" "News-Software:"
128        "X-Received:" "X-References:" "X-Envelope-To:"
129        "X-VMS-" "Remailed-" "X-Plantation:" "X-Windows:" "X-Pgp-"
130        )
131      "\\|")
132     "\\)"))
133   "*Gubbish header fields one would rather not see.")
134
135
136 ;;;###autoload
137 (defcustom mail-yank-ignored-headers
138   (purecopy
139    (concat rmail-ignored-headers "\\|"
140            "^\\("
141            (mapconcat 'identity
142                       '(;; RFC822
143                         "Resent-To:" "Resent-By:" "Resent-CC:"
144                         "To:" "Subject:" "In-Reply-To:"
145                         )
146                       "\\|")
147            "\\)")) "\
148 *Delete these headers from old message when it's inserted in a reply."
149   :type 'regexp
150   :group 'sendmail)
151
152 ;; Useful to set in site-init.el
153 ;;;###autoload
154 (defcustom send-mail-function (if (or (eq system-type 'windows-nt)
155                                       (eq system-type 'cygwin32))
156                                   ;; there is no sendmail on windows.
157                                   'smtpmail-send-it
158                                 'sendmail-send-it)
159   "Function to call to send the current buffer as mail.
160 The headers should be delimited by a line which is
161 not a valid RFC822 header or continuation line,
162 that match the variable `mail-header-separator'.
163 This is used by the default mail-sending commands.  See also
164 `message-send-mail-function' for use with the Message package."
165   :type '(radio (function-item sendmail-send-it :tag "Use Sendmail package")
166                 (function-item smtpmail-send-it :tag "Use SMTPmail package")
167                 (function-item feedmail-send-it :tag "Use Feedmail package")
168                 function)
169   :group 'sendmail)
170
171 ;;;###autoload
172 (defcustom mail-header-separator "--text follows this line--" "\
173 *Line used to separate headers from text in messages being composed."
174   :type 'string
175   :group 'sendmail)
176
177 ;; Set up mail-header-separator for use as a category text property.
178 (put 'mail-header-separator 'rear-nonsticky '(category))
179 ;;; This was a nice idea, for preventing accidental modification of
180 ;;; the separator.   But I found it also prevented or obstructed
181 ;;; certain deliberate operations, such as copying the separator line
182 ;;; up to the top to send myself a copy of an already sent outgoing message
183 ;;; and other things.  So I turned it off.  --rms.
184 ;;;(put 'mail-header-separator 'read-only t)
185
186 ;;;###autoload
187 (defcustom mail-archive-file-name nil "\
188 *Name of file to write all outgoing messages in, or nil for none.
189 This can be an inbox file or an Rmail file."
190   :type '(choice file (const nil))
191   :group 'sendmail)
192
193 ;;;###autoload
194 (defcustom mail-default-reply-to nil
195   "*Address to insert as default Reply-to field of outgoing messages.
196 If nil, it will be initialized from the REPLYTO environment variable
197 when you first send mail."
198   :type '(choice (const nil) string)
199   :group 'sendmail)
200
201 ;;;###autoload
202 (defcustom mail-alias-file nil
203   "*If non-nil, the name of a file to use instead of `/usr/lib/aliases'.
204 This file defines aliases to be expanded by the mailer; this is a different
205 feature from that of defining aliases in `.mailrc' to be expanded in Emacs.
206 This variable has no effect unless your system uses sendmail as its mailer."
207   :type '(choice (const nil) file)
208   :group 'sendmail)
209
210 ; XEmacs:
211 (defvaralias 'mail-personal-alias-file 'mail-abbrev-mailrc-file)
212
213 (defcustom mail-setup-hook nil
214   "Normal hook, run each time a new outgoing mail message is initialized.
215 The function `mail-setup' runs this hook."
216   :type 'hook
217   :options '(fortune-to-signature spook mail-abbrevs-setup)
218   :group 'sendmail)
219
220 ;; XEmacs: `mail-aliases' moved to `mail-abbrevs.el'.
221
222 (defvar mail-alias-modtime nil
223   "The modification time of your mail alias file when it was last examined.")
224
225 ;;;###autoload
226 (defcustom mail-yank-prefix "> "                ; XEmacs change
227   "*Prefix insert on lines of yanked message being replied to.
228 nil means use indentation."
229   :type '(choice (const nil) string)
230   :group 'sendmail)
231
232 (defcustom mail-indentation-spaces 3
233   "*Number of spaces to insert at the beginning of each cited line.
234 Used by `mail-yank-original' via `mail-indent-citation'."
235   :type 'integer
236   :group 'sendmail)
237 (defvar mail-yank-hooks nil
238   "Obsolete hook for modifying a citation just inserted in the mail buffer.
239 Each hook function can find the citation between (point) and (mark t).
240 And each hook function should leave point and mark around the citation
241 text as modified.
242
243 This is a normal hook, misnamed for historical reasons.
244 It is semi-obsolete and mail agents should no longer use it.")
245
246 (defcustom mail-citation-hook nil
247   "*Hook for modifying a citation just inserted in the mail buffer.
248 Each hook function can find the citation between (point) and (mark t),
249 and should leave point and mark around the citation text as modified.
250 The hook functions can find the header of the cited message
251 in the variable `mail-citation-header', whether or not this is included
252 in the cited portion of the message.
253
254 If this hook is entirely empty (nil), a default action is taken
255 instead of no action."
256   :type 'hook
257   :group 'sendmail)
258
259 (defvar mail-citation-header nil
260   "While running `mail-citation-hook', this variable holds the message header.
261 This enables the hook functions to see the whole message header
262 regardless of what part of it (if any) is included in the cited text.")
263
264 (defcustom mail-citation-prefix-regexp "[ \t]*[-a-z0-9A-Z]*>+[ \t]*\\|[ \t]*"
265   "*Regular expression to match a citation prefix plus whitespace.
266 It should match whatever sort of citation prefixes you want to handle,
267 with whitespace before and after; it should also match just whitespace.
268 The default value matches citations like `foo-bar>' plus whitespace."
269   :type 'regexp
270   :group 'sendmail)
271
272 (defvar mail-abbrevs-loaded nil)
273 (defvar mail-mode-map nil)
274
275 ; XEmacs: Removed autoloads of `build-mail-aliases' and `expand-mail-aliases'.
276 ; See `mail-abbrevs.el'.
277
278 ;;;###autoload
279 (defcustom mail-signature nil
280   "*Text inserted at end of mail buffer when a message is initialized.
281 If t, it means to insert the contents of the file `mail-signature-file'.
282 If a string, that string is inserted.
283  (To make a proper signature, the string should begin with \\n\\n-- \\n,
284   which is the standard way to delimit a signature in a message.)
285 Otherwise, it should be an expression; it is evaluated
286 and should insert whatever you want to insert."
287   :type '(choice (const :tag "None" nil)
288                  (const :tag "Use `.signature' file" t)
289                  (string :tag "String to insert")
290                  (sexp :tag "Expression to evaluate"))
291   :group 'sendmail)
292 (put 'mail-signature 'risky-local-variable t)
293
294 (defcustom mail-signature-file "~/.signature"
295   "*File containing the text inserted at end of mail buffer."
296   :type 'file
297   :group 'sendmail)
298
299 ;;;###autoload
300 (defcustom mail-default-directory "~/"
301   "*Directory for mail buffers.
302 Value of `default-directory' for mail buffers.
303 This directory is used for auto-save files of mail buffers."
304   :type '(directory :tag "Directory")
305   :group 'sendmail
306   :version "21.4")
307
308 (defvar mail-reply-action nil)
309 (defvar mail-send-actions nil
310   "A list of actions to be performed upon successful sending of a message.")
311 (put 'mail-reply-action 'permanent-local t)
312 (put 'mail-send-actions 'permanent-local t)
313
314 (defcustom mail-default-headers nil
315   "*A string containing header lines, to be inserted in outgoing messages.
316 It is inserted before you edit the message,
317 so you can edit or delete these lines."
318   :type '(choice (const nil) string)
319   :group 'sendmail)
320
321 (defcustom mail-bury-selects-summary t
322   "*If non-nil, try to show RMAIL summary buffer after returning from mail.
323 The functions \\[mail-send-on-exit] or \\[mail-dont-send] select
324 the RMAIL summary buffer before returning, if it exists and this variable
325 is non-nil."
326   :type 'boolean
327   :group 'sendmail)
328
329 (defcustom mail-send-nonascii 'mime
330   "*Specify whether to allow sending non-ASCII characters in mail.
331 If t, that means do allow it.  nil means don't allow it.
332 `query' means ask the user each time.
333 `mime' means add an appropriate MIME header if none already present.
334 The default is `mime'.
335 Including non-ASCII characters in a mail message can be problematical
336 for the recipient, who may not know how to decode them properly."
337   :type '(choice (const t) (const nil) (const query) (const mime))
338   :group 'sendmail)
339
340 (defcustom mail-use-dsn nil
341   "*Ask MTA for notification of failed, delayed or successful delivery.
342 Note that only some MTAs (currently only recent versions of Sendmail)
343 support Delivery Status Notification."
344   :group 'sendmail
345   :type '(repeat (radio (const :tag "Failure" failure)
346                         (const :tag "Delay" delay)
347                         (const :tag "Success" success)))
348   :version "21.4")
349
350 ;; Note: could use /usr/ucb/mail instead of sendmail;
351 ;; options -t, and -v if not interactive.
352 (defvar mail-mailer-swallows-blank-line
353   (if (and (string-match "sparc-sun-sunos\\(\\'\\|[^5]\\)" system-configuration)
354            (file-readable-p "/etc/sendmail.cf")
355            (let ((buffer (get-buffer-create " *temp*")))
356              (unwind-protect
357                  (save-excursion
358                    (set-buffer buffer)
359                    (insert-file-contents "/etc/sendmail.cf")
360                    (goto-char (point-min))
361                    (let ((case-fold-search nil))
362                      (re-search-forward "^OR\\>" nil t)))
363                (kill-buffer buffer))))
364       ;; According to RFC822, "The field-name must be composed of printable
365       ;; ASCII characters (i.e. characters that have decimal values between
366       ;; 33 and 126, except colon)", i.e. any chars except ctl chars,
367       ;; space, or colon.
368       '(looking-at "[ \t]\\|[][!\"#$%&'()*+,-./0-9;<=>?@A-Z\\\\^_`a-z{|}~]+:"))
369   "Set this non-nil if the system's mailer runs the header and body together.
370 \(This problem exists on Sunos 4 when sendmail is run in remote mode.)
371 The value should be an expression to test whether the problem will
372 actually occur.")
373
374 ;; XEmacs: RMS doesn't like this behavior but it seems more logical to
375 ;; me. --ben
376 (defcustom mail-use-multiple-buffers-p t 
377   "Non-nil means `mail' will create a new buffer if one already exists."
378   :type 'boolean
379   :group 'sendmail)
380
381 (defvar mail-mode-syntax-table
382   (let ((st (make-syntax-table)))
383     ;; define-derived-mode will make it inherit from text-mode-syntax-table.
384     (modify-syntax-entry ?% ". " st)
385     st)
386   "Syntax table used while in `mail-mode'.")
387
388 (defvar mail-font-lock-keywords
389   (eval-when-compile
390     (let* ((cite-chars "[>|}]")
391            (cite-prefix "[A-Za-z]")
392            (cite-suffix (concat cite-prefix "0-9_.@-`'\"")))
393       (list '("^\\(To\\|Newsgroups\\):" . font-lock-function-name-face)
394             '("^\\(B?CC\\|Reply-to\\):" . font-lock-keyword-face)
395             '("^\\(Subject:\\)[ \t]*\\(.+\\)?"
396               (1 font-lock-comment-face) (2 font-lock-type-face nil t))
397             ;; Use EVAL to delay in case `mail-header-separator' gets changed.
398             '(eval .
399               (let ((separator (if (zerop (length mail-header-separator))
400                                    " \\`\\' "
401                                  (regexp-quote mail-header-separator))))
402                 (cons (concat "^" separator "$") 'font-lock-warning-face)))
403             ;; Use MATCH-ANCHORED to effectively anchor the regexp left side.
404             `(,cite-chars
405               (,(concat "\\=[ \t]*"
406                         "\\(\\([" cite-prefix "]+[" cite-suffix "]*\\)?"
407                         "\\(" cite-chars "[ \t]*\\)\\)+"
408                         "\\(.*\\)")
409                (beginning-of-line) (end-of-line)
410                (2 font-lock-constant-face nil t)
411                (4 font-lock-comment-face nil t)))
412             '("^\\(X-[A-Za-z0-9-]+\\|In-reply-to\\):.*\\(\n[ \t]+.*\\)*$"
413               . font-lock-string-face))))
414   "Additional expressions to highlight in Mail mode.")
415 (put 'mail-mode 'font-lock-defaults '(mail-font-lock-keywords t))
416
417 ;; Courtesy of Per Abrahamsen <abraham@dina.kvl.dk> in an attempt to make
418 ;; Emacs and XEmacs less stupid about default mail addresses.
419
420 ;; We trust the administrator if he has set `mail-host-address'.
421 ;; We trust the user if he has already customized `user-mail-address'.
422 (defcustom query-user-mail-address (and (not mail-host-address)
423                                         (not user-mail-address))
424   "If non-nil, prompt the user for his mail address."
425   :group 'message
426   :type 'boolean)
427
428 ;;;###autoload
429 (defun user-mail-address ()
430   "Query the user for his mail address, unless it is already known."
431   (interactive)
432   (when (and (not noninteractive) query-user-mail-address)
433     (let ((addr (or user-mail-address
434                     (concat (user-login-name) "@"
435                                     (or mail-host-address
436                                         (system-name))))))
437       (setq user-mail-address
438             (read-string "Your mail address? " (cons addr 0)))
439       (setq query-user-mail-address nil)
440       ;; TODO: Run sanity check from Gnus here.
441       (when (y-or-n-p "Save address for future sessions? ")
442         (put 'user-mail-address 'saved-value
443              (list user-mail-address))
444         (put 'query-user-mail-address 'saved-value '(nil))
445         (require 'cus-edit)
446         (custom-save-all))))
447   (if user-mail-address
448       user-mail-address
449     (setq user-mail-address (concat (user-login-name) "@"
450                                     (or mail-host-address
451                                         (system-name))))))
452
453 (defun sendmail-sync-aliases ()
454   (let ((modtime (nth 5 (file-attributes (mail-abbrev-mailrc-file))))) ; XEmacs
455     (or (equal mail-alias-modtime modtime)
456         (setq mail-alias-modtime modtime
457               mail-aliases t))))
458
459 (defun mail-setup (to subject in-reply-to cc replybuffer actions)
460   (require 'mail-abbrevs)
461   (or mail-default-reply-to
462       (setq mail-default-reply-to (getenv "REPLYTO")))
463   (sendmail-sync-aliases)
464   (if (eq mail-aliases t)
465       (progn
466         (setq mail-aliases nil)
467         ;; XEmacs: call to `build-mail-alias' removed, done by
468         ;; `mail-aliases-setup' below.
469         ))
470   ;; Don't leave this around from a previous message.
471   (kill-local-variable 'buffer-file-coding-system)
472   ;; This doesn't work for enable-multibyte-characters.
473   ;; (kill-local-variable 'enable-multibyte-characters)
474   ;; XEmacs, don't (set-buffer-multibyte default-enable-multibyte-characters)
475   (if (and (boundp 'current-input-method) current-input-method) ;; XEmacs
476       (inactivate-input-method))
477   (setq mail-send-actions actions)
478   (mail-aliases-setup) ;; XEmacs
479   (setq mail-reply-action replybuffer)
480   (goto-char (point-min))
481   (insert "To: ")
482   (save-excursion
483     (if to
484         ;; Here removed code to extract names from within <...>
485         ;; on the assumption that mail-strip-quoted-names
486         ;; has been called and has done so.
487         (let ((fill-prefix "\t")
488               (address-start (point)))
489           (insert to "\n")
490           (fill-region-as-paragraph address-start (point-max))
491           (goto-char (point-max))
492           (unless (bolp)
493             (newline)))
494       (newline))
495     (if cc
496         (let ((fill-prefix "\t")
497               (address-start (progn (insert "CC: ") (point))))
498           (insert cc "\n")
499           (fill-region-as-paragraph address-start (point-max))
500           (goto-char (point-max))
501           (unless (bolp)
502             (newline))))
503     (if in-reply-to
504         (let ((fill-prefix "\t")
505               (fill-column 78)
506               (address-start (point)))
507           (insert "In-reply-to: " in-reply-to "\n")
508           (fill-region-as-paragraph address-start (point-max))
509           (goto-char (point-max))
510           (unless (bolp)
511             (newline))))
512     (insert "Subject: " (or subject "") "\n")
513     (if mail-default-headers
514         (insert mail-default-headers))
515     (if mail-default-reply-to
516         (insert "Reply-to: " mail-default-reply-to "\n"))
517     (if mail-self-blind
518         (insert "BCC: " (user-login-name) "\n")) ;; XEmacs
519     (if mail-archive-file-name
520         (insert "FCC: " mail-archive-file-name "\n"))
521     (put-text-property (point)
522                        (progn
523                          (insert mail-header-separator "\n")
524                          (1- (point)))
525                        'category 'mail-header-separator)
526     ;; Insert the signature.  But remember the beginning of the message.
527     (if to (setq to (point)))
528     (cond ((eq mail-signature t)
529            (if (file-exists-p mail-signature-file)
530                (progn
531                  (insert "\n\n-- \n")
532                  (insert-file-contents mail-signature-file))))
533           ((stringp mail-signature)
534            (insert mail-signature))
535           (t
536            (eval mail-signature)))
537     (goto-char (point-max))
538     (or (bolp) (newline)))
539   (if to (goto-char to))
540   (or to subject in-reply-to
541       (set-buffer-modified-p nil))
542   (run-hooks 'mail-setup-hook))
543 \f
544 (defcustom mail-mode-hook nil
545   "Hook run by Mail mode."
546   :group 'sendmail
547   :type 'hook
548   :options '(footnote-mode))
549
550 (defvar mail-mode-abbrev-table text-mode-abbrev-table)
551 ;;;###autoload
552 (define-derived-mode mail-mode text-mode "Mail"
553   "Major mode for editing mail to be sent.
554 Like Text Mode but with these additional commands:
555 \\[mail-send]  mail-send (send the message)    \\[mail-send-and-exit]  mail-send-and-exit
556 Here are commands that move to a header field (and create it if there isn't):
557          \\[mail-to]  move to To:       \\[mail-subject]  move to Subject:
558          \\[mail-cc]  move to CC:       \\[mail-bcc]  move to BCC:
559          \\[mail-fcc]  move to FCC:      \\[mail-reply-to] move to Reply-To:
560 \\[mail-text]  mail-text (move to beginning of message text).
561 \\[mail-signature]  mail-signature (insert `mail-signature-file' file).
562 \\[mail-yank-original]  mail-yank-original (insert current message, in Rmail).
563 \\[mail-fill-yanked-message]  mail-fill-yanked-message (fill what was yanked).
564 \\[mail-sent-via]  mail-sent-via (add a Sent-via field for each To or CC).
565 Turning on Mail mode runs the normal hooks `text-mode-hook' and
566 `mail-mode-hook' (in that order)."
567   (make-local-variable 'mail-reply-action)
568   (make-local-variable 'mail-send-actions)
569   (setq buffer-offer-save t)
570   (setq mail-reply-action nil)
571   (make-local-variable 'mail-send-actions)
572   (make-local-variable 'paragraph-separate)
573   (make-local-variable 'paragraph-start)
574   (make-local-variable 'normal-auto-fill-function)
575   (setq normal-auto-fill-function 'mail-mode-auto-fill)
576   (make-local-variable 'fill-paragraph-function)
577   (setq fill-paragraph-function 'mail-mode-fill-paragraph)
578   ;; Allow using comment commands to add/remove quoting (this only does
579   ;; anything if mail-yank-prefix is set to a non-nil value).
580   (set (make-local-variable 'comment-start) mail-yank-prefix)
581   (make-local-variable 'adaptive-fill-regexp)
582   (setq adaptive-fill-regexp
583         (concat "[ \t]*[-[A-Za-z0-9]]+>+[ \t]*\\|"
584                 adaptive-fill-regexp))
585   (make-local-variable 'adaptive-fill-first-line-regexp)
586   (setq adaptive-fill-first-line-regexp
587         (concat "[ \t]*[-[A-Za-z0-9]]*>+[ \t]*\\|"
588                 adaptive-fill-first-line-regexp))
589   (turn-on-auto-fill)   ; XEmacs - maybe filladapt should be default, too.
590   ;; `-- ' precedes the signature.  `-----' appears at the start of the
591   ;; lines that delimit forwarded messages.
592   ;; Lines containing just >= 3 dashes, perhaps after whitespace,
593   ;; are also sometimes used and should be separators.
594   (setq paragraph-start (concat (regexp-quote mail-header-separator)
595                                 "$\\|\t*\\([-|#;>* ]\\|(?[0-9]+[.)]\\)+$"
596                                 "\\|[ \t]*[[A-Za-z0-9]]*>+[ \t]*$\\|[ \t]*$\\|"
597                                 "-- $\\|---+$\\|"
598                                 page-delimiter))
599   (setq paragraph-separate paragraph-start)
600   (if (featurep 'menubar)
601       (easy-menu-add mail-menubar-menu)))
602
603 (defun mail-header-end ()
604   "Return the buffer location of the end of headers, as a number."
605   (save-restriction
606     (widen)
607     (save-excursion
608       (rfc822-goto-eoh)
609       (point))))
610
611 (defun mail-text-start ()
612   "Return the buffer location of the start of text, as a number."
613   (save-restriction
614     (widen)
615     (save-excursion
616       (rfc822-goto-eoh)
617       (forward-line 1)
618       (point))))
619
620 (defun mail-sendmail-delimit-header ()
621   "Set up whatever header delimiter convention sendmail will use.
622 Concretely: replace the first blank line in the header with the separator."
623   (rfc822-goto-eoh)
624   (insert mail-header-separator)
625   (point))
626
627 (defun mail-sendmail-undelimit-header ()
628   "Remove header separator to put the message in correct form for sendmail.
629 Leave point at the start of the delimiter line."
630   (rfc822-goto-eoh)
631   (delete-region (point) (progn (end-of-line) (point))))
632
633 (defun mail-mode-auto-fill ()
634   "Carry out Auto Fill for Mail mode.
635 If within the headers, this makes the new lines into continuation lines."
636   (if (< (point) (mail-header-end))
637       (let ((old-line-start (save-excursion (beginning-of-line) (point))))
638         (if (do-auto-fill)
639             (save-excursion
640               (beginning-of-line)
641               (while (not (eq (point) old-line-start))
642                 ;; Use insert-before-markers in case we're inserting
643                 ;; before the saved value of point (which is common).
644                 (insert-before-markers "   ")
645                 (forward-line -1))
646               t)))
647     (do-auto-fill)))
648
649 (defun mail-mode-fill-paragraph (arg)
650   ;; Do something special only if within the headers.
651   (if (< (point) (mail-header-end))
652       (let (beg end fieldname)
653         (when (prog1 (re-search-backward "^[-a-zA-Z]+:" nil 'yes)
654                 (setq beg (point)))
655         (setq fieldname
656                 (downcase (buffer-substring beg (1- (match-end 0))))))
657         (forward-line 1)
658         ;; Find continuation lines and get rid of their continuation markers.
659         (while (looking-at "[ \t]")
660           (delete-horizontal-space)
661           (forward-line 1))
662         (setq end (point-marker))
663         (goto-char beg)
664         ;; If this field contains addresses,
665         ;; make sure we can fill after each address.
666         (if (member fieldname
667                     '("to" "cc" "bcc" "from" "reply-to"
668                       "resent-to" "resent-cc" "resent-bcc"
669                       "resent-from" "resent-reply-to"))
670             (while (search-forward "," end t)
671               (or (looking-at "[ \t]")
672                   (insert " "))))
673         (fill-region-as-paragraph beg end)
674         ;; Mark all lines except the first as continuations.
675         (goto-char beg)
676         (forward-line 1)
677         (while (< (point) end)
678           (insert "  ")
679           (forward-line 1))
680         (move-marker end nil)
681         t)))
682 \f
683 ;;; Set up keymap.
684
685 (if mail-mode-map
686     nil
687   (setq mail-mode-map (make-sparse-keymap))
688   ;; XEmacs: (define-key mail-mode-map "\M-\t" 'mail-complete)
689   (define-key mail-mode-map "\C-c?" 'describe-mode)
690   (define-key mail-mode-map "\C-c\C-f\C-t" 'mail-to)
691   (define-key mail-mode-map "\C-c\C-f\C-b" 'mail-bcc)
692   (define-key mail-mode-map "\C-c\C-f\C-f" 'mail-fcc)
693   (define-key mail-mode-map "\C-c\C-f\C-c" 'mail-cc)
694   (define-key mail-mode-map "\C-c\C-f\C-s" 'mail-subject)
695   (define-key mail-mode-map "\C-c\C-f\C-r" 'mail-reply-to)
696   (define-key mail-mode-map "\C-c\C-t" 'mail-text)
697   (define-key mail-mode-map "\C-c\C-y" 'mail-yank-original)
698   (define-key mail-mode-map "\C-c\C-r" 'mail-yank-region)
699   ;; XEmacs: (define-key mail-mode-map [remap split-line] 'mail-split-line)
700   (define-key mail-mode-map "\C-c\C-q" 'mail-fill-yanked-message)
701   (define-key mail-mode-map "\C-c\C-w" 'mail-signature)
702   ;; XEmacs: (define-key mail-mode-map "\C-c\C-v" 'mail-sent-via)
703   (define-key mail-mode-map "\C-c\C-c" 'mail-send-and-exit)
704   (define-key mail-mode-map "\C-c\C-s" 'mail-send)
705   (define-key mail-mode-map "\C-c\C-i" 'mail-attach-file))
706
707 ;;; mail-mode popup menu
708
709 (defvar mail-menubar-menu
710   (purecopy
711    '("Mail"
712      "Sending Mail:"
713      "----"
714      ["Send and Exit"           mail-send-and-exit              t]
715      ["Send Mail"               mail-send                       t]
716      "----"
717      "Navigation:"
718      "----"
719      ["To:"                     mail-to                         t]
720      ["Subject:"                mail-subject                    t]
721      ["CC:"                     mail-cc                         t]
722      ["FCC:"                    mail-fcc                        t]
723      ["BCC:"                    mail-bcc                        t]
724      ["Reply-To:"               mail-reply-to                   t]
725      ;; XEmacs   ["Sent Via:"           mail-sent-via                   t]
726      ["Text"                    mail-text                       t]
727      "----"
728      "Miscellaneous Commands:"
729      "----"
730      ["Expand Aliases"          expand-mail-aliases             t]
731      ["Yank Original"           mail-yank-original
732       (not (null mail-reply-action))]
733      ["Fill Yanked Message"     mail-fill-yanked-message
734       (save-excursion
735         (goto-char (point-min))
736         (and (search-forward (concat "\n" mail-header-separator
737                                      "\n") nil t)
738              (not (looking-at "[ \t\n]*\\'"))))]
739      ["Insert Signature"        mail-signature
740       (and (stringp mail-signature-file)
741            (file-exists-p mail-signature-file))]
742      ["Insert File..."          insert-file                     t]
743      ["Insert Buffer..."        insert-buffer                   t]
744      "----"
745      ["Cancel"                  mail-dont-send                  t]
746      ))
747    "Menubar menu for `mail-mode'.")
748
749 (defvar mail-popup-menu
750   (purecopy
751    (cons "Sendmail Commands"
752          (cdr mail-menubar-menu)))
753   "Menubar menu for `mail-mode'.")
754
755 \f
756 ;; User-level commands for sending.
757
758 (defun mail-send-and-exit (arg)
759   "Send message like `mail-send', then, if no errors, exit from mail buffer.
760 Prefix arg means don't delete this window."
761   (interactive "P")
762   (mail-send)
763   (mail-bury arg))
764
765 (defun mail-dont-send (arg)
766   "Don't send the message you have been editing.
767 Prefix arg means don't delete this window."
768   (interactive "P")
769   (mail-bury arg))
770
771 (defun mail-bury (arg)
772   "Bury this mail buffer."
773   (let ((newbuf (other-buffer (current-buffer))))
774     (bury-buffer (current-buffer))
775     (if (and (or (window-dedicated-p (frame-selected-window))
776                  (cdr (assq 'mail-dedicated-frame (frame-parameters))))
777              (not (null (delq (selected-frame) (visible-frame-list)))))
778         (delete-frame (selected-frame))
779       (let (rmail-flag summary-buffer)
780         (and (not arg)
781              (not (one-window-p))
782              (save-excursion
783                (set-buffer (window-buffer (next-window (selected-window) 'not)))
784                (setq rmail-flag (eq major-mode 'rmail-mode))
785                (setq summary-buffer
786                      (and mail-bury-selects-summary
787                           (boundp 'rmail-summary-buffer)
788                           rmail-summary-buffer
789                           (buffer-name rmail-summary-buffer)
790                           (not (get-buffer-window rmail-summary-buffer))
791                           rmail-summary-buffer))))
792         (if rmail-flag
793             ;; If the Rmail buffer has a summary, show that.
794             (if summary-buffer (switch-to-buffer summary-buffer)
795               (delete-window))
796           (switch-to-buffer newbuf))))))
797
798 (defcustom mail-send-hook nil
799   "Hook run just before sending mail with `mail-send'."
800   :type 'hook
801   :options '(flyspell-mode-off)
802   :group 'sendmail)
803
804 (defun mail-send ()
805   "Send the message in the current buffer.
806 If `mail-interactive' is non-nil, wait for success indication
807 or error messages, and inform user.
808 Otherwise any failure is reported in a message back to
809 the user from the mailer."
810   (interactive)
811   (if (if buffer-file-name
812         (y-or-n-p "Send buffer contents as mail message? ")
813       (or (buffer-modified-p)
814           (y-or-n-p "Message already sent; resend? ")))
815       (let ((inhibit-read-only t)
816             (opoint (point)))
817         ;; Complain about any invalid line.
818         (goto-char (point-min))
819         (while (< (point) (mail-header-end))
820           (unless (looking-at "[ \t]\\|.*:\\|$")
821             (push-mark opoint)
822             (error "Invalid header line (maybe a continuation line lacks initial whitespace)"))
823           (forward-line 1))
824         (goto-char opoint)
825         (if (interactive-p) (expand-abbrev))  ;; XEmacs: for mail-abbrevs
826         (run-hooks 'mail-send-hook)
827         (message "Sending...")
828         (funcall send-mail-function)
829         ;; Now perform actions on successful sending.
830         (while mail-send-actions
831           (condition-case nil
832               (apply (car (car mail-send-actions))
833                      (cdr (car mail-send-actions)))
834             (error))
835           (setq mail-send-actions (cdr mail-send-actions)))
836         (message "Sending...done")
837
838         ;; If buffer has no file, mark it as unmodified and delete autosave.
839         (cond ((or (not buffer-file-name)
840                    (not (buffer-modified-p)))
841                (set-buffer-modified-p nil)
842                (delete-auto-save-file-if-necessary t))
843               ((or noninteractive
844                    (y-or-n-p (format "Save file %s? " buffer-file-name)))
845                (save-buffer))))))
846
847 (defun mail-envelope-from ()
848   "Return the envelope mail address to use when sending mail.
849 This function uses `mail-envelope-from'."
850   (if (eq mail-envelope-from 'header)
851       (nth 1 (mail-extract-address-components
852               (mail-fetch-field "From")))
853     mail-envelope-from))
854 \f
855 ;; This does the real work of sending a message via sendmail.
856 ;; It is called via the variable send-mail-function.
857
858 (defun sendmail-send-it ()
859   "Send the current mail buffer using the Sendmail package.
860 This is a suitable value for `send-mail-function'.  It sends using the
861 external program defined by `sendmail-program'."
862   (require 'mail-utils)
863   (let ((errbuf (if mail-interactive
864                     (generate-new-buffer " sendmail errors")
865                   0))
866         (tembuf (generate-new-buffer " sendmail temp"))
867         ;; XEmacs: comment out
868 ;       (multibyte enable-multibyte-characters)
869         (case-fold-search nil)
870         ;; XEmacs: comment out
871 ;       (selected-coding (select-message-coding-system))
872 ;;;     resend-to-addresses
873         delimline
874         fcc-was-found
875         (mailbuf (current-buffer))
876         (program (if (boundp 'sendmail-program)
877                      sendmail-program
878                    "/usr/lib/sendmail"))
879         ;; Examine these variables now, so that
880         ;; local binding in the mail buffer will take effect.
881         (envelope-from
882          (and mail-specify-envelope-from
883               ;; XEmacs: use (user-mail-address)
884               (or (mail-envelope-from) (user-mail-address)))))
885     (unwind-protect
886         (save-excursion
887           (set-buffer tembuf)
888           (erase-buffer)
889           ;; XEmacs: commented out
890 ;         (unless multibyte
891 ;           (set-buffer-multibyte nil))
892           (insert-buffer-substring mailbuf)
893           (goto-char (point-max))
894           ;; require one newline at the end.
895           (or (= (preceding-char) ?\n)
896               (insert ?\n))
897           ;; Change header-delimiter to be what sendmail expects.
898           (goto-char (mail-header-end))
899           (delete-region (point) (progn (end-of-line) (point)))
900           (setq delimline (point-marker))
901 ;XEmacs removed.  See second paragraph in `mail-abbrevs.el' for rationale.
902 ;         (sendmail-sync-aliases)
903 ;         (if mail-aliases
904 ;             (expand-mail-aliases (point-min) delimline))
905           (goto-char (point-min))
906           ;; Ignore any blank lines in the header
907           (while (and (re-search-forward "\n\n\n*" delimline t)
908                       (< (point) delimline))
909             (replace-match "\n"))
910           (goto-char (point-min))
911           (let ((case-fold-search t))
912 ;;;         (goto-char (point-min))
913 ;;;         (while (re-search-forward "^Resent-\\(to\\|cc\\|bcc\\):" delimline t)
914 ;;;           (setq resend-to-addresses
915 ;;;                 (save-restriction
916 ;;;                   (narrow-to-region (point)
917 ;;;                                     (save-excursion
918 ;;;                                       (forward-line 1)
919 ;;;                                       (while (looking-at "^[ \t]")
920 ;;;                                         (forward-line 1))
921 ;;;                                       (point)))
922 ;;;                   (append (mail-parse-comma-list)
923 ;;;                           resend-to-addresses)))
924 ;;;           ;; Delete Resent-BCC ourselves
925 ;;;           (if (save-excursion (beginning-of-line)
926 ;;;                               (looking-at "resent-bcc"))
927 ;;;               (delete-region (save-excursion (beginning-of-line) (point))
928 ;;;                              (save-excursion (end-of-line) (1+ (point))))))
929 ;;; Apparently this causes a duplicate Sender.
930 ;;;         ;; If the From is different than current user, insert Sender.
931 ;;;         (goto-char (point-min))
932 ;;;         (and (re-search-forward "^From:"  delimline t)
933 ;;;              (progn
934 ;;;                (require 'mail-utils)
935 ;;;                (not (string-equal
936 ;;;                      (mail-strip-quoted-names
937 ;;;                       (save-restriction
938 ;;;                         (narrow-to-region (point-min) delimline)
939 ;;;                         (mail-fetch-field "From")))
940 ;;;                      (user-login-name))))
941 ;;;              (progn
942 ;;;                (forward-line 1)
943 ;;;                (insert "Sender: " (user-login-name) "\n")))
944             ;; Don't send out a blank subject line
945             (goto-char (point-min))
946             (if (re-search-forward "^Subject:\\([ \t]*\n\\)+\\b" delimline t)
947                 (replace-match "")
948               ;; This one matches a Subject just before the header delimiter.
949               (if (and (re-search-forward "^Subject:\\([ \t]*\n\\)+" delimline t)
950                        (= (match-end 0) delimline))
951                   (replace-match "")))
952             ;; Put the "From:" field in unless for some odd reason
953             ;; they put one in themselves.
954             (goto-char (point-min))
955             (if (not (re-search-forward "^From:" delimline t))
956                 (let* ((login (user-mail-address))
957                        (fullname (user-full-name)))
958                   (cond ((eq mail-from-style 'angles)
959                          (insert "From: " fullname)
960                          (let ((fullname-start (+ (point-min) 6))
961                                (fullname-end (point-marker)))
962                            (goto-char fullname-start)
963                            ;; Look for a character that cannot appear unquoted
964                            ;; according to RFC 822.
965                            (if (re-search-forward "[^- !#-'*+/-9=?A-Z^-~]"
966                                                   fullname-end 1)
967                                (progn
968                                  ;; Quote fullname, escaping specials.
969                                  (goto-char fullname-start)
970                                  (insert "\"")
971                                  (while (re-search-forward "[\"\\]"
972                                                            fullname-end 1)
973                                    (replace-match "\\\\\\&" t))
974                                  (insert "\""))))
975                          (insert " <" login ">\n"))
976                         ((eq mail-from-style 'parens)
977                          (insert "From: " login " (")
978                          (let ((fullname-start (point)))
979                            (insert fullname)
980                            (let ((fullname-end (point-marker)))
981                              (goto-char fullname-start)
982                              ;; RFC 822 says \ and nonmatching parentheses
983                              ;; must be escaped in comments.
984                              ;; Escape every instance of ()\ ...
985                              (while (re-search-forward "[()\\]" fullname-end 1)
986                                (replace-match "\\\\\\&" t))
987                              ;; ... then undo escaping of matching parentheses,
988                              ;; including matching nested parentheses.
989                              (goto-char fullname-start)
990                              (while (re-search-forward
991                                      "\\(\\=\\|[^\\]\\(\\\\\\\\\\)*\\)\\\\(\\(\\([^\\]\\|\\\\\\\\\\)*\\)\\\\)"
992                                      fullname-end 1)
993                                (replace-match "\\1(\\3)" t)
994                                (goto-char fullname-start))))
995                          (insert ")\n"))
996                         ((null mail-from-style)
997                          (insert "From: " login "\n"))
998                         ((eq mail-from-style 'system-default)
999                          nil)
1000                         (t (error "Invalid value for `mail-from-style'")))))
1001             ;; XEmacs: commented out #### why?
1002 ;           ;; Possibly add a MIME header for the current coding system
1003 ;           (let (charset)
1004 ;             (goto-char (point-min))
1005 ;             (and (eq mail-send-nonascii 'mime)
1006 ;                  (not (re-search-forward "^MIME-version:" delimline t))
1007 ;                  (progn (skip-chars-forward "\0-\177")
1008 ;                         (/= (point) (point-max)))
1009 ;                  selected-coding
1010 ;                  (setq charset
1011 ;                        (coding-system-get selected-coding 'mime-charset))
1012 ;                  (goto-char delimline)
1013 ;                  (insert "MIME-version: 1.0\n"
1014 ;                          "Content-type: text/plain; charset="
1015 ;                          (symbol-name charset) "\n"
1016 ;                          "Content-Transfer-Encoding: 8bit\n")))
1017             ;; Insert an extra newline if we need it to work around
1018             ;; Sun's bug that swallows newlines.
1019             (goto-char (1+ delimline))
1020             (if (eval mail-mailer-swallows-blank-line)
1021                 (newline))
1022             ;; Find and handle any FCC fields.
1023             (goto-char (point-min))
1024             (if (re-search-forward "^FCC:" delimline t)
1025                 (progn
1026                   (setq fcc-was-found t)
1027                   (mail-do-fcc delimline)))
1028             (if mail-interactive
1029                 (save-excursion
1030                   (set-buffer errbuf)
1031                   (erase-buffer))))
1032
1033           ;; XEmacs change
1034           (mail-check-safe-charset)
1035
1036           (goto-char (point-min))
1037           (if (let ((case-fold-search t))
1038                 (re-search-forward "^To:\\|^cc:\\|^bcc:\\|^resent-to:\
1039 \\|^resent-cc:\\|^resent-bcc:"
1040                                    delimline t))
1041               (let* ((default-directory "/")
1042                      ;; XEmacs: changed to no-conversion-unix
1043                      (coding-system-for-write 'no-conversion-unix)
1044                      (args
1045                       (append (list (point-min) (point-max)
1046                                     program
1047                                     nil errbuf nil "-oi")
1048                               ;; Always specify who from,
1049                               ;; since some systems have
1050                               ;; broken sendmails.
1051                               (and envelope-from
1052                                    (list "-f" envelope-from))
1053 ;;;                           ;; Don't say "from root" if running under su.
1054 ;;;                           (and (equal (user-real-login-name) "root")
1055 ;;;                                (list "-f" (user-login-name)))
1056                               (and mail-alias-file
1057                                    (list (concat "-oA" mail-alias-file)))
1058                               (if mail-interactive
1059                                   ;; These mean "report errors to terminal"
1060                                   ;; and "deliver interactively"
1061                                   '("-oep" "-odi")
1062                                 ;; These mean "report errors by mail"
1063                                 ;; and "deliver in background".
1064                                 '("-oem" "-odb"))
1065 ;;;                           ;; Get the addresses from the message
1066 ;;;                           ;; unless this is a resend.
1067 ;;;                           ;; We must not do that for a resend
1068 ;;;                           ;; because we would find the original addresses.
1069 ;;;                           ;; For a resend, include the specific addresses.
1070 ;;;                           (or resend-to-addresses
1071                                   '("-t")
1072 ;;;                               )
1073                               (if mail-use-dsn
1074                                   (list "-N" (mapconcat 'symbol-name
1075                                                         mail-use-dsn ",")))
1076                               )
1077                       )
1078                      (exit-value (apply 'call-process-region args)))
1079                 (or (null exit-value) (zerop exit-value)
1080                     (error "Sending...failed with exit value %d" exit-value)))
1081             (or fcc-was-found
1082                 (error "No recipients")))
1083           (if mail-interactive
1084               (save-excursion
1085                 (set-buffer errbuf)
1086                 (goto-char (point-min))
1087                 (while (re-search-forward "\n\n* *" nil t)
1088                   (replace-match "; "))
1089                 (if (not (zerop (buffer-size)))
1090                     (error "Sending...failed to %s"
1091                            (buffer-substring (point-min) (point-max)))))))
1092       (kill-buffer tembuf)
1093       (if (bufferp errbuf)
1094           (kill-buffer errbuf)))))
1095
1096 ;;; XEmacs FCC hackery, by jwz.  This version works on BABYL and VM buffers.
1097 ;;; To accomplish the latter, VM is loaded when this file is compiled.
1098 ;;; Don't worry, it's only loaded at compile-time.
1099 ;;; The hackery somewhat reverted by jas to prevent mail lossage.
1100
1101 (defun mail-do-fcc (header-end)
1102   (let (fcc-list
1103         (rmailbuf (current-buffer))
1104         (time (current-time))
1105         (tembuf (generate-new-buffer " rmail output"))
1106         (case-fold-search t))
1107     (or (markerp header-end) (error "header-end must be a marker"))
1108     (save-excursion
1109       (goto-char (point-min))
1110       (while (re-search-forward "^FCC:[ \t]*" header-end t)
1111         (setq fcc-list (cons (buffer-substring (point)
1112                                                (progn
1113                                                  (end-of-line)
1114                                                  (skip-chars-backward " \t")
1115                                                  (point)))
1116                              fcc-list))
1117         (delete-region (match-beginning 0)
1118                        (progn (forward-line 1) (point))))
1119       (set-buffer tembuf)
1120       (erase-buffer)
1121       ;; This initial newline is written out if the fcc file already exists.
1122       (insert "\nFrom " (user-login-name) " "
1123               (current-time-string time) "\n")
1124       ;; Insert the time zone before the year.
1125       (forward-char -1)
1126       (forward-word -1)
1127       (require 'mail-utils)
1128       (insert (mail-rfc822-time-zone time) " ")
1129       (goto-char (point-max))
1130       (insert-buffer-substring rmailbuf)
1131       ;; Make sure messages are separated.
1132       (goto-char (point-max))
1133       (insert ?\n)
1134       (goto-char 2)
1135       ;; ``Quote'' "^From " as ">From "
1136       ;;  (note that this isn't really quoting, as there is no requirement
1137       ;;   that "^[>]+From " be quoted in the same transparent way.)
1138       (let ((case-fold-search nil))
1139         (while (search-forward "\nFrom " nil t)
1140           (forward-char -5)
1141           (insert ?>)))
1142       (while fcc-list
1143         (let* ((buffer (find-buffer-visiting (car fcc-list)))
1144                (curbuf (current-buffer))
1145                dont-write-the-file
1146                buffer-matches-file
1147                (beg (point-min)) (end (point-max))
1148                (beg2 (save-excursion (goto-char (point-min))
1149                                      (forward-line 2) (point))))
1150           (if buffer
1151               ;; File is present in a buffer => append to that buffer.
1152               (save-excursion
1153                 (set-buffer buffer)
1154                 (setq buffer-matches-file
1155                       (and (not (buffer-modified-p))
1156                            (verify-visited-file-modtime buffer)))
1157                 ;; Keep the end of the accessible portion at the same place
1158                 ;; unless it is the end of the buffer.
1159                 (let ((max (if (/= (1+ (buffer-size)) (point-max))
1160                                (point-max))))
1161                   (unwind-protect
1162                       ;; Code below lifted from rmailout.el
1163                       ;; function rmail-output-to-rmail-file:
1164                       (let ((buffer-read-only nil)
1165                             (msg (and (boundp 'rmail-current-message)
1166                                       rmail-current-message)))
1167                         ;; If MSG is non-nil, buffer is in RMAIL mode.
1168                         (if msg
1169                             (progn
1170                               ;; Append to an ordinary buffer as a
1171                               ;; Unix mail message.
1172                               (rmail-maybe-set-message-counters)
1173                               (widen)
1174                               (narrow-to-region (point-max) (point-max))
1175                               (insert "\C-l\n0, unseen,,\n*** EOOH ***\n"
1176                                       "Date: " (mail-rfc822-date) "\n")
1177                               (insert-buffer-substring curbuf beg2 end)
1178                               (insert "\n\C-_")
1179                               (goto-char (point-min))
1180                               (widen)
1181                               (search-backward "\n\^_")
1182                               (narrow-to-region (point) (point-max))
1183                               (rmail-count-new-messages t)
1184                               (rmail-show-message msg)
1185                               (setq max nil))
1186                           (if (with-current-buffer buffer ;; XEmacs
1187                                 (eq major-mode 'vm-mode))
1188                               (with-current-buffer buffer
1189                                 (mail-do-fcc-vm-internal tembuf))
1190                             ;; Output file not in rmail mode
1191                             ;; => just insert at the end.
1192                             (narrow-to-region (point-min) (1+ (buffer-size)))
1193                             (goto-char (point-max))
1194                             (insert-buffer-substring curbuf beg end)))
1195                         (or buffer-matches-file
1196                             (progn
1197                               (if (y-or-n-p (format "Save file %s? "
1198                                                     (car fcc-list)))
1199                                   (save-buffer))
1200                               (setq dont-write-the-file t))))
1201                     (if max (narrow-to-region (point-min) max))))))
1202           ;; Append to the file directly,
1203           ;; unless we've already taken care of it.
1204           ;; (It's OK if it is an RMAIL or VM file -- the message will be
1205           ;; parsed when the file is read in.)
1206           (unless dont-write-the-file
1207             (if (and (file-exists-p (car fcc-list))
1208                      ;; Check that the file isn't empty.  We don't
1209                      ;; want to insert a newline at the start of an
1210                      ;; empty file.
1211                      (not (zerop (nth 7 (file-attributes (car fcc-list)))))
1212                      (mail-file-babyl-p (car fcc-list)))
1213                 ;; If the file is a Babyl file,
1214                 ;; convert the message to Babyl format.
1215                 (let ((coding-system-for-write
1216                        (or rmail-file-coding-system
1217                            ;; XEmacs change; emacs-mule is not available,
1218                            ;; escape-quoted serves much the same
1219                            ;; purpose. #### This means an incompatibility
1220                            ;; with Babyl files created with GNU Emacs, but
1221                            ;; that's probably okay, since no-one uses them.
1222                            'escape-quoted)))
1223                   (save-excursion
1224                     (set-buffer (get-buffer-create " mail-temp"))
1225                     (setq buffer-read-only nil)
1226                     (erase-buffer)
1227                     (insert "\C-l\n0, unseen,,\n*** EOOH ***\n"
1228                             "Date: " (mail-rfc822-date) "\n")
1229                     (insert-buffer-substring curbuf beg2 end)
1230                     (insert "\n\C-_")
1231                     (write-region (point-min) (point-max) (car fcc-list) t)
1232                     (erase-buffer)))
1233               ;; MIME processing should have eliminated any characters
1234               ;; outside the range 0-255; if MIME processing hasn't been
1235               ;; done, there's no reasonable expectation that the data will
1236               ;; be preserved in a Unix mailbox, so we just write tildes for
1237               ;; those chars. We set the coding system for write to 'binary
1238               ;; to prevent any double-encoding that may occur if
1239               ;; buffer-file-coding-system has been initialised to a
1240               ;; non-binary-compatible coding system like, say, UTF-8.
1241               (let ((coding-system-for-write 'binary))
1242                 (write-region (1+ (point-min)) (point-max) (car fcc-list) t))))
1243           (and buffer (not dont-write-the-file)
1244                (with-current-buffer buffer
1245                  (set-visited-file-modtime))))
1246         (setq fcc-list (cdr fcc-list))))
1247     (kill-buffer tembuf)))
1248
1249 ;; XEmacs:
1250 (defun mail-do-fcc-vm-internal (buffer)
1251   (or (eq major-mode 'vm-mode) (error "this only works in vm-mode"))
1252   (let ((buffer-read-only nil)
1253         (foreign-folder-p (not (eq vm-folder-type 'From_))))
1254
1255     (if foreign-folder-p
1256         ;; `buffer' has already been prepared with a "From " line which
1257         ;; has a sensible user-id and date in it, but if we're FCCing to
1258         ;; a VM folder that isn't in From_ format, we must discard that
1259         ;; and let VM do whatever voodoo it needs to do.  (Actually we
1260         ;; could do this all the time, but then all FCCed messages would
1261         ;; have "From VM ..." envelopes, which is less attractive.)
1262         (save-excursion
1263           (set-buffer buffer)
1264           (goto-char (point-min))
1265           (skip-chars-forward "\n")
1266           (forward-line)
1267           (delete-region (point-min) (point))))
1268
1269     ;; Use eval to inhibit compilation of the following code.
1270     ;; The code contains macros, and to compile them a (reuqire
1271     ;; 'vm-misc) is needed.  When VM stopped being bundled with
1272     ;; XEmacs, this require call became impossible.
1273     (eval
1274      (quote
1275       ;; Largely copied from #'vm-save-message in vm-save.el
1276       (vm-save-restriction
1277        (widen)
1278        (goto-char (point-max))
1279        (if foreign-folder-p
1280            (vm-write-string (current-buffer)
1281                             (vm-leading-message-separator vm-folder-type)))
1282        (insert-buffer-substring buffer)
1283        (if foreign-folder-p
1284            (vm-write-string (current-buffer)
1285                             (vm-trailing-message-separator vm-folder-type)))
1286
1287        (vm-increment vm-messages-not-on-disk)
1288        (vm-set-buffer-modified-p t)
1289        (vm-clear-modification-flag-undos)
1290        (vm-check-for-killed-summary)
1291        (vm-assimilate-new-messages)
1292        (vm-update-summary-and-mode-line))))))
1293
1294 (defun mail-sent-via ()
1295   "Make a Sent-via header line from each To or CC header line."
1296   (interactive)
1297   (save-excursion
1298     ;; put a marker at the end of the header
1299     (let ((end (copy-marker (mail-header-end)))
1300           (case-fold-search t)
1301           ;to-line
1302           )
1303       (goto-char (point-min))
1304       ;; search for the To: lines and make Sent-via: lines from them
1305       ;; search for the next To: line
1306       (while (re-search-forward "^\\(to\\|cc\\):" end t)
1307         ;; Grab this line plus all its continuations, sans the `to:'.
1308         (let ((to-line
1309                (buffer-substring (point)
1310                                  (progn
1311                                    (if (re-search-forward "^[^ \t\n]" end t)
1312                                        (backward-char 1)
1313                                      (goto-char end))
1314                                    (point)))))
1315           ;; Insert a copy, with altered header field name.
1316           (insert-before-markers "Sent-via:" to-line))))))
1317 \f
1318 (defun mail-to ()
1319   "Move point to end of To-field."
1320   (interactive)
1321   (if (interactive-p) (expand-abbrev))  ; XEmacs: for mail-abbrevs
1322   (mail-position-on-field "To"))
1323
1324 (defun mail-subject ()
1325   "Move point to end of Subject-field."
1326   (interactive)
1327   (if (interactive-p) (expand-abbrev))  ; XEmacs: for mail-abbrevs
1328   (mail-position-on-field "Subject"))
1329
1330 (defun mail-cc ()
1331   "Move point to end of CC-field.  Create a CC field if none."
1332   (interactive)
1333   (if (interactive-p) (expand-abbrev))  ; XEmacs: for mail-abbrevs
1334   (or (mail-position-on-field "cc" t)
1335       (progn (mail-position-on-field "to")
1336              (insert "\nCC: "))))
1337
1338 (defun mail-bcc ()
1339   "Move point to end of BCC-field.  Create a BCC field if none."
1340   (interactive)
1341   (if (interactive-p) (expand-abbrev))  ; XEmacs: for mail-abbrevs
1342   (or (mail-position-on-field "bcc" t)
1343       (progn (mail-position-on-field "to")
1344              (insert "\nBCC: "))))
1345
1346 (defun mail-fcc (folder)
1347   "Add a new FCC field, with file name completion."
1348   (interactive "FFolder carbon copy: ")
1349   (if (interactive-p) (expand-abbrev))  ; XEmacs: for mail-abbrevs
1350   (or (mail-position-on-field "fcc" t)  ;Put new field after exiting FCC.
1351       (mail-position-on-field "to"))
1352   (insert "\nFCC: " folder))
1353
1354 (defun mail-reply-to ()
1355   "Move point to end of Reply-To-field.  Create a Reply-To field if none."
1356   (interactive)
1357   (if (interactive-p) (expand-abbrev))  ; XEmacs: for mail-abbrevs
1358   (mail-position-on-field "Reply-To"))
1359
1360 (defun mail-position-on-field (field &optional soft)
1361   (let (end
1362         (case-fold-search t))
1363     (setq end (mail-header-end))
1364     (goto-char (point-min))
1365     (if (re-search-forward (concat "^" (regexp-quote field) ":") end t)
1366         (progn
1367           (re-search-forward "^[^ \t]" nil 'move)
1368           (beginning-of-line)
1369           (skip-chars-backward "\n")
1370           t)
1371       (or soft
1372           (progn (goto-char end)
1373                  (insert field ": \n")
1374                  (skip-chars-backward "\n")))
1375       nil)))
1376
1377 (defun mail-text ()
1378   "Move point to beginning of message text."
1379   (interactive)
1380   (if (interactive-p) (expand-abbrev))  ; XEmacs: for mail-abbrevs
1381   (goto-char (mail-text-start)))
1382 \f
1383 (defun mail-signature (&optional atpoint)
1384   "Sign letter with signature based on `mail-signature-file'.
1385 Prefix arg means put contents at point."
1386   (interactive "P")
1387   (save-excursion
1388     (or atpoint
1389         (goto-char (point-max)))
1390     (skip-chars-backward " \t\n")
1391     (end-of-line)
1392     (or atpoint
1393         (delete-region (point) (point-max)))
1394     (if (stringp mail-signature)
1395         (insert mail-signature)
1396       (insert "\n\n-- \n")
1397       (insert-file-contents (expand-file-name mail-signature-file)))))
1398
1399 (defun mail-fill-yanked-message (&optional justifyp)
1400   "Fill the paragraphs of a message yanked into this one.
1401 Numeric argument means justify as well."
1402   (interactive "P")
1403   (save-excursion
1404     (goto-char (mail-text-start))
1405     (fill-individual-paragraphs (point)
1406                                 (point-max)
1407                                 justifyp
1408                                 mail-citation-prefix-regexp)))
1409
1410 (defun mail-indent-citation ()
1411   "Modify text just inserted from a message to be cited.
1412 The inserted text should be the region.
1413 When this function returns, the region is again around the modified text.
1414
1415 Normally, indent each nonblank line `mail-indentation-spaces' spaces.
1416 However, if `mail-yank-prefix' is non-nil, insert that prefix on each line."
1417   (mail-yank-clear-headers (region-beginning) (region-end))
1418   (if (null mail-yank-prefix)
1419       (indent-rigidly (region-beginning) (region-end)
1420                       mail-indentation-spaces)
1421     (save-excursion
1422       (let ((end (set-marker (make-marker) (region-end))))
1423         (goto-char (region-beginning))
1424         (while (< (point) end)
1425           (insert mail-yank-prefix)
1426           (forward-line 1))))))
1427
1428 (defun mail-yank-original (arg)
1429   "Insert the message being replied to, if any (in rmail).
1430 Puts point after the text and mark before.
1431 Normally, indents each nonblank line ARG spaces (default 3).
1432 However, if `mail-yank-prefix' is non-nil, insert that prefix on each line.
1433
1434 Just \\[universal-argument] as argument means don't indent, insert no prefix,
1435 and don't delete any header fields."
1436   (interactive "P")
1437   (if mail-reply-action
1438       (let ((start (point))
1439             (original mail-reply-action))
1440         (and (consp original) (eq (car original) 'insert-buffer)
1441              (setq original (nth 1 original)))
1442         (if (consp original)
1443             (apply (car original) (cdr original))
1444           ;; If the original message is in another window in the same frame,
1445           ;; delete that window to save screen space.
1446           ;; t means don't alter other frames.
1447           (delete-windows-on original t)
1448           (insert-buffer original)
1449           (set-text-properties (point) (mark t) nil))
1450         (if (consp arg)
1451             nil
1452           (goto-char start)
1453           (let ((mail-indentation-spaces (if arg (prefix-numeric-value arg)
1454                                            mail-indentation-spaces))
1455                 ;; Avoid error in Transient Mark mode
1456                 ;; on account of mark's being inactive.
1457                 (mark-even-if-inactive t))
1458             (cond (mail-citation-hook
1459                    ;; Bind mail-citation-header to the inserted
1460                    ;; message's header.
1461                    (let ((mail-citation-header
1462                           (buffer-substring-no-properties
1463                            start
1464                            (save-excursion
1465                              (save-restriction
1466                                (narrow-to-region start (point-max))
1467                                (goto-char start)
1468                                (rfc822-goto-eoh)
1469                                (point))))))
1470                      (run-hooks 'mail-citation-hook)))
1471                   (mail-yank-hooks
1472                    (run-hooks 'mail-yank-hooks))
1473                   (t
1474                    (mail-indent-citation)))))
1475         ;; This is like exchange-point-and-mark, but doesn't activate the mark.
1476         ;; It is cleaner to avoid activation, even though the command
1477         ;; loop would deactivate the mark because we inserted text.
1478         (goto-char (prog1 (mark t)
1479                      (set-marker (mark-marker) (point) (current-buffer))))
1480         (if (not (eolp)) (insert ?\n)))))
1481
1482 (defun mail-yank-clear-headers (start end)
1483   (if (< end start)
1484       (let (temp)
1485         (setq temp start start end end temp)))
1486   (if mail-yank-ignored-headers
1487       (save-excursion
1488         (goto-char start)
1489         (if (search-forward "\n\n" end t)
1490             (save-restriction
1491               (narrow-to-region start (point))
1492               (goto-char start)
1493               (while (let ((case-fold-search t))
1494                        (re-search-forward mail-yank-ignored-headers nil t))
1495                 (beginning-of-line)
1496                 (delete-region (point)
1497                                (progn (re-search-forward "\n[^ \t]")
1498                                       (forward-char -1)
1499                                       (point)))))))))
1500
1501 (defun mail-yank-region (arg)
1502   "Insert the selected region from the message being replied to.
1503 Puts point after the text and mark before.
1504 Normally, indents each nonblank line ARG spaces (default 3).
1505 However, if `mail-yank-prefix' is non-nil, insert that prefix on each line.
1506
1507 Just \\[universal-argument] as argument means don't indent, insert no prefix,
1508 and don't delete any header fields."
1509   (interactive "P")
1510   (and (consp mail-reply-action)
1511        (eq (car mail-reply-action) 'insert-buffer)
1512        (with-current-buffer (nth 1 mail-reply-action)
1513          (or (mark t)
1514              (error "No mark set: %S" (current-buffer))))
1515        (let ((buffer (nth 1 mail-reply-action))
1516              (start (point))
1517              ;; Avoid error in Transient Mark mode
1518              ;; on account of mark's being inactive.
1519              (mark-even-if-inactive t))
1520          ;; Insert the citation text.
1521          (insert (with-current-buffer buffer
1522                    (buffer-substring-no-properties (point) (mark))))
1523          (push-mark start)
1524          ;; Indent or otherwise annotate the citation text.
1525          (if (consp arg)
1526              nil
1527            (let ((mail-indentation-spaces (if arg (prefix-numeric-value arg)
1528                                             mail-indentation-spaces)))
1529              (if mail-citation-hook
1530                  ;; Bind mail-citation-hook to the original message's header.
1531                  (let ((mail-citation-header
1532                         (with-current-buffer buffer
1533                           (buffer-substring-no-properties
1534                            (point-min)
1535                            (save-excursion
1536                              (goto-char (point-min))
1537                              (rfc822-goto-eoh)
1538                              (point))))))
1539                    (run-hooks 'mail-citation-hook))
1540                (if mail-yank-hooks
1541                    (run-hooks 'mail-yank-hooks)
1542                  (mail-indent-citation))))))))
1543
1544 (defun mail-split-line ()
1545   "Split current line, moving portion beyond point vertically down.
1546 If the current line has `mail-yank-prefix', insert it on the new line."
1547   (interactive "*")
1548   (split-line
1549    ;; XEmacs: #### fix this
1550    ;mail-yank-prefix
1551    ))
1552
1553 \f
1554 (defun mail-attach-file (&optional file)
1555   "Insert a file at the end of the buffer, with separator lines around it."
1556   (interactive "fAttach file: ")
1557   (save-excursion
1558     (goto-char (point-max))
1559     (or (bolp) (newline))
1560     (newline)
1561     (let ((start (point))
1562           middle)
1563       (insert (format "===File %s===" file))
1564       (insert-char ?= (max 0 (- 60 (current-column))))
1565       (newline)
1566       (setq middle (point))
1567       (insert "============================================================\n")
1568       (push-mark)
1569       (goto-char middle)
1570       (insert-file-contents file)
1571       (or (bolp) (newline))
1572       (goto-char start))))
1573 \f
1574 ;; Put these commands last, to reduce chance of lossage from quitting
1575 ;; in middle of loading the file.
1576
1577 ;;;###autoload (add-hook 'same-window-buffer-names "*mail*")
1578
1579 ;;;###autoload
1580 (defun mail (&optional noerase to subject in-reply-to cc replybuffer actions)
1581   "Edit a message to be sent.  Prefix arg means resume editing (don't erase).
1582 When this function returns, the buffer `*mail*' is selected.
1583 The value is t if the message was newly initialized; otherwise, nil.
1584
1585 Optionally, the signature file `mail-signature-file' can be inserted at the
1586 end; see the variable `mail-signature'.
1587
1588 \\<mail-mode-map>
1589 While editing message, type \\[mail-send-and-exit] to send the message and exit.
1590
1591 Various special commands starting with C-c are available in sendmail mode
1592 to move to message header fields:
1593 \\{mail-mode-map}
1594
1595 If `mail-self-blind' is non-nil, a BCC to yourself is inserted
1596 when the message is initialized.
1597
1598 If `mail-default-reply-to' is non-nil, it should be an address (a string);
1599 a Reply-to: field with that address is inserted.
1600
1601 If `mail-archive-file-name' is non-nil, an FCC field with that file name
1602 is inserted.
1603
1604 The normal hook `mail-setup-hook' is run after the message is
1605 initialized.  It can add more default fields to the message.
1606
1607 When calling from a program, the first argument if non-nil says
1608 not to erase the existing contents of the `*mail*' buffer.
1609
1610 The second through fifth arguments,
1611  TO, SUBJECT, IN-REPLY-TO and CC, specify if non-nil
1612  the initial contents of those header fields.
1613  These arguments should not have final newlines.
1614 The sixth argument REPLYBUFFER is a buffer which contains an
1615  original message being replied to, or else an action
1616  of the form (FUNCTION . ARGS) which says how to insert the original.
1617  Or it can be nil, if not replying to anything.
1618 The seventh argument ACTIONS is a list of actions to take
1619  if/when the message is sent.  Each action looks like (FUNCTION . ARGS);
1620  when the message is sent, we apply FUNCTION to ARGS.
1621  This is how Rmail arranges to mark messages `answered'."
1622   (interactive "P")
1623   ;; XEmacs: major changes here
1624   (if mail-use-multiple-buffers-p
1625 ;;; This is commented out because I found it was confusing in practice.
1626 ;;; It is easy enough to rename *mail* by hand with rename-buffer
1627 ;;; if you want to have multiple mail buffers.
1628 ;;; And then you can control which messages to save. --rms.
1629 ;;; Put it back in, conditional on mail-use-multiple-buffers-p --jas
1630       (let ((index 1)
1631             buffer)
1632         ;; If requested, look for a mail buffer that is modified and go to it.
1633         (if noerase
1634             (progn
1635               (while (and (setq buffer
1636                                 (get-buffer (if (= 1 index) "*mail*"
1637                                               (format "*mail*<%d>" index))))
1638                           (not (buffer-modified-p buffer)))
1639                 (setq index (1+ index)))
1640               (if buffer (switch-to-buffer buffer)
1641                 ;; If none exists, start a new message.
1642                 ;; This will never re-use an existing unmodified mail buffer
1643                 ;; (since index is not 1 anymore).  Perhaps it should.
1644                 (setq noerase nil))))
1645         ;; Unless we found a modified message and are happy, start a
1646         ;; new message.
1647         (if (not noerase)
1648             (progn
1649               ;; Look for existing unmodified mail buffer.
1650               (while (and (setq buffer
1651                                 (get-buffer (if (= 1 index) "*mail*"
1652                                               (format "*mail*<%d>" index))))
1653                           (buffer-modified-p buffer))
1654                 (setq index (1+ index)))
1655               ;; If none, make a new one.
1656               (or buffer
1657                   (setq buffer (generate-new-buffer "*mail*")))
1658               ;; Go there and initialize it.
1659               (switch-to-buffer buffer)
1660               (erase-buffer)
1661               (setq default-directory (expand-file-name "~/"))
1662               (auto-save-mode auto-save-default)
1663               (mail-mode)
1664               (mail-setup to subject in-reply-to cc replybuffer actions)
1665               (if (and buffer-auto-save-file-name
1666                        (file-exists-p buffer-auto-save-file-name))
1667                   (message "Auto save file for draft message exists; consider M-x mail-recover"))
1668               t)))
1669
1670     (pop-to-buffer "*mail*")
1671     ;; Avoid danger that the auto-save file can't be written.
1672     (let ((dir (expand-file-name
1673                 (file-name-as-directory mail-default-directory))))
1674       (if (file-exists-p dir)
1675           (setq default-directory dir)))
1676     ;; Only call auto-save-mode if necessary, to avoid changing auto-save file.
1677     (if (or (and auto-save-default (not buffer-auto-save-file-name))
1678             (and (not auto-save-default) buffer-auto-save-file-name))
1679         (auto-save-mode auto-save-default))
1680     (mail-mode)
1681     ;; Disconnect the buffer from its visited file
1682     ;; (in case the user has actually visited a file *mail*).
1683 ;  (set-visited-file-name nil)
1684     (let (initialized)
1685       (and (not noerase)
1686          (if buffer-file-name
1687              (if (buffer-modified-p)
1688                  (when (y-or-n-p "Buffer has unsaved changes; reinitialize it and discard them? ")
1689                    (if (y-or-n-p "Disconnect buffer from visited file? ")
1690                        (set-visited-file-name nil))
1691                    t)
1692                (when (y-or-n-p "Reinitialize buffer, and disconnect it from the visited file? ")
1693                  (set-visited-file-name nil)
1694                  t))
1695            ;; A non-file-visiting buffer.
1696            (if (buffer-modified-p)
1697                (y-or-n-p "Unsent message being composed; erase it? ")
1698              t))
1699          (let ((inhibit-read-only t))
1700            (erase-buffer)
1701            (mail-setup to subject in-reply-to cc replybuffer actions)
1702            (setq initialized t)))
1703       (if (and buffer-auto-save-file-name
1704                (file-exists-p buffer-auto-save-file-name))
1705           (message "Auto save file for draft message exists; consider M-x mail-recover"))
1706       initialized)))
1707
1708 (defun mail-recover-1 ()
1709   "Pop up a list of auto-saved draft messages so you can recover one of them."
1710   (interactive)
1711   (let ((file-name (make-auto-save-file-name))
1712         (ls-lisp-support-shell-wildcards t)
1713         non-random-len wildcard)
1714     ;; Remove the random part from the auto-save-file-name, and
1715     ;; create a wildcard which matches possible candidates.
1716     ;; Note: this knows that make-auto-save-file-name appends
1717     ;; "#<RANDOM-STUFF>#" to the buffer name, where RANDOM-STUFF
1718     ;; is the result of (make-temp-name "").
1719     (setq non-random-len
1720           (- (length file-name) (length (make-temp-name "")) 1))
1721     (setq wildcard (concat (substring file-name 0 non-random-len) "*"))
1722     (if (null (file-expand-wildcards wildcard))
1723         (message "There are no auto-saved drafts to recover")
1724       ;; Bind dired-trivial-filenames to t because all auto-save file
1725       ;; names are normally ``trivial'', so Dired will set point after
1726       ;; all the files, at buffer bottom.  We want it on the first
1727       ;; file instead.
1728       (let ((dired-trivial-filenames t))
1729         (dired-other-window wildcard (concat dired-listing-switches "t")))
1730       (rename-buffer "*Auto-saved Drafts*" t)
1731       (save-excursion
1732         (goto-char (point-min))
1733         (or (looking-at " Move to the draft file you want to recover,")
1734             (let ((inhibit-read-only t))
1735               ;; Each line starts with a space so that Font Lock mode
1736               ;; won't highlight the first character.
1737               (insert "\
1738  Move to the draft file you want to recover, then type C-c C-c
1739  to recover text of message whose composition was interrupted.
1740  To browse text of a draft, type v on the draft file's line.
1741
1742  You can also delete some of these files;
1743  type d on a line to mark that file for deletion.
1744
1745  List of possible auto-save files for recovery:
1746
1747 "))))
1748       (use-local-map
1749        (let ((map (make-sparse-keymap)))
1750          (set-keymap-parent map (current-local-map))
1751          map))
1752       (define-key (current-local-map) "v"
1753         (lambda ()
1754           (interactive)
1755           ;; XEmacs change; emacs-mule is not available, escape-quoted
1756           ;; serves the same purpose.
1757           (let ((coding-system-for-read 'escape-quoted))
1758             (dired-view-file))))
1759       (define-key (current-local-map) "\C-c\C-c"
1760         (lambda ()
1761           (interactive)
1762           (let ((fname (dired-get-filename))
1763                 ;; Auto-saved files are written in the internal
1764                 ;; representation, so they should be read accordingly.
1765                 ;; XEmacs change; emacs-mule is not available, escape-quoted
1766                 ;; serves the same purpose.
1767                 (coding-system-for-read 'escape-quoted))
1768             (switch-to-buffer-other-window "*mail*")
1769             (let ((buffer-read-only nil))
1770               (erase-buffer)
1771               (insert-file-contents fname nil)
1772               ;; insert-file-contents will set buffer-file-coding-system to
1773               ;; escape-quoted on XEmacs, which is not what they want to use
1774               ;; for sending the message.  But we don't know what was its
1775               ;; value before the buffer was killed or Emacs crashed.  We
1776               ;; therefore reset buffer-file-coding-system to the default
1777               ;; value, so that either the default does TRT, or the user
1778               ;; will get prompted for the right encoding when they send the
1779               ;; message.
1780               (setq buffer-file-coding-system
1781                     default-buffer-file-coding-system))))))))
1782
1783 (defun mail-recover ()
1784   "Recover interrupted mail composition from auto-save files.
1785
1786 If the mail buffer has a current valid auto-save file,
1787 the command recovers that file.  Otherwise, it displays a
1788 buffer showing the existing auto-saved draft messages;
1789 you can move to one of them and type C-c C-c to recover that one."
1790   (interactive)
1791   ;; In case they invoke us from some random buffer...
1792   (switch-to-buffer "*mail*")
1793   ;; If *mail* didn't exist, set its directory, so that auto-saved
1794   ;; drafts will be found.
1795   (let ((dir (expand-file-name
1796               (file-name-as-directory mail-default-directory))))
1797     (if (file-exists-p dir)
1798         (setq default-directory dir)))
1799   (or (eq major-mode 'mail-mode)
1800       (mail-mode))
1801   (let ((file-name buffer-auto-save-file-name))
1802     (cond ((and file-name (file-exists-p file-name))
1803            (let ((dispbuf
1804                   ;; This used to invoke `ls' via call-process, but
1805                   ;; dired-noselect is more portable to systems where
1806                   ;; `ls' is not a standard program (it will use
1807                   ;; ls-lisp instead).
1808                   (dired-noselect file-name
1809                                   (concat dired-listing-switches "t"))))
1810              (save-selected-window
1811                (select-window (display-buffer dispbuf t))
1812                (goto-char (point-min))
1813                (forward-line 2)
1814                (dired-move-to-filename)
1815                (setq dispbuf (rename-buffer "*Directory*" t)))
1816              (if (not (yes-or-no-p
1817                        (format "Recover mail draft from auto save file %s? "
1818                                file-name)))
1819                  (error "mail-recover cancelled")
1820                (let ((buffer-read-only nil)
1821                      (buffer-coding buffer-file-coding-system)
1822                      ;; Auto-save files are written in internal
1823                      ;; representation of non-ASCII characters.
1824                      ;; XEmacs change; emacs-mule is not available,
1825                      ;; escape-quoted serves the same purpose. 
1826                      (coding-system-for-read 'escape-quoted))
1827                  (erase-buffer)
1828                  (insert-file-contents file-name nil)
1829                  (setq buffer-file-coding-system buffer-coding)))))
1830           (t (mail-recover-1)))))
1831
1832 ;;;###autoload
1833 (defun mail-other-window (&optional noerase to subject in-reply-to cc replybuffer sendactions)
1834   "Like `mail' command, but display mail buffer in another window."
1835   (interactive "P")
1836   (let ((pop-up-windows t)
1837         (special-display-buffer-names nil)
1838         (special-display-regexps nil)
1839         (same-window-buffer-names nil)
1840         (same-window-regexps nil))
1841     (pop-to-buffer "*mail*"))
1842   (mail noerase to subject in-reply-to cc replybuffer sendactions))
1843
1844 ;;;###autoload
1845 (defun mail-other-frame (&optional noerase to subject in-reply-to cc replybuffer sendactions)
1846   "Like `mail' command, but display mail buffer in another frame."
1847   (interactive "P")
1848   (let ((pop-up-frames t)
1849         (special-display-buffer-names nil)
1850         (special-display-regexps nil)
1851         (same-window-buffer-names nil)
1852         (same-window-regexps nil))
1853     (pop-to-buffer "*mail*"))
1854   (mail noerase to subject in-reply-to cc replybuffer sendactions))
1855
1856 ;;; Do not add anything but external entries on this page.
1857
1858 (provide 'sendmail)
1859
1860 ;;; sendmail.el ends here