(minibuffer-default-add-shell-commands): Fix doc-string typo.
[emacs.git] / lisp / gnus / nnir.el
blob84c9d9f802d3ac9015c39eddce8ce4d34dae93f9
1 ;;; nnir.el --- search mail with various search engines -*- coding: iso-8859-1 -*-
3 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
4 ;; 2007, 2008 Free Software Foundation, Inc.
6 ;; Author: Kai Großjohann <grossjohann@ls6.cs.uni-dortmund.de>
7 ;; Swish-e and Swish++ backends by:
8 ;; Christoph Conrad <christoph.conrad@gmx.de>.
9 ;; IMAP backend by: Simon Josefsson <jas@pdc.kth.se>.
10 ;; IMAP search by: Torsten Hilbrich <torsten.hilbrich <at> gmx.net>
11 ;; IMAP search improved by Daniel Pittman <daniel@rimspace.net>.
12 ;; nnmaildir support for Swish++ and Namazu backends by:
13 ;; Justus Piater <Justus <at> Piater.name>
14 ;; Keywords: news mail searching ir
16 ;; This file is part of GNU Emacs.
18 ;; GNU Emacs is free software: you can redistribute it and/or modify
19 ;; it under the terms of the GNU General Public License as published by
20 ;; the Free Software Foundation, either version 3 of the License, or
21 ;; (at your option) any later version.
23 ;; GNU Emacs is distributed in the hope that it will be useful,
24 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
25 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 ;; GNU General Public License for more details.
28 ;; You should have received a copy of the GNU General Public License
29 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
31 ;;; Commentary:
33 ;; TODO: Documentation in the Gnus manual
35 ;; From: Reiner Steib
36 ;; Subject: Re: Including nnir.el
37 ;; Newsgroups: gmane.emacs.gnus.general
38 ;; Message-ID: <v9d5dnp6aq.fsf@marauder.physik.uni-ulm.de>
39 ;; Date: 2006-06-05 22:49:01 GMT
41 ;; On Sun, Jun 04 2006, Sascha Wilde wrote:
43 ;; > The one thing most hackers like to forget: Documentation. By now the
44 ;; > documentation is only in the comments at the head of the source, I
45 ;; > would use it as basis to cook up some minimal texinfo docs.
46 ;; >
47 ;; > Where in the existing gnus manual would this fit best?
49 ;; Maybe (info "(gnus)Combined Groups") for a general description.
50 ;; `gnus-group-make-nnir-group' might be described in (info
51 ;; "(gnus)Foreign Groups") as well.
54 ;; The most recent version of this can always be fetched from the Gnus
55 ;; CVS repository. See http://www.gnus.org/ for more information.
57 ;; This code is still in the development stage but I'd like other
58 ;; people to have a look at it. Please do not hesitate to contact me
59 ;; with your ideas.
61 ;; What does it do? Well, it allows you to index your mail using some
62 ;; search engine (freeWAIS-sf, swish-e and others -- see later),
63 ;; then type `G G' in the Group buffer and issue a query to the search
64 ;; engine. You will then get a buffer which shows all articles
65 ;; matching the query, sorted by Retrieval Status Value (score).
67 ;; When looking at the retrieval result (in the Summary buffer) you
68 ;; can type `G T' (aka M-x gnus-summary-nnir-goto-thread RET) on an
69 ;; article. You will be teleported into the group this article came
70 ;; from, showing the thread this article is part of. (See below for
71 ;; restrictions.)
73 ;; The Lisp installation is simple: just put this file on your
74 ;; load-path, byte-compile it, and load it from ~/.gnus or something.
75 ;; This will install a new command `G G' in your Group buffer for
76 ;; searching your mail. Note that you also need to configure a number
77 ;; of variables, as described below.
79 ;; Restrictions:
81 ;; * If you don't use HyREX as your search engine, this expects that
82 ;; you use nnml or another one-file-per-message backend, because the
83 ;; others doesn't support nnfolder.
84 ;; * It can only search the mail backend's which are supported by one
85 ;; search engine, because of different query languages.
86 ;; * There are restrictions to the Wais setup.
87 ;; * There are restrictions to the imap setup.
88 ;; * gnus-summary-nnir-goto-thread: Fetches whole group first, before
89 ;; limiting to the right articles. This is much too slow, of
90 ;; course. May issue a query for number of articles to fetch; you
91 ;; must accept the default of all articles at this point or things
92 ;; may break.
94 ;; The Lisp setup involves setting a few variables and setting up the
95 ;; search engine. You can define the variables in the server definition
96 ;; like this :
97 ;; (setq gnus-secondary-select-methods '(
98 ;; (nnimap "" (nnimap-address "localhost")
99 ;; (nnir-search-engine hyrex)
100 ;; (nnir-hyrex-additional-switches ("-d" "ddl-nnimap.xml"))
101 ;; )))
102 ;; Or you can define the global ones. The variables set in the mailer-
103 ;; definition will be used first.
104 ;; The variable to set is `nnir-search-engine'. Choose one of the engines
105 ;; listed in `nnir-engines'. (Actually `nnir-engines' is an alist,
106 ;; type `C-h v nnir-engines RET' for more information; this includes
107 ;; examples for setting `nnir-search-engine', too.)
109 ;; The variable nnir-mail-backend isn't used anymore.
112 ;; You must also set up a search engine. I'll tell you about the two
113 ;; search engines currently supported:
115 ;; 1. freeWAIS-sf
117 ;; As always with freeWAIS-sf, you need a so-called `format file'. I
118 ;; use the following file:
120 ;; ,-----
121 ;; | # Kai's format file for freeWAIS-sf for indexing mails.
122 ;; | # Each mail is in a file, much like the MH format.
123 ;; |
124 ;; | # Document separator should never match -- each file is a document.
125 ;; | record-sep: /^@this regex should never match@$/
126 ;; |
127 ;; | # Searchable fields specification.
128 ;; |
129 ;; | region: /^[sS]ubject:/ /^[sS]ubject: */
130 ;; | subject "Subject header" stemming TEXT BOTH
131 ;; | end: /^[^ \t]/
132 ;; |
133 ;; | region: /^([tT][oO]|[cC][cC]):/ /^([tT][oO]|[cC][cC]): */
134 ;; | to "To and Cc headers" SOUNDEX BOTH
135 ;; | end: /^[^ \t]/
136 ;; |
137 ;; | region: /^[fF][rR][oO][mM]:/ /^[fF][rR][oO][mM]: */
138 ;; | from "From header" SOUNDEX BOTH
139 ;; | end: /^[^ \t]/
140 ;; |
141 ;; | region: /^$/
142 ;; | stemming TEXT GLOBAL
143 ;; | end: /^@this regex should never match@$/
144 ;; `-----
146 ;; 1998-07-22: waisindex would dump core on me for large articles with
147 ;; the above settings. I used /^$/ as the end regex for the global
148 ;; field. That seemed to work okay.
150 ;; There is a Perl module called `WAIS.pm' which is available from
151 ;; CPAN as well as ls6-ftp.cs.uni-dortmund.de:/pub/wais/Perl. This
152 ;; module comes with a nifty tool called `makedb', which I use for
153 ;; indexing. Here's my `makedb.conf':
155 ;; ,-----
156 ;; | # Config file for makedb
157 ;; |
158 ;; | # Global options
159 ;; | waisindex = /usr/local/bin/waisindex
160 ;; | wais_opt = -stem -t fields
161 ;; | # `-stem' option necessary when `stemming' is specified for the
162 ;; | # global field in the *.fmt file
163 ;; |
164 ;; | # Own variables
165 ;; | homedir = /home/kai
166 ;; |
167 ;; | # The mail database.
168 ;; | database = mail
169 ;; | files = `find $homedir/Mail -name \*[0-9] -print`
170 ;; | dbdir = $homedir/.wais
171 ;; | limit = 100
172 ;; `-----
174 ;; The Lisp setup involves the `nnir-wais-*' variables. The most
175 ;; difficult to understand variable is probably
176 ;; `nnir-wais-remove-prefix'. Here's what it does: the output of
177 ;; `waissearch' basically contains the file name and the (full)
178 ;; directory name. As Gnus works with group names rather than
179 ;; directory names, the directory name is transformed into a group
180 ;; name as follows: first, a prefix is removed from the (full)
181 ;; directory name, then all `/' are replaced with `.'. The variable
182 ;; `nnir-wais-remove-prefix' should contain a regex matching exactly
183 ;; this prefix. It defaults to `$HOME/Mail/' (note the trailing
184 ;; slash).
186 ;; 2. Namazu
188 ;; The Namazu backend requires you to have one directory containing all
189 ;; index files, this is controlled by the `nnir-namazu-index-directory'
190 ;; variable. To function the `nnir-namazu-remove-prefix' variable must
191 ;; also be correct, see the documentation for `nnir-wais-remove-prefix'
192 ;; above.
194 ;; It is particularly important not to pass any any switches to namazu
195 ;; that will change the output format. Good switches to use include
196 ;; `--sort', `--ascending', `--early' and `--late'. Refer to the Namazu
197 ;; documentation for further information on valid switches.
199 ;; To index my mail with the `mknmz' program I use the following
200 ;; configuration file:
202 ;; ,----
203 ;; | package conf; # Don't remove this line!
204 ;; |
205 ;; | # Paths which will not be indexed. Don't use `^' or `$' anchors.
206 ;; | $EXCLUDE_PATH = "spam|sent";
207 ;; |
208 ;; | # Header fields which should be searchable. case-insensitive
209 ;; | $REMAIN_HEADER = "from|date|message-id|subject";
210 ;; |
211 ;; | # Searchable fields. case-insensitive
212 ;; | $SEARCH_FIELD = "from|date|message-id|subject";
213 ;; |
214 ;; | # The max length of a word.
215 ;; | $WORD_LENG_MAX = 128;
216 ;; |
217 ;; | # The max length of a field.
218 ;; | $MAX_FIELD_LENGTH = 256;
219 ;; `----
221 ;; My mail is stored in the directories ~/Mail/mail/, ~/Mail/lists/ and
222 ;; ~/Mail/archive/, so to index them I go to the directory set in
223 ;; `nnir-namazu-index-directory' and issue the following command.
225 ;; mknmz --mailnews ~/Mail/archive/ ~/Mail/mail/ ~/Mail/lists/
227 ;; For maximum searching efficiency I have a cron job set to run this
228 ;; command every four hours.
230 ;; 3. HyREX
232 ;; The HyREX backend requires you to have one directory from where all
233 ;; your relative paths are to, if you use them. This directory must be
234 ;; set in the `nnir-hyrex-index-directory' variable, which defaults to
235 ;; your home directory. You must also pass the base, class and
236 ;; directory options or simply your dll to the `nnir-hyrex-programm' by
237 ;; setting the `nnir-hyrex-additional-switches' variable accordently.
238 ;; To function the `nnir-hyrex-remove-prefix' variable must also be
239 ;; correct, see the documentation for `nnir-wais-remove-prefix' above.
241 ;; 4. find-grep
243 ;; The find-grep engine simply runs find(1) to locate eligible
244 ;; articles and searches them with grep(1). This, of course, is much
245 ;; slower than using a proper search engine but OTOH doesn't require
246 ;; maintenance of an index and is still faster than using any built-in
247 ;; means for searching. The method specification of the server to
248 ;; search must include a directory for this engine to work (E.g.,
249 ;; `nnml-directory'). The tools must be POSIX compliant. GNU Find
250 ;; prior to version 4.2.12 (4.2.26 on Linux due to incorrect ARG_MAX
251 ;; handling) does not work.
252 ;; ,----
253 ;; | ;; find-grep configuration for searching the Gnus Cache
254 ;; |
255 ;; | (nnml "cache"
256 ;; | (nnml-get-new-mail nil)
257 ;; | (nnir-search-engine find-grep)
258 ;; | (nnml-directory "~/News/cache/")
259 ;; | (nnml-active-file "~/News/cache/active"))
260 ;; `----
262 ;; Developer information:
264 ;; I have tried to make the code expandable. Basically, it is divided
265 ;; into two layers. The upper layer is somewhat like the `nnvirtual'
266 ;; or `nnkiboze' backends: given a specification of what articles to
267 ;; show from another backend, it creates a group containing exactly
268 ;; those articles. The lower layer issues a query to a search engine
269 ;; and produces such a specification of what articles to show from the
270 ;; other backend.
272 ;; The interface between the two layers consists of the single
273 ;; function `nnir-run-query', which just selects the appropriate
274 ;; function for the search engine one is using. The input to
275 ;; `nnir-run-query' is a string, representing the query as input by
276 ;; the user. The output of `nnir-run-query' is supposed to be a
277 ;; vector, each element of which should in turn be a three-element
278 ;; vector. The first element should be full group name of the article,
279 ;; the second element should be the article number, and the third
280 ;; element should be the Retrieval Status Value (RSV) as returned from
281 ;; the search engine. An RSV is the score assigned to the document by
282 ;; the search engine. For Boolean search engines, the
283 ;; RSV is always 1000 (or 1 or 100, or whatever you like).
285 ;; The sorting order of the articles in the summary buffer created by
286 ;; nnir is based on the order of the articles in the above mentioned
287 ;; vector, so that's where you can do the sorting you'd like. Maybe
288 ;; it would be nice to have a way of displaying the search result
289 ;; sorted differently?
291 ;; So what do you need to do when you want to add another search
292 ;; engine? You write a function that executes the query. Temporary
293 ;; data from the search engine can be put in `nnir-tmp-buffer'. This
294 ;; function should return the list of articles as a vector, as
295 ;; described above. Then, you need to register this backend in
296 ;; `nnir-engines'. Then, users can choose the backend by setting
297 ;; `nnir-search-engine'.
299 ;; Todo, or future ideas:
301 ;; * It should be possible to restrict search to certain groups.
303 ;; * There is currently no error checking.
305 ;; * The summary buffer display is currently really ugly, with all the
306 ;; added information in the subjects. How could I make this
307 ;; prettier?
309 ;; * A function which can be called from an nnir summary buffer which
310 ;; teleports you into the group the current article came from and
311 ;; shows you the whole thread this article is part of.
312 ;; Implementation suggestions?
313 ;; (1998-07-24: There is now a preliminary implementation, but
314 ;; it is much too slow and quite fragile.)
316 ;; * Support other mail backends. In particular, probably quite a few
317 ;; people use nnfolder. How would one go about searching nnfolders
318 ;; and producing the right data needed? The group name and the RSV
319 ;; are simple, but what about the article number?
320 ;; - The article number is encoded in the `X-Gnus-Article-Number'
321 ;; header of each mail.
322 ;; - The HyREX engine supports nnfolder.
324 ;; * Support compressed mail files. Probably, just stripping off the
325 ;; `.gz' or `.Z' file name extension is sufficient.
327 ;; * At least for imap, the query is performed twice.
330 ;; Have you got other ideas?
332 ;;; Setup Code:
334 (require 'nnoo)
335 (require 'gnus-group)
336 (require 'gnus-sum)
337 (require 'message)
338 (require 'gnus-util)
339 (eval-and-compile
340 (require 'cl))
342 (nnoo-declare nnir)
343 (nnoo-define-basics nnir)
345 (gnus-declare-backend "nnir" 'mail)
347 (defvar nnir-imap-search-field "TEXT"
348 "The IMAP search item when doing an nnir search")
350 (defvar nnir-imap-search-arguments
351 '(("Whole message" . "TEXT")
352 ("Subject" . "SUBJECT")
353 ("To" . "TO")
354 ("From" . "FROM")
355 (nil . "HEADER \"%s\""))
356 "Mapping from user readable strings to IMAP search items for use in nnir")
358 (defvar nnir-imap-search-argument-history ()
359 "The history for querying search options in nnir")
361 ;;; Developer Extension Variable:
363 (defvar nnir-engines
364 `((wais nnir-run-waissearch
366 (imap nnir-run-imap
367 ((criteria
368 "Search in: " ; Prompt
369 ,nnir-imap-search-arguments ; alist for completing
370 nil ; no filtering
371 nil ; allow any user input
372 nil ; initial value
373 nnir-imap-search-argument-history ; the history to use
374 ,nnir-imap-search-field ; default
376 (swish++ nnir-run-swish++
377 ((group . "Group spec: ")))
378 (swish-e nnir-run-swish-e
379 ((group . "Group spec: ")))
380 (namazu nnir-run-namazu
382 (hyrex nnir-run-hyrex
383 ((group . "Group spec: ")))
384 (find-grep nnir-run-find-grep
385 ((grep-options . "Grep options: "))))
386 "Alist of supported search engines.
387 Each element in the alist is a three-element list (ENGINE FUNCTION ARGS).
388 ENGINE is a symbol designating the searching engine. FUNCTION is also
389 a symbol, giving the function that does the search. The third element
390 ARGS is a list of cons pairs (PARAM . PROMPT). When issuing a query,
391 the FUNCTION will issue a query for each of the PARAMs, using PROMPT.
393 The value of `nnir-search-engine' must be one of the ENGINE symbols.
394 For example, use the following line for searching using freeWAIS-sf:
395 (setq nnir-search-engine 'wais)
396 Use the following line if you read your mail via IMAP and your IMAP
397 server supports searching:
398 (setq nnir-search-engine 'imap)
399 Note that you have to set additional variables for most backends. For
400 example, the `wais' backend needs the variables `nnir-wais-program',
401 `nnir-wais-database' and `nnir-wais-remove-prefix'.
403 Add an entry here when adding a new search engine.")
405 ;;; User Customizable Variables:
407 (defgroup nnir nil
408 "Search nnmh and nnml groups in Gnus with swish-e, freeWAIS-sf, or EWS."
409 :group 'gnus)
411 ;; Mail backend.
413 ;; TODO:
414 ;; If `nil', use server parameters to find out which server to search. CCC
416 (defcustom nnir-mail-backend '(nnml "")
417 "*Specifies which backend should be searched.
418 More precisely, this is used to determine from which backend to fetch the
419 messages found.
421 This must be equal to an existing server, so maybe it is best to use
422 something like the following:
423 (setq nnir-mail-backend (nth 0 gnus-secondary-select-methods))
424 The above line works fine if the mail backend you want to search is
425 the first element of gnus-secondary-select-methods (`nth' starts counting
426 at zero)."
427 :type '(sexp)
428 :group 'nnir)
430 ;; Search engine to use.
432 (defcustom nnir-search-engine 'wais
433 "*The search engine to use. Must be a symbol.
434 See `nnir-engines' for a list of supported engines, and for example
435 settings of `nnir-search-engine'."
436 :type '(sexp)
437 :group 'nnir)
439 ;; freeWAIS-sf.
441 (defcustom nnir-wais-program "waissearch"
442 "*Name of waissearch executable."
443 :type '(string)
444 :group 'nnir)
446 (defcustom nnir-wais-database (expand-file-name "~/.wais/mail")
447 "*Name of Wais database containing the mail.
449 Note that this should be a file name without extension. For example,
450 if you have a file /home/john/.wais/mail.fmt, use this:
451 (setq nnir-wais-database \"/home/john/.wais/mail\")
452 The string given here is passed to `waissearch -d' as-is."
453 :type '(file)
454 :group 'nnir)
456 (defcustom nnir-wais-remove-prefix (concat (getenv "HOME") "/Mail/")
457 "*The prefix to remove from each directory name returned by waissearch
458 in order to get a group name (albeit with / instead of .). This is a
459 regular expression.
461 For example, suppose that Wais returns file names such as
462 \"/home/john/Mail/mail/misc/42\". For this example, use the following
463 setting: (setq nnir-wais-remove-prefix \"/home/john/Mail/\")
464 Note the trailing slash. Removing this prefix gives \"mail/misc/42\".
465 `nnir' knows to remove the \"/42\" and to replace \"/\" with \".\" to
466 arrive at the correct group name, \"mail.misc\"."
467 :type '(regexp)
468 :group 'nnir)
470 (defcustom nnir-swish++-configuration-file
471 (expand-file-name "~/Mail/swish++.conf")
472 "*Configuration file for swish++."
473 :type '(file)
474 :group 'nnir)
476 (defcustom nnir-swish++-program "search"
477 "*Name of swish++ search executable."
478 :type '(string)
479 :group 'nnir)
481 (defcustom nnir-swish++-additional-switches '()
482 "*A list of strings, to be given as additional arguments to swish++.
484 Note that this should be a list. Ie, do NOT use the following:
485 (setq nnir-swish++-additional-switches \"-i -w\") ; wrong
486 Instead, use this:
487 (setq nnir-swish++-additional-switches '(\"-i\" \"-w\"))"
488 :type '(repeat (string))
489 :group 'nnir)
491 (defcustom nnir-swish++-remove-prefix (concat (getenv "HOME") "/Mail/")
492 "*The prefix to remove from each file name returned by swish++
493 in order to get a group name (albeit with / instead of .). This is a
494 regular expression.
496 This variable is very similar to `nnir-wais-remove-prefix', except
497 that it is for swish++, not Wais."
498 :type '(regexp)
499 :group 'nnir)
501 ;; Swish-E.
502 ;; URL: http://sunsite.berkeley.edu/SWISH-E/
503 ;; New version: http://www.boe.es/swish-e
504 ;; Variables `nnir-swish-e-index-file', `nnir-swish-e-program' and
505 ;; `nnir-swish-e-additional-switches'
507 (make-obsolete-variable 'nnir-swish-e-index-file
508 'nnir-swish-e-index-files)
509 (defcustom nnir-swish-e-index-file
510 (expand-file-name "~/Mail/index.swish-e")
511 "*Index file for swish-e.
512 This could be a server parameter.
513 It is never consulted once `nnir-swish-e-index-files', which should be
514 used instead, has been customized."
515 :type '(file)
516 :group 'nnir)
518 (defcustom nnir-swish-e-index-files
519 (list nnir-swish-e-index-file)
520 "*List of index files for swish-e.
521 This could be a server parameter."
522 :type '(repeat (file))
523 :group 'nnir)
525 (defcustom nnir-swish-e-program "swish-e"
526 "*Name of swish-e search executable.
527 This cannot be a server parameter."
528 :type '(string)
529 :group 'nnir)
531 (defcustom nnir-swish-e-additional-switches '()
532 "*A list of strings, to be given as additional arguments to swish-e.
534 Note that this should be a list. Ie, do NOT use the following:
535 (setq nnir-swish-e-additional-switches \"-i -w\") ; wrong
536 Instead, use this:
537 (setq nnir-swish-e-additional-switches '(\"-i\" \"-w\"))
539 This could be a server parameter."
540 :type '(repeat (string))
541 :group 'nnir)
543 (defcustom nnir-swish-e-remove-prefix (concat (getenv "HOME") "/Mail/")
544 "*The prefix to remove from each file name returned by swish-e
545 in order to get a group name (albeit with / instead of .). This is a
546 regular expression.
548 This variable is very similar to `nnir-wais-remove-prefix', except
549 that it is for swish-e, not Wais.
551 This could be a server parameter."
552 :type '(regexp)
553 :group 'nnir)
555 ;; HyREX engine, see <URL:http://ls6-www.cs.uni-dortmund.de/>
557 (defcustom nnir-hyrex-program "nnir-search"
558 "*Name of the nnir-search executable."
559 :type '(string)
560 :group 'nnir)
562 (defcustom nnir-hyrex-additional-switches '()
563 "*A list of strings, to be given as additional arguments for nnir-search.
564 Note that this should be a list. Ie, do NOT use the following:
565 (setq nnir-hyrex-additional-switches \"-ddl ddl.xml -c nnir\") ; wrong !
566 Instead, use this:
567 (setq nnir-hyrex-additional-switches '(\"-ddl\" \"ddl.xml\" \"-c\" \"nnir\"))"
568 :type '(repeat (string))
569 :group 'nnir)
571 (defcustom nnir-hyrex-index-directory (getenv "HOME")
572 "*Index directory for HyREX."
573 :type '(directory)
574 :group 'nnir)
576 (defcustom nnir-hyrex-remove-prefix (concat (getenv "HOME") "/Mail/")
577 "*The prefix to remove from each file name returned by HyREX
578 in order to get a group name (albeit with / instead of .).
580 For example, suppose that HyREX returns file names such as
581 \"/home/john/Mail/mail/misc/42\". For this example, use the following
582 setting: (setq nnir-hyrex-remove-prefix \"/home/john/Mail/\")
583 Note the trailing slash. Removing this prefix gives \"mail/misc/42\".
584 `nnir' knows to remove the \"/42\" and to replace \"/\" with \".\" to
585 arrive at the correct group name, \"mail.misc\"."
586 :type '(directory)
587 :group 'nnir)
589 ;; Namazu engine, see <URL:http://ww.namazu.org/>
591 (defcustom nnir-namazu-program "namazu"
592 "*Name of Namazu search executable."
593 :type '(string)
594 :group 'nnir)
596 (defcustom nnir-namazu-index-directory (expand-file-name "~/Mail/namazu/")
597 "*Index directory for Namazu."
598 :type '(directory)
599 :group 'nnir)
601 (defcustom nnir-namazu-additional-switches '()
602 "*A list of strings, to be given as additional arguments to namazu.
603 The switches `-q', `-a', and `-s' are always used, very few other switches
604 make any sense in this context.
606 Note that this should be a list. Ie, do NOT use the following:
607 (setq nnir-namazu-additional-switches \"-i -w\") ; wrong
608 Instead, use this:
609 (setq nnir-namazu-additional-switches '(\"-i\" \"-w\"))"
610 :type '(repeat (string))
611 :group 'nnir)
613 (defcustom nnir-namazu-remove-prefix (concat (getenv "HOME") "/Mail/")
614 "*The prefix to remove from each file name returned by Namazu
615 in order to get a group name (albeit with / instead of .).
617 This variable is very similar to `nnir-wais-remove-prefix', except
618 that it is for Namazu, not Wais."
619 :type '(directory)
620 :group 'nnir)
622 ;;; Internal Variables:
624 (defvar nnir-current-query nil
625 "Internal: stores current query (= group name).")
627 (defvar nnir-current-server nil
628 "Internal: stores current server (does it ever change?).")
630 (defvar nnir-current-group-marked nil
631 "Internal: stores current list of process-marked groups.")
633 (defvar nnir-artlist nil
634 "Internal: stores search result.")
636 (defvar nnir-tmp-buffer " *nnir*"
637 "Internal: temporary buffer.")
639 ;;; Code:
641 ;; Gnus glue.
643 (defun gnus-group-make-nnir-group (extra-parms query)
644 "Create an nnir group. Asks for query."
645 (interactive "P\nsQuery: ")
646 (setq nnir-current-query nil
647 nnir-current-server nil
648 nnir-current-group-marked nil
649 nnir-artlist nil)
650 (let ((parms nil))
651 (if extra-parms
652 (setq parms (nnir-read-parms query))
653 (setq parms (list (cons 'query query))))
654 (add-to-list 'parms (cons 'unique-id (message-unique-id)) t)
655 (gnus-group-read-ephemeral-group
656 (concat "nnir:" (prin1-to-string parms)) '(nnir "") t
657 (cons (current-buffer)
658 gnus-current-window-configuration)
659 nil)))
661 (eval-when-compile
662 (when (featurep 'xemacs)
663 ;; The `kbd' macro requires that the `read-kbd-macro' macro is available.
664 (require 'edmacro)))
666 (defun nnir-group-mode-hook ()
667 (define-key gnus-group-mode-map (kbd "G G")
668 'gnus-group-make-nnir-group))
669 (add-hook 'gnus-group-mode-hook 'nnir-group-mode-hook)
671 ;; Why is this needed? Is this for compatibility with old/new gnusae? Using
672 ;; gnus-group-server instead works for me. -- Justus Piater
673 (defmacro nnir-group-server (group)
674 "Return the server for a newsgroup GROUP.
675 The returned format is as `gnus-server-to-method' needs it. See
676 `gnus-group-real-prefix' and `gnus-group-real-name'."
677 `(let ((gname ,group))
678 (if (string-match "^\\([^:]+\\):" gname)
679 (progn
680 (setq gname (match-string 1 gname))
681 (if (string-match "^\\([^+]+\\)\\+\\(.+\\)$" gname)
682 (format "%s:%s" (match-string 1 gname) (match-string 2 gname))
683 (concat gname ":")))
684 (format "%s:%s" (car gnus-select-method) (cadr gnus-select-method)))))
686 ;; Summary mode commands.
688 (defun gnus-summary-nnir-goto-thread ()
689 "Only applies to nnir groups. Go to group this article came from
690 and show thread that contains this article."
691 (interactive)
692 (unless (eq 'nnir (car (gnus-find-method-for-group gnus-newsgroup-name)))
693 (error "Can't execute this command unless in nnir group."))
694 (let* ((cur (gnus-summary-article-number))
695 (group (nnir-artlist-artitem-group nnir-artlist cur))
696 (backend-number (nnir-artlist-artitem-number nnir-artlist cur))
697 server backend-group)
698 (setq server (nnir-group-server group))
699 (setq backend-group (gnus-group-real-name group))
700 (gnus-group-read-ephemeral-group
701 backend-group
702 (gnus-server-to-method server)
703 t ; activate
704 (cons (current-buffer)
705 'summary) ; window config
707 (list backend-number))
708 (gnus-summary-limit (list backend-number))
709 (gnus-summary-refer-thread)))
711 (if (fboundp 'eval-after-load)
712 (eval-after-load "gnus-sum"
713 '(define-key gnus-summary-goto-map
714 "T" 'gnus-summary-nnir-goto-thread))
715 (add-hook 'gnus-summary-mode-hook
716 (function (lambda ()
717 (define-key gnus-summary-goto-map
718 "T" 'gnus-summary-nnir-goto-thread)))))
722 ;; Gnus backend interface functions.
724 (deffoo nnir-open-server (server &optional definitions)
725 ;; Just set the server variables appropriately.
726 (nnoo-change-server 'nnir server definitions))
728 (deffoo nnir-request-group (group &optional server fast)
729 "GROUP is the query string."
730 (nnir-possibly-change-server server)
731 ;; Check for cache and return that if appropriate.
732 (if (and (equal group nnir-current-query)
733 (equal gnus-group-marked nnir-current-group-marked)
734 (or (null server)
735 (equal server nnir-current-server)))
736 nnir-artlist
737 ;; Cache miss.
738 (setq nnir-artlist (nnir-run-query group)))
739 (save-excursion
740 (set-buffer nntp-server-buffer)
741 (if (zerop (length nnir-artlist))
742 (progn
743 (setq nnir-current-query nil
744 nnir-current-server nil
745 nnir-current-group-marked nil
746 nnir-artlist nil)
747 (nnheader-report 'nnir "Search produced empty results."))
748 ;; Remember data for cache.
749 (setq nnir-current-query group)
750 (when server (setq nnir-current-server server))
751 (setq nnir-current-group-marked gnus-group-marked)
752 (nnheader-insert "211 %d %d %d %s\n"
753 (nnir-artlist-length nnir-artlist) ; total #
754 1 ; first #
755 (nnir-artlist-length nnir-artlist) ; last #
756 group)))) ; group name
758 (deffoo nnir-retrieve-headers (articles &optional group server fetch-old)
759 (save-excursion
760 (let ((artlist (copy-sequence articles))
761 art artitem artgroup artno artrsv artfullgroup
762 novitem novdata foo server)
763 (while (not (null artlist))
764 (setq art (car artlist))
765 (or (numberp art)
766 (nnheader-report
767 'nnir
768 "nnir-retrieve-headers doesn't grok message ids: %s"
769 art))
770 (setq artitem (nnir-artlist-article nnir-artlist art))
771 (setq artrsv (nnir-artitem-rsv artitem))
772 (setq artfullgroup (nnir-artitem-group artitem))
773 (setq artno (nnir-artitem-number artitem))
774 (setq artgroup (gnus-group-real-name artfullgroup))
775 (setq server (nnir-group-server artfullgroup))
776 ;; retrieve NOV or HEAD data for this article, transform into
777 ;; NOV data and prepend to `novdata'
778 (set-buffer nntp-server-buffer)
779 (nnir-possibly-change-server server)
780 (let ((gnus-override-method
781 (gnus-server-to-method server)))
782 (case (setq foo (gnus-retrieve-headers (list artno) artfullgroup nil))
783 (nov
784 (goto-char (point-min))
785 (setq novitem (nnheader-parse-nov))
786 (unless novitem
787 (pop-to-buffer nntp-server-buffer)
788 (error
789 "nnheader-parse-nov returned nil for article %s in group %s"
790 artno artfullgroup)))
791 (headers
792 (goto-char (point-min))
793 (setq novitem (nnheader-parse-head))
794 (unless novitem
795 (pop-to-buffer nntp-server-buffer)
796 (error
797 "nnheader-parse-head returned nil for article %s in group %s"
798 artno artfullgroup)))
799 (t (error "Unknown header type %s while requesting article %s of group %s"
800 foo artno artfullgroup))))
801 ;; replace article number in original group with article number
802 ;; in nnir group
803 (mail-header-set-number novitem art)
804 (mail-header-set-from novitem
805 (mail-header-from novitem))
806 (mail-header-set-subject
807 novitem
808 (format "[%d: %s/%d] %s"
809 artrsv artgroup artno
810 (mail-header-subject novitem)))
811 ;;-(mail-header-set-extra novitem nil)
812 (push novitem novdata)
813 (setq artlist (cdr artlist)))
814 (setq novdata (nreverse novdata))
815 (set-buffer nntp-server-buffer) (erase-buffer)
816 (mapc 'nnheader-insert-nov novdata)
817 'nov)))
819 (deffoo nnir-request-article (article
820 &optional group server to-buffer)
821 (if (stringp article)
822 (nnheader-report
823 'nnir
824 "nnir-retrieve-headers doesn't grok message ids: %s"
825 article)
826 (save-excursion
827 (let* ((artitem (nnir-artlist-article nnir-artlist
828 article))
829 (artfullgroup (nnir-artitem-group artitem))
830 (artno (nnir-artitem-number artitem))
831 ;; Bug?
832 ;; Why must we bind nntp-server-buffer here? It won't
833 ;; work if `buf' is used, say. (Of course, the set-buffer
834 ;; line below must then be updated, too.)
835 (nntp-server-buffer (or to-buffer nntp-server-buffer)))
836 (set-buffer nntp-server-buffer)
837 (erase-buffer)
838 (message "Requesting article %d from group %s"
839 artno artfullgroup)
840 (gnus-request-article artno artfullgroup nntp-server-buffer)
841 (cons artfullgroup artno)))))
844 (nnoo-define-skeleton nnir)
847 (defmacro nnir-add-result (dirnam artno score prefix server artlist)
848 "Ask `nnir-compose-result' to construct a result vector,
849 and if it is non-nil, add it to artlist."
850 `(let ((result (nnir-compose-result ,dirnam ,artno ,score ,prefix ,server)))
851 (when (not (null result))
852 (push result ,artlist))))
854 (autoload 'nnmaildir-base-name-to-article-number "nnmaildir")
856 ;; Helper function currently used by the Swish++ and Namazu backends;
857 ;; perhaps useful for other backends as well
858 (defun nnir-compose-result (dirnam article score prefix server)
859 "Extract the group from dirnam, and create a result vector
860 ready to be added to the list of search results."
862 ;; remove nnir-*-remove-prefix from beginning of dirnam filename
863 (when (string-match (concat "^" prefix) dirnam)
864 (setq dirnam (replace-match "" t t dirnam)))
866 (when (file-readable-p (concat prefix dirnam article))
867 ;; remove trailing slash and, for nnmaildir, cur/new/tmp
868 (setq dirnam
869 (substring dirnam 0 (if (string= server "nnmaildir:") -5 -1)))
871 ;; Set group to dirnam without any leading dots or slashes,
872 ;; and with all subsequent slashes replaced by dots
873 (let ((group (gnus-replace-in-string
874 (gnus-replace-in-string dirnam "^[./\\]" "" t)
875 "[/\\]" "." t)))
877 (vector (nnir-group-full-name group server)
878 (if (string= server "nnmaildir:")
879 (nnmaildir-base-name-to-article-number
880 (substring article 0 (string-match ":" article))
881 group nil)
882 (string-to-number article))
883 (string-to-number score)))))
885 ;;; Search Engine Interfaces:
887 ;; freeWAIS-sf interface.
888 (defun nnir-run-waissearch (query server &optional group)
889 "Run given query agains waissearch. Returns vector of (group name, file name)
890 pairs (also vectors, actually)."
891 (when group
892 (error "The freeWAIS-sf backend cannot search specific groups."))
893 (save-excursion
894 (let ((qstring (cdr (assq 'query query)))
895 (prefix (nnir-read-server-parm 'nnir-wais-remove-prefix server))
896 artlist score artno dirnam)
897 (set-buffer (get-buffer-create nnir-tmp-buffer))
898 (erase-buffer)
899 (message "Doing WAIS query %s..." query)
900 (call-process nnir-wais-program
901 nil ; input from /dev/null
902 t ; output to current buffer
903 nil ; don't redisplay
904 "-d" (nnir-read-server-parm 'nnir-wais-database server) ; database to search
905 qstring)
906 (message "Massaging waissearch output...")
907 ;; remove superfluous lines
908 (keep-lines "Score:")
909 ;; extract data from result lines
910 (goto-char (point-min))
911 (while (re-search-forward
912 "Score: +\\([0-9]+\\).*'\\([0-9]+\\) +\\([^']+\\)/'" nil t)
913 (setq score (match-string 1)
914 artno (match-string 2)
915 dirnam (match-string 3))
916 (unless (string-match prefix dirnam)
917 (nnheader-report 'nnir "Dir name %s doesn't contain prefix %s"
918 dirnam prefix))
919 (setq group (substitute ?. ?/ (replace-match "" t t dirnam)))
920 (push (vector (nnir-group-full-name group server)
921 (string-to-number artno)
922 (string-to-number score))
923 artlist))
924 (message "Massaging waissearch output...done")
925 (apply 'vector
926 (sort* artlist
927 (function (lambda (x y)
928 (> (nnir-artitem-rsv x)
929 (nnir-artitem-rsv y)))))))))
931 ;; IMAP interface.
932 ;; todo:
933 ;; nnir invokes this two (2) times???!
934 ;; we should not use nnimap at all but open our own server connection
935 ;; we should not LIST * but use nnimap-list-pattern from defs
936 ;; send queries as literals
937 ;; handle errors
939 (autoload 'nnimap-open-server "nnimap")
940 (defvar nnimap-server-buffer) ;; nnimap.el
941 (autoload 'imap-mailbox-select "imap")
942 (autoload 'imap-search "imap")
943 (autoload 'imap-quote-specials "imap")
945 (defun nnir-run-imap (query srv &optional group-option)
946 "Run a search against an IMAP back-end server.
947 This uses a custom query language parser; see `nnir-imap-make-query' for
948 details on the language and supported extensions"
949 (save-excursion
950 (let ((qstring (cdr (assq 'query query)))
951 (server (cadr (gnus-server-to-method srv)))
952 (group (or group-option (gnus-group-group-name)))
953 (defs (caddr (gnus-server-to-method srv)))
954 (criteria (or (cdr (assq 'criteria query))
955 nnir-imap-search-field))
956 artlist buf)
957 (message "Opening server %s" server)
958 (condition-case ()
959 (when (nnimap-open-server server defs) ;; xxx
960 (setq buf nnimap-server-buffer) ;; xxx
961 (message "Searching %s..." group)
962 (let ((arts 0)
963 (mbx (gnus-group-real-name group)))
964 (when (imap-mailbox-select mbx nil buf)
965 (mapc
966 (lambda (artnum)
967 (push (vector group artnum 1) artlist)
968 (setq arts (1+ arts)))
969 (imap-search (nnir-imap-make-query criteria qstring) buf))
970 (message "Searching %s... %d matches" mbx arts)))
971 (message "Searching %s...done" group))
972 (quit nil))
973 (reverse artlist))))
975 (defun nnir-imap-make-query (criteria qstring)
976 "Parse the query string and criteria into an appropriate IMAP search
977 expression, returning the string query to make.
979 This implements a little language designed to return the expected results
980 to an arbitrary query string to the end user.
982 The search is always case-insensitive, as defined by RFC2060, and supports
983 the following features (inspired by the Google search input language):
985 Automatic \"and\" queries
986 If you specify multiple words then they will be treated as an \"and\"
987 expression intended to match all components.
989 Phrase searches
990 If you wrap your query in double-quotes then it will be treated as a
991 literal string.
993 Negative terms
994 If you precede a term with \"-\" then it will negate that.
996 \"OR\" queries
997 If you include an upper-case \"OR\" in your search it will cause the
998 term before it and the term after it to be treated as alternatives.
1000 In future the following will be added to the language:
1001 * support for date matches
1002 * support for location of text matching within the query
1003 * from/to/etc headers
1004 * additional search terms
1005 * flag based searching
1006 * anything else that the RFC supports, basically."
1007 ;; Walk through the query and turn it into an IMAP query string.
1008 (nnir-imap-query-to-imap criteria (nnir-imap-parse-query qstring)))
1011 (defun nnir-imap-query-to-imap (criteria query)
1012 "Turn a s-expression format query into IMAP."
1013 (mapconcat
1014 ;; Turn the expressions into IMAP text
1015 (lambda (item)
1016 (nnir-imap-expr-to-imap criteria item))
1017 ;; The query, already in s-expr format.
1018 query
1019 ;; Append a space between each expression
1020 " "))
1023 (defun nnir-imap-expr-to-imap (criteria expr)
1024 "Convert EXPR into an IMAP search expression on CRITERIA"
1025 ;; What sort of expression is this, eh?
1026 (cond
1027 ;; Simple string term
1028 ((stringp expr)
1029 (format "%s \"%s\"" criteria (imap-quote-specials expr)))
1030 ;; Trivial term: and
1031 ((eq expr 'and) nil)
1032 ;; Composite term: or expression
1033 ((eq (car-safe expr) 'or)
1034 (format "OR %s %s"
1035 (nnir-imap-expr-to-imap criteria (second expr))
1036 (nnir-imap-expr-to-imap criteria (third expr))))
1037 ;; Composite term: just the fax, mam
1038 ((eq (car-safe expr) 'not)
1039 (format "NOT (%s)" (nnir-imap-query-to-imap criteria (rest expr))))
1040 ;; Composite term: just expand it all.
1041 ((and (not (null expr)) (listp expr))
1042 (format "(%s)" (nnir-imap-query-to-imap criteria expr)))
1043 ;; Complex value, give up for now.
1044 (t (error "Unhandled input: %S" expr))))
1047 (defun nnir-imap-parse-query (string)
1048 "Turn STRING into an s-expression based query based on the IMAP
1049 query language as defined in `nnir-imap-make-query'.
1051 This involves turning individual tokens into higher level terms
1052 that the search language can then understand and use."
1053 (with-temp-buffer
1054 ;; Set up the parsing environment.
1055 (insert string)
1056 (goto-char (point-min))
1057 ;; Now, collect the output terms and return them.
1058 (let (out)
1059 (while (not (nnir-imap-end-of-input))
1060 (push (nnir-imap-next-expr) out))
1061 (reverse out))))
1064 (defun nnir-imap-next-expr (&optional count)
1065 "Return the next expression from the current buffer."
1066 (let ((term (nnir-imap-next-term count))
1067 (next (nnir-imap-peek-symbol)))
1068 ;; Are we looking at an 'or' expression?
1069 (cond
1070 ;; Handle 'expr or expr'
1071 ((eq next 'or)
1072 (list 'or term (nnir-imap-next-expr 2)))
1073 ;; Anything else
1074 (t term))))
1077 (defun nnir-imap-next-term (&optional count)
1078 "Return the next TERM from the current buffer."
1079 (let ((term (nnir-imap-next-symbol count)))
1080 ;; What sort of term is this?
1081 (cond
1082 ;; and -- just ignore it
1083 ((eq term 'and) 'and)
1084 ;; negated term
1085 ((eq term 'not) (list 'not (nnir-imap-next-expr)))
1086 ;; generic term
1087 (t term))))
1090 (defun nnir-imap-peek-symbol ()
1091 "Return the next symbol from the current buffer, but don't consume it."
1092 (save-excursion
1093 (nnir-imap-next-symbol)))
1095 (defun nnir-imap-next-symbol (&optional count)
1096 "Return the next symbol from the current buffer, or nil if we are
1097 at the end of the buffer. If supplied COUNT skips some symbols before
1098 returning the one at the supplied position."
1099 (when (and (numberp count) (> count 1))
1100 (nnir-imap-next-symbol (1- count)))
1101 (let ((case-fold-search t))
1102 ;; end of input stream?
1103 (unless (nnir-imap-end-of-input)
1104 ;; No, return the next symbol from the stream.
1105 (cond
1106 ;; negated expression -- return it and advance one char.
1107 ((looking-at "-") (forward-char 1) 'not)
1108 ;; quoted string
1109 ((looking-at "\"") (nnir-imap-delimited-string "\""))
1110 ;; list expression -- we parse the content and return this as a list.
1111 ((looking-at "(")
1112 (nnir-imap-parse-query (nnir-imap-delimited-string ")")))
1113 ;; keyword input -- return a symbol version
1114 ((looking-at "\\band\\b") (forward-char 3) 'and)
1115 ((looking-at "\\bor\\b") (forward-char 2) 'or)
1116 ((looking-at "\\bnot\\b") (forward-char 3) 'not)
1117 ;; Simple, boring keyword
1118 (t (let ((start (point))
1119 (end (if (search-forward-regexp "[[:blank:]]" nil t)
1120 (prog1
1121 (match-beginning 0)
1122 ;; unskip if we hit a non-blank terminal character.
1123 (when (string-match "[^[:blank:]]" (match-string 0))
1124 (backward-char 1)))
1125 (goto-char (point-max)))))
1126 (buffer-substring start end)))))))
1128 (defun nnir-imap-delimited-string (delimiter)
1129 "Return a delimited string from the current buffer."
1130 (let ((start (point)) end)
1131 (forward-char 1) ; skip the first delimiter.
1132 (while (not end)
1133 (unless (search-forward delimiter nil t)
1134 (error "Unmatched delimited input with %s in query" delimiter))
1135 (let ((here (point)))
1136 (unless (equal (buffer-substring (- here 2) (- here 1)) "\\")
1137 (setq end (point)))))
1138 (buffer-substring (1+ start) (1- end))))
1140 (defun nnir-imap-end-of-input ()
1141 "Are we at the end of input?"
1142 (skip-chars-forward "[[:blank:]]")
1143 (looking-at "$"))
1146 ;; Swish++ interface.
1147 ;; -cc- Todo
1148 ;; Search by
1149 ;; - group
1150 ;; Sort by
1151 ;; - rank (default)
1152 ;; - article number
1153 ;; - file size
1154 ;; - group
1155 (defun nnir-run-swish++ (query server &optional group)
1156 "Run QUERY against swish++.
1157 Returns a vector of (group name, file name) pairs (also vectors,
1158 actually).
1160 Tested with swish++ 4.7 on GNU/Linux and with swish++ 5.0b2 on
1161 Windows NT 4.0."
1163 (when group
1164 (error "The swish++ backend cannot search specific groups."))
1166 (save-excursion
1167 (let ( (qstring (cdr (assq 'query query)))
1168 (groupspec (cdr (assq 'group query)))
1169 (prefix (nnir-read-server-parm 'nnir-swish++-remove-prefix server))
1170 artlist
1171 ;; nnml-use-compressed-files might be any string, but probably this
1172 ;; is sufficient. Note that we can't only use the value of
1173 ;; nnml-use-compressed-files because old articles might have been
1174 ;; saved with a different value.
1175 (article-pattern (if (string= server "nnmaildir:")
1176 ":[0-9]+"
1177 "^[0-9]+\\(\\.[a-z0-9]+\\)?$"))
1178 score artno dirnam filenam)
1180 (when (equal "" qstring)
1181 (error "swish++: You didn't enter anything."))
1183 (set-buffer (get-buffer-create nnir-tmp-buffer))
1184 (erase-buffer)
1186 (if groupspec
1187 (message "Doing swish++ query %s on %s..." qstring groupspec)
1188 (message "Doing swish++ query %s..." qstring))
1190 (let* ((cp-list `( ,nnir-swish++-program
1191 nil ; input from /dev/null
1192 t ; output
1193 nil ; don't redisplay
1194 "--config-file" ,(nnir-read-server-parm 'nnir-swish++-configuration-file server)
1195 ,@(nnir-read-server-parm 'nnir-swish++-additional-switches server)
1196 ,qstring ; the query, in swish++ format
1198 (exitstatus
1199 (progn
1200 (message "%s args: %s" nnir-swish++-program
1201 (mapconcat 'identity (cddddr cp-list) " ")) ;; ???
1202 (apply 'call-process cp-list))))
1203 (unless (or (null exitstatus)
1204 (zerop exitstatus))
1205 (nnheader-report 'nnir "Couldn't run swish++: %s" exitstatus)
1206 ;; swish++ failure reason is in this buffer, show it if
1207 ;; the user wants it.
1208 (when (> gnus-verbose 6)
1209 (display-buffer nnir-tmp-buffer))))
1211 ;; The results are output in the format of:
1212 ;; V 4.7 Linux
1213 ;; rank relative-path-name file-size file-title
1214 ;; V 5.0b2:
1215 ;; rank relative-path-name file-size topic??
1216 ;; where rank is an integer from 1 to 100.
1217 (goto-char (point-min))
1218 (while (re-search-forward
1219 "\\(^[0-9]+\\) \\([^ ]+\\) [0-9]+ \\(.*\\)$" nil t)
1220 (setq score (match-string 1)
1221 filenam (match-string 2)
1222 artno (file-name-nondirectory filenam)
1223 dirnam (file-name-directory filenam))
1225 ;; don't match directories
1226 (when (string-match article-pattern artno)
1227 (when (not (null dirnam))
1229 ;; maybe limit results to matching groups.
1230 (when (or (not groupspec)
1231 (string-match groupspec dirnam))
1232 (nnir-add-result dirnam artno score prefix server artlist)))))
1234 (message "Massaging swish++ output...done")
1236 ;; Sort by score
1237 (apply 'vector
1238 (sort* artlist
1239 (function (lambda (x y)
1240 (> (nnir-artitem-rsv x)
1241 (nnir-artitem-rsv y)))))))))
1243 ;; Swish-E interface.
1244 (defun nnir-run-swish-e (query server &optional group)
1245 "Run given query against swish-e.
1246 Returns a vector of (group name, file name) pairs (also vectors,
1247 actually).
1249 Tested with swish-e-2.0.1 on Windows NT 4.0."
1251 ;; swish-e crashes with empty parameter to "-w" on commandline...
1252 (when group
1253 (error "The swish-e backend cannot search specific groups."))
1255 (save-excursion
1256 (let ((qstring (cdr (assq 'query query)))
1257 (prefix
1258 (or (nnir-read-server-parm 'nnir-swish-e-remove-prefix server)
1259 (error "Missing parameter `nnir-swish-e-remove-prefix'")))
1260 artlist score artno dirnam group )
1262 (when (equal "" qstring)
1263 (error "swish-e: You didn't enter anything."))
1265 (set-buffer (get-buffer-create nnir-tmp-buffer))
1266 (erase-buffer)
1268 (message "Doing swish-e query %s..." query)
1269 (let* ((index-files
1270 (or (nnir-read-server-parm
1271 'nnir-swish-e-index-files server)
1272 (error "Missing parameter `nnir-swish-e-index-files'")))
1273 (additional-switches
1274 (nnir-read-server-parm
1275 'nnir-swish-e-additional-switches server))
1276 (cp-list `(,nnir-swish-e-program
1277 nil ; input from /dev/null
1278 t ; output
1279 nil ; don't redisplay
1280 "-f" ,@index-files
1281 ,@additional-switches
1282 "-w"
1283 ,qstring ; the query, in swish-e format
1285 (exitstatus
1286 (progn
1287 (message "%s args: %s" nnir-swish-e-program
1288 (mapconcat 'identity (cddddr cp-list) " "))
1289 (apply 'call-process cp-list))))
1290 (unless (or (null exitstatus)
1291 (zerop exitstatus))
1292 (nnheader-report 'nnir "Couldn't run swish-e: %s" exitstatus)
1293 ;; swish-e failure reason is in this buffer, show it if
1294 ;; the user wants it.
1295 (when (> gnus-verbose 6)
1296 (display-buffer nnir-tmp-buffer))))
1298 ;; The results are output in the format of:
1299 ;; rank path-name file-title file-size
1300 (goto-char (point-min))
1301 (while (re-search-forward
1302 "\\(^[0-9]+\\) \\([^ ]+\\) \"\\([^\"]+\\)\" [0-9]+$" nil t)
1303 (setq score (match-string 1)
1304 artno (match-string 3)
1305 dirnam (file-name-directory (match-string 2)))
1307 ;; don't match directories
1308 (when (string-match "^[0-9]+$" artno)
1309 (when (not (null dirnam))
1311 ;; remove nnir-swish-e-remove-prefix from beginning of dirname
1312 (when (string-match (concat "^" prefix) dirnam)
1313 (setq dirnam (replace-match "" t t dirnam)))
1315 (setq dirnam (substring dirnam 0 -1))
1316 ;; eliminate all ".", "/", "\" from beginning. Always matches.
1317 (string-match "^[./\\]*\\(.*\\)$" dirnam)
1318 ;; "/" -> "."
1319 (setq group (substitute ?. ?/ (match-string 1 dirnam)))
1320 ;; Windows "\\" -> "."
1321 (setq group (substitute ?. ?\\ group))
1323 (push (vector (nnir-group-full-name group server)
1324 (string-to-number artno)
1325 (string-to-number score))
1326 artlist))))
1328 (message "Massaging swish-e output...done")
1330 ;; Sort by score
1331 (apply 'vector
1332 (sort* artlist
1333 (function (lambda (x y)
1334 (> (nnir-artitem-rsv x)
1335 (nnir-artitem-rsv y)))))))))
1337 ;; HyREX interface
1338 (defun nnir-run-hyrex (query server &optional group)
1339 (save-excursion
1340 (let ((artlist nil)
1341 (groupspec (cdr (assq 'group query)))
1342 (qstring (cdr (assq 'query query)))
1343 (prefix (nnir-read-server-parm 'nnir-hyrex-remove-prefix server))
1344 score artno dirnam)
1345 (when (and group groupspec)
1346 (error (concat "It does not make sense to use a group spec"
1347 " with process-marked groups.")))
1348 (when group
1349 (setq groupspec (gnus-group-real-name group)))
1350 (when (and group (not (equal group (nnir-group-full-name groupspec server))))
1351 (message "%s vs. %s" group (nnir-group-full-name groupspec server))
1352 (error "Server with groupspec doesn't match group !"))
1353 (set-buffer (get-buffer-create nnir-tmp-buffer))
1354 (erase-buffer)
1355 (if groupspec
1356 (message "Doing hyrex-search query %s on %s..." query groupspec)
1357 (message "Doing hyrex-search query %s..." query))
1358 (let* ((cp-list
1359 `( ,nnir-hyrex-program
1360 nil ; input from /dev/null
1361 t ; output
1362 nil ; don't redisplay
1363 "-i",(nnir-read-server-parm 'nnir-hyrex-index-directory server) ; index directory
1364 ,@(nnir-read-server-parm 'nnir-hyrex-additional-switches server)
1365 ,qstring ; the query, in hyrex-search format
1367 (exitstatus
1368 (progn
1369 (message "%s args: %s" nnir-hyrex-program
1370 (mapconcat 'identity (cddddr cp-list) " "))
1371 (apply 'call-process cp-list))))
1372 (unless (or (null exitstatus)
1373 (zerop exitstatus))
1374 (nnheader-report 'nnir "Couldn't run hyrex-search: %s" exitstatus)
1375 ;; nnir-search failure reason is in this buffer, show it if
1376 ;; the user wants it.
1377 (when (> gnus-verbose 6)
1378 (display-buffer nnir-tmp-buffer)))) ;; FIXME: Dont clear buffer !
1379 (if groupspec
1380 (message "Doing hyrex-search query \"%s\" on %s...done" qstring groupspec)
1381 (message "Doing hyrex-search query \"%s\"...done" qstring))
1382 (sit-for 0)
1383 ;; nnir-search returns:
1384 ;; for nnml/nnfolder: "filename mailid weigth"
1385 ;; for nnimap: "group mailid weigth"
1386 (goto-char (point-min))
1387 (delete-non-matching-lines "^\\S + [0-9]+ [0-9]+$")
1388 ;; HyREX couldn't search directly in groups -- so filter out here.
1389 (when groupspec
1390 (keep-lines groupspec))
1391 ;; extract data from result lines
1392 (goto-char (point-min))
1393 (while (re-search-forward
1394 "\\(\\S +\\) \\([0-9]+\\) \\([0-9]+\\)" nil t)
1395 (setq dirnam (match-string 1)
1396 artno (match-string 2)
1397 score (match-string 3))
1398 (when (string-match prefix dirnam)
1399 (setq dirnam (replace-match "" t t dirnam)))
1400 (push (vector (nnir-group-full-name (substitute ?. ?/ dirnam) server)
1401 (string-to-number artno)
1402 (string-to-number score))
1403 artlist))
1404 (message "Massaging hyrex-search output...done.")
1405 (apply 'vector
1406 (sort* artlist
1407 (function (lambda (x y)
1408 (if (string-lessp (nnir-artitem-group x)
1409 (nnir-artitem-group y))
1411 (< (nnir-artitem-number x)
1412 (nnir-artitem-number y)))))))
1415 ;; Namazu interface
1416 (defun nnir-run-namazu (query server &optional group)
1417 "Run given query against Namazu. Returns a vector of (group name, file name)
1418 pairs (also vectors, actually).
1420 Tested with Namazu 2.0.6 on a GNU/Linux system."
1421 (when group
1422 (error "The Namazu backend cannot search specific groups"))
1423 (save-excursion
1424 (let ((article-pattern (if (string= server "nnmaildir:")
1425 ":[0-9]+"
1426 "^[0-9]+$"))
1427 artlist
1428 (qstring (cdr (assq 'query query)))
1429 (prefix (nnir-read-server-parm 'nnir-namazu-remove-prefix server))
1430 score group article
1431 (process-environment (copy-sequence process-environment)))
1432 (setenv "LC_MESSAGES" "C")
1433 (set-buffer (get-buffer-create nnir-tmp-buffer))
1434 (erase-buffer)
1435 (let* ((cp-list
1436 `( ,nnir-namazu-program
1437 nil ; input from /dev/null
1438 t ; output
1439 nil ; don't redisplay
1440 "-q" ; don't be verbose
1441 "-a" ; show all matches
1442 "-s" ; use short format
1443 ,@(nnir-read-server-parm 'nnir-namazu-additional-switches server)
1444 ,qstring ; the query, in namazu format
1445 ,(nnir-read-server-parm 'nnir-namazu-index-directory server) ; index directory
1447 (exitstatus
1448 (progn
1449 (message "%s args: %s" nnir-namazu-program
1450 (mapconcat 'identity (cddddr cp-list) " "))
1451 (apply 'call-process cp-list))))
1452 (unless (or (null exitstatus)
1453 (zerop exitstatus))
1454 (nnheader-report 'nnir "Couldn't run namazu: %s" exitstatus)
1455 ;; Namazu failure reason is in this buffer, show it if
1456 ;; the user wants it.
1457 (when (> gnus-verbose 6)
1458 (display-buffer nnir-tmp-buffer))))
1460 ;; Namazu output looks something like this:
1461 ;; 2. Re: Gnus agent expire broken (score: 55)
1462 ;; /home/henrik/Mail/mail/sent/1310 (4,138 bytes)
1464 (goto-char (point-min))
1465 (while (re-search-forward
1466 "^\\([0-9]+\\.\\).*\\((score: \\([0-9]+\\)\\))\n\\([^ ]+\\)"
1467 nil t)
1468 (setq score (match-string 3)
1469 group (file-name-directory (match-string 4))
1470 article (file-name-nondirectory (match-string 4)))
1472 ;; make sure article and group is sane
1473 (when (and (string-match article-pattern article)
1474 (not (null group)))
1475 (nnir-add-result group article score prefix server artlist)))
1477 ;; sort artlist by score
1478 (apply 'vector
1479 (sort* artlist
1480 (function (lambda (x y)
1481 (> (nnir-artitem-rsv x)
1482 (nnir-artitem-rsv y)))))))))
1484 (defun nnir-run-find-grep (query server &optional group)
1485 "Run find and grep to obtain matching articles."
1486 (let* ((method (gnus-server-to-method server))
1487 (sym (intern
1488 (concat (symbol-name (car method)) "-directory")))
1489 (directory (cadr (assoc sym (cddr method))))
1490 (regexp (cdr (assoc 'query query)))
1491 (grep-options (cdr (assoc 'grep-options query)))
1492 artlist)
1493 (unless directory
1494 (error "No directory found in method specification of server %s"
1495 server))
1496 (message "Searching %s using find-grep..." (or group server))
1497 (save-window-excursion
1498 (set-buffer (get-buffer-create nnir-tmp-buffer))
1499 (erase-buffer)
1500 (if (> gnus-verbose 6)
1501 (pop-to-buffer (current-buffer)))
1502 (cd directory) ; Using relative paths simplifies postprocessing.
1503 (let ((group
1504 (if (not group)
1506 ;; Try accessing the group literally as well as
1507 ;; interpreting dots as directory separators so the
1508 ;; engine works with plain nnml as well as the Gnus
1509 ;; Cache.
1510 (find-if 'file-directory-p
1511 (let ((group (gnus-group-real-name group)))
1512 (list group (gnus-replace-in-string group "\\." "/" t)))))))
1513 (unless group
1514 (error "Cannot locate directory for group"))
1515 (save-excursion
1516 (apply
1517 'call-process "find" nil t
1518 "find" group "-type" "f" "-name" "[0-9]*" "-exec"
1519 "grep"
1520 `("-l" ,@(and grep-options
1521 ;; Note: the 3rd arg of `split-string' is not
1522 ;; available in Emacs 21.
1523 (delete "" (split-string grep-options "\\s-")))
1524 "-e" ,regexp "{}" "+"))))
1526 ;; Translate relative paths to group names.
1527 (while (not (eobp))
1528 (let* ((path (delete
1530 (split-string
1531 (buffer-substring (point) (line-end-position)) "/")))
1532 (art (string-to-number (car (last path)))))
1533 (while (string= "." (car path))
1534 (setq path (cdr path)))
1535 (let ((group (mapconcat 'identity (subseq path 0 -1) ".")))
1536 (push (vector (nnir-group-full-name group server) art 0)
1537 artlist))
1538 (forward-line 1)))
1539 (message "Searching %s using find-grep...done" (or group server))
1540 artlist)))
1542 ;;; Util Code:
1544 (defun nnir-read-parms (query)
1545 "Reads additional search parameters according to `nnir-engines'."
1546 (let ((parmspec (caddr (assoc nnir-search-engine nnir-engines))))
1547 (cons (cons 'query query)
1548 (mapcar 'nnir-read-parm parmspec))))
1550 (defun nnir-read-parm (parmspec)
1551 "Reads a single search parameter.
1552 `parmspec' is a cons cell, the car is a symbol, the cdr is a prompt."
1553 (let ((sym (car parmspec))
1554 (prompt (cdr parmspec)))
1555 (if (listp prompt)
1556 (let* ((result (apply 'completing-read prompt))
1557 (mapping (or (assoc result nnir-imap-search-arguments)
1558 (assoc nil nnir-imap-search-arguments))))
1559 (cons sym (format (cdr mapping) result)))
1560 (cons sym (read-string prompt)))))
1562 (defun nnir-run-query (query)
1563 "Invoke appropriate search engine function (see `nnir-engines').
1564 If some groups were process-marked, run the query for each of the groups
1565 and concat the results."
1566 (let ((q (car (read-from-string query))))
1567 (if gnus-group-marked
1568 (apply 'vconcat
1569 (mapcar (lambda (x)
1570 (let ((server (nnir-group-server x))
1571 search-func)
1572 (setq search-func (cadr
1573 (assoc
1574 (nnir-read-server-parm 'nnir-search-engine server) nnir-engines)))
1575 (if search-func
1576 (funcall search-func q server x)
1577 nil)))
1578 gnus-group-marked)
1580 (apply 'vconcat
1581 (mapcar (lambda (x)
1582 (if (and (equal (cadr x) 'ok) (not (equal (cadar x) "-ephemeral")))
1583 (let ((server (format "%s:%s" (caar x) (cadar x)))
1584 search-func)
1585 (setq search-func (cadr
1586 (assoc
1587 (nnir-read-server-parm 'nnir-search-engine server) nnir-engines)))
1588 (if search-func
1589 (funcall search-func q server nil)
1590 nil))
1591 nil))
1592 gnus-opened-servers)
1596 (defun nnir-read-server-parm (key server)
1597 "Returns the parameter value of for the given server, where server is of
1598 form 'backend:name'."
1599 (let ((method (gnus-server-to-method server)))
1600 (cond ((and method (assq key (cddr method)))
1601 (nth 1 (assq key (cddr method))))
1602 ((and nnir-mail-backend
1603 (gnus-server-equal method nnir-mail-backend))
1604 (symbol-value key))
1605 (t nil))))
1606 ;; (if method
1607 ;; (if (assq key (cddr method))
1608 ;; (nth 1 (assq key (cddr method)))
1609 ;; (symbol-value key))
1610 ;; (symbol-value key))
1611 ;; ))
1613 (defun nnir-group-full-name (shortname server)
1614 "For the given group name, return a full Gnus group name.
1615 The Gnus backend/server information is added."
1616 (gnus-group-prefixed-name shortname (gnus-server-to-method server)))
1618 (defun nnir-possibly-change-server (server)
1619 (unless (and server (nnir-server-opened server))
1620 (nnir-open-server server)))
1623 ;; Data type article list.
1625 (defun nnir-artlist-length (artlist)
1626 "Returns number of articles in artlist."
1627 (length artlist))
1629 (defun nnir-artlist-article (artlist n)
1630 "Returns from ARTLIST the Nth artitem (counting starting at 1)."
1631 (elt artlist (1- n)))
1633 (defun nnir-artitem-group (artitem)
1634 "Returns the group from the ARTITEM."
1635 (elt artitem 0))
1637 (defun nnir-artlist-artitem-group (artlist n)
1638 "Returns from ARTLIST the group of the Nth artitem (counting from 1)."
1639 (nnir-artitem-group (nnir-artlist-article artlist n)))
1641 (defun nnir-artitem-number (artitem)
1642 "Returns the number from the ARTITEM."
1643 (elt artitem 1))
1645 (defun nnir-artlist-artitem-number (artlist n)
1646 "Returns from ARTLIST the number of the Nth artitem (counting from 1)."
1647 (nnir-artitem-number (nnir-artlist-article artlist n)))
1649 (defun nnir-artitem-rsv (artitem)
1650 "Returns the Retrieval Status Value (RSV, score) from the ARTITEM."
1651 (elt artitem 2))
1653 (defun nnir-artlist-artitem-rsv (artlist n)
1654 "Returns from ARTLIST the Retrieval Status Value of the Nth artitem
1655 \(counting from 1)."
1656 (nnir-artitem-rsv (nnir-artlist-article artlist n)))
1658 ;; unused?
1659 (defun nnir-artlist-groups (artlist)
1660 "Returns a list of all groups in the given ARTLIST."
1661 (let ((res nil)
1662 (with-dups nil))
1663 ;; from each artitem, extract group component
1664 (setq with-dups (mapcar 'nnir-artitem-group artlist))
1665 ;; remove duplicates from above
1666 (mapc (function (lambda (x) (add-to-list 'res x)))
1667 with-dups)
1668 res))
1671 ;; The end.
1672 (provide 'nnir)
1674 ;; arch-tag: 9b3fecf8-4397-4bbb-bf3c-6ac3cbbc6664
1675 ;;; nnir.el ends here