nnir.el 62 KB

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