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, 2009, 2010 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/>.
33 ;; TODO: Documentation in the Gnus manual
35 ;; Where in the existing gnus manual would this fit best?
37 ;; What does it do? Well, it allows you to search your mail using
38 ;; some search engine (imap, namazu, swish-e, gmane and others -- see
39 ;; later) by typing `G G' in the Group buffer. You will then get a
40 ;; buffer which shows all articles matching the query, sorted by
41 ;; Retrieval Status Value (score).
43 ;; When looking at the retrieval result (in the Summary buffer) you
44 ;; can type `G T' (aka M-x gnus-summary-nnir-goto-thread RET) on an
45 ;; article. You will be teleported into the group this article came
46 ;; from, showing 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
51 ;; (setq gnus-secondary-select-methods '(
52 ;; (nnimap "" (nnimap-address "localhost")
53 ;; (nnir-search-engine namazu)
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.
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'
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:
80 ;; | package conf; # Don't remove this line!
82 ;; | # Paths which will not be indexed. Don't use `^' or `$' anchors.
83 ;; | $EXCLUDE_PATH = "spam|sent";
85 ;; | # Header fields which should be searchable. case-insensitive
86 ;; | $REMAIN_HEADER = "from|date|message-id|subject";
88 ;; | # Searchable fields. case-insensitive
89 ;; | $SEARCH_FIELD = "from|date|message-id|subject";
91 ;; | # The max length of a word.
92 ;; | $WORD_LENG_MAX = 128;
94 ;; | # The max length of a field.
95 ;; | $MAX_FIELD_LENGTH = 256;
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.
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.
119 ;; | ;; find-grep configuration for searching the Gnus 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"))
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
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.
168 (require 'gnus-group)
177 (autoload 'nnimap-buffer "nnimap")
178 (autoload 'nnimap-command "nnimap")
179 (autoload 'nnimap-possibly-change-group "nnimap"))
182 (nnoo-define-basics nnir)
184 (gnus-declare-backend "nnir" 'mail)
186 (defvar nnir-imap-search-arguments
187 '(("Whole message" . "TEXT")
188 ("Subject" . "SUBJECT")
192 "Mapping from user readable keys to IMAP search items for use in nnir")
194 (defvar nnir-imap-search-other "HEADER %S"
195 "The IMAP search item to use for anything other than
196 `nnir-imap-search-arguments'. By default this is the name of an
199 (defvar nnir-imap-search-argument-history ()
200 "The history for querying search options in nnir")
202 (defvar nnir-search-history ()
203 "The history for querying search options in nnir")
205 (defvar nnir-get-article-nov-override-function nil
206 "If non-nil, a function that will be passed each search result. This
207 should return a message's headers in NOV format.
209 If this variable is nil, or if the provided function returns nil for a search
210 result, `gnus-retrieve-headers' will be called instead.")
212 ;;; Developer Extension Variable:
215 `((wais nnir-run-waissearch
219 "Imap Search in" ; Prompt
220 ,(mapcar 'car nnir-imap-search-arguments) ; alist for completing
221 nil ; allow any user input
223 nnir-imap-search-argument-history ; the history to use
224 ,nnir-imap-default-search-key ; default
226 (gmane nnir-run-gmane
227 ((author . "Gmane Author: ")))
228 (swish++ nnir-run-swish++
229 ((group . "Swish++ Group spec: ")))
230 (swish-e nnir-run-swish-e
231 ((group . "Swish-e Group spec: ")))
232 (namazu nnir-run-namazu
234 (hyrex nnir-run-hyrex
235 ((group . "Hyrex Group spec: ")))
236 (find-grep nnir-run-find-grep
237 ((grep-options . "Grep options: "))))
238 "Alist of supported search engines.
239 Each element in the alist is a three-element list (ENGINE FUNCTION ARGS).
240 ENGINE is a symbol designating the searching engine. FUNCTION is also
241 a symbol, giving the function that does the search. The third element
242 ARGS is a list of cons pairs (PARAM . PROMPT). When issuing a query,
243 the FUNCTION will issue a query for each of the PARAMs, using PROMPT.
245 The value of `nnir-search-engine' must be one of the ENGINE symbols.
246 For example, for searching a server using namazu include
247 (nnir-search-engine namazu)
248 in the server definition. Note that you have to set additional
249 variables for most backends. For example, the `namazu' backend
250 needs the variables `nnir-namazu-program',
251 `nnir-namazu-index-directory' and `nnir-namazu-remove-prefix'.
253 Add an entry here when adding a new search engine.")
255 ;;; User Customizable Variables:
258 "Search groups in Gnus with assorted seach engines."
261 (defcustom nnir-method-default-engines
264 "*Alist of default search engines keyed by server method"
268 (defcustom nnir-imap-default-search-key "Whole message"
269 "*The default IMAP search key for an nnir search. Must be one of
270 the keys in `nnir-imap-search-arguments'. To use raw imap queries
271 by default set this to \"Imap\""
275 (defcustom nnir-wais-program "waissearch"
276 "*Name of waissearch executable."
280 (defcustom nnir-wais-database (expand-file-name "~/.wais/mail")
281 "*Name of Wais database containing the mail.
283 Note that this should be a file name without extension. For example,
284 if you have a file /home/john/.wais/mail.fmt, use this:
285 (setq nnir-wais-database \"/home/john/.wais/mail\")
286 The string given here is passed to `waissearch -d' as-is."
290 (defcustom nnir-wais-remove-prefix (concat (getenv "HOME") "/Mail/")
291 "*The prefix to remove from each directory name returned by waissearch
292 in order to get a group name (albeit with / instead of .). This is a
295 For example, suppose that Wais returns file names such as
296 \"/home/john/Mail/mail/misc/42\". For this example, use the following
297 setting: (setq nnir-wais-remove-prefix \"/home/john/Mail/\")
298 Note the trailing slash. Removing this prefix gives \"mail/misc/42\".
299 `nnir' knows to remove the \"/42\" and to replace \"/\" with \".\" to
300 arrive at the correct group name, \"mail.misc\"."
304 (defcustom nnir-swish++-configuration-file
305 (expand-file-name "~/Mail/swish++.conf")
306 "*Configuration file for swish++."
310 (defcustom nnir-swish++-program "search"
311 "*Name of swish++ search executable."
315 (defcustom nnir-swish++-additional-switches '()
316 "*A list of strings, to be given as additional arguments to swish++.
318 Note that this should be a list. Ie, do NOT use the following:
319 (setq nnir-swish++-additional-switches \"-i -w\") ; wrong
321 (setq nnir-swish++-additional-switches '(\"-i\" \"-w\"))"
322 :type '(repeat (string))
325 (defcustom nnir-swish++-remove-prefix (concat (getenv "HOME") "/Mail/")
326 "*The prefix to remove from each file name returned by swish++
327 in order to get a group name (albeit with / instead of .). This is a
330 This variable is very similar to `nnir-namazu-remove-prefix', except
331 that it is for swish++, not Namazu."
336 ;; URL: http://swish-e.org/
337 ;; Variables `nnir-swish-e-index-file', `nnir-swish-e-program' and
338 ;; `nnir-swish-e-additional-switches'
340 (make-obsolete-variable 'nnir-swish-e-index-file
341 'nnir-swish-e-index-files "Emacs 23.1")
342 (defcustom nnir-swish-e-index-file
343 (expand-file-name "~/Mail/index.swish-e")
344 "*Index file for swish-e.
345 This could be a server parameter.
346 It is never consulted once `nnir-swish-e-index-files', which should be
347 used instead, has been customized."
351 (defcustom nnir-swish-e-index-files
352 (list nnir-swish-e-index-file)
353 "*List of index files for swish-e.
354 This could be a server parameter."
355 :type '(repeat (file))
358 (defcustom nnir-swish-e-program "swish-e"
359 "*Name of swish-e search executable.
360 This cannot be a server parameter."
364 (defcustom nnir-swish-e-additional-switches '()
365 "*A list of strings, to be given as additional arguments to swish-e.
367 Note that this should be a list. Ie, do NOT use the following:
368 (setq nnir-swish-e-additional-switches \"-i -w\") ; wrong
370 (setq nnir-swish-e-additional-switches '(\"-i\" \"-w\"))
372 This could be a server parameter."
373 :type '(repeat (string))
376 (defcustom nnir-swish-e-remove-prefix (concat (getenv "HOME") "/Mail/")
377 "*The prefix to remove from each file name returned by swish-e
378 in order to get a group name (albeit with / instead of .). This is a
381 This variable is very similar to `nnir-namazu-remove-prefix', except
382 that it is for swish-e, not Namazu.
384 This could be a server parameter."
388 ;; HyREX engine, see <URL:http://ls6-www.cs.uni-dortmund.de/>
390 (defcustom nnir-hyrex-program "nnir-search"
391 "*Name of the nnir-search executable."
395 (defcustom nnir-hyrex-additional-switches '()
396 "*A list of strings, to be given as additional arguments for nnir-search.
397 Note that this should be a list. Ie, do NOT use the following:
398 (setq nnir-hyrex-additional-switches \"-ddl ddl.xml -c nnir\") ; wrong !
400 (setq nnir-hyrex-additional-switches '(\"-ddl\" \"ddl.xml\" \"-c\" \"nnir\"))"
401 :type '(repeat (string))
404 (defcustom nnir-hyrex-index-directory (getenv "HOME")
405 "*Index directory for HyREX."
409 (defcustom nnir-hyrex-remove-prefix (concat (getenv "HOME") "/Mail/")
410 "*The prefix to remove from each file name returned by HyREX
411 in order to get a group name (albeit with / instead of .).
413 For example, suppose that HyREX returns file names such as
414 \"/home/john/Mail/mail/misc/42\". For this example, use the following
415 setting: (setq nnir-hyrex-remove-prefix \"/home/john/Mail/\")
416 Note the trailing slash. Removing this prefix gives \"mail/misc/42\".
417 `nnir' knows to remove the \"/42\" and to replace \"/\" with \".\" to
418 arrive at the correct group name, \"mail.misc\"."
422 ;; Namazu engine, see <URL:http://www.namazu.org/>
424 (defcustom nnir-namazu-program "namazu"
425 "*Name of Namazu search executable."
429 (defcustom nnir-namazu-index-directory (expand-file-name "~/Mail/namazu/")
430 "*Index directory for Namazu."
434 (defcustom nnir-namazu-additional-switches '()
435 "*A list of strings, to be given as additional arguments to namazu.
436 The switches `-q', `-a', and `-s' are always used, very few other switches
437 make any sense in this context.
439 Note that this should be a list. Ie, do NOT use the following:
440 (setq nnir-namazu-additional-switches \"-i -w\") ; wrong
442 (setq nnir-namazu-additional-switches '(\"-i\" \"-w\"))"
443 :type '(repeat (string))
446 (defcustom nnir-namazu-remove-prefix (concat (getenv "HOME") "/Mail/")
447 "*The prefix to remove from each file name returned by Namazu
448 in order to get a group name (albeit with / instead of .).
450 For example, suppose that Namazu returns file names such as
451 \"/home/john/Mail/mail/misc/42\". For this example, use the following
452 setting: (setq nnir-namazu-remove-prefix \"/home/john/Mail/\")
453 Note the trailing slash. Removing this prefix gives \"mail/misc/42\".
454 `nnir' knows to remove the \"/42\" and to replace \"/\" with \".\" to
455 arrive at the correct group name, \"mail.misc\"."
459 ;;; Internal Variables:
461 (defvar nnir-current-query nil
462 "Internal: stores current query (= group name).")
464 (defvar nnir-current-server nil
465 "Internal: stores current server (does it ever change?).")
467 (defvar nnir-current-group-marked nil
468 "Internal: stores current list of process-marked groups.")
470 (defvar nnir-artlist nil
471 "Internal: stores search result.")
473 (defvar nnir-tmp-buffer " *nnir*"
474 "Internal: temporary buffer.")
476 (defvar nnir-extra-parms nil
477 "Internal: stores request for extra search parms")
483 (defun gnus-group-make-nnir-group (nnir-extra-parms)
484 "Create an nnir group. Asks for query."
486 (setq nnir-current-query nil
487 nnir-current-server nil
488 nnir-current-group-marked nil
490 (let* ((query (read-string "Query: " nil 'nnir-search-history))
491 (parms (list (cons 'query query))))
492 (add-to-list 'parms (cons 'unique-id (message-unique-id)) t)
493 (gnus-group-read-ephemeral-group
494 (concat "nnir:" (prin1-to-string parms)) '(nnir "") t
495 (cons (current-buffer) gnus-current-window-configuration)
498 ;; Summary mode commands.
500 (defun gnus-summary-nnir-goto-thread ()
501 "Only applies to nnir groups. Go to group this article came from
502 and show thread that contains this article."
504 (unless (eq 'nnir (car (gnus-find-method-for-group gnus-newsgroup-name)))
505 (error "Can't execute this command unless in nnir group"))
506 (let* ((cur (gnus-summary-article-number))
507 (group (nnir-artlist-artitem-group nnir-artlist cur))
508 (backend-number (nnir-artlist-artitem-number nnir-artlist cur))
509 (id (mail-header-id (gnus-summary-article-header)))
511 (mail-header-references (gnus-summary-article-header)))))
512 (if (eq (car (gnus-find-method-for-group group)) 'nnimap)
514 (nnimap-possibly-change-group (gnus-group-short-name group) nil)
515 (with-current-buffer (nnimap-buffer)
519 "(OR HEADER REFERENCES %s HEADER Message-Id %s)"
521 (dolist (refid refs value)
524 "(OR (OR HEADER Message-Id %s HEADER REFERENCES %s) %s)"
525 refid refid value)))))
526 (result (nnimap-command "UID SEARCH %s" cmd)))
527 (gnus-summary-read-group-1
528 group t t gnus-summary-buffer nil
532 (cdr (assoc "SEARCH" (cdr result))))))))))
533 (gnus-summary-read-group-1 group t t gnus-summary-buffer
534 nil (list backend-number))
535 (gnus-summary-limit (list backend-number))
536 (gnus-summary-refer-thread))))
539 (if (fboundp 'eval-after-load)
540 (eval-after-load "gnus-sum"
541 '(define-key gnus-summary-goto-map
542 "T" 'gnus-summary-nnir-goto-thread))
543 (add-hook 'gnus-summary-mode-hook
545 (define-key gnus-summary-goto-map
546 "T" 'gnus-summary-nnir-goto-thread)))))
550 ;; Gnus backend interface functions.
552 (deffoo nnir-open-server (server &optional definitions)
553 ;; Just set the server variables appropriately.
554 (nnoo-change-server 'nnir server definitions))
556 (deffoo nnir-request-group (group &optional server fast info)
557 "GROUP is the query string."
558 (nnir-possibly-change-server server)
559 ;; Check for cache and return that if appropriate.
560 (if (and (equal group nnir-current-query)
561 (equal gnus-group-marked nnir-current-group-marked)
563 (equal server nnir-current-server)))
566 (setq nnir-artlist (nnir-run-query group)))
567 (with-current-buffer nntp-server-buffer
568 (setq nnir-current-query group)
569 (when server (setq nnir-current-server server))
570 (setq nnir-current-group-marked gnus-group-marked)
571 (if (zerop (length nnir-artlist))
572 (nnheader-report 'nnir "Search produced empty results.")
573 ;; Remember data for cache.
574 (nnheader-insert "211 %d %d %d %s\n"
575 (nnir-artlist-length nnir-artlist) ; total #
577 (nnir-artlist-length nnir-artlist) ; last #
578 group)))) ; group name
580 (deffoo nnir-retrieve-headers (articles &optional group server fetch-old)
582 (let ((artlist (copy-sequence articles))
583 art artitem artgroup artno artrsv artfullgroup
584 novitem novdata foo server)
585 (while (not (null artlist))
586 (setq art (car artlist))
590 "nnir-retrieve-headers doesn't grok message ids: %s"
592 (setq artitem (nnir-artlist-article nnir-artlist art))
593 (setq artrsv (nnir-artitem-rsv artitem))
594 (setq artfullgroup (nnir-artitem-group artitem))
595 (setq artno (nnir-artitem-number artitem))
596 (setq artgroup (gnus-group-real-name artfullgroup))
597 (setq server (gnus-group-server artfullgroup))
598 ;; retrieve NOV or HEAD data for this article, transform into
599 ;; NOV data and prepend to `novdata'
600 (set-buffer nntp-server-buffer)
601 (nnir-possibly-change-server server)
602 (let ((gnus-override-method
603 (gnus-server-to-method server)))
604 ;; if nnir-get-article-nov-override-function is set, use it
605 (if nnir-get-article-nov-override-function
606 (setq novitem (funcall nnir-get-article-nov-override-function
608 ;; else, set novitem through nnheader-parse-nov/nnheader-parse-head
609 (case (setq foo (gnus-retrieve-headers (list artno)
612 (goto-char (point-min))
613 (setq novitem (nnheader-parse-nov)))
615 (goto-char (point-min))
616 (setq novitem (nnheader-parse-head)))
617 (t (error "Unknown header type %s while requesting article %s of group %s"
618 foo artno artfullgroup)))))
619 ;; replace article number in original group with article number
622 (mail-header-set-number novitem art)
623 (mail-header-set-from novitem
624 (mail-header-from novitem))
625 (mail-header-set-subject
627 (format "[%d: %s/%d] %s"
628 artrsv artgroup artno
629 (mail-header-subject novitem)))
630 (push novitem novdata)
631 (setq artlist (cdr artlist))))
632 (setq novdata (nreverse novdata))
633 (set-buffer nntp-server-buffer) (erase-buffer)
634 (mapc 'nnheader-insert-nov novdata)
637 (deffoo nnir-request-article (article
638 &optional group server to-buffer)
639 (if (stringp article)
642 "nnir-retrieve-headers doesn't grok message ids: %s"
645 (let* ((artitem (nnir-artlist-article nnir-artlist
647 (artfullgroup (nnir-artitem-group artitem))
648 (artno (nnir-artitem-number artitem))
650 ;; Why must we bind nntp-server-buffer here? It won't
651 ;; work if `buf' is used, say. (Of course, the set-buffer
652 ;; line below must then be updated, too.)
653 (nntp-server-buffer (or to-buffer nntp-server-buffer)))
654 (set-buffer nntp-server-buffer)
656 (message "Requesting article %d from group %s"
658 (gnus-request-article artno artfullgroup nntp-server-buffer)
659 (cons artfullgroup artno)))))
662 (nnoo-define-skeleton nnir)
665 (defmacro nnir-add-result (dirnam artno score prefix server artlist)
666 "Ask `nnir-compose-result' to construct a result vector,
667 and if it is non-nil, add it to artlist."
668 `(let ((result (nnir-compose-result ,dirnam ,artno ,score ,prefix ,server)))
669 (when (not (null result))
670 (push result ,artlist))))
672 (autoload 'nnmaildir-base-name-to-article-number "nnmaildir")
674 ;; Helper function currently used by the Swish++ and Namazu backends;
675 ;; perhaps useful for other backends as well
676 (defun nnir-compose-result (dirnam article score prefix server)
677 "Extract the group from dirnam, and create a result vector
678 ready to be added to the list of search results."
680 ;; remove nnir-*-remove-prefix from beginning of dirnam filename
681 (when (string-match (concat "^" prefix) dirnam)
682 (setq dirnam (replace-match "" t t dirnam)))
684 (when (file-readable-p (concat prefix dirnam article))
685 ;; remove trailing slash and, for nnmaildir, cur/new/tmp
688 (if (string= (gnus-group-server server) "nnmaildir")
691 ;; Set group to dirnam without any leading dots or slashes,
692 ;; and with all subsequent slashes replaced by dots
693 (let ((group (gnus-replace-in-string
694 (gnus-replace-in-string dirnam "^[./\\]" "" t)
697 (vector (nnir-group-full-name group server)
698 (if (string= (gnus-group-server server) "nnmaildir")
699 (nnmaildir-base-name-to-article-number
700 (substring article 0 (string-match ":" article))
702 (string-to-number article))
703 (string-to-number score)))))
705 ;;; Search Engine Interfaces:
707 ;; freeWAIS-sf interface.
708 (defun nnir-run-waissearch (query server &optional group)
709 "Run given query agains waissearch. Returns vector of (group name, file name)
710 pairs (also vectors, actually)."
712 ;; (error "The freeWAIS-sf backend cannot search specific groups"))
714 (let ((qstring (cdr (assq 'query query)))
715 (prefix (nnir-read-server-parm 'nnir-wais-remove-prefix server))
716 artlist score artno dirnam)
717 (set-buffer (get-buffer-create nnir-tmp-buffer))
719 (message "Doing WAIS query %s..." query)
720 (call-process nnir-wais-program
721 nil ; input from /dev/null
722 t ; output to current buffer
723 nil ; don't redisplay
724 "-d" (nnir-read-server-parm 'nnir-wais-database server) ; database to search
726 (message "Massaging waissearch output...")
727 ;; remove superfluous lines
728 (keep-lines "Score:")
729 ;; extract data from result lines
730 (goto-char (point-min))
731 (while (re-search-forward
732 "Score: +\\([0-9]+\\).*'\\([0-9]+\\) +\\([^']+\\)/'" nil t)
733 (setq score (match-string 1)
734 artno (match-string 2)
735 dirnam (match-string 3))
736 (unless (string-match prefix dirnam)
737 (nnheader-report 'nnir "Dir name %s doesn't contain prefix %s"
739 (setq group (gnus-replace-in-string
740 (replace-match "" t t dirnam) "/" "."))
741 (push (vector (nnir-group-full-name group server)
742 (string-to-number artno)
743 (string-to-number score))
745 (message "Massaging waissearch output...done")
748 (function (lambda (x y)
749 (> (nnir-artitem-rsv x)
750 (nnir-artitem-rsv y)))))))))
753 (defun nnir-run-imap (query srv &optional groups)
754 "Run a search against an IMAP back-end server.
755 This uses a custom query language parser; see `nnir-imap-make-query' for
756 details on the language and supported extensions"
758 (let ((qstring (cdr (assq 'query query)))
759 (server (cadr (gnus-server-to-method srv)))
760 (defs (caddr (gnus-server-to-method srv)))
761 (criteria (or (cdr (assq 'criteria query))
762 (cdr (assoc nnir-imap-default-search-key
763 nnir-imap-search-arguments))))
764 (gnus-inhibit-demon t)
766 (message "Opening server %s" server)
773 (when (nnimap-possibly-change-group
774 (gnus-group-short-name group) server)
775 (with-current-buffer (nnimap-buffer)
776 (message "Searching %s..." group)
778 (result (nnimap-command "UID SEARCH %s"
779 (if (string= criteria "")
781 (nnir-imap-make-query
782 criteria qstring)))))
784 (lambda (artnum) (push (vector group artnum 1) artlist)
785 (setq arts (1+ arts)))
787 (delete 0 (mapcar #'string-to-number
790 (message "Searching %s... %d matches" group arts)))
791 (message "Searching %s...done" group))
796 (defun nnir-imap-make-query (criteria qstring)
797 "Parse the query string and criteria into an appropriate IMAP search
798 expression, returning the string query to make.
800 This implements a little language designed to return the expected results
801 to an arbitrary query string to the end user.
803 The search is always case-insensitive, as defined by RFC2060, and supports
804 the following features (inspired by the Google search input language):
806 Automatic \"and\" queries
807 If you specify multiple words then they will be treated as an \"and\"
808 expression intended to match all components.
811 If you wrap your query in double-quotes then it will be treated as a
815 If you precede a term with \"-\" then it will negate that.
818 If you include an upper-case \"OR\" in your search it will cause the
819 term before it and the term after it to be treated as alternatives.
821 In future the following will be added to the language:
822 * support for date matches
823 * support for location of text matching within the query
824 * from/to/etc headers
825 * additional search terms
826 * flag based searching
827 * anything else that the RFC supports, basically."
828 ;; Walk through the query and turn it into an IMAP query string.
829 (nnir-imap-query-to-imap criteria (nnir-imap-parse-query qstring)))
832 (defun nnir-imap-query-to-imap (criteria query)
833 "Turn a s-expression format query into IMAP."
835 ;; Turn the expressions into IMAP text
837 (nnir-imap-expr-to-imap criteria item))
838 ;; The query, already in s-expr format.
840 ;; Append a space between each expression
844 (defun nnir-imap-expr-to-imap (criteria expr)
845 "Convert EXPR into an IMAP search expression on CRITERIA"
846 ;; What sort of expression is this, eh?
848 ;; Simple string term
850 (format "%s %S" criteria expr))
853 ;; Composite term: or expression
854 ((eq (car-safe expr) 'or)
856 (nnir-imap-expr-to-imap criteria (second expr))
857 (nnir-imap-expr-to-imap criteria (third expr))))
858 ;; Composite term: just the fax, mam
859 ((eq (car-safe expr) 'not)
860 (format "NOT (%s)" (nnir-imap-query-to-imap criteria (rest expr))))
861 ;; Composite term: just expand it all.
862 ((and (not (null expr)) (listp expr))
863 (format "(%s)" (nnir-imap-query-to-imap criteria expr)))
864 ;; Complex value, give up for now.
865 (t (error "Unhandled input: %S" expr))))
868 (defun nnir-imap-parse-query (string)
869 "Turn STRING into an s-expression based query based on the IMAP
870 query language as defined in `nnir-imap-make-query'.
872 This involves turning individual tokens into higher level terms
873 that the search language can then understand and use."
875 ;; Set up the parsing environment.
877 (goto-char (point-min))
878 ;; Now, collect the output terms and return them.
880 (while (not (nnir-imap-end-of-input))
881 (push (nnir-imap-next-expr) out))
885 (defun nnir-imap-next-expr (&optional count)
886 "Return the next expression from the current buffer."
887 (let ((term (nnir-imap-next-term count))
888 (next (nnir-imap-peek-symbol)))
889 ;; Are we looking at an 'or' expression?
891 ;; Handle 'expr or expr'
893 (list 'or term (nnir-imap-next-expr 2)))
898 (defun nnir-imap-next-term (&optional count)
899 "Return the next TERM from the current buffer."
900 (let ((term (nnir-imap-next-symbol count)))
901 ;; What sort of term is this?
903 ;; and -- just ignore it
904 ((eq term 'and) 'and)
906 ((eq term 'not) (list 'not (nnir-imap-next-expr)))
911 (defun nnir-imap-peek-symbol ()
912 "Return the next symbol from the current buffer, but don't consume it."
914 (nnir-imap-next-symbol)))
916 (defun nnir-imap-next-symbol (&optional count)
917 "Return the next symbol from the current buffer, or nil if we are
918 at the end of the buffer. If supplied COUNT skips some symbols before
919 returning the one at the supplied position."
920 (when (and (numberp count) (> count 1))
921 (nnir-imap-next-symbol (1- count)))
922 (let ((case-fold-search t))
923 ;; end of input stream?
924 (unless (nnir-imap-end-of-input)
925 ;; No, return the next symbol from the stream.
927 ;; negated expression -- return it and advance one char.
928 ((looking-at "-") (forward-char 1) 'not)
930 ((looking-at "\"") (nnir-imap-delimited-string "\""))
931 ;; list expression -- we parse the content and return this as a list.
933 (nnir-imap-parse-query (nnir-imap-delimited-string ")")))
934 ;; keyword input -- return a symbol version
935 ((looking-at "\\band\\b") (forward-char 3) 'and)
936 ((looking-at "\\bor\\b") (forward-char 2) 'or)
937 ((looking-at "\\bnot\\b") (forward-char 3) 'not)
938 ;; Simple, boring keyword
939 (t (let ((start (point))
940 (end (if (search-forward-regexp "[[:blank:]]" nil t)
943 ;; unskip if we hit a non-blank terminal character.
944 (when (string-match "[^[:blank:]]" (match-string 0))
946 (goto-char (point-max)))))
947 (buffer-substring start end)))))))
949 (defun nnir-imap-delimited-string (delimiter)
950 "Return a delimited string from the current buffer."
951 (let ((start (point)) end)
952 (forward-char 1) ; skip the first delimiter.
954 (unless (search-forward delimiter nil t)
955 (error "Unmatched delimited input with %s in query" delimiter))
956 (let ((here (point)))
957 (unless (equal (buffer-substring (- here 2) (- here 1)) "\\")
958 (setq end (point)))))
959 (buffer-substring (1+ start) (1- end))))
961 (defun nnir-imap-end-of-input ()
962 "Are we at the end of input?"
963 (skip-chars-forward "[[:blank:]]")
967 ;; Swish++ interface.
976 (defun nnir-run-swish++ (query server &optional group)
977 "Run QUERY against swish++.
978 Returns a vector of (group name, file name) pairs (also vectors,
981 Tested with swish++ 4.7 on GNU/Linux and with swish++ 5.0b2 on
985 ;; (error "The swish++ backend cannot search specific groups"))
988 (let ( (qstring (cdr (assq 'query query)))
989 (groupspec (cdr (assq 'group query)))
990 (prefix (nnir-read-server-parm 'nnir-swish++-remove-prefix server))
992 ;; nnml-use-compressed-files might be any string, but probably this
993 ;; is sufficient. Note that we can't only use the value of
994 ;; nnml-use-compressed-files because old articles might have been
995 ;; saved with a different value.
996 (article-pattern (if (string= (gnus-group-server server) "nnmaildir")
998 "^[0-9]+\\(\\.[a-z0-9]+\\)?$"))
999 score artno dirnam filenam)
1001 (when (equal "" qstring)
1002 (error "swish++: You didn't enter anything"))
1004 (set-buffer (get-buffer-create nnir-tmp-buffer))
1008 (message "Doing swish++ query %s on %s..." qstring groupspec)
1009 (message "Doing swish++ query %s..." qstring))
1011 (let* ((cp-list `( ,nnir-swish++-program
1012 nil ; input from /dev/null
1014 nil ; don't redisplay
1015 "--config-file" ,(nnir-read-server-parm 'nnir-swish++-configuration-file server)
1016 ,@(nnir-read-server-parm 'nnir-swish++-additional-switches server)
1017 ,qstring ; the query, in swish++ format
1021 (message "%s args: %s" nnir-swish++-program
1022 (mapconcat 'identity (cddddr cp-list) " ")) ;; ???
1023 (apply 'call-process cp-list))))
1024 (unless (or (null exitstatus)
1026 (nnheader-report 'nnir "Couldn't run swish++: %s" exitstatus)
1027 ;; swish++ failure reason is in this buffer, show it if
1028 ;; the user wants it.
1029 (when (> gnus-verbose 6)
1030 (display-buffer nnir-tmp-buffer))))
1032 ;; The results are output in the format of:
1034 ;; rank relative-path-name file-size file-title
1036 ;; rank relative-path-name file-size topic??
1037 ;; where rank is an integer from 1 to 100.
1038 (goto-char (point-min))
1039 (while (re-search-forward
1040 "\\(^[0-9]+\\) \\([^ ]+\\) [0-9]+ \\(.*\\)$" nil t)
1041 (setq score (match-string 1)
1042 filenam (match-string 2)
1043 artno (file-name-nondirectory filenam)
1044 dirnam (file-name-directory filenam))
1046 ;; don't match directories
1047 (when (string-match article-pattern artno)
1048 (when (not (null dirnam))
1050 ;; maybe limit results to matching groups.
1051 (when (or (not groupspec)
1052 (string-match groupspec dirnam))
1053 (nnir-add-result dirnam artno score prefix server artlist)))))
1055 (message "Massaging swish++ output...done")
1060 (function (lambda (x y)
1061 (> (nnir-artitem-rsv x)
1062 (nnir-artitem-rsv y)))))))))
1064 ;; Swish-E interface.
1065 (defun nnir-run-swish-e (query server &optional group)
1066 "Run given query against swish-e.
1067 Returns a vector of (group name, file name) pairs (also vectors,
1070 Tested with swish-e-2.0.1 on Windows NT 4.0."
1072 ;; swish-e crashes with empty parameter to "-w" on commandline...
1074 ;; (error "The swish-e backend cannot search specific groups"))
1077 (let ((qstring (cdr (assq 'query query)))
1079 (or (nnir-read-server-parm 'nnir-swish-e-remove-prefix server)
1080 (error "Missing parameter `nnir-swish-e-remove-prefix'")))
1081 artlist score artno dirnam group )
1083 (when (equal "" qstring)
1084 (error "swish-e: You didn't enter anything"))
1086 (set-buffer (get-buffer-create nnir-tmp-buffer))
1089 (message "Doing swish-e query %s..." query)
1091 (or (nnir-read-server-parm
1092 'nnir-swish-e-index-files server)
1093 (error "Missing parameter `nnir-swish-e-index-files'")))
1094 (additional-switches
1095 (nnir-read-server-parm
1096 'nnir-swish-e-additional-switches server))
1097 (cp-list `(,nnir-swish-e-program
1098 nil ; input from /dev/null
1100 nil ; don't redisplay
1102 ,@additional-switches
1104 ,qstring ; the query, in swish-e format
1108 (message "%s args: %s" nnir-swish-e-program
1109 (mapconcat 'identity (cddddr cp-list) " "))
1110 (apply 'call-process cp-list))))
1111 (unless (or (null exitstatus)
1113 (nnheader-report 'nnir "Couldn't run swish-e: %s" exitstatus)
1114 ;; swish-e failure reason is in this buffer, show it if
1115 ;; the user wants it.
1116 (when (> gnus-verbose 6)
1117 (display-buffer nnir-tmp-buffer))))
1119 ;; The results are output in the format of:
1120 ;; rank path-name file-title file-size
1121 (goto-char (point-min))
1122 (while (re-search-forward
1123 "\\(^[0-9]+\\) \\([^ ]+\\) \"\\([^\"]+\\)\" [0-9]+$" nil t)
1124 (setq score (match-string 1)
1125 artno (match-string 3)
1126 dirnam (file-name-directory (match-string 2)))
1128 ;; don't match directories
1129 (when (string-match "^[0-9]+$" artno)
1130 (when (not (null dirnam))
1132 ;; remove nnir-swish-e-remove-prefix from beginning of dirname
1133 (when (string-match (concat "^" prefix) dirnam)
1134 (setq dirnam (replace-match "" t t dirnam)))
1136 (setq dirnam (substring dirnam 0 -1))
1137 ;; eliminate all ".", "/", "\" from beginning. Always matches.
1138 (string-match "^[./\\]*\\(.*\\)$" dirnam)
1140 (setq group (gnus-replace-in-string (match-string 1 dirnam) "/" "."))
1141 ;; Windows "\\" -> "."
1142 (setq group (gnus-replace-in-string group "\\\\" "."))
1144 (push (vector (nnir-group-full-name group server)
1145 (string-to-number artno)
1146 (string-to-number score))
1149 (message "Massaging swish-e output...done")
1154 (function (lambda (x y)
1155 (> (nnir-artitem-rsv x)
1156 (nnir-artitem-rsv y)))))))))
1159 (defun nnir-run-hyrex (query server &optional group)
1162 (groupspec (cdr (assq 'group query)))
1163 (qstring (cdr (assq 'query query)))
1164 (prefix (nnir-read-server-parm 'nnir-hyrex-remove-prefix server))
1166 (when (and (not groupspec) group)
1169 (mapcar (lambda (x) (gnus-group-real-name x)) group))))
1170 (set-buffer (get-buffer-create nnir-tmp-buffer))
1172 (message "Doing hyrex-search query %s..." query)
1174 `( ,nnir-hyrex-program
1175 nil ; input from /dev/null
1177 nil ; don't redisplay
1178 "-i",(nnir-read-server-parm 'nnir-hyrex-index-directory server) ; index directory
1179 ,@(nnir-read-server-parm 'nnir-hyrex-additional-switches server)
1180 ,qstring ; the query, in hyrex-search format
1184 (message "%s args: %s" nnir-hyrex-program
1185 (mapconcat 'identity (cddddr cp-list) " "))
1186 (apply 'call-process cp-list))))
1187 (unless (or (null exitstatus)
1189 (nnheader-report 'nnir "Couldn't run hyrex-search: %s" exitstatus)
1190 ;; nnir-search failure reason is in this buffer, show it if
1191 ;; the user wants it.
1192 (when (> gnus-verbose 6)
1193 (display-buffer nnir-tmp-buffer)))) ;; FIXME: Dont clear buffer !
1194 (message "Doing hyrex-search query \"%s\"...done" qstring)
1196 ;; nnir-search returns:
1197 ;; for nnml/nnfolder: "filename mailid weigth"
1198 ;; for nnimap: "group mailid weigth"
1199 (goto-char (point-min))
1200 (delete-non-matching-lines "^\\S + [0-9]+ [0-9]+$")
1201 ;; HyREX doesn't search directly in groups -- so filter out here.
1203 (keep-lines groupspec))
1204 ;; extract data from result lines
1205 (goto-char (point-min))
1206 (while (re-search-forward
1207 "\\(\\S +\\) \\([0-9]+\\) \\([0-9]+\\)" nil t)
1208 (setq dirnam (match-string 1)
1209 artno (match-string 2)
1210 score (match-string 3))
1211 (when (string-match prefix dirnam)
1212 (setq dirnam (replace-match "" t t dirnam)))
1213 (push (vector (nnir-group-full-name
1214 (gnus-replace-in-string dirnam "/" ".") server)
1215 (string-to-number artno)
1216 (string-to-number score))
1218 (message "Massaging hyrex-search output...done.")
1221 (function (lambda (x y)
1222 (if (string-lessp (nnir-artitem-group x)
1223 (nnir-artitem-group y))
1225 (< (nnir-artitem-number x)
1226 (nnir-artitem-number y)))))))
1230 (defun nnir-run-namazu (query server &optional group)
1231 "Run given query against Namazu. Returns a vector of (group name, file name)
1232 pairs (also vectors, actually).
1234 Tested with Namazu 2.0.6 on a GNU/Linux system."
1236 ;; (error "The Namazu backend cannot search specific groups"))
1238 (let ((article-pattern (if (string= (gnus-group-server server) "nnmaildir")
1242 (qstring (cdr (assq 'query query)))
1243 (prefix (nnir-read-server-parm 'nnir-namazu-remove-prefix server))
1245 (process-environment (copy-sequence process-environment)))
1246 (setenv "LC_MESSAGES" "C")
1247 (set-buffer (get-buffer-create nnir-tmp-buffer))
1250 `( ,nnir-namazu-program
1251 nil ; input from /dev/null
1253 nil ; don't redisplay
1254 "-q" ; don't be verbose
1255 "-a" ; show all matches
1256 "-s" ; use short format
1257 ,@(nnir-read-server-parm 'nnir-namazu-additional-switches server)
1258 ,qstring ; the query, in namazu format
1259 ,(nnir-read-server-parm 'nnir-namazu-index-directory server) ; index directory
1263 (message "%s args: %s" nnir-namazu-program
1264 (mapconcat 'identity (cddddr cp-list) " "))
1265 (apply 'call-process cp-list))))
1266 (unless (or (null exitstatus)
1268 (nnheader-report 'nnir "Couldn't run namazu: %s" exitstatus)
1269 ;; Namazu failure reason is in this buffer, show it if
1270 ;; the user wants it.
1271 (when (> gnus-verbose 6)
1272 (display-buffer nnir-tmp-buffer))))
1274 ;; Namazu output looks something like this:
1275 ;; 2. Re: Gnus agent expire broken (score: 55)
1276 ;; /home/henrik/Mail/mail/sent/1310 (4,138 bytes)
1278 (goto-char (point-min))
1279 (while (re-search-forward
1280 "^\\([0-9]+\\.\\).*\\((score: \\([0-9]+\\)\\))\n\\([^ ]+\\)"
1282 (setq score (match-string 3)
1283 group (file-name-directory (match-string 4))
1284 article (file-name-nondirectory (match-string 4)))
1286 ;; make sure article and group is sane
1287 (when (and (string-match article-pattern article)
1289 (nnir-add-result group article score prefix server artlist)))
1291 ;; sort artlist by score
1294 (function (lambda (x y)
1295 (> (nnir-artitem-rsv x)
1296 (nnir-artitem-rsv y)))))))))
1298 (defun nnir-run-find-grep (query server &optional grouplist)
1299 "Run find and grep to obtain matching articles."
1300 (let* ((method (gnus-server-to-method server))
1302 (concat (symbol-name (car method)) "-directory")))
1303 (directory (cadr (assoc sym (cddr method))))
1304 (regexp (cdr (assoc 'query query)))
1305 (grep-options (cdr (assoc 'grep-options query)))
1308 (error "No directory found in method specification of server %s"
1314 (message "Searching %s using find-grep..."
1316 (save-window-excursion
1317 (set-buffer (get-buffer-create nnir-tmp-buffer))
1319 (if (> gnus-verbose 6)
1320 (pop-to-buffer (current-buffer)))
1321 (cd directory) ; Using relative paths simplifies
1326 ;; Try accessing the group literally as
1327 ;; well as interpreting dots as directory
1328 ;; separators so the engine works with
1329 ;; plain nnml as well as the Gnus Cache.
1330 (let ((group (gnus-group-real-name group)))
1331 ;; Replace cl-func find-if.
1332 (if (file-directory-p group)
1334 (if (file-directory-p
1336 (gnus-replace-in-string
1341 (error "Cannot locate directory for group"))
1344 'call-process "find" nil t
1345 "find" group "-type" "f" "-name" "[0-9]*" "-exec"
1347 `("-l" ,@(and grep-options
1348 (split-string grep-options "\\s-" t))
1349 "-e" ,regexp "{}" "+"))))
1351 ;; Translate relative paths to group names.
1353 (let* ((path (split-string
1356 (line-end-position)) "/" t))
1357 (art (string-to-number (car (last path)))))
1358 (while (string= "." (car path))
1359 (setq path (cdr path)))
1360 (let ((group (mapconcat 'identity
1362 ;; (subseq path 0 -1)
1363 (let ((end (1- (length path)))
1366 (>= (setq end (1- end)) 0)
1367 (push (pop path) res))
1371 (vector (nnir-group-full-name group server) art 0)
1374 (message "Searching %s using find-grep...done"
1380 (defun nnir-run-gmane (query srv &optional groups)
1381 "Run a search against a gmane back-end server."
1382 (if (string-match-p "gmane" srv)
1383 (let* ((case-fold-search t)
1384 (qstring (cdr (assq 'query query)))
1385 (server (cadr (gnus-server-to-method srv)))
1386 (groupspec (if groups
1388 (function (lambda (x)
1390 (gnus-group-short-name x))))
1393 (if (assq 'author query)
1394 (format "author:%s" (cdr (assq 'author query))) ""))
1395 (search (format "%s %s %s"
1396 qstring groupspec authorspec))
1398 (with-current-buffer nntp-server-buffer
1402 "http://search.gmane.org/nov.php"
1404 (mm-url-encode-www-form-urlencoded
1405 `(("query" . ,search)
1406 ("HITSPERPAGE" . "999")))))
1407 (unless (featurep 'xemacs) (set-buffer-multibyte t))
1408 (mm-decode-coding-region (point-min) (point-max) 'utf-8)
1409 (goto-char (point-min))
1412 (unless (or (eolp) (looking-at "\x0d"))
1413 (let ((header (nnheader-parse-nov)))
1414 (let ((xref (mail-header-xref header)))
1415 (when (string-match " \\([^:]+\\)[:/]\\([0-9]+\\)" xref)
1418 (gnus-group-prefixed-name (match-string 1 xref) srv)
1419 (string-to-number (match-string 2 xref)) 1)
1423 (message "Can't search non-gmane nntp groups")))
1427 (defun nnir-read-parms (query)
1428 "Reads additional search parameters according to `nnir-engines'."
1429 (let ((parmspec (caddr (assoc nnir-search-engine nnir-engines))))
1431 (mapcar 'nnir-read-parm parmspec))))
1433 (defun nnir-read-parm (parmspec)
1434 "Reads a single search parameter.
1435 `parmspec' is a cons cell, the car is a symbol, the cdr is a prompt."
1436 (let ((sym (car parmspec))
1437 (prompt (cdr parmspec)))
1439 (let* ((result (apply 'gnus-completing-read prompt))
1440 (mapping (or (assoc result nnir-imap-search-arguments)
1441 (cons nil nnir-imap-search-other))))
1442 (cons sym (format (cdr mapping) result)))
1443 (cons sym (read-string prompt)))))
1445 (defun nnir-run-query (query)
1446 "Invoke appropriate search engine function (see `nnir-engines').
1447 If some groups were process-marked, run the query for each of the groups
1448 and concat the results."
1449 (let ((q (car (read-from-string query)))
1450 (groups (nnir-sort-groups-by-server
1451 (or gnus-group-marked (list (gnus-group-group-name))))))
1454 (let* ((server (car x))
1456 (or (nnir-read-server-parm 'nnir-search-engine
1459 (gnus-server-to-method server))
1460 nnir-method-default-engines))))
1462 (setq search-func (cadr
1463 (assoc nnir-search-engine
1466 (funcall search-func
1467 (if nnir-extra-parms
1474 (defun nnir-read-server-parm (key server)
1475 "Returns the parameter value of key for the given server, where
1476 server is of form 'backend:name'."
1477 (let ((method (gnus-server-to-method server)))
1478 (cond ((and method (assq key (cddr method)))
1479 (nth 1 (assq key (cddr method))))
1482 (defun nnir-group-full-name (shortname server)
1483 "For the given group name, return a full Gnus group name.
1484 The Gnus backend/server information is added."
1485 (gnus-group-prefixed-name shortname (gnus-server-to-method server)))
1487 (defun nnir-possibly-change-server (server)
1488 (unless (and server (nnir-server-opened server))
1489 (nnir-open-server server)))
1492 ;; Data type article list.
1494 (defun nnir-artlist-length (artlist)
1495 "Returns number of articles in artlist."
1498 (defun nnir-artlist-article (artlist n)
1499 "Returns from ARTLIST the Nth artitem (counting starting at 1)."
1500 (elt artlist (1- n)))
1502 (defun nnir-artitem-group (artitem)
1503 "Returns the group from the ARTITEM."
1506 (defun nnir-artlist-artitem-group (artlist n)
1507 "Returns from ARTLIST the group of the Nth artitem (counting from 1)."
1508 (nnir-artitem-group (nnir-artlist-article artlist n)))
1510 (defun nnir-artitem-number (artitem)
1511 "Returns the number from the ARTITEM."
1514 (defun nnir-artlist-artitem-number (artlist n)
1515 "Returns from ARTLIST the number of the Nth artitem (counting from 1)."
1516 (nnir-artitem-number (nnir-artlist-article artlist n)))
1518 (defun nnir-artitem-rsv (artitem)
1519 "Returns the Retrieval Status Value (RSV, score) from the ARTITEM."
1522 (defun nnir-artlist-artitem-rsv (artlist n)
1523 "Returns from ARTLIST the Retrieval Status Value of the Nth
1524 artitem (counting from 1)."
1525 (nnir-artitem-rsv (nnir-artlist-article artlist n)))
1528 (defun nnir-artlist-groups (artlist)
1529 "Returns a list of all groups in the given ARTLIST."
1532 ;; from each artitem, extract group component
1533 (setq with-dups (mapcar 'nnir-artitem-group artlist))
1534 ;; remove duplicates from above
1535 (mapc (function (lambda (x) (add-to-list 'res x)))
1539 (defun nnir-sort-groups-by-server (groups)
1540 "sorts a list of groups into an alist keyed by server"
1543 (dolist (var groups value)
1544 (let ((server (gnus-group-server var)))
1545 (if (assoc server value)
1546 (nconc (cdr (assoc server value)) (list var))
1547 (push (cons (gnus-group-server var) (list var)) value))))
1554 ;;; nnir.el ends here