1 ;;; mh-search --- MH-Search mode
3 ;; Copyright (C) 1993, 1995,
4 ;; 2001, 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
6 ;; Author: Indexed search by Satyaki Das <satyaki@theforce.stanford.edu>
7 ;; Maintainer: Bill Wohler <wohler@newt.com>
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 2, or (at your option)
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
26 ;; Boston, MA 02110-1301, USA.
30 ;; Mode used to compose search criteria.
32 ;; (1) The following search engines are supported:
40 ;; (2) To use this package, you first have to build an index. Please
41 ;; read the documentation for `mh-search' to get started. That
42 ;; documentation will direct you to the specific instructions for
43 ;; your particular searcher.
55 (defvar mh-searcher nil
56 "Cached value of chosen search program.")
58 (defvar mh-search-function nil
59 "Function which executes the search program.")
61 (defvar mh-search-next-result-function nil
62 "Function to parse the next line of output.
63 Expected to return a list of three strings: name of the folder,
64 message number, and optionally the match.")
66 (defvar mh-search-regexp-builder nil
67 "Function used to construct search regexp.")
69 (defvar mh-index-folder
"+mhe-index"
70 "Folder that contains the folders resulting from the index searches.")
72 (defvar mh-flists-results-folder
"sequence"
73 "Subfolder for `mh-index-folder' where flists output is placed.")
75 (defvar mh-flists-sequence
)
77 (defvar mh-flists-called-flag nil
)
81 ;;; MH-Folder Commands
84 (defun mh-search (folder search-regexp
85 &optional redo-search-flag window-config
)
88 This command helps you find messages in your entire corpus of
89 mail. You can search for messages to or from a particular person
90 or about a particular subject. In fact, you can also search for
91 messages containing selected strings in any arbitrary header
92 field or any string found within the messages.
94 Out of the box, MH-E uses \"pick\" to find messages. With a
95 little extra effort, you can set an indexing program which
96 rewards you with extremely quick results. The drawback is that
97 sometimes the index does not contain the words you're looking
98 for. You can still use \"pick\" in these situations.
100 You are prompted for the FOLDER to search. This can be \"all\" to
101 search all folders. Note that the search works recursively on the
104 Next, an MH-Search buffer appears where you can enter search
105 criteria SEARCH-REGEXP.
114 Edit this template by entering your search criteria in an
115 appropriate header field that is already there, or create a new
116 field yourself. If the string you're looking for could be
117 anywhere in a message, then place the string underneath the row
120 As an example, let's say that we want to find messages from
121 Ginnean about horseback riding in the Kosciusko National
122 Park (Australia) during January, 1994. Normally we would start
123 with a broad search and narrow it down if necessary to produce a
124 manageable amount of data, but we'll cut to the chase and create
125 a fairly restrictive set of criteria as follows:\\<mh-search-mode-map>
136 As with MH-Letter mode, MH-Search provides commands like
137 \\[mh-to-field] to help you fill in the blanks.\\<mh-folder-mode-map>
139 If you find that you do the same thing over and over when editing
140 the search template, you may wish to bind some shortcuts to keys.
141 This can be done with the variable `mh-search-mode-hook', which is
142 called when \\[mh-search] is run on a new pattern.\\<mh-search-mode-map>
144 To perform the search, type \\[mh-index-do-search].
146 Sometimes you're searching for text that is either not indexed,
147 or hasn't been indexed yet. In this case you can override the
148 default method with the pick method by running the command
149 \\[mh-pick-do-search].
151 The messages that are found are put in a temporary sub-folder of
152 \"+mhe-index\" and are displayed in an MH-Folder buffer. This
153 buffer is special because it displays messages from multiple
154 folders; each set of messages from a given folder has a heading
155 with the folder name.\\<mh-folder-mode-map>
157 The appearance of the heading can be modified by customizing the
158 face `mh-search-folder'. You can jump back and forth between the
159 headings using the commands \\[mh-index-next-folder] and
160 \\[mh-index-previous-folder].
162 In addition, the command \\[mh-index-visit-folder] can be used to
163 visit the folder of the message at point. Initially, only the
164 messages that matched the search criteria are displayed in the
165 folder. While the temporary buffer has its own set of message
166 numbers, the actual messages numbers are shown in the visited
167 folder. Thus, the command \\[mh-index-visit-folder] is useful to
168 find the actual message number of an interesting message, or to
169 view surrounding messages with the command \\[mh-rescan-folder].
171 Because this folder is temporary, you'll probably get in the
172 habit of killing it when you're done with \\[mh-kill-folder].
174 You can regenerate the results by running this command with a
175 prefix argument REDO-SEARCH-FLAG.
177 Note: This command uses an \"X-MHE-Checksum:\" header field to
178 cache the MD5 checksum of a message. This means that if an
179 incoming message already contains an \"X-MHE-Checksum:\" field,
180 that message might not be found by this command. The following
181 \"procmail\" recipe avoids this problem by renaming the existing
185 | formail -R \"X-MHE-Checksum\" \"X-Old-MHE-Checksum\"
187 Configuring Indexed Searches
189 The command \\[mh-search] runs the command defined by the option
190 `mh-search-program'. The default value is \"Auto-detect\" which
191 means that MH-E will automatically choose one of \"swish++\",
192 \"swish-e\", \"mairix\", \"namazu\", \"pick\" and \"grep\" in
193 that order. If, for example, you have both \"swish++\" and
194 \"mairix\" installed and you want to use \"mairix\", then you can
195 set this option to \"mairix\".
197 The documentation for the following commands describe how to set
198 up the various indexing programs to use with MH-E.
200 - `mh-swish++-execute-search'
201 - `mh-swish-execute-search'
202 - `mh-mairix-execute-search'
203 - `mh-namazu-execute-search'
204 - `mh-pick-execute-search'
205 - `mh-grep-execute-search'
207 In a program, if FOLDER is \"+\" or nil, then mail in all folders
208 are searched. Optional argument WINDOW-CONFIG stores the window
209 configuration that will be restored after the user quits the
210 folder containing the index search results."
214 ;; Yes, we do want to call mh-search-choose every time in case the
215 ;; user has switched the searcher manually.
216 (unless (mh-search-choose (and current-prefix-arg
217 mh-index-previous-search
218 (cadr mh-index-previous-search
)))
219 (error "No search program found"))
220 (or (and current-prefix-arg mh-index-sequence-search-flag
)
221 (and current-prefix-arg
(car mh-index-previous-search
))
222 (mh-prompt-for-folder "Search" "+" nil
"all" t
)))
223 (or (and current-prefix-arg
(caddr mh-index-previous-search
))
224 mh-search-regexp-builder
225 (read-string (format "%s regexp: "
226 (upcase-initials (symbol-name mh-searcher
)))))
228 (if (and (not (and current-prefix-arg
229 (caddr mh-index-previous-search
)))
230 mh-search-regexp-builder
)
231 (current-window-configuration)
234 ;; Redoing a sequence search?
235 (when (and redo-search-flag mh-index-data mh-index-sequence-search-flag
236 (not mh-flists-called-flag
))
237 (let ((mh-flists-called-flag t
))
238 (apply #'mh-index-sequenced-messages mh-index-previous-search
))
239 (return-from mh-search
))
240 ;; We have fancy query parsing.
241 (when (symbolp search-regexp
)
242 (mh-search-folder folder window-config
)
243 (return-from mh-search
))
244 ;; Begin search proper.
246 (let ((result-count 0)
247 (old-window-config (or window-config mh-previous-window-config
))
248 (previous-search mh-index-previous-search
)
249 (index-folder (format "%s/%s" mh-index-folder
250 (mh-index-generate-pretty-name search-regexp
))))
251 ;; Create a new folder for the search results or recreate the old one...
252 (if (and redo-search-flag mh-index-previous-search
)
253 (let ((buffer-name (buffer-name (current-buffer))))
254 (mh-process-or-undo-commands buffer-name
)
255 (save-excursion (mh-exec-cmd-quiet nil
"rmf" buffer-name
))
256 (mh-exec-cmd-quiet nil
"folder" "-create" "-fast" buffer-name
)
257 (setq index-folder buffer-name
))
258 (setq index-folder
(mh-index-new-folder index-folder search-regexp
)))
260 (let ((folder-path (format "%s%s" mh-user-path
(substring folder
1)))
261 (folder-results-map (make-hash-table :test
#'equal
))
262 (origin-map (make-hash-table :test
#'equal
)))
263 ;; Run search program...
264 (message "Executing %s... " mh-searcher
)
265 (funcall mh-search-function folder-path search-regexp
)
267 ;; Parse searcher output.
268 (message "Processing %s output... " mh-searcher
)
269 (goto-char (point-min))
270 (loop for next-result
= (funcall mh-search-next-result-function
)
272 do
(unless (eq next-result
'error
)
273 (unless (gethash (car next-result
) folder-results-map
)
274 (setf (gethash (car next-result
) folder-results-map
)
275 (make-hash-table :test
#'equal
)))
276 (setf (gethash (cadr next-result
)
277 (gethash (car next-result
) folder-results-map
))
280 ;; Copy the search results over.
281 (maphash #'(lambda (folder msgs
)
282 (let ((cur (car (mh-translate-range folder
"cur")))
283 (msgs (sort (loop for msg being the hash-keys of msgs
286 (mh-exec-cmd "refile" msgs
"-src" folder
287 "-link" index-folder
)
288 ;; Restore cur to old value, that refile changed
290 (mh-exec-cmd-quiet nil
"mark" folder
"-add" "-zero"
292 "cur" (format "%s" cur
)))
293 (loop for msg in msgs
294 do
(incf result-count
)
295 (setf (gethash result-count origin-map
)
296 (cons folder msg
)))))
299 ;; Vist the results folder.
300 (mh-visit-folder index-folder
() (list folder-results-map origin-map
))
302 (goto-char (point-min))
304 (mh-update-sequences)
307 ;; Update the speedbar, if needed.
308 (when (mh-speed-flists-active-p)
309 (mh-speed-flists t mh-current-folder
))
312 (when (or (and redo-search-flag previous-search
) window-config
)
313 (setq mh-previous-window-config old-window-config
))
314 (setq mh-index-previous-search
(list folder mh-searcher search-regexp
))
316 ;; Write out data to disk.
317 (unless mh-flists-called-flag
(mh-index-write-data))
319 (message "%s found %s matches in %s folders"
320 (upcase-initials (symbol-name mh-searcher
))
321 (loop for msg-hash being hash-values of mh-index-data
322 sum
(hash-table-count msg-hash
))
323 (loop for msg-hash being hash-values of mh-index-data
324 count
(> (hash-table-count msg-hash
) 0)))))))
327 (eval-when-compile (mh-do-in-xemacs (defvar pick-folder
)))
329 (defun mh-search-folder (folder window-config
)
330 "Search FOLDER for messages matching a pattern.
332 In a program, argument WINDOW-CONFIG is the current window
333 configuration and is used when the search folder is dismissed."
334 (interactive (list (mh-prompt-for-folder "Search" mh-current-folder nil nil t
)
335 (current-window-configuration)))
336 (let ((pick-folder (if (equal folder
"+") mh-current-folder folder
)))
337 (switch-to-buffer-other-window "search-pattern")
338 (if (or (zerop (buffer-size))
339 (not (y-or-n-p "Reuse pattern? ")))
340 (mh-make-pick-template)
342 (mh-make-local-vars 'mh-current-folder folder
343 'mh-previous-window-config window-config
)
344 (message "%s" (substitute-command-keys
345 (concat "Type \\[mh-index-do-search] to search messages, "
346 "\\[mh-pick-do-search] to use pick, "
347 "\\[mh-help] for help")))))
349 (defun mh-make-pick-template ()
350 "Initialize the current buffer with a template for a pick pattern."
351 (let ((inhibit-read-only t
)) (erase-buffer))
359 (goto-char (point-min))
361 (add-text-properties (point) (1+ (point)) '(front-sticky t
))
362 (add-text-properties (- (mh-line-end-position) 2)
363 (1- (mh-line-end-position))
365 (add-text-properties (point) (1- (mh-line-end-position)) '(read-only t
))
367 (add-text-properties (point) (1+ (point)) '(front-sticky t
))
368 (add-text-properties (point) (1- (mh-line-end-position)) '(read-only t
))
369 (goto-char (point-max)))
374 (defun mh-index-new-messages (folders)
375 "Display unseen messages.
377 If you use a program such as \"procmail\" to use \"rcvstore\" to file
378 your incoming mail automatically, you can display new, unseen,
379 messages using this command. All messages in the \"unseen\"
380 sequence from the folders in `mh-new-messages-folders' are
383 With a prefix argument, enter a space-separated list of FOLDERS,
384 or nothing to search all folders."
386 (list (if current-prefix-arg
387 (split-string (read-string "Search folder(s) (default all): "))
388 mh-new-messages-folders
)))
389 (mh-index-sequenced-messages folders mh-unseen-seq
))
392 (defun mh-index-ticked-messages (folders)
393 "Display ticked messages.
395 All messages in `mh-tick-seq' from the folders in
396 `mh-ticked-messages-folders' are listed.
398 With a prefix argument, enter a space-separated list of FOLDERS,
399 or nothing to search all folders."
401 (list (if current-prefix-arg
402 (split-string (read-string "Search folder(s) (default all): "))
403 mh-ticked-messages-folders
)))
404 (mh-index-sequenced-messages folders mh-tick-seq
))
409 (defvar mh-mairix-folder
)
410 (defvar mh-flists-search-folders
)))
413 (defun mh-index-sequenced-messages (folders sequence
)
414 "Display messages in any sequence.
416 All messages from the FOLDERS in `mh-new-messages-folders' in the
417 SEQUENCE you provide are listed. With a prefix argument, enter a
418 space-separated list of folders at the prompt, or nothing to
421 (list (if current-prefix-arg
422 (split-string (read-string "Search folder(s) (default all): "))
423 mh-new-messages-folders
)
424 (mh-read-seq-default "Search" nil
)))
425 (unless sequence
(setq sequence mh-unseen-seq
))
426 (let* ((mh-flists-search-folders folders
)
427 (mh-flists-sequence sequence
)
428 (mh-flists-called-flag t
)
429 (mh-searcher 'flists
)
430 (mh-search-function 'mh-flists-execute
)
431 (mh-search-next-result-function 'mh-mairix-next-result
)
432 (mh-mairix-folder mh-user-path
)
433 (mh-search-regexp-builder nil
)
434 (new-folder (format "%s/%s/%s" mh-index-folder
435 mh-flists-results-folder sequence
))
436 (window-config (if (equal new-folder mh-current-folder
)
437 mh-previous-window-config
438 (current-window-configuration)))
441 (cond ((buffer-live-p (get-buffer new-folder
))
442 ;; The destination folder is being visited. Trick `mh-search'
443 ;; into thinking that the folder resulted from a previous search.
444 (set-buffer new-folder
)
445 (setq mh-index-previous-search
(list folders mh-searcher sequence
))
447 ((mh-folder-exists-p new-folder
)
448 ;; Folder exists but we don't have it open. That means they are
449 ;; stale results from a old flists search. Clear it out.
450 (mh-exec-cmd-quiet nil
"rmf" new-folder
)))
451 (setq message
(mh-search "+" mh-flists-results-folder
452 redo-flag window-config
)
453 mh-index-sequence-search-flag t
454 mh-index-previous-search
(list folders mh-searcher sequence
))
455 (mh-index-write-data)
456 (when (stringp message
) (message "%s" message
))))
458 (defvar mh-flists-search-folders
)
460 (defun mh-flists-execute (&rest args
)
462 Search for messages belonging to `mh-flists-sequence' in the
463 folders specified by `mh-flists-search-folders'. If
464 `mh-recursive-folders-flag' is t, then the folders are searched
465 recursively. All parameters ARGS are ignored."
466 (set-buffer (get-buffer-create mh-temp-index-buffer
))
468 (unless (executable-find "sh")
469 (error "Didn't find sh"))
471 (let ((seq (symbol-name mh-flists-sequence
)))
472 (insert "for folder in `" (expand-file-name "flists" mh-progs
) " "
473 (cond ((eq mh-flists-search-folders t
)
474 (mh-quote-for-shell mh-inbox
))
475 ((eq mh-flists-search-folders nil
) "")
476 ((listp mh-flists-search-folders
)
477 (loop for folder in mh-flists-search-folders
479 (concat " " (mh-quote-for-shell folder
)))))
480 (if mh-recursive-folders-flag
" -recurse" "")
481 " -sequence " seq
" -noshowzero -fast` ; do\n"
482 (expand-file-name "mhpath" mh-progs
) " \"+$folder\" " seq
"\n"
485 (point-min) (point-max) "sh" nil
(get-buffer mh-temp-index-buffer
))))
490 (defun mh-index-next-folder (&optional backward-flag
)
491 "Jump to the next folder marker.
493 With non-nil optional argument BACKWARD-FLAG, jump to the previous
496 (if (null mh-index-data
)
497 (message "Only applicable in an MH-E index search buffer")
498 (let ((point (point)))
499 (forward-line (if backward-flag
0 1))
500 (cond ((if backward-flag
501 (re-search-backward "^+" (point-min) t
)
502 (re-search-forward "^+" (point-max) t
))
504 ((and (if backward-flag
505 (goto-char (point-max))
506 (goto-char (point-min)))
509 (re-search-backward "^+" (point-min) t
)
510 (re-search-forward "^+" (point-max) t
))
512 (t (goto-char point
))))))
515 (defun mh-index-previous-folder ()
516 "Jump to the previous folder marker."
518 (mh-index-next-folder t
))
521 (defun mh-index-visit-folder ()
522 "Visit original folder from where the message at point was found."
524 (unless mh-index-data
525 (error "Not in an index folder"))
528 (cond ((and (bolp) (eolp))
529 (ignore-errors (forward-line -
1))
530 (setq msg
(mh-get-msg-num t
)))
531 ((equal (char-after (mh-line-beginning-position)) ?
+)
532 (setq folder
(buffer-substring-no-properties
533 (mh-line-beginning-position)
534 (mh-line-end-position))))
535 (t (setq msg
(mh-get-msg-num t
)))))
537 (setq folder
(car (gethash (gethash msg mh-index-msg-checksum-map
)
538 mh-index-checksum-origin-map
))))
539 (when (or (not (get-buffer folder
))
540 (y-or-n-p (format "Reuse buffer displaying %s? " folder
)))
542 folder
(loop for x being the hash-keys of
(gethash folder mh-index-data
)
543 when
(mh-msg-exists-p x folder
) collect x
)))))
550 mh-pick-menu mh-search-mode-map
"Menu for MH-E Search"
552 ["Perform Search" mh-index-do-search t
]
553 ["Search with pick" mh-pick-do-search t
]))
559 ;; If this changes, modify mh-search-mode-help-messages accordingly, below.
560 (gnus-define-keys mh-search-mode-map
562 "\C-c\C-c" mh-index-do-search
563 "\C-c\C-p" mh-pick-do-search
564 "\C-c\C-f\C-b" mh-to-field
565 "\C-c\C-f\C-c" mh-to-field
566 "\C-c\C-f\C-m" mh-to-field
567 "\C-c\C-f\C-s" mh-to-field
568 "\C-c\C-f\C-t" mh-to-field
569 "\C-c\C-fb" mh-to-field
570 "\C-c\C-fc" mh-to-field
571 "\C-c\C-fm" mh-to-field
572 "\C-c\C-fs" mh-to-field
573 "\C-c\C-ft" mh-to-field
)
577 ;;; MH-Search Help Messages
579 ;; Group messages logically, more or less.
580 (defvar mh-search-mode-help-messages
582 "Perform search: \\[mh-index-do-search]\n"
583 "Search with pick: \\[mh-pick-do-search]\n\n"
584 "Move to a field by typing C-c C-f C-<field>\n"
585 "where <field> is the first letter of the desired field\n"
586 "(except for From: which uses \"m\")."))
587 "Key binding cheat sheet.
589 This is an associative array which is used to show the most common
590 commands. The key is a prefix char. The value is one or more strings
591 which are concatenated together and displayed in the minibuffer if ?
592 is pressed after the prefix character. The special key nil is used to
593 display the non-prefixed commands.
595 The substitutions described in `substitute-command-keys' are performed
602 (put 'mh-search-mode
'mode-class
'special
)
604 (define-derived-mode mh-search-mode fundamental-mode
"MH-Search"
605 "Mode for creating search templates in MH-E.\\<mh-search-mode-map>
607 Edit this template by entering your search criteria in an
608 appropriate header field that is already there, or create a new
609 field yourself. If the string you're looking for could be
610 anywhere in a message, then place the string underneath the row
613 To perform the search, type \\[mh-index-do-search].
615 Sometimes you're searching for text that is either not indexed,
616 or hasn't been indexed yet. In this case you can override the
617 default method with the pick method by running the command
618 \\[mh-pick-do-search].
620 The hook `mh-search-mode-hook' is called upon entry to this mode.
622 \\{mh-search-mode-map}"
624 (easy-menu-add mh-pick-menu
)
625 (mh-set-help mh-search-mode-help-messages
))
629 ;;; MH-Search Commands
631 (defun mh-index-do-search (&optional searcher
)
632 "Find messages using `mh-search-program'.
633 If optional argument SEARCHER is present, use it instead of
634 `mh-search-program'."
636 (unless (mh-search-choose searcher
) (error "No search program found"))
637 (let* ((regexp-list (mh-pick-parse-search-buffer))
638 (pattern (funcall mh-search-regexp-builder regexp-list
)))
640 (mh-search mh-current-folder pattern nil mh-previous-window-config
)
641 (error "No search terms"))))
643 (defun mh-pick-do-search ()
644 "Find messages using \"pick\".
646 Uses the pick method described in `mh-pick-execute-search'."
648 (mh-index-do-search 'pick
))
650 (defun mh-pick-parse-search-buffer ()
651 "Parse the search buffer contents.
652 The function returns a alist. The car of each element is either
653 the header name to search in or nil to search the whole message.
654 The cdr of the element is the pattern to search."
656 (let ((pattern-list ())
659 (goto-char (point-min))
661 (if (search-forward "--------" (mh-line-end-position) t
)
662 (setq in-body-flag t
)
665 (setq start
(if in-body-flag
667 (search-forward ":" (mh-line-end-position) t
)
669 (push (cons (and (not in-body-flag
)
671 (buffer-substring-no-properties
673 (mh-index-parse-search-regexp
674 (buffer-substring-no-properties
675 start
(mh-line-end-position))))
680 (defun mh-index-parse-search-regexp (input-string)
681 "Construct parse tree for INPUT-STRING.
682 All occurrences of &, |, ! and ~ in INPUT-STRING are replaced by
683 AND, OR and NOT as appropriate. Then the resulting string is
687 (insert input-string
)
689 (mh-replace-string "\t" " ")
691 (mh-replace-string " AND " " and ")
692 (mh-replace-string "&" " and ")
693 (mh-replace-string " -and " " and ")
695 (mh-replace-string " OR " " or ")
696 (mh-replace-string "|" " or ")
697 (mh-replace-string " -or " " or ")
699 (mh-replace-string " NOT " " not ")
700 (mh-replace-string "!" " not ")
701 (mh-replace-string "~" " not ")
702 (mh-replace-string " -not " " not ")
703 ;; synonyms of left brace
704 (mh-replace-string "(" " ( ")
705 (mh-replace-string " -lbrace " " ( ")
706 ;; synonyms of right brace
707 (mh-replace-string ")" " ) ")
708 (mh-replace-string " -rbrace " " ) ")
709 ;; get the normalized input
710 (setq input
(format "( %s )" (buffer-substring (point-min) (point-max)))))
712 (let ((tokens (mh-index-add-implicit-ops (split-string input
)))
716 (dolist (token tokens
)
717 (cond ((equal token
"(") (push 'paren op-stack
))
718 ((equal token
"not") (push 'not op-stack
))
719 ((equal token
"or") (push 'or op-stack
))
720 ((equal token
"and") (push 'and op-stack
))
722 (multiple-value-setq (op-stack operand-stack
)
723 (mh-index-evaluate op-stack operand-stack
))
724 (when (eq (car op-stack
) 'not
)
725 (setq op-stack
(cdr op-stack
))
726 (push `(not ,(pop operand-stack
)) operand-stack
))
727 (when (eq (car op-stack
) 'and
)
728 (setq op-stack
(cdr op-stack
))
729 (setq oper1
(pop operand-stack
))
730 (push `(and ,(pop operand-stack
) ,oper1
) operand-stack
)))
731 ((eq (car op-stack
) 'not
)
732 (setq op-stack
(cdr op-stack
))
733 (push `(not ,token
) operand-stack
)
734 (when (eq (car op-stack
) 'and
)
735 (setq op-stack
(cdr op-stack
))
736 (setq oper1
(pop operand-stack
))
737 (push `(and ,(pop operand-stack
) ,oper1
) operand-stack
)))
738 ((eq (car op-stack
) 'and
)
739 (setq op-stack
(cdr op-stack
))
740 (push `(and ,(pop operand-stack
) ,token
) operand-stack
))
741 (t (push token operand-stack
))))
742 (prog1 (pop operand-stack
)
743 (when (or op-stack operand-stack
)
744 (error "Invalid regexp: %s" input
))))))
746 (defun mh-index-add-implicit-ops (tokens)
747 "Add implicit operators in the list TOKENS."
752 (setq current
(pop tokens
))
753 (cond ((or (equal current
")") (equal current
"and") (equal current
"or"))
754 (setq literal-seen nil
)
755 (push current result
))
758 (setq literal-seen nil
)
761 (push current result
)
762 (unless (or (equal current
"(") (equal current
"not"))
763 (setq literal-seen t
)))))
766 (defun mh-index-evaluate (op-stack operand-stack
)
767 "Read expression till starting paren based on OP-STACK and OPERAND-STACK."
768 (block mh-index-evaluate
771 (setq op
(pop op-stack
))
772 (cond ((eq op
'paren
)
773 (return-from mh-index-evaluate
(values op-stack operand-stack
)))
775 (push `(not ,(pop operand-stack
)) operand-stack
))
776 ((or (eq op
'and
) (eq op
'or
))
777 (setq oper1
(pop operand-stack
))
778 (push `(,op
,(pop operand-stack
) ,oper1
) operand-stack
))))
779 (error "Ran out of tokens"))))
783 ;;; Indexing Functions
785 ;; Support different search programs
786 (defvar mh-search-choices
788 mh-swish
++-binary mh-swish
++-execute-search mh-swish
++-next-result
789 mh-swish
++-regexp-builder
)
791 mh-swish-binary mh-swish-execute-search mh-swish-next-result nil
)
793 mh-mairix-binary mh-mairix-execute-search mh-mairix-next-result
794 mh-mairix-regexp-builder
)
796 mh-namazu-binary mh-namazu-execute-search mh-namazu-next-result nil
)
798 mh-pick-binary mh-pick-execute-search mh-pick-next-result
799 mh-pick-regexp-builder
)
801 mh-grep-binary mh-grep-execute-search mh-grep-next-result nil
))
802 "List of possible searcher choices.")
804 (defun mh-search-choose (&optional searcher
)
805 "Choose a searching function.
806 The side-effects of this function are that the variables
807 `mh-searcher', `mh-search-function', and
808 `mh-search-next-result-function' are set according to the first
809 searcher in `mh-search-choices' present on the system. If
810 optional argument SEARCHER is present, use it instead of
811 `mh-search-program'."
813 (let ((program-alist (cond (searcher
814 (list (assoc searcher mh-search-choices
)))
817 (assoc mh-search-program mh-search-choices
)))
818 (t mh-search-choices
))))
820 (let* ((current (pop program-alist
))
821 (executable (symbol-value (cadr current
))))
823 (setq mh-searcher
(car current
))
824 (setq mh-search-function
(nth 2 current
))
825 (setq mh-search-next-result-function
(nth 3 current
))
826 (setq mh-search-regexp-builder
(nth 4 current
))
827 (return mh-searcher
))))
832 (defvar mh-swish
++-binary
(or (executable-find "search++")
833 (executable-find "search")))
834 (defvar mh-swish
++-directory
".swish++")
835 (defvar mh-swish-folder nil
)
837 (defun mh-swish++-execute-search
(folder-path search-regexp
)
840 In the examples below, replace \"/home/user/Mail\" with the path to
843 First create the directory \"/home/user/Mail/.swish++\". Then create
844 the file \"/home/user/Mail/.swish++/swish++.conf\" with the following
847 IncludeMeta Bcc Cc Comments Content-Description From Keywords
848 IncludeMeta Newsgroups Resent-To Subject To
849 IncludeMeta Message-Id References In-Reply-To
851 IndexFile /home/user/Mail/.swish++/swish++.index
853 Use the following command line to generate the swish index. Run
854 this daily from cron:
856 find /home/user/Mail -path /home/user/Mail/mhe-index -prune \\
857 -o -path /home/user/Mail/.swish++ -prune \\
858 -o -name \"[0-9]*\" -print \\
859 | index -c /home/user/Mail/.swish++/swish++.conf -
861 This command does not index the folders that hold the results of your
862 searches in \"+mhe-index\" since they tend to be ephemeral and the
863 original messages are indexed anyway.
865 On some systems (Debian GNU/Linux, for example), use \"index++\"
866 instead of \"index\".
868 In a program, FOLDER-PATH is the directory in which SEARCH-REGEXP is
870 (set-buffer (get-buffer-create mh-temp-index-buffer
))
872 (unless mh-swish
++-binary
873 (error "Set `mh-swish++-binary' appropriately"))
874 (call-process mh-swish
++-binary nil
'(t nil
) nil
876 (format "-i%s%s/swish++.index"
877 mh-user-path mh-swish
++-directory
)
879 (goto-char (point-min))
880 (setq mh-swish-folder
881 (let ((last-char (substring folder-path
(1- (length folder-path
)))))
882 (if (equal last-char
"/")
884 (format "%s/" folder-path
)))))
886 (defalias 'mh-swish
++-next-result
'mh-swish-next-result
)
888 (defun mh-swish++-regexp-builder
(regexp-list)
889 "Generate query for swish++.
890 REGEXP-LIST is an alist of fields and values."
892 (dolist (elem regexp-list
)
894 (setq regexp
(concat regexp
" and "
895 (if (car elem
) "(" "")
896 (if (car elem
) (symbol-name (car elem
)) "")
897 (if (car elem
) " = " "")
898 (mh-swish++-print-regexp
(cdr elem
))
899 (if (car elem
) ")" "")))))
900 (substring regexp
4)))
902 (defun mh-swish++-print-regexp
(expr)
903 "Return infix expression corresponding to EXPR."
904 (cond ((atom expr
) (format "%s" expr
))
905 ((eq (car expr
) 'not
)
906 (format "(not %s)" (mh-swish++-print-regexp
(cadr expr
))))
907 (t (format "(%s %s %s)" (mh-swish++-print-regexp
(cadr expr
))
908 (symbol-name (car expr
))
909 (mh-swish++-print-regexp
(caddr expr
))))))
913 (defvar mh-swish-binary
(executable-find "swish-e"))
914 (defvar mh-swish-directory
".swish")
916 (defun mh-swish-execute-search (folder-path search-regexp
)
919 In the examples below, replace \"/home/user/Mail\" with the path
920 to your MH directory.
922 First create the directory \"/home/user/Mail/.swish\". Then
923 create the file \"/home/user/Mail/.swish/config\" with the
927 IndexDir /home/user/Mail
928 IndexFile /home/user/Mail/.swish/index
929 IndexName \"Mail Index\"
930 IndexDescription \"Mail Index\"
931 IndexPointer \"http://nowhere\"
932 IndexAdmin \"nobody\"
937 IgnoreTotalWordCountWhenRanking yes
938 WordCharacters abcdefghijklmnopqrstuvwxyz0123456789-
939 BeginCharacters abcdefghijklmnopqrstuvwxyz
940 EndCharacters abcdefghijklmnopqrstuvwxyz0123456789
943 FileRules filename contains \\D
944 FileRules pathname contains /home/user/Mail/.swish
945 FileRules pathname contains /home/user/Mail/mhe-index
947 This configuration does not index the folders that hold the
948 results of your searches in \"+mhe-index\" since they tend to be
949 ephemeral and the original messages are indexed anyway.
951 If there are any directories you would like to ignore, append
952 lines like the following to \"config\":
954 FileRules pathname contains /home/user/Mail/scripts
956 Use the following command line to generate the swish index. Run
957 this daily from cron:
959 swish-e -c /home/user/Mail/.swish/config
961 In a program, FOLDER-PATH is the directory in which SEARCH-REGEXP
963 (set-buffer (get-buffer-create mh-temp-index-buffer
))
965 (unless mh-swish-binary
966 (error "Set `mh-swish-binary' appropriately"))
967 (call-process mh-swish-binary nil
'(t nil
) nil
969 "-f" (format "%s%s/index" mh-user-path mh-swish-directory
))
970 (goto-char (point-min))
971 (setq mh-swish-folder
972 (let ((last-char (substring folder-path
(1- (length folder-path
)))))
973 (if (equal last-char
"/")
975 (format "%s/" folder-path
)))))
977 (defun mh-swish-next-result ()
978 "Get the next result from swish output."
981 (when (or (eobp) (equal (char-after (point)) ?.
))
983 (when (equal (char-after (point)) ?
#)
985 (let* ((start (search-forward " " (mh-line-end-position) t
))
986 (end (search-forward " " (mh-line-end-position) t
)))
987 (unless (and start end
)
990 (unless (file-exists-p (buffer-substring-no-properties start end
))
992 (unless (search-backward "/" start t
)
994 (list (let* ((s (buffer-substring-no-properties start
(1+ (point)))))
995 (unless (string-match mh-swish-folder s
)
997 (if (and (string-match mh-user-path s
)
998 (< (match-end 0) (1- (length s
))))
1000 (substring s
(match-end 0) (1- (length s
))))
1002 (let* ((s (buffer-substring-no-properties (1+ (point)) end
))
1003 (n (ignore-errors (string-to-number s
))))
1004 (if n n
(return 'error
)))
1010 (defvar mh-mairix-binary
(executable-find "mairix"))
1011 (defvar mh-mairix-directory
".mairix")
1012 (defvar mh-mairix-folder nil
)
1014 (defun mh-mairix-execute-search (folder-path search-regexp-list
)
1017 In the examples below, replace \"/home/user/Mail\" with the path
1018 to your MH directory.
1020 First create the directory \"/home/user/Mail/.mairix\". Then
1021 create the file \"/home/user/Mail/.mairix/config\" with the
1024 base=/home/user/Mail
1026 # List of folders that should be indexed. 3 dots at the end means there
1027 # are subfolders within the folder
1028 mh=archive...:inbox:drafts:news:sent:trash
1031 database=/home/user/Mail/mairix/database
1033 Use the following command line to generate the mairix index. Run
1034 this daily from cron:
1036 mairix -f /home/user/Mail/.mairix/config
1038 In a program, FOLDER-PATH is the directory in which
1039 SEARCH-REGEXP-LIST is used to search."
1040 (set-buffer (get-buffer-create mh-temp-index-buffer
))
1042 (unless mh-mairix-binary
1043 (error "Set `mh-mairix-binary' appropriately"))
1044 (apply #'call-process mh-mairix-binary nil
'(t nil
) nil
1045 "-r" "-f" (format "%s%s/config" mh-user-path mh-mairix-directory
)
1047 (goto-char (point-min))
1048 (setq mh-mairix-folder
1049 (let ((last-char (substring folder-path
(1- (length folder-path
)))))
1050 (if (equal last-char
"/")
1052 (format "%s/" folder-path
)))))
1054 (defun mh-mairix-next-result ()
1055 "Return next result from mairix output."
1058 (when (or (eobp) (and (bolp) (eolp)))
1060 (unless (eq (char-after) ?
/)
1062 (let ((start (point))
1064 (setq end
(mh-line-end-position))
1065 (unless (search-forward mh-mairix-folder end t
)
1067 (goto-char (match-beginning 0))
1068 (unless (equal (point) start
)
1071 (unless (search-backward "/" start t
)
1073 (setq msg-start
(1+ (point)))
1075 (unless (search-forward mh-user-path end t
)
1077 (list (format "+%s" (buffer-substring-no-properties
1078 (point) (1- msg-start
)))
1080 (buffer-substring-no-properties msg-start end
))
1084 (defun mh-mairix-regexp-builder (regexp-list)
1085 "Generate query for mairix.
1086 REGEXP-LIST is an alist of fields and values."
1088 (dolist (pair regexp-list
)
1092 (cond ((eq (car pair
) 'to
) "t:")
1093 ((eq (car pair
) 'from
) "f:")
1094 ((eq (car pair
) 'cc
) "c:")
1095 ((eq (car pair
) 'subject
) "s:")
1096 ((eq (car pair
) 'date
) "d:")
1098 (let ((sop (cdr (mh-mairix-convert-to-sop* (cdr pair
))))
1100 (dolist (conjunct sop
)
1101 (let ((expr-list (cdr conjunct
))
1103 (dolist (e expr-list
)
1104 (setq expr-string
(concat expr-string
","
1105 (if (atom e
) "" "~")
1106 (if (atom e
) e
(cadr e
)))))
1107 (setq final
(concat final
"/" (substring expr-string
1)))))
1108 (substring final
1)))
1112 (defun mh-mairix-convert-to-sop* (expr)
1113 "Convert EXPR to sum of product form."
1114 (cond ((atom expr
) `(or (and ,expr
)))
1115 ((eq (car expr
) 'or
)
1117 (loop for e in
(mapcar #'mh-mairix-convert-to-sop
* (cdr expr
))
1119 ((eq (car expr
) 'and
)
1120 (let ((conjuncts (mapcar #'mh-mairix-convert-to-sop
* (cdr expr
)))
1122 (setq result
(pop conjuncts
))
1124 (setq next-factor
(pop conjuncts
))
1125 (setq result
(let ((res ()))
1126 (dolist (t1 (cdr result
))
1127 (dolist (t2 (cdr next-factor
))
1128 (push `(and ,@(cdr t1
) ,@(cdr t2
)) res
)))
1131 ((atom (cadr expr
)) `(or (and ,expr
)))
1132 ((eq (caadr expr
) 'not
) (mh-mairix-convert-to-sop* (cadadr expr
)))
1133 ((eq (caadr expr
) 'and
) (mh-mairix-convert-to-sop*
1134 `(or ,@(mapcar #'(lambda (x) `(not ,x
))
1136 ((eq (caadr expr
) 'or
) (mh-mairix-convert-to-sop*
1137 `(and ,@(mapcar #'(lambda (x) `(not ,x
))
1139 (t (error "Unreachable: %s" expr
))))
1143 (defvar mh-namazu-binary
(executable-find "namazu"))
1144 (defvar mh-namazu-directory
".namazu")
1145 (defvar mh-namazu-folder nil
)
1147 (defun mh-namazu-execute-search (folder-path search-regexp
)
1150 In the examples below, replace \"/home/user/Mail\" with the path to
1153 First create the directory \"/home/user/Mail/.namazu\". Then create
1154 the file \"/home/user/Mail/.namazu/mknmzrc\" with the following
1157 package conf; # Don't remove this line!
1158 $ADDRESS = 'user@localhost';
1159 $ALLOW_FILE = \"[0-9]*\";
1160 $EXCLUDE_PATH = \"^/home/user/Mail/(mhe-index|spam)\";
1162 This configuration does not index the folders that hold the results of
1163 your searches in \"+mhe-index\" since they tend to be ephemeral and
1164 the original messages are indexed anyway.
1166 Use the following command line to generate the namazu index. Run this
1169 mknmz -f /home/user/Mail/.namazu/mknmzrc -O /home/user/Mail/.namazu \\
1172 In a program, FOLDER-PATH is the directory in which SEARCH-REGEXP
1174 (let ((namazu-index-directory
1175 (format "%s%s" mh-user-path mh-namazu-directory
)))
1176 (unless (file-exists-p namazu-index-directory
)
1177 (error "Namazu directory %s not present" namazu-index-directory
))
1178 (unless (executable-find mh-namazu-binary
)
1179 (error "Set `mh-namazu-binary' appropriately"))
1180 (set-buffer (get-buffer-create mh-temp-index-buffer
))
1182 (call-process mh-namazu-binary nil
'(t nil
) nil
1183 "-alR" search-regexp namazu-index-directory
)
1184 (goto-char (point-min))
1185 (setq mh-namazu-folder
1186 (let ((last (substring folder-path
(1- (length folder-path
)))))
1187 (if (equal last
"/")
1189 (format "%s/" folder-path
))))))
1191 (defun mh-namazu-next-result ()
1192 "Get the next result from namazu output."
1195 (when (eobp) (return nil
))
1196 (let ((file-name (buffer-substring-no-properties
1197 (point) (mh-line-end-position))))
1198 (unless (equal (string-match mh-namazu-folder file-name
) 0)
1200 (unless (file-exists-p file-name
)
1202 (string-match mh-user-path file-name
)
1203 (let* ((folder/msg
(substring file-name
(match-end 0)))
1204 (mark (mh-search-from-end ?
/ folder
/msg
)))
1205 (unless mark
(return 'error
))
1206 (list (format "+%s" (substring folder
/msg
0 mark
))
1207 (let ((n (ignore-errors (string-to-number
1208 (substring folder
/msg
(1+ mark
))))))
1209 (if n n
(return 'error
)))
1215 (defvar mh-index-pick-folder
)
1216 (defvar mh-pick-binary
"pick")
1217 (defconst mh-pick-single-dash
'(cc date from subject to
)
1218 "Search components that are supported by single-dash option in pick.")
1220 (defun mh-pick-execute-search (folder-path search-regexp
)
1223 Read \"pick(1)\" or the section Finding Messages with pick in the
1224 MH book to find out more about how to enter the criteria (see URL
1225 `http://www.ics.uci.edu/~mh/book/mh/finpic.htm').
1227 In a program, FOLDER-PATH is the directory in which SEARCH-REGEXP
1229 (set-buffer (get-buffer-create mh-temp-index-buffer
))
1232 (mh-folder-list (substring folder-path
(length mh-user-path
)))))
1233 (loop for folder in folders do
1234 (setq folder
(concat "+" folder
))
1235 (insert folder
"\n")
1236 (apply #'call-process
(expand-file-name "pick" mh-progs
)
1237 nil
'(t nil
) nil folder
"-list" search-regexp
)))
1238 (goto-char (point-min)))
1240 (defun mh-pick-next-result ()
1241 "Return the next pick search result."
1244 (when (eobp) (return nil
))
1245 (when (search-forward-regexp "^\+" (mh-line-end-position) t
)
1246 (setq mh-index-pick-folder
1247 (buffer-substring-no-properties (mh-line-beginning-position)
1248 (mh-line-end-position)))
1250 (unless (search-forward-regexp "^[1-9][0-9]*$" (mh-line-end-position) t
)
1252 (list mh-index-pick-folder
1254 (buffer-substring-no-properties (mh-line-beginning-position)
1255 (mh-line-end-position)))
1259 ;; All implementations of pick have special options -cc, -date, -from and
1260 ;; -subject that allow to search for corresponding components. Any other
1261 ;; component is searched using option --COMPNAME, for example: `pick
1262 ;; --x-mailer mh-e'. Mailutils "pick" supports this option using a certain
1263 ;; kludge, but it prefers the following syntax for this purpose:
1264 ;; "--component=COMPNAME --pattern=PATTERN".
1265 ;; -- Sergey Poznyakoff, Aug 2003
1266 (defun mh-pick-regexp-builder (pattern-list)
1267 "Generate pick search expression from PATTERN-LIST."
1269 (dolist (pattern pattern-list
)
1271 (setq result
`(,@result
"-and" "-lbrace"
1272 ,@(mh-pick-construct-regexp
1273 (if (and (mh-variant-p 'mu-mh
) (car pattern
))
1274 (format "--pattern=%s" (cdr pattern
))
1278 ((mh-variant-p 'mu-mh
)
1279 (format "--component=%s" (car pattern
)))
1280 ((member (car pattern
) mh-pick-single-dash
)
1281 (format "-%s" (car pattern
)))
1283 (format "--%s" (car pattern
))))
1288 (defun mh-pick-construct-regexp (expr component
)
1289 "Construct pick compatible expression corresponding to EXPR.
1290 COMPONENT is the component to search."
1291 (cond ((atom expr
) (list component expr
))
1292 ((eq (car expr
) 'and
)
1293 `("-lbrace" ,@(mh-pick-construct-regexp (cadr expr
) component
) "-and"
1294 ,@(mh-pick-construct-regexp (caddr expr
) component
) "-rbrace"))
1295 ((eq (car expr
) 'or
)
1296 `("-lbrace" ,@(mh-pick-construct-regexp (cadr expr
) component
) "-or"
1297 ,@(mh-pick-construct-regexp (caddr expr
) component
) "-rbrace"))
1298 ((eq (car expr
) 'not
)
1299 `("-lbrace" "-not" ,@(mh-pick-construct-regexp (cadr expr
) component
)
1301 (t (error "Unknown operator %s seen" (car expr
)))))
1305 (defvar mh-grep-binary
(executable-find "grep"))
1307 (defun mh-grep-execute-search (folder-path search-regexp
)
1310 Unlike the other search methods, this method does not use the
1311 MH-Search buffer. Instead, you simply enter a regular expression
1312 in the minibuffer. For help in constructing regular expressions,
1313 see your man page for \"grep\".
1315 In a program, FOLDER-PATH is the directory in which SEARCH-REGEXP
1317 (set-buffer (get-buffer-create mh-temp-index-buffer
))
1319 (call-process mh-grep-binary nil
'(t nil
) nil
1320 "-i" "-r" search-regexp folder-path
)
1321 (goto-char (point-min)))
1323 (defun mh-grep-next-result ()
1324 "Read the next result.
1325 Parse it and return the message folder, message index and the
1326 match. If no other matches left then return nil. If the current
1327 record is invalid return 'error."
1332 (let ((eol-pos (mh-line-end-position))
1333 (bol-pos (mh-line-beginning-position))
1334 folder-start msg-end
)
1336 (unless (search-forward mh-user-path eol-pos t
)
1338 (setq folder-start
(point))
1339 (unless (search-forward ":" eol-pos t
)
1341 (let ((match (buffer-substring-no-properties (point) eol-pos
)))
1343 (setq msg-end
(point))
1344 (unless (search-backward "/" folder-start t
)
1346 (list (format "+%s" (buffer-substring-no-properties
1347 folder-start
(point)))
1348 (let ((n (ignore-errors (string-to-number
1349 (buffer-substring-no-properties
1350 (1+ (point)) msg-end
)))))
1351 (if n n
(return 'error
)))
1357 ;;; Folder Utilities
1360 (defun mh-index-group-by-folder ()
1361 "Partition the messages based on source folder.
1362 Returns an alist with the the folder names in the car and the cdr
1363 being the list of messages originally from that folder."
1365 (goto-char (point-min))
1366 (let ((result-table (make-hash-table :test
#'equal
)))
1367 (loop for msg being hash-keys of mh-index-msg-checksum-map
1368 do
(push msg
(gethash (car (gethash
1369 (gethash msg mh-index-msg-checksum-map
)
1370 mh-index-checksum-origin-map
))
1372 (loop for x being the hash-keys of result-table
1373 collect
(cons x
(nreverse (gethash x result-table
)))))))
1376 (defun mh-index-insert-folder-headers ()
1377 "Annotate the search results with original folder names."
1378 (let ((cur-msg (mh-get-msg-num nil
))
1379 (old-buffer-modified-flag (buffer-modified-p))
1380 (buffer-read-only nil
)
1381 current-folder last-folder
)
1382 (goto-char (point-min))
1384 (setq current-folder
(car (gethash (gethash (mh-get-msg-num nil
)
1385 mh-index-msg-checksum-map
)
1386 mh-index-checksum-origin-map
)))
1387 (when (and current-folder
(not (equal current-folder last-folder
)))
1388 (insert (if last-folder
"\n" "") current-folder
"\n")
1389 (setq last-folder current-folder
))
1393 (mh-goto-msg cur-msg t
))
1394 (set-buffer-modified-p old-buffer-modified-flag
))
1395 (mh-index-create-imenu-index))
1398 (defun mh-index-delete-folder-headers ()
1399 "Delete the folder headers."
1400 (let ((cur-msg (mh-get-msg-num nil
))
1401 (old-buffer-modified-flag (buffer-modified-p))
1402 (buffer-read-only nil
))
1403 (while (and (not cur-msg
) (not (eobp)))
1405 (setq cur-msg
(mh-get-msg-num nil
)))
1406 (goto-char (point-min))
1408 (if (or (char-equal (char-after) ?
+) (char-equal (char-after) 10))
1409 (delete-region (point) (progn (forward-line) (point)))
1411 (when cur-msg
(mh-goto-msg cur-msg t t
))
1412 (set-buffer-modified-p old-buffer-modified-flag
)))
1414 (mh-require 'which-func nil t
)
1418 (if (or mh-xemacs-flag
(< emacs-major-version
22))
1419 (defvar which-func-mode
)))
1422 (defun mh-index-create-imenu-index ()
1423 "Create alist of folder names and positions in index folder buffers."
1425 (if (boundp 'which-func-mode
)
1426 (setq which-func-mode t
))
1428 (goto-char (point-min))
1429 (while (re-search-forward "^+" nil t
)
1432 (push (cons (buffer-substring-no-properties
1433 (point) (mh-line-end-position))
1434 (set-marker (make-marker) (point)))
1436 (setq imenu--index-alist
(nreverse alist
)))))
1439 (defun mh-search-p ()
1440 "Non-nil means that this folder was generated by searching."
1444 (eval-when-compile (if mh-xemacs-flag
(defvar mh-speed-flists-inhibit-flag
)))
1447 (defun mh-index-execute-commands ()
1448 "Delete/refile the actual messages.
1449 The copies in the searched folder are then deleted/refiled to get
1450 the desired result. Before deleting the messages we make sure
1451 that the message being deleted is identical to the one that the
1452 user has marked in the index buffer."
1455 (mh-speed-flists-inhibit-flag t
))
1457 (lambda (folder msgs
)
1458 (push folder folders
)
1459 (if (not (get-buffer folder
))
1460 ;; If source folder not open, just delete the messages...
1461 (apply #'mh-exec-cmd
"rmm" folder
(mh-coalesce-msg-list msgs
))
1462 ;; Otherwise delete the messages in the source buffer...
1465 (let ((old-refile-list mh-refile-list
)
1466 (old-delete-list mh-delete-list
))
1467 (setq mh-refile-list nil
1468 mh-delete-list msgs
)
1469 (unwind-protect (mh-execute-commands)
1470 (setq mh-refile-list
1473 (loop for y in
(cdr x
)
1474 unless
(memq y msgs
) collect y
)))
1477 (loop for x in old-delete-list
1478 unless
(memq x msgs
) collect x
))
1479 (mh-set-folder-modified-p (mh-outstanding-commands-p))
1480 (when (mh-outstanding-commands-p)
1481 (mh-notate-deleted-and-refiled)))))))
1482 (mh-index-matching-source-msgs (append (loop for x in mh-refile-list
1488 (defun mh-index-generate-pretty-name (string)
1489 "Given STRING generate a name which is suitable for use as a folder name.
1490 White space from the beginning and end are removed. All spaces in
1491 the name are replaced with underscores and all / are replaced
1492 with $. If STRING is longer than 20 it is truncated too. STRING
1493 could be a list of strings in which case they are concatenated to
1494 construct the base name."
1496 (if (stringp string
)
1498 (when (car string
) (insert (car string
)))
1499 (dolist (s (cdr string
))
1501 (setq string
(mh-replace-string "-lbrace" " "))
1502 (setq string
(mh-replace-string "-rbrace" " "))
1503 (setq string
(mh-replace-string "-search" " "))
1504 (subst-char-in-region (point-min) (point-max) ?
( ? t
)
1505 (subst-char-in-region (point-min) (point-max) ?
) ? t
)
1506 (subst-char-in-region (point-min) (point-max) ?- ? t
)
1507 (goto-char (point-min))
1508 (while (and (not (eobp)) (memq (char-after) '(? ?
\t ?
\n ?
\r ?_
)))
1510 (goto-char (point-max))
1511 (while (and (not (bobp)) (memq (char-before) '(? ?
\t ?
\n ?
\r ?_
)))
1512 (delete-backward-char 1))
1513 (subst-char-in-region (point-min) (point-max) ? ?_ t
)
1514 (subst-char-in-region (point-min) (point-max) ?
\t ?_ t
)
1515 (subst-char-in-region (point-min) (point-max) ?
\n ?_ t
)
1516 (subst-char-in-region (point-min) (point-max) ?
\r ?_ t
)
1517 (subst-char-in-region (point-min) (point-max) ?
/ ?$ t
)
1518 (let ((out (truncate-string-to-width (buffer-string) 20)))
1519 (cond ((eq mh-searcher
'flists
)
1520 (format "%s/%s" mh-flists-results-folder mh-flists-sequence
))
1521 ((equal out mh-flists-results-folder
) (concat out
"1"))
1524 (defun mh-folder-exists-p (folder)
1525 "Check if FOLDER exists."
1526 (and (mh-folder-name-p folder
)
1529 (mh-exec-cmd-output "folder" nil
"-fast" "-nocreate" folder
)
1530 (goto-char (point-min))
1533 (defun mh-msg-exists-p (msg folder
)
1534 "Check if MSG exists in FOLDER."
1535 (file-exists-p (format "%s%s/%s" mh-user-path
(substring folder
1) msg
)))
1537 (defun mh-index-new-folder (name search-regexp
)
1538 "Return a folder name based on NAME for search results of SEARCH-REGEXP.
1540 If folder NAME already exists and was generated for the same
1541 SEARCH-REGEXP then it is reused.
1543 Otherwise if the folder NAME was generated from a different
1544 search then check if NAME<2> can be used. Otherwise try NAME<3>.
1545 This is repeated till we find a new folder name.
1547 If the folder returned doesn't exist then it is created."
1548 (unless (mh-folder-name-p name
)
1549 (error "The argument should be a valid MH folder name"))
1552 for candidate
= (if (equal i
1) name
(format "%s<%s>" name i
))
1553 when
(or (not (mh-folder-exists-p candidate
))
1554 (equal (mh-index-folder-search-regexp candidate
)
1557 ;; Do pending refiles/deletes...
1558 (when (get-buffer chosen-name
)
1559 (mh-process-or-undo-commands chosen-name
))
1560 ;; Recreate folder...
1561 (save-excursion (mh-exec-cmd-quiet nil
"rmf" chosen-name
))
1562 (mh-exec-cmd-quiet nil
"folder" "-create" "-fast" chosen-name
)
1563 (mh-remove-from-sub-folders-cache chosen-name
)
1564 (when (boundp 'mh-speed-folder-map
)
1565 (mh-speed-add-folder chosen-name
))
1568 (defun mh-index-folder-search-regexp (folder)
1569 "If FOLDER was created by a index search, return the search regexp.
1570 Return nil if FOLDER doesn't exist or the .mhe_index file is
1574 (insert-file-contents
1575 (format "%s%s/%s" mh-user-path
(substring folder
1) mh-index-data-file
))
1576 (goto-char (point-min))
1578 (cadr (read (current-buffer))))))
1582 ;;; Sequence Support
1585 (defun mh-index-create-sequences ()
1586 "Mirror sequences present in source folders in index folder."
1587 (let ((seq-hash (make-hash-table :test
#'equal
))
1589 (loop for folder being the hash-keys of mh-index-data
1590 do
(setf (gethash folder seq-hash
)
1591 (mh-create-sequence-map
1592 (mh-read-folder-sequences folder nil
))))
1593 (dolist (msg (mh-translate-range mh-current-folder
"all"))
1594 (let* ((checksum (gethash msg mh-index-msg-checksum-map
))
1595 (pair (gethash checksum mh-index-checksum-origin-map
))
1596 (ofolder (car pair
))
1598 (loop for seq in
(ignore-errors
1599 (gethash omsg
(gethash ofolder seq-hash
)))
1600 do
(if (assoc seq seq-list
)
1601 (push msg
(cdr (assoc seq seq-list
)))
1602 (push (list seq msg
) seq-list
)))))
1603 (loop for seq in seq-list
1604 do
(apply #'mh-exec-cmd
"mark" mh-current-folder
1605 "-sequence" (symbol-name (car seq
)) "-add"
1606 (mapcar #'(lambda (x) (format "%s" x
)) (cdr seq
))))))
1609 (defun mh-create-sequence-map (seq-list)
1610 "Return a map from msg number to list of sequences in which it is present.
1611 SEQ-LIST is an assoc list whose keys are sequence names and whose
1612 cdr is the list of messages in that sequence."
1613 (loop with map
= (make-hash-table)
1615 when
(and (not (memq (car seq
) (mh-unpropagated-sequences)))
1616 (mh-valid-seq-p (car seq
)))
1617 do
(loop for msg in
(cdr seq
)
1618 do
(push (car seq
) (gethash msg map
)))
1619 finally return map
))
1622 (defun mh-index-add-to-sequence (seq msgs
)
1623 "Add to SEQ the messages in the list MSGS.
1624 This function updates the source folder sequences. Also makes an
1625 attempt to update the source folder buffer if we have it open."
1626 ;; Don't need to do anything for cur
1628 (when (and (not (memq seq
(mh-unpropagated-sequences)))
1629 (mh-valid-seq-p seq
))
1631 (mh-speed-flists-inhibit-flag t
))
1632 (maphash (lambda (folder msgs
)
1633 (push folder folders
)
1634 ;; Add messages to sequence in source folder...
1635 (apply #'mh-exec-cmd-quiet nil
"mark" folder
1636 "-add" "-nozero" "-sequence" (symbol-name seq
)
1637 (mapcar (lambda (x) (format "%s" x
))
1638 (mh-coalesce-msg-list msgs
)))
1639 ;; Update source folder buffer if we have it open...
1640 (when (get-buffer folder
)
1643 (mh-put-msg-in-seq msgs seq
))))
1644 (mh-index-matching-source-msgs msgs
))
1648 (defun mh-index-delete-from-sequence (seq msgs
)
1649 "Delete from SEQ the messages in MSGS.
1650 This function updates the source folder sequences. Also makes an
1651 attempt to update the source folder buffer if present."
1653 (when (and (not (memq seq
(mh-unpropagated-sequences)))
1654 (mh-valid-seq-p seq
))
1656 (mh-speed-flists-inhibit-flag t
))
1657 (maphash (lambda (folder msgs
)
1658 (push folder folders
)
1659 ;; Remove messages from sequence in source folder...
1660 (apply #'mh-exec-cmd-quiet nil
"mark" folder
1661 "-del" "-nozero" "-sequence" (symbol-name seq
)
1662 (mapcar (lambda (x) (format "%s" x
))
1663 (mh-coalesce-msg-list msgs
)))
1664 ;; Update source folder buffer if we have it open...
1665 (when (get-buffer folder
)
1668 (mh-delete-msg-from-seq msgs seq t
))))
1669 (mh-index-matching-source-msgs msgs
))
1672 (defvar mh-unpropagated-sequences
'(cur range subject search
)
1673 "List of sequences that aren't preserved.")
1675 (defun mh-unpropagated-sequences ()
1676 "Return a list of sequences that aren't propagated to the source folders.
1677 It is just the sequences in the variable
1678 `mh-unpropagated-sequences' in addition to the
1679 Previous-Sequence (see mh-profile 5)."
1681 (cons mh-previous-seq mh-unpropagated-sequences
)
1682 mh-unpropagated-sequences
))
1684 (defun mh-index-matching-source-msgs (msgs &optional delete-from-index-data
)
1685 "Return a table of original messages and folders for messages in MSGS.
1686 If optional argument DELETE-FROM-INDEX-DATA is non-nil, then each
1687 of the messages, whose counter-part is found in some source
1688 folder, is removed from `mh-index-data'."
1689 (let ((table (make-hash-table :test
#'equal
)))
1691 (let* ((checksum (gethash msg mh-index-msg-checksum-map
))
1692 (pair (gethash checksum mh-index-checksum-origin-map
)))
1693 (when (and checksum
(car pair
) (cdr pair
)
1694 (mh-index-match-checksum (cdr pair
) (car pair
) checksum
))
1695 (push (cdr pair
) (gethash (car pair
) table
))
1696 (when delete-from-index-data
1697 (remhash (cdr pair
) (gethash (car pair
) mh-index-data
))))))
1700 (defun mh-index-match-checksum (msg folder checksum
)
1701 "Check if MSG in FOLDER has X-MHE-Checksum header value of CHECKSUM."
1703 (mh-exec-cmd-output mh-scan-prog nil
"-width" "80"
1704 "-format" "%{x-mhe-checksum}\n" folder msg
)
1705 (goto-char (point-min))
1706 (string-equal (buffer-substring-no-properties
1707 (point) (mh-line-end-position))
1712 ;;; Serialization of Index Data
1714 (defun mh-index-write-data ()
1715 "Write index data to file."
1717 (unless (eq major-mode
'mh-folder-mode
)
1718 (error "Can't be called from folder in \"%s\"" major-mode
))
1719 (let ((data mh-index-data
)
1720 (msg-checksum-map mh-index-msg-checksum-map
)
1721 (checksum-origin-map mh-index-checksum-origin-map
)
1722 (previous-search mh-index-previous-search
)
1723 (sequence-search-flag mh-index-sequence-search-flag
)
1724 (outfile (concat buffer-file-name mh-index-data-file
))
1727 (with-temp-file outfile
1728 (mh-index-write-hashtable
1729 data
(lambda (x) (loop for y being the hash-keys of x collect y
)))
1730 (mh-index-write-hashtable msg-checksum-map
#'identity
)
1731 (mh-index-write-hashtable checksum-origin-map
#'identity
)
1732 (pp previous-search
(current-buffer)) (insert "\n")
1733 (pp sequence-search-flag
(current-buffer)) (insert "\n")))))
1735 (defun mh-index-write-hashtable (table proc
)
1736 "Write TABLE to `current-buffer'.
1737 PROC is used to serialize the values corresponding to the hash
1739 (pp (loop for x being the hash-keys of table
1740 collect
(cons x
(funcall proc
(gethash x table
))))
1745 (defun mh-index-read-data ()
1746 "Read index data from file."
1748 (unless (eq major-mode
'mh-folder-mode
)
1749 (error "Can't be called from folder in \"%s\"" major-mode
))
1750 (let ((infile (concat buffer-file-name mh-index-data-file
))
1753 (insert-file-contents-literally infile
)
1754 (goto-char (point-min))
1755 (setq t1
(mh-index-read-hashtable
1757 (loop with table
= (make-hash-table :test
#'equal
)
1758 for x in data do
(setf (gethash x table
) t
)
1759 finally return table
)))
1760 t2
(mh-index-read-hashtable #'identity
)
1761 t3
(mh-index-read-hashtable #'identity
)
1762 t4
(read (current-buffer))
1763 t5
(read (current-buffer))))
1764 (setq mh-index-data t1
1765 mh-index-msg-checksum-map t2
1766 mh-index-checksum-origin-map t3
1767 mh-index-previous-search t4
1768 mh-index-sequence-search-flag t5
))))
1770 (defun mh-index-read-hashtable (proc)
1771 "From BUFFER read a hash table serialized as a list.
1772 PROC is used to convert the value to actual data."
1773 (loop with table
= (make-hash-table :test
#'equal
)
1774 for pair in
(read (current-buffer))
1775 do
(setf (gethash (car pair
) table
) (funcall proc
(cdr pair
)))
1776 finally return table
))
1780 ;;; Checksum Routines
1782 ;; A few different checksum programs are supported. The supported
1789 ;; To add support for your favorite checksum program add a clause to
1790 ;; the cond statement in mh-checksum-choose. This should set the
1791 ;; variable mh-checksum-cmd to the command line needed to run the
1792 ;; checsum program and should set mh-checksum-parser to a function
1793 ;; which returns a cons cell containing the message number and
1796 (defvar mh-checksum-cmd
)
1797 (defvar mh-checksum-parser
)
1799 (defun mh-checksum-choose ()
1800 "Check if a program to create a checksum is present."
1801 (unless (boundp 'mh-checksum-cmd
)
1802 (let ((exec-path (append '("/sbin" "/usr/sbin") exec-path
)))
1803 (cond ((executable-find "md5sum")
1804 (setq mh-checksum-cmd
(list (executable-find "md5sum")))
1805 (setq mh-checksum-parser
#'mh-md5sum-parser
))
1806 ((executable-find "openssl")
1807 (setq mh-checksum-cmd
(list (executable-find "openssl") "md5"))
1808 (setq mh-checksum-parser
#'mh-openssl-parser
))
1809 ((executable-find "md5")
1810 (setq mh-checksum-cmd
(list (executable-find "md5")))
1811 (setq mh-checksum-parser
#'mh-md5-parser
))
1812 (t (error "No suitable checksum program"))))))
1814 (defun mh-md5sum-parser ()
1815 "Parse md5sum output."
1816 (let ((begin (mh-line-beginning-position))
1817 (end (mh-line-end-position))
1818 first-space last-slash
)
1819 (setq first-space
(search-forward " " end t
))
1821 (setq last-slash
(search-backward "/" begin t
))
1822 (cond ((and first-space last-slash
)
1823 (cons (string-to-number (buffer-substring-no-properties
1824 (1+ last-slash
) end
))
1825 (buffer-substring-no-properties begin
(1- first-space
))))
1826 (t (cons nil nil
)))))
1828 (defun mh-openssl-parser ()
1829 "Parse openssl output."
1830 (let ((begin (mh-line-beginning-position))
1831 (end (mh-line-end-position))
1832 last-space last-slash
)
1834 (setq last-space
(search-backward " " begin t
))
1835 (setq last-slash
(search-backward "/" begin t
))
1836 (cond ((and last-slash last-space
)
1837 (cons (string-to-number (buffer-substring-no-properties
1838 (1+ last-slash
) (1- last-space
)))
1839 (buffer-substring-no-properties (1+ last-space
) end
))))))
1841 (defalias 'mh-md5-parser
'mh-openssl-parser
)
1844 (defun mh-index-update-maps (folder &optional origin-map
)
1845 "Annotate all as yet unannotated messages in FOLDER with their MD5 hash.
1846 As a side effect msg -> checksum map is updated. Optional
1847 argument ORIGIN-MAP is a hashtable which maps each message in the
1848 index folder to the original folder and message from whence it
1849 was copied. If present the checksum -> (origin-folder,
1850 origin-index) map is updated too."
1851 (clrhash mh-index-msg-checksum-map
)
1853 ;; Clear temp buffer
1854 (set-buffer (get-buffer-create mh-temp-checksum-buffer
))
1856 ;; Run scan to check if any messages needs MD5 annotations at all
1858 (mh-exec-cmd-output mh-scan-prog nil
"-width" "80"
1859 "-format" "%(msg)\n%{x-mhe-checksum}\n"
1861 (goto-char (point-min))
1864 (setq msg
(buffer-substring-no-properties
1865 (point) (mh-line-end-position)))
1868 (cond ((not (string-match "^[0-9]*$" msg
)))
1870 ;; need to compute checksum
1871 (set-buffer mh-temp-checksum-buffer
)
1872 (insert mh-user-path
(substring folder
1) "/" msg
"\n"))
1875 (setq checksum
(buffer-substring-no-properties
1876 (point) (mh-line-end-position)))
1877 (let ((msg (string-to-number msg
)))
1879 (mh-index-update-single-msg msg checksum origin-map
)))))
1881 ;; Run checksum program if needed
1882 (unless (and (eobp) (bobp))
1883 (apply #'mh-xargs mh-checksum-cmd
)
1884 (goto-char (point-min))
1886 (let* ((intermediate (funcall mh-checksum-parser
))
1887 (msg (car intermediate
))
1888 (checksum (cdr intermediate
)))
1891 (mh-exec-cmd "anno" folder msg
"-component" "X-MHE-Checksum"
1892 "-nodate" "-text" checksum
"-inplace")
1896 (mh-index-update-single-msg msg checksum origin-map
)))
1898 (mh-index-write-data))
1900 (defun mh-index-update-single-msg (msg checksum origin-map
)
1901 "Update various maps for one message.
1902 MSG is a index folder message, CHECKSUM its MD5 hash and
1903 ORIGIN-MAP, if non-nil, a hashtable containing which maps each
1904 message in the index folder to the folder and message that it was
1905 copied from. The function updates the hash tables
1906 `mh-index-msg-checksum-map' and `mh-index-checksum-origin-map'.
1908 This function should only be called in the appropriate index
1910 (cond ((and origin-map
(gethash checksum mh-index-checksum-origin-map
))
1911 (let* ((intermediate (gethash msg origin-map
))
1912 (ofolder (car intermediate
))
1913 (omsg (cdr intermediate
)))
1914 ;; This is most probably a duplicate. So eliminate it.
1915 (call-process "rm" nil nil nil
1916 (format "%s%s/%s" mh-user-path
1917 (substring mh-current-folder
1) msg
))
1918 (when (gethash ofolder mh-index-data
)
1919 (remhash omsg
(gethash ofolder mh-index-data
)))))
1921 (setf (gethash msg mh-index-msg-checksum-map
) checksum
)
1923 (setf (gethash checksum mh-index-checksum-origin-map
)
1924 (gethash msg origin-map
))))))
1926 (provide 'mh-search
)
1929 ;; indent-tabs-mode: nil
1930 ;; sentence-end-double-space: nil
1933 ;; arch-tag: 607762ad-0dff-4fe1-a27e-6c0dde0dcc47
1934 ;;; mh-search ends here