comparison lisp/gnus/nnvirtual.el @ 70:131b0175ea99 r20-0b30

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