Don't quote lambda expressions with `quote'.
[bpt/emacs.git] / lisp / mail / sendmail.el
index c0b9ee5..cfacf5f 100644 (file)
@@ -1,7 +1,6 @@
 ;;; sendmail.el --- mail sending commands for Emacs.  -*- byte-compile-dynamic: t -*-
 
-;; Copyright (C) 1985, 1986, 1992, 1993, 1994, 1995, 1996, 1998, 2000,
-;;   2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
+;; Copyright (C) 1985-1986, 1992-1996, 1998, 2000-2011
 ;;   Free Software Foundation, Inc.
 
 ;; Maintainer: FSF
 ;; documented in the Emacs user's manual.
 
 ;;; Code:
-(eval-when-compile
-  ;; Necessary to avoid recursive `require's.
-  (provide 'sendmail)
-  (require 'rmail)
-  (require 'mailalias))
+(require 'mail-utils)
 
 (autoload 'rfc2047-encode-string "rfc2047")
 
   :version "22.1")
 
 (defcustom sendmail-program
-  (cond
-    ((file-exists-p "/usr/sbin/sendmail") "/usr/sbin/sendmail")
-    ((file-exists-p "/usr/lib/sendmail") "/usr/lib/sendmail")
-    ((file-exists-p "/usr/ucblib/sendmail") "/usr/ucblib/sendmail")
-    (t "fakemail"))                    ;In ../etc, to interface to /bin/mail.
+  (or (executable-find "sendmail")
+      (cond
+       ((file-exists-p "/usr/sbin/sendmail") "/usr/sbin/sendmail")
+       ((file-exists-p "/usr/lib/sendmail") "/usr/lib/sendmail")
+       ((file-exists-p "/usr/ucblib/sendmail") "/usr/ucblib/sendmail")
+       (t "sendmail")))
   "Program used to send messages."
+  :version "24.1"              ; add executable-find, remove fakemail
   :group 'mail
   :type 'file)
 
 ;;;###autoload
-(defcustom mail-from-style 'angles
+(defcustom mail-from-style 'default
   "Specifies how \"From:\" fields look.
 
 If `nil', they contain just the return address like:
@@ -67,15 +64,14 @@ If `parens', they look like:
        king@grassland.com (Elvis Parsley)
 If `angles', they look like:
        Elvis Parsley <king@grassland.com>
-If `system-default', allows the mailer to insert its default From field
-derived from the envelope-from address.
-
-In old versions of Emacs, the `system-default' setting also caused
-Emacs to pass the proper email address from `user-mail-address'
-to the mailer to specify the envelope-from address.  But that is now
-controlled by a separate variable, `mail-specify-envelope-from'."
-  :type '(choice (const nil) (const parens) (const angles)
-                (const system-default))
+
+Otherwise, most addresses look like `angles', but they look like
+`parens' if `angles' would need quoting and `parens' would not."
+  ;; The value `system-default' is now deprecated.
+  :type '(choice (const :tag "simple" nil)
+                (const parens)
+                (const angles)
+                (const default))
   :version "20.3"
   :group 'sendmail)
 
@@ -147,14 +143,18 @@ Otherwise, let mailer send back a message to report errors."
 ;; standard value.
 ;;;###autoload
 (put 'send-mail-function 'standard-value
-     '((if (and window-system (memq system-type '(darwin windows-nt)))
+     ;; MS-Windows can access the clipboard even under -nw.
+     '((if (or (and window-system (eq system-type 'darwin))
+              (eq system-type 'windows-nt))
           'mailclient-send-it
         'sendmail-send-it)))
 
 ;; Useful to set in site-init.el
 ;;;###autoload
 (defcustom send-mail-function
-  (if (and window-system (memq system-type '(darwin windows-nt)))
+  (if (or (and window-system (eq system-type 'darwin))
+         ;; MS-Windows can access the clipboard even under -nw.
+         (eq system-type 'windows-nt))
       'mailclient-send-it
     'sendmail-send-it)
   "Function to call to send the current buffer as mail.
@@ -168,10 +168,13 @@ This is used by the default mail-sending commands.  See also
                (function-item feedmail-send-it :tag "Use Feedmail package")
                (function-item mailclient-send-it :tag "Use Mailclient package")
                function)
+  :initialize 'custom-initialize-delay
   :group 'sendmail)
 
+;;;###autoload(custom-initialize-delay 'send-mail-function nil)
+
 ;;;###autoload
-(defcustom mail-header-separator "--text follows this line--"
+(defcustom mail-header-separator (purecopy "--text follows this line--")
   "Line used to separate headers from text in messages being composed."
   :type 'string
   :group 'sendmail)
@@ -201,17 +204,18 @@ when you first send mail."
   :type '(choice (const nil) string)
   :group 'sendmail)
 
-;;;###autoload
 (defcustom mail-alias-file nil
-  "If non-nil, the name of a file to use instead of `/usr/lib/aliases'.
+  "If non-nil, the name of a file to use instead of the sendmail default.
 This file defines aliases to be expanded by the mailer; this is a different
 feature from that of defining aliases in `.mailrc' to be expanded in Emacs.
-This variable has no effect unless your system uses sendmail as its mailer."
-  :type '(choice (const nil) file)
+This variable has no effect unless your system uses sendmail as its mailer.
+The default file is defined in sendmail's configuration file, e.g.
+`/etc/aliases'."
+  :type '(choice (const :tag "Sendmail default" nil) file)
   :group 'sendmail)
 
 ;;;###autoload
-(defcustom mail-personal-alias-file "~/.mailrc"
+(defcustom mail-personal-alias-file (purecopy "~/.mailrc")
   "If non-nil, the name of the user's personal mail alias file.
 This file typically should be in same format as the `.mailrc' file used by
 the `Mail' or `mailx' program.
@@ -221,8 +225,7 @@ This file need not actually exist."
 
 ;;;###autoload
 (defcustom mail-setup-hook nil
-  "Normal hook, run each time a new outgoing mail message is initialized.
-The function `mail-setup' runs this hook."
+  "Normal hook, run each time a new outgoing message is initialized."
   :type 'hook
   :options '(fortune-to-signature spook mail-abbrevs-setup)
   :group 'sendmail)
@@ -240,7 +243,7 @@ The alias definitions in the file have this form:
   "The modification time of your mail alias file when it was last examined.")
 
 ;;;###autoload
-(defcustom mail-yank-prefix nil
+(defcustom mail-yank-prefix "> "
   "Prefix insert on lines of yanked message being replied to.
 If this is nil, use indentation, as specified by `mail-indentation-spaces'."
   :type '(choice (const nil) string)
@@ -283,19 +286,20 @@ This enables the hook functions to see the whole message header
 regardless of what part of it (if any) is included in the cited text.")
 
 ;;;###autoload
-(defcustom mail-citation-prefix-regexp "[ \t]*[-a-z0-9A-Z]*>+[ \t]*\\|[ \t]*"
+(defcustom mail-citation-prefix-regexp
+  (purecopy "\\([ \t]*\\(\\w\\|[_.]\\)+>+\\|[ \t]*[]>|]\\)+")
   "Regular expression to match a citation prefix plus whitespace.
 It should match whatever sort of citation prefixes you want to handle,
 with whitespace before and after; it should also match just whitespace.
 The default value matches citations like `foo-bar>' plus whitespace."
   :type 'regexp
   :group 'sendmail
-  :version "20.3")
+  :version "24.1")
 
 (defvar mail-abbrevs-loaded nil)
 (defvar mail-mode-map
   (let ((map (make-sparse-keymap)))
-    (define-key map "\M-\t" 'mail-complete)
+    (define-key map "\M-\t" 'completion-at-point)
     (define-key map "\C-c?" 'describe-mode)
     (define-key map "\C-c\C-f\C-t" 'mail-to)
     (define-key map "\C-c\C-f\C-b" 'mail-bcc)
@@ -311,7 +315,6 @@ The default value matches citations like `foo-bar>' plus whitespace."
     (define-key map [remap split-line] 'mail-split-line)
     (define-key map "\C-c\C-q" 'mail-fill-yanked-message)
     (define-key map "\C-c\C-w" 'mail-signature)
-    (define-key map "\C-c\C-v" 'mail-sent-via)
     (define-key map "\C-c\C-c" 'mail-send-and-exit)
     (define-key map "\C-c\C-s" 'mail-send)
     (define-key map "\C-c\C-i" 'mail-attach-file)
@@ -351,9 +354,6 @@ The default value matches citations like `foo-bar>' plus whitespace."
     (define-key map [menu-bar headers expand-aliases]
       '("Expand Aliases" . expand-mail-aliases))
 
-    (define-key map [menu-bar headers sent-via]
-      '("Sent-Via" . mail-sent-via))
-
     (define-key map [menu-bar headers mail-reply-to]
       '("Mail-Reply-To" . mail-mail-reply-to))
 
@@ -381,18 +381,11 @@ The default value matches citations like `foo-bar>' plus whitespace."
     map))
 
 (autoload 'build-mail-aliases "mailalias"
-  "Read mail aliases from user's personal aliases file and set `mail-aliases'."
-  nil)
-
-(autoload 'expand-mail-aliases "mailalias"
-  "Expand all mail aliases in suitable header fields found between BEG and END.
-Suitable header fields are `To', `Cc' and `Bcc' and their `Resent-' variants.
-Optional second arg EXCLUDE may be a regular expression defining text to be
-removed from alias expansions."
-  nil)
+  "Read mail aliases from personal aliases file and set `mail-aliases'.
+By default, this is the file specified by `mail-personal-alias-file'." t)
 
 ;;;###autoload
-(defcustom mail-signature nil
+(defcustom mail-signature t
   "Text inserted at end of mail buffer when a message is initialized.
 If t, it means to insert the contents of the file `mail-signature-file'.
 If a string, that string is inserted.
@@ -408,16 +401,18 @@ and should insert whatever you want to insert."
 (put 'mail-signature 'risky-local-variable t)
 
 ;;;###autoload
-(defcustom mail-signature-file "~/.signature"
+(defcustom mail-signature-file (purecopy "~/.signature")
   "File containing the text inserted at end of mail buffer."
   :type 'file
   :group 'sendmail)
 
 ;;;###autoload
-(defcustom mail-default-directory "~/"
-  "Directory for mail buffers.
-Value of `default-directory' for mail buffers.
-This directory is used for auto-save files of mail buffers."
+(defcustom mail-default-directory (purecopy "~/")
+  "Value of `default-directory' for Mail mode buffers.
+This directory is used for auto-save files of Mail mode buffers.
+
+Note that Message mode does not use this variable; it auto-saves
+in `message-auto-save-directory'."
   :type '(directory :tag "Directory")
   :group 'sendmail
   :version "22.1")
@@ -425,8 +420,7 @@ This directory is used for auto-save files of mail buffers."
 (defvar mail-reply-action nil)
 (defvar mail-send-actions nil
   "A list of actions to be performed upon successful sending of a message.")
-(put 'mail-reply-action 'permanent-local t)
-(put 'mail-send-actions 'permanent-local t)
+(defvar mail-return-action nil)
 
 ;;;###autoload
 (defcustom mail-default-headers nil
@@ -436,8 +430,6 @@ before you edit the message, so you can edit or delete the lines."
   :type '(choice (const nil) string)
   :group 'sendmail)
 
-;; FIXME no need for autoload
-;;;###autoload
 (defcustom mail-bury-selects-summary t
   "If non-nil, try to show Rmail summary buffer after returning from mail.
 The functions \\[mail-send-on-exit] or \\[mail-dont-send] select
@@ -446,8 +438,6 @@ is non-nil."
   :type 'boolean
   :group 'sendmail)
 
-;; FIXME no need for autoload
-;;;###autoload
 (defcustom mail-send-nonascii 'mime
   "Specify whether to allow sending non-ASCII characters in mail.
 If t, that means do allow it.  nil means don't allow it.
@@ -471,23 +461,16 @@ support Delivery Status Notification."
 
 ;; Note: could use /usr/ucb/mail instead of sendmail;
 ;; options -t, and -v if not interactive.
-(defvar mail-mailer-swallows-blank-line
-  (if (and (string-match "sparc-sun-sunos\\(\\'\\|[^5]\\)" system-configuration)
-          (file-readable-p "/etc/sendmail.cf")
-           (with-temp-buffer
-             (insert-file-contents "/etc/sendmail.cf")
-             (goto-char (point-min))
-             (let ((case-fold-search nil))
-               (re-search-forward "^OR\\>" nil t))))
-      ;; According to RFC822, "The field-name must be composed of printable
-      ;; ASCII characters (i.e. characters that have decimal values between
-      ;; 33 and 126, except colon)", i.e. any chars except ctl chars,
-      ;; space, or colon.
-      '(looking-at "[ \t]\\|[][!\"#$%&'()*+,-./0-9;<=>?@A-Z\\\\^_`a-z{|}~]+:"))
+(defvar mail-mailer-swallows-blank-line nil
   "Set this non-nil if the system's mailer runs the header and body together.
-\(This problem exists on Sunos 4 when sendmail is run in remote mode.)
-The value should be an expression to test whether the problem will
-actually occur.")
+The actual value should be an expression to evaluate that returns
+non-nil if the problem will actually occur.
+\(As far as we know, this is not an issue on any system still supported
+by Emacs.)")
+
+(put 'mail-mailer-swallows-blank-line 'risky-local-variable t) ; gets evalled
+(make-obsolete-variable 'mail-mailer-swallows-blank-line
+                       "no need to set this on any modern system." "24.1")
 
 (defvar mail-mode-syntax-table
   ;; define-derived-mode will make it inherit from text-mode-syntax-table.
@@ -538,25 +521,67 @@ actually occur.")
          (setq mail-alias-modtime modtime
                mail-aliases t)))))
 
-(defun mail-setup (to subject in-reply-to cc replybuffer actions)
+\f
+;;;###autoload
+(define-mail-user-agent 'sendmail-user-agent
+  'sendmail-user-agent-compose
+  'mail-send-and-exit)
+
+;;;###autoload
+(defun sendmail-user-agent-compose (&optional to subject other-headers
+                                   continue switch-function yank-action
+                                   send-actions return-action
+                                   &rest ignored)
+  (if switch-function
+      (let ((special-display-buffer-names nil)
+           (special-display-regexps nil)
+           (same-window-buffer-names nil)
+           (same-window-regexps nil))
+       (funcall switch-function "*mail*")))
+  (let ((cc (cdr (assoc-string "cc" other-headers t)))
+       (in-reply-to (cdr (assoc-string "in-reply-to" other-headers t)))
+       (body (cdr (assoc-string "body" other-headers t))))
+    (or (mail continue to subject in-reply-to cc yank-action
+             send-actions return-action)
+       continue
+       (error "Message aborted"))
+    (save-excursion
+      (rfc822-goto-eoh)
+      (while other-headers
+       (unless (member-ignore-case (car (car other-headers))
+                                   '("in-reply-to" "cc" "body"))
+         (insert (car (car other-headers)) ": "
+                 (cdr (car other-headers))
+                 (if use-hard-newlines hard-newline "\n")))
+       (setq other-headers (cdr other-headers)))
+      (when body
+       (forward-line 1)
+       (insert body))
+      t)))
+
+(defun mail-setup (to subject in-reply-to cc replybuffer
+                  actions return-action)
   (or mail-default-reply-to
       (setq mail-default-reply-to (getenv "REPLYTO")))
   (sendmail-sync-aliases)
-  (if (eq mail-aliases t)
-      (progn
-       (setq mail-aliases nil)
-       (when mail-personal-alias-file
-         (if (file-exists-p mail-personal-alias-file)
-             (build-mail-aliases)))))
+  (when (eq mail-aliases t)
+    (setq mail-aliases nil)
+    (and mail-personal-alias-file
+        (file-exists-p mail-personal-alias-file)
+        (build-mail-aliases)))
   ;; Don't leave this around from a previous message.
   (kill-local-variable 'buffer-file-coding-system)
   ;; This doesn't work for enable-multibyte-characters.
   ;; (kill-local-variable 'enable-multibyte-characters)
-  (set-buffer-multibyte default-enable-multibyte-characters)
+  (set-buffer-multibyte (default-value 'enable-multibyte-characters))
   (if current-input-method
       (inactivate-input-method))
+
+  ;; Local variables for Mail mode.
   (setq mail-send-actions actions)
   (setq mail-reply-action replybuffer)
+  (setq mail-return-action return-action)
+
   (goto-char (point-min))
   (if mail-setup-with-from
       (mail-insert-from-field))
@@ -642,11 +667,11 @@ Here are commands that move to a header field (and create it if there isn't):
 \\[mail-signature]  mail-signature (insert `mail-signature-file' file).
 \\[mail-yank-original]  mail-yank-original (insert current message, in Rmail).
 \\[mail-fill-yanked-message]  mail-fill-yanked-message (fill what was yanked).
-\\[mail-sent-via]  mail-sent-via (add a sent-via field for each To or CC).
 Turning on Mail mode runs the normal hooks `text-mode-hook' and
 `mail-mode-hook' (in that order)."
   (make-local-variable 'mail-reply-action)
   (make-local-variable 'mail-send-actions)
+  (make-local-variable 'mail-return-action)
   (setq buffer-offer-save t)
   (make-local-variable 'font-lock-defaults)
   (setq font-lock-defaults '(mail-font-lock-keywords t t))
@@ -669,6 +694,8 @@ Turning on Mail mode runs the normal hooks `text-mode-hook' and
   (setq adaptive-fill-first-line-regexp
        (concat "[ \t]*[-[:alnum:]]*>+[ \t]*\\|"
                adaptive-fill-first-line-regexp))
+  (add-hook 'completion-at-point-functions #'mail-completion-at-point-function
+            nil 'local)
   ;; `-- ' precedes the signature.  `-----' appears at the start of the
   ;; lines that delimit forwarded messages.
   ;; Lines containing just >= 3 dashes, perhaps after whitespace,
@@ -714,7 +741,7 @@ Leave point at the start of the delimiter line."
   "Carry out Auto Fill for Mail mode.
 If within the headers, this makes the new lines into continuation lines."
   (if (< (point) (mail-header-end))
-      (let ((old-line-start (save-excursion (beginning-of-line) (point))))
+      (let ((old-line-start (line-beginning-position)))
        (if (do-auto-fill)
            (save-excursion
              (beginning-of-line)
@@ -780,42 +807,12 @@ Prefix arg means don't delete this window."
   "Bury this mail buffer."
   (let ((newbuf (other-buffer (current-buffer))))
     (bury-buffer (current-buffer))
-    (if (and (or nil
-                ;; In this case, we need to go to a different frame.
-                (window-dedicated-p (frame-selected-window))
-                ;; In this mode of operation, the frame was probably
-                ;; made for this buffer, so the user probably wants
-                ;; to delete it now.
-                (and pop-up-frames (one-window-p))
-                (cdr (assq 'mail-dedicated-frame (frame-parameters))))
-            (not (null (delq (selected-frame) (visible-frame-list)))))
-       (progn
-         (if (display-multi-frame-p)
-             (delete-frame (selected-frame))
-           ;; The previous frame is where normally they have the
-           ;; Rmail buffer displayed.
-           (other-frame -1)))
-      (let (rmail-flag summary-buffer)
-       (and (not arg)
-            (not (one-window-p))
-            (with-current-buffer
-                 (window-buffer (next-window (selected-window) 'not))
-              (setq rmail-flag (eq major-mode 'rmail-mode))
-              (setq summary-buffer
-                    (and mail-bury-selects-summary
-                         (boundp 'rmail-summary-buffer)
-                         rmail-summary-buffer
-                         (buffer-name rmail-summary-buffer)
-                         (not (get-buffer-window rmail-summary-buffer))
-                         rmail-summary-buffer))))
-       (if rmail-flag
-           ;; If the Rmail buffer has a summary, show that.
-           (if summary-buffer (switch-to-buffer summary-buffer)
-             (delete-window))
-         (switch-to-buffer newbuf))))))
+    (if (and (null arg) mail-return-action)
+       (apply (car mail-return-action) (cdr mail-return-action))
+      (switch-to-buffer newbuf))))
 
 (defcustom mail-send-hook nil
-  "Hook run just before sending mail with `mail-send'."
+  "Hook run just before sending a message."
   :type 'hook
   :options '(flyspell-mode-off)
   :group 'sendmail)
@@ -867,9 +864,9 @@ the user from the mailer."
                          (let ((l))
                            (mapc
                             ;; remove duplicates
-                            '(lambda (e)
-                               (unless (member e l)
-                                 (push e l)))
+                            (lambda (e)
+                               (unless (member e l)
+                                 (push e l)))
                             (split-string new-header-values
                                           ",[[:space:]]+" t))
                            (mapconcat 'identity l ", "))
@@ -931,7 +928,7 @@ This function uses `mail-envelope-from'."
 ;;;###autoload
 (defvar sendmail-coding-system nil
   "*Coding system for encoding the outgoing mail.
-This has higher priority than `default-buffer-file-coding-system'
+This has higher priority than the default `buffer-file-coding-system'
 and `default-sendmail-coding-system',
 but lower priority than the local value of `buffer-file-coding-system'.
 See also the function `select-message-coding-system'.")
@@ -954,7 +951,21 @@ See also the function `select-message-coding-system'.")
     (if (string-match "[^\0-\177]" fullname)
        (setq fullname (rfc2047-encode-string fullname)
              quote-fullname t))
-    (cond ((eq mail-from-style 'angles)
+    (cond ((null mail-from-style)
+          (insert "From: " login "\n"))
+         ;; This is deprecated.
+         ((eq mail-from-style 'system-default)
+          nil)
+         ((or (eq mail-from-style 'angles)
+              (and (not (eq mail-from-style 'parens))
+                   ;; Use angles if no quoting is needed, or if
+                   ;; parens would need quoting too.
+                   (or (not (string-match "[^- !#-'*+/-9=?A-Z^-~]" fullname))
+                       (let ((tmp (concat fullname nil)))
+                         (while (string-match "([^()]*)" tmp)
+                           (aset tmp (match-beginning 0) ?-)
+                           (aset tmp (1- (match-end 0)) ?-))
+                         (string-match "[\\()]" tmp)))))
           (insert "From: " fullname)
           (let ((fullname-start (+ (point-min) 6))
                 (fullname-end (point-marker)))
@@ -973,7 +984,8 @@ See also the function `select-message-coding-system'.")
                     (replace-match "\\\\\\&" t))
                   (insert "\""))))
           (insert " <" login ">\n"))
-         ((eq mail-from-style 'parens)
+         ;; 'parens or default
+         (t
           (insert "From: " login " (")
           (let ((fullname-start (point)))
             (if quote-fullname
@@ -996,12 +1008,7 @@ See also the function `select-message-coding-system'.")
                       fullname-end 1)
                 (replace-match "\\1(\\3)" t)
                 (goto-char fullname-start))))
-          (insert ")\n"))
-         ((null mail-from-style)
-          (insert "From: " login "\n"))
-         ((eq mail-from-style 'system-default)
-          nil)
-         (t (error "Invalid value for `mail-from-style'")))))
+          (insert ")\n")))))
 
 ;; Normally you will not need to modify these options unless you are
 ;; using some non-genuine substitute for sendmail which does not
@@ -1032,9 +1039,6 @@ external program defined by `sendmail-program'."
        delimline
        fcc-was-found
        (mailbuf (current-buffer))
-       (program (if (boundp 'sendmail-program)
-                    sendmail-program
-                  "/usr/lib/sendmail"))
        ;; Examine these variables now, so that
        ;; local binding in the mail buffer will take effect.
        (envelope-from
@@ -1082,23 +1086,23 @@ external program defined by `sendmail-program'."
              ;; Delete Resent-BCC ourselves
              (if (save-excursion (beginning-of-line)
                                  (looking-at "resent-bcc"))
-                 (delete-region (save-excursion (beginning-of-line) (point))
-                                (save-excursion (end-of-line) (1+ (point))))))
-;;;  Apparently this causes a duplicate Sender.
-;;;        ;; If the From is different than current user, insert Sender.
-;;;        (goto-char (point-min))
-;;;        (and (re-search-forward "^From:"  delimline t)
-;;;             (progn
-;;;               (require 'mail-utils)
-;;;               (not (string-equal
-;;;                     (mail-strip-quoted-names
-;;;                      (save-restriction
-;;;                        (narrow-to-region (point-min) delimline)
-;;;                        (mail-fetch-field "From")))
-;;;                     (user-login-name))))
-;;;             (progn
-;;;               (forward-line 1)
-;;;               (insert "Sender: " (user-login-name) "\n")))
+                 (delete-region (line-beginning-position)
+                                (line-beginning-position 2))))
+            ;; Apparently this causes a duplicate Sender.
+           ;; ;; If the From is different than current user, insert Sender.
+           ;; (goto-char (point-min))
+           ;; (and (re-search-forward "^From:"  delimline t)
+           ;;      (progn
+           ;;        (require 'mail-utils)
+           ;;        (not (string-equal
+           ;;           (mail-strip-quoted-names
+           ;;            (save-restriction
+           ;;              (narrow-to-region (point-min) delimline)
+           ;;              (mail-fetch-field "From")))
+           ;;           (user-login-name))))
+           ;;      (progn
+           ;;        (forward-line 1)
+           ;;        (insert "Sender: " (user-login-name) "\n")))
            ;; Don't send out a blank subject line
            (goto-char (point-min))
            (if (re-search-forward "^Subject:\\([ \t]*\n\\)+\\b" delimline t)
@@ -1113,7 +1117,10 @@ external program defined by `sendmail-program'."
            (if (not (re-search-forward "^From:" delimline t))
                (mail-insert-from-field))
            ;; Possibly add a MIME header for the current coding system
-           (let (charset)
+           (let (charset where-content-type)
+             (goto-char (point-min))
+             (setq where-content-type
+                   (re-search-forward "^Content-type:" delimline t))
              (goto-char (point-min))
              (and (eq mail-send-nonascii 'mime)
                   (not (re-search-forward "^MIME-version:" delimline t))
@@ -1122,11 +1129,18 @@ external program defined by `sendmail-program'."
                   selected-coding
                   (setq charset
                         (coding-system-get selected-coding :mime-charset))
-                  (goto-char delimline)
-                  (insert "MIME-version: 1.0\n"
-                          "Content-type: text/plain; charset="
-                          (symbol-name charset)
-                          "\nContent-Transfer-Encoding: 8bit\n")))
+                  (progn
+                    (goto-char delimline)
+                    (insert "MIME-version: 1.0\n"
+                            "Content-type: text/plain; charset="
+                            (symbol-name charset)
+                            "\nContent-Transfer-Encoding: 8bit\n")
+                  ;; The character set we will actually use
+                  ;; should override any specified in the message itself.
+                    (when where-content-type
+                      (goto-char where-content-type)
+                      (delete-region (point-at-bol)
+                                     (progn (forward-line 1) (point)))))))
            ;; Insert an extra newline if we need it to work around
            ;; Sun's bug that swallows newlines.
            (goto-char (1+ delimline))
@@ -1150,13 +1164,13 @@ external program defined by `sendmail-program'."
                     (coding-system-for-write selected-coding)
                     (args
                      (append (list (point-min) (point-max)
-                                   program
+                                   sendmail-program
                                    nil errbuf nil "-oi")
                              (and envelope-from
                                   (list "-f" envelope-from))
-;;;                          ;; Don't say "from root" if running under su.
-;;;                          (and (equal (user-real-login-name) "root")
-;;;                               (list "-f" (user-login-name)))
+                             ;; ;; Don't say "from root" if running under su.
+                             ;; (and (equal (user-real-login-name) "root")
+                             ;;      (list "-f" (user-login-name)))
                              (and mail-alias-file
                                   (list (concat "-oA" mail-alias-file)))
                              (if mail-interactive
@@ -1332,6 +1346,9 @@ just append to the file, in Babyl format if necessary."
                                   (point)))))
          ;; Insert a copy, with altered header field name.
          (insert-before-markers "Sent-via:" to-line))))))
+
+(make-obsolete 'mail-sent-via "nobody can remember what it is for." "24.1")
+
 \f
 (defun mail-to ()
   "Move point to end of To field, creating it if necessary."
@@ -1425,7 +1442,7 @@ Prefix argument ATPOINT means insert at point rather than the end."
   ;; whitespace, so that we don't modify the buffer needlessly.
   (if (and (memq mail-signature '(t nil))
           (not (file-readable-p mail-signature-file)))
-      (if (interactive-p)
+      (if (called-interactively-p 'interactive)
          (message "The signature file `%s' could not be read"
                   mail-signature-file))
     (save-excursion
@@ -1484,18 +1501,34 @@ and don't delete any header fields."
   (interactive "P")
   (if mail-reply-action
       (let ((start (point))
-           (original mail-reply-action))
+           (original mail-reply-action)
+           (omark (mark t)))
        (and (consp original) (eq (car original) 'insert-buffer)
             (setq original (nth 1 original)))
        (if (consp original)
-           (apply (car original) (cdr original))
-         ;; If the original message is in another window in the same frame,
-         ;; delete that window to save screen space.
-         ;; t means don't alter other frames.
+           (progn
+             ;; Call yank function, and set the mark if it doesn't.
+             (apply (car original) (cdr original))
+             (if (eq omark (mark t))
+                 (push-mark (point))))
+         ;; If the original message is in another window in the same
+         ;; frame, delete that window to save space.
          (delete-windows-on original t)
          (with-no-warnings
            ;; We really want this to set mark.
-           (insert-buffer original))
+           (insert-buffer original)
+           ;; If they yank the original text, the encoding of the
+           ;; original message is a better default than
+           ;; the default buffer-file-coding-system.
+           (and (coding-system-equal
+                 (default-value 'buffer-file-coding-system)
+                 buffer-file-coding-system)
+                (setq buffer-file-coding-system
+                      (coding-system-change-text-conversion
+                       buffer-file-coding-system
+                       (coding-system-base
+                        (with-current-buffer original
+                          buffer-file-coding-system))))))
          (set-text-properties (point) (mark t) nil))
        (if (consp arg)
            nil
@@ -1621,10 +1654,12 @@ If the current line has `mail-yank-prefix', insert it on the new line."
 ;; Put these commands last, to reduce chance of lossage from quitting
 ;; in middle of loading the file.
 
-;;;###autoload (add-hook 'same-window-buffer-names "*mail*")
+;;;###autoload (add-hook 'same-window-buffer-names (purecopy "*mail*"))
+;;;###autoload (add-hook 'same-window-buffer-names (purecopy "*unsent mail*"))
 
 ;;;###autoload
-(defun mail (&optional noerase to subject in-reply-to cc replybuffer actions)
+(defun mail (&optional noerase to subject in-reply-to cc replybuffer
+                      actions return-action)
   "Edit a message to be sent.  Prefix arg means resume editing (don't erase).
 When this function returns, the buffer `*mail*' is selected.
 The value is t if the message was newly initialized; otherwise, nil.
@@ -1672,49 +1707,6 @@ The seventh argument ACTIONS is a list of actions to take
  when the message is sent, we apply FUNCTION to ARGS.
  This is how Rmail arranges to mark messages `answered'."
   (interactive "P")
-;;;  This is commented out because I found it was confusing in practice.
-;;;  It is easy enough to rename *mail* by hand with rename-buffer
-;;;  if you want to have multiple mail buffers.
-;;;  And then you can control which messages to save. --rms.
-;;;  (let ((index 1)
-;;;    buffer)
-;;;    ;; If requested, look for a mail buffer that is modified and go to it.
-;;;    (if noerase
-;;;    (progn
-;;;      (while (and (setq buffer
-;;;                        (get-buffer (if (= 1 index) "*mail*"
-;;;                                      (format "*mail*<%d>" index))))
-;;;                  (not (buffer-modified-p buffer)))
-;;;        (setq index (1+ index)))
-;;;      (if buffer (switch-to-buffer buffer)
-;;;        ;; If none exists, start a new message.
-;;;        ;; This will never re-use an existing unmodified mail buffer
-;;;        ;; (since index is not 1 anymore).  Perhaps it should.
-;;;        (setq noerase nil))))
-;;;    ;; Unless we found a modified message and are happy, start a new message.
-;;;    (if (not noerase)
-;;;    (progn
-;;;      ;; Look for existing unmodified mail buffer.
-;;;      (while (and (setq buffer
-;;;                        (get-buffer (if (= 1 index) "*mail*"
-;;;                                      (format "*mail*<%d>" index))))
-;;;                  (buffer-modified-p buffer))
-;;;        (setq index (1+ index)))
-;;;      ;; If none, make a new one.
-;;;      (or buffer
-;;;          (setq buffer (generate-new-buffer "*mail*")))
-;;;      ;; Go there and initialize it.
-;;;      (switch-to-buffer buffer)
-;;;      (erase-buffer)
-;;;          (setq default-directory (expand-file-name "~/"))
-;;;          (auto-save-mode auto-save-default)
-;;;          (mail-mode)
-;;;          (mail-setup to subject in-reply-to cc replybuffer actions)
-;;;      (if (and buffer-auto-save-file-name
-;;;               (file-exists-p buffer-auto-save-file-name))
-;;;          (message "Auto save file for draft message exists; consider M-x mail-recover"))
-;;;          t))
-
   (if (eq noerase 'new)
       (pop-to-buffer (generate-new-buffer "*mail*"))
     (and noerase
@@ -1734,7 +1726,7 @@ The seventh argument ACTIONS is a list of actions to take
   (mail-mode)
   ;; Disconnect the buffer from its visited file
   ;; (in case the user has actually visited a file *mail*).
-;;;  (set-visited-file-name nil)
+  ;; (set-visited-file-name nil)
   (let (initialized)
     (and (not (and noerase
                   (not (eq noerase 'new))))
@@ -1753,7 +1745,8 @@ The seventh argument ACTIONS is a list of actions to take
             t))
         (let ((inhibit-read-only t))
           (erase-buffer)
-          (mail-setup to subject in-reply-to cc replybuffer actions)
+          (mail-setup to subject in-reply-to cc replybuffer actions
+                      return-action)
           (setq initialized t)))
     (if (and buffer-auto-save-file-name
             (file-exists-p buffer-auto-save-file-name))
@@ -1783,6 +1776,9 @@ The seventh argument ACTIONS is a list of actions to take
       ;; names are normally ``trivial'', so Dired will set point after
       ;; all the files, at buffer bottom.  We want it on the first
       ;; file instead.
+      ;; Require dired so that dired-trivial-filenames does not get
+      ;; unbound on exit from the let.
+      (require 'dired)
       (let ((dired-trivial-filenames t))
        (dired-other-window wildcard (concat dired-listing-switches "t")))
       (rename-buffer "*Auto-saved Drafts*" t)
@@ -1832,7 +1828,7 @@ The seventh argument ACTIONS is a list of actions to take
              ;; TRT, or the user will get prompted for the right
              ;; encoding when they send the message.
              (setq buffer-file-coding-system
-                   default-buffer-file-coding-system))))))))
+                   (default-value 'buffer-file-coding-system)))))))))
 
 (declare-function dired-move-to-filename "dired" (&optional raise-error eol))
 (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
@@ -1913,5 +1909,4 @@ you can move to one of them and type C-c C-c to recover that one."
 
 (provide 'sendmail)
 
-;; arch-tag: 48bc1025-d993-4d31-8d81-2a29491f0626
 ;;; sendmail.el ends here