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