home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / source / a2.0bemacs-src.lha / Emacs-19.25 / lisp / sendmail.el! < prev    next >
Encoding:
Text File  |  1994-05-23  |  30.5 KB  |  876 lines

  1. ;;; sendmail.el --- mail sending commands for Emacs.
  2.  
  3. ;; Copyright (C) 1985, 1986, 1992, 1993, 1994 Free Software Foundation, Inc.
  4.  
  5. ;; Maintainer: FSF
  6. ;; Keywords: mail
  7.  
  8. ;; This file is part of GNU Emacs.
  9.  
  10. ;; GNU Emacs is free software; you can redistribute it and/or modify
  11. ;; it under the terms of the GNU General Public License as published by
  12. ;; the Free Software Foundation; either version 2, or (at your option)
  13. ;; any later version.
  14.  
  15. ;; GNU Emacs is distributed in the hope that it will be useful,
  16. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18. ;; GNU General Public License for more details.
  19.  
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs; see the file COPYING.  If not, write to
  22. ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  23.  
  24. ;;; Commentary:
  25.  
  26. ;; This mode provides mail-sending facilities from within Emacs.  It is
  27. ;; documented in the Emacs user's manual.
  28.  
  29. ;;; Code:
  30.  
  31. ;;;###autoload
  32. (defvar mail-self-blind nil "\
  33. Non-nil means insert BCC to self in messages to be sent.
  34. This is done when the message is initialized,
  35. so you can remove or alter the BCC field to override the default.")
  36.  
  37. ;;;###autoload
  38. (defvar mail-interactive nil "\
  39. Non-nil means when sending a message wait for and display errors.
  40. nil means let mailer mail back a message to report errors.")
  41.  
  42. ;;;###autoload
  43. (defvar mail-yank-ignored-headers "^via:\\|^mail-from:\\|^origin:\\|^status:\\|^remailed\\|^received:\\|^message-id:\\|^summary-line:\\|^to:\\|^subject:\\|^in-reply-to:\\|^return-path:" "\
  44. Delete these headers from old message when it's inserted in a reply.")
  45.  
  46. ;; Useful to set in site-init.el
  47. ;;;###autoload
  48. (defvar send-mail-function 'sendmail-send-it "\
  49. Function to call to send the current buffer as mail.
  50. The headers are be delimited by a line which is `mail-header-separator'.")
  51.  
  52. ;;;###autoload
  53. (defvar mail-header-separator "--text follows this line--" "\
  54. *Line used to separate headers from text in messages being composed.")
  55.  
  56. ;;;###autoload
  57. (defvar mail-archive-file-name nil "\
  58. *Name of file to write all outgoing messages in, or nil for none.
  59. Do not use an rmail file here!  Instead, use its inbox file.")
  60.  
  61. ;;;###autoload
  62. (defvar mail-default-reply-to nil
  63.   "*Address to insert as default Reply-to field of outgoing messages.")
  64.  
  65. (defvar mail-alias-file nil
  66.   "*If non-nil, the name of a file to use instead of `/usr/lib/aliases'.
  67. This file defines aliases to be expanded by the mailer; this is a different
  68. feature from that of defining aliases in `.mailrc' to be expanded in Emacs.
  69. This variable has no effect unless your system uses sendmail as its mailer.")
  70.  
  71. (defvar mail-aliases t
  72.   "Alist of mail address aliases,
  73. or t meaning should be initialized from `~/.mailrc'.
  74. The alias definitions in `~/.mailrc' have this form:
  75.     alias ALIAS MEANING")
  76.  
  77. (defvar mail-yank-prefix nil
  78.   "*Prefix insert on lines of yanked message being replied to.
  79. nil means use indentation.")
  80. (defvar mail-indentation-spaces 3
  81.   "*Number of spaces to insert at the beginning of each cited line.
  82. Used by `mail-yank-original' via `mail-yank-cite'.")
  83. (defvar mail-yank-hooks nil
  84.   "Obsolete hook for modifying a citation just inserted in the mail buffer.
  85. Each hook function can find the citation between (point) and (mark t).
  86. And each hook function should leave point and mark around the citation
  87. text as modified.
  88.  
  89. This is a normal hook, misnamed for historical reasons.
  90. It is semi-obsolete and mail agents should no longer use it.")
  91.  
  92. (defvar mail-citation-hook nil
  93.   "*Hook for modifying a citation just inserted in the mail buffer.
  94. Each hook function can find the citation between (point) and (mark t).
  95. And each hook function should leave point and mark around the citation
  96. text as modified.
  97.  
  98. If this hook is entirely empty (nil), a default action is taken
  99. instead of no action.")
  100.  
  101. (defvar mail-abbrevs-loaded nil)
  102. (defvar mail-mode-map nil)
  103.  
  104. (autoload 'build-mail-aliases "mailalias"
  105.   "Read mail aliases from `~/.mailrc' and set `mail-aliases'."
  106.   nil)
  107.  
  108. (autoload 'expand-mail-aliases "mailalias"
  109.   "Expand all mail aliases in suitable header fields found between BEG and END.
  110. Suitable header fields are `To', `Cc' and `Bcc' and their `Resent-' variants.
  111. Optional second arg EXCLUDE may be a regular expression defining text to be
  112. removed from alias expansions."
  113.   nil)
  114.  
  115. ;;;###autoload
  116. (defvar mail-signature nil
  117.   "*Text inserted at end of mail buffer when a message is initialized.
  118. If t, it means to insert the contents of the file `~/.signature'.")
  119.  
  120. (defvar mail-reply-buffer nil)
  121. (defvar mail-send-actions nil
  122.   "A list of actions to be performed upon successful sending of a message.")
  123.  
  124. (defvar mail-default-headers nil
  125.   "*A string containing header lines, to be inserted in outgoing messages.
  126. It is inserted before you edit the message,
  127. so you can edit or delete these lines.")
  128.  
  129. ;; Note: could use /usr/ucb/mail instead of sendmail;
  130. ;; options -t, and -v if not interactive.
  131. (defvar mail-mailer-swallows-blank-line
  132.   (if (and (string-match "sparc-sun-sunos\\(\\'\\|[^5]\\)" system-configuration)
  133.        (file-exists-p "/etc/sendmail.cf")
  134.        (let ((buffer (get-buffer-create " *temp*")))
  135.          (unwind-protect
  136.          (save-excursion
  137.            (set-buffer buffer)
  138.            (insert-file-contents "/etc/sendmail.cf")
  139.            (goto-char (point-min))
  140.            (let ((case-fold-search nil))
  141.              (re-search-forward "^OR\\>" nil t)))
  142.            (kill-buffer buffer))))
  143.       '(looking-at "[ \t]\\|[-a-zA-Z]+:"))
  144.   "Set this non-nil if the system's mailer runs the header and body together.
  145. \(This problem exists on Sunos 4 when sendmail is run in remote mode.)
  146. The value should be an expression to test whether the problem will
  147. actually occur.")
  148.  
  149. (defvar mail-mode-syntax-table nil
  150.   "Syntax table used while in mail mode.")
  151.  
  152. (if (null mail-mode-syntax-table)
  153.     (progn
  154.      (setq mail-mode-syntax-table (copy-syntax-table text-mode-syntax-table))
  155.      (modify-syntax-entry ?% ". " mail-mode-syntax-table)))
  156.  
  157. (defun mail-setup (to subject in-reply-to cc replybuffer actions)
  158.   (if (eq mail-aliases t)
  159.       (progn
  160.     (setq mail-aliases nil)
  161.     (if (file-exists-p "~/.mailrc")
  162.         (build-mail-aliases))))
  163.   (setq mail-send-actions actions)
  164.   (setq mail-reply-buffer replybuffer)
  165.   (goto-char (point-min))
  166.   (insert "To: ")
  167.   (save-excursion
  168.     (if to
  169.     ;; Here removed code to extract names from within <...>
  170.     ;; on the assumption that mail-strip-quoted-names
  171.     ;; has been called and has done so.
  172.     (let ((fill-prefix "\t")
  173.           (address-start (point)))
  174.       (insert to "\n")
  175.       (fill-region-as-paragraph address-start (point-max)))
  176.       (newline))
  177.     (if cc
  178.     (let ((fill-prefix "\t")
  179.           (address-start (progn (insert "CC: ") (point))))
  180.       (insert cc "\n")
  181.       (fill-region-as-paragraph address-start (point-max))))
  182.     (if in-reply-to
  183.     (insert "In-reply-to: " in-reply-to "\n"))
  184.     (insert "Subject: " (or subject "") "\n")
  185.     (if mail-default-headers
  186.     (insert mail-default-headers))
  187.     (if mail-default-reply-to
  188.     (insert "Reply-to: " mail-default-reply-to "\n"))
  189.     (if mail-self-blind
  190.     (insert "BCC: " (user-login-name) "\n"))
  191.     (if mail-archive-file-name
  192.     (insert "FCC: " mail-archive-file-name "\n"))
  193.     (insert mail-header-separator "\n")
  194.     ;; Insert the signature.  But remember the beginning of the message.
  195.     (if to (setq to (point)))
  196.     (cond ((eq mail-signature t)
  197.        (if (file-exists-p "~/.signature")
  198.            (insert-file-contents "~/.signature")))
  199.       (mail-signature
  200.        (insert mail-signature)))
  201.     (goto-char (point-max))
  202.     (or (bolp) (newline)))
  203.   (if to (goto-char to))
  204.   (or to subject in-reply-to
  205.       (set-buffer-modified-p nil))
  206.   (run-hooks 'mail-setup-hook))
  207.  
  208. ;;;###autoload
  209. (defun mail-mode ()
  210.   "Major mode for editing mail to be sent.
  211. Like Text Mode but with these additional commands:
  212. C-c C-s  mail-send (send the message)    C-c C-c  mail-send-and-exit
  213. C-c C-f  move to a header field (and create it if there isn't):
  214.      C-c C-f C-t  move to To:    C-c C-f C-s  move to Subj:
  215.      C-c C-f C-b  move to BCC:    C-c C-f C-c  move to CC:
  216.      C-c C-f C-f  move to FCC:
  217. C-c C-t  move to message text.
  218. C-c C-y  mail-yank-original (insert current message, in Rmail).
  219. C-c C-q  mail-fill-yanked-message (fill what was yanked).
  220. C-c C-v  mail-sent-via (add a sent-via field for each To or CC)."
  221.   (interactive)
  222.   (kill-all-local-variables)
  223.   (make-local-variable 'mail-reply-buffer)
  224.   (setq mail-reply-buffer nil)
  225.   (make-local-variable 'mail-send-actions)
  226.   (set-syntax-table mail-mode-syntax-table)
  227.   (use-local-map mail-mode-map)
  228.   (setq local-abbrev-table text-mode-abbrev-table)
  229.   (setq major-mode 'mail-mode)
  230.   (setq mode-name "Mail")
  231.   (setq buffer-offer-save t)
  232.   (make-local-variable 'paragraph-separate)
  233.   (make-local-variable 'paragraph-start)
  234.   (setq paragraph-start (concat "^" mail-header-separator
  235.                 "$\\|^[ \t]*[-_][-_][-_]+$\\|"
  236.                 paragraph-start))
  237.   (setq paragraph-separate (concat "^" mail-header-separator
  238.                    "$\\|^[ \t]*[-_][-_][-_]+$\\|"
  239.                    paragraph-separate))
  240.   (run-hooks 'text-mode-hook 'mail-mode-hook))
  241.  
  242. ;;; Set up keymap.
  243.  
  244. (if mail-mode-map
  245.     nil
  246.   (setq mail-mode-map (nconc (make-sparse-keymap) text-mode-map))
  247.   (define-key mail-mode-map "\C-c?" 'describe-mode)
  248.   (define-key mail-mode-map "\C-c\C-f\C-t" 'mail-to)
  249.   (define-key mail-mode-map "\C-c\C-f\C-b" 'mail-bcc)
  250.   (define-key mail-mode-map "\C-c\C-f\C-f" 'mail-fcc)
  251.   (define-key mail-mode-map "\C-c\C-f\C-c" 'mail-cc)
  252.   (define-key mail-mode-map "\C-c\C-f\C-s" 'mail-subject)
  253.   (define-key mail-mode-map "\C-c\C-t" 'mail-text)
  254.   (define-key mail-mode-map "\C-c\C-y" 'mail-yank-original)
  255.   (define-key mail-mode-map "\C-c\C-q" 'mail-fill-yanked-message)
  256.   (define-key mail-mode-map "\C-c\C-w" 'mail-signature)
  257.   (define-key mail-mode-map "\C-c\C-v" 'mail-sent-via)
  258.   (define-key mail-mode-map "\C-c\C-c" 'mail-send-and-exit)
  259.   (define-key mail-mode-map "\C-c\C-s" 'mail-send))
  260.  
  261. (define-key mail-mode-map [menu-bar mail]
  262.   (cons "Mail" (make-sparse-keymap "Mail")))
  263.  
  264. (define-key mail-mode-map [menu-bar mail fill]
  265.   '("Fill Citation" . mail-fill-yanked-message))
  266.  
  267. (define-key mail-mode-map [menu-bar mail yank]
  268.   '("Cite Original" . mail-yank-original))
  269.  
  270. (define-key mail-mode-map [menu-bar mail signature]
  271.   '("Insert Signature" . mail-signature))
  272.  
  273. (define-key mail-mode-map [menu-bar mail cancel]
  274.   '("Cancel" . mail-dont-send))
  275.  
  276. (define-key mail-mode-map [menu-bar mail send-stay]
  277.   '("Send, Keep Editing" . mail-send))
  278.  
  279. (define-key mail-mode-map [menu-bar mail send]
  280.   '("Send Message" . mail-send-and-exit))
  281.  
  282. (define-key mail-mode-map [menu-bar headers]
  283.   (cons "Headers" (make-sparse-keymap "Headers")))
  284.  
  285. (define-key mail-mode-map [menu-bar headers sent-via]
  286.   '("Sent Via" . mail-sent-via))
  287.  
  288. (define-key mail-mode-map [menu-bar headers text]
  289.   '("Text" . mail-text))
  290.  
  291. (define-key mail-mode-map [menu-bar headers bcc]
  292.   '("Bcc" . mail-bcc))
  293.  
  294. (define-key mail-mode-map [menu-bar headers fcc]
  295.   '("Fcc" . mail-fcc))
  296.  
  297. (define-key mail-mode-map [menu-bar headers cc]
  298.   '("Cc" . mail-cc))
  299.  
  300. (define-key mail-mode-map [menu-bar headers subject]
  301.   '("Subject" . mail-subject))
  302.  
  303. (define-key mail-mode-map [menu-bar headers to]
  304.   '("To" . mail-to))
  305.  
  306. (defun mail-send-and-exit (arg)
  307.   "Send message like `mail-send', then, if no errors, exit from mail buffer.
  308. Prefix arg means don't delete this window."
  309.   (interactive "P")
  310.   (mail-send)
  311.   (mail-bury arg))
  312.  
  313. (defun mail-dont-send (arg)
  314.   "Don't send the message you have been editing.
  315. Prefix arg means don't delete this window."
  316.   (interactive "P")
  317.   (mail-bury arg))
  318.  
  319. (defun mail-bury (arg)
  320.   "Bury this mail buffer."
  321.   (let ((newbuf (other-buffer (current-buffer))))
  322.     (bury-buffer (current-buffer))
  323.     (if (and (fboundp 'frame-parameters)
  324.          (cdr (assq 'dedicated (frame-parameters)))
  325.          (not (null (delq (selected-frame) (visible-frame-list)))))
  326.     (delete-frame (selected-frame))
  327.       (if (and (not arg)
  328.            (not (one-window-p))
  329.            (save-excursion
  330.          (set-buffer (window-buffer (next-window (selected-window) 'not)))
  331.          (eq major-mode 'rmail-mode)))
  332.       (delete-window)
  333.     (switch-to-buffer newbuf)))))
  334.  
  335. (defun mail-send ()
  336.   "Send the message in the current buffer.
  337. If `mail-interactive' is non-nil, wait for success indication
  338. or error messages, and inform user.
  339. Otherwise any failure is reported in a message back to
  340. the user from the mailer."
  341.   (interactive)
  342.   (if (if buffer-file-name
  343.       (y-or-n-p "Send buffer contents as mail message? ")
  344.     (or (buffer-modified-p)
  345.         (y-or-n-p "Message already sent; resend? ")))
  346.       (progn
  347.     (message "Sending...")
  348.     (run-hooks 'mail-send-hook)
  349.     (funcall send-mail-function)
  350.     ;; Now perform actions on successful sending.
  351.     (while mail-send-actions
  352.       (condition-case nil
  353.           (apply (car (car mail-send-actions))
  354.              (cdr (car mail-send-actions)))
  355.         (error))
  356.       (setq mail-send-actions (cdr mail-send-actions)))
  357.     (message "Sending...done")
  358.     ;; If buffer has no file, mark it as unmodified and delete autosave.
  359.     (if (not buffer-file-name)
  360.         (progn
  361.           (set-buffer-modified-p nil)
  362.           (delete-auto-save-file-if-necessary t))))))
  363.  
  364. (defun sendmail-send-it ()
  365.   (let ((errbuf (if mail-interactive
  366.             (generate-new-buffer " sendmail errors")
  367.           0))
  368.     (tembuf (generate-new-buffer " sendmail temp"))
  369.     (case-fold-search nil)
  370.     delimline
  371.     (mailbuf (current-buffer)))
  372.     (unwind-protect
  373.     (save-excursion
  374.       (set-buffer tembuf)
  375.       (erase-buffer)
  376.       (insert-buffer-substring mailbuf)
  377.       (goto-char (point-max))
  378.       ;; require one newline at the end.
  379.       (or (= (preceding-char) ?\n)
  380.           (insert ?\n))
  381.       ;; Change header-delimiter to be what sendmail expects.
  382.       (goto-char (point-min))
  383.       (re-search-forward
  384.         (concat "^" (regexp-quote mail-header-separator) "\n"))
  385.       (replace-match "\n")
  386.       (backward-char 1)
  387.       (setq delimline (point-marker))
  388.       (if mail-aliases
  389.           (expand-mail-aliases (point-min) delimline))
  390.       (goto-char (point-min))
  391.       ;; ignore any blank lines in the header
  392.       (while (and (re-search-forward "\n\n\n*" delimline t)
  393.               (< (point) delimline))
  394.         (replace-match "\n"))
  395.       (let ((case-fold-search t))
  396.         (goto-char (point-min))
  397.         (if (re-search-forward "^Sender:" delimline t)
  398.         (error "Sender may not be specified."))
  399.         ;; Find and handle any FCC fields.
  400.         (goto-char (point-min))
  401.         (if (re-search-forward "^FCC:" delimline t)
  402.         (mail-do-fcc delimline))
  403. ;;; Apparently this causes a duplicate Sender.
  404. ;;;        ;; If the From is different than current user, insert Sender.
  405. ;;;        (goto-char (point-min))
  406. ;;;        (and (re-search-forward "^From:"  delimline t)
  407. ;;;         (progn
  408. ;;;           (require 'mail-utils)
  409. ;;;           (not (string-equal
  410. ;;;             (mail-strip-quoted-names
  411. ;;;              (save-restriction
  412. ;;;                (narrow-to-region (point-min) delimline)
  413. ;;;                (mail-fetch-field "From")))
  414. ;;;             (user-login-name))))
  415. ;;;         (progn
  416. ;;;           (forward-line 1)
  417. ;;;           (insert "Sender: " (user-login-name) "\n")))
  418.         ;; "S:" is an abbreviation for "Subject:".
  419.         (goto-char (point-min))
  420.         (if (re-search-forward "^S:" delimline t)
  421.         (replace-match "Subject:"))
  422.         ;; Don't send out a blank subject line
  423.         (goto-char (point-min))
  424.         (if (re-search-forward "^Subject:[ \t]*\n" delimline t)
  425.         (replace-match ""))
  426.         ;; Insert an extra newline if we need it to work around
  427.         ;; Sun's bug that swallows newlines.
  428.         (goto-char (1+ delimline))
  429.         (if (eval mail-mailer-swallows-blank-line)
  430.         (newline))
  431.         (if mail-interactive
  432.         (save-excursion
  433.           (set-buffer errbuf)
  434.           (erase-buffer))))
  435.       (apply 'call-process-region
  436.          (append (list (point-min) (point-max)
  437.                    (if (boundp 'sendmail-program)
  438.                    sendmail-program
  439.                  "/usr/lib/sendmail")
  440.                    nil errbuf nil
  441.                    "-oi" "-t")
  442.              ;; Always specify who from,
  443.              ;; since some systems have broken sendmails.
  444.              (list "-f" (user-login-name))
  445. ;;;             ;; Don't say "from root" if running under su.
  446. ;;;             (and (equal (user-real-login-name) "root")
  447. ;;;                  (list "-f" (user-login-name)))
  448.              (and mail-alias-file
  449.                   (list (concat "-oA" mail-alias-file)))
  450.              ;; These mean "report errors by mail"
  451.              ;; and "deliver in background".
  452.              (if (null mail-interactive) '("-oem" "-odb"))))
  453.       (if mail-interactive
  454.           (save-excursion
  455.         (set-buffer errbuf)
  456.         (goto-char (point-min))
  457.         (while (re-search-forward "\n\n* *" nil t)
  458.           (replace-match "; "))
  459.         (if (not (zerop (buffer-size)))
  460.             (error "Sending...failed to %s"
  461.                (buffer-substring (point-min) (point-max)))))))
  462.       (kill-buffer tembuf)
  463.       (if (bufferp errbuf)
  464.       (kill-buffer errbuf)))))
  465.  
  466. (defun mail-do-fcc (header-end)
  467.   (let (fcc-list
  468.     (rmailbuf (current-buffer))
  469.     (time (current-time))
  470.     (tembuf (generate-new-buffer " rmail output"))
  471.     (case-fold-search t))
  472.     (save-excursion
  473.       (goto-char (point-min))
  474.       (while (re-search-forward "^FCC:[ \t]*" header-end t)
  475.     (setq fcc-list (cons (buffer-substring (point)
  476.                            (progn
  477.                          (end-of-line)
  478.                          (skip-chars-backward " \t")
  479.                          (point)))
  480.                  fcc-list))
  481.     (delete-region (match-beginning 0)
  482.                (progn (forward-line 1) (point))))
  483.       (set-buffer tembuf)
  484.       (erase-buffer)
  485.       ;; This initial newline is written out if the fcc file already exists.
  486.       (insert "\nFrom " (user-login-name) " "
  487.           (current-time-string time) "\n")
  488.       ;; Insert the time zone before the year.
  489.       (forward-char -1)
  490.       (forward-word -1)
  491.       (require 'mail-utils)
  492.       (insert (mail-rfc822-time-zone time) " ")
  493.       (goto-char (point-max))
  494.       (insert-buffer-substring rmailbuf)
  495.       ;; Make sure messages are separated.
  496.       (goto-char (point-max))
  497.       (insert ?\n)
  498.       (goto-char 2)
  499.       ;; ``Quote'' "^From " as ">From "
  500.       ;;  (note that this isn't really quoting, as there is no requirement
  501.       ;;   that "^[>]+From " be quoted in the same transparent way.)
  502.       (let ((case-fold-search nil))
  503.     (while (search-forward "\nFrom " nil t)
  504.       (forward-char -5)
  505.       (insert ?>)))
  506.       (while fcc-list
  507.     (let ((buffer (get-file-buffer (car fcc-list))))
  508.       (if buffer
  509.           ;; File is present in a buffer => append to that buffer.
  510.           (let ((curbuf (current-buffer))
  511.             (beg (point-min)) (end (point-max))
  512.             (beg2 (save-excursion (goto-char (point-min))
  513.                       (forward-line 2) (point))))
  514.         (save-excursion
  515.           (set-buffer buffer)
  516.           ;; Keep the end of the accessible portion at the same place
  517.           ;; unless it is the end of the buffer.
  518.           (let ((max (if (/= (1+ (buffer-size)) (point-max))
  519.                  (point-max))))
  520.             (unwind-protect
  521.             ;; Code below lifted from rmailout.el
  522.             ;; function rmail-output-to-rmail-file:
  523.             (let ((buffer-read-only nil)
  524.                   (msg (and (boundp 'rmail-current-message)
  525.                     rmail-current-message)))
  526.               ;; If MSG is non-nil, buffer is in RMAIL mode.
  527.               (if msg
  528.                   (progn
  529.                 (rmail-maybe-set-message-counters)
  530.                 (widen)
  531.                 (narrow-to-region (point-max) (point-max))
  532.                 (insert "\C-l\n0, unseen,,\n*** EOOH ***\n"
  533.                     "From: " (user-login-name) "\n"
  534.                     "Date: " (mail-rfc822-date) "\n")
  535.                 (insert-buffer-substring curbuf beg2 end)
  536.                 (insert "\n\C-_")
  537.                 (goto-char (point-min))
  538.                 (widen)
  539.                 (search-backward "\n\^_")
  540.                 (narrow-to-region (point) (point-max))
  541.                 (rmail-count-new-messages t)
  542.                 (rmail-show-message msg)
  543.                 (setq max nil))
  544.                 ;; Output file not in rmail mode
  545.                 ;; => just insert at the end.
  546.                 (narrow-to-region (point-min) (1+ (buffer-size)))
  547.                 (goto-char (point-max))
  548.                 (insert-buffer-substring curbuf beg end)))
  549.               (if max (narrow-to-region (point-min) max))))))
  550.         ;; Else append to the file directly.
  551.         (write-region
  552.          (1+ (point-min)) (point-max) (car fcc-list) t)))
  553.     (setq fcc-list (cdr fcc-list))))
  554.     (kill-buffer tembuf)))
  555.  
  556. (defun mail-sent-via ()
  557.   "Make a Sent-via header line from each To or CC header line."
  558.   (interactive)
  559.   (save-excursion
  560.     (goto-char (point-min))
  561.     ;; find the header-separator
  562.     (search-forward (concat "\n" mail-header-separator "\n"))
  563.     (forward-line -1)
  564.     ;; put a marker at the end of the header
  565.     (let ((end (point-marker))
  566.       (case-fold-search t)
  567.       to-line)
  568.       (goto-char (point-min))
  569.       ;; search for the To: lines and make Sent-via: lines from them
  570.       ;; search for the next To: line
  571.       (while (re-search-forward "^\\(to\\|cc\\):" end t)
  572.     ;; Grab this line plus all its continuations, sans the `to:'.
  573.     (let ((to-line
  574.            (buffer-substring (point)
  575.                  (progn
  576.                    (if (re-search-forward "^[^ \t\n]" end t)
  577.                        (backward-char 1)
  578.                      (goto-char end))
  579.                    (point)))))
  580.       ;; Insert a copy, with altered header field name.
  581.       (insert-before-markers "Sent-via:" to-line))))))
  582.  
  583. (defun mail-to ()
  584.   "Move point to end of To-field."
  585.   (interactive)
  586.   (expand-abbrev)
  587.   (mail-position-on-field "To"))
  588.  
  589. (defun mail-subject ()
  590.   "Move point to end of Subject-field."
  591.   (interactive)
  592.   (expand-abbrev)
  593.   (mail-position-on-field "Subject"))
  594.  
  595. (defun mail-cc ()
  596.   "Move point to end of CC-field.  Create a CC field if none."
  597.   (interactive)
  598.   (expand-abbrev)
  599.   (or (mail-position-on-field "cc" t)
  600.       (progn (mail-position-on-field "to")
  601.          (insert "\nCC: "))))
  602.  
  603. (defun mail-bcc ()
  604.   "Move point to end of BCC-field.  Create a BCC field if none."
  605.   (interactive)
  606.   (expand-abbrev)
  607.   (or (mail-position-on-field "bcc" t)
  608.       (progn (mail-position-on-field "to")
  609.          (insert "\nBCC: "))))
  610.  
  611. (defun mail-fcc (folder)
  612.   "Add a new FCC field, with file name completion."
  613.   (interactive "FFolder carbon copy: ")
  614.   (expand-abbrev)
  615.   (or (mail-position-on-field "fcc" t)    ;Put new field after exiting FCC.
  616.       (mail-position-on-field "to"))
  617.   (insert "\nFCC: " folder))
  618.  
  619. (defun mail-position-on-field (field &optional soft)
  620.   (let (end
  621.     (case-fold-search t))
  622.     (goto-char (point-min))
  623.     (re-search-forward (concat "^" (regexp-quote mail-header-separator) "\n"))
  624.     (setq end (match-beginning 0))
  625.     (goto-char (point-min))
  626.     (if (re-search-forward (concat "^" (regexp-quote field) ":") end t)
  627.     (progn
  628.       (re-search-forward "^[^ \t]" nil 'move)
  629.       (beginning-of-line)
  630.       (skip-chars-backward "\n")
  631.       t)
  632.       (or soft
  633.       (progn (goto-char end)
  634.          (insert field ": \n")
  635.          (skip-chars-backward "\n")))
  636.       nil)))
  637.  
  638. (defun mail-text ()
  639.   "Move point to beginning of text field."
  640.   (interactive)
  641.   (goto-char (point-min))
  642.   (search-forward (concat "\n" mail-header-separator "\n")))
  643.  
  644. (defun mail-signature (atpoint)
  645.   "Sign letter with contents of the file `~/.signature'.
  646. Prefix arg means put contents at point."
  647.   (interactive "P")
  648.   (save-excursion
  649.     (or atpoint
  650.     (goto-char (point-max)))
  651.     (skip-chars-backward " \t\n")
  652.     (end-of-line)
  653.     (or atpoint
  654.     (delete-region (point) (point-max)))
  655.     (insert "\n\n-- \n")
  656.     (insert-file-contents (expand-file-name "~/.signature"))))
  657.  
  658. (defun mail-fill-yanked-message (&optional justifyp)
  659.   "Fill the paragraphs of a message yanked into this one.
  660. Numeric argument means justify as well."
  661.   (interactive "P")
  662.   (save-excursion
  663.     (goto-char (point-min))
  664.     (search-forward (concat "\n" mail-header-separator "\n") nil t)
  665.     (fill-individual-paragraphs (point)
  666.                 (point-max)
  667.                 justifyp
  668.                 t)))
  669.  
  670. (defun mail-indent-citation ()
  671.   "Modify text just inserted from a message to be cited.
  672. The inserted text should be the region.
  673. When this function returns, the region is again around the modified text.
  674.  
  675. Normally, indent each nonblank line `mail-indentation-spaces' spaces.
  676. However, if `mail-yank-prefix' is non-nil, insert that prefix on each line."
  677.   (let ((start (point)))
  678.     (mail-yank-clear-headers start (mark t))
  679.     (if (null mail-yank-prefix)
  680.     (indent-rigidly start (mark t) mail-indentation-spaces)
  681.       (save-excursion
  682.     (goto-char start)
  683.     (while (< (point) (mark t))
  684.       (insert mail-yank-prefix)
  685.       (forward-line 1))))))
  686.  
  687. (defun mail-yank-original (arg)
  688.   "Insert the message being replied to, if any (in rmail).
  689. Puts point before the text and mark after.
  690. Normally, indents each nonblank line ARG spaces (default 3).
  691. However, if `mail-yank-prefix' is non-nil, insert that prefix on each line.
  692.  
  693. Just \\[universal-argument] as argument means don't indent, insert no prefix,
  694. and don't delete any header fields."
  695.   (interactive "P")
  696.   (if mail-reply-buffer
  697.       (let ((start (point)))
  698.     ;; If the original message is in another window in the same frame,
  699.     ;; delete that window to save screen space.
  700.     ;; t means don't alter other frames.
  701.     (delete-windows-on mail-reply-buffer t)
  702.     (insert-buffer mail-reply-buffer)
  703.     (if (consp arg)
  704.         nil
  705.       (goto-char start)
  706.       (let ((mail-indentation-spaces (if arg (prefix-numeric-value arg)
  707.                        mail-indentation-spaces)))
  708.         (if mail-citation-hook
  709.         (run-hooks 'mail-citation-hook)
  710.           (if mail-yank-hooks
  711.           (run-hooks 'mail-yank-hooks)
  712.         (mail-indent-citation)))))
  713.     ;; This is like exchange-point-and-mark, but doesn't activate the mark.
  714.     ;; It is cleaner to avoid activation, even though the command
  715.     ;; loop would deactivate the mark because we inserted text.
  716.     (goto-char (prog1 (mark t)
  717.              (set-marker (mark-marker) (point) (current-buffer))))
  718.     (if (not (eolp)) (insert ?\n)))))
  719.  
  720. (defun mail-yank-clear-headers (start end)
  721.   (save-excursion
  722.     (goto-char start)
  723.     (if (search-forward "\n\n" end t)
  724.     (save-restriction
  725.       (narrow-to-region start (point))
  726.       (goto-char start)
  727.       (while (let ((case-fold-search t))
  728.            (re-search-forward mail-yank-ignored-headers nil t))
  729.         (beginning-of-line)
  730.         (delete-region (point)
  731.                (progn (re-search-forward "\n[^ \t]")
  732.                   (forward-char -1)
  733.                   (point))))))))
  734.  
  735. ;; Put these last, to reduce chance of lossage from quitting in middle of loading the file.
  736.  
  737. ;;;###autoload
  738. (defun mail (&optional noerase to subject in-reply-to cc replybuffer actions)
  739.   "Edit a message to be sent.  Prefix arg means resume editing (don't erase).
  740. When this function returns, the buffer `*mail*' is selected.
  741. The value is t if the message was newly initialized; otherwise, nil.
  742.  
  743. By default, the signature file `~/.signature' is inserted at the end;
  744. see the variable `mail-signature'.
  745.  
  746. \\<mail-mode-map>
  747. While editing message, type \\[mail-send-and-exit] to send the message and exit.
  748.  
  749. Various special commands starting with C-c are available in sendmail mode
  750. to move to message header fields:
  751. \\{mail-mode-map}
  752.  
  753. If `mail-self-blind' is non-nil, a BCC to yourself is inserted
  754. when the message is initialized.
  755.  
  756. If `mail-default-reply-to' is non-nil, it should be an address (a string);
  757. a Reply-to: field with that address is inserted.
  758.  
  759. If `mail-archive-file-name' is non-nil, an FCC field with that file name
  760. is inserted.
  761.  
  762. If `mail-setup-hook' is bound, its value is called with no arguments
  763. after the message is initialized.  It can add more default fields.
  764.  
  765. When calling from a program, the second through fifth arguments
  766.  TO, SUBJECT, IN-REPLY-TO and CC specify if non-nil
  767.  the initial contents of those header fields.
  768.  These arguments should not have final newlines.
  769. The sixth argument REPLYBUFFER is a buffer whose contents
  770.  should be yanked if the user types C-c C-y.
  771. The seventh argument ACTIONS is a list of actions to take
  772.  if/when the message is sent.  Each action looks like (FUNCTION . ARGS);
  773.  when the message is sent, we apply FUNCTION to ARGS.
  774.  This is how Rmail arranges to mark messages `answered'."
  775.   (interactive "P")
  776. ;;; This is commented out because I found it was confusing in practice.
  777. ;;; It is easy enough to rename *mail* by hand with rename-buffer
  778. ;;; if you want to have multiple mail buffers.
  779. ;;; And then you can control which messages to save. --rms.
  780. ;;;  (let ((index 1)
  781. ;;;    buffer)
  782. ;;;    ;; If requested, look for a mail buffer that is modified and go to it.
  783. ;;;    (if noerase
  784. ;;;    (progn
  785. ;;;      (while (and (setq buffer
  786. ;;;                (get-buffer (if (= 1 index) "*mail*"
  787. ;;;                      (format "*mail*<%d>" index))))
  788. ;;;              (not (buffer-modified-p buffer)))
  789. ;;;        (setq index (1+ index)))
  790. ;;;      (if buffer (switch-to-buffer buffer)
  791. ;;;        ;; If none exists, start a new message.
  792. ;;;        ;; This will never re-use an existing unmodified mail buffer
  793. ;;;        ;; (since index is not 1 anymore).  Perhaps it should.
  794. ;;;        (setq noerase nil))))
  795. ;;;    ;; Unless we found a modified message and are happy, start a new message.
  796. ;;;    (if (not noerase)
  797. ;;;    (progn
  798. ;;;      ;; Look for existing unmodified mail buffer.
  799. ;;;      (while (and (setq buffer
  800. ;;;                (get-buffer (if (= 1 index) "*mail*"
  801. ;;;                      (format "*mail*<%d>" index))))
  802. ;;;              (buffer-modified-p buffer))
  803. ;;;        (setq index (1+ index)))
  804. ;;;      ;; If none, make a new one.
  805. ;;;      (or buffer
  806. ;;;          (setq buffer (generate-new-buffer "*mail*")))
  807. ;;;      ;; Go there and initialize it.
  808. ;;;      (switch-to-buffer buffer)
  809. ;;;      (erase-buffer)
  810. ;;;          (setq default-directory (expand-file-name "~/"))
  811. ;;;          (auto-save-mode auto-save-default)
  812. ;;;          (mail-mode)
  813. ;;;          (mail-setup to subject in-reply-to cc replybuffer actions)
  814. ;;;      (if (and buffer-auto-save-file-name
  815. ;;;           (file-exists-p buffer-auto-save-file-name))
  816. ;;;          (message "Auto save file for draft message exists; consider M-x mail-recover"))
  817. ;;;          t))
  818.   (switch-to-buffer "*mail*")
  819.   (setq default-directory (expand-file-name "~/"))
  820.   (auto-save-mode auto-save-default)
  821.   (mail-mode)
  822.   (let (initialized)
  823.     (and (not noerase)
  824.      (or (not (buffer-modified-p))
  825.          (y-or-n-p "Unsent message being composed; erase it? "))
  826.      (progn (erase-buffer)
  827.         (mail-setup to subject in-reply-to cc replybuffer actions)
  828.         (setq initialized t)))
  829.     (if (and buffer-auto-save-file-name
  830.          (file-exists-p buffer-auto-save-file-name))
  831.     (message "Auto save file for draft message exists; consider M-x mail-recover"))
  832.     initialized))
  833.  
  834. (defun mail-recover ()
  835.   "Reread contents of current buffer from its last auto-save file."
  836.   (interactive)
  837.   (let ((file-name (make-auto-save-file-name)))
  838.     (cond ((save-window-excursion
  839.          (if (not (eq system-type 'vax-vms))
  840.          (with-output-to-temp-buffer "*Directory*"
  841.            (buffer-disable-undo standard-output)
  842.            (call-process "ls" nil standard-output nil "-l" file-name)))
  843.          (yes-or-no-p (format "Recover auto save file %s? " file-name)))
  844.        (let ((buffer-read-only nil))
  845.          (erase-buffer)
  846.          (insert-file-contents file-name nil)))
  847.       (t (error "mail-recover cancelled.")))))
  848.  
  849. ;;;###autoload
  850. (defun mail-other-window (&optional noerase to subject in-reply-to cc replybuffer sendactions)
  851.   "Like `mail' command, but display mail buffer in another window."
  852.   (interactive "P")
  853.   (let ((pop-up-windows t))
  854.     (pop-to-buffer "*mail*"))
  855.   (mail noerase to subject in-reply-to cc replybuffer sendactions))
  856.  
  857. ;;;###autoload
  858. (defun mail-other-frame (&optional noerase to subject in-reply-to cc replybuffer sendactions)
  859.   "Like `mail' command, but display mail buffer in another frame."
  860.   (interactive "P")
  861.   (let ((pop-up-frames t))
  862.     (pop-to-buffer "*mail*"))
  863.   (mail noerase to subject in-reply-to cc replybuffer sendactions))
  864.  
  865. ;;; Do not execute these when sendmail.el is loaded,
  866. ;;; only in loaddefs.el.
  867. ;;;###autoload (define-key ctl-x-map "m" 'mail)
  868. ;;;###autoload (define-key ctl-x-4-map "m" 'mail-other-window)
  869. ;;;###autoload (define-key ctl-x-5-map "m" 'mail-other-frame)
  870.  
  871. ;;; Do not add anything but external entries on this page.
  872.  
  873. (provide 'sendmail)
  874.  
  875. ;;; sendmail.el ends here
  876.