1 ;;; mh-index -- MH-E interface to indexing programs
3 ;; Copyright (C) 2002, 2003, 2004, 2005, 2006 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 last search.
364 Unlike regular searches, the prompt for the folder to search can
365 be \"all\" to search all folders; in addition, the search works
366 recursively on the listed folder. The search criteria are entered
367 in an MH-Pick buffer as described in `mh-search-folder'.\\<mh-pick-mode-map>
369 To perform the search, type \\[mh-do-search]. Another difference
370 from the regular searches is that because the search operates on
371 more than one folder, the messages that are found are put in a
372 temporary sub-folder of \"+mhe-index\" and are displayed in an
373 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.\\<mh-folder-mode-map>
377 The appearance of the heading can be modified by customizing the
378 face `mh-index-folder'. You can jump back and forth between the
379 headings using the commands \\[mh-index-next-folder] and
380 \\[mh-index-previous-folder].
382 In addition, the command \\[mh-index-visit-folder] can be used to
383 visit the folder of the message at point. Initially, only the
384 messages that matched the search criteria are displayed in the
385 folder. While the temporary buffer has its own set of message
386 numbers, the actual messages numbers are shown in the visited
387 folder. Thus, the command \\[mh-index-visit-folder] is useful to
388 find the actual message number of an interesting message, or to
389 view surrounding messages with the command \\[mh-rescan-folder].
391 Because this folder is temporary, you'll probably get in the
392 habit of killing it when you're done with
395 If you have run the command \\[mh-search-folder], but change your
396 mind while entering the search criteria and actually want to run
397 an indexed search, then you can use the command
398 \\<mh-pick-mode-map>\\[mh-index-do-search] in the MH-Pick
399 buffer.\\<mh-folder-mode-map>
401 The command \\[mh-index-search] runs the command defined by the
402 option `mh-index-program'. The default value is \"Auto-detect\"
403 which means that MH-E will automatically choose one of
404 \"swish++\", \"swish-e\", \"mairix\", \"namazu\", \"pick\" and
405 \"grep\" in that order. If, for example, you have both
406 \"swish++\" and \"mairix\" installed and you want to use
407 \"mairix\", then you can set this option to \"mairix\".
411 The \"pick\" and \"grep\" commands do not perform a
412 recursive search on the given folder.
414 This command uses an \"X-MHE-Checksum:\" header field to cache
415 the MD5 checksum of a message. This means that if an incoming
416 message already contains an \"X-MHE-Checksum:\" field, that
417 message might not be found by this command. The following
418 \"procmail\" recipe avoids this problem by renaming the existing
422 | formail -R \"X-MHE-Checksum\" \"X-Old-MHE-Checksum\"
424 The documentation for the following commands describe how to set
425 up the various indexing programs to use with MH-E. The \"pick\"
426 and \"grep\" commands do not require additional configuration.
428 - `mh-swish++-execute-search'
429 - `mh-swish-execute-search'
430 - `mh-mairix-execute-search'
431 - `mh-namazu-execute-search'
432 - `mh-pick-execute-search'
433 - `mh-grep-execute-search'
435 In a program, if REDO-SEARCH-FLAG is non-nil and the current
436 folder buffer was generated by a index search, then the search is
437 repeated. Otherwise, FOLDER is searched with SEARCH-REGEXP and
438 the results are presented in an MH-E folder. If FOLDER is \"+\"
439 then mail in all folders are searched. Optional argument
440 WINDOW-CONFIG stores the window configuration that will be
441 restored after the user quits the folder containing the index
444 (list current-prefix-arg
446 (unless mh-find-path-run (mh-find-path))
447 (or (and current-prefix-arg mh-index-sequence-search-flag)
448 (and current-prefix-arg (car mh-index-previous-search))
449 (mh-prompt-for-folder "Search" "+" nil "all" t)))
451 ;; Yes, we do want to call mh-index-choose every time in case the
452 ;; user has switched the indexer manually.
453 (unless (mh-index-choose) (error "No indexing program found"))
454 (or (and current-prefix-arg (cadr mh-index-previous-search))
455 mh-index-regexp-builder
456 (read-string (format "%s regexp: "
458 (symbol-name mh-indexer))))))
460 (and current-prefix-arg (cadr mh-index-previous-search)))
461 mh-index-regexp-builder)
462 (current-window-configuration)
464 ;; Redoing a sequence search?
465 (when (and redo-search-flag mh-index-data mh-index-sequence-search-flag
466 (not mh-flists-called-flag))
467 (let ((mh-flists-called-flag t))
468 (apply #'mh-index-sequenced-messages mh-index-previous-search))
469 (return-from mh-index-search))
470 ;; We have fancy query parsing
471 (when (symbolp search-regexp)
472 (mh-search-folder folder window-config)
473 (setq mh-searching-function 'mh-index-do-search)
474 (return-from mh-index-search))
476 (let ((result-count 0)
477 (old-window-config (or window-config mh-previous-window-config))
478 (previous-search mh-index-previous-search)
479 (index-folder (format "%s/%s" mh-index-folder
480 (mh-index-generate-pretty-name search-regexp))))
481 ;; Create a new folder for the search results or recreate the old one...
482 (if (and redo-search-flag mh-index-previous-search)
483 (let ((buffer-name (buffer-name (current-buffer))))
484 (mh-process-or-undo-commands buffer-name)
485 (save-excursion (mh-exec-cmd-quiet nil "rmf" buffer-name))
486 (mh-exec-cmd-quiet nil "folder" "-create" "-fast" buffer-name)
487 (setq index-folder buffer-name))
488 (setq index-folder (mh-index-new-folder index-folder search-regexp)))
490 (let ((folder-path (format "%s%s" mh-user-path (substring folder 1)))
491 (folder-results-map (make-hash-table :test #'equal))
492 (origin-map (make-hash-table :test #'equal)))
493 ;; Run search program...
494 (message "Executing %s... " mh-indexer)
495 (funcall mh-index-execute-search-function folder-path search-regexp)
497 ;; Parse indexer output
498 (message "Processing %s output... " mh-indexer)
499 (goto-char (point-min))
500 (loop for next-result = (funcall mh-index-next-result-function)
502 do (unless (eq next-result 'error)
503 (unless (gethash (car next-result) folder-results-map)
504 (setf (gethash (car next-result) folder-results-map)
505 (make-hash-table :test #'equal)))
506 (setf (gethash (cadr next-result)
507 (gethash (car next-result) folder-results-map))
510 ;; Copy the search results over
511 (maphash #'(lambda (folder msgs)
512 (let ((cur (car (mh-translate-range folder "cur")))
513 (msgs (sort (loop for msg being the hash-keys of msgs
516 (mh-exec-cmd "refile" msgs "-src" folder
517 "-link" index-folder)
518 ;; Restore cur to old value, that refile changed
520 (mh-exec-cmd-quiet nil "mark" folder "-add" "-zero"
521 "-sequence" "cur" (format "%s" cur)))
522 (loop for msg in msgs
523 do (incf result-count)
524 (setf (gethash result-count origin-map)
525 (cons folder msg)))))
528 ;; Vist the results folder
529 (mh-visit-folder index-folder () (list folder-results-map origin-map))
531 (goto-char (point-min))
533 (mh-update-sequences)
536 ;; Update the speedbar, if needed
537 (when (mh-speed-flists-active-p)
538 (mh-speed-flists t mh-current-folder))
541 (when (or (and redo-search-flag previous-search) window-config)
542 (setq mh-previous-window-config old-window-config))
543 (setq mh-index-previous-search (list folder search-regexp))
545 ;; Write out data to disk
546 (unless mh-flists-called-flag (mh-index-write-data))
548 (message "%s found %s matches in %s folders"
549 (upcase-initials (symbol-name mh-indexer))
550 (loop for msg-hash being hash-values of mh-index-data
551 sum (hash-table-count msg-hash))
552 (loop for msg-hash being hash-values of mh-index-data
553 count (> (hash-table-count msg-hash) 0))))))
557 ;;; Functions to serialize index data...
559 (defun mh-index-write-data ()
560 "Write index data to file."
562 (unless (eq major-mode 'mh-folder-mode)
563 (error "Can't be called from folder in \"%s\"" major-mode))
564 (let ((data mh-index-data)
565 (msg-checksum-map mh-index-msg-checksum-map)
566 (checksum-origin-map mh-index-checksum-origin-map)
567 (previous-search mh-index-previous-search)
568 (sequence-search-flag mh-index-sequence-search-flag)
569 (outfile (concat buffer-file-name mh-index-data-file))
572 (with-temp-file outfile
573 (mh-index-write-hashtable
574 data (lambda (x) (loop for y being the hash-keys of x collect y)))
575 (mh-index-write-hashtable msg-checksum-map #'identity)
576 (mh-index-write-hashtable checksum-origin-map #'identity)
577 (pp previous-search (current-buffer)) (insert "\n")
578 (pp sequence-search-flag (current-buffer)) (insert "\n")))))
581 (defun mh-index-read-data ()
582 "Read index data from file."
584 (unless (eq major-mode 'mh-folder-mode)
585 (error "Can't be called from folder in \"%s\"" major-mode))
586 (let ((infile (concat buffer-file-name mh-index-data-file))
589 (insert-file-contents-literally infile)
590 (goto-char (point-min))
591 (setq t1 (mh-index-read-hashtable
593 (loop with table = (make-hash-table :test #'equal)
594 for x in data do (setf (gethash x table) t)
595 finally return table)))
596 t2 (mh-index-read-hashtable #'identity)
597 t3 (mh-index-read-hashtable #'identity)
598 t4 (read (current-buffer))
599 t5 (read (current-buffer))))
600 (setq mh-index-data t1
601 mh-index-msg-checksum-map t2
602 mh-index-checksum-origin-map t3
603 mh-index-previous-search t4
604 mh-index-sequence-search-flag t5))))
606 (defun mh-index-write-hashtable (table proc)
607 "Write TABLE to `current-buffer'.
608 PROC is used to serialize the values corresponding to the hash
610 (pp (loop for x being the hash-keys of table
611 collect (cons x (funcall proc (gethash x table))))
615 (defun mh-index-read-hashtable (proc)
616 "From BUFFER read a hash table serialized as a list.
617 PROC is used to convert the value to actual data."
618 (loop with table = (make-hash-table :test #'equal)
619 for pair in (read (current-buffer))
620 do (setf (gethash (car pair) table) (funcall proc (cdr pair)))
621 finally return table))
625 "Non-nil means that this folder was generated by an index search."
629 (defun mh-index-do-search ()
630 "Find messages that match the qualifications in the current pattern buffer."
632 (unless (mh-index-choose) (error "No indexing program found"))
633 (let* ((regexp-list (mh-pick-parse-search-buffer))
634 (pattern (funcall mh-index-regexp-builder regexp-list)))
636 (mh-index-search nil mh-current-folder pattern
637 mh-previous-window-config)
638 (error "No search terms"))))
641 (defun mh-index-parse-search-regexp (input-string)
642 "Construct parse tree for INPUT-STRING.
643 All occurrences of &, |, ! and ~ in INPUT-STRING are replaced by
644 AND, OR and NOT as appropriate. Then the resulting string is
648 (insert input-string)
650 (mh-replace-string "\t" " ")
652 (mh-replace-string " AND " " and ")
653 (mh-replace-string "&" " and ")
654 (mh-replace-string " -and " " and ")
656 (mh-replace-string " OR " " or ")
657 (mh-replace-string "|" " or ")
658 (mh-replace-string " -or " " or ")
660 (mh-replace-string " NOT " " not ")
661 (mh-replace-string "!" " not ")
662 (mh-replace-string "~" " not ")
663 (mh-replace-string " -not " " not ")
664 ;; synonyms of left brace
665 (mh-replace-string "(" " ( ")
666 (mh-replace-string " -lbrace " " ( ")
667 ;; synonyms of right brace
668 (mh-replace-string ")" " ) ")
669 (mh-replace-string " -rbrace " " ) ")
670 ;; get the normalized input
671 (setq input (format "( %s )" (buffer-substring (point-min) (point-max)))))
673 (let ((tokens (mh-index-add-implicit-ops (split-string input)))
677 (dolist (token tokens)
678 (cond ((equal token "(") (push 'paren op-stack))
679 ((equal token "not") (push 'not op-stack))
680 ((equal token "or") (push 'or op-stack))
681 ((equal token "and") (push 'and op-stack))
683 (multiple-value-setq (op-stack operand-stack)
684 (mh-index-evaluate op-stack operand-stack))
685 (when (eq (car op-stack) 'not)
686 (setq op-stack (cdr op-stack))
687 (push `(not ,(pop operand-stack)) operand-stack))
688 (when (eq (car op-stack) 'and)
689 (setq op-stack (cdr op-stack))
690 (setq oper1 (pop operand-stack))
691 (push `(and ,(pop operand-stack) ,oper1) operand-stack)))
692 ((eq (car op-stack) 'not)
693 (setq op-stack (cdr op-stack))
694 (push `(not ,token) operand-stack)
695 (when (eq (car op-stack) 'and)
696 (setq op-stack (cdr op-stack))
697 (setq oper1 (pop operand-stack))
698 (push `(and ,(pop operand-stack) ,oper1) operand-stack)))
699 ((eq (car op-stack) 'and)
700 (setq op-stack (cdr op-stack))
701 (push `(and ,(pop operand-stack) ,token) operand-stack))
702 (t (push token operand-stack))))
703 (prog1 (pop operand-stack)
704 (when (or op-stack operand-stack)
705 (error "Invalid regexp: %s" input))))))
707 (defun mh-index-add-implicit-ops (tokens)
708 "Add implicit operators in the list TOKENS."
713 (setq current (pop tokens))
714 (cond ((or (equal current ")") (equal current "and") (equal current "or"))
715 (setq literal-seen nil)
716 (push current result))
719 (setq literal-seen nil)
722 (push current result)
723 (unless (or (equal current "(") (equal current "not"))
724 (setq literal-seen t)))))
727 (defun mh-index-evaluate (op-stack operand-stack)
728 "Read expression till starting paren based on OP-STACK and OPERAND-STACK."
729 (block mh-index-evaluate
732 (setq op (pop op-stack))
733 (cond ((eq op 'paren)
734 (return-from mh-index-evaluate (values op-stack operand-stack)))
736 (push `(not ,(pop operand-stack)) operand-stack))
737 ((or (eq op 'and) (eq op 'or))
738 (setq oper1 (pop operand-stack))
739 (push `(,op ,(pop operand-stack) ,oper1) operand-stack))))
740 (error "Ran out of tokens"))))
743 (defun mh-index-next-folder (&optional backward-flag)
744 "Jump to the next folder marker.
746 With non-nil optional argument BACKWARD-FLAG, jump to the previous
749 (if (null mh-index-data)
750 (message "Only applicable in an MH-E index search buffer")
751 (let ((point (point)))
752 (forward-line (if backward-flag -1 1))
753 (cond ((if backward-flag
754 (re-search-backward "^+" (point-min) t)
755 (re-search-forward "^+" (point-max) t))
757 ((and (if backward-flag
758 (goto-char (point-max))
759 (goto-char (point-min)))
762 (re-search-backward "^+" (point-min) t)
763 (re-search-forward "^+" (point-max) t))
765 (t (goto-char point))))))
768 (defun mh-index-previous-folder ()
769 "Jump to the previous folder marker."
771 (mh-index-next-folder t))
773 (defun mh-folder-exists-p (folder)
774 "Check if FOLDER exists."
775 (and (mh-folder-name-p folder)
778 (mh-exec-cmd-output "folder" nil "-fast" "-nocreate" folder)
779 (goto-char (point-min))
782 (defun mh-msg-exists-p (msg folder)
783 "Check if MSG exists in FOLDER."
784 (file-exists-p (format "%s%s/%s" mh-user-path (substring folder 1) msg)))
786 (defun mh-index-new-folder (name search-regexp)
787 "Return a folder name based on NAME for search results of SEARCH-REGEXP.
789 If folder NAME already exists and was generated for the same
790 SEARCH-REGEXP then it is reused.
792 Otherwise if the folder NAME was generated from a different
793 search then check if NAME<2> can be used. Otherwise try NAME<3>.
794 This is repeated till we find a new folder name.
796 If the folder returned doesn't exist then it is created."
797 (unless (mh-folder-name-p name)
798 (error "The argument should be a valid MH folder name"))
801 for candidate = (if (equal i 1) name (format "%s<%s>" name i))
802 when (or (not (mh-folder-exists-p candidate))
803 (equal (mh-index-folder-search-regexp candidate)
806 ;; Do pending refiles/deletes...
807 (when (get-buffer chosen-name)
808 (mh-process-or-undo-commands chosen-name))
809 ;; Recreate folder...
810 (save-excursion (mh-exec-cmd-quiet nil "rmf" chosen-name))
811 (mh-exec-cmd-quiet nil "folder" "-create" "-fast" chosen-name)
812 (mh-remove-from-sub-folders-cache chosen-name)
813 (when (boundp 'mh-speed-folder-map)
814 (mh-speed-add-folder chosen-name))
817 (defun mh-index-folder-search-regexp (folder)
818 "If FOLDER was created by a index search, return the search regexp.
819 Return nil if FOLDER doesn't exist or the .mhe_index file is
823 (insert-file-contents
824 (format "%s%s/%s" mh-user-path (substring folder 1) mh-index-data-file))
825 (goto-char (point-min))
827 (cadr (read (current-buffer))))))
830 (defun mh-index-insert-folder-headers ()
831 "Annotate the search results with original folder names."
832 (let ((cur-msg (mh-get-msg-num nil))
833 (old-buffer-modified-flag (buffer-modified-p))
834 (buffer-read-only nil)
835 current-folder last-folder)
836 (goto-char (point-min))
838 (setq current-folder (car (gethash (gethash (mh-get-msg-num nil)
839 mh-index-msg-checksum-map)
840 mh-index-checksum-origin-map)))
841 (when (and current-folder (not (equal current-folder last-folder)))
842 (insert (if last-folder "\n" "") current-folder "\n")
843 (setq last-folder current-folder))
847 (mh-goto-msg cur-msg t))
848 (set-buffer-modified-p old-buffer-modified-flag))
849 (mh-index-create-imenu-index))
852 (defun mh-index-create-imenu-index ()
853 "Create alist of folder names and positions in index folder buffers."
855 (setq which-func-mode t)
857 (goto-char (point-min))
858 (while (re-search-forward "^+" nil t)
861 (push (cons (buffer-substring-no-properties
862 (point) (line-end-position))
863 (set-marker (make-marker) (point)))
865 (setq imenu--index-alist (nreverse alist)))))
868 (defun mh-index-group-by-folder ()
869 "Partition the messages based on source folder.
870 Returns an alist with the the folder names in the car and the cdr
871 being the list of messages originally from that folder."
873 (goto-char (point-min))
874 (let ((result-table (make-hash-table :test #'equal)))
875 (loop for msg being hash-keys of mh-index-msg-checksum-map
876 do (push msg (gethash (car (gethash
877 (gethash msg mh-index-msg-checksum-map)
878 mh-index-checksum-origin-map))
880 (loop for x being the hash-keys of result-table
881 collect (cons x (nreverse (gethash x result-table)))))))
884 (defun mh-index-delete-folder-headers ()
885 "Delete the folder headers."
886 (let ((cur-msg (mh-get-msg-num nil))
887 (old-buffer-modified-flag (buffer-modified-p))
888 (buffer-read-only nil))
889 (while (and (not cur-msg) (not (eobp)))
891 (setq cur-msg (mh-get-msg-num nil)))
892 (goto-char (point-min))
894 (if (or (char-equal (char-after) ?+) (char-equal (char-after) 10))
895 (delete-region (point) (progn (forward-line) (point)))
897 (when cur-msg (mh-goto-msg cur-msg t t))
898 (set-buffer-modified-p old-buffer-modified-flag)))
901 (defun mh-index-visit-folder ()
902 "Visit original folder from where the message at point was found."
904 (unless mh-index-data
905 (error "Not in an index folder"))
908 (cond ((and (bolp) (eolp))
909 (ignore-errors (forward-line -1))
910 (setq msg (mh-get-msg-num t)))
911 ((equal (char-after (line-beginning-position)) ?+)
912 (setq folder (buffer-substring-no-properties
913 (line-beginning-position) (line-end-position))))
914 (t (setq msg (mh-get-msg-num t)))))
916 (setq folder (car (gethash (gethash msg mh-index-msg-checksum-map)
917 mh-index-checksum-origin-map))))
918 (when (or (not (get-buffer folder))
919 (y-or-n-p (format "Reuse buffer displaying %s? " folder)))
921 folder (loop for x being the hash-keys of (gethash folder mh-index-data)
922 when (mh-msg-exists-p x folder) collect x)))))
924 (defun mh-index-match-checksum (msg folder checksum)
925 "Check if MSG in FOLDER has X-MHE-Checksum header value of CHECKSUM."
927 (mh-exec-cmd-output mh-scan-prog nil "-width" "80"
928 "-format" "%{x-mhe-checksum}\n" folder msg)
929 (goto-char (point-min))
930 (string-equal (buffer-substring-no-properties (point) (line-end-position))
933 (defun mh-index-matching-source-msgs (msgs &optional delete-from-index-data)
934 "Return a table of original messages and folders for messages in MSGS.
935 If optional argument DELETE-FROM-INDEX-DATA is non-nil, then each
936 of the messages, whose counter-part is found in some source
937 folder, is removed from `mh-index-data'."
938 (let ((table (make-hash-table :test #'equal)))
940 (let* ((checksum (gethash msg mh-index-msg-checksum-map))
941 (pair (gethash checksum mh-index-checksum-origin-map)))
942 (when (and checksum (car pair) (cdr pair)
943 (mh-index-match-checksum (cdr pair) (car pair) checksum))
944 (push (cdr pair) (gethash (car pair) table))
945 (when delete-from-index-data
946 (remhash (cdr pair) (gethash (car pair) mh-index-data))))))
950 (defun mh-index-execute-commands ()
951 "Delete/refile the actual messages.
952 The copies in the searched folder are then deleted/refiled to get
953 the desired result. Before deleting the messages we make sure
954 that the message being deleted is identical to the one that the
955 user has marked in the index buffer."
958 (mh-speed-flists-inhibit-flag t))
960 (lambda (folder msgs)
961 (push folder folders)
962 (if (not (get-buffer folder))
963 ;; If source folder not open, just delete the messages...
964 (apply #'mh-exec-cmd "rmm" folder (mh-coalesce-msg-list msgs))
965 ;; Otherwise delete the messages in the source buffer...
968 (let ((old-refile-list mh-refile-list)
969 (old-delete-list mh-delete-list))
970 (setq mh-refile-list nil
972 (unwind-protect (mh-execute-commands)
976 (loop for y in (cdr x)
977 unless (memq y msgs) collect y)))
980 (loop for x in old-delete-list
981 unless (memq x msgs) collect x))
982 (mh-set-folder-modified-p (mh-outstanding-commands-p))
983 (when (mh-outstanding-commands-p)
984 (mh-notate-deleted-and-refiled)))))))
985 (mh-index-matching-source-msgs (append (loop for x in mh-refile-list
992 (defun mh-index-add-to-sequence (seq msgs)
993 "Add to SEQ the messages in the list MSGS.
994 This function updates the source folder sequences. Also makes an
995 attempt to update the source folder buffer if we have it open."
996 ;; Don't need to do anything for cur
998 (when (and (not (memq seq (mh-unpropagated-sequences)))
999 (mh-valid-seq-p seq))
1001 (mh-speed-flists-inhibit-flag t))
1002 (maphash (lambda (folder msgs)
1003 (push folder folders)
1004 ;; Add messages to sequence in source folder...
1005 (apply #'mh-exec-cmd-quiet nil "mark" folder
1006 "-add" "-nozero" "-sequence" (symbol-name seq)
1007 (mapcar (lambda (x) (format "%s" x))
1008 (mh-coalesce-msg-list msgs)))
1009 ;; Update source folder buffer if we have it open...
1010 (when (get-buffer folder)
1013 (mh-put-msg-in-seq msgs seq))))
1014 (mh-index-matching-source-msgs msgs))
1018 (defun mh-index-delete-from-sequence (seq msgs)
1019 "Delete from SEQ the messages in MSGS.
1020 This function updates the source folder sequences. Also makes an
1021 attempt to update the source folder buffer if present."
1023 (when (and (not (memq seq (mh-unpropagated-sequences)))
1024 (mh-valid-seq-p seq))
1026 (mh-speed-flists-inhibit-flag t))
1027 (maphash (lambda (folder msgs)
1028 (push folder folders)
1029 ;; Remove messages from sequence in source folder...
1030 (apply #'mh-exec-cmd-quiet nil "mark" folder
1031 "-del" "-nozero" "-sequence" (symbol-name seq)
1032 (mapcar (lambda (x) (format "%s" x))
1033 (mh-coalesce-msg-list msgs)))
1034 ;; Update source folder buffer if we have it open...
1035 (when (get-buffer folder)
1038 (mh-delete-msg-from-seq msgs seq t))))
1039 (mh-index-matching-source-msgs msgs))
1046 (defvar mh-index-pick-folder)
1047 (defvar mh-pick-binary "pick")
1049 (defun mh-pick-execute-search (folder-path search-regexp)
1052 Unlike the other index search programs \"pick\" only searches
1053 messages present in the folder itself and does not descend into
1054 any sub-folders that may be present.
1056 In a program, FOLDER-PATH is the directory in which SEARCH-REGEXP
1058 (set-buffer (get-buffer-create mh-index-temp-buffer))
1060 (setq mh-index-pick-folder
1061 (concat "+" (substring folder-path (length mh-user-path))))
1062 (apply #'call-process (expand-file-name "pick" mh-progs) nil '(t nil) nil
1063 mh-index-pick-folder "-list" search-regexp)
1064 (goto-char (point-min)))
1066 (defun mh-pick-next-result ()
1067 "Return the next pick search result."
1069 (when (eobp) (return nil))
1070 (unless (re-search-forward "^[1-9][0-9]*$" (line-end-position) t)
1072 (list mh-index-pick-folder
1073 (car (read-from-string (buffer-substring-no-properties
1074 (line-beginning-position)
1075 (line-end-position))))
1083 (defvar mh-grep-binary (executable-find "grep"))
1085 (defun mh-grep-execute-search (folder-path search-regexp)
1086 "Execute grep and read the results.
1088 Unlike the other index search programs \"grep\" only searches
1089 messages present in the folder itself and does not descend into
1090 any sub-folders that may be present.
1092 In a program, FOLDER-PATH is the directory in which SEARCH-REGEXP
1094 (set-buffer (get-buffer-create mh-index-temp-buffer))
1096 (call-process mh-grep-binary nil '(t nil) nil
1097 "-i" "-r" search-regexp folder-path)
1098 (goto-char (point-min)))
1100 (defun mh-grep-next-result ()
1101 "Read the next result.
1102 Parse it and return the message folder, message index and the
1103 match. If no other matches left then return nil. If the current
1104 record is invalid return 'error."
1109 (let ((eol-pos (line-end-position))
1110 (bol-pos (line-beginning-position))
1111 folder-start msg-end)
1113 (unless (search-forward mh-user-path eol-pos t)
1115 (setq folder-start (point))
1116 (unless (search-forward ":" eol-pos t)
1118 (let ((match (buffer-substring-no-properties (point) eol-pos)))
1120 (setq msg-end (point))
1121 (unless (search-backward "/" folder-start t)
1123 (list (format "+%s" (buffer-substring-no-properties
1124 folder-start (point)))
1125 (let ((val (ignore-errors (read-from-string
1126 (buffer-substring-no-properties
1127 (1+ (point)) msg-end)))))
1128 (if (and (consp val) (integerp (car val)))
1138 (defvar mh-mairix-binary (executable-find "mairix"))
1139 (defvar mh-mairix-directory ".mairix")
1140 (defvar mh-mairix-folder nil)
1142 (defun mh-mairix-execute-search (folder-path search-regexp-list)
1143 "Execute mairix and read the results.
1145 In the examples below, replace \"/home/user/Mail\" with the path
1146 to your MH directory.
1148 First create the directory \"/home/user/Mail/.mairix\". Then
1149 create the file \"/home/user/Mail/.mairix/config\" with the
1152 base=/home/user/Mail
1154 # List of folders that should be indexed. 3 dots at the end means there
1155 # are subfolders within the folder
1156 mh=archive...:inbox:drafts:news:sent:trash
1159 database=/home/user/Mail/mairix/database
1161 Use the following command line to generate the mairix index. Run
1162 this daily from cron:
1164 mairix -f /home/user/Mail/.mairix/config
1166 In a program, FOLDER-PATH is the directory in which
1167 SEARCH-REGEXP-LIST is used to search."
1168 (set-buffer (get-buffer-create mh-index-temp-buffer))
1170 (unless mh-mairix-binary
1171 (error "Set `mh-mairix-binary' appropriately"))
1172 (apply #'call-process mh-mairix-binary nil '(t nil) nil
1173 "-r" "-f" (format "%s%s/config" mh-user-path mh-mairix-directory)
1175 (goto-char (point-min))
1176 (setq mh-mairix-folder
1177 (let ((last-char (substring folder-path (1- (length folder-path)))))
1178 (if (equal last-char "/")
1180 (format "%s/" folder-path)))))
1182 (defun mh-mairix-next-result ()
1183 "Return next result from mairix output."
1186 (when (or (eobp) (and (bolp) (eolp)))
1188 (unless (eq (char-after) ?/)
1190 (let ((start (point))
1192 (setq end (line-end-position))
1193 (unless (search-forward mh-mairix-folder end t)
1195 (goto-char (match-beginning 0))
1196 (unless (equal (point) start)
1199 (unless (search-backward "/" start t)
1201 (setq msg-start (1+ (point)))
1203 (unless (search-forward mh-user-path end t)
1205 (list (format "+%s" (buffer-substring-no-properties
1206 (point) (1- msg-start)))
1207 (car (read-from-string
1208 (buffer-substring-no-properties msg-start end)))
1212 (defun mh-mairix-regexp-builder (regexp-list)
1213 "Generate query for mairix.
1214 REGEXP-LIST is an alist of fields and values."
1216 (dolist (pair regexp-list)
1220 (cond ((eq (car pair) 'to) "t:")
1221 ((eq (car pair) 'from) "f:")
1222 ((eq (car pair) 'cc) "c:")
1223 ((eq (car pair) 'subject) "s:")
1224 ((eq (car pair) 'date) "d:")
1226 (let ((sop (cdr (mh-mairix-convert-to-sop* (cdr pair))))
1228 (dolist (conjunct sop)
1229 (let ((expr-list (cdr conjunct))
1231 (dolist (e expr-list)
1232 (setq expr-string (concat expr-string ","
1233 (if (atom e) "" "~")
1234 (if (atom e) e (cadr e)))))
1235 (setq final (concat final "/" (substring expr-string 1)))))
1236 (substring final 1)))
1240 (defun mh-mairix-convert-to-sop* (expr)
1241 "Convert EXPR to sum of product form."
1242 (cond ((atom expr) `(or (and ,expr)))
1243 ((eq (car expr) 'or)
1245 (loop for e in (mapcar #'mh-mairix-convert-to-sop* (cdr expr))
1247 ((eq (car expr) 'and)
1248 (let ((conjuncts (mapcar #'mh-mairix-convert-to-sop* (cdr expr)))
1250 (setq result (pop conjuncts))
1252 (setq next-factor (pop conjuncts))
1253 (setq result (let ((res ()))
1254 (dolist (t1 (cdr result))
1255 (dolist (t2 (cdr next-factor))
1256 (push `(and ,@(cdr t1) ,@(cdr t2)) res)))
1259 ((atom (cadr expr)) `(or (and ,expr)))
1260 ((eq (caadr expr) 'not) (mh-mairix-convert-to-sop* (cadadr expr)))
1261 ((eq (caadr expr) 'and) (mh-mairix-convert-to-sop*
1262 `(or ,@(mapcar #'(lambda (x) `(not ,x))
1264 ((eq (caadr expr) 'or) (mh-mairix-convert-to-sop*
1265 `(and ,@(mapcar #'(lambda (x) `(not ,x))
1267 (t (error "Unreachable: %s" expr))))
1271 ;; Interface to unseen messages script
1273 (defvar mh-flists-search-folders)
1275 ;; XXX: This should probably be in mh-utils.el and used in other places where
1276 ;; MH-E calls out to /bin/sh.
1277 (defun mh-index-quote-for-shell (string)
1278 "Quote STRING for /bin/sh."
1280 (loop for x across string
1281 concat (format (if (memq x '(?\\ ?` ?$)) "\\%c" "%c") x))
1284 (defun mh-flists-execute (&rest args)
1286 Search for messages belonging to `mh-flists-sequence' in the
1287 folders specified by `mh-flists-search-folders'. If
1288 `mh-recursive-folders-flag' is t, then the folders are searched
1289 recursively. All parameters ARGS are ignored."
1290 (set-buffer (get-buffer-create mh-index-temp-buffer))
1292 (unless (executable-find "sh")
1293 (error "Didn't find sh"))
1295 (let ((seq (symbol-name mh-flists-sequence)))
1296 (insert "for folder in `" (expand-file-name "flists" mh-progs) " "
1297 (cond ((eq mh-flists-search-folders t)
1298 (mh-index-quote-for-shell mh-inbox))
1299 ((eq mh-flists-search-folders nil) "")
1300 ((listp mh-flists-search-folders)
1301 (loop for folder in mh-flists-search-folders
1303 (concat " " (mh-index-quote-for-shell folder)))))
1304 (if mh-recursive-folders-flag " -recurse" "")
1305 " -sequence " seq " -noshowzero -fast` ; do\n"
1306 (expand-file-name "mhpath" mh-progs) " \"+$folder\" " seq "\n"
1308 (call-process-region
1309 (point-min) (point-max) "sh" nil (get-buffer mh-index-temp-buffer))))
1312 (defun mh-index-sequenced-messages (folders sequence)
1313 "Display messages in any sequence.
1315 All messages from the FOLDERS in `mh-new-messages-folders' in the
1316 SEQUENCE you provide are listed. With a prefix argument, enter a
1317 space-separated list of folders at the prompt, or nothing to
1318 search all folders."
1320 (list (if current-prefix-arg
1321 (split-string (read-string "Search folder(s) (default all): "))
1322 mh-new-messages-folders)
1323 (mh-read-seq-default "Search" nil)))
1324 (unless sequence (setq sequence mh-unseen-seq))
1325 (let* ((mh-flists-search-folders folders)
1326 (mh-flists-sequence sequence)
1327 (mh-flists-called-flag t)
1328 (mh-indexer 'flists)
1329 (mh-index-execute-search-function 'mh-flists-execute)
1330 (mh-index-next-result-function 'mh-mairix-next-result)
1331 (mh-mairix-folder mh-user-path)
1332 (mh-index-regexp-builder nil)
1333 (new-folder (format "%s/%s/%s" mh-index-folder
1334 mh-flists-results-folder sequence))
1335 (window-config (if (equal new-folder mh-current-folder)
1336 mh-previous-window-config
1337 (current-window-configuration)))
1340 (cond ((buffer-live-p (get-buffer new-folder))
1341 ;; The destination folder is being visited. Trick `mh-index-search'
1342 ;; into thinking that the folder resulted from a previous search.
1343 (set-buffer new-folder)
1344 (setq mh-index-previous-search (list folders sequence))
1346 ((mh-folder-exists-p new-folder)
1347 ;; Folder exists but we don't have it open. That means they are
1348 ;; stale results from a old flists search. Clear it out.
1349 (mh-exec-cmd-quiet nil "rmf" new-folder)))
1350 (setq message (mh-index-search redo-flag "+" mh-flists-results-folder
1352 mh-index-sequence-search-flag t
1353 mh-index-previous-search (list folders sequence))
1354 (mh-index-write-data)
1355 (when (stringp message) (message "%s" message))))
1358 (defun mh-index-new-messages (folders)
1359 "Display unseen messages.
1361 If you use a program such as \"procmail\" to use \"rcvstore\" to file
1362 your incoming mail automatically, you can display new, unseen,
1363 messages using this command. All messages in the \"unseen\"
1364 sequence from the folders in `mh-new-messages-folders' are
1367 With a prefix argument, enter a space-separated list of FOLDERS,
1368 or nothing to search all folders."
1370 (list (if current-prefix-arg
1371 (split-string (read-string "Search folder(s) (default all): "))
1372 mh-new-messages-folders)))
1373 (mh-index-sequenced-messages folders mh-unseen-seq))
1376 (defun mh-index-ticked-messages (folders)
1377 "Display ticked messages.
1379 All messages in `mh-tick-seq' from the folders in
1380 `mh-ticked-messages-folders' are listed.
1382 With a prefix argument, enter a space-separated list of FOLDERS,
1383 or nothing to search all folders."
1385 (list (if current-prefix-arg
1386 (split-string (read-string "Search folder(s) (default all): "))
1387 mh-ticked-messages-folders)))
1388 (mh-index-sequenced-messages folders mh-tick-seq))
1394 (defvar mh-swish-binary (executable-find "swish-e"))
1395 (defvar mh-swish-directory ".swish")
1396 (defvar mh-swish-folder nil)
1399 (defun mh-swish-execute-search (folder-path search-regexp)
1400 "Execute swish-e and read the results.
1402 In the examples below, replace \"/home/user/Mail\" with the path
1403 to your MH directory.
1405 First create the directory \"/home/user/Mail/.swish\". Then
1406 create the file \"/home/user/Mail/.swish/config\" with the
1409 DefaultContents TXT*
1410 IndexDir /home/user/Mail
1411 IndexFile /home/user/Mail/.swish/index
1412 IndexName \"Mail Index\"
1413 IndexDescription \"Mail Index\"
1414 IndexPointer \"http://nowhere\"
1415 IndexAdmin \"nobody\"
1416 #MetaNames automatic
1420 IgnoreTotalWordCountWhenRanking yes
1421 WordCharacters abcdefghijklmnopqrstuvwxyz0123456789-
1422 BeginCharacters abcdefghijklmnopqrstuvwxyz
1423 EndCharacters abcdefghijklmnopqrstuvwxyz0123456789
1426 FileRules filename contains \\D
1427 FileRules pathname contains /home/user/Mail/.swish
1428 FileRules pathname contains /home/user/Mail/mhe-index
1430 This configuration does not index the folders that hold the
1431 results of your searches in \"+mhe-index\" since they tend to be
1432 ephemeral and the original messages are indexed anyway.
1434 If there are any directories you would like to ignore, append
1435 lines like the following to \"config\":
1437 FileRules pathname contains /home/user/Mail/scripts
1439 Use the following command line to generate the swish index. Run
1440 this daily from cron:
1442 swish-e -c /home/user/Mail/.swish/config
1444 In a program, FOLDER-PATH is the directory in which SEARCH-REGEXP
1446 (set-buffer (get-buffer-create mh-index-temp-buffer))
1448 (unless mh-swish-binary
1449 (error "Set `mh-swish-binary' appropriately"))
1450 (call-process mh-swish-binary nil '(t nil) nil
1452 "-f" (format "%s%s/index" mh-user-path mh-swish-directory))
1453 (goto-char (point-min))
1454 (setq mh-swish-folder
1455 (let ((last-char (substring folder-path (1- (length folder-path)))))
1456 (if (equal last-char "/")
1458 (format "%s/" folder-path)))))
1460 (defun mh-swish-next-result ()
1461 "Get the next result from swish output."
1464 (when (or (eobp) (equal (char-after (point)) ?.))
1466 (when (equal (char-after (point)) ?#)
1468 (let* ((start (search-forward " " (line-end-position) t))
1469 (end (search-forward " " (line-end-position) t)))
1470 (unless (and start end)
1473 (unless (file-exists-p (buffer-substring-no-properties start end))
1475 (unless (search-backward "/" start t)
1477 (list (let* ((s (buffer-substring-no-properties start (1+ (point)))))
1478 (unless (string-match mh-swish-folder s)
1480 (if (and (string-match mh-user-path s)
1481 (< (match-end 0) (1- (length s))))
1483 (substring s (match-end 0) (1- (length s))))
1485 (let* ((s (buffer-substring-no-properties (1+ (point)) end))
1486 (val (ignore-errors (read-from-string s))))
1487 (if (and (consp val) (numberp (car val)))
1495 ;; Swish++ interface
1497 (defvar mh-swish++-binary (or (executable-find "search++")
1498 (executable-find "search")))
1499 (defvar mh-swish++-directory ".swish++")
1502 (defun mh-swish++-execute-search (folder-path search-regexp)
1503 "Execute swish++ and read the results.
1505 In the examples below, replace \"/home/user/Mail\" with the path to
1508 First create the directory \"/home/user/Mail/.swish++\". Then create
1509 the file \"/home/user/Mail/.swish++/swish++.conf\" with the following
1512 IncludeMeta Bcc Cc Comments Content-Description From Keywords
1513 IncludeMeta Newsgroups Resent-To Subject To
1514 IncludeMeta Message-Id References In-Reply-To
1516 IndexFile /home/user/Mail/.swish++/swish++.index
1518 Use the following command line to generate the swish index. Run
1519 this daily from cron:
1521 find /home/user/Mail -path /home/user/Mail/mhe-index -prune \\
1522 -o -path /home/user/Mail/.swish++ -prune \\
1523 -o -name \"[0-9]*\" -print \\
1524 | index -c /home/user/Mail/.swish++/swish++.conf -
1526 This command does not index the folders that hold the results of your
1527 searches in \"+mhe-index\" since they tend to be ephemeral and the
1528 original messages are indexed anyway.
1530 On some systems (Debian GNU/Linux, for example), use \"index++\"
1531 instead of \"index\".
1533 In a program, FOLDER-PATH is the directory in which SEARCH-REGEXP is
1535 (set-buffer (get-buffer-create mh-index-temp-buffer))
1537 (unless mh-swish++-binary
1538 (error "Set `mh-swish++-binary' appropriately"))
1539 (call-process mh-swish++-binary nil '(t nil) nil
1541 (format "-i%s%s/swish++.index"
1542 mh-user-path mh-swish++-directory)
1544 (goto-char (point-min))
1545 (setq mh-swish-folder
1546 (let ((last-char (substring folder-path (1- (length folder-path)))))
1547 (if (equal last-char "/")
1549 (format "%s/" folder-path)))))
1551 (defalias 'mh-swish++-next-result 'mh-swish-next-result)
1553 (defun mh-swish++-regexp-builder (regexp-list)
1554 "Generate query for swish++.
1555 REGEXP-LIST is an alist of fields and values."
1557 (dolist (elem regexp-list)
1559 (setq regexp (concat regexp " and "
1560 (if (car elem) "(" "")
1561 (if (car elem) (symbol-name (car elem)) "")
1562 (if (car elem) " = " "")
1563 (mh-swish++-print-regexp (cdr elem))
1564 (if (car elem) ")" "")))))
1565 (substring regexp 4)))
1567 (defun mh-swish++-print-regexp (expr)
1568 "Return infix expression corresponding to EXPR."
1569 (cond ((atom expr) (format "%s" expr))
1570 ((eq (car expr) 'not)
1571 (format "(not %s)" (mh-swish++-print-regexp (cadr expr))))
1572 (t (format "(%s %s %s)" (mh-swish++-print-regexp (cadr expr))
1573 (symbol-name (car expr))
1574 (mh-swish++-print-regexp (caddr expr))))))
1580 (defvar mh-namazu-binary (executable-find "namazu"))
1581 (defvar mh-namazu-directory ".namazu")
1582 (defvar mh-namazu-folder nil)
1585 (defun mh-namazu-execute-search (folder-path search-regexp)
1586 "Execute namazu and read the results.
1588 In the examples below, replace \"/home/user/Mail\" with the path to
1591 First create the directory \"/home/user/Mail/.namazu\". Then create
1592 the file \"/home/user/Mail/.namazu/mknmzrc\" with the following
1595 package conf; # Don't remove this line!
1596 $ADDRESS = 'user@localhost';
1597 $ALLOW_FILE = \"[0-9]*\";
1598 $EXCLUDE_PATH = \"^/home/user/Mail/(mhe-index|spam)\";
1600 This configuration does not index the folders that hold the results of
1601 your searches in \"+mhe-index\" since they tend to be ephemeral and
1602 the original messages are indexed anyway.
1604 Use the following command line to generate the namazu index. Run this
1607 mknmz -f /home/user/Mail/.namazu/mknmzrc -O /home/user/Mail/.namazu \\
1610 In a program, FOLDER-PATH is the directory in which SEARCH-REGEXP
1612 (let ((namazu-index-directory
1613 (format "%s%s" mh-user-path mh-namazu-directory)))
1614 (unless (file-exists-p namazu-index-directory)
1615 (error "Namazu directory %s not present" namazu-index-directory))
1616 (unless (executable-find mh-namazu-binary)
1617 (error "Set `mh-namazu-binary' appropriately"))
1618 (set-buffer (get-buffer-create mh-index-temp-buffer))
1620 (call-process mh-namazu-binary nil '(t nil) nil
1621 "-alR" search-regexp namazu-index-directory)
1622 (goto-char (point-min))
1623 (setq mh-namazu-folder
1624 (let ((last (substring folder-path (1- (length folder-path)))))
1625 (if (equal last "/")
1627 (format "%s/" folder-path))))))
1629 (defun mh-namazu-next-result ()
1630 "Get the next result from namazu output."
1633 (when (eobp) (return nil))
1634 (let ((file-name (buffer-substring-no-properties
1635 (point) (line-end-position))))
1636 (unless (equal (string-match mh-namazu-folder file-name) 0)
1638 (unless (file-exists-p file-name)
1640 (string-match mh-user-path file-name)
1641 (let* ((folder/msg (substring file-name (match-end 0)))
1642 (mark (mh-search-from-end ?/ folder/msg)))
1643 (unless mark (return 'error))
1644 (list (format "+%s" (substring folder/msg 0 mark))
1645 (let ((n (ignore-errors (read-from-string
1646 (substring folder/msg (1+ mark))))))
1647 (if (and (consp n) (numberp (car n)))
1656 (defun mh-index-choose ()
1657 "Choose an indexing function.
1658 The side-effects of this function are that the variables
1659 `mh-indexer', `mh-index-execute-search-function', and
1660 `mh-index-next-result-function' are set according to the first
1661 indexer in `mh-indexer-choices' present on the system."
1663 ;; The following favors the user's preference; otherwise, the last
1664 ;; automatically chosen indexer is used for efficiency rather than going
1665 ;; through the list.
1666 (let ((program-alist (cond (mh-index-program
1668 (assoc mh-index-program mh-indexer-choices)))
1670 (list (assoc mh-indexer mh-indexer-choices)))
1671 (t mh-indexer-choices))))
1672 (while program-alist
1673 (let* ((current (pop program-alist))
1674 (executable (symbol-value (cadr current))))
1676 (setq mh-indexer (car current))
1677 (setq mh-index-execute-search-function (nth 2 current))
1678 (setq mh-index-next-result-function (nth 3 current))
1679 (setq mh-index-regexp-builder (nth 4 current))
1680 (return mh-indexer))))
1688 ;; indent-tabs-mode: nil
1689 ;; sentence-end-double-space: nil
1692 ;; arch-tag: 607762ad-0dff-4fe1-a27e-6c0dde0dcc47
1693 ;;; mh-index ends here