1 ;;; mh-index -- MH-E interface to indexing programs
3 ;; Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
5 ;; Author: Satyaki Das <satyaki@theforce.stanford.edu>
6 ;; Maintainer: Bill Wohler <wohler@newt.com>
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
29 ;; (1) The following search engines are supported:
37 ;; (2) To use this package, you first have to build an index. Please read
38 ;; the documentation for `mh-index-search' to get started. That
39 ;; documentation will direct you to the specific instructions for your
40 ;; particular indexer.
46 (eval-when-compile (require 'mh-acros))
52 (autoload 'gnus-local-map-property "gnus-util")
53 (autoload 'gnus-eval-format "gnus-spec")
54 (autoload 'widget-convert-button "wid-edit")
55 (autoload 'executable-find "executable")
57 ;; Support different indexing programs
58 (defvar mh-indexer-choices
60 mh-swish++-binary mh-swish++-execute-search mh-swish++-next-result
61 mh-swish++-regexp-builder)
63 mh-swish-binary mh-swish-execute-search mh-swish-next-result nil)
65 mh-mairix-binary mh-mairix-execute-search mh-mairix-next-result
66 mh-mairix-regexp-builder)
68 mh-namazu-binary mh-namazu-execute-search mh-namazu-next-result nil)
70 mh-pick-binary mh-pick-execute-search mh-pick-next-result
71 mh-pick-regexp-builder)
73 mh-grep-binary mh-grep-execute-search mh-grep-next-result nil))
74 "List of possible indexer choices.")
75 (defvar mh-indexer nil
76 "Chosen index program.")
77 (defvar mh-index-execute-search-function nil
78 "Function which executes the search program.")
79 (defvar mh-index-next-result-function nil
80 "Function to parse the next line of output.")
81 (defvar mh-index-regexp-builder nil
82 "Function used to construct search regexp.")
84 ;; FIXME: This should be a defcustom...
85 (defvar mh-index-folder "+mhe-index"
86 "Folder that contains the folders resulting from the index searches.")
88 ;; Temporary buffers for search results
89 (defvar mh-index-temp-buffer " *mh-index-temp*")
90 (defvar mh-checksum-buffer " *mh-checksum-buffer*")
94 ;; A few different checksum programs are supported. The supported programs
100 ;; To add support for your favorite checksum program add a clause to the cond
101 ;; statement in mh-checksum-choose. This should set the variable
102 ;; mh-checksum-cmd to the command line needed to run the checsum program and
103 ;; should set mh-checksum-parser to a function which returns a cons cell
104 ;; containing the message number and checksum string.
106 (defvar mh-checksum-cmd)
107 (defvar mh-checksum-parser)
109 (defun mh-checksum-choose ()
110 "Check if a program to create a checksum is present."
111 (unless (boundp 'mh-checksum-cmd)
112 (let ((exec-path (append '("/sbin" "/usr/sbin") exec-path)))
113 (cond ((executable-find "md5sum")
114 (setq mh-checksum-cmd (list (executable-find "md5sum")))
115 (setq mh-checksum-parser #'mh-md5sum-parser))
116 ((executable-find "openssl")
117 (setq mh-checksum-cmd (list (executable-find "openssl") "md5"))
118 (setq mh-checksum-parser #'mh-openssl-parser))
119 ((executable-find "md5")
120 (setq mh-checksum-cmd (list (executable-find "md5")))
121 (setq mh-checksum-parser #'mh-md5-parser))
122 (t (error "No suitable checksum program"))))))
124 (defun mh-md5sum-parser ()
125 "Parse md5sum output."
126 (let ((begin (line-beginning-position))
127 (end (line-end-position))
128 first-space last-slash)
129 (setq first-space (search-forward " " end t))
131 (setq last-slash (search-backward "/" begin t))
132 (cond ((and first-space last-slash)
133 (cons (car (read-from-string (buffer-substring-no-properties
134 (1+ last-slash) end)))
135 (buffer-substring-no-properties begin (1- first-space))))
136 (t (cons nil nil)))))
138 (defun mh-openssl-parser ()
139 "Parse openssl output."
140 (let ((begin (line-beginning-position))
141 (end (line-end-position))
142 last-space last-slash)
144 (setq last-space (search-backward " " begin t))
145 (setq last-slash (search-backward "/" begin t))
146 (cond ((and last-slash last-space)
147 (cons (car (read-from-string (buffer-substring-no-properties
148 (1+ last-slash) (1- last-space))))
149 (buffer-substring-no-properties (1+ last-space) end))))))
151 (defalias 'mh-md5-parser 'mh-openssl-parser)
155 ;; Make sure that we don't produce too long a command line.
156 (defvar mh-index-max-cmdline-args 500
157 "Maximum number of command line args.")
159 (defun mh-index-execute (cmd &rest args)
160 "Partial imitation of xargs.
161 The current buffer contains a list of strings, one on each line.
162 The function will execute CMD with ARGS and pass the first
163 `mh-index-max-cmdline-args' strings to it. This is repeated till
164 all the strings have been used."
165 (goto-char (point-min))
166 (let ((current-buffer (current-buffer)))
168 (let ((out (current-buffer)))
169 (set-buffer current-buffer)
171 (let ((arg-list (reverse args))
173 (while (and (not (eobp)) (< count mh-index-max-cmdline-args))
174 (push (buffer-substring-no-properties (point) (line-end-position))
178 (apply #'call-process cmd nil (list out nil) nil
179 (nreverse arg-list))))
181 (insert-buffer-substring out)))))
185 (defun mh-index-update-single-msg (msg checksum origin-map)
186 "Update various maps for one message.
187 MSG is a index folder message, CHECKSUM its MD5 hash and
188 ORIGIN-MAP, if non-nil, a hashtable containing which maps each
189 message in the index folder to the folder and message that it was
190 copied from. The function updates the hash tables
191 `mh-index-msg-checksum-map' and `mh-index-checksum-origin-map'.
193 This function should only be called in the appropriate index
195 (cond ((and origin-map (gethash checksum mh-index-checksum-origin-map))
196 (let* ((intermediate (gethash msg origin-map))
197 (ofolder (car intermediate))
198 (omsg (cdr intermediate)))
199 ;; This is most probably a duplicate. So eliminate it.
200 (call-process "rm" nil nil nil
201 (format "%s%s/%s" mh-user-path
202 (substring mh-current-folder 1) msg))
203 (when (gethash ofolder mh-index-data)
204 (remhash omsg (gethash ofolder mh-index-data)))))
206 (setf (gethash msg mh-index-msg-checksum-map) checksum)
208 (setf (gethash checksum mh-index-checksum-origin-map)
209 (gethash msg origin-map))))))
212 (defun mh-index-update-maps (folder &optional origin-map)
213 "Annotate all as yet unannotated messages in FOLDER with their MD5 hash.
214 As a side effect msg -> checksum map is updated. Optional
215 argument ORIGIN-MAP is a hashtable which maps each message in the
216 index folder to the original folder and message from whence it
217 was copied. If present the checksum -> (origin-folder,
218 origin-index) map is updated too."
219 (clrhash mh-index-msg-checksum-map)
222 (set-buffer (get-buffer-create mh-checksum-buffer))
224 ;; Run scan to check if any messages needs MD5 annotations at all
226 (mh-exec-cmd-output mh-scan-prog nil "-width" "80"
227 "-format" "%(msg)\n%{x-mhe-checksum}\n"
229 (goto-char (point-min))
232 (setq msg (buffer-substring-no-properties
233 (point) (line-end-position)))
236 (cond ((not (string-match "^[0-9]*$" msg)))
238 ;; need to compute checksum
239 (set-buffer mh-checksum-buffer)
240 (insert mh-user-path (substring folder 1) "/" msg "\n"))
243 (setq checksum (buffer-substring-no-properties
244 (point) (line-end-position)))
245 (let ((msg (car (read-from-string msg))))
247 (mh-index-update-single-msg msg checksum origin-map)))))
249 ;; Run checksum program if needed
250 (unless (and (eobp) (bobp))
251 (apply #'mh-index-execute mh-checksum-cmd)
252 (goto-char (point-min))
254 (let* ((intermediate (funcall mh-checksum-parser))
255 (msg (car intermediate))
256 (checksum (cdr intermediate)))
259 (mh-exec-cmd "anno" folder msg "-component" "X-MHE-Checksum"
260 "-nodate" "-text" checksum "-inplace")
264 (mh-index-update-single-msg msg checksum origin-map)))
266 (mh-index-write-data))
268 (defvar mh-unpropagated-sequences '(cur range subject search)
269 "List of sequences that aren't preserved.")
271 (defun mh-unpropagated-sequences ()
272 "Return a list of sequences that aren't propagated to the source folders.
273 It is just the sequences in the variable
274 `mh-unpropagated-sequences' in addition to the
275 Previous-Sequence (see mh-profile 5)."
277 (cons mh-previous-seq mh-unpropagated-sequences)
278 mh-unpropagated-sequences))
281 (defun mh-create-sequence-map (seq-list)
282 "Return a map from msg number to list of sequences in which it is present.
283 SEQ-LIST is an assoc list whose keys are sequence names and whose
284 cdr is the list of messages in that sequence."
285 (loop with map = (make-hash-table)
287 when (and (not (memq (car seq) (mh-unpropagated-sequences)))
288 (mh-valid-seq-p (car seq)))
289 do (loop for msg in (cdr seq)
290 do (push (car seq) (gethash msg map)))
294 (defun mh-index-create-sequences ()
295 "Mirror sequences present in source folders in index folder."
296 (let ((seq-hash (make-hash-table :test #'equal))
298 (loop for folder being the hash-keys of mh-index-data
299 do (setf (gethash folder seq-hash)
300 (mh-create-sequence-map
301 (mh-read-folder-sequences folder nil))))
302 (dolist (msg (mh-translate-range mh-current-folder "all"))
303 (let* ((checksum (gethash msg mh-index-msg-checksum-map))
304 (pair (gethash checksum mh-index-checksum-origin-map))
307 (loop for seq in (ignore-errors
308 (gethash omsg (gethash ofolder seq-hash)))
309 do (if (assoc seq seq-list)
310 (push msg (cdr (assoc seq seq-list)))
311 (push (list seq msg) seq-list)))))
312 (loop for seq in seq-list
313 do (apply #'mh-exec-cmd "mark" mh-current-folder
314 "-sequence" (symbol-name (car seq)) "-add"
315 (mapcar #'(lambda (x) (format "%s" x)) (cdr seq))))))
317 (defvar mh-flists-results-folder "sequence"
318 "Subfolder for `mh-index-folder' where flists output is placed.")
319 (defvar mh-flists-sequence)
320 (defvar mh-flists-called-flag nil)
322 (defun mh-index-generate-pretty-name (string)
323 "Given STRING generate a name which is suitable for use as a folder name.
324 White space from the beginning and end are removed. All spaces in
325 the name are replaced with underscores and all / are replaced
326 with $. If STRING is longer than 20 it is truncated too. STRING
327 could be a list of strings in which case they are concatenated to
328 construct the base name."
332 (when (car string) (insert (car string)))
333 (dolist (s (cdr string))
335 (setq string (mh-replace-string "-lbrace" " "))
336 (setq string (mh-replace-string "-rbrace" " "))
337 (subst-char-in-region (point-min) (point-max) ?( ? t)
338 (subst-char-in-region (point-min) (point-max) ?) ? t)
339 (subst-char-in-region (point-min) (point-max) ?- ? t)
340 (goto-char (point-min))
341 (while (and (not (eobp)) (memq (char-after) '(? ?\t ?\n ?\r ?_)))
343 (goto-char (point-max))
344 (while (and (not (bobp)) (memq (char-before) '(? ?\t ?\n ?\r ?_)))
345 (delete-backward-char 1))
346 (subst-char-in-region (point-min) (point-max) ? ?_ t)
347 (subst-char-in-region (point-min) (point-max) ?\t ?_ t)
348 (subst-char-in-region (point-min) (point-max) ?\n ?_ t)
349 (subst-char-in-region (point-min) (point-max) ?\r ?_ t)
350 (subst-char-in-region (point-min) (point-max) ?/ ?$ t)
351 (let ((out (truncate-string-to-width (buffer-string) 20)))
352 (cond ((eq mh-indexer 'flists)
353 (format "%s/%s" mh-flists-results-folder mh-flists-sequence))
354 ((equal out mh-flists-results-folder) (concat out "1"))
358 (defun* mh-index-search (redo-search-flag folder search-regexp
359 &optional window-config)
360 "Perform an indexed search in an MH mail folder.
362 Use a prefix argument to repeat the search.
364 Unlike regular searches, the prompt for the folder to search can be
365 \"all\" to search all folders; in addition, the search works recursively
366 on the listed folder. The search criteria are entered in an MH-Pick
367 buffer as described in `mh-search-folder'.
369 To perform the search, type \\<mh-pick-mode-map>\\[mh-do-search].
370 Another difference from the regular searches is that because the
371 search operates on more than one folder, the messages that are found
372 are put in a temporary sub-folder of \"+mhe-index\" and are displayed in
373 an MH-Folder buffer. This buffer is special because it displays
374 messages from multiple folders; each set of messages from a given
375 folder has a heading with the folder name.
377 In addition, the \\<mh-folder-mode-map>\\[mh-index-visit-folder]
378 command can be used to visit the folder of the message at point.
379 Initially, only the messages that matched the search criteria are
380 displayed in the folder. While the temporary buffer has its own set of
381 message numbers, the actual messages numbers are shown in the visited
382 folder. Thus, the \\[mh-index-visit-folder] command is useful to find
383 the actual message number of an interesting message, or to view
384 surrounding messages with the \\[mh-rescan-folder] command.
386 Because this folder is temporary, you'll probably get in the habit of
387 killing it when you're done with \\[mh-kill-folder].
389 If you have run the \\[mh-search-folder] command, but change your mind
390 while entering the search criteria and actually want to run an indexed
391 search, then you can use the
392 \\<mh-pick-mode-map>\\[mh-index-do-search] command in the MH-Pick
395 The \\<mh-folder-mode-map>\\[mh-index-search] command runs the command
396 defined by the `mh-index-program' option. The default value is
397 \"Auto-detect\" which means that MH-E will automatically choose one of
398 \"swish++\", \"swish-e\", \"mairix\", \"namazu\", \"pick\" and
399 \"grep\" in that order. If, for example, you have both \"swish++\" and
400 \"mairix\" installed and you want to use \"mairix\", then you can set
401 this option to \"mairix\".
405 The \"pick\" and \"grep\" commands do not perform a
406 recursive search on the given folder.
408 This command uses an \"X-MHE-Checksum:\" header field to cache
409 the MD5 checksum of a message. This means that if an incoming
410 message already contains an \"X-MHE-Checksum:\" field, that
411 message might not be found by this command. The following
412 \"procmail\" recipe avoids this problem by renaming the existing
416 | formail -R \"X-MHE-Checksum\" \"X-Old-MHE-Checksum\"
418 The documentation for the following commands describe how to set
419 up the various indexing programs to use with MH-E. The \"pick\"
420 and \"grep\" commands do not require additional configuration.
422 - `mh-swish++-execute-search'
423 - `mh-swish-execute-search'
424 - `mh-mairix-execute-search'
425 - `mh-namazu-execute-search'
426 - `mh-pick-execute-search'
427 - `mh-grep-execute-search'
429 In a program, if REDO-SEARCH-FLAG is non-nil and the current
430 folder buffer was generated by a index search, then the search is
431 repeated. Otherwise, FOLDER is searched with SEARCH-REGEXP and
432 the results are presented in an MH-E folder. If FOLDER is \"+\"
433 then mail in all folders are searched. Optional argument
434 WINDOW-CONFIG stores the window configuration that will be
435 restored after the user quits the folder containing the index
438 (list current-prefix-arg
440 (unless mh-find-path-run (mh-find-path))
441 (or (and current-prefix-arg mh-index-sequence-search-flag)
442 (and current-prefix-arg (car mh-index-previous-search))
443 (mh-prompt-for-folder "Search" "+" nil "all" t)))
445 ;; Yes, we do want to call mh-index-choose every time in case the
446 ;; user has switched the indexer manually.
447 (unless (mh-index-choose) (error "No indexing program found"))
448 (or (and current-prefix-arg (cadr mh-index-previous-search))
449 mh-index-regexp-builder
450 (read-string (format "%s regexp: "
452 (symbol-name mh-indexer))))))
454 (and current-prefix-arg (cadr mh-index-previous-search)))
455 mh-index-regexp-builder)
456 (current-window-configuration)
458 ;; Redoing a sequence search?
459 (when (and redo-search-flag mh-index-data mh-index-sequence-search-flag
460 (not mh-flists-called-flag))
461 (let ((mh-flists-called-flag t))
462 (apply #'mh-index-sequenced-messages mh-index-previous-search))
463 (return-from mh-index-search))
464 ;; We have fancy query parsing
465 (when (symbolp search-regexp)
466 (mh-search-folder folder window-config)
467 (setq mh-searching-function 'mh-index-do-search)
468 (return-from mh-index-search))
470 (let ((result-count 0)
471 (old-window-config (or window-config mh-previous-window-config))
472 (previous-search mh-index-previous-search)
473 (index-folder (format "%s/%s" mh-index-folder
474 (mh-index-generate-pretty-name search-regexp))))
475 ;; Create a new folder for the search results or recreate the old one...
476 (if (and redo-search-flag mh-index-previous-search)
477 (let ((buffer-name (buffer-name (current-buffer))))
478 (mh-process-or-undo-commands buffer-name)
479 (save-excursion (mh-exec-cmd-quiet nil "rmf" buffer-name))
480 (mh-exec-cmd-quiet nil "folder" "-create" "-fast" buffer-name)
481 (setq index-folder buffer-name))
482 (setq index-folder (mh-index-new-folder index-folder search-regexp)))
484 (let ((folder-path (format "%s%s" mh-user-path (substring folder 1)))
485 (folder-results-map (make-hash-table :test #'equal))
486 (origin-map (make-hash-table :test #'equal)))
487 ;; Run search program...
488 (message "Executing %s... " mh-indexer)
489 (funcall mh-index-execute-search-function folder-path search-regexp)
491 ;; Parse indexer output
492 (message "Processing %s output... " mh-indexer)
493 (goto-char (point-min))
494 (loop for next-result = (funcall mh-index-next-result-function)
496 do (unless (eq next-result 'error)
497 (unless (gethash (car next-result) folder-results-map)
498 (setf (gethash (car next-result) folder-results-map)
499 (make-hash-table :test #'equal)))
500 (setf (gethash (cadr next-result)
501 (gethash (car next-result) folder-results-map))
504 ;; Copy the search results over
505 (maphash #'(lambda (folder msgs)
506 (let ((cur (car (mh-translate-range folder "cur")))
507 (msgs (sort (loop for msg being the hash-keys of msgs
510 (mh-exec-cmd "refile" msgs "-src" folder
511 "-link" index-folder)
512 ;; Restore cur to old value, that refile changed
514 (mh-exec-cmd-quiet nil "mark" folder "-add" "-zero"
515 "-sequence" "cur" (format "%s" cur)))
516 (loop for msg in msgs
517 do (incf result-count)
518 (setf (gethash result-count origin-map)
519 (cons folder msg)))))
522 ;; Vist the results folder
523 (mh-visit-folder index-folder () (list folder-results-map origin-map))
525 (goto-char (point-min))
527 (mh-update-sequences)
530 ;; Update the speedbar, if needed
531 (when (mh-speed-flists-active-p)
532 (mh-speed-flists t mh-current-folder))
535 (when (or (and redo-search-flag previous-search) window-config)
536 (setq mh-previous-window-config old-window-config))
537 (setq mh-index-previous-search (list folder search-regexp))
539 ;; Write out data to disk
540 (unless mh-flists-called-flag (mh-index-write-data))
542 (message "%s found %s matches in %s folders"
543 (upcase-initials (symbol-name mh-indexer))
544 (loop for msg-hash being hash-values of mh-index-data
545 sum (hash-table-count msg-hash))
546 (loop for msg-hash being hash-values of mh-index-data
547 count (> (hash-table-count msg-hash) 0))))))
551 ;;; Functions to serialize index data...
553 (defun mh-index-write-data ()
554 "Write index data to file."
556 (unless (eq major-mode 'mh-folder-mode)
557 (error "Can't be called from folder in \"%s\"" major-mode))
558 (let ((data mh-index-data)
559 (msg-checksum-map mh-index-msg-checksum-map)
560 (checksum-origin-map mh-index-checksum-origin-map)
561 (previous-search mh-index-previous-search)
562 (sequence-search-flag mh-index-sequence-search-flag)
563 (outfile (concat buffer-file-name mh-index-data-file))
566 (with-temp-file outfile
567 (mh-index-write-hashtable
568 data (lambda (x) (loop for y being the hash-keys of x collect y)))
569 (mh-index-write-hashtable msg-checksum-map #'identity)
570 (mh-index-write-hashtable checksum-origin-map #'identity)
571 (pp previous-search (current-buffer)) (insert "\n")
572 (pp sequence-search-flag (current-buffer)) (insert "\n")))))
575 (defun mh-index-read-data ()
576 "Read index data from file."
578 (unless (eq major-mode 'mh-folder-mode)
579 (error "Can't be called from folder in \"%s\"" major-mode))
580 (let ((infile (concat buffer-file-name mh-index-data-file))
583 (insert-file-contents-literally infile)
584 (goto-char (point-min))
585 (setq t1 (mh-index-read-hashtable
587 (loop with table = (make-hash-table :test #'equal)
588 for x in data do (setf (gethash x table) t)
589 finally return table)))
590 t2 (mh-index-read-hashtable #'identity)
591 t3 (mh-index-read-hashtable #'identity)
592 t4 (read (current-buffer))
593 t5 (read (current-buffer))))
594 (setq mh-index-data t1
595 mh-index-msg-checksum-map t2
596 mh-index-checksum-origin-map t3
597 mh-index-previous-search t4
598 mh-index-sequence-search-flag t5))))
600 (defun mh-index-write-hashtable (table proc)
601 "Write TABLE to `current-buffer'.
602 PROC is used to serialize the values corresponding to the hash
604 (pp (loop for x being the hash-keys of table
605 collect (cons x (funcall proc (gethash x table))))
609 (defun mh-index-read-hashtable (proc)
610 "From BUFFER read a hash table serialized as a list.
611 PROC is used to convert the value to actual data."
612 (loop with table = (make-hash-table :test #'equal)
613 for pair in (read (current-buffer))
614 do (setf (gethash (car pair) table) (funcall proc (cdr pair)))
615 finally return table))
619 "Non-nil means that this folder was generated by an index search."
623 (defun mh-index-do-search ()
624 "Construct appropriate regexp and call `mh-index-search'."
626 (unless (mh-index-choose) (error "No indexing program found"))
627 (let* ((regexp-list (mh-pick-parse-search-buffer))
628 (pattern (funcall mh-index-regexp-builder regexp-list)))
630 (mh-index-search nil mh-current-folder pattern
631 mh-previous-window-config)
632 (error "No search terms"))))
635 (defun mh-index-parse-search-regexp (input-string)
636 "Construct parse tree for INPUT-STRING.
637 All occurrences of &, |, ! and ~ in INPUT-STRING are replaced by
638 AND, OR and NOT as appropriate. Then the resulting string is
642 (insert input-string)
644 (mh-replace-string "\t" " ")
646 (mh-replace-string " AND " " and ")
647 (mh-replace-string "&" " and ")
648 (mh-replace-string " -and " " and ")
650 (mh-replace-string " OR " " or ")
651 (mh-replace-string "|" " or ")
652 (mh-replace-string " -or " " or ")
654 (mh-replace-string " NOT " " not ")
655 (mh-replace-string "!" " not ")
656 (mh-replace-string "~" " not ")
657 (mh-replace-string " -not " " not ")
658 ;; synonyms of left brace
659 (mh-replace-string "(" " ( ")
660 (mh-replace-string " -lbrace " " ( ")
661 ;; synonyms of right brace
662 (mh-replace-string ")" " ) ")
663 (mh-replace-string " -rbrace " " ) ")
664 ;; get the normalized input
665 (setq input (format "( %s )" (buffer-substring (point-min) (point-max)))))
667 (let ((tokens (mh-index-add-implicit-ops (split-string input)))
671 (dolist (token tokens)
672 (cond ((equal token "(") (push 'paren op-stack))
673 ((equal token "not") (push 'not op-stack))
674 ((equal token "or") (push 'or op-stack))
675 ((equal token "and") (push 'and op-stack))
677 (multiple-value-setq (op-stack operand-stack)
678 (mh-index-evaluate op-stack operand-stack))
679 (when (eq (car op-stack) 'not)
680 (setq op-stack (cdr op-stack))
681 (push `(not ,(pop operand-stack)) operand-stack))
682 (when (eq (car op-stack) 'and)
683 (setq op-stack (cdr op-stack))
684 (setq oper1 (pop operand-stack))
685 (push `(and ,(pop operand-stack) ,oper1) operand-stack)))
686 ((eq (car op-stack) 'not)
687 (setq op-stack (cdr op-stack))
688 (push `(not ,token) operand-stack)
689 (when (eq (car op-stack) 'and)
690 (setq op-stack (cdr op-stack))
691 (setq oper1 (pop operand-stack))
692 (push `(and ,(pop operand-stack) ,oper1) operand-stack)))
693 ((eq (car op-stack) 'and)
694 (setq op-stack (cdr op-stack))
695 (push `(and ,(pop operand-stack) ,token) operand-stack))
696 (t (push token operand-stack))))
697 (prog1 (pop operand-stack)
698 (when (or op-stack operand-stack)
699 (error "Invalid regexp: %s" input))))))
701 (defun mh-index-add-implicit-ops (tokens)
702 "Add implicit operators in the list TOKENS."
707 (setq current (pop tokens))
708 (cond ((or (equal current ")") (equal current "and") (equal current "or"))
709 (setq literal-seen nil)
710 (push current result))
713 (setq literal-seen nil)
716 (push current result)
717 (unless (or (equal current "(") (equal current "not"))
718 (setq literal-seen t)))))
721 (defun mh-index-evaluate (op-stack operand-stack)
722 "Read expression till starting paren based on OP-STACK and OPERAND-STACK."
723 (block mh-index-evaluate
726 (setq op (pop op-stack))
727 (cond ((eq op 'paren)
728 (return-from mh-index-evaluate (values op-stack operand-stack)))
730 (push `(not ,(pop operand-stack)) operand-stack))
731 ((or (eq op 'and) (eq op 'or))
732 (setq oper1 (pop operand-stack))
733 (push `(,op ,(pop operand-stack) ,oper1) operand-stack))))
734 (error "Ran out of tokens"))))
737 (defun mh-index-next-folder (&optional backward-flag)
738 "Jump to the next folder marker.
739 The function is only applicable to folders displaying index search
741 With non-nil optional argument BACKWARD-FLAG, jump to the previous
744 (if (null mh-index-data)
745 (message "Only applicable in an MH-E index search buffer")
746 (let ((point (point)))
747 (forward-line (if backward-flag -1 1))
748 (cond ((if backward-flag
749 (re-search-backward "^+" (point-min) t)
750 (re-search-forward "^+" (point-max) t))
752 ((and (if backward-flag
753 (goto-char (point-max))
754 (goto-char (point-min)))
757 (re-search-backward "^+" (point-min) t)
758 (re-search-forward "^+" (point-max) t))
760 (t (goto-char point))))))
763 (defun mh-index-previous-folder ()
764 "Jump to the previous folder marker."
766 (mh-index-next-folder t))
768 (defun mh-folder-exists-p (folder)
769 "Check if FOLDER exists."
770 (and (mh-folder-name-p folder)
773 (mh-exec-cmd-output "folder" nil "-fast" "-nocreate" folder)
774 (goto-char (point-min))
777 (defun mh-msg-exists-p (msg folder)
778 "Check if MSG exists in FOLDER."
779 (file-exists-p (format "%s%s/%s" mh-user-path (substring folder 1) msg)))
781 (defun mh-index-new-folder (name search-regexp)
782 "Return a folder name based on NAME for search results of SEARCH-REGEXP.
784 If folder NAME already exists and was generated for the same
785 SEARCH-REGEXP then it is reused.
787 Otherwise if the folder NAME was generated from a different
788 search then check if NAME<2> can be used. Otherwise try NAME<3>.
789 This is repeated till we find a new folder name.
791 If the folder returned doesn't exist then it is created."
792 (unless (mh-folder-name-p name)
793 (error "The argument should be a valid MH folder name"))
796 for candidate = (if (equal i 1) name (format "%s<%s>" name i))
797 when (or (not (mh-folder-exists-p candidate))
798 (equal (mh-index-folder-search-regexp candidate)
801 ;; Do pending refiles/deletes...
802 (when (get-buffer chosen-name)
803 (mh-process-or-undo-commands chosen-name))
804 ;; Recreate folder...
805 (save-excursion (mh-exec-cmd-quiet nil "rmf" chosen-name))
806 (mh-exec-cmd-quiet nil "folder" "-create" "-fast" chosen-name)
807 (mh-remove-from-sub-folders-cache chosen-name)
808 (when (boundp 'mh-speed-folder-map)
809 (mh-speed-add-folder chosen-name))
812 (defun mh-index-folder-search-regexp (folder)
813 "If FOLDER was created by a index search, return the search regexp.
814 Return nil if FOLDER doesn't exist or the .mhe_index file is
818 (insert-file-contents
819 (format "%s%s/%s" mh-user-path (substring folder 1) mh-index-data-file))
820 (goto-char (point-min))
822 (cadr (read (current-buffer))))))
825 (defun mh-index-insert-folder-headers ()
826 "Annotate the search results with original folder names."
827 (let ((cur-msg (mh-get-msg-num nil))
828 (old-buffer-modified-flag (buffer-modified-p))
829 (buffer-read-only nil)
830 current-folder last-folder)
831 (goto-char (point-min))
833 (setq current-folder (car (gethash (gethash (mh-get-msg-num nil)
834 mh-index-msg-checksum-map)
835 mh-index-checksum-origin-map)))
836 (when (and current-folder (not (equal current-folder last-folder)))
837 (insert (if last-folder "\n" "") current-folder "\n")
838 (setq last-folder current-folder))
842 (mh-goto-msg cur-msg t))
843 (set-buffer-modified-p old-buffer-modified-flag))
844 (mh-index-create-imenu-index))
847 (defun mh-index-create-imenu-index ()
848 "Create alist of folder names and positions in index folder buffers."
850 (setq which-func-mode t)
852 (goto-char (point-min))
853 (while (re-search-forward "^+" nil t)
856 (push (cons (buffer-substring-no-properties
857 (point) (line-end-position))
858 (set-marker (make-marker) (point)))
860 (setq imenu--index-alist (nreverse alist)))))
863 (defun mh-index-group-by-folder ()
864 "Partition the messages based on source folder.
865 Returns an alist with the the folder names in the car and the cdr
866 being the list of messages originally from that folder."
868 (goto-char (point-min))
869 (let ((result-table (make-hash-table :test #'equal)))
870 (loop for msg being hash-keys of mh-index-msg-checksum-map
871 do (push msg (gethash (car (gethash
872 (gethash msg mh-index-msg-checksum-map)
873 mh-index-checksum-origin-map))
875 (loop for x being the hash-keys of result-table
876 collect (cons x (nreverse (gethash x result-table)))))))
879 (defun mh-index-delete-folder-headers ()
880 "Delete the folder headers."
881 (let ((cur-msg (mh-get-msg-num nil))
882 (old-buffer-modified-flag (buffer-modified-p))
883 (buffer-read-only nil))
884 (while (and (not cur-msg) (not (eobp)))
886 (setq cur-msg (mh-get-msg-num nil)))
887 (goto-char (point-min))
889 (if (or (char-equal (char-after) ?+) (char-equal (char-after) 10))
890 (delete-region (point) (progn (forward-line) (point)))
892 (when cur-msg (mh-goto-msg cur-msg t t))
893 (set-buffer-modified-p old-buffer-modified-flag)))
896 (defun mh-index-visit-folder ()
897 "Visit original folder from where the message at point was found."
899 (unless mh-index-data
900 (error "Not in an index folder"))
903 (cond ((and (bolp) (eolp))
904 (ignore-errors (forward-line -1))
905 (setq msg (mh-get-msg-num t)))
906 ((equal (char-after (line-beginning-position)) ?+)
907 (setq folder (buffer-substring-no-properties
908 (line-beginning-position) (line-end-position))))
909 (t (setq msg (mh-get-msg-num t)))))
911 (setq folder (car (gethash (gethash msg mh-index-msg-checksum-map)
912 mh-index-checksum-origin-map))))
913 (when (or (not (get-buffer folder))
914 (y-or-n-p (format "Reuse buffer displaying %s? " folder)))
916 folder (loop for x being the hash-keys of (gethash folder mh-index-data)
917 when (mh-msg-exists-p x folder) collect x)))))
919 (defun mh-index-match-checksum (msg folder checksum)
920 "Check if MSG in FOLDER has X-MHE-Checksum header value of CHECKSUM."
922 (mh-exec-cmd-output mh-scan-prog nil "-width" "80"
923 "-format" "%{x-mhe-checksum}\n" folder msg)
924 (goto-char (point-min))
925 (string-equal (buffer-substring-no-properties (point) (line-end-position))
928 (defun mh-index-matching-source-msgs (msgs &optional delete-from-index-data)
929 "Return a table of original messages and folders for messages in MSGS.
930 If optional argument DELETE-FROM-INDEX-DATA is non-nil, then each
931 of the messages, whose counter-part is found in some source
932 folder, is removed from `mh-index-data'."
933 (let ((table (make-hash-table :test #'equal)))
935 (let* ((checksum (gethash msg mh-index-msg-checksum-map))
936 (pair (gethash checksum mh-index-checksum-origin-map)))
937 (when (and checksum (car pair) (cdr pair)
938 (mh-index-match-checksum (cdr pair) (car pair) checksum))
939 (push (cdr pair) (gethash (car pair) table))
940 (when delete-from-index-data
941 (remhash (cdr pair) (gethash (car pair) mh-index-data))))))
945 (defun mh-index-execute-commands ()
946 "Delete/refile the actual messages.
947 The copies in the searched folder are then deleted/refiled to get
948 the desired result. Before deleting the messages we make sure
949 that the message being deleted is identical to the one that the
950 user has marked in the index buffer."
953 (mh-speed-flists-inhibit-flag t))
955 (lambda (folder msgs)
956 (push folder folders)
957 (if (not (get-buffer folder))
958 ;; If source folder not open, just delete the messages...
959 (apply #'mh-exec-cmd "rmm" folder (mh-coalesce-msg-list msgs))
960 ;; Otherwise delete the messages in the source buffer...
963 (let ((old-refile-list mh-refile-list)
964 (old-delete-list mh-delete-list))
965 (setq mh-refile-list nil
967 (unwind-protect (mh-execute-commands)
971 (loop for y in (cdr x)
972 unless (memq y msgs) collect y)))
975 (loop for x in old-delete-list
976 unless (memq x msgs) collect x))
977 (mh-set-folder-modified-p (mh-outstanding-commands-p))
978 (when (mh-outstanding-commands-p)
979 (mh-notate-deleted-and-refiled)))))))
980 (mh-index-matching-source-msgs (append (loop for x in mh-refile-list
987 (defun mh-index-add-to-sequence (seq msgs)
988 "Add to SEQ the messages in the list MSGS.
989 This function updates the source folder sequences. Also makes an
990 attempt to update the source folder buffer if we have it open."
991 ;; Don't need to do anything for cur
993 (when (and (not (memq seq (mh-unpropagated-sequences)))
994 (mh-valid-seq-p seq))
996 (mh-speed-flists-inhibit-flag t))
997 (maphash (lambda (folder msgs)
998 (push folder folders)
999 ;; Add messages to sequence in source folder...
1000 (apply #'mh-exec-cmd-quiet nil "mark" folder
1001 "-add" "-nozero" "-sequence" (symbol-name seq)
1002 (mapcar (lambda (x) (format "%s" x))
1003 (mh-coalesce-msg-list msgs)))
1004 ;; Update source folder buffer if we have it open...
1005 (when (get-buffer folder)
1008 (mh-put-msg-in-seq msgs seq))))
1009 (mh-index-matching-source-msgs msgs))
1013 (defun mh-index-delete-from-sequence (seq msgs)
1014 "Delete from SEQ the messages in MSGS.
1015 This function updates the source folder sequences. Also makes an
1016 attempt to update the source folder buffer if present."
1018 (when (and (not (memq seq (mh-unpropagated-sequences)))
1019 (mh-valid-seq-p seq))
1021 (mh-speed-flists-inhibit-flag t))
1022 (maphash (lambda (folder msgs)
1023 (push folder folders)
1024 ;; Remove messages from sequence in source folder...
1025 (apply #'mh-exec-cmd-quiet nil "mark" folder
1026 "-del" "-nozero" "-sequence" (symbol-name seq)
1027 (mapcar (lambda (x) (format "%s" x))
1028 (mh-coalesce-msg-list msgs)))
1029 ;; Update source folder buffer if we have it open...
1030 (when (get-buffer folder)
1033 (mh-delete-msg-from-seq msgs seq t))))
1034 (mh-index-matching-source-msgs msgs))
1041 (defvar mh-index-pick-folder)
1042 (defvar mh-pick-binary "pick")
1044 (defun mh-pick-execute-search (folder-path search-regexp)
1047 Unlike the other index search programs \"pick\" only searches
1048 messages present in the folder itself and does not descend into
1049 any sub-folders that may be present.
1051 In a program, FOLDER-PATH is the directory in which SEARCH-REGEXP
1053 (set-buffer (get-buffer-create mh-index-temp-buffer))
1055 (setq mh-index-pick-folder
1056 (concat "+" (substring folder-path (length mh-user-path))))
1057 (apply #'call-process (expand-file-name "pick" mh-progs) nil '(t nil) nil
1058 mh-index-pick-folder "-list" search-regexp)
1059 (goto-char (point-min)))
1061 (defun mh-pick-next-result ()
1062 "Return the next pick search result."
1064 (when (eobp) (return nil))
1065 (unless (re-search-forward "^[1-9][0-9]*$" (line-end-position) t)
1067 (list mh-index-pick-folder
1068 (car (read-from-string (buffer-substring-no-properties
1069 (line-beginning-position)
1070 (line-end-position))))
1078 (defvar mh-grep-binary (executable-find "grep"))
1080 (defun mh-grep-execute-search (folder-path search-regexp)
1081 "Execute grep and read the results.
1083 Unlike the other index search programs \"grep\" only searches
1084 messages present in the folder itself and does not descend into
1085 any sub-folders that may be present.
1087 In a program, FOLDER-PATH is the directory in which SEARCH-REGEXP
1089 (set-buffer (get-buffer-create mh-index-temp-buffer))
1091 (call-process mh-grep-binary nil '(t nil) nil
1092 "-i" "-r" search-regexp folder-path)
1093 (goto-char (point-min)))
1095 (defun mh-grep-next-result ()
1096 "Read the next result.
1097 Parse it and return the message folder, message index and the
1098 match. If no other matches left then return nil. If the current
1099 record is invalid return 'error."
1104 (let ((eol-pos (line-end-position))
1105 (bol-pos (line-beginning-position))
1106 folder-start msg-end)
1108 (unless (search-forward mh-user-path eol-pos t)
1110 (setq folder-start (point))
1111 (unless (search-forward ":" eol-pos t)
1113 (let ((match (buffer-substring-no-properties (point) eol-pos)))
1115 (setq msg-end (point))
1116 (unless (search-backward "/" folder-start t)
1118 (list (format "+%s" (buffer-substring-no-properties
1119 folder-start (point)))
1120 (let ((val (ignore-errors (read-from-string
1121 (buffer-substring-no-properties
1122 (1+ (point)) msg-end)))))
1123 (if (and (consp val) (integerp (car val)))
1133 (defvar mh-mairix-binary (executable-find "mairix"))
1134 (defvar mh-mairix-directory ".mairix")
1135 (defvar mh-mairix-folder nil)
1137 (defun mh-mairix-execute-search (folder-path search-regexp-list)
1138 "Execute mairix and read the results.
1140 In the examples below, replace \"/home/user/Mail\" with the path
1141 to your MH directory.
1143 First create the directory \"/home/user/Mail/.mairix\". Then
1144 create the file \"/home/user/Mail/.mairix/config\" with the
1147 base=/home/user/Mail
1149 # List of folders that should be indexed. 3 dots at the end means there
1150 # are subfolders within the folder
1151 mh=archive...:inbox:drafts:news:sent:trash
1154 database=/home/user/Mail/mairix/database
1156 Use the following command line to generate the mairix index. Run
1157 this daily from cron:
1159 mairix -f /home/user/Mail/.mairix/config
1161 In a program, FOLDER-PATH is the directory in which
1162 SEARCH-REGEXP-LIST is used to search."
1163 (set-buffer (get-buffer-create mh-index-temp-buffer))
1165 (unless mh-mairix-binary
1166 (error "Set mh-mairix-binary appropriately"))
1167 (apply #'call-process mh-mairix-binary nil '(t nil) nil
1168 "-r" "-f" (format "%s%s/config" mh-user-path mh-mairix-directory)
1170 (goto-char (point-min))
1171 (setq mh-mairix-folder
1172 (let ((last-char (substring folder-path (1- (length folder-path)))))
1173 (if (equal last-char "/")
1175 (format "%s/" folder-path)))))
1177 (defun mh-mairix-next-result ()
1178 "Return next result from mairix output."
1181 (when (or (eobp) (and (bolp) (eolp)))
1183 (unless (eq (char-after) ?/)
1185 (let ((start (point))
1187 (setq end (line-end-position))
1188 (unless (search-forward mh-mairix-folder end t)
1190 (goto-char (match-beginning 0))
1191 (unless (equal (point) start)
1194 (unless (search-backward "/" start t)
1196 (setq msg-start (1+ (point)))
1198 (unless (search-forward mh-user-path end t)
1200 (list (format "+%s" (buffer-substring-no-properties
1201 (point) (1- msg-start)))
1202 (car (read-from-string
1203 (buffer-substring-no-properties msg-start end)))
1207 (defun mh-mairix-regexp-builder (regexp-list)
1208 "Generate query for mairix.
1209 REGEXP-LIST is an alist of fields and values."
1211 (dolist (pair regexp-list)
1215 (cond ((eq (car pair) 'to) "t:")
1216 ((eq (car pair) 'from) "f:")
1217 ((eq (car pair) 'cc) "c:")
1218 ((eq (car pair) 'subject) "s:")
1219 ((eq (car pair) 'date) "d:")
1221 (let ((sop (cdr (mh-mairix-convert-to-sop* (cdr pair))))
1223 (dolist (conjunct sop)
1224 (let ((expr-list (cdr conjunct))
1226 (dolist (e expr-list)
1227 (setq expr-string (concat expr-string ","
1228 (if (atom e) "" "~")
1229 (if (atom e) e (cadr e)))))
1230 (setq final (concat final "/" (substring expr-string 1)))))
1231 (substring final 1)))
1235 (defun mh-mairix-convert-to-sop* (expr)
1236 "Convert EXPR to sum of product form."
1237 (cond ((atom expr) `(or (and ,expr)))
1238 ((eq (car expr) 'or)
1240 (loop for e in (mapcar #'mh-mairix-convert-to-sop* (cdr expr))
1242 ((eq (car expr) 'and)
1243 (let ((conjuncts (mapcar #'mh-mairix-convert-to-sop* (cdr expr)))
1245 (setq result (pop conjuncts))
1247 (setq next-factor (pop conjuncts))
1248 (setq result (let ((res ()))
1249 (dolist (t1 (cdr result))
1250 (dolist (t2 (cdr next-factor))
1251 (push `(and ,@(cdr t1) ,@(cdr t2)) res)))
1254 ((atom (cadr expr)) `(or (and ,expr)))
1255 ((eq (caadr expr) 'not) (mh-mairix-convert-to-sop* (cadadr expr)))
1256 ((eq (caadr expr) 'and) (mh-mairix-convert-to-sop*
1257 `(or ,@(mapcar #'(lambda (x) `(not ,x))
1259 ((eq (caadr expr) 'or) (mh-mairix-convert-to-sop*
1260 `(and ,@(mapcar #'(lambda (x) `(not ,x))
1262 (t (error "Unreachable: %s" expr))))
1266 ;; Interface to unseen messages script
1268 (defvar mh-flists-search-folders)
1270 ;; XXX: This should probably be in mh-utils.el and used in other places where
1271 ;; MH-E calls out to /bin/sh.
1272 (defun mh-index-quote-for-shell (string)
1273 "Quote STRING for /bin/sh."
1275 (loop for x across string
1276 concat (format (if (memq x '(?\\ ?` ?$)) "\\%c" "%c") x))
1279 (defun mh-flists-execute (&rest args)
1281 Search for messages belonging to `mh-flists-sequence' in the
1282 folders specified by `mh-flists-search-folders'. If
1283 `mh-recursive-folders-flag' is t, then the folders are searched
1284 recursively. All parameters ARGS are ignored."
1285 (set-buffer (get-buffer-create mh-index-temp-buffer))
1287 (unless (executable-find "sh")
1288 (error "Didn't find sh"))
1290 (let ((seq (symbol-name mh-flists-sequence)))
1291 (insert "for folder in `" (expand-file-name "flists" mh-progs) " "
1292 (cond ((eq mh-flists-search-folders t)
1293 (mh-index-quote-for-shell mh-inbox))
1294 ((eq mh-flists-search-folders nil) "")
1295 ((listp mh-flists-search-folders)
1296 (loop for folder in mh-flists-search-folders
1298 (concat " " (mh-index-quote-for-shell folder)))))
1299 (if mh-recursive-folders-flag " -recurse" "")
1300 " -sequence " seq " -noshowzero -fast` ; do\n"
1301 (expand-file-name "mhpath" mh-progs) " \"+$folder\" " seq "\n"
1303 (call-process-region
1304 (point-min) (point-max) "sh" nil (get-buffer mh-index-temp-buffer))))
1307 (defun mh-index-sequenced-messages (folders sequence)
1308 "Display messages from FOLDERS in SEQUENCE.
1309 All messages in the sequence you provide from the folders in
1310 `mh-new-messages-folders' are listed. With a prefix argument,
1311 enter a space-separated list of folders, or nothing to search all
1314 (list (if current-prefix-arg
1315 (split-string (read-string "Search folder(s) (default all): "))
1316 mh-new-messages-folders)
1317 (mh-read-seq-default "Search" nil)))
1318 (unless sequence (setq sequence mh-unseen-seq))
1319 (let* ((mh-flists-search-folders folders)
1320 (mh-flists-sequence sequence)
1321 (mh-flists-called-flag t)
1322 (mh-indexer 'flists)
1323 (mh-index-execute-search-function 'mh-flists-execute)
1324 (mh-index-next-result-function 'mh-mairix-next-result)
1325 (mh-mairix-folder mh-user-path)
1326 (mh-index-regexp-builder nil)
1327 (new-folder (format "%s/%s/%s" mh-index-folder
1328 mh-flists-results-folder sequence))
1329 (window-config (if (equal new-folder mh-current-folder)
1330 mh-previous-window-config
1331 (current-window-configuration)))
1334 (cond ((buffer-live-p (get-buffer new-folder))
1335 ;; The destination folder is being visited. Trick `mh-index-search'
1336 ;; into thinking that the folder resulted from a previous search.
1337 (set-buffer new-folder)
1338 (setq mh-index-previous-search (list folders sequence))
1340 ((mh-folder-exists-p new-folder)
1341 ;; Folder exists but we don't have it open. That means they are
1342 ;; stale results from a old flists search. Clear it out.
1343 (mh-exec-cmd-quiet nil "rmf" new-folder)))
1344 (setq message (mh-index-search redo-flag "+" mh-flists-results-folder
1346 mh-index-sequence-search-flag t
1347 mh-index-previous-search (list folders sequence))
1348 (mh-index-write-data)
1349 (when (stringp message) (message "%s" message))))
1352 (defun mh-index-new-messages (folders)
1353 "Display unseen messages.
1355 If you use a program such as \"procmail\" to use \"rcvstore\" to file
1356 your incoming mail automatically, you can display new, unseen,
1357 messages using this command. All messages in the \"unseen\"
1358 sequence from the folders in `mh-new-messages-folders' are
1361 With a prefix argument, enter a space-separated list of FOLDERS,
1362 or nothing to search all folders."
1364 (list (if current-prefix-arg
1365 (split-string (read-string "Search folder(s) (default all): "))
1366 mh-new-messages-folders)))
1367 (mh-index-sequenced-messages folders mh-unseen-seq))
1370 (defun mh-index-ticked-messages (folders)
1371 "Display ticked messages.
1373 All messages in `mh-tick-seq' from the folders in
1374 `mh-ticked-messages-folders' are listed.
1376 With a prefix argument, enter a space-separated list of FOLDERS,
1377 or nothing to search all folders."
1379 (list (if current-prefix-arg
1380 (split-string (read-string "Search folder(s) (default all): "))
1381 mh-ticked-messages-folders)))
1382 (mh-index-sequenced-messages folders mh-tick-seq))
1388 (defvar mh-swish-binary (executable-find "swish-e"))
1389 (defvar mh-swish-directory ".swish")
1390 (defvar mh-swish-folder nil)
1393 (defun mh-swish-execute-search (folder-path search-regexp)
1394 "Execute swish-e and read the results.
1396 In the examples below, replace \"/home/user/Mail\" with the path
1397 to your MH directory.
1399 First create the directory \"/home/user/Mail/.swish\". Then
1400 create the file \"/home/user/Mail/.swish/config\" with the
1403 DefaultContents TXT*
1404 IndexDir /home/user/Mail
1405 IndexFile /home/user/Mail/.swish/index
1406 IndexName \"Mail Index\"
1407 IndexDescription \"Mail Index\"
1408 IndexPointer \"http://nowhere\"
1409 IndexAdmin \"nobody\"
1410 #MetaNames automatic
1414 IgnoreTotalWordCountWhenRanking yes
1415 WordCharacters abcdefghijklmnopqrstuvwxyz0123456789-
1416 BeginCharacters abcdefghijklmnopqrstuvwxyz
1417 EndCharacters abcdefghijklmnopqrstuvwxyz0123456789
1420 FileRules filename contains \\D
1421 FileRules pathname contains /home/user/Mail/.swish
1422 FileRules pathname contains /home/user/Mail/mhe-index
1424 This configuration does not index the folders that hold the
1425 results of your searches in \"+mhe-index\" since they tend to be
1426 ephemeral and the original messages are indexed anyway.
1428 If there are any directories you would like to ignore, append
1429 lines like the following to \"config\":
1431 FileRules pathname contains /home/user/Mail/scripts
1433 Use the following command line to generate the swish index. Run
1434 this daily from cron:
1436 swish-e -c /home/user/Mail/.swish/config
1438 In a program, FOLDER-PATH is the directory in which SEARCH-REGEXP
1440 (set-buffer (get-buffer-create mh-index-temp-buffer))
1442 (unless mh-swish-binary
1443 (error "Set mh-swish-binary appropriately"))
1444 (call-process mh-swish-binary nil '(t nil) nil
1446 "-f" (format "%s%s/index" mh-user-path mh-swish-directory))
1447 (goto-char (point-min))
1448 (setq mh-swish-folder
1449 (let ((last-char (substring folder-path (1- (length folder-path)))))
1450 (if (equal last-char "/")
1452 (format "%s/" folder-path)))))
1454 (defun mh-swish-next-result ()
1455 "Get the next result from swish output."
1458 (when (or (eobp) (equal (char-after (point)) ?.))
1460 (when (equal (char-after (point)) ?#)
1462 (let* ((start (search-forward " " (line-end-position) t))
1463 (end (search-forward " " (line-end-position) t)))
1464 (unless (and start end)
1467 (unless (file-exists-p (buffer-substring-no-properties start end))
1469 (unless (search-backward "/" start t)
1471 (list (let* ((s (buffer-substring-no-properties start (1+ (point)))))
1472 (unless (string-match mh-swish-folder s)
1474 (if (and (string-match mh-user-path s)
1475 (< (match-end 0) (1- (length s))))
1477 (substring s (match-end 0) (1- (length s))))
1479 (let* ((s (buffer-substring-no-properties (1+ (point)) end))
1480 (val (ignore-errors (read-from-string s))))
1481 (if (and (consp val) (numberp (car val)))
1489 ;; Swish++ interface
1491 (defvar mh-swish++-binary (or (executable-find "search++")
1492 (executable-find "search")))
1493 (defvar mh-swish++-directory ".swish++")
1496 (defun mh-swish++-execute-search (folder-path search-regexp)
1497 "Execute swish++ and read the results.
1499 In the examples below, replace \"/home/user/Mail\" with the path to
1502 First create the directory \"/home/user/Mail/.swish++\". Then create
1503 the file \"/home/user/Mail/.swish++/swish++.conf\" with the following
1506 IncludeMeta Bcc Cc Comments Content-Description From Keywords
1507 IncludeMeta Newsgroups Resent-To Subject To
1508 IncludeMeta Message-Id References In-Reply-To
1510 IndexFile /home/user/Mail/.swish++/swish++.index
1512 Use the following command line to generate the swish index. Run
1513 this daily from cron:
1515 find /home/user/Mail -path /home/user/Mail/mhe-index -prune \\
1516 -o -path /home/user/Mail/.swish++ -prune \\
1517 -o -name \"[0-9]*\" -print \\
1518 | index -c /home/user/Mail/.swish++/swish++.conf -
1520 This command does not index the folders that hold the results of your
1521 searches in \"+mhe-index\" since they tend to be ephemeral and the
1522 original messages are indexed anyway.
1524 On some systems (Debian GNU/Linux, for example), use \"index++\"
1525 instead of \"index\".
1527 In a program, FOLDER-PATH is the directory in which SEARCH-REGEXP is
1529 (set-buffer (get-buffer-create mh-index-temp-buffer))
1531 (unless mh-swish++-binary
1532 (error "Set mh-swish++-binary appropriately"))
1533 (call-process mh-swish++-binary nil '(t nil) nil
1535 (format "-i%s%s/swish++.index"
1536 mh-user-path mh-swish++-directory)
1538 (goto-char (point-min))
1539 (setq mh-swish-folder
1540 (let ((last-char (substring folder-path (1- (length folder-path)))))
1541 (if (equal last-char "/")
1543 (format "%s/" folder-path)))))
1545 (defalias 'mh-swish++-next-result 'mh-swish-next-result)
1547 (defun mh-swish++-regexp-builder (regexp-list)
1548 "Generate query for swish++.
1549 REGEXP-LIST is an alist of fields and values."
1551 (dolist (elem regexp-list)
1553 (setq regexp (concat regexp " and "
1554 (if (car elem) "(" "")
1555 (if (car elem) (symbol-name (car elem)) "")
1556 (if (car elem) " = " "")
1557 (mh-swish++-print-regexp (cdr elem))
1558 (if (car elem) ")" "")))))
1559 (substring regexp 4)))
1561 (defun mh-swish++-print-regexp (expr)
1562 "Return infix expression corresponding to EXPR."
1563 (cond ((atom expr) (format "%s" expr))
1564 ((eq (car expr) 'not)
1565 (format "(not %s)" (mh-swish++-print-regexp (cadr expr))))
1566 (t (format "(%s %s %s)" (mh-swish++-print-regexp (cadr expr))
1567 (symbol-name (car expr))
1568 (mh-swish++-print-regexp (caddr expr))))))
1574 (defvar mh-namazu-binary (executable-find "namazu"))
1575 (defvar mh-namazu-directory ".namazu")
1576 (defvar mh-namazu-folder nil)
1579 (defun mh-namazu-execute-search (folder-path search-regexp)
1580 "Execute namazu and read the results.
1582 In the examples below, replace \"/home/user/Mail\" with the path to
1585 First create the directory \"/home/user/Mail/.namazu\". Then create
1586 the file \"/home/user/Mail/.namazu/mknmzrc\" with the following
1589 package conf; # Don't remove this line!
1590 $ADDRESS = 'user@localhost';
1591 $ALLOW_FILE = \"[0-9]*\";
1592 $EXCLUDE_PATH = \"^/home/user/Mail/(mhe-index|spam)\";
1594 This configuration does not index the folders that hold the results of
1595 your searches in \"+mhe-index\" since they tend to be ephemeral and
1596 the original messages are indexed anyway.
1598 Use the following command line to generate the namazu index. Run this
1601 mknmz -f /home/user/Mail/.namazu/mknmzrc -O /home/user/Mail/.namazu \\
1604 In a program, FOLDER-PATH is the directory in which SEARCH-REGEXP
1606 (let ((namazu-index-directory
1607 (format "%s%s" mh-user-path mh-namazu-directory)))
1608 (unless (file-exists-p namazu-index-directory)
1609 (error "Namazu directory %s not present" namazu-index-directory))
1610 (unless (executable-find mh-namazu-binary)
1611 (error "Set mh-namazu-binary appropriately"))
1612 (set-buffer (get-buffer-create mh-index-temp-buffer))
1614 (call-process mh-namazu-binary nil '(t nil) nil
1615 "-alR" search-regexp namazu-index-directory)
1616 (goto-char (point-min))
1617 (setq mh-namazu-folder
1618 (let ((last (substring folder-path (1- (length folder-path)))))
1619 (if (equal last "/")
1621 (format "%s/" folder-path))))))
1623 (defun mh-namazu-next-result ()
1624 "Get the next result from namazu output."
1627 (when (eobp) (return nil))
1628 (let ((file-name (buffer-substring-no-properties
1629 (point) (line-end-position))))
1630 (unless (equal (string-match mh-namazu-folder file-name) 0)
1632 (unless (file-exists-p file-name)
1634 (string-match mh-user-path file-name)
1635 (let* ((folder/msg (substring file-name (match-end 0)))
1636 (mark (mh-search-from-end ?/ folder/msg)))
1637 (unless mark (return 'error))
1638 (list (format "+%s" (substring folder/msg 0 mark))
1639 (let ((n (ignore-errors (read-from-string
1640 (substring folder/msg (1+ mark))))))
1641 (if (and (consp n) (numberp (car n)))
1650 (defun mh-index-choose ()
1651 "Choose an indexing function.
1652 The side-effects of this function are that the variables
1653 `mh-indexer', `mh-index-execute-search-function', and
1654 `mh-index-next-result-function' are set according to the first
1655 indexer in `mh-indexer-choices' present on the system."
1657 ;; The following favors the user's preference; otherwise, the last
1658 ;; automatically chosen indexer is used for efficiency rather than going
1659 ;; through the list.
1660 (let ((program-alist (cond (mh-index-program
1662 (assoc mh-index-program mh-indexer-choices)))
1664 (list (assoc mh-indexer mh-indexer-choices)))
1665 (t mh-indexer-choices))))
1666 (while program-alist
1667 (let* ((current (pop program-alist))
1668 (executable (symbol-value (cadr current))))
1670 (setq mh-indexer (car current))
1671 (setq mh-index-execute-search-function (nth 2 current))
1672 (setq mh-index-next-result-function (nth 3 current))
1673 (setq mh-index-regexp-builder (nth 4 current))
1674 (return mh-indexer))))
1682 ;; indent-tabs-mode: nil
1683 ;; sentence-end-double-space: nil
1686 ;; arch-tag: 607762ad-0dff-4fe1-a27e-6c0dde0dcc47
1687 ;;; mh-index ends here