Debian Codegraph @

mew-search.el

raw
1 ;;; mew-search.el --- Index Search
2
3 ;; Author: Kazu Yamamoto <[email protected]>
4 ;; Created: Aug 24, 2005
5
6 ;;; Code:
7
8 (require 'mew)
9
10 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
11 ;;;
12 ;;; Variables
13 ;;;
14
15 (defvar mew-prog-spotlight "mdfind")
16 (defvar mew-prog-wds "wdsgrep.exe")
17 (defvar mew-prog-google "gdgrep.rb")
18 (defvar mew-prog-est "estcmd")
19
20 (mew-defstruct search
21 key name prog
22 func-search func-virtual
23 func-index-folder func-index-all
24 func-canonicalize-pattern
25 func-register func-unregister func-filter)
26
27 (defvar mew-search-switch
28 `((spotlight "Spotlight" ,mew-prog-spotlight
29 mew-search-with-spotlight mew-search-virtual-with-spotlight
30 mew-spotlight-index-folder mew-spotlight-index-all
31 mew-pick-canonicalize-pattern-spotlight
32 nil nil)
33 (wds "WDS" ,mew-prog-wds
34 mew-search-with-wds mew-search-virtual-with-wds
35 mew-wds-index-folder mew-wds-index-all
36 mew-pick-canonicalize-pattern-wds
37 mew-wds-register mew-wds-unregister)
38 (google "Google" ,mew-prog-google
39 mew-search-with-google mew-search-virtual-with-google
40 mew-google-index-folder mew-google-index-all
41 mew-pick-canonicalize-pattern-google
42 mew-google-register mew-google-unregister)
43 (est "Hyper Estraier" ,mew-prog-est
44 mew-search-with-est mew-search-virtual-with-est
45 mew-est-index-folder mew-est-index-all
46 mew-pick-canonicalize-pattern-est
47 nil nil
48 mew-est-input-filter)))
49
50 (defun mew-search-get-list (func)
51 (let ((sw mew-search-switch)
52 ent ret)
53 (while sw
54 (setq ent (car sw))
55 (setq sw (cdr sw))
56 (if (mew-which-exec (mew-search-get-prog ent))
57 (setq ret (cons (funcall func ent) ret))))
58 (nreverse ret)))
59
60 (defun mew-search-get-ent (method)
61 (assoc method mew-search-switch))
62
63 (defvar mew-search-method (car (mew-search-get-list 'mew-search-get-key)))
64
65 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
66 ;;;
67 ;;; Launcher
68 ;;;
69
70 (defun mew-summary-search-change-method ()
71 "Change a search method."
72 (interactive)
73 (let* ((names (mew-search-get-list 'mew-search-get-name))
74 (ent (mew-search-get-ent mew-search-method))
75 (default (mew-search-get-name ent))
76 name)
77 (if (not names)
78 (message "No search method")
79 (setq names (mapcar (lambda (x) (list x)) names))
80 (setq name (completing-read (format "Search method (%s): " default)
81 names nil t nil nil default))
82 (setq mew-search-method (nth 0 (mew-assoc-equal name mew-search-switch 1))))))
83
84 (defun mew-summary-search ()
85 "Pick messages according to a specified pick pattern
86 with a search method. Then put the '*' mark onto them. "
87 (interactive)
88 (mew-pickable
89 (if (not mew-search-method)
90 (message "No search method")
91 (let* ((ent (mew-search-get-ent mew-search-method))
92 (func (mew-search-get-func-search ent))
93 (name (mew-search-get-name ent))
94 (canon-func (mew-search-get-func-canonicalize-pattern ent))
95 (flt-func (mew-search-get-func-filter ent))
96 (folder (mew-summary-physical-folder))
97 pattern msgs filter)
98 (if (not (fboundp func))
99 (message "This command cannot be used")
100 (setq pattern (if flt-func
101 (read-string (concat name " pick pattern: "))
102 (mew-input-pick-pattern (concat name " pick"))))
103 (if (and (string= pattern "") (not (fboundp flt-func)))
104 (message (mew-substitute-for-summary "Keyword must be specified. You may use '\\[mew-summary-pick]' instead"))
105 (when (and canon-func (fboundp canon-func))
106 (setq pattern (funcall canon-func pattern)))
107 (if (fboundp flt-func)
108 (setq filter (funcall flt-func)))
109 (setq msgs (funcall func pattern folder filter))
110 (mew-summary-pick-ls folder msgs)))))))
111
112 (defun mew-summary-selection-by-search (&optional ask-folder)
113 "Making selection according to a specified pick pattern
114 with a search method."
115 (interactive "P")
116 (if (not mew-search-method)
117 (message "No search method")
118 (let* ((ofolder (mew-summary-folder-name 'ext))
119 (ent (mew-search-get-ent mew-search-method))
120 (func (mew-search-get-func-virtual ent))
121 (name (mew-search-get-name ent))
122 (canon-func (mew-search-get-func-canonicalize-pattern ent))
123 (flt-func (mew-search-get-func-filter ent))
124 vfolder opattern pattern dfunc file opts rttl file-rttl flds filter)
125 (if (not (fboundp func))
126 (message "This command cannot be used")
127 (if ask-folder
128 (setq flds (mew-input-folders
129 (or (mew-summary-folder-name)
130 (mew-case-folder
131 mew-case
132 (mew-proto-inbox-folder (mew-proto mew-case)))))))
133 (setq opattern (if flt-func
134 (read-string (concat name " virtual pattern: "))
135 (mew-input-pick-pattern (concat name " virtual"))))
136 (if (and (string= opattern "") (not (fboundp flt-func)))
137 (message (mew-substitute-for-summary "Keyword must be specified"))
138 (if (string= opattern "") (setq opattern " "))
139 (if (and canon-func (fboundp canon-func))
140 (setq pattern (funcall canon-func opattern))
141 (setq pattern opattern))
142 (when (fboundp flt-func)
143 (setq filter (funcall flt-func))
144 (if (string= opattern " ") (setq opattern ""))
145 (setq opattern (concat opattern filter)))
146 (setq vfolder (mew-folder-to-selection opattern))
147 (mew-summary-switch-to-folder vfolder)
148 (mew-vinfo-set-mode 'selection)
149 (mew-vinfo-set-physical-folder nil)
150 (mew-vinfo-set-original-folder ofolder)
151 (mew-sinfo-set-find-key opattern)
152 (make-local-variable 'mew-summary-form-mark-delete)
153 (setq mew-summary-form-mark-delete nil)
154 (make-local-variable 'mew-summary-form-mark-spam)
155 (setq mew-summary-form-mark-spam nil)
156 (when (mew-summary-exclusive-p)
157 (with-temp-buffer
158 (mew-set-buffer-multibyte t)
159 (mew-piolet mew-cs-text-for-read mew-cs-text-for-write
160 (setq file-rttl (funcall func pattern flds filter)))))
161 (mew-set '(file rttl) file-rttl)
162 (setq dfunc `(lambda () (mew-delete-file ,file)))
163 (setq opts (list "-i" file))
164 (mew-local-retrieve 'vir opts dfunc nil nil rttl))))))
165
166 (defun mew-summary-make-index-folder ()
167 "Make index for this folder."
168 (interactive)
169 (mew-summary-only
170 (if (not mew-search-method)
171 (message "No search method")
172 (let* ((ent (mew-search-get-ent mew-search-method))
173 (func (mew-search-get-func-index-folder ent))
174 (folder (mew-summary-folder-name 'ext)))
175 (if (not (fboundp func))
176 (message "This command cannot be used")
177 (funcall func folder))))))
178
179 (defun mew-summary-make-index-all ()
180 "Make index for all folders."
181 (interactive)
182 (if (not mew-search-method)
183 (message "No search method")
184 (let* ((ent (mew-search-get-ent mew-search-method))
185 (func (mew-search-get-func-index-all ent)))
186 (if (fboundp func)
187 (funcall func)
188 (message "This command cannot be used")))))
189
190 (defun mew-summary-search-register ()
191 (interactive)
192 (if (not mew-search-method)
193 (message "No search method")
194 (let* ((ent (mew-search-get-ent mew-search-method))
195 (func (mew-search-get-func-register ent)))
196 (if (not (fboundp func))
197 (message "This command cannot be used")
198 (funcall func)))))
199
200 (defun mew-summary-search-unregister ()
201 (interactive)
202 (if (not mew-search-method)
203 (message "No search method")
204 (let* ((ent (mew-search-get-ent mew-search-method))
205 (func (mew-search-get-func-unregister ent)))
206 (if (not (fboundp func))
207 (message "This command cannot be used")
208 (funcall func)))))
209
210 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
211 ;;;
212 ;;; Spotlight
213 ;;;
214
215 (defun mew-assoc-match-list (key alist nth)
216 (let ((case-fold-search t) a n ret)
217 (while alist
218 (setq a (car alist))
219 (setq n (nth nth a))
220 (if (or (and (stringp n) (string-match key n))
221 (equal n key) (eq n t))
222 (setq ret (cons a ret)))
223 (setq alist (cdr alist)))
224 ret)) ;; not reversed
225
226 (defun mew-expand-wildcard-folder (wlist)
227 (let (case fld ent ret flds regex alist)
228 (while wlist
229 (setq ent (car wlist))
230 (setq wlist (cdr wlist))
231 (setq case (mew-case:folder-case ent))
232 (setq fld (mew-case:folder-folder ent))
233 (if (not (string-match "\\*$" fld))
234 (setq ret (cons ent ret))
235 (setq regex (substring fld 0 -1))
236 (setq regex (concat "^" (regexp-quote regex)))
237 (cond
238 ((mew-folder-popp fld)
239 (setq alist (mew-pop-folder-alist)))
240 ((mew-folder-nntpp fld)
241 (setq alist (mew-nntp-folder-alist case)))
242 ((mew-folder-imapp fld)
243 (setq alist (mew-imap-folder-alist case)))
244 (t
245 (setq alist (mew-local-folder-alist))))
246 (setq flds (mew-assoc-match-list regex alist 0))
247 (setq flds (mapcar (lambda (x) (mew-case-folder case (nth 0 x))) flds))
248 (setq ret (nconc flds ret))))
249 (nreverse ret)))
250
251 (defun mew-search-spotlight (pattern path)
252 (setq pattern (mew-cs-encode-string pattern 'utf-8))
253 (let* ((ent (mew-search-get-ent mew-search-method))
254 (prog (mew-search-get-prog ent)))
255 (mew-plet
256 (mew-alet
257 (call-process prog nil t nil "-onlyin" path pattern)))))
258
259 (defun mew-search-with-spotlight (pattern folder &optional _dummy)
260 (let ((path (mew-expand-folder folder))
261 msgs)
262 (with-temp-buffer
263 (mew-set-buffer-multibyte t)
264 (mew-search-spotlight pattern path)
265 (goto-char (point-min))
266 (while (re-search-forward mew-regex-message-files5 nil t)
267 (setq msgs (cons (mew-match-string 1) msgs))
268 (forward-line))
269 (setq msgs (nreverse msgs))
270 (setq msgs (sort (mapcar 'string-to-number msgs) '<))
271 (mapcar 'number-to-string msgs))))
272
273 (defun mew-search-virtual-with-spotlight (pattern flds &optional _dummy)
274 (let* ((mpath (mew-expand-folder mew-folder-local))
275 (mail-regex (regexp-quote (file-name-as-directory mpath)))
276 (regex (format "^%s%s\\([0-9]+\\)\\(%s\\)?$" mail-regex (file-name-as-directory "\\(.*\\)") mew-suffix))
277 (file (mew-make-temp-name))
278 (prev "") (rttl 0) (n 1) crnt path)
279 (unless flds (setq flds (list mew-folder-local)))
280 (setq flds (mew-expand-wildcard-folder flds))
281 (while flds
282 (setq path (mew-expand-folder (car flds)))
283 (setq flds (cdr flds))
284 (mew-search-spotlight pattern path)
285 (goto-char (point-min))
286 (while (not (eobp))
287 (when (looking-at regex)
288 (setq rttl (1+ rttl))
289 (setq crnt (match-string 1))
290 (delete-region (match-beginning 0) (match-beginning 2))
291 (when (not (string= crnt prev))
292 (beginning-of-line)
293 (insert "CD:" mew-folder-local crnt "\n"))
294 (setq prev crnt)
295 (forward-line)))
296 (mew-frwlet mew-cs-text-for-read mew-cs-text-for-write
297 (write-region (point-min) (point-max) file (> n 1) 'no-msg))
298 (mew-erase-buffer)
299 (setq n (1+ n)))
300 (list file rttl)))
301
302 (defun mew-spotlight-index (dir)
303 (let* ((default-directory dir)
304 (msgs (mew-dir-messages dir mew-regex-message-files3 'full))
305 dirent subdir)
306 (while msgs
307 (mew-set-file-type (car msgs))
308 (setq msgs (cdr msgs)))
309 (when (/= (mew-file-get-links dir) 2)
310 (setq dirent (directory-files "." nil mew-regex-folder-candidate))
311 (while dirent
312 (setq subdir (car dirent))
313 (setq dirent (cdr dirent))
314 (when (and (file-directory-p subdir)
315 (not (file-symlink-p subdir)))
316 (mew-spotlight-index (expand-file-name subdir dir)))))))
317
318 (defun mew-spotlight-index-folder (folder)
319 "Making spotlight index for this folder."
320 (let* ((dir (mew-expand-folder folder))
321 (msgs (mew-dir-messages dir mew-regex-message-files3 'full)))
322 (message "Spotlight indexing for %s..." folder)
323 (while msgs
324 (mew-set-file-type (car msgs))
325 (setq msgs (cdr msgs)))
326 (message "Spotlight indexing for %s...done" folder)))
327
328 (defun mew-spotlight-index-all ()
329 "Making spotlight index for all messages."
330 (interactive)
331 (when (y-or-n-p "Make Spotlight index for all folders? ")
332 (message "Spotlight indexing for all folders...")
333 (let ((flds '("+" "+#imap" "+#pop" "+#nntp"))
334 dir)
335 (while flds
336 (setq dir (mew-expand-folder (car flds)))
337 (setq flds (cdr flds))
338 (if (file-directory-p dir) (mew-spotlight-index dir))))
339 (message "Spotlight indexing for all folders...done")))
340
341 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
342 ;;;
343 ;;; Google Desktop
344 ;;;
345
346 (defun mew-search-google (pattern path)
347 (setq pattern (mew-cs-encode-string pattern 'shift_jis))
348 (let* ((ent (mew-search-get-ent mew-search-method))
349 (prog (mew-search-get-prog ent)))
350 (mew-plet
351 (mew-alet
352 (call-process prog nil t nil "-m" "-p" path "-s" "-q" pattern)))))
353
354 (defun mew-search-with-google (pattern folder &optional _dummy)
355 (let* ((path (mew-expand-folder folder))
356 msgs)
357 (with-temp-buffer
358 (mew-set-buffer-multibyte t)
359 (mew-search-google pattern path)
360 (goto-char (point-min))
361 (while (re-search-forward mew-regex-message-files5 nil t)
362 (setq msgs (cons (mew-match-string 1) msgs))
363 (forward-line))
364 (setq msgs (nreverse msgs))
365 (setq msgs (sort (mapcar 'string-to-number msgs) '<))
366 (mapcar 'number-to-string msgs))))
367
368 ;; xxx flds are not used at this moment
369 (defun mew-search-virtual-with-google (pattern _flds &optional _dummy)
370 (let* ((path (mew-expand-folder mew-folder-local))
371 (mail-regex (regexp-quote (file-name-as-directory path)))
372 (regex (concat "^" mail-regex "\\(.*\\)/" "\\([0-9]+\\)"))
373 (file (mew-make-temp-name))
374 (prev "") (rttl 0)
375 crnt)
376 (mew-search-google pattern path)
377 (goto-char (point-min))
378 (while (not (eobp))
379 (when (looking-at regex)
380 (setq rttl (1+ rttl))
381 (setq crnt (match-string 1))
382 (delete-region (match-beginning 0) (match-beginning 2))
383 (when (not (string= crnt prev))
384 (beginning-of-line)
385 (insert "CD:" mew-folder-local crnt "\n"))
386 (setq prev crnt)
387 (forward-line)))
388 (mew-frwlet mew-cs-text-for-read mew-cs-text-for-write
389 (write-region (point-min) (point-max) file nil 'no-msg))
390 (list file rttl)))
391
392 (defun mew-google-index-folder (folder)
393 "Make Google index for this folder."
394 (let* ((ent (mew-search-get-ent mew-search-method))
395 (prog (mew-search-get-prog ent)))
396 (start-process prog nil prog "-m" "-i" (mew-expand-folder folder))
397 (message "Google indexing for %s in background..." folder)))
398
399 (defun mew-google-index-all ()
400 "Make Google index for all folders."
401 (interactive)
402 (let* ((ent (mew-search-get-ent mew-search-method))
403 (prog (mew-search-get-prog ent)))
404 (start-process prog nil prog "-m" "-r" "-i" (mew-expand-folder "+"))
405 (message "Google indexing for all folders in background...")))
406
407 (defun mew-google-register ()
408 "Register Google component"
409 (interactive)
410 (message "Registering Google component...")
411 (call-process mew-prog-google nil nil nil "-R")
412 (message "Registering Google component...done"))
413
414 (defun mew-google-unregister ()
415 "Unregister Google component"
416 (interactive)
417 (message "Unregistering Google component...")
418 (call-process mew-prog-google nil nil nil "-U")
419 (message "Unregistering Google component...done"))
420
421
422 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
423 ;;;
424 ;;; WDS Desktop
425 ;;;
426
427 (defun mew-search-wds (pattern path)
428 (setq pattern (mew-cs-encode-string pattern default-file-name-coding-system))
429 (let* ((ent (mew-search-get-ent mew-search-method))
430 (prog (mew-search-get-prog ent)))
431 (mew-plet
432 (mew-alet
433 (call-process prog nil t nil "-e" mew-suffix "-p" path "-s" pattern)))))
434
435 (defun mew-search-with-wds (pattern folder &optional _dummy)
436 (let* ((path (mew-expand-folder folder))
437 msgs)
438 (with-temp-buffer
439 (mew-set-buffer-multibyte t)
440 (mew-search-wds pattern path)
441 (goto-char (point-min))
442 (while (re-search-forward mew-regex-message-files5 nil t)
443 (setq msgs (cons (mew-match-string 1) msgs))
444 (forward-line))
445 (setq msgs (nreverse msgs))
446 (setq msgs (sort (mapcar 'string-to-number msgs) '<))
447 (mapcar 'number-to-string msgs))))
448
449 ;; xxx flds are not used at this moment
450 (defun mew-search-virtual-with-wds (pattern _flds &optional _dummy)
451 (let* ((path (mew-expand-folder mew-folder-local))
452 (mail-regex (regexp-quote (file-name-as-directory path)))
453 (regex (concat "^" mail-regex "\\(.*\\)/" "\\([0-9]+\\)"))
454 (file (mew-make-temp-name))
455 (prev "") (rttl 0)
456 crnt)
457 (mew-search-wds pattern path)
458 (goto-char (point-min))
459 (while (not (eobp))
460 (when (looking-at regex)
461 (setq rttl (1+ rttl))
462 (setq crnt (match-string 1))
463 (delete-region (match-beginning 0) (match-beginning 2))
464 (when (not (string= crnt prev))
465 (beginning-of-line)
466 (insert "CD:" mew-folder-local crnt "\n"))
467 (setq prev crnt))
468 (forward-line))
469 (mew-frwlet mew-cs-text-for-read mew-cs-text-for-write
470 (write-region (point-min) (point-max) file nil 'no-msg))
471 (list file rttl)))
472
473 (defun mew-wds-index-folder (_folder)
474 "Make WDS index for all folders."
475 (mew-wds-index-all))
476
477 (defun mew-wds-index-all ()
478 "Make WDS index for all folders."
479 (interactive)
480 (let* ((ent (mew-search-get-ent mew-search-method))
481 (prog (mew-search-get-prog ent)))
482 (start-process prog nil prog "-R" (format "%s=%s" mew-suffix ".eml"))
483 (message "WDS indexing for all folders in background...")))
484
485 (defun mew-wds-register ()
486 "Register '.mew' to WDS"
487 (interactive)
488 (message "Registering suffix '%s' to WDS..." mew-suffix)
489 (call-process mew-prog-wds nil nil nil "-R" (format "%s=%s" mew-suffix ".eml"))
490 (message "Registering suffix '%s' to WDS...done" mew-suffix))
491
492 (defun mew-wds-unregister ()
493 "Unregister '.mew' from WDS"
494 (interactive)
495 (message "This command cannot be used"))
496
497 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
498 ;;;
499 ;;; Hyper Estraier
500 ;;;
501
502 (defun mew-est-input-filter ()
503 (mew-input-pick-pattern "Hyper Estraier filter"))
504
505 (defvar mew-search-est-db "casket")
506 (defvar mew-prog-est-update "mewest")
507
508 (defun mew-search-est (pattern path filter)
509 (setq pattern (mew-cs-encode-string pattern mew-cs-est))
510 (if (string= filter "")
511 (setq filter nil)
512 (setq filter (mew-cs-encode-string filter mew-cs-est))
513 (setq filter (let ((mew-inherit-pick-omit-and t))
514 (mew-pick-parse (mew-pick-lex filter))))
515 (setq filter (mapcar 'mew-pick-filter-est-kyvl filter)))
516 (let* ((ent (mew-search-get-ent mew-search-method))
517 (prog (mew-search-get-prog ent))
518 (casket (expand-file-name mew-search-est-db mew-conf-path))
519 ;; mew-home is not good for Zaurus
520 (pregex (concat "^"
521 (regexp-quote
522 (file-name-directory
523 (directory-file-name
524 (file-truename
525 (expand-file-name mew-mail-path)))))))
526 attr args)
527 (setq path (file-truename (file-name-as-directory path)))
528 (when (string-match pregex path)
529 (setq path (substring path (match-end 0)))
530 (mew-plet
531 (mew-alet
532 (setq attr (format "@uri STRINC %s" (mew-q-encode-string path ?%)))
533 (cond
534 ((string-match "^ *ANDNOT " pattern)
535 (setq pattern (concat "[UVSET] " pattern)))
536 ((string-match "^ *$" pattern)
537 (setq pattern "[UVSET]")))
538 (setq args (list "-vu" "-max" "-1" "-ord" "@cdate NUMA" casket pattern))
539 (unless (eq mew-cs-est 'utf-8)
540 (setq args (cons (mew-cs-to-charset mew-cs-est) args))
541 (setq args (cons "-ic" args)))
542 (setq filter (nreverse filter))
543 (while filter
544 (setq args (cons "-attr" (cons (car filter) args)))
545 (setq filter (cdr filter)))
546 (setq args (cons "-attr" (cons attr args)))
547 (apply 'call-process prog nil t nil "search" args))))))
548
549 (defun mew-search-with-est (pattern folder filter)
550 (let* ((path (mew-expand-folder folder))
551 (regex (format "file://.*/%s/.*/\\([0-9]+\\)\\(%s\\)?$"
552 (file-name-nondirectory mew-mail-path)
553 (regexp-quote mew-suffix)))
554 msgs)
555 (with-temp-buffer
556 (mew-set-buffer-multibyte t)
557 (mew-search-est pattern path filter)
558 (goto-char (point-min))
559 (while (re-search-forward regex nil t)
560 (setq msgs (cons (mew-match-string 1) msgs))
561 (forward-line))
562 (setq msgs (nreverse msgs))
563 (setq msgs (sort (mapcar 'string-to-number msgs) '<))
564 (mapcar 'number-to-string msgs))))
565
566 (defun mew-search-virtual-with-est (pattern flds filter)
567 (let* ((regex (format "file://.*/%s/\\(.*\\)/\\([0-9]+\\)\\(%s\\)?$"
568 (file-name-nondirectory mew-mail-path)
569 (regexp-quote mew-suffix)))
570 (file (mew-make-temp-name))
571 (prev "") (rttl 0) (n 1)
572 path crnt start)
573 (unless flds (setq flds (list mew-folder-local)))
574 (setq flds (mew-expand-wildcard-folder flds))
575 (while flds
576 (setq path (mew-expand-folder (car flds)))
577 (setq flds (cdr flds))
578 (mew-search-est pattern path filter)
579 (goto-char (point-min))
580 (setq start (point))
581 (while (re-search-forward regex nil t)
582 (setq rttl (1+ rttl))
583 (setq crnt (match-string 1))
584 (delete-region start (match-beginning 2))
585 (when (not (string= crnt prev))
586 (beginning-of-line)
587 (insert "CD:" mew-folder-local (mew-q-decode-string crnt ?%) "\n"))
588 (setq prev crnt)
589 (forward-line)
590 (setq start (point)))
591 (delete-region start (point-max))
592 (mew-frwlet mew-cs-text-for-read mew-cs-text-for-write
593 (write-region (point-min) (point-max) file (> n 1) 'no-msg))
594 (mew-erase-buffer)
595 (setq n (1+ n)))
596 (list file rttl)))
597
598 (defun mew-est-index-folder (folder)
599 "Make Hyper Estraier index for this folder."
600 (interactive)
601 (if (not (mew-which-exec mew-prog-est-update))
602 (message "\"%s\" does not exist" mew-prog-est-update)
603 (message "Hyper Estraier indexing for %s..." folder)
604 (let* ((path (file-truename (mew-expand-folder folder)))
605 (pro (start-process "*Mew EST*" nil mew-prog-est-update "-s" mew-suffix "-b" (mew-expand-folder mew-mail-path) path)))
606 (set-process-filter pro 'mew-est-index-filter)
607 (set-process-sentinel pro 'mew-est-index-sentinel))))
608
609 (defun mew-est-index-all ()
610 "Make Hyper Estraier index for all folders."
611 (interactive)
612 (if (not (mew-which-exec mew-prog-est-update))
613 (message "'%s' does not exist" mew-prog-est-update)
614 (message "Hyper Estraier indexing...")
615 (let ((pro (start-process "*Mew EST*" nil mew-prog-est-update "-s" mew-suffix "-b" (mew-expand-folder mew-mail-path))))
616 (set-process-filter pro 'mew-est-index-filter)
617 (set-process-sentinel pro 'mew-est-index-sentinel))))
618
619 (defun mew-est-index-filter (_process string)
620 (save-excursion
621 (cond
622 ((string-match "exists" string)
623 (message "Another Hyper Estraier indexer is running"))
624 ((string-match "\.\.\.failed" string)
625 (message "Hyper Estraier indexing ...failed"))
626 ((string-match "old messages\.\.\.done" string)
627 (message "Hyper Estraier purging ...done"))
628 ((string-match "new messages\.\.\.done" string)
629 (message "Hyper Estraier indexing ...done")))))
630
631 (defun mew-est-index-sentinel (_process _event)
632 (save-excursion ))
633
634 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
635 ;;;
636 ;;; View
637 ;;;
638
639 (defvar mew-prog-smew "smew")
640 (defvar mew-prog-cmew "cmew")
641 (defvar mew-id-db-file "id.db")
642 (defvar mew-id-db-ignore-regex "^\\./casket$|^\\./casket/|/\\.")
643
644 (defmacro mew-msgid-check (&rest body)
645 (declare (debug (&rest form)))
646 `(let ((db (mew-expand-file "+" mew-id-db-file)))
647 (cond
648 ((not (file-exists-p db))
649 (message "%s not found" mew-id-db-file))
650 ((not (mew-which-exec mew-prog-smew))
651 (message "%s not found" mew-prog-smew))
652 (t
653 ,@body))))
654
655 (defun mew-summary-selection-by-msgid ()
656 "Creating Virtual mode with messages relating to the current message"
657 (interactive)
658 (mew-msgid-check
659 (mew-summary-msg
660 (let* ((ofolder (mew-summary-folder-name 'ext))
661 (subj (or (mew-summary-get-subject) mew-error-no-subject))
662 (str (mew-subject-simplify2 subj))
663 (vfolder (mew-folder-to-selection
664 (if (string= str "") mew-error-no-subject subj)))
665 (regex (format "\\(.*\\)/\\([0-9]+\\)\\(%s\\)?$" (regexp-quote mew-suffix)))
666 (rttl 0)
667 (file (mew-make-temp-name))
668 (db (mew-expand-file "+" mew-id-db-file))
669 (mydir (substring (mew-path-to-folder (mew-expand-folder (mew-summary-folder-name))) 1))
670 crnt start prev opts dfunc myid)
671 (mew-sumsyn-match mew-regex-sumsyn-long)
672 (setq myid (mew-sumsyn-my-id))
673 ;;
674 (mew-summary-switch-to-folder vfolder)
675 (mew-vinfo-set-mode 'selection)
676 (mew-vinfo-set-physical-folder nil)
677 (mew-vinfo-set-original-folder ofolder)
678 (mew-erase-buffer)
679 ;;
680 (make-local-variable 'mew-summary-form-mark-delete)
681 (setq mew-summary-form-mark-delete nil)
682 (make-local-variable 'mew-summary-form-mark-spam)
683 (setq mew-summary-form-mark-spam nil)
684 (when (mew-summary-exclusive-p)
685 (message "Creating selection by message-id...")
686 (mew-redraw)
687 (with-temp-buffer
688 (mew-set-buffer-multibyte t)
689 (mew-piolet mew-cs-text-for-read mew-cs-text-for-write
690 (call-process mew-prog-smew nil t nil myid db mydir)
691 (goto-char (point-min))
692 (setq start (point))
693 (while (re-search-forward regex nil t)
694 (setq rttl (1+ rttl))
695 (setq crnt (match-string 1))
696 (delete-region start (match-beginning 2))
697 (when (not (string= crnt prev))
698 (beginning-of-line)
699 (insert "CD:" mew-folder-local crnt "\n"))
700 (setq prev crnt)
701 (forward-line)
702 (setq start (point)))
703 (delete-region start (point-max))
704 (mew-frwlet mew-cs-text-for-read mew-cs-text-for-write
705 (write-region (point-min) (point-max) file nil 'no-msg))))
706 (setq dfunc `(lambda () (mew-delete-file ,file)))
707 (setq opts (list "-i" file))
708 (mew-local-retrieve 'vir opts dfunc nil nil rttl))))))
709
710 (defun mew-summary-get-subject ()
711 (let* ((fld (mew-summary-folder-name))
712 (msg (mew-summary-message-number))
713 (cache (mew-cache-hit fld msg))
714 subj)
715 (if (and cache (get-buffer cache))
716 (with-current-buffer cache
717 (setq subj (mew-header-get-value mew-subj:)))
718 (with-temp-buffer
719 (mew-insert-message
720 fld msg mew-cs-text-for-read mew-header-reasonable-size)
721 (mew-refile-decode-subject)
722 (setq subj (mew-header-get-value mew-subj:))))
723 subj))
724
725 (defun mew-summary-make-id-index-folder ()
726 "Make ID database for this folder."
727 (interactive)
728 (mew-summary-only
729 (cond
730 ((not (file-exists-p (expand-file-name mew-id-db-file mew-mail-path)))
731 (message (mew-substitute-for-summary "Type '\\[mew-summary-make-id-index-all]' to create ID database")))
732 ((not (mew-which-exec mew-prog-cmew))
733 (message "%s not found" mew-prog-cmew))
734 (t
735 (let* ((dbfile (expand-file-name mew-id-db-file mew-mail-path))
736 (path (expand-file-name mew-mail-path))
737 (regex mew-id-db-ignore-regex)
738 (target (mew-folder-string
739 (mew-path-to-folder
740 (mew-expand-folder (mew-summary-folder-name 'ext)))))
741 (options (list dbfile path regex target))
742 (buf (generate-new-buffer (concat mew-buffer-prefix "cmew")))
743 pro)
744 (message "Updating ID database...")
745 (setq pro (apply 'start-process "cmew" buf mew-prog-cmew options))
746 (set-process-filter pro 'mew-summary-make-id-index-filter)
747 (set-process-sentinel pro 'mew-summary-make-id-index-sentinel))))))
748
749 (defun mew-summary-make-id-index-all (&optional full-update)
750 "Make ID database for all folders.
751 If called with '\\[universal-argument]', the database is created
752 from scratch."
753 (interactive "P")
754 (if (not (mew-which-exec mew-prog-cmew))
755 (message "%s not found" mew-prog-cmew)
756 (let* ((dbfile (expand-file-name mew-id-db-file mew-mail-path))
757 (path (expand-file-name mew-mail-path))
758 (options (list dbfile path mew-id-db-ignore-regex))
759 (buf (generate-new-buffer (concat mew-buffer-prefix "cmew")))
760 pro)
761 (if full-update (setq options (cons "-f" options)))
762 (message "Updating ID database...")
763 (setq pro (apply 'start-process "cmew" buf mew-prog-cmew options))
764 (set-process-filter pro 'mew-summary-make-id-index-filter)
765 (set-process-sentinel pro 'mew-summary-make-id-index-sentinel))))
766
767 (defun mew-summary-make-id-index-filter (process string)
768 (save-excursion
769 (mew-filter
770 (goto-char (point-max))
771 (insert string))))
772
773 (defun mew-summary-make-id-index-sentinel (process _event)
774 (let ((buf (process-buffer process)))
775 (with-current-buffer buf
776 (goto-char (point-max))
777 (forward-line -1)
778 (let ((str (mew-buffer-substring (line-beginning-position)
779 (line-end-position))))
780 (message "Updating ID database...done (%s)" str)))
781 (mew-remove-buffer buf)))
782
783 (provide 'mew-search)
784
785 ;;; Copyright Notice:
786
787 ;; Copyright (C) 2005-2015 Mew developing team.
788 ;; All rights reserved.
789
790 ;; Redistribution and use in source and binary forms, with or without
791 ;; modification, are permitted provided that the following conditions
792 ;; are met:
793 ;;
794 ;; 1. Redistributions of source code must retain the above copyright
795 ;; notice, this list of conditions and the following disclaimer.
796 ;; 2. Redistributions in binary form must reproduce the above copyright
797 ;; notice, this list of conditions and the following disclaimer in the
798 ;; documentation and/or other materials provided with the distribution.
799 ;; 3. Neither the name of the team nor the names of its contributors
800 ;; may be used to endorse or promote products derived from this software
801 ;; without specific prior written permission.
802 ;;
803 ;; THIS SOFTWARE IS PROVIDED BY THE TEAM AND CONTRIBUTORS ``AS IS'' AND
804 ;; ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
805 ;; IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
806 ;; PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE TEAM OR CONTRIBUTORS BE
807 ;; LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
808 ;; CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
809 ;; SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
810 ;; BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
811 ;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
812 ;; OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
813 ;; IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
814
815 ;;; mew-search.el ends here