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.
54 (require 'which-func nil t)
56 (defvar mh-searcher nil
57 "Cached value of chosen search program.")
59 (defvar mh-search-function nil
60 "Function which executes the search program.")
62 (defvar mh-search-next-result-function nil
63 "Function to parse the next line of output.
64 Expected to return a list of three strings: name of the folder,
65 message number, and optionally the match.")
67 (defvar mh-search-regexp-builder nil
68 "Function used to construct search regexp.")
70 (defvar mh-index-folder "+mhe-index"
71 "Folder that contains the folders resulting from the index searches.")
73 (defvar mh-flists-results-folder "sequence"
74 "Subfolder for `mh-index-folder' where flists output is placed.")
76 (defvar mh-flists-sequence)
78 (defvar mh-flists-called-flag nil)
82 ;;; MH-Folder Commands
85 (defun* mh-search (folder search-regexp
86 &optional redo-search-flag window-config)
89 This command helps you find messages in your entire corpus of
90 mail. You can search for messages to or from a particular person
91 or about a particular subject. In fact, you can also search for
92 messages containing selected strings in any arbitrary header
93 field or any string found within the messages.
95 Out of the box, MH-E uses \"pick\" to find messages. With a
96 little extra effort, you can set an indexing program which
97 rewards you with extremely quick results. The drawback is that
98 sometimes the index does not contain the words you're looking
99 for. You can still use \"pick\" in these situations.
101 You are prompted for the FOLDER to search. This can be \"all\" to
102 search all folders. Note that the search works recursively on the
105 Next, an MH-Search buffer appears where you can enter search
106 criteria SEARCH-REGEXP.
115 Edit this template by entering your search criteria in an
116 appropriate header field that is already there, or create a new
117 field yourself. If the string you're looking for could be
118 anywhere in a message, then place the string underneath the row
121 As an example, let's say that we want to find messages from
122 Ginnean about horseback riding in the Kosciusko National
123 Park (Australia) during January, 1994. Normally we would start
124 with a broad search and narrow it down if necessary to produce a
125 manageable amount of data, but we'll cut to the chase and create
126 a fairly restrictive set of criteria as follows:\\<mh-search-mode-map>
137 As with MH-Letter mode, MH-Search provides commands like
138 \\[mh-to-field] to help you fill in the blanks.\\<mh-folder-mode-map>
140 If you find that you do the same thing over and over when editing
141 the search template, you may wish to bind some shortcuts to keys.
142 This can be done with the variable `mh-search-mode-hook', which is
143 called when \\[mh-search] is run on a new pattern.\\<mh-search-mode-map>
145 To perform the search, type \\[mh-index-do-search].
147 Sometimes you're searching for text that is either not indexed,
148 or hasn't been indexed yet. In this case you can override the
149 default method with the pick method by running the command
150 \\[mh-pick-do-search].
152 The messages that are found are put in a temporary sub-folder of
153 \"+mhe-index\" and are displayed in an MH-Folder buffer. This
154 buffer is special because it displays messages from multiple
155 folders; each set of messages from a given folder has a heading
156 with the folder name.\\<mh-folder-mode-map>
158 The appearance of the heading can be modified by customizing the
159 face `mh-search-folder'. You can jump back and forth between the
160 headings using the commands \\[mh-index-next-folder] and
161 \\[mh-index-previous-folder].
163 In addition, the command \\[mh-index-visit-folder] can be used to
164 visit the folder of the message at point. Initially, only the
165 messages that matched the search criteria are displayed in the
166 folder. While the temporary buffer has its own set of message
167 numbers, the actual messages numbers are shown in the visited
168 folder. Thus, the command \\[mh-index-visit-folder] is useful to
169 find the actual message number of an interesting message, or to
170 view surrounding messages with the command \\[mh-rescan-folder].
172 Because this folder is temporary, you'll probably get in the
173 habit of killing it when you're done with \\[mh-kill-folder].
175 You can regenerate the results by running this command with a
176 prefix argument REDO-SEARCH-FLAG.
178 Note: This command uses an \"X-MHE-Checksum:\" header field to
179 cache the MD5 checksum of a message. This means that if an
180 incoming message already contains an \"X-MHE-Checksum:\" field,
181 that message might not be found by this command. The following
182 \"procmail\" recipe avoids this problem by renaming the existing
186 | formail -R \"X-MHE-Checksum\" \"X-Old-MHE-Checksum\"
188 Configuring Indexed Searches
190 The command \\[mh-search] runs the command defined by the option
191 `mh-search-program'. The default value is \"Auto-detect\" which
192 means that MH-E will automatically choose one of \"swish++\",
193 \"swish-e\", \"mairix\", \"namazu\", \"pick\" and \"grep\" in
194 that order. If, for example, you have both \"swish++\" and
195 \"mairix\" installed and you want to use \"mairix\", then you can
196 set this option to \"mairix\".
198 The documentation for the following commands describe how to set
199 up the various indexing programs to use with MH-E.
201 - `mh-swish++-execute-search'
202 - `mh-swish-execute-search'
203 - `mh-mairix-execute-search'
204 - `mh-namazu-execute-search'
205 - `mh-pick-execute-search'
206 - `mh-grep-execute-search'
208 In a program, if FOLDER is \"+\" or nil, then mail in all folders
209 are searched. Optional argument WINDOW-CONFIG stores the window
210 configuration that will be restored after the user quits the
211 folder containing the index search results."
215 ;; Yes, we do want to call mh-search-choose every time in case the
216 ;; user has switched the searcher manually.
217 (unless (mh-search-choose (and current-prefix-arg
218 mh-index-previous-search
219 (cadr mh-index-previous-search)))
220 (error "No search program found"))
221 (or (and current-prefix-arg mh-index-sequence-search-flag)
222 (and current-prefix-arg (car mh-index-previous-search))
223 (mh-prompt-for-folder "Search" "+" nil "all" t)))
224 (or (and current-prefix-arg (caddr mh-index-previous-search))
225 mh-search-regexp-builder
226 (read-string (format "%s regexp: "
227 (upcase-initials (symbol-name mh-searcher)))))
229 (if (and (not (and current-prefix-arg
230 (caddr mh-index-previous-search)))
231 mh-search-regexp-builder)
232 (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"
291 "-sequence" "cur" (format "%s" cur)))
292 (loop for msg in msgs
293 do (incf result-count)
294 (setf (gethash result-count origin-map)
295 (cons folder msg)))))
298 ;; Vist the results folder.
299 (mh-visit-folder index-folder () (list folder-results-map origin-map))
301 (goto-char (point-min))
303 (mh-update-sequences)
306 ;; Update the speedbar, if needed.
307 (when (mh-speed-flists-active-p)
308 (mh-speed-flists t mh-current-folder))
311 (when (or (and redo-search-flag previous-search) window-config)
312 (setq mh-previous-window-config old-window-config))
313 (setq mh-index-previous-search (list folder mh-searcher search-regexp))
315 ;; Write out data to disk.
316 (unless mh-flists-called-flag (mh-index-write-data))
318 (message "%s found %s matches in %s folders"
319 (upcase-initials (symbol-name mh-searcher))
320 (loop for msg-hash being hash-values of mh-index-data
321 sum (hash-table-count msg-hash))
322 (loop for msg-hash being hash-values of mh-index-data
323 count (> (hash-table-count msg-hash) 0))))))
326 (eval-when-compile (mh-do-in-xemacs (defvar pick-folder)))
328 (defun mh-search-folder (folder window-config)
329 "Search FOLDER for messages matching a pattern.
331 In a program, argument WINDOW-CONFIG is the current window
332 configuration and is used when the search folder is dismissed."
333 (interactive (list (mh-prompt-for-folder "Search" mh-current-folder nil nil t)
334 (current-window-configuration)))
335 (let ((pick-folder (if (equal folder "+") mh-current-folder folder)))
336 (switch-to-buffer-other-window "search-pattern")
337 (if (or (zerop (buffer-size))
338 (not (y-or-n-p "Reuse pattern? ")))
339 (mh-make-pick-template)
341 (mh-make-local-vars 'mh-current-folder folder
342 'mh-previous-window-config window-config)
343 (message "%s" (substitute-command-keys
344 (concat "Type \\[mh-index-do-search] to search messages, "
345 "\\[mh-pick-do-search] to use pick, "
346 "\\[mh-help] for help")))))
348 (defun mh-make-pick-template ()
349 "Initialize the current buffer with a template for a pick pattern."
350 (let ((inhibit-read-only t)) (erase-buffer))
358 (goto-char (point-min))
360 (add-text-properties (point) (1+ (point)) '(front-sticky t))
361 (add-text-properties (- (line-end-position) 2) (1- (line-end-position))
363 (add-text-properties (point) (1- (line-end-position)) '(read-only t))
365 (add-text-properties (point) (1+ (point)) '(front-sticky t))
366 (add-text-properties (point) (1- (line-end-position)) '(read-only t))
367 (goto-char (point-max)))
372 (defun mh-index-new-messages (folders)
373 "Display unseen messages.
375 If you use a program such as \"procmail\" to use \"rcvstore\" to file
376 your incoming mail automatically, you can display new, unseen,
377 messages using this command. All messages in the \"unseen\"
378 sequence from the folders in `mh-new-messages-folders' are
381 With a prefix argument, enter a space-separated list of FOLDERS,
382 or nothing to search all folders."
384 (list (if current-prefix-arg
385 (split-string (read-string "Search folder(s) (default all): "))
386 mh-new-messages-folders)))
387 (mh-index-sequenced-messages folders mh-unseen-seq))
390 (defun mh-index-ticked-messages (folders)
391 "Display ticked messages.
393 All messages in `mh-tick-seq' from the folders in
394 `mh-ticked-messages-folders' are listed.
396 With a prefix argument, enter a space-separated list of FOLDERS,
397 or nothing to search all folders."
399 (list (if current-prefix-arg
400 (split-string (read-string "Search folder(s) (default all): "))
401 mh-ticked-messages-folders)))
402 (mh-index-sequenced-messages folders mh-tick-seq))
407 (defvar mh-mairix-folder)
408 (defvar mh-flists-search-folders)))
411 (defun mh-index-sequenced-messages (folders sequence)
412 "Display messages in any sequence.
414 All messages from the FOLDERS in `mh-new-messages-folders' in the
415 SEQUENCE you provide are listed. With a prefix argument, enter a
416 space-separated list of folders at the prompt, or nothing to
419 (list (if current-prefix-arg
420 (split-string (read-string "Search folder(s) (default all): "))
421 mh-new-messages-folders)
422 (mh-read-seq-default "Search" nil)))
423 (unless sequence (setq sequence mh-unseen-seq))
424 (let* ((mh-flists-search-folders folders)
425 (mh-flists-sequence sequence)
426 (mh-flists-called-flag t)
427 (mh-searcher 'flists)
428 (mh-search-function 'mh-flists-execute)
429 (mh-search-next-result-function 'mh-mairix-next-result)
430 (mh-mairix-folder mh-user-path)
431 (mh-search-regexp-builder nil)
432 (new-folder (format "%s/%s/%s" mh-index-folder
433 mh-flists-results-folder sequence))
434 (window-config (if (equal new-folder mh-current-folder)
435 mh-previous-window-config
436 (current-window-configuration)))
439 (cond ((buffer-live-p (get-buffer new-folder))
440 ;; The destination folder is being visited. Trick `mh-search'
441 ;; into thinking that the folder resulted from a previous search.
442 (set-buffer new-folder)
443 (setq mh-index-previous-search (list folders mh-searcher sequence))
445 ((mh-folder-exists-p new-folder)
446 ;; Folder exists but we don't have it open. That means they are
447 ;; stale results from a old flists search. Clear it out.
448 (mh-exec-cmd-quiet nil "rmf" new-folder)))
449 (setq message (mh-search "+" mh-flists-results-folder
450 redo-flag window-config)
451 mh-index-sequence-search-flag t
452 mh-index-previous-search (list folders mh-searcher sequence))
453 (mh-index-write-data)
454 (when (stringp message) (message "%s" message))))
456 (defvar mh-flists-search-folders)
458 (defun mh-flists-execute (&rest args)
460 Search for messages belonging to `mh-flists-sequence' in the
461 folders specified by `mh-flists-search-folders'. If
462 `mh-recursive-folders-flag' is t, then the folders are searched
463 recursively. All parameters ARGS are ignored."
464 (set-buffer (get-buffer-create mh-temp-index-buffer))
466 (unless (executable-find "sh")
467 (error "Didn't find sh"))
469 (let ((seq (symbol-name mh-flists-sequence)))
470 (insert "for folder in `" (expand-file-name "flists" mh-progs) " "
471 (cond ((eq mh-flists-search-folders t)
472 (mh-quote-for-shell mh-inbox))
473 ((eq mh-flists-search-folders nil) "")
474 ((listp mh-flists-search-folders)
475 (loop for folder in mh-flists-search-folders
477 (concat " " (mh-quote-for-shell folder)))))
478 (if mh-recursive-folders-flag " -recurse" "")
479 " -sequence " seq " -noshowzero -fast` ; do\n"
480 (expand-file-name "mhpath" mh-progs) " \"+$folder\" " seq "\n"
483 (point-min) (point-max) "sh" nil (get-buffer mh-temp-index-buffer))))
488 (defun mh-index-next-folder (&optional backward-flag)
489 "Jump to the next folder marker.
491 With non-nil optional argument BACKWARD-FLAG, jump to the previous
494 (if (null mh-index-data)
495 (message "Only applicable in an MH-E index search buffer")
496 (let ((point (point)))
497 (forward-line (if backward-flag 0 1))
498 (cond ((if backward-flag
499 (re-search-backward "^+" (point-min) t)
500 (re-search-forward "^+" (point-max) t))
502 ((and (if backward-flag
503 (goto-char (point-max))
504 (goto-char (point-min)))
507 (re-search-backward "^+" (point-min) t)
508 (re-search-forward "^+" (point-max) t))
510 (t (goto-char point))))))
513 (defun mh-index-previous-folder ()
514 "Jump to the previous folder marker."
516 (mh-index-next-folder t))
519 (defun mh-index-visit-folder ()
520 "Visit original folder from where the message at point was found."
522 (unless mh-index-data
523 (error "Not in an index folder"))
526 (cond ((and (bolp) (eolp))
527 (ignore-errors (forward-line -1))
528 (setq msg (mh-get-msg-num t)))
529 ((equal (char-after (line-beginning-position)) ?+)
530 (setq folder (buffer-substring-no-properties
531 (line-beginning-position) (line-end-position))))
532 (t (setq msg (mh-get-msg-num t)))))
534 (setq folder (car (gethash (gethash msg mh-index-msg-checksum-map)
535 mh-index-checksum-origin-map))))
536 (when (or (not (get-buffer folder))
537 (y-or-n-p (format "Reuse buffer displaying %s? " folder)))
539 folder (loop for x being the hash-keys of (gethash folder mh-index-data)
540 when (mh-msg-exists-p x folder) collect x)))))
547 mh-pick-menu mh-search-mode-map "Menu for MH-E Search"
549 ["Perform Search" mh-index-do-search t]
550 ["Search with pick" mh-pick-do-search t]))
556 ;; If this changes, modify mh-search-mode-help-messages accordingly, below.
557 (gnus-define-keys mh-search-mode-map
559 "\C-c\C-c" mh-index-do-search
560 "\C-c\C-p" mh-pick-do-search
561 "\C-c\C-f\C-b" mh-to-field
562 "\C-c\C-f\C-c" mh-to-field
563 "\C-c\C-f\C-m" mh-to-field
564 "\C-c\C-f\C-s" mh-to-field
565 "\C-c\C-f\C-t" mh-to-field
566 "\C-c\C-fb" mh-to-field
567 "\C-c\C-fc" mh-to-field
568 "\C-c\C-fm" mh-to-field
569 "\C-c\C-fs" mh-to-field
570 "\C-c\C-ft" mh-to-field)
574 ;;; MH-Search Help Messages
576 ;; Group messages logically, more or less.
577 (defvar mh-search-mode-help-messages
579 "Perform search: \\[mh-index-do-search]\n"
580 "Search with pick: \\[mh-pick-do-search]\n\n"
581 "Move to a field by typing C-c C-f C-<field>\n"
582 "where <field> is the first letter of the desired field\n"
583 "(except for From: which uses \"m\")."))
584 "Key binding cheat sheet.
586 This is an associative array which is used to show the most common
587 commands. The key is a prefix char. The value is one or more strings
588 which are concatenated together and displayed in the minibuffer if ?
589 is pressed after the prefix character. The special key nil is used to
590 display the non-prefixed commands.
592 The substitutions described in `substitute-command-keys' are performed
599 (put 'mh-search-mode 'mode-class 'special)
601 (define-derived-mode mh-search-mode fundamental-mode "MH-Search"
602 "Mode for creating search templates in MH-E.\\<mh-search-mode-map>
604 Edit this template by entering your search criteria in an
605 appropriate header field that is already there, or create a new
606 field yourself. If the string you're looking for could be
607 anywhere in a message, then place the string underneath the row
610 To perform the search, type \\[mh-index-do-search].
612 Sometimes you're searching for text that is either not indexed,
613 or hasn't been indexed yet. In this case you can override the
614 default method with the pick method by running the command
615 \\[mh-pick-do-search].
617 The hook `mh-search-mode-hook' is called upon entry to this mode.
619 \\{mh-search-mode-map}"
621 (easy-menu-add mh-pick-menu)
622 (mh-set-help mh-search-mode-help-messages))
626 ;;; MH-Search Commands
628 (defun mh-index-do-search (&optional searcher)
629 "Find messages using `mh-search-program'.
630 If optional argument SEARCHER is present, use it instead of
631 `mh-search-program'."
633 (unless (mh-search-choose searcher) (error "No search program found"))
634 (let* ((regexp-list (mh-pick-parse-search-buffer))
635 (pattern (funcall mh-search-regexp-builder regexp-list)))
637 (mh-search mh-current-folder pattern nil mh-previous-window-config)
638 (error "No search terms"))))
640 (defun mh-pick-do-search ()
641 "Find messages using \"pick\".
643 Uses the pick method described in `mh-pick-execute-search'."
645 (mh-index-do-search 'pick))
647 (defun mh-pick-parse-search-buffer ()
648 "Parse the search buffer contents.
649 The function returns a alist. The car of each element is either
650 the header name to search in or nil to search the whole message.
651 The cdr of the element is the pattern to search."
653 (let ((pattern-list ())
656 (goto-char (point-min))
658 (if (search-forward "--------" (line-end-position) t)
659 (setq in-body-flag t)
662 (setq start (if in-body-flag
664 (search-forward ":" (line-end-position) t)
666 (push (cons (and (not in-body-flag)
668 (buffer-substring-no-properties
670 (mh-index-parse-search-regexp
671 (buffer-substring-no-properties
672 start (line-end-position))))
677 (defun mh-index-parse-search-regexp (input-string)
678 "Construct parse tree for INPUT-STRING.
679 All occurrences of &, |, ! and ~ in INPUT-STRING are replaced by
680 AND, OR and NOT as appropriate. Then the resulting string is
684 (insert input-string)
686 (mh-replace-string "\t" " ")
688 (mh-replace-string " AND " " and ")
689 (mh-replace-string "&" " and ")
690 (mh-replace-string " -and " " and ")
692 (mh-replace-string " OR " " or ")
693 (mh-replace-string "|" " or ")
694 (mh-replace-string " -or " " or ")
696 (mh-replace-string " NOT " " not ")
697 (mh-replace-string "!" " not ")
698 (mh-replace-string "~" " not ")
699 (mh-replace-string " -not " " not ")
700 ;; synonyms of left brace
701 (mh-replace-string "(" " ( ")
702 (mh-replace-string " -lbrace " " ( ")
703 ;; synonyms of right brace
704 (mh-replace-string ")" " ) ")
705 (mh-replace-string " -rbrace " " ) ")
706 ;; get the normalized input
707 (setq input (format "( %s )" (buffer-substring (point-min) (point-max)))))
709 (let ((tokens (mh-index-add-implicit-ops (split-string input)))
713 (dolist (token tokens)
714 (cond ((equal token "(") (push 'paren op-stack))
715 ((equal token "not") (push 'not op-stack))
716 ((equal token "or") (push 'or op-stack))
717 ((equal token "and") (push 'and op-stack))
719 (multiple-value-setq (op-stack operand-stack)
720 (mh-index-evaluate op-stack operand-stack))
721 (when (eq (car op-stack) 'not)
722 (setq op-stack (cdr op-stack))
723 (push `(not ,(pop operand-stack)) operand-stack))
724 (when (eq (car op-stack) 'and)
725 (setq op-stack (cdr op-stack))
726 (setq oper1 (pop operand-stack))
727 (push `(and ,(pop operand-stack) ,oper1) operand-stack)))
728 ((eq (car op-stack) 'not)
729 (setq op-stack (cdr op-stack))
730 (push `(not ,token) operand-stack)
731 (when (eq (car op-stack) 'and)
732 (setq op-stack (cdr op-stack))
733 (setq oper1 (pop operand-stack))
734 (push `(and ,(pop operand-stack) ,oper1) operand-stack)))
735 ((eq (car op-stack) 'and)
736 (setq op-stack (cdr op-stack))
737 (push `(and ,(pop operand-stack) ,token) operand-stack))
738 (t (push token operand-stack))))
739 (prog1 (pop operand-stack)
740 (when (or op-stack operand-stack)
741 (error "Invalid regexp: %s" input))))))
743 (defun mh-index-add-implicit-ops (tokens)
744 "Add implicit operators in the list TOKENS."
749 (setq current (pop tokens))
750 (cond ((or (equal current ")") (equal current "and") (equal current "or"))
751 (setq literal-seen nil)
752 (push current result))
755 (setq literal-seen nil)
758 (push current result)
759 (unless (or (equal current "(") (equal current "not"))
760 (setq literal-seen t)))))
763 (defun mh-index-evaluate (op-stack operand-stack)
764 "Read expression till starting paren based on OP-STACK and OPERAND-STACK."
765 (block mh-index-evaluate
768 (setq op (pop op-stack))
769 (cond ((eq op 'paren)
770 (return-from mh-index-evaluate (values op-stack operand-stack)))
772 (push `(not ,(pop operand-stack)) operand-stack))
773 ((or (eq op 'and) (eq op 'or))
774 (setq oper1 (pop operand-stack))
775 (push `(,op ,(pop operand-stack) ,oper1) operand-stack))))
776 (error "Ran out of tokens"))))
780 ;;; Indexing Functions
782 ;; Support different search programs
783 (defvar mh-search-choices
785 mh-swish++-binary mh-swish++-execute-search mh-swish++-next-result
786 mh-swish++-regexp-builder)
788 mh-swish-binary mh-swish-execute-search mh-swish-next-result nil)
790 mh-mairix-binary mh-mairix-execute-search mh-mairix-next-result
791 mh-mairix-regexp-builder)
793 mh-namazu-binary mh-namazu-execute-search mh-namazu-next-result nil)
795 mh-pick-binary mh-pick-execute-search mh-pick-next-result
796 mh-pick-regexp-builder)
798 mh-grep-binary mh-grep-execute-search mh-grep-next-result nil))
799 "List of possible searcher choices.")
801 (defun mh-search-choose (&optional searcher)
802 "Choose a searching function.
803 The side-effects of this function are that the variables
804 `mh-searcher', `mh-search-function', and
805 `mh-search-next-result-function' are set according to the first
806 searcher in `mh-search-choices' present on the system. If
807 optional argument SEARCHER is present, use it instead of
808 `mh-search-program'."
810 (let ((program-alist (cond (searcher
811 (list (assoc searcher mh-search-choices)))
814 (assoc mh-search-program mh-search-choices)))
815 (t mh-search-choices))))
817 (let* ((current (pop program-alist))
818 (executable (symbol-value (cadr current))))
820 (setq mh-searcher (car current))
821 (setq mh-search-function (nth 2 current))
822 (setq mh-search-next-result-function (nth 3 current))
823 (setq mh-search-regexp-builder (nth 4 current))
824 (return mh-searcher))))
829 (defvar mh-swish++-binary (or (executable-find "search++")
830 (executable-find "search")))
831 (defvar mh-swish++-directory ".swish++")
832 (defvar mh-swish-folder nil)
834 (defun mh-swish++-execute-search (folder-path search-regexp)
837 In the examples below, replace \"/home/user/Mail\" with the path to
840 First create the directory \"/home/user/Mail/.swish++\". Then create
841 the file \"/home/user/Mail/.swish++/swish++.conf\" with the following
844 IncludeMeta Bcc Cc Comments Content-Description From Keywords
845 IncludeMeta Newsgroups Resent-To Subject To
846 IncludeMeta Message-Id References In-Reply-To
848 IndexFile /home/user/Mail/.swish++/swish++.index
850 Use the following command line to generate the swish index. Run
851 this daily from cron:
853 find /home/user/Mail -path /home/user/Mail/mhe-index -prune \\
854 -o -path /home/user/Mail/.swish++ -prune \\
855 -o -name \"[0-9]*\" -print \\
856 | index -c /home/user/Mail/.swish++/swish++.conf -
858 This command does not index the folders that hold the results of your
859 searches in \"+mhe-index\" since they tend to be ephemeral and the
860 original messages are indexed anyway.
862 On some systems (Debian GNU/Linux, for example), use \"index++\"
863 instead of \"index\".
865 In a program, FOLDER-PATH is the directory in which SEARCH-REGEXP is
867 (set-buffer (get-buffer-create mh-temp-index-buffer))
869 (unless mh-swish++-binary
870 (error "Set `mh-swish++-binary' appropriately"))
871 (call-process mh-swish++-binary nil '(t nil) nil
873 (format "-i%s%s/swish++.index"
874 mh-user-path mh-swish++-directory)
876 (goto-char (point-min))
877 (setq mh-swish-folder
878 (let ((last-char (substring folder-path (1- (length folder-path)))))
879 (if (equal last-char "/")
881 (format "%s/" folder-path)))))
883 (defalias 'mh-swish++-next-result 'mh-swish-next-result)
885 (defun mh-swish++-regexp-builder (regexp-list)
886 "Generate query for swish++.
887 REGEXP-LIST is an alist of fields and values."
889 (dolist (elem regexp-list)
891 (setq regexp (concat regexp " and "
892 (if (car elem) "(" "")
893 (if (car elem) (symbol-name (car elem)) "")
894 (if (car elem) " = " "")
895 (mh-swish++-print-regexp (cdr elem))
896 (if (car elem) ")" "")))))
897 (substring regexp 4)))
899 (defun mh-swish++-print-regexp (expr)
900 "Return infix expression corresponding to EXPR."
901 (cond ((atom expr) (format "%s" expr))
902 ((eq (car expr) 'not)
903 (format "(not %s)" (mh-swish++-print-regexp (cadr expr))))
904 (t (format "(%s %s %s)" (mh-swish++-print-regexp (cadr expr))
905 (symbol-name (car expr))
906 (mh-swish++-print-regexp (caddr expr))))))
910 (defvar mh-swish-binary (executable-find "swish-e"))
911 (defvar mh-swish-directory ".swish")
913 (defun mh-swish-execute-search (folder-path search-regexp)
916 In the examples below, replace \"/home/user/Mail\" with the path
917 to your MH directory.
919 First create the directory \"/home/user/Mail/.swish\". Then
920 create the file \"/home/user/Mail/.swish/config\" with the
924 IndexDir /home/user/Mail
925 IndexFile /home/user/Mail/.swish/index
926 IndexName \"Mail Index\"
927 IndexDescription \"Mail Index\"
928 IndexPointer \"http://nowhere\"
929 IndexAdmin \"nobody\"
934 IgnoreTotalWordCountWhenRanking yes
935 WordCharacters abcdefghijklmnopqrstuvwxyz0123456789-
936 BeginCharacters abcdefghijklmnopqrstuvwxyz
937 EndCharacters abcdefghijklmnopqrstuvwxyz0123456789
940 FileRules filename contains \\D
941 FileRules pathname contains /home/user/Mail/.swish
942 FileRules pathname contains /home/user/Mail/mhe-index
944 This configuration does not index the folders that hold the
945 results of your searches in \"+mhe-index\" since they tend to be
946 ephemeral and the original messages are indexed anyway.
948 If there are any directories you would like to ignore, append
949 lines like the following to \"config\":
951 FileRules pathname contains /home/user/Mail/scripts
953 Use the following command line to generate the swish index. Run
954 this daily from cron:
956 swish-e -c /home/user/Mail/.swish/config
958 In a program, FOLDER-PATH is the directory in which SEARCH-REGEXP
960 (set-buffer (get-buffer-create mh-temp-index-buffer))
962 (unless mh-swish-binary
963 (error "Set `mh-swish-binary' appropriately"))
964 (call-process mh-swish-binary nil '(t nil) nil
966 "-f" (format "%s%s/index" mh-user-path mh-swish-directory))
967 (goto-char (point-min))
968 (setq mh-swish-folder
969 (let ((last-char (substring folder-path (1- (length folder-path)))))
970 (if (equal last-char "/")
972 (format "%s/" folder-path)))))
974 (defun mh-swish-next-result ()
975 "Get the next result from swish output."
978 (when (or (eobp) (equal (char-after (point)) ?.))
980 (when (equal (char-after (point)) ?#)
982 (let* ((start (search-forward " " (line-end-position) t))
983 (end (search-forward " " (line-end-position) t)))
984 (unless (and start end)
987 (unless (file-exists-p (buffer-substring-no-properties start end))
989 (unless (search-backward "/" start t)
991 (list (let* ((s (buffer-substring-no-properties start (1+ (point)))))
992 (unless (string-match mh-swish-folder s)
994 (if (and (string-match mh-user-path s)
995 (< (match-end 0) (1- (length s))))
997 (substring s (match-end 0) (1- (length s))))
999 (let* ((s (buffer-substring-no-properties (1+ (point)) end))
1000 (n (ignore-errors (string-to-number s))))
1001 (if n n (return 'error)))
1007 (defvar mh-mairix-binary (executable-find "mairix"))
1008 (defvar mh-mairix-directory ".mairix")
1009 (defvar mh-mairix-folder nil)
1011 (defun mh-mairix-execute-search (folder-path search-regexp-list)
1014 In the examples below, replace \"/home/user/Mail\" with the path
1015 to your MH directory.
1017 First create the directory \"/home/user/Mail/.mairix\". Then
1018 create the file \"/home/user/Mail/.mairix/config\" with the
1021 base=/home/user/Mail
1023 # List of folders that should be indexed. 3 dots at the end means there
1024 # are subfolders within the folder
1025 mh=archive...:inbox:drafts:news:sent:trash
1028 database=/home/user/Mail/mairix/database
1030 Use the following command line to generate the mairix index. Run
1031 this daily from cron:
1033 mairix -f /home/user/Mail/.mairix/config
1035 In a program, FOLDER-PATH is the directory in which
1036 SEARCH-REGEXP-LIST is used to search."
1037 (set-buffer (get-buffer-create mh-temp-index-buffer))
1039 (unless mh-mairix-binary
1040 (error "Set `mh-mairix-binary' appropriately"))
1041 (apply #'call-process mh-mairix-binary nil '(t nil) nil
1042 "-r" "-f" (format "%s%s/config" mh-user-path mh-mairix-directory)
1044 (goto-char (point-min))
1045 (setq mh-mairix-folder
1046 (let ((last-char (substring folder-path (1- (length folder-path)))))
1047 (if (equal last-char "/")
1049 (format "%s/" folder-path)))))
1051 (defun mh-mairix-next-result ()
1052 "Return next result from mairix output."
1055 (when (or (eobp) (and (bolp) (eolp)))
1057 (unless (eq (char-after) ?/)
1059 (let ((start (point))
1061 (setq end (line-end-position))
1062 (unless (search-forward mh-mairix-folder end t)
1064 (goto-char (match-beginning 0))
1065 (unless (equal (point) start)
1068 (unless (search-backward "/" start t)
1070 (setq msg-start (1+ (point)))
1072 (unless (search-forward mh-user-path end t)
1074 (list (format "+%s" (buffer-substring-no-properties
1075 (point) (1- msg-start)))
1077 (buffer-substring-no-properties msg-start end))
1081 (defun mh-mairix-regexp-builder (regexp-list)
1082 "Generate query for mairix.
1083 REGEXP-LIST is an alist of fields and values."
1085 (dolist (pair regexp-list)
1089 (cond ((eq (car pair) 'to) "t:")
1090 ((eq (car pair) 'from) "f:")
1091 ((eq (car pair) 'cc) "c:")
1092 ((eq (car pair) 'subject) "s:")
1093 ((eq (car pair) 'date) "d:")
1095 (let ((sop (cdr (mh-mairix-convert-to-sop* (cdr pair))))
1097 (dolist (conjunct sop)
1098 (let ((expr-list (cdr conjunct))
1100 (dolist (e expr-list)
1101 (setq expr-string (concat expr-string ","
1102 (if (atom e) "" "~")
1103 (if (atom e) e (cadr e)))))
1104 (setq final (concat final "/" (substring expr-string 1)))))
1105 (substring final 1)))
1109 (defun mh-mairix-convert-to-sop* (expr)
1110 "Convert EXPR to sum of product form."
1111 (cond ((atom expr) `(or (and ,expr)))
1112 ((eq (car expr) 'or)
1114 (loop for e in (mapcar #'mh-mairix-convert-to-sop* (cdr expr))
1116 ((eq (car expr) 'and)
1117 (let ((conjuncts (mapcar #'mh-mairix-convert-to-sop* (cdr expr)))
1119 (setq result (pop conjuncts))
1121 (setq next-factor (pop conjuncts))
1122 (setq result (let ((res ()))
1123 (dolist (t1 (cdr result))
1124 (dolist (t2 (cdr next-factor))
1125 (push `(and ,@(cdr t1) ,@(cdr t2)) res)))
1128 ((atom (cadr expr)) `(or (and ,expr)))
1129 ((eq (caadr expr) 'not) (mh-mairix-convert-to-sop* (cadadr expr)))
1130 ((eq (caadr expr) 'and) (mh-mairix-convert-to-sop*
1131 `(or ,@(mapcar #'(lambda (x) `(not ,x))
1133 ((eq (caadr expr) 'or) (mh-mairix-convert-to-sop*
1134 `(and ,@(mapcar #'(lambda (x) `(not ,x))
1136 (t (error "Unreachable: %s" expr))))
1140 (defvar mh-namazu-binary (executable-find "namazu"))
1141 (defvar mh-namazu-directory ".namazu")
1142 (defvar mh-namazu-folder nil)
1144 (defun mh-namazu-execute-search (folder-path search-regexp)
1147 In the examples below, replace \"/home/user/Mail\" with the path to
1150 First create the directory \"/home/user/Mail/.namazu\". Then create
1151 the file \"/home/user/Mail/.namazu/mknmzrc\" with the following
1154 package conf; # Don't remove this line!
1155 $ADDRESS = 'user@localhost';
1156 $ALLOW_FILE = \"[0-9]*\";
1157 $EXCLUDE_PATH = \"^/home/user/Mail/(mhe-index|spam)\";
1159 This configuration does not index the folders that hold the results of
1160 your searches in \"+mhe-index\" since they tend to be ephemeral and
1161 the original messages are indexed anyway.
1163 Use the following command line to generate the namazu index. Run this
1166 mknmz -f /home/user/Mail/.namazu/mknmzrc -O /home/user/Mail/.namazu \\
1169 In a program, FOLDER-PATH is the directory in which SEARCH-REGEXP
1171 (let ((namazu-index-directory
1172 (format "%s%s" mh-user-path mh-namazu-directory)))
1173 (unless (file-exists-p namazu-index-directory)
1174 (error "Namazu directory %s not present" namazu-index-directory))
1175 (unless (executable-find mh-namazu-binary)
1176 (error "Set `mh-namazu-binary' appropriately"))
1177 (set-buffer (get-buffer-create mh-temp-index-buffer))
1179 (call-process mh-namazu-binary nil '(t nil) nil
1180 "-alR" search-regexp namazu-index-directory)
1181 (goto-char (point-min))
1182 (setq mh-namazu-folder
1183 (let ((last (substring folder-path (1- (length folder-path)))))
1184 (if (equal last "/")
1186 (format "%s/" folder-path))))))
1188 (defun mh-namazu-next-result ()
1189 "Get the next result from namazu output."
1192 (when (eobp) (return nil))
1193 (let ((file-name (buffer-substring-no-properties
1194 (point) (line-end-position))))
1195 (unless (equal (string-match mh-namazu-folder file-name) 0)
1197 (unless (file-exists-p file-name)
1199 (string-match mh-user-path file-name)
1200 (let* ((folder/msg (substring file-name (match-end 0)))
1201 (mark (mh-search-from-end ?/ folder/msg)))
1202 (unless mark (return 'error))
1203 (list (format "+%s" (substring folder/msg 0 mark))
1204 (let ((n (ignore-errors (string-to-number
1205 (substring folder/msg (1+ mark))))))
1206 (if n n (return 'error)))
1212 (defvar mh-index-pick-folder)
1213 (defvar mh-pick-binary "pick")
1214 (defconst mh-pick-single-dash '(cc date from subject to)
1215 "Search components that are supported by single-dash option in pick.")
1217 (defun mh-pick-execute-search (folder-path search-regexp)
1220 Read \"pick(1)\" or the section Finding Messages with pick in the
1221 MH book to find out more about how to enter the criteria (see URL
1222 `http://www.ics.uci.edu/~mh/book/mh/finpic.htm').
1224 In a program, FOLDER-PATH is the directory in which SEARCH-REGEXP
1226 (set-buffer (get-buffer-create mh-temp-index-buffer))
1229 (mh-folder-list (substring folder-path (length mh-user-path)))))
1230 (loop for folder in folders do
1231 (setq folder (concat "+" folder))
1232 (insert folder "\n")
1233 (apply #'call-process (expand-file-name "pick" mh-progs)
1234 nil '(t nil) nil folder "-list" search-regexp)))
1235 (goto-char (point-min)))
1237 (defun mh-pick-next-result ()
1238 "Return the next pick search result."
1241 (when (eobp) (return nil))
1242 (when (search-forward-regexp "^\+" (line-end-position) t)
1243 (setq mh-index-pick-folder
1244 (buffer-substring-no-properties (line-beginning-position)
1245 (line-end-position)))
1247 (unless (search-forward-regexp "^[1-9][0-9]*$" (line-end-position) t)
1249 (list mh-index-pick-folder
1251 (buffer-substring-no-properties (line-beginning-position)
1252 (line-end-position)))
1256 ;; All implementations of pick have special options -cc, -date, -from and
1257 ;; -subject that allow to search for corresponding components. Any other
1258 ;; component is searched using option --COMPNAME, for example: `pick
1259 ;; --x-mailer mh-e'. Mailutils "pick" supports this option using a certain
1260 ;; kludge, but it prefers the following syntax for this purpose:
1261 ;; "--component=COMPNAME --pattern=PATTERN".
1262 ;; -- Sergey Poznyakoff, Aug 2003
1263 (defun mh-pick-regexp-builder (pattern-list)
1264 "Generate pick search expression from PATTERN-LIST."
1266 (dolist (pattern pattern-list)
1268 (setq result `(,@result "-and" "-lbrace"
1269 ,@(mh-pick-construct-regexp
1270 (if (and (mh-variant-p 'mu-mh) (car pattern))
1271 (format "--pattern=%s" (cdr pattern))
1275 ((mh-variant-p 'mu-mh)
1276 (format "--component=%s" (car pattern)))
1277 ((member (car pattern) mh-pick-single-dash)
1278 (format "-%s" (car pattern)))
1280 (format "--%s" (car pattern))))
1285 (defun mh-pick-construct-regexp (expr component)
1286 "Construct pick compatible expression corresponding to EXPR.
1287 COMPONENT is the component to search."
1288 (cond ((atom expr) (list component expr))
1289 ((eq (car expr) 'and)
1290 `("-lbrace" ,@(mh-pick-construct-regexp (cadr expr) component) "-and"
1291 ,@(mh-pick-construct-regexp (caddr expr) component) "-rbrace"))
1292 ((eq (car expr) 'or)
1293 `("-lbrace" ,@(mh-pick-construct-regexp (cadr expr) component) "-or"
1294 ,@(mh-pick-construct-regexp (caddr expr) component) "-rbrace"))
1295 ((eq (car expr) 'not)
1296 `("-lbrace" "-not" ,@(mh-pick-construct-regexp (cadr expr) component)
1298 (t (error "Unknown operator %s seen" (car expr)))))
1302 (defvar mh-grep-binary (executable-find "grep"))
1304 (defun mh-grep-execute-search (folder-path search-regexp)
1307 Unlike the other search methods, this method does not use the
1308 MH-Search buffer. Instead, you simply enter a regular expression
1309 in the minibuffer. For help in constructing regular expressions,
1310 see your man page for \"grep\".
1312 In a program, FOLDER-PATH is the directory in which SEARCH-REGEXP
1314 (set-buffer (get-buffer-create mh-temp-index-buffer))
1316 (call-process mh-grep-binary nil '(t nil) nil
1317 "-i" "-r" search-regexp folder-path)
1318 (goto-char (point-min)))
1320 (defun mh-grep-next-result ()
1321 "Read the next result.
1322 Parse it and return the message folder, message index and the
1323 match. If no other matches left then return nil. If the current
1324 record is invalid return 'error."
1329 (let ((eol-pos (line-end-position))
1330 (bol-pos (line-beginning-position))
1331 folder-start msg-end)
1333 (unless (search-forward mh-user-path eol-pos t)
1335 (setq folder-start (point))
1336 (unless (search-forward ":" eol-pos t)
1338 (let ((match (buffer-substring-no-properties (point) eol-pos)))
1340 (setq msg-end (point))
1341 (unless (search-backward "/" folder-start t)
1343 (list (format "+%s" (buffer-substring-no-properties
1344 folder-start (point)))
1345 (let ((n (ignore-errors (string-to-number
1346 (buffer-substring-no-properties
1347 (1+ (point)) msg-end)))))
1348 (if n n (return 'error)))
1354 ;;; Folder Utilities
1357 (defun mh-index-group-by-folder ()
1358 "Partition the messages based on source folder.
1359 Returns an alist with the the folder names in the car and the cdr
1360 being the list of messages originally from that folder."
1362 (goto-char (point-min))
1363 (let ((result-table (make-hash-table :test #'equal)))
1364 (loop for msg being hash-keys of mh-index-msg-checksum-map
1365 do (push msg (gethash (car (gethash
1366 (gethash msg mh-index-msg-checksum-map)
1367 mh-index-checksum-origin-map))
1369 (loop for x being the hash-keys of result-table
1370 collect (cons x (nreverse (gethash x result-table)))))))
1373 (defun mh-index-insert-folder-headers ()
1374 "Annotate the search results with original folder names."
1375 (let ((cur-msg (mh-get-msg-num nil))
1376 (old-buffer-modified-flag (buffer-modified-p))
1377 (buffer-read-only nil)
1378 current-folder last-folder)
1379 (goto-char (point-min))
1381 (setq current-folder (car (gethash (gethash (mh-get-msg-num nil)
1382 mh-index-msg-checksum-map)
1383 mh-index-checksum-origin-map)))
1384 (when (and current-folder (not (equal current-folder last-folder)))
1385 (insert (if last-folder "\n" "") current-folder "\n")
1386 (setq last-folder current-folder))
1390 (mh-goto-msg cur-msg t))
1391 (set-buffer-modified-p old-buffer-modified-flag))
1392 (mh-index-create-imenu-index))
1395 (defun mh-index-delete-folder-headers ()
1396 "Delete the folder headers."
1397 (let ((cur-msg (mh-get-msg-num nil))
1398 (old-buffer-modified-flag (buffer-modified-p))
1399 (buffer-read-only nil))
1400 (while (and (not cur-msg) (not (eobp)))
1402 (setq cur-msg (mh-get-msg-num nil)))
1403 (goto-char (point-min))
1405 (if (or (char-equal (char-after) ?+) (char-equal (char-after) 10))
1406 (delete-region (point) (progn (forward-line) (point)))
1408 (when cur-msg (mh-goto-msg cur-msg t t))
1409 (set-buffer-modified-p old-buffer-modified-flag)))
1412 (eval-when-compile (mh-do-in-xemacs (defvar which-func-mode)))
1415 (defun mh-index-create-imenu-index ()
1416 "Create alist of folder names and positions in index folder buffers."
1418 (if (boundp 'which-func-mode)
1419 (setq which-func-mode t))
1421 (goto-char (point-min))
1422 (while (re-search-forward "^+" nil t)
1425 (push (cons (buffer-substring-no-properties
1426 (point) (line-end-position))
1427 (set-marker (make-marker) (point)))
1429 (setq imenu--index-alist (nreverse alist)))))
1432 (defun mh-search-p ()
1433 "Non-nil means that this folder was generated by searching."
1437 (eval-when-compile (if mh-xemacs-flag (defvar mh-speed-flists-inhibit-flag)))
1440 (defun mh-index-execute-commands ()
1441 "Delete/refile the actual messages.
1442 The copies in the searched folder are then deleted/refiled to get
1443 the desired result. Before deleting the messages we make sure
1444 that the message being deleted is identical to the one that the
1445 user has marked in the index buffer."
1448 (mh-speed-flists-inhibit-flag t))
1450 (lambda (folder msgs)
1451 (push folder folders)
1452 (if (not (get-buffer folder))
1453 ;; If source folder not open, just delete the messages...
1454 (apply #'mh-exec-cmd "rmm" folder (mh-coalesce-msg-list msgs))
1455 ;; Otherwise delete the messages in the source buffer...
1458 (let ((old-refile-list mh-refile-list)
1459 (old-delete-list mh-delete-list))
1460 (setq mh-refile-list nil
1461 mh-delete-list msgs)
1462 (unwind-protect (mh-execute-commands)
1463 (setq mh-refile-list
1466 (loop for y in (cdr x)
1467 unless (memq y msgs) collect y)))
1470 (loop for x in old-delete-list
1471 unless (memq x msgs) collect x))
1472 (mh-set-folder-modified-p (mh-outstanding-commands-p))
1473 (when (mh-outstanding-commands-p)
1474 (mh-notate-deleted-and-refiled)))))))
1475 (mh-index-matching-source-msgs (append (loop for x in mh-refile-list
1481 (defun mh-index-generate-pretty-name (string)
1482 "Given STRING generate a name which is suitable for use as a folder name.
1483 White space from the beginning and end are removed. All spaces in
1484 the name are replaced with underscores and all / are replaced
1485 with $. If STRING is longer than 20 it is truncated too. STRING
1486 could be a list of strings in which case they are concatenated to
1487 construct the base name."
1489 (if (stringp string)
1491 (when (car string) (insert (car string)))
1492 (dolist (s (cdr string))
1494 (setq string (mh-replace-string "-lbrace" " "))
1495 (setq string (mh-replace-string "-rbrace" " "))
1496 (setq string (mh-replace-string "-search" " "))
1497 (subst-char-in-region (point-min) (point-max) ?( ? t)
1498 (subst-char-in-region (point-min) (point-max) ?) ? t)
1499 (subst-char-in-region (point-min) (point-max) ?- ? t)
1500 (goto-char (point-min))
1501 (while (and (not (eobp)) (memq (char-after) '(? ?\t ?\n ?\r ?_)))
1503 (goto-char (point-max))
1504 (while (and (not (bobp)) (memq (char-before) '(? ?\t ?\n ?\r ?_)))
1505 (delete-backward-char 1))
1506 (subst-char-in-region (point-min) (point-max) ? ?_ t)
1507 (subst-char-in-region (point-min) (point-max) ?\t ?_ t)
1508 (subst-char-in-region (point-min) (point-max) ?\n ?_ t)
1509 (subst-char-in-region (point-min) (point-max) ?\r ?_ t)
1510 (subst-char-in-region (point-min) (point-max) ?/ ?$ t)
1511 (let ((out (truncate-string-to-width (buffer-string) 20)))
1512 (cond ((eq mh-searcher 'flists)
1513 (format "%s/%s" mh-flists-results-folder mh-flists-sequence))
1514 ((equal out mh-flists-results-folder) (concat out "1"))
1517 (defun mh-folder-exists-p (folder)
1518 "Check if FOLDER exists."
1519 (and (mh-folder-name-p folder)
1522 (mh-exec-cmd-output "folder" nil "-fast" "-nocreate" folder)
1523 (goto-char (point-min))
1526 (defun mh-msg-exists-p (msg folder)
1527 "Check if MSG exists in FOLDER."
1528 (file-exists-p (format "%s%s/%s" mh-user-path (substring folder 1) msg)))
1530 (defun mh-index-new-folder (name search-regexp)
1531 "Return a folder name based on NAME for search results of SEARCH-REGEXP.
1533 If folder NAME already exists and was generated for the same
1534 SEARCH-REGEXP then it is reused.
1536 Otherwise if the folder NAME was generated from a different
1537 search then check if NAME<2> can be used. Otherwise try NAME<3>.
1538 This is repeated till we find a new folder name.
1540 If the folder returned doesn't exist then it is created."
1541 (unless (mh-folder-name-p name)
1542 (error "The argument should be a valid MH folder name"))
1545 for candidate = (if (equal i 1) name (format "%s<%s>" name i))
1546 when (or (not (mh-folder-exists-p candidate))
1547 (equal (mh-index-folder-search-regexp candidate)
1550 ;; Do pending refiles/deletes...
1551 (when (get-buffer chosen-name)
1552 (mh-process-or-undo-commands chosen-name))
1553 ;; Recreate folder...
1554 (save-excursion (mh-exec-cmd-quiet nil "rmf" chosen-name))
1555 (mh-exec-cmd-quiet nil "folder" "-create" "-fast" chosen-name)
1556 (mh-remove-from-sub-folders-cache chosen-name)
1557 (when (boundp 'mh-speed-folder-map)
1558 (mh-speed-add-folder chosen-name))
1561 (defun mh-index-folder-search-regexp (folder)
1562 "If FOLDER was created by a index search, return the search regexp.
1563 Return nil if FOLDER doesn't exist or the .mhe_index file is
1567 (insert-file-contents
1568 (format "%s%s/%s" mh-user-path (substring folder 1) mh-index-data-file))
1569 (goto-char (point-min))
1571 (cadr (read (current-buffer))))))
1575 ;;; Sequence Support
1578 (defun mh-index-create-sequences ()
1579 "Mirror sequences present in source folders in index folder."
1580 (let ((seq-hash (make-hash-table :test #'equal))
1582 (loop for folder being the hash-keys of mh-index-data
1583 do (setf (gethash folder seq-hash)
1584 (mh-create-sequence-map
1585 (mh-read-folder-sequences folder nil))))
1586 (dolist (msg (mh-translate-range mh-current-folder "all"))
1587 (let* ((checksum (gethash msg mh-index-msg-checksum-map))
1588 (pair (gethash checksum mh-index-checksum-origin-map))
1589 (ofolder (car pair))
1591 (loop for seq in (ignore-errors
1592 (gethash omsg (gethash ofolder seq-hash)))
1593 do (if (assoc seq seq-list)
1594 (push msg (cdr (assoc seq seq-list)))
1595 (push (list seq msg) seq-list)))))
1596 (loop for seq in seq-list
1597 do (apply #'mh-exec-cmd "mark" mh-current-folder
1598 "-sequence" (symbol-name (car seq)) "-add"
1599 (mapcar #'(lambda (x) (format "%s" x)) (cdr seq))))))
1602 (defun mh-create-sequence-map (seq-list)
1603 "Return a map from msg number to list of sequences in which it is present.
1604 SEQ-LIST is an assoc list whose keys are sequence names and whose
1605 cdr is the list of messages in that sequence."
1606 (loop with map = (make-hash-table)
1608 when (and (not (memq (car seq) (mh-unpropagated-sequences)))
1609 (mh-valid-seq-p (car seq)))
1610 do (loop for msg in (cdr seq)
1611 do (push (car seq) (gethash msg map)))
1612 finally return map))
1615 (defun mh-index-add-to-sequence (seq msgs)
1616 "Add to SEQ the messages in the list MSGS.
1617 This function updates the source folder sequences. Also makes an
1618 attempt to update the source folder buffer if we have it open."
1619 ;; Don't need to do anything for cur
1621 (when (and (not (memq seq (mh-unpropagated-sequences)))
1622 (mh-valid-seq-p seq))
1624 (mh-speed-flists-inhibit-flag t))
1625 (maphash (lambda (folder msgs)
1626 (push folder folders)
1627 ;; Add messages to sequence in source folder...
1628 (apply #'mh-exec-cmd-quiet nil "mark" folder
1629 "-add" "-nozero" "-sequence" (symbol-name seq)
1630 (mapcar (lambda (x) (format "%s" x))
1631 (mh-coalesce-msg-list msgs)))
1632 ;; Update source folder buffer if we have it open...
1633 (when (get-buffer folder)
1636 (mh-put-msg-in-seq msgs seq))))
1637 (mh-index-matching-source-msgs msgs))
1641 (defun mh-index-delete-from-sequence (seq msgs)
1642 "Delete from SEQ the messages in MSGS.
1643 This function updates the source folder sequences. Also makes an
1644 attempt to update the source folder buffer if present."
1646 (when (and (not (memq seq (mh-unpropagated-sequences)))
1647 (mh-valid-seq-p seq))
1649 (mh-speed-flists-inhibit-flag t))
1650 (maphash (lambda (folder msgs)
1651 (push folder folders)
1652 ;; Remove messages from sequence in source folder...
1653 (apply #'mh-exec-cmd-quiet nil "mark" folder
1654 "-del" "-nozero" "-sequence" (symbol-name seq)
1655 (mapcar (lambda (x) (format "%s" x))
1656 (mh-coalesce-msg-list msgs)))
1657 ;; Update source folder buffer if we have it open...
1658 (when (get-buffer folder)
1661 (mh-delete-msg-from-seq msgs seq t))))
1662 (mh-index-matching-source-msgs msgs))
1665 (defvar mh-unpropagated-sequences '(cur range subject search)
1666 "List of sequences that aren't preserved.")
1668 (defun mh-unpropagated-sequences ()
1669 "Return a list of sequences that aren't propagated to the source folders.
1670 It is just the sequences in the variable
1671 `mh-unpropagated-sequences' in addition to the
1672 Previous-Sequence (see mh-profile 5)."
1674 (cons mh-previous-seq mh-unpropagated-sequences)
1675 mh-unpropagated-sequences))
1677 (defun mh-index-matching-source-msgs (msgs &optional delete-from-index-data)
1678 "Return a table of original messages and folders for messages in MSGS.
1679 If optional argument DELETE-FROM-INDEX-DATA is non-nil, then each
1680 of the messages, whose counter-part is found in some source
1681 folder, is removed from `mh-index-data'."
1682 (let ((table (make-hash-table :test #'equal)))
1684 (let* ((checksum (gethash msg mh-index-msg-checksum-map))
1685 (pair (gethash checksum mh-index-checksum-origin-map)))
1686 (when (and checksum (car pair) (cdr pair)
1687 (mh-index-match-checksum (cdr pair) (car pair) checksum))
1688 (push (cdr pair) (gethash (car pair) table))
1689 (when delete-from-index-data
1690 (remhash (cdr pair) (gethash (car pair) mh-index-data))))))
1693 (defun mh-index-match-checksum (msg folder checksum)
1694 "Check if MSG in FOLDER has X-MHE-Checksum header value of CHECKSUM."
1696 (mh-exec-cmd-output mh-scan-prog nil "-width" "80"
1697 "-format" "%{x-mhe-checksum}\n" folder msg)
1698 (goto-char (point-min))
1699 (string-equal (buffer-substring-no-properties (point) (line-end-position))
1704 ;;; Serialization of Index Data
1706 (defun mh-index-write-data ()
1707 "Write index data to file."
1709 (unless (eq major-mode 'mh-folder-mode)
1710 (error "Can't be called from folder in \"%s\"" major-mode))
1711 (let ((data mh-index-data)
1712 (msg-checksum-map mh-index-msg-checksum-map)
1713 (checksum-origin-map mh-index-checksum-origin-map)
1714 (previous-search mh-index-previous-search)
1715 (sequence-search-flag mh-index-sequence-search-flag)
1716 (outfile (concat buffer-file-name mh-index-data-file))
1719 (with-temp-file outfile
1720 (mh-index-write-hashtable
1721 data (lambda (x) (loop for y being the hash-keys of x collect y)))
1722 (mh-index-write-hashtable msg-checksum-map #'identity)
1723 (mh-index-write-hashtable checksum-origin-map #'identity)
1724 (pp previous-search (current-buffer)) (insert "\n")
1725 (pp sequence-search-flag (current-buffer)) (insert "\n")))))
1727 (defun mh-index-write-hashtable (table proc)
1728 "Write TABLE to `current-buffer'.
1729 PROC is used to serialize the values corresponding to the hash
1731 (pp (loop for x being the hash-keys of table
1732 collect (cons x (funcall proc (gethash x table))))
1737 (defun mh-index-read-data ()
1738 "Read index data from file."
1740 (unless (eq major-mode 'mh-folder-mode)
1741 (error "Can't be called from folder in \"%s\"" major-mode))
1742 (let ((infile (concat buffer-file-name mh-index-data-file))
1745 (insert-file-contents-literally infile)
1746 (goto-char (point-min))
1747 (setq t1 (mh-index-read-hashtable
1749 (loop with table = (make-hash-table :test #'equal)
1750 for x in data do (setf (gethash x table) t)
1751 finally return table)))
1752 t2 (mh-index-read-hashtable #'identity)
1753 t3 (mh-index-read-hashtable #'identity)
1754 t4 (read (current-buffer))
1755 t5 (read (current-buffer))))
1756 (setq mh-index-data t1
1757 mh-index-msg-checksum-map t2
1758 mh-index-checksum-origin-map t3
1759 mh-index-previous-search t4
1760 mh-index-sequence-search-flag t5))))
1762 (defun mh-index-read-hashtable (proc)
1763 "From BUFFER read a hash table serialized as a list.
1764 PROC is used to convert the value to actual data."
1765 (loop with table = (make-hash-table :test #'equal)
1766 for pair in (read (current-buffer))
1767 do (setf (gethash (car pair) table) (funcall proc (cdr pair)))
1768 finally return table))
1772 ;;; Checksum Routines
1774 ;; A few different checksum programs are supported. The supported
1781 ;; To add support for your favorite checksum program add a clause to
1782 ;; the cond statement in mh-checksum-choose. This should set the
1783 ;; variable mh-checksum-cmd to the command line needed to run the
1784 ;; checsum program and should set mh-checksum-parser to a function
1785 ;; which returns a cons cell containing the message number and
1788 (defvar mh-checksum-cmd)
1789 (defvar mh-checksum-parser)
1791 (defun mh-checksum-choose ()
1792 "Check if a program to create a checksum is present."
1793 (unless (boundp 'mh-checksum-cmd)
1794 (let ((exec-path (append '("/sbin" "/usr/sbin") exec-path)))
1795 (cond ((executable-find "md5sum")
1796 (setq mh-checksum-cmd (list (executable-find "md5sum")))
1797 (setq mh-checksum-parser #'mh-md5sum-parser))
1798 ((executable-find "openssl")
1799 (setq mh-checksum-cmd (list (executable-find "openssl") "md5"))
1800 (setq mh-checksum-parser #'mh-openssl-parser))
1801 ((executable-find "md5")
1802 (setq mh-checksum-cmd (list (executable-find "md5")))
1803 (setq mh-checksum-parser #'mh-md5-parser))
1804 (t (error "No suitable checksum program"))))))
1806 (defun mh-md5sum-parser ()
1807 "Parse md5sum output."
1808 (let ((begin (line-beginning-position))
1809 (end (line-end-position))
1810 first-space last-slash)
1811 (setq first-space (search-forward " " end t))
1813 (setq last-slash (search-backward "/" begin t))
1814 (cond ((and first-space last-slash)
1815 (cons (string-to-number (buffer-substring-no-properties
1816 (1+ last-slash) end))
1817 (buffer-substring-no-properties begin (1- first-space))))
1818 (t (cons nil nil)))))
1820 (defun mh-openssl-parser ()
1821 "Parse openssl output."
1822 (let ((begin (line-beginning-position))
1823 (end (line-end-position))
1824 last-space last-slash)
1826 (setq last-space (search-backward " " begin t))
1827 (setq last-slash (search-backward "/" begin t))
1828 (cond ((and last-slash last-space)
1829 (cons (string-to-number (buffer-substring-no-properties
1830 (1+ last-slash) (1- last-space)))
1831 (buffer-substring-no-properties (1+ last-space) end))))))
1833 (defalias 'mh-md5-parser 'mh-openssl-parser)
1836 (defun mh-index-update-maps (folder &optional origin-map)
1837 "Annotate all as yet unannotated messages in FOLDER with their MD5 hash.
1838 As a side effect msg -> checksum map is updated. Optional
1839 argument ORIGIN-MAP is a hashtable which maps each message in the
1840 index folder to the original folder and message from whence it
1841 was copied. If present the checksum -> (origin-folder,
1842 origin-index) map is updated too."
1843 (clrhash mh-index-msg-checksum-map)
1845 ;; Clear temp buffer
1846 (set-buffer (get-buffer-create mh-temp-checksum-buffer))
1848 ;; Run scan to check if any messages needs MD5 annotations at all
1850 (mh-exec-cmd-output mh-scan-prog nil "-width" "80"
1851 "-format" "%(msg)\n%{x-mhe-checksum}\n"
1853 (goto-char (point-min))
1856 (setq msg (buffer-substring-no-properties
1857 (point) (line-end-position)))
1860 (cond ((not (string-match "^[0-9]*$" msg)))
1862 ;; need to compute checksum
1863 (set-buffer mh-temp-checksum-buffer)
1864 (insert mh-user-path (substring folder 1) "/" msg "\n"))
1867 (setq checksum (buffer-substring-no-properties
1868 (point) (line-end-position)))
1869 (let ((msg (string-to-number msg)))
1871 (mh-index-update-single-msg msg checksum origin-map)))))
1873 ;; Run checksum program if needed
1874 (unless (and (eobp) (bobp))
1875 (apply #'mh-xargs mh-checksum-cmd)
1876 (goto-char (point-min))
1878 (let* ((intermediate (funcall mh-checksum-parser))
1879 (msg (car intermediate))
1880 (checksum (cdr intermediate)))
1883 (mh-exec-cmd "anno" folder msg "-component" "X-MHE-Checksum"
1884 "-nodate" "-text" checksum "-inplace")
1888 (mh-index-update-single-msg msg checksum origin-map)))
1890 (mh-index-write-data))
1892 (defun mh-index-update-single-msg (msg checksum origin-map)
1893 "Update various maps for one message.
1894 MSG is a index folder message, CHECKSUM its MD5 hash and
1895 ORIGIN-MAP, if non-nil, a hashtable containing which maps each
1896 message in the index folder to the folder and message that it was
1897 copied from. The function updates the hash tables
1898 `mh-index-msg-checksum-map' and `mh-index-checksum-origin-map'.
1900 This function should only be called in the appropriate index
1902 (cond ((and origin-map (gethash checksum mh-index-checksum-origin-map))
1903 (let* ((intermediate (gethash msg origin-map))
1904 (ofolder (car intermediate))
1905 (omsg (cdr intermediate)))
1906 ;; This is most probably a duplicate. So eliminate it.
1907 (call-process "rm" nil nil nil
1908 (format "%s%s/%s" mh-user-path
1909 (substring mh-current-folder 1) msg))
1910 (when (gethash ofolder mh-index-data)
1911 (remhash omsg (gethash ofolder mh-index-data)))))
1913 (setf (gethash msg mh-index-msg-checksum-map) checksum)
1915 (setf (gethash checksum mh-index-checksum-origin-map)
1916 (gethash msg origin-map))))))
1918 (provide 'mh-search)
1921 ;; indent-tabs-mode: nil
1922 ;; sentence-end-double-space: nil
1925 ;; arch-tag: 607762ad-0dff-4fe1-a27e-6c0dde0dcc47
1926 ;;; mh-search ends here