home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / lucid / lemacs-19.6 / lisp / packages / mh-e.el < prev    next >
Encoding:
Text File  |  1992-06-29  |  92.8 KB  |  2,766 lines

  1. ;;;  mh-e.el    (Version: 3.6 for GNU Emacs Version 18 and MH.5 and MH.6)
  2.  
  3. (defvar mh-e-RCS-id)
  4. (setq mh-e-RCS-id "$Header: /cadillac-inferno-5/cvs-master/lemacs/lisp/packages/mh-e.el,v 1.1.1.1 1992/06/29 22:30:47 devin Exp $")
  5. (provide 'mh-e)
  6.  
  7. ;;;  Copyright (C) 1985-89 Free Software Foundation
  8. ;;;     Author:  James Larus (larus@ginger.Berkeley.EDU or ucbvax!larus)
  9. ;;;    Please send suggestions and corrections to the above address.
  10. ;;;
  11. ;;;  This file contains mh-e, a GNU Emacs front end to the MH mail system.
  12.  
  13.  
  14. ;; GNU Emacs is distributed in the hope that it will be useful,
  15. ;; but without any warranty.  No author or distributor
  16. ;; accepts responsibility to anyone for the consequences of using it
  17. ;; or for whether it serves any particular purpose or works at all,
  18. ;; unless he says so in writing.
  19.  
  20. ;; Everyone is granted permission to copy, modify and redistribute
  21. ;; GNU Emacs, but only under the conditions described in the
  22. ;; document "GNU Emacs copying permission notice".   An exact copy
  23. ;; of the document is supposed to have been given to you along with
  24. ;; GNU Emacs so that you can know how you may redistribute it all.
  25. ;; It should be in a file named COPYING.  Among other things, the
  26. ;; copyright notice and this notice must be preserved on all copies.
  27.  
  28.  
  29. ;;;  Original version for Gosling emacs by Brian Reid, Stanford, 1982.
  30. ;;;  Modified by James Larus, BBN, July 1984 and UCB, 1984 & 1985.
  31. ;;;  Rewritten for GNU Emacs, James Larus 1985.  larus@ginger.berkeley.edu
  32. ;;;  Modified by Stephen Gildea 1988.  gildea@bbn.com
  33.  
  34.  
  35. ;;;  NB.  MH must have been compiled with the MHE compiler flag or several
  36. ;;;  features necessary mh-e will be missing from MH commands, specifically
  37. ;;;  the -build switch to repl and forw.
  38.  
  39.  
  40.  
  41. ;;; Constants:
  42.  
  43. ;;; Set for local environment:
  44. ;;;* These are now in paths.el.
  45. ;;;(defvar mh-progs "/usr/new/mh/" "Directory containing MH commands.")
  46. ;;;(defvar mh-lib "/usr/new/lib/mh/" "Directory of MH library.")
  47.  
  48. (defvar mh-redist-full-contents t
  49.   "Non-nil if the `dist' command needs whole letter for redistribution.
  50. This is the case when `send' is compiled with the BERK option.")
  51.  
  52.  
  53. ;;; Hooks:
  54.  
  55. (defvar mh-folder-mode-hook nil
  56.   "Invoked in mh-folder-mode on a new folder.")
  57.  
  58. (defvar mh-letter-mode-hook nil
  59.   "Invoked in mh-letter-mode on a new letter.")
  60.  
  61. (defvar mh-compose-letter-function nil
  62.   "Invoked in mh-compose-and-send-mail on a draft letter.
  63. It is passed three arguments: TO recipients, SUBJECT, and CC recipients.")
  64.  
  65. (defvar mh-before-send-letter-hook nil
  66.   "Invoked at the beginning of the \\[mh-send-letter] command.")
  67.  
  68. (defvar mh-inc-folder-hook nil
  69.   "Invoked after incorporating mail into a folder with \\[mh-inc-folder].")
  70.  
  71. (defvar mh-quit-hook nil
  72.   "Invoked after quitting mh-e with \\[mh-quit].")
  73.  
  74.  
  75. ;;; Personal preferences:
  76.  
  77. (defvar mh-clean-message-header nil
  78.   "*Non-nil means clean headers of messages that are displayed or inserted.
  79. The variables mh-visible-headers and mh-invisible-headers control what is
  80. removed.")
  81.  
  82. (defvar mh-visible-headers nil
  83.   "*If non-nil, contains a regexp specifying the headers to keep when cleaning.
  84. Only used if mh-clean-message-header is non-nil. Setting this variable
  85. overrides mh-invisible-headers.")
  86.  
  87. (defvar mhl-formfile nil
  88.   "*Name of format file to be used by mhl to show messages.
  89. A value of T means use the default format file.
  90. Nil means don't use mhl to format messages.")
  91.  
  92. (defvar mh-lpr-command-format "lpr -p -J '%s'"
  93.   "*Format for Unix command that prints a message.
  94. The string should be a Unix command line, with the string '%s' where
  95. the job's name (folder and message number) should appear.  The message text
  96. is piped to this command.")
  97.  
  98. (defvar mh-print-background nil
  99.   "*Print messages in the background if non-nil.
  100. WARNING: do not delete the messages until printing is finished;
  101. otherwise, your output may be truncated.")
  102.  
  103. (defvar mh-summary-height 4
  104.   "*Number of lines in summary window.")
  105.  
  106. (defvar mh-recenter-summary-p nil
  107.   "*Recenter summary window when the show window is toggled off if non-nil.")
  108.  
  109. (defvar mh-ins-buf-prefix ">> "
  110.   "*String to put before each non-blank line of a yanked or inserted message.
  111. Used when the message is inserted in an outgoing letter.")
  112.  
  113. (defvar mh-do-not-confirm nil
  114.   "*Non-nil means do not prompt for confirmation before some commands.
  115. Only affects certain innocuous commands.")
  116.  
  117. (defvar mh-bury-show-buffer t
  118.   "*Non-nil means that the displayed show buffer for a folder is buried.")
  119.  
  120. (defvar mh-delete-yanked-msg-window nil
  121.   "*Controls window display when a message is yanked by \\[mh-yank-cur-msg].
  122. If non-nil, yanking the current message into a draft letter deletes any
  123. windows displaying the message.")
  124.  
  125. (defvar mh-yank-from-start-of-msg t
  126.   "*Controls which part of a message is yanked by \\[mh-yank-cur-msg].
  127. If non-nil, include the entire message.  If the symbol `body, then yank the
  128. message minus the header.  If nil, yank only the portion of the message
  129. following the point.  If the show buffer has a region, this variable is
  130. ignored.")
  131.  
  132. (defvar mh-reply-default-reply-to nil
  133.   "*Sets the person or persons to whom a reply will be sent.
  134. If nil, prompt for recipient. If non-nil, then \\[mh-reply] will use this
  135. value and it should be one of \"from\", \"to\", or \"cc\".")
  136.  
  137. (defvar mh-recursive-folders nil
  138.   "*If non-nil, then commands which operate on folders do so recursively.")
  139.  
  140.  
  141. ;;; Parameterize mh-e to work with different scan formats.  The defaults work
  142. ;;; the standard MH scan listings.
  143.  
  144. (defvar mh-cmd-note 4
  145.   "Offset to insert notation.")
  146.  
  147. (defvar mh-note-repl "-"
  148.   "String whose first character is used to notate replied to messages.")
  149.  
  150. (defvar mh-note-forw "F"
  151.   "String whose first character is used to notate forwarded messages.")
  152.  
  153. (defvar mh-note-dist "R"
  154.   "String whose first character is used to notate redistributed messages.")
  155.  
  156. (defvar mh-good-msg-regexp  "^....[^D^]"
  157.   "Regexp specifiying the scan lines that are 'good' messages.")
  158.  
  159. (defvar mh-deleted-msg-regexp "^....D"
  160.   "Regexp matching scan lines of deleted messages.")
  161.  
  162. (defvar mh-refiled-msg-regexp  "^....\\^"
  163.   "Regexp matching scan lines of refiled messages.")
  164.  
  165. (defvar mh-valid-scan-line "^ *[0-9]"
  166.   "Regexp matching scan lines for messages (not error messages).")
  167.  
  168. (defvar mh-msg-number-regexp "^ *\\([0-9]+\\)"
  169.   "Regexp to find the number of a message in a scan line.
  170. The message's number must be surrounded with \\( \\)")
  171.  
  172. (defvar mh-msg-search-regexp "^[^0-9]*%d[^0-9]"
  173.   "Format string containing a regexp matching the scan listing for a message.
  174. The desired message's number will be an argument to format.")
  175.  
  176. (defvar mh-flagged-scan-msg-regexp "^....\\D\\|^....\\^\\|^....\\+\\|^.....%"
  177.   "Regexp matching flagged scan lines.
  178. Matches lines marked as deleted, refiled, in a sequence, or the cur message.")
  179.  
  180. (defvar mh-cur-scan-msg-regexp "^....\\+"
  181.   "Regexp matching scan line for the cur message.")
  182.  
  183. (defvar mh-show-buffer-mode-line-buffer-id "{%%b}  %s/%d"
  184.   "Format string to produce mode-line-buffer-id for show buffers.
  185. First argument is folder name.  Second is message number.")
  186.  
  187.  
  188. ;;; Real constants:
  189.  
  190. (defvar mh-invisible-headers
  191.   "^Received: \\|^Message-Id: \\|^Remailed-\\|^Via: \\|^Mail-from: \\|^Return-Path: \\|^In-Reply-To: \\|^Resent-"
  192.   "Regexp matching lines in a message header that are not to be shown.
  193. If mh-visible-headers is non-nil, it is used instead to specify what to keep.")
  194.  
  195. (defvar mh-rejected-letter-start "^   ----- Unsent message follows -----$"
  196.   "Regexp specifying the beginning of the wrapper around a returned letter.
  197. This wrapper is generated by the mail system when rejecting a letter.")
  198.  
  199. (defvar mh-to-field-choices '((?t . "To:") (?s . "Subject:") (?c . "Cc:")
  200.                   (?b . "Bcc:") (?f . "Fcc:"))
  201.   "A-list of (character . field name) strings for mh-to-field.")
  202.  
  203.  
  204. ;;; Global variables:
  205.  
  206. (defvar mh-user-path  ""
  207.   "User's mail folder.")
  208.  
  209. (defvar mh-last-destination nil
  210.   "Destination of last refile or write command.")
  211.  
  212. (defvar mh-folder-mode-map (make-keymap)
  213.   "Keymap for MH folders.")
  214.  
  215. (defvar mh-letter-mode-map (copy-keymap text-mode-map)
  216.   "Keymap for composing mail.")
  217.  
  218. (defvar mh-pick-mode-map (make-sparse-keymap)
  219.   "Keymap for searching folder.")
  220.  
  221. (defvar mh-letter-mode-syntax-table nil
  222.   "Syntax table used while in mh-e letter mode.")
  223.  
  224. (if mh-letter-mode-syntax-table
  225.     ()
  226.     (setq mh-letter-mode-syntax-table
  227.       (make-syntax-table text-mode-syntax-table))
  228.     (set-syntax-table mh-letter-mode-syntax-table)
  229.     (modify-syntax-entry ?% "." mh-letter-mode-syntax-table))
  230.  
  231. (defvar mh-folder-list nil
  232.   "List of folder names for completion.")
  233.  
  234. (defvar mh-draft-folder nil
  235.   "Name of folder containing draft messages.
  236. NIL means do not use draft folder.")
  237.  
  238. (defvar mh-unseen-seq nil
  239.   "Name of the unseen sequence.")
  240.  
  241. (defvar mh-previous-window-config nil
  242.   "Window configuration before mh-e command.")
  243.  
  244. (defvar mh-previous-seq nil
  245.   "Name of the sequence to which a message was last added.")
  246.  
  247.  
  248. ;;; Macros and generic functions:
  249.  
  250. (defmacro mh-push (v l)
  251.   (list 'setq l (list 'cons v l)))
  252.  
  253.  
  254. (defmacro mh-when (pred &rest body)
  255.   (list 'cond (cons pred body)))
  256.  
  257.  
  258. (defmacro with-mh-folder-updating (save-modification-flag-p &rest body)
  259.   ;; Format is (with-mh-folder-updating (SAVE-MODIFICATION-FLAG-P) &body BODY).
  260.   ;; Execute BODY, which can modify the folder buffer without having to
  261.   ;; worry about file locking or the read-only flag, and return its result.
  262.   ;; If SAVE-MODIFICATION-FLAG-P is non-nil, the buffer's modification
  263.   ;; flag is unchanged, otherwise it is cleared.
  264.   (setq save-modification-flag-p (car save-modification-flag-p)) ; CL style
  265.   (` (let ((folder-updating-mod-flag (buffer-modified-p)))
  266.        (prog1
  267.        (let ((buffer-read-only nil)
  268.          (buffer-file-name nil)) ; don't let the buffer get locked
  269.          (,@ body))
  270.      (, (if save-modification-flag-p
  271.         '(mh-set-folder-modified-p folder-updating-mod-flag)
  272.           '(mh-set-folder-modified-p nil)))))))
  273.  
  274.  
  275. (defun mh-mapc (func list)
  276.   (while list
  277.     (funcall func (car list))
  278.     (setq list (cdr list))))
  279.  
  280.  
  281.  
  282. ;;; Entry points:
  283.  
  284. (defun mh-rmail (&optional arg)
  285.   "Inc(orporate) new mail (no arg) or scan a MH mail box (arg given).
  286. This front end uses the MH mail system, which uses different conventions
  287. >from the usual mail system."
  288.   (interactive "P")
  289.   (mh-find-path)
  290.   (if arg
  291.       (call-interactively 'mh-visit-folder)
  292.       (mh-inc-folder)))
  293.  
  294.  
  295. (defun mh-smail ()
  296.   "Send mail using the MH mail system."
  297.   (interactive)
  298.   (mh-find-path)
  299.   (call-interactively 'mh-send))
  300.  
  301.  
  302. (defun mh-smail-other-window ()
  303.   "Send mail in other window using the MH mail system."
  304.   (interactive)
  305.   (mh-find-path)
  306.   (call-interactively 'mh-send-other-window))
  307.  
  308.  
  309.  
  310. ;;; User executable mh-e commands:
  311.  
  312. (defun mh-burst-digest ()
  313.   "Burst apart the current message, which should be a digest.
  314. The message is replaced by its table of contents and the letters from the
  315. digest are inserted into the folder after that message."
  316.   (interactive)
  317.   (let ((digest (mh-get-msg-num t)))
  318.     (mh-process-or-undo-commands mh-current-folder)
  319.     (mh-set-folder-modified-p t)        ; lock folder while bursting
  320.     (message "Bursting digest...")
  321.     (mh-exec-cmd "burst" mh-current-folder digest "-inplace")
  322.     (mh-scan-folder mh-current-folder (format "%d-last" mh-first-msg-num))
  323.     (message "Bursting digest...done")))
  324.  
  325.  
  326. (defun mh-copy-msg (prefix-provided msg-or-seq dest)
  327.   "Copy specified MESSAGE(s) to another FOLDER without deleting them.
  328. Default is the displayed message.  If (optional) prefix argument is
  329. provided, then prompt for the message sequence."
  330.   (interactive (list current-prefix-arg
  331.              (if current-prefix-arg
  332.              (mh-read-seq-default "Copy" t)
  333.              nil)
  334.              (mh-prompt-for-folder "Copy to" "" t)))
  335.   (mh-exec-cmd "refile" msg-or-seq "-link" "-src" mh-current-folder dest)
  336.   (if prefix-provided
  337.       (mh-notate-seq msg-or-seq ?C mh-cmd-note)
  338.       (mh-notate msg-or-seq ?C mh-cmd-note)))
  339.  
  340.  
  341. (defun mh-delete-msg (msg-or-seq)
  342.   "Mark the specified MESSAGE(s) for subsequent deletion and move to the next.
  343. Default is the displayed message.  If (optional) prefix argument is
  344. provided, then prompt for the message sequence."
  345.   (interactive (list (if current-prefix-arg
  346.              (mh-read-seq-default "Delete" t)
  347.              (mh-get-msg-num t))))
  348.   (if (numberp msg-or-seq)
  349.       (mh-delete-a-msg msg-or-seq)
  350.       (mh-map-to-seq-msgs 'mh-delete-a-msg msg-or-seq))
  351.   (mh-next-msg))
  352.  
  353.  
  354. (defun mh-delete-msg-no-motion (msg-or-seq)
  355.   "Mark the specified MESSAGE(s) for subsequent deletion.
  356. Default is the displayed message.  If (optional) prefix argument is
  357. provided, then prompt for the message sequence."
  358.   (interactive (list (if current-prefix-arg
  359.              (mh-read-seq-default "Delete" t)
  360.              (mh-get-msg-num t))))
  361.   (if (numberp msg-or-seq)
  362.       (mh-delete-a-msg msg-or-seq)
  363.       (mh-map-to-seq-msgs 'mh-delete-a-msg msg-or-seq)))
  364.  
  365.  
  366. (defun mh-delete-msg-from-seq (prefix-provided msg-or-seq &optional from-seq)
  367.   "Delete MESSAGE (default: displayed message) from SEQUENCE.
  368. If (optional) prefix argument provided, then delete all messages from a
  369. sequence."
  370.   (interactive (let ((argp current-prefix-arg))
  371.          (list argp
  372.                (if argp
  373.                (mh-read-seq-default "Delete" t)
  374.                (mh-get-msg-num t))
  375.                (if (not argp)
  376.                (mh-read-seq-default "Delete from" t)))))
  377.   (if prefix-provided
  378.       (mh-remove-seq msg-or-seq)
  379.       (mh-remove-msg-from-seq msg-or-seq from-seq)))
  380.  
  381.  
  382. (defun mh-edit-again (msg)
  383.   "Clean-up a draft or a message previously sent and make it resendable."
  384.   (interactive (list (mh-get-msg-num t)))
  385.   (let* ((from-folder mh-current-folder)
  386.      (config (current-window-configuration))
  387.      (draft
  388.       (cond ((and mh-draft-folder (equal from-folder mh-draft-folder))
  389.          (find-file (mh-msg-filename msg))
  390.          (rename-buffer (format "draft-%d" msg))
  391.          (buffer-name))
  392.         (t
  393.          (mh-read-draft "clean-up" (mh-msg-filename msg) nil)))))
  394.     (mh-clean-msg-header (point-min)
  395.              "^Date:\\|^Received:\\|^Message-Id:\\|^From:"
  396.              nil)
  397.     (goto-char (point-min))
  398.     (set-buffer-modified-p nil)
  399.     (mh-compose-and-send-mail draft "" from-folder nil nil nil nil nil nil
  400.                   config)))
  401.  
  402.  
  403. (defun mh-execute-commands ()
  404.   "Process outstanding delete and refile requests."
  405.   (interactive)
  406.   (if mh-narrowed-to-seq (mh-widen))
  407.   (mh-process-commands mh-current-folder)
  408.   (mh-set-scan-mode)
  409.   (mh-goto-cur-msg)            ; after mh-set-scan-mode for efficiency
  410.   (mh-make-folder-mode-line)
  411.   t)                    ; return t for write-file-hooks
  412.  
  413.  
  414. (defun mh-extract-rejected-mail (msg)
  415.   "Extract a letter returned by the mail system and make it resendable.
  416. Default is the displayed message."
  417.   (interactive (list (mh-get-msg-num t)))
  418.   (let ((from-folder mh-current-folder)
  419.     (config (current-window-configuration))
  420.     (draft (mh-read-draft "extraction" (mh-msg-filename msg) nil)))
  421.     (goto-char (point-min))
  422.     (cond ((re-search-forward mh-rejected-letter-start nil t)
  423.        (forward-char 1)
  424.        (delete-region (point-min) (point))
  425.        (mh-clean-msg-header (point-min)
  426.                 "^Date:\\|^Received:\\|^Message-Id:\\|^From:\\|^Sender:"
  427.                 nil))
  428.       (t
  429.        (message "Does not appear to be a rejected letter.")))
  430.     (goto-char (point-min))
  431.     (set-buffer-modified-p nil)
  432.     (mh-compose-and-send-mail draft "" from-folder msg (mh-get-field "To")
  433.                   (mh-get-field "From") (mh-get-field "cc")
  434.                   nil nil config)))
  435.  
  436.  
  437. (defun mh-forward (prefix-provided msg-or-seq to cc)
  438.   "Forward MESSAGE(s) (default: displayed message).
  439. If (optional) prefix argument provided, then prompt for the message sequence."
  440.   (interactive (list current-prefix-arg
  441.              (if current-prefix-arg
  442.              (mh-read-seq-default "Forward" t)
  443.              (mh-get-msg-num t))
  444.              (read-string "To: ")
  445.              (read-string "Cc: ")))
  446.   (let* ((folder mh-current-folder)
  447.      (config (current-window-configuration))
  448.      ;; forw always leaves file in "draft" since it doesn't have -draft
  449.      (draft-name (expand-file-name "draft" mh-user-path))
  450.      (draft (cond ((or (not (file-exists-p draft-name))
  451.                (y-or-n-p "The file 'draft' exists.  Discard it? "))
  452.                (mh-exec-cmd "forw"
  453.                     "-build" mh-current-folder msg-or-seq)
  454.                (prog1
  455.                (mh-read-draft "" draft-name t)
  456.              (mh-insert-fields "To:" to "Cc:" cc)
  457.              (set-buffer-modified-p nil)))
  458.               (t
  459.                (mh-read-draft "" draft-name nil)))))
  460.     (goto-char (point-min))
  461.     (re-search-forward "^------- Forwarded Message")
  462.     (forward-line -1)
  463.     (narrow-to-region (point) (point-max))
  464.     (let* ((subject (save-excursion (mh-get-field "From:")))
  465.        (trim (string-match "<" subject))
  466.        (forw-subject (save-excursion (mh-get-field "Subject:"))))
  467.       (if trim
  468.       (setq subject (substring subject 0 (1- trim))))
  469.       (widen)
  470.       (save-excursion
  471.     (mh-insert-fields "Subject:" (format "[%s: %s]" subject forw-subject)))
  472.       (delete-other-windows)
  473.       (if prefix-provided
  474.       (mh-add-msgs-to-seq (mh-seq-to-msgs msg-or-seq) 'forwarded t)
  475.       (mh-add-msgs-to-seq msg-or-seq 'forwarded t))
  476.       (mh-compose-and-send-mail draft "" folder msg-or-seq
  477.                 to subject cc
  478.                 mh-note-forw "Forwarded:"
  479.                 config))))
  480.  
  481.  
  482. (defun mh-goto-msg (number &optional no-error-if-no-message dont-show)
  483.   "Position the cursor at message NUMBER.
  484. Non-nil second argument means do not signal an error if message does not exist.
  485. Non-nil third argument means not to show the message.
  486. Return non-nil if cursor is at message."
  487.   (interactive "NMessage number? ")
  488.   (let ((cur-msg (mh-get-msg-num nil))
  489.     (starting-place (point))
  490.     (msg-pattern (mh-msg-search-pat number)))
  491.     (cond ((cond ((and cur-msg (= cur-msg number)) t)
  492.          ((and cur-msg
  493.                (< cur-msg number)
  494.                (re-search-forward msg-pattern nil t)) t)
  495.          ((and cur-msg
  496.                (> cur-msg number)
  497.                (re-search-backward msg-pattern nil t)) t)
  498.          (t            ; Do thorough search of buffer
  499.           (goto-char (point-min))
  500.           (re-search-forward msg-pattern nil t)))
  501.         (beginning-of-line)
  502.         (if (not dont-show) (mh-maybe-show number))
  503.         t)
  504.       (t
  505.        (goto-char starting-place)
  506.        (if (not no-error-if-no-message)
  507.            (error "No message %d" number))
  508.        nil))))
  509.  
  510.  
  511. (defun mh-inc-folder (&optional maildrop-name)
  512.   "Inc(orporate) new mail into +inbox.
  513. Optional prefix argument specifies an alternate maildrop from the default.
  514. If this is given, mail is incorporated into the current folder, rather
  515. than +inbox.  Run mh-inc-folder-hook after incorporating new mail."
  516.   (interactive (list (if current-prefix-arg
  517.              (expand-file-name
  518.               (read-file-name "inc mail from file: "
  519.                       mh-user-path)))))
  520.   (let ((config (current-window-configuration)))
  521.     (if (not maildrop-name)
  522.     (cond ((not (get-buffer "+inbox"))
  523.            (mh-make-folder "+inbox")
  524.            (setq mh-previous-window-config config))
  525.           ((not (eq (current-buffer) (get-buffer "+inbox")))
  526.            (switch-to-buffer "+inbox")
  527.            (setq mh-previous-window-config config)))))
  528.   (mh-get-new-mail maildrop-name)
  529.   (run-hooks 'mh-inc-folder-hook))
  530.  
  531.  
  532. (defun mh-kill-folder ()
  533.   "Remove the current folder."
  534.   (interactive)
  535.   (if (or mh-do-not-confirm
  536.       (yes-or-no-p (format "Remove folder %s? " mh-current-folder)))
  537.       (let ((folder mh-current-folder))
  538.     (mh-set-folder-modified-p t)    ; lock folder to kill it
  539.     (mh-exec-cmd-daemon "rmf" folder)
  540.     (mh-remove-folder-from-folder-list folder)
  541.     (message "Folder %s removed" folder)
  542.     (mh-set-folder-modified-p nil)    ; so kill-buffer doesn't complain
  543.     (kill-buffer folder))
  544.       (message "Folder not removed")))
  545.  
  546.  
  547. (defun mh-list-folders ()
  548.   "List mail folders."
  549.   (interactive)
  550.   (with-output-to-temp-buffer " *mh-temp*"
  551.     (save-excursion
  552.       (switch-to-buffer " *mh-temp*")
  553.       (erase-buffer)
  554.       (message "Listing folders...")
  555.       (mh-exec-cmd-output "folders" t)
  556.       (goto-char (point-min))
  557.       (message "Listing folders...done"))))
  558.  
  559.  
  560. (defun mh-msg-is-in-seq (msg)
  561.   "Display the sequences that contain MESSAGE (default: displayed message)."
  562.   (interactive (list (mh-get-msg-num t)))
  563.   (message "Message %d is in sequences: %s"
  564.        msg
  565.        (mapconcat 'concat
  566.               (mh-list-to-string (mh-seq-containing-msg msg))
  567.               " ")))
  568.  
  569.  
  570. (defun mh-narrow-to-seq (seq)
  571.   "Restrict display of this folder to just messages in a sequence.
  572. Reads which sequence.  Use \\[mh-widen] to undo this command."
  573.   (interactive (list (mh-read-seq "Narrow to" t)))
  574.   (let ((eob (point-max)))
  575.     (with-mh-folder-updating (t)
  576.       (cond ((mh-seq-to-msgs seq)
  577.          (mh-copy-seq-to-point seq eob)
  578.          (narrow-to-region eob (point-max))
  579.          (mh-make-folder-mode-line (symbol-name seq))
  580.          (mh-recenter nil)
  581.          (setq mh-narrowed-to-seq seq))
  582.         (t
  583.          (error "No messages in sequence `%s'" (symbol-name seq)))))))
  584.  
  585.  
  586. (defun mh-next-undeleted-msg (&optional arg)
  587.   "Move to next undeleted message in window."
  588.   (interactive "P")
  589.   (forward-line (prefix-numeric-value arg))
  590.   (setq mh-next-direction 'forward)
  591.   (cond ((re-search-forward mh-good-msg-regexp nil 0 arg)
  592.      (beginning-of-line)
  593.      (mh-maybe-show))
  594.     (t
  595.      (forward-line -1)
  596.      (if (get-buffer mh-show-buffer)
  597.          (delete-windows-on mh-show-buffer)))))
  598.  
  599.  
  600. (defun mh-pack-folder (range)
  601.   "Renumber the messages of a folder to be 1..n.
  602. First, offer to execute any outstanding commands for the current folder.
  603. If (optional) prefix argument provided, prompt for the range of messages
  604. to display after packing.  Otherwise, show the entire folder."
  605.   (interactive (list (if current-prefix-arg
  606.              (read-msg-range "Range [all]? ")
  607.              "all")))
  608.   (mh-pack-folder-1 range)
  609.   (mh-goto-cur-msg)
  610.   (message "Packing folder...done"))
  611.  
  612.  
  613. (defun mh-pipe-msg (prefix-provided command)
  614.   "Pipe the current message through the given shell COMMAND.
  615. If (optional) prefix argument is provided, send the entire message.
  616. Otherwise just send the message's body."
  617.   (interactive
  618.    (list current-prefix-arg (read-string "Shell command on message: ")))
  619.   (save-excursion
  620.     (set-buffer mh-show-buffer)
  621.     (goto-char (point-min))
  622.     (if (not prefix-provided) (search-forward "\n\n"))
  623.     (shell-command-on-region (point) (point-max) command nil)))
  624.  
  625.  
  626. (defun mh-refile-msg (prefix-provided msg-or-seq dest)
  627.   "Refile MESSAGE(s) (default: displayed message) in FOLDER.
  628. If (optional) prefix argument provided, then prompt for message sequence."
  629.   (interactive
  630.    (list current-prefix-arg
  631.      (if current-prefix-arg
  632.          (mh-read-seq-default "Refile" t)
  633.          (mh-get-msg-num t))
  634.      (intern
  635.       (mh-prompt-for-folder "Destination"
  636.                 (if (eq 'refile (car mh-last-destination))
  637.                     (symbol-name (cdr mh-last-destination))
  638.                     "")
  639.                 t))))
  640.   (setq mh-last-destination (cons 'refile dest))
  641.   (if prefix-provided
  642.       (mh-map-to-seq-msgs 'mh-refile-a-msg msg-or-seq dest)
  643.       (mh-refile-a-msg msg-or-seq dest))
  644.   (mh-next-msg))
  645.  
  646.  
  647. (defun mh-refile-or-write-again (msg)
  648.   "Re-execute the last refile or write command on the given MESSAGE.
  649. Default is the displayed message.  Use the same folder or file as the
  650. previous refile or write command."
  651.   (interactive (list (mh-get-msg-num t)))
  652.   (if (null mh-last-destination)
  653.       (error "No previous refile or write"))
  654.   (cond ((eq (car mh-last-destination) 'refile)
  655.      (mh-refile-a-msg msg (cdr mh-last-destination))
  656.      (message "Destination folder: %s" (cdr mh-last-destination)))
  657.     (t
  658.      (mh-write-msg-to-file msg (cdr mh-last-destination))
  659.      (message "Destination: %s" (cdr mh-last-destination))))
  660.   (mh-next-msg))
  661.  
  662.  
  663. (defun mh-reply (prefix-provided msg)
  664.   "Reply to a MESSAGE (default: displayed message).
  665. If (optional) prefix argument provided, then include the message in the reply."
  666.   (interactive (list current-prefix-arg (mh-get-msg-num t)))
  667.   (let ((minibuffer-help-form
  668.      "from => Sender only\nto => Sender and primary recipients\ncc or all => Sender and all recipients"))
  669.     (let ((reply-to (or mh-reply-default-reply-to
  670.             (completing-read "Reply to whom: "
  671.                      '(("from") ("to") ("cc") ("all"))
  672.                      nil
  673.                      t)))
  674.       (folder mh-current-folder)
  675.       (show-buffer mh-show-buffer)
  676.       (config (current-window-configuration)))
  677.       (message "Composing a reply...")
  678.       (cond ((or (equal reply-to "from") (equal reply-to ""))
  679.          (apply 'mh-exec-cmd
  680.             "repl" "-build"
  681.             "-nodraftfolder" mh-current-folder
  682.             msg
  683.             "-nocc" "all"
  684.             (if prefix-provided
  685.             (list "-filter" "mhl.reply"))))
  686.         ((equal reply-to "to")
  687.          (apply 'mh-exec-cmd
  688.             "repl" "-build"
  689.             "-nodraftfolder" mh-current-folder
  690.             msg
  691.             "-cc" "to"
  692.             (if prefix-provided
  693.             (list "-filter" "mhl.reply"))))
  694.         ((or (equal reply-to "cc") (equal reply-to "all"))
  695.          (apply 'mh-exec-cmd
  696.             "repl" "-build"
  697.             "-nodraftfolder" mh-current-folder
  698.             msg
  699.             "-cc" "all" "-nocc" "me"
  700.             (if prefix-provided
  701.             (list "-filter" "mhl.reply")))))
  702.  
  703.       (let ((draft (mh-read-draft "reply"
  704.                   (expand-file-name "reply" mh-user-path)
  705.                   t)))
  706.     (delete-other-windows)
  707.     (set-buffer-modified-p nil)
  708.  
  709.     (let ((to (mh-get-field "To:"))
  710.           (subject (mh-get-field "Subject:"))
  711.           (cc (mh-get-field "Cc:")))
  712.       (goto-char (point-min))
  713.       (mh-goto-header-end 1)
  714.       (if (not prefix-provided)
  715.           (mh-display-msg msg folder))
  716.       (mh-add-msgs-to-seq msg 'answered t)
  717.       (message "Composing a reply...done")
  718.       (mh-compose-and-send-mail draft "" folder msg to subject cc
  719.                     mh-note-repl "Replied:" config))))))
  720.  
  721.  
  722. (defun mh-quit ()
  723.   "Restore the previous window configuration, if one exists.
  724. Finish by running mh-quit-hook."
  725.   (interactive)
  726.   (if mh-previous-window-config
  727.       (set-window-configuration mh-previous-window-config))
  728.   (run-hooks 'mh-quit-hook))
  729.  
  730.  
  731. (defun mh-page-digest ()
  732.   "Advance displayed message to next digested message."
  733.   (interactive)
  734.   (save-excursion
  735.     (mh-show-message-in-other-window)
  736.     ;; Go to top of screen (in case user moved point).
  737.     (move-to-window-line 0)
  738.     (let ((case-fold-search nil))
  739.       ;; Search for blank line and then for From:
  740.       (mh-when (not (and (search-forward "\n\n" nil t)
  741.              (search-forward "From:" nil t)))
  742.     (other-window -1)
  743.     (error "No more messages")))
  744.     ;; Go back to previous blank line, then forward to the first non-blank.
  745.     (search-backward "\n\n" nil t)
  746.     (forward-line 2)
  747.     (mh-recenter 0)
  748.     (other-window -1)))
  749.  
  750.  
  751. (defun mh-page-digest-backwards ()
  752.   "Back up displayed message to previous digested message."
  753.   (interactive)
  754.   (save-excursion
  755.     (mh-show-message-in-other-window)
  756.     ;; Go to top of screen (in case user moved point).
  757.     (move-to-window-line 0)
  758.     (let ((case-fold-search nil))
  759.       (beginning-of-line)
  760.       (mh-when (not (and (search-backward "\n\n" nil t)
  761.              (search-backward "From:" nil t)))
  762.     (other-window -1)
  763.     (error "No more messages")))
  764.     ;; Go back to previous blank line, then forward to the first non-blank.
  765.     (search-backward "\n\n" nil t)
  766.     (forward-line 2)
  767.     (mh-recenter 0)
  768.     (other-window -1)))
  769.  
  770.  
  771. (defun mh-page-msg (&optional arg)
  772.   "Page the displayed message forwards.
  773. Scrolls ARG lines or a full screen if no argument is supplied."
  774.   (interactive "P")
  775.   (scroll-other-window arg))
  776.  
  777.  
  778. (defun mh-previous-page (&optional arg)
  779.   "Page the displayed message backwards.
  780. Scrolls ARG lines or a full screen if no argument is supplied."
  781.   (interactive "P")
  782.   (save-excursion
  783.     (mh-show-message-in-other-window)
  784.     (unwind-protect
  785.     (scroll-down arg)
  786.       (other-window -1))))
  787.  
  788.  
  789. (defun mh-previous-undeleted-msg (&optional arg)
  790.   "Move to previous undeleted message in window."
  791.   (interactive "p")
  792.   (setq mh-next-direction 'backward)
  793.   (beginning-of-line)
  794.   (cond ((re-search-backward mh-good-msg-regexp nil 0 arg)
  795.      (mh-maybe-show))
  796.     (t
  797.      (if (get-buffer mh-show-buffer)
  798.          (delete-windows-on mh-show-buffer)))))
  799.  
  800.  
  801. (defun mh-print-msg (prefix-provided msg-or-seq)
  802.   "Print MESSAGE(s) (default: displayed message) on a line printer.
  803. If (optional) prefix argument provided, then prompt for the message sequence."
  804.   (interactive (list current-prefix-arg
  805.              (if current-prefix-arg
  806.              (reverse (mh-seq-to-msgs
  807.                    (mh-read-seq-default "Print" t)))
  808.              (mh-get-msg-num t))))
  809.   (if prefix-provided
  810.       (message "Printing sequence...")
  811.       (message "Printing message..."))
  812.   (let ((print-command
  813.      (if prefix-provided
  814.          (format "(scan -clear %s ; %s -nobell -clear %s %s) | %s"
  815.              (mapconcat (function (lambda (msg) msg)) msg-or-seq " ")
  816.              (expand-file-name "mhl" mh-lib)
  817.              (if (stringp mhl-formfile)
  818.              (format "-form %s" mhl-formfile)
  819.                  "")
  820.              (mh-msg-filenames msg-or-seq)
  821.              (format mh-lpr-command-format
  822.                  (if prefix-provided
  823.                  (format "Sequence from %s" mh-current-folder)
  824.                  (format "%s/%d" mh-current-folder
  825.                      msg-or-seq))))
  826.          (format "%s -nobell -clear %s %s | %s"
  827.              (expand-file-name "mhl" mh-lib)
  828.              (mh-msg-filename msg-or-seq)
  829.              (if (stringp mhl-formfile)
  830.              (format "-form %s" mhl-formfile)
  831.                  "")
  832.              (format mh-lpr-command-format
  833.                  (if prefix-provided
  834.                  (format "Sequence from %s" mh-current-folder)
  835.                  (format "%s/%d" mh-current-folder
  836.                      msg-or-seq)))))))
  837.     (if mh-print-background
  838.     (mh-exec-cmd-daemon shell-file-name "-c" print-command)
  839.     (call-process shell-file-name nil nil nil "-c" print-command))
  840.     (if prefix-provided
  841.     (mh-notate-seq msg-or-seq ?P mh-cmd-note)
  842.     (mh-notate msg-or-seq ?P mh-cmd-note))
  843.     (mh-add-msgs-to-seq msg-or-seq 'printed t)
  844.     (if prefix-provided
  845.     (message "Printing sequence...done")
  846.         (message "Printing message...done"))))
  847.  
  848.  
  849. (defun mh-put-msg-in-seq (prefix-provided from to)
  850.   "Add MESSAGE(s) (default: displayed message) to SEQUENCE.
  851. If (optional) prefix argument provided, then prompt for the message sequence."
  852.   (interactive (list current-prefix-arg
  853.              (if current-prefix-arg
  854.              (mh-seq-to-msgs
  855.                (mh-read-seq-default "Add messages from" t))
  856.              (mh-get-msg-num t))
  857.              (mh-read-seq-default "Add to" nil)))
  858.   (setq mh-previous-seq to)
  859.   (mh-add-msgs-to-seq from to))
  860.  
  861.  
  862. (defun mh-rescan-folder (range)
  863.   "Rescan a folder after optionally processing the outstanding commands.
  864. If (optional) prefix argument is provided, prompt for the range of
  865. messages to display.  Otherwise show the entire folder."
  866.   (interactive (list (if current-prefix-arg
  867.              (read-msg-range "Range [all]? ")
  868.              nil)))
  869.   (setq mh-next-direction 'forward)
  870.   (mh-scan-folder mh-current-folder (or range "all")))
  871.  
  872.  
  873. (defun mh-redistribute (to cc msg)
  874.   "Redistribute a letter.
  875. Depending on how your copy of MH was compiled, you may need to change the
  876. setting of the variable mh-redist-full-contents.  See its documentation."
  877.   (interactive (list (read-string "Redist-To: ")
  878.              (read-string "Redist-Cc: ")
  879.              (mh-get-msg-num t)))
  880.   (save-window-excursion
  881.     (let ((folder mh-current-folder)
  882.       (draft (mh-read-draft "redistribution"
  883.                 (if mh-redist-full-contents
  884.                     (mh-msg-filename msg)
  885.                     nil)
  886.                 nil)))
  887.       (mh-goto-header-end 0)
  888.       (insert "Resent-To: " to "\n")
  889.       (if (not (equal cc "")) (insert "Resent-cc: " cc "\n"))
  890.       (mh-clean-msg-header (point-min)
  891.                "^Message-Id:\\|^Received:\\|^Return-Path:\\|^Sender:\\|^Date:\\|^From:"
  892.                nil)
  893.       (save-buffer)
  894.       (message "Redistributing...")
  895.       (if mh-redist-full-contents
  896.       (call-process "/bin/sh" nil 0 nil "-c"
  897.             (format "mhdist=1 mhaltmsg=%s %s -push %s"
  898.                 (buffer-file-name)
  899.                 (expand-file-name "send" mh-progs)
  900.                 (buffer-file-name)))
  901.       (call-process "/bin/sh" nil 0 nil "-c"
  902.             (format "mhdist=1 mhaltmsg=%s mhannotate=1 %s -push %s"
  903.                 (mh-msg-filename msg folder)
  904.                 (expand-file-name "send" mh-progs)
  905.                 (buffer-file-name))))
  906.       (mh-annotate-msg msg folder mh-note-dist
  907.                "-component" "Resent:"
  908.                "-text" (format "\"%s %s\"" to cc))
  909.       (kill-buffer draft)
  910.       (message "Redistributing...done"))))
  911.  
  912.  
  913. (defun mh-write-msg-to-file (msg file)
  914.   "Append MESSAGE to the end of a FILE."
  915.   (interactive
  916.    (list (mh-get-msg-num t)
  917.      (let ((default-dir (if (eq 'write (car mh-last-destination))
  918.                 (file-name-directory (cdr mh-last-destination))
  919.                 default-directory)))
  920.        (read-file-name "Save message in file: " default-dir
  921.                (expand-file-name "mail.out" default-dir)))))
  922.   (let ((file-name (mh-msg-filename msg))
  923.     (output-file (mh-expand-file-name file)))
  924.     (setq mh-last-destination (cons 'write file))
  925.     (save-excursion
  926.       (set-buffer (get-buffer-create " *mh-temp*"))
  927.       (erase-buffer)
  928.       (insert-file-contents file-name)
  929.       (append-to-file (point-min) (point-max) output-file))))
  930.  
  931.  
  932. (defun mh-search-folder (folder)
  933.   "Search FOLDER for messages matching a pattern."
  934.   (interactive (list (mh-prompt-for-folder "Search"
  935.                        mh-current-folder
  936.                        t)))
  937.   (switch-to-buffer-other-window "pick-pattern")
  938.   (if (or (zerop (buffer-size))
  939.       (not (y-or-n-p "Reuse pattern? ")))
  940.       (mh-make-pick-template)
  941.       (message ""))
  942.   (setq mh-searching-folder folder))
  943.  
  944.  
  945. (defun mh-send (to cc subject)
  946.   "Compose and send a letter."
  947.   (interactive "sTo: \nsCc: \nsSubject: ")
  948.   (let ((config (current-window-configuration)))
  949.     (delete-other-windows)
  950.     (mh-send-sub to cc subject config)))
  951.  
  952.  
  953. (defun mh-send-other-window (to cc subject)
  954.   "Compose and send a letter in another window.."
  955.   (interactive "sTo: \nsCc: \nsSubject: ")
  956.   (let ((pop-up-windows t))
  957.     (mh-send-sub to cc subject (current-window-configuration))))
  958.  
  959.  
  960. (defun mh-send-sub (to cc subject config)
  961.   "Do the real work of composing and sending a letter.
  962. Expects the TO, CC, and SUBJECT fields as arguments.
  963. CONFIG is the window configuration before sending mail."
  964.   (let ((folder mh-current-folder)
  965.     (msg-num (mh-get-msg-num nil)))
  966.     (message "Composing a message...")
  967.     (let ((draft (mh-read-draft
  968.           "message"
  969.           (if (file-exists-p
  970.                (expand-file-name "components" mh-user-path))
  971.               (expand-file-name "components" mh-user-path)
  972.               (if (file-exists-p
  973.                (expand-file-name "components" mh-lib))
  974.               (expand-file-name "components" mh-lib)
  975.               (error "Can't find components file")))
  976.           nil)))
  977.       (mh-insert-fields "To:" to "Subject:" subject "Cc:" cc)
  978.       (set-buffer-modified-p nil)
  979.       (goto-char (point-max))
  980.       (message "Composing a message...done")
  981.       (mh-compose-and-send-mail draft "" folder msg-num
  982.                 to subject cc
  983.                 nil nil config))))
  984.  
  985.  
  986. (defun mh-show (&optional msg)
  987.   "Show MESSAGE (default: displayed message).
  988. Forces a two-window display with the folder window on top (size
  989. mh-summary-height) and the show buffer below it."
  990.   (interactive)
  991.   (if (not msg)
  992.       (setq msg (mh-get-msg-num t)))
  993.   (setq mh-showing t)
  994.   (mh-set-mode-name "mh-e show")
  995.   (if (not (eql (next-window (minibuffer-window)) (selected-window)))
  996.       (delete-other-windows))        ; force ourself to the top window
  997.   (let ((folder mh-current-folder))
  998.     (mh-show-message-in-other-window)
  999.     (mh-display-msg msg folder))
  1000.   (other-window -1)
  1001.   (shrink-window (- (window-height) mh-summary-height))
  1002.   (mh-recenter nil)
  1003.   (if (not (memq msg mh-seen-list)) (mh-push msg mh-seen-list)))
  1004.  
  1005.  
  1006. (defun mh-sort-folder ()
  1007.   "Sort the messages in the current folder by date."
  1008.   (interactive "")
  1009.   (mh-process-or-undo-commands mh-current-folder)
  1010.   (setq mh-next-direction 'forward)
  1011.   (mh-set-folder-modified-p t)        ; lock folder while sorting
  1012.   (message "Sorting folder...")
  1013.   (mh-exec-cmd "sortm" mh-current-folder)
  1014.   (message "Sorting folder...done")
  1015.   (mh-scan-folder mh-current-folder "all"))
  1016.  
  1017.  
  1018. (defun mh-toggle-showing ()
  1019.   "Toggle the scanning mode/showing mode of displaying messages."
  1020.   (interactive)
  1021.   (if mh-showing
  1022.       (mh-set-scan-mode)
  1023.       (mh-show)))
  1024.  
  1025.  
  1026. (defun mh-undo (prefix-provided msg-or-seq)
  1027.   "Undo the deletion or refile of the specified MESSAGE(s).
  1028. Default is the displayed message.  If (optional) prefix argument is
  1029. provided, then prompt for the message sequence."
  1030.   (interactive (list current-prefix-arg
  1031.              (if current-prefix-arg
  1032.              (mh-read-seq-default "Undo" t)
  1033.              (mh-get-msg-num t))))
  1034.   (beginning-of-line)
  1035.   (cond ((looking-at mh-deleted-msg-regexp)
  1036.      (cond (prefix-provided
  1037.         (mh-mapc
  1038.          (function (lambda (msg)
  1039.                  (setq mh-delete-list (delq msg mh-delete-list))
  1040.                  (mh-remove-msg-from-seq msg 'deleted t)))
  1041.          (mh-seq-to-msgs msg-or-seq))
  1042.         (mh-notate-seq msg-or-seq ?  mh-cmd-note))
  1043.            (t
  1044.         (setq mh-delete-list (delq msg-or-seq mh-delete-list))
  1045.         (mh-remove-msg-from-seq msg-or-seq 'deleted t)
  1046.         (mh-notate msg-or-seq ?  mh-cmd-note))))
  1047.  
  1048.     ((looking-at mh-refiled-msg-regexp)
  1049.      (cond (prefix-provided
  1050.         (mh-mapc
  1051.          (function (lambda (msg)
  1052.                  (mh-mapc (function
  1053.                        (lambda (dest)
  1054.                      (mh-remove-msg-from-seq msg dest t)))
  1055.                       mh-refile-list)))
  1056.          (mh-seq-to-msgs msg-or-seq))
  1057.         (mh-notate-seq msg-or-seq ?  mh-cmd-note))
  1058.            (t
  1059.         (mh-mapc
  1060.          (function (lambda (dest)
  1061.                  (mh-remove-msg-from-seq msg-or-seq dest t)))
  1062.          mh-refile-list)
  1063.         (mh-notate msg-or-seq ?  mh-cmd-note)))
  1064.      (mh-mapc (function
  1065.            (lambda (elt)
  1066.              (if (not (mh-seq-to-msgs elt))
  1067.              (setq mh-refile-list (delq elt mh-refile-list)))))
  1068.           mh-refile-list))
  1069.  
  1070.     (t nil))
  1071.   (if (not (mh-outstanding-commands-p))
  1072.       (mh-set-folder-modified-p nil)))
  1073.  
  1074.  
  1075. (defun mh-undo-folder (&rest ignore)
  1076.   "Undo all commands in current folder."
  1077.   (interactive "")
  1078.   (cond ((or mh-do-not-confirm
  1079.          (yes-or-no-p "Undo all commands in folder? "))
  1080.      (setq mh-delete-list nil
  1081.            mh-refile-list nil
  1082.            mh-seq-list nil
  1083.            mh-next-direction 'forward)
  1084.      (with-mh-folder-updating (nil)
  1085.        (mh-unmark-all-headers t)))
  1086.     (t
  1087.      (message "Commands not undone.")
  1088.      (sit-for 2))))
  1089.  
  1090.  
  1091. (defun mh-visit-folder (folder &optional range)
  1092.   "Visit FOLDER and display RANGE of messages."
  1093.   (interactive (list (mh-prompt-for-folder "Visit" "+inbox" t)
  1094.              (read-msg-range "Range [all]? ")))
  1095.   (let ((config (current-window-configuration)))
  1096.     (mh-scan-folder folder (or range "all"))
  1097.     (setq mh-previous-window-config config)))
  1098.  
  1099.  
  1100. (defun mh-widen ()
  1101.   "Remove restrictions from the current folder, thereby showing all messages."
  1102.   (interactive "")
  1103.   (with-mh-folder-updating (t)
  1104.     (delete-region (point-min) (point-max))
  1105.     (widen)
  1106.     (mh-make-folder-mode-line))
  1107.   (setq mh-narrowed-to-seq nil))
  1108.  
  1109.  
  1110.  
  1111. ;;; Support routines.
  1112.  
  1113. (defun mh-delete-a-msg (msg)
  1114.   ;; Delete the MESSAGE.
  1115.   (save-excursion
  1116.     (mh-goto-msg msg nil t)
  1117.     (if (looking-at mh-refiled-msg-regexp)
  1118.     (error "Message %d is refiled.  Undo refile before deleting." msg))
  1119.     (if (looking-at mh-deleted-msg-regexp)
  1120.     nil
  1121.     (mh-set-folder-modified-p t)
  1122.     (mh-push msg mh-delete-list)
  1123.     (mh-add-msgs-to-seq msg 'deleted t)
  1124.     (mh-notate msg ?D mh-cmd-note))))
  1125.  
  1126.  
  1127. (defun mh-refile-a-msg (msg destination)
  1128.   ;; Refile the MESSAGE in the FOLDER.
  1129.   (save-excursion
  1130.     (mh-goto-msg msg nil t)
  1131.     (cond ((looking-at mh-deleted-msg-regexp)
  1132.        (error "Message %d is deleted.  Undo delete before moving." msg))
  1133.       ((looking-at mh-refiled-msg-regexp)
  1134.        (message "Message %d is already refiled.  Copying..." msg)
  1135.        (mh-exec-cmd "refile"
  1136.             (mh-get-msg-num t) "-link"
  1137.             "-src" mh-current-folder (symbol-name destination))))
  1138.     (mh-set-folder-modified-p t)
  1139.     (if (not (memq destination mh-refile-list))
  1140.     (mh-push destination mh-refile-list))
  1141.     (if (not (memq msg (mh-seq-to-msgs destination)))
  1142.     (mh-add-msgs-to-seq msg destination t))
  1143.     (mh-notate msg ?^ mh-cmd-note)))
  1144.  
  1145.  
  1146. (defun mh-display-msg (msg-num folder)
  1147.   ;; Display message NUMBER of FOLDER.
  1148.   (set-buffer folder)
  1149.   ;; Bind variables in folder buffer in case they are local
  1150.   (let ((formfile mhl-formfile)
  1151.     (clean-message-header mh-clean-message-header)
  1152.     (invisible-headers mh-invisible-headers)
  1153.     (visible-headers mh-visible-headers)
  1154.     (msg-filename (mh-msg-filename msg-num))
  1155.     (show-buffer mh-show-buffer)
  1156.     (folder mh-current-folder))
  1157.     (if (not (file-exists-p msg-filename))
  1158.     (error "Message %d does not exist" msg-num))
  1159.     (switch-to-buffer show-buffer)
  1160.     (if mh-bury-show-buffer (bury-buffer (current-buffer)))
  1161.     (mh-when (not (equal msg-filename buffer-file-name))
  1162.       ;; Buffer does not yet contain message.
  1163.       (clear-visited-file-modtime)
  1164.       (unlock-buffer)
  1165.       (setq buffer-file-name nil)    ; no locking during setup
  1166.       (erase-buffer)
  1167.       (if formfile
  1168.       (if (stringp formfile)
  1169.           (mh-exec-lib-cmd-output "mhl" "-nobell" "-noclear"
  1170.                       "-form" formfile msg-filename)
  1171.           (mh-exec-lib-cmd-output "mhl" "-nobell" "-noclear"
  1172.                       msg-filename))
  1173.       (insert-file-contents msg-filename))
  1174.       (goto-char (point-min))
  1175.       (cond (clean-message-header
  1176.          (mh-clean-msg-header (point-min)
  1177.                   invisible-headers
  1178.                   visible-headers)
  1179.          (goto-char (point-min)))
  1180.         (t
  1181.          (let ((case-fold-search t))
  1182.            (re-search-forward
  1183.         "^To:\\|^From:\\|^Subject:\\|^Date:" nil t)
  1184.            (beginning-of-line)
  1185.            (mh-recenter 0))))
  1186.       (set-buffer-modified-p nil)
  1187.       (setq buffer-file-name msg-filename)
  1188.       (set-mark nil)
  1189.       (setq mode-line-buffer-identification
  1190.         (list (format mh-show-buffer-mode-line-buffer-id
  1191.               folder msg-num))))))
  1192.  
  1193.  
  1194. (defun mh-invalidate-show-buffer ()
  1195.   ;; Invalidate the show buffer so we must update it to use it.
  1196.   (if (get-buffer mh-show-buffer)
  1197.       (save-excursion
  1198.     (set-buffer mh-show-buffer)
  1199.     (setq buffer-file-name nil))))
  1200.  
  1201.  
  1202. (defun mh-show-message-in-other-window ()
  1203.   (let ((buffer mh-show-buffer))
  1204.     (switch-to-buffer-other-window buffer)
  1205.     (if mh-bury-show-buffer (bury-buffer (current-buffer)))))
  1206.  
  1207.  
  1208. (defun mh-clean-msg-header (start invisible-headers visible-headers)
  1209.   ;; Flush extraneous lines in a message header, from the given POINT to the
  1210.   ;; end of the message header.  If VISIBLE-HEADERS is non-nil, it contains a
  1211.   ;; regular expression specifying the lines to display, otherwise
  1212.   ;; INVISIBLE-HEADERS contains a regular expression specifying lines to
  1213.   ;; delete from the header.
  1214.   (let ((case-fold-search t))
  1215.     (save-restriction
  1216.       (goto-char start)
  1217.       (if (search-forward "\n\n" nil t)
  1218.       (backward-char 2))
  1219.       (narrow-to-region start (point))
  1220.       (goto-char (point-min))
  1221.       (if visible-headers
  1222.       (while (< (point) (point-max))
  1223.         (beginning-of-line)
  1224.         (cond ((looking-at visible-headers)
  1225.            (forward-line 1)
  1226.            (while (looking-at "^[ \t]+") (forward-line 1)))
  1227.           (t
  1228.             (mh-delete-line 1)
  1229.             (while (looking-at "^[ \t]+")
  1230.               (beginning-of-line)
  1231.               (mh-delete-line 1)))))
  1232.       (while (re-search-forward invisible-headers nil t)
  1233.         (beginning-of-line)
  1234.         (mh-delete-line 1)
  1235.         (while (looking-at "^[ \t]+")
  1236.           (beginning-of-line)
  1237.           (mh-delete-line 1))))
  1238.       (unlock-buffer))))
  1239.  
  1240.  
  1241. (defun mh-delete-line (lines)
  1242.   ;; Delete version of kill-line.
  1243.   (delete-region (point) (save-excursion (forward-line lines) (point))))
  1244.  
  1245.  
  1246. (defun mh-read-draft (use initial-contents delete-contents-file)
  1247.   ;; Read draft file into a draft buffer and make that buffer the current one.
  1248.   ;; USE is a message used for prompting about the intended use of the message.
  1249.   ;; INITIAL-CONTENTS is filename that is read into an empty buffer, or NIL
  1250.   ;; if buffer should not be modified.  Delete the initial-contents file if
  1251.   ;; DELETE-CONTENTS-FILE flag is set.
  1252.   ;; Returns the draft folder's name.
  1253.   ;; If the draft folder facility is enabled in ~/.mh_profile, a new buffer is
  1254.   ;; used each time and saved in the draft folder.  The draft file can then be
  1255.   ;; reused.
  1256.   (cond (mh-draft-folder
  1257.      (let ((orig-default-dir default-directory))
  1258.        (pop-to-buffer (find-file-noselect (mh-new-draft-name) t))
  1259.        (rename-buffer (format "draft-%s" (buffer-name)))
  1260.        (setq default-directory orig-default-dir)))
  1261.     (t
  1262.      (let ((draft-name (expand-file-name "draft" mh-user-path)))
  1263.        (pop-to-buffer "draft")    ; Create if necessary
  1264.        (if (buffer-modified-p)
  1265.            (if (y-or-n-p "Draft has been modified; kill anyway? ")
  1266.            (set-buffer-modified-p nil)
  1267.            (error "Draft preserved")))
  1268.        (setq buffer-file-name draft-name)
  1269.        (clear-visited-file-modtime)
  1270.        (unlock-buffer)
  1271.        (mh-when (and (file-exists-p draft-name)
  1272.              (not (equal draft-name initial-contents)))
  1273.          (insert-file-contents draft-name)
  1274.          (delete-file draft-name)))))
  1275.   (mh-when (and initial-contents
  1276.         (or (zerop (buffer-size))
  1277.             (not (y-or-n-p
  1278.               (format "A draft exists.  Use for %s? " use)))))
  1279.     (erase-buffer)
  1280.     (insert-file-contents initial-contents)
  1281.     (if delete-contents-file (delete-file initial-contents)))
  1282.   (auto-save-mode 1)
  1283.   (if mh-draft-folder
  1284.       (save-buffer))            ; Do not reuse draft name
  1285.   (buffer-name))
  1286.  
  1287.  
  1288. (defun mh-new-draft-name ()
  1289.   ;; Returns the pathname of folder for draft messages.
  1290.   (save-excursion
  1291.     (set-buffer (get-buffer-create " *mh-temp*"))
  1292.     (erase-buffer)
  1293.     (mh-exec-cmd-output "mhpath" nil mh-draft-folder "new")
  1294.     (buffer-substring (point) (1- (mark)))))
  1295.  
  1296.  
  1297. (defun mh-next-msg ()
  1298.   ;; Move backward or forward to the next undeleted message in the buffer.
  1299.   (if (eq mh-next-direction 'forward)
  1300.       (mh-next-undeleted-msg 1)
  1301.       (mh-previous-undeleted-msg 1)))
  1302.  
  1303.  
  1304. (defun mh-set-scan-mode ()
  1305.   ;; Display the scan listing buffer, but do not show a message.
  1306.   (if (get-buffer mh-show-buffer)
  1307.       (delete-windows-on mh-show-buffer))
  1308.   (mh-set-mode-name "mh-e scan")
  1309.   (setq mh-showing nil)
  1310.   (if mh-recenter-summary-p
  1311.       (mh-recenter nil)))
  1312.  
  1313.  
  1314. (defun mh-maybe-show (&optional msg)
  1315.   ;; If in showing mode, then display the message pointed to by the cursor.
  1316.   (if mh-showing (mh-show msg)))
  1317.  
  1318.  
  1319. (defun mh-set-mode-name (mode-name-string)
  1320.   ;; Set the mode-name and ensure that the mode line is updated.
  1321.   (setq mode-name mode-name-string)
  1322.   ;; Force redisplay of all buffers' mode lines to be considered.
  1323.   (save-excursion (set-buffer (other-buffer)))
  1324.   (set-buffer-modified-p (buffer-modified-p)))
  1325.  
  1326.  
  1327.  
  1328. ;;; The folder data abstraction.
  1329.  
  1330. (defvar mh-current-folder nil "Name of current folder, a string.")
  1331. (defvar mh-show-buffer nil "Buffer that displays mesage for this folder.")
  1332. (defvar mh-folder-filename nil "Full path of directory for this folder.")
  1333. (defvar mh-showing nil "If non-nil, show the message in a separate window.")
  1334. (defvar mh-next-seq-num nil "Index of free sequence id.")
  1335. (defvar mh-delete-list nil "List of msg numbers to delete.")
  1336. (defvar mh-refile-list nil "List of folder names in mh-seq-list.")
  1337. (defvar mh-seq-list nil "Alist of (seq . msgs) numbers.")
  1338. (defvar mh-seen-list nil "List of displayed messages.")
  1339. (defvar mh-next-direction 'forward "Direction to move to next message.")
  1340. (defvar mh-narrowed-to-seq nil "Sequence display is narrowed to.")
  1341. (defvar mh-first-msg-num nil "Number of first msg in buffer.")
  1342. (defvar mh-last-msg-num nil "Number of last msg in buffer.")
  1343.  
  1344.  
  1345. (defun mh-make-folder (name)
  1346.   ;; Create and initialize a new mail folder called NAME and make it the
  1347.   ;; current folder.
  1348.   (switch-to-buffer name)
  1349.   (setq buffer-read-only nil)
  1350.   (erase-buffer)
  1351.   (setq buffer-read-only t)
  1352.   (mh-folder-mode)
  1353.   (mh-set-folder-modified-p nil)
  1354.   (setq buffer-file-name mh-folder-filename)
  1355.   (mh-set-mode-name "mh-e scan"))
  1356.  
  1357.  
  1358. ;;; Don't use this mode when creating buffers if default-major-mode is nil.
  1359. (put 'mh-folder-mode 'mode-class 'special)
  1360.  
  1361. (defun mh-folder-mode ()
  1362.   "Major mode for \"editing\" an MH folder scan listing.
  1363. Messages can be marked for refiling and deletion.  However, both actions
  1364. are deferred until you request execution with \\[mh-execute-commands].
  1365. \\{mh-folder-mode-map}
  1366.   A prefix argument (\\[universal-argument]) to delete, refile, list, or undo
  1367. applies the action to a message sequence.
  1368.  
  1369. Variables controlling mh-e operation are (defaults in parentheses):
  1370.  
  1371.  mh-bury-show-buffer (t)
  1372.     Non-nil means that the buffer used to display message is buried.
  1373.     It will never be offered as the default other buffer.
  1374.  
  1375.  mh-clean-message-header (nil)
  1376.     Non-nil means remove header lines matching the regular expression
  1377.     specified in mh-invisible-headers from messages.
  1378.  
  1379.  mh-visible-headers (nil)
  1380.     If non-nil, it contains a regexp specifying the headers that are shown in
  1381.     a message if mh-clean-message-header is non-nil.  Setting this variable
  1382.     overrides mh-invisible-headers.
  1383.  
  1384.  mh-do-not-confirm (nil)
  1385.     Non-nil means do not prompt for confirmation before executing some
  1386.     non-recoverable commands such as mh-kill-folder and mh-undo-folder.
  1387.  
  1388.  mhl-formfile (nil)
  1389.     Name of format file to be used by mhl to show messages.
  1390.     A value of T means use the default format file.
  1391.     Nil means don't use mhl to format messages.
  1392.  
  1393.  mh-lpr-command-format (\"lpr -p -J '%s'\")
  1394.     Format for command used to print a message on a system printer.
  1395.  
  1396.  mh-recenter-summary-p (nil)
  1397.     If non-nil, then the scan listing is recentered when the window displaying
  1398.     a messages is toggled off.
  1399.  
  1400.  mh-summary-height (4)
  1401.     Number of lines in the summary window.
  1402.  
  1403.  mh-ins-buf-prefix (\">> \")
  1404.     String to insert before each non-blank line of a message as it is
  1405.     inserted in a draft letter.
  1406.  
  1407. The value of mh-folder-mode-hook is called when a new folder is set up."
  1408.  
  1409.   (kill-all-local-variables)
  1410.   (use-local-map mh-folder-mode-map)
  1411.   (setq major-mode 'mh-folder-mode)
  1412.   (mh-set-mode-name "mh-e folder")
  1413.   (make-local-vars
  1414.    'mh-current-folder (buffer-name)    ; Name of folder, a string
  1415.    'mh-show-buffer (format "show-%s" (buffer-name)) ; Buffer that displays msgs
  1416.    'mh-folder-filename            ; e.g. "/usr/foobar/Mail/inbox/"
  1417.    (file-name-as-directory (mh-expand-file-name (buffer-name)))
  1418.    'mh-showing nil            ; Show message also?
  1419.    'mh-next-seq-num 0            ; Index of free sequence id
  1420.    'mh-delete-list nil            ; List of msgs nums to delete
  1421.    'mh-refile-list nil            ; List of folder names in mh-seq-list
  1422.    'mh-seq-list nil            ; Alist of (seq . msgs) nums
  1423.    'mh-seen-list nil            ; List of displayed messages
  1424.    'mh-next-direction 'forward        ; Direction to move to next message
  1425.    'mh-narrowed-to-seq nil        ; Sequence display is narrowed to
  1426.    'mh-first-msg-num nil        ; Number of first msg in buffer
  1427.    'mh-last-msg-num nil            ; Number of last msg in buffer
  1428.    'mh-previous-window-config nil)    ; Previous window configuration
  1429.   (auto-save-mode -1)
  1430.   (setq buffer-offer-save t)
  1431.   (make-local-variable 'write-file-hooks)
  1432.   (setq write-file-hooks '(mh-execute-commands))
  1433.   (make-local-variable 'revert-buffer-function)
  1434.   (setq revert-buffer-function 'mh-undo-folder)
  1435.   (run-hooks 'mh-folder-mode-hook))
  1436.  
  1437.  
  1438. (defun make-local-vars (&rest pairs)
  1439.   ;; Take VARIABLE-VALUE pairs and makes local variables initialized to the
  1440.   ;; value.
  1441.   (while pairs
  1442.     (make-variable-buffer-local (car pairs))
  1443.     (set (car pairs) (car (cdr pairs)))
  1444.     (setq pairs (cdr (cdr pairs)))))
  1445.  
  1446.  
  1447. (defun mh-scan-folder (folder range)
  1448.   ;; Scan the FOLDER over the RANGE.  Return in the folder's buffer.
  1449.   (cond ((null (get-buffer folder))
  1450.      (mh-make-folder folder))
  1451.     (t
  1452.      (mh-process-or-undo-commands folder)
  1453.      (switch-to-buffer folder)))
  1454.   (mh-regenerate-headers range)
  1455.   (mh-when (zerop (count-lines (point-min) (point-max)))
  1456.     (if (equal range "all")
  1457.     (message "Folder %s is empty" folder)
  1458.     (message "No messages in %s, range %s" folder range))
  1459.     (sit-for 5))
  1460.   (mh-goto-cur-msg))
  1461.  
  1462.  
  1463. (defun mh-regenerate-headers (range)
  1464.   ;; Replace buffer with scan of its contents over range RANGE.
  1465.   (let ((folder mh-current-folder))
  1466.     (message "Scanning %s..." folder)
  1467.     (with-mh-folder-updating (nil)
  1468.       (erase-buffer)
  1469.       (mh-exec-cmd-output "scan" nil
  1470.               "-noclear" "-noheader"
  1471.               "-width" (window-width)
  1472.               folder range)
  1473.       (goto-char (point-min))
  1474.       (cond ((looking-at "scan: no messages in")
  1475.          (keep-lines mh-valid-scan-line)) ; Flush random scan lines
  1476.         ((looking-at "scan: "))    ; Keep error messages
  1477.         (t
  1478.          (keep-lines mh-valid-scan-line))) ; Flush random scan lines
  1479.       (mh-delete-seq-locally 'cur)    ; To pick up new one
  1480.       (setq mh-seq-list (mh-read-folder-sequences folder nil))
  1481.       (mh-notate-user-sequences)
  1482.       (mh-make-folder-mode-line (if (equal range "all") nil "select")))
  1483.     (message "Scanning %s...done" folder)))
  1484.  
  1485.  
  1486. (defun mh-get-new-mail (maildrop-name)
  1487.   ;; Read new mail from a maildrop into the current buffer.
  1488.   ;; Return T if there was new mail, NIL otherwise.  Return in the current
  1489.   ;; buffer.
  1490.   (let ((point-before-inc (point))
  1491.     (folder mh-current-folder)
  1492.     (return-value t))
  1493.     (with-mh-folder-updating (t)
  1494.       (message (if maildrop-name
  1495.            (format "inc %s -file %s..." folder maildrop-name)
  1496.            (format "inc %s..." folder)))
  1497.       (mh-unmark-all-headers nil)
  1498.       (setq mh-next-direction 'forward)
  1499.       (goto-char (point-max))
  1500.       (let ((start-of-inc (point)))
  1501.     (if maildrop-name
  1502.         (mh-exec-cmd-output "inc" nil folder
  1503.                 "-file" (expand-file-name maildrop-name)
  1504.                 "-width" (window-width)
  1505.                 "-truncate")
  1506.         (mh-exec-cmd-output "inc" nil
  1507.                 "-width" (window-width)))
  1508.     (message
  1509.      (if maildrop-name
  1510.          (format "inc %s -file %s...done" folder maildrop-name)
  1511.          (format "inc %s...done" folder)))
  1512.     (goto-char start-of-inc)
  1513.     (cond ((looking-at "inc: no mail")
  1514.            (keep-lines mh-valid-scan-line) ; Flush random scan lines
  1515.            (goto-char point-before-inc)
  1516.            (message "No new mail%s%s" (if maildrop-name " in " "")
  1517.             (if maildrop-name maildrop-name "")))
  1518.           ((re-search-forward "^inc:" nil t) ; Error messages
  1519.            (error "inc error"))
  1520.           (t
  1521.            (mh-delete-seq-locally 'cur) ; To pick up new one
  1522.            (setq mh-seq-list (mh-read-folder-sequences folder t))
  1523.            (mh-notate-user-sequences)
  1524.            (keep-lines mh-valid-scan-line)
  1525.            (mh-make-folder-mode-line)
  1526.            (mh-goto-cur-msg)
  1527.            (setq return-value t))))
  1528.       return-value)))
  1529.  
  1530.  
  1531. (defun mh-make-folder-mode-line (&optional annotation)
  1532.   ;; Set the fields of the mode line for a folder buffer.
  1533.   ;; The optional ANNOTATION string is displayed after the folder's name.
  1534.   (save-excursion
  1535.     (goto-char (point-min))
  1536.     (setq mh-first-msg-num (mh-get-msg-num nil))
  1537.     (goto-char (point-max))
  1538.     (let ((lines-checked 0))
  1539.       (while (and (< (setq lines-checked (1+ lines-checked)) 5)
  1540.           (not (setq mh-last-msg-num (mh-get-msg-num nil))))
  1541.     (forward-line -1)))
  1542.     (let ((lines (count-lines (point-min) (point-max))))
  1543.       (setq mode-line-buffer-identification
  1544.         (list (format "{%%b%s}  %d msg%s"
  1545.               (if annotation (format "/%s" annotation) "")
  1546.               lines
  1547.               (if (zerop lines)
  1548.                   "s"
  1549.                   (if (> lines 1)
  1550.                   (format "s (%d-%d)" mh-first-msg-num
  1551.                       mh-last-msg-num)
  1552.                   (format " (%d)" mh-first-msg-num)))))))))
  1553.  
  1554.  
  1555. (defun mh-unmark-all-headers (remove-all-flags)
  1556.   ;; Remove all '+' flags from the headers, and if called with a non-nil
  1557.   ;; argument, remove all 'D', '^' and '%' flags too.
  1558.   ;; Optimized for speed (i.e., no regular expressions).
  1559.   (save-excursion
  1560.     (let ((case-fold-search nil)
  1561.       (last-line (- (point-max) mh-cmd-note))
  1562.       char)
  1563.       (goto-char (point-min))
  1564.       (while (<= (point) last-line)
  1565.     (forward-char mh-cmd-note)
  1566.     (setq char (following-char))
  1567.     (if (or (and remove-all-flags
  1568.              (or (eql char ?D)
  1569.              (eql char ?^)
  1570.              (eql char ?%)))
  1571.         (eql char ?+))
  1572.         (progn
  1573.           (delete-char 1)
  1574.           (insert " ")))
  1575.     (forward-line)))))
  1576.  
  1577.  
  1578. (defun mh-goto-cur-msg ()
  1579.   ;; Position the cursor at the current message.
  1580.   (let ((cur-msg (car (mh-seq-to-msgs 'cur))))
  1581.     (cond ((and cur-msg
  1582.         (mh-goto-msg cur-msg t nil))
  1583.        (mh-notate nil ?+ mh-cmd-note)
  1584.        (mh-recenter 0)
  1585.        (mh-maybe-show cur-msg))
  1586.       (t
  1587.        (goto-char (point-max))
  1588.        (forward-line -1)
  1589.        (message "No current message")))))
  1590.  
  1591.  
  1592. (defun mh-pack-folder-1 (range)
  1593.   ;; Close and pack the current folder.
  1594.   (mh-process-or-undo-commands mh-current-folder)
  1595.   (message "Packing folder...")
  1596.   (mh-set-folder-modified-p t)        ; lock folder while packing
  1597.   (save-excursion
  1598.     (mh-exec-cmd-quiet " *mh-temp*" "folder" mh-current-folder "-pack"))
  1599.   (mh-regenerate-headers range))
  1600.  
  1601.  
  1602. (defun mh-process-or-undo-commands (folder)
  1603.   ;; If FOLDER has outstanding commands, then either process or discard them.
  1604.   (set-buffer folder)
  1605.   (if (mh-outstanding-commands-p)
  1606.       (if (or mh-do-not-confirm
  1607.           (y-or-n-p
  1608.         "Process outstanding deletes and refiles (or lose them)? "))
  1609.       (mh-process-commands folder)
  1610.       (mh-undo-folder))
  1611.       (mh-invalidate-show-buffer)))
  1612.  
  1613.  
  1614. (defun mh-process-commands (folder)
  1615.   ;; Process outstanding commands for the folder FOLDER.
  1616.   (message "Processing deletes and refiles for %s..." folder)
  1617.   (set-buffer folder)
  1618.   (with-mh-folder-updating (nil)
  1619.     ;; Update the unseen sequence if it exists
  1620.     (if (and mh-seen-list (mh-seq-to-msgs mh-unseen-seq))
  1621.     (mh-undefine-sequence mh-unseen-seq mh-seen-list))
  1622.  
  1623.     ;; Then refile messages
  1624.     (mh-mapc
  1625.      (function
  1626.       (lambda (dest)
  1627.     (let ((msgs (mh-seq-to-msgs dest)))
  1628.       (mh-when msgs
  1629.         (apply 'mh-exec-cmd "refile"
  1630.            "-src" folder (symbol-name dest) msgs)
  1631.         (mh-delete-scan-msgs msgs)))))
  1632.      mh-refile-list)
  1633.  
  1634.     ;; Now delete messages
  1635.     (mh-when mh-delete-list
  1636.       (apply 'mh-exec-cmd "rmm" folder mh-delete-list)
  1637.       (mh-delete-scan-msgs mh-delete-list))
  1638.  
  1639.     ;; Don't need to remove sequences since delete and refile do so.
  1640.  
  1641.     ;; Mark cur message
  1642.     (if (> (buffer-size) 0)
  1643.     (mh-define-sequence 'cur (list (or (mh-get-msg-num nil) "last"))))
  1644.  
  1645.     (mh-invalidate-show-buffer)
  1646.  
  1647.     (setq mh-delete-list nil
  1648.       mh-refile-list nil
  1649.       mh-seq-list (mh-read-folder-sequences mh-current-folder nil)
  1650.       mh-seen-list nil)
  1651.     (mh-unmark-all-headers t)
  1652.     (mh-notate-user-sequences)
  1653.     (message "Processing deletes and refiles for %s...done" folder)))
  1654.  
  1655.  
  1656. (defun mh-delete-scan-msgs (msgs)
  1657.   ;; Delete the scan listing lines for each of the msgs in the LIST.
  1658.   ;; Optimized for speed (i.e., no regular expressions).
  1659.   (setq msgs (sort msgs (function <)))    ;okay to clobber msgs
  1660.   (save-excursion
  1661.     (goto-char (point-min))
  1662.     (while (and msgs (< (point) (point-max)))
  1663.       (cond ((= (mh-get-msg-num nil) (car msgs))
  1664.          (delete-region (point) (save-excursion (forward-line) (point)))
  1665.          (setq msgs (cdr msgs)))
  1666.         (t
  1667.          (forward-line))))))
  1668.  
  1669.  
  1670. (defun mh-set-folder-modified-p (flag)
  1671.   "Mark current folder as modified or unmodified according to FLAG."
  1672.   (set-buffer-modified-p flag))
  1673.  
  1674.  
  1675. (defun mh-outstanding-commands-p ()
  1676.   ;; Returns non-nil if there are outstanding deletes or refiles.
  1677.   (or mh-delete-list mh-refile-list))
  1678.  
  1679.  
  1680.  
  1681. ;;; Mode for composing and sending a draft message.
  1682.  
  1683. (defvar mh-sent-from-folder nil
  1684.   "Folder of msg associated with this letter.")
  1685.  
  1686. (defvar mh-sent-from-msg nil
  1687.   "Number of msg associated with this letter.")
  1688.  
  1689. (defvar mh-send-args nil
  1690.   "Extra arguments to pass to \"send\" command.")
  1691.  
  1692. (defvar mh-annotate-char nil
  1693.   "Character to use to annotate mh-sent-from-msg.")
  1694.  
  1695. (defvar mh-annotate-field nil
  1696.   "Field name for message annotation.")
  1697.  
  1698. (defun mh-letter-mode ()
  1699.   "Mode for composing letters in mh-e.
  1700. When you have finished composing, type \\[mh-send-letter] to send the letter.
  1701.  
  1702. Variables controlling this mode (defaults in parentheses):
  1703.  
  1704.  mh-delete-yanked-msg-window (nil)
  1705.     If non-nil, \\[mh-yank-cur-msg] will delete any windows displaying
  1706.     the yanked message.
  1707.  
  1708.  mh-yank-from-start-of-msg (t)
  1709.     If non-nil, \\[mh-yank-cur-msg] will include the entire message.
  1710.     If `body', just yank the body (no header).
  1711.     If nil, only the portion of the message following the point will be yanked.
  1712.     If there is a region, this variable is ignored.
  1713.  
  1714. Upon invoking mh-letter-mode, text-mode-hook and mh-letter-mode-hook are
  1715. invoked with no args, if those values are non-nil.
  1716.  
  1717. \\{mh-letter-mode-map}"
  1718.   (interactive)
  1719.   (kill-all-local-variables)
  1720.   (make-local-variable 'paragraph-start)
  1721.   (setq paragraph-start (concat "^[ \t]*[-_][-_][-_]+$\\|" paragraph-start))
  1722.   (make-local-variable 'paragraph-separate)
  1723.   (setq paragraph-separate
  1724.     (concat "^[ \t]*[-_][-_][-_]+$\\|" paragraph-separate))
  1725.   (make-local-variable 'mh-send-args)
  1726.   (make-local-variable 'mh-annotate-char)
  1727.   (make-local-variable 'mh-annotate-field)
  1728.   (make-local-variable 'mh-previous-window-config)
  1729.   (make-local-variable 'mh-sent-from-folder)
  1730.   (make-local-variable 'mh-sent-from-msg)
  1731.   (use-local-map mh-letter-mode-map)
  1732.   (setq major-mode 'mh-letter-mode)
  1733.   (mh-set-mode-name "mh-e letter")
  1734.   (set-syntax-table mh-letter-mode-syntax-table)
  1735.   (run-hooks 'text-mode-hook 'mh-letter-mode-hook))
  1736.  
  1737.  
  1738. (defun mh-to-field ()
  1739.   "Move point to the end of a specified header field.
  1740. The field is indicated by the previous keystroke.  Create the field if
  1741. it does not exist.  Set the mark to point before moving."
  1742.   (interactive "")
  1743.   (expand-abbrev)
  1744.   (let ((target (cdr (assoc (logior last-input-char ?`) mh-to-field-choices)))
  1745.     (case-fold-search t))
  1746.     (cond ((mh-position-on-field target t)
  1747.        (if (not (looking-at "[ \t]")) (insert " ")))
  1748.       (t
  1749.        (goto-char (dot-min))
  1750.        (re-search-forward "^To:")
  1751.        (forward-line 1)
  1752.        (while (looking-at "^[ \t]") (forward-line 1))
  1753.        (insert (format "%s \n" target))
  1754.        (backward-char 1)))))
  1755.  
  1756.  
  1757. (defun mh-to-fcc ()
  1758.   "Insert a Fcc: field in the current message.
  1759. Prompt for the field name with a completion list of the current folders."
  1760.   (interactive)
  1761.   (expand-abbrev)
  1762.   (save-excursion
  1763.     (mh-insert-fields "Fcc:"
  1764.               (substring (mh-prompt-for-folder "Fcc" "" t) 1 nil))))
  1765.  
  1766.  
  1767. (defun mh-insert-signature ()
  1768.   "Insert the file ~/.signature at the current point."
  1769.   (interactive "")
  1770.   (insert-file-contents "~/.signature")
  1771.   (set-buffer-modified-p (buffer-modified-p))) ; force mode line update
  1772.  
  1773.  
  1774. (defun mh-check-whom ()
  1775.   "Verify recipients of the current letter."
  1776.   (interactive)
  1777.   (let ((file-name (buffer-file-name)))
  1778.     (set-buffer-modified-p t)        ; Force writing of contents
  1779.     (save-buffer)
  1780.     (message "Checking recipients...")
  1781.     (switch-to-buffer-other-window "*Mail Recipients*")
  1782.     (bury-buffer (current-buffer))
  1783.     (erase-buffer)
  1784.     (mh-exec-cmd-output "whom" t file-name)
  1785.     (other-window -1)
  1786.     (message "Checking recipients...done")))
  1787.  
  1788.  
  1789.  
  1790. ;;; Routines to make a search pattern and search for a message.
  1791.  
  1792. (defvar mh-searching-folder nil "Folder this pick is searching.")
  1793.  
  1794.  
  1795. (defun mh-make-pick-template ()
  1796.   ;; Initialize the current buffer with a template for a pick pattern.
  1797.   (erase-buffer)
  1798.   (kill-all-local-variables)
  1799.   (make-local-variable 'mh-searching-folder)
  1800.   (insert "From: \n"
  1801.       "To: \n"
  1802.       "Cc: \n"
  1803.       "Date: \n"
  1804.       "Subject: \n"
  1805.       "---------\n")
  1806.   (mh-letter-mode)
  1807.   (use-local-map mh-pick-mode-map)
  1808.   (goto-char (point-min))
  1809.   (end-of-line))
  1810.  
  1811.  
  1812. (defun mh-do-pick-search ()
  1813.   "Find messages that match the qualifications in the current pattern buffer.
  1814. Messages are searched for in the folder named in mh-searching-folder.
  1815. Put messages found in a sequence named `search'."
  1816.   (interactive)
  1817.   (let ((pattern-buffer (buffer-name))
  1818.     (searching-buffer mh-searching-folder)
  1819.     (range)
  1820.     (pattern nil)
  1821.     (new-buffer nil))
  1822.     (save-excursion
  1823.       (cond ((get-buffer searching-buffer)
  1824.          (set-buffer searching-buffer)
  1825.          (setq range (format "%d-%d" mh-first-msg-num mh-last-msg-num)))
  1826.         (t
  1827.          (mh-make-folder searching-buffer)
  1828.          (setq range "all")
  1829.          (setq new-buffer t))))
  1830.     (message "Searching...")
  1831.     (goto-char (point-min))
  1832.     (while (setq pattern (mh-next-pick-field pattern-buffer))
  1833.       (setq msgs (mh-seq-from-command searching-buffer
  1834.                       'search
  1835.                       (nconc (cons "pick" pattern)
  1836.                          (list searching-buffer
  1837.                            range
  1838.                            "-sequence" "search"
  1839.                            "-list"))))
  1840.       (setq range "search"))
  1841.     (message "Searching...done")
  1842.     (if new-buffer
  1843.     (mh-scan-folder searching-buffer msgs)
  1844.     (switch-to-buffer searching-buffer))
  1845.     (delete-other-windows)
  1846.     (mh-notate-seq 'search ?% (1+ mh-cmd-note))))
  1847.  
  1848.  
  1849. (defun mh-next-pick-field (buffer)
  1850.   ;; Return the next piece of a pick argument that can be extracted from the
  1851.   ;; BUFFER.  Returns nil if no pieces remain.
  1852.   (set-buffer buffer)
  1853.   (let ((case-fold-search t))
  1854.     (cond ((eobp)
  1855.        nil)
  1856.       ((re-search-forward "^\\([a-z].*\\):[ \t]*\\([a-z0-9].*\\)$" nil t)
  1857.        (let* ((component
  1858.            (format "--%s"
  1859.                (downcase (buffer-substring (match-beginning 1)
  1860.                                (match-end 1)))))
  1861.           (pat (buffer-substring (match-beginning 2) (match-end 2))))
  1862.            (forward-line 1)
  1863.            (list component pat)))
  1864.       ((re-search-forward "^-*$" nil t)
  1865.        (forward-char 1)
  1866.        (let ((body (buffer-substring (point) (point-max))))
  1867.          (if (and (> (length body) 0) (not (equal body "\n")))
  1868.          (list "-search" body)
  1869.          nil)))
  1870.       (t
  1871.        nil))))
  1872.  
  1873.  
  1874.  
  1875. ;;; Routines to compose and send a letter.
  1876.  
  1877. (defun mh-compose-and-send-mail (draft send-args
  1878.                        sent-from-folder sent-from-msg
  1879.                        to subject cc
  1880.                        annotate-char annotate-field
  1881.                        config)
  1882.   ;; Edit and compose a draft message in buffer DRAFT and send or save it.
  1883.   ;; SENT-FROM-FOLDER is buffer containing scan listing of current folder, or
  1884.   ;; nil if none exists.
  1885.   ;; SENT-FROM-MSG is the message number or sequence name or nil.
  1886.   ;; SEND-ARGS is an optional argument passed to the send command.
  1887.   ;; The TO, SUBJECT, and CC fields are passed to the
  1888.   ;; mh-compose-letter-function.
  1889.   ;; If ANNOTATE-CHAR is non-null, it is used to notate the scan listing of the
  1890.   ;; message.  In that case, the ANNOTATE-FIELD is used to build a string
  1891.   ;; for mh-annotate-msg.
  1892.   ;; CONFIG is the window configuration to restore after sending the letter.
  1893.   (pop-to-buffer draft)
  1894.   (mh-letter-mode)
  1895.   (setq mh-sent-from-folder sent-from-folder)
  1896.   (setq mh-sent-from-msg sent-from-msg)
  1897.   (setq mh-send-args send-args)
  1898.   (setq mh-annotate-char annotate-char)
  1899.   (setq mh-annotate-field annotate-field)
  1900.   (setq mh-previous-window-config config)
  1901.   (setq mode-line-buffer-identification (list "{%b}"))
  1902.   (if (and (boundp 'mh-compose-letter-function)
  1903.        (symbol-value 'mh-compose-letter-function))
  1904.       ;; run-hooks will not pass arguments.
  1905.       (let ((value (symbol-value 'mh-compose-letter-function)))
  1906.     (if (and (listp value) (not (eq (car value) 'lambda)))
  1907.         (while value
  1908.           (funcall (car value) to subject cc)
  1909.           (setq value (cdr value)))
  1910.         (funcall mh-compose-letter-function to subject cc)))))
  1911.  
  1912.  
  1913. (defun mh-send-letter (&optional arg)
  1914.   "Send the draft letter in the current buffer.
  1915. If (optional) prefix argument is provided, monitor delivery.
  1916. Run mh-before-send-letter-hook before doing anything."
  1917.   (interactive "P")
  1918.   (run-hooks 'mh-before-send-letter-hook)
  1919.   (set-buffer-modified-p t)        ; Make sure buffer is written
  1920.   (save-buffer)
  1921.   (message "Sending...")
  1922.   (let ((draft-buffer (current-buffer))
  1923.     (file-name (buffer-file-name))
  1924.     (config mh-previous-window-config))
  1925.     (cond (arg
  1926.        (pop-to-buffer "MH mail delivery")
  1927.        (erase-buffer)
  1928.        (if mh-send-args
  1929.            (mh-exec-cmd-output "send" t "-watch" "-nopush"
  1930.                    "-nodraftfolder" mh-send-args file-name)
  1931.            (mh-exec-cmd-output "send" t "-watch" "-nopush"
  1932.                    "-nodraftfolder" file-name))
  1933.        (set-buffer draft-buffer))    ; for annotation below
  1934.       (mh-send-args
  1935.        (mh-exec-cmd-daemon "send" "-nodraftfolder" "-noverbose"
  1936.                    mh-send-args file-name))
  1937.       (t
  1938.        (mh-exec-cmd-daemon "send" "-nodraftfolder" "-noverbose"
  1939.                    file-name)))
  1940.  
  1941.     (if mh-annotate-char
  1942.     (mh-annotate-msg mh-sent-from-msg
  1943.              mh-sent-from-folder
  1944.              mh-annotate-char
  1945.              "-component" mh-annotate-field
  1946.              "-text" (format "\"%s %s\""
  1947.                      (mh-get-field "To:")
  1948.                      (mh-get-field "Cc:"))))
  1949.  
  1950.     (mh-when (or (not arg)
  1951.          (y-or-n-p "Kill draft buffer? "))
  1952.       (kill-buffer draft-buffer)
  1953.       (if config
  1954.       (set-window-configuration config)))
  1955.     (message "Sending...done")))
  1956.  
  1957.  
  1958. (defun mh-insert-letter (prefix-provided folder msg)
  1959.   "Insert a message from any folder into the current letter.
  1960. Removes the message's headers using mh-invisible-headers.
  1961. Prefixes each non-blank line with mh-ins-buf-prefix (default \">> \").
  1962. If (optional) prefix argument provided, do not indent and do not delete
  1963. headers.
  1964. Leaves the mark before the letter and point after it."
  1965.   (interactive
  1966.    (list current-prefix-arg
  1967.      (mh-prompt-for-folder "Message from" mh-sent-from-folder nil)
  1968.      (read-input (format "Message number%s: "
  1969.                  (if mh-sent-from-msg
  1970.                  (format " [%d]" mh-sent-from-msg)
  1971.                  "")))))
  1972.   (save-restriction
  1973.     (narrow-to-region (point) (point))
  1974.     (let ((start (point-min)))
  1975.       (if (equal msg "") (setq msg (int-to-string mh-sent-from-msg)))
  1976.       (mh-exec-lib-cmd-output "mhl" "-nobell" "-noclear"
  1977.                   (expand-file-name msg
  1978.                         (mh-expand-file-name folder)))
  1979.       (mh-when (not prefix-provided)
  1980.     (mh-clean-msg-header start mh-invisible-headers mh-visible-headers)
  1981.     (set-mark start)        ; since mh-clean-msg-header moves it
  1982.     (mh-insert-prefix-string mh-ins-buf-prefix)))))
  1983.  
  1984.  
  1985. (defun mh-yank-cur-msg ()
  1986.   "Insert the current message into the draft buffer.
  1987. Prefix each non-blank line in the message with the string in
  1988. mh-ins-buf-prefix.  If a region is set in the message's buffer, then
  1989. only the region will be inserted.  Otherwise, the entire message will
  1990. be inserted if mh-yank-from-start-of-msg is non-nil.  If this variable
  1991. is nil, the portion of the message following the point will be yanked.
  1992. If mh-delete-yanked-msg-window is non-nil, any window displaying the
  1993. yanked message will be deleted."
  1994.   (interactive)
  1995.   (if (and mh-sent-from-folder mh-sent-from-msg)
  1996.       (let ((to-point (point))
  1997.         (to-buffer (current-buffer)))
  1998.     (set-buffer mh-sent-from-folder)
  1999.     (if mh-delete-yanked-msg-window
  2000.         (delete-windows-on mh-show-buffer))
  2001.     (set-buffer mh-show-buffer)    ; Find displayed message
  2002.     (let ((mh-ins-str (cond ((mark)
  2003.                  (buffer-substring (point) (mark)))
  2004.                 ((eq 'body mh-yank-from-start-of-msg)
  2005.                  (buffer-substring
  2006.                   (save-excursion
  2007.                     (mh-goto-header-end 1)
  2008.                     (point))
  2009.                   (point-max)))
  2010.                 (mh-yank-from-start-of-msg
  2011.                  (buffer-substring (point-min) (point-max)))
  2012.                 (t
  2013.                  (buffer-substring (point) (point-max))))))
  2014.       (set-buffer to-buffer)
  2015.       (narrow-to-region to-point to-point)
  2016.       (push-mark)
  2017.       (insert mh-ins-str)
  2018.       (mh-insert-prefix-string mh-ins-buf-prefix)
  2019.       (insert "\n")
  2020.       (widen)))
  2021.       (error "There is no current message")))
  2022.  
  2023.  
  2024. (defun mh-insert-prefix-string (ins-string)
  2025.   ;; Preface each line in the current buffer with STRING.
  2026.   (goto-char (point-min))
  2027.   (while (not (eobp))
  2028.     (insert ins-string)
  2029.     (forward-line 1)))
  2030.  
  2031.  
  2032. (defun mh-fully-kill-draft ()
  2033.   "Kill the draft message file and the draft message buffer.
  2034. Use \\[kill-buffer] if you don't want to delete the draft message file."
  2035.   (interactive "")
  2036.   (if (y-or-n-p "Kill draft message? ")
  2037.       (let ((config mh-previous-window-config))
  2038.     (if (file-exists-p (buffer-file-name))
  2039.         (delete-file (buffer-file-name)))
  2040.     (set-buffer-modified-p nil)
  2041.     (kill-buffer (buffer-name))
  2042.     (message "")
  2043.     (if config
  2044.         (set-window-configuration config)))
  2045.     (error "Message not killed")))
  2046.  
  2047.  
  2048. (defun mh-recenter (arg)
  2049.   ;; Like recenter but with two improvements: nil arg means recenter,
  2050.   ;; and only does anything if the current buffer is in the selected
  2051.   ;; window.  (Commands like save-some-buffers can make this false.)
  2052.   (if (eql (get-buffer-window (current-buffer))
  2053.        (selected-window))
  2054.       (recenter (if arg arg '(t)))))
  2055.  
  2056.  
  2057.  
  2058. ;;; Commands to manipulate sequences.  Sequences are stored in an alist
  2059. ;;; of the form:
  2060. ;;;    ((seq-name msgs ...) (seq-name msgs ...) ...)
  2061.  
  2062. (defun mh-make-seq (name msgs) (cons name msgs))
  2063.  
  2064. (defmacro mh-seq-name (pair) (list 'car pair))
  2065.  
  2066. (defmacro mh-seq-msgs (pair) (list 'cdr pair))
  2067.  
  2068. (defun mh-find-seq (name) (assoc name mh-seq-list))
  2069.  
  2070.  
  2071. (defun mh-seq-to-msgs (seq)
  2072.   "Return a list of the messages in SEQUENCE."
  2073.   (mh-seq-msgs (mh-find-seq seq)))
  2074.  
  2075.  
  2076. (defun mh-seq-containing-msg (msg)
  2077.   ;; Return a list of the sequences containing MESSAGE.
  2078.   (let ((l mh-seq-list)
  2079.     (seqs ()))
  2080.     (while l
  2081.       (if (memq msg (mh-seq-msgs (car l)))
  2082.       (mh-push (mh-seq-name (car l)) seqs))
  2083.       (setq l (cdr l)))
  2084.     seqs))
  2085.  
  2086.  
  2087. (defun mh-msg-to-seq (msg)
  2088.   ;; Given a MESSAGE number, return the first sequence in which it occurs.
  2089.   (car (mh-seq-containing-msg msg)))
  2090.  
  2091.  
  2092. (defun mh-read-seq-default (prompt not-empty)
  2093.   ; Read and return sequence name with default narrowed or previous sequence.
  2094.   (mh-read-seq prompt not-empty (or mh-narrowed-to-seq mh-previous-seq)))
  2095.  
  2096.  
  2097. (defun mh-read-seq (prompt not-empty &optional default)
  2098.   ;; Read and return a sequence name.  Prompt with PROMPT, raise an error
  2099.   ;; if the sequence is empty and the NOT-EMPTY flag is non-nil, and supply
  2100.   ;; an optional DEFAULT sequence.
  2101.   ;; A reply of '%' defaults to the first sequence containing the current
  2102.   ;; message.
  2103.   (let* ((input (completing-read (format "%s %s %s" prompt "sequence:"
  2104.                      (if default
  2105.                          (format "[%s] " default)
  2106.                          ""))
  2107.                  (mh-seq-names mh-seq-list)))
  2108.      (seq (cond ((equal input "%") (mh-msg-to-seq (mh-get-msg-num t)))
  2109.             ((equal input "") default)
  2110.             (t (intern input))))
  2111.      (msgs (mh-seq-to-msgs seq)))
  2112.     (if (and (null msgs) not-empty)
  2113.     (error (format "No messages in sequence `%s'" seq)))
  2114.     seq))
  2115.  
  2116.  
  2117. (defun mh-read-folder-sequences (folder define-sequences)
  2118.   ;; Read and return the predefined sequences for a FOLDER.  If
  2119.   ;; DEFINE-SEQUENCES is non-nil, then define mh-e's sequences before
  2120.   ;; reading MH's sequences.
  2121.   (let ((seqs ()))
  2122.     (mh-when define-sequences
  2123.       (mh-define-sequences mh-seq-list)
  2124.       (mh-mapc (function (lambda (seq)    ; Save the internal sequences
  2125.                (if (mh-folder-name-p (mh-seq-name seq))
  2126.                    (mh-push seq seqs))))
  2127.            mh-seq-list))
  2128.     (save-excursion
  2129.       (mh-exec-cmd-quiet " *mh-temp*" "mark" folder "-list")
  2130.       (goto-char (point-min))
  2131.       (while (re-search-forward "^[a-zA-Z]+" nil t)
  2132.     (mh-push (mh-make-seq (intern (buffer-substring (match-beginning 0)
  2133.                             (match-end 0)))
  2134.                   (mh-read-msg-list))
  2135.          seqs))
  2136.       (delete-region (point-min) (point))) ; avoid race with mh-process-daemon
  2137.     seqs))
  2138.  
  2139.  
  2140. (defun mh-seq-names (seq-list)
  2141.   ;; Return an alist containing the names of the SEQUENCES.
  2142.   (mapcar (function (lambda (entry) (list (symbol-name (mh-seq-name entry)))))
  2143.       seq-list))
  2144.  
  2145.  
  2146. (defun mh-seq-from-command (folder seq seq-command)
  2147.   ;; In FOLDER, make a sequence named SEQ by executing COMMAND.
  2148.   ;; COMMAND is a list.  The first element is a program name
  2149.   ;; and the subsequent elements are its arguments, all strings.
  2150.   (let ((msg)
  2151.     (msgs ())
  2152.     (case-fold-search t))
  2153.     (save-excursion
  2154.       (save-window-excursion
  2155.     (apply 'mh-exec-cmd-quiet " *mh-temp*" seq-command)
  2156.     (goto-char (point-min))
  2157.     (while (setq msg (car (mh-read-msg-list)))
  2158.       (mh-push msg msgs)
  2159.       (forward-line 1)))
  2160.       (set-buffer folder)
  2161.       (setq msgs (nreverse msgs))    ; Put in ascending order
  2162.       (mh-push (mh-make-seq seq msgs) mh-seq-list)
  2163.       msgs)))
  2164.  
  2165.  
  2166. (defun mh-read-msg-list ()
  2167.   ;; Return a list of message numbers from the current point to the end of
  2168.   ;; the line.
  2169.   (let ((msgs ())
  2170.     (end-of-line (save-excursion (end-of-line) (point)))
  2171.     num)
  2172.     (while (re-search-forward "[0-9]+" end-of-line t)
  2173.       (setq num (string-to-int (buffer-substring (match-beginning 0)
  2174.                          (match-end 0))))
  2175.       (cond ((looking-at "-")        ; Message range
  2176.          (forward-char 1)
  2177.          (re-search-forward "[0-9]+" end-of-line t)
  2178.          (let ((num2 (string-to-int (buffer-substring (match-beginning 0)
  2179.                               (match-end 0)))))
  2180.            (if (< num2 num)
  2181.            (error "Bad message range: %d-%d" num num2))
  2182.            (while (<= num num2)
  2183.          (mh-push num msgs)
  2184.          (setq num (1+ num)))))
  2185.         ((not (zerop num)) (mh-push num msgs))))
  2186.     msgs))
  2187.  
  2188.  
  2189. (defun mh-remove-seq (seq)
  2190.   ;; Delete the SEQUENCE.
  2191.   (mh-map-to-seq-msgs 'mh-notate-if-in-one-seq seq ?  (1+ mh-cmd-note) seq)
  2192.   (mh-undefine-sequence seq (list "all"))
  2193.   (mh-delete-seq-locally seq))
  2194.  
  2195.  
  2196. (defun mh-delete-seq-locally (seq)
  2197.   ;; Remove mh-e's record of SEQUENCE.
  2198.   (let ((entry (mh-find-seq seq)))
  2199.     (setq mh-seq-list (delq entry mh-seq-list))))
  2200.  
  2201.  
  2202. (defun mh-remove-msg-from-seq (msg seq &optional internal-flag)
  2203.   ;; Remove MESSAGE from the SEQUENCE.  If optional FLAG is non-nil, do not
  2204.   ;; inform MH of the change.
  2205.   (let ((entry (mh-find-seq seq)))
  2206.     (mh-when entry
  2207.       (mh-notate-if-in-one-seq msg ?  (1+ mh-cmd-note) (mh-seq-name entry))
  2208.       (if (not internal-flag)
  2209.       (mh-undefine-sequence seq (list msg)))
  2210.       (setcdr entry (delq msg (mh-seq-msgs entry))))))
  2211.  
  2212.  
  2213. (defun mh-add-msgs-to-seq (msgs seq &optional internal-flag)
  2214.   ;; Add MESSAGE(s) to the SEQUENCE.  If optional FLAG is non-nil, do not mark
  2215.   ;; the message in the scan listing or inform MH of the addition.
  2216.   (let ((entry (mh-find-seq seq)))
  2217.     (if (and msgs (atom msgs)) (setq msgs (list msgs)))
  2218.     (if (null entry)
  2219.     (mh-push (mh-make-seq seq msgs) mh-seq-list)
  2220.     (if msgs (setcdr entry (append msgs (cdr entry)))))
  2221.     (mh-when (not internal-flag)
  2222.       (mh-add-to-sequence seq msgs)
  2223.       (mh-notate-seq seq ?% (1+ mh-cmd-note)))))
  2224.  
  2225.  
  2226. (defun mh-rename-seq (seq new-name)
  2227.   "Rename a SEQUENCE to have a new NAME."
  2228.   (interactive "SOld sequence name: \nSNew name: ")
  2229.   (let ((old-seq (mh-find-seq seq)))
  2230.     (if old-seq
  2231.     (rplaca old-seq new-name)
  2232.     (error "Sequence %s does not exists" seq))
  2233.     (mh-undefine-sequence seq (mh-seq-msgs old-seq))
  2234.     (mh-define-sequence new-name (mh-seq-msgs old-seq))))
  2235.  
  2236.  
  2237. (defun mh-notate-user-sequences ()
  2238.   ;; Mark the scan listing of all messages in user-defined sequences.
  2239.   (let ((seqs mh-seq-list)
  2240.     name)
  2241.     (while seqs
  2242.       (setq name (mh-seq-name (car seqs)))
  2243.       (if (not (mh-internal-seq name))
  2244.       (mh-notate-seq name ?% (1+ mh-cmd-note)))
  2245.       (setq seqs (cdr seqs)))))
  2246.  
  2247.  
  2248. (defun mh-internal-seq (name)
  2249.   ;; Return non-NIL if NAME is the name of an internal mh-e sequence.
  2250.   (or (memq name '(answered cur deleted forwarded printed))
  2251.       (eq name mh-unseen-seq)
  2252.       (mh-folder-name-p name)))
  2253.  
  2254.  
  2255. (defun mh-folder-name-p (name)
  2256.   ;; Return non-NIL if NAME is possibly the name of a folder.
  2257.   ;; A name can be a folder name if it begins with "+".
  2258.   (if (symbolp name)
  2259.       (eql (aref (symbol-name name) 0) ?+)
  2260.       (eql (aref name 0) ?+)))
  2261.  
  2262.  
  2263. (defun mh-notate-seq (seq notation offset)
  2264.   ;; Mark the scan listing of all messages in the SEQUENCE with the CHARACTER
  2265.   ;; at the given OFFSET from the beginning of the listing line.
  2266.   (mh-map-to-seq-msgs 'mh-notate seq notation offset))
  2267.  
  2268.  
  2269. (defun mh-notate-if-in-one-seq (msg notation offset seq)
  2270.   ;; If the MESSAGE is in only the SEQUENCE, then mark the scan listing of the
  2271.   ;; message with the CHARACTER at the given OFFSET from the beginning of the
  2272.   ;; listing line.
  2273.   (let ((in-seqs (mh-seq-containing-msg msg)))
  2274.     (if (and (eq seq (car in-seqs)) (null (cdr in-seqs)))
  2275.     (mh-notate msg notation offset))))
  2276.  
  2277.  
  2278. (defun mh-map-to-seq-msgs (func seq &rest args)
  2279.   ;; Invoke the FUNCTION at each message in the SEQUENCE, passing the
  2280.   ;; remaining ARGS as arguments.
  2281.   (save-excursion
  2282.     (let ((msgs (mh-seq-to-msgs seq)))
  2283.       (while msgs
  2284.     (if (mh-goto-msg (car msgs) t t)
  2285.         (apply func (car msgs) args))
  2286.     (setq msgs (cdr msgs))))))
  2287.  
  2288.  
  2289. (defun mh-map-over-seqs (func seq-list)
  2290.   ;; Apply the FUNCTION to each element in the list of SEQUENCES,
  2291.   ;; passing the sequence name and the list of messages as arguments.
  2292.   (while seq-list
  2293.     (funcall func (mh-seq-name (car seq-list)) (mh-seq-msgs (car seq-list)))
  2294.     (setq seq-list (cdr seq-list))))
  2295.  
  2296.  
  2297. (defun mh-define-sequences (seq-list)
  2298.   ;; Define the sequences in SEQ-LIST.
  2299.   (mh-map-over-seqs 'mh-define-sequence seq-list))
  2300.  
  2301.  
  2302. (defun mh-add-to-sequence (seq msgs)
  2303.   ;; Add to a SEQUENCE each message the list of MSGS.
  2304.   (if (not (mh-folder-name-p seq))
  2305.       (if msgs
  2306.       (apply 'mh-exec-cmd "mark" mh-current-folder
  2307.          "-sequence" (symbol-name seq)
  2308.          "-add" msgs))))
  2309.  
  2310.  
  2311. (defun mh-define-sequence (seq msgs)
  2312.   ;; Define the SEQUENCE to contain the list of MSGS.  Do not mark
  2313.   ;; pseudo-sequences or empty sequences.
  2314.   (if (and msgs
  2315.        (not (mh-folder-name-p seq)))
  2316.       (save-excursion
  2317.     (apply 'mh-exec-cmd "mark" mh-current-folder
  2318.            "-sequence" (symbol-name seq)
  2319.            "-add" "-zero" (mh-list-to-string msgs)))))
  2320.  
  2321.  
  2322. (defun mh-undefine-sequence (seq msgs)
  2323.   ;; Remove from the SEQUENCE the list of MSGS.
  2324.   (apply 'mh-exec-cmd "mark" mh-current-folder
  2325.      "-sequence" (symbol-name seq)
  2326.      "-delete" msgs))
  2327.  
  2328.  
  2329. (defun mh-copy-seq-to-point (seq location)
  2330.   ;; Copy the scan listing of the messages in SEQUENCE to after the point
  2331.   ;; LOCATION in the current buffer.
  2332.   (mh-map-to-seq-msgs 'mh-copy-line-to-point seq location))
  2333.  
  2334.  
  2335. (defun mh-copy-line-to-point (msg location)
  2336.   ;; Copy the current line to the LOCATION in the current buffer.
  2337.   (beginning-of-line)
  2338.   (let ((beginning-of-line (point)))
  2339.     (forward-line 1)
  2340.     (copy-region-as-kill beginning-of-line (point))
  2341.     (goto-char location)
  2342.     (yank)
  2343.     (goto-char beginning-of-line)))
  2344.  
  2345.  
  2346.  
  2347. ;;; Issue commands to MH.
  2348.  
  2349. (defun mh-exec-cmd (command &rest args)
  2350.   ;; Execute MH command COMMAND with ARGS.
  2351.   ;; Any output is assumed to be an error and is shown to the user.
  2352.   (save-excursion
  2353.     (set-buffer " *mh-temp*")
  2354.     (erase-buffer)
  2355.     (apply 'call-process
  2356.        (expand-file-name command mh-progs) nil t nil
  2357.        (mh-list-to-string args))
  2358.     (if (> (buffer-size) 0)
  2359.     (save-window-excursion
  2360.       (switch-to-buffer-other-window " *mh-temp*")
  2361.       (sit-for 5)))))
  2362.  
  2363.  
  2364. (defun mh-exec-cmd-quiet (buffer command &rest args)
  2365.   ;; In BUFFER, execute MH command COMMAND with ARGS.
  2366.   ;; ARGS is a list of strings.  Return in BUFFER, if one exists.
  2367.   (mh-when (stringp buffer)
  2368.     (set-buffer buffer)
  2369.     (erase-buffer))
  2370.   (apply 'call-process
  2371.      (expand-file-name command mh-progs) nil buffer nil
  2372.      args))
  2373.  
  2374.  
  2375. (defun mh-exec-cmd-output (command display &rest args)
  2376.   ;; Execute MH command COMMAND with DISPLAY flag and ARGS putting the output
  2377.   ;; into buffer after point.  Set mark after inserted text.
  2378.   (push-mark (point) t)
  2379.   (apply 'call-process
  2380.      (expand-file-name command mh-progs) nil t display
  2381.      (mh-list-to-string args))
  2382.   (exchange-point-and-mark))
  2383.  
  2384.  
  2385. (defun mh-exec-cmd-daemon (command &rest args)
  2386.   ;; Execute MH command COMMAND with ARGS.  Any output from command is
  2387.   ;; displayed in an asynchronous pop-up window.
  2388.   (save-excursion
  2389.     (set-buffer (get-buffer-create " *mh-temp*"))
  2390.     (erase-buffer))
  2391.   (let ((process (apply 'start-process
  2392.             command nil
  2393.             (expand-file-name command mh-progs)
  2394.             (mh-list-to-string args))))
  2395.     (set-process-filter process 'mh-process-daemon)))
  2396.  
  2397.  
  2398. (defun mh-process-daemon (process output)
  2399.   ;; Process daemon that puts output into a temporary buffer.
  2400.   (set-buffer (get-buffer-create " *mh-temp*"))
  2401.   (insert-before-markers output)
  2402.   (display-buffer " *mh-temp*"))
  2403.  
  2404.  
  2405. (defun mh-exec-lib-cmd-output (command &rest args)
  2406.   ;; Execute MH library command COMMAND with ARGS.
  2407.   ;; Put the output into buffer after point.  Set mark after inserted text.
  2408.   (push-mark (point) t)
  2409.   (apply 'call-process
  2410.      (expand-file-name command mh-lib) nil t nil
  2411.      (mh-list-to-string args))
  2412.   (exchange-point-and-mark))
  2413.  
  2414.  
  2415. (defun mh-list-to-string (l)
  2416.   ;; Flattens the list L and makes every element of the new list into a string.
  2417.   (let ((new-list nil))
  2418.     (while l
  2419.       (cond ((null (car l)))
  2420.         ((symbolp (car l)) (mh-push (symbol-name (car l)) new-list))
  2421.         ((numberp (car l)) (mh-push (int-to-string (car l)) new-list))
  2422.         ((equal (car l) ""))
  2423.         ((stringp (car l)) (mh-push (car l) new-list))
  2424.         ((listp (car l))
  2425.          (setq new-list (nconc (nreverse (mh-list-to-string (car l)))
  2426.                    new-list)))
  2427.         (t (error "Bad element in mh-list-to-string: %s" (car l))))
  2428.       (setq l (cdr l)))
  2429.     (nreverse new-list)))
  2430.  
  2431.  
  2432.  
  2433. ;;; Commands to annotate a message.
  2434.  
  2435. (defun mh-annotate-msg (msg buffer note &rest args)
  2436.   ;; Mark the MESSAGE in BUFFER listing with the character NOTE and annotate
  2437.   ;; the saved message with ARGS.
  2438.   (apply 'mh-exec-cmd "anno" buffer msg args)
  2439.   (save-excursion
  2440.     (set-buffer buffer)
  2441.     (if (symbolp msg)
  2442.     (mh-notate-seq msg note (1+ mh-cmd-note))
  2443.     (mh-notate msg note (1+ mh-cmd-note)))))
  2444.  
  2445.  
  2446. (defun mh-notate (msg notation offset)
  2447.   ;; Marks MESSAGE with the character NOTATION at position OFFSET.
  2448.   ;; Null MESSAGE means the message that the cursor points to.
  2449.   (save-excursion
  2450.     (if (or (null msg)
  2451.         (mh-goto-msg msg t t))
  2452.     (with-mh-folder-updating (t)
  2453.       (beginning-of-line)
  2454.       (forward-char offset)
  2455.       (delete-char 1)
  2456.       (insert notation)))))
  2457.  
  2458.  
  2459.  
  2460. ;;; User prompting commands.
  2461.  
  2462. (defun mh-prompt-for-folder (prompt default can-create)
  2463.   ;; Prompt for a folder name with PROMPT.  Returns the folder's name.
  2464.   ;; DEFAULT is used if the folder exists and the user types return.
  2465.   ;; If the CAN-CREATE flag is t, then a non-existant folder is made.
  2466.   (let* ((prompt (format "%s folder%s" prompt
  2467.              (if (equal "" default)
  2468.                  "? "
  2469.                  (format " [%s]? " default))))
  2470.      name)
  2471.     (if (null mh-folder-list)
  2472.     (setq mh-folder-list (mh-make-folder-list)))
  2473.     (while (and (setq name (completing-read prompt mh-folder-list
  2474.                         nil nil "+"))
  2475.         (equal name "")
  2476.         (equal default "")))
  2477.     (cond ((or (equal name "") (equal name "+"))
  2478.        (setq name default))
  2479.       ((not (mh-folder-name-p name))
  2480.        (setq name (format "+%s" name))))
  2481.     (let ((new-file-p (not (file-exists-p (mh-expand-file-name name)))))
  2482.       (cond ((and new-file-p
  2483.           (y-or-n-p
  2484.            (format "Folder %s does not exist. Create it? " name)))
  2485.          (message "Creating %s" name)
  2486.          (call-process "mkdir" nil nil nil (mh-expand-file-name name))
  2487.          (message "Creating %s...done" name)
  2488.          (mh-push (list name) mh-folder-list)
  2489.          (mh-push (list (substring name 1 nil)) mh-folder-list))
  2490.         (new-file-p
  2491.          (error "Folder %s is not created" name))
  2492.         (t
  2493.          (mh-when (null (assoc name mh-folder-list))
  2494.            (mh-push (list name) mh-folder-list)
  2495.            (mh-push (list (substring name 1 nil)) mh-folder-list)))))
  2496.     name))
  2497.  
  2498.  
  2499. (defun mh-make-folder-list ()
  2500.   "Return a list of the user's folders.
  2501. Result is in a form suitable for completing read."
  2502.   (interactive)
  2503.   (message "Collecting folder names...")
  2504.   (save-window-excursion
  2505.     (mh-exec-cmd-quiet " *mh-temp*" "folders" "-fast"
  2506.                (if mh-recursive-folders
  2507.                "-recurse"
  2508.                "-norecurse"))
  2509.     (goto-char (point-min))
  2510.     (let ((list nil)
  2511.       start)
  2512.       (while (not (eobp))
  2513.     (setq start (point))
  2514.     (forward-line 1)
  2515.     (mh-push (list (format "+%s" (buffer-substring start (1- (point)))))
  2516.          list))
  2517.       (message "Collecting folder names...done")
  2518.       list)))
  2519.  
  2520.  
  2521. (defun mh-remove-folder-from-folder-list (folder)
  2522.   ;; Remove FOLDER from the list of folders.
  2523.   (setq mh-folder-list
  2524.     (delq (assoc (substring folder 1 nil) mh-folder-list)
  2525.           mh-folder-list)))
  2526.  
  2527.  
  2528. (defun read-msg-range (prompt)
  2529.   ;; Read a list of blank-separated items.
  2530.   (let* ((buf (read-string prompt))
  2531.      (buf-size (length buf))
  2532.      (start 0)
  2533.      (input ()))
  2534.     (while (< start buf-size)
  2535.       (let ((next (read-from-string buf start buf-size)))
  2536.     (mh-push (car next) input)
  2537.     (setq start (cdr next))))
  2538.     (nreverse input)))
  2539.  
  2540.  
  2541.  
  2542. ;;; Misc. functions.
  2543.  
  2544. (defun mh-get-msg-num (error-if-no-message)
  2545.   ;; Return the message number of the displayed message.  If the argument
  2546.   ;; ERROR-IF-NO-MESSAGE is non-nil, then complain if the cursor is not
  2547.   ;; pointing to a message.
  2548.   (save-excursion
  2549.     (beginning-of-line)
  2550.     (cond ((looking-at mh-msg-number-regexp)
  2551.        (string-to-int (buffer-substring (match-beginning 1)
  2552.                         (match-end 1))))
  2553.       (error-if-no-message
  2554.        (error "Cursor not pointing to message"))
  2555.       (t nil))))
  2556.  
  2557.  
  2558. (defun mh-msg-search-pat (n)
  2559.   ;; Return a search pattern for message N in the scan listing.
  2560.   (format mh-msg-search-regexp n))
  2561.  
  2562.  
  2563. (defun mh-msg-filename (msg &optional folder)
  2564.   ;; Return the file name of MESSAGE in FOLDER (default current folder).
  2565.   (expand-file-name (int-to-string msg)
  2566.             (if folder
  2567.             (mh-expand-file-name folder)
  2568.             mh-folder-filename)))
  2569.  
  2570.  
  2571. (defun mh-msg-filenames (msgs &optional folder)
  2572.   ;; Return a list of file names for MSGS in FOLDER (default current folder).
  2573.   (mapconcat (function (lambda (msg) (mh-msg-filename msg folder))) msgs " "))
  2574.  
  2575.  
  2576. (defun mh-expand-file-name (filename &optional default)
  2577.   "Just like expand-file-name, but also handles MH folder names.
  2578. Assumes that any filename that starts with '+' is a folder name."
  2579.    (if (mh-folder-name-p filename)
  2580.        (expand-file-name (substring filename 1) mh-user-path)
  2581.      (expand-file-name filename default)))
  2582.  
  2583.  
  2584. (defun mh-find-path ()
  2585.   ;; Set mh-user-path, mh-draft-folder, and mh-unseen-seq from profile file.
  2586.   (save-excursion
  2587.     ;; Be sure profile is fully expanded before switching buffers
  2588.     (let ((profile (expand-file-name (or (getenv "MH") "~/.mh_profile"))))
  2589.       (if (not (file-exists-p profile))
  2590.       (error "Cannot find MH profile %s" profile))
  2591.       (set-buffer (get-buffer-create " *mh-temp*"))
  2592.       (erase-buffer)
  2593.       (insert-file-contents profile)
  2594.       (setq mh-draft-folder (mh-get-field "Draft-Folder:"))
  2595.       (cond ((equal mh-draft-folder "")
  2596.          (setq mh-draft-folder nil))
  2597.         ((not (mh-folder-name-p mh-draft-folder))
  2598.          (setq mh-draft-folder (format "+%s" mh-draft-folder))))
  2599.       (setq mh-user-path (mh-get-field "Path:"))
  2600.       (if (equal mh-user-path "")
  2601.       (setq mh-user-path "Mail"))
  2602.       (setq mh-user-path
  2603.         (file-name-as-directory
  2604.          (expand-file-name mh-user-path (expand-file-name "~"))))
  2605.       (if (and mh-draft-folder
  2606.            (not (file-exists-p (mh-expand-file-name mh-draft-folder))))
  2607.       (error "Draft folder %s does not exist.  Create it and try again."
  2608.          mh-draft-folder))
  2609.       (setq mh-unseen-seq (mh-get-field "Unseen-Sequence:"))
  2610.       (if (equal mh-unseen-seq "")
  2611.       (setq mh-unseen-seq 'unseen)
  2612.       (setq mh-unseen-seq (intern mh-unseen-seq))))))
  2613.  
  2614.  
  2615. (defun mh-get-field (field)
  2616.   ;; Find and return the value of field FIELD in the current buffer.
  2617.   ;; Returns the empty string if the field is not in the message.
  2618.   (let ((case-fold-search t))
  2619.     (goto-char (point-min))
  2620.     (cond ((not (search-forward field nil t)) "")
  2621.       ((looking-at "[\t ]*$") "")
  2622.       (t
  2623.        (re-search-forward "[\t ]*\\([^\t \n].*\\)$" nil t)
  2624.        (let ((field (buffer-substring (match-beginning 1)
  2625.                       (match-end 1)))
  2626.          (end-of-match (point)))
  2627.          (forward-line)
  2628.          (while (looking-at "[ \t]") (forward-line 1))
  2629.          (backward-char 1)
  2630.          (if (<= (point) end-of-match)
  2631.          field
  2632.          (format "%s%s"
  2633.              field
  2634.              (buffer-substring end-of-match (point)))))))))
  2635.  
  2636.  
  2637. (defun mh-insert-fields (&rest name-values)
  2638.   ;; Insert the NAME-VALUE pairs in the current buffer.
  2639.   ;; Do not insert any pairs whose value is the empty string.
  2640.   (let ((case-fold-search t))
  2641.     (while name-values
  2642.       (let ((field-name (car name-values))
  2643.         (value (car (cdr name-values))))
  2644.     (mh-when (not (equal value ""))
  2645.       (goto-char (point-min))
  2646.       (cond ((not (re-search-forward (format "^%s" field-name) nil t))
  2647.          (mh-goto-header-end 0)
  2648.          (insert field-name " " value "\n"))
  2649.         (t
  2650.          (end-of-line)
  2651.          (insert " " value))))
  2652.     (setq name-values (cdr (cdr name-values)))))))
  2653.  
  2654.  
  2655. (defun mh-position-on-field (field set-mark)
  2656.   ;; Set point to the end of the line beginning with FIELD.
  2657.   ;; Set the mark to the old value of point, if SET-MARK is non-nil.
  2658.   (let ((case-fold-search t))
  2659.     (if set-mark (push-mark))
  2660.     (goto-char (point-min))
  2661.     (mh-goto-header-end 0)
  2662.     (if (re-search-backward (format "^%s" field) nil t)
  2663.     (progn (end-of-line) t)
  2664.     nil)))
  2665.  
  2666.  
  2667. (defun mh-goto-header-end (arg)
  2668.   ;; Find the end of the message header in the current buffer and position
  2669.   ;; the cursor at the ARG'th newline after the header.
  2670.   (if (re-search-forward "^$\\|^-+$" nil nil)
  2671.       (forward-line arg)))
  2672.  
  2673.  
  2674.  
  2675. ;;; Build the folder-mode keymap:
  2676.  
  2677. (suppress-keymap mh-folder-mode-map)
  2678. (define-key mh-folder-mode-map "q" 'mh-quit)
  2679. (define-key mh-folder-mode-map "b" 'mh-quit)
  2680. (define-key mh-folder-mode-map "?" 'mh-msg-is-in-seq)
  2681. (define-key mh-folder-mode-map "%" 'mh-put-msg-in-seq)
  2682. (define-key mh-folder-mode-map "|" 'mh-pipe-msg)
  2683. (define-key mh-folder-mode-map "\ea" 'mh-edit-again)
  2684. (define-key mh-folder-mode-map "\e%" 'mh-delete-msg-from-seq)
  2685. (define-key mh-folder-mode-map "\C-xn" 'mh-narrow-to-seq)
  2686. (define-key mh-folder-mode-map "\C-xw" 'mh-widen)
  2687. (define-key mh-folder-mode-map "\eb" 'mh-burst-digest)
  2688. (define-key mh-folder-mode-map "\eu" 'mh-undo-folder)
  2689. (define-key mh-folder-mode-map "\e " 'mh-page-digest)
  2690. (define-key mh-folder-mode-map "\e\177" 'mh-page-digest-backwards)
  2691. (define-key mh-folder-mode-map "\ee" 'mh-extract-rejected-mail)
  2692. (define-key mh-folder-mode-map "\ef" 'mh-visit-folder)
  2693. (define-key mh-folder-mode-map "\ek" 'mh-kill-folder)
  2694. (define-key mh-folder-mode-map "\el" 'mh-list-folders)
  2695. (define-key mh-folder-mode-map "\ep" 'mh-pack-folder)
  2696. (define-key mh-folder-mode-map "\es" 'mh-search-folder)
  2697. (define-key mh-folder-mode-map "\er" 'mh-rescan-folder)
  2698. (define-key mh-folder-mode-map "l" 'mh-print-msg)
  2699. (define-key mh-folder-mode-map "t" 'mh-toggle-showing)
  2700. (define-key mh-folder-mode-map "c" 'mh-copy-msg)
  2701. (define-key mh-folder-mode-map ">" 'mh-write-msg-to-file)
  2702. (define-key mh-folder-mode-map "i" 'mh-inc-folder)
  2703. (define-key mh-folder-mode-map "x" 'mh-execute-commands)
  2704. (define-key mh-folder-mode-map "e" 'mh-execute-commands)
  2705. (define-key mh-folder-mode-map "r" 'mh-redistribute)
  2706. (define-key mh-folder-mode-map "f" 'mh-forward)
  2707. (define-key mh-folder-mode-map "s" 'mh-send)
  2708. (define-key mh-folder-mode-map "m" 'mh-send)
  2709. (define-key mh-folder-mode-map "a" 'mh-reply)
  2710. (define-key mh-folder-mode-map "j" 'mh-goto-msg)
  2711. (define-key mh-folder-mode-map "g" 'mh-goto-msg)
  2712. (define-key mh-folder-mode-map "\177" 'mh-previous-page)
  2713. (define-key mh-folder-mode-map " " 'mh-page-msg)
  2714. (define-key mh-folder-mode-map "." 'mh-show)
  2715. (define-key mh-folder-mode-map "u" 'mh-undo)
  2716. (define-key mh-folder-mode-map "!" 'mh-refile-or-write-again)
  2717. (define-key mh-folder-mode-map "^" 'mh-refile-msg)
  2718. (define-key mh-folder-mode-map "d" 'mh-delete-msg)
  2719. (define-key mh-folder-mode-map "\C-d" 'mh-delete-msg-no-motion)
  2720. (define-key mh-folder-mode-map "p" 'mh-previous-undeleted-msg)
  2721. (define-key mh-folder-mode-map "n" 'mh-next-undeleted-msg)
  2722.  
  2723.  
  2724. ;;; Build the letter-mode keymap:
  2725.  
  2726. (define-key mh-letter-mode-map "\C-c\C-f\C-b" 'mh-to-field)
  2727. (define-key mh-letter-mode-map "\C-c\C-f\C-c" 'mh-to-field)
  2728. (define-key mh-letter-mode-map "\C-c\C-f\C-f" 'mh-to-field)
  2729. (define-key mh-letter-mode-map "\C-c\C-f\C-s" 'mh-to-field)
  2730. (define-key mh-letter-mode-map "\C-c\C-f\C-t" 'mh-to-field)
  2731. (define-key mh-letter-mode-map "\C-c\C-fb" 'mh-to-field)
  2732. (define-key mh-letter-mode-map "\C-c\C-fc" 'mh-to-field)
  2733. (define-key mh-letter-mode-map "\C-c\C-ff" 'mh-to-field)
  2734. (define-key mh-letter-mode-map "\C-c\C-fs" 'mh-to-field)
  2735. (define-key mh-letter-mode-map "\C-c\C-ft" 'mh-to-field)
  2736. (define-key mh-letter-mode-map "\C-c\C-q" 'mh-fully-kill-draft)
  2737. (define-key mh-letter-mode-map "\C-c\C-w" 'mh-check-whom)
  2738. (define-key mh-letter-mode-map "\C-c\C-i" 'mh-insert-letter)
  2739. (define-key mh-letter-mode-map "\C-c\C-y" 'mh-yank-cur-msg)
  2740. (define-key mh-letter-mode-map "\C-c\C-s" 'mh-insert-signature)
  2741. (define-key mh-letter-mode-map "\C-c\C-c" 'mh-send-letter)
  2742.  
  2743.  
  2744. ;;; Build the pick-mode keymap:
  2745.  
  2746. (define-key mh-pick-mode-map "\C-c\C-c" 'mh-do-pick-search)
  2747. (define-key mh-pick-mode-map "\C-c\C-f\C-b" 'mh-to-field)
  2748. (define-key mh-pick-mode-map "\C-c\C-f\C-c" 'mh-to-field)
  2749. (define-key mh-pick-mode-map "\C-c\C-f\C-f" 'mh-to-field)
  2750. (define-key mh-pick-mode-map "\C-c\C-f\C-s" 'mh-to-field)
  2751. (define-key mh-pick-mode-map "\C-c\C-f\C-t" 'mh-to-field)
  2752. (define-key mh-pick-mode-map "\C-c\C-fb" 'mh-to-field)
  2753. (define-key mh-pick-mode-map "\C-c\C-fc" 'mh-to-field)
  2754. (define-key mh-pick-mode-map "\C-c\C-ff" 'mh-to-field)
  2755. (define-key mh-pick-mode-map "\C-c\C-fs" 'mh-to-field)
  2756. (define-key mh-pick-mode-map "\C-c\C-ft" 'mh-to-field)
  2757. (define-key mh-pick-mode-map "\C-c\C-w" 'mh-check-whom)
  2758.  
  2759.  
  2760.  
  2761. ;;; For Gnu Emacs.
  2762. ;;; Local Variables: ***
  2763. ;;; eval: (put 'mh-when 'lisp-indent-hook 1) ***
  2764. ;;; eval: (put 'with-mh-folder-updating 'lisp-indent-hook 1) ***
  2765. ;;; End: ***
  2766.