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.
157 (defvar mh-index-max-cmdline-args
500
158 "Maximum number of command line args.")
160 (defun mh-index-execute (cmd &rest args
)
161 "Partial imitation of xargs.
162 The current buffer contains a list of strings, one on each line. The function
163 will execute CMD with ARGS and pass the first `mh-index-max-cmdline-args'
164 strings to it. This is repeated till 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 ORIGIN-MAP, if
188 non-nil, a hashtable containing which maps each message in the index folder to
189 the folder and message that it was copied from. The function updates the hash
190 tables `mh-index-msg-checksum-map' and `mh-index-checksum-origin-map'.
192 This function should only be called in the appropriate index folder buffer."
193 (cond ((and origin-map
(gethash checksum mh-index-checksum-origin-map
))
194 (let* ((intermediate (gethash msg origin-map
))
195 (ofolder (car intermediate
))
196 (omsg (cdr intermediate
)))
197 ;; This is most probably a duplicate. So eliminate it.
198 (call-process "rm" nil nil nil
199 (format "%s%s/%s" mh-user-path
200 (substring mh-current-folder
1) msg
))
201 (when (gethash ofolder mh-index-data
)
202 (remhash omsg
(gethash ofolder mh-index-data
)))))
204 (setf (gethash msg mh-index-msg-checksum-map
) checksum
)
206 (setf (gethash checksum mh-index-checksum-origin-map
)
207 (gethash msg origin-map
))))))
210 (defun mh-index-update-maps (folder &optional origin-map
)
211 "Annotate all as yet unannotated messages in FOLDER with their MD5 hash.
212 As a side effect msg -> checksum map is updated. Optional argument ORIGIN-MAP
213 is a hashtable which maps each message in the index folder to the original
214 folder and message from whence it was copied. If present the
215 checksum -> (origin-folder, origin-index) map is updated too."
216 (clrhash mh-index-msg-checksum-map
)
219 (set-buffer (get-buffer-create mh-checksum-buffer
))
221 ;; Run scan to check if any messages needs MD5 annotations at all
223 (mh-exec-cmd-output mh-scan-prog nil
"-width" "80"
224 "-format" "%(msg)\n%{x-mhe-checksum}\n"
226 (goto-char (point-min))
229 (setq msg
(buffer-substring-no-properties
230 (point) (line-end-position)))
233 (cond ((not (string-match "^[0-9]*$" msg
)))
235 ;; need to compute checksum
236 (set-buffer mh-checksum-buffer
)
237 (insert mh-user-path
(substring folder
1) "/" msg
"\n"))
240 (setq checksum
(buffer-substring-no-properties
241 (point) (line-end-position)))
242 (let ((msg (car (read-from-string msg
))))
244 (mh-index-update-single-msg msg checksum origin-map
)))))
246 ;; Run checksum program if needed
247 (unless (and (eobp) (bobp))
248 (apply #'mh-index-execute mh-checksum-cmd
)
249 (goto-char (point-min))
251 (let* ((intermediate (funcall mh-checksum-parser
))
252 (msg (car intermediate
))
253 (checksum (cdr intermediate
)))
256 (mh-exec-cmd "anno" folder msg
"-component" "X-MHE-Checksum"
257 "-nodate" "-text" checksum
"-inplace")
261 (mh-index-update-single-msg msg checksum origin-map
)))
263 (mh-index-write-data))
265 (defvar mh-unpropagated-sequences
'(cur range subject search
)
266 "List of sequences that aren't preserved.")
268 (defun mh-unpropagated-sequences ()
269 "Return a list of sequences that aren't propagated to the source folders.
270 It is just the sequences in the variable `mh-unpropagated-sequences' in
271 addition to the Previous-Sequence (see mh-profile 5)."
273 (cons mh-previous-seq mh-unpropagated-sequences
)
274 mh-unpropagated-sequences
))
277 (defun mh-create-sequence-map (seq-list)
278 "Return a map from msg number to list of sequences in which it is present.
279 SEQ-LIST is an assoc list whose keys are sequence names and whose cdr is the
280 list of messages in that sequence."
281 (loop with map
= (make-hash-table)
283 when
(and (not (memq (car seq
) (mh-unpropagated-sequences)))
284 (mh-valid-seq-p (car seq
)))
285 do
(loop for msg in
(cdr seq
)
286 do
(push (car seq
) (gethash msg map
)))
290 (defun mh-index-create-sequences ()
291 "Mirror sequences present in source folders in index folder."
292 (let ((seq-hash (make-hash-table :test
#'equal
))
294 (loop for folder being the hash-keys of mh-index-data
295 do
(setf (gethash folder seq-hash
)
296 (mh-create-sequence-map
297 (mh-read-folder-sequences folder nil
))))
298 (dolist (msg (mh-translate-range mh-current-folder
"all"))
299 (let* ((checksum (gethash msg mh-index-msg-checksum-map
))
300 (pair (gethash checksum mh-index-checksum-origin-map
))
303 (loop for seq in
(ignore-errors
304 (gethash omsg
(gethash ofolder seq-hash
)))
305 do
(if (assoc seq seq-list
)
306 (push msg
(cdr (assoc seq seq-list
)))
307 (push (list seq msg
) seq-list
)))))
308 (loop for seq in seq-list
309 do
(apply #'mh-exec-cmd
"mark" mh-current-folder
310 "-sequence" (symbol-name (car seq
)) "-add"
311 (mapcar #'(lambda (x) (format "%s" x
)) (cdr seq
))))))
313 (defvar mh-flists-results-folder
"sequence"
314 "Subfolder for `mh-index-folder' where flists output is placed.")
315 (defvar mh-flists-sequence
)
316 (defvar mh-flists-called-flag nil
)
318 (defun mh-index-generate-pretty-name (string)
319 "Given STRING generate a name which is suitable for use as a folder name.
320 White space from the beginning and end are removed. All spaces in the name are
321 replaced with underscores and all / are replaced with $. If STRING is longer
322 than 20 it is truncated too. STRING could be a list of strings in which case
323 they are concatenated to construct the base name."
327 (when (car string
) (insert (car string
)))
328 (dolist (s (cdr string
))
330 (setq string
(mh-replace-string "-lbrace" " "))
331 (setq string
(mh-replace-string "-rbrace" " "))
332 (subst-char-in-region (point-min) (point-max) ?
( ? t
)
333 (subst-char-in-region (point-min) (point-max) ?
) ? t
)
334 (subst-char-in-region (point-min) (point-max) ?- ? t
)
335 (goto-char (point-min))
336 (while (and (not (eobp)) (memq (char-after) '(? ?
\t ?
\n ?
\r ?_
)))
338 (goto-char (point-max))
339 (while (and (not (bobp)) (memq (char-before) '(? ?
\t ?
\n ?
\r ?_
)))
340 (delete-backward-char 1))
341 (subst-char-in-region (point-min) (point-max) ? ?_ t
)
342 (subst-char-in-region (point-min) (point-max) ?
\t ?_ t
)
343 (subst-char-in-region (point-min) (point-max) ?
\n ?_ t
)
344 (subst-char-in-region (point-min) (point-max) ?
\r ?_ t
)
345 (subst-char-in-region (point-min) (point-max) ?
/ ?$ t
)
346 (let ((out (truncate-string-to-width (buffer-string) 20)))
347 (cond ((eq mh-indexer
'flists
)
348 (format "%s/%s" mh-flists-results-folder mh-flists-sequence
))
349 ((equal out mh-flists-results-folder
) (concat out
"1"))
353 (defun* mh-index-search
(redo-search-flag folder search-regexp
354 &optional window-config
)
355 "Perform an indexed search in an MH mail folder.
356 Use a prefix argument to repeat the search.
358 Unlike regular searches, the prompt for the folder to search can be `all' to
359 search all folders; in addition, the search works recursively on the listed
360 folder. The search criteria are entered in an MH-Pick buffer as described in
363 To perform the search, type \\<mh-pick-mode-map>\\[mh-do-search]. Another
364 difference from the regular searches is that because the search operates on
365 more than one folder, the messages that are found are put in a temporary
366 sub-folder of `+mhe-index' and are displayed in an MH-Folder buffer. This
367 buffer is special because it displays messages from multiple folders; each set
368 of messages from a given folder has a heading with the folder name.
370 In addition, the \\<mh-folder-mode-map>\\[mh-index-visit-folder] command can
371 be used to visit the folder of the message at point. Initially, only the
372 messages that matched the search criteria are displayed in the folder. While
373 the temporary buffer has its own set of message numbers, the actual messages
374 numbers are shown in the visited folder. Thus, the \\[mh-index-visit-folder]
375 command is useful to find the actual message number of an interesting message,
376 or to view surrounding messages with the \\[mh-rescan-folder] command.
378 Because this folder is temporary, you'll probably get in the habit of killing
379 it when you're done with \\[mh-kill-folder].
381 If you have run the \\[mh-search-folder] command, but change your mind while
382 entering the search criteria and actually want to run an indexed search, then
383 you can use the \\<mh-pick-mode-map>\\[mh-index-do-search] command in the
386 The \\<mh-folder-mode-map>\\[mh-index-search] command runs the command defined
387 by the `mh-index-program' option. The default value is \"Auto-detect\" which
388 means that MH-E will automatically choose one of \"swish++\", \"swish-e\",
389 \"mairix\", \"namazu\", \"pick\" and \"grep\" in that order. If, for example,
390 you have both \"swish++\" and \"mairix\" installed and you want to use
391 \"mairix\", then you can set this option to \"mairix\".
395 The \"pick\" and \"grep\" commands do not perform a recursive search on
398 This command uses an \"X-MHE-Checksum:\" header field to cache the MD5
399 checksum of a message. This means that if an incoming message already contains
400 an \"X-MHE-Checksum:\" field, that message might not be found by this command.
401 The following \"procmail\" recipe avoids this problem by renaming the existing
405 | formail -R \"X-MHE-Checksum\" \"X-Old-MHE-Checksum\"
407 The documentation for the following commands describe how to set up the
408 various indexing programs to use with MH-E. The \"pick\" and \"grep\" commands
409 do not require additional configuration.
411 - `mh-swish++-execute-search'
412 - `mh-swish-execute-search'
413 - `mh-mairix-execute-search'
414 - `mh-namazu-execute-search'
415 - `mh-pick-execute-search'
416 - `mh-grep-execute-search'
418 In a program, if REDO-SEARCH-FLAG is non-nil and the current folder buffer was
419 generated by a index search, then the search is repeated. Otherwise, FOLDER is
420 searched with SEARCH-REGEXP and the results are presented in an MH-E folder.
421 If FOLDER is \"+\" then mail in all folders are searched. Optional argument
422 WINDOW-CONFIG stores the window configuration that will be restored after the
423 user quits the folder containing the index search results."
425 (list current-prefix-arg
427 (unless mh-find-path-run
(mh-find-path))
428 (or (and current-prefix-arg mh-index-sequence-search-flag
)
429 (and current-prefix-arg
(car mh-index-previous-search
))
430 (mh-prompt-for-folder "Search" "+" nil
"all" t
)))
432 ;; Yes, we do want to call mh-index-choose every time in case the
433 ;; user has switched the indexer manually.
434 (unless (mh-index-choose) (error "No indexing program found"))
435 (or (and current-prefix-arg
(cadr mh-index-previous-search
))
436 mh-index-regexp-builder
437 (read-string (format "%s regexp: "
439 (symbol-name mh-indexer
))))))
441 (and current-prefix-arg
(cadr mh-index-previous-search
)))
442 mh-index-regexp-builder
)
443 (current-window-configuration)
445 ;; Redoing a sequence search?
446 (when (and redo-search-flag mh-index-data mh-index-sequence-search-flag
447 (not mh-flists-called-flag
))
448 (let ((mh-flists-called-flag t
))
449 (apply #'mh-index-sequenced-messages mh-index-previous-search
))
450 (return-from mh-index-search
))
451 ;; We have fancy query parsing
452 (when (symbolp search-regexp
)
453 (mh-search-folder folder window-config
)
454 (setq mh-searching-function
'mh-index-do-search
)
455 (return-from mh-index-search
))
457 (let ((result-count 0)
458 (old-window-config (or window-config mh-previous-window-config
))
459 (previous-search mh-index-previous-search
)
460 (index-folder (format "%s/%s" mh-index-folder
461 (mh-index-generate-pretty-name search-regexp
))))
462 ;; Create a new folder for the search results or recreate the old one...
463 (if (and redo-search-flag mh-index-previous-search
)
464 (let ((buffer-name (buffer-name (current-buffer))))
465 (mh-process-or-undo-commands buffer-name
)
466 (save-excursion (mh-exec-cmd-quiet nil
"rmf" buffer-name
))
467 (mh-exec-cmd-quiet nil
"folder" "-create" "-fast" buffer-name
)
468 (setq index-folder buffer-name
))
469 (setq index-folder
(mh-index-new-folder index-folder search-regexp
)))
471 (let ((folder-path (format "%s%s" mh-user-path
(substring folder
1)))
472 (folder-results-map (make-hash-table :test
#'equal
))
473 (origin-map (make-hash-table :test
#'equal
)))
474 ;; Run search program...
475 (message "Executing %s... " mh-indexer
)
476 (funcall mh-index-execute-search-function folder-path search-regexp
)
478 ;; Parse indexer output
479 (message "Processing %s output... " mh-indexer
)
480 (goto-char (point-min))
481 (loop for next-result
= (funcall mh-index-next-result-function
)
483 do
(unless (eq next-result
'error
)
484 (unless (gethash (car next-result
) folder-results-map
)
485 (setf (gethash (car next-result
) folder-results-map
)
486 (make-hash-table :test
#'equal
)))
487 (setf (gethash (cadr next-result
)
488 (gethash (car next-result
) folder-results-map
))
491 ;; Copy the search results over
492 (maphash #'(lambda (folder msgs
)
493 (let ((cur (car (mh-translate-range folder
"cur")))
494 (msgs (sort (loop for msg being the hash-keys of msgs
497 (mh-exec-cmd "refile" msgs
"-src" folder
498 "-link" index-folder
)
499 ;; Restore cur to old value, that refile changed
501 (mh-exec-cmd-quiet nil
"mark" folder
"-add" "-zero"
502 "-sequence" "cur" (format "%s" cur
)))
503 (loop for msg in msgs
504 do
(incf result-count
)
505 (setf (gethash result-count origin-map
)
506 (cons folder msg
)))))
509 ;; Vist the results folder
510 (mh-visit-folder index-folder
() (list folder-results-map origin-map
))
512 (goto-char (point-min))
514 (mh-update-sequences)
517 ;; Update the speedbar, if needed
518 (when (mh-speed-flists-active-p)
519 (mh-speed-flists t mh-current-folder
))
522 (when (or (and redo-search-flag previous-search
) window-config
)
523 (setq mh-previous-window-config old-window-config
))
524 (setq mh-index-previous-search
(list folder search-regexp
))
526 ;; Write out data to disk
527 (unless mh-flists-called-flag
(mh-index-write-data))
529 (message "%s found %s matches in %s folders"
530 (upcase-initials (symbol-name mh-indexer
))
531 (loop for msg-hash being hash-values of mh-index-data
532 sum
(hash-table-count msg-hash
))
533 (loop for msg-hash being hash-values of mh-index-data
534 count
(> (hash-table-count msg-hash
) 0))))))
538 ;;; Functions to serialize index data...
540 (defun mh-index-write-data ()
541 "Write index data to file."
543 (unless (eq major-mode
'mh-folder-mode
)
544 (error "Can't be called from folder in `%s'" major-mode
))
545 (let ((data mh-index-data
)
546 (msg-checksum-map mh-index-msg-checksum-map
)
547 (checksum-origin-map mh-index-checksum-origin-map
)
548 (previous-search mh-index-previous-search
)
549 (sequence-search-flag mh-index-sequence-search-flag
)
550 (outfile (concat buffer-file-name mh-index-data-file
))
553 (with-temp-file outfile
554 (mh-index-write-hashtable
555 data
(lambda (x) (loop for y being the hash-keys of x collect y
)))
556 (mh-index-write-hashtable msg-checksum-map
#'identity
)
557 (mh-index-write-hashtable checksum-origin-map
#'identity
)
558 (pp previous-search
(current-buffer)) (insert "\n")
559 (pp sequence-search-flag
(current-buffer)) (insert "\n")))))
562 (defun mh-index-read-data ()
563 "Read index data from file."
565 (unless (eq major-mode
'mh-folder-mode
)
566 (error "Can't be called from folder in `%s'" major-mode
))
567 (let ((infile (concat buffer-file-name mh-index-data-file
))
570 (insert-file-contents-literally infile
)
571 (goto-char (point-min))
572 (setq t1
(mh-index-read-hashtable
574 (loop with table
= (make-hash-table :test
#'equal
)
575 for x in data do
(setf (gethash x table
) t
)
576 finally return table
)))
577 t2
(mh-index-read-hashtable #'identity
)
578 t3
(mh-index-read-hashtable #'identity
)
579 t4
(read (current-buffer))
580 t5
(read (current-buffer))))
581 (setq mh-index-data t1
582 mh-index-msg-checksum-map t2
583 mh-index-checksum-origin-map t3
584 mh-index-previous-search t4
585 mh-index-sequence-search-flag t5
))))
587 (defun mh-index-write-hashtable (table proc
)
588 "Write TABLE to `current-buffer'.
589 PROC is used to serialize the values corresponding to the hash table keys."
590 (pp (loop for x being the hash-keys of table
591 collect
(cons x
(funcall proc
(gethash x table
))))
595 (defun mh-index-read-hashtable (proc)
596 "From BUFFER read a hash table serialized as a list.
597 PROC is used to convert the value to actual data."
598 (loop with table
= (make-hash-table :test
#'equal
)
599 for pair in
(read (current-buffer))
600 do
(setf (gethash (car pair
) table
) (funcall proc
(cdr pair
)))
601 finally return table
))
605 "Non-nil means that this folder was generated by an index search."
609 (defun mh-index-do-search ()
610 "Construct appropriate regexp and call `mh-index-search'."
612 (unless (mh-index-choose) (error "No indexing program found"))
613 (let* ((regexp-list (mh-pick-parse-search-buffer))
614 (pattern (funcall mh-index-regexp-builder regexp-list
)))
616 (mh-index-search nil mh-current-folder pattern
617 mh-previous-window-config
)
618 (error "No search terms"))))
621 (defun mh-index-parse-search-regexp (input-string)
622 "Construct parse tree for INPUT-STRING.
623 All occurrences of &, |, ! and ~ in INPUT-STRING are replaced by AND, OR and
624 NOT as appropriate. Then the resulting string is parsed."
627 (insert input-string
)
629 (mh-replace-string "\t" " ")
631 (mh-replace-string " AND " " and ")
632 (mh-replace-string "&" " and ")
633 (mh-replace-string " -and " " and ")
635 (mh-replace-string " OR " " or ")
636 (mh-replace-string "|" " or ")
637 (mh-replace-string " -or " " or ")
639 (mh-replace-string " NOT " " not ")
640 (mh-replace-string "!" " not ")
641 (mh-replace-string "~" " not ")
642 (mh-replace-string " -not " " not ")
643 ;; synonyms of left brace
644 (mh-replace-string "(" " ( ")
645 (mh-replace-string " -lbrace " " ( ")
646 ;; synonyms of right brace
647 (mh-replace-string ")" " ) ")
648 (mh-replace-string " -rbrace " " ) ")
649 ;; get the normalized input
650 (setq input
(format "( %s )" (buffer-substring (point-min) (point-max)))))
652 (let ((tokens (mh-index-add-implicit-ops (split-string input
)))
656 (dolist (token tokens
)
657 (cond ((equal token
"(") (push 'paren op-stack
))
658 ((equal token
"not") (push 'not op-stack
))
659 ((equal token
"or") (push 'or op-stack
))
660 ((equal token
"and") (push 'and op-stack
))
662 (multiple-value-setq (op-stack operand-stack
)
663 (mh-index-evaluate op-stack operand-stack
))
664 (when (eq (car op-stack
) 'not
)
665 (setq op-stack
(cdr op-stack
))
666 (push `(not ,(pop operand-stack
)) operand-stack
))
667 (when (eq (car op-stack
) 'and
)
668 (setq op-stack
(cdr op-stack
))
669 (setq oper1
(pop operand-stack
))
670 (push `(and ,(pop operand-stack
) ,oper1
) operand-stack
)))
671 ((eq (car op-stack
) 'not
)
672 (setq op-stack
(cdr op-stack
))
673 (push `(not ,token
) operand-stack
)
674 (when (eq (car op-stack
) 'and
)
675 (setq op-stack
(cdr op-stack
))
676 (setq oper1
(pop operand-stack
))
677 (push `(and ,(pop operand-stack
) ,oper1
) operand-stack
)))
678 ((eq (car op-stack
) 'and
)
679 (setq op-stack
(cdr op-stack
))
680 (push `(and ,(pop operand-stack
) ,token
) operand-stack
))
681 (t (push token operand-stack
))))
682 (prog1 (pop operand-stack
)
683 (when (or op-stack operand-stack
)
684 (error "Invalid regexp: %s" input
))))))
686 (defun mh-index-add-implicit-ops (tokens)
687 "Add implicit operators in the list TOKENS."
692 (setq current
(pop tokens
))
693 (cond ((or (equal current
")") (equal current
"and") (equal current
"or"))
694 (setq literal-seen nil
)
695 (push current result
))
698 (setq literal-seen nil
)
701 (push current result
)
702 (unless (or (equal current
"(") (equal current
"not"))
703 (setq literal-seen t
)))))
706 (defun mh-index-evaluate (op-stack operand-stack
)
707 "Read expression till starting paren based on OP-STACK and OPERAND-STACK."
708 (block mh-index-evaluate
711 (setq op
(pop op-stack
))
712 (cond ((eq op
'paren
)
713 (return-from mh-index-evaluate
(values op-stack operand-stack
)))
715 (push `(not ,(pop operand-stack
)) operand-stack
))
716 ((or (eq op
'and
) (eq op
'or
))
717 (setq oper1
(pop operand-stack
))
718 (push `(,op
,(pop operand-stack
) ,oper1
) operand-stack
))))
719 (error "Ran out of tokens"))))
722 (defun mh-index-next-folder (&optional backward-flag
)
723 "Jump to the next folder marker.
724 The function is only applicable to folders displaying index search results.
725 With non-nil optional argument BACKWARD-FLAG, jump to the previous group of
728 (if (null mh-index-data
)
729 (message "Only applicable in an MH-E index search buffer")
730 (let ((point (point)))
731 (forward-line (if backward-flag -
1 1))
732 (cond ((if backward-flag
733 (re-search-backward "^+" (point-min) t
)
734 (re-search-forward "^+" (point-max) t
))
736 ((and (if backward-flag
737 (goto-char (point-max))
738 (goto-char (point-min)))
741 (re-search-backward "^+" (point-min) t
)
742 (re-search-forward "^+" (point-max) t
))
744 (t (goto-char point
))))))
747 (defun mh-index-previous-folder ()
748 "Jump to the previous folder marker."
750 (mh-index-next-folder t
))
752 (defun mh-folder-exists-p (folder)
753 "Check if FOLDER exists."
754 (and (mh-folder-name-p folder
)
757 (mh-exec-cmd-output "folder" nil
"-fast" "-nocreate" folder
)
758 (goto-char (point-min))
761 (defun mh-msg-exists-p (msg folder
)
762 "Check if MSG exists in FOLDER."
763 (file-exists-p (format "%s%s/%s" mh-user-path
(substring folder
1) msg
)))
765 (defun mh-index-new-folder (name search-regexp
)
766 "Return a folder name based on NAME for search results of SEARCH-REGEXP.
768 If folder NAME already exists and was generated for the same SEARCH-REGEXP
771 Otherwise if the folder NAME was generated from a different search then check
772 if NAME<2> can be used. Otherwise try NAME<3>. This is repeated till we find a
775 If the folder returned doesn't exist then it is created."
776 (unless (mh-folder-name-p name
)
777 (error "The argument should be a valid MH folder name"))
780 for candidate
= (if (equal i
1) name
(format "%s<%s>" name i
))
781 when
(or (not (mh-folder-exists-p candidate
))
782 (equal (mh-index-folder-search-regexp candidate
)
785 ;; Do pending refiles/deletes...
786 (when (get-buffer chosen-name
)
787 (mh-process-or-undo-commands chosen-name
))
788 ;; Recreate folder...
789 (save-excursion (mh-exec-cmd-quiet nil
"rmf" chosen-name
))
790 (mh-exec-cmd-quiet nil
"folder" "-create" "-fast" chosen-name
)
791 (mh-remove-from-sub-folders-cache chosen-name
)
792 (when (boundp 'mh-speed-folder-map
)
793 (mh-speed-add-folder chosen-name
))
796 (defun mh-index-folder-search-regexp (folder)
797 "If FOLDER was created by a index search, return the search regexp.
798 Return nil if FOLDER doesn't exist or the .mhe_index file is garbled."
801 (insert-file-contents
802 (format "%s%s/%s" mh-user-path
(substring folder
1) mh-index-data-file
))
803 (goto-char (point-min))
805 (cadr (read (current-buffer))))))
808 (defun mh-index-insert-folder-headers ()
809 "Annotate the search results with original folder names."
810 (let ((cur-msg (mh-get-msg-num nil
))
811 (old-buffer-modified-flag (buffer-modified-p))
812 (buffer-read-only nil
)
813 current-folder last-folder
)
814 (goto-char (point-min))
816 (setq current-folder
(car (gethash (gethash (mh-get-msg-num nil
)
817 mh-index-msg-checksum-map
)
818 mh-index-checksum-origin-map
)))
819 (when (and current-folder
(not (equal current-folder last-folder
)))
820 (insert (if last-folder
"\n" "") current-folder
"\n")
821 (setq last-folder current-folder
))
825 (mh-goto-msg cur-msg t
))
826 (set-buffer-modified-p old-buffer-modified-flag
))
827 (mh-index-create-imenu-index))
830 (defun mh-index-create-imenu-index ()
831 "Create alist of folder names and positions in index folder buffers."
833 (setq which-func-mode t
)
835 (goto-char (point-min))
836 (while (re-search-forward "^+" nil t
)
839 (push (cons (buffer-substring-no-properties
840 (point) (line-end-position))
841 (set-marker (make-marker) (point)))
843 (setq imenu--index-alist
(nreverse alist
)))))
846 (defun mh-index-group-by-folder ()
847 "Partition the messages based on source folder.
848 Returns an alist with the the folder names in the car and the cdr being the
849 list of messages originally from that folder."
851 (goto-char (point-min))
852 (let ((result-table (make-hash-table :test
#'equal
)))
853 (loop for msg being hash-keys of mh-index-msg-checksum-map
854 do
(push msg
(gethash (car (gethash
855 (gethash msg mh-index-msg-checksum-map
)
856 mh-index-checksum-origin-map
))
858 (loop for x being the hash-keys of result-table
859 collect
(cons x
(nreverse (gethash x result-table
)))))))
862 (defun mh-index-delete-folder-headers ()
863 "Delete the folder headers."
864 (let ((cur-msg (mh-get-msg-num nil
))
865 (old-buffer-modified-flag (buffer-modified-p))
866 (buffer-read-only nil
))
867 (while (and (not cur-msg
) (not (eobp)))
869 (setq cur-msg
(mh-get-msg-num nil
)))
870 (goto-char (point-min))
872 (if (or (char-equal (char-after) ?
+) (char-equal (char-after) 10))
873 (delete-region (point) (progn (forward-line) (point)))
875 (when cur-msg
(mh-goto-msg cur-msg t t
))
876 (set-buffer-modified-p old-buffer-modified-flag
)))
879 (defun mh-index-visit-folder ()
880 "Visit original folder from where the message at point was found."
882 (unless mh-index-data
883 (error "Not in an index folder"))
886 (cond ((and (bolp) (eolp))
887 (ignore-errors (forward-line -
1))
888 (setq msg
(mh-get-msg-num t
)))
889 ((equal (char-after (line-beginning-position)) ?
+)
890 (setq folder
(buffer-substring-no-properties
891 (line-beginning-position) (line-end-position))))
892 (t (setq msg
(mh-get-msg-num t
)))))
894 (setq folder
(car (gethash (gethash msg mh-index-msg-checksum-map
)
895 mh-index-checksum-origin-map
))))
896 (when (or (not (get-buffer folder
))
897 (y-or-n-p (format "Reuse buffer displaying %s? " folder
)))
899 folder
(loop for x being the hash-keys of
(gethash folder mh-index-data
)
900 when
(mh-msg-exists-p x folder
) collect x
)))))
902 (defun mh-index-match-checksum (msg folder checksum
)
903 "Check if MSG in FOLDER has X-MHE-Checksum header value of CHECKSUM."
905 (mh-exec-cmd-output mh-scan-prog nil
"-width" "80"
906 "-format" "%{x-mhe-checksum}\n" folder msg
)
907 (goto-char (point-min))
908 (string-equal (buffer-substring-no-properties (point) (line-end-position))
911 (defun mh-index-matching-source-msgs (msgs &optional delete-from-index-data
)
912 "Return a table of original messages and folders for messages in MSGS.
913 If optional argument DELETE-FROM-INDEX-DATA is non-nil, then each of the
914 messages, whose counter-part is found in some source folder, is removed from
916 (let ((table (make-hash-table :test
#'equal
)))
918 (let* ((checksum (gethash msg mh-index-msg-checksum-map
))
919 (pair (gethash checksum mh-index-checksum-origin-map
)))
920 (when (and checksum
(car pair
) (cdr pair
)
921 (mh-index-match-checksum (cdr pair
) (car pair
) checksum
))
922 (push (cdr pair
) (gethash (car pair
) table
))
923 (when delete-from-index-data
924 (remhash (cdr pair
) (gethash (car pair
) mh-index-data
))))))
928 (defun mh-index-execute-commands ()
929 "Delete/refile the actual messages.
930 The copies in the searched folder are then deleted/refiled to get the desired
931 result. Before deleting the messages we make sure that the message being
932 deleted is identical to the one that the user has marked in the index buffer."
935 (mh-speed-flists-inhibit-flag t
))
937 (lambda (folder msgs
)
938 (push folder folders
)
939 (if (not (get-buffer folder
))
940 ;; If source folder not open, just delete the messages...
941 (apply #'mh-exec-cmd
"rmm" folder
(mh-coalesce-msg-list msgs
))
942 ;; Otherwise delete the messages in the source buffer...
945 (let ((old-refile-list mh-refile-list
)
946 (old-delete-list mh-delete-list
))
947 (setq mh-refile-list nil
949 (unwind-protect (mh-execute-commands)
953 (loop for y in
(cdr x
)
954 unless
(memq y msgs
) collect y
)))
957 (loop for x in old-delete-list
958 unless
(memq x msgs
) collect x
))
959 (mh-set-folder-modified-p (mh-outstanding-commands-p))
960 (when (mh-outstanding-commands-p)
961 (mh-notate-deleted-and-refiled)))))))
962 (mh-index-matching-source-msgs (append (loop for x in mh-refile-list
969 (defun mh-index-add-to-sequence (seq msgs
)
970 "Add to SEQ the messages in the list MSGS.
971 This function updates the source folder sequences. Also makes an attempt to
972 update the source folder buffer if we have it open."
973 ;; Don't need to do anything for cur
975 (when (and (not (memq seq
(mh-unpropagated-sequences)))
976 (mh-valid-seq-p seq
))
978 (mh-speed-flists-inhibit-flag t
))
979 (maphash (lambda (folder msgs
)
980 (push folder folders
)
981 ;; Add messages to sequence in source folder...
982 (apply #'mh-exec-cmd-quiet nil
"mark" folder
983 "-add" "-nozero" "-sequence" (symbol-name seq
)
984 (mapcar (lambda (x) (format "%s" x
))
985 (mh-coalesce-msg-list msgs
)))
986 ;; Update source folder buffer if we have it open...
987 (when (get-buffer folder
)
990 (mh-put-msg-in-seq msgs seq
))))
991 (mh-index-matching-source-msgs msgs
))
995 (defun mh-index-delete-from-sequence (seq msgs
)
996 "Delete from SEQ the messages in MSGS.
997 This function updates the source folder sequences. Also makes an attempt to
998 update the source folder buffer if present."
1000 (when (and (not (memq seq
(mh-unpropagated-sequences)))
1001 (mh-valid-seq-p seq
))
1003 (mh-speed-flists-inhibit-flag t
))
1004 (maphash (lambda (folder msgs
)
1005 (push folder folders
)
1006 ;; Remove messages from sequence in source folder...
1007 (apply #'mh-exec-cmd-quiet nil
"mark" folder
1008 "-del" "-nozero" "-sequence" (symbol-name seq
)
1009 (mapcar (lambda (x) (format "%s" x
))
1010 (mh-coalesce-msg-list msgs
)))
1011 ;; Update source folder buffer if we have it open...
1012 (when (get-buffer folder
)
1015 (mh-delete-msg-from-seq msgs seq t
))))
1016 (mh-index-matching-source-msgs msgs
))
1023 (defvar mh-index-pick-folder
)
1024 (defvar mh-pick-binary
"pick")
1026 (defun mh-pick-execute-search (folder-path search-regexp
)
1029 Unlike the other index search programs \"pick\" only searches messages present
1030 in the folder itself and does not descend into any sub-folders that may be
1033 In a program, FOLDER-PATH is the directory in which SEARCH-REGEXP is used
1035 (set-buffer (get-buffer-create mh-index-temp-buffer
))
1037 (setq mh-index-pick-folder
1038 (concat "+" (substring folder-path
(length mh-user-path
))))
1039 (apply #'call-process
(expand-file-name "pick" mh-progs
) nil
'(t nil
) nil
1040 mh-index-pick-folder
"-list" search-regexp
)
1041 (goto-char (point-min)))
1043 (defun mh-pick-next-result ()
1044 "Return the next pick search result."
1046 (when (eobp) (return nil
))
1047 (unless (re-search-forward "^[1-9][0-9]*$" (line-end-position) t
)
1049 (list mh-index-pick-folder
1050 (car (read-from-string (buffer-substring-no-properties
1051 (line-beginning-position)
1052 (line-end-position))))
1060 (defvar mh-grep-binary
(executable-find "grep"))
1062 (defun mh-grep-execute-search (folder-path search-regexp
)
1063 "Execute grep and read the results.
1065 Unlike the other index search programs \"grep\" only searches messages present
1066 in the folder itself and does not descend into any sub-folders that may be
1069 In a program, FOLDER-PATH is the directory in which SEARCH-REGEXP is used
1071 (set-buffer (get-buffer-create mh-index-temp-buffer
))
1073 (call-process mh-grep-binary nil
'(t nil
) nil
1074 "-i" "-r" search-regexp folder-path
)
1075 (goto-char (point-min)))
1077 (defun mh-grep-next-result ()
1078 "Read the next result.
1079 Parse it and return the message folder, message index and the match. If no
1080 other matches left then return nil. If the current record is invalid return
1086 (let ((eol-pos (line-end-position))
1087 (bol-pos (line-beginning-position))
1088 folder-start msg-end
)
1090 (unless (search-forward mh-user-path eol-pos t
)
1092 (setq folder-start
(point))
1093 (unless (search-forward ":" eol-pos t
)
1095 (let ((match (buffer-substring-no-properties (point) eol-pos
)))
1097 (setq msg-end
(point))
1098 (unless (search-backward "/" folder-start t
)
1100 (list (format "+%s" (buffer-substring-no-properties
1101 folder-start
(point)))
1102 (let ((val (ignore-errors (read-from-string
1103 (buffer-substring-no-properties
1104 (1+ (point)) msg-end
)))))
1105 (if (and (consp val
) (integerp (car val
)))
1115 (defvar mh-mairix-binary
(executable-find "mairix"))
1116 (defvar mh-mairix-directory
".mairix")
1117 (defvar mh-mairix-folder nil
)
1119 (defun mh-mairix-execute-search (folder-path search-regexp-list
)
1120 "Execute mairix and read the results.
1122 In the examples below, replace \"/home/user/Mail\" with the path to your MH
1125 First create the directory \"/home/user/Mail/.mairix\". Then create the file
1126 \"/home/user/Mail/.mairix/config\" with the following contents:
1128 base=/home/user/Mail
1130 # List of folders that should be indexed. 3 dots at the end means there
1131 # are subfolders within the folder
1132 mh=archive...:inbox:drafts:news:sent:trash
1135 database=/home/user/Mail/mairix/database
1137 Use the following command line to generate the mairix index. Run this daily
1140 mairix -f /home/user/Mail/.mairix/config
1142 In a program, FOLDER-PATH is the directory in which SEARCH-REGEXP-LIST is used
1144 (set-buffer (get-buffer-create mh-index-temp-buffer
))
1146 (unless mh-mairix-binary
1147 (error "Set mh-mairix-binary appropriately"))
1148 (apply #'call-process mh-mairix-binary nil
'(t nil
) nil
1149 "-r" "-f" (format "%s%s/config" mh-user-path mh-mairix-directory
)
1151 (goto-char (point-min))
1152 (setq mh-mairix-folder
1153 (let ((last-char (substring folder-path
(1- (length folder-path
)))))
1154 (if (equal last-char
"/")
1156 (format "%s/" folder-path
)))))
1158 (defun mh-mairix-next-result ()
1159 "Return next result from mairix output."
1162 (when (or (eobp) (and (bolp) (eolp)))
1164 (unless (eq (char-after) ?
/)
1166 (let ((start (point))
1168 (setq end
(line-end-position))
1169 (unless (search-forward mh-mairix-folder end t
)
1171 (goto-char (match-beginning 0))
1172 (unless (equal (point) start
)
1175 (unless (search-backward "/" start t
)
1177 (setq msg-start
(1+ (point)))
1179 (unless (search-forward mh-user-path end t
)
1181 (list (format "+%s" (buffer-substring-no-properties
1182 (point) (1- msg-start
)))
1183 (car (read-from-string
1184 (buffer-substring-no-properties msg-start end
)))
1188 (defun mh-mairix-regexp-builder (regexp-list)
1189 "Generate query for mairix.
1190 REGEXP-LIST is an alist of fields and values."
1192 (dolist (pair regexp-list
)
1196 (cond ((eq (car pair
) 'to
) "t:")
1197 ((eq (car pair
) 'from
) "f:")
1198 ((eq (car pair
) 'cc
) "c:")
1199 ((eq (car pair
) 'subject
) "s:")
1200 ((eq (car pair
) 'date
) "d:")
1202 (let ((sop (cdr (mh-mairix-convert-to-sop* (cdr pair
))))
1204 (dolist (conjunct sop
)
1205 (let ((expr-list (cdr conjunct
))
1207 (dolist (e expr-list
)
1208 (setq expr-string
(concat expr-string
","
1209 (if (atom e
) "" "~")
1210 (if (atom e
) e
(cadr e
)))))
1211 (setq final
(concat final
"/" (substring expr-string
1)))))
1212 (substring final
1)))
1216 (defun mh-mairix-convert-to-sop* (expr)
1217 "Convert EXPR to sum of product form."
1218 (cond ((atom expr
) `(or (and ,expr
)))
1219 ((eq (car expr
) 'or
)
1221 (loop for e in
(mapcar #'mh-mairix-convert-to-sop
* (cdr expr
))
1223 ((eq (car expr
) 'and
)
1224 (let ((conjuncts (mapcar #'mh-mairix-convert-to-sop
* (cdr expr
)))
1226 (setq result
(pop conjuncts
))
1228 (setq next-factor
(pop conjuncts
))
1229 (setq result
(let ((res ()))
1230 (dolist (t1 (cdr result
))
1231 (dolist (t2 (cdr next-factor
))
1232 (push `(and ,@(cdr t1
) ,@(cdr t2
)) res
)))
1235 ((atom (cadr expr
)) `(or (and ,expr
)))
1236 ((eq (caadr expr
) 'not
) (mh-mairix-convert-to-sop* (cadadr expr
)))
1237 ((eq (caadr expr
) 'and
) (mh-mairix-convert-to-sop*
1238 `(or ,@(mapcar #'(lambda (x) `(not ,x
))
1240 ((eq (caadr expr
) 'or
) (mh-mairix-convert-to-sop*
1241 `(and ,@(mapcar #'(lambda (x) `(not ,x
))
1243 (t (error "Unreachable: %s" expr
))))
1247 ;; Interface to unseen messages script
1249 (defvar mh-flists-search-folders
)
1251 ;; XXX: This should probably be in mh-utils.el and used in other places where
1252 ;; MH-E calls out to /bin/sh.
1253 (defun mh-index-quote-for-shell (string)
1254 "Quote STRING for /bin/sh."
1256 (loop for x across string
1257 concat
(format (if (memq x
'(?
\\ ?
` ?$
)) "\\%c" "%c") x
))
1260 (defun mh-flists-execute (&rest args
)
1262 Search for messages belonging to `mh-flists-sequence' in the folders
1263 specified by `mh-flists-search-folders'. If `mh-recursive-folders-flag' is t,
1264 then the folders are searched recursively. All parameters ARGS are ignored."
1265 (set-buffer (get-buffer-create mh-index-temp-buffer
))
1267 (unless (executable-find "sh")
1268 (error "Didn't find sh"))
1270 (let ((seq (symbol-name mh-flists-sequence
)))
1271 (insert "for folder in `" (expand-file-name "flists" mh-progs
) " "
1272 (cond ((eq mh-flists-search-folders t
)
1273 (mh-index-quote-for-shell mh-inbox
))
1274 ((eq mh-flists-search-folders nil
) "")
1275 ((listp mh-flists-search-folders
)
1276 (loop for folder in mh-flists-search-folders
1278 (concat " " (mh-index-quote-for-shell folder
)))))
1279 (if mh-recursive-folders-flag
" -recurse" "")
1280 " -sequence " seq
" -noshowzero -fast` ; do\n"
1281 (expand-file-name "mhpath" mh-progs
) " \"+$folder\" " seq
"\n"
1283 (call-process-region
1284 (point-min) (point-max) "sh" nil
(get-buffer mh-index-temp-buffer
))))
1287 (defun mh-index-sequenced-messages (folders sequence
)
1288 "Display messages from FOLDERS in SEQUENCE.
1289 All messages in the sequence you provide from the folders in
1290 `mh-index-new-messages-folders' are listed. With a prefix argument, enter a
1291 space-separated list of folders, or nothing to search all folders."
1293 (list (if current-prefix-arg
1294 (split-string (read-string "Search folder(s): [all] "))
1295 mh-index-new-messages-folders
)
1296 (mh-read-seq-default "Search" nil
)))
1297 (unless sequence
(setq sequence mh-unseen-seq
))
1298 (let* ((mh-flists-search-folders folders
)
1299 (mh-flists-sequence sequence
)
1300 (mh-flists-called-flag t
)
1301 (mh-indexer 'flists
)
1302 (mh-index-execute-search-function 'mh-flists-execute
)
1303 (mh-index-next-result-function 'mh-mairix-next-result
)
1304 (mh-mairix-folder mh-user-path
)
1305 (mh-index-regexp-builder nil
)
1306 (new-folder (format "%s/%s/%s" mh-index-folder
1307 mh-flists-results-folder sequence
))
1308 (window-config (if (equal new-folder mh-current-folder
)
1309 mh-previous-window-config
1310 (current-window-configuration)))
1313 (cond ((buffer-live-p (get-buffer new-folder
))
1314 ;; The destination folder is being visited. Trick `mh-index-search'
1315 ;; into thinking that the folder resulted from a previous search.
1316 (set-buffer new-folder
)
1317 (setq mh-index-previous-search
(list folders sequence
))
1319 ((mh-folder-exists-p new-folder
)
1320 ;; Folder exists but we don't have it open. That means they are
1321 ;; stale results from a old flists search. Clear it out.
1322 (mh-exec-cmd-quiet nil
"rmf" new-folder
)))
1323 (setq message
(mh-index-search redo-flag
"+" mh-flists-results-folder
1325 mh-index-sequence-search-flag t
1326 mh-index-previous-search
(list folders sequence
))
1327 (mh-index-write-data)
1328 (when (stringp message
) (message message
))))
1331 (defun mh-index-new-messages (folders)
1332 "Display unseen messages.
1333 If you use a program such as `procmail' to use `rcvstore' to file your
1334 incoming mail automatically, you can display new, unseen, messages using this
1335 command. All messages in the `unseen' sequence from the folders in
1336 `mh-index-new-messages-folders' are listed. With a prefix argument, enter a
1337 space-separated list of FOLDERS, or nothing to search all folders."
1339 (list (if current-prefix-arg
1340 (split-string (read-string "Search folder(s): [all] "))
1341 mh-index-new-messages-folders
)))
1342 (mh-index-sequenced-messages folders mh-unseen-seq
))
1345 (defun mh-index-ticked-messages (folders)
1346 "Display ticked messages.
1347 All messages in `mh-tick-seq' from the folders in
1348 `mh-index-ticked-messages-folders' are listed. With a prefix argument, enter a
1349 space-separated list of FOLDERS, or nothing to search all folders."
1351 (list (if current-prefix-arg
1352 (split-string (read-string "Search folder(s): [all] "))
1353 mh-index-ticked-messages-folders
)))
1354 (mh-index-sequenced-messages folders mh-tick-seq
))
1360 (defvar mh-swish-binary
(executable-find "swish-e"))
1361 (defvar mh-swish-directory
".swish")
1362 (defvar mh-swish-folder nil
)
1365 (defun mh-swish-execute-search (folder-path search-regexp
)
1366 "Execute swish-e and read the results.
1368 In the examples below, replace \"/home/user/Mail\" with the path to your
1371 First create the directory \"/home/user/Mail/.swish\". Then create the file
1372 \"/home/user/Mail/.swish/config\" with the following contents:
1374 DefaultContents TXT*
1375 IndexDir /home/user/Mail
1376 IndexFile /home/user/Mail/.swish/index
1377 IndexName \"Mail Index\"
1378 IndexDescription \"Mail Index\"
1379 IndexPointer \"http://nowhere\"
1380 IndexAdmin \"nobody\"
1381 #MetaNames automatic
1385 IgnoreTotalWordCountWhenRanking yes
1386 WordCharacters abcdefghijklmnopqrstuvwxyz0123456789-
1387 BeginCharacters abcdefghijklmnopqrstuvwxyz
1388 EndCharacters abcdefghijklmnopqrstuvwxyz0123456789
1391 FileRules filename contains \\D
1392 FileRules pathname contains /home/user/Mail/.swish
1393 FileRules pathname contains /home/user/Mail/mhe-index
1395 This configuration does not index the folders that hold the results of your
1396 searches in \"+mhe-index\" since they tend to be ephemeral and the original
1397 messages are indexed anyway.
1399 If there are any directories you would like to ignore, append lines like the
1400 following to \"config\":
1402 FileRules pathname contains /home/user/Mail/scripts
1404 Use the following command line to generate the swish index. Run this daily
1407 swish-e -c /home/user/Mail/.swish/config
1409 In a program, FOLDER-PATH is the directory in which SEARCH-REGEXP is used to
1411 (set-buffer (get-buffer-create mh-index-temp-buffer
))
1413 (unless mh-swish-binary
1414 (error "Set mh-swish-binary appropriately"))
1415 (call-process mh-swish-binary nil
'(t nil
) nil
1417 "-f" (format "%s%s/index" mh-user-path mh-swish-directory
))
1418 (goto-char (point-min))
1419 (setq mh-swish-folder
1420 (let ((last-char (substring folder-path
(1- (length folder-path
)))))
1421 (if (equal last-char
"/")
1423 (format "%s/" folder-path
)))))
1425 (defun mh-swish-next-result ()
1426 "Get the next result from swish output."
1429 (when (or (eobp) (equal (char-after (point)) ?.
))
1431 (when (equal (char-after (point)) ?
#)
1433 (let* ((start (search-forward " " (line-end-position) t
))
1434 (end (search-forward " " (line-end-position) t
)))
1435 (unless (and start end
)
1438 (unless (file-exists-p (buffer-substring-no-properties start end
))
1440 (unless (search-backward "/" start t
)
1442 (list (let* ((s (buffer-substring-no-properties start
(1+ (point)))))
1443 (unless (string-match mh-swish-folder s
)
1445 (if (and (string-match mh-user-path s
)
1446 (< (match-end 0) (1- (length s
))))
1448 (substring s
(match-end 0) (1- (length s
))))
1450 (let* ((s (buffer-substring-no-properties (1+ (point)) end
))
1451 (val (ignore-errors (read-from-string s
))))
1452 (if (and (consp val
) (numberp (car val
)))
1460 ;; Swish++ interface
1462 (defvar mh-swish
++-binary
(or (executable-find "search++")
1463 (executable-find "search")))
1464 (defvar mh-swish
++-directory
".swish++")
1467 (defun mh-swish++-execute-search
(folder-path search-regexp
)
1468 "Execute swish++ and read the results.
1470 In the examples below, replace \"/home/user/Mail\" with the path to your MH
1473 First create the directory \"/home/user/Mail/.swish++\". Then create the file
1474 \"/home/user/Mail/.swish++/swish++.conf\" with the following contents:
1476 IncludeMeta Bcc Cc Comments Content-Description From Keywords
1477 IncludeMeta Newsgroups Resent-To Subject To
1478 IncludeMeta Message-Id References In-Reply-To
1480 IndexFile /home/user/Mail/.swish++/swish++.index
1482 Use the following command line to generate the swish index. Run this daily
1485 find /home/user/Mail -path /home/user/Mail/mhe-index -prune \\
1486 -o -path /home/user/Mail/.swish++ -prune \\
1487 -o -name \"[0-9]*\" -print \\
1488 | index -c /home/user/Mail/.swish++/swish++.conf -
1490 This command does not index the folders that hold the results of your searches
1491 in \"+mhe-index\" since they tend to be ephemeral and the original messages
1494 On some systems (Debian GNU/Linux, for example), use \"index++\" instead of
1497 In a program, FOLDER-PATH is the directory in which SEARCH-REGEXP is used to
1499 (set-buffer (get-buffer-create mh-index-temp-buffer
))
1501 (unless mh-swish
++-binary
1502 (error "Set mh-swish++-binary appropriately"))
1503 (call-process mh-swish
++-binary nil
'(t nil
) nil
1505 (format "-i%s%s/swish++.index"
1506 mh-user-path mh-swish
++-directory
)
1508 (goto-char (point-min))
1509 (setq mh-swish-folder
1510 (let ((last-char (substring folder-path
(1- (length folder-path
)))))
1511 (if (equal last-char
"/")
1513 (format "%s/" folder-path
)))))
1515 (defalias 'mh-swish
++-next-result
'mh-swish-next-result
)
1517 (defun mh-swish++-regexp-builder
(regexp-list)
1518 "Generate query for swish++.
1519 REGEXP-LIST is an alist of fields and values."
1521 (dolist (elem regexp-list
)
1523 (setq regexp
(concat regexp
" and "
1524 (if (car elem
) "(" "")
1525 (if (car elem
) (symbol-name (car elem
)) "")
1526 (if (car elem
) " = " "")
1527 (mh-swish++-print-regexp
(cdr elem
))
1528 (if (car elem
) ")" "")))))
1529 (substring regexp
4)))
1531 (defun mh-swish++-print-regexp
(expr)
1532 "Return infix expression corresponding to EXPR."
1533 (cond ((atom expr
) (format "%s" expr
))
1534 ((eq (car expr
) 'not
)
1535 (format "(not %s)" (mh-swish++-print-regexp
(cadr expr
))))
1536 (t (format "(%s %s %s)" (mh-swish++-print-regexp
(cadr expr
))
1537 (symbol-name (car expr
))
1538 (mh-swish++-print-regexp
(caddr expr
))))))
1544 (defvar mh-namazu-binary
(executable-find "namazu"))
1545 (defvar mh-namazu-directory
".namazu")
1546 (defvar mh-namazu-folder nil
)
1549 (defun mh-namazu-execute-search (folder-path search-regexp
)
1550 "Execute namazu and read the results.
1552 In the examples below, replace \"/home/user/Mail\" with the path to your MH
1555 First create the directory \"/home/user/Mail/.namazu\". Then create the file
1556 \"/home/user/Mail/.namazu/mknmzrc\" with the following contents:
1558 package conf; # Don't remove this line!
1559 $ADDRESS = 'user@localhost';
1560 $ALLOW_FILE = \"[0-9]*\";
1561 $EXCLUDE_PATH = \"^/home/user/Mail/(mhe-index|spam)\";
1563 This configuration does not index the folders that hold the results of your
1564 searches in \"+mhe-index\" since they tend to be ephemeral and the original
1565 messages are indexed anyway.
1567 Use the following command line to generate the namazu index. Run this daily
1570 mknmz -f /home/user/Mail/.namazu/mknmzrc -O /home/user/Mail/.namazu \\
1573 In a program, FOLDER-PATH is the directory in which SEARCH-REGEXP is used to
1575 (let ((namazu-index-directory
1576 (format "%s%s" mh-user-path mh-namazu-directory
)))
1577 (unless (file-exists-p namazu-index-directory
)
1578 (error "Namazu directory %s not present" namazu-index-directory
))
1579 (unless (executable-find mh-namazu-binary
)
1580 (error "Set mh-namazu-binary appropriately"))
1581 (set-buffer (get-buffer-create mh-index-temp-buffer
))
1583 (call-process mh-namazu-binary nil
'(t nil
) nil
1584 "-alR" search-regexp namazu-index-directory
)
1585 (goto-char (point-min))
1586 (setq mh-namazu-folder
1587 (let ((last (substring folder-path
(1- (length folder-path
)))))
1588 (if (equal last
"/")
1590 (format "%s/" folder-path
))))))
1592 (defun mh-namazu-next-result ()
1593 "Get the next result from namazu output."
1596 (when (eobp) (return nil
))
1597 (let ((file-name (buffer-substring-no-properties
1598 (point) (line-end-position))))
1599 (unless (equal (string-match mh-namazu-folder file-name
) 0)
1601 (unless (file-exists-p file-name
)
1603 (string-match mh-user-path file-name
)
1604 (let* ((folder/msg
(substring file-name
(match-end 0)))
1605 (mark (mh-search-from-end ?
/ folder
/msg
)))
1606 (unless mark
(return 'error
))
1607 (list (format "+%s" (substring folder
/msg
0 mark
))
1608 (let ((n (ignore-errors (read-from-string
1609 (substring folder
/msg
(1+ mark
))))))
1610 (if (and (consp n
) (numberp (car n
)))
1619 (defun mh-index-choose ()
1620 "Choose an indexing function.
1621 The side-effects of this function are that the variables `mh-indexer',
1622 `mh-index-execute-search-function', and `mh-index-next-result-function' are
1623 set according to the first indexer in `mh-indexer-choices' present on the
1626 ;; The following favors the user's preference; otherwise, the last
1627 ;; automatically chosen indexer is used for efficiency rather than going
1628 ;; through the list.
1629 (let ((program-alist (cond (mh-index-program
1631 (assoc mh-index-program mh-indexer-choices
)))
1633 (list (assoc mh-indexer mh-indexer-choices
)))
1634 (t mh-indexer-choices
))))
1635 (while program-alist
1636 (let* ((current (pop program-alist
))
1637 (executable (symbol-value (cadr current
))))
1639 (setq mh-indexer
(car current
))
1640 (setq mh-index-execute-search-function
(nth 2 current
))
1641 (setq mh-index-next-result-function
(nth 3 current
))
1642 (setq mh-index-regexp-builder
(nth 4 current
))
1643 (return mh-indexer
))))
1650 ;;; Local Variables:
1651 ;;; indent-tabs-mode: nil
1652 ;;; sentence-end-double-space: nil
1655 ;;; arch-tag: 607762ad-0dff-4fe1-a27e-6c0dde0dcc47
1656 ;;; mh-index ends here