Synch to No Gnus 200511220621.
[elisp/gnus.git-] / lisp / nnir.el
1 ;;; nnir.el --- search mail with various search engines
2 ;; Copyright (C) 1998 Kai Großjohann
3
4 ;; Author: Kai Großjohann <grossjohann@ls6.cs.uni-dortmund.de>
5 ;; Keywords: news, mail, searching, ir, glimpse, wais
6
7 ;; This file is not part of GNU Emacs.
8
9 ;; This is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; any later version.
13
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 ;; GNU General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
21 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 ;; Boston, MA 02110-1301, USA.
23
24 ;;; Commentary:
25
26 ;; The most recent version of this can always be fetched from the
27 ;; following FTP site:
28 ;; ls6-ftp.cs.uni-dortmund.de:/pub/src/emacs
29
30 ;; This code is still in the development stage but I'd like other
31 ;; people to have a look at it.  Please do not hesitate to contact me
32 ;; with your ideas.
33
34 ;; What does it do?  Well, it allows you to index your mail using some
35 ;; search engine (freeWAIS-sf and Glimpse are currently supported),
36 ;; then type `G G' in the Group buffer and issue a query to the search
37 ;; engine.  You will then get a buffer which shows all articles
38 ;; matching the query, sorted by Retrieval Status Value (score).
39
40 ;; When looking at the retrieval result (in the Summary buffer) you
41 ;; can type `G T' (aka M-x gnus-summary-nnir-goto-thread RET) on an
42 ;; article.  You will be teleported into the group this article came
43 ;; from, showing the thread this article is part of.  (See below for
44 ;; restrictions.)
45
46 ;; The Lisp installation is simple: just put this file on your
47 ;; load-path, byte-compile it, and load it from ~/.gnus or something.
48 ;; This will install a new command `G G' in your Group buffer for
49 ;; searching your mail.  Note that you also need to configure a number
50 ;; of variables, as described below.
51
52 ;; Restrictions:
53 ;;
54 ;; * Currently, this expects that you use nnml or another
55 ;;   one-file-per-message backend.
56 ;; * It can only search one mail backend.
57 ;; * There are restrictions to the Glimpse setup.
58 ;; * There are restrictions to the Wais setup.
59 ;; * gnus-summary-nnir-goto-thread: Fetches whole group first, before
60 ;;   limiting to the right articles.  This is much too slow, of
61 ;;   course.  May issue a query for number of articles to fetch; you
62 ;;   must accept the default of all articles at this point or things
63 ;;   may break.
64
65 ;; The Lisp setup involves setting a few variables and setting up the
66 ;; search engine.  The first variable to set is `nnir-mail-backend'.
67 ;; For me, `gnus-secondary-select-methods' contains just one select
68 ;; method, and this is also what I put in `nnir-mail-backend'.  Type
69 ;; `C-h v nnir-mail-backend RET' for more information -- the variable
70 ;; documentation includes more details and a few examples.  The second
71 ;; variable to set is `nnir-search-engine'.  Choose one of the engines
72 ;; listed in `nnir-engines'.  (Actually `nnir-engines' is an alist,
73 ;; type `C-h v nnir-engines RET' for more information; this includes
74 ;; examples for setting `nnir-search-engine', too.)
75
76 ;; You must also set up a search engine.  I'll tell you about the two
77 ;; search engines currently supported:
78
79 ;; 1. freeWAIS-sf
80 ;;
81 ;; As always with freeWAIS-sf, you need a so-called `format file'.  I
82 ;; use the following file:
83 ;;
84 ;; ,-----
85 ;; | # Kai's format file for freeWAIS-sf for indexing mails.
86 ;; | # Each mail is in a file, much like the MH format.
87 ;; |
88 ;; | # Document separator should never match -- each file is a document.
89 ;; | record-sep: /^@this regex should never match@$/
90 ;; |
91 ;; | # Searchable fields specification.
92 ;; |
93 ;; | region: /^[sS]ubject:/ /^[sS]ubject: */
94 ;; |         subject "Subject header" stemming TEXT BOTH
95 ;; | end: /^[^ \t]/
96 ;; |
97 ;; | region: /^([tT][oO]|[cC][cC]):/ /^([tT][oO]|[cC][cC]): */
98 ;; |         to "To and Cc headers" SOUNDEX BOTH
99 ;; | end: /^[^ \t]/
100 ;; |
101 ;; | region: /^[fF][rR][oO][mM]:/ /^[fF][rR][oO][mM]: */
102 ;; |         from "From header" SOUNDEX BOTH
103 ;; | end: /^[^ \t]/
104 ;; |
105 ;; | region: /^$/
106 ;; |         stemming TEXT GLOBAL
107 ;; | end: /^@this regex should never match@$/
108 ;; `-----
109 ;;
110 ;; 1998-07-22: waisindex would dump core on me for large articles with
111 ;; the above settings.  I used /^$/ as the end regex for the global
112 ;; field.  That seemed to work okay.
113
114 ;; There is a Perl module called `WAIS.pm' which is available from
115 ;; CPAN as well as ls6-ftp.cs.uni-dortmund.de:/pub/wais/Perl.  This
116 ;; module comes with a nifty tool called `makedb', which I use for
117 ;; indexing.  Here's my `makedb.conf':
118 ;;
119 ;; ,-----
120 ;; | # Config file for makedb
121 ;; |
122 ;; | # Global options
123 ;; | waisindex = /usr/local/bin/waisindex
124 ;; | wais_opt  = -stem -t fields
125 ;; | # `-stem' option necessary when `stemming' is specified for the
126 ;; | # global field in the *.fmt file
127 ;; |
128 ;; | # Own variables
129 ;; | homedir = /home/kai
130 ;; |
131 ;; | # The mail database.
132 ;; | database        = mail
133 ;; | files           = `find $homedir/Mail -name \*[0-9] -print`
134 ;; | dbdir           = $homedir/.wais
135 ;; | limit           = 100
136 ;; `-----
137 ;;
138 ;; The Lisp setup involves the `nnir-wais-*' variables.  The most
139 ;; difficult to understand variable is probably
140 ;; `nnir-wais-remove-prefix'.  Here's what it does: the output of
141 ;; `waissearch' basically contains the file name and the (full)
142 ;; directory name.  As Gnus works with group names rather than
143 ;; directory names, the directory name is transformed into a group
144 ;; name as follows: first, a prefix is removed from the (full)
145 ;; directory name, then all `/' are replaced with `.'.  The variable
146 ;; `nnir-wais-remove-prefix' should contain a regex matching exactly
147 ;; this prefix.  It defaults to `$HOME/Mail/' (note the trailing
148 ;; slash).
149
150 ;; 2. Glimpse
151 ;;
152 ;; The code expects you to have one Glimpse index which contains all
153 ;; your mail files.  The Lisp setup involves setting the
154 ;; `nnir-glimpse-*' variables.  The most difficult to understand
155 ;; variable is probably `nnir-glimpse-remove-prefix', it corresponds
156 ;; to `nnir-wais-remove-prefix', see above.  The `nnir-glimpse-home'
157 ;; variable should be set to the value of the `-H' option which allows
158 ;; one to search this Glimpse index.  I have indexed my whole home
159 ;; directory with Glimpse, so I assume a default of `$HOME'.
160
161 ;; 3. Namazu
162 ;;
163 ;; The Namazu backend requires you to have one directory containing all
164 ;; index files, this is controlled by the `nnir-namazu-index-directory'
165 ;; variable.  To function the `nnir-namazu-remove-prefix' variable must
166 ;; also be correct, see the documentation for `nnir-wais-remove-prefix'
167 ;; above.
168 ;;
169 ;; It is particularly important not to pass any any switches to namazu
170 ;; that will change the output format.  Good switches to use include
171 ;; `--sort', `--ascending', `--early' and `--late'.  Refer to the Namazu
172 ;; documentation for further information on valid switches.
173 ;;
174 ;; To index my mail with the `mknmz' program I use the following
175 ;; configuration file:
176 ;;
177 ;; ,----
178 ;; | package conf;  # Don't remove this line!
179 ;; |
180 ;; | # Paths which will not be indexed. Don't use `^' or `$' anchors.
181 ;; | $EXCLUDE_PATH = "spam|sent";
182 ;; |
183 ;; | # Header fields which should be searchable. case-insensitive
184 ;; | $REMAIN_HEADER = "from|date|message-id|subject";
185 ;; |
186 ;; | # Searchable fields. case-insensitive
187 ;; | $SEARCH_FIELD = "from|date|message-id|subject";
188 ;; |
189 ;; | # The max length of a word.
190 ;; | $WORD_LENG_MAX = 128;
191 ;; |
192 ;; | # The max length of a field.
193 ;; | $MAX_FIELD_LENGTH = 256;
194 ;; `----
195 ;;
196 ;; My mail is stored in the directories ~/Mail/mail/, ~/Mail/lists/ and
197 ;; ~/Mail/archive/, so to index them I go to the directory set in
198 ;; `nnir-namazu-index-directory' and issue the following command.
199 ;;
200 ;;      mknmz --mailnews ~/Mail/archive/ ~/Mail/mail/ ~/Mail/lists/
201 ;;
202 ;; For maximum searching efficiency I have a cron job set to run this
203 ;; command every four hours.
204
205 ;; Developer information:
206
207 ;; I have tried to make the code expandable.  Basically, it is divided
208 ;; into two layers.  The upper layer is somewhat like the `nnvirtual'
209 ;; or `nnkiboze' backends: given a specification of what articles to
210 ;; show from another backend, it creates a group containing exactly
211 ;; those articles.  The lower layer issues a query to a search engine
212 ;; and produces such a specification of what articles to show from the
213 ;; other backend.
214
215 ;; The interface between the two layers consists of the single
216 ;; function `nnir-run-query', which just selects the appropriate
217 ;; function for the search engine one is using.  The input to
218 ;; `nnir-run-query' is a string, representing the query as input by
219 ;; the user.  The output of `nnir-run-query' is supposed to be a
220 ;; vector, each element of which should in turn be a three-element
221 ;; vector.  The first element should be group name of the article, the
222 ;; second element should be the article number, and the third element
223 ;; should be the Retrieval Status Value (RSV) as returned from the
224 ;; search engine.  An RSV is the score assigned to the document by the
225 ;; search engine.  For Boolean search engines like Glimpse, the RSV is
226 ;; always 1000 (or 1 or 100, or whatever you like).
227
228 ;; The sorting order of the articles in the summary buffer created by
229 ;; nnir is based on the order of the articles in the above mentioned
230 ;; vector, so that's where you can do the sorting you'd like.  Maybe
231 ;; it would be nice to have a way of displaying the search result
232 ;; sorted differently?
233
234 ;; So what do you need to do when you want to add another search
235 ;; engine?  You write a function that executes the query.  Temporary
236 ;; data from the search engine can be put in `nnir-tmp-buffer'.  This
237 ;; function should return the list of articles as a vector, as
238 ;; described above.  Then, you need to register this backend in
239 ;; `nnir-engines'.  Then, users can choose the backend by setting
240 ;; `nnir-search-engine'.
241
242 ;; Todo, or future ideas:
243
244 ;; * Make it so that Glimpse can also be called without `-F'.
245 ;;
246 ;; * It should be possible to restrict search to certain groups.
247 ;;
248 ;; * There is currently no error checking.
249 ;;
250 ;; * The summary buffer display is currently really ugly, with all the
251 ;;   added information in the subjects.  How could I make this
252 ;;   prettier?
253 ;;
254 ;; * A function which can be called from an nnir summary buffer which
255 ;;   teleports you into the group the current article came from and
256 ;;   shows you the whole thread this article is part of.
257 ;;   Implementation suggestions?
258 ;;   (1998-07-24: There is now a preliminary implementation, but
259 ;;   it is much too slow and quite fragile.)
260 ;;
261 ;; * Support other mail backends.  In particular, probably quite a few
262 ;;   people use nnfolder.  How would one go about searching nnfolders
263 ;;   and producing the right data needed?  The group name and the RSV
264 ;;   are simple, but what about the article number?
265 ;;
266 ;; * Support compressed mail files.  Probably, just stripping off the
267 ;;   `.gz' or `.Z' file name extension is sufficient.
268 ;;
269 ;; * Support a find/grep combination.
270 ;;
271 ;; * At least for imap, the query is performed twice.
272 ;;
273 ;; * Support multiple mail backends.  The information that is needed
274 ;;   by nnir could be put in the server parameters.  (Use sensible
275 ;;   default values, though: include the name of the backend in the
276 ;;   default value such that people do not have to mess with the
277 ;;   server parameters if they don't want to.)  It is not clear how to
278 ;;   do the user interface, though.  Hm.  Maybe offer the user a
279 ;;   completable list of backends to search?  Or use the
280 ;;   process-marked groups to find out which backends to search?  Or
281 ;;   always search all backends?
282 ;;
283
284 ;; Have you got other ideas?
285
286 ;;; Setup Code:
287
288 (defconst nnir-version "1.72"
289   "Version of NNIR.")
290
291 (require 'cl)
292 (require 'nnoo)
293 (require 'gnus-group)
294 (require 'gnus-sum)
295 (eval-and-compile
296   (require 'gnus-util))
297
298 (nnoo-declare nnir)
299 (nnoo-define-basics nnir)
300
301 (gnus-declare-backend "nnir" 'mail)
302
303 ;;; Developer Extension Variable:
304
305 (defvar nnir-engines
306   '((glimpse nnir-run-glimpse
307              ((group . "Group spec: ")))
308     (wais    nnir-run-waissearch
309              ())
310     (excite  nnir-run-excite-search
311              ())
312     (imap    nnir-run-imap
313              ())
314     (swish++ nnir-run-swish++
315              ((group . "Group spec: ")))
316     (swish-e nnir-run-swish-e
317              ((group . "Group spec: ")))
318     (namazu nnir-run-namazu
319             ()))
320 "Alist of supported search engines.
321 Each element in the alist is a three-element list (ENGINE FUNCTION ARGS).
322 ENGINE is a symbol designating the searching engine.  FUNCTION is also
323 a symbol, giving the function that does the search.  The third element
324 ARGS is a list of cons pairs (PARAM . PROMPT).  When issuing a query,
325 the FUNCTION will issue a query for each of the PARAMs, using PROMPT.
326
327 The value of `nnir-search-engine' must be one of the ENGINE symbols.
328 For example, use the following line for searching using freeWAIS-sf:
329     (setq nnir-search-engine 'wais)
330 Use the following line if you read your mail via IMAP and your IMAP
331 server supports searching:
332     (setq nnir-search-engine 'imap)
333 Note that you have to set additional variables for most backends.  For
334 example, the `wais' backend needs the variables `nnir-wais-program',
335 `nnir-wais-database' and `nnir-wais-remove-prefix'.
336
337 Add an entry here when adding a new search engine.")
338
339 ;;; User Customizable Variables:
340
341 (defgroup nnir nil
342   "Search nnmh and nnml groups in Gnus with Glimpse, freeWAIS-sf, or EWS."
343   :group 'gnus)
344
345 ;; Mail backend.
346
347 ;; TODO:
348 ;; If `nil', use server parameters to find out which server to search. CCC
349 ;;
350 (defcustom nnir-mail-backend '(nnml "")
351   "*Specifies which backend should be searched.
352 More precisely, this is used to determine from which backend to fetch the
353 messages found.
354
355 This must be equal to an existing server, so maybe it is best to use
356 something like the following:
357     (setq nnir-mail-backend (nth 0 gnus-secondary-select-methods))
358 The above line works fine if the mail backend you want to search is
359 the first element of gnus-secondary-select-methods (`nth' starts counting
360 at zero)."
361   :type '(sexp)
362   :group 'nnir)
363
364 ;; Search engine to use.
365
366 (defcustom nnir-search-engine 'wais
367   "*The search engine to use.  Must be a symbol.
368 See `nnir-engines' for a list of supported engines, and for example
369 settings of `nnir-search-engine'."
370   :type '(sexp)
371   :group 'nnir)
372
373 ;; Glimpse engine.
374
375 (defcustom nnir-glimpse-program "glimpse"
376   "*Name of Glimpse executable."
377   :type '(string)
378   :group 'nnir)
379
380 (defcustom nnir-glimpse-home (getenv "HOME")
381   "*Value of `-H' glimpse option.
382 `~' and environment variables must be expanded, see the functions
383 `expand-file-name' and `substitute-in-file-name'."
384   :type '(directory)
385   :group 'nnir)
386
387 (defcustom nnir-glimpse-remove-prefix (concat (getenv "HOME") "/Mail/")
388   "*The prefix to remove from each file name returned by Glimpse
389 in order to get a group name (albeit with / instead of .).  This is a
390 regular expression.
391
392 For example, suppose that Glimpse returns file names such as
393 \"/home/john/Mail/mail/misc/42\".  For this example, use the following
394 setting:  (setq nnir-glimpse-remove-prefix \"/home/john/Mail/\")
395 Note the trailing slash.  Removing this prefix gives \"mail/misc/42\".
396 `nnir' knows to remove the \"/42\" and to replace \"/\" with \".\" to
397 arrive at the correct group name, \"mail.misc\"."
398   :type '(regexp)
399   :group 'nnir)
400
401 (defcustom nnir-glimpse-additional-switches '("-i")
402   "*A list of strings, to be given as additional arguments to glimpse.
403 The switches `-H', `-W', `-l' and `-y' are always used -- calling
404 glimpse without them does not make sense in our situation.
405 Suggested elements to put here are `-i' and `-w'.
406
407 Note that this should be a list.  Ie, do NOT use the following:
408     (setq nnir-glimpse-additional-switches \"-i -w\") ; wrong!
409 Instead, use this:
410     (setq nnir-glimpse-additional-switches '(\"-i\" \"-w\"))"
411   :type '(repeat (string))
412   :group 'nnir)
413
414 ;; freeWAIS-sf.
415
416 (defcustom nnir-wais-program "waissearch"
417   "*Name of waissearch executable."
418   :type '(string)
419   :group 'nnir)
420
421 (defcustom nnir-wais-database (expand-file-name "~/.wais/mail")
422   "*Name of Wais database containing the mail.
423
424 Note that this should be a file name without extension.  For example,
425 if you have a file /home/john/.wais/mail.fmt, use this:
426     (setq nnir-wais-database \"/home/john/.wais/mail\")
427 The string given here is passed to `waissearch -d' as-is."
428   :type '(file)
429   :group 'nnir)
430
431 (defcustom nnir-wais-remove-prefix (concat (getenv "HOME") "/Mail/")
432   "*The prefix to remove from each directory name returned by waissearch
433 in order to get a group name (albeit with / instead of .).  This is a
434 regular expression.
435
436 This variable is similar to `nnir-glimpse-remove-prefix', only for Wais,
437 not Glimpse."
438   :type '(regexp)
439   :group 'nnir)
440
441 ;; EWS (Excite for Web Servers) engine.
442
443 (defcustom nnir-excite-aquery-program "aquery.pl"
444   "*Name of the EWS query program.  Should be `aquery.pl' or a path to same."
445   :type '(string)
446   :group 'nnir)
447
448 (defcustom nnir-excite-collection "Mail"
449   "*Name of the EWS collection to search."
450   :type '(string)
451   :group 'nnir)
452
453 (defcustom nnir-excite-remove-prefix (concat (getenv "HOME") "/Mail/")
454   "*The prefix to remove from each file name returned by EWS
455 in order to get a group name (albeit with / instead of .).  This is a
456 regular expression.
457
458 This variable is very similar to `nnir-glimpse-remove-prefix', except
459 that it is for EWS, not Glimpse."
460   :type '(regexp)
461   :group 'nnir)
462
463 (defcustom nnir-imap-default-charset nil
464   "*Name of the charset of the strings that appear in the search criteria."
465   :type '(choice (const nil) symbol)
466   :group 'nnir)
467
468 ;; Swish++.  Next three variables Copyright (C) 2000, 2001 Christoph
469 ;; Conrad <christoph.conrad@gmx.de>.
470 ;; Swish++ home page: http://homepage.mac.com/pauljlucas/software/swish/
471
472 (defcustom nnir-swish++-configuration-file
473   (expand-file-name "~/Mail/swish++.conf")
474   "*Configuration file for swish++."
475   :type '(file)
476   :group 'nnir)
477
478 (defcustom nnir-swish++-program "search"
479   "*Name of swish++ search executable."
480   :type '(string)
481   :group 'nnir)
482
483 (defcustom nnir-swish++-additional-switches '()
484     "*A list of strings, to be given as additional arguments to swish++.
485
486 Note that this should be a list.  Ie, do NOT use the following:
487     (setq nnir-swish++-additional-switches \"-i -w\") ; wrong
488 Instead, use this:
489     (setq nnir-swish++-additional-switches '(\"-i\" \"-w\"))"
490   :type '(repeat (string))
491   :group 'nnir)
492
493 (defcustom nnir-swish++-remove-prefix (concat (getenv "HOME") "/Mail/")
494   "*The prefix to remove from each file name returned by swish++
495 in order to get a group name (albeit with / instead of .).  This is a
496 regular expression.
497
498 This variable is very similar to `nnir-glimpse-remove-prefix', except
499 that it is for swish++, not Glimpse."
500   :type '(regexp)
501   :group 'nnir)
502
503 ;; Swish-E.  Next three variables Copyright (C) 2000 Christoph Conrad
504 ;; <christoph.conrad@gmx.de>.
505 ;; URL: http://sunsite.berkeley.edu/SWISH-E/
506 ;; New version: http://www.boe.es/swish-e
507
508 (defcustom nnir-swish-e-index-file
509   (expand-file-name "~/Mail/index.swish-e")
510   "*Index file for swish-e."
511   :type '(file)
512   :group 'nnir)
513
514 (defcustom nnir-swish-e-program "swish-e"
515   "*Name of swish-e search executable."
516   :type '(string)
517   :group 'nnir)
518
519 (defcustom nnir-swish-e-additional-switches '()
520   "*A list of strings, to be given as additional arguments to swish-e.
521
522 Note that this should be a list.  Ie, do NOT use the following:
523     (setq nnir-swish-e-additional-switches \"-i -w\") ; wrong
524 Instead, use this:
525     (setq nnir-swish-e-additional-switches '(\"-i\" \"-w\"))"
526   :type '(repeat (string))
527   :group 'nnir)
528
529 (defcustom nnir-swish-e-remove-prefix (concat (getenv "HOME") "/Mail/")
530   "*The prefix to remove from each file name returned by swish-e
531 in order to get a group name (albeit with / instead of .).  This is a
532 regular expression.
533
534 This variable is very similar to `nnir-glimpse-remove-prefix', except
535 that it is for swish-e, not Glimpse."
536   :type '(regexp)
537   :group 'nnir)
538
539 ;; Namazu engine, see <URL:http://ww.namazu.org/>
540
541 (defcustom nnir-namazu-program "namazu"
542   "*Name of Namazu search executable."
543   :type '(string)
544   :group 'nnir)
545
546 (defcustom nnir-namazu-index-directory (expand-file-name "~/Mail/namazu/")
547   "*Index directory for Namazu."
548   :type '(directory)
549   :group 'nnir)
550
551 (defcustom nnir-namazu-additional-switches '()
552   "*A list of strings, to be given as additional arguments to namazu.
553 The switches `-q', `-a', and `-s' are always used, very few other switches
554 make any sense in this context.
555
556 Note that this should be a list.  Ie, do NOT use the following:
557     (setq nnir-namazu-additional-switches \"-i -w\") ; wrong
558 Instead, use this:
559     (setq nnir-namazu-additional-switches '(\"-i\" \"-w\"))"
560   :type '(repeat (string))
561   :group 'nnir)
562
563 (defcustom nnir-namazu-remove-prefix (concat (getenv "HOME") "/Mail/")
564   "*The prefix to remove from each file name returned by Namazu
565 in order to get a group name (albeit with / instead of .).
566
567 This variable is very similar to `nnir-glimpse-remove-prefix', except
568 that it is for Namazu, not Glimpse."
569   :type '(directory)
570   :group 'nnir)
571
572 ;;; Internal Variables:
573
574 (defvar nnir-current-query nil
575   "Internal: stores current query (= group name).")
576
577 (defvar nnir-current-server nil
578   "Internal: stores current server (does it ever change?).")
579
580 (defvar nnir-current-group-marked nil
581   "Internal: stores current list of process-marked groups.")
582
583 (defvar nnir-artlist nil
584   "Internal: stores search result.")
585
586 (defvar nnir-tmp-buffer " *nnir*"
587   "Internal: temporary buffer.")
588
589 ;;; Code:
590
591 ;; Gnus glue.
592
593 (defun gnus-group-make-nnir-group (extra-parms query)
594   "Create an nnir group.  Asks for query."
595   (interactive "P\nsQuery: ")
596   (let ((parms nil))
597     (if extra-parms
598         (setq parms (nnir-read-parms query))
599       (setq parms (list (cons 'query query))))
600     (gnus-group-read-ephemeral-group
601      (concat "nnir:" (prin1-to-string parms)) '(nnir "") t
602      (cons (current-buffer)
603            gnus-current-window-configuration)
604      nil)))
605
606 ;; Emacs 19 compatibility?
607 (or (fboundp 'kbd) (defalias 'kbd 'read-kbd-macro))
608
609 (defun nnir-group-mode-hook ()
610   (define-key gnus-group-mode-map
611     (if (fboundp 'read-kbd-macro)
612         (kbd "G G")
613       "GG")                             ; XEmacs 19 compat
614     'gnus-group-make-nnir-group))
615 (add-hook 'gnus-group-mode-hook
616           (lambda ()
617             (unless (string-match "T-gnus" gnus-version)
618               (nnir-group-mode-hook))))
619
620
621
622 ;; Summary mode commands.
623
624 (defun gnus-summary-nnir-goto-thread ()
625   "Only applies to nnir groups.  Go to group this article came from
626 and show thread that contains this article."
627   (interactive)
628   (unless (eq 'nnir (car (gnus-find-method-for-group gnus-newsgroup-name)))
629     (error "Can't execute this command unless in nnir group."))
630   (let* ((cur (gnus-summary-article-number))
631          (backend-group (nnir-artlist-artitem-group nnir-artlist cur))
632          (backend-number (nnir-artlist-artitem-number nnir-artlist cur)))
633     (gnus-group-read-ephemeral-group
634      backend-group
635      nnir-mail-backend
636      t                                  ; activate
637      (cons (current-buffer)
638            'summary)                    ; window config
639      nil
640      (list backend-number))
641     (gnus-summary-limit (list backend-number))
642     (gnus-summary-refer-thread)))
643
644 (if (fboundp 'eval-after-load)
645     (eval-after-load "gnus-sum"
646       '(define-key gnus-summary-goto-map
647          "T" 'gnus-summary-nnir-goto-thread))
648   (add-hook 'gnus-summary-mode-hook
649             (function (lambda ()
650                         (define-key gnus-summary-goto-map
651                           "T" 'gnus-summary-nnir-goto-thread)))))
652
653
654
655 ;; Gnus backend interface functions.
656
657 (deffoo nnir-open-server (server &optional definitions)
658   ;; Just set the server variables appropriately.
659   (nnoo-change-server 'nnir server definitions))
660
661 (deffoo nnir-request-group (group &optional server fast)
662   "GROUP is the query string."
663   (nnir-possibly-change-server server)
664   ;; Check for cache and return that if appropriate.
665   (if (and (equal group nnir-current-query)
666            (equal gnus-group-marked nnir-current-group-marked)
667            (or (null server)
668                (equal server nnir-current-server)))
669       nnir-artlist
670     ;; Cache miss.
671     (setq nnir-artlist (nnir-run-query group))
672     (save-excursion
673       (set-buffer nntp-server-buffer)
674       (if (zerop (length nnir-artlist))
675           (progn
676             (setq nnir-current-query nil
677                   nnir-current-server nil
678                   nnir-current-group-marked nil
679                   nnir-artlist nil)
680             (nnheader-report 'nnir "Search produced empty results."))
681         ;; Remember data for cache.
682         (setq nnir-current-query group)
683         (when server (setq nnir-current-server server))
684         (setq nnir-current-group-marked gnus-group-marked)
685         (nnheader-insert "211 %d %d %d %s\n"
686                          (nnir-artlist-length nnir-artlist) ; total #
687                          1              ; first #
688                          (nnir-artlist-length nnir-artlist) ; last #
689                          group)))))     ; group name
690
691 (deffoo nnir-retrieve-headers (articles &optional group server fetch-old)
692   (save-excursion
693     (let ((artlist (copy-sequence articles))
694           (idx 1)
695           (art nil)
696           (artitem nil)
697           (artgroup nil) (artno nil)
698           (artrsv nil)
699           (artfullgroup nil)
700           (novitem nil)
701           (novdata nil)
702           (foo nil))
703       (while (not (null artlist))
704         (setq art (car artlist))
705         (or (numberp art)
706             (nnheader-report
707              'nnir
708              "nnir-retrieve-headers doesn't grok message ids: %s"
709              art))
710         (setq artitem (nnir-artlist-article nnir-artlist art))
711         (setq artrsv (nnir-artitem-rsv artitem))
712         (setq artgroup (nnir-artitem-group artitem))
713         (setq artno (nnir-artitem-number artitem))
714         (setq artfullgroup (nnir-group-full-name artgroup))
715         ;; retrieve NOV or HEAD data for this article, transform into
716         ;; NOV data and prepend to `novdata'
717         (set-buffer nntp-server-buffer)
718         (case (setq foo (gnus-retrieve-headers (list artno) artfullgroup nil))
719           (nov
720            (goto-char (point-min))
721            (setq novitem (nnheader-parse-nov))
722            (unless novitem
723              (pop-to-buffer nntp-server-buffer)
724              (error
725               "nnheader-parse-nov returned nil for article %s in group %s"
726               artno artfullgroup)))
727           (headers
728            (goto-char (point-min))
729            (setq novitem (nnheader-parse-head))
730            (unless novitem
731              (pop-to-buffer nntp-server-buffer)
732              (error
733               "nnheader-parse-head returned nil for article %s in group %s"
734               artno artfullgroup)))
735           (t (nnheader-report 'nnir "Don't support header type %s." foo)))
736         ;; replace article number in original group with article number
737         ;; in nnir group
738         (mail-header-set-number novitem idx)
739         (mail-header-set-from novitem
740                               (mail-header-from novitem))
741         (mail-header-set-subject
742          novitem
743          (format "[%d: %s/%d] %s"
744                  artrsv artgroup artno
745                  (mail-header-subject novitem)))
746         ;;-(mail-header-set-extra novitem nil)
747         (push novitem novdata)
748         (setq artlist (cdr artlist))
749         (setq idx (1+ idx)))
750       (setq novdata (nreverse novdata))
751       (set-buffer nntp-server-buffer) (erase-buffer)
752       (mapcar 'nnheader-insert-nov novdata)
753       'nov)))
754
755 (deffoo nnir-request-article (article
756                               &optional group server to-buffer)
757   (save-excursion
758     (let* ((artitem (nnir-artlist-article nnir-artlist
759                                           article))
760            (artgroup (nnir-artitem-group artitem))
761            (artno (nnir-artitem-number artitem))
762            ;; Bug?
763            ;; Why must we bind nntp-server-buffer here?  It won't
764            ;; work if `buf' is used, say.  (Of course, the set-buffer
765            ;; line below must then be updated, too.)
766            (nntp-server-buffer (or to-buffer nntp-server-buffer)))
767       (set-buffer nntp-server-buffer)
768       (erase-buffer)
769       (message "Requesting article %d from group %s"
770                artno
771                (nnir-group-full-name artgroup))
772       (gnus-request-article artno (nnir-group-full-name artgroup)
773                             nntp-server-buffer)
774       (cons artgroup artno))))
775
776
777 (nnoo-define-skeleton nnir)
778
779 ;;; Search Engine Interfaces:
780
781 ;; Glimpse interface.
782 (defun nnir-run-glimpse (query &optional group)
783   "Run given query against glimpse.  Returns a vector of (group name, file name)
784 pairs (also vectors, actually)."
785   (save-excursion
786     (let ((artlist nil)
787           (groupspec (cdr (assq 'group query)))
788           (qstring (cdr (assq 'query query))))
789       (when (and group groupspec)
790         (error (concat "It does not make sense to use a group spec"
791                        " with process-marked groups.")))
792       (when group
793         (setq groupspec (gnus-group-real-name group)))
794       (set-buffer (get-buffer-create nnir-tmp-buffer))
795       (erase-buffer)
796       (if groupspec
797           (message "Doing glimpse query %s on %s..." query groupspec)
798         (message "Doing glimpse query %s..." query))
799       (let* ((cp-list
800               `( ,nnir-glimpse-program
801                  nil                    ; input from /dev/null
802                  t                      ; output
803                  nil                    ; don't redisplay
804                  "-H" ,nnir-glimpse-home ; search home dir
805                  "-W"                   ; match pattern in file
806                  "-l" "-y"              ; misc options
807                  ,@nnir-glimpse-additional-switches
808                  "-F" ,nnir-glimpse-remove-prefix ; restrict output to mail
809                  ,qstring               ; the query, in glimpse format
810                 ))
811              (exitstatus
812               (progn
813                 (message "%s args: %s" nnir-glimpse-program
814                          (mapconcat 'identity (cddddr cp-list) " "))
815                 (apply 'call-process cp-list))))
816         (unless (or (null exitstatus)
817                     (zerop exitstatus))
818           (nnheader-report 'nnir "Couldn't run glimpse: %s" exitstatus)
819           ;; Glimpse failure reason is in this buffer, show it if
820           ;; the user wants it.
821           (when (> gnus-verbose 6)
822             (display-buffer nnir-tmp-buffer))))
823       (when groupspec
824         (keep-lines groupspec))
825       (if groupspec
826           (message "Doing glimpse query %s on %s...done" query groupspec)
827         (message "Doing glimpse query %s...done" query))
828       (sit-for 0)
829       ;; CCC: The following work of extracting group name and article
830       ;; number from the Glimpse output can probably better be done by
831       ;; just going through the buffer once, and plucking out the
832       ;; right information from each line.
833       ;; remove superfluous stuff from glimpse output
834       (goto-char (point-min))
835       (delete-non-matching-lines "/[0-9]+$")
836       ;;(delete-matching-lines "\\.overview~?$")
837       (goto-char (point-min))
838       (while (re-search-forward (concat "^" nnir-glimpse-remove-prefix) nil t)
839         (replace-match ""))
840       ;; separate group name from article number with \t
841       ;; XEmacs compatible version
842       (goto-char (point-max))
843       (while (re-search-backward "/[0-9]+$" nil t)
844         (delete-char 1 nil)
845         (insert-char ?\t 1))
846 ; Emacs compatible version
847 ;      (goto-char (point-min))
848 ;      (while (re-search-forward "\\(/\\)[0-9]+$" nil t)
849 ;        (replace-match "\t" t t nil 1))
850       ;; replace / with . in group names
851       (subst-char-in-region (point-min) (point-max) ?/ ?. t)
852       ;; massage buffer to contain some Lisp;
853       ;; this depends on the artlist encoding internals
854       ;; maybe this dependency should be removed?
855       (goto-char (point-min))
856       (while (not (eobp))
857         (insert "[\"")
858         (skip-chars-forward "^\t")
859         (insert "\" ")
860         (end-of-line)
861         (insert " 1000 ]")              ; 1000 = score
862         (forward-line 1))
863       (insert "])\n")
864       (goto-char (point-min))
865       (insert "(setq artlist [\n")
866       (eval-buffer)
867       (sort* artlist
868              (function (lambda (x y)
869                          (if (string-lessp (nnir-artitem-group x)
870                                            (nnir-artitem-group y))
871                              t
872                            (< (nnir-artitem-number x)
873                               (nnir-artitem-number y))))))
874       )))
875
876 ;; freeWAIS-sf interface.
877 (defun nnir-run-waissearch (query &optional group)
878   "Run given query agains waissearch.  Returns vector of (group name, file name)
879 pairs (also vectors, actually)."
880   (when group
881     (error "The freeWAIS-sf backend cannot search specific groups."))
882   (save-excursion
883     (let ((qstring (cdr (assq 'query query)))
884           (artlist nil)
885           (score nil) (artno nil) (dirnam nil) (group nil))
886       (set-buffer (get-buffer-create nnir-tmp-buffer))
887       (erase-buffer)
888       (message "Doing WAIS query %s..." query)
889       (call-process nnir-wais-program
890                     nil                 ; input from /dev/null
891                     t                   ; output to current buffer
892                     nil                 ; don't redisplay
893                     "-d" nnir-wais-database ; database to search
894                     qstring)
895       (message "Massaging waissearch output...")
896       ;; remove superfluous lines
897       (keep-lines "Score:")
898       ;; extract data from result lines
899       (goto-char (point-min))
900       (while (re-search-forward
901               "Score: +\\([0-9]+\\).*'\\([0-9]+\\) +\\([^']+\\)/'" nil t)
902         (setq score (match-string 1)
903               artno (match-string 2)
904               dirnam (match-string 3))
905         (unless (string-match nnir-wais-remove-prefix dirnam)
906           (nnheader-report 'nnir "Dir name %s doesn't contain prefix %s"
907                            dirnam nnir-wais-remove-prefix))
908         (setq group (substitute ?. ?/ (replace-match "" t t dirnam)))
909         (push (vector group
910                       (string-to-number artno)
911                       (string-to-number score))
912               artlist))
913       (message "Massaging waissearch output...done")
914       (apply 'vector
915              (sort* artlist
916                     (function (lambda (x y)
917                                 (> (nnir-artitem-rsv x)
918                                    (nnir-artitem-rsv y)))))))))
919
920 ;; EWS (Excite for Web Servers) interface
921 (defun nnir-run-excite-search (query &optional group)
922   "Run a given query against EWS.  Returns vector of (group name, file name)
923 pairs (also vectors, actually)."
924   (when group
925     (error "Searching specific groups not implemented for EWS."))
926   (save-excursion
927     (let ((qstring (cdr (assq 'query query)))
928           artlist group article-num article)
929       (setq nnir-current-query query)
930       (set-buffer (get-buffer-create nnir-tmp-buffer))
931       (erase-buffer)
932       (message "Doing EWS query %s..." qstring)
933       (call-process nnir-excite-aquery-program
934                     nil                 ; input from /dev/null
935                     t                   ; output to current buffer
936                     nil                 ; don't redisplay
937                     nnir-excite-collection
938                     (if (string= (substring qstring 0 1) "(")
939                         qstring
940                       (format "(concept %s)" qstring)))
941       (message "Gathering query output...")
942
943       (goto-char (point-min))
944       (while (re-search-forward
945               "^[0-9]+\\s-[0-9]+\\s-[0-9]+\\s-\\(\\S-*\\)" nil t)
946         (setq article (match-string 1))
947         (unless (string-match
948                  (concat "^" (regexp-quote nnir-excite-remove-prefix)
949                          "\\(.*\\)/\\([0-9]+\\)") article)
950           (nnheader-report 'nnir "Dir name %s doesn't contain prefix %s"
951                            article nnir-excite-remove-prefix))
952         (setq group (substitute ?. ?/ (match-string 1 article)))
953         (setq article-num (match-string 2 article))
954         (setq artlist (vconcat artlist (vector (vector group
955                                                        (string-to-number article-num)
956                                                        1000)))))
957       (message "Gathering query output...done")
958       artlist)))
959
960 ;; IMAP interface.  The following function is Copyright (C) 1998 Simon
961 ;; Josefsson <jas@pdc.kth.se>.
962 ;; todo:
963 ;; nnir invokes this two (2) times???!
964 ;; we should not use nnimap at all but open our own server connection
965 ;; we should not LIST * but use nnimap-list-pattern from defs
966 ;; send queries as literals
967 ;; handle errors
968
969 (eval-when-compile
970   (defvar nnimap-server-buffer))
971
972 (defun nnir-run-imap (query &optional group)
973   (require 'imap)
974   (require 'nnimap)
975   (require 'mm-util)
976   (unless group
977     (error "Must specify groups for IMAP searching."))
978   (save-excursion
979     (let ((qstring (cdr (assq 'query query)))
980           (server (cadr nnir-mail-backend))
981           (defs (caddr nnir-mail-backend))
982           artlist buf)
983       (message "Opening server %s" server)
984       (condition-case ()
985           (when (nnimap-open-server server defs) ;; xxx
986             (setq buf nnimap-server-buffer) ;; xxx
987             (message "Searching %s..." group)
988             (let ((arts 0)
989                   (mbx (gnus-group-real-name group))
990                   (multibyte-p (mm-multibyte-p))
991                   charset coding-system)
992               (when (imap-mailbox-select mbx nil buf)
993                 (with-temp-buffer
994                   (if multibyte-p
995                       (mm-enable-multibyte))
996                   (insert qstring)
997                   (setq charset (car (mm-find-mime-charset-region
998                                       (point-min)(point-max)))))
999                 (unless charset
1000                   (setq charset nnir-imap-default-charset))
1001                 (mapcar
1002                  (lambda (artnum)
1003                    (push (vector mbx artnum 1) artlist)
1004                    (setq arts (1+ arts)))
1005                  (if (and (not (eq charset 'us-ascii))
1006                           (setq coding-system (mm-charset-to-coding-system
1007                                                charset)))
1008                      (imap-search
1009                       (concat "CHARSET " (symbol-name charset) " TEXT \""
1010                               (mm-encode-coding-string qstring coding-system)
1011                               "\"") buf)
1012                    (imap-search (concat "TEXT \"" qstring "\"") buf)))
1013                 (message "Searching %s... %d matches" mbx arts)))
1014             (message "Searching %s...done" group))
1015         (quit nil))
1016       (reverse artlist))))
1017
1018 ;; Swish++ interface.  The following function is Copyright (C) 2000,
1019 ;; 2001 Christoph Conrad <christoph.conrad@gmx.de>.
1020 ;; -cc- Todo
1021 ;; Search by
1022 ;; - group
1023 ;; Sort by
1024 ;; - rank (default)
1025 ;; - article number
1026 ;; - file size
1027 ;; - group
1028 (defun nnir-run-swish++ (query &optional group)
1029   "Run given query against swish++.
1030 Returns a vector of (group name, file name) pairs (also vectors,
1031 actually).
1032
1033 Tested with swish++ 4.7 on GNU/Linux and with with swish++ 5.0b2 on
1034 Windows NT 4.0."
1035
1036   (when group
1037     (error "The swish++ backend cannot search specific groups."))
1038
1039   (save-excursion
1040     (let ( (qstring (cdr (assq 'query query)))
1041            (groupspec (cdr (assq 'group query)))
1042            (artlist nil)
1043            (score nil) (artno nil) (dirnam nil) (group nil) )
1044
1045       (when (equal "" qstring)
1046         (error "swish++: You didn't enter anything."))
1047
1048       (set-buffer (get-buffer-create nnir-tmp-buffer))
1049       (erase-buffer)
1050
1051       (if groupspec
1052           (message "Doing swish++ query %s on %s..." qstring groupspec)
1053         (message "Doing swish++ query %s..." qstring))
1054
1055       (let* ((cp-list `( ,nnir-swish++-program
1056                          nil            ; input from /dev/null
1057                          t              ; output
1058                          nil            ; don't redisplay
1059                          "--config-file" ,nnir-swish++-configuration-file
1060                          ,@nnir-swish++-additional-switches
1061                          ,qstring       ; the query, in swish++ format
1062                          ))
1063              (exitstatus
1064               (progn
1065                 (message "%s args: %s" nnir-swish++-program
1066                          (mapconcat 'identity (cddddr cp-list) " "));; ???
1067                 (apply 'call-process cp-list))))
1068         (unless (or (null exitstatus)
1069                     (zerop exitstatus))
1070           (nnheader-report 'nnir "Couldn't run swish++: %s" exitstatus)
1071           ;; swish++ failure reason is in this buffer, show it if
1072           ;; the user wants it.
1073           (when (> gnus-verbose 6)
1074             (display-buffer nnir-tmp-buffer))))
1075
1076       ;; The results are output in the format of:
1077       ;; V 4.7 Linux
1078       ;; rank relative-path-name file-size file-title
1079       ;; V 5.0b2:
1080       ;; rank relative-path-name file-size topic??
1081       ;; where rank is an integer from 1 to 100.
1082       (goto-char (point-min))
1083       (while (re-search-forward
1084               "\\(^[0-9]+\\) \\([^ ]+\\) [0-9]+ \\(.*\\)$" nil t)
1085         (setq score (match-string 1)
1086               artno (file-name-nondirectory (match-string 2))
1087               dirnam (file-name-directory (match-string 2)))
1088
1089         ;; don't match directories
1090         (when (string-match "^[0-9]+$" artno)
1091           (when (not (null dirnam))
1092
1093             ; maybe limit results to matching groups.
1094             (when (or (not groupspec)
1095                       (string-match groupspec dirnam))
1096
1097               ;; remove nnir-swish++-remove-prefix from beginning of dirname
1098               (when (string-match (concat "^" nnir-swish++-remove-prefix)
1099                                   dirnam)
1100                 (setq dirnam (replace-match "" t t dirnam)))
1101
1102               (setq dirnam (substring dirnam 0 -1))
1103               ;; eliminate all ".", "/", "\" from beginning. Always matches.
1104               (string-match "^[./\\]*\\(.*\\)$" dirnam)
1105               ;; "/" -> "."
1106               (setq group (substitute ?. ?/ (match-string 1 dirnam)))
1107               ;; "\\" -> "."
1108               (setq group (substitute ?. ?\\ group))
1109
1110               (push (vector group
1111                             (string-to-number artno)
1112                             (string-to-number score))
1113                     artlist)))))
1114
1115       (message "Massaging swish++ output...done")
1116
1117       ;; Sort by score
1118       (apply 'vector
1119              (sort* artlist
1120                     (function (lambda (x y)
1121                                 (> (nnir-artitem-rsv x)
1122                                    (nnir-artitem-rsv y)))))))))
1123
1124 ;; Swish-E interface.  The following function is Copyright (C) 2000,
1125 ;; 2001 by Christoph Conrad <christoph.conrad@gmx.de>.
1126 (defun nnir-run-swish-e (query &optional group)
1127   "Run given query against swish-e.
1128 Returns a vector of (group name, file name) pairs (also vectors,
1129 actually).
1130
1131 Tested with swish-e-2.0.1 on Windows NT 4.0."
1132
1133   ;; swish-e crashes with empty parameter to "-w" on commandline...
1134   (when group
1135     (error "The swish-e backend cannot search specific groups."))
1136
1137   (save-excursion
1138     (let ( (qstring (cdr (assq 'query query)))
1139            (artlist nil)
1140            (score nil) (artno nil) (dirnam nil) (group nil) )
1141
1142       (when (equal "" qstring)
1143         (error "swish-e: You didn't enter anything."))
1144
1145       (set-buffer (get-buffer-create nnir-tmp-buffer))
1146       (erase-buffer)
1147
1148       (message "Doing swish-e query %s..." query)
1149       (let* ((cp-list `( ,nnir-swish-e-program
1150                          nil            ; input from /dev/null
1151                          t              ; output
1152                          nil            ; don't redisplay
1153                          "-f" ,nnir-swish-e-index-file
1154                          ,@nnir-swish-e-additional-switches
1155                          "-w"
1156                          ,qstring       ; the query, in swish-e format
1157                          ))
1158              (exitstatus
1159               (progn
1160                 (message "%s args: %s" nnir-swish-e-program
1161                          (mapconcat 'identity (cddddr cp-list) " "))
1162                 (apply 'call-process cp-list))))
1163         (unless (or (null exitstatus)
1164                     (zerop exitstatus))
1165           (nnheader-report 'nnir "Couldn't run swish-e: %s" exitstatus)
1166           ;; swish-e failure reason is in this buffer, show it if
1167           ;; the user wants it.
1168           (when (> gnus-verbose 6)
1169             (display-buffer nnir-tmp-buffer))))
1170
1171       ;; The results are output in the format of:
1172       ;; rank path-name file-title file-size
1173       (goto-char (point-min))
1174       (while (re-search-forward
1175               "\\(^[0-9]+\\) \\([^ ]+\\) \"\\([^\"]+\\)\" [0-9]+$" nil t)
1176         (setq score (match-string 1)
1177               artno (match-string 3)
1178               dirnam (file-name-directory (match-string 2)))
1179
1180         ;; don't match directories
1181         (when (string-match "^[0-9]+$" artno)
1182           (when (not (null dirnam))
1183
1184             ;; remove nnir-swish-e-remove-prefix from beginning of dirname
1185             (when (string-match (concat "^" nnir-swish-e-remove-prefix)
1186                                 dirnam)
1187               (setq dirnam (replace-match "" t t dirnam)))
1188
1189             (setq dirnam (substring dirnam 0 -1))
1190             ;; eliminate all ".", "/", "\" from beginning. Always matches.
1191             (string-match "^[./\\]*\\(.*\\)$" dirnam)
1192             ;; "/" -> "."
1193             (setq group (substitute ?. ?/ (match-string 1 dirnam)))
1194             ;; Windows "\\" -> "."
1195             (setq group (substitute ?. ?\\ group))
1196
1197             (push (vector group
1198                           (string-to-number artno)
1199                           (string-to-number score))
1200                   artlist))))
1201
1202       (message "Massaging swish-e output...done")
1203
1204       ;; Sort by score
1205       (apply 'vector
1206              (sort* artlist
1207                     (function (lambda (x y)
1208                                 (> (nnir-artitem-rsv x)
1209                                    (nnir-artitem-rsv y)))))))))
1210
1211 ;; Namazu interface
1212 (defun nnir-run-namazu (query &optional group)
1213   "Run given query against Namazu.  Returns a vector of (group name, file name)
1214 pairs (also vectors, actually).
1215
1216 Tested with Namazu 2.0.6 on a GNU/Linux system."
1217   (when group
1218     (error "The Namazu backend cannot search specific groups"))
1219   (save-excursion
1220     (let (
1221           (artlist nil)
1222           (qstring (cdr (assq 'query query)))
1223           (score nil)
1224           (group nil)
1225           (article nil)
1226           )
1227       (set-buffer (get-buffer-create nnir-tmp-buffer))
1228       (erase-buffer)
1229       (let* ((cp-list
1230               `( ,nnir-namazu-program
1231                  nil              ; input from /dev/null
1232                  t                ; output
1233                  nil              ; don't redisplay
1234                  "-q"             ; don't be verbose
1235                  "-a"             ; show all matches
1236                  "-s"             ; use short format
1237                  ,@nnir-namazu-additional-switches
1238                  ,qstring         ; the query, in namazu format
1239                  ,nnir-namazu-index-directory ; index directory
1240                  ))
1241              (exitstatus
1242               (let ((process-environment (copy-sequence process-environment)))
1243                 ;; Disable locale.
1244                 (setenv "LC_ALL" "C")
1245                 (message "%s args: %s" nnir-namazu-program
1246                          (mapconcat 'identity (cddddr cp-list) " "))
1247                 (apply 'call-process cp-list))))
1248         (unless (or (null exitstatus)
1249                     (zerop exitstatus))
1250           (nnheader-report 'nnir "Couldn't run namazu: %s" exitstatus)
1251           ;; Namazu failure reason is in this buffer, show it if
1252           ;; the user wants it.
1253           (when (> gnus-verbose 6)
1254             (display-buffer nnir-tmp-buffer))))
1255
1256       ;; Namazu output looks something like this:
1257       ;; 2. Re: Gnus agent expire broken (score: 55)
1258       ;; /home/henrik/Mail/mail/sent/1310 (4,138 bytes)
1259
1260       (goto-char (point-min))
1261       (while (re-search-forward
1262               "^\\([0-9]+\\.\\).*\\((score: \\([0-9]+\\)\\))\n\\([^ ]+\\)"
1263               nil t)
1264         (setq score (match-string 3)
1265               group (file-name-directory (match-string 4))
1266               article (file-name-nondirectory (match-string 4)))
1267
1268         ;; make sure article and group is sane
1269         (when (and (string-match "^[0-9]+$" article)
1270                    (not (null group)))
1271           (when (string-match (concat "^" nnir-namazu-remove-prefix) group)
1272             (setq group (replace-match "" t t group)))
1273
1274           ;; remove trailing slash from groupname
1275           (setq group (substring group 0 -1))
1276
1277           ;; stuff results into artlist vector
1278           (push (vector (substitute ?. ?/ group)
1279                         (string-to-number article)
1280                         (string-to-number score)) artlist)))
1281
1282       ;; sort artlist by score
1283       (apply 'vector
1284              (sort* artlist
1285                     (function (lambda (x y)
1286                                 (> (nnir-artitem-rsv x)
1287                                    (nnir-artitem-rsv y)))))))))
1288
1289 ;;; Util Code:
1290
1291 (defun nnir-read-parms (query)
1292   "Reads additional search parameters according to `nnir-engines'."
1293   (let ((parmspec (caddr (assoc nnir-search-engine nnir-engines))))
1294     (cons (cons 'query query)
1295           (mapcar 'nnir-read-parm parmspec))))
1296
1297 (defun nnir-read-parm (parmspec)
1298   "Reads a single search parameter.
1299 `parmspec' is a cons cell, the car is a symbol, the cdr is a prompt."
1300   (let ((sym (car parmspec))
1301         (prompt (cdr parmspec)))
1302     (cons sym (read-string prompt))))
1303
1304 (defun nnir-run-query (query)
1305   "Invoke appropriate search engine function (see `nnir-engines').
1306 If some groups were process-marked, run the query for each of the groups
1307 and concat the results."
1308   (let ((search-func (cadr (assoc nnir-search-engine nnir-engines)))
1309         (q (car (read-from-string query))))
1310     (if gnus-group-marked
1311         (apply 'append
1312                (mapcar (lambda (x)
1313                          (funcall search-func q x))
1314                        gnus-group-marked))
1315       (funcall search-func q nil))))
1316
1317 (defun nnir-group-full-name (shortname)
1318   "For the given group name, return a full Gnus group name.
1319 The Gnus backend/server information is added."
1320   (gnus-group-prefixed-name shortname nnir-mail-backend))
1321
1322 (defun nnir-possibly-change-server (server)
1323   (unless (and server (nnir-server-opened server))
1324     (nnir-open-server server)))
1325
1326
1327 ;; Data type article list.
1328
1329 (defun nnir-artlist-length (artlist)
1330   "Returns number of articles in artlist."
1331   (length artlist))
1332
1333 (defun nnir-artlist-article (artlist n)
1334   "Returns from ARTLIST the Nth artitem (counting starting at 1)."
1335   (elt artlist (1- n)))
1336
1337 (defun nnir-artitem-group (artitem)
1338   "Returns the group from the ARTITEM."
1339   (elt artitem 0))
1340
1341 (defun nnir-artlist-artitem-group (artlist n)
1342   "Returns from ARTLIST the group of the Nth artitem (counting from 1)."
1343   (nnir-artitem-group (nnir-artlist-article artlist n)))
1344
1345 (defun nnir-artitem-number (artitem)
1346   "Returns the number from the ARTITEM."
1347   (elt artitem 1))
1348
1349 (defun nnir-artlist-artitem-number (artlist n)
1350   "Returns from ARTLIST the number of the Nth artitem (counting from 1)."
1351   (nnir-artitem-number (nnir-artlist-article artlist n)))
1352
1353 (defun nnir-artitem-rsv (artitem)
1354   "Returns the Retrieval Status Value (RSV, score) from the ARTITEM."
1355   (elt artitem 2))
1356
1357 (defun nnir-artlist-artitem-rsv (artlist n)
1358   "Returns from ARTLIST the Retrieval Status Value of the Nth artitem
1359 (counting from 1)."
1360   (nnir-artitem-rsv (nnir-artlist-article artlist n)))
1361
1362 ;; unused?
1363 (defun nnir-artlist-groups (artlist)
1364   "Returns a list of all groups in the given ARTLIST."
1365   (let ((res nil)
1366         (with-dups nil))
1367     ;; from each artitem, extract group component
1368     (setq with-dups (mapcar 'nnir-artitem-group artlist))
1369     ;; remove duplicates from above
1370     (mapcar (function (lambda (x) (add-to-list 'res x)))
1371             with-dups)
1372     res))
1373
1374
1375 ;; The end.
1376 (provide 'nnir)