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