* lisp/emacs-lisp/lisp-mnt.el: Comments.
[bpt/emacs.git] / lisp / gnus / nnir.el
CommitLineData
c38e0c97 1;;; nnir.el --- search mail with various search engines -*- coding: utf-8 -*-
e6d2d263 2
ba318903 3;; Copyright (C) 1998-2014 Free Software Foundation, Inc.
e6d2d263 4
c38e0c97 5;; Author: Kai Großjohann <grossjohann@ls6.cs.uni-dortmund.de>
e6d2d263
MB
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>
de1765ab
GM
13;; Keywords: news mail searching ir
14
15;; This file is part of GNU Emacs.
16
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.
21
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.
26
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/>.
29
30;;; Commentary:
e6d2d263 31
953d41c4
G
32;; What does it do? Well, it allows you to search your mail using
33;; some search engine (imap, namazu, swish-e, gmane and others -- see
34;; later) by typing `G G' in the Group buffer. You will then get a
35;; buffer which shows all articles matching the query, sorted by
36;; Retrieval Status Value (score).
e6d2d263
MB
37
38;; When looking at the retrieval result (in the Summary buffer) you
59e75882 39;; can type `A W' (aka M-x gnus-warp-to-article RET) on an article. You
ed797193 40;; will be warped into the group this article came from. Typing `A T'
4ddab346
G
41;; (aka M-x gnus-summary-refer-thread RET) will warp to the group and
42;; also show the thread this article is part of.
e6d2d263 43
953d41c4 44;; The Lisp setup may involve setting a few variables and setting up the
e6d2d263
MB
45;; search engine. You can define the variables in the server definition
46;; like this :
47;; (setq gnus-secondary-select-methods '(
48;; (nnimap "" (nnimap-address "localhost")
cfe94935 49;; (nnir-search-engine namazu)
e6d2d263 50;; )))
953d41c4
G
51;; The main variable to set is `nnir-search-engine'. Choose one of
52;; the engines listed in `nnir-engines'. (Actually `nnir-engines' is
53;; an alist, type `C-h v nnir-engines RET' for more information; this
54;; includes examples for setting `nnir-search-engine', too.)
e6d2d263 55
953d41c4
G
56;; If you use one of the local indices (namazu, find-grep, swish) you
57;; must also set up a search engine backend.
e6d2d263 58
953d41c4 59;; 1. Namazu
e6d2d263
MB
60;;
61;; The Namazu backend requires you to have one directory containing all
62;; index files, this is controlled by the `nnir-namazu-index-directory'
63;; variable. To function the `nnir-namazu-remove-prefix' variable must
953d41c4 64;; also be correct, see the documentation for `nnir-namazu-remove-prefix'
e6d2d263
MB
65;; above.
66;;
67;; It is particularly important not to pass any any switches to namazu
68;; that will change the output format. Good switches to use include
69;; `--sort', `--ascending', `--early' and `--late'. Refer to the Namazu
70;; documentation for further information on valid switches.
71;;
72;; To index my mail with the `mknmz' program I use the following
73;; configuration file:
74;;
75;; ,----
76;; | package conf; # Don't remove this line!
77;; |
78;; | # Paths which will not be indexed. Don't use `^' or `$' anchors.
79;; | $EXCLUDE_PATH = "spam|sent";
80;; |
81;; | # Header fields which should be searchable. case-insensitive
82;; | $REMAIN_HEADER = "from|date|message-id|subject";
83;; |
84;; | # Searchable fields. case-insensitive
85;; | $SEARCH_FIELD = "from|date|message-id|subject";
86;; |
87;; | # The max length of a word.
88;; | $WORD_LENG_MAX = 128;
89;; |
90;; | # The max length of a field.
91;; | $MAX_FIELD_LENGTH = 256;
92;; `----
93;;
94;; My mail is stored in the directories ~/Mail/mail/, ~/Mail/lists/ and
95;; ~/Mail/archive/, so to index them I go to the directory set in
96;; `nnir-namazu-index-directory' and issue the following command.
97;;
98;; mknmz --mailnews ~/Mail/archive/ ~/Mail/mail/ ~/Mail/lists/
99;;
100;; For maximum searching efficiency I have a cron job set to run this
101;; command every four hours.
102
953d41c4 103;; 2. find-grep
e6d2d263
MB
104;;
105;; The find-grep engine simply runs find(1) to locate eligible
106;; articles and searches them with grep(1). This, of course, is much
107;; slower than using a proper search engine but OTOH doesn't require
108;; maintenance of an index and is still faster than using any built-in
109;; means for searching. The method specification of the server to
110;; search must include a directory for this engine to work (E.g.,
111;; `nnml-directory'). The tools must be POSIX compliant. GNU Find
112;; prior to version 4.2.12 (4.2.26 on Linux due to incorrect ARG_MAX
113;; handling) does not work.
114;; ,----
115;; | ;; find-grep configuration for searching the Gnus Cache
116;; |
117;; | (nnml "cache"
118;; | (nnml-get-new-mail nil)
119;; | (nnir-search-engine find-grep)
120;; | (nnml-directory "~/News/cache/")
121;; | (nnml-active-file "~/News/cache/active"))
122;; `----
123
124;; Developer information:
125
126;; I have tried to make the code expandable. Basically, it is divided
127;; into two layers. The upper layer is somewhat like the `nnvirtual'
f7362445
LMI
128;; backend: given a specification of what articles to show from
129;; another backend, it creates a group containing exactly those
130;; articles. The lower layer issues a query to a search engine and
131;; produces such a specification of what articles to show from the
e6d2d263
MB
132;; other backend.
133
134;; The interface between the two layers consists of the single
f83a656e
AC
135;; function `nnir-run-query', which dispatches the search to the
136;; proper search function. The argument of `nnir-run-query' is an
137;; alist with two keys: 'nnir-query-spec and 'nnir-group-spec. The
138;; value for 'nnir-query-spec is an alist. The only required key/value
139;; pair is (query . "query") specifying the search string to pass to
140;; the query engine. Individual engines may have other elements. The
141;; value of 'nnir-group-spec is a list with the specification of the
142;; groups/servers to search. The format of the 'nnir-group-spec is
143;; (("server1" ("group11" "group12")) ("server2" ("group21"
144;; "group22"))). If any of the group lists is absent then all groups
145;; on that server are searched.
146
147;; The output of `nnir-run-query' is supposed to be a vector, each
148;; element of which should in turn be a three-element vector. The
149;; first element should be full group name of the article, the second
150;; element should be the article number, and the third element should
151;; be the Retrieval Status Value (RSV) as returned from the search
152;; engine. An RSV is the score assigned to the document by the search
153;; engine. For Boolean search engines, the RSV is always 1000 (or 1
154;; or 100, or whatever you like).
e6d2d263
MB
155
156;; The sorting order of the articles in the summary buffer created by
157;; nnir is based on the order of the articles in the above mentioned
158;; vector, so that's where you can do the sorting you'd like. Maybe
159;; it would be nice to have a way of displaying the search result
160;; sorted differently?
161
162;; So what do you need to do when you want to add another search
163;; engine? You write a function that executes the query. Temporary
164;; data from the search engine can be put in `nnir-tmp-buffer'. This
165;; function should return the list of articles as a vector, as
166;; described above. Then, you need to register this backend in
167;; `nnir-engines'. Then, users can choose the backend by setting
953d41c4 168;; `nnir-search-engine' as a server variable.
e6d2d263 169
1b811c90
AC
170;;; Code:
171
172;;; Setup:
e6d2d263 173
e195d639
GM
174;; For Emacs <22.2 and XEmacs.
175(eval-and-compile
7d964492 176 (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
e195d639 177
e6d2d263
MB
178(require 'nnoo)
179(require 'gnus-group)
e6d2d263
MB
180(require 'message)
181(require 'gnus-util)
ec179403 182(eval-when-compile
e6d2d263
MB
183 (require 'cl))
184
1b811c90
AC
185;;; Internal Variables:
186
f83a656e
AC
187(defvar nnir-memo-query nil
188 "Internal: stores current query.")
1b811c90 189
f83a656e
AC
190(defvar nnir-memo-server nil
191 "Internal: stores current server.")
1b811c90
AC
192
193(defvar nnir-artlist nil
194 "Internal: stores search result.")
195
1b811c90
AC
196(defvar nnir-search-history ()
197 "Internal: the history for querying search options in nnir")
198
f83a656e
AC
199(defconst nnir-tmp-buffer " *nnir*"
200 "Internal: temporary buffer.")
201
1b811c90
AC
202
203;; Imap variables
204
205(defvar nnir-imap-search-arguments
4a3988d5
G
206 '(("whole message" . "TEXT")
207 ("subject" . "SUBJECT")
208 ("to" . "TO")
209 ("from" . "FROM")
210 ("body" . "BODY")
211 ("imap" . ""))
1b811c90
AC
212 "Mapping from user readable keys to IMAP search items for use in nnir")
213
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")
218
219(defvar nnir-imap-search-argument-history ()
220 "The history for querying search options in nnir")
221
222;;; Helper macros
223
224;; Data type article list.
225
226(defmacro nnir-artlist-length (artlist)
227 "Returns number of articles in artlist."
228 `(length ,artlist))
229
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))))
234
235(defmacro nnir-artitem-group (artitem)
236 "Returns the group from the ARTITEM."
237 `(elt ,artitem 0))
238
239(defmacro nnir-artitem-number (artitem)
240 "Returns the number from the ARTITEM."
241 `(elt ,artitem 1))
242
243(defmacro nnir-artitem-rsv (artitem)
244 "Returns the Retrieval Status Value (RSV, score) from the ARTITEM."
245 `(elt ,artitem 2))
246
247(defmacro nnir-article-group (article)
248 "Returns the group for ARTICLE"
249 `(nnir-artitem-group (nnir-artlist-article nnir-artlist ,article)))
250
251(defmacro nnir-article-number (article)
252 "Returns the number for ARTICLE"
253 `(nnir-artitem-number (nnir-artlist-article nnir-artlist ,article)))
254
255(defmacro nnir-article-rsv (article)
256 "Returns the rsv for ARTICLE"
257 `(nnir-artitem-rsv (nnir-artlist-article nnir-artlist ,article)))
258
259(defsubst nnir-article-ids (article)
260 "Returns the pair `(nnir id . real id)' of ARTICLE"
261 (cons article (nnir-article-number article)))
262
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))'.
266The category key for a member of the sequence is obtained
267as `(keyfunc member)' and the corresponding element is just
268`member'. If `valuefunc' is non-nil, the element of the list
269is `(valuefunc member)'."
270 `(unless (null ,sequence)
271 (let (value)
04db63bc 272 (mapc
1b811c90
AC
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)))
283
284;;; Finish setup:
285
286(require 'gnus-sum)
dab0271f
G
287
288(eval-when-compile
289 (autoload 'nnimap-buffer "nnimap")
290 (autoload 'nnimap-command "nnimap")
eaa610c3 291 (autoload 'nnimap-change-group "nnimap")
47f0b35e 292 (autoload 'nnimap-make-thread-query "nnimap")
f83a656e
AC
293 (autoload 'gnus-registry-action "gnus-registry")
294 (autoload 'gnus-registry-get-id-key "gnus-registry")
295 (autoload 'gnus-group-topic-name "gnus-topic"))
296
27625a58 297
e6d2d263
MB
298(nnoo-declare nnir)
299(nnoo-define-basics nnir)
300
b152f5d3 301(gnus-declare-backend "nnir" 'mail 'virtual)
e6d2d263 302
e6d2d263
MB
303
304;;; User Customizable Variables:
305
306(defgroup nnir nil
9858f6c3 307 "Search groups in Gnus with assorted search engines."
e6d2d263
MB
308 :group 'gnus)
309
ed797193
G
310(defcustom nnir-ignored-newsgroups ""
311 "*A regexp to match newsgroups in the active file that should
312 be skipped when searching."
2bed3f04 313 :version "24.1"
ed797193
G
314 :type '(regexp)
315 :group 'nnir)
316
317(defcustom nnir-summary-line-format nil
318 "*The format specification of the lines in an nnir summary buffer.
319
320All the items from `gnus-summary-line-format' are available, along
321with three items unique to nnir summary buffers:
322
323%Z Search retrieval score value (integer)
324%G Article original full group name (string)
325%g Article original short group name (string)
326
327If nil this will use `gnus-summary-line-format'."
2bed3f04 328 :version "24.1"
a931698a 329 :type '(choice (const :tag "gnus-summary-line-format" nil) string)
ed797193
G
330 :group 'nnir)
331
1b811c90
AC
332(defcustom nnir-retrieve-headers-override-function nil
333 "*If non-nil, a function that accepts an article list and group
334and populates the `nntp-server-buffer' with the retrieved
335headers. Must return either 'nov or 'headers indicating the
336retrieved header format.
337
338If this variable is nil, or if the provided function returns nil for a search
339result, `gnus-retrieve-headers' will be called instead."
2bed3f04 340 :version "24.1"
a931698a 341 :type '(choice (const :tag "gnus-retrieve-headers" nil) function)
1b811c90
AC
342 :group 'nnir)
343
4a3988d5 344(defcustom nnir-imap-default-search-key "whole message"
953d41c4
G
345 "*The default IMAP search key for an nnir search. Must be one of
346 the keys in `nnir-imap-search-arguments'. To use raw imap queries
f83a656e 347 by default set this to \"imap\"."
2bed3f04 348 :version "24.1"
549c9aed
G
349 :type `(choice ,@(mapcar (lambda (elem) (list 'const (car elem)))
350 nnir-imap-search-arguments))
e6d2d263
MB
351 :group 'nnir)
352
e6d2d263
MB
353(defcustom nnir-swish++-configuration-file
354 (expand-file-name "~/Mail/swish++.conf")
355 "*Configuration file for swish++."
356 :type '(file)
357 :group 'nnir)
358
359(defcustom nnir-swish++-program "search"
360 "*Name of swish++ search executable."
361 :type '(string)
362 :group 'nnir)
363
364(defcustom nnir-swish++-additional-switches '()
365 "*A list of strings, to be given as additional arguments to swish++.
366
367Note that this should be a list. Ie, do NOT use the following:
368 (setq nnir-swish++-additional-switches \"-i -w\") ; wrong
369Instead, use this:
370 (setq nnir-swish++-additional-switches '(\"-i\" \"-w\"))"
371 :type '(repeat (string))
372 :group 'nnir)
373
374(defcustom nnir-swish++-remove-prefix (concat (getenv "HOME") "/Mail/")
375 "*The prefix to remove from each file name returned by swish++
376in order to get a group name (albeit with / instead of .). This is a
377regular expression.
378
953d41c4
G
379This variable is very similar to `nnir-namazu-remove-prefix', except
380that it is for swish++, not Namazu."
e6d2d263
MB
381 :type '(regexp)
382 :group 'nnir)
383
384;; Swish-E.
66627fa9 385;; URL: http://swish-e.org/
e6d2d263
MB
386;; Variables `nnir-swish-e-index-file', `nnir-swish-e-program' and
387;; `nnir-swish-e-additional-switches'
388
389(make-obsolete-variable 'nnir-swish-e-index-file
265ac10b 390 'nnir-swish-e-index-files "Emacs 23.1")
e6d2d263
MB
391(defcustom nnir-swish-e-index-file
392 (expand-file-name "~/Mail/index.swish-e")
393 "*Index file for swish-e.
394This could be a server parameter.
395It is never consulted once `nnir-swish-e-index-files', which should be
396used instead, has been customized."
397 :type '(file)
398 :group 'nnir)
399
400(defcustom nnir-swish-e-index-files
401 (list nnir-swish-e-index-file)
402 "*List of index files for swish-e.
403This could be a server parameter."
404 :type '(repeat (file))
405 :group 'nnir)
406
407(defcustom nnir-swish-e-program "swish-e"
408 "*Name of swish-e search executable.
409This cannot be a server parameter."
410 :type '(string)
411 :group 'nnir)
412
413(defcustom nnir-swish-e-additional-switches '()
414 "*A list of strings, to be given as additional arguments to swish-e.
415
416Note that this should be a list. Ie, do NOT use the following:
417 (setq nnir-swish-e-additional-switches \"-i -w\") ; wrong
418Instead, use this:
419 (setq nnir-swish-e-additional-switches '(\"-i\" \"-w\"))
420
421This could be a server parameter."
422 :type '(repeat (string))
423 :group 'nnir)
424
425(defcustom nnir-swish-e-remove-prefix (concat (getenv "HOME") "/Mail/")
426 "*The prefix to remove from each file name returned by swish-e
427in order to get a group name (albeit with / instead of .). This is a
428regular expression.
429
953d41c4
G
430This variable is very similar to `nnir-namazu-remove-prefix', except
431that it is for swish-e, not Namazu.
e6d2d263
MB
432
433This could be a server parameter."
434 :type '(regexp)
435 :group 'nnir)
436
953d41c4
G
437;; HyREX engine, see <URL:http://ls6-www.cs.uni-dortmund.de/>
438
439(defcustom nnir-hyrex-program "nnir-search"
440 "*Name of the nnir-search executable."
441 :type '(string)
442 :group 'nnir)
443
444(defcustom nnir-hyrex-additional-switches '()
445 "*A list of strings, to be given as additional arguments for nnir-search.
446Note that this should be a list. Ie, do NOT use the following:
447 (setq nnir-hyrex-additional-switches \"-ddl ddl.xml -c nnir\") ; wrong !
448Instead, use this:
449 (setq nnir-hyrex-additional-switches '(\"-ddl\" \"ddl.xml\" \"-c\" \"nnir\"))"
450 :type '(repeat (string))
451 :group 'nnir)
452
453(defcustom nnir-hyrex-index-directory (getenv "HOME")
454 "*Index directory for HyREX."
455 :type '(directory)
456 :group 'nnir)
457
458(defcustom nnir-hyrex-remove-prefix (concat (getenv "HOME") "/Mail/")
459 "*The prefix to remove from each file name returned by HyREX
460in order to get a group name (albeit with / instead of .).
461
462For example, suppose that HyREX returns file names such as
463\"/home/john/Mail/mail/misc/42\". For this example, use the following
464setting: (setq nnir-hyrex-remove-prefix \"/home/john/Mail/\")
465Note the trailing slash. Removing this prefix gives \"mail/misc/42\".
466`nnir' knows to remove the \"/42\" and to replace \"/\" with \".\" to
467arrive at the correct group name, \"mail.misc\"."
468 :type '(directory)
469 :group 'nnir)
470
66627fa9 471;; Namazu engine, see <URL:http://www.namazu.org/>
e6d2d263
MB
472
473(defcustom nnir-namazu-program "namazu"
474 "*Name of Namazu search executable."
475 :type '(string)
476 :group 'nnir)
477
478(defcustom nnir-namazu-index-directory (expand-file-name "~/Mail/namazu/")
479 "*Index directory for Namazu."
480 :type '(directory)
481 :group 'nnir)
482
483(defcustom nnir-namazu-additional-switches '()
484 "*A list of strings, to be given as additional arguments to namazu.
485The switches `-q', `-a', and `-s' are always used, very few other switches
486make any sense in this context.
487
488Note that this should be a list. Ie, do NOT use the following:
489 (setq nnir-namazu-additional-switches \"-i -w\") ; wrong
490Instead, use this:
491 (setq nnir-namazu-additional-switches '(\"-i\" \"-w\"))"
492 :type '(repeat (string))
493 :group 'nnir)
494
495(defcustom nnir-namazu-remove-prefix (concat (getenv "HOME") "/Mail/")
496 "*The prefix to remove from each file name returned by Namazu
497in order to get a group name (albeit with / instead of .).
498
953d41c4
G
499For example, suppose that Namazu returns file names such as
500\"/home/john/Mail/mail/misc/42\". For this example, use the following
501setting: (setq nnir-namazu-remove-prefix \"/home/john/Mail/\")
502Note the trailing slash. Removing this prefix gives \"mail/misc/42\".
503`nnir' knows to remove the \"/42\" and to replace \"/\" with \".\" to
504arrive at the correct group name, \"mail.misc\"."
e6d2d263
MB
505 :type '(directory)
506 :group 'nnir)
507
d30dd079
G
508(defcustom nnir-notmuch-program "notmuch"
509 "*Name of notmuch search executable."
2bed3f04 510 :version "24.1"
d30dd079
G
511 :type '(string)
512 :group 'nnir)
513
514(defcustom nnir-notmuch-additional-switches '()
515 "*A list of strings, to be given as additional arguments to notmuch.
516
517Note that this should be a list. Ie, do NOT use the following:
518 (setq nnir-notmuch-additional-switches \"-i -w\") ; wrong
519Instead, use this:
520 (setq nnir-notmuch-additional-switches '(\"-i\" \"-w\"))"
2bed3f04 521 :version "24.1"
d30dd079
G
522 :type '(repeat (string))
523 :group 'nnir)
524
525(defcustom nnir-notmuch-remove-prefix (concat (getenv "HOME") "/Mail/")
526 "*The prefix to remove from each file name returned by notmuch
527in order to get a group name (albeit with / instead of .). This is a
528regular expression.
529
530This variable is very similar to `nnir-namazu-remove-prefix', except
531that it is for notmuch, not Namazu."
2bed3f04 532 :version "24.1"
d30dd079
G
533 :type '(regexp)
534 :group 'nnir)
535
953d41c4
G
536;;; Developer Extension Variable:
537
538(defvar nnir-engines
14782c59 539 `((imap nnir-run-imap
953d41c4
G
540 ((criteria
541 "Imap Search in" ; Prompt
542 ,(mapcar 'car nnir-imap-search-arguments) ; alist for completing
543 nil ; allow any user input
544 nil ; initial value
545 nnir-imap-search-argument-history ; the history to use
546 ,nnir-imap-default-search-key ; default
547 )))
548 (gmane nnir-run-gmane
f83a656e 549 ((gmane-author . "Gmane Author: ")))
953d41c4 550 (swish++ nnir-run-swish++
6ed7a66a 551 ((swish++-group . "Swish++ Group spec (regexp): ")))
953d41c4 552 (swish-e nnir-run-swish-e
6ed7a66a 553 ((swish-e-group . "Swish-e Group spec (regexp): ")))
953d41c4
G
554 (namazu nnir-run-namazu
555 ())
d30dd079
G
556 (notmuch nnir-run-notmuch
557 ())
953d41c4 558 (hyrex nnir-run-hyrex
6ed7a66a 559 ((hyrex-group . "Hyrex Group spec (regexp): ")))
953d41c4
G
560 (find-grep nnir-run-find-grep
561 ((grep-options . "Grep options: "))))
562 "Alist of supported search engines.
563Each element in the alist is a three-element list (ENGINE FUNCTION ARGS).
564ENGINE is a symbol designating the searching engine. FUNCTION is also
565a symbol, giving the function that does the search. The third element
566ARGS is a list of cons pairs (PARAM . PROMPT). When issuing a query,
567the FUNCTION will issue a query for each of the PARAMs, using PROMPT.
568
569The value of `nnir-search-engine' must be one of the ENGINE symbols.
570For example, for searching a server using namazu include
571 (nnir-search-engine namazu)
572in the server definition. Note that you have to set additional
573variables for most backends. For example, the `namazu' backend
574needs the variables `nnir-namazu-program',
575`nnir-namazu-index-directory' and `nnir-namazu-remove-prefix'.
576
577Add an entry here when adding a new search engine.")
578
7c5ef69a 579(defcustom nnir-method-default-engines '((nnimap . imap) (nntp . gmane))
549c9aed 580 "*Alist of default search engines keyed by server method."
2bed3f04 581 :version "24.1"
f83a656e 582 :group 'nnir
df8abd0b 583 :type `(repeat (cons (choice (const nnimap) (const nntp) (const nnspool)
549c9aed
G
584 (const nneething) (const nndir) (const nnmbox)
585 (const nnml) (const nnmh) (const nndraft)
586 (const nnfolder) (const nnmaildir))
587 (choice
588 ,@(mapcar (lambda (elem) (list 'const (car elem)))
f83a656e 589 nnir-engines)))))
ed797193 590
e6d2d263
MB
591;; Gnus glue.
592
f83a656e
AC
593(defun gnus-group-make-nnir-group (nnir-extra-parms &optional specs)
594 "Create an nnir group. Prompt for a search query and determine
595the groups to search as follows: if called from the *Server*
596buffer search all groups belonging to the server on the current
597line; if called from the *Group* buffer search any marked groups,
598or the group on the current line, or all the groups under the
599current topic. Calling with a prefix-arg prompts for additional
600search-engine specific constraints. A non-nil `specs' arg must be
601an alist with `nnir-query-spec' and `nnir-group-spec' keys, and
602skips all prompting."
953d41c4 603 (interactive "P")
f83a656e 604 (let* ((group-spec
1ec75f95 605 (or (cdr (assq 'nnir-group-spec specs))
f83a656e
AC
606 (if (gnus-server-server-name)
607 (list (list (gnus-server-server-name)))
608 (nnir-categorize
609 (or gnus-group-marked
610 (if (gnus-group-group-name)
611 (list (gnus-group-group-name))
612 (cdr (assoc (gnus-group-topic-name) gnus-topic-alist))))
613 gnus-group-server))))
614 (query-spec
1ec75f95 615 (or (cdr (assq 'nnir-query-spec specs))
f83a656e
AC
616 (apply
617 'append
618 (list (cons 'query
619 (read-string "Query: " nil 'nnir-search-history)))
620 (when nnir-extra-parms
621 (mapcar
622 (lambda (x)
623 (nnir-read-parms (nnir-server-to-search-engine (car x))))
624 group-spec))))))
e6d2d263 625 (gnus-group-read-ephemeral-group
f83a656e
AC
626 (concat "nnir-" (message-unique-id))
627 (list 'nnir "nnir")
628 nil
629; (cons (current-buffer) gnus-current-window-configuration)
630 nil
631 nil nil
632 (list
633 (cons 'nnir-specs (list (cons 'nnir-query-spec query-spec)
634 (cons 'nnir-group-spec group-spec)))
635 (cons 'nnir-artlist nil)))))
636
637(defun gnus-summary-make-nnir-group (nnir-extra-parms)
638 "Search a group from the summary buffer."
639 (interactive "P")
640 (gnus-warp-to-article)
641 (let ((spec
642 (list
643 (cons 'nnir-group-spec
644 (list (list
645 (gnus-group-server gnus-newsgroup-name)
646 (list gnus-newsgroup-name)))))))
647 (gnus-group-make-nnir-group nnir-extra-parms spec)))
e6d2d263 648
e6d2d263
MB
649
650;; Gnus backend interface functions.
651
652(deffoo nnir-open-server (server &optional definitions)
653 ;; Just set the server variables appropriately.
f83a656e
AC
654 (let ((backend (car (gnus-server-to-method server))))
655 (if backend
656 (nnoo-change-server backend server definitions)
657 (add-hook 'gnus-summary-mode-hook 'nnir-mode)
658 (nnoo-change-server 'nnir server definitions))))
659
660(deffoo nnir-request-group (group &optional server dont-check info)
661 (nnir-possibly-change-group group server)
1ec75f95 662 (let ((pgroup (gnus-group-guess-full-name-from-command-method group))
f83a656e
AC
663 length)
664 ;; Check for cached search result or run the query and cache the
665 ;; result.
666 (unless (and nnir-artlist dont-check)
667 (gnus-group-set-parameter
668 pgroup 'nnir-artlist
669 (setq nnir-artlist
670 (nnir-run-query
671 (gnus-group-get-parameter pgroup 'nnir-specs t))))
672 (nnir-request-update-info pgroup (gnus-get-info pgroup)))
673 (with-current-buffer nntp-server-buffer
674 (if (zerop (setq length (nnir-artlist-length nnir-artlist)))
675 (progn
676 (nnir-close-group group)
677 (nnheader-report 'nnir "Search produced empty results."))
678 (nnheader-insert "211 %d %d %d %s\n"
679 length ; total #
680 1 ; first #
681 length ; last #
682 group)))) ; group name
683 nnir-artlist)
e6d2d263
MB
684
685(deffoo nnir-retrieve-headers (articles &optional group server fetch-old)
ed797193
G
686 (with-current-buffer nntp-server-buffer
687 (let ((gnus-inhibit-demon t)
688 (articles-by-group (nnir-categorize
689 articles nnir-article-group nnir-article-ids))
690 headers)
691 (while (not (null articles-by-group))
692 (let* ((group-articles (pop articles-by-group))
693 (artgroup (car group-articles))
694 (articleids (cadr group-articles))
695 (artlist (sort (mapcar 'cdr articleids) '<))
696 (server (gnus-group-server artgroup))
697 (gnus-override-method (gnus-server-to-method server))
698 parsefunc)
f83a656e 699 ;; (nnir-possibly-change-group nil server)
ed797193
G
700 (erase-buffer)
701 (case (setq gnus-headers-retrieved-by
702 (or
703 (and
704 nnir-retrieve-headers-override-function
705 (funcall nnir-retrieve-headers-override-function
706 artlist artgroup))
707 (gnus-retrieve-headers artlist artgroup nil)))
708 (nov
709 (setq parsefunc 'nnheader-parse-nov))
710 (headers
711 (setq parsefunc 'nnheader-parse-head))
712 (t (error "Unknown header type %s while requesting articles \
713 of group %s" gnus-headers-retrieved-by artgroup)))
714 (goto-char (point-min))
715 (while (not (eobp))
716 (let* ((novitem (funcall parsefunc))
9937bef4
G
717 (artno (and novitem
718 (mail-header-number novitem)))
47ac6170 719 (art (car (rassq artno articleids))))
ed797193
G
720 (when art
721 (mail-header-set-number novitem art)
ed797193
G
722 (push novitem headers))
723 (forward-line 1)))))
724 (setq headers
725 (sort headers
726 (lambda (x y)
727 (< (mail-header-number x) (mail-header-number y)))))
728 (erase-buffer)
729 (mapc 'nnheader-insert-nov headers)
e6d2d263
MB
730 'nov)))
731
ed797193 732(deffoo nnir-request-article (article &optional group server to-buffer)
f83a656e 733 (nnir-possibly-change-group group server)
b09c3fe0
G
734 (if (and (stringp article)
735 (not (eq 'nnimap (car (gnus-server-to-method server)))))
e6d2d263
MB
736 (nnheader-report
737 'nnir
c1dccd20 738 "nnir-request-article only groks message ids for nnimap servers: %s"
b09c3fe0 739 server)
e6d2d263 740 (save-excursion
b09c3fe0 741 (let ((article article)
f83a656e
AC
742 query)
743 (when (stringp article)
744 (setq gnus-override-method (gnus-server-to-method server))
745 (setq query
746 (list
747 (cons 'query (format "HEADER Message-ID %s" article))
748 (cons 'criteria "")
749 (cons 'shortcut t)))
750 (unless (and nnir-artlist (equal query nnir-memo-query)
751 (equal server nnir-memo-server))
752 (setq nnir-artlist (nnir-run-imap query server)
753 nnir-memo-query query
754 nnir-memo-server server))
755 (setq article 1))
756 (unless (zerop (nnir-artlist-length nnir-artlist))
757 (let ((artfullgroup (nnir-article-group article))
758 (artno (nnir-article-number article)))
759 (message "Requesting article %d from group %s"
760 artno artfullgroup)
761 (if to-buffer
762 (with-current-buffer to-buffer
763 (let ((gnus-article-decode-hook nil))
764 (gnus-request-article-this-buffer artno artfullgroup)))
765 (gnus-request-article artno artfullgroup))
766 (cons artfullgroup artno)))))))
e6d2d263 767
6ec9acb3
AC
768(deffoo nnir-request-move-article (article group server accept-form
769 &optional last internal-move-group)
f83a656e 770 (nnir-possibly-change-group group server)
ed797193
G
771 (let* ((artfullgroup (nnir-article-group article))
772 (artno (nnir-article-number article))
6ec9acb3
AC
773 (to-newsgroup (nth 1 accept-form))
774 (to-method (gnus-find-method-for-group to-newsgroup))
775 (from-method (gnus-find-method-for-group artfullgroup))
7454326a 776 (move-is-internal (gnus-server-equal from-method to-method)))
ed797193
G
777 (unless (gnus-check-backend-function
778 'request-move-article artfullgroup)
779 (error "The group %s does not support article moving" artfullgroup))
6ec9acb3
AC
780 (gnus-request-move-article
781 artno
782 artfullgroup
783 (nth 1 from-method)
784 accept-form
785 last
786 (and move-is-internal
787 to-newsgroup ; Not respooling
67a3b17c 788 (gnus-group-real-name to-newsgroup)))))
e6d2d263 789
71044abe 790(deffoo nnir-request-expire-articles (articles group &optional server force)
f83a656e 791 (nnir-possibly-change-group group server)
3b84b005
G
792 (if force
793 (let ((articles-by-group (nnir-categorize
794 articles nnir-article-group nnir-article-ids))
795 not-deleted)
796 (while (not (null articles-by-group))
797 (let* ((group-articles (pop articles-by-group))
798 (artgroup (car group-articles))
799 (articleids (cadr group-articles))
800 (artlist (sort (mapcar 'cdr articleids) '<)))
801 (unless (gnus-check-backend-function 'request-expire-articles
802 artgroup)
803 (error "The group %s does not support article deletion" artgroup))
804 (unless (gnus-check-server (gnus-find-method-for-group artgroup))
805 (error "Couldn't open server for group %s" artgroup))
806 (push (gnus-request-expire-articles
807 artlist artgroup force)
808 not-deleted)))
809 (sort (delq nil not-deleted) '<))
810 articles))
71044abe 811
4ddab346 812(deffoo nnir-warp-to-article ()
f83a656e 813 (nnir-possibly-change-group gnus-newsgroup-name)
67a3b17c
AC
814 (let* ((cur (if (> (gnus-summary-article-number) 0)
815 (gnus-summary-article-number)
f83a656e 816 (error "Can't warp to a pseudo-article")))
bca46f6b
G
817 (backend-article-group (nnir-article-group cur))
818 (backend-article-number (nnir-article-number cur))
819 (quit-config (gnus-ephemeral-group-p gnus-newsgroup-name)))
f83a656e
AC
820
821 ;; what should we do here? we could leave all the buffers around
822 ;; and assume that we have to exit from them one by one. or we can
823 ;; try to clean up directly
824
825 ;;first exit from the nnir summary buffer.
826; (gnus-summary-exit)
bca46f6b
G
827 ;; and if the nnir summary buffer in turn came from another
828 ;; summary buffer we have to clean that summary up too.
f83a656e
AC
829 ; (when (not (eq (cdr quit-config) 'group))
830; (gnus-summary-exit))
bca46f6b 831 (gnus-summary-read-group-1 backend-article-group t t nil
f83a656e
AC
832 nil (list backend-article-number))))
833
54451ffa
AC
834(deffoo nnir-request-update-mark (group article mark)
835 (let ((artgroup (nnir-article-group article))
836 (artnumber (nnir-article-number article)))
94304aea
LI
837 (when (and artgroup artnumber)
838 (gnus-request-update-mark artgroup artnumber mark))))
54451ffa 839
7d964492 840(deffoo nnir-request-set-mark (group actions &optional server)
35c5bbba 841 (nnir-possibly-change-group group server)
7d964492
AC
842 (let (mlist)
843 (dolist (action actions)
844 (destructuring-bind (range action marks) action
845 (let ((articles-by-group (nnir-categorize
846 (gnus-uncompress-range range)
847 nnir-article-group nnir-article-number)))
848 (dolist (artgroup articles-by-group)
849 (push (list
850 (car artgroup)
851 (list (gnus-compress-sequence
852 (sort (cadr artgroup) '<)) action marks)) mlist)))))
853 (dolist (request (nnir-categorize mlist car cadr))
854 (gnus-request-set-mark (car request) (cadr request)))))
855
f83a656e
AC
856
857(deffoo nnir-request-update-info (group info &optional server)
35c5bbba 858 (nnir-possibly-change-group group server)
7d964492
AC
859 ;; clear out all existing marks.
860 (gnus-info-set-marks info nil)
861 (gnus-info-set-read info nil)
862 (let ((group (gnus-group-guess-full-name-from-command-method group))
863 (articles-by-group
f83a656e 864 (nnir-categorize
7d964492 865 (gnus-uncompress-range (cons 1 (nnir-artlist-length nnir-artlist)))
f83a656e
AC
866 nnir-article-group nnir-article-ids)))
867 (gnus-set-active group
868 (cons 1 (nnir-artlist-length nnir-artlist)))
869 (while (not (null articles-by-group))
870 (let* ((group-articles (pop articles-by-group))
871 (articleids (reverse (cadr group-articles)))
872 (group-info (gnus-get-info (car group-articles)))
873 (marks (gnus-info-marks group-info))
874 (read (gnus-info-read group-info)))
875 (gnus-info-set-read
876 info
877 (gnus-add-to-range
878 (gnus-info-read info)
7d964492
AC
879 (delq nil
880 (mapcar
881 #'(lambda (art)
882 (when (gnus-member-of-range (cdr art) read) (car art)))
883 articleids))))
884 (dolist (mark marks)
885 (destructuring-bind (type . range) mark
f07accae 886 (gnus-add-marked-articles
7d964492
AC
887 group type
888 (delq nil
f07accae 889 (mapcar
7d964492
AC
890 #'(lambda (art)
891 (when (gnus-member-of-range (cdr art) range) (car art)))
892 articleids)))))))))
f83a656e
AC
893
894
895(deffoo nnir-close-group (group &optional server)
f0a97030 896 (nnir-possibly-change-group group server)
1ec75f95 897 (let ((pgroup (gnus-group-guess-full-name-from-command-method group)))
f83a656e
AC
898 (when (and nnir-artlist (not (gnus-ephemeral-group-p pgroup)))
899 (gnus-group-set-parameter pgroup 'nnir-artlist nnir-artlist))
900 (setq nnir-artlist nil)
901 (when (gnus-ephemeral-group-p pgroup)
902 (gnus-kill-ephemeral-group pgroup)
903 (setq gnus-ephemeral-servers
904 (delq (assq 'nnir gnus-ephemeral-servers)
905 gnus-ephemeral-servers)))))
906;; (gnus-opened-servers-remove
907;; (car (assoc '(nnir "nnir-ephemeral" (nnir-address "nnir"))
908;; gnus-opened-servers))))
4ddab346 909
ab9a3f05 910
e6d2d263
MB
911
912
913(defmacro nnir-add-result (dirnam artno score prefix server artlist)
de1765ab 914 "Ask `nnir-compose-result' to construct a result vector,
e6d2d263
MB
915and if it is non-nil, add it to artlist."
916 `(let ((result (nnir-compose-result ,dirnam ,artno ,score ,prefix ,server)))
917 (when (not (null result))
918 (push result ,artlist))))
919
920(autoload 'nnmaildir-base-name-to-article-number "nnmaildir")
921
922;; Helper function currently used by the Swish++ and Namazu backends;
923;; perhaps useful for other backends as well
924(defun nnir-compose-result (dirnam article score prefix server)
925 "Extract the group from dirnam, and create a result vector
926ready to be added to the list of search results."
927
928 ;; remove nnir-*-remove-prefix from beginning of dirnam filename
929 (when (string-match (concat "^" prefix) dirnam)
930 (setq dirnam (replace-match "" t t dirnam)))
931
932 (when (file-readable-p (concat prefix dirnam article))
933 ;; remove trailing slash and, for nnmaildir, cur/new/tmp
934 (setq dirnam
6b958814 935 (substring dirnam 0
f83a656e 936 (if (string-match "\\`nnmaildir:" (gnus-group-server server))
6b958814 937 -5 -1)))
e6d2d263
MB
938
939 ;; Set group to dirnam without any leading dots or slashes,
940 ;; and with all subsequent slashes replaced by dots
941 (let ((group (gnus-replace-in-string
942 (gnus-replace-in-string dirnam "^[./\\]" "" t)
943 "[/\\]" "." t)))
944
ed797193 945 (vector (gnus-group-full-name group server)
f83a656e 946 (if (string-match "\\`nnmaildir:" (gnus-group-server server))
e6d2d263
MB
947 (nnmaildir-base-name-to-article-number
948 (substring article 0 (string-match ":" article))
949 group nil)
950 (string-to-number article))
951 (string-to-number score)))))
952
953;;; Search Engine Interfaces:
954
953d41c4
G
955;; imap interface
956(defun nnir-run-imap (query srv &optional groups)
e6d2d263
MB
957 "Run a search against an IMAP back-end server.
958This uses a custom query language parser; see `nnir-imap-make-query' for
67aa99ff 959details on the language and supported extensions."
e6d2d263
MB
960 (save-excursion
961 (let ((qstring (cdr (assq 'query query)))
953d41c4
G
962 (server (cadr (gnus-server-to-method srv)))
963 (defs (caddr (gnus-server-to-method srv)))
964 (criteria (or (cdr (assq 'criteria query))
965 (cdr (assoc nnir-imap-default-search-key
966 nnir-imap-search-arguments))))
967 (gnus-inhibit-demon t)
c0f9edce 968 (groups (or groups (nnir-get-active srv))))
e6d2d263 969 (message "Opening server %s" server)
953d41c4
G
970 (apply
971 'vconcat
67aa99ff 972 (catch 'found
f83a656e 973 (mapcar
1ec75f95 974 #'(lambda (group)
f83a656e
AC
975 (let (artlist)
976 (condition-case ()
eaa610c3 977 (when (nnimap-change-group
f83a656e
AC
978 (gnus-group-short-name group) server)
979 (with-current-buffer (nnimap-buffer)
980 (message "Searching %s..." group)
981 (let ((arts 0)
982 (result (nnimap-command "UID SEARCH %s"
983 (if (string= criteria "")
984 qstring
985 (nnir-imap-make-query
986 criteria qstring)))))
987 (mapc
988 (lambda (artnum)
989 (let ((artn (string-to-number artnum)))
990 (when (> artn 0)
991 (push (vector group artn 100)
992 artlist)
993 (when (assq 'shortcut query)
994 (throw 'found (list artlist)))
995 (setq arts (1+ arts)))))
996 (and (car result)
997 (cdr (assoc "SEARCH" (cdr result)))))
998 (message "Searching %s... %d matches" group arts)))
999 (message "Searching %s...done" group))
1000 (quit nil))
1001 (nreverse artlist)))
1002 groups))))))
e6d2d263
MB
1003
1004(defun nnir-imap-make-query (criteria qstring)
1005 "Parse the query string and criteria into an appropriate IMAP search
1006expression, returning the string query to make.
1007
1008This implements a little language designed to return the expected results
1009to an arbitrary query string to the end user.
1010
1011The search is always case-insensitive, as defined by RFC2060, and supports
de1765ab 1012the following features (inspired by the Google search input language):
e6d2d263
MB
1013
1014Automatic \"and\" queries
1015 If you specify multiple words then they will be treated as an \"and\"
1016 expression intended to match all components.
1017
1018Phrase searches
1019 If you wrap your query in double-quotes then it will be treated as a
1020 literal string.
1021
1022Negative terms
1023 If you precede a term with \"-\" then it will negate that.
1024
1025\"OR\" queries
1026 If you include an upper-case \"OR\" in your search it will cause the
1027 term before it and the term after it to be treated as alternatives.
1028
1029In future the following will be added to the language:
1030 * support for date matches
1031 * support for location of text matching within the query
1032 * from/to/etc headers
1033 * additional search terms
1034 * flag based searching
1035 * anything else that the RFC supports, basically."
1036 ;; Walk through the query and turn it into an IMAP query string.
1037 (nnir-imap-query-to-imap criteria (nnir-imap-parse-query qstring)))
1038
1039
1040(defun nnir-imap-query-to-imap (criteria query)
1041 "Turn a s-expression format query into IMAP."
1042 (mapconcat
1043 ;; Turn the expressions into IMAP text
1044 (lambda (item)
1045 (nnir-imap-expr-to-imap criteria item))
1046 ;; The query, already in s-expr format.
1047 query
1048 ;; Append a space between each expression
1049 " "))
1050
1051
1052(defun nnir-imap-expr-to-imap (criteria expr)
1053 "Convert EXPR into an IMAP search expression on CRITERIA"
1054 ;; What sort of expression is this, eh?
1055 (cond
1056 ;; Simple string term
1057 ((stringp expr)
dab0271f 1058 (format "%s %S" criteria expr))
e6d2d263
MB
1059 ;; Trivial term: and
1060 ((eq expr 'and) nil)
1061 ;; Composite term: or expression
1062 ((eq (car-safe expr) 'or)
1063 (format "OR %s %s"
1064 (nnir-imap-expr-to-imap criteria (second expr))
1065 (nnir-imap-expr-to-imap criteria (third expr))))
1066 ;; Composite term: just the fax, mam
1067 ((eq (car-safe expr) 'not)
1068 (format "NOT (%s)" (nnir-imap-query-to-imap criteria (rest expr))))
1069 ;; Composite term: just expand it all.
1070 ((and (not (null expr)) (listp expr))
1071 (format "(%s)" (nnir-imap-query-to-imap criteria expr)))
1072 ;; Complex value, give up for now.
1073 (t (error "Unhandled input: %S" expr))))
1074
1075
1076(defun nnir-imap-parse-query (string)
1077 "Turn STRING into an s-expression based query based on the IMAP
1078query language as defined in `nnir-imap-make-query'.
1079
1080This involves turning individual tokens into higher level terms
1081that the search language can then understand and use."
1082 (with-temp-buffer
1083 ;; Set up the parsing environment.
1084 (insert string)
1085 (goto-char (point-min))
1086 ;; Now, collect the output terms and return them.
1087 (let (out)
1088 (while (not (nnir-imap-end-of-input))
1089 (push (nnir-imap-next-expr) out))
1090 (reverse out))))
1091
1092
1093(defun nnir-imap-next-expr (&optional count)
1094 "Return the next expression from the current buffer."
1095 (let ((term (nnir-imap-next-term count))
1096 (next (nnir-imap-peek-symbol)))
1097 ;; Are we looking at an 'or' expression?
1098 (cond
1099 ;; Handle 'expr or expr'
1100 ((eq next 'or)
1101 (list 'or term (nnir-imap-next-expr 2)))
1102 ;; Anything else
1103 (t term))))
1104
1105
1106(defun nnir-imap-next-term (&optional count)
1107 "Return the next TERM from the current buffer."
1108 (let ((term (nnir-imap-next-symbol count)))
1109 ;; What sort of term is this?
1110 (cond
1111 ;; and -- just ignore it
1112 ((eq term 'and) 'and)
1113 ;; negated term
1114 ((eq term 'not) (list 'not (nnir-imap-next-expr)))
1115 ;; generic term
1116 (t term))))
1117
1118
1119(defun nnir-imap-peek-symbol ()
1120 "Return the next symbol from the current buffer, but don't consume it."
1121 (save-excursion
1122 (nnir-imap-next-symbol)))
1123
1124(defun nnir-imap-next-symbol (&optional count)
1125 "Return the next symbol from the current buffer, or nil if we are
1126at the end of the buffer. If supplied COUNT skips some symbols before
1127returning the one at the supplied position."
1128 (when (and (numberp count) (> count 1))
1129 (nnir-imap-next-symbol (1- count)))
1130 (let ((case-fold-search t))
1131 ;; end of input stream?
1132 (unless (nnir-imap-end-of-input)
1133 ;; No, return the next symbol from the stream.
1134 (cond
1135 ;; negated expression -- return it and advance one char.
1136 ((looking-at "-") (forward-char 1) 'not)
1137 ;; quoted string
1138 ((looking-at "\"") (nnir-imap-delimited-string "\""))
1139 ;; list expression -- we parse the content and return this as a list.
1140 ((looking-at "(")
1141 (nnir-imap-parse-query (nnir-imap-delimited-string ")")))
1142 ;; keyword input -- return a symbol version
1143 ((looking-at "\\band\\b") (forward-char 3) 'and)
1144 ((looking-at "\\bor\\b") (forward-char 2) 'or)
1145 ((looking-at "\\bnot\\b") (forward-char 3) 'not)
1146 ;; Simple, boring keyword
1147 (t (let ((start (point))
1148 (end (if (search-forward-regexp "[[:blank:]]" nil t)
1149 (prog1
1150 (match-beginning 0)
1151 ;; unskip if we hit a non-blank terminal character.
1152 (when (string-match "[^[:blank:]]" (match-string 0))
1153 (backward-char 1)))
1154 (goto-char (point-max)))))
1155 (buffer-substring start end)))))))
1156
1157(defun nnir-imap-delimited-string (delimiter)
1158 "Return a delimited string from the current buffer."
1159 (let ((start (point)) end)
1160 (forward-char 1) ; skip the first delimiter.
1161 (while (not end)
1162 (unless (search-forward delimiter nil t)
1163 (error "Unmatched delimited input with %s in query" delimiter))
1164 (let ((here (point)))
1165 (unless (equal (buffer-substring (- here 2) (- here 1)) "\\")
1166 (setq end (point)))))
1167 (buffer-substring (1+ start) (1- end))))
1168
1169(defun nnir-imap-end-of-input ()
1170 "Are we at the end of input?"
1171 (skip-chars-forward "[[:blank:]]")
1172 (looking-at "$"))
de1765ab 1173
e6d2d263
MB
1174
1175;; Swish++ interface.
1176;; -cc- Todo
1177;; Search by
1178;; - group
1179;; Sort by
1180;; - rank (default)
1181;; - article number
1182;; - file size
1183;; - group
1184(defun nnir-run-swish++ (query server &optional group)
1185 "Run QUERY against swish++.
1186Returns a vector of (group name, file name) pairs (also vectors,
1187actually).
1188
1189Tested with swish++ 4.7 on GNU/Linux and with swish++ 5.0b2 on
1190Windows NT 4.0."
1191
953d41c4
G
1192 ;; (when group
1193 ;; (error "The swish++ backend cannot search specific groups"))
e6d2d263
MB
1194
1195 (save-excursion
1196 (let ( (qstring (cdr (assq 'query query)))
f83a656e 1197 (groupspec (cdr (assq 'swish++-group query)))
e6d2d263
MB
1198 (prefix (nnir-read-server-parm 'nnir-swish++-remove-prefix server))
1199 artlist
1200 ;; nnml-use-compressed-files might be any string, but probably this
1201 ;; is sufficient. Note that we can't only use the value of
1202 ;; nnml-use-compressed-files because old articles might have been
1203 ;; saved with a different value.
f83a656e 1204 (article-pattern (if (string-match "\\`nnmaildir:"
bbd6590c 1205 (gnus-group-server server))
e6d2d263
MB
1206 ":[0-9]+"
1207 "^[0-9]+\\(\\.[a-z0-9]+\\)?$"))
1208 score artno dirnam filenam)
1209
1210 (when (equal "" qstring)
d93ec753 1211 (error "swish++: You didn't enter anything"))
e6d2d263
MB
1212
1213 (set-buffer (get-buffer-create nnir-tmp-buffer))
1214 (erase-buffer)
1215
1216 (if groupspec
1217 (message "Doing swish++ query %s on %s..." qstring groupspec)
1218 (message "Doing swish++ query %s..." qstring))
1219
1220 (let* ((cp-list `( ,nnir-swish++-program
1221 nil ; input from /dev/null
1222 t ; output
1223 nil ; don't redisplay
1224 "--config-file" ,(nnir-read-server-parm 'nnir-swish++-configuration-file server)
1225 ,@(nnir-read-server-parm 'nnir-swish++-additional-switches server)
1226 ,qstring ; the query, in swish++ format
1227 ))
1228 (exitstatus
1229 (progn
1230 (message "%s args: %s" nnir-swish++-program
1231 (mapconcat 'identity (cddddr cp-list) " ")) ;; ???
1232 (apply 'call-process cp-list))))
1233 (unless (or (null exitstatus)
1234 (zerop exitstatus))
1235 (nnheader-report 'nnir "Couldn't run swish++: %s" exitstatus)
1236 ;; swish++ failure reason is in this buffer, show it if
1237 ;; the user wants it.
1238 (when (> gnus-verbose 6)
1239 (display-buffer nnir-tmp-buffer))))
1240
1241 ;; The results are output in the format of:
1242 ;; V 4.7 Linux
1243 ;; rank relative-path-name file-size file-title
1244 ;; V 5.0b2:
1245 ;; rank relative-path-name file-size topic??
1246 ;; where rank is an integer from 1 to 100.
1247 (goto-char (point-min))
1248 (while (re-search-forward
1249 "\\(^[0-9]+\\) \\([^ ]+\\) [0-9]+ \\(.*\\)$" nil t)
1250 (setq score (match-string 1)
1251 filenam (match-string 2)
1252 artno (file-name-nondirectory filenam)
1253 dirnam (file-name-directory filenam))
1254
1255 ;; don't match directories
1256 (when (string-match article-pattern artno)
1257 (when (not (null dirnam))
1258
1259 ;; maybe limit results to matching groups.
1260 (when (or (not groupspec)
1261 (string-match groupspec dirnam))
1262 (nnir-add-result dirnam artno score prefix server artlist)))))
1263
1264 (message "Massaging swish++ output...done")
1265
1266 ;; Sort by score
1267 (apply 'vector
ec179403
GM
1268 (sort artlist
1269 (function (lambda (x y)
1270 (> (nnir-artitem-rsv x)
1271 (nnir-artitem-rsv y)))))))))
e6d2d263
MB
1272
1273;; Swish-E interface.
1274(defun nnir-run-swish-e (query server &optional group)
1275 "Run given query against swish-e.
1276Returns a vector of (group name, file name) pairs (also vectors,
1277actually).
1278
1279Tested with swish-e-2.0.1 on Windows NT 4.0."
1280
1281 ;; swish-e crashes with empty parameter to "-w" on commandline...
953d41c4
G
1282 ;; (when group
1283 ;; (error "The swish-e backend cannot search specific groups"))
e6d2d263
MB
1284
1285 (save-excursion
1286 (let ((qstring (cdr (assq 'query query)))
1287 (prefix
1288 (or (nnir-read-server-parm 'nnir-swish-e-remove-prefix server)
1289 (error "Missing parameter `nnir-swish-e-remove-prefix'")))
1290 artlist score artno dirnam group )
1291
1292 (when (equal "" qstring)
d93ec753 1293 (error "swish-e: You didn't enter anything"))
e6d2d263
MB
1294
1295 (set-buffer (get-buffer-create nnir-tmp-buffer))
1296 (erase-buffer)
1297
1298 (message "Doing swish-e query %s..." query)
1299 (let* ((index-files
1300 (or (nnir-read-server-parm
1301 'nnir-swish-e-index-files server)
1302 (error "Missing parameter `nnir-swish-e-index-files'")))
1303 (additional-switches
1304 (nnir-read-server-parm
1305 'nnir-swish-e-additional-switches server))
1306 (cp-list `(,nnir-swish-e-program
1307 nil ; input from /dev/null
1308 t ; output
1309 nil ; don't redisplay
1310 "-f" ,@index-files
1311 ,@additional-switches
1312 "-w"
1313 ,qstring ; the query, in swish-e format
1314 ))
1315 (exitstatus
1316 (progn
1317 (message "%s args: %s" nnir-swish-e-program
1318 (mapconcat 'identity (cddddr cp-list) " "))
1319 (apply 'call-process cp-list))))
1320 (unless (or (null exitstatus)
1321 (zerop exitstatus))
1322 (nnheader-report 'nnir "Couldn't run swish-e: %s" exitstatus)
1323 ;; swish-e failure reason is in this buffer, show it if
1324 ;; the user wants it.
1325 (when (> gnus-verbose 6)
1326 (display-buffer nnir-tmp-buffer))))
1327
1328 ;; The results are output in the format of:
1329 ;; rank path-name file-title file-size
1330 (goto-char (point-min))
1331 (while (re-search-forward
1332 "\\(^[0-9]+\\) \\([^ ]+\\) \"\\([^\"]+\\)\" [0-9]+$" nil t)
1333 (setq score (match-string 1)
1334 artno (match-string 3)
1335 dirnam (file-name-directory (match-string 2)))
1336
1337 ;; don't match directories
1338 (when (string-match "^[0-9]+$" artno)
1339 (when (not (null dirnam))
1340
1341 ;; remove nnir-swish-e-remove-prefix from beginning of dirname
1342 (when (string-match (concat "^" prefix) dirnam)
1343 (setq dirnam (replace-match "" t t dirnam)))
1344
1345 (setq dirnam (substring dirnam 0 -1))
1346 ;; eliminate all ".", "/", "\" from beginning. Always matches.
1347 (string-match "^[./\\]*\\(.*\\)$" dirnam)
1348 ;; "/" -> "."
ec179403 1349 (setq group (gnus-replace-in-string (match-string 1 dirnam) "/" "."))
e6d2d263 1350 ;; Windows "\\" -> "."
ec179403 1351 (setq group (gnus-replace-in-string group "\\\\" "."))
e6d2d263 1352
ed797193 1353 (push (vector (gnus-group-full-name group server)
e6d2d263
MB
1354 (string-to-number artno)
1355 (string-to-number score))
1356 artlist))))
1357
1358 (message "Massaging swish-e output...done")
1359
1360 ;; Sort by score
1361 (apply 'vector
ec179403
GM
1362 (sort artlist
1363 (function (lambda (x y)
1364 (> (nnir-artitem-rsv x)
1365 (nnir-artitem-rsv y)))))))))
e6d2d263 1366
953d41c4
G
1367;; HyREX interface
1368(defun nnir-run-hyrex (query server &optional group)
1369 (save-excursion
1370 (let ((artlist nil)
f83a656e 1371 (groupspec (cdr (assq 'hyrex-group query)))
953d41c4
G
1372 (qstring (cdr (assq 'query query)))
1373 (prefix (nnir-read-server-parm 'nnir-hyrex-remove-prefix server))
1374 score artno dirnam)
1375 (when (and (not groupspec) group)
1376 (setq groupspec
1377 (regexp-opt
1378 (mapcar (lambda (x) (gnus-group-real-name x)) group))))
1379 (set-buffer (get-buffer-create nnir-tmp-buffer))
1380 (erase-buffer)
1381 (message "Doing hyrex-search query %s..." query)
1382 (let* ((cp-list
1383 `( ,nnir-hyrex-program
1384 nil ; input from /dev/null
1385 t ; output
1386 nil ; don't redisplay
1387 "-i",(nnir-read-server-parm 'nnir-hyrex-index-directory server) ; index directory
1388 ,@(nnir-read-server-parm 'nnir-hyrex-additional-switches server)
1389 ,qstring ; the query, in hyrex-search format
1390 ))
1391 (exitstatus
1392 (progn
1393 (message "%s args: %s" nnir-hyrex-program
1394 (mapconcat 'identity (cddddr cp-list) " "))
1395 (apply 'call-process cp-list))))
1396 (unless (or (null exitstatus)
1397 (zerop exitstatus))
1398 (nnheader-report 'nnir "Couldn't run hyrex-search: %s" exitstatus)
1399 ;; nnir-search failure reason is in this buffer, show it if
1400 ;; the user wants it.
1401 (when (> gnus-verbose 6)
fe7a3057 1402 (display-buffer nnir-tmp-buffer)))) ;; FIXME: Don't clear buffer !
953d41c4
G
1403 (message "Doing hyrex-search query \"%s\"...done" qstring)
1404 (sit-for 0)
1405 ;; nnir-search returns:
c80e3b4a
PE
1406 ;; for nnml/nnfolder: "filename mailid weight"
1407 ;; for nnimap: "group mailid weight"
953d41c4
G
1408 (goto-char (point-min))
1409 (delete-non-matching-lines "^\\S + [0-9]+ [0-9]+$")
1410 ;; HyREX doesn't search directly in groups -- so filter out here.
1411 (when groupspec
1412 (keep-lines groupspec))
1413 ;; extract data from result lines
1414 (goto-char (point-min))
1415 (while (re-search-forward
1416 "\\(\\S +\\) \\([0-9]+\\) \\([0-9]+\\)" nil t)
1417 (setq dirnam (match-string 1)
1418 artno (match-string 2)
1419 score (match-string 3))
1420 (when (string-match prefix dirnam)
1421 (setq dirnam (replace-match "" t t dirnam)))
ed797193 1422 (push (vector (gnus-group-full-name
953d41c4
G
1423 (gnus-replace-in-string dirnam "/" ".") server)
1424 (string-to-number artno)
1425 (string-to-number score))
1426 artlist))
1427 (message "Massaging hyrex-search output...done.")
1428 (apply 'vector
1429 (sort artlist
1430 (function (lambda (x y)
1431 (if (string-lessp (nnir-artitem-group x)
1432 (nnir-artitem-group y))
1433 t
1434 (< (nnir-artitem-number x)
1435 (nnir-artitem-number y)))))))
1436 )))
1437
e6d2d263
MB
1438;; Namazu interface
1439(defun nnir-run-namazu (query server &optional group)
1440 "Run given query against Namazu. Returns a vector of (group name, file name)
1441pairs (also vectors, actually).
1442
1443Tested with Namazu 2.0.6 on a GNU/Linux system."
953d41c4
G
1444 ;; (when group
1445 ;; (error "The Namazu backend cannot search specific groups"))
e6d2d263 1446 (save-excursion
f83a656e 1447 (let ((article-pattern (if (string-match "\\`nnmaildir:"
bbd6590c 1448 (gnus-group-server server))
e6d2d263
MB
1449 ":[0-9]+"
1450 "^[0-9]+$"))
1451 artlist
1452 (qstring (cdr (assq 'query query)))
1453 (prefix (nnir-read-server-parm 'nnir-namazu-remove-prefix server))
1454 score group article
1455 (process-environment (copy-sequence process-environment)))
1456 (setenv "LC_MESSAGES" "C")
1457 (set-buffer (get-buffer-create nnir-tmp-buffer))
1458 (erase-buffer)
1459 (let* ((cp-list
1460 `( ,nnir-namazu-program
1461 nil ; input from /dev/null
1462 t ; output
1463 nil ; don't redisplay
1464 "-q" ; don't be verbose
1465 "-a" ; show all matches
1466 "-s" ; use short format
1467 ,@(nnir-read-server-parm 'nnir-namazu-additional-switches server)
1468 ,qstring ; the query, in namazu format
1469 ,(nnir-read-server-parm 'nnir-namazu-index-directory server) ; index directory
1470 ))
1471 (exitstatus
1472 (progn
1473 (message "%s args: %s" nnir-namazu-program
1474 (mapconcat 'identity (cddddr cp-list) " "))
1475 (apply 'call-process cp-list))))
1476 (unless (or (null exitstatus)
1477 (zerop exitstatus))
1478 (nnheader-report 'nnir "Couldn't run namazu: %s" exitstatus)
1479 ;; Namazu failure reason is in this buffer, show it if
1480 ;; the user wants it.
1481 (when (> gnus-verbose 6)
1482 (display-buffer nnir-tmp-buffer))))
1483
1484 ;; Namazu output looks something like this:
1485 ;; 2. Re: Gnus agent expire broken (score: 55)
1486 ;; /home/henrik/Mail/mail/sent/1310 (4,138 bytes)
1487
1488 (goto-char (point-min))
1489 (while (re-search-forward
1490 "^\\([0-9]+\\.\\).*\\((score: \\([0-9]+\\)\\))\n\\([^ ]+\\)"
1491 nil t)
1492 (setq score (match-string 3)
1493 group (file-name-directory (match-string 4))
1494 article (file-name-nondirectory (match-string 4)))
1495
1496 ;; make sure article and group is sane
1497 (when (and (string-match article-pattern article)
1498 (not (null group)))
1499 (nnir-add-result group article score prefix server artlist)))
1500
1501 ;; sort artlist by score
1502 (apply 'vector
ec179403
GM
1503 (sort artlist
1504 (function (lambda (x y)
1505 (> (nnir-artitem-rsv x)
1506 (nnir-artitem-rsv y)))))))))
e6d2d263 1507
d30dd079
G
1508(defun nnir-run-notmuch (query server &optional group)
1509 "Run QUERY against notmuch.
1510Returns a vector of (group name, file name) pairs (also vectors,
1511actually)."
1512
1513 ;; (when group
1514 ;; (error "The notmuch backend cannot search specific groups"))
1515
1516 (save-excursion
1517 (let ( (qstring (cdr (assq 'query query)))
f83a656e 1518 (groupspec (cdr (assq 'notmuch-group query)))
d30dd079
G
1519 (prefix (nnir-read-server-parm 'nnir-notmuch-remove-prefix server))
1520 artlist
f83a656e 1521 (article-pattern (if (string-match "\\`nnmaildir:"
bbd6590c 1522 (gnus-group-server server))
d30dd079
G
1523 ":[0-9]+"
1524 "^[0-9]+$"))
1525 artno dirnam filenam)
1526
1527 (when (equal "" qstring)
1528 (error "notmuch: You didn't enter anything"))
1529
1530 (set-buffer (get-buffer-create nnir-tmp-buffer))
1531 (erase-buffer)
1532
1533 (if groupspec
1534 (message "Doing notmuch query %s on %s..." qstring groupspec)
1535 (message "Doing notmuch query %s..." qstring))
1536
1537 (let* ((cp-list `( ,nnir-notmuch-program
1538 nil ; input from /dev/null
1539 t ; output
1540 nil ; don't redisplay
1541 "search"
1542 "--format=text"
1543 "--output=files"
1544 ,@(nnir-read-server-parm 'nnir-notmuch-additional-switches server)
1545 ,qstring ; the query, in notmuch format
1546 ))
1547 (exitstatus
1548 (progn
1549 (message "%s args: %s" nnir-notmuch-program
1550 (mapconcat 'identity (cddddr cp-list) " ")) ;; ???
1551 (apply 'call-process cp-list))))
1552 (unless (or (null exitstatus)
1553 (zerop exitstatus))
1554 (nnheader-report 'nnir "Couldn't run notmuch: %s" exitstatus)
1555 ;; notmuch failure reason is in this buffer, show it if
1556 ;; the user wants it.
1557 (when (> gnus-verbose 6)
1558 (display-buffer nnir-tmp-buffer))))
1559
1560 ;; The results are output in the format of:
1561 ;; absolute-path-name
1562 (goto-char (point-min))
1563 (while (not (eobp))
1564 (setq filenam (buffer-substring-no-properties (line-beginning-position)
1565 (line-end-position))
1566 artno (file-name-nondirectory filenam)
1567 dirnam (file-name-directory filenam))
1568 (forward-line 1)
1569
1570 ;; don't match directories
1571 (when (string-match article-pattern artno)
1572 (when (not (null dirnam))
1573
1574 ;; maybe limit results to matching groups.
1575 (when (or (not groupspec)
1576 (string-match groupspec dirnam))
1577 (nnir-add-result dirnam artno "" prefix server artlist)))))
1578
1579 (message "Massaging notmuch output...done")
1580
1581 artlist)))
1582
953d41c4 1583(defun nnir-run-find-grep (query server &optional grouplist)
e6d2d263
MB
1584 "Run find and grep to obtain matching articles."
1585 (let* ((method (gnus-server-to-method server))
1586 (sym (intern
1587 (concat (symbol-name (car method)) "-directory")))
1588 (directory (cadr (assoc sym (cddr method))))
1589 (regexp (cdr (assoc 'query query)))
1590 (grep-options (cdr (assoc 'grep-options query)))
f83a656e 1591 (grouplist (or grouplist (nnir-get-active server))))
e6d2d263
MB
1592 (unless directory
1593 (error "No directory found in method specification of server %s"
1594 server))
953d41c4
G
1595 (apply
1596 'vconcat
1597 (mapcar (lambda (x)
f83a656e
AC
1598 (let ((group x)
1599 artlist)
953d41c4
G
1600 (message "Searching %s using find-grep..."
1601 (or group server))
1602 (save-window-excursion
1603 (set-buffer (get-buffer-create nnir-tmp-buffer))
953d41c4
G
1604 (if (> gnus-verbose 6)
1605 (pop-to-buffer (current-buffer)))
1606 (cd directory) ; Using relative paths simplifies
f83a656e 1607 ; postprocessing.
953d41c4
G
1608 (let ((group
1609 (if (not group)
1610 "."
1611 ;; Try accessing the group literally as
1612 ;; well as interpreting dots as directory
1613 ;; separators so the engine works with
1614 ;; plain nnml as well as the Gnus Cache.
1615 (let ((group (gnus-group-real-name group)))
1616 ;; Replace cl-func find-if.
1617 (if (file-directory-p group)
1618 group
1619 (if (file-directory-p
1620 (setq group
1621 (gnus-replace-in-string
1622 group
1623 "\\." "/" t)))
1624 group))))))
1625 (unless group
1626 (error "Cannot locate directory for group"))
1627 (save-excursion
1628 (apply
1629 'call-process "find" nil t
f83a656e
AC
1630 "find" group "-maxdepth" "1" "-type" "f"
1631 "-name" "[0-9]*" "-exec"
953d41c4
G
1632 "grep"
1633 `("-l" ,@(and grep-options
1634 (split-string grep-options "\\s-" t))
1635 "-e" ,regexp "{}" "+"))))
1636
1637 ;; Translate relative paths to group names.
1638 (while (not (eobp))
1639 (let* ((path (split-string
1640 (buffer-substring
1641 (point)
1642 (line-end-position)) "/" t))
1643 (art (string-to-number (car (last path)))))
1644 (while (string= "." (car path))
1645 (setq path (cdr path)))
1646 (let ((group (mapconcat 'identity
1647 ;; Replace cl-func:
1648 ;; (subseq path 0 -1)
1649 (let ((end (1- (length path)))
1650 res)
1651 (while
1652 (>= (setq end (1- end)) 0)
1653 (push (pop path) res))
1654 (nreverse res))
1655 ".")))
1656 (push
ed797193 1657 (vector (gnus-group-full-name group server) art 0)
953d41c4
G
1658 artlist))
1659 (forward-line 1)))
1660 (message "Searching %s using find-grep...done"
1661 (or group server))
1662 artlist)))
1663 grouplist))))
1664
e195d639
GM
1665(declare-function mm-url-insert "mm-url" (url &optional follow-refresh))
1666(declare-function mm-url-encode-www-form-urlencoded "mm-url" (pairs))
1667
953d41c4
G
1668;; gmane interface
1669(defun nnir-run-gmane (query srv &optional groups)
1670 "Run a search against a gmane back-end server."
953d41c4
G
1671 (let* ((case-fold-search t)
1672 (qstring (cdr (assq 'query query)))
1673 (server (cadr (gnus-server-to-method srv)))
47ac6170
AC
1674 (groupspec (mapconcat
1675 (lambda (x)
99e58ed5
AC
1676 (if (gnus-string-match-p "gmane" x)
1677 (format "group:%s" (gnus-group-short-name x))
1678 (error "Can't search non-gmane groups: %s" x)))
1679 groups " "))
953d41c4 1680 (authorspec
f83a656e
AC
1681 (if (assq 'gmane-author query)
1682 (format "author:%s" (cdr (assq 'gmane-author query))) ""))
953d41c4
G
1683 (search (format "%s %s %s"
1684 qstring groupspec authorspec))
2960c86f 1685 (gnus-inhibit-demon t)
953d41c4 1686 artlist)
e195d639 1687 (require 'mm-url)
59e75882 1688 (with-current-buffer (get-buffer-create nnir-tmp-buffer)
953d41c4
G
1689 (erase-buffer)
1690 (mm-url-insert
1691 (concat
1692 "http://search.gmane.org/nov.php"
1693 "?"
1694 (mm-url-encode-www-form-urlencoded
1695 `(("query" . ,search)
1696 ("HITSPERPAGE" . "999")))))
1697 (unless (featurep 'xemacs) (set-buffer-multibyte t))
1698 (mm-decode-coding-region (point-min) (point-max) 'utf-8)
1699 (goto-char (point-min))
1700 (forward-line 1)
1701 (while (not (eobp))
1702 (unless (or (eolp) (looking-at "\x0d"))
1703 (let ((header (nnheader-parse-nov)))
389b76fa
G
1704 (let ((xref (mail-header-xref header))
1705 (xscore (string-to-number (cdr (assoc 'X-Score
1706 (mail-header-extra header))))))
953d41c4
G
1707 (when (string-match " \\([^:]+\\)[:/]\\([0-9]+\\)" xref)
1708 (push
1709 (vector
1710 (gnus-group-prefixed-name (match-string 1 xref) srv)
389b76fa 1711 (string-to-number (match-string 2 xref)) xscore)
953d41c4
G
1712 artlist)))))
1713 (forward-line 1)))
99e58ed5 1714 (apply 'vector (nreverse (mm-delete-duplicates artlist)))))
e6d2d263
MB
1715
1716;;; Util Code:
1717
567d89ca
AC
1718(defun gnus-nnir-group-p (group)
1719 "Say whether GROUP is nnir or not."
1720 (if (gnus-group-prefixed-p group)
1721 (eq 'nnir (car (gnus-find-method-for-group group)))
1722 (and group (string-match "^nnir" group))))
1723
f83a656e 1724(defun nnir-read-parms (nnir-search-engine)
e6d2d263
MB
1725 "Reads additional search parameters according to `nnir-engines'."
1726 (let ((parmspec (caddr (assoc nnir-search-engine nnir-engines))))
f83a656e 1727 (mapcar 'nnir-read-parm parmspec)))
e6d2d263
MB
1728
1729(defun nnir-read-parm (parmspec)
1730 "Reads a single search parameter.
1731`parmspec' is a cons cell, the car is a symbol, the cdr is a prompt."
1732 (let ((sym (car parmspec))
1733 (prompt (cdr parmspec)))
1734 (if (listp prompt)
1736ad36 1735 (let* ((result (apply 'gnus-completing-read prompt))
e6d2d263 1736 (mapping (or (assoc result nnir-imap-search-arguments)
dab0271f 1737 (cons nil nnir-imap-search-other))))
e6d2d263
MB
1738 (cons sym (format (cdr mapping) result)))
1739 (cons sym (read-string prompt)))))
1740
f83a656e
AC
1741(defun nnir-run-query (specs)
1742 "Invoke appropriate search engine function (see `nnir-engines')."
1743 (apply 'vconcat
1744 (mapcar
1745 (lambda (x)
1746 (let* ((server (car x))
1747 (search-engine (nnir-server-to-search-engine server))
1748 (search-func (cadr (assoc search-engine nnir-engines))))
1749 (and search-func
1750 (funcall search-func (cdr (assq 'nnir-query-spec specs))
1751 server (cadr x)))))
1752 (cdr (assq 'nnir-group-spec specs)))))
1753
1754(defun nnir-server-to-search-engine (server)
1755 (or (nnir-read-server-parm 'nnir-search-engine server t)
1756 (cdr (assoc (car (gnus-server-to-method server))
1757 nnir-method-default-engines))))
e6d2d263 1758
19fe0c2e
AC
1759(defun nnir-read-server-parm (key server &optional not-global)
1760 "Returns the parameter value corresponding to `key' for
1761`server'. If no server-specific value is found consult the global
1762environment unless `not-global' is non-nil."
e6d2d263
MB
1763 (let ((method (gnus-server-to-method server)))
1764 (cond ((and method (assq key (cddr method)))
19fe0c2e
AC
1765 (nth 1 (assq key (cddr method))))
1766 ((and (not not-global) (boundp key)) (symbol-value key))
1767 (t nil))))
1768
f83a656e
AC
1769(defun nnir-possibly-change-group (group &optional server)
1770 (or (not server) (nnir-server-opened server) (nnir-open-server server))
567d89ca 1771 (when (gnus-nnir-group-p group)
f83a656e
AC
1772 (setq nnir-artlist (gnus-group-get-parameter
1773 (gnus-group-prefixed-name
1774 (gnus-group-short-name group) '(nnir "nnir"))
1775 'nnir-artlist t))))
e6d2d263 1776
f83a656e
AC
1777(defun nnir-server-opened (&optional server)
1778 (let ((backend (car (gnus-server-to-method server))))
1779 (nnoo-current-server-p (or backend 'nnir) server)))
e6d2d263 1780
47f0b35e 1781(defun nnir-search-thread (header)
f83a656e
AC
1782 "Make an nnir group based on the thread containing the article
1783header. The current server will be searched. If the registry is
1784installed, the server that the registry reports the current
1785article came from is also searched."
1786 (let* ((query
1787 (list (cons 'query (nnimap-make-thread-query header))
1788 (cons 'criteria "")))
1789 (server
1790 (list (list (gnus-method-to-server
1791 (gnus-find-method-for-group gnus-newsgroup-name)))))
1792 (registry-group (and
1793 (gnus-bound-and-true-p 'gnus-registry-enabled)
1794 (car (gnus-registry-get-id-key
1795 (mail-header-id header) 'group))))
1796 (registry-server
1797 (and registry-group
1798 (gnus-method-to-server
1799 (gnus-find-method-for-group registry-group)))))
1800 (when registry-server (add-to-list 'server (list registry-server)))
1801 (gnus-group-make-nnir-group nil (list
1802 (cons 'nnir-query-spec query)
1803 (cons 'nnir-group-spec server)))
bca46f6b 1804 (gnus-summary-goto-subject (gnus-id-to-article (mail-header-id header)))))
e6d2d263 1805
389b76fa
G
1806(defun nnir-get-active (srv)
1807 (let ((method (gnus-server-to-method srv))
1808 groups)
1809 (gnus-request-list method)
1810 (with-current-buffer nntp-server-buffer
1811 (let ((cur (current-buffer))
1812 name)
1813 (goto-char (point-min))
70041e9a
G
1814 (unless (or (null nnir-ignored-newsgroups)
1815 (string= nnir-ignored-newsgroups ""))
ed797193
G
1816 (delete-matching-lines nnir-ignored-newsgroups))
1817 (if (eq (car method) 'nntp)
1818 (while (not (eobp))
1819 (ignore-errors
1820 (push (mm-string-as-unibyte
1821 (gnus-group-full-name
1822 (buffer-substring
1823 (point)
1824 (progn
1825 (skip-chars-forward "^ \t")
1826 (point))) method))
1827 groups))
1828 (forward-line))
1829 (while (not (eobp))
1830 (ignore-errors
1831 (push (mm-string-as-unibyte
1832 (if (eq (char-after) ?\")
1833 (gnus-group-full-name (read cur) method)
1834 (let ((p (point)) (name ""))
1835 (skip-chars-forward "^ \t\\\\")
1836 (setq name (buffer-substring p (point)))
1837 (while (eq (char-after) ?\\)
1838 (setq p (1+ (point)))
1839 (forward-char 2)
1840 (skip-chars-forward "^ \t\\\\")
1841 (setq name (concat name (buffer-substring
1842 p (point)))))
1843 (gnus-group-full-name name method))))
1844 groups))
1845 (forward-line)))))
389b76fa
G
1846 groups))
1847
ed797193
G
1848(defun nnir-registry-action (action data-header from &optional to method)
1849 "Call `gnus-registry-action' with the original article group."
1850 (gnus-registry-action
1851 action
1852 data-header
1853 (nnir-article-group (mail-header-number data-header))
1854 to
1855 method))
1856
1857(defun nnir-mode ()
1858 (when (eq (car (gnus-find-method-for-group gnus-newsgroup-name)) 'nnir)
1859 (setq gnus-summary-line-format
1860 (or nnir-summary-line-format gnus-summary-line-format))
b7351677 1861 (when (gnus-bound-and-true-p 'gnus-registry-enabled)
27625a58
AC
1862 (remove-hook 'gnus-summary-article-delete-hook 'gnus-registry-action t)
1863 (remove-hook 'gnus-summary-article-move-hook 'gnus-registry-action t)
1864 (remove-hook 'gnus-summary-article-expire-hook 'gnus-registry-action t)
1865 (add-hook 'gnus-summary-article-delete-hook 'nnir-registry-action t t)
1866 (add-hook 'gnus-summary-article-move-hook 'nnir-registry-action t t)
1867 (add-hook 'gnus-summary-article-expire-hook 'nnir-registry-action t t))))
ed797193
G
1868
1869
d406cffa
AC
1870(defun gnus-summary-create-nnir-group ()
1871 (interactive)
1ec75f95 1872 (or (nnir-server-opened "") (nnir-open-server "nnir"))
d406cffa 1873 (let ((name (gnus-read-group "Group name: "))
1ec75f95
AC
1874 (method '(nnir ""))
1875 (pgroup
1876 (gnus-group-guess-full-name-from-command-method gnus-newsgroup-name)))
d406cffa
AC
1877 (with-current-buffer gnus-group-buffer
1878 (gnus-group-make-group
1879 name method nil
1880 (gnus-group-find-parameter pgroup)))))
1881
ed797193 1882
f83a656e
AC
1883(deffoo nnir-request-create-group (group &optional server args)
1884 (message "Creating nnir group %s" group)
d406cffa 1885 (let* ((group (gnus-group-prefixed-name group '(nnir "nnir")))
1ec75f95 1886 (specs (assq 'nnir-specs args))
d406cffa 1887 (query-spec
1ec75f95 1888 (or (cdr (assq 'nnir-query-spec specs))
d406cffa
AC
1889 (list (cons 'query
1890 (read-string "Query: " nil 'nnir-search-history)))))
1891 (group-spec
1ec75f95 1892 (or (cdr (assq 'nnir-group-spec specs))
d406cffa
AC
1893 (list (list (read-string "Server: " nil nil)))))
1894 (nnir-specs (list (cons 'nnir-query-spec query-spec)
1895 (cons 'nnir-group-spec group-spec))))
1896 (gnus-group-set-parameter group 'nnir-specs nnir-specs)
f83a656e
AC
1897 (gnus-group-set-parameter
1898 group 'nnir-artlist
1ec75f95 1899 (or (cdr (assq 'nnir-artlist args))
d406cffa 1900 (nnir-run-query nnir-specs)))
f83a656e
AC
1901 (nnir-request-update-info group (gnus-get-info group)))
1902 t)
1903
1904(deffoo nnir-request-delete-group (group &optional force server)
1905 t)
1906
1907(deffoo nnir-request-list (&optional server)
1908 t)
1909
1910(deffoo nnir-request-scan (group method)
7d964492 1911 t)
f83a656e 1912
ab9a3f05
AC
1913(deffoo nnir-request-close ()
1914 t)
1915
ab9a3f05 1916(nnoo-define-skeleton nnir)
f83a656e 1917
e6d2d263
MB
1918;; The end.
1919(provide 'nnir)
1920
de1765ab 1921;;; nnir.el ends here