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