home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / lucid / lemacs-19.6 / lisp / modes / sendmail.el < prev    next >
Encoding:
Text File  |  1993-03-14  |  27.2 KB  |  780 lines

  1. ;; Mail sending commands for Emacs.
  2. ;; Copyright (C) 1985, 1986, 1992, 1993 Free Software Foundation, Inc.
  3.  
  4. ;; This file is part of GNU Emacs.
  5.  
  6. ;; GNU Emacs is free software; you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation; either version 2, or (at your option)
  9. ;; any later version.
  10.  
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. ;; GNU General Public License for more details.
  15.  
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs; see the file COPYING.  If not, write to
  18. ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  
  20.  
  21. (provide 'sendmail)
  22.  
  23. ;(defconst mail-self-blind nil
  24. ;  "Non-nil means insert BCC to self in messages to be sent.
  25. ;This is done when the message is initialized,
  26. ;so you can remove or alter the BCC field to override the default.")
  27.  
  28. ;(defconst mail-interactive nil
  29. ;  "Non-nil means when sending a message wait for and display errors.
  30. ;nil means let mailer mail back a message to report errors.")
  31.  
  32. ;(defconst mail-yank-ignored-headers
  33. ;   "^via:\\|^mail-from:\\|^origin:\\|^status:\\|^remailed\\|^received:\\|^message-id:\\|^summary-line:\\|^to:\\|^subject:\\|^in-reply-to:\\|^return-path:"
  34. ;   "Delete these headers from old message when it's inserted in a reply.")
  35. ;(defvar send-mail-function 'sendmail-send-it
  36. ;  "Function to call to send the current buffer as mail.
  37. ;The headers are be delimited by a line which is mail-header-separator"")
  38.  
  39. ; really defined in loaddefs for emacs 17.17+
  40. ;(defvar mail-header-separator "--text follows this line--"
  41. ;  "*Line used to separate headers from text in messages being composed.")
  42. ; really defined in loaddefs for emacs 17.17+
  43. ;(defvar mail-archive-file-name nil
  44. ;  "*Name of file to write all outgoing messages in, or nil for none.")
  45. ; really defined in loaddefs for emacs 17.17+
  46.  
  47. (defvar mail-default-reply-to nil
  48.   "*Address to insert as default Reply-to field of outgoing messages.")
  49.  
  50. (defvar mail-yank-prefix nil
  51.   "*Prefix insert on lines of yanked message being replied to.
  52. nil means use indentation.")
  53.  
  54. (defvar mail-abbrevs-loaded nil)
  55. (defvar mail-mode-map nil)
  56.  
  57. (defvar mail-reply-buffer nil)
  58. (defvar mail-send-actions nil
  59.   "A list of actions to be performed upon successful sending of a message.")
  60.  
  61. (defvar mail-signature-file "~/.signature"
  62.   "File to be inserted at the end of a message. Usually, this file is called
  63. \"~/.signature\".")
  64.  
  65. (defvar mail-insert-signature nil
  66.   "If T, insert automaticcally the file denoted by the variable
  67. `mail-signature-file' before sending a message.")
  68.  
  69. (defvar mail-signature-inserted nil
  70.   "Non-nil means signature already inserted; don't reinsert it.")
  71.  
  72. (defvar mail-mode-syntax-table nil
  73.   "Syntax table used while in mail mode.")
  74.  
  75. (if (null mail-mode-syntax-table)
  76.     (progn
  77.      (setq mail-mode-syntax-table (copy-syntax-table text-mode-syntax-table))
  78.      (modify-syntax-entry ?% ". " mail-mode-syntax-table)))
  79.  
  80. (autoload 'mail-aliases-setup "mail-abbrevs")
  81.  
  82. (defun mail-setup (to subject in-reply-to cc replybuffer actions)
  83.   (setq mail-send-actions actions)
  84.   (mail-aliases-setup)
  85.   (setq mail-reply-buffer replybuffer)
  86.   (setq mail-signature-inserted nil)
  87.   (goto-char (point-min))
  88.   (insert "To: ")
  89.   (save-excursion
  90.     (if to
  91.     (progn
  92.       (insert to "\n")
  93.       ;;; Here removed code to extract names from within <...>
  94.       ;;; on the assumption that mail-strip-quoted-names
  95.       ;;; has been called and has done so.
  96.       (let ((fill-prefix "\t"))
  97.         (fill-region (point-min) (point-max))))
  98.       (newline))
  99.     (if cc
  100.     (let ((opos (point))
  101.           (fill-prefix "\t"))
  102.       (insert "CC: " cc "\n")
  103.       (fill-region-as-paragraph opos (point-max))))
  104.     (if in-reply-to
  105.     (insert "In-reply-to: " in-reply-to "\n"))
  106.     (insert "Subject: " (or subject "") "\n")
  107.     (if mail-default-reply-to
  108.     (insert "Reply-to: " mail-default-reply-to "\n"))
  109.     (if mail-self-blind
  110.     (insert "BCC: " (user-login-name) "\n"))
  111.     (if mail-archive-file-name
  112.     (insert "FCC: " mail-archive-file-name "\n"))
  113.     (insert mail-header-separator "\n"))
  114.   (if to (goto-char (point-max)))
  115.   (or to subject in-reply-to
  116.       (set-buffer-modified-p nil))
  117.   (run-hooks 'mail-setup-hook))
  118.  
  119. (defun mail-mode ()
  120.   "Major mode for editing mail to be sent.
  121. Like Text Mode but with these additional commands:
  122. C-c C-s  mail-send (send the message)    C-c C-c  mail-send-and-exit
  123. C-c C-f  move to a header field (and create it if there isn't):
  124.      C-c C-f C-t  move to To:    C-c C-f C-s  move to Subj:
  125.      C-c C-f C-b  move to BCC:    C-c C-f C-c  move to CC:
  126. C-c C-t  move to message text.
  127. C-c C-y  mail-yank-original (insert current message, in Rmail).
  128. C-c C-w  mail-signature (insert signature file).
  129. C-c C-q  mail-fill-yanked-message (fill what was yanked).
  130. C-c C-v  mail-sent-via (add a sent-via field for each To or CC)
  131.  
  132. Button3  Popup menu with the above commands."
  133.   (interactive)
  134.   (kill-all-local-variables)
  135.   (make-local-variable 'mail-reply-buffer)
  136.   (setq mail-reply-buffer nil)
  137.   (make-local-variable 'mail-send-actions)
  138.   (make-local-variable 'mail-signature-inserted)
  139.   (set-syntax-table mail-mode-syntax-table)
  140.   (use-local-map mail-mode-map)
  141.   (setq local-abbrev-table text-mode-abbrev-table)
  142.   (setq major-mode 'mail-mode)
  143.   (setq mode-name "Mail")
  144.   (setq buffer-offer-save t)
  145.   (make-local-variable 'paragraph-separate)
  146.   (make-local-variable 'paragraph-start)
  147.   (setq paragraph-start (concat "^" mail-header-separator
  148.                 "$\\|^[ \t]*[-_][-_][-_]+$\\|"
  149.                 paragraph-start))
  150.   (setq paragraph-separate (concat "^" mail-header-separator
  151.                    "$\\|^[ \t]*[-_][-_][-_]+$\\|"
  152.                    paragraph-separate))
  153.   (run-hooks 'text-mode-hook 'mail-mode-hook))
  154.  
  155. (if mail-mode-map
  156.     nil
  157.   (setq mail-mode-map (make-sparse-keymap))
  158.   (set-keymap-parent mail-mode-map text-mode-map)
  159.   (define-key mail-mode-map "\C-c?" 'describe-mode)
  160.   (define-key mail-mode-map "\C-c\C-f\C-t" 'mail-to)
  161.   (define-key mail-mode-map "\C-c\C-f\C-b" 'mail-bcc)
  162.   (define-key mail-mode-map "\C-c\C-f\C-c" 'mail-cc)
  163.   (define-key mail-mode-map "\C-c\C-f\C-s" 'mail-subject)
  164.   (define-key mail-mode-map "\C-c\C-t" 'mail-text)
  165.   (define-key mail-mode-map "\C-c\C-y" 'mail-yank-original)
  166.   (define-key mail-mode-map "\C-c\C-w" 'mail-signature)
  167.   (define-key mail-mode-map "\C-c\C-q" 'mail-fill-yanked-message)
  168.   (define-key mail-mode-map "\C-c\C-v" 'mail-sent-via)
  169.   (define-key mail-mode-map "\C-c\C-c" 'mail-send-and-exit)
  170.   (define-key mail-mode-map "\C-c\C-s" 'mail-send)
  171.   (define-key mail-mode-map 'button3   'mail-mode-menu))
  172.  
  173. ;;; mail-mode popup menu
  174.  
  175. (defvar mail-mode-menu
  176.   '("Mail Mode"
  177.     "Sending Mail:"
  178.     "----"
  179.     ["Send and Exit"        mail-send-and-exit        t]
  180.     ["Send Mail"        mail-send            t]
  181.     ["Sent Via"            mail-sent-via            t]
  182.     "----"
  183.     "Go to Field:"
  184.     "----"
  185.     ["To:"            mail-to                t]
  186.     ["Subject:"            mail-subject            t]
  187.     ["CC:"            mail-cc                t]
  188.     ["BCC:"            mail-bcc            t]
  189.     ["Text"            mail-text            t]
  190.     "----"
  191.     "Miscellaneous Commands:"
  192.     "----"
  193.     ["Yank Original"        mail-yank-original        t]
  194.     ["Fill Yanked Message"    mail-fill-yanked-message    t]
  195.     ["Insert Signature"        mail-signature            t]
  196.     "----"
  197.     ["Abort" kill-buffer t]
  198.     )
  199.   "Popup menu called by the function `mail-mode-menu'.")
  200.  
  201. (defun mail-mode-menu (event)
  202.   "Pop up the mail mode menu, defined by the variable `mail-mode-menu'."
  203.   (interactive "e")
  204.   (select-window (event-window event))
  205.   ;; Correctly sensitize the "Yank Original" and "Insert Signature" items.
  206.   (let (yank sig (rest mail-mode-menu))
  207.     (while rest
  208.       (if (vectorp (car rest))
  209.       (cond ((eq (aref (car rest) 1) 'mail-yank-original)
  210.          (setq yank (car rest)))
  211.         ((eq (aref (car rest) 1) 'mail-signature)
  212.          (setq sig (car rest)))))
  213.       (setq rest (cdr rest)))
  214.     (if yank (aset yank 2 (not (null mail-reply-buffer))))
  215.     (if sig (aset sig 2 (and (stringp mail-signature-file)
  216.                  (file-exists-p mail-signature-file)))))
  217.   (popup-menu 'mail-mode-menu))
  218.  
  219.  
  220. (defun mail-send-and-exit (arg)
  221.   "Send message like mail-send, then, if no errors, exit from mail buffer.
  222. Prefix arg means don't delete this window."
  223.   (interactive "P")
  224.   (mail-send)
  225.   (bury-buffer (current-buffer))
  226.   (if (and (not arg)
  227.        (not (one-window-p))
  228.        (save-excursion
  229.          (set-buffer (window-buffer (next-window (selected-window) 'not)))
  230.          (eq major-mode 'rmail-mode)))
  231.       (delete-window)
  232.     (switch-to-buffer (other-buffer (current-buffer)))))
  233.  
  234. (defun mail-send ()
  235.   "Send the message in the current buffer.  If the file denoted by the variable
  236. `mail-signature-file' exists, and the variable `mail-insert-signature' is
  237. non-nil, it is inserted at the end.  If `mail-insert-signature' is nil, your
  238. .signature file will not be inserted unless you do it explicitly with C-c C-w.
  239. If `mail-interactive' is non-nil, wait for success indication or error
  240. messages, and inform user.  Otherwise any failure is reported in a message
  241. back to the user from the mailer."
  242.   (interactive)
  243.   (if (or (buffer-modified-p)
  244.           (y-or-n-p "Message already sent; resend? "))
  245.       (progn
  246.     (message "Sending...")
  247.     (and (not mail-signature-inserted)
  248.          (file-exists-p mail-signature-file)
  249.          mail-insert-signature
  250.          (mail-signature))
  251.     (run-hooks 'mail-send-hook)
  252.     (funcall send-mail-function)
  253.     ;; Now perform actions on successful sending.
  254.     (while mail-send-actions
  255.       (condition-case nil
  256.           (apply (car (car mail-send-actions)) (cdr (car mail-send-actions)))
  257.         (error))
  258.       (setq mail-send-actions (cdr mail-send-actions)))
  259.  
  260. ;    (set-buffer-modified-p nil)
  261. ;    (delete-auto-save-file-if-necessary t)
  262.  
  263.     (if (and (buffer-modified-p) buffer-file-name)
  264.         (if (or noninteractive
  265.             (y-or-n-p (format "Save file %s? " buffer-file-name)))
  266.         (save-buffer))
  267.       (set-buffer-modified-p nil)
  268.       (delete-auto-save-file-if-necessary t))
  269.  
  270.     (message "Sending...done"))))
  271.  
  272. (defun sendmail-send-it ()
  273.   (let ((errbuf (if mail-interactive
  274.             (generate-new-buffer " sendmail errors")
  275.           0))
  276.     (tembuf (generate-new-buffer " sendmail temp"))
  277.     (case-fold-search nil)
  278.     delimline
  279.     (mailbuf (current-buffer)))
  280.     (unwind-protect
  281.     (save-excursion
  282.       (set-buffer tembuf)
  283.       (erase-buffer)
  284.       (insert-buffer-substring mailbuf)
  285.       (goto-char (point-max))
  286.       ;; require one newline at the end.
  287.       (or (= (preceding-char) ?\n)
  288.           (insert ?\n))
  289.       ;; Change header-delimiter to be what sendmail expects.
  290.       (goto-char (point-min))
  291.       (re-search-forward
  292.         (concat "^" (regexp-quote mail-header-separator) "\n"))
  293.       (replace-match "\n")
  294.       (backward-char 1)
  295.       (setq delimline (point-marker))
  296.       (goto-char (point-min))
  297.       ;; ignore any blank lines in the header
  298.       (while (and (re-search-forward "\n\n\n*" delimline t)
  299.               (< (point) delimline))
  300.         (replace-match "\n"))
  301.       (let ((case-fold-search t))
  302.         (goto-char (point-min))
  303.         (if (re-search-forward "^Sender:" delimline t)
  304.         (error "Sender may not be specified."))
  305.         ;; Find and handle any FCC fields.
  306.         (goto-char (point-min))
  307.         (if (re-search-forward "^FCC:" delimline t)
  308.         (mail-do-fcc delimline))
  309.         ;; If the From is different than current user, insert Sender.
  310.         (goto-char (point-min))
  311.         (and (re-search-forward "^From:"  delimline t)
  312.          (progn
  313.            (require 'mail-utils)
  314.            (not (string-equal
  315.              (mail-strip-quoted-names
  316.               (save-restriction
  317.                 (narrow-to-region (point-min) delimline)
  318.                 (mail-fetch-field "From")))
  319.              (user-login-name))))
  320.          (progn
  321.            (forward-line 1)
  322.            (insert "Sender: " (user-login-name) "\n")))
  323.         ;; "S:" is an abbreviation for "Subject:".
  324.         (goto-char (point-min))
  325.         (if (re-search-forward "^S:" delimline t)
  326.         (replace-match "Subject:"))
  327.         ;; Don't send out a blank subject line
  328.         (goto-char (point-min))
  329.         (if (re-search-forward "^Subject:[ \t]*\n" delimline t)
  330.         (replace-match ""))
  331.         (if mail-interactive
  332.         (save-excursion
  333.           (set-buffer errbuf)
  334.           (erase-buffer))))
  335.       (apply 'call-process-region
  336.          (append (list (point-min) (point-max)
  337.                    (if (boundp 'sendmail-program)
  338.                    sendmail-program
  339.                  "/usr/lib/sendmail")
  340.                    nil errbuf nil
  341.                    "-oi" "-t")
  342.              ;; Always specify who from,
  343.              ;; since some systems have broken sendmails.
  344.              (list "-f" (user-login-name))
  345. ;;;             ;; Don't say "from root" if running under su.
  346. ;;;             (and (equal (user-real-login-name) "root")
  347. ;;;                  (list "-f" (user-login-name)))
  348.              ;; These mean "report errors by mail"
  349.              ;; and "deliver in background".
  350.              (if (null mail-interactive) '("-oem" "-odb"))))
  351.       (if mail-interactive
  352.           (save-excursion
  353.         (set-buffer errbuf)
  354.         (goto-char (point-min))
  355.         (while (re-search-forward "\n\n* *" nil t)
  356.           (replace-match "; "))
  357.         (if (not (zerop (buffer-size)))
  358.             (error "Sending...failed to %s"
  359.                (buffer-substring (point-min) (point-max)))))))
  360.       (kill-buffer tembuf)
  361.       (if (bufferp errbuf)
  362.       (kill-buffer errbuf)))))
  363.  
  364. (defun mail-sent-via ()
  365.   "Make a Sent-via header line from each To or CC header line."
  366.   (interactive)
  367.   (save-excursion
  368.     (goto-char (point-min))
  369.     ;; find the header-separator
  370.     (search-forward (concat "\n" mail-header-separator "\n"))
  371.     (forward-line -1)
  372.     ;; put a marker at the end of the header
  373.     (let ((end (point-marker))
  374.       (case-fold-search t)
  375.       to-line)
  376.       (goto-char (point-min))
  377.       ;; search for the To: lines and make Sent-via: lines from them
  378.       ;; search for the next To: line
  379.       (while (re-search-forward "^\\(to\\|cc\\):" end t)
  380.     ;; Grab this line plus all its continuations, sans the `to:'.
  381.     (let ((to-line
  382.            (buffer-substring (point)
  383.                  (progn
  384.                    (if (re-search-forward "^[^ \t\n]" end t)
  385.                        (backward-char 1)
  386.                      (goto-char end))
  387.                    (point)))))
  388.       ;; Insert a copy, with altered header field name.
  389.       (insert-before-markers "Sent-via:" to-line))))))
  390.  
  391. (defun mail-to ()
  392.   "Move point to end of To-field."
  393.   (interactive)
  394.   (expand-abbrev)
  395.   (mail-position-on-field "To"))
  396.  
  397. (defun mail-subject ()
  398.   "Move point to end of Subject-field."
  399.   (interactive)
  400.   (expand-abbrev)
  401.   (mail-position-on-field "Subject"))
  402.  
  403. (defun mail-cc ()
  404.   "Move point to end of CC-field.  Create a CC field if none."
  405.   (interactive)
  406.   (expand-abbrev)
  407.   (or (mail-position-on-field "cc" t)
  408.       (progn (mail-position-on-field "to")
  409.          (insert "\nCC: "))))
  410.  
  411. (defun mail-bcc ()
  412.   "Move point to end of BCC-field.  Create a BCC field if none."
  413.   (interactive)
  414.   (expand-abbrev)
  415.   (or (mail-position-on-field "bcc" t)
  416.       (progn (mail-position-on-field "to")
  417.          (insert "\nBCC: "))))
  418.  
  419. (defun mail-position-on-field (field &optional soft)
  420.   (let (end
  421.     (case-fold-search t))
  422.     (goto-char (point-min))
  423.     (search-forward (concat "\n" mail-header-separator "\n"))
  424.     (setq end (match-beginning 0))
  425.     (goto-char (point-min))
  426.     (if (re-search-forward (concat "^" (regexp-quote field) ":") end t)
  427.     (progn
  428.       (re-search-forward "^[^ \t]" nil 'move)
  429.       (beginning-of-line)
  430.       (skip-chars-backward "\n")
  431.       t)
  432.       (or soft
  433.       (progn (goto-char end)
  434.          (skip-chars-backward "\n")
  435.          (insert "\n" field ": ")))
  436.       nil)))
  437.  
  438. (defun mail-text ()
  439.   "Move point to beginning of text field."
  440.   (interactive)
  441.   (goto-char (point-min))
  442.   (search-forward (concat "\n" mail-header-separator "\n")))
  443.  
  444. (defun mail-signature ()
  445.   "Sign letter with contents of the signature file, 
  446. which is named by the variable `mail-signature-file'."
  447.   (interactive)
  448.   (save-excursion
  449.     (goto-char (point-max))
  450.     (skip-chars-backward " \t\n")
  451.     (end-of-line)
  452.     (delete-region (point) (point-max))
  453.     (insert "\n\n--\n")
  454.     (insert-file-contents (expand-file-name mail-signature-file))
  455.     (setq mail-signature-inserted t)))
  456.  
  457. (defun mail-fill-yanked-message (&optional justifyp)
  458.   "Fill the paragraphs of a message yanked into this one.
  459. Numeric argument means justify as well."
  460.   (interactive "P")
  461.   (save-excursion
  462.     (goto-char (point-min))
  463.     (search-forward (concat "\n" mail-header-separator "\n") nil t)
  464.     (fill-individual-paragraphs (point)
  465.                 (point-max)
  466.                 justifyp
  467.                 t)))
  468.  
  469. (defun mail-yank-original (arg)
  470.   "Insert the message being replied to, if any (in rmail).
  471. Puts point before the text and mark after.
  472. Normally, indents each nonblank line ARG spaces (default 3).
  473. However, if `mail-yank-prefix' is non-nil, insert that prefix on each line.
  474.  
  475. Just \\[universal-argument] as argument means don't indent, insert no prefix,
  476. and don't delete any header fields."
  477.   (interactive "P")
  478.   (if mail-reply-buffer
  479.       (let ((start (point)))
  480.     (delete-windows-on mail-reply-buffer)
  481.     (insert-buffer mail-reply-buffer)
  482.     (if (consp arg)
  483.         nil
  484.       (mail-yank-clear-headers start (mark t))
  485.       (if (null mail-yank-prefix)
  486.           (indent-rigidly start (mark t)
  487.                   (if arg (prefix-numeric-value arg) 3))
  488.         (save-excursion
  489.           (goto-char start)
  490.           (while (< (point) (mark t))
  491.         (insert mail-yank-prefix)
  492.         (forward-line 1)))))
  493.     (let ((zmacs-regions nil))
  494.       (exchange-point-and-mark))
  495.     (if (not (eolp)) (insert ?\n)))))
  496.  
  497. (defun mail-yank-clear-headers (start end)
  498.   (save-excursion
  499.     (goto-char start)
  500.     (if (search-forward "\n\n" end t)
  501.     (save-restriction
  502.       (narrow-to-region start (point))
  503.       (goto-char start)
  504.       (while (let ((case-fold-search t))
  505.            (re-search-forward mail-yank-ignored-headers nil t))
  506.         (beginning-of-line)
  507.         (delete-region (point)
  508.                (progn (re-search-forward "\n[^ \t]")
  509.                   (forward-char -1)
  510.                   (point))))))))
  511.  
  512.  
  513. ;;; FCC hackery, by jwz.  This version works on BABYL and VM buffers.
  514. ;;; To accomplish the latter, VM is loaded when this file is compiled.
  515. ;;; Don't worry, it's only loaded at compile-time.
  516.  
  517. (defun mail-do-fcc (header-end)
  518.   (let (fcc-list
  519.     (send-mail-buffer (current-buffer))
  520.     (tembuf (generate-new-buffer " rmail output"))
  521.     (case-fold-search t)
  522.     beg end)
  523.     (or (markerp header-end) (error "header-end must be a marker"))
  524.     (save-excursion
  525.       (goto-char (point-min))
  526.       (while (re-search-forward "^FCC:[ \t]*" header-end t)
  527.     (setq fcc-list (cons (buffer-substring (point)
  528.                            (progn
  529.                          (end-of-line)
  530.                          (skip-chars-backward " \t")
  531.                          (point)))
  532.                  fcc-list))
  533.     (delete-region (match-beginning 0)
  534.                (progn (forward-line 1) (point))))
  535.       (set-buffer tembuf)
  536.       (erase-buffer)
  537.       ;; insert just the headers to avoid moving the gap more than
  538.       ;; necessary (the message body could be arbitrarily huge.)
  539.       (insert-buffer-substring send-mail-buffer 1 header-end)
  540.  
  541.       ;; if there's no From: or Date: field, cons some.
  542.       (goto-char (point-min))
  543.       (or (re-search-forward "^From[ \t]*:" header-end t)
  544.       (insert "From: " (user-login-name) " (" (user-full-name) ")\n"))
  545.       (goto-char (point-min))
  546.       (or (re-search-forward "^Date[ \t]*:" header-end t)
  547.       (mail-do-fcc-insert-date-header))
  548.  
  549.       ;; insert a magic From_ line.
  550.       (goto-char (point-min))
  551.       (insert "\nFrom " (user-login-name) " " (current-time-string) "\n")
  552.       (goto-char (point-max))
  553.       (insert-buffer-substring send-mail-buffer header-end)
  554.       (goto-char (point-max))
  555.       (insert ?\n)
  556.       (goto-char (1- header-end))
  557.  
  558.       ;; ``Quote'' "^From " as ">From "
  559.       ;;  (note that this isn't really quoting, as there is no requirement
  560.       ;;   that "^[>]+From " be quoted in the same transparent way.)
  561.       (let ((case-fold-search nil))
  562.     (while (search-forward "\nFrom " nil t)
  563.       (forward-char -5)
  564.       (insert ?>)))
  565.  
  566.       (setq beg (point-min)
  567.         end (point-max))
  568.       (while fcc-list
  569.     (let ((target-buffer (get-file-buffer (car fcc-list))))
  570.       (if target-buffer
  571.           ;; File is present in a buffer => append to that buffer.
  572.           (save-excursion
  573.         (set-buffer target-buffer)
  574.         (cond ((eq major-mode 'rmail-mode)
  575.                (mail-do-fcc-rmail-internal tembuf))
  576.               ((eq major-mode 'vm-mode)
  577.                (mail-do-fcc-vm-internal tembuf))
  578.               (t
  579.                ;; Append to an ordinary buffer as a Unix mail message.
  580.                (goto-char (point-max))
  581.                (insert-buffer-substring tembuf beg end))))
  582.         ;; Else append to the file directly.
  583.         ;; (It's OK if it is an RMAIL or VM file -- the message will be
  584.         ;; parsed when the file is read in.)
  585.         (write-region
  586.          ;; Include a blank line before if file already exists.
  587.          (if (file-exists-p (car fcc-list)) (point-min) (1+ (point-min)))
  588.          (point-max) (car fcc-list) t)))
  589.     (setq fcc-list (cdr fcc-list))))
  590.     (kill-buffer tembuf)))
  591.  
  592. (defvar mail-do-fcc-cached-timezone nil)
  593.  
  594. (defun mail-do-fcc-insert-date-header ()
  595.   ;; Convert the ctime() format that `current-time-string' returns into
  596.   ;; an RFC-822-legal date.  
  597.   (let ((s (current-time-string))
  598.     zone)
  599.     (string-match "\\`\\([A-Z][a-z][a-z]\\) +\\([A-Z][a-z][a-z]\\) +\\([0-9][0-9]?\\) *\\([0-9][0-9]?:[0-9][0-9]:[0-9][0-9]\\) *[0-9]?[0-9]?\\([0-9][0-9]\\)"
  600.           s)
  601.     (insert "Date: "
  602.         (substring s (match-beginning 1) (match-end 1)) ", "
  603.         (substring s (match-beginning 3) (match-end 3)) " "
  604.         (substring s (match-beginning 2) (match-end 2)) " "
  605.         (substring s (match-beginning 5) (match-end 5)) " "
  606.         (substring s (match-beginning 4) (match-end 4)) " ")
  607.  
  608.     (if mail-do-fcc-cached-timezone
  609.     (insert mail-do-fcc-cached-timezone "\n")
  610.       ;;
  611.       ;; First, try to use the current-time-zone function, which may not be
  612.       ;; defined, and even if it is defined, may error or return nil.
  613.       ;;
  614.       (or (condition-case ()
  615.           (let ((zoneinfo (current-time-zone)))
  616.         (setq mail-do-fcc-cached-timezone
  617.               (or (if (nth 1 zoneinfo) (nth 3 zoneinfo))
  618.               (nth 2 zoneinfo)))
  619.         (if mail-do-fcc-cached-timezone
  620.             (insert mail-do-fcc-cached-timezone "\n"))
  621.         mail-do-fcc-cached-timezone)
  622.         (error nil))
  623.       ;;
  624.       ;; Otherwise, run date(1) and parse its output.  Yuck!
  625.       ;;
  626.       (save-restriction
  627.         (narrow-to-region (point) (point))
  628.         (call-process "date" nil t nil)
  629.         (end-of-line)
  630.         (insert "\n")
  631.         (forward-word -1)        ; skip back over year
  632.         (delete-region (1- (point)) (1- (point-max))) ; nuke year to end
  633.         (forward-word -1)        ; skip back over zone
  634.         (delete-region (point-min) (point)) ; nuke beginning to zone
  635.         (setq mail-do-fcc-cached-timezone
  636.           (buffer-substring (point-min) (1- (point-max)))))))))
  637.  
  638. (defun mail-do-fcc-rmail-internal (buffer)
  639.   (or (eq major-mode 'rmail-mode) (error "this only works in rmail-mode"))
  640.   (let ((b (point-min))
  641.     (e (point-max))
  642.     (buffer-read-only nil))
  643.     (unwind-protect
  644.     (progn
  645.       (widen)
  646.       (goto-char (point-max))
  647.       ;; This forces RMAIL's message counters to be recomputed when the
  648.       ;; next RMAIL operation is done on the buffer.
  649.       ;; See rmail-maybe-set-message-counters.
  650.       (setq rmail-total-messages nil)
  651.       (insert "\^L\n0, unseen,,\n*** EOOH ***")
  652.       (insert-buffer-substring buffer)
  653.       (insert "\n\C-_"))
  654.       (narrow-to-region b e)
  655.       (rmail-maybe-set-message-counters))))
  656.  
  657. ;;; Load VM into the compilation environment but not the load environment.
  658. (eval-when-compile (require 'vm))
  659.  
  660. (defun mail-do-fcc-vm-internal (buffer)
  661.   (or (eq major-mode 'vm-mode) (error "this only works in vm-mode"))
  662.   (let ((buffer-read-only nil))
  663.     (vm-save-restriction
  664.      (widen)
  665.      (goto-char (point-max))
  666.      (insert-buffer-substring buffer)
  667.      (vm-increment vm-messages-not-on-disk)
  668.      (vm-set-buffer-modified-p t)
  669.      (vm-clear-modification-flag-undos)
  670.      (vm-check-for-killed-summary)
  671.      (vm-assimilate-new-messages)
  672.      (vm-update-summary-and-mode-line))))
  673.  
  674.  
  675. ;; Put these last, to reduce chance of lossage from quitting in middle of loading the file.
  676.  
  677. (defun mail (&optional noerase to subject in-reply-to cc replybuffer actions)
  678.   "Edit a message to be sent.  Argument means resume editing (don't erase).
  679. Search for an existing mail buffer currently not in use and initialize it,
  680. or make a new one if all existing mail buffers are busy.
  681. With an argument, search for a busy existing mail buffer and re-select it.
  682.  
  683. Returns with message buffer selected; value t if message freshly initialized.
  684.  
  685. While editing message, type C-c C-c to send the message and exit.
  686.  
  687. Various special commands starting with C-c are available in sendmail mode
  688. to move to message header fields:
  689. \\{mail-mode-map}
  690.  
  691. If `mail-insert-signature' is non-nil, the signature file, denoted by
  692. the variable `mail-signature-file', is automatically inserted at the
  693. end of the message before sending.  (Otherwise use C-c C-w).
  694.  
  695. If `mail-self-blind' is non-nil, a BCC to yourself is inserted
  696. when the message is initialized.
  697.  
  698. If `mail-default-reply-to' is non-nil, it should be an address (a string);
  699. a Reply-to: field with that address is inserted.
  700.  
  701. If `mail-archive-file-name' is non-nil, an FCC field with that file name
  702. is inserted.
  703.  
  704. If `mail-setup-hook' is bound, its value is called with no arguments
  705. after the message is initialized.  It can add more default fields.
  706.  
  707. When calling from a program, the second through fifth arguments
  708.  TO, SUBJECT, IN-REPLY-TO and CC specify if non-nil
  709.  the initial contents of those header fields.
  710.  These arguments should not have final newlines.
  711. The sixth argument REPLYBUFFER is a buffer whose contents
  712.  should be yanked if the user types C-c C-y.
  713. The seventh argument ACTIONS is a list of actions to take
  714.  if/when the message is sent.  Each action looks like (FUNCTION . ARGS);
  715.  when the message is sent, we apply FUNCTION to ARGS.
  716.  This is how Rmail arranges to mark messages `answered'."
  717.   (interactive "P")
  718.   (let ((index 1)
  719.     buffer)
  720.     ;; If requested, look for a mail buffer that is modified and go to it.
  721.     (if noerase
  722.     (progn
  723.       (while (and (setq buffer
  724.                 (get-buffer (if (= 1 index) "*mail*"
  725.                       (format "*mail*<%d>" index))))
  726.               (not (buffer-modified-p buffer)))
  727.         (setq index (1+ index)))
  728.       (if buffer (switch-to-buffer buffer)
  729.         ;; If none exists, start a new message.
  730.         ;; This will never re-use an existing unmodified mail buffer
  731.         ;; (since index is not 1 anymore).  Perhaps it should.
  732.         (setq noerase nil))))
  733.     ;; Unless we found a modified message and are happy, start a new message.
  734.     (if (not noerase)
  735.     (progn
  736.       ;; Look for existing unmodified mail buffer.
  737.       (while (and (setq buffer
  738.                 (get-buffer (if (= 1 index) "*mail*"
  739.                       (format "*mail*<%d>" index))))
  740.               (buffer-modified-p buffer))
  741.         (setq index (1+ index)))
  742.       ;; If none, make a new one.
  743.       (or buffer
  744.           (setq buffer (generate-new-buffer "*mail*")))
  745.       ;; Go there and initialize it.
  746.       (switch-to-buffer buffer)
  747.       (erase-buffer)
  748.           (setq default-directory (expand-file-name "~/"))
  749.           (auto-save-mode auto-save-default)
  750.           (mail-mode)
  751.           (mail-setup to subject in-reply-to cc replybuffer actions)
  752.       (if (and buffer-auto-save-file-name
  753.            (file-exists-p buffer-auto-save-file-name))
  754.           (message "Auto save file for draft message exists; consider M-x mail-recover"))
  755.           t))))
  756.  
  757. (defun mail-recover ()
  758.   "Reread contents of current buffer from its last auto-save file."
  759.   (interactive)
  760.   (let ((file-name (make-auto-save-file-name)))
  761.     (cond ((save-window-excursion
  762.          (if (not (eq system-type 'vax-vms))
  763.          (with-output-to-temp-buffer "*Directory*"
  764.            (buffer-disable-undo standard-output)
  765.            (call-process "ls" nil standard-output nil "-l" file-name)))
  766.          (yes-or-no-p (format "Recover auto save file %s? " file-name)))
  767.        (let ((buffer-read-only nil))
  768.          (erase-buffer)
  769.          (insert-file-contents file-name nil)))
  770.       (t (error "mail-recover cancelled.")))))
  771.  
  772. (defun mail-other-window (&optional noerase to subject in-reply-to cc replybuffer sendactions)
  773.   "Like `mail' command, but display mail buffer in another window."
  774.   (interactive "P")
  775.   (let ((pop-up-windows t))
  776.     (pop-to-buffer "*mail*"))
  777.   (mail noerase to subject in-reply-to cc replybuffer sendactions))
  778.  
  779. ;;; Do not add anything but external entries on this page.
  780.