home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / lucid / lemacs-19.6 / lisp / rmail / rmail.el < prev    next >
Encoding:
Text File  |  1993-02-14  |  52.9 KB  |  1,492 lines

  1. ;; "RMAIL" mail reader for Emacs.
  2. ;; Copyright (C) 1985-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. ;; Souped up by shane@mit-ajax based on ideas of rlk@athena.mit.edu
  22. ;;   New features include attribute and keyword support, message
  23. ;;   selection by dispatch table, summary by attributes and keywords,
  24. ;;   expunging by dispatch table, sticky options for file commands.
  25.  
  26. (require 'mail-utils)
  27. (provide 'rmail)
  28.  
  29. ; these variables now declared in loaddefs or paths.el
  30. ;(defvar rmail-spool-directory "/usr/spool/mail/"
  31. ;  "This is the name of the directory used by the system mailer for\n\
  32. ;delivering new mail.  It's name should end with a slash.")
  33. ;(defvar rmail-dont-reply-to-names
  34. ;  nil
  35. ;  "*A regexp specifying names to prune of reply to messages.
  36. ;nil means dont reply to yourself.")
  37. ;(defvar rmail-ignored-headers
  38. ;   "^via:\\|^mail-from:\\|^origin:\\|^status:\\|^received:\\|^message-id:\\|^summary-line:"
  39. ;   "*Gubbish headers one would rather not see.")
  40. ;(defvar rmail-file-name
  41. ;  (expand-file-name "~/RMAIL")
  42. ;  "")
  43. ;
  44. ;(defvar rmail-delete-after-output nil
  45. ;  "*Non-nil means automatically delete a message that is copied to a file.")
  46. ;
  47. ;(defvar rmail-primary-inbox-list 
  48. ;  '("/usr/spool/mail/$USER" "~/mbox")
  49. ; "")
  50.  
  51. ;; these may be altered by site-init.el to match the format of mmdf files
  52. ;;  delimitation used on a given host (delim1 and delim2 from the config
  53. ;;  files)
  54.  
  55. (defvar mmdf-delim1 "^\001\001\001\001\n"
  56.   "Regexp marking the start of an mmdf message")
  57. (defvar mmdf-delim2 "^\001\001\001\001\n"
  58.   "Regexp marking the end of an mmdf message")
  59.  
  60. (defvar rmail-message-filter nil
  61.   "If non nil, is a filter function for new headers in RMAIL.
  62. Called with region narrowed to unformatted header.")
  63.  
  64. (defvar rmail-mode-map nil)
  65.  
  66. (defvar rmail-inbox-list nil)
  67. (defvar rmail-keywords nil)
  68.  
  69. ;; Message counters and markers.  Deleted flags.
  70.  
  71. (defvar rmail-current-message nil)
  72. (defvar rmail-total-messages nil)
  73. (defvar rmail-message-vector nil)
  74. (defvar rmail-deleted-vector nil)
  75.  
  76. ;; These are used by autoloaded rmail-summary.
  77.  
  78. (defvar rmail-summary-buffer nil)
  79. (defvar rmail-summary-vector nil)
  80.  
  81. ;; `Sticky' default variables.
  82.  
  83. ;; Last individual label specified to a or k.
  84. (defvar rmail-last-label nil)
  85. ;; Last set of labels specified to C-M-n or C-M-p or C-M-l.
  86. (defvar rmail-last-multi-labels nil)
  87. (defvar rmail-last-file nil)
  88. (defvar rmail-last-rmail-file nil)
  89.  
  90. ;;;; *** Rmail Mode ***
  91.  
  92. (defun rmail (&optional file-name-arg)
  93.   "Read and edit incoming mail.
  94. Moves messages into file named by  rmail-file-name  (a babyl format file)
  95.  and edits that file in RMAIL Mode.
  96. Type \\[describe-mode] once editing that file, for a list of RMAIL commands.
  97.  
  98. May be called with filename as argument;
  99. then performs rmail editing on that file,
  100. but does not copy any new mail into the file."
  101.   (interactive (if current-prefix-arg
  102.            (list (read-file-name "Run rmail on RMAIL file: "
  103.                      nil nil t))))
  104.   (if (string-match "Lucid" emacs-version)
  105.       (require 'rmail-lucid))
  106.   (or rmail-last-file
  107.       (setq rmail-last-file (expand-file-name "~/xmail")))
  108.   (or rmail-last-rmail-file
  109.       (setq rmail-last-rmail-file (expand-file-name "~/XMAIL")))
  110.   (let* ((file-name (expand-file-name (or file-name-arg rmail-file-name)))
  111.      (existed (get-file-buffer file-name)))
  112.     ;; Like find-file, but in the case where a buffer existed
  113.     ;; and the file was reverted, recompute the message-data.
  114.     (if (and existed (not (verify-visited-file-modtime existed)))
  115.     (progn
  116.       ;; Don't be confused by apparent local-variables spec
  117.       ;; in the last message in the RMAIL file.
  118.       (let ((enable-local-variables nil))
  119.         (find-file file-name))
  120.       (if (verify-visited-file-modtime existed)
  121.           (progn (rmail-forget-messages)
  122.              (rmail-set-message-counters))))
  123.       (let ((enable-local-variables nil))
  124.     (find-file file-name)))
  125.     (if (and existed (> (buffer-size) 0))
  126.     ;; Buffer not new and not empty; ensure in proper mode, but that's all.
  127.     (or (eq major-mode 'rmail-mode)
  128.         (rmail-mode-2))
  129.       (rmail-mode-2)
  130.       ;; Provide default set of inboxes for primary mail file ~/RMAIL.
  131.       (and (null rmail-inbox-list)
  132.        (null file-name-arg)
  133.        (setq rmail-inbox-list
  134.          (or rmail-primary-inbox-list
  135.              (list "~/mbox"
  136.                (or (getenv "MAIL")
  137.                    (concat rmail-spool-directory
  138.                        (user-original-login-name)))))))
  139.       ;; Convert all or part to Babyl file if possible.
  140.       (rmail-convert-file)
  141.       (goto-char (point-max))
  142.       (if (null rmail-inbox-list)
  143.       (progn
  144.         (rmail-set-message-counters)
  145.         (rmail-show-message))))
  146.     (rmail-get-new-mail)))
  147.  
  148. ;; Given the value of MAILPATH, return a list of inbox file names.
  149. ;; This is turned off because it is not clear that the user wants
  150. ;; all these inboxes to feed into the primary rmail file.
  151. ; (defun rmail-convert-mailpath (string)
  152. ;   (let (idx list)
  153. ;     (while (setq idx (string-match "[%:]" string))
  154. ;       (let ((this (substring string 0 idx)))
  155. ;     (setq string (substring string (1+ idx)))
  156. ;     (setq list (cons (if (string-match "%" this)
  157. ;                  (substring this 0 (string-match "%" this))
  158. ;                this)
  159. ;              list))))
  160. ;     list))
  161.  
  162. (defun rmail-convert-file ()
  163.   (let (convert)
  164.     (widen)
  165.     (goto-char (point-min))
  166.     ;; If file doesn't start like a Babyl file,
  167.     ;; convert it to one, by adding a header and converting each message.
  168.     (cond ((looking-at "BABYL OPTIONS:"))
  169.       ((looking-at "Version: 5\n")
  170.        ;; Losing babyl file made by old version of Rmail.
  171.        ;; Just fix the babyl file header; don't make a new one,
  172.        ;; so we don't lose the Labels: file attribute, etc.
  173.        (let ((buffer-read-only nil))
  174.          (insert "BABYL OPTIONS:\n")))
  175.       (t
  176.        (setq convert t)
  177.        (rmail-insert-rmail-file-header)))
  178.     ;; If file was not a Babyl file or if there are
  179.     ;; Unix format messages added at the end,
  180.     ;; convert file as necessary.
  181.     (if (or convert
  182.         (progn (goto-char (point-max))
  183.            (search-backward "\^_")
  184.            (forward-char 1)
  185.            (looking-at "\n*From ")))
  186.     (let ((buffer-read-only nil))
  187.       (message "Converting to Babyl format...")
  188.       (narrow-to-region (if (looking-at "\n") (1+ (point)) (point))
  189.                 (point-max))
  190.       (rmail-convert-to-babyl-format)
  191.       (message "Converting to Babyl format...done")))))
  192.  
  193. (defun rmail-insert-rmail-file-header ()
  194.   (let ((buffer-read-only nil))
  195.     (insert "BABYL OPTIONS:
  196. Version: 5
  197. Labels:
  198. Note:   This is the header of an rmail file.
  199. Note:   If you are seeing it in rmail,
  200. Note:    it means the file has no messages in it.\n\^_")))
  201.  
  202. (if rmail-mode-map
  203.     nil
  204.   (setq rmail-mode-map (make-keymap))
  205.   (suppress-keymap rmail-mode-map)
  206.   (define-key rmail-mode-map "." 'rmail-beginning-of-message)
  207.   (define-key rmail-mode-map " " 'scroll-up)
  208.   (define-key rmail-mode-map "\177" 'scroll-down)
  209.   (define-key rmail-mode-map "n" 'rmail-next-undeleted-message)
  210.   (define-key rmail-mode-map "p" 'rmail-previous-undeleted-message)
  211.   (define-key rmail-mode-map "\en" 'rmail-next-message)
  212.   (define-key rmail-mode-map "\ep" 'rmail-previous-message)
  213.   (define-key rmail-mode-map "\e\C-n" 'rmail-next-labeled-message)
  214.   (define-key rmail-mode-map "\e\C-p" 'rmail-previous-labeled-message)
  215.   (define-key rmail-mode-map "a" 'rmail-add-label)
  216.   (define-key rmail-mode-map "k" 'rmail-kill-label)
  217.   (define-key rmail-mode-map "d" 'rmail-delete-forward)
  218.   (define-key rmail-mode-map "u" 'rmail-undelete-previous-message)
  219.   (define-key rmail-mode-map "x" 'rmail-expunge)
  220.   (define-key rmail-mode-map "e" 'rmail-expunge)
  221.   (define-key rmail-mode-map "s" 'rmail-expunge-and-save)
  222.   (define-key rmail-mode-map "g" 'rmail-get-new-mail)
  223.   (define-key rmail-mode-map "h" 'rmail-summary)
  224.   (define-key rmail-mode-map "\e\C-h" 'rmail-summary)
  225.   (define-key rmail-mode-map "l" 'rmail-summary-by-labels)
  226.   (define-key rmail-mode-map "\e\C-l" 'rmail-summary-by-labels)
  227.   (define-key rmail-mode-map "\e\C-r" 'rmail-summary-by-recipients)
  228.   (define-key rmail-mode-map "\e\C-s" 'rmail-summary-by-regexp)
  229.   (define-key rmail-mode-map "t" 'rmail-toggle-header)
  230.   (define-key rmail-mode-map "m" 'rmail-mail)
  231.   (define-key rmail-mode-map "r" 'rmail-reply)
  232.   (define-key rmail-mode-map "\e\C-m" 'rmail-retry-failure)
  233.   (define-key rmail-mode-map "c" 'rmail-continue)
  234.   (define-key rmail-mode-map "f" 'rmail-forward)
  235.   (define-key rmail-mode-map "\es" 'rmail-search)
  236.   (define-key rmail-mode-map "j" 'rmail-show-message)
  237.   (define-key rmail-mode-map "o" 'rmail-output-to-rmail-file)
  238.   (define-key rmail-mode-map "\C-o" 'rmail-output)
  239.   (define-key rmail-mode-map "i" 'rmail-input)
  240.   (define-key rmail-mode-map "q" 'rmail-quit)
  241.   (define-key rmail-mode-map ">" 'rmail-last-message)
  242.   (define-key rmail-mode-map "?" 'describe-mode)
  243.   (define-key rmail-mode-map "w" 'rmail-edit-current-message)
  244.   (define-key rmail-mode-map "\C-d" 'rmail-delete-backward))
  245.  
  246. ;; Rmail mode is suitable only for specially formatted data.
  247. (put 'rmail-mode 'mode-class 'special)
  248.  
  249. (defun rmail-mode ()
  250.   "Rmail Mode is used by \\[rmail] for editing Rmail files.
  251. All normal editing commands are turned off.
  252. Instead, these commands are available:
  253.  
  254. .    Move point to front of this message (same as \\[beginning-of-buffer]).
  255. SPC    Scroll to next screen of this message.
  256. DEL    Scroll to previous screen of this message.
  257. n    Move to Next non-deleted message.
  258. p    Move to Previous non-deleted message.
  259. M-n    Move to Next message whether deleted or not.
  260. M-p    Move to Previous message whether deleted or not.
  261. >    Move to the last message in Rmail file.
  262. j    Jump to message specified by numeric position in file.
  263. M-s    Search for string and show message it is found in.
  264. d    Delete this message, move to next nondeleted.
  265. C-d    Delete this message, move to previous nondeleted.
  266. u    Undelete message.  Tries current message, then earlier messages
  267.     till a deleted message is found.
  268. x    Expunge deleted messages.
  269. s    Expunge and save the file.
  270. q       Quit Rmail: expunge, save, then switch to another buffer.
  271. C-x C-s Save without expunging.
  272. g    Move new mail from system spool directory or mbox into this file.
  273. m    Mail a message (same as \\[mail-other-window]).
  274. c    Continue composing outgoing message started before.
  275. r    Reply to this message.  Like m but initializes some fields.
  276. C-M-m   Send this message again.  Used on a mailer failure message.
  277. f    Forward this message to another user.
  278. o       Output this message to an Rmail file (append it).
  279. C-o    Output this message to a Unix-format mail file (append it).
  280. i    Input Rmail file.  Run Rmail on that file.
  281. a    Add label to message.  It will be displayed in the mode line.
  282. k    Kill label.  Remove a label from current message.
  283. C-M-n   Move to Next message with specified label
  284.           (label defaults to last one specified).
  285.           Standard labels: filed, unseen, answered, forwarded, deleted.
  286.           Any other label is present only if you add it with `a'.
  287. C-M-p   Move to Previous message with specified label
  288. C-M-h    Show headers buffer, with a one line summary of each message.
  289. C-M-l    Like h only just messages with particular label(s) are summarized.
  290. C-M-r   Like h only just messages with particular recipient(s) are summarized.
  291. t    Toggle header, show Rmail header if unformatted or vice versa.
  292. w    Edit the current message.  C-c C-c to return to Rmail."
  293.   (interactive)
  294.   (rmail-mode-2)
  295.   (rmail-set-message-counters)
  296.   (rmail-show-message))
  297.  
  298. (defun rmail-mode-2 ()
  299.   (kill-all-local-variables)
  300.   (rmail-mode-1)
  301.   (rmail-variables)
  302.   (run-hooks 'rmail-mode-hook))
  303.  
  304. (defun rmail-mode-1 ()
  305.   (setq major-mode 'rmail-mode)
  306.   (setq mode-name "RMAIL")
  307.   (setq buffer-read-only t)
  308.   ;; No need to auto save RMAIL files.
  309.   (setq buffer-auto-save-file-name nil)
  310.   (if (boundp 'mode-line-modified)
  311.       (setq mode-line-modified "--- ")
  312.     (setq mode-line-format
  313.       (cons "--- " (cdr (default-value 'mode-line-format)))))
  314.   (use-local-map rmail-mode-map)
  315.   (set-syntax-table text-mode-syntax-table)
  316.   (setq local-abbrev-table text-mode-abbrev-table))
  317.  
  318. (defun rmail-variables ()
  319.   (make-local-variable 'revert-buffer-function)
  320.   (setq revert-buffer-function 'rmail-revert)
  321.   (make-local-variable 'rmail-last-label)
  322.   (make-local-variable 'rmail-deleted-vector)
  323.   (make-local-variable 'rmail-summary-buffer)
  324.   (make-local-variable 'rmail-summary-vector)
  325.   (make-local-variable 'rmail-current-message)
  326.   (make-local-variable 'rmail-total-messages)
  327.   (make-local-variable 'require-final-newline)
  328.   (setq require-final-newline nil)
  329.   (make-local-variable 'version-control)
  330.   (setq version-control 'never)
  331.   (make-local-variable 'file-precious-flag)
  332.   (setq file-precious-flag t)
  333.   (make-local-variable 'rmail-message-vector)
  334.   (make-local-variable 'rmail-last-file)
  335.   (make-local-variable 'rmail-inbox-list)
  336.   (setq rmail-inbox-list (rmail-parse-file-inboxes))
  337.   (make-local-variable 'rmail-keywords)
  338.   ;; this gets generated as needed
  339.   (setq rmail-keywords nil)
  340. ;  (make-local-variable 'save-buffers-skip)
  341. ;  (setq save-buffers-skip t)
  342.   )
  343.  
  344. ;; Handle M-x revert-buffer done in an rmail-mode buffer.
  345. (defun rmail-revert (arg noconfirm)
  346.   (let (revert-buffer-function)
  347.     ;; Call our caller again, but this time it does the default thing.
  348.     (if (revert-buffer arg noconfirm)
  349.     ;; If the user said "yes", and we changed something,
  350.     ;; reparse the messages.
  351.     (progn
  352.       (rmail-convert-file)
  353.       (goto-char (point-max))
  354.       (rmail-set-message-counters)
  355.       (rmail-show-message)))))
  356.  
  357. ;; Return a list of files from this buffer's Mail: option.
  358. ;; Does not assume that messages have been parsed.
  359. ;; Just returns nil if buffer does not look like Babyl format.
  360. (defun rmail-parse-file-inboxes ()
  361.   (save-excursion
  362.     (save-restriction
  363.       (widen)
  364.       (goto-char 1)
  365.       (cond ((looking-at "Babyl options:")
  366.          (search-forward "\n\^_" nil 'move)
  367.          (narrow-to-region 1 (point))
  368.          (goto-char 1)
  369.          (if (search-forward "\nMail:" nil t)
  370.          (progn
  371.            (narrow-to-region (point) (progn (end-of-line) (point)))
  372.            (goto-char (point-min))
  373.            (mail-parse-comma-list))))))))
  374.  
  375. (defun rmail-expunge-and-save ()
  376.   "Expunge and save RMAIL file."
  377.   (interactive)
  378.   (rmail-expunge)
  379.   (save-buffer))
  380.  
  381. (defun rmail-quit ()
  382.   "Quit out of RMAIL."
  383.   (interactive)
  384.   (rmail-expunge-and-save)
  385.   ;; Don't switch to the summary buffer even if it was recently visible.
  386.   (if rmail-summary-buffer
  387.       (bury-buffer rmail-summary-buffer))
  388.   (let ((obuf (current-buffer)))
  389.     (switch-to-buffer (other-buffer))
  390.     (bury-buffer obuf)))
  391.  
  392. (defun rmail-input (filename)
  393.   "Run RMAIL on file FILENAME."
  394.   (interactive "FRun rmail on RMAIL file: ")
  395.   (rmail filename))
  396.  
  397.  
  398. ;;;; *** Rmail input ***
  399.  
  400. ;; RLK feature not added in this version:
  401. ;; argument specifies inbox file or files in various ways.
  402.  
  403. (defun rmail-get-new-mail (&optional file-name)
  404.   "Move any new mail from this RMAIL file's inbox files.
  405. The inbox files can be specified with the file's Mail: option.
  406. The variable rmail-primary-inbox-list specifies the inboxes for
  407. your primary RMAIL file if it has no Mail: option.
  408. These are normally your ~/mbox and your /usr/spool/mail/$USER.
  409.  
  410. You can also specify the file to get new mail from.  In this
  411. case, the file of new mail is not changed or deleted.
  412. Noninteractively, you can pass the inbox file name as an argument.
  413. Interactively, a prefix argument causes us to read a file name
  414. and use that file as the inbox."
  415.   (interactive
  416.    (list (if current-prefix-arg
  417.          (read-file-name "Get new mail from file: "))))
  418.   (or (verify-visited-file-modtime (current-buffer))
  419.       (progn
  420.     (find-file (buffer-file-name))
  421.     (if (verify-visited-file-modtime (current-buffer))
  422.         (rmail-forget-messages))))
  423.   (rmail-maybe-set-message-counters)
  424.   (widen)
  425.   (unwind-protect
  426.       (let ((opoint (point))
  427.         (new-messages 0)
  428.         (delete-files ())
  429.         ;; If buffer has not changed yet, and has not been saved yet,
  430.         ;; don't replace the old backup file now.
  431.         (make-backup-files (and make-backup-files (buffer-modified-p)))
  432.         (buffer-read-only nil))
  433.     (goto-char (point-max))
  434.     (skip-chars-backward " \t\n")        ; just in case of brain damage
  435.     (delete-region (point) (point-max)) ; caused by require-final-newline
  436.     (save-excursion
  437.       (save-restriction
  438.         (narrow-to-region (point) (point))
  439.         ;; Read in the contents of the inbox files,
  440.         ;; renaming them as necessary,
  441.         ;; and adding to the list of files to delete eventually.
  442.         (if file-name
  443.         (rmail-insert-inbox-text (list file-name) nil)
  444.           (setq delete-files (rmail-insert-inbox-text rmail-inbox-list t)))
  445.         ;; Scan the new text and convert each message to babyl format.
  446.         (goto-char (point-min))
  447.         (save-excursion
  448.           (setq new-messages (rmail-convert-to-babyl-format)))
  449.         (or (zerop new-messages)
  450.         (let (success)
  451.           (widen)
  452.           (search-backward "\n\^_")
  453.           (narrow-to-region (point) (point-max))
  454.           (goto-char (1+ (point-min)))
  455.           (rmail-count-new-messages)
  456.           (save-buffer)))
  457.         ;; Delete the old files, now that babyl file is saved.
  458.         (while delete-files
  459.           (condition-case ()
  460.           (delete-file (car delete-files))
  461.         (file-error nil))
  462.           (setq delete-files (cdr delete-files)))))
  463.     (if (= new-messages 0)
  464.         (progn (goto-char opoint)
  465.            (if (or file-name rmail-inbox-list)
  466.                (message "(No new mail has arrived)")))
  467.       (message "%d new message%s read"
  468.            new-messages (if (= 1 new-messages) "" "s"))
  469.       (and (boundp 'display-time-string)
  470.            display-time-string
  471.            (string-match " Mail" display-time-string)
  472.            (setq display-time-string
  473.              (concat
  474.               (substring display-time-string 0 (match-beginning 0))
  475.               (substring display-time-string (match-end 0))))
  476.            (force-mode-line-update 'all))))
  477.     ;; Don't leave the buffer screwed up if we get a disk-full error.
  478.     (rmail-show-message)))
  479.  
  480. (defun rmail-insert-inbox-text (files renamep)
  481.   (let (file tofile delete-files movemail)
  482.     (while files
  483.       (setq file (expand-file-name (substitute-in-file-name (car files)))
  484.         ;;>> un*x specific <<
  485.         tofile (concat file "~"))
  486.       ;; If getting from mail spool directory,
  487.       ;; use movemail to move rather than just renaming,
  488.       ;; so as to interlock with the mailer.
  489.       (setq movemail (equal (file-name-directory file) rmail-spool-directory))
  490.       (if movemail
  491.       (progn
  492.         (setq tofile (expand-file-name
  493.               ;; Generate name to move to from inbox name,
  494.               ;; in case of multiple inboxes that need moving.
  495.               (concat ".newmail-" (file-name-nondirectory file))
  496.               (file-name-directory
  497.                (expand-file-name rmail-file-name))))
  498.         ;; On some systems, /usr/spool/mail/foo is a directory
  499.         ;; and the actual inbox is /usr/spool/mail/foo/foo.
  500.         (if (file-directory-p file)
  501.         (setq file (expand-file-name (user-original-login-name)
  502.                          file)))))
  503.       (if (or (file-exists-p tofile) (file-exists-p file))
  504.       (message "Getting mail from %s..." file))
  505.       ;; Set TOFILE if have not already done so, and
  506.       ;; rename or copy the file FILE to TOFILE if and as appropriate.
  507.       (cond ((not renamep)
  508.          (setq tofile file))
  509.         ((or (file-exists-p tofile) (not (file-exists-p file)))
  510.          nil)
  511.         ((not movemail)
  512.          (rename-file file tofile nil))
  513.         (t
  514.          (let ((errors nil))
  515.            (unwind-protect
  516.            (save-excursion
  517.              (setq errors (generate-new-buffer " *rmail loss*"))
  518.              (buffer-disable-undo errors)
  519.              (call-process
  520.                (expand-file-name "movemail" exec-directory)
  521.                nil errors nil file tofile)
  522.              (if (not (buffer-modified-p errors))
  523.              ;; No output => movemail won
  524.              nil
  525.                (set-buffer errors)
  526.                (subst-char-in-region (point-min) (point-max)
  527.                          ?\n ?\  )
  528.                (goto-char (point-max))
  529.                (skip-chars-backward " \t")
  530.                (delete-region (point) (point-max))
  531.                (goto-char (point-min))
  532.                (if (looking-at "movemail: ")
  533.                (delete-region (point-min) (match-end 0)))
  534.                (signal 'file-error
  535.                    (list "movemail"
  536.                      (buffer-substring (point-min)
  537.                                (point-max))
  538.                      ;file tofile
  539.                      ))))
  540.          (if errors (kill-buffer errors))))))
  541.       ;; At this point, TOFILE contains the name to read:
  542.       ;; Either the alternate name (if we renamed)
  543.       ;; or the actual inbox (if not renaming).
  544.       (if (file-exists-p tofile)
  545.       (let (size)
  546.         (goto-char (point-max))
  547.         (setq size (nth 1 (insert-file-contents tofile)))
  548.         (goto-char (point-max))
  549.         (or (= (preceding-char) ?\n)
  550.         (zerop size)
  551.         (insert ?\n))
  552.         (setq delete-files (cons tofile delete-files))))
  553.       (message "")
  554.       (setq files (cdr files)))
  555.     delete-files))
  556.  
  557. ;; the  rmail-break-forwarded-messages  feature is not implemented
  558. (defun rmail-convert-to-babyl-format ()
  559.   (let ((count 0) start
  560.     (case-fold-search nil))
  561.     (goto-char (point-min))
  562.     (save-restriction
  563.       (while (not (eobp))
  564.     (cond ((looking-at "Babyl Options:");Babyl header
  565.            (search-forward "\n\^_")
  566.            (delete-region (point-min) (point)))
  567.           ;; Babyl format message
  568.           ((looking-at "\^L")
  569.            (or (search-forward "\n\^_" nil t)
  570.            (progn
  571.              (message "Invalid Babyl format in inbox!")
  572.              (sit-for 1)
  573.              (goto-char (point-max))))
  574.            (setq count (1+ count))
  575.            (skip-chars-forward " \t\n")
  576.            (narrow-to-region (point) (point-max)))
  577.           ;;*** MMDF format
  578.           ((let ((case-fold-search t))
  579.          (looking-at mmdf-delim1))
  580.            (let ((case-fold-search t))
  581.          (replace-match "\^L\n0, unseen,,\n*** EOOH ***\n")
  582.          (setq start (point))
  583.          (re-search-forward mmdf-delim2 nil t)
  584.          (replace-match "\^_"))
  585.            (save-excursion
  586.          (save-restriction
  587.            (narrow-to-region start (1- (point)))
  588.            (goto-char (point-min))
  589.            (while (search-forward "\n\^_" nil t); single char "\^_"
  590.              (replace-match "\n^_")))); 2 chars: "^" and "_"
  591.            (narrow-to-region (point) (point-max))
  592.            (setq count (1+ count)))
  593.           ;;*** Mail format
  594.           ((looking-at "^From ")
  595.            (setq start (point))
  596.            (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
  597.            (rmail-nuke-pinhead-header)
  598.            (if (re-search-forward
  599.             (concat "^[\^_]?\\("
  600.                 "From [^ \n]*\\(\\|\".*\"[^ \n]*\\)  ?[^ \n]* [^ \n]* *"
  601.                 "[0-9]* [0-9:]*\\( ?[A-Z]?[A-Z][A-Z]T\\| ?[-+]?[0-9][0-9][0-9][0-9]\\|\\) " ; EDT, -0500
  602.                 "19[0-9]* *$\\|"
  603.                 mmdf-delim1 "\\|"
  604.                 "^Babyl Options:\\|"
  605.                 "\^L\n[01],\\)") nil t)
  606.            (goto-char (match-beginning 1))
  607.          (goto-char (point-max)))
  608.            (setq count (1+ count))
  609.            (save-excursion
  610.          (save-restriction
  611.            (narrow-to-region start (point))
  612.            (goto-char (point-min))
  613.            (while (search-forward "\n\^_" nil t); single char
  614.              (replace-match "\n^_")))); 2 chars: "^" and "_"
  615.            (insert ?\^_)
  616.            (narrow-to-region (point) (point-max)))
  617.           ;;
  618.           ;;This is a kludge, in case we're wrong about mmdf not
  619.           ;;allowing anything in between.  If it loses, we'll have
  620.           ;;to look for something else
  621.           (t (error "Cannot convert to babyl format")))))
  622.     count))
  623.  
  624. (defun rmail-nuke-pinhead-header ()
  625.   (save-excursion
  626.     (save-restriction
  627.       (let ((start (point))
  628.           (end (progn
  629.            (condition-case ()
  630.                (search-forward "\n\n")
  631.              (error
  632.               (goto-char (point-max))
  633.               (insert "\n\n")))
  634.            (point)))
  635.         has-from has-date)
  636.     (narrow-to-region start end)
  637.     (let ((case-fold-search t))
  638.       (goto-char start)
  639.       (setq has-from (search-forward "\nFrom:" nil t))
  640.       (goto-char start)
  641.       (setq has-date (and (search-forward "\nDate:" nil t) (point)))
  642.       (goto-char start))
  643.     (let ((case-fold-search nil))
  644.       (if (re-search-forward
  645.            "^From \\([^ ]*\\(\\|\".*\"[^ ]*\\)\\)  ?\\([^ ]*\\) \\([^ ]*\\) *\\([0-9]*\\) \\([0-9:]*\\)\\( ?[A-Z]?[A-Z][A-Z]T\\| ?[-+]?[0-9][0-9][0-9][0-9]\\|\\) 19\\([0-9]*\\) *\n" nil t)
  646.           (replace-match
  647.         (concat
  648.           ;; Keep and reformat the date if we don't
  649.           ;;  have a Date: field.
  650.           (if has-date
  651.               ""
  652.             ;; If no time zone specified, assume est.
  653.             (if (= (match-beginning 7) (match-end 7))
  654.             "Date: \\3, \\5 \\4 \\8 \\6 EST\n"
  655.             "Date: \\3, \\5 \\4 \\8 \\6\\7\n"))
  656.           ;; Keep and reformat the sender if we don't
  657.           ;; have a From: field.
  658.           (if has-from
  659.               ""
  660.             "From: \\1\n")))))))))
  661.  
  662. ;;;; *** Rmail Message Formatting and Header Manipulation ***
  663.  
  664. (defun rmail-reformat-message (beg end)
  665.   (goto-char beg)
  666.   (forward-line 1)
  667.   (if (/= (following-char) ?0)
  668.       (error "Bad format in RMAIL file."))
  669.   (let ((buffer-read-only nil)
  670.     (delta (- (buffer-size) end)))
  671.     (delete-char 1)
  672.     (insert ?1)
  673.     (forward-line 1)
  674.     (if (looking-at "Summary-line: ")
  675.     (forward-line 1))
  676.     (if (looking-at "\\*\\*\\* EOOH \\*\\*\\*\n")
  677.     (delete-region (point)
  678.                (progn (forward-line 1) (point))))
  679.     (let ((str (buffer-substring (point)
  680.                  (save-excursion (search-forward "\n\n" end 'move)
  681.                          (point)))))
  682.       (insert str "*** EOOH ***\n")
  683.       (narrow-to-region (point) (- (buffer-size) delta)))
  684.     (goto-char (point-min))
  685.     (if rmail-ignored-headers (rmail-clear-headers))
  686.     (if rmail-message-filter (funcall rmail-message-filter))))
  687.  
  688. (defun rmail-clear-headers ()
  689.   (if (search-forward "\n\n" nil t)
  690.       (save-restriction
  691.         (narrow-to-region (point-min) (point))
  692.     (let ((buffer-read-only nil))
  693.       (while (let ((case-fold-search t))
  694.            (goto-char (point-min))
  695.            (re-search-forward rmail-ignored-headers nil t))
  696.         (beginning-of-line)
  697.         (delete-region (point)
  698.                (progn (re-search-forward "\n[^ \t]")
  699.                   (forward-char -1)
  700.                   (point))))))))
  701.  
  702. (defun rmail-toggle-header ()
  703.   "Show original message header if pruned header currently shown, or vice versa."
  704.   (interactive)
  705.   (rmail-maybe-set-message-counters)
  706.   (narrow-to-region (rmail-msgbeg rmail-current-message) (point-max))
  707.   (let ((buffer-read-only nil))
  708.     (goto-char (point-min))
  709.     (forward-line 1)
  710.     (if (= (following-char) ?1)
  711.     (progn (delete-char 1)
  712.            (insert ?0)
  713.            (forward-line 1)
  714.            (if (looking-at "Summary-Line:")
  715.            (forward-line 1))
  716.            (insert "*** EOOH ***\n")
  717.            (forward-char -1)
  718.            (search-forward "\n*** EOOH ***\n")
  719.            (forward-line -1)
  720.            (let ((temp (point)))
  721.          (and (search-forward "\n\n" nil t)
  722.               (delete-region temp (point))))
  723.            (goto-char (point-min))
  724.            (search-forward "\n*** EOOH ***\n")
  725.            (narrow-to-region (point) (point-max)))
  726.       (rmail-reformat-message (point-min) (point-max)))))
  727.  
  728. ;;;; *** Rmail Attributes and Keywords ***
  729.  
  730. ;; Make a string describing current message's attributes and keywords
  731. ;; and set it up as the name of a minor mode
  732. ;; so it will appear in the mode line.
  733. (defun rmail-display-labels ()
  734.   (let ((blurb "") (beg (point-min-marker)) (end (point-max-marker)))
  735.     (save-excursion
  736.       (unwind-protect
  737.       (progn
  738.         (widen)
  739.         (goto-char (rmail-msgbeg rmail-current-message))
  740.         (forward-line 1)
  741.         (if (looking-at "[01],")
  742.         (progn
  743.           (narrow-to-region (point) (progn (end-of-line) (point)))
  744.           ;; Truly valid BABYL format requires a space before each
  745.           ;; attribute or keyword name.  Put them in if missing.
  746.           (let (buffer-read-only)
  747.             (goto-char (point-min))
  748.             (while (search-forward "," nil t)
  749.               (or (looking-at "[ ,]") (eobp)
  750.               (insert " "))))
  751.           (goto-char (point-max))
  752.           (if (search-backward ",," nil 'move)
  753.               (progn
  754.             (if (> (point) (1+ (point-min)))
  755.                 (setq blurb (buffer-substring (+ 1 (point-min)) (point))))
  756.             (if (> (- (point-max) (point)) 2)
  757.                 (setq blurb
  758.                   (concat blurb
  759.                       ";"
  760.                       (buffer-substring (+ (point) 3)
  761.                                 (1- (point-max)))))))))))
  762.     ;; Note: we don't use save-restriction because that does not work right
  763.     ;; if changes are made outside the saved restriction
  764.     ;; before that restriction is restored.
  765.     (narrow-to-region beg end)
  766.     (set-marker beg nil)
  767.     (set-marker end nil)))
  768.     (while (string-match " +," blurb)
  769.       (setq blurb (concat (substring blurb 0 (match-beginning 0)) ","
  770.               (substring blurb (match-end 0)))))
  771.     (while (string-match ", +" blurb)
  772.       (setq blurb (concat (substring blurb 0 (match-beginning 0)) ","
  773.               (substring blurb (match-end 0)))))
  774.     (setq mode-line-process
  775.       (concat " " rmail-current-message "/" rmail-total-messages
  776.           blurb))))
  777.  
  778. ;; Turn an attribute of a message on or off according to STATE.
  779. ;; ATTR is the name of the attribute, as a string.
  780. ;; MSGNUM is message number to change; nil means current message.
  781. (defun rmail-set-attribute (attr state &optional msgnum)
  782.   (let ((omax (point-max-marker))
  783.     (omin (point-min-marker))
  784.     (buffer-read-only nil))
  785.     (or msgnum (setq msgnum rmail-current-message))
  786.     (unwind-protect
  787.     (save-excursion
  788.       (widen)
  789.       (goto-char (+ 3 (rmail-msgbeg msgnum)))
  790.       (let ((curstate (not (null (search-backward (concat ", " attr ",")
  791.                               (prog1 (point) (end-of-line)) t)))))
  792.         (or (eq curstate (not (not state)))
  793.         (if curstate
  794.             (delete-region (point) (1- (match-end 0)))
  795.           (beginning-of-line)
  796.           (forward-char 2)
  797.           (insert " " attr ","))))
  798.       (if (string= attr "deleted")
  799.           (rmail-set-message-deleted-p msgnum state)))
  800.       ;; Note: we don't use save-restriction because that does not work right
  801.       ;; if changes are made outside the saved restriction
  802.       ;; before that restriction is restored.
  803.       (narrow-to-region omin omax)
  804.       (set-marker omin nil)
  805.       (set-marker omax nil)
  806.       (if (= msgnum rmail-current-message)
  807.       (rmail-display-labels)))))
  808.  
  809. ;; Return t if the attributes/keywords line of msg number MSG
  810. ;; contains a match for the regexp LABELS.
  811. (defun rmail-message-labels-p (msg labels)
  812.   (save-excursion
  813.     (save-restriction
  814.       (widen)
  815.       (goto-char (rmail-msgbeg msg))
  816.       (forward-char 3)
  817.       (re-search-backward labels (prog1 (point) (end-of-line)) t))))
  818.  
  819. ;;;; *** Rmail Message Selection And Support ***
  820.  
  821. (defun rmail-msgend (n)
  822.   (marker-position (aref rmail-message-vector (1+ n))))
  823.  
  824. (defun rmail-msgbeg (n)
  825.   (marker-position (aref rmail-message-vector n)))
  826.  
  827. (defun rmail-widen-to-current-msgbeg (function)
  828.   "Call FUNCTION with point at start of internal data of current message.
  829. Assumes that bounds were previously narrowed to display the message in Rmail.
  830. The bounds are widened enough to move point where desired,
  831. then narrowed again afterward.
  832.  
  833. FUNCTION may not change the visible text of the message, but it may
  834. change the invisible header text."
  835.   (save-excursion
  836.     (let ((obeg (- (point-max) (point-min)))) ; jwz: typo
  837.       (unwind-protect
  838.       (progn
  839.         (narrow-to-region (rmail-msgbeg rmail-current-message)
  840.                   (point-max))
  841.         (goto-char (point-min))
  842.         (funcall function))
  843.     ;; Note: we don't use save-restriction because that does not work right
  844.     ;; if changes are made outside the saved restriction
  845.     ;; before that restriction is restored.
  846.     ;; Here we assume that changes made by FUNCTION
  847.     ;; occur before the visible region of the message.
  848.     (narrow-to-region (- (point-max) obeg) (point-max))))))
  849.  
  850. (defun rmail-forget-messages ()
  851.   (unwind-protect
  852.       (if (vectorp rmail-message-vector)
  853.       (let* ((i 0)
  854.          (v rmail-message-vector)
  855.          (n (length v)))
  856.         (while (< i n)
  857.           (move-marker (aref v i)  nil)
  858.           (setq i (1+ i)))))
  859.     (setq rmail-message-vector nil)
  860.     (setq rmail-deleted-vector nil)))
  861.  
  862. (defun rmail-maybe-set-message-counters ()
  863.   (if (not (and rmail-deleted-vector
  864.         rmail-message-vector
  865.         rmail-current-message
  866.         rmail-total-messages))
  867.       (rmail-set-message-counters)))
  868.  
  869. (defun rmail-count-new-messages (&optional nomsg)
  870.   (let* ((case-fold-search nil)
  871.      (total-messages 0)
  872.      (messages-head nil)
  873.      (deleted-head nil))
  874.     (or nomsg (message "Counting new messages..."))
  875.     (goto-char (point-max))
  876.     ;; Put at the end of messages-head
  877.     ;; the entry for message N+1, which marks
  878.     ;; the end of message N.  (N = number of messages).
  879.     (search-backward "\n\^_")
  880.     (forward-char 1)
  881.     (setq messages-head (list (point-marker)))
  882.     (rmail-set-message-counters-counter (point-min))
  883.     (setq rmail-current-message (1+ rmail-total-messages))
  884.     (setq rmail-total-messages
  885.       (+ rmail-total-messages total-messages))
  886.     (setq rmail-message-vector
  887.       (vconcat rmail-message-vector (cdr messages-head)))
  888.     (aset rmail-message-vector
  889.       rmail-current-message (car messages-head))
  890.     (setq rmail-deleted-vector
  891.       (concat rmail-deleted-vector deleted-head))
  892.     (setq rmail-summary-vector
  893.       (vconcat rmail-summary-vector (make-vector total-messages nil)))
  894.     (goto-char (point-min))
  895.     (or nomsg (message "Counting new messages...done (%d)" total-messages))))
  896.  
  897. (defun rmail-set-message-counters ()
  898.   (rmail-forget-messages)
  899.   (save-excursion
  900.     (save-restriction
  901.       (widen)
  902.       (let* ((point-save (point))
  903.          (total-messages 0)
  904.          (messages-after-point)
  905.          (case-fold-search nil)
  906.          (messages-head nil)
  907.          (deleted-head nil))
  908.     (message "Counting messages...")
  909.     (goto-char (point-max))
  910.     ;; Put at the end of messages-head
  911.     ;; the entry for message N+1, which marks
  912.     ;; the end of message N.  (N = number of messages).
  913.     (search-backward "\n\^_")
  914.     (forward-char 1)
  915.     (setq messages-head (list (point-marker)))
  916.     (rmail-set-message-counters-counter (min (point) point-save))
  917.     (setq messages-after-point total-messages)
  918.     (rmail-set-message-counters-counter)
  919.     (setq rmail-total-messages total-messages)
  920.     (setq rmail-current-message
  921.           (min total-messages
  922.            (max 1 (- total-messages messages-after-point))))
  923.     (setq rmail-message-vector
  924.           (apply 'vector (cons (point-min-marker) messages-head))
  925.           rmail-deleted-vector (concat "D" deleted-head)
  926.           rmail-summary-vector (make-vector rmail-total-messages nil))
  927.     (message "Counting messages...done")))))
  928.     
  929. (defun rmail-set-message-counters-counter (&optional stop)
  930.   (while (search-backward "\n\^_\^L\n" stop t)
  931.     (forward-char 1)
  932.     (setq messages-head (cons (point-marker) messages-head))
  933.     (save-excursion
  934.       (setq deleted-head
  935.         (cons (if (search-backward ", deleted,"
  936.                        (prog1 (point)
  937.                      (forward-line 2))
  938.                        t)
  939.               ?D ?\ )
  940.           deleted-head)))
  941.     (if (zerop (% (setq total-messages (1+ total-messages)) 20))
  942.     (message "Counting messages...%d" total-messages))))
  943.  
  944. (defun rmail-beginning-of-message ()
  945.   "Show current message starting from the beginning."
  946.   (interactive)
  947.   (rmail-show-message rmail-current-message))
  948.  
  949. (defun rmail-show-message (&optional n)
  950.   "Show message number N (prefix argument), counting from start of file."
  951.   (interactive "p")
  952.   (rmail-maybe-set-message-counters)
  953.   (widen)
  954.   (if (zerop rmail-total-messages)
  955.       (progn (narrow-to-region (point-min) (1- (point-max)))
  956.          (goto-char (point-min))
  957.          (setq mode-line-process nil))
  958.     (let (blurb)
  959.       (if (not n)
  960.       (setq n rmail-current-message)
  961.     (cond ((<= n 0)
  962.            (setq n 1
  963.              rmail-current-message 1
  964.              blurb "No previous message"))
  965.           ((> n rmail-total-messages)
  966.            (setq n rmail-total-messages
  967.              rmail-current-message rmail-total-messages
  968.              blurb "No following message"))
  969.           (t
  970.            (setq rmail-current-message n))))
  971.       (let ((beg (rmail-msgbeg n))
  972.         (end (rmail-msgend n)))
  973.     (goto-char beg)
  974.     (forward-line 1)
  975.     (if (= (following-char) ?0)
  976.         (progn
  977.           (rmail-reformat-message beg end)
  978.           (rmail-set-attribute "unseen" nil))
  979.       (search-forward "\n*** EOOH ***\n" end t)
  980.       (narrow-to-region (point) end))
  981.     (goto-char (point-min))
  982.     (rmail-display-labels)
  983.     (run-hooks 'rmail-show-message-hook)
  984.     (if blurb
  985.         (message blurb))))))
  986.  
  987. (defun rmail-next-message (n)
  988.   "Show following message whether deleted or not.
  989. With prefix argument N, moves forward N messages,
  990. or backward if N is negative."
  991.   (interactive "p")
  992.   (rmail-maybe-set-message-counters)
  993.   (rmail-show-message (+ rmail-current-message n)))
  994.  
  995. (defun rmail-previous-message (n)
  996.   "Show previous message whether deleted or not.
  997. With prefix argument N, moves backward N messages,
  998. or forward if N is negative."
  999.   (interactive "p")
  1000.   (rmail-next-message (- n)))  
  1001.  
  1002. (defun rmail-next-undeleted-message (n)
  1003.   "Show following non-deleted message.
  1004. With prefix argument N, moves forward N non-deleted messages,
  1005. or backward if N is negative."
  1006.   (interactive "p")
  1007.   (rmail-maybe-set-message-counters)
  1008.   (let ((lastwin rmail-current-message)
  1009.     (current rmail-current-message))
  1010.     (while (and (> n 0) (< current rmail-total-messages))
  1011.       (setq current (1+ current))
  1012.       (if (not (rmail-message-deleted-p current))
  1013.       (setq lastwin current n (1- n))))
  1014.     (while (and (< n 0) (> current 1))
  1015.       (setq current (1- current))
  1016.       (if (not (rmail-message-deleted-p current))
  1017.       (setq lastwin current n (1+ n))))
  1018.     (if (/= lastwin rmail-current-message)
  1019.     (rmail-show-message lastwin))
  1020.     (if (< n 0)
  1021.     (error "No previous nondeleted message"))
  1022.     (if (> n 0)
  1023.     (error "No following nondeleted message"))))
  1024.  
  1025. (defun rmail-previous-undeleted-message (n)
  1026.   "Show previous non-deleted message.
  1027. With prefix argument N, moves backward N non-deleted messages,
  1028. or forward if N is negative."
  1029.   (interactive "p")
  1030.   (rmail-next-undeleted-message (- n)))
  1031.  
  1032. (defun rmail-last-message ()
  1033.   "Show last message in file."
  1034.   (interactive)
  1035.   (rmail-maybe-set-message-counters)
  1036.   (rmail-show-message rmail-total-messages))
  1037.  
  1038. (defun rmail-what-message ()
  1039.   (let ((where (point))
  1040.     (low 1)
  1041.     (high rmail-total-messages)
  1042.     (mid (/ rmail-total-messages 2)))
  1043.     (while (> (- high low) 1)
  1044.       (if (>= where (rmail-msgbeg mid))
  1045.       (setq low mid)
  1046.     (setq high mid))
  1047.       (setq mid (+ low (/ (- high low) 2))))
  1048.     (if (>= where (rmail-msgbeg high)) high low)))
  1049.  
  1050. (defvar rmail-search-last-regexp nil)
  1051. (defun rmail-search (regexp &optional reversep)
  1052.   "Show message containing next match for REGEXP.
  1053. Search in reverse (earlier messages) with non-nil 2nd arg REVERSEP.
  1054. Interactively, empty argument means use same regexp used last time,
  1055. and reverse search is specified by a negative numeric arg."
  1056.   (interactive
  1057.     (let* ((reversep (< (prefix-numeric-value current-prefix-arg) 0))
  1058.        (prompt (concat (if reversep "Reverse " "") "Rmail search (regexp): "))
  1059.        regexp)
  1060.       (if rmail-search-last-regexp
  1061.       (setq prompt (concat prompt
  1062.                    "(default "
  1063.                    rmail-search-last-regexp
  1064.                    ") ")))
  1065.       (setq regexp (read-string prompt))
  1066.       (cond ((not (equal regexp ""))
  1067.          (setq rmail-search-last-regexp regexp))
  1068.         ((not rmail-search-last-regexp)
  1069.          (error "No previous Rmail search string")))
  1070.       (list rmail-search-last-regexp reversep)))
  1071.   (message "%sRmail search for %s..."
  1072.        (if reversep "Reverse " "")
  1073.        regexp)
  1074.   (let ((omin (point-min))
  1075.     (omax (point-max))
  1076.     (opoint (point))
  1077.     win
  1078.     (msg rmail-current-message))
  1079.     (unwind-protect
  1080.     (progn
  1081.       (widen)
  1082.       ;; Check messages one by one, advancing message number up or down
  1083.       ;; but searching forward through each message.
  1084.       (if reversep
  1085.           (while (and (null win) (> msg 1))
  1086.         (goto-char (rmail-msgbeg (setq msg (1- msg))))
  1087.         (setq win (re-search-forward
  1088.                regexp (rmail-msgend msg) t)))
  1089.         (while (and (null win) (< msg rmail-total-messages))
  1090.           (goto-char (rmail-msgbeg (setq msg (1+ msg))))
  1091.           (setq win (re-search-forward regexp (rmail-msgend msg) t)))))
  1092.       (if win
  1093.       (progn
  1094.         ;; If this is a reverse search and we found a message,
  1095.         ;; search backward thru this message to position point.
  1096.         (if reversep
  1097.         (progn
  1098.           (goto-char (rmail-msgend msg))
  1099.           (re-search-backward
  1100.            regexp (rmail-msgbeg msg) t)))
  1101.         (setq win (point))
  1102.         (rmail-show-message msg)
  1103.         (message "%sRmail search for %s...done"
  1104.              (if reversep "Reverse " "")
  1105.              regexp)
  1106.         (goto-char win))
  1107.     (goto-char opoint)
  1108.     (narrow-to-region omin omax)
  1109.     (ding)
  1110.     (message "Search failed: %s" regexp)))))
  1111.  
  1112. ;;;; *** Rmail Message Deletion Commands ***
  1113.  
  1114. (defun rmail-message-deleted-p (n)
  1115.   (= (aref rmail-deleted-vector n) ?D))
  1116.  
  1117. (defun rmail-set-message-deleted-p (n state)
  1118.   (aset rmail-deleted-vector n (if state ?D ?\ )))
  1119.  
  1120. (defun rmail-delete-message ()
  1121.   "Delete this message and stay on it."
  1122.   (interactive)
  1123.   (rmail-set-attribute "deleted" t))
  1124.  
  1125. (defun rmail-undelete-previous-message ()
  1126.   "Back up to deleted message, select it, and undelete it."
  1127.   (interactive)
  1128.   (let ((msg rmail-current-message))
  1129.     (while (and (> msg 0)
  1130.         (not (rmail-message-deleted-p msg)))
  1131.       (setq msg (1- msg)))
  1132.     (if (= msg 0)
  1133.     (error "No previous deleted message")
  1134.       (if (/= msg rmail-current-message)
  1135.       (rmail-show-message msg))
  1136.       (rmail-set-attribute "deleted" nil))))
  1137.  
  1138. (defun rmail-delete-forward (&optional backward)
  1139.   "Delete this message and move to next nondeleted one.
  1140. Deleted messages stay in the file until the \\[rmail-expunge] command is given.
  1141. With prefix argument, delete and move backward.
  1142. If there is no nondeleted message to move to
  1143. in the preferred or specified direction, move in the other direction."
  1144.   (interactive "P")
  1145.   (rmail-set-attribute "deleted" t)
  1146.   (condition-case ()
  1147.       (rmail-next-undeleted-message (if backward -1 1))
  1148.     (error
  1149.      (condition-case ()
  1150.      (rmail-previous-undeleted-message (if backward -1 1))
  1151.        (error nil)))))
  1152.  
  1153. (defun rmail-delete-backward ()
  1154.   "Delete this message and move to previous nondeleted one.
  1155. Deleted messages stay in the file until the \\[rmail-expunge] command is given."
  1156.   (interactive)
  1157.   (rmail-delete-forward t))
  1158.  
  1159. (defun rmail-expunge ()
  1160.   "Actually erase all deleted messages in the file."
  1161.   (interactive)
  1162.   (message "Expunging deleted messages...")
  1163.   (rmail-maybe-set-message-counters)
  1164.   (let* ((omax (- (buffer-size) (point-max)))
  1165.      (omin (- (buffer-size) (point-min)))
  1166.      (opoint (if (and (> rmail-current-message 0)
  1167.               (= ?D (aref rmail-deleted-vector rmail-current-message)))
  1168.              0 (- (point) (point-min))))
  1169.      (messages-head (cons (aref rmail-message-vector 0) nil))
  1170.      (messages-tail messages-head)
  1171.      (win))
  1172.     (unwind-protect
  1173.     (save-excursion
  1174.       (widen)
  1175.       (goto-char (point-min))
  1176.       (let ((counter 0)
  1177.         (number 1)
  1178.         (total rmail-total-messages)
  1179.         (new-message-number rmail-current-message)
  1180.         (new-summary nil)
  1181.         (buffer-read-only nil)
  1182.         (messages rmail-message-vector)
  1183.         (deleted rmail-deleted-vector)
  1184.         (summary rmail-summary-vector))
  1185.         (setq rmail-total-messages nil
  1186.           rmail-current-message nil
  1187.           rmail-message-vector nil
  1188.           rmail-deleted-vector nil
  1189.           rmail-summary-vector nil)
  1190.         (while (<= number total)
  1191.           (if (= (aref deleted number) ?D)
  1192.           (progn
  1193.             (delete-region
  1194.               (marker-position (aref messages number))
  1195.               (marker-position (aref messages (1+ number))))
  1196.             (move-marker (aref messages number) nil)
  1197.             (if (> new-message-number counter)
  1198.             (setq new-message-number (1- new-message-number))))
  1199.         (setq counter (1+ counter))
  1200.         (setq messages-tail
  1201.               (setcdr messages-tail
  1202.                   (cons (aref messages number) nil)))
  1203.         (setq new-summary
  1204.               (cons (if (= counter number) (aref summary (1- number)))
  1205.                 new-summary)))
  1206.           (if (zerop (% (setq number (1+ number)) 20))
  1207.           (message "Expunging deleted messages...%d" number)))
  1208.         (setq messages-tail
  1209.           (setcdr messages-tail
  1210.               (cons (aref messages number) nil)))
  1211.         (setq rmail-current-message new-message-number
  1212.           rmail-total-messages counter
  1213.           rmail-message-vector (apply 'vector messages-head)
  1214.           rmail-deleted-vector (make-string (1+ counter) ?\ )
  1215.           rmail-summary-vector (vconcat (nreverse new-summary))
  1216.           win t)))
  1217.       (message "Expunging deleted messages...done")
  1218.       (if (not win)
  1219.       (narrow-to-region (- (buffer-size) omin) (- (buffer-size) omax)))
  1220.       (rmail-show-message
  1221.        (if (zerop rmail-current-message) 1 nil))
  1222.       (forward-char opoint))))
  1223.  
  1224. ;;;; *** Rmail Mailing Commands ***
  1225.  
  1226. (defun rmail-mail ()
  1227.   "Send mail in another window.
  1228. While composing the message, use \\[mail-yank-original] to yank the
  1229. original message into it."
  1230.   (interactive)
  1231.   (mail-other-window nil nil nil nil nil (current-buffer)))
  1232.  
  1233. (defun rmail-continue ()
  1234.   "Continue composing outgoing message previously being composed."
  1235.   (interactive)
  1236.   (mail-other-window t))
  1237.  
  1238. (defun rmail-reply (just-sender)
  1239.   "Reply to the current message.
  1240. Normally include CC: to all other recipients of original message;
  1241. prefix argument means ignore them.
  1242. While composing the reply, use \\[mail-yank-original] to yank the
  1243. original message into it."
  1244.   (interactive "P")
  1245.   (let (from reply-to cc subject date to message-id resent-reply-to)
  1246.     (save-excursion
  1247.       (save-restriction
  1248.     (widen)
  1249.     (goto-char (rmail-msgbeg rmail-current-message))
  1250.     (forward-line 1)
  1251.     (if (= (following-char) ?0)
  1252.         (narrow-to-region
  1253.          (progn (forward-line 2)
  1254.             (point))
  1255.          (progn (search-forward "\n\n" (rmail-msgend rmail-current-message)
  1256.                     'move)
  1257.             (point)))
  1258.       (narrow-to-region (point)
  1259.                 (progn (search-forward "\n*** EOOH ***\n")
  1260.                    (beginning-of-line) (point))))
  1261.     (setq resent-reply-to (mail-fetch-field "resent-reply-to" t)
  1262.           from (mail-fetch-field "from")
  1263.           reply-to (or resent-reply-to
  1264.                (mail-fetch-field "reply-to" nil t)
  1265.                from)
  1266.           cc (cond (just-sender nil)
  1267.                (resent-reply-to (mail-fetch-field "resent-cc" t))
  1268.                (t (mail-fetch-field "cc" nil t)))
  1269.           subject (or (and resent-reply-to
  1270.                    (mail-fetch-field "resent-subject" t))
  1271.               (mail-fetch-field "subject"))
  1272.           date (cond (resent-reply-to
  1273.               (mail-fetch-field "resent-date" t))
  1274.              ((mail-fetch-field "date")))
  1275.           to (cond (resent-reply-to
  1276.             (mail-fetch-field "resent-to" t))
  1277.                ((mail-fetch-field "to" nil t))
  1278.                ;((mail-fetch-field "apparently-to")) ack gag barf
  1279.                (t ""))
  1280.           message-id (cond (resent-reply-to
  1281.                 (mail-fetch-field "resent-message-id" t))
  1282.                    ((mail-fetch-field "message-id"))))))
  1283.     (and subject
  1284.      (string-match "\\`Re: " subject)
  1285.      (setq subject (substring subject 4)))
  1286.     (mail-other-window nil
  1287.       (mail-strip-quoted-names reply-to)
  1288.       subject
  1289.       (rmail-make-in-reply-to-field from date message-id)
  1290.       (if just-sender
  1291.       nil
  1292.     (let* ((cc-list (rmail-dont-reply-to
  1293.               (mail-strip-quoted-names
  1294.                 (if (null cc) to (concat to ", " cc))))))
  1295.       (if (string= cc-list "") nil cc-list)))
  1296.       (current-buffer)
  1297.       (list (list '(lambda (buf msgnum)
  1298.              (save-excursion
  1299.                (set-buffer buf)
  1300.                (rmail-set-attribute "answered" t msgnum)))
  1301.           (current-buffer) rmail-current-message)))))
  1302.  
  1303. (defun rmail-make-in-reply-to-field (from date message-id)
  1304.   (cond ((not from)
  1305.          (if message-id
  1306.              message-id
  1307.              nil))
  1308.         (mail-use-rfc822
  1309.          (require 'rfc822)
  1310.          (let ((tem (car (rfc822-addresses from))))
  1311.            (if message-id
  1312.                (if (string-match
  1313.                     (regexp-quote (if (string-match "@[^@]*\\'" tem)
  1314.                                       (substring tem 0 (match-beginning 0))
  1315.                                       tem))
  1316.                     message-id)
  1317.                    ;; Message-ID is sufficiently informative
  1318.                    message-id
  1319.                    (concat message-id " (" tem ")"))
  1320.                ;; Use prin1 to fake RFC822 quoting
  1321.                (let ((field (prin1-to-string tem)))
  1322.                  (if date
  1323.                      (concat field "'s message of " date)
  1324.                      field)))))
  1325.         ((let* ((foo "[^][\000-\037\177-\377()<>@,;:\\\" ]+")
  1326.                 (bar "[^][\000-\037\177-\377()<>@,;:\\\"]+"))
  1327.            ;; Can't use format because format loses on \000 (unix *^&%*^&%$!!)
  1328.            (or (string-match (concat "\\`[ \t]*\\(" bar
  1329.                                      "\\)\\(<" foo "@" foo ">\\)?[ \t]*\\'")
  1330.                              ;; "Unix Loser <Foo@bar.edu>" => "Unix Loser"
  1331.                              from)
  1332.                (string-match (concat "\\`[ \t]*<" foo "@" foo ">[ \t]*(\\("
  1333.                                      bar "\\))[ \t]*\\'")
  1334.                              ;; "<Bugs@bar.edu>" (Losing Unix) => "Losing Unix"
  1335.                              from)))
  1336.          (let ((start (match-beginning 1))
  1337.                (end (match-end 1)))
  1338.            ;; Trim whitespace which above regexp match allows
  1339.            (while (and (< start end)
  1340.                        (memq (aref from start) '(?\t ?\ )))
  1341.              (setq start (1+ start)))
  1342.            (while (and (< start end)
  1343.                        (memq (aref from (1- end)) '(?\t ?\ )))
  1344.              (setq end (1- end)))
  1345.            (let ((field (substring from start end)))
  1346.              (if date (setq field (concat "message from " field " on " date)))
  1347.              (if message-id
  1348.                  ;; "<AA259@bar.edu> (message from Unix Loser on 1-Apr-89)"
  1349.                  (concat message-id " (" field ")")
  1350.                  field))))
  1351.         (t
  1352.          ;; If we can't kludge it simply, do it correctly
  1353.          (let ((mail-use-rfc822 t))
  1354.            (rmail-make-in-reply-to-field from date message-id)))))
  1355.  
  1356. (defun rmail-forward ()
  1357.   "Forward the current message to another user."
  1358.   (interactive)
  1359.   (let ((forward-buffer (current-buffer))
  1360.     (subject (concat "["
  1361.              (mail-strip-quoted-names (mail-fetch-field "From"))
  1362.              ": " (or (mail-fetch-field "Subject") "") "]")))
  1363.     ;; If only one window, use it for the mail buffer.
  1364.     ;; Otherwise, use another window for the mail buffer
  1365.     ;; so that the Rmail buffer remains visible
  1366.     ;; and sending the mail will get back to it.
  1367.     (if (funcall (if (one-window-p t)
  1368.              (function mail)
  1369.            (function mail-other-window))
  1370.                  nil nil subject nil nil nil
  1371.                  (list (list (function (lambda (buf msgnum)
  1372.                                (save-excursion
  1373.                                  (set-buffer buf)
  1374.                                  (rmail-set-attribute "forwarded" t msgnum))))
  1375.                              (current-buffer)
  1376.                              rmail-current-message)))
  1377.     (save-excursion
  1378.       (goto-char (point-max))
  1379.       (forward-line 1)
  1380.       (insert-buffer forward-buffer)))))
  1381.  
  1382. (defvar mail-unsent-separator "^   ----- Unsent message follows -----$")
  1383.  
  1384. (defun rmail-retry-failure ()
  1385.   "Edit a mail message which is based on the contents of the current message.
  1386. For a message rejected by the mail system, extract the interesting headers and
  1387. the body of the original message; otherwise copy the current message."
  1388.   (interactive)
  1389.   (require 'mail-utils)
  1390.   (let (to subj irp2 cc orig-message)
  1391.     (save-excursion
  1392.       ;; Narrow down to just the quoted original message
  1393.       (rmail-beginning-of-message)
  1394.       (or (re-search-forward mail-unsent-separator nil t)
  1395.       (error "Cannot parse this as a failure message"))
  1396.       (save-restriction
  1397.     (narrow-to-region (point) (point-max))
  1398.     ;; Now mail-fetch-field will get from headers of the original message,
  1399.     ;; not from the headers of the rejection.
  1400.     (setq to   (mail-fetch-field "To")
  1401.           subj (mail-fetch-field "Subject")
  1402.           irp2 (mail-fetch-field "In-reply-to")
  1403.           cc   (mail-fetch-field "Cc"))
  1404.     ;; Get the entire text (not headers) of the original message.
  1405.     (setq orig-message
  1406.           (buffer-substring
  1407.            (progn (search-forward "\n\n") (point))
  1408.            (point-max)))))
  1409.     ;; Start sending a new message; default header fields from the original.
  1410.     (if (mail-other-window nil to subj irp2 cc (current-buffer))
  1411.     ;; Insert original text as initial text of new draft message.
  1412.     (progn
  1413.       (goto-char (point-max))
  1414.       (insert orig-message)
  1415.       (goto-char (point-min))
  1416.       (end-of-line)))))
  1417.  
  1418. ;;;; *** Rmail Specify Inbox Files ***
  1419.  
  1420. (autoload 'set-rmail-inbox-list "rmailmsc"
  1421.   "Set the inbox list of the current RMAIL file to FILE-NAME.
  1422. This may be a list of file names separated by commas.
  1423. If FILE-NAME is empty, remove any inbox list."
  1424.   t)
  1425.  
  1426. ;;;; *** Rmail Commands for Labels ***
  1427.  
  1428. (autoload 'rmail-add-label "rmailkwd"
  1429.   "Add LABEL to labels associated with current RMAIL message.
  1430. Completion is performed over known labels when reading."
  1431.   t)
  1432.  
  1433. (autoload 'rmail-kill-label "rmailkwd"
  1434.   "Remove LABEL from labels associated with current RMAIL message.
  1435. Completion is performed over known labels when reading."
  1436.   t)
  1437.  
  1438. (autoload 'rmail-next-labeled-message "rmailkwd"
  1439.   "Show next message with LABEL.  Defaults to last label used.
  1440. With prefix argument N moves forward N messages with this label."
  1441.   t)
  1442.  
  1443. (autoload 'rmail-previous-labeled-message "rmailkwd"
  1444.   "Show previous message with LABEL.  Defaults to last label used.
  1445. With prefix argument N moves backward N messages with this label."
  1446.   t)
  1447.  
  1448. ;;;; *** Rmail Edit Mode ***
  1449.  
  1450. (autoload 'rmail-edit-current-message "rmailedit"
  1451.   "Edit the contents of the current message"
  1452.   t)
  1453.  
  1454. ;;;; *** Rmail Summary Mode ***
  1455.  
  1456. (autoload 'rmail-summary "rmailsum"
  1457.   "Display a summary of all messages, one line per message."
  1458.   t)
  1459.  
  1460. (autoload 'rmail-summary-by-labels "rmailsum"
  1461.   "Display a summary of all messages with one or more LABELS.
  1462. LABELS should be a string containing the desired labels, separated by commas."
  1463.   t)
  1464.  
  1465. (autoload 'rmail-summary-by-recipients "rmailsum"
  1466.   "Display a summary of all messages with the given RECIPIENTS.
  1467. Normally checks the To, From and Cc fields of headers;
  1468. but if PRIMARY-ONLY is non-nil (prefix arg given),
  1469.  only look in the To and From fields.
  1470. RECIPIENTS is a string of names separated by commas."
  1471.   t)
  1472.  
  1473. ;;;; *** Rmail output messages to files ***
  1474.  
  1475. (autoload 'rmail-output-to-rmail-file "rmailout"
  1476.   "Append the current message to an Rmail file named FILE-NAME.
  1477. If the file does not exist, ask if it should be created.
  1478. If file is being visited, the message is appended to the Emacs
  1479. buffer visiting that file."
  1480.   t)
  1481.  
  1482. (autoload 'rmail-output "rmailout"
  1483.   "Append this message to Unix mail file named FILE-NAME."
  1484.   t)
  1485.  
  1486. ;;;; *** Rmail undigestification ***
  1487.  
  1488. (autoload 'undigestify-rmail-message "undigest"
  1489.   "Break up a digest message into its constituent messages.
  1490. Leaves original message, deleted, before the undigestified messages."
  1491.   t)
  1492.