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