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