nnvirtual.el 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805
  1. ;;; nnvirtual.el --- virtual newsgroups access for Gnus
  2. ;; Copyright (C) 1994-2015 Free Software Foundation, Inc.
  3. ;; Author: David Moore <dmoore@ucsd.edu>
  4. ;; Lars Magne Ingebrigtsen <larsi@gnus.org>
  5. ;; Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
  6. ;; Keywords: news
  7. ;; This file is part of GNU Emacs.
  8. ;; GNU Emacs is free software: you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;; The other access methods (nntp, nnspool, etc) are general news
  20. ;; access methods. This module relies on Gnus and can not be used
  21. ;; separately.
  22. ;;; Code:
  23. (require 'nntp)
  24. (require 'nnheader)
  25. (require 'gnus)
  26. (require 'nnoo)
  27. (require 'gnus-util)
  28. (require 'gnus-start)
  29. (require 'gnus-sum)
  30. (require 'gnus-msg)
  31. (eval-when-compile (require 'cl))
  32. (nnoo-declare nnvirtual)
  33. (defvoo nnvirtual-always-rescan t
  34. "If non-nil, always scan groups for unread articles when entering a group.
  35. If this variable is nil and you read articles in a component group
  36. after the virtual group has been activated, the read articles from the
  37. component group will show up when you enter the virtual group.")
  38. (defvoo nnvirtual-component-regexp nil
  39. "Regexp to match component groups.")
  40. (defvoo nnvirtual-component-groups nil
  41. "Component group in this nnvirtual group.")
  42. (defconst nnvirtual-version "nnvirtual 1.1")
  43. (defvoo nnvirtual-current-group nil)
  44. (defvoo nnvirtual-mapping-table nil
  45. "Table of rules on how to map between component group and article number to virtual article number.")
  46. (defvoo nnvirtual-mapping-offsets nil
  47. "Table indexed by component group to an offset to be applied to article numbers in that group.")
  48. (defvoo nnvirtual-mapping-len 0
  49. "Number of articles in this virtual group.")
  50. (defvoo nnvirtual-mapping-reads nil
  51. "Compressed sequence of read articles on the virtual group as computed from the unread status of individual component groups.")
  52. (defvoo nnvirtual-mapping-marks nil
  53. "Compressed marks alist for the virtual group as computed from the marks of individual component groups.")
  54. (defvoo nnvirtual-info-installed nil
  55. "T if we have already installed the group info for this group, and shouldn't blast over it again.")
  56. (defvoo nnvirtual-status-string "")
  57. (autoload 'gnus-cache-articles-in-group "gnus-cache")
  58. ;;; Interface functions.
  59. (nnoo-define-basics nnvirtual)
  60. (deffoo nnvirtual-retrieve-headers (articles &optional newsgroup
  61. server fetch-old)
  62. (when (nnvirtual-possibly-change-server server)
  63. (with-current-buffer nntp-server-buffer
  64. (erase-buffer)
  65. (if (stringp (car articles))
  66. 'headers
  67. (let ((vbuf (nnheader-set-temp-buffer
  68. (get-buffer-create " *virtual headers*")))
  69. (carticles (nnvirtual-partition-sequence articles))
  70. (sysname (system-name))
  71. cgroup carticle article result prefix)
  72. (while carticles
  73. (setq cgroup (caar carticles))
  74. (setq articles (cdar carticles))
  75. (pop carticles)
  76. (when (and articles
  77. (gnus-check-server
  78. (gnus-find-method-for-group cgroup) t)
  79. (gnus-request-group cgroup t)
  80. (setq prefix (gnus-group-real-prefix cgroup))
  81. ;; FIX FIX FIX we want to check the cache!
  82. ;; This is probably evil if people have set
  83. ;; gnus-use-cache to nil themselves, but I
  84. ;; have no way of finding the true value of it.
  85. (let ((gnus-use-cache t))
  86. (setq result (gnus-retrieve-headers
  87. articles cgroup nil))))
  88. (set-buffer nntp-server-buffer)
  89. ;; If we got HEAD headers, we convert them into NOV
  90. ;; headers. This is slow, inefficient and, come to think
  91. ;; of it, downright evil. So sue me. I couldn't be
  92. ;; bothered to write a header parse routine that could
  93. ;; parse a mixed HEAD/NOV buffer.
  94. (when (eq result 'headers)
  95. (nnvirtual-convert-headers))
  96. (goto-char (point-min))
  97. (while (not (eobp))
  98. (delete-region (point)
  99. (progn
  100. (setq carticle (read nntp-server-buffer))
  101. (point)))
  102. ;; We remove this article from the articles list, if
  103. ;; anything is left in the articles list after going through
  104. ;; the entire buffer, then those articles have been
  105. ;; expired or canceled, so we appropriately update the
  106. ;; component group below. They should be coming up
  107. ;; generally in order, so this shouldn't be slow.
  108. (setq articles (delq carticle articles))
  109. (setq article (nnvirtual-reverse-map-article cgroup carticle))
  110. (if (null article)
  111. ;; This line has no reverse mapping, that means it
  112. ;; was an extra article reference returned by nntp.
  113. (progn
  114. (beginning-of-line)
  115. (delete-region (point) (progn (forward-line 1) (point))))
  116. ;; Otherwise insert the virtual article number,
  117. ;; and clean up the xrefs.
  118. (princ article nntp-server-buffer)
  119. (nnvirtual-update-xref-header cgroup carticle
  120. prefix sysname)
  121. (forward-line 1))
  122. )
  123. (set-buffer vbuf)
  124. (goto-char (point-max))
  125. (insert-buffer-substring nntp-server-buffer))
  126. ;; Anything left in articles is expired or canceled.
  127. ;; Could be smart and not tell it about articles already known?
  128. (when articles
  129. (gnus-group-make-articles-read cgroup articles))
  130. )
  131. ;; The headers are ready for reading, so they are inserted into
  132. ;; the nntp-server-buffer, which is where Gnus expects to find
  133. ;; them.
  134. (prog1
  135. (with-current-buffer nntp-server-buffer
  136. (erase-buffer)
  137. (insert-buffer-substring vbuf)
  138. ;; FIX FIX FIX, we should be able to sort faster than
  139. ;; this if needed, since each cgroup is sorted, we just
  140. ;; need to merge
  141. (sort-numeric-fields 1 (point-min) (point-max))
  142. 'nov)
  143. (kill-buffer vbuf)))))))
  144. (defvoo nnvirtual-last-accessed-component-group nil)
  145. (deffoo nnvirtual-request-article (article &optional group server buffer)
  146. (when (nnvirtual-possibly-change-server server)
  147. (if (stringp article)
  148. ;; This is a fetch by Message-ID.
  149. (cond
  150. ((not nnvirtual-last-accessed-component-group)
  151. (nnheader-report
  152. 'nnvirtual "Don't know what server to request from"))
  153. (t
  154. (save-excursion
  155. (when buffer
  156. (set-buffer buffer))
  157. (let* ((gnus-override-method nil)
  158. (gnus-command-method
  159. (gnus-find-method-for-group
  160. nnvirtual-last-accessed-component-group)))
  161. (funcall (gnus-get-function gnus-command-method 'request-article)
  162. article nil (nth 1 gnus-command-method) buffer)))))
  163. ;; This is a fetch by number.
  164. (let* ((amap (nnvirtual-map-article article))
  165. (cgroup (car amap)))
  166. (cond
  167. ((not amap)
  168. (nnheader-report 'nnvirtual "No such article: %s" article))
  169. ((not (gnus-check-group cgroup))
  170. (nnheader-report
  171. 'nnvirtual "Can't open server where %s exists" cgroup))
  172. ((not (gnus-request-group cgroup t))
  173. (nnheader-report 'nnvirtual "Can't open component group %s" cgroup))
  174. (t
  175. (setq nnvirtual-last-accessed-component-group cgroup)
  176. (if buffer
  177. (with-current-buffer buffer
  178. ;; We bind this here to avoid double decoding.
  179. (let ((gnus-article-decode-hook nil))
  180. (gnus-request-article-this-buffer (cdr amap) cgroup)))
  181. (gnus-request-article (cdr amap) cgroup))))))))
  182. (deffoo nnvirtual-open-server (server &optional defs)
  183. (unless (assq 'nnvirtual-component-regexp defs)
  184. (push `(nnvirtual-component-regexp ,server)
  185. defs))
  186. (nnoo-change-server 'nnvirtual server defs)
  187. (if nnvirtual-component-groups
  188. t
  189. (setq nnvirtual-mapping-table nil
  190. nnvirtual-mapping-offsets nil
  191. nnvirtual-mapping-len 0
  192. nnvirtual-mapping-reads nil
  193. nnvirtual-mapping-marks nil
  194. nnvirtual-info-installed nil)
  195. (when nnvirtual-component-regexp
  196. ;; Go through the newsrc alist and find all component groups.
  197. (let ((newsrc (cdr gnus-newsrc-alist))
  198. group)
  199. (while (setq group (car (pop newsrc)))
  200. (when (string-match nnvirtual-component-regexp group) ; Match
  201. ;; Add this group to the list of component groups.
  202. (setq nnvirtual-component-groups
  203. (cons group (delete group nnvirtual-component-groups)))))))
  204. (if (not nnvirtual-component-groups)
  205. (nnheader-report 'nnvirtual "No component groups: %s" server)
  206. t)))
  207. (deffoo nnvirtual-request-group (group &optional server dont-check info)
  208. (nnvirtual-possibly-change-server server)
  209. (setq nnvirtual-component-groups
  210. (delete (nnvirtual-current-group) nnvirtual-component-groups))
  211. (cond
  212. ((null nnvirtual-component-groups)
  213. (setq nnvirtual-current-group nil)
  214. (nnheader-report 'nnvirtual "No component groups in %s" group))
  215. (t
  216. (setq nnvirtual-current-group group)
  217. (nnvirtual-create-mapping dont-check)
  218. (when nnvirtual-always-rescan
  219. (nnvirtual-request-update-info
  220. (nnvirtual-current-group)
  221. (gnus-get-info (nnvirtual-current-group))))
  222. (nnheader-insert "211 %d 1 %d %s\n"
  223. nnvirtual-mapping-len nnvirtual-mapping-len group))))
  224. (deffoo nnvirtual-request-type (group &optional article)
  225. (if (not article)
  226. 'unknown
  227. (if (numberp article)
  228. (let ((mart (nnvirtual-map-article article)))
  229. (if mart
  230. (gnus-request-type (car mart) (cdr mart))))
  231. (gnus-request-type
  232. nnvirtual-last-accessed-component-group nil))))
  233. (deffoo nnvirtual-request-update-mark (group article mark)
  234. (let* ((nart (nnvirtual-map-article article))
  235. (cgroup (car nart)))
  236. (when (and nart
  237. (memq mark gnus-auto-expirable-marks)
  238. ;; The component group might be a virtual group.
  239. (= mark (gnus-request-update-mark cgroup (cdr nart) mark))
  240. (gnus-group-auto-expirable-p cgroup))
  241. (setq mark gnus-expirable-mark)))
  242. mark)
  243. (deffoo nnvirtual-close-group (group &optional server)
  244. (when (and (nnvirtual-possibly-change-server server)
  245. (not (gnus-ephemeral-group-p (nnvirtual-current-group))))
  246. (nnvirtual-update-read-and-marked t t))
  247. t)
  248. (deffoo nnvirtual-request-newgroups (date &optional server)
  249. (nnheader-report 'nnvirtual "NEWGROUPS is not supported."))
  250. (deffoo nnvirtual-request-list-newsgroups (&optional server)
  251. (nnheader-report 'nnvirtual "LIST NEWSGROUPS is not implemented."))
  252. (deffoo nnvirtual-request-update-info (group info &optional server)
  253. (when (and (nnvirtual-possibly-change-server server)
  254. (not nnvirtual-info-installed))
  255. ;; Install the precomputed lists atomically, so the virtual group
  256. ;; is not left in a half-way state in case of C-g.
  257. (gnus-atomic-progn
  258. (setcar (cddr info) nnvirtual-mapping-reads)
  259. (if (nthcdr 3 info)
  260. (setcar (nthcdr 3 info) nnvirtual-mapping-marks)
  261. (when nnvirtual-mapping-marks
  262. (setcdr (nthcdr 2 info) (list nnvirtual-mapping-marks))))
  263. (setq nnvirtual-info-installed t))
  264. t))
  265. (deffoo nnvirtual-catchup-group (group &optional server all)
  266. (when (and (nnvirtual-possibly-change-server server)
  267. (not (gnus-ephemeral-group-p (nnvirtual-current-group))))
  268. ;; copy over existing marks first, in case they set anything
  269. (nnvirtual-update-read-and-marked nil nil)
  270. ;; do a catchup on all component groups
  271. (let ((gnus-group-marked (copy-sequence nnvirtual-component-groups))
  272. (gnus-expert-user t))
  273. ;; Make sure all groups are activated.
  274. (mapc
  275. (lambda (g)
  276. (when (not (numberp (gnus-group-unread g)))
  277. (gnus-activate-group g)))
  278. nnvirtual-component-groups)
  279. (with-current-buffer gnus-group-buffer
  280. (gnus-group-catchup-current nil all)))))
  281. (deffoo nnvirtual-find-group-art (group article)
  282. "Return the real group and article for virtual GROUP and ARTICLE."
  283. (nnvirtual-map-article article))
  284. (deffoo nnvirtual-request-post (&optional server)
  285. (if (not gnus-message-group-art)
  286. (nnheader-report 'nnvirtual "Can't post to an nnvirtual group")
  287. (let ((group (car (nnvirtual-find-group-art
  288. (car gnus-message-group-art)
  289. (cdr gnus-message-group-art)))))
  290. (gnus-request-post (gnus-find-method-for-group group)))))
  291. (deffoo nnvirtual-request-expire-articles (articles group
  292. &optional server force)
  293. (nnvirtual-possibly-change-server server)
  294. (setq nnvirtual-component-groups
  295. (delete (nnvirtual-current-group) nnvirtual-component-groups))
  296. (let (unexpired)
  297. (dolist (group nnvirtual-component-groups)
  298. (setq unexpired (nconc unexpired
  299. (mapcar
  300. #'(lambda (article)
  301. (nnvirtual-reverse-map-article
  302. group article))
  303. (gnus-uncompress-range
  304. (gnus-group-expire-articles-1 group))))))
  305. (sort (delq nil unexpired) '<)))
  306. ;;; Internal functions.
  307. (defun nnvirtual-convert-headers ()
  308. "Convert HEAD headers into NOV headers."
  309. (with-current-buffer nntp-server-buffer
  310. (let* ((dependencies (make-vector 100 0))
  311. (headers (gnus-get-newsgroup-headers dependencies)))
  312. (erase-buffer)
  313. (mapc 'nnheader-insert-nov headers))))
  314. (defun nnvirtual-update-xref-header (group article prefix sysname)
  315. "Edit current NOV header in current buffer to have an xref to the component group, and also server prefix any existing xref lines."
  316. ;; Move to beginning of Xref field, creating a slot if needed.
  317. (beginning-of-line)
  318. (looking-at
  319. "[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t")
  320. (goto-char (match-end 0))
  321. (unless (search-forward "\t" (point-at-eol) 'move)
  322. (insert "\t"))
  323. ;; Remove any spaces at the beginning of the Xref field.
  324. (while (eq (char-after (1- (point))) ? )
  325. (forward-char -1)
  326. (delete-char 1))
  327. (insert "Xref: " sysname " " group ":")
  328. (princ article (current-buffer))
  329. (insert " ")
  330. ;; If there were existing xref lines, clean them up to have the correct
  331. ;; component server prefix.
  332. (save-restriction
  333. (narrow-to-region (point)
  334. (or (search-forward "\t" (point-at-eol) t)
  335. (point-at-eol)))
  336. (goto-char (point-min))
  337. (when (re-search-forward "Xref: *[^\n:0-9 ]+ *" nil t)
  338. (replace-match "" t t))
  339. (goto-char (point-min))
  340. (when (re-search-forward
  341. (concat (regexp-quote (gnus-group-real-name group)) ":[0-9]+")
  342. nil t)
  343. (replace-match "" t t))
  344. (unless (eobp)
  345. (insert " ")
  346. (when (not (string= "" prefix))
  347. (while (re-search-forward "[^ ]+:[0-9]+" nil t)
  348. (save-excursion
  349. (goto-char (match-beginning 0))
  350. (insert prefix))))))
  351. ;; Ensure a trailing \t.
  352. (end-of-line)
  353. (or (eq (char-after (1- (point))) ?\t)
  354. (insert ?\t)))
  355. (defun nnvirtual-possibly-change-server (server)
  356. (or (not server)
  357. (nnoo-current-server-p 'nnvirtual server)
  358. (nnvirtual-open-server server)))
  359. (defun nnvirtual-update-read-and-marked (read-p update-p)
  360. "Copy marks from the virtual group to the component groups.
  361. If READ-P is not nil, update the (un)read status of the components.
  362. If UPDATE-P is not nil, call gnus-group-update-group on the components."
  363. (when nnvirtual-current-group
  364. (let ((unreads (and read-p
  365. (nnvirtual-partition-sequence
  366. (gnus-list-of-unread-articles
  367. (nnvirtual-current-group)))))
  368. (type-marks
  369. (delq nil
  370. (mapcar (lambda (ml)
  371. (if (eq (car ml) 'score)
  372. nil
  373. (cons (car ml)
  374. (nnvirtual-partition-sequence (cdr ml)))))
  375. (gnus-info-marks (gnus-get-info
  376. (nnvirtual-current-group))))))
  377. type groups info)
  378. ;; Ok, atomically move all of the (un)read info, clear any old
  379. ;; marks, and move all of the current marks. This way if someone
  380. ;; hits C-g, you won't leave the component groups in a half-way state.
  381. (progn
  382. ;; move (un)read
  383. ;; bind for workaround guns-update-read-articles
  384. (let ((gnus-newsgroup-active nil))
  385. (dolist (entry unreads)
  386. (gnus-update-read-articles (car entry) (cdr entry))))
  387. ;; clear all existing marks on the component groups
  388. (dolist (group nnvirtual-component-groups)
  389. (when (and (setq info (gnus-get-info group))
  390. (gnus-info-marks info))
  391. (gnus-info-set-marks
  392. info
  393. (if (assq 'score (gnus-info-marks info))
  394. (list (assq 'score (gnus-info-marks info)))
  395. nil))))
  396. ;; Ok, currently type-marks is an assq list with keys of a mark type,
  397. ;; with data of an assq list with keys of component group names
  398. ;; and the articles which correspond to that key/group pair.
  399. (dolist (mark type-marks)
  400. (setq type (car mark))
  401. (setq groups (cdr mark))
  402. (dolist (carticles groups)
  403. (gnus-add-marked-articles (car carticles) type (cdr carticles)
  404. nil t))))
  405. ;; possibly update the display, it is really slow
  406. (when update-p
  407. (dolist (group nnvirtual-component-groups)
  408. (gnus-group-update-group group t))))))
  409. (defun nnvirtual-current-group ()
  410. "Return the prefixed name of the current nnvirtual group."
  411. (concat "nnvirtual:" nnvirtual-current-group))
  412. ;;; This is currently O(kn^2) to merge n lists of length k.
  413. ;;; You could do it in O(knlogn), but we have a small n, and the
  414. ;;; overhead of the other approach is probably greater.
  415. (defun nnvirtual-merge-sorted-lists (&rest lists)
  416. "Merge many sorted lists of numbers."
  417. (if (null (cdr lists))
  418. (car lists)
  419. (sort (apply 'nconc lists) '<)))
  420. ;;; We map between virtual articles and real articles in a manner
  421. ;;; which keeps the size of the virtual active list the same as the
  422. ;;; sum of the component active lists.
  423. ;;; To achieve fair mixing of the groups, the last article in each of
  424. ;;; N component groups will be in the last N articles in the virtual
  425. ;;; group.
  426. ;;; If you have 3 components A, B and C, with articles 1-8, 1-5, and
  427. ;;; 6-7 respectively, then the virtual article numbers look like:
  428. ;;;
  429. ;;; 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
  430. ;;; A1 A2 A3 A4 B1 A5 B2 A6 B3 A7 B4 C6 A8 B5 C7
  431. ;;; To compute these mappings we generate a couple tables and then
  432. ;;; do some fast operations on them. Tables for the example above:
  433. ;;;
  434. ;;; Offsets - [(A 0) (B -3) (C -1)]
  435. ;;;
  436. ;;; a b c d e
  437. ;;; Mapping - ([ 3 0 1 3 0 ]
  438. ;;; [ 6 3 2 9 3 ]
  439. ;;; [ 8 6 3 15 9 ])
  440. ;;;
  441. ;;; (note column 'e' is different in real algorithm, which is slightly
  442. ;;; different than described here, but this gives you the methodology.)
  443. ;;;
  444. ;;; The basic idea is this, when going from component->virtual, apply
  445. ;;; the appropriate offset to the article number. Then search the first
  446. ;;; column of the table for a row where 'a' is less than or equal to the
  447. ;;; modified number. You can see that only group A can therefore go to
  448. ;;; the first row, groups A and B to the second, and all to the last.
  449. ;;; The third column of the table is telling us the number of groups
  450. ;;; which might be able to reach that row (it might increase by more than
  451. ;;; 1 if several groups have the same size).
  452. ;;; Then column 'b' provides an additional offset you apply when you have
  453. ;;; found the correct row. You then multiply by 'c' and add on the groups
  454. ;;; _position_ in the offset table. The basic idea here is that on
  455. ;;; any given row we are going to map back and forth using X'=X*c+Y and
  456. ;;; X=(X'/c), Y=(X' mod c). Then once you've done this transformation,
  457. ;;; you apply a final offset from column 'e' to give the virtual article.
  458. ;;;
  459. ;;; Going the other direction, you instead search on column 'd' instead
  460. ;;; of 'a', and apply everything in reverse order.
  461. ;;; Convert component -> virtual:
  462. ;;; set num = num - Offset(group)
  463. ;;; find first row in Mapping where num <= 'a'
  464. ;;; num = (num-'b')*c + Position(group) + 'e'
  465. ;;; Convert virtual -> component:
  466. ;;; find first row in Mapping where num <= 'd'
  467. ;;; num = num - 'e'
  468. ;;; group_pos = num mod 'c'
  469. ;;; num = (num / 'c') + 'b' + Offset(group_pos)
  470. ;;; Easy no? :)
  471. ;;;
  472. ;;; Well actually, you need to keep column e offset smaller by the 'c'
  473. ;;; column for that line, and always add 1 more when going from
  474. ;;; component -> virtual. Otherwise you run into a problem with
  475. ;;; unique reverse mapping.
  476. (defun nnvirtual-map-article (article)
  477. "Return a cons of the component group and article corresponding to the given virtual ARTICLE."
  478. (let ((table nnvirtual-mapping-table)
  479. entry group-pos)
  480. (while (and table
  481. (> article (aref (car table) 3)))
  482. (setq table (cdr table)))
  483. (when (and table
  484. (> article 0))
  485. (setq entry (car table))
  486. (setq article (- article (aref entry 4) 1))
  487. (setq group-pos (mod article (aref entry 2)))
  488. (cons (car (aref nnvirtual-mapping-offsets group-pos))
  489. (+ (/ article (aref entry 2))
  490. (aref entry 1)
  491. (cdr (aref nnvirtual-mapping-offsets group-pos)))
  492. ))
  493. ))
  494. (defun nnvirtual-reverse-map-article (group article)
  495. "Return the virtual article number corresponding to the given component GROUP and ARTICLE."
  496. (when (numberp article)
  497. (let ((table nnvirtual-mapping-table)
  498. (group-pos 0)
  499. entry)
  500. (while (not (string= group (car (aref nnvirtual-mapping-offsets
  501. group-pos))))
  502. (setq group-pos (1+ group-pos)))
  503. (setq article (- article (cdr (aref nnvirtual-mapping-offsets
  504. group-pos))))
  505. (while (and table
  506. (> article (aref (car table) 0)))
  507. (setq table (cdr table)))
  508. (setq entry (car table))
  509. (when (and entry
  510. (> article 0)
  511. (< group-pos (aref entry 2))) ; article not out of range below
  512. (+ (aref entry 4)
  513. group-pos
  514. (* (- article (aref entry 1))
  515. (aref entry 2))
  516. 1))
  517. )))
  518. (defsubst nnvirtual-reverse-map-sequence (group articles)
  519. "Return list of virtual article numbers for all ARTICLES in GROUP.
  520. The ARTICLES should be sorted, and can be a compressed sequence.
  521. If any of the article numbers has no corresponding virtual article,
  522. then it is left out of the result."
  523. (when (numberp (cdr-safe articles))
  524. (setq articles (list articles)))
  525. (let (result a i j new-a)
  526. (while (setq a (pop articles))
  527. (if (atom a)
  528. (setq i a
  529. j a)
  530. (setq i (car a)
  531. j (cdr a)))
  532. (while (<= i j)
  533. ;; If this is slow, you can optimize by moving article checking
  534. ;; into here. You don't have to recompute the group-pos,
  535. ;; nor scan the table every time.
  536. (when (setq new-a (nnvirtual-reverse-map-article group i))
  537. (push new-a result))
  538. (setq i (1+ i))))
  539. (nreverse result)))
  540. (defun nnvirtual-partition-sequence (articles)
  541. "Return an association list of component article numbers.
  542. These are indexed by elements of nnvirtual-component-groups, based on
  543. the sequence ARTICLES of virtual article numbers. ARTICLES should be
  544. sorted, and can be a compressed sequence. If any of the article
  545. numbers has no corresponding component article, then it is left out of
  546. the result."
  547. (when (numberp (cdr-safe articles))
  548. (setq articles (list articles)))
  549. (let ((carticles (mapcar 'list nnvirtual-component-groups))
  550. a i j article entry)
  551. (while (setq a (pop articles))
  552. (if (atom a)
  553. (setq i a
  554. j a)
  555. (setq i (car a)
  556. j (cdr a)))
  557. (while (<= i j)
  558. (when (setq article (nnvirtual-map-article i))
  559. (setq entry (assoc (car article) carticles))
  560. (setcdr entry (cons (cdr article) (cdr entry))))
  561. (setq i (1+ i))))
  562. (mapc (lambda (x) (setcdr x (nreverse (cdr x))))
  563. carticles)
  564. carticles))
  565. (defun nnvirtual-create-mapping (dont-check)
  566. "Build the tables necessary to map between component (group, article) to virtual article.
  567. Generate the set of read messages and marks for the virtual group
  568. based on the marks on the component groups."
  569. (let ((cnt 0)
  570. (tot 0)
  571. (M 0)
  572. (i 0)
  573. actives all-unreads all-marks
  574. active min max size unreads marks
  575. next-M next-tot
  576. reads beg)
  577. ;; Ok, we loop over all component groups and collect a lot of
  578. ;; information:
  579. ;; Into actives we place (g size max), where size is max-min+1.
  580. ;; Into all-unreads we put (g unreads).
  581. ;; Into all-marks we put (g marks).
  582. ;; We also increment cnt and tot here, and compute M (max of sizes).
  583. (mapc (lambda (g)
  584. (setq active (or (and dont-check
  585. (gnus-active g))
  586. (gnus-activate-group g))
  587. min (car active)
  588. max (cdr active))
  589. (when (and active (>= max min) (not (zerop max)))
  590. ;; store active information
  591. (push (list g (- max min -1) max) actives)
  592. ;; collect unread/mark info for later
  593. (setq unreads (gnus-list-of-unread-articles g))
  594. (setq marks (gnus-info-marks (gnus-get-info g)))
  595. (when gnus-use-cache
  596. (push (cons 'cache
  597. (gnus-cache-articles-in-group g))
  598. marks))
  599. (push (cons g unreads) all-unreads)
  600. (push (cons g marks) all-marks)
  601. ;; count groups, total #articles, and max size
  602. (setq size (- max min -1))
  603. (setq cnt (1+ cnt)
  604. tot (+ tot size)
  605. M (max M size))))
  606. nnvirtual-component-groups)
  607. ;; Number of articles in the virtual group.
  608. (setq nnvirtual-mapping-len tot)
  609. ;; We want the actives list sorted by size, to build the tables.
  610. (setq actives (sort actives (lambda (g1 g2) (< (nth 1 g1) (nth 1 g2)))))
  611. ;; Build the offset table. Largest sized groups are at the front.
  612. (setq nnvirtual-mapping-offsets
  613. (vconcat
  614. (nreverse
  615. (mapcar (lambda (entry)
  616. (cons (nth 0 entry)
  617. (- (nth 2 entry) M)))
  618. actives))))
  619. ;; Build the mapping table.
  620. (setq nnvirtual-mapping-table nil)
  621. (setq actives (mapcar (lambda (entry) (nth 1 entry)) actives))
  622. (while actives
  623. (setq size (car actives))
  624. (setq next-M (- M size))
  625. (setq next-tot (- tot (* cnt size)))
  626. ;; make current row in table
  627. (push (vector M next-M cnt tot (- next-tot cnt))
  628. nnvirtual-mapping-table)
  629. ;; update M and tot
  630. (setq M next-M)
  631. (setq tot next-tot)
  632. ;; subtract the current size from all entries.
  633. (setq actives (mapcar (lambda (x) (- x size)) actives))
  634. ;; remove anything that went to 0.
  635. (while (and actives
  636. (= (car actives) 0))
  637. (pop actives)
  638. (setq cnt (- cnt 1))))
  639. ;; Now that the mapping tables are generated, we can convert
  640. ;; and combine the separate component unreads and marks lists
  641. ;; into single lists of virtual article numbers.
  642. (setq unreads (apply 'nnvirtual-merge-sorted-lists
  643. (mapcar (lambda (x)
  644. (nnvirtual-reverse-map-sequence
  645. (car x) (cdr x)))
  646. all-unreads)))
  647. (setq marks (mapcar
  648. (lambda (type)
  649. (cons (cdr type)
  650. (gnus-compress-sequence
  651. (apply
  652. 'nnvirtual-merge-sorted-lists
  653. (mapcar (lambda (x)
  654. (nnvirtual-reverse-map-sequence
  655. (car x)
  656. (cdr (assq (cdr type) (cdr x)))))
  657. all-marks)))))
  658. gnus-article-mark-lists))
  659. ;; Remove any empty marks lists, and store.
  660. (setq nnvirtual-mapping-marks nil)
  661. (dolist (mark marks)
  662. (when (cdr mark)
  663. (push mark nnvirtual-mapping-marks)))
  664. ;; We need to convert the unreads to reads. We compress the
  665. ;; sequence as we go, otherwise it could be huge.
  666. (while (and (<= (incf i) nnvirtual-mapping-len)
  667. unreads)
  668. (if (= i (car unreads))
  669. (setq unreads (cdr unreads))
  670. ;; try to get a range.
  671. (setq beg i)
  672. (while (and (<= (incf i) nnvirtual-mapping-len)
  673. (not (= i (car unreads)))))
  674. (setq i (- i 1))
  675. (if (= i beg)
  676. (push i reads)
  677. (push (cons beg i) reads))
  678. ))
  679. (when (<= i nnvirtual-mapping-len)
  680. (if (= i nnvirtual-mapping-len)
  681. (push i reads)
  682. (push (cons i nnvirtual-mapping-len) reads)))
  683. ;; Store the reads list for later use.
  684. (setq nnvirtual-mapping-reads (nreverse reads))
  685. ;; Throw flag to show we changed the info.
  686. (setq nnvirtual-info-installed nil)
  687. ))
  688. (provide 'nnvirtual)
  689. ;;; nnvirtual.el ends here