home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 31 / CDASC_31_1996_juillet_aout.iso / vrac_os2 / e31el3.zip / EMACS / 19.31 / LISP / MH-E.EL < prev    next >
Lisp/Scheme  |  1996-05-27  |  54KB  |  1,485 lines

  1. ;;; mh-e.el --- GNU Emacs interface to the MH mail system
  2.  
  3. ;; Copyright (C) 1985,86,87,88,90,92,93,94,95 Free Software Foundation, Inc.
  4.  
  5. ;; Maintainer: Stephen Gildea <gildea@lcs.mit.edu>
  6. ;; Version: 5.0.2
  7. ;; Keywords: mail
  8. ;; Bug-reports: include `M-x mh-version' output in any correspondence
  9.  
  10. ;; This file is part of mh-e, part of GNU Emacs.
  11.  
  12. ;; GNU Emacs is free software; you can redistribute it and/or modify
  13. ;; it under the terms of the GNU General Public License as published by
  14. ;; the Free Software Foundation; either version 2, or (at your option)
  15. ;; any later version.
  16.  
  17. ;; GNU Emacs is distributed in the hope that it will be useful,
  18. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20. ;; GNU General Public License for more details.
  21.  
  22. ;; You should have received a copy of the GNU General Public License
  23. ;; along with GNU Emacs; see the file COPYING.  If not, write to the
  24. ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  25. ;; Boston, MA 02111-1307, USA.
  26.  
  27. ;;; Commentary:
  28.  
  29. ;; HOW TO USE:
  30. ;; M-x mh-rmail to read mail.  Type C-h m there for a list of commands.
  31. ;; C-u M-x mh-rmail to visit any folder.
  32. ;; M-x mh-smail to send mail.  From within the mail reader, "m" works, too.
  33.  
  34. ;; MH (Message Handler) is a powerful mail reader.  The MH newsgroup
  35. ;; is comp.mail.mh; the mailing list is mh-users@ics.uci.edu (send to
  36. ;; mh-users-request to be added).  See the monthly Frequently Asked
  37. ;; Questions posting there for information on getting MH and mh-e.
  38.  
  39. ;; mh-e is an Emacs interface to the MH mail system.
  40. ;; The mailing list mh-e@x.org is for discussion of mh-e and
  41. ;; announcements of new versions.  Send a "subscribe" message to
  42. ;; mh-e-request@x.org to be added.  Do not report bugs here; mail
  43. ;; them directly to the author (see top of mh-e.el source).
  44. ;; Include the output of M-x mh-version in any bug report.
  45.  
  46. ;; mh-e works with GNU Emacs 18 or 19, and MH 6.
  47.  
  48. ;; NB.  MH must have been compiled with the MHE compiler flag or several
  49. ;; features necessary for mh-e will be missing from MH commands, specifically
  50. ;; the -build switch to repl and forw.
  51.  
  52. ;; Your .emacs might benefit from these bindings:
  53. ;; (global-set-key "\C-cr" 'mh-rmail)
  54. ;; (global-set-key "\C-xm" 'mh-smail)
  55. ;; (global-set-key "\C-x4m" 'mh-smail-other-window)
  56.  
  57. ;;; Change Log:
  58.  
  59. ;; Original version for Gosling emacs by Brian Reid, Stanford, 1982.
  60. ;; Modified by James Larus, BBN, July 1984 and UCB, 1984 & 1985.
  61. ;; Rewritten for GNU Emacs, James Larus 1985.  larus@ginger.berkeley.edu
  62. ;; Modified by Stephen Gildea 1988.  gildea@lcs.mit.edu
  63. (defconst mh-e-RCS-id "$Id: mh-e.el,v 1.14 1996/05/21 17:26:31 kwzh Exp $")
  64.  
  65. ;;; Code:
  66.  
  67. (provide 'mh-e)
  68. (require 'mh-utils)
  69.  
  70.  
  71. ;;; Hooks:
  72.  
  73. (defvar mh-folder-mode-hook nil
  74.   "Invoked in MH-Folder mode on a new folder.")
  75.  
  76. (defvar mh-inc-folder-hook nil
  77.   "Invoked by \\<mh-folder-mode-map>`\\[mh-inc-folder]' after incorporating mail into a folder.")
  78.  
  79. (defvar mh-show-hook nil
  80.   "Invoked after \\<mh-folder-mode-map>`\\[mh-show]' shows a message.")
  81.  
  82. (defvar mh-show-mode-hook nil
  83.   "Invoked in MH-Show mode on each message.")
  84.  
  85. (defvar mh-delete-msg-hook nil
  86.   "Invoked after marking each message for deletion.")
  87.  
  88. (defvar mh-refile-msg-hook nil
  89.   "Invoked after marking each message for refiling.")
  90.  
  91. (defvar mh-before-quit-hook nil
  92.   "Invoked by \\<mh-folder-mode-map>`\\[mh-quit]' before quitting mh-e.  See also  mh-quit-hook.")
  93.  
  94. (defvar mh-quit-hook nil
  95.   "Invoked after \\<mh-folder-mode-map>`\\[mh-quit]' quits mh-e.  See also  mh-before-quit-hook.")
  96.  
  97.  
  98.  
  99. ;;; Personal preferences:
  100.  
  101. (defvar mh-lpr-command-format "lpr -J '%s'"
  102.   "*Format for Unix command that prints a message.
  103. The string should be a Unix command line, with the string '%s' where
  104. the job's name (folder and message number) should appear.  The formatted
  105. message text is piped to this command when you type \\<mh-folder-mode-map>`\\[mh-print-msg]'.")
  106.  
  107. (defvar mh-scan-prog "scan"
  108.   "*Program to run to generate one-line-per-message listing of a folder.
  109. Normally \"scan\" or a file name linked to scan.  This file is searched
  110. for relative to the mh-progs directory unless it is an absolute pathname.
  111. Automatically becomes buffer-local when set in any fashion.")
  112. (make-variable-buffer-local 'mh-scan-prog)
  113.  
  114. (defvar mh-inc-prog "inc"
  115.   "*Program to run to incorporate new mail into a folder.
  116. Normally \"inc\".  This file is searched for relative to
  117. the mh-progs directory unless it is an absolute pathname.")
  118.  
  119. (defvar mh-print-background nil
  120.   "*Print messages in the background if non-nil.
  121. WARNING: do not delete the messages until printing is finished;
  122. otherwise, your output may be truncated.")
  123.  
  124. (defvar mh-recenter-summary-p nil
  125.   "*Recenter summary window when the show window is toggled off if non-nil.")
  126.  
  127. (defvar mh-do-not-confirm nil
  128.   "*Non-nil means do not prompt for confirmation before some mh-e commands.
  129. Affects non-recoverable commands such as mh-kill-folder and mh-undo-folder.")
  130.  
  131. (defvar mh-store-default-directory nil
  132.   "*Last directory used by \\[mh-store-msg]; default for next store.
  133. A directory name string, or nil to use current directory.")
  134.  
  135. ;;; Parameterize mh-e to work with different scan formats.  The defaults work
  136. ;;; with the standard MH scan listings, in which the first 4 characters on
  137. ;;; the line are the message number, followed by two places for notations.
  138.  
  139. (defvar mh-good-msg-regexp  "^....[^D^]"
  140.   "Regexp specifying the scan lines that are 'good' messages.")
  141.  
  142. (defvar mh-deleted-msg-regexp "^....D"
  143.   "Regexp matching scan lines of deleted messages.")
  144.  
  145. (defvar mh-refiled-msg-regexp  "^....\\^"
  146.   "Regexp matching scan lines of refiled messages.")
  147.  
  148. (defvar mh-valid-scan-line "^ *[0-9]"
  149.   "Regexp matching scan lines for messages (not error messages).")
  150.  
  151. (defvar mh-cur-scan-msg-regexp "^....\\+"
  152.   "Regexp matching scan line for the cur message.")
  153.  
  154. (defvar mh-note-deleted "D"
  155.   "String whose first character is used to notate deleted messages.")
  156.  
  157. (defvar mh-note-refiled "^"
  158.   "String whose first character is used to notate refiled messages.")
  159.  
  160. (defvar mh-note-cur "+"
  161.   "String whose first character is used to notate the current message.")
  162.  
  163. (defvar mh-partial-folder-mode-line-annotation "select"
  164.   "Annotation when displaying part of a folder.
  165. The string is displayed after the folder's name.  NIL for no annotation.")
  166.  
  167.  
  168. ;;; Internal variables:
  169.  
  170. (defvar mh-last-destination nil)    ;Destination of last refile or write command.
  171.  
  172. (defvar mh-folder-mode-map (make-keymap)
  173.   "Keymap for MH folders.")
  174.  
  175. (defvar mh-delete-list nil)        ;List of msg numbers to delete.
  176.  
  177. (defvar mh-refile-list nil)        ;List of folder names in mh-seq-list.
  178.  
  179. (defvar mh-next-direction 'forward)    ;Direction to move to next message.
  180.  
  181. (defvar mh-narrowed-to-seq nil)        ;Sequence display is narrowed to or nil if not narrowed.
  182.  
  183. (defvar mh-first-msg-num nil)        ;Number of first msg in buffer.
  184.  
  185. (defvar mh-last-msg-num nil)        ;Number of last msg in buffer.
  186.  
  187. (defvar mh-mode-line-annotation nil)    ;Indiction this is not the full folder.
  188.  
  189. ;;; Macros and generic functions:
  190.  
  191. (defun mh-mapc (func list)
  192.   (while list
  193.     (funcall func (car list))
  194.     (setq list (cdr list))))
  195.  
  196.  
  197.  
  198. ;;; Entry points:
  199.  
  200. ;;;###autoload
  201. (defun mh-rmail (&optional arg)
  202.   "Inc(orporate) new mail with MH, or, with arg, scan an MH mail folder.
  203. This function is an entry point to mh-e, the Emacs front end
  204. to the MH mail system."
  205.   (interactive "P")
  206.   (mh-find-path)
  207.   (if arg
  208.       (call-interactively 'mh-visit-folder)
  209.       (mh-inc-folder)))
  210.  
  211.  
  212. ;;; mh-smail and mh-smail-other-window have been moved to the new file
  213. ;;; mh-comp.el, but Emacs 18 still looks for them here, so provide a
  214. ;;; definition here, too, for a while.
  215.  
  216. (defun mh-smail ()
  217.   "Compose and send mail with the MH mail system.
  218. This function is an entry point to mh-e, the Emacs front end
  219. to the MH mail system."
  220.   (interactive)
  221.   (mh-find-path)
  222.   (require 'mh-comp)
  223.   (call-interactively 'mh-send))
  224.  
  225.  
  226. (defun mh-smail-other-window ()
  227.   "Compose and send mail in other window with the MH mail system.
  228. This function is an entry point to mh-e, the Emacs front end
  229. to the MH mail system."
  230.   (interactive)
  231.   (mh-find-path)
  232.   (require 'mh-comp)
  233.   (call-interactively 'mh-send-other-window))
  234.  
  235.  
  236.  
  237. ;;; User executable mh-e commands:
  238.  
  239.  
  240. (defun mh-delete-msg (msg-or-seq)
  241.   "Mark the specified MESSAGE(s) for subsequent deletion and move to the next.
  242. Default is the displayed message.  If optional prefix argument is
  243. given then prompt for the message sequence."
  244.   (interactive (list (if current-prefix-arg
  245.              (mh-read-seq-default "Delete" t)
  246.              (mh-get-msg-num t))))
  247.   (mh-delete-msg-no-motion msg-or-seq)
  248.   (mh-next-msg))
  249.  
  250.  
  251. (defun mh-delete-msg-no-motion (msg-or-seq)
  252.   "Mark the specified MESSAGE(s) for subsequent deletion.
  253. Default is the displayed message.  If optional prefix argument is
  254. provided, then prompt for the message sequence."
  255.   (interactive (list (if current-prefix-arg
  256.              (mh-read-seq-default "Delete" t)
  257.              (mh-get-msg-num t))))
  258.   (if (numberp msg-or-seq)
  259.       (mh-delete-a-msg msg-or-seq)
  260.       (mh-map-to-seq-msgs 'mh-delete-a-msg msg-or-seq)))
  261.  
  262.  
  263. (defun mh-execute-commands ()
  264.   "Process outstanding delete and refile requests."
  265.   (interactive)
  266.   (if mh-narrowed-to-seq (mh-widen))
  267.   (mh-process-commands mh-current-folder)
  268.   (mh-set-scan-mode)
  269.   (mh-goto-cur-msg)            ; after mh-set-scan-mode for efficiency
  270.   (mh-make-folder-mode-line)
  271.   t)                    ; return t for [local-]write-file-hooks
  272.  
  273.  
  274. (defun mh-first-msg ()
  275.   "Move to the first message."
  276.   (interactive)
  277.   (goto-char (point-min))
  278.   (while (and (not (eobp)) (not (looking-at mh-valid-scan-line)))
  279.     (forward-line 1)))
  280.  
  281.  
  282. (defun mh-header-display ()
  283.   "Show the current message with all its headers.
  284. Displays headers that might have been suppressed by setting the
  285. variables `mh-clean-message-header' or `mhl-formfile', or by the fallback
  286. behavior of scrolling uninteresting headers off the top of the window.
  287. Type \"\\[mh-show]\" to show the message normally again."
  288.   (interactive)
  289.   (and (not mh-showing-with-headers)
  290.        (or mhl-formfile mh-clean-message-header)
  291.        (mh-invalidate-show-buffer))
  292.   (let ((mhl-formfile nil)
  293.     (mh-clean-message-header nil))
  294.     (mh-show-msg nil)
  295.     (mh-in-show-buffer (mh-show-buffer)
  296.       (goto-char (point-min))
  297.       (mh-recenter 0))
  298.     (setq mh-showing-with-headers t)))
  299.  
  300.  
  301. (defun mh-inc-folder (&optional maildrop-name)
  302.   "Inc(orporate)s new mail into the Inbox folder.
  303. Optional prefix argument specifies an alternate maildrop from the default.
  304. If the prefix argument is given, incorporates mail into the current
  305. folder, otherwise uses the folder named by `mh-inbox'.
  306. Runs `mh-inc-folder-hook' after incorporating new mail.
  307. Do not call this function from outside mh-e; use \\[mh-rmail] instead."
  308.   (interactive (list (if current-prefix-arg
  309.              (expand-file-name
  310.               (read-file-name "inc mail from file: "
  311.                       mh-user-path)))))
  312.   (let ((config (current-window-configuration)))
  313.     (if (not maildrop-name)
  314.     (cond ((not (get-buffer mh-inbox))
  315.            (mh-make-folder mh-inbox)
  316.            (setq mh-previous-window-config config))
  317.           ((not (eq (current-buffer) (get-buffer mh-inbox)))
  318.            (switch-to-buffer mh-inbox)
  319.            (setq mh-previous-window-config config)))))
  320.   (mh-get-new-mail maildrop-name)
  321.   (run-hooks 'mh-inc-folder-hook))
  322.  
  323.  
  324. (defun mh-last-msg ()
  325.   "Move to the last message."
  326.   (interactive)
  327.   (goto-char (point-max))
  328.   (while (and (not (bobp)) (looking-at "^$"))
  329.     (forward-line -1)))
  330.  
  331.  
  332. (defun mh-next-undeleted-msg (&optional arg)
  333.   "Move to the NTH next undeleted message in window."
  334.   (interactive "p")
  335.   (setq mh-next-direction 'forward)
  336.   (forward-line 1)
  337.   (cond ((re-search-forward mh-good-msg-regexp nil 0 arg)
  338.      (beginning-of-line)
  339.      (mh-maybe-show))
  340.     (t
  341.      (forward-line -1)
  342.      (if (get-buffer mh-show-buffer)
  343.          (delete-windows-on mh-show-buffer)))))
  344.  
  345.  
  346. (defun mh-refile-msg (msg-or-seq folder)
  347.   "Refile MESSAGE(s) (default: displayed message) into FOLDER.
  348. If optional prefix argument provided, then prompt for message sequence."
  349.   (interactive
  350.    (list (if current-prefix-arg
  351.          (mh-read-seq-default "Refile" t)
  352.        (mh-get-msg-num t))
  353.      (intern
  354.       (mh-prompt-for-folder
  355.        "Destination"
  356.        (or (and mh-default-folder-for-message-function
  357.             (let ((refile-file (mh-msg-filename (mh-get-msg-num t))))
  358.               (save-excursion
  359.             (set-buffer (get-buffer-create mh-temp-buffer))
  360.             (erase-buffer)
  361.             (insert-file-contents refile-file)
  362.             (let ((buffer-file-name refile-file))
  363.               (funcall mh-default-folder-for-message-function)))))
  364.            (and (eq 'refile (car mh-last-destination))
  365.             (symbol-name (cdr mh-last-destination)))
  366.            "")
  367.        t))))
  368.   (setq mh-last-destination (cons 'refile folder))
  369.   (if (numberp msg-or-seq)
  370.       (mh-refile-a-msg msg-or-seq folder)
  371.       (mh-map-to-seq-msgs 'mh-refile-a-msg msg-or-seq folder))
  372.   (mh-next-msg))
  373.  
  374.  
  375. (defun mh-refile-or-write-again (message)
  376.   "Re-execute the last refile or write command on the given MESSAGE.
  377. Default is the displayed message.  Use the same folder or file as the
  378. previous refile or write command."
  379.   (interactive (list (mh-get-msg-num t)))
  380.   (if (null mh-last-destination)
  381.       (error "No previous refile or write"))
  382.   (cond ((eq (car mh-last-destination) 'refile)
  383.      (mh-refile-a-msg message (cdr mh-last-destination))
  384.      (message "Destination folder: %s" (cdr mh-last-destination)))
  385.     (t
  386.      (apply 'mh-write-msg-to-file message (cdr mh-last-destination))
  387.      (message "Destination: %s" (cdr mh-last-destination))))
  388.   (mh-next-msg))
  389.  
  390.  
  391. (defun mh-quit ()
  392.   "Quit the current mh-e folder.
  393. Start by running mh-before-quit-hook.  Restore the previous window
  394. configuration, if one exists.  Finish by running mh-quit-hook."
  395.   (interactive)
  396.   (run-hooks 'mh-before-quit-hook) 
  397.   (mh-update-sequences)
  398.   (mh-invalidate-show-buffer)
  399.   (bury-buffer (current-buffer))
  400.   (if (get-buffer mh-show-buffer)
  401.       (bury-buffer mh-show-buffer))
  402.   (if mh-previous-window-config
  403.       (set-window-configuration mh-previous-window-config))
  404.   (run-hooks 'mh-quit-hook))
  405.  
  406. (defun mh-page-msg (&optional arg)
  407.   "Page the displayed message forwards.
  408. Scrolls ARG lines or a full screen if no argument is supplied."
  409.   (interactive "P")
  410.   (scroll-other-window arg))
  411.  
  412.  
  413. (defun mh-previous-page (&optional arg)
  414.   "Page the displayed message backwards.
  415. Scrolls ARG lines or a full screen if no argument is supplied."
  416.   (interactive "P")
  417.   (mh-in-show-buffer (mh-show-buffer)
  418.     (scroll-down arg)))
  419.  
  420.  
  421. (defun mh-previous-undeleted-msg (&optional arg)
  422.   "Move to the NTH previous undeleted message in window."
  423.   (interactive "p")
  424.   (setq mh-next-direction 'backward)
  425.   (beginning-of-line)
  426.   (cond ((re-search-backward mh-good-msg-regexp nil 0 arg)
  427.      (mh-maybe-show))
  428.     (t
  429.      (if (get-buffer mh-show-buffer)
  430.          (delete-windows-on mh-show-buffer)))))
  431.  
  432.  
  433. (defun mh-rescan-folder (&optional range)
  434.   "Rescan a folder after optionally processing the outstanding commands.
  435. If optional prefix argument is provided, prompt for the range of
  436. messages to display.  Otherwise show the entire folder."
  437.   (interactive (list (if current-prefix-arg
  438.              (mh-read-msg-range "Range to scan [all]? ")
  439.                nil)))
  440.   (setq mh-next-direction 'forward)
  441.   (mh-scan-folder mh-current-folder (or range "all")))
  442.  
  443.  
  444. (defun mh-write-msg-to-file (msg file no-headers)
  445.   "Append MESSAGE to the end of a FILE.
  446. If NO-HEADERS (prefix argument) is provided, write only the message body.
  447. Otherwise send the entire message including the headers."
  448.   (interactive
  449.    (list (mh-get-msg-num t)
  450.      (let ((default-dir (if (eq 'write (car mh-last-destination))
  451.                 (file-name-directory (car (cdr mh-last-destination)))
  452.                   default-directory)))
  453.        (read-file-name (format "Save message%s in file: "
  454.                    (if current-prefix-arg " body" ""))
  455.                default-dir
  456.                (if (eq 'write (car mh-last-destination))
  457.                    (car (cdr mh-last-destination))
  458.                  (expand-file-name "mail.out" default-dir))))
  459.      current-prefix-arg))
  460.   (let ((msg-file-to-output (mh-msg-filename msg))
  461.     (output-file (mh-expand-file-name file)))
  462.     (setq mh-last-destination (list 'write file (if no-headers 'no-headers)))
  463.     (save-excursion
  464.       (set-buffer (get-buffer-create mh-temp-buffer))
  465.       (erase-buffer)
  466.       (insert-file-contents msg-file-to-output)
  467.       (goto-char (point-min))
  468.       (if no-headers (search-forward "\n\n"))
  469.       (append-to-file (point) (point-max) output-file))))
  470.  
  471.  
  472. (defun mh-toggle-showing ()
  473.   "Toggle the scanning mode/showing mode of displaying messages."
  474.   (interactive)
  475.   (if mh-showing
  476.       (mh-set-scan-mode)
  477.       (mh-show)))
  478.  
  479.  
  480. (defun mh-undo (msg-or-seq)
  481.   "Undo the pending deletion or refile of the specified MESSAGE(s).
  482. Default is the displayed message.  If optional prefix argument is
  483. provided, then prompt for the message sequence."
  484.   (interactive (list (if current-prefix-arg
  485.              (mh-read-seq-default "Undo" t)
  486.              (mh-get-msg-num t))))
  487.   (cond ((numberp msg-or-seq)
  488.      (let ((original-position (point)))
  489.        (beginning-of-line)
  490.        (while (not (or (looking-at mh-deleted-msg-regexp)
  491.                (looking-at mh-refiled-msg-regexp)
  492.                (and (eq mh-next-direction 'forward) (bobp))
  493.                (and (eq mh-next-direction 'backward)
  494.                 (save-excursion (forward-line) (eobp)))))
  495.          (forward-line (if (eq mh-next-direction 'forward) -1 1)))
  496.        (if (or (looking-at mh-deleted-msg-regexp)
  497.            (looking-at mh-refiled-msg-regexp))
  498.            (progn
  499.          (mh-undo-msg (mh-get-msg-num t))
  500.          (mh-maybe-show))
  501.            (goto-char original-position)
  502.            (error "Nothing to undo"))))
  503.     (t
  504.      (mh-map-to-seq-msgs 'mh-undo-msg msg-or-seq)))
  505.   ;; update the mh-refile-list so mh-outstanding-commands-p will work
  506.   (mh-mapc (function
  507.         (lambda (elt)
  508.           (if (not (mh-seq-to-msgs elt))
  509.           (setq mh-refile-list (delq elt mh-refile-list)))))
  510.        mh-refile-list)
  511.   (if (not (mh-outstanding-commands-p))
  512.       (mh-set-folder-modified-p nil)))
  513.  
  514.  
  515. ;;;###autoload
  516. (defun mh-version ()
  517.   "Display version information about mh-e and the MH mail handling system."
  518.   (interactive)
  519.   (mh-find-progs)
  520.   (set-buffer (get-buffer-create mh-temp-buffer))
  521.   (erase-buffer)
  522.   (insert "  mh-e info:\n\nversion: " mh-e-RCS-id
  523.       "\nEmacs: " emacs-version " on " (symbol-name system-type) " ")
  524.   (condition-case ()
  525.       (call-process "uname" nil t nil "-a")
  526.     (file-error))
  527.   (insert "\n\n  MH info:\n\n" (expand-file-name "inc" mh-progs) ":\n")
  528.   (let ((help-start (point)))
  529.     (condition-case err-data
  530.     (mh-exec-cmd-output "inc" nil "-help")
  531.       (file-error (insert (mapconcat 'concat (cdr err-data) ": "))))
  532.     (goto-char help-start)
  533.     (search-forward "version: " nil t)
  534.     (beginning-of-line)
  535.     (delete-region help-start (point))
  536.     (goto-char (point-min)))
  537.   (display-buffer mh-temp-buffer))
  538.  
  539.  
  540. (defun mh-visit-folder (folder &optional range)
  541.   "Visit FOLDER and display RANGE of messages.
  542. Do not call this function from outside mh-e; see \\[mh-rmail] instead."
  543.   (interactive (list (mh-prompt-for-folder "Visit" mh-inbox t)
  544.              (mh-read-msg-range "Range [all]? ")))
  545.   (let ((config (current-window-configuration)))
  546.     (mh-scan-folder folder (or range "all"))
  547.     (setq mh-previous-window-config config))
  548.   nil)
  549.  
  550.  
  551. (defun mh-compat-quit ()
  552.   "The \"b\" key is obsolescent; will assume you want \"\\[mh-quit]\" ..."
  553.   ;; Was going to make it run mh-burst-digest, but got complaint that
  554.   ;; 'b' should mean 'back', as it does in info, less, and rn.
  555.   ;; This is a temporary compatibility function.
  556.   (interactive)
  557.   (message "%s" (documentation this-command))
  558.   (sit-for 1)
  559.   (call-interactively 'mh-quit))
  560.  
  561.  
  562. (defun mh-update-sequences ()
  563.   "Update MH's Unseen sequence and current folder and message.
  564. Flush mh-e's state out to MH.  The message at the cursor becomes current."
  565.   (interactive)
  566.   ;; mh-update-sequences is the opposite of mh-read-folder-sequences,
  567.   ;; which updates mh-e's state from MH.
  568.   (let ((folder-set (mh-update-unseen))
  569.     (new-cur (mh-get-msg-num nil)))
  570.     (if new-cur
  571.     (let ((seq-entry (mh-find-seq 'cur)))
  572.       (mh-remove-cur-notation)
  573.       (setcdr seq-entry (list new-cur)) ;delete-seq-locally, add-msgs-to-seq
  574.       (mh-define-sequence 'cur (list new-cur))
  575.       (beginning-of-line)
  576.       (if (looking-at mh-good-msg-regexp)
  577.           (mh-notate nil mh-note-cur mh-cmd-note)))
  578.       (or folder-set
  579.       (save-excursion
  580.         (mh-exec-cmd-quiet t "folder" mh-current-folder "-fast"))))))
  581.  
  582.  
  583.  
  584.  
  585. ;;; Support routines.
  586.  
  587. (defun mh-delete-a-msg (msg)
  588.   ;; Delete the MESSAGE.
  589.   (save-excursion
  590.     (mh-goto-msg msg nil t)
  591.     (if (looking-at mh-refiled-msg-regexp)
  592.     (error "Message %d is refiled.  Undo refile before deleting." msg))
  593.     (if (looking-at mh-deleted-msg-regexp)
  594.     nil
  595.     (mh-set-folder-modified-p t)
  596.     (setq mh-delete-list (cons msg mh-delete-list))
  597.     (mh-add-msgs-to-seq msg 'deleted t)
  598.     (mh-notate msg mh-note-deleted mh-cmd-note)
  599.     (run-hooks 'mh-delete-msg-hook))))
  600.  
  601. (defun mh-refile-a-msg (msg destination)
  602.   ;; Refile MESSAGE in FOLDER.  FOLDER is a symbol, not a string.
  603.   (save-excursion
  604.     (mh-goto-msg msg nil t)
  605.     (cond ((looking-at mh-deleted-msg-regexp)
  606.        (error "Message %d is deleted.  Undo delete before moving." msg))
  607.       ((looking-at mh-refiled-msg-regexp)
  608.        (if (y-or-n-p
  609.         (format "Message %d already refiled.  Copy to %s as well? "
  610.             msg destination))
  611.            (mh-exec-cmd "refile" (mh-get-msg-num t) "-link"
  612.                 "-src" mh-current-folder
  613.                 (symbol-name destination))
  614.            (message "Message not copied.")))
  615.       (t
  616.        (mh-set-folder-modified-p t)
  617.        (if (not (memq destination mh-refile-list))
  618.            (setq mh-refile-list (cons destination mh-refile-list)))
  619.        (if (not (memq msg (mh-seq-to-msgs destination)))
  620.            (mh-add-msgs-to-seq msg destination t))
  621.        (mh-notate msg mh-note-refiled mh-cmd-note)
  622.        (run-hooks 'mh-refile-msg-hook)))))
  623.  
  624.  
  625. (defun mh-next-msg ()
  626.   ;; Move backward or forward to the next undeleted message in the buffer.
  627.   (if (eq mh-next-direction 'forward)
  628.       (mh-next-undeleted-msg 1)
  629.       (mh-previous-undeleted-msg 1)))
  630.  
  631.  
  632. (defun mh-set-scan-mode ()
  633.   ;; Display the scan listing buffer, but do not show a message.
  634.   (if (get-buffer mh-show-buffer)
  635.       (delete-windows-on mh-show-buffer))
  636.   (setq mh-showing nil)
  637.   (force-mode-line-update)
  638.   (if mh-recenter-summary-p
  639.       (mh-recenter nil)))
  640.  
  641.  
  642. (defun mh-undo-msg (msg)
  643.   ;; Undo the deletion or refile of one MESSAGE.
  644.   (cond ((memq msg mh-delete-list)
  645.      (setq mh-delete-list (delq msg mh-delete-list))
  646.      (mh-delete-msg-from-seq msg 'deleted t))
  647.     (t
  648.      (mh-mapc (function (lambda (dest)
  649.                   (mh-delete-msg-from-seq msg dest t)))
  650.           mh-refile-list)))
  651.   (mh-notate msg ?  mh-cmd-note))
  652.  
  653.  
  654.  
  655.  
  656. ;;; The folder data abstraction.
  657.  
  658. (defun mh-make-folder (name)
  659.   ;; Create and initialize a new mail folder called NAME and make it the
  660.   ;; current folder.
  661.   (switch-to-buffer name)
  662.   (setq buffer-read-only nil)
  663.   (erase-buffer)
  664.   (setq buffer-read-only t)
  665.   (mh-folder-mode)
  666.   (mh-set-folder-modified-p nil)
  667.   (setq buffer-file-name mh-folder-filename)
  668.   (mh-make-folder-mode-line))
  669.  
  670.  
  671. ;;; Ensure new buffers won't get this mode if default-major-mode is nil.
  672. (put 'mh-folder-mode 'mode-class 'special)
  673.  
  674. (defun mh-folder-mode ()
  675.   "Major mh-e mode for \"editing\" an MH folder scan listing.\\<mh-folder-mode-map>
  676. You can show the message the cursor is pointing to, and step through the
  677. messages.  Messages can be marked for deletion or refiling into another
  678. folder; these commands are executed all at once with a separate command.
  679.  
  680. A prefix argument (\\[universal-argument]) to delete, refile, list, or undo
  681. applies the action to a message sequence.
  682.  
  683. Here is a list of the standard keys for mh-e commands, grouped by function.
  684. This list is purposefully not customized; mh-e has a long history, and many
  685. alternate key bindings as a result.  This list is to encourage users to use
  686. standard keys so the other keys can perhaps someday be put to new uses.
  687.  
  688. t    toggle show or scan-only mode
  689. RET    show message, or back to top if already showing
  690.  
  691. SPC    page message forward
  692. DEL    page message back
  693.  
  694. n    next message
  695. p    previous message
  696. g    go to message by number
  697.  
  698. d    mark for deletion
  699. o, ^    mark for output (refile) to another folder
  700. ?    show folder of pending refile
  701. u    undo delete or refile marking
  702.  
  703. x    execute marked deletes and refiles
  704. i    incorporate new mail
  705.  
  706. m    mail a new message
  707. r    reply to a message
  708. f    forward a message
  709.  
  710. q    quit mh-e
  711.  
  712. M-f    visit new folder
  713. M-r    rescan this folder
  714.  
  715. Here are all the commands with their current binding, listed in key order:
  716. \\{mh-folder-mode-map}
  717.  
  718. Variables controlling mh-e operation are (defaults in parentheses):
  719.  
  720.  mh-recursive-folders (nil)
  721.     Non-nil means commands which operate on folders do so recursively.
  722.  
  723.  mh-bury-show-buffer (t)
  724.     Non-nil means that the buffer used to display message is buried.
  725.     It will never be offered as the default other buffer.
  726.  
  727.  mh-clean-message-header (nil)
  728.     Non-nil means remove header lines matching the regular expression
  729.     specified in mh-invisible-headers from messages.
  730.  
  731.  mh-visible-headers (nil)
  732.     If non-nil, it contains a regexp specifying the headers that are shown in
  733.     a message if mh-clean-message-header is non-nil.  Setting this variable
  734.     overrides mh-invisible-headers.
  735.  
  736.  mh-do-not-confirm (nil)
  737.     Non-nil means do not prompt for confirmation before executing some
  738.     non-recoverable commands such as mh-kill-folder and mh-undo-folder.
  739.  
  740.  mhl-formfile (nil)
  741.     Name of format file to be used by mhl to show messages.
  742.     A value of T means use the default format file.
  743.     Nil means don't use mhl to format messages.
  744.  
  745.  mh-lpr-command-format (\"lpr -p -J '%s'\")
  746.     Format for command used to print a message on a system printer.
  747.  
  748.  mh-scan-prog (\"scan\")
  749.     Program to run to generate one-line-per-message listing of a folder.
  750.     Normally \"scan\" or a file name linked to scan.  This file is searched
  751.     for relative to the mh-progs directory unless it is an absolute pathname.
  752.     Automatically becomes buffer-local when set in any fashion.
  753.  
  754.  mh-print-background (nil)
  755.     Print messages in the background if non-nil.
  756.     WARNING: do not delete the messages until printing is finished;
  757.     otherwise, your output may be truncated.
  758.  
  759.  mh-recenter-summary-p (nil)
  760.     If non-nil, then the scan listing is recentered when the window displaying
  761.     a messages is toggled off.
  762.  
  763.  mh-summary-height (4)
  764.     Number of lines in the summary window including the mode line.
  765.  
  766. The value of mh-folder-mode-hook is called when a new folder is set up."
  767.  
  768.   (kill-all-local-variables)
  769.   (use-local-map mh-folder-mode-map)
  770.   (setq major-mode 'mh-folder-mode)
  771.   (mh-set-mode-name "MH-Folder")
  772.   (mh-make-local-vars
  773.    'mh-current-folder (buffer-name)    ; Name of folder, a string
  774.    'mh-show-buffer (format "show-%s" (buffer-name)) ; Buffer that displays msgs
  775.    'mh-folder-filename            ; e.g. "/usr/foobar/Mail/inbox/"
  776.    (file-name-as-directory (mh-expand-file-name (buffer-name)))
  777.    'mh-showing nil            ; Show message also?
  778.    'mh-delete-list nil            ; List of msgs nums to delete
  779.    'mh-refile-list nil            ; List of folder names in mh-seq-list
  780.    'mh-seq-list nil            ; Alist of (seq . msgs) nums
  781.    'mh-seen-list nil            ; List of displayed messages
  782.    'mh-next-direction 'forward        ; Direction to move to next message
  783.    'mh-narrowed-to-seq nil        ; Sequence display is narrowed to
  784.    'mh-first-msg-num nil        ; Number of first msg in buffer
  785.    'mh-last-msg-num nil            ; Number of last msg in buffer
  786.    'mh-msg-count nil            ; Number of msgs in buffer
  787.    'mh-mode-line-annotation nil        ; Indiction this is not the full folder
  788.    'mh-previous-window-config nil)    ; Previous window configuration
  789.   (setq truncate-lines t)
  790.   (auto-save-mode -1)
  791.   (setq buffer-offer-save t)
  792.   (if (boundp 'local-write-file-hooks)
  793.       (setq local-write-file-hooks '(mh-execute-commands)) ;Emacs 19
  794.     (make-local-variable 'write-file-hooks)
  795.     (setq write-file-hooks '(mh-execute-commands))) ;Emacs 18
  796.   (make-local-variable 'revert-buffer-function)
  797.   (setq revert-buffer-function 'mh-undo-folder)
  798.   (or (assq 'mh-showing minor-mode-alist)
  799.       (setq minor-mode-alist
  800.         (cons '(mh-showing " Show") minor-mode-alist)))
  801.   (run-hooks 'mh-folder-mode-hook))
  802.  
  803.  
  804. (defun mh-make-local-vars (&rest pairs)
  805.   ;; Take VARIABLE-VALUE pairs and make local variables initialized to the
  806.   ;; value.
  807.   (while pairs
  808.     (make-variable-buffer-local (car pairs))
  809.     (set (car pairs) (car (cdr pairs)))
  810.     (setq pairs (cdr (cdr pairs)))))
  811.  
  812.  
  813. (defun mh-scan-folder (folder range)
  814.   ;; Scan the FOLDER over the RANGE.  Return in the folder's buffer.
  815.   (cond ((null (get-buffer folder))
  816.      (mh-make-folder folder))
  817.     (t
  818.      (mh-process-or-undo-commands folder)
  819.      (switch-to-buffer folder)))
  820.   (mh-regenerate-headers range)
  821.   (cond ((zerop (buffer-size))
  822.      (if (equal range "all")
  823.          (message "Folder %s is empty" folder)
  824.        (message "No messages in %s, range %s" folder range))
  825.      (sit-for 5)))
  826.   (mh-goto-cur-msg))
  827.  
  828.  
  829. (defun mh-regenerate-headers (range &optional update)
  830.   ;; scan folder over range RANGE.
  831.   ;; If UPDATE, append the scan lines, otherwise replace.
  832.   (let ((folder mh-current-folder)
  833.     scan-start)
  834.     (message "Scanning %s..." folder)
  835.     (with-mh-folder-updating (nil)
  836.       (if update
  837.       (goto-char (point-max))
  838.     (erase-buffer))
  839.       (setq scan-start (point))
  840.       (mh-exec-cmd-output mh-scan-prog nil
  841.               "-noclear" "-noheader"
  842.               "-width" (window-width)
  843.               folder range)
  844.       (goto-char scan-start)
  845.       (cond ((looking-at "scan: no messages in")
  846.          (keep-lines mh-valid-scan-line)) ; Flush random scan lines
  847.         ((looking-at "scan: "))    ; Keep error messages
  848.         (t
  849.          (keep-lines mh-valid-scan-line))) ; Flush random scan lines
  850.       (setq mh-seq-list (mh-read-folder-sequences folder nil))
  851.       (mh-notate-user-sequences)
  852.       (or update
  853.       (setq mh-mode-line-annotation
  854.         (if (equal range "all")
  855.             nil
  856.           mh-partial-folder-mode-line-annotation)))
  857.       (mh-make-folder-mode-line))
  858.     (message "Scanning %s...done" folder)))
  859.  
  860.  
  861. (defun mh-get-new-mail (maildrop-name)
  862.   ;; Read new mail from a maildrop into the current buffer.
  863.   ;; Return in the current buffer.
  864.   (let ((point-before-inc (point))
  865.     (folder mh-current-folder)
  866.     (new-mail-p nil))
  867.     (with-mh-folder-updating (t)
  868.       (if maildrop-name
  869.       (message "inc %s -file %s..." folder maildrop-name)
  870.     (message "inc %s..." folder))
  871.       (setq mh-next-direction 'forward)
  872.       (goto-char (point-max))
  873.       (let ((start-of-inc (point)))
  874.     (if maildrop-name
  875.         ;; I think MH 5 used "-ms-file" instead of "-file",
  876.         ;; which would make inc'ing from maildrops fail.
  877.         (mh-exec-cmd-output mh-inc-prog nil folder
  878.                 "-file" (expand-file-name maildrop-name)
  879.                 "-width" (window-width)
  880.                 "-truncate")
  881.         (mh-exec-cmd-output mh-inc-prog nil
  882.                 "-width" (window-width)))
  883.     (if maildrop-name
  884.         (message "inc %s -file %s...done" folder maildrop-name)
  885.       (message "inc %s...done" folder))
  886.     (goto-char start-of-inc)
  887.     (cond ((save-excursion
  888.          (re-search-forward "^inc: no mail" nil t))
  889.            (message "No new mail%s%s" (if maildrop-name " in " "")
  890.             (if maildrop-name maildrop-name "")))
  891.           ((re-search-forward "^inc:" nil t) ; Error messages
  892.            (error "inc error"))
  893.           (t
  894.            (mh-remove-cur-notation)
  895.            (setq new-mail-p t)))
  896.     (keep-lines mh-valid-scan-line) ; Flush random scan lines
  897.     (setq mh-seq-list (mh-read-folder-sequences folder t))
  898.     (mh-notate-user-sequences)
  899.     (if new-mail-p
  900.         (progn
  901.           (mh-make-folder-mode-line)
  902.           (mh-goto-cur-msg))
  903.         (goto-char point-before-inc))))))
  904.  
  905.  
  906. (defun mh-make-folder-mode-line (&optional ignored)
  907.   ;; Set the fields of the mode line for a folder buffer.
  908.   ;; The optional argument is now obsolete.  It used to be used to pass
  909.   ;; in what is now stored in the buffer-local variable
  910.   ;; mh-mode-line-annotation.
  911.   (save-excursion
  912.     (mh-first-msg)
  913.     (setq mh-first-msg-num (mh-get-msg-num nil))
  914.     (mh-last-msg)
  915.     (setq mh-last-msg-num (mh-get-msg-num nil))
  916.     (setq mh-msg-count (count-lines (point-min) (point-max)))
  917.     (setq mode-line-buffer-identification
  918.       (list (format "{%%b%s} %d msg%s"
  919.             (if mh-mode-line-annotation
  920.                 (format "/%s" mh-mode-line-annotation)
  921.               "")
  922.             mh-msg-count
  923.             (if (zerop mh-msg-count)
  924.                 "s"
  925.               (if (> mh-msg-count 1)
  926.                   (format "s (%d-%d)" mh-first-msg-num
  927.                       mh-last-msg-num)
  928.                 (format " (%d)" mh-first-msg-num))))))))
  929.  
  930.  
  931. (defun mh-unmark-all-headers (remove-all-flags)
  932.   ;; Remove all '+' flags from the headers, and if called with a non-nil
  933.   ;; argument, remove all 'D', '^' and '%' flags too.
  934.   ;; Optimized for speed (i.e., no regular expressions).
  935.   (save-excursion
  936.     (let ((case-fold-search nil)
  937.       (last-line (1- (point-max)))
  938.       char)
  939.       (mh-first-msg)
  940.       (while (<= (point) last-line)
  941.     (forward-char mh-cmd-note)
  942.     (setq char (following-char))
  943.     (if (or (and remove-all-flags
  944.              (or (eql char (aref mh-note-deleted 0))
  945.              (eql char (aref mh-note-refiled 0))))
  946.         (eql char (aref mh-note-cur 0)))
  947.         (progn
  948.           (delete-char 1)
  949.           (insert " ")))
  950.     (if remove-all-flags
  951.         (progn
  952.           (forward-char 1)
  953.           (if (eql (following-char) (aref mh-note-seq 0))
  954.           (progn
  955.             (delete-char 1)
  956.             (insert " ")))))
  957.     (forward-line)))))
  958.  
  959.  
  960. (defun mh-remove-cur-notation ()
  961.   ;; Remove old cur notation (cf mh-goto-cur-msg code).
  962.   (let ((cur-msg (car (mh-seq-to-msgs 'cur))))
  963.     (save-excursion
  964.       (and cur-msg
  965.        (mh-goto-msg cur-msg t t)
  966.        (looking-at mh-cur-scan-msg-regexp)
  967.        (mh-notate nil ?  mh-cmd-note)))))
  968.  
  969. (defun mh-goto-cur-msg ()
  970.   ;; Position the cursor at the current message.
  971.   (let ((cur-msg (car (mh-seq-to-msgs 'cur))))
  972.     (cond ((and cur-msg
  973.         (mh-goto-msg cur-msg t t))
  974.        (mh-notate nil mh-note-cur mh-cmd-note)
  975.        (mh-recenter 0)
  976.        (mh-maybe-show cur-msg))
  977.       (t
  978.        (mh-last-msg)
  979.        (message "No current message")))))
  980.  
  981.  
  982. (defun mh-process-or-undo-commands (folder)
  983.   ;; If FOLDER has outstanding commands, then either process or discard them.
  984.   ;; Called by functions like mh-sort-folder, so also invalidate show buffer.
  985.   (set-buffer folder)
  986.   (if (mh-outstanding-commands-p)
  987.       (if (or mh-do-not-confirm
  988.           (y-or-n-p
  989.         "Process outstanding deletes and refiles (or lose them)? "))
  990.       (mh-process-commands folder)
  991.       (mh-undo-folder)))
  992.   (mh-update-unseen)
  993.   (mh-invalidate-show-buffer))
  994.  
  995.  
  996. (defun mh-process-commands (folder)
  997.   ;; Process outstanding commands for the folder FOLDER.
  998.   (message "Processing deletes and refiles for %s..." folder)
  999.   (set-buffer folder)
  1000.   (with-mh-folder-updating (nil)
  1001.     ;; Update the unseen sequence if it exists
  1002.     (mh-update-unseen)
  1003.  
  1004.     ;; Then refile messages
  1005.     (mh-mapc
  1006.      (function
  1007.       (lambda (dest)
  1008.     (let ((msgs (mh-seq-to-msgs dest)))
  1009.       (cond (msgs
  1010.          (apply 'mh-exec-cmd "refile"
  1011.             "-src" folder (symbol-name dest)
  1012.             (mh-coalesce-msg-list msgs))
  1013.          (mh-delete-scan-msgs msgs))))))
  1014.      mh-refile-list)
  1015.     (setq mh-refile-list nil)
  1016.  
  1017.     ;; Now delete messages
  1018.     (cond (mh-delete-list
  1019.        (apply 'mh-exec-cmd "rmm" folder
  1020.           (mh-coalesce-msg-list mh-delete-list))
  1021.        (mh-delete-scan-msgs mh-delete-list)
  1022.        (setq mh-delete-list nil)))
  1023.  
  1024.     ;; Don't need to remove sequences since delete and refile do so.
  1025.  
  1026.     ;; Mark cur message
  1027.     (if (> (buffer-size) 0)
  1028.     (mh-define-sequence 'cur (list (or (mh-get-msg-num nil) "last"))))
  1029.  
  1030.     (and (buffer-file-name (get-buffer mh-show-buffer))
  1031.      (not (file-exists-p (buffer-file-name (get-buffer mh-show-buffer))))
  1032.      ;; If "inc" were to put a new msg in this file,
  1033.      ;; we would not notice, so mark it invalid now.
  1034.      (mh-invalidate-show-buffer))
  1035.  
  1036.     (setq mh-seq-list (mh-read-folder-sequences mh-current-folder nil))
  1037.     (mh-unmark-all-headers t)
  1038.     (mh-notate-user-sequences)
  1039.     (message "Processing deletes and refiles for %s...done" folder)))
  1040.  
  1041.  
  1042. (defun mh-update-unseen ()
  1043.   ;; Flush updates to the Unseen sequence out to MH.
  1044.   ;; Return non-NIL iff set the MH folder.
  1045.   (if mh-seen-list
  1046.       (let* ((unseen-seq (mh-find-seq mh-unseen-seq))
  1047.          (unseen-msgs (mh-seq-msgs unseen-seq)))
  1048.     (if unseen-msgs
  1049.         (progn
  1050.           (mh-undefine-sequence mh-unseen-seq mh-seen-list)
  1051.           (while mh-seen-list
  1052.         (setq unseen-msgs (delq (car mh-seen-list) unseen-msgs))
  1053.         (setq mh-seen-list (cdr mh-seen-list)))
  1054.           (setcdr unseen-seq unseen-msgs)
  1055.           t)            ;since we set the folder
  1056.       (setq mh-seen-list nil)))))
  1057.  
  1058.  
  1059. (defun mh-delete-scan-msgs (msgs)
  1060.   ;; Delete the scan listing lines for each of the msgs in the LIST.
  1061.   (save-excursion
  1062.     (while msgs
  1063.       (if (mh-goto-msg (car msgs) t t)
  1064.       (mh-delete-line 1))
  1065.       (setq msgs (cdr msgs)))))
  1066.  
  1067.  
  1068. (defun mh-outstanding-commands-p ()
  1069.   ;; Returns non-nil if there are outstanding deletes or refiles.
  1070.   (or mh-delete-list mh-refile-list))
  1071.  
  1072.  
  1073. (defun mh-coalesce-msg-list (messages)
  1074.   ;; Give a list of MESSAGES, return a list of message number ranges.
  1075.   ;; Sort of the opposite of mh-read-msg-list, which expands ranges.
  1076.   ;; Message lists passed to MH programs go through this so
  1077.   ;; command line arguments won't exceed system limits.
  1078.   (let ((msgs (sort (copy-sequence messages) 'mh-greaterp))
  1079.     (range-high nil)
  1080.     (prev -1)
  1081.     (ranges nil))
  1082.     (while prev
  1083.       (if range-high
  1084.       (if (or (not (numberp prev))
  1085.           (not (eql (car msgs) (1- prev))))
  1086.           (progn            ;non-sequential, flush old range
  1087.         (if (eql prev range-high)
  1088.             (setq ranges (cons range-high ranges))
  1089.           (setq ranges (cons (format "%s-%s" prev range-high) ranges)))
  1090.         (setq range-high nil))))
  1091.       (or range-high
  1092.       (setq range-high (car msgs))) ;start new or first range
  1093.       (setq prev (car msgs))
  1094.       (setq msgs (cdr msgs)))
  1095.     ranges))
  1096.  
  1097. (defun mh-greaterp (msg1 msg2)
  1098.   ;; Sort two message indicators.  Strings are "smaller" than numbers.
  1099.   ;; Legal values are things like "cur", "last", 1, and 1820.
  1100.   (if (numberp msg1)
  1101.      (if (numberp msg2)
  1102.          (> msg1 msg2)
  1103.        t)
  1104.     (if (numberp msg2)
  1105.     nil
  1106.       (string-lessp msg2 msg1))))
  1107.  
  1108.  
  1109.  
  1110. ;;; Basic sequence handling
  1111.  
  1112. (defun mh-delete-seq-locally (seq)
  1113.   ;; Remove mh-e's record of SEQUENCE.
  1114.   (let ((entry (mh-find-seq seq)))
  1115.     (setq mh-seq-list (delq entry mh-seq-list))))
  1116.  
  1117. (defun mh-read-folder-sequences (folder save-refiles)
  1118.   ;; Read and return the predefined sequences for a FOLDER.
  1119.   ;; If SAVE-REFILES is non-nil, then keep the sequences
  1120.   ;; that note messages to be refiled.
  1121.   (let ((seqs ()))
  1122.     (cond (save-refiles
  1123.         (mh-mapc (function (lambda (seq) ; Save the refiling sequences
  1124.                  (if (mh-folder-name-p (mh-seq-name seq))
  1125.                      (setq seqs (cons seq seqs)))))
  1126.              mh-seq-list)))
  1127.     (save-excursion
  1128.       (if (eq 0 (mh-exec-cmd-quiet nil "mark" folder "-list"))
  1129.       (progn
  1130.         ;; look for name in line of form "cur: 4" or "myseq (private): 23"
  1131.         (while (re-search-forward "^[^: ]+" nil t)
  1132.           (setq seqs (cons (mh-make-seq (intern (buffer-substring
  1133.                              (match-beginning 0)
  1134.                              (match-end 0)))
  1135.                         (mh-read-msg-list))
  1136.                    seqs)))
  1137.         (delete-region (point-min) (point))))) ; avoid race with mh-process-daemon
  1138.     seqs))
  1139.  
  1140. (defun mh-read-msg-list ()
  1141.   ;; Return a list of message numbers from the current point to the end of
  1142.   ;; the line.  Expands ranges into set of individual numbers.
  1143.   (let ((msgs ())
  1144.     (end-of-line (save-excursion (end-of-line) (point)))
  1145.     num)
  1146.     (while (re-search-forward "[0-9]+" end-of-line t)
  1147.       (setq num (string-to-int (buffer-substring (match-beginning 0)
  1148.                          (match-end 0))))
  1149.       (cond ((looking-at "-")        ; Message range
  1150.          (forward-char 1)
  1151.          (re-search-forward "[0-9]+" end-of-line t)
  1152.          (let ((num2 (string-to-int (buffer-substring (match-beginning 0)
  1153.                               (match-end 0)))))
  1154.            (if (< num2 num)
  1155.            (error "Bad message range: %d-%d" num num2))
  1156.            (while (<= num num2)
  1157.          (setq msgs (cons num msgs))
  1158.          (setq num (1+ num)))))
  1159.         ((not (zerop num))        ;"pick" outputs "0" to mean no match
  1160.          (setq msgs (cons num msgs)))))
  1161.     msgs))
  1162.  
  1163. (defun mh-notate-user-sequences ()
  1164.   ;; Mark the scan listing of all messages in user-defined sequences.
  1165.   (let ((seqs mh-seq-list)
  1166.     name)
  1167.     (while seqs
  1168.       (setq name (mh-seq-name (car seqs)))
  1169.       (if (not (mh-internal-seq name))
  1170.       (mh-notate-seq name mh-note-seq (1+ mh-cmd-note)))
  1171.       (setq seqs (cdr seqs)))))
  1172.  
  1173.  
  1174. (defun mh-internal-seq (name)
  1175.   ;; Return non-NIL if NAME is the name of an internal mh-e sequence.
  1176.   (or (memq name '(answered cur deleted forwarded printed))
  1177.       (eq name mh-unseen-seq)
  1178.       (eq name mh-previous-seq)
  1179.       (mh-folder-name-p name)))
  1180.  
  1181.  
  1182. (defun mh-delete-msg-from-seq (message sequence &optional internal-flag)
  1183.   "Delete MESSAGE from SEQUENCE.  MESSAGE defaults to displayed message.
  1184. From Lisp, optional third arg INTERNAL-FLAG non-nil means do not
  1185. inform MH of the change."
  1186.   (interactive (list (mh-get-msg-num t)
  1187.              (mh-read-seq-default "Delete from" t)
  1188.              nil))
  1189.   (let ((entry (mh-find-seq sequence)))
  1190.     (cond (entry
  1191.        (mh-notate-if-in-one-seq message ?  (1+ mh-cmd-note) sequence)
  1192.        (if (not internal-flag)
  1193.            (mh-undefine-sequence sequence (list message)))
  1194.        (setcdr entry (delq message (mh-seq-msgs entry)))))))
  1195.  
  1196.  
  1197. (defun mh-undefine-sequence (seq msgs)
  1198.   ;; Remove from the SEQUENCE the list of MSGS.
  1199.   (mh-exec-cmd "mark" mh-current-folder "-delete"
  1200.            "-sequence" (symbol-name seq)
  1201.            (mh-coalesce-msg-list msgs)))
  1202.  
  1203.  
  1204. (defun mh-define-sequence (seq msgs)
  1205.   ;; Define the SEQUENCE to contain the list of MSGS.
  1206.   ;; Do not mark pseudo-sequences or empty sequences.
  1207.   ;; Signals an error if SEQUENCE is an illegal name.
  1208.   (if (and msgs
  1209.        (not (mh-folder-name-p seq)))
  1210.       (save-excursion
  1211.     (mh-exec-cmd-error nil "mark" mh-current-folder "-add" "-zero"
  1212.                "-sequence" (symbol-name seq)
  1213.                (mh-coalesce-msg-list msgs)))))
  1214.  
  1215.  
  1216. (defun mh-map-over-seqs (func seq-list)
  1217.   ;; Apply the FUNCTION to each element in the list of SEQUENCES,
  1218.   ;; passing the sequence name and the list of messages as arguments.
  1219.   (while seq-list
  1220.     (funcall func (mh-seq-name (car seq-list)) (mh-seq-msgs (car seq-list)))
  1221.     (setq seq-list (cdr seq-list))))
  1222.  
  1223.  
  1224. (defun mh-notate-if-in-one-seq (msg notation offset seq)
  1225.   ;; If the MESSAGE is in only the SEQUENCE, then mark the scan listing of the
  1226.   ;; message with the CHARACTER at the given OFFSET from the beginning of the
  1227.   ;; listing line.
  1228.   (let ((in-seqs (mh-seq-containing-msg msg nil)))
  1229.     (if (and (eq seq (car in-seqs)) (null (cdr in-seqs)))
  1230.     (mh-notate msg notation offset))))
  1231.  
  1232.  
  1233. (defun mh-seq-containing-msg (msg &optional include-internal-p)
  1234.   ;; Return a list of the sequences containing MESSAGE.
  1235.   ;; If INCLUDE-INTERNAL-P non-nil, include mh-e internal sequences in list.
  1236.   (let ((l mh-seq-list)
  1237.     (seqs ()))
  1238.     (while l
  1239.       (and (memq msg (mh-seq-msgs (car l)))
  1240.        (or include-internal-p
  1241.            (not (mh-internal-seq (mh-seq-name (car l)))))
  1242.        (setq seqs (cons (mh-seq-name (car l)) seqs)))
  1243.       (setq l (cdr l)))
  1244.     seqs))
  1245.  
  1246.  
  1247.  
  1248.  
  1249. ;;; User prompting commands.
  1250.  
  1251.  
  1252. (defun mh-read-msg-range (prompt)
  1253.   ;; Read a list of blank-separated items.
  1254.   (let* ((buf (read-string prompt))
  1255.      (buf-size (length buf))
  1256.      (start 0)
  1257.      (input ()))
  1258.     (while (< start buf-size)
  1259.       (let ((next (read-from-string buf start buf-size)))
  1260.     (setq input (cons (car next) input))
  1261.     (setq start (cdr next))))
  1262.     (nreverse input)))
  1263.  
  1264.  
  1265.  
  1266. ;;; Build the folder-mode keymap:
  1267.  
  1268. (suppress-keymap mh-folder-mode-map)
  1269. (define-key mh-folder-mode-map "q" 'mh-quit)
  1270. (define-key mh-folder-mode-map "b" 'mh-compat-quit)
  1271. (define-key mh-folder-mode-map "?" 'mh-msg-is-in-seq)
  1272. (define-key mh-folder-mode-map "%" 'mh-put-msg-in-seq)
  1273. (define-key mh-folder-mode-map "|" 'mh-pipe-msg)
  1274. (define-key mh-folder-mode-map "\ea" 'mh-edit-again)
  1275. (define-key mh-folder-mode-map "\e%" 'mh-delete-msg-from-seq)
  1276. (define-key mh-folder-mode-map "\e#" 'mh-delete-seq)
  1277. (define-key mh-folder-mode-map "\C-xn" 'mh-narrow-to-seq)
  1278. (define-key mh-folder-mode-map "\C-xw" 'mh-widen)
  1279. (define-key mh-folder-mode-map "\eb" 'mh-burst-digest)
  1280. (define-key mh-folder-mode-map "\eu" 'mh-undo-folder)
  1281. (define-key mh-folder-mode-map "\e " 'mh-page-digest)
  1282. (define-key mh-folder-mode-map "\e\177" 'mh-page-digest-backwards)
  1283. (define-key mh-folder-mode-map "\ed" 'mh-redistribute)
  1284. (define-key mh-folder-mode-map "\ee" 'mh-extract-rejected-mail)
  1285. (define-key mh-folder-mode-map "\ef" 'mh-visit-folder)
  1286. (define-key mh-folder-mode-map "\ek" 'mh-kill-folder)
  1287. (define-key mh-folder-mode-map "\el" 'mh-list-folders)
  1288. (define-key mh-folder-mode-map "\en" 'mh-store-msg)
  1289. (define-key mh-folder-mode-map "\ep" 'mh-pack-folder)
  1290. (define-key mh-folder-mode-map "\eq" 'mh-list-sequences)
  1291. (define-key mh-folder-mode-map "\es" 'mh-search-folder)
  1292. (define-key mh-folder-mode-map "\er" 'mh-rescan-folder)
  1293. (define-key mh-folder-mode-map "l" 'mh-print-msg)
  1294. (define-key mh-folder-mode-map "t" 'mh-toggle-showing)
  1295. (define-key mh-folder-mode-map "c" 'mh-copy-msg)
  1296. (define-key mh-folder-mode-map "i" 'mh-inc-folder)
  1297. (define-key mh-folder-mode-map "x" 'mh-execute-commands)
  1298. (define-key mh-folder-mode-map "e" 'mh-execute-commands)
  1299. (define-key mh-folder-mode-map "f" 'mh-forward)
  1300. (define-key mh-folder-mode-map "m" 'mh-send)
  1301. (define-key mh-folder-mode-map "s" 'mh-send)
  1302. (define-key mh-folder-mode-map "r" 'mh-reply)
  1303. (define-key mh-folder-mode-map "a" 'mh-reply)
  1304. (define-key mh-folder-mode-map "j" 'mh-goto-msg)
  1305. (define-key mh-folder-mode-map "g" 'mh-goto-msg)
  1306. (define-key mh-folder-mode-map "\e<" 'mh-first-msg)
  1307. (define-key mh-folder-mode-map "\e>" 'mh-last-msg)
  1308. (define-key mh-folder-mode-map "\177" 'mh-previous-page)
  1309. (define-key mh-folder-mode-map " " 'mh-page-msg)
  1310. (define-key mh-folder-mode-map "\r" 'mh-show)
  1311. (define-key mh-folder-mode-map "." 'mh-show)
  1312. (define-key mh-folder-mode-map "," 'mh-header-display)
  1313. (define-key mh-folder-mode-map "u" 'mh-undo)
  1314. (define-key mh-folder-mode-map "d" 'mh-delete-msg)
  1315. (define-key mh-folder-mode-map "\C-d" 'mh-delete-msg-no-motion)
  1316. (define-key mh-folder-mode-map "p" 'mh-previous-undeleted-msg)
  1317. (define-key mh-folder-mode-map "n" 'mh-next-undeleted-msg)
  1318. (define-key mh-folder-mode-map "o" 'mh-refile-msg)
  1319. (define-key mh-folder-mode-map "^" 'mh-refile-msg)
  1320. (define-key mh-folder-mode-map "\C-o" 'mh-write-msg-to-file)
  1321. (define-key mh-folder-mode-map ">" 'mh-write-msg-to-file)
  1322. (define-key mh-folder-mode-map "!" 'mh-refile-or-write-again)
  1323.  
  1324. ;; "C-c /" prefix is used in mh-folder-mode by pgp.el and mailcrypt
  1325.  
  1326.  
  1327.  
  1328. ;;;autoload the other mh-e parts
  1329.  
  1330. ;;; mh-comp
  1331.  
  1332. (autoload 'mh-smail "mh-comp"
  1333.   "Compose and send mail with the MH mail system.
  1334. This function is an entry point to mh-e, the Emacs front end
  1335. to the MH mail system.
  1336. See documentation of `\\[mh-send]' for more details on composing mail." t)
  1337.  
  1338. (autoload 'mh-smail-other-window "mh-comp"
  1339.   "Compose and send mail in other window with the MH mail system.
  1340. This function is an entry point to mh-e, the Emacs front end
  1341. to the MH mail system.
  1342. See documentation of `\\[mh-send]' for more details on composing mail." t)
  1343.  
  1344. (autoload 'mh-edit-again "mh-comp"
  1345.   "Clean-up a draft or a message previously sent and make it resendable.
  1346. Default is the current message.
  1347. The variable mh-new-draft-cleaned-headers specifies the headers to remove.
  1348. See also documentation for `\\[mh-send]' function." t)
  1349.  
  1350. (autoload 'mh-extract-rejected-mail "mh-comp"
  1351.   "Extract a letter returned by the mail system and make it resendable.
  1352. Default is the current message.  The variable mh-new-draft-cleaned-headers
  1353. gives the headers to clean out of the original message.
  1354. See also documentation for `\\[mh-send]' function." t)
  1355.  
  1356. (autoload 'mh-forward "mh-comp"
  1357.   "Forward a message or message sequence.  Defaults to displayed message.
  1358. If optional prefix argument provided, then prompt for the message sequence.
  1359. See also documentation for `\\[mh-send]' function." t)
  1360.  
  1361. (autoload 'mh-redistribute "mh-comp"
  1362.   "Redistribute a letter.
  1363. Depending on how your copy of MH was compiled, you may need to change the
  1364. setting of the variable mh-redist-full-contents.  See its documentation." t)
  1365.  
  1366. (autoload 'mh-reply "mh-comp"
  1367.   "Reply to a MESSAGE (default: displayed message).
  1368. If optional prefix argument INCLUDEP provided, then include the message
  1369. in the reply using filter mhl.reply in your MH directory.
  1370. Prompts for type of addresses to reply to:
  1371.    from    sender only,
  1372.    to      sender and primary recipients,
  1373.    cc/all  sender and all recipients.
  1374. If the file named by `mh-repl-formfile' exists, it is used as a skeleton
  1375. for the reply.  See also documentation for `\\[mh-send]' function." t)
  1376.  
  1377. (autoload 'mh-send "mh-comp"
  1378.   "Compose and send a letter.
  1379. The file named by `mh-comp-formfile' will be used as the form.
  1380. Do not call this function from outside mh-e; use \\[mh-smail] instead.
  1381. The letter is composed in mh-letter-mode; see its documentation for more
  1382. details.  If `mh-compose-letter-function' is defined, it is called on the
  1383. draft and passed three arguments: to, subject, and cc." t)
  1384.  
  1385. (autoload 'mh-send-other-window "mh-comp"
  1386.   "Compose and send a letter in another window.
  1387. Do not call this function from outside mh-e;
  1388. use \\[mh-smail-other-window] instead.
  1389. See also documentation for `\\[mh-send]' function." t)
  1390.  
  1391. (autoload 'mh-letter-mode "mh-comp"
  1392.   "Mode for composing letters in mh-e.
  1393. For more details, type \\[describe-mode] while in MH-Letter mode." t)
  1394.  
  1395.  
  1396. ;;; mh-funcs
  1397.  
  1398. (autoload 'mh-burst-digest "mh-funcs"
  1399.   "Burst apart the current message, which should be a digest.
  1400. The message is replaced by its table of contents and the messages from the
  1401. digest are inserted into the folder after that message." t)
  1402.  
  1403. (autoload 'mh-copy-msg "mh-funcs"
  1404.   "Copy to another FOLDER the specified MESSAGE(s) without deleting them.
  1405. Default is the displayed message.  If optional prefix argument is
  1406. provided, then prompt for the message sequence." t)
  1407.  
  1408. (autoload 'mh-kill-folder "mh-funcs"
  1409.   "Remove the current folder." t)
  1410.  
  1411. (autoload 'mh-list-folders "mh-funcs"
  1412.   "List mail folders." t)
  1413.  
  1414. (autoload 'mh-pack-folder "mh-funcs"
  1415.   "Renumber the messages of a folder to be 1..n.
  1416. First, offer to execute any outstanding commands for the current folder.
  1417. If optional prefix argument provided, prompt for the range of messages
  1418. to display after packing.  Otherwise, show the entire folder." t)
  1419.  
  1420. (autoload 'mh-pipe-msg "mh-funcs"
  1421.   "Pipe the current message through the given shell COMMAND.
  1422. If INCLUDE-HEADERS (prefix argument) is provided, send the entire message.
  1423. Otherwise just send the message's body without the headers." t)
  1424.  
  1425. (autoload 'mh-page-digest "mh-funcs"
  1426.   "Advance displayed message to next digested message." t)
  1427.  
  1428. (autoload 'mh-page-digest-backwards "mh-funcs"
  1429.   "Back up displayed message to previous digested message." t)
  1430.  
  1431. (autoload 'mh-print-msg "mh-funcs"
  1432.   "Print MESSAGE(s) (default: displayed message) on printer.
  1433. If optional prefix argument provided, then prompt for the message sequence.
  1434. The variable mh-lpr-command-format is used to generate the print command.
  1435. The messages are formatted by mhl.  See the variable mhl-formfile." t)
  1436.  
  1437. (autoload 'mh-sort-folder "mh-funcs"
  1438.   "Sort the messages in the current folder by date.
  1439. Calls the MH program sortm to do the work.
  1440. The arguments in the list  mh-sortm-args  are passed to sortm
  1441. if this function is passed an argument." t)
  1442.  
  1443. (autoload 'mh-undo-folder "mh-funcs"
  1444.   "Undo all commands in current folder." t)
  1445.  
  1446. (autoload 'mh-store-msg "mh-funcs"
  1447.   "Store the file(s) contained in the current message into DIRECTORY.
  1448. The message can contain a shar file or uuencoded file.
  1449. Default directory is the last directory used, or initially the value of
  1450. mh-store-default-directory  or the current directory." t)
  1451.  
  1452. (autoload 'mh-store-buffer "mh-funcs"
  1453.   "Store the file(s) contained in the current buffer into DIRECTORY.
  1454. The buffer can contain a shar file or uuencoded file.
  1455. Default directory is the last directory used, or initially the value of
  1456. `mh-store-default-directory' or the current directory." t)
  1457.  
  1458.  
  1459. ;;; mh-pick
  1460.  
  1461. (autoload 'mh-search-folder "mh-pick"
  1462.   "Search FOLDER for messages matching a pattern.
  1463. Add the messages found to the sequence named `search'." t)
  1464.  
  1465. ;;; mh-seq
  1466.  
  1467. (autoload 'mh-delete-seq "mh-seq"
  1468.   "Delete the SEQUENCE." t)
  1469. (autoload 'mh-list-sequences "mh-seq"
  1470.   "List the sequences defined in FOLDER." t)
  1471. (autoload 'mh-msg-is-in-seq "mh-seq"
  1472.   "Display the sequences that contain MESSAGE (default: displayed message)." t)
  1473. (autoload 'mh-narrow-to-seq "mh-seq"
  1474.   "Restrict display of this folder to just messages in SEQUENCE
  1475. Use \\[mh-widen] to undo this command." t)
  1476. (autoload 'mh-put-msg-in-seq "mh-seq"
  1477.   "Add MESSAGE(s) (default: displayed message) to SEQUENCE.
  1478. If optional prefix argument provided, then prompt for the message sequence." t)
  1479. (autoload 'mh-widen "mh-seq"
  1480.   "Remove restrictions from current folder, thereby showing all messages." t)
  1481. (autoload 'mh-rename-seq "mh-seq"
  1482.   "Rename SEQUENCE to have NEW-NAME." t)
  1483.  
  1484. ;;; mh-e.el ends here
  1485.