*** 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@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 (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     (setq nnvirtual-current-group group)
266     (nnheader-insert "211 %d 1 %d %s\n"
267                      nnvirtual-mapping-len nnvirtual-mapping-len group))))
268
269
270 (deffoo nnvirtual-request-type (group &optional article)
271   (if (not article)
272       'unknown
273     (let ((mart (nnvirtual-map-article article)))
274       (when mart
275         (gnus-request-type (car mart) (cdr mart))))))
276
277 (deffoo nnvirtual-request-update-mark (group article mark)
278   (let* ((nart (nnvirtual-map-article article))
279          (cgroup (car nart))
280          ;; The component group might be a virtual group.
281          (nmark (gnus-request-update-mark cgroup (cdr nart) mark)))
282     (when (and nart
283                (= mark nmark)
284                (gnus-group-auto-expirable-p cgroup))
285       (setq mark gnus-expirable-mark)))
286   mark)
287
288
289 (deffoo nnvirtual-close-group (group &optional server)
290   (when (and (nnvirtual-possibly-change-server server)
291              (not (gnus-ephemeral-group-p (nnvirtual-current-group))))
292     (nnvirtual-update-read-and-marked t t))
293   t)
294
295
296 (deffoo nnvirtual-request-list (&optional server)
297   (nnheader-report 'nnvirtual "LIST is not implemented."))
298
299
300 (deffoo nnvirtual-request-newgroups (date &optional server)
301   (nnheader-report 'nnvirtual "NEWGROUPS is not supported."))
302
303
304 (deffoo nnvirtual-request-list-newsgroups (&optional server)
305   (nnheader-report 'nnvirtual "LIST NEWSGROUPS is not implemented."))
306
307
308 (deffoo nnvirtual-request-update-info (group info &optional server)
309   (when (and (nnvirtual-possibly-change-server server)
310              (not nnvirtual-info-installed))
311     ;; Install the precomputed lists atomically, so the virtual group
312     ;; is not left in a half-way state in case of C-g.
313     (gnus-atomic-progn
314       (setcar (cddr info) nnvirtual-mapping-reads)
315       (if (nthcdr 3 info)
316           (setcar (nthcdr 3 info) nnvirtual-mapping-marks)
317         (when nnvirtual-mapping-marks
318           (setcdr (nthcdr 2 info) (list nnvirtual-mapping-marks))))
319       (setq nnvirtual-info-installed t))
320     t))
321
322
323 (deffoo nnvirtual-catchup-group (group &optional server all)
324   (when (and (nnvirtual-possibly-change-server server)
325              (not (gnus-ephemeral-group-p (nnvirtual-current-group))))
326     ;; copy over existing marks first, in case they set anything
327     (nnvirtual-update-read-and-marked nil nil)
328     ;; do a catchup on all component groups
329     (let ((gnus-group-marked (copy-sequence nnvirtual-component-groups))
330           (gnus-expert-user t))
331       ;; Make sure all groups are activated.
332       (mapcar
333        (lambda (g)
334          (when (not (numberp (car (gnus-gethash g gnus-newsrc-hashtb))))
335            (gnus-activate-group g)))
336        nnvirtual-component-groups)
337       (save-excursion
338         (set-buffer gnus-group-buffer)
339         (gnus-group-catchup-current nil all)))))
340
341
342 (deffoo nnvirtual-find-group-art (group article)
343   "Return the real group and article for virtual GROUP and ARTICLE."
344   (nnvirtual-map-article article))
345
346
347 (deffoo nnvirtual-request-post (&optional server)
348   (if (not gnus-message-group-art)
349       (nnheader-report 'nnvirtual "Can't post to an nnvirtual group")
350     (let ((group (car (nnvirtual-find-group-art
351                        (car gnus-message-group-art)
352                        (cdr gnus-message-group-art)))))
353       (gnus-request-post (gnus-find-method-for-group group)))))
354
355 \f
356 ;;; Internal functions.
357
358 (defun nnvirtual-convert-headers ()
359   "Convert HEAD headers into NOV headers."
360   (save-excursion
361     (set-buffer nntp-server-buffer)
362     (let* ((dependencies (make-vector 100 0))
363            (headers (gnus-get-newsgroup-headers dependencies))
364            header)
365       (erase-buffer)
366       (while (setq header (pop headers))
367         (nnheader-insert-nov header)))))
368
369
370 (defun nnvirtual-update-xref-header (group article prefix system-name)
371   "Edit current NOV header in current buffer to have an xref to the component group, and also server prefix any existing xref lines."
372   ;; Move to beginning of Xref field, creating a slot if needed.
373   (beginning-of-line)
374   (looking-at
375    "[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t")
376   (goto-char (match-end 0))
377   (unless (search-forward "\t" (gnus-point-at-eol) 'move)
378     (insert "\t"))
379
380   ;; Remove any spaces at the beginning of the Xref field.
381   (while (= (char-after (1- (point))) ? )
382     (forward-char -1)
383     (delete-char 1))
384
385   (insert "Xref: " system-name " " group ":")
386   (princ article (current-buffer))
387   (insert " ")
388
389   ;; If there were existing xref lines, clean them up to have the correct
390   ;; component server prefix.
391   (save-restriction
392     (narrow-to-region (point)
393                       (or (search-forward "\t" (gnus-point-at-eol) t)
394                           (gnus-point-at-eol)))
395     (goto-char (point-min))
396     (when (re-search-forward "Xref: *[^\n:0-9 ]+ *" nil t)
397       (replace-match "" t t))
398     (goto-char (point-min))
399     (when (re-search-forward
400            (concat (gnus-group-real-name group) ":[0-9]+")
401            nil t)
402       (replace-match "" t t))
403     (unless (= (point) (point-max))
404       (insert " ")
405       (when (not (string= "" prefix))
406         (while (re-search-forward "[^ ]+:[0-9]+" nil t)
407           (save-excursion
408             (goto-char (match-beginning 0))
409             (insert prefix))))))
410
411   ;; Ensure a trailing \t.
412   (end-of-line)
413   (or (= (char-after (1- (point))) ?\t)
414       (insert ?\t)))
415
416
417 (defun nnvirtual-possibly-change-server (server)
418   (or (not server)
419       (nnoo-current-server-p 'nnvirtual server)
420       (nnvirtual-open-server server)))
421
422
423 (defun nnvirtual-update-read-and-marked (read-p update-p)
424   "Copy marks from the virtual group to the component groups.
425 If READ-P is not nil, update the (un)read status of the components.
426 If UPDATE-P is not nil, call gnus-group-update-group on the components."
427   (when nnvirtual-current-group
428     (let ((unreads (and read-p
429                         (nnvirtual-partition-sequence
430                          (gnus-list-of-unread-articles
431                           (nnvirtual-current-group)))))
432           (type-marks (mapcar (lambda (ml)
433                                 (cons (car ml)
434                                       (nnvirtual-partition-sequence (cdr ml))))
435                               (gnus-info-marks (gnus-get-info
436                                                 (nnvirtual-current-group)))))
437           mark type groups carticles info entry)
438
439       ;; Ok, atomically move all of the (un)read info, clear any old
440       ;; marks, and move all of the current marks.  This way if someone
441       ;; hits C-g, you won't leave the component groups in a half-way state.
442       (gnus-atomic-progn
443         ;; move (un)read
444         (let ((gnus-newsgroup-active nil)) ;workaround guns-update-read-articles
445           (while (setq entry (pop unreads))
446             (gnus-update-read-articles (car entry) (cdr entry))))
447
448         ;; clear all existing marks on the component groups
449         (setq groups nnvirtual-component-groups)
450         (while groups
451           (when (and (setq info (gnus-get-info (pop groups)))
452                      (gnus-info-marks info))
453             (gnus-info-set-marks info nil)))
454
455         ;; Ok, currently type-marks is an assq list with keys of a mark type,
456         ;; with data of an assq list with keys of component group names
457         ;; and the articles which correspond to that key/group pair.
458         (while (setq mark (pop type-marks))
459           (setq type (car mark))
460           (setq groups (cdr mark))
461           (while (setq carticles (pop groups))
462             (gnus-add-marked-articles (car carticles) type (cdr carticles)
463                                       nil t))))
464
465       ;; possibly update the display, it is really slow
466       (when update-p
467         (setq groups nnvirtual-component-groups)
468         (while groups
469           (gnus-group-update-group (pop groups) t))))))
470
471
472 (defun nnvirtual-current-group ()
473   "Return the prefixed name of the current nnvirtual group."
474   (concat "nnvirtual:" nnvirtual-current-group))
475
476
477
478 ;;; This is currently O(kn^2) to merge n lists of length k.
479 ;;; You could do it in O(knlogn), but we have a small n, and the
480 ;;; overhead of the other approach is probably greater.
481 (defun nnvirtual-merge-sorted-lists (&rest lists)
482   "Merge many sorted lists of numbers."
483   (if (null (cdr lists))
484       (car lists)
485     (sort (apply 'nconc lists) '<)))
486
487
488 ;;; We map between virtual articles and real articles in a manner
489 ;;; which keeps the size of the virtual active list the same as
490 ;;; the sum of the component active lists.
491 ;;; To achieve fair mixing of the groups, the last article in
492 ;;; each of N component groups will be in the the last N articles
493 ;;; in the virtual group.
494
495 ;;; If you have 3 components A, B and C, with articles 1-8, 1-5, and 6-7
496 ;;; resprectively, then the virtual article numbers look like:
497 ;;;
498 ;;;  1  2  3  4  5  6  7  8  9  10 11 12 13 14 15
499 ;;;  A1 A2 A3 A4 B1 A5 B2 A6 B3 A7 B4 C6 A8 B5 C7
500
501 ;;; To compute these mappings we generate a couple tables and then
502 ;;; do some fast operations on them.  Tables for the example above:
503 ;;;
504 ;;; Offsets - [(A 0) (B -3) (C -1)]
505 ;;;
506 ;;;               a  b  c  d  e
507 ;;; Mapping - ([  3  0  1  3  0 ]
508 ;;;            [  6  3  2  9  3 ]
509 ;;;            [  8  6  3 15  9 ])
510 ;;;
511 ;;; (note column 'e' is different in real algorithm, which is slightly
512 ;;;  different than described here, but this gives you the methodology.)
513 ;;;
514 ;;; The basic idea is this, when going from component->virtual, apply
515 ;;; the appropriate offset to the article number.  Then search the first
516 ;;; column of the table for a row where 'a' is less than or equal to the
517 ;;; modified number.  You can see that only group A can therefore go to
518 ;;; the first row, groups A and B to the second, and all to the last.
519 ;;; The third column of the table is telling us the number of groups
520 ;;; which might be able to reach that row (it might increase by more than
521 ;;; 1 if several groups have the same size).
522 ;;; Then column 'b' provides an additional offset you apply when you have
523 ;;; found the correct row.  You then multiply by 'c' and add on the groups
524 ;;; _position_ in the offset table.  The basic idea here is that on
525 ;;; any given row we are going to map back and forth using X'=X*c+Y and
526 ;;; X=(X'/c), Y=(X' mod c).  Then once you've done this transformation,
527 ;;; you apply a final offset from column 'e' to give the virtual article.
528 ;;;
529 ;;; Going the other direction, you instead search on column 'd' instead
530 ;;; of 'a', and apply everything in reverse order.
531
532 ;;; Convert component -> virtual:
533 ;;; set num = num - Offset(group)
534 ;;; find first row in Mapping where num <= 'a'
535 ;;; num = (num-'b')*c + Position(group) + 'e'
536
537 ;;; Convert virtual -> component:
538 ;;; find first row in Mapping where num <= 'd'
539 ;;; num = num - 'e'
540 ;;; group_pos = num mod 'c'
541 ;;; num = (num / 'c') + 'b' + Offset(group_pos)
542
543 ;;; Easy no? :)
544 ;;;
545 ;;; Well actually, you need to keep column e offset smaller by the 'c'
546 ;;; column for that line, and always add 1 more when going from
547 ;;; component -> virtual.  Otherwise you run into a problem with
548 ;;; unique reverse mapping.
549
550 (defun nnvirtual-map-article (article)
551   "Return a cons of the component group and article corresponding to the given virtual ARTICLE."
552   (let ((table nnvirtual-mapping-table)
553         entry group-pos)
554     (while (and table
555                 (> article (aref (car table) 3)))
556       (setq table (cdr table)))
557     (when (and table
558                (> article 0))
559       (setq entry (car table))
560       (setq article (- article (aref entry 4) 1))
561       (setq group-pos (mod article (aref entry 2)))
562       (cons (car (aref nnvirtual-mapping-offsets group-pos))
563             (+ (/ article (aref entry 2))
564                (aref entry 1)
565                (cdr (aref nnvirtual-mapping-offsets group-pos)))
566             ))
567       ))
568
569
570
571 (defun nnvirtual-reverse-map-article (group article)
572   "Return the virtual article number corresponding to the given component GROUP and ARTICLE."
573   (when (numberp article)
574     (let ((table nnvirtual-mapping-table)
575           (group-pos 0)
576           entry)
577       (while (not (string= group (car (aref nnvirtual-mapping-offsets
578                                             group-pos))))
579         (setq group-pos (1+ group-pos)))
580       (setq article (- article (cdr (aref nnvirtual-mapping-offsets
581                                           group-pos))))
582       (while (and table
583                   (> article (aref (car table) 0)))
584         (setq table (cdr table)))
585       (setq entry (car table))
586       (when (and entry
587                  (> article 0)
588                  (< group-pos (aref entry 2))) ; article not out of range below
589         (+ (aref entry 4)
590            group-pos
591            (* (- article (aref entry 1))
592               (aref entry 2))
593            1))
594       )))
595
596
597 (defsubst nnvirtual-reverse-map-sequence (group articles)
598   "Return list of virtual article numbers for all ARTICLES in GROUP.
599 The ARTICLES should be sorted, and can be a compressed sequence.
600 If any of the article numbers has no corresponding virtual article,
601 then it is left out of the result."
602   (when (numberp (cdr-safe articles))
603     (setq articles (list articles)))
604   (let (result a i j new-a)
605     (while (setq a (pop articles))
606       (if (atom a)
607           (setq i a
608                 j a)
609         (setq i (car a)
610               j (cdr a)))
611       (while (<= i j)
612         ;; If this is slow, you can optimize by moving article checking
613         ;; into here.  You don't have to recompute the group-pos,
614         ;; nor scan the table every time.
615         (when (setq new-a (nnvirtual-reverse-map-article group i))
616           (push new-a result))
617         (setq i (1+ i))))
618     (nreverse result)))
619
620
621 (defun nnvirtual-partition-sequence (articles)
622   "Return an association list of component article numbers.
623 These are indexed by elements of nnvirtual-component-groups, based on
624 the sequence ARTICLES of virtual article numbers.  ARTICLES should be
625 sorted, and can be a compressed sequence. If any of the article
626 numbers has no corresponding component article, then it is left out of
627 the result."
628   (when (numberp (cdr-safe articles))
629     (setq articles (list articles)))
630   (let ((carticles (mapcar (lambda (g) (list g))
631                            nnvirtual-component-groups))
632         a i j article entry)
633     (while (setq a (pop articles))
634       (if (atom a)
635           (setq i a
636                 j a)
637         (setq i (car a)
638               j (cdr a)))
639       (while (<= i j)
640         (when (setq article (nnvirtual-map-article i))
641           (setq entry (assoc (car article) carticles))
642           (setcdr entry (cons (cdr article) (cdr entry))))
643         (setq i (1+ i))))
644     (mapcar (lambda (x) (setcdr x (nreverse (cdr x))))
645             carticles)
646     carticles))
647
648
649 (defun nnvirtual-create-mapping ()
650   "Build the tables necessary to map between component (group, article) to virtual article.
651 Generate the set of read messages and marks for the virtual group
652 based on the marks on the component groups."
653   (let ((cnt 0)
654         (tot 0)
655         (M 0)
656         (i 0)
657         actives all-unreads all-marks
658         active min max size unreads marks
659         next-M next-tot
660         reads beg)
661     ;; Ok, we loop over all component groups and collect a lot of
662     ;; information:
663     ;; Into actives we place (g size max), where size is max-min+1.
664     ;; Into all-unreads we put (g unreads).
665     ;; Into all-marks we put (g marks).
666     ;; We also increment cnt and tot here, and compute M (max of sizes).
667     (mapc (lambda (g)
668             (setq active (gnus-activate-group g)
669                   min (car active)
670                   max (cdr active))
671             (when (and active (>= max min) (not (zerop max)))
672               ;; store active information
673               (push (list g (- max min -1) max) actives)
674               ;; collect unread/mark info for later
675               (setq unreads (gnus-list-of-unread-articles g))
676               (setq marks (gnus-info-marks (gnus-get-info g)))
677               (when gnus-use-cache
678                 (push (cons 'cache
679                             (gnus-cache-articles-in-group g))
680                       marks))
681               (push (cons g unreads) all-unreads)
682               (push (cons g marks) all-marks)
683               ;; count groups, total #articles, and max size
684               (setq size (- max min -1))
685               (setq cnt (1+ cnt)
686                     tot (+ tot size)
687                     M (max M size))))
688           nnvirtual-component-groups)
689
690     ;; Number of articles in the virtual group.
691     (setq nnvirtual-mapping-len tot)
692
693
694     ;; We want the actives list sorted by size, to build the tables.
695     (setq actives (sort actives (lambda (g1 g2) (< (nth 1 g1) (nth 1 g2)))))
696
697     ;; Build the offset table.  Largest sized groups are at the front.
698     (setq nnvirtual-mapping-offsets
699           (vconcat
700            (nreverse
701             (mapcar (lambda (entry)
702                       (cons (nth 0 entry)
703                             (- (nth 2 entry) M)))
704                     actives))))
705
706     ;; Build the mapping table.
707     (setq nnvirtual-mapping-table nil)
708     (setq actives (mapcar (lambda (entry) (nth 1 entry)) actives))
709     (while actives
710       (setq size (car actives))
711       (setq next-M (- M size))
712       (setq next-tot (- tot (* cnt size)))
713       ;; make current row in table
714       (push (vector M next-M cnt tot (- next-tot cnt))
715             nnvirtual-mapping-table)
716       ;; update M and tot
717       (setq M next-M)
718       (setq tot next-tot)
719       ;; subtract the current size from all entries.
720       (setq actives (mapcar (lambda (x) (- x size)) actives))
721       ;; remove anything that went to 0.
722       (while (and actives
723                   (= (car actives) 0))
724         (pop actives)
725         (setq cnt (- cnt 1))))
726
727
728     ;; Now that the mapping tables are generated, we can convert
729     ;; and combine the separate component unreads and marks lists
730     ;; into single lists of virtual article numbers.
731     (setq unreads (apply 'nnvirtual-merge-sorted-lists
732                          (mapcar (lambda (x)
733                                    (nnvirtual-reverse-map-sequence
734                                     (car x) (cdr x)))
735                                  all-unreads)))
736     (setq marks (mapcar
737                  (lambda (type)
738                    (cons (cdr type)
739                          (gnus-compress-sequence
740                           (apply
741                            'nnvirtual-merge-sorted-lists
742                            (mapcar (lambda (x)
743                                      (nnvirtual-reverse-map-sequence
744                                       (car x)
745                                       (cdr (assq (cdr type) (cdr x)))))
746                                    all-marks)))))
747                  gnus-article-mark-lists))
748
749     ;; Remove any empty marks lists, and store.
750     (setq nnvirtual-mapping-marks nil)
751     (while marks
752       (if (cdr (car marks))
753           (push (car marks) nnvirtual-mapping-marks))
754       (setq marks (cdr marks)))
755
756     ;; We need to convert the unreads to reads.  We compress the
757     ;; sequence as we go, otherwise it could be huge.
758     (while (and (<= (incf i) nnvirtual-mapping-len)
759                 unreads)
760       (if (= i (car unreads))
761           (setq unreads (cdr unreads))
762         ;; try to get a range.
763         (setq beg i)
764         (while (and (<= (incf i) nnvirtual-mapping-len)
765                     (not (= i (car unreads)))))
766         (setq i (- i 1))
767         (if (= i beg)
768             (push i reads)
769           (push (cons beg i) reads))
770         ))
771     (when (<= i nnvirtual-mapping-len)
772       (if (= i nnvirtual-mapping-len)
773           (push i reads)
774         (push (cons i nnvirtual-mapping-len) reads)))
775
776     ;; Store the reads list for later use.
777     (setq nnvirtual-mapping-reads (nreverse reads))
778
779     ;; Throw flag to show we changed the info.
780     (setq nnvirtual-info-installed nil)
781     ))
782
783 (provide 'nnvirtual)
784
785 ;;; nnvirtual.el ends here