Minor tweak.
[bpt/emacs.git] / lisp / mh-e / mh-index.el
CommitLineData
bdcfe844
BW
1;;; mh-index -- MH-E interface to indexing programs
2
e495eaec 3;; Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
bdcfe844 4
c3d9274a 5;; Author: Satyaki Das <satyaki@theforce.stanford.edu>
bdcfe844
BW
6;; Maintainer: Bill Wohler <wohler@newt.com>
7;; Keywords: mail
8;; See: mh-e.el
9
10;; This file is part of GNU Emacs.
11
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)
15;; any later version.
16
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.
21
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
3a35cf56
LK
24;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25;; Boston, MA 02110-1301, USA.
bdcfe844
BW
26
27;;; Commentary:
28
cee9f5c6
BW
29;; (1) The following search engines are supported:
30;; swish++
31;; swish-e
32;; mairix
33;; namazu
34;; pick
35;; grep
36;;
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.
bdcfe844
BW
41
42;;; Change Log:
43
bdcfe844
BW
44;;; Code:
45
f0d73c14 46(eval-when-compile (require 'mh-acros))
a66894d8 47(mh-require-cl)
bdcfe844
BW
48(require 'mh-e)
49(require 'mh-mime)
3d7ca223 50(require 'mh-pick)
bdcfe844 51
bdcfe844
BW
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")
56
bdcfe844
BW
57;; Support different indexing programs
58(defvar mh-indexer-choices
59 '((swish++
3d7ca223
BW
60 mh-swish++-binary mh-swish++-execute-search mh-swish++-next-result
61 mh-swish++-regexp-builder)
bdcfe844 62 (swish
3d7ca223
BW
63 mh-swish-binary mh-swish-execute-search mh-swish-next-result nil)
64 (mairix
65 mh-mairix-binary mh-mairix-execute-search mh-mairix-next-result
66 mh-mairix-regexp-builder)
bdcfe844 67 (namazu
3d7ca223 68 mh-namazu-binary mh-namazu-execute-search mh-namazu-next-result nil)
3d7ca223
BW
69 (pick
70 mh-pick-binary mh-pick-execute-search mh-pick-next-result
71 mh-pick-regexp-builder)
bdcfe844 72 (grep
3d7ca223 73 mh-grep-binary mh-grep-execute-search mh-grep-next-result nil))
bdcfe844
BW
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.")
3d7ca223
BW
81(defvar mh-index-regexp-builder nil
82 "Function used to construct search regexp.")
bdcfe844 83
c3d9274a
BW
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.")
87
88;; Temporary buffers for search results
bdcfe844 89(defvar mh-index-temp-buffer " *mh-index-temp*")
c3d9274a
BW
90(defvar mh-checksum-buffer " *mh-checksum-buffer*")
91
92\f
bdcfe844 93
cee9f5c6
BW
94;; A few different checksum programs are supported. The supported programs
95;; are:
96;; 1. md5sum
97;; 2. md5
98;; 3. openssl
99;;
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.
c3d9274a
BW
105
106(defvar mh-checksum-cmd)
107(defvar mh-checksum-parser)
108
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"))))))
123
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))
130 (goto-char end)
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)))))
137
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)
143 (goto-char end)
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))))))
150
151(defalias 'mh-md5-parser 'mh-openssl-parser)
bdcfe844
BW
152
153\f
154
cee9f5c6 155;; Make sure that we don't produce too long a command line.
c3d9274a
BW
156(defvar mh-index-max-cmdline-args 500
157 "Maximum number of command line args.")
158
159(defun mh-index-execute (cmd &rest args)
160 "Partial imitation of xargs.
2dcf34f9
BW
161The current buffer contains a list of strings, one on each line.
162The function will execute CMD with ARGS and pass the first
163`mh-index-max-cmdline-args' strings to it. This is repeated till
164all the strings have been used."
c3d9274a 165 (goto-char (point-min))
924df208
BW
166 (let ((current-buffer (current-buffer)))
167 (with-temp-buffer
168 (let ((out (current-buffer)))
169 (set-buffer current-buffer)
170 (while (not (eobp))
171 (let ((arg-list (reverse args))
172 (count 0))
173 (while (and (not (eobp)) (< count mh-index-max-cmdline-args))
174 (push (buffer-substring-no-properties (point) (line-end-position))
175 arg-list)
176 (incf count)
177 (forward-line))
178 (apply #'call-process cmd nil (list out nil) nil
179 (nreverse arg-list))))
180 (erase-buffer)
181 (insert-buffer-substring out)))))
bdcfe844
BW
182
183\f
184
c3d9274a
BW
185(defun mh-index-update-single-msg (msg checksum origin-map)
186 "Update various maps for one message.
2dcf34f9
BW
187MSG is a index folder message, CHECKSUM its MD5 hash and
188ORIGIN-MAP, if non-nil, a hashtable containing which maps each
189message in the index folder to the folder and message that it was
190copied from. The function updates the hash tables
191`mh-index-msg-checksum-map' and `mh-index-checksum-origin-map'.
192
193This function should only be called in the appropriate index
194folder buffer."
c3d9274a
BW
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))
f0d73c14
BW
203 (when (gethash ofolder mh-index-data)
204 (remhash omsg (gethash ofolder mh-index-data)))))
c3d9274a
BW
205 (t
206 (setf (gethash msg mh-index-msg-checksum-map) checksum)
207 (when origin-map
208 (setf (gethash checksum mh-index-checksum-origin-map)
209 (gethash msg origin-map))))))
210
211;;;###mh-autoload
212(defun mh-index-update-maps (folder &optional origin-map)
213 "Annotate all as yet unannotated messages in FOLDER with their MD5 hash.
2dcf34f9
BW
214As a side effect msg -> checksum map is updated. Optional
215argument ORIGIN-MAP is a hashtable which maps each message in the
216index folder to the original folder and message from whence it
217was copied. If present the checksum -> (origin-folder,
218origin-index) map is updated too."
c3d9274a
BW
219 (clrhash mh-index-msg-checksum-map)
220 (save-excursion
221 ;; Clear temp buffer
222 (set-buffer (get-buffer-create mh-checksum-buffer))
223 (erase-buffer)
224 ;; Run scan to check if any messages needs MD5 annotations at all
225 (with-temp-buffer
226 (mh-exec-cmd-output mh-scan-prog nil "-width" "80"
227 "-format" "%(msg)\n%{x-mhe-checksum}\n"
228 folder "all")
229 (goto-char (point-min))
230 (let (msg checksum)
231 (while (not (eobp))
232 (setq msg (buffer-substring-no-properties
233 (point) (line-end-position)))
234 (forward-line)
235 (save-excursion
924df208
BW
236 (cond ((not (string-match "^[0-9]*$" msg)))
237 ((eolp)
c3d9274a
BW
238 ;; need to compute checksum
239 (set-buffer mh-checksum-buffer)
240 (insert mh-user-path (substring folder 1) "/" msg "\n"))
241 (t
242 ;; update maps
243 (setq checksum (buffer-substring-no-properties
244 (point) (line-end-position)))
245 (let ((msg (car (read-from-string msg))))
246 (set-buffer folder)
247 (mh-index-update-single-msg msg checksum origin-map)))))
248 (forward-line))))
249 ;; Run checksum program if needed
250 (unless (and (eobp) (bobp))
251 (apply #'mh-index-execute mh-checksum-cmd)
252 (goto-char (point-min))
253 (while (not (eobp))
254 (let* ((intermediate (funcall mh-checksum-parser))
255 (msg (car intermediate))
256 (checksum (cdr intermediate)))
257 (when msg
258 ;; annotate
259 (mh-exec-cmd "anno" folder msg "-component" "X-MHE-Checksum"
260 "-nodate" "-text" checksum "-inplace")
261 ;; update maps
262 (save-excursion
263 (set-buffer folder)
264 (mh-index-update-single-msg msg checksum origin-map)))
a66894d8
BW
265 (forward-line)))))
266 (mh-index-write-data))
c3d9274a 267
a66894d8
BW
268(defvar mh-unpropagated-sequences '(cur range subject search)
269 "List of sequences that aren't preserved.")
270
271(defun mh-unpropagated-sequences ()
272 "Return a list of sequences that aren't propagated to the source folders.
2dcf34f9
BW
273It is just the sequences in the variable
274`mh-unpropagated-sequences' in addition to the
275Previous-Sequence (see mh-profile 5)."
a66894d8
BW
276 (if mh-previous-seq
277 (cons mh-previous-seq mh-unpropagated-sequences)
278 mh-unpropagated-sequences))
279
280;;;###mh-autoload
281(defun mh-create-sequence-map (seq-list)
282 "Return a map from msg number to list of sequences in which it is present.
2dcf34f9
BW
283SEQ-LIST is an assoc list whose keys are sequence names and whose
284cdr is the list of messages in that sequence."
a66894d8
BW
285 (loop with map = (make-hash-table)
286 for seq in seq-list
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)))
291 finally return map))
292
293;;;###mh-autoload
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))
297 (seq-list ()))
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))
305 (ofolder (car pair))
306 (omsg (cdr pair)))
f0d73c14
BW
307 (loop for seq in (ignore-errors
308 (gethash omsg (gethash ofolder seq-hash)))
a66894d8
BW
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))))))
316
317(defvar mh-flists-results-folder "sequence"
924df208 318 "Subfolder for `mh-index-folder' where flists output is placed.")
a66894d8
BW
319(defvar mh-flists-sequence)
320(defvar mh-flists-called-flag nil)
924df208 321
c3d9274a
BW
322(defun mh-index-generate-pretty-name (string)
323 "Given STRING generate a name which is suitable for use as a folder name.
2dcf34f9
BW
324White space from the beginning and end are removed. All spaces in
325the name are replaced with underscores and all / are replaced
326with $. If STRING is longer than 20 it is truncated too. STRING
327could be a list of strings in which case they are concatenated to
328construct the base name."
c3d9274a 329 (with-temp-buffer
3d7ca223
BW
330 (if (stringp string)
331 (insert string)
332 (when (car string) (insert (car string)))
333 (dolist (s (cdr string))
334 (insert "_" s)))
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)
c3d9274a 340 (goto-char (point-min))
3d7ca223 341 (while (and (not (eobp)) (memq (char-after) '(? ?\t ?\n ?\r ?_)))
c3d9274a
BW
342 (delete-char 1))
343 (goto-char (point-max))
3d7ca223 344 (while (and (not (bobp)) (memq (char-before) '(? ?\t ?\n ?\r ?_)))
c3d9274a 345 (delete-backward-char 1))
3d7ca223 346 (subst-char-in-region (point-min) (point-max) ? ?_ t)
c3d9274a
BW
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)
924df208 351 (let ((out (truncate-string-to-width (buffer-string) 20)))
a66894d8
BW
352 (cond ((eq mh-indexer 'flists)
353 (format "%s/%s" mh-flists-results-folder mh-flists-sequence))
924df208
BW
354 ((equal out mh-flists-results-folder) (concat out "1"))
355 (t out)))))
c3d9274a
BW
356
357;;;###mh-autoload
3d7ca223 358(defun* mh-index-search (redo-search-flag folder search-regexp
a66894d8 359 &optional window-config)
bdcfe844 360 "Perform an indexed search in an MH mail folder.
2dcf34f9 361
e495eaec
BW
362Use a prefix argument to repeat the search.
363
2dcf34f9 364Unlike regular searches, the prompt for the folder to search can be
5a4aad03 365\"all\" to search all folders; in addition, the search works recursively
2dcf34f9
BW
366on the listed folder. The search criteria are entered in an MH-Pick
367buffer as described in `mh-search-folder'.
368
369To perform the search, type \\<mh-pick-mode-map>\\[mh-do-search].
370Another difference from the regular searches is that because the
371search operates on more than one folder, the messages that are found
5a4aad03 372are put in a temporary sub-folder of \"+mhe-index\" and are displayed in
2dcf34f9
BW
373an MH-Folder buffer. This buffer is special because it displays
374messages from multiple folders; each set of messages from a given
375folder has a heading with the folder name.
376
377In addition, the \\<mh-folder-mode-map>\\[mh-index-visit-folder]
378command can be used to visit the folder of the message at point.
379Initially, only the messages that matched the search criteria are
380displayed in the folder. While the temporary buffer has its own set of
381message numbers, the actual messages numbers are shown in the visited
382folder. Thus, the \\[mh-index-visit-folder] command is useful to find
383the actual message number of an interesting message, or to view
384surrounding messages with the \\[mh-rescan-folder] command.
385
386Because this folder is temporary, you'll probably get in the habit of
387killing it when you're done with \\[mh-kill-folder].
388
389If you have run the \\[mh-search-folder] command, but change your mind
390while entering the search criteria and actually want to run an indexed
391search, then you can use the
392\\<mh-pick-mode-map>\\[mh-index-do-search] command in the MH-Pick
393buffer.
394
395The \\<mh-folder-mode-map>\\[mh-index-search] command runs the command
396defined by the `mh-index-program' option. The default value is
397\"Auto-detect\" which means that MH-E will automatically choose one of
398\"swish++\", \"swish-e\", \"mairix\", \"namazu\", \"pick\" and
399\"grep\" in that order. If, for example, you have both \"swish++\" and
400\"mairix\" installed and you want to use \"mairix\", then you can set
401this option to \"mairix\".
e495eaec
BW
402
403 *NOTE*
404
2dcf34f9
BW
405 The \"pick\" and \"grep\" commands do not perform a
406 recursive search on the given folder.
e495eaec 407
2dcf34f9
BW
408This command uses an \"X-MHE-Checksum:\" header field to cache
409the MD5 checksum of a message. This means that if an incoming
410message already contains an \"X-MHE-Checksum:\" field, that
411message might not be found by this command. The following
412\"procmail\" recipe avoids this problem by renaming the existing
e495eaec
BW
413header field:
414
415 :0 wf
416 | formail -R \"X-MHE-Checksum\" \"X-Old-MHE-Checksum\"
417
2dcf34f9
BW
418The documentation for the following commands describe how to set
419up the various indexing programs to use with MH-E. The \"pick\"
420and \"grep\" commands do not require additional configuration.
bdcfe844
BW
421
422 - `mh-swish++-execute-search'
423 - `mh-swish-execute-search'
3d7ca223 424 - `mh-mairix-execute-search'
bdcfe844 425 - `mh-namazu-execute-search'
3d7ca223
BW
426 - `mh-pick-execute-search'
427 - `mh-grep-execute-search'
428
2dcf34f9
BW
429In a program, if REDO-SEARCH-FLAG is non-nil and the current
430folder buffer was generated by a index search, then the search is
431repeated. Otherwise, FOLDER is searched with SEARCH-REGEXP and
432the results are presented in an MH-E folder. If FOLDER is \"+\"
433then mail in all folders are searched. Optional argument
434WINDOW-CONFIG stores the window configuration that will be
435restored after the user quits the folder containing the index
436search results."
bdcfe844 437 (interactive
c3d9274a
BW
438 (list current-prefix-arg
439 (progn
bdcfe844 440 (unless mh-find-path-run (mh-find-path))
a66894d8
BW
441 (or (and current-prefix-arg mh-index-sequence-search-flag)
442 (and current-prefix-arg (car mh-index-previous-search))
3d7ca223 443 (mh-prompt-for-folder "Search" "+" nil "all" t)))
bdcfe844
BW
444 (progn
445 ;; Yes, we do want to call mh-index-choose every time in case the
446 ;; user has switched the indexer manually.
447 (unless (mh-index-choose) (error "No indexing program found"))
c3d9274a 448 (or (and current-prefix-arg (cadr mh-index-previous-search))
3d7ca223 449 mh-index-regexp-builder
c3d9274a
BW
450 (read-string (format "%s regexp: "
451 (upcase-initials
3d7ca223
BW
452 (symbol-name mh-indexer))))))
453 (if (and (not
454 (and current-prefix-arg (cadr mh-index-previous-search)))
455 mh-index-regexp-builder)
456 (current-window-configuration)
457 nil)))
a66894d8
BW
458 ;; Redoing a sequence search?
459 (when (and redo-search-flag mh-index-data mh-index-sequence-search-flag
460 (not mh-flists-called-flag))
461 (let ((mh-flists-called-flag t))
462 (apply #'mh-index-sequenced-messages mh-index-previous-search))
463 (return-from mh-index-search))
464 ;; We have fancy query parsing
3d7ca223
BW
465 (when (symbolp search-regexp)
466 (mh-search-folder folder window-config)
467 (setq mh-searching-function 'mh-index-do-search)
468 (return-from mh-index-search))
c3d9274a
BW
469 (mh-checksum-choose)
470 (let ((result-count 0)
3d7ca223 471 (old-window-config (or window-config mh-previous-window-config))
c3d9274a
BW
472 (previous-search mh-index-previous-search)
473 (index-folder (format "%s/%s" mh-index-folder
474 (mh-index-generate-pretty-name search-regexp))))
475 ;; Create a new folder for the search results or recreate the old one...
476 (if (and redo-search-flag mh-index-previous-search)
477 (let ((buffer-name (buffer-name (current-buffer))))
478 (mh-process-or-undo-commands buffer-name)
479 (save-excursion (mh-exec-cmd-quiet nil "rmf" buffer-name))
480 (mh-exec-cmd-quiet nil "folder" "-create" "-fast" buffer-name)
481 (setq index-folder buffer-name))
f0d73c14 482 (setq index-folder (mh-index-new-folder index-folder search-regexp)))
c3d9274a
BW
483
484 (let ((folder-path (format "%s%s" mh-user-path (substring folder 1)))
485 (folder-results-map (make-hash-table :test #'equal))
486 (origin-map (make-hash-table :test #'equal)))
bdcfe844 487 ;; Run search program...
c3d9274a 488 (message "Executing %s... " mh-indexer)
bdcfe844
BW
489 (funcall mh-index-execute-search-function folder-path search-regexp)
490
c3d9274a 491 ;; Parse indexer output
bdcfe844
BW
492 (message "Processing %s output... " mh-indexer)
493 (goto-char (point-min))
c3d9274a 494 (loop for next-result = (funcall mh-index-next-result-function)
924df208 495 while next-result
c3d9274a
BW
496 do (unless (eq next-result 'error)
497 (unless (gethash (car next-result) folder-results-map)
498 (setf (gethash (car next-result) folder-results-map)
499 (make-hash-table :test #'equal)))
500 (setf (gethash (cadr next-result)
501 (gethash (car next-result) folder-results-map))
502 t)))
503
504 ;; Copy the search results over
505 (maphash #'(lambda (folder msgs)
a66894d8
BW
506 (let ((cur (car (mh-translate-range folder "cur")))
507 (msgs (sort (loop for msg being the hash-keys of msgs
c3d9274a
BW
508 collect msg)
509 #'<)))
510 (mh-exec-cmd "refile" msgs "-src" folder
511 "-link" index-folder)
a66894d8
BW
512 ;; Restore cur to old value, that refile changed
513 (when cur
514 (mh-exec-cmd-quiet nil "mark" folder "-add" "-zero"
515 "-sequence" "cur" (format "%s" cur)))
c3d9274a
BW
516 (loop for msg in msgs
517 do (incf result-count)
518 (setf (gethash result-count origin-map)
519 (cons folder msg)))))
520 folder-results-map)
bdcfe844 521
a66894d8 522 ;; Vist the results folder
924df208 523 (mh-visit-folder index-folder () (list folder-results-map origin-map))
c3d9274a 524
bdcfe844
BW
525 (goto-char (point-min))
526 (forward-line)
c3d9274a
BW
527 (mh-update-sequences)
528 (mh-recenter nil)
529
a66894d8
BW
530 ;; Update the speedbar, if needed
531 (when (mh-speed-flists-active-p)
532 (mh-speed-flists t mh-current-folder))
533
c3d9274a 534 ;; Maintain history
3d7ca223 535 (when (or (and redo-search-flag previous-search) window-config)
c3d9274a
BW
536 (setq mh-previous-window-config old-window-config))
537 (setq mh-index-previous-search (list folder search-regexp))
bdcfe844 538
a66894d8
BW
539 ;; Write out data to disk
540 (unless mh-flists-called-flag (mh-index-write-data))
541
bdcfe844
BW
542 (message "%s found %s matches in %s folders"
543 (upcase-initials (symbol-name mh-indexer))
c3d9274a
BW
544 (loop for msg-hash being hash-values of mh-index-data
545 sum (hash-table-count msg-hash))
546 (loop for msg-hash being hash-values of mh-index-data
547 count (> (hash-table-count msg-hash) 0))))))
548
a66894d8
BW
549\f
550
551;;; Functions to serialize index data...
552
553(defun mh-index-write-data ()
554 "Write index data to file."
555 (ignore-errors
556 (unless (eq major-mode 'mh-folder-mode)
5a4aad03 557 (error "Can't be called from folder in \"%s\"" major-mode))
a66894d8
BW
558 (let ((data mh-index-data)
559 (msg-checksum-map mh-index-msg-checksum-map)
560 (checksum-origin-map mh-index-checksum-origin-map)
561 (previous-search mh-index-previous-search)
562 (sequence-search-flag mh-index-sequence-search-flag)
563 (outfile (concat buffer-file-name mh-index-data-file))
564 (print-length nil)
565 (print-level nil))
566 (with-temp-file outfile
567 (mh-index-write-hashtable
568 data (lambda (x) (loop for y being the hash-keys of x collect y)))
569 (mh-index-write-hashtable msg-checksum-map #'identity)
570 (mh-index-write-hashtable checksum-origin-map #'identity)
571 (pp previous-search (current-buffer)) (insert "\n")
572 (pp sequence-search-flag (current-buffer)) (insert "\n")))))
573
574;;;###mh-autoload
575(defun mh-index-read-data ()
576 "Read index data from file."
577 (ignore-errors
578 (unless (eq major-mode 'mh-folder-mode)
5a4aad03 579 (error "Can't be called from folder in \"%s\"" major-mode))
a66894d8
BW
580 (let ((infile (concat buffer-file-name mh-index-data-file))
581 t1 t2 t3 t4 t5)
582 (with-temp-buffer
583 (insert-file-contents-literally infile)
584 (goto-char (point-min))
585 (setq t1 (mh-index-read-hashtable
586 (lambda (data)
587 (loop with table = (make-hash-table :test #'equal)
588 for x in data do (setf (gethash x table) t)
589 finally return table)))
590 t2 (mh-index-read-hashtable #'identity)
591 t3 (mh-index-read-hashtable #'identity)
592 t4 (read (current-buffer))
593 t5 (read (current-buffer))))
594 (setq mh-index-data t1
595 mh-index-msg-checksum-map t2
596 mh-index-checksum-origin-map t3
597 mh-index-previous-search t4
598 mh-index-sequence-search-flag t5))))
599
600(defun mh-index-write-hashtable (table proc)
601 "Write TABLE to `current-buffer'.
2dcf34f9
BW
602PROC is used to serialize the values corresponding to the hash
603table keys."
a66894d8
BW
604 (pp (loop for x being the hash-keys of table
605 collect (cons x (funcall proc (gethash x table))))
606 (current-buffer))
607 (insert "\n"))
608
609(defun mh-index-read-hashtable (proc)
610 "From BUFFER read a hash table serialized as a list.
611PROC is used to convert the value to actual data."
612 (loop with table = (make-hash-table :test #'equal)
613 for pair in (read (current-buffer))
614 do (setf (gethash (car pair) table) (funcall proc (cdr pair)))
615 finally return table))
616
617;;;###mh-autoload
618(defun mh-index-p ()
619 "Non-nil means that this folder was generated by an index search."
620 mh-index-data)
621
3d7ca223
BW
622;;;###mh-autoload
623(defun mh-index-do-search ()
624 "Construct appropriate regexp and call `mh-index-search'."
625 (interactive)
626 (unless (mh-index-choose) (error "No indexing program found"))
627 (let* ((regexp-list (mh-pick-parse-search-buffer))
628 (pattern (funcall mh-index-regexp-builder regexp-list)))
629 (if pattern
630 (mh-index-search nil mh-current-folder pattern
631 mh-previous-window-config)
632 (error "No search terms"))))
633
3d7ca223
BW
634;;;###mh-autoload
635(defun mh-index-parse-search-regexp (input-string)
636 "Construct parse tree for INPUT-STRING.
2dcf34f9
BW
637All occurrences of &, |, ! and ~ in INPUT-STRING are replaced by
638AND, OR and NOT as appropriate. Then the resulting string is
639parsed."
3d7ca223
BW
640 (let (input)
641 (with-temp-buffer
642 (insert input-string)
3d7ca223
BW
643 ;; replace tabs
644 (mh-replace-string "\t" " ")
645 ;; synonyms of AND
a66894d8 646 (mh-replace-string " AND " " and ")
3d7ca223
BW
647 (mh-replace-string "&" " and ")
648 (mh-replace-string " -and " " and ")
649 ;; synonyms of OR
a66894d8 650 (mh-replace-string " OR " " or ")
3d7ca223
BW
651 (mh-replace-string "|" " or ")
652 (mh-replace-string " -or " " or ")
653 ;; synonyms of NOT
a66894d8 654 (mh-replace-string " NOT " " not ")
3d7ca223
BW
655 (mh-replace-string "!" " not ")
656 (mh-replace-string "~" " not ")
657 (mh-replace-string " -not " " not ")
658 ;; synonyms of left brace
659 (mh-replace-string "(" " ( ")
660 (mh-replace-string " -lbrace " " ( ")
661 ;; synonyms of right brace
662 (mh-replace-string ")" " ) ")
663 (mh-replace-string " -rbrace " " ) ")
664 ;; get the normalized input
665 (setq input (format "( %s )" (buffer-substring (point-min) (point-max)))))
666
667 (let ((tokens (mh-index-add-implicit-ops (split-string input)))
668 (op-stack ())
669 (operand-stack ())
670 oper1)
671 (dolist (token tokens)
672 (cond ((equal token "(") (push 'paren op-stack))
673 ((equal token "not") (push 'not op-stack))
674 ((equal token "or") (push 'or op-stack))
675 ((equal token "and") (push 'and op-stack))
676 ((equal token ")")
677 (multiple-value-setq (op-stack operand-stack)
678 (mh-index-evaluate op-stack operand-stack))
679 (when (eq (car op-stack) 'not)
a66894d8 680 (setq op-stack (cdr op-stack))
3d7ca223
BW
681 (push `(not ,(pop operand-stack)) operand-stack))
682 (when (eq (car op-stack) 'and)
a66894d8 683 (setq op-stack (cdr op-stack))
3d7ca223
BW
684 (setq oper1 (pop operand-stack))
685 (push `(and ,(pop operand-stack) ,oper1) operand-stack)))
686 ((eq (car op-stack) 'not)
a66894d8 687 (setq op-stack (cdr op-stack))
3d7ca223
BW
688 (push `(not ,token) operand-stack)
689 (when (eq (car op-stack) 'and)
a66894d8 690 (setq op-stack (cdr op-stack))
3d7ca223
BW
691 (setq oper1 (pop operand-stack))
692 (push `(and ,(pop operand-stack) ,oper1) operand-stack)))
693 ((eq (car op-stack) 'and)
a66894d8 694 (setq op-stack (cdr op-stack))
3d7ca223
BW
695 (push `(and ,(pop operand-stack) ,token) operand-stack))
696 (t (push token operand-stack))))
697 (prog1 (pop operand-stack)
698 (when (or op-stack operand-stack)
699 (error "Invalid regexp: %s" input))))))
700
701(defun mh-index-add-implicit-ops (tokens)
702 "Add implicit operators in the list TOKENS."
703 (let ((result ())
704 (literal-seen nil)
705 current)
706 (while tokens
707 (setq current (pop tokens))
708 (cond ((or (equal current ")") (equal current "and") (equal current "or"))
709 (setq literal-seen nil)
710 (push current result))
711 ((and literal-seen
712 (push "and" result)
713 (setq literal-seen nil)
714 nil))
715 (t
716 (push current result)
717 (unless (or (equal current "(") (equal current "not"))
718 (setq literal-seen t)))))
719 (nreverse result)))
720
721(defun mh-index-evaluate (op-stack operand-stack)
722 "Read expression till starting paren based on OP-STACK and OPERAND-STACK."
723 (block mh-index-evaluate
724 (let (op oper1)
725 (while op-stack
726 (setq op (pop op-stack))
727 (cond ((eq op 'paren)
728 (return-from mh-index-evaluate (values op-stack operand-stack)))
729 ((eq op 'not)
730 (push `(not ,(pop operand-stack)) operand-stack))
731 ((or (eq op 'and) (eq op 'or))
732 (setq oper1 (pop operand-stack))
733 (push `(,op ,(pop operand-stack) ,oper1) operand-stack))))
734 (error "Ran out of tokens"))))
735
c3d9274a
BW
736;;;###mh-autoload
737(defun mh-index-next-folder (&optional backward-flag)
738 "Jump to the next folder marker.
2dcf34f9
BW
739The function is only applicable to folders displaying index search
740results.
741With non-nil optional argument BACKWARD-FLAG, jump to the previous
742group of results."
c3d9274a 743 (interactive "P")
924df208
BW
744 (if (null mh-index-data)
745 (message "Only applicable in an MH-E index search buffer")
c3d9274a
BW
746 (let ((point (point)))
747 (forward-line (if backward-flag -1 1))
748 (cond ((if backward-flag
749 (re-search-backward "^+" (point-min) t)
750 (re-search-forward "^+" (point-max) t))
751 (beginning-of-line))
752 ((and (if backward-flag
753 (goto-char (point-max))
754 (goto-char (point-min)))
755 nil))
756 ((if backward-flag
757 (re-search-backward "^+" (point-min) t)
758 (re-search-forward "^+" (point-max) t))
759 (beginning-of-line))
760 (t (goto-char point))))))
761
762;;;###mh-autoload
763(defun mh-index-previous-folder ()
764 "Jump to the previous folder marker."
bdcfe844 765 (interactive)
c3d9274a
BW
766 (mh-index-next-folder t))
767
768(defun mh-folder-exists-p (folder)
769 "Check if FOLDER exists."
770 (and (mh-folder-name-p folder)
771 (save-excursion
772 (with-temp-buffer
773 (mh-exec-cmd-output "folder" nil "-fast" "-nocreate" folder)
774 (goto-char (point-min))
775 (not (eobp))))))
776
777(defun mh-msg-exists-p (msg folder)
778 "Check if MSG exists in FOLDER."
779 (file-exists-p (format "%s%s/%s" mh-user-path (substring folder 1) msg)))
780
f0d73c14
BW
781(defun mh-index-new-folder (name search-regexp)
782 "Return a folder name based on NAME for search results of SEARCH-REGEXP.
783
2dcf34f9
BW
784If folder NAME already exists and was generated for the same
785SEARCH-REGEXP then it is reused.
f0d73c14 786
2dcf34f9
BW
787Otherwise if the folder NAME was generated from a different
788search then check if NAME<2> can be used. Otherwise try NAME<3>.
789This is repeated till we find a new folder name.
f0d73c14
BW
790
791If the folder returned doesn't exist then it is created."
c3d9274a
BW
792 (unless (mh-folder-name-p name)
793 (error "The argument should be a valid MH folder name"))
f0d73c14
BW
794 (let ((chosen-name
795 (loop for i from 1
796 for candidate = (if (equal i 1) name (format "%s<%s>" name i))
797 when (or (not (mh-folder-exists-p candidate))
798 (equal (mh-index-folder-search-regexp candidate)
799 search-regexp))
800 return candidate)))
801 ;; Do pending refiles/deletes...
802 (when (get-buffer chosen-name)
803 (mh-process-or-undo-commands chosen-name))
804 ;; Recreate folder...
805 (save-excursion (mh-exec-cmd-quiet nil "rmf" chosen-name))
c3d9274a 806 (mh-exec-cmd-quiet nil "folder" "-create" "-fast" chosen-name)
3d7ca223 807 (mh-remove-from-sub-folders-cache chosen-name)
c3d9274a
BW
808 (when (boundp 'mh-speed-folder-map)
809 (mh-speed-add-folder chosen-name))
c3d9274a
BW
810 chosen-name))
811
f0d73c14
BW
812(defun mh-index-folder-search-regexp (folder)
813 "If FOLDER was created by a index search, return the search regexp.
2dcf34f9
BW
814Return nil if FOLDER doesn't exist or the .mhe_index file is
815garbled."
f0d73c14
BW
816 (ignore-errors
817 (with-temp-buffer
818 (insert-file-contents
819 (format "%s%s/%s" mh-user-path (substring folder 1) mh-index-data-file))
820 (goto-char (point-min))
821 (forward-list 3)
822 (cadr (read (current-buffer))))))
823
c3d9274a
BW
824;;;###mh-autoload
825(defun mh-index-insert-folder-headers ()
826 "Annotate the search results with original folder names."
827 (let ((cur-msg (mh-get-msg-num nil))
828 (old-buffer-modified-flag (buffer-modified-p))
829 (buffer-read-only nil)
830 current-folder last-folder)
bdcfe844 831 (goto-char (point-min))
c3d9274a
BW
832 (while (not (eobp))
833 (setq current-folder (car (gethash (gethash (mh-get-msg-num nil)
834 mh-index-msg-checksum-map)
835 mh-index-checksum-origin-map)))
a66894d8 836 (when (and current-folder (not (equal current-folder last-folder)))
c3d9274a
BW
837 (insert (if last-folder "\n" "") current-folder "\n")
838 (setq last-folder current-folder))
839 (forward-line))
f0d73c14
BW
840 (when cur-msg
841 (mh-notate-cur)
842 (mh-goto-msg cur-msg t))
843 (set-buffer-modified-p old-buffer-modified-flag))
844 (mh-index-create-imenu-index))
845
846;;;###mh-autoload
847(defun mh-index-create-imenu-index ()
848 "Create alist of folder names and positions in index folder buffers."
849 (save-excursion
850 (setq which-func-mode t)
851 (let ((alist ()))
852 (goto-char (point-min))
853 (while (re-search-forward "^+" nil t)
854 (save-excursion
855 (beginning-of-line)
856 (push (cons (buffer-substring-no-properties
857 (point) (line-end-position))
858 (set-marker (make-marker) (point)))
859 alist)))
860 (setq imenu--index-alist (nreverse alist)))))
c3d9274a 861
924df208
BW
862;;;###mh-autoload
863(defun mh-index-group-by-folder ()
864 "Partition the messages based on source folder.
2dcf34f9
BW
865Returns an alist with the the folder names in the car and the cdr
866being the list of messages originally from that folder."
924df208
BW
867 (save-excursion
868 (goto-char (point-min))
a66894d8 869 (let ((result-table (make-hash-table :test #'equal)))
924df208
BW
870 (loop for msg being hash-keys of mh-index-msg-checksum-map
871 do (push msg (gethash (car (gethash
872 (gethash msg mh-index-msg-checksum-map)
873 mh-index-checksum-origin-map))
874 result-table)))
875 (loop for x being the hash-keys of result-table
876 collect (cons x (nreverse (gethash x result-table)))))))
877
c3d9274a
BW
878;;;###mh-autoload
879(defun mh-index-delete-folder-headers ()
880 "Delete the folder headers."
881 (let ((cur-msg (mh-get-msg-num nil))
882 (old-buffer-modified-flag (buffer-modified-p))
883 (buffer-read-only nil))
3d7ca223
BW
884 (while (and (not cur-msg) (not (eobp)))
885 (forward-line)
886 (setq cur-msg (mh-get-msg-num nil)))
c3d9274a
BW
887 (goto-char (point-min))
888 (while (not (eobp))
889 (if (or (char-equal (char-after) ?+) (char-equal (char-after) 10))
890 (delete-region (point) (progn (forward-line) (point)))
bdcfe844 891 (forward-line)))
c3d9274a
BW
892 (when cur-msg (mh-goto-msg cur-msg t t))
893 (set-buffer-modified-p old-buffer-modified-flag)))
bdcfe844 894
c3d9274a
BW
895;;;###mh-autoload
896(defun mh-index-visit-folder ()
897 "Visit original folder from where the message at point was found."
bdcfe844 898 (interactive)
c3d9274a
BW
899 (unless mh-index-data
900 (error "Not in an index folder"))
901 (let (folder msg)
902 (save-excursion
903 (cond ((and (bolp) (eolp))
904 (ignore-errors (forward-line -1))
905 (setq msg (mh-get-msg-num t)))
906 ((equal (char-after (line-beginning-position)) ?+)
907 (setq folder (buffer-substring-no-properties
908 (line-beginning-position) (line-end-position))))
909 (t (setq msg (mh-get-msg-num t)))))
910 (when (not folder)
911 (setq folder (car (gethash (gethash msg mh-index-msg-checksum-map)
912 mh-index-checksum-origin-map))))
924df208
BW
913 (when (or (not (get-buffer folder))
914 (y-or-n-p (format "Reuse buffer displaying %s? " folder)))
915 (mh-visit-folder
916 folder (loop for x being the hash-keys of (gethash folder mh-index-data)
917 when (mh-msg-exists-p x folder) collect x)))))
918
c3d9274a
BW
919(defun mh-index-match-checksum (msg folder checksum)
920 "Check if MSG in FOLDER has X-MHE-Checksum header value of CHECKSUM."
921 (with-temp-buffer
922 (mh-exec-cmd-output mh-scan-prog nil "-width" "80"
923 "-format" "%{x-mhe-checksum}\n" folder msg)
924 (goto-char (point-min))
925 (string-equal (buffer-substring-no-properties (point) (line-end-position))
926 checksum)))
927
a66894d8
BW
928(defun mh-index-matching-source-msgs (msgs &optional delete-from-index-data)
929 "Return a table of original messages and folders for messages in MSGS.
2dcf34f9
BW
930If optional argument DELETE-FROM-INDEX-DATA is non-nil, then each
931of the messages, whose counter-part is found in some source
932folder, is removed from `mh-index-data'."
a66894d8
BW
933 (let ((table (make-hash-table :test #'equal)))
934 (dolist (msg msgs)
935 (let* ((checksum (gethash msg mh-index-msg-checksum-map))
936 (pair (gethash checksum mh-index-checksum-origin-map)))
937 (when (and checksum (car pair) (cdr pair)
938 (mh-index-match-checksum (cdr pair) (car pair) checksum))
939 (push (cdr pair) (gethash (car pair) table))
940 (when delete-from-index-data
941 (remhash (cdr pair) (gethash (car pair) mh-index-data))))))
942 table))
943
c3d9274a
BW
944;;;###mh-autoload
945(defun mh-index-execute-commands ()
946 "Delete/refile the actual messages.
2dcf34f9
BW
947The copies in the searched folder are then deleted/refiled to get
948the desired result. Before deleting the messages we make sure
949that the message being deleted is identical to the one that the
950user has marked in the index buffer."
a66894d8
BW
951 (save-excursion
952 (let ((folders ())
953 (mh-speed-flists-inhibit-flag t))
954 (maphash
955 (lambda (folder msgs)
956 (push folder folders)
957 (if (not (get-buffer folder))
958 ;; If source folder not open, just delete the messages...
959 (apply #'mh-exec-cmd "rmm" folder (mh-coalesce-msg-list msgs))
960 ;; Otherwise delete the messages in the source buffer...
961 (save-excursion
962 (set-buffer folder)
963 (let ((old-refile-list mh-refile-list)
964 (old-delete-list mh-delete-list))
965 (setq mh-refile-list nil
966 mh-delete-list msgs)
967 (unwind-protect (mh-execute-commands)
968 (setq mh-refile-list
969 (mapcar (lambda (x)
970 (cons (car x)
971 (loop for y in (cdr x)
972 unless (memq y msgs) collect y)))
973 old-refile-list)
974 mh-delete-list
975 (loop for x in old-delete-list
976 unless (memq x msgs) collect x))
977 (mh-set-folder-modified-p (mh-outstanding-commands-p))
978 (when (mh-outstanding-commands-p)
979 (mh-notate-deleted-and-refiled)))))))
980 (mh-index-matching-source-msgs (append (loop for x in mh-refile-list
981 append (cdr x))
982 mh-delete-list)
983 t))
984 folders)))
985
986;;;###mh-autoload
987(defun mh-index-add-to-sequence (seq msgs)
988 "Add to SEQ the messages in the list MSGS.
2dcf34f9
BW
989This function updates the source folder sequences. Also makes an
990attempt to update the source folder buffer if we have it open."
a66894d8
BW
991 ;; Don't need to do anything for cur
992 (save-excursion
993 (when (and (not (memq seq (mh-unpropagated-sequences)))
994 (mh-valid-seq-p seq))
995 (let ((folders ())
996 (mh-speed-flists-inhibit-flag t))
997 (maphash (lambda (folder msgs)
998 (push folder folders)
999 ;; Add messages to sequence in source folder...
1000 (apply #'mh-exec-cmd-quiet nil "mark" folder
1001 "-add" "-nozero" "-sequence" (symbol-name seq)
1002 (mapcar (lambda (x) (format "%s" x))
1003 (mh-coalesce-msg-list msgs)))
1004 ;; Update source folder buffer if we have it open...
1005 (when (get-buffer folder)
1006 (save-excursion
1007 (set-buffer folder)
1008 (mh-put-msg-in-seq msgs seq))))
1009 (mh-index-matching-source-msgs msgs))
1010 folders))))
1011
1012;;;###mh-autoload
1013(defun mh-index-delete-from-sequence (seq msgs)
1014 "Delete from SEQ the messages in MSGS.
2dcf34f9
BW
1015This function updates the source folder sequences. Also makes an
1016attempt to update the source folder buffer if present."
a66894d8
BW
1017 (save-excursion
1018 (when (and (not (memq seq (mh-unpropagated-sequences)))
1019 (mh-valid-seq-p seq))
1020 (let ((folders ())
1021 (mh-speed-flists-inhibit-flag t))
1022 (maphash (lambda (folder msgs)
1023 (push folder folders)
1024 ;; Remove messages from sequence in source folder...
1025 (apply #'mh-exec-cmd-quiet nil "mark" folder
1026 "-del" "-nozero" "-sequence" (symbol-name seq)
1027 (mapcar (lambda (x) (format "%s" x))
1028 (mh-coalesce-msg-list msgs)))
1029 ;; Update source folder buffer if we have it open...
1030 (when (get-buffer folder)
1031 (save-excursion
1032 (set-buffer folder)
1033 (mh-delete-msg-from-seq msgs seq t))))
1034 (mh-index-matching-source-msgs msgs))
1035 folders))))
bdcfe844
BW
1036
1037\f
1038
3d7ca223
BW
1039;; Pick interface
1040
1041(defvar mh-index-pick-folder)
1042(defvar mh-pick-binary "pick")
1043
1044(defun mh-pick-execute-search (folder-path search-regexp)
1045 "Execute pick.
1046
2dcf34f9
BW
1047Unlike the other index search programs \"pick\" only searches
1048messages present in the folder itself and does not descend into
1049any sub-folders that may be present.
3d7ca223 1050
2dcf34f9
BW
1051In a program, FOLDER-PATH is the directory in which SEARCH-REGEXP
1052is used to search."
3d7ca223
BW
1053 (set-buffer (get-buffer-create mh-index-temp-buffer))
1054 (erase-buffer)
1055 (setq mh-index-pick-folder
1056 (concat "+" (substring folder-path (length mh-user-path))))
1057 (apply #'call-process (expand-file-name "pick" mh-progs) nil '(t nil) nil
1058 mh-index-pick-folder "-list" search-regexp)
1059 (goto-char (point-min)))
1060
1061(defun mh-pick-next-result ()
1062 "Return the next pick search result."
1063 (prog1 (block nil
1064 (when (eobp) (return nil))
1065 (unless (re-search-forward "^[1-9][0-9]*$" (line-end-position) t)
1066 (return 'error))
1067 (list mh-index-pick-folder
1068 (car (read-from-string (buffer-substring-no-properties
1069 (line-beginning-position)
1070 (line-end-position))))
1071 nil))
1072 (forward-line)))
1073
1074\f
1075
bdcfe844
BW
1076;; Grep interface
1077
1078(defvar mh-grep-binary (executable-find "grep"))
1079
1080(defun mh-grep-execute-search (folder-path search-regexp)
1081 "Execute grep and read the results.
e495eaec 1082
2dcf34f9
BW
1083Unlike the other index search programs \"grep\" only searches
1084messages present in the folder itself and does not descend into
1085any sub-folders that may be present.
e495eaec 1086
2dcf34f9
BW
1087In a program, FOLDER-PATH is the directory in which SEARCH-REGEXP
1088is used to search."
bdcfe844
BW
1089 (set-buffer (get-buffer-create mh-index-temp-buffer))
1090 (erase-buffer)
1091 (call-process mh-grep-binary nil '(t nil) nil
1092 "-i" "-r" search-regexp folder-path)
1093 (goto-char (point-min)))
1094
1095(defun mh-grep-next-result ()
1096 "Read the next result.
2dcf34f9
BW
1097Parse it and return the message folder, message index and the
1098match. If no other matches left then return nil. If the current
1099record is invalid return 'error."
bdcfe844 1100 (prog1
c3d9274a
BW
1101 (block nil
1102 (when (eobp)
1103 (return nil))
1104 (let ((eol-pos (line-end-position))
1105 (bol-pos (line-beginning-position))
1106 folder-start msg-end)
1107 (goto-char bol-pos)
1108 (unless (search-forward mh-user-path eol-pos t)
bdcfe844 1109 (return 'error))
c3d9274a
BW
1110 (setq folder-start (point))
1111 (unless (search-forward ":" eol-pos t)
1112 (return 'error))
1113 (let ((match (buffer-substring-no-properties (point) eol-pos)))
1114 (forward-char -1)
1115 (setq msg-end (point))
1116 (unless (search-backward "/" folder-start t)
1117 (return 'error))
1118 (list (format "+%s" (buffer-substring-no-properties
1119 folder-start (point)))
1120 (let ((val (ignore-errors (read-from-string
1121 (buffer-substring-no-properties
1122 (1+ (point)) msg-end)))))
1123 (if (and (consp val) (integerp (car val)))
1124 (car val)
1125 (return 'error)))
1126 match))))
bdcfe844
BW
1127 (forward-line)))
1128
1129\f
1130
3d7ca223
BW
1131;; Mairix interface
1132
1133(defvar mh-mairix-binary (executable-find "mairix"))
1134(defvar mh-mairix-directory ".mairix")
1135(defvar mh-mairix-folder nil)
1136
1137(defun mh-mairix-execute-search (folder-path search-regexp-list)
1138 "Execute mairix and read the results.
1139
2dcf34f9
BW
1140In the examples below, replace \"/home/user/Mail\" with the path
1141to your MH directory.
3d7ca223 1142
2dcf34f9
BW
1143First create the directory \"/home/user/Mail/.mairix\". Then
1144create the file \"/home/user/Mail/.mairix/config\" with the
1145following contents:
3d7ca223 1146
e495eaec 1147 base=/home/user/Mail
a1506d29 1148
e495eaec
BW
1149 # List of folders that should be indexed. 3 dots at the end means there
1150 # are subfolders within the folder
1151 mh=archive...:inbox:drafts:news:sent:trash
a1506d29 1152
e495eaec
BW
1153 vfolder_format=raw
1154 database=/home/user/Mail/mairix/database
3d7ca223 1155
2dcf34f9
BW
1156Use the following command line to generate the mairix index. Run
1157this daily from cron:
3d7ca223 1158
e495eaec 1159 mairix -f /home/user/Mail/.mairix/config
3d7ca223 1160
2dcf34f9
BW
1161In a program, FOLDER-PATH is the directory in which
1162SEARCH-REGEXP-LIST is used to search."
3d7ca223
BW
1163 (set-buffer (get-buffer-create mh-index-temp-buffer))
1164 (erase-buffer)
1165 (unless mh-mairix-binary
1166 (error "Set mh-mairix-binary appropriately"))
1167 (apply #'call-process mh-mairix-binary nil '(t nil) nil
e495eaec 1168 "-r" "-f" (format "%s%s/config" mh-user-path mh-mairix-directory)
3d7ca223
BW
1169 search-regexp-list)
1170 (goto-char (point-min))
1171 (setq mh-mairix-folder
1172 (let ((last-char (substring folder-path (1- (length folder-path)))))
1173 (if (equal last-char "/")
1174 folder-path
1175 (format "%s/" folder-path)))))
1176
1177(defun mh-mairix-next-result ()
1178 "Return next result from mairix output."
1179 (prog1
1180 (block nil
1181 (when (or (eobp) (and (bolp) (eolp)))
1182 (return nil))
1183 (unless (eq (char-after) ?/)
924df208 1184 (return 'error))
3d7ca223
BW
1185 (let ((start (point))
1186 end msg-start)
1187 (setq end (line-end-position))
1188 (unless (search-forward mh-mairix-folder end t)
1189 (return 'error))
1190 (goto-char (match-beginning 0))
1191 (unless (equal (point) start)
1192 (return 'error))
1193 (goto-char end)
1194 (unless (search-backward "/" start t)
1195 (return 'error))
1196 (setq msg-start (1+ (point)))
1197 (goto-char start)
1198 (unless (search-forward mh-user-path end t)
1199 (return 'error))
1200 (list (format "+%s" (buffer-substring-no-properties
1201 (point) (1- msg-start)))
1202 (car (read-from-string
1203 (buffer-substring-no-properties msg-start end)))
1204 ())))
1205 (forward-line)))
1206
1207(defun mh-mairix-regexp-builder (regexp-list)
1208 "Generate query for mairix.
1209REGEXP-LIST is an alist of fields and values."
1210 (let ((result ()))
1211 (dolist (pair regexp-list)
1212 (when (cdr pair)
1213 (push
1214 (concat
1215 (cond ((eq (car pair) 'to) "t:")
1216 ((eq (car pair) 'from) "f:")
1217 ((eq (car pair) 'cc) "c:")
1218 ((eq (car pair) 'subject) "s:")
1219 ((eq (car pair) 'date) "d:")
1220 (t ""))
1221 (let ((sop (cdr (mh-mairix-convert-to-sop* (cdr pair))))
1222 (final ""))
1223 (dolist (conjunct sop)
1224 (let ((expr-list (cdr conjunct))
1225 (expr-string ""))
1226 (dolist (e expr-list)
e495eaec 1227 (setq expr-string (concat expr-string ","
3d7ca223
BW
1228 (if (atom e) "" "~")
1229 (if (atom e) e (cadr e)))))
e495eaec 1230 (setq final (concat final "/" (substring expr-string 1)))))
3d7ca223
BW
1231 (substring final 1)))
1232 result)))
1233 result))
1234
1235(defun mh-mairix-convert-to-sop* (expr)
1236 "Convert EXPR to sum of product form."
1237 (cond ((atom expr) `(or (and ,expr)))
1238 ((eq (car expr) 'or)
1239 (cons 'or
1240 (loop for e in (mapcar #'mh-mairix-convert-to-sop* (cdr expr))
1241 append (cdr e))))
1242 ((eq (car expr) 'and)
1243 (let ((conjuncts (mapcar #'mh-mairix-convert-to-sop* (cdr expr)))
1244 result next-factor)
1245 (setq result (pop conjuncts))
1246 (while conjuncts
1247 (setq next-factor (pop conjuncts))
1248 (setq result (let ((res ()))
1249 (dolist (t1 (cdr result))
1250 (dolist (t2 (cdr next-factor))
1251 (push `(and ,@(cdr t1) ,@(cdr t2)) res)))
1252 (cons 'or res))))
1253 result))
1254 ((atom (cadr expr)) `(or (and ,expr)))
1255 ((eq (caadr expr) 'not) (mh-mairix-convert-to-sop* (cadadr expr)))
1256 ((eq (caadr expr) 'and) (mh-mairix-convert-to-sop*
1257 `(or ,@(mapcar #'(lambda (x) `(not ,x))
1258 (cdadr expr)))))
1259 ((eq (caadr expr) 'or) (mh-mairix-convert-to-sop*
1260 `(and ,@(mapcar #'(lambda (x) `(not ,x))
1261 (cdadr expr)))))
1262 (t (error "Unreachable: %s" expr))))
1263
1264\f
1265
924df208
BW
1266;; Interface to unseen messages script
1267
1268(defvar mh-flists-search-folders)
1269
a66894d8
BW
1270;; XXX: This should probably be in mh-utils.el and used in other places where
1271;; MH-E calls out to /bin/sh.
1272(defun mh-index-quote-for-shell (string)
1273 "Quote STRING for /bin/sh."
1274 (concat "\""
1275 (loop for x across string
1276 concat (format (if (memq x '(?\\ ?` ?$)) "\\%c" "%c") x))
1277 "\""))
1278
924df208 1279(defun mh-flists-execute (&rest args)
a66894d8 1280 "Execute flists.
2dcf34f9
BW
1281Search for messages belonging to `mh-flists-sequence' in the
1282folders specified by `mh-flists-search-folders'. If
1283`mh-recursive-folders-flag' is t, then the folders are searched
1284recursively. All parameters ARGS are ignored."
924df208
BW
1285 (set-buffer (get-buffer-create mh-index-temp-buffer))
1286 (erase-buffer)
1287 (unless (executable-find "sh")
1288 (error "Didn't find sh"))
1289 (with-temp-buffer
a66894d8
BW
1290 (let ((seq (symbol-name mh-flists-sequence)))
1291 (insert "for folder in `" (expand-file-name "flists" mh-progs) " "
1292 (cond ((eq mh-flists-search-folders t)
1293 (mh-index-quote-for-shell mh-inbox))
924df208
BW
1294 ((eq mh-flists-search-folders nil) "")
1295 ((listp mh-flists-search-folders)
1296 (loop for folder in mh-flists-search-folders
a66894d8
BW
1297 concat
1298 (concat " " (mh-index-quote-for-shell folder)))))
924df208 1299 (if mh-recursive-folders-flag " -recurse" "")
a66894d8
BW
1300 " -sequence " seq " -noshowzero -fast` ; do\n"
1301 (expand-file-name "mhpath" mh-progs) " \"+$folder\" " seq "\n"
1302 "done\n"))
924df208
BW
1303 (call-process-region
1304 (point-min) (point-max) "sh" nil (get-buffer mh-index-temp-buffer))))
1305
1306;;;###mh-autoload
a66894d8
BW
1307(defun mh-index-sequenced-messages (folders sequence)
1308 "Display messages from FOLDERS in SEQUENCE.
f0d73c14 1309All messages in the sequence you provide from the folders in
2dcf34f9
BW
1310`mh-new-messages-folders' are listed. With a prefix argument,
1311enter a space-separated list of folders, or nothing to search all
1312folders."
924df208
BW
1313 (interactive
1314 (list (if current-prefix-arg
a8a47814
BW
1315 (split-string (read-string "Search folder(s) (default all): "))
1316 mh-new-messages-folders)
a66894d8
BW
1317 (mh-read-seq-default "Search" nil)))
1318 (unless sequence (setq sequence mh-unseen-seq))
924df208 1319 (let* ((mh-flists-search-folders folders)
a66894d8
BW
1320 (mh-flists-sequence sequence)
1321 (mh-flists-called-flag t)
924df208
BW
1322 (mh-indexer 'flists)
1323 (mh-index-execute-search-function 'mh-flists-execute)
1324 (mh-index-next-result-function 'mh-mairix-next-result)
1325 (mh-mairix-folder mh-user-path)
1326 (mh-index-regexp-builder nil)
a66894d8
BW
1327 (new-folder (format "%s/%s/%s" mh-index-folder
1328 mh-flists-results-folder sequence))
924df208
BW
1329 (window-config (if (equal new-folder mh-current-folder)
1330 mh-previous-window-config
1331 (current-window-configuration)))
a66894d8
BW
1332 (redo-flag nil)
1333 message)
924df208
BW
1334 (cond ((buffer-live-p (get-buffer new-folder))
1335 ;; The destination folder is being visited. Trick `mh-index-search'
a66894d8 1336 ;; into thinking that the folder resulted from a previous search.
924df208 1337 (set-buffer new-folder)
a66894d8 1338 (setq mh-index-previous-search (list folders sequence))
924df208
BW
1339 (setq redo-flag t))
1340 ((mh-folder-exists-p new-folder)
1341 ;; Folder exists but we don't have it open. That means they are
1342 ;; stale results from a old flists search. Clear it out.
1343 (mh-exec-cmd-quiet nil "rmf" new-folder)))
a66894d8
BW
1344 (setq message (mh-index-search redo-flag "+" mh-flists-results-folder
1345 window-config)
1346 mh-index-sequence-search-flag t
1347 mh-index-previous-search (list folders sequence))
1348 (mh-index-write-data)
47570699 1349 (when (stringp message) (message "%s" message))))
a66894d8
BW
1350
1351;;;###mh-autoload
1352(defun mh-index-new-messages (folders)
1353 "Display unseen messages.
a8a47814 1354
5a4aad03 1355If you use a program such as \"procmail\" to use \"rcvstore\" to file
2dcf34f9 1356your incoming mail automatically, you can display new, unseen,
5a4aad03 1357messages using this command. All messages in the \"unseen\"
2dcf34f9
BW
1358sequence from the folders in `mh-new-messages-folders' are
1359listed.
a8a47814 1360
2dcf34f9
BW
1361With a prefix argument, enter a space-separated list of FOLDERS,
1362or nothing to search all folders."
a66894d8
BW
1363 (interactive
1364 (list (if current-prefix-arg
a8a47814
BW
1365 (split-string (read-string "Search folder(s) (default all): "))
1366 mh-new-messages-folders)))
a66894d8
BW
1367 (mh-index-sequenced-messages folders mh-unseen-seq))
1368
1369;;;###mh-autoload
1370(defun mh-index-ticked-messages (folders)
1371 "Display ticked messages.
a8a47814 1372
2dcf34f9
BW
1373All messages in `mh-tick-seq' from the folders in
1374`mh-ticked-messages-folders' are listed.
a8a47814 1375
2dcf34f9
BW
1376With a prefix argument, enter a space-separated list of FOLDERS,
1377or nothing to search all folders."
a66894d8
BW
1378 (interactive
1379 (list (if current-prefix-arg
a8a47814
BW
1380 (split-string (read-string "Search folder(s) (default all): "))
1381 mh-ticked-messages-folders)))
a66894d8 1382 (mh-index-sequenced-messages folders mh-tick-seq))
924df208
BW
1383
1384\f
1385
bdcfe844
BW
1386;; Swish interface
1387
1388(defvar mh-swish-binary (executable-find "swish-e"))
1389(defvar mh-swish-directory ".swish")
1390(defvar mh-swish-folder nil)
1391
c3d9274a 1392;;;###mh-autoload
bdcfe844
BW
1393(defun mh-swish-execute-search (folder-path search-regexp)
1394 "Execute swish-e and read the results.
1395
2dcf34f9
BW
1396In the examples below, replace \"/home/user/Mail\" with the path
1397to your MH directory.
e495eaec 1398
2dcf34f9
BW
1399First create the directory \"/home/user/Mail/.swish\". Then
1400create the file \"/home/user/Mail/.swish/config\" with the
1401following contents:
e495eaec
BW
1402
1403 DefaultContents TXT*
1404 IndexDir /home/user/Mail
1405 IndexFile /home/user/Mail/.swish/index
1406 IndexName \"Mail Index\"
1407 IndexDescription \"Mail Index\"
1408 IndexPointer \"http://nowhere\"
1409 IndexAdmin \"nobody\"
1410 #MetaNames automatic
1411 IndexReport 3
1412 FollowSymLinks no
1413 UseStemming no
1414 IgnoreTotalWordCountWhenRanking yes
1415 WordCharacters abcdefghijklmnopqrstuvwxyz0123456789-
1416 BeginCharacters abcdefghijklmnopqrstuvwxyz
1417 EndCharacters abcdefghijklmnopqrstuvwxyz0123456789
1418 IgnoreLimit 50 1000
1419 IndexComments 0
1420 FileRules filename contains \\D
1421 FileRules pathname contains /home/user/Mail/.swish
1422 FileRules pathname contains /home/user/Mail/mhe-index
1423
2dcf34f9
BW
1424This configuration does not index the folders that hold the
1425results of your searches in \"+mhe-index\" since they tend to be
1426ephemeral and the original messages are indexed anyway.
bdcfe844 1427
2dcf34f9
BW
1428If there are any directories you would like to ignore, append
1429lines like the following to \"config\":
bdcfe844 1430
e495eaec 1431 FileRules pathname contains /home/user/Mail/scripts
c3d9274a 1432
2dcf34f9
BW
1433Use the following command line to generate the swish index. Run
1434this daily from cron:
bdcfe844 1435
e495eaec 1436 swish-e -c /home/user/Mail/.swish/config
bdcfe844 1437
2dcf34f9
BW
1438In a program, FOLDER-PATH is the directory in which SEARCH-REGEXP
1439is used to search."
bdcfe844
BW
1440 (set-buffer (get-buffer-create mh-index-temp-buffer))
1441 (erase-buffer)
1442 (unless mh-swish-binary
1443 (error "Set mh-swish-binary appropriately"))
1444 (call-process mh-swish-binary nil '(t nil) nil
1445 "-w" search-regexp
1446 "-f" (format "%s%s/index" mh-user-path mh-swish-directory))
1447 (goto-char (point-min))
1448 (setq mh-swish-folder
1449 (let ((last-char (substring folder-path (1- (length folder-path)))))
1450 (if (equal last-char "/")
1451 folder-path
1452 (format "%s/" folder-path)))))
1453
1454(defun mh-swish-next-result ()
1455 "Get the next result from swish output."
1456 (prog1
1457 (block nil
1458 (when (or (eobp) (equal (char-after (point)) ?.))
1459 (return nil))
1460 (when (equal (char-after (point)) ?#)
1461 (return 'error))
1462 (let* ((start (search-forward " " (line-end-position) t))
1463 (end (search-forward " " (line-end-position) t)))
1464 (unless (and start end)
1465 (return 'error))
1466 (setq end (1- end))
1467 (unless (file-exists-p (buffer-substring-no-properties start end))
1468 (return 'error))
1469 (unless (search-backward "/" start t)
1470 (return 'error))
1471 (list (let* ((s (buffer-substring-no-properties start (1+ (point)))))
1472 (unless (string-match mh-swish-folder s)
1473 (return 'error))
e495eaec
BW
1474 (if (and (string-match mh-user-path s)
1475 (< (match-end 0) (1- (length s))))
bdcfe844
BW
1476 (format "+%s"
1477 (substring s (match-end 0) (1- (length s))))
1478 (return 'error)))
1479 (let* ((s (buffer-substring-no-properties (1+ (point)) end))
1480 (val (ignore-errors (read-from-string s))))
1481 (if (and (consp val) (numberp (car val)))
1482 (car val)
1483 (return 'error)))
1484 nil)))
1485 (forward-line)))
1486
1487\f
1488
1489;; Swish++ interface
1490
1491(defvar mh-swish++-binary (or (executable-find "search++")
c3d9274a 1492 (executable-find "search")))
bdcfe844
BW
1493(defvar mh-swish++-directory ".swish++")
1494
c3d9274a 1495;;;###mh-autoload
bdcfe844
BW
1496(defun mh-swish++-execute-search (folder-path search-regexp)
1497 "Execute swish++ and read the results.
1498
2dcf34f9
BW
1499In the examples below, replace \"/home/user/Mail\" with the path to
1500your MH directory.
bdcfe844 1501
2dcf34f9
BW
1502First create the directory \"/home/user/Mail/.swish++\". Then create
1503the file \"/home/user/Mail/.swish++/swish++.conf\" with the following
1504contents:
bdcfe844 1505
e495eaec
BW
1506 IncludeMeta Bcc Cc Comments Content-Description From Keywords
1507 IncludeMeta Newsgroups Resent-To Subject To
1508 IncludeMeta Message-Id References In-Reply-To
1509 IncludeFile Mail *
1510 IndexFile /home/user/Mail/.swish++/swish++.index
bdcfe844 1511
2dcf34f9
BW
1512Use the following command line to generate the swish index. Run
1513this daily from cron:
bdcfe844 1514
e495eaec
BW
1515 find /home/user/Mail -path /home/user/Mail/mhe-index -prune \\
1516 -o -path /home/user/Mail/.swish++ -prune \\
1517 -o -name \"[0-9]*\" -print \\
1518 | index -c /home/user/Mail/.swish++/swish++.conf -
c3d9274a 1519
2dcf34f9
BW
1520This command does not index the folders that hold the results of your
1521searches in \"+mhe-index\" since they tend to be ephemeral and the
1522original messages are indexed anyway.
bdcfe844 1523
2dcf34f9
BW
1524On some systems (Debian GNU/Linux, for example), use \"index++\"
1525instead of \"index\".
bdcfe844 1526
2dcf34f9
BW
1527In a program, FOLDER-PATH is the directory in which SEARCH-REGEXP is
1528used to search."
bdcfe844
BW
1529 (set-buffer (get-buffer-create mh-index-temp-buffer))
1530 (erase-buffer)
1531 (unless mh-swish++-binary
1532 (error "Set mh-swish++-binary appropriately"))
1533 (call-process mh-swish++-binary nil '(t nil) nil
1534 "-m" "10000"
1535 (format "-i%s%s/swish++.index"
1536 mh-user-path mh-swish++-directory)
1537 search-regexp)
1538 (goto-char (point-min))
1539 (setq mh-swish-folder
1540 (let ((last-char (substring folder-path (1- (length folder-path)))))
1541 (if (equal last-char "/")
1542 folder-path
1543 (format "%s/" folder-path)))))
1544
1545(defalias 'mh-swish++-next-result 'mh-swish-next-result)
1546
3d7ca223
BW
1547(defun mh-swish++-regexp-builder (regexp-list)
1548 "Generate query for swish++.
1549REGEXP-LIST is an alist of fields and values."
924df208 1550 (let ((regexp ""))
3d7ca223
BW
1551 (dolist (elem regexp-list)
1552 (when (cdr elem)
1553 (setq regexp (concat regexp " and "
1554 (if (car elem) "(" "")
1555 (if (car elem) (symbol-name (car elem)) "")
1556 (if (car elem) " = " "")
1557 (mh-swish++-print-regexp (cdr elem))
1558 (if (car elem) ")" "")))))
1559 (substring regexp 4)))
1560
1561(defun mh-swish++-print-regexp (expr)
1562 "Return infix expression corresponding to EXPR."
1563 (cond ((atom expr) (format "%s" expr))
1564 ((eq (car expr) 'not)
1565 (format "(not %s)" (mh-swish++-print-regexp (cadr expr))))
1566 (t (format "(%s %s %s)" (mh-swish++-print-regexp (cadr expr))
1567 (symbol-name (car expr))
1568 (mh-swish++-print-regexp (caddr expr))))))
1569
bdcfe844
BW
1570\f
1571
1572;; Namazu interface
1573
1574(defvar mh-namazu-binary (executable-find "namazu"))
1575(defvar mh-namazu-directory ".namazu")
1576(defvar mh-namazu-folder nil)
1577
c3d9274a 1578;;;###mh-autoload
bdcfe844
BW
1579(defun mh-namazu-execute-search (folder-path search-regexp)
1580 "Execute namazu and read the results.
1581
2dcf34f9
BW
1582In the examples below, replace \"/home/user/Mail\" with the path to
1583your MH directory.
bdcfe844 1584
2dcf34f9
BW
1585First create the directory \"/home/user/Mail/.namazu\". Then create
1586the file \"/home/user/Mail/.namazu/mknmzrc\" with the following
1587contents:
bdcfe844 1588
e495eaec
BW
1589 package conf; # Don't remove this line!
1590 $ADDRESS = 'user@localhost';
1591 $ALLOW_FILE = \"[0-9]*\";
1592 $EXCLUDE_PATH = \"^/home/user/Mail/(mhe-index|spam)\";
c3d9274a 1593
2dcf34f9
BW
1594This configuration does not index the folders that hold the results of
1595your searches in \"+mhe-index\" since they tend to be ephemeral and
1596the original messages are indexed anyway.
c3d9274a 1597
2dcf34f9
BW
1598Use the following command line to generate the namazu index. Run this
1599daily from cron:
bdcfe844 1600
e495eaec
BW
1601 mknmz -f /home/user/Mail/.namazu/mknmzrc -O /home/user/Mail/.namazu \\
1602 /home/user/Mail
bdcfe844 1603
2dcf34f9
BW
1604In a program, FOLDER-PATH is the directory in which SEARCH-REGEXP
1605is used to search."
bdcfe844 1606 (let ((namazu-index-directory
c3d9274a 1607 (format "%s%s" mh-user-path mh-namazu-directory)))
bdcfe844
BW
1608 (unless (file-exists-p namazu-index-directory)
1609 (error "Namazu directory %s not present" namazu-index-directory))
1610 (unless (executable-find mh-namazu-binary)
1611 (error "Set mh-namazu-binary appropriately"))
1612 (set-buffer (get-buffer-create mh-index-temp-buffer))
1613 (erase-buffer)
1614 (call-process mh-namazu-binary nil '(t nil) nil
1615 "-alR" search-regexp namazu-index-directory)
1616 (goto-char (point-min))
1617 (setq mh-namazu-folder
1618 (let ((last (substring folder-path (1- (length folder-path)))))
1619 (if (equal last "/")
1620 folder-path
1621 (format "%s/" folder-path))))))
1622
1623(defun mh-namazu-next-result ()
1624 "Get the next result from namazu output."
1625 (prog1
1626 (block nil
1627 (when (eobp) (return nil))
1628 (let ((file-name (buffer-substring-no-properties
1629 (point) (line-end-position))))
1630 (unless (equal (string-match mh-namazu-folder file-name) 0)
1631 (return 'error))
1632 (unless (file-exists-p file-name)
1633 (return 'error))
1634 (string-match mh-user-path file-name)
1635 (let* ((folder/msg (substring file-name (match-end 0)))
c3d9274a 1636 (mark (mh-search-from-end ?/ folder/msg)))
bdcfe844
BW
1637 (unless mark (return 'error))
1638 (list (format "+%s" (substring folder/msg 0 mark))
1639 (let ((n (ignore-errors (read-from-string
1640 (substring folder/msg (1+ mark))))))
1641 (if (and (consp n) (numberp (car n)))
1642 (car n)
1643 (return 'error)))
1644 nil))))
1645 (forward-line)))
1646
1647\f
1648
924df208 1649;;;###mh-autoload
bdcfe844
BW
1650(defun mh-index-choose ()
1651 "Choose an indexing function.
2dcf34f9
BW
1652The side-effects of this function are that the variables
1653`mh-indexer', `mh-index-execute-search-function', and
1654`mh-index-next-result-function' are set according to the first
1655indexer in `mh-indexer-choices' present on the system."
bdcfe844
BW
1656 (block nil
1657 ;; The following favors the user's preference; otherwise, the last
1658 ;; automatically chosen indexer is used for efficiency rather than going
1659 ;; through the list.
1660 (let ((program-alist (cond (mh-index-program
1661 (list
c3d9274a 1662 (assoc mh-index-program mh-indexer-choices)))
bdcfe844
BW
1663 (mh-indexer
1664 (list (assoc mh-indexer mh-indexer-choices)))
1665 (t mh-indexer-choices))))
1666 (while program-alist
1667 (let* ((current (pop program-alist))
1668 (executable (symbol-value (cadr current))))
1669 (when executable
1670 (setq mh-indexer (car current))
3d7ca223
BW
1671 (setq mh-index-execute-search-function (nth 2 current))
1672 (setq mh-index-next-result-function (nth 3 current))
1673 (setq mh-index-regexp-builder (nth 4 current))
bdcfe844
BW
1674 (return mh-indexer))))
1675 nil)))
1676
1677\f
1678
bdcfe844
BW
1679(provide 'mh-index)
1680
cee9f5c6
BW
1681;; Local Variables:
1682;; indent-tabs-mode: nil
1683;; sentence-end-double-space: nil
1684;; End:
bdcfe844 1685
cee9f5c6 1686;; arch-tag: 607762ad-0dff-4fe1-a27e-6c0dde0dcc47
bdcfe844 1687;;; mh-index ends here