Function: mh-search

mh-search is an autoloaded, interactive and byte-compiled function defined in mh-search.el.gz.

Signature

(mh-search FOLDER SEARCH-REGEXP &optional REDO-SEARCH-FLAG WINDOW-CONFIG)

Documentation

Search your MH mail.

This command helps you find messages in your entire corpus of mail. You can search for messages to or from a particular person or about a particular subject. In fact, you can also search for messages containing selected strings in any arbitrary header field or any string found within the messages.

Out of the box, MH-E uses "pick" to find messages. With a little extra effort, you can set an indexing program which rewards you with extremely quick results. The drawback is that sometimes the index does not contain the words you're looking for. You can still use "pick" in these situations.

You are prompted for the FOLDER to search. This can be "all" to search all folders. Note that the search works recursively on the listed folder.

Next, an MH-Search buffer appears where you can enter search criteria SEARCH-REGEXP.

     From:
     To:
     Cc:
     Date:
     Subject:
     --------

Edit this template by entering your search criteria in an appropriate header field that is already there, or create a new field yourself. If the string you're looking for could be anywhere in a message, then place the string underneath the row of dashes.

As an example, let's say that we want to find messages from Ginnean about horseback riding in the Kosciusko National Park (Australia) during January, 1994. Normally we would start with a broad search and narrow it down if necessary to produce a manageable amount of data, but we'll cut to the chase and create a fairly restrictive set of criteria as follows:
     From: ginnean
     To:
     Cc:
     Date: Jan 1994
     Subject:
     --------
     horse
     kosciusko

As with MH-Letter mode, MH-Search provides commands like C-c C-f t (mh-to-field) to help you fill in the blanks. If you find that you do the same thing over and over when editing the search template, you may wish to bind some shortcuts to keys. This can be done with the variable mh-search-mode-hook, which is called when F s (mh-search) is run on a new pattern. To perform the search, type C-c C-c (mh-index-do-search).

Sometimes you're searching for text that is either not indexed, or hasn't been indexed yet. In this case you can override the default method with the pick method by running the command C-c C-p (mh-pick-do-search).

The messages that are found are put in a temporary sub-folder of
"+mhe-index" and are displayed in an MH-Folder buffer. This
buffer is special because it displays messages from multiple folders; each set of messages from a given folder has a heading with the folder name. The appearance of the heading can be modified by customizing the face mh-search-folder. You can jump back and forth between the headings using the commands TAB (mh-index-next-folder) and C-M-i (mh-index-previous-folder).

In addition, the command v (mh-index-visit-folder) can be used to visit the folder of the message at point. Initially, only the messages that matched the search criteria are displayed in the folder. While the temporary buffer has its own set of message numbers, the actual messages numbers are shown in the visited folder. Thus, the command v (mh-index-visit-folder) is useful to find the actual message number of an interesting message, or to view surrounding messages with the command F r (mh-rescan-folder).

Because this folder is temporary, you'll probably get in the habit of killing it when you're done with F k (mh-kill-folder).

You can regenerate the results by running this command with a prefix argument REDO-SEARCH-FLAG.

Note: This command uses an "X-MHE-Checksum:" header field to cache the MD5 checksum of a message. This means that if an incoming message already contains an "X-MHE-Checksum:" field, that message might not be found by this command. The following
"procmail" recipe avoids this problem by renaming the existing
header field:

     :0 wf
     | formail -R "X-MHE-Checksum" "X-Old-MHE-Checksum"

Configuring Indexed Searches

The command F s (mh-search) runs the command defined by the option mh-search-program. The default value is "Auto-detect" which means that MH-E will automatically choose one of "swish++",
"swish-e", "mairix", "namazu", "pick" and "grep" in
that order. If, for example, you have both "swish++" and
"mairix" installed and you want to use "mairix", then you can
set this option to "mairix".

The documentation for the following commands describe how to set up the various indexing programs to use with MH-E.

    - mh-swish++-execute-search
    - mh-swish-execute-search
    - mh-mairix-execute-search
    - mh-namazu-execute-search
    - mh-pick-execute-search
    - mh-grep-execute-search

In a program, if FOLDER is "+" or nil, then mail in all folders are searched. Optional argument WINDOW-CONFIG stores the window configuration that will be restored after the user quits the folder containing the index search results.

Key Bindings

Source Code

;; Defined in /usr/src/emacs/lisp/mh-e/mh-search.el.gz
;;; MH-Folder Commands

;;;###mh-autoload
(defun mh-search (folder search-regexp
                         &optional redo-search-flag window-config)
  "Search your MH mail.

This command helps you find messages in your entire corpus of
mail. You can search for messages to or from a particular person
or about a particular subject. In fact, you can also search for
messages containing selected strings in any arbitrary header
field or any string found within the messages.

Out of the box, MH-E uses \"pick\" to find messages. With a
little extra effort, you can set an indexing program which
rewards you with extremely quick results. The drawback is that
sometimes the index does not contain the words you're looking
for. You can still use \"pick\" in these situations.

You are prompted for the FOLDER to search. This can be \"all\" to
search all folders. Note that the search works recursively on the
listed folder.

Next, an MH-Search buffer appears where you can enter search
criteria SEARCH-REGEXP.

     From:
     To:
     Cc:
     Date:
     Subject:
     --------

Edit this template by entering your search criteria in an
appropriate header field that is already there, or create a new
field yourself. If the string you're looking for could be
anywhere in a message, then place the string underneath the row
of dashes.

As an example, let's say that we want to find messages from
Ginnean about horseback riding in the Kosciusko National
Park (Australia) during January, 1994. Normally we would start
with a broad search and narrow it down if necessary to produce a
manageable amount of data, but we'll cut to the chase and create
a fairly restrictive set of criteria as follows:\\<mh-search-mode-map>

     From: ginnean
     To:
     Cc:
     Date: Jan 1994
     Subject:
     --------
     horse
     kosciusko

As with MH-Letter mode, MH-Search provides commands like
\\[mh-to-field] to help you fill in the blanks.\\<mh-folder-mode-map>

If you find that you do the same thing over and over when editing
the search template, you may wish to bind some shortcuts to keys.
This can be done with the variable `mh-search-mode-hook', which is
called when \\[mh-search] is run on a new pattern.\\<mh-search-mode-map>

To perform the search, type \\[mh-index-do-search].

Sometimes you're searching for text that is either not indexed,
or hasn't been indexed yet. In this case you can override the
default method with the pick method by running the command
\\[mh-pick-do-search].

The messages that are found are put in a temporary sub-folder of
\"+mhe-index\" and are displayed in an MH-Folder buffer. This
buffer is special because it displays messages from multiple
folders; each set of messages from a given folder has a heading
with the folder name.\\<mh-folder-mode-map>

The appearance of the heading can be modified by customizing the
face `mh-search-folder'. You can jump back and forth between the
headings using the commands \\[mh-index-next-folder] and
\\[mh-index-previous-folder].

In addition, the command \\[mh-index-visit-folder] can be used to
visit the folder of the message at point. Initially, only the
messages that matched the search criteria are displayed in the
folder. While the temporary buffer has its own set of message
numbers, the actual messages numbers are shown in the visited
folder. Thus, the command \\[mh-index-visit-folder] is useful to
find the actual message number of an interesting message, or to
view surrounding messages with the command \\[mh-rescan-folder].

Because this folder is temporary, you'll probably get in the
habit of killing it when you're done with \\[mh-kill-folder].

You can regenerate the results by running this command with a
prefix argument REDO-SEARCH-FLAG.

Note: This command uses an \"X-MHE-Checksum:\" header field to
cache the MD5 checksum of a message. This means that if an
incoming message already contains an \"X-MHE-Checksum:\" field,
that message might not be found by this command. The following
\"procmail\" recipe avoids this problem by renaming the existing
header field:

     :0 wf
     | formail -R \"X-MHE-Checksum\" \"X-Old-MHE-Checksum\"

Configuring Indexed Searches

The command \\[mh-search] runs the command defined by the option
`mh-search-program'. The default value is \"Auto-detect\" which
means that MH-E will automatically choose one of \"swish++\",
\"swish-e\", \"mairix\", \"namazu\", \"pick\" and \"grep\" in
that order. If, for example, you have both \"swish++\" and
\"mairix\" installed and you want to use \"mairix\", then you can
set this option to \"mairix\".

The documentation for the following commands describe how to set
up the various indexing programs to use with MH-E.

    - `mh-swish++-execute-search'
    - `mh-swish-execute-search'
    - `mh-mairix-execute-search'
    - `mh-namazu-execute-search'
    - `mh-pick-execute-search'
    - `mh-grep-execute-search'

In a program, if FOLDER is \"+\" or nil, then mail in all folders
are searched. Optional argument WINDOW-CONFIG stores the window
configuration that will be restored after the user quits the
folder containing the index search results."
  (interactive
   (list (progn
           (mh-find-path)
           ;; Yes, we do want to call mh-search-choose every time in case the
           ;; user has switched the searcher manually.
           (unless (mh-search-choose (and current-prefix-arg
                                          mh-index-previous-search
                                          (cadr mh-index-previous-search)))
             (error "No search program found"))
           (or (and current-prefix-arg mh-index-sequence-search-flag)
               (and current-prefix-arg (car mh-index-previous-search))
               (mh-prompt-for-folder "Search" "+" nil "all" t)))
         (or (and current-prefix-arg (caddr mh-index-previous-search))
             mh-search-regexp-builder
             (read-string (format "%s regexp: "
                                  (upcase-initials (symbol-name mh-searcher)))))
         current-prefix-arg
         (if (and (not (and current-prefix-arg
                            (caddr mh-index-previous-search)))
                  mh-search-regexp-builder)
             (current-window-configuration)
           nil)))
  (cl-block mh-search
    ;; Redoing a sequence search?
    (when (and redo-search-flag mh-index-data mh-index-sequence-search-flag
               (not mh-flists-called-flag))
      (let ((mh-flists-called-flag t))
        (apply #'mh-index-sequenced-messages mh-index-previous-search))
      (cl-return-from mh-search))
    ;; We have fancy query parsing.
    (when (symbolp search-regexp)
      (mh-search-folder folder window-config)
      (cl-return-from mh-search))
    ;; Begin search proper.
    (mh-checksum-choose)
    (let ((result-count 0)
          (old-window-config (or window-config mh-previous-window-config))
          (previous-search mh-index-previous-search)
          (index-folder (format "%s/%s" mh-index-folder
                                (mh-index-generate-pretty-name search-regexp))))
      ;; Create a new folder for the search results or recreate the old one...
      (if (and redo-search-flag mh-index-previous-search)
          (let ((buffer-name (buffer-name (current-buffer))))
            (mh-process-or-undo-commands buffer-name)
            (save-excursion (mh-exec-cmd-quiet nil "rmf" buffer-name))
            (mh-exec-cmd-quiet nil "folder" "-create" "-fast" buffer-name)
            (setq index-folder buffer-name))
        (setq index-folder (mh-index-new-folder index-folder search-regexp)))

      (let ((folder-path (format "%s%s" mh-user-path (substring folder 1)))
            (folder-results-map (make-hash-table :test #'equal))
            (origin-map (make-hash-table :test #'equal)))
        ;; Run search program...
        (message "Executing %s... " mh-searcher)
        (funcall mh-search-function folder-path search-regexp)

        ;; Parse searcher output.
        (message "Processing %s output... " mh-searcher)
        (goto-char (point-min))
        (cl-loop for next-result = (funcall mh-search-next-result-function)
                 while next-result
                 do (unless (eq next-result 'error)
                      (unless (gethash (car next-result) folder-results-map)
                        (setf (gethash (car next-result) folder-results-map)
                              (make-hash-table :test #'equal)))
                      (setf (gethash (cadr next-result)
                                     (gethash (car next-result) folder-results-map))
                            t)))

        ;; Copy the search results over.
        (maphash (lambda (folder msgs)
                   (let ((cur (car (mh-translate-range folder "cur")))
                         (msgs (sort (cl-loop
                                      for msg being the hash-keys of msgs
                                      collect msg)
                                     #'<)))
                     (mh-exec-cmd "refile" msgs "-src" folder
                                  "-link" index-folder)
                     ;; Restore cur to old value, that refile changed
                     (when cur
                       (mh-exec-cmd-quiet nil "mark" folder "-add" "-zero"
                                          "-sequence"
                                          "cur" (format "%s" cur)))
                     (cl-loop for msg in msgs
                              do (incf result-count)
                              (setf (gethash result-count origin-map)
                                    (cons folder msg)))))
                 folder-results-map)

        ;; Visit the results folder.
        (mh-visit-folder index-folder () (list folder-results-map origin-map))

        (goto-char (point-min))
        (forward-line)
        (mh-update-sequences)
        (mh-recenter nil)

        ;; Update the speedbar, if needed.
        (when (mh-speed-flists-active-p)
          (mh-speed-flists t mh-current-folder))

        ;; Maintain history.
        (when (or (and redo-search-flag previous-search) window-config)
          (setq mh-previous-window-config old-window-config))
        (setq mh-index-previous-search (list folder mh-searcher search-regexp))

        ;; Write out data to disk.
        (unless mh-flists-called-flag (mh-index-write-data))

        (message "%s found %s matches in %s folders"
                 (upcase-initials (symbol-name mh-searcher))
                 (cl-loop for msg-hash being the hash-values of mh-index-data
                          sum (hash-table-count msg-hash))
                 (cl-loop for msg-hash being the hash-values of mh-index-data
                          count (> (hash-table-count msg-hash) 0)))))))