home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / languages / elisp / epoch / tek-epoch-stuff / mh-e.el < prev    next >
Encoding:
Text File  |  1991-12-11  |  94.6 KB  |  2,837 lines

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