Merge changes made in Gnus trunk.
[emacs/old-mirror.git] / lisp / gnus / nnir.el
blobd35e6560e714bbe03f4c5f969ab1031eb95483e9
1 ;;; nnir.el --- search mail with various search engines -*- coding: iso-8859-1 -*-
3 ;; Copyright (C) 1998-2011 Free Software Foundation, Inc.
5 ;; Author: Kai Großjohann <grossjohann@ls6.cs.uni-dortmund.de>
6 ;; Swish-e and Swish++ backends by:
7 ;; Christoph Conrad <christoph.conrad@gmx.de>.
8 ;; IMAP backend by: Simon Josefsson <jas@pdc.kth.se>.
9 ;; IMAP search by: Torsten Hilbrich <torsten.hilbrich <at> gmx.net>
10 ;; IMAP search improved by Daniel Pittman <daniel@rimspace.net>.
11 ;; nnmaildir support for Swish++ and Namazu backends by:
12 ;; Justus Piater <Justus <at> Piater.name>
13 ;; Keywords: news mail searching ir
15 ;; This file is part of GNU Emacs.
17 ;; GNU Emacs is free software: you can redistribute it and/or modify
18 ;; it under the terms of the GNU General Public License as published by
19 ;; the Free Software Foundation, either version 3 of the License, or
20 ;; (at your option) any later version.
22 ;; GNU Emacs is distributed in the hope that it will be useful,
23 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
24 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 ;; GNU General Public License for more details.
27 ;; You should have received a copy of the GNU General Public License
28 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
30 ;;; Commentary:
32 ;; TODO: Documentation in the Gnus manual
34 ;; Where in the existing gnus manual would this fit best?
36 ;; What does it do? Well, it allows you to search your mail using
37 ;; some search engine (imap, namazu, swish-e, gmane and others -- see
38 ;; later) by typing `G G' in the Group buffer. You will then get a
39 ;; buffer which shows all articles matching the query, sorted by
40 ;; Retrieval Status Value (score).
42 ;; When looking at the retrieval result (in the Summary buffer) you
43 ;; can type `A W' (aka M-x gnus-warp-to-article RET) on an article. You
44 ;; will be warped into the group this article came from. Typing `A T'
45 ;; (aka M-x gnus-summary-refer-thread RET) will warp to the group and
46 ;; also show the thread this article is part of.
48 ;; The Lisp setup may involve setting a few variables and setting up the
49 ;; search engine. You can define the variables in the server definition
50 ;; like this :
51 ;; (setq gnus-secondary-select-methods '(
52 ;; (nnimap "" (nnimap-address "localhost")
53 ;; (nnir-search-engine namazu)
54 ;; )))
55 ;; The main variable to set is `nnir-search-engine'. Choose one of
56 ;; the engines listed in `nnir-engines'. (Actually `nnir-engines' is
57 ;; an alist, type `C-h v nnir-engines RET' for more information; this
58 ;; includes examples for setting `nnir-search-engine', too.)
60 ;; If you use one of the local indices (namazu, find-grep, swish) you
61 ;; must also set up a search engine backend.
63 ;; 1. Namazu
65 ;; The Namazu backend requires you to have one directory containing all
66 ;; index files, this is controlled by the `nnir-namazu-index-directory'
67 ;; variable. To function the `nnir-namazu-remove-prefix' variable must
68 ;; also be correct, see the documentation for `nnir-namazu-remove-prefix'
69 ;; above.
71 ;; It is particularly important not to pass any any switches to namazu
72 ;; that will change the output format. Good switches to use include
73 ;; `--sort', `--ascending', `--early' and `--late'. Refer to the Namazu
74 ;; documentation for further information on valid switches.
76 ;; To index my mail with the `mknmz' program I use the following
77 ;; configuration file:
79 ;; ,----
80 ;; | package conf; # Don't remove this line!
81 ;; |
82 ;; | # Paths which will not be indexed. Don't use `^' or `$' anchors.
83 ;; | $EXCLUDE_PATH = "spam|sent";
84 ;; |
85 ;; | # Header fields which should be searchable. case-insensitive
86 ;; | $REMAIN_HEADER = "from|date|message-id|subject";
87 ;; |
88 ;; | # Searchable fields. case-insensitive
89 ;; | $SEARCH_FIELD = "from|date|message-id|subject";
90 ;; |
91 ;; | # The max length of a word.
92 ;; | $WORD_LENG_MAX = 128;
93 ;; |
94 ;; | # The max length of a field.
95 ;; | $MAX_FIELD_LENGTH = 256;
96 ;; `----
98 ;; My mail is stored in the directories ~/Mail/mail/, ~/Mail/lists/ and
99 ;; ~/Mail/archive/, so to index them I go to the directory set in
100 ;; `nnir-namazu-index-directory' and issue the following command.
102 ;; mknmz --mailnews ~/Mail/archive/ ~/Mail/mail/ ~/Mail/lists/
104 ;; For maximum searching efficiency I have a cron job set to run this
105 ;; command every four hours.
107 ;; 2. find-grep
109 ;; The find-grep engine simply runs find(1) to locate eligible
110 ;; articles and searches them with grep(1). This, of course, is much
111 ;; slower than using a proper search engine but OTOH doesn't require
112 ;; maintenance of an index and is still faster than using any built-in
113 ;; means for searching. The method specification of the server to
114 ;; search must include a directory for this engine to work (E.g.,
115 ;; `nnml-directory'). The tools must be POSIX compliant. GNU Find
116 ;; prior to version 4.2.12 (4.2.26 on Linux due to incorrect ARG_MAX
117 ;; handling) does not work.
118 ;; ,----
119 ;; | ;; find-grep configuration for searching the Gnus Cache
120 ;; |
121 ;; | (nnml "cache"
122 ;; | (nnml-get-new-mail nil)
123 ;; | (nnir-search-engine find-grep)
124 ;; | (nnml-directory "~/News/cache/")
125 ;; | (nnml-active-file "~/News/cache/active"))
126 ;; `----
128 ;; Developer information:
130 ;; I have tried to make the code expandable. Basically, it is divided
131 ;; into two layers. The upper layer is somewhat like the `nnvirtual'
132 ;; backend: given a specification of what articles to show from
133 ;; another backend, it creates a group containing exactly those
134 ;; articles. The lower layer issues a query to a search engine and
135 ;; produces such a specification of what articles to show from the
136 ;; other backend.
138 ;; The interface between the two layers consists of the single
139 ;; function `nnir-run-query', which just selects the appropriate
140 ;; function for the search engine one is using. The input to
141 ;; `nnir-run-query' is a string, representing the query as input by
142 ;; the user. The output of `nnir-run-query' is supposed to be a
143 ;; vector, each element of which should in turn be a three-element
144 ;; vector. The first element should be full group name of the article,
145 ;; the second element should be the article number, and the third
146 ;; element should be the Retrieval Status Value (RSV) as returned from
147 ;; the search engine. An RSV is the score assigned to the document by
148 ;; the search engine. For Boolean search engines, the
149 ;; RSV is always 1000 (or 1 or 100, or whatever you like).
151 ;; The sorting order of the articles in the summary buffer created by
152 ;; nnir is based on the order of the articles in the above mentioned
153 ;; vector, so that's where you can do the sorting you'd like. Maybe
154 ;; it would be nice to have a way of displaying the search result
155 ;; sorted differently?
157 ;; So what do you need to do when you want to add another search
158 ;; engine? You write a function that executes the query. Temporary
159 ;; data from the search engine can be put in `nnir-tmp-buffer'. This
160 ;; function should return the list of articles as a vector, as
161 ;; described above. Then, you need to register this backend in
162 ;; `nnir-engines'. Then, users can choose the backend by setting
163 ;; `nnir-search-engine' as a server variable.
165 ;;; Code:
167 ;;; Setup:
169 ;; For Emacs <22.2 and XEmacs.
170 (eval-and-compile
171 (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
173 (require 'nnoo)
174 (require 'gnus-group)
175 (require 'message)
176 (require 'gnus-util)
177 (eval-when-compile
178 (require 'cl))
180 ;;; Internal Variables:
182 (defvar nnir-current-query nil
183 "Internal: stores current query (= group name).")
185 (defvar nnir-current-server nil
186 "Internal: stores current server (does it ever change?).")
188 (defvar nnir-current-group-marked nil
189 "Internal: stores current list of process-marked groups.")
191 (defvar nnir-artlist nil
192 "Internal: stores search result.")
194 (defvar nnir-tmp-buffer " *nnir*"
195 "Internal: temporary buffer.")
197 (defvar nnir-search-history ()
198 "Internal: the history for querying search options in nnir")
200 (defvar nnir-extra-parms nil
201 "Internal: stores request for extra search parms")
203 ;; Imap variables
205 (defvar nnir-imap-search-arguments
206 '(("whole message" . "TEXT")
207 ("subject" . "SUBJECT")
208 ("to" . "TO")
209 ("from" . "FROM")
210 ("body" . "BODY")
211 ("imap" . ""))
212 "Mapping from user readable keys to IMAP search items for use in nnir")
214 (defvar nnir-imap-search-other "HEADER %S"
215 "The IMAP search item to use for anything other than
216 `nnir-imap-search-arguments'. By default this is the name of an
217 email header field")
219 (defvar nnir-imap-search-argument-history ()
220 "The history for querying search options in nnir")
222 ;;; Helper macros
224 ;; Data type article list.
226 (defmacro nnir-artlist-length (artlist)
227 "Returns number of articles in artlist."
228 `(length ,artlist))
230 (defmacro nnir-artlist-article (artlist n)
231 "Returns from ARTLIST the Nth artitem (counting starting at 1)."
232 `(when (> ,n 0)
233 (elt ,artlist (1- ,n))))
235 (defmacro nnir-artitem-group (artitem)
236 "Returns the group from the ARTITEM."
237 `(elt ,artitem 0))
239 (defmacro nnir-artitem-number (artitem)
240 "Returns the number from the ARTITEM."
241 `(elt ,artitem 1))
243 (defmacro nnir-artitem-rsv (artitem)
244 "Returns the Retrieval Status Value (RSV, score) from the ARTITEM."
245 `(elt ,artitem 2))
247 (defmacro nnir-article-group (article)
248 "Returns the group for ARTICLE"
249 `(nnir-artitem-group (nnir-artlist-article nnir-artlist ,article)))
251 (defmacro nnir-article-number (article)
252 "Returns the number for ARTICLE"
253 `(nnir-artitem-number (nnir-artlist-article nnir-artlist ,article)))
255 (defmacro nnir-article-rsv (article)
256 "Returns the rsv for ARTICLE"
257 `(nnir-artitem-rsv (nnir-artlist-article nnir-artlist ,article)))
259 (defsubst nnir-article-ids (article)
260 "Returns the pair `(nnir id . real id)' of ARTICLE"
261 (cons article (nnir-article-number article)))
263 (defmacro nnir-categorize (sequence keyfunc &optional valuefunc)
264 "Sorts a sequence into categories and returns a list of the form
265 `((key1 (element11 element12)) (key2 (element21 element22))'.
266 The category key for a member of the sequence is obtained
267 as `(keyfunc member)' and the corresponding element is just
268 `member'. If `valuefunc' is non-nil, the element of the list
269 is `(valuefunc member)'."
270 `(unless (null ,sequence)
271 (let (value)
272 (mapc
273 (lambda (member)
274 (let ((y (,keyfunc member))
275 (x ,(if valuefunc
276 `(,valuefunc member)
277 'member)))
278 (if (assoc y value)
279 (push x (cadr (assoc y value)))
280 (push (list y (list x)) value))))
281 ,sequence)
282 value)))
284 ;;; Finish setup:
286 (require 'gnus-sum)
288 (eval-when-compile
289 (autoload 'nnimap-buffer "nnimap")
290 (autoload 'nnimap-command "nnimap")
291 (autoload 'nnimap-possibly-change-group "nnimap")
292 (autoload 'nnimap-make-thread-query "nnimap")
293 (autoload 'gnus-registry-action "gnus-registry")
294 (defvar gnus-registry-install))
297 (nnoo-declare nnir)
298 (nnoo-define-basics nnir)
300 (defvoo nnir-address nil
301 "The address of the nnir server.")
303 (gnus-declare-backend "nnir" 'mail)
306 ;;; User Customizable Variables:
308 (defgroup nnir nil
309 "Search groups in Gnus with assorted seach engines."
310 :group 'gnus)
312 (defcustom nnir-ignored-newsgroups ""
313 "*A regexp to match newsgroups in the active file that should
314 be skipped when searching."
315 :type '(regexp)
316 :group 'nnir)
318 (defcustom nnir-summary-line-format nil
319 "*The format specification of the lines in an nnir summary buffer.
321 All the items from `gnus-summary-line-format' are available, along
322 with three items unique to nnir summary buffers:
324 %Z Search retrieval score value (integer)
325 %G Article original full group name (string)
326 %g Article original short group name (string)
328 If nil this will use `gnus-summary-line-format'."
329 :type '(string)
330 :group 'nnir)
332 (defcustom nnir-retrieve-headers-override-function nil
333 "*If non-nil, a function that accepts an article list and group
334 and populates the `nntp-server-buffer' with the retrieved
335 headers. Must return either 'nov or 'headers indicating the
336 retrieved header format.
338 If this variable is nil, or if the provided function returns nil for a search
339 result, `gnus-retrieve-headers' will be called instead."
340 :type '(function)
341 :group 'nnir)
343 (defcustom nnir-imap-default-search-key "whole message"
344 "*The default IMAP search key for an nnir search. Must be one of
345 the keys in `nnir-imap-search-arguments'. To use raw imap queries
346 by default set this to \"Imap\"."
347 :type `(choice ,@(mapcar (lambda (elem) (list 'const (car elem)))
348 nnir-imap-search-arguments))
349 :group 'nnir)
351 (defcustom nnir-swish++-configuration-file
352 (expand-file-name "~/Mail/swish++.conf")
353 "*Configuration file for swish++."
354 :type '(file)
355 :group 'nnir)
357 (defcustom nnir-swish++-program "search"
358 "*Name of swish++ search executable."
359 :type '(string)
360 :group 'nnir)
362 (defcustom nnir-swish++-additional-switches '()
363 "*A list of strings, to be given as additional arguments to swish++.
365 Note that this should be a list. Ie, do NOT use the following:
366 (setq nnir-swish++-additional-switches \"-i -w\") ; wrong
367 Instead, use this:
368 (setq nnir-swish++-additional-switches '(\"-i\" \"-w\"))"
369 :type '(repeat (string))
370 :group 'nnir)
372 (defcustom nnir-swish++-remove-prefix (concat (getenv "HOME") "/Mail/")
373 "*The prefix to remove from each file name returned by swish++
374 in order to get a group name (albeit with / instead of .). This is a
375 regular expression.
377 This variable is very similar to `nnir-namazu-remove-prefix', except
378 that it is for swish++, not Namazu."
379 :type '(regexp)
380 :group 'nnir)
382 ;; Swish-E.
383 ;; URL: http://swish-e.org/
384 ;; Variables `nnir-swish-e-index-file', `nnir-swish-e-program' and
385 ;; `nnir-swish-e-additional-switches'
387 (make-obsolete-variable 'nnir-swish-e-index-file
388 'nnir-swish-e-index-files "Emacs 23.1")
389 (defcustom nnir-swish-e-index-file
390 (expand-file-name "~/Mail/index.swish-e")
391 "*Index file for swish-e.
392 This could be a server parameter.
393 It is never consulted once `nnir-swish-e-index-files', which should be
394 used instead, has been customized."
395 :type '(file)
396 :group 'nnir)
398 (defcustom nnir-swish-e-index-files
399 (list nnir-swish-e-index-file)
400 "*List of index files for swish-e.
401 This could be a server parameter."
402 :type '(repeat (file))
403 :group 'nnir)
405 (defcustom nnir-swish-e-program "swish-e"
406 "*Name of swish-e search executable.
407 This cannot be a server parameter."
408 :type '(string)
409 :group 'nnir)
411 (defcustom nnir-swish-e-additional-switches '()
412 "*A list of strings, to be given as additional arguments to swish-e.
414 Note that this should be a list. Ie, do NOT use the following:
415 (setq nnir-swish-e-additional-switches \"-i -w\") ; wrong
416 Instead, use this:
417 (setq nnir-swish-e-additional-switches '(\"-i\" \"-w\"))
419 This could be a server parameter."
420 :type '(repeat (string))
421 :group 'nnir)
423 (defcustom nnir-swish-e-remove-prefix (concat (getenv "HOME") "/Mail/")
424 "*The prefix to remove from each file name returned by swish-e
425 in order to get a group name (albeit with / instead of .). This is a
426 regular expression.
428 This variable is very similar to `nnir-namazu-remove-prefix', except
429 that it is for swish-e, not Namazu.
431 This could be a server parameter."
432 :type '(regexp)
433 :group 'nnir)
435 ;; HyREX engine, see <URL:http://ls6-www.cs.uni-dortmund.de/>
437 (defcustom nnir-hyrex-program "nnir-search"
438 "*Name of the nnir-search executable."
439 :type '(string)
440 :group 'nnir)
442 (defcustom nnir-hyrex-additional-switches '()
443 "*A list of strings, to be given as additional arguments for nnir-search.
444 Note that this should be a list. Ie, do NOT use the following:
445 (setq nnir-hyrex-additional-switches \"-ddl ddl.xml -c nnir\") ; wrong !
446 Instead, use this:
447 (setq nnir-hyrex-additional-switches '(\"-ddl\" \"ddl.xml\" \"-c\" \"nnir\"))"
448 :type '(repeat (string))
449 :group 'nnir)
451 (defcustom nnir-hyrex-index-directory (getenv "HOME")
452 "*Index directory for HyREX."
453 :type '(directory)
454 :group 'nnir)
456 (defcustom nnir-hyrex-remove-prefix (concat (getenv "HOME") "/Mail/")
457 "*The prefix to remove from each file name returned by HyREX
458 in order to get a group name (albeit with / instead of .).
460 For example, suppose that HyREX returns file names such as
461 \"/home/john/Mail/mail/misc/42\". For this example, use the following
462 setting: (setq nnir-hyrex-remove-prefix \"/home/john/Mail/\")
463 Note the trailing slash. Removing this prefix gives \"mail/misc/42\".
464 `nnir' knows to remove the \"/42\" and to replace \"/\" with \".\" to
465 arrive at the correct group name, \"mail.misc\"."
466 :type '(directory)
467 :group 'nnir)
469 ;; Namazu engine, see <URL:http://www.namazu.org/>
471 (defcustom nnir-namazu-program "namazu"
472 "*Name of Namazu search executable."
473 :type '(string)
474 :group 'nnir)
476 (defcustom nnir-namazu-index-directory (expand-file-name "~/Mail/namazu/")
477 "*Index directory for Namazu."
478 :type '(directory)
479 :group 'nnir)
481 (defcustom nnir-namazu-additional-switches '()
482 "*A list of strings, to be given as additional arguments to namazu.
483 The switches `-q', `-a', and `-s' are always used, very few other switches
484 make any sense in this context.
486 Note that this should be a list. Ie, do NOT use the following:
487 (setq nnir-namazu-additional-switches \"-i -w\") ; wrong
488 Instead, use this:
489 (setq nnir-namazu-additional-switches '(\"-i\" \"-w\"))"
490 :type '(repeat (string))
491 :group 'nnir)
493 (defcustom nnir-namazu-remove-prefix (concat (getenv "HOME") "/Mail/")
494 "*The prefix to remove from each file name returned by Namazu
495 in order to get a group name (albeit with / instead of .).
497 For example, suppose that Namazu returns file names such as
498 \"/home/john/Mail/mail/misc/42\". For this example, use the following
499 setting: (setq nnir-namazu-remove-prefix \"/home/john/Mail/\")
500 Note the trailing slash. Removing this prefix gives \"mail/misc/42\".
501 `nnir' knows to remove the \"/42\" and to replace \"/\" with \".\" to
502 arrive at the correct group name, \"mail.misc\"."
503 :type '(directory)
504 :group 'nnir)
506 (defcustom nnir-notmuch-program "notmuch"
507 "*Name of notmuch search executable."
508 :type '(string)
509 :group 'nnir)
511 (defcustom nnir-notmuch-additional-switches '()
512 "*A list of strings, to be given as additional arguments to notmuch.
514 Note that this should be a list. Ie, do NOT use the following:
515 (setq nnir-notmuch-additional-switches \"-i -w\") ; wrong
516 Instead, use this:
517 (setq nnir-notmuch-additional-switches '(\"-i\" \"-w\"))"
518 :type '(repeat (string))
519 :group 'nnir)
521 (defcustom nnir-notmuch-remove-prefix (concat (getenv "HOME") "/Mail/")
522 "*The prefix to remove from each file name returned by notmuch
523 in order to get a group name (albeit with / instead of .). This is a
524 regular expression.
526 This variable is very similar to `nnir-namazu-remove-prefix', except
527 that it is for notmuch, not Namazu."
528 :type '(regexp)
529 :group 'nnir)
531 ;;; Developer Extension Variable:
533 (defvar nnir-engines
534 `((imap nnir-run-imap
535 ((criteria
536 "Imap Search in" ; Prompt
537 ,(mapcar 'car nnir-imap-search-arguments) ; alist for completing
538 nil ; allow any user input
539 nil ; initial value
540 nnir-imap-search-argument-history ; the history to use
541 ,nnir-imap-default-search-key ; default
543 (gmane nnir-run-gmane
544 ((author . "Gmane Author: ")))
545 (swish++ nnir-run-swish++
546 ((group . "Swish++ Group spec: ")))
547 (swish-e nnir-run-swish-e
548 ((group . "Swish-e Group spec: ")))
549 (namazu nnir-run-namazu
551 (notmuch nnir-run-notmuch
553 (hyrex nnir-run-hyrex
554 ((group . "Hyrex Group spec: ")))
555 (find-grep nnir-run-find-grep
556 ((grep-options . "Grep options: "))))
557 "Alist of supported search engines.
558 Each element in the alist is a three-element list (ENGINE FUNCTION ARGS).
559 ENGINE is a symbol designating the searching engine. FUNCTION is also
560 a symbol, giving the function that does the search. The third element
561 ARGS is a list of cons pairs (PARAM . PROMPT). When issuing a query,
562 the FUNCTION will issue a query for each of the PARAMs, using PROMPT.
564 The value of `nnir-search-engine' must be one of the ENGINE symbols.
565 For example, for searching a server using namazu include
566 (nnir-search-engine namazu)
567 in the server definition. Note that you have to set additional
568 variables for most backends. For example, the `namazu' backend
569 needs the variables `nnir-namazu-program',
570 `nnir-namazu-index-directory' and `nnir-namazu-remove-prefix'.
572 Add an entry here when adding a new search engine.")
574 (defcustom nnir-method-default-engines
575 '((nnimap . imap)
576 (nntp . gmane))
577 "*Alist of default search engines keyed by server method."
578 :type `(repeat (cons (choice (const nnimap) (const nttp) (const nnspool)
579 (const nneething) (const nndir) (const nnmbox)
580 (const nnml) (const nnmh) (const nndraft)
581 (const nnfolder) (const nnmaildir))
582 (choice
583 ,@(mapcar (lambda (elem) (list 'const (car elem)))
584 nnir-engines))))
585 :group 'nnir)
587 ;; Gnus glue.
589 (defun gnus-group-make-nnir-group (nnir-extra-parms &optional parms)
590 "Create an nnir group. Asks for query."
591 (interactive "P")
592 (setq nnir-current-query nil
593 nnir-current-server nil
594 nnir-current-group-marked nil
595 nnir-artlist nil)
596 (let* ((query (unless parms (read-string "Query: " nil 'nnir-search-history)))
597 (parms (or parms (list (cons 'query query))))
598 (srv (or (cdr (assq 'server parms)) (gnus-server-server-name) "nnir")))
599 (add-to-list 'parms (cons 'unique-id (message-unique-id)) t)
600 (gnus-group-read-ephemeral-group
601 (concat "nnir:" (prin1-to-string parms)) (list 'nnir srv) t
602 (cons (current-buffer) gnus-current-window-configuration)
603 nil)))
606 ;; Gnus backend interface functions.
608 (deffoo nnir-open-server (server &optional definitions)
609 ;; Just set the server variables appropriately.
610 (add-hook 'gnus-summary-mode-hook 'nnir-mode)
611 (nnoo-change-server 'nnir server definitions))
613 (deffoo nnir-request-group (group &optional server fast info)
614 "GROUP is the query string."
615 (nnir-possibly-change-server server)
616 ;; Check for cache and return that if appropriate.
617 (if (and (equal group nnir-current-query)
618 (equal gnus-group-marked nnir-current-group-marked)
619 (or (null server)
620 (equal server nnir-current-server)))
621 nnir-artlist
622 ;; Cache miss.
623 (setq nnir-artlist (nnir-run-query group)))
624 (with-current-buffer nntp-server-buffer
625 (setq nnir-current-query group)
626 (when server (setq nnir-current-server server))
627 (setq nnir-current-group-marked gnus-group-marked)
628 (if (zerop (length nnir-artlist))
629 (nnheader-report 'nnir "Search produced empty results.")
630 ;; Remember data for cache.
631 (nnheader-insert "211 %d %d %d %s\n"
632 (nnir-artlist-length nnir-artlist) ; total #
633 1 ; first #
634 (nnir-artlist-length nnir-artlist) ; last #
635 group)))) ; group name
637 (deffoo nnir-retrieve-headers (articles &optional group server fetch-old)
638 (with-current-buffer nntp-server-buffer
639 (let ((gnus-inhibit-demon t)
640 (articles-by-group (nnir-categorize
641 articles nnir-article-group nnir-article-ids))
642 headers)
643 (while (not (null articles-by-group))
644 (let* ((group-articles (pop articles-by-group))
645 (artgroup (car group-articles))
646 (articleids (cadr group-articles))
647 (artlist (sort (mapcar 'cdr articleids) '<))
648 (server (gnus-group-server artgroup))
649 (gnus-override-method (gnus-server-to-method server))
650 parsefunc)
651 ;; (or (numberp art)
652 ;; (nnheader-report
653 ;; 'nnir
654 ;; "nnir-retrieve-headers doesn't grok message ids: %s"
655 ;; art))
656 (nnir-possibly-change-server server)
657 ;; is this needed?
658 (erase-buffer)
659 (case (setq gnus-headers-retrieved-by
661 (and
662 nnir-retrieve-headers-override-function
663 (funcall nnir-retrieve-headers-override-function
664 artlist artgroup))
665 (gnus-retrieve-headers artlist artgroup nil)))
666 (nov
667 (setq parsefunc 'nnheader-parse-nov))
668 (headers
669 (setq parsefunc 'nnheader-parse-head))
670 (t (error "Unknown header type %s while requesting articles \
671 of group %s" gnus-headers-retrieved-by artgroup)))
672 (goto-char (point-min))
673 (while (not (eobp))
674 (let* ((novitem (funcall parsefunc))
675 (artno (mail-header-number novitem))
676 (art (car (rassq artno articleids))))
677 (when art
678 (mail-header-set-number novitem art)
679 (push novitem headers))
680 (forward-line 1)))))
681 (setq headers
682 (sort headers
683 (lambda (x y)
684 (< (mail-header-number x) (mail-header-number y)))))
685 (erase-buffer)
686 (mapc 'nnheader-insert-nov headers)
687 'nov)))
689 (deffoo nnir-request-article (article &optional group server to-buffer)
690 (if (and (stringp article)
691 (not (eq 'nnimap (car (gnus-server-to-method server)))))
692 (nnheader-report
693 'nnir
694 "nnir-request-article only groks message ids for nnimap servers: %s"
695 server)
696 (save-excursion
697 (let ((article article)
698 query)
699 (when (stringp article)
700 (setq gnus-override-method (gnus-server-to-method server))
701 (setq query
702 (list
703 (cons 'query (format "HEADER Message-ID %s" article))
704 (cons 'unique-id article)
705 (cons 'criteria "")
706 (cons 'shortcut t)))
707 (unless (and (equal query nnir-current-query)
708 (equal server nnir-current-server))
709 (setq nnir-artlist (nnir-run-imap query server))
710 (setq nnir-current-query query)
711 (setq nnir-current-server server))
712 (setq article 1))
713 (unless (zerop (length nnir-artlist))
714 (let ((artfullgroup (nnir-article-group article))
715 (artno (nnir-article-number article)))
716 (message "Requesting article %d from group %s"
717 artno artfullgroup)
718 (if to-buffer
719 (with-current-buffer to-buffer
720 (let ((gnus-article-decode-hook nil))
721 (gnus-request-article-this-buffer artno artfullgroup)))
722 (gnus-request-article artno artfullgroup))
723 (cons artfullgroup artno)))))))
725 (deffoo nnir-request-move-article (article group server accept-form
726 &optional last internal-move-group)
727 (let* ((artfullgroup (nnir-article-group article))
728 (artno (nnir-article-number article))
729 (to-newsgroup (nth 1 accept-form))
730 (to-method (gnus-find-method-for-group to-newsgroup))
731 (from-method (gnus-find-method-for-group artfullgroup))
732 (move-is-internal (gnus-server-equal from-method to-method)))
733 (unless (gnus-check-backend-function
734 'request-move-article artfullgroup)
735 (error "The group %s does not support article moving" artfullgroup))
736 (gnus-request-move-article
737 artno
738 artfullgroup
739 (nth 1 from-method)
740 accept-form
741 last
742 (and move-is-internal
743 to-newsgroup ; Not respooling
744 (gnus-group-real-name to-newsgroup)))))
746 (deffoo nnir-request-expire-articles (articles group &optional server force)
747 (if force
748 (let ((articles-by-group (nnir-categorize
749 articles nnir-article-group nnir-article-ids))
750 not-deleted)
751 (while (not (null articles-by-group))
752 (let* ((group-articles (pop articles-by-group))
753 (artgroup (car group-articles))
754 (articleids (cadr group-articles))
755 (artlist (sort (mapcar 'cdr articleids) '<)))
756 (unless (gnus-check-backend-function 'request-expire-articles
757 artgroup)
758 (error "The group %s does not support article deletion" artgroup))
759 (unless (gnus-check-server (gnus-find-method-for-group artgroup))
760 (error "Couldn't open server for group %s" artgroup))
761 (push (gnus-request-expire-articles
762 artlist artgroup force)
763 not-deleted)))
764 (sort (delq nil not-deleted) '<))
765 articles))
767 (deffoo nnir-warp-to-article ()
768 (let* ((cur (if (> (gnus-summary-article-number) 0)
769 (gnus-summary-article-number)
770 (error "This is not a real article")))
771 (backend-article-group (nnir-article-group cur))
772 (backend-article-number (nnir-article-number cur))
773 (quit-config (gnus-ephemeral-group-p gnus-newsgroup-name)))
774 ;; first exit from the nnir summary buffer.
775 (gnus-summary-exit)
776 ;; and if the nnir summary buffer in turn came from another
777 ;; summary buffer we have to clean that summary up too.
778 (when (eq (cdr quit-config) 'summary)
779 (gnus-summary-exit))
780 (gnus-summary-read-group-1 backend-article-group t t nil
781 nil (list backend-article-number))))
783 (nnoo-define-skeleton nnir)
786 (defmacro nnir-add-result (dirnam artno score prefix server artlist)
787 "Ask `nnir-compose-result' to construct a result vector,
788 and if it is non-nil, add it to artlist."
789 `(let ((result (nnir-compose-result ,dirnam ,artno ,score ,prefix ,server)))
790 (when (not (null result))
791 (push result ,artlist))))
793 (autoload 'nnmaildir-base-name-to-article-number "nnmaildir")
795 ;; Helper function currently used by the Swish++ and Namazu backends;
796 ;; perhaps useful for other backends as well
797 (defun nnir-compose-result (dirnam article score prefix server)
798 "Extract the group from dirnam, and create a result vector
799 ready to be added to the list of search results."
801 ;; remove nnir-*-remove-prefix from beginning of dirnam filename
802 (when (string-match (concat "^" prefix) dirnam)
803 (setq dirnam (replace-match "" t t dirnam)))
805 (when (file-readable-p (concat prefix dirnam article))
806 ;; remove trailing slash and, for nnmaildir, cur/new/tmp
807 (setq dirnam
808 (substring dirnam 0
809 (if (string= (gnus-group-server server) "nnmaildir")
810 -5 -1)))
812 ;; Set group to dirnam without any leading dots or slashes,
813 ;; and with all subsequent slashes replaced by dots
814 (let ((group (gnus-replace-in-string
815 (gnus-replace-in-string dirnam "^[./\\]" "" t)
816 "[/\\]" "." t)))
818 (vector (gnus-group-full-name group server)
819 (if (string= (gnus-group-server server) "nnmaildir")
820 (nnmaildir-base-name-to-article-number
821 (substring article 0 (string-match ":" article))
822 group nil)
823 (string-to-number article))
824 (string-to-number score)))))
826 ;;; Search Engine Interfaces:
828 ;; imap interface
829 (defun nnir-run-imap (query srv &optional groups)
830 "Run a search against an IMAP back-end server.
831 This uses a custom query language parser; see `nnir-imap-make-query' for
832 details on the language and supported extensions."
833 (save-excursion
834 (let ((qstring (cdr (assq 'query query)))
835 (server (cadr (gnus-server-to-method srv)))
836 (defs (caddr (gnus-server-to-method srv)))
837 (criteria (or (cdr (assq 'criteria query))
838 (cdr (assoc nnir-imap-default-search-key
839 nnir-imap-search-arguments))))
840 (gnus-inhibit-demon t)
841 (groups (or groups (nnir-get-active srv))))
842 (message "Opening server %s" server)
843 (apply
844 'vconcat
845 (catch 'found
846 (mapcar
847 (lambda (group)
848 (let (artlist)
849 (condition-case ()
850 (when (nnimap-possibly-change-group
851 (gnus-group-short-name group) server)
852 (with-current-buffer (nnimap-buffer)
853 (message "Searching %s..." group)
854 (let ((arts 0)
855 (result (nnimap-command "UID SEARCH %s"
856 (if (string= criteria "")
857 qstring
858 (nnir-imap-make-query
859 criteria qstring)))))
860 (mapc
861 (lambda (artnum)
862 (let ((artn (string-to-number artnum)))
863 (when (> artn 0)
864 (push (vector group artn 100)
865 artlist)
866 (when (assq 'shortcut query)
867 (throw 'found (list artlist)))
868 (setq arts (1+ arts)))))
869 (and (car result) (cdr (assoc "SEARCH" (cdr result)))))
870 (message "Searching %s... %d matches" group arts)))
871 (message "Searching %s...done" group))
872 (quit nil))
873 (nreverse artlist)))
874 groups))))))
876 (defun nnir-imap-make-query (criteria qstring)
877 "Parse the query string and criteria into an appropriate IMAP search
878 expression, returning the string query to make.
880 This implements a little language designed to return the expected results
881 to an arbitrary query string to the end user.
883 The search is always case-insensitive, as defined by RFC2060, and supports
884 the following features (inspired by the Google search input language):
886 Automatic \"and\" queries
887 If you specify multiple words then they will be treated as an \"and\"
888 expression intended to match all components.
890 Phrase searches
891 If you wrap your query in double-quotes then it will be treated as a
892 literal string.
894 Negative terms
895 If you precede a term with \"-\" then it will negate that.
897 \"OR\" queries
898 If you include an upper-case \"OR\" in your search it will cause the
899 term before it and the term after it to be treated as alternatives.
901 In future the following will be added to the language:
902 * support for date matches
903 * support for location of text matching within the query
904 * from/to/etc headers
905 * additional search terms
906 * flag based searching
907 * anything else that the RFC supports, basically."
908 ;; Walk through the query and turn it into an IMAP query string.
909 (nnir-imap-query-to-imap criteria (nnir-imap-parse-query qstring)))
912 (defun nnir-imap-query-to-imap (criteria query)
913 "Turn a s-expression format query into IMAP."
914 (mapconcat
915 ;; Turn the expressions into IMAP text
916 (lambda (item)
917 (nnir-imap-expr-to-imap criteria item))
918 ;; The query, already in s-expr format.
919 query
920 ;; Append a space between each expression
921 " "))
924 (defun nnir-imap-expr-to-imap (criteria expr)
925 "Convert EXPR into an IMAP search expression on CRITERIA"
926 ;; What sort of expression is this, eh?
927 (cond
928 ;; Simple string term
929 ((stringp expr)
930 (format "%s %S" criteria expr))
931 ;; Trivial term: and
932 ((eq expr 'and) nil)
933 ;; Composite term: or expression
934 ((eq (car-safe expr) 'or)
935 (format "OR %s %s"
936 (nnir-imap-expr-to-imap criteria (second expr))
937 (nnir-imap-expr-to-imap criteria (third expr))))
938 ;; Composite term: just the fax, mam
939 ((eq (car-safe expr) 'not)
940 (format "NOT (%s)" (nnir-imap-query-to-imap criteria (rest expr))))
941 ;; Composite term: just expand it all.
942 ((and (not (null expr)) (listp expr))
943 (format "(%s)" (nnir-imap-query-to-imap criteria expr)))
944 ;; Complex value, give up for now.
945 (t (error "Unhandled input: %S" expr))))
948 (defun nnir-imap-parse-query (string)
949 "Turn STRING into an s-expression based query based on the IMAP
950 query language as defined in `nnir-imap-make-query'.
952 This involves turning individual tokens into higher level terms
953 that the search language can then understand and use."
954 (with-temp-buffer
955 ;; Set up the parsing environment.
956 (insert string)
957 (goto-char (point-min))
958 ;; Now, collect the output terms and return them.
959 (let (out)
960 (while (not (nnir-imap-end-of-input))
961 (push (nnir-imap-next-expr) out))
962 (reverse out))))
965 (defun nnir-imap-next-expr (&optional count)
966 "Return the next expression from the current buffer."
967 (let ((term (nnir-imap-next-term count))
968 (next (nnir-imap-peek-symbol)))
969 ;; Are we looking at an 'or' expression?
970 (cond
971 ;; Handle 'expr or expr'
972 ((eq next 'or)
973 (list 'or term (nnir-imap-next-expr 2)))
974 ;; Anything else
975 (t term))))
978 (defun nnir-imap-next-term (&optional count)
979 "Return the next TERM from the current buffer."
980 (let ((term (nnir-imap-next-symbol count)))
981 ;; What sort of term is this?
982 (cond
983 ;; and -- just ignore it
984 ((eq term 'and) 'and)
985 ;; negated term
986 ((eq term 'not) (list 'not (nnir-imap-next-expr)))
987 ;; generic term
988 (t term))))
991 (defun nnir-imap-peek-symbol ()
992 "Return the next symbol from the current buffer, but don't consume it."
993 (save-excursion
994 (nnir-imap-next-symbol)))
996 (defun nnir-imap-next-symbol (&optional count)
997 "Return the next symbol from the current buffer, or nil if we are
998 at the end of the buffer. If supplied COUNT skips some symbols before
999 returning the one at the supplied position."
1000 (when (and (numberp count) (> count 1))
1001 (nnir-imap-next-symbol (1- count)))
1002 (let ((case-fold-search t))
1003 ;; end of input stream?
1004 (unless (nnir-imap-end-of-input)
1005 ;; No, return the next symbol from the stream.
1006 (cond
1007 ;; negated expression -- return it and advance one char.
1008 ((looking-at "-") (forward-char 1) 'not)
1009 ;; quoted string
1010 ((looking-at "\"") (nnir-imap-delimited-string "\""))
1011 ;; list expression -- we parse the content and return this as a list.
1012 ((looking-at "(")
1013 (nnir-imap-parse-query (nnir-imap-delimited-string ")")))
1014 ;; keyword input -- return a symbol version
1015 ((looking-at "\\band\\b") (forward-char 3) 'and)
1016 ((looking-at "\\bor\\b") (forward-char 2) 'or)
1017 ((looking-at "\\bnot\\b") (forward-char 3) 'not)
1018 ;; Simple, boring keyword
1019 (t (let ((start (point))
1020 (end (if (search-forward-regexp "[[:blank:]]" nil t)
1021 (prog1
1022 (match-beginning 0)
1023 ;; unskip if we hit a non-blank terminal character.
1024 (when (string-match "[^[:blank:]]" (match-string 0))
1025 (backward-char 1)))
1026 (goto-char (point-max)))))
1027 (buffer-substring start end)))))))
1029 (defun nnir-imap-delimited-string (delimiter)
1030 "Return a delimited string from the current buffer."
1031 (let ((start (point)) end)
1032 (forward-char 1) ; skip the first delimiter.
1033 (while (not end)
1034 (unless (search-forward delimiter nil t)
1035 (error "Unmatched delimited input with %s in query" delimiter))
1036 (let ((here (point)))
1037 (unless (equal (buffer-substring (- here 2) (- here 1)) "\\")
1038 (setq end (point)))))
1039 (buffer-substring (1+ start) (1- end))))
1041 (defun nnir-imap-end-of-input ()
1042 "Are we at the end of input?"
1043 (skip-chars-forward "[[:blank:]]")
1044 (looking-at "$"))
1047 ;; Swish++ interface.
1048 ;; -cc- Todo
1049 ;; Search by
1050 ;; - group
1051 ;; Sort by
1052 ;; - rank (default)
1053 ;; - article number
1054 ;; - file size
1055 ;; - group
1056 (defun nnir-run-swish++ (query server &optional group)
1057 "Run QUERY against swish++.
1058 Returns a vector of (group name, file name) pairs (also vectors,
1059 actually).
1061 Tested with swish++ 4.7 on GNU/Linux and with swish++ 5.0b2 on
1062 Windows NT 4.0."
1064 ;; (when group
1065 ;; (error "The swish++ backend cannot search specific groups"))
1067 (save-excursion
1068 (let ( (qstring (cdr (assq 'query query)))
1069 (groupspec (cdr (assq 'group query)))
1070 (prefix (nnir-read-server-parm 'nnir-swish++-remove-prefix server))
1071 artlist
1072 ;; nnml-use-compressed-files might be any string, but probably this
1073 ;; is sufficient. Note that we can't only use the value of
1074 ;; nnml-use-compressed-files because old articles might have been
1075 ;; saved with a different value.
1076 (article-pattern (if (string= (gnus-group-server server) "nnmaildir")
1077 ":[0-9]+"
1078 "^[0-9]+\\(\\.[a-z0-9]+\\)?$"))
1079 score artno dirnam filenam)
1081 (when (equal "" qstring)
1082 (error "swish++: You didn't enter anything"))
1084 (set-buffer (get-buffer-create nnir-tmp-buffer))
1085 (erase-buffer)
1087 (if groupspec
1088 (message "Doing swish++ query %s on %s..." qstring groupspec)
1089 (message "Doing swish++ query %s..." qstring))
1091 (let* ((cp-list `( ,nnir-swish++-program
1092 nil ; input from /dev/null
1093 t ; output
1094 nil ; don't redisplay
1095 "--config-file" ,(nnir-read-server-parm 'nnir-swish++-configuration-file server)
1096 ,@(nnir-read-server-parm 'nnir-swish++-additional-switches server)
1097 ,qstring ; the query, in swish++ format
1099 (exitstatus
1100 (progn
1101 (message "%s args: %s" nnir-swish++-program
1102 (mapconcat 'identity (cddddr cp-list) " ")) ;; ???
1103 (apply 'call-process cp-list))))
1104 (unless (or (null exitstatus)
1105 (zerop exitstatus))
1106 (nnheader-report 'nnir "Couldn't run swish++: %s" exitstatus)
1107 ;; swish++ failure reason is in this buffer, show it if
1108 ;; the user wants it.
1109 (when (> gnus-verbose 6)
1110 (display-buffer nnir-tmp-buffer))))
1112 ;; The results are output in the format of:
1113 ;; V 4.7 Linux
1114 ;; rank relative-path-name file-size file-title
1115 ;; V 5.0b2:
1116 ;; rank relative-path-name file-size topic??
1117 ;; where rank is an integer from 1 to 100.
1118 (goto-char (point-min))
1119 (while (re-search-forward
1120 "\\(^[0-9]+\\) \\([^ ]+\\) [0-9]+ \\(.*\\)$" nil t)
1121 (setq score (match-string 1)
1122 filenam (match-string 2)
1123 artno (file-name-nondirectory filenam)
1124 dirnam (file-name-directory filenam))
1126 ;; don't match directories
1127 (when (string-match article-pattern artno)
1128 (when (not (null dirnam))
1130 ;; maybe limit results to matching groups.
1131 (when (or (not groupspec)
1132 (string-match groupspec dirnam))
1133 (nnir-add-result dirnam artno score prefix server artlist)))))
1135 (message "Massaging swish++ output...done")
1137 ;; Sort by score
1138 (apply 'vector
1139 (sort artlist
1140 (function (lambda (x y)
1141 (> (nnir-artitem-rsv x)
1142 (nnir-artitem-rsv y)))))))))
1144 ;; Swish-E interface.
1145 (defun nnir-run-swish-e (query server &optional group)
1146 "Run given query against swish-e.
1147 Returns a vector of (group name, file name) pairs (also vectors,
1148 actually).
1150 Tested with swish-e-2.0.1 on Windows NT 4.0."
1152 ;; swish-e crashes with empty parameter to "-w" on commandline...
1153 ;; (when group
1154 ;; (error "The swish-e backend cannot search specific groups"))
1156 (save-excursion
1157 (let ((qstring (cdr (assq 'query query)))
1158 (prefix
1159 (or (nnir-read-server-parm 'nnir-swish-e-remove-prefix server)
1160 (error "Missing parameter `nnir-swish-e-remove-prefix'")))
1161 artlist score artno dirnam group )
1163 (when (equal "" qstring)
1164 (error "swish-e: You didn't enter anything"))
1166 (set-buffer (get-buffer-create nnir-tmp-buffer))
1167 (erase-buffer)
1169 (message "Doing swish-e query %s..." query)
1170 (let* ((index-files
1171 (or (nnir-read-server-parm
1172 'nnir-swish-e-index-files server)
1173 (error "Missing parameter `nnir-swish-e-index-files'")))
1174 (additional-switches
1175 (nnir-read-server-parm
1176 'nnir-swish-e-additional-switches server))
1177 (cp-list `(,nnir-swish-e-program
1178 nil ; input from /dev/null
1179 t ; output
1180 nil ; don't redisplay
1181 "-f" ,@index-files
1182 ,@additional-switches
1183 "-w"
1184 ,qstring ; the query, in swish-e format
1186 (exitstatus
1187 (progn
1188 (message "%s args: %s" nnir-swish-e-program
1189 (mapconcat 'identity (cddddr cp-list) " "))
1190 (apply 'call-process cp-list))))
1191 (unless (or (null exitstatus)
1192 (zerop exitstatus))
1193 (nnheader-report 'nnir "Couldn't run swish-e: %s" exitstatus)
1194 ;; swish-e failure reason is in this buffer, show it if
1195 ;; the user wants it.
1196 (when (> gnus-verbose 6)
1197 (display-buffer nnir-tmp-buffer))))
1199 ;; The results are output in the format of:
1200 ;; rank path-name file-title file-size
1201 (goto-char (point-min))
1202 (while (re-search-forward
1203 "\\(^[0-9]+\\) \\([^ ]+\\) \"\\([^\"]+\\)\" [0-9]+$" nil t)
1204 (setq score (match-string 1)
1205 artno (match-string 3)
1206 dirnam (file-name-directory (match-string 2)))
1208 ;; don't match directories
1209 (when (string-match "^[0-9]+$" artno)
1210 (when (not (null dirnam))
1212 ;; remove nnir-swish-e-remove-prefix from beginning of dirname
1213 (when (string-match (concat "^" prefix) dirnam)
1214 (setq dirnam (replace-match "" t t dirnam)))
1216 (setq dirnam (substring dirnam 0 -1))
1217 ;; eliminate all ".", "/", "\" from beginning. Always matches.
1218 (string-match "^[./\\]*\\(.*\\)$" dirnam)
1219 ;; "/" -> "."
1220 (setq group (gnus-replace-in-string (match-string 1 dirnam) "/" "."))
1221 ;; Windows "\\" -> "."
1222 (setq group (gnus-replace-in-string group "\\\\" "."))
1224 (push (vector (gnus-group-full-name group server)
1225 (string-to-number artno)
1226 (string-to-number score))
1227 artlist))))
1229 (message "Massaging swish-e output...done")
1231 ;; Sort by score
1232 (apply 'vector
1233 (sort artlist
1234 (function (lambda (x y)
1235 (> (nnir-artitem-rsv x)
1236 (nnir-artitem-rsv y)))))))))
1238 ;; HyREX interface
1239 (defun nnir-run-hyrex (query server &optional group)
1240 (save-excursion
1241 (let ((artlist nil)
1242 (groupspec (cdr (assq 'group query)))
1243 (qstring (cdr (assq 'query query)))
1244 (prefix (nnir-read-server-parm 'nnir-hyrex-remove-prefix server))
1245 score artno dirnam)
1246 (when (and (not groupspec) group)
1247 (setq groupspec
1248 (regexp-opt
1249 (mapcar (lambda (x) (gnus-group-real-name x)) group))))
1250 (set-buffer (get-buffer-create nnir-tmp-buffer))
1251 (erase-buffer)
1252 (message "Doing hyrex-search query %s..." query)
1253 (let* ((cp-list
1254 `( ,nnir-hyrex-program
1255 nil ; input from /dev/null
1256 t ; output
1257 nil ; don't redisplay
1258 "-i",(nnir-read-server-parm 'nnir-hyrex-index-directory server) ; index directory
1259 ,@(nnir-read-server-parm 'nnir-hyrex-additional-switches server)
1260 ,qstring ; the query, in hyrex-search format
1262 (exitstatus
1263 (progn
1264 (message "%s args: %s" nnir-hyrex-program
1265 (mapconcat 'identity (cddddr cp-list) " "))
1266 (apply 'call-process cp-list))))
1267 (unless (or (null exitstatus)
1268 (zerop exitstatus))
1269 (nnheader-report 'nnir "Couldn't run hyrex-search: %s" exitstatus)
1270 ;; nnir-search failure reason is in this buffer, show it if
1271 ;; the user wants it.
1272 (when (> gnus-verbose 6)
1273 (display-buffer nnir-tmp-buffer)))) ;; FIXME: Dont clear buffer !
1274 (message "Doing hyrex-search query \"%s\"...done" qstring)
1275 (sit-for 0)
1276 ;; nnir-search returns:
1277 ;; for nnml/nnfolder: "filename mailid weigth"
1278 ;; for nnimap: "group mailid weigth"
1279 (goto-char (point-min))
1280 (delete-non-matching-lines "^\\S + [0-9]+ [0-9]+$")
1281 ;; HyREX doesn't search directly in groups -- so filter out here.
1282 (when groupspec
1283 (keep-lines groupspec))
1284 ;; extract data from result lines
1285 (goto-char (point-min))
1286 (while (re-search-forward
1287 "\\(\\S +\\) \\([0-9]+\\) \\([0-9]+\\)" nil t)
1288 (setq dirnam (match-string 1)
1289 artno (match-string 2)
1290 score (match-string 3))
1291 (when (string-match prefix dirnam)
1292 (setq dirnam (replace-match "" t t dirnam)))
1293 (push (vector (gnus-group-full-name
1294 (gnus-replace-in-string dirnam "/" ".") server)
1295 (string-to-number artno)
1296 (string-to-number score))
1297 artlist))
1298 (message "Massaging hyrex-search output...done.")
1299 (apply 'vector
1300 (sort artlist
1301 (function (lambda (x y)
1302 (if (string-lessp (nnir-artitem-group x)
1303 (nnir-artitem-group y))
1305 (< (nnir-artitem-number x)
1306 (nnir-artitem-number y)))))))
1309 ;; Namazu interface
1310 (defun nnir-run-namazu (query server &optional group)
1311 "Run given query against Namazu. Returns a vector of (group name, file name)
1312 pairs (also vectors, actually).
1314 Tested with Namazu 2.0.6 on a GNU/Linux system."
1315 ;; (when group
1316 ;; (error "The Namazu backend cannot search specific groups"))
1317 (save-excursion
1318 (let ((article-pattern (if (string= (gnus-group-server server) "nnmaildir")
1319 ":[0-9]+"
1320 "^[0-9]+$"))
1321 artlist
1322 (qstring (cdr (assq 'query query)))
1323 (prefix (nnir-read-server-parm 'nnir-namazu-remove-prefix server))
1324 score group article
1325 (process-environment (copy-sequence process-environment)))
1326 (setenv "LC_MESSAGES" "C")
1327 (set-buffer (get-buffer-create nnir-tmp-buffer))
1328 (erase-buffer)
1329 (let* ((cp-list
1330 `( ,nnir-namazu-program
1331 nil ; input from /dev/null
1332 t ; output
1333 nil ; don't redisplay
1334 "-q" ; don't be verbose
1335 "-a" ; show all matches
1336 "-s" ; use short format
1337 ,@(nnir-read-server-parm 'nnir-namazu-additional-switches server)
1338 ,qstring ; the query, in namazu format
1339 ,(nnir-read-server-parm 'nnir-namazu-index-directory server) ; index directory
1341 (exitstatus
1342 (progn
1343 (message "%s args: %s" nnir-namazu-program
1344 (mapconcat 'identity (cddddr cp-list) " "))
1345 (apply 'call-process cp-list))))
1346 (unless (or (null exitstatus)
1347 (zerop exitstatus))
1348 (nnheader-report 'nnir "Couldn't run namazu: %s" exitstatus)
1349 ;; Namazu failure reason is in this buffer, show it if
1350 ;; the user wants it.
1351 (when (> gnus-verbose 6)
1352 (display-buffer nnir-tmp-buffer))))
1354 ;; Namazu output looks something like this:
1355 ;; 2. Re: Gnus agent expire broken (score: 55)
1356 ;; /home/henrik/Mail/mail/sent/1310 (4,138 bytes)
1358 (goto-char (point-min))
1359 (while (re-search-forward
1360 "^\\([0-9]+\\.\\).*\\((score: \\([0-9]+\\)\\))\n\\([^ ]+\\)"
1361 nil t)
1362 (setq score (match-string 3)
1363 group (file-name-directory (match-string 4))
1364 article (file-name-nondirectory (match-string 4)))
1366 ;; make sure article and group is sane
1367 (when (and (string-match article-pattern article)
1368 (not (null group)))
1369 (nnir-add-result group article score prefix server artlist)))
1371 ;; sort artlist by score
1372 (apply 'vector
1373 (sort artlist
1374 (function (lambda (x y)
1375 (> (nnir-artitem-rsv x)
1376 (nnir-artitem-rsv y)))))))))
1378 (defun nnir-run-notmuch (query server &optional group)
1379 "Run QUERY against notmuch.
1380 Returns a vector of (group name, file name) pairs (also vectors,
1381 actually)."
1383 ;; (when group
1384 ;; (error "The notmuch backend cannot search specific groups"))
1386 (save-excursion
1387 (let ( (qstring (cdr (assq 'query query)))
1388 (groupspec (cdr (assq 'group query)))
1389 (prefix (nnir-read-server-parm 'nnir-notmuch-remove-prefix server))
1390 artlist
1391 (article-pattern (if (string= (gnus-group-server server) "nnmaildir")
1392 ":[0-9]+"
1393 "^[0-9]+$"))
1394 artno dirnam filenam)
1396 (when (equal "" qstring)
1397 (error "notmuch: You didn't enter anything"))
1399 (set-buffer (get-buffer-create nnir-tmp-buffer))
1400 (erase-buffer)
1402 (if groupspec
1403 (message "Doing notmuch query %s on %s..." qstring groupspec)
1404 (message "Doing notmuch query %s..." qstring))
1406 (let* ((cp-list `( ,nnir-notmuch-program
1407 nil ; input from /dev/null
1408 t ; output
1409 nil ; don't redisplay
1410 "search"
1411 "--format=text"
1412 "--output=files"
1413 ,@(nnir-read-server-parm 'nnir-notmuch-additional-switches server)
1414 ,qstring ; the query, in notmuch format
1416 (exitstatus
1417 (progn
1418 (message "%s args: %s" nnir-notmuch-program
1419 (mapconcat 'identity (cddddr cp-list) " ")) ;; ???
1420 (apply 'call-process cp-list))))
1421 (unless (or (null exitstatus)
1422 (zerop exitstatus))
1423 (nnheader-report 'nnir "Couldn't run notmuch: %s" exitstatus)
1424 ;; notmuch failure reason is in this buffer, show it if
1425 ;; the user wants it.
1426 (when (> gnus-verbose 6)
1427 (display-buffer nnir-tmp-buffer))))
1429 ;; The results are output in the format of:
1430 ;; absolute-path-name
1431 (goto-char (point-min))
1432 (while (not (eobp))
1433 (setq filenam (buffer-substring-no-properties (line-beginning-position)
1434 (line-end-position))
1435 artno (file-name-nondirectory filenam)
1436 dirnam (file-name-directory filenam))
1437 (forward-line 1)
1439 ;; don't match directories
1440 (when (string-match article-pattern artno)
1441 (when (not (null dirnam))
1443 ;; maybe limit results to matching groups.
1444 (when (or (not groupspec)
1445 (string-match groupspec dirnam))
1446 (nnir-add-result dirnam artno "" prefix server artlist)))))
1448 (message "Massaging notmuch output...done")
1450 artlist)))
1452 (defun nnir-run-find-grep (query server &optional grouplist)
1453 "Run find and grep to obtain matching articles."
1454 (let* ((method (gnus-server-to-method server))
1455 (sym (intern
1456 (concat (symbol-name (car method)) "-directory")))
1457 (directory (cadr (assoc sym (cddr method))))
1458 (regexp (cdr (assoc 'query query)))
1459 (grep-options (cdr (assoc 'grep-options query)))
1460 (grouplist (or grouplist (nnir-get-active server)))
1461 artlist)
1462 (unless directory
1463 (error "No directory found in method specification of server %s"
1464 server))
1465 (apply
1466 'vconcat
1467 (mapcar (lambda (x)
1468 (let ((group x))
1469 (message "Searching %s using find-grep..."
1470 (or group server))
1471 (save-window-excursion
1472 (set-buffer (get-buffer-create nnir-tmp-buffer))
1473 (erase-buffer)
1474 (if (> gnus-verbose 6)
1475 (pop-to-buffer (current-buffer)))
1476 (cd directory) ; Using relative paths simplifies
1477 ; postprocessing.
1478 (let ((group
1479 (if (not group)
1481 ;; Try accessing the group literally as
1482 ;; well as interpreting dots as directory
1483 ;; separators so the engine works with
1484 ;; plain nnml as well as the Gnus Cache.
1485 (let ((group (gnus-group-real-name group)))
1486 ;; Replace cl-func find-if.
1487 (if (file-directory-p group)
1488 group
1489 (if (file-directory-p
1490 (setq group
1491 (gnus-replace-in-string
1492 group
1493 "\\." "/" t)))
1494 group))))))
1495 (unless group
1496 (error "Cannot locate directory for group"))
1497 (save-excursion
1498 (apply
1499 'call-process "find" nil t
1500 "find" group "-type" "f" "-name" "[0-9]*" "-exec"
1501 "grep"
1502 `("-l" ,@(and grep-options
1503 (split-string grep-options "\\s-" t))
1504 "-e" ,regexp "{}" "+"))))
1506 ;; Translate relative paths to group names.
1507 (while (not (eobp))
1508 (let* ((path (split-string
1509 (buffer-substring
1510 (point)
1511 (line-end-position)) "/" t))
1512 (art (string-to-number (car (last path)))))
1513 (while (string= "." (car path))
1514 (setq path (cdr path)))
1515 (let ((group (mapconcat 'identity
1516 ;; Replace cl-func:
1517 ;; (subseq path 0 -1)
1518 (let ((end (1- (length path)))
1519 res)
1520 (while
1521 (>= (setq end (1- end)) 0)
1522 (push (pop path) res))
1523 (nreverse res))
1524 ".")))
1525 (push
1526 (vector (gnus-group-full-name group server) art 0)
1527 artlist))
1528 (forward-line 1)))
1529 (message "Searching %s using find-grep...done"
1530 (or group server))
1531 artlist)))
1532 grouplist))))
1534 (declare-function mm-url-insert "mm-url" (url &optional follow-refresh))
1535 (declare-function mm-url-encode-www-form-urlencoded "mm-url" (pairs))
1537 ;; gmane interface
1538 (defun nnir-run-gmane (query srv &optional groups)
1539 "Run a search against a gmane back-end server."
1540 (let* ((case-fold-search t)
1541 (qstring (cdr (assq 'query query)))
1542 (server (cadr (gnus-server-to-method srv)))
1543 (groupspec (mapconcat
1544 (lambda (x)
1545 (if (gnus-string-match-p "gmane" x)
1546 (format "group:%s" (gnus-group-short-name x))
1547 (error "Can't search non-gmane groups: %s" x)))
1548 groups " "))
1549 (authorspec
1550 (if (assq 'author query)
1551 (format "author:%s" (cdr (assq 'author query))) ""))
1552 (search (format "%s %s %s"
1553 qstring groupspec authorspec))
1554 (gnus-inhibit-demon t)
1555 artlist)
1556 (require 'mm-url)
1557 (with-current-buffer (get-buffer-create nnir-tmp-buffer)
1558 (erase-buffer)
1559 (mm-url-insert
1560 (concat
1561 "http://search.gmane.org/nov.php"
1563 (mm-url-encode-www-form-urlencoded
1564 `(("query" . ,search)
1565 ("HITSPERPAGE" . "999")))))
1566 (unless (featurep 'xemacs) (set-buffer-multibyte t))
1567 (mm-decode-coding-region (point-min) (point-max) 'utf-8)
1568 (goto-char (point-min))
1569 (forward-line 1)
1570 (while (not (eobp))
1571 (unless (or (eolp) (looking-at "\x0d"))
1572 (let ((header (nnheader-parse-nov)))
1573 (let ((xref (mail-header-xref header))
1574 (xscore (string-to-number (cdr (assoc 'X-Score
1575 (mail-header-extra header))))))
1576 (when (string-match " \\([^:]+\\)[:/]\\([0-9]+\\)" xref)
1577 (push
1578 (vector
1579 (gnus-group-prefixed-name (match-string 1 xref) srv)
1580 (string-to-number (match-string 2 xref)) xscore)
1581 artlist)))))
1582 (forward-line 1)))
1583 (apply 'vector (nreverse (mm-delete-duplicates artlist)))))
1585 ;;; Util Code:
1587 (defun nnir-read-parms (query nnir-search-engine)
1588 "Reads additional search parameters according to `nnir-engines'."
1589 (let ((parmspec (caddr (assoc nnir-search-engine nnir-engines))))
1590 (append query
1591 (mapcar 'nnir-read-parm parmspec))))
1593 (defun nnir-read-parm (parmspec)
1594 "Reads a single search parameter.
1595 `parmspec' is a cons cell, the car is a symbol, the cdr is a prompt."
1596 (let ((sym (car parmspec))
1597 (prompt (cdr parmspec)))
1598 (if (listp prompt)
1599 (let* ((result (apply 'gnus-completing-read prompt))
1600 (mapping (or (assoc result nnir-imap-search-arguments)
1601 (cons nil nnir-imap-search-other))))
1602 (cons sym (format (cdr mapping) result)))
1603 (cons sym (read-string prompt)))))
1605 (autoload 'gnus-group-topic-name "gnus-topic")
1607 (defun nnir-run-query (query)
1608 "Invoke appropriate search engine function (see `nnir-engines').
1609 If some groups were process-marked, run the query for each of the groups
1610 and concat the results."
1611 (let ((q (car (read-from-string query)))
1612 (groups (if (not (string= "nnir" nnir-address))
1613 (list (list nnir-address))
1614 (nnir-categorize
1615 (or gnus-group-marked
1616 (if (gnus-group-group-name)
1617 (list (gnus-group-group-name))
1618 (cdr (assoc (gnus-group-topic-name)
1619 gnus-topic-alist))))
1620 gnus-group-server))))
1621 (apply 'vconcat
1622 (mapcar
1623 (lambda (x)
1624 (let* ((server (car x))
1625 (nnir-search-engine
1626 (or (nnir-read-server-parm 'nnir-search-engine
1627 server)
1628 (cdr (assoc (car
1629 (gnus-server-to-method server))
1630 nnir-method-default-engines))))
1631 search-func)
1632 (setq search-func (cadr (assoc nnir-search-engine
1633 nnir-engines)))
1634 (if search-func
1635 (funcall
1636 search-func
1637 (if nnir-extra-parms
1638 (or (and (eq nnir-search-engine 'imap)
1639 (assq 'criteria q) q)
1640 (setq q (nnir-read-parms q nnir-search-engine)))
1642 server (cadr x))
1643 nil)))
1644 groups))))
1646 (defun nnir-read-server-parm (key server)
1647 "Returns the parameter value of key for the given server, where
1648 server is of form 'backend:name'."
1649 (let ((method (gnus-server-to-method server)))
1650 (cond ((and method (assq key (cddr method)))
1651 (nth 1 (assq key (cddr method))))
1652 ((boundp key) (symbol-value key))
1653 (t nil))))
1655 (defun nnir-possibly-change-server (server)
1656 (unless (and server (nnir-server-opened server))
1657 (nnir-open-server server)))
1660 (defun nnir-search-thread (header)
1661 "Make an nnir group based on the thread containing the article header"
1662 (let ((parm (list
1663 (cons 'query
1664 (nnimap-make-thread-query header))
1665 (cons 'criteria "")
1666 (cons 'server (gnus-method-to-server
1667 (gnus-find-method-for-group
1668 gnus-newsgroup-name))))))
1669 (gnus-group-make-nnir-group nil parm)
1670 (gnus-summary-goto-subject (gnus-id-to-article (mail-header-id header)))))
1672 ;; unused?
1673 (defun nnir-artlist-groups (artlist)
1674 "Returns a list of all groups in the given ARTLIST."
1675 (let ((res nil)
1676 (with-dups nil))
1677 ;; from each artitem, extract group component
1678 (setq with-dups (mapcar 'nnir-artitem-group artlist))
1679 ;; remove duplicates from above
1680 (mapc (function (lambda (x) (add-to-list 'res x)))
1681 with-dups)
1682 res))
1684 (defun nnir-get-active (srv)
1685 (let ((method (gnus-server-to-method srv))
1686 groups)
1687 (gnus-request-list method)
1688 (with-current-buffer nntp-server-buffer
1689 (let ((cur (current-buffer))
1690 name)
1691 (goto-char (point-min))
1692 (unless (or (null nnir-ignored-newsgroups)
1693 (string= nnir-ignored-newsgroups ""))
1694 (delete-matching-lines nnir-ignored-newsgroups))
1695 (if (eq (car method) 'nntp)
1696 (while (not (eobp))
1697 (ignore-errors
1698 (push (mm-string-as-unibyte
1699 (gnus-group-full-name
1700 (buffer-substring
1701 (point)
1702 (progn
1703 (skip-chars-forward "^ \t")
1704 (point))) method))
1705 groups))
1706 (forward-line))
1707 (while (not (eobp))
1708 (ignore-errors
1709 (push (mm-string-as-unibyte
1710 (if (eq (char-after) ?\")
1711 (gnus-group-full-name (read cur) method)
1712 (let ((p (point)) (name ""))
1713 (skip-chars-forward "^ \t\\\\")
1714 (setq name (buffer-substring p (point)))
1715 (while (eq (char-after) ?\\)
1716 (setq p (1+ (point)))
1717 (forward-char 2)
1718 (skip-chars-forward "^ \t\\\\")
1719 (setq name (concat name (buffer-substring
1720 p (point)))))
1721 (gnus-group-full-name name method))))
1722 groups))
1723 (forward-line)))))
1724 groups))
1726 (defun nnir-registry-action (action data-header from &optional to method)
1727 "Call `gnus-registry-action' with the original article group."
1728 (gnus-registry-action
1729 action
1730 data-header
1731 (nnir-article-group (mail-header-number data-header))
1733 method))
1735 (defun nnir-mode ()
1736 (when (eq (car (gnus-find-method-for-group gnus-newsgroup-name)) 'nnir)
1737 (setq gnus-summary-line-format
1738 (or nnir-summary-line-format gnus-summary-line-format))
1739 (when (and (boundp 'gnus-registry-install)
1740 (eq gnus-registry-install t))
1741 (remove-hook 'gnus-summary-article-delete-hook 'gnus-registry-action t)
1742 (remove-hook 'gnus-summary-article-move-hook 'gnus-registry-action t)
1743 (remove-hook 'gnus-summary-article-expire-hook 'gnus-registry-action t)
1744 (add-hook 'gnus-summary-article-delete-hook 'nnir-registry-action t t)
1745 (add-hook 'gnus-summary-article-move-hook 'nnir-registry-action t t)
1746 (add-hook 'gnus-summary-article-expire-hook 'nnir-registry-action t t))))
1750 ;; The end.
1751 (provide 'nnir)
1753 ;;; nnir.el ends here