89605801337a2c81ad1080a14152d0c7c7211f4b
[elisp/gnus.git-] / lisp / gnus-cache.el
1 ;;; gnus-cache.el --- cache interface for Gnus
2 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;;         Tatsuya Ichikawa <t-ichi@po.shiojiri.ne.jp>
7 ;;         MORIOKA Tomohiko <morioka@jaist.ac.jp>
8 ;; Keywords: news
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
26
27 ;;; Commentary:
28
29 ;;; Code:
30
31 (eval-when-compile (require 'cl))
32 (eval-when-compile (require 'gnus-clfns))
33
34 (require 'gnus)
35 (require 'gnus-int)
36 (require 'gnus-range)
37 (require 'gnus-start)
38 (eval-when-compile
39   (require 'gnus-sum))
40
41 (defcustom gnus-cache-active-file
42   (concat (file-name-as-directory gnus-cache-directory) "active")
43   "*The cache active file."
44   :group 'gnus-cache
45   :type 'file)
46
47 (defcustom gnus-cache-enter-articles '(ticked dormant)
48   "Classes of articles to enter into the cache."
49   :group 'gnus-cache
50   :type '(set (const ticked) (const dormant) (const unread) (const read)))
51
52 (defcustom gnus-cache-remove-articles '(read)
53   "Classes of articles to remove from the cache."
54   :group 'gnus-cache
55   :type '(set (const ticked) (const dormant) (const unread) (const read)))
56
57 (defcustom gnus-cacheable-groups nil
58   "*Groups that match this regexp will be cached.
59
60 If you only want to cache your nntp groups, you could set this
61 variable to \"^nntp\".
62
63 If a group matches both gnus-cacheable-groups and gnus-uncacheable-groups
64 it's not cached."
65   :group 'gnus-cache
66   :type '(choice (const :tag "off" nil)
67                  regexp))
68
69 (defcustom gnus-uncacheable-groups nil
70   "*Groups that match this regexp will not be cached.
71
72 If you want to avoid caching your nnml groups, you could set this
73 variable to \"^nnml\".
74
75 If a group matches both gnus-cacheable-groups and gnus-uncacheable-groups
76 it's not cached."
77   :group 'gnus-cache
78   :type '(choice (const :tag "off" nil)
79                  regexp))
80
81 (defvar gnus-cache-overview-coding-system 'raw-text
82   "Coding system used on Gnus cache files.")
83
84 (defvar gnus-cache-coding-system 'raw-text
85   "Coding system used on Gnus cache files.")
86
87 \f
88
89 ;;; Internal variables.
90
91 (defvar gnus-cache-removable-articles nil)
92 (defvar gnus-cache-buffer nil)
93 (defvar gnus-cache-active-hashtb nil)
94 (defvar gnus-cache-active-altered nil)
95 (defvar gnus-cache-write-file-coding-system 'raw-text)
96
97 (eval-and-compile
98   (autoload 'nnml-generate-nov-databases-1 "nnml")
99   (autoload 'nnvirtual-find-group-art "nnvirtual"))
100
101 \f
102
103 ;;; Functions called from Gnus.
104
105 (defun gnus-cache-open ()
106   "Initialize the cache."
107   (when (or (file-exists-p gnus-cache-directory)
108             (and gnus-use-cache
109                  (not (eq gnus-use-cache 'passive))))
110     (gnus-cache-read-active)))
111
112 ;; Complexities of byte-compiling make this kludge necessary.  Eeek.
113 (ignore-errors
114   (gnus-add-shutdown 'gnus-cache-close 'gnus))
115
116 (defun gnus-cache-close ()
117   "Shut down the cache."
118   (gnus-cache-write-active)
119   (gnus-cache-save-buffers)
120   (setq gnus-cache-active-hashtb nil))
121
122 (defun gnus-cache-save-buffers ()
123   ;; save the overview buffer if it exists and has been modified
124   ;; delete empty cache subdirectories
125   (when gnus-cache-buffer
126     (let ((buffer (cdr gnus-cache-buffer))
127           (overview-file (gnus-cache-file-name
128                           (car gnus-cache-buffer) ".overview")))
129       ;; write the overview only if it was modified
130       (when (buffer-modified-p buffer)
131         (save-excursion
132           (set-buffer buffer)
133           (if (> (buffer-size) 0)
134               ;; Non-empty overview, write it to a file.
135               (gnus-write-buffer-as-coding-system
136                gnus-cache-overview-coding-system overview-file)
137             ;; Empty overview file, remove it
138             (when (file-exists-p overview-file)
139               (delete-file overview-file))
140             ;; If possible, remove group's cache subdirectory.
141             (condition-case nil
142                 ;; FIXME: we can detect the error type and warn the user
143                 ;; of any inconsistencies (articles w/o nov entries?).
144                 ;; for now, just be conservative...delete only if safe -- sj
145                 (delete-directory (file-name-directory overview-file))
146               (error nil)))))
147       ;; Kill the buffer -- it's either unmodified or saved.
148       (gnus-kill-buffer buffer)
149       (setq gnus-cache-buffer nil))))
150
151 (defun gnus-cache-possibly-enter-article
152   (group article headers ticked dormant unread &optional force)
153   (when (and (or force (not (eq gnus-use-cache 'passive)))
154              (numberp article)
155              (> article 0)
156              (vectorp headers))         ; This might be a dummy article.
157     (let ((number article) file)
158       ;; If this is a virtual group, we find the real group.
159       (when (gnus-virtual-group-p group)
160         (let ((result (nnvirtual-find-group-art
161                        (gnus-group-real-name group) article)))
162           (setq group (car result)
163                 number (cdr result))))
164       (when (and number
165                  (> number 0)           ; Reffed article.
166                  (or force
167                      (and (or (not gnus-cacheable-groups)
168                               (string-match gnus-cacheable-groups group))
169                           (or (not gnus-uncacheable-groups)
170                               (not (string-match
171                                     gnus-uncacheable-groups group)))
172                           (gnus-cache-member-of-class
173                            gnus-cache-enter-articles ticked dormant unread)))
174                  (not (file-exists-p (setq file (gnus-cache-file-name
175                                                  group number)))))
176         ;; Possibly create the cache directory.
177         (gnus-make-directory (file-name-directory file))
178         ;; Save the article in the cache.
179         (if (file-exists-p file)
180             t                           ; The article already is saved.
181           (save-excursion
182             (set-buffer nntp-server-buffer)
183             (require 'gnus-art)
184             (let ((gnus-use-cache nil)
185                   (gnus-article-decode-hook nil))
186               (gnus-request-article-this-buffer number group))
187             (when (> (buffer-size) 0)
188               (gnus-write-buffer-as-coding-system
189                gnus-cache-write-file-coding-system file)
190               (setq headers (nnheader-parse-head t))
191               (mail-header-set-number headers number)
192               (gnus-cache-change-buffer group)
193               (set-buffer (cdr gnus-cache-buffer))
194               (goto-char (point-max))
195               (forward-line -1)
196               (while (condition-case ()
197                          (when (not (bobp))
198                            (> (read (current-buffer)) number))
199                        (error
200                         ;; The line was malformed, so we just remove it!!
201                         (gnus-delete-line)
202                         t))
203                 (forward-line -1))
204               (if (bobp)
205                   (if (not (eobp))
206                       (progn
207                         (beginning-of-line)
208                         (when (< (read (current-buffer)) number)
209                           (forward-line 1)))
210                     (beginning-of-line))
211                 (forward-line 1))
212               (beginning-of-line)
213               (nnheader-insert-nov headers)
214               ;; Update the active info.
215               (set-buffer gnus-summary-buffer)
216               (gnus-cache-update-active group number)
217               (push article gnus-newsgroup-cached)
218               (gnus-summary-update-secondary-mark article))
219             t))))))
220
221 (defun gnus-cache-enter-remove-article (article)
222   "Mark ARTICLE for later possible removal."
223   (when article
224     (push article gnus-cache-removable-articles)))
225
226 (defun gnus-cache-possibly-remove-articles ()
227   "Possibly remove some of the removable articles."
228   (if (not (gnus-virtual-group-p gnus-newsgroup-name))
229       (gnus-cache-possibly-remove-articles-1)
230     (let ((arts gnus-cache-removable-articles)
231           ga)
232       (while arts
233         (when (setq ga (nnvirtual-find-group-art
234                         (gnus-group-real-name gnus-newsgroup-name) (pop arts)))
235           (let ((gnus-cache-removable-articles (list (cdr ga)))
236                 (gnus-newsgroup-name (car ga)))
237             (gnus-cache-possibly-remove-articles-1)))))
238     (setq gnus-cache-removable-articles nil)))
239
240 (defun gnus-cache-possibly-remove-articles-1 ()
241   "Possibly remove some of the removable articles."
242   (unless (eq gnus-use-cache 'passive)
243     (let ((articles gnus-cache-removable-articles)
244           (cache-articles gnus-newsgroup-cached)
245           article)
246       (gnus-cache-change-buffer gnus-newsgroup-name)
247       (while articles
248         (when (memq (setq article (pop articles)) cache-articles)
249           ;; The article was in the cache, so we see whether we are
250           ;; supposed to remove it from the cache.
251           (gnus-cache-possibly-remove-article
252            article (memq article gnus-newsgroup-marked)
253            (memq article gnus-newsgroup-dormant)
254            (or (memq article gnus-newsgroup-unreads)
255                (memq article gnus-newsgroup-unselected))))))
256     ;; The overview file might have been modified, save it
257     ;; safe because we're only called at group exit anyway.
258     (gnus-cache-save-buffers)))
259
260 (defun gnus-cache-request-article (article group)
261   "Retrieve ARTICLE in GROUP from the cache."
262   (let ((file (gnus-cache-file-name group article))
263         (buffer-read-only nil))
264     (when (file-exists-p file)
265       (erase-buffer)
266       (gnus-kill-all-overlays)
267       (let ((nnheader-file-coding-system gnus-cache-coding-system))
268         (nnheader-insert-file-contents file))
269       t)))
270
271 (defun gnus-cache-possibly-alter-active (group active)
272   "Alter the ACTIVE info for GROUP to reflect the articles in the cache."
273   (when gnus-cache-active-hashtb
274     (let ((cache-active (gnus-gethash group gnus-cache-active-hashtb)))
275       (when cache-active
276         (when (< (car cache-active) (car active))
277           (setcar active (car cache-active)))
278         (when (> (cdr cache-active) (cdr active))
279           (setcdr active (cdr cache-active)))))))
280
281 (defun gnus-cache-retrieve-headers (articles group &optional fetch-old)
282   "Retrieve the headers for ARTICLES in GROUP."
283   (let ((cached
284          (setq gnus-newsgroup-cached (gnus-cache-articles-in-group group))))
285     (if (not cached)
286         ;; No cached articles here, so we just retrieve them
287         ;; the normal way.
288         (let ((gnus-use-cache nil))
289           (gnus-retrieve-headers articles group fetch-old))
290       (let ((uncached-articles (gnus-sorted-intersection
291                                 (gnus-sorted-complement articles cached)
292                                 articles))
293             (cache-file (gnus-cache-file-name group ".overview"))
294             type)
295         ;; We first retrieve all the headers that we don't have in
296         ;; the cache.
297         (let ((gnus-use-cache nil))
298           (when uncached-articles
299             (setq type (and articles
300                             (gnus-retrieve-headers
301                              uncached-articles group fetch-old)))))
302         (gnus-cache-save-buffers)
303         ;; Then we insert the cached headers.
304         (save-excursion
305           (cond
306            ((not (file-exists-p cache-file))
307             ;; There are no cached headers.
308             type)
309            ((null type)
310             ;; There were no uncached headers (or retrieval was
311             ;; unsuccessful), so we use the cached headers exclusively.
312             (set-buffer nntp-server-buffer)
313             (erase-buffer)
314             (let ((nnheader-file-coding-system
315                    gnus-cache-overview-coding-system))
316               (nnheader-insert-file-contents cache-file))
317             'nov)
318            ((eq type 'nov)
319             ;; We have both cached and uncached NOV headers, so we
320             ;; braid them.
321             (gnus-cache-braid-nov group cached)
322             type)
323            (t
324             ;; We braid HEADs.
325             (gnus-cache-braid-heads group (gnus-sorted-intersection
326                                            cached articles))
327             type)))))))
328
329 (defun gnus-cache-retrieve-parsed-headers (articles group &optional fetch-old
330                                                     dependencies force-new)
331   "Retrieve the parsed-headers for ARTICLES in GROUP."
332   (let ((cached
333          (setq gnus-newsgroup-cached (gnus-cache-articles-in-group group))))
334     (if (not cached)
335         ;; No cached articles here, so we just retrieve them
336         ;; the normal way.
337         (let ((gnus-use-cache nil))
338           (gnus-retrieve-parsed-headers articles group fetch-old
339                                         dependencies force-new))
340       (let ((uncached-articles (gnus-sorted-intersection
341                                 (gnus-sorted-complement articles cached)
342                                 articles))
343             (cache-file (gnus-cache-file-name group ".overview")))
344         (gnus-cache-braid-headers
345          ;; We first retrieve all the headers that we don't have in
346          ;; the cache.
347          (prog1
348              (let ((gnus-use-cache nil))
349                (when uncached-articles
350                  (and articles
351                       (gnus-retrieve-parsed-headers
352                        uncached-articles group fetch-old
353                        dependencies))
354                  ))
355            (gnus-cache-save-buffers))
356          ;; Then we insert the cached headers.
357          (cond ((not (file-exists-p cache-file))
358                 ;; There are no cached headers.
359                 )
360                ((eq gnus-headers-retrieved-by 'nov)
361                 (with-current-buffer nntp-server-buffer
362                   (erase-buffer)
363                   (nnheader-insert-file-contents cache-file)
364                   (nnheader-get-newsgroup-headers-xover*
365                    articles nil dependencies group)
366                   ))
367                (t
368                 ;; We braid HEADs.
369                 (nnheader-retrieve-headers-from-directory*
370                  cached
371                  (expand-file-name
372                   (file-name-as-directory
373                    (nnheader-translate-file-chars
374                     (if (gnus-use-long-file-name 'not-cache)
375                         group
376                       (let ((group
377                              (nnheader-replace-chars-in-string group ?/ ?_)))
378                         ;; Translate the first colon into a slash.
379                         (when (string-match ":" group)
380                           (aset group (match-beginning 0) ?/))
381                         (nnheader-replace-chars-in-string group ?. ?/)))
382                     t))
383                   gnus-cache-directory)
384                  dependencies)
385                 )))
386         ))))
387
388 (defun gnus-cache-enter-article (&optional n)
389   "Enter the next N articles into the cache.
390 If not given a prefix, use the process marked articles instead.
391 Returns the list of articles entered."
392   (interactive "P")
393   (let ((articles (gnus-summary-work-articles n))
394         article out)
395     (while (setq article (pop articles))
396       (gnus-summary-remove-process-mark article)
397       (if (natnump article)
398           (when (gnus-cache-possibly-enter-article
399                  gnus-newsgroup-name article
400                  (gnus-summary-article-header article)
401                  nil nil nil t)
402             (push article out))
403         (gnus-message 2 "Can't cache article %d" article))
404       (gnus-summary-update-secondary-mark article))
405     (gnus-summary-next-subject 1)
406     (gnus-summary-position-point)
407     (nreverse out)))
408
409 (defun gnus-cache-remove-article (n)
410   "Remove the next N articles from the cache.
411 If not given a prefix, use the process marked articles instead.
412 Returns the list of articles removed."
413   (interactive "P")
414   (gnus-cache-change-buffer gnus-newsgroup-name)
415   (let ((articles (gnus-summary-work-articles n))
416         article out)
417     (while articles
418       (setq article (pop articles))
419       (gnus-summary-remove-process-mark article)
420       (when (gnus-cache-possibly-remove-article article nil nil nil t)
421         (push article out))
422       (gnus-summary-update-secondary-mark article))
423     (gnus-summary-next-subject 1)
424     (gnus-summary-position-point)
425     (nreverse out)))
426
427 (defun gnus-cached-article-p (article)
428   "Say whether ARTICLE is cached in the current group."
429   (memq article gnus-newsgroup-cached))
430
431 (defun gnus-summary-insert-cached-articles ()
432   "Insert all the articles cached for this group into the current buffer."
433   (interactive)
434   (let ((cached (sort (copy-sequence gnus-newsgroup-cached) '>))
435         (gnus-verbose (max 6 gnus-verbose)))
436     (unless cached
437       (gnus-message 3 "No cached articles for this group"))
438     (while cached
439       (gnus-summary-goto-subject (pop cached) t))))
440
441 (defalias 'gnus-summary-limit-include-cached
442   'gnus-summary-insert-cached-articles)
443
444 ;;; Internal functions.
445
446 (defun gnus-cache-change-buffer (group)
447   (and gnus-cache-buffer
448        ;; See if the current group's overview cache has been loaded.
449        (or (string= group (car gnus-cache-buffer))
450            ;; Another overview cache is current, save it.
451            (gnus-cache-save-buffers)))
452   ;; if gnus-cache buffer is nil, create it
453   (unless gnus-cache-buffer
454     ;; Create cache buffer
455     (save-excursion
456       (setq gnus-cache-buffer
457             (cons group
458                   (set-buffer (gnus-get-buffer-create
459                                " *gnus-cache-overview*"))))
460       ;; Insert the contents of this group's cache overview.
461       (erase-buffer)
462       (let ((file (gnus-cache-file-name group ".overview")))
463         (when (file-exists-p file)
464           (nnheader-insert-file-contents file)))
465       ;; We have a fresh (empty/just loaded) buffer,
466       ;; mark it as unmodified to save a redundant write later.
467       (set-buffer-modified-p nil))))
468
469 ;; Return whether an article is a member of a class.
470 (defun gnus-cache-member-of-class (class ticked dormant unread)
471   (or (and ticked (memq 'ticked class))
472       (and dormant (memq 'dormant class))
473       (and unread (memq 'unread class))
474       (and (not unread) (not ticked) (not dormant) (memq 'read class))))
475
476 (defun gnus-cache-file-name (group article)
477   (concat (file-name-as-directory gnus-cache-directory)
478           (file-name-as-directory
479            (nnheader-translate-file-chars
480             (if (gnus-use-long-file-name 'not-cache)
481                 group
482               (let ((group (nnheader-replace-duplicate-chars-in-string
483                             (nnheader-replace-chars-in-string group ?/ ?_)
484                             ?. ?_)))
485                 ;; Translate the first colon into a slash.
486                 (when (string-match ":" group)
487                   (aset group (match-beginning 0) ?/))
488                 (nnheader-replace-chars-in-string group ?. ?/)))
489             t))
490           (if (stringp article) article (int-to-string article))))
491
492 (defun gnus-cache-update-article (group article)
493   "If ARTICLE is in the cache, remove it and re-enter it."
494   (gnus-cache-change-buffer group)
495   (when (gnus-cache-possibly-remove-article article nil nil nil t)
496     (let ((gnus-use-cache nil))
497       (gnus-cache-possibly-enter-article
498        gnus-newsgroup-name article (gnus-summary-article-header article)
499        nil nil nil t))))
500
501 (defun gnus-cache-possibly-remove-article (article ticked dormant unread
502                                                    &optional force)
503   "Possibly remove ARTICLE from the cache."
504   (let ((group gnus-newsgroup-name)
505         (number article)
506         file)
507     ;; If this is a virtual group, we find the real group.
508     (when (gnus-virtual-group-p group)
509       (let ((result (nnvirtual-find-group-art
510                      (gnus-group-real-name group) article)))
511         (setq group (car result)
512               number (cdr result))))
513     (setq file (gnus-cache-file-name group number))
514     (when (and (file-exists-p file)
515                (or force
516                    (gnus-cache-member-of-class
517                     gnus-cache-remove-articles ticked dormant unread)))
518       (save-excursion
519         (delete-file file)
520         (set-buffer (cdr gnus-cache-buffer))
521         (goto-char (point-min))
522         (when (or (looking-at (concat (int-to-string number) "\t"))
523                   (search-forward (concat "\n" (int-to-string number) "\t")
524                                   (point-max) t))
525           (delete-region (progn (beginning-of-line) (point))
526                          (progn (forward-line 1) (point)))))
527       (setq gnus-newsgroup-cached
528             (delq article gnus-newsgroup-cached))
529       (gnus-summary-update-secondary-mark article)
530       t)))
531
532 (defun gnus-cache-articles-in-group (group)
533   "Return a sorted list of cached articles in GROUP."
534   (let ((dir (file-name-directory (gnus-cache-file-name group 1)))
535         articles)
536     (when (file-exists-p dir)
537       (setq articles
538             (sort (mapcar (lambda (name) (string-to-int name))
539                           (directory-files dir nil "^[0-9]+$" t))
540                   '<))
541       ;; Update the cache active file, just to synch more.
542       (when articles
543         (gnus-cache-update-active group (car articles) t)
544         (gnus-cache-update-active group (car (last articles))))
545       articles)))
546
547 (defun gnus-cache-braid-nov (group cached &optional file)
548   (let ((cache-buf (gnus-get-buffer-create " *gnus-cache*"))
549         beg end)
550     (gnus-cache-save-buffers)
551     (save-excursion
552       (set-buffer cache-buf)
553       (erase-buffer)
554       (let ((nnheader-file-coding-system gnus-cache-overview-coding-system))
555         (nnheader-insert-file-contents
556          (or file (gnus-cache-file-name group ".overview"))))
557       (goto-char (point-min))
558       (insert "\n")
559       (goto-char (point-min)))
560     (set-buffer nntp-server-buffer)
561     (goto-char (point-min))
562     (while cached
563       (while (and (not (eobp))
564                   (< (read (current-buffer)) (car cached)))
565         (forward-line 1))
566       (beginning-of-line)
567       (save-excursion
568         (set-buffer cache-buf)
569         (if (search-forward (concat "\n" (int-to-string (car cached)) "\t")
570                             nil t)
571             (setq beg (progn (beginning-of-line) (point))
572                   end (progn (end-of-line) (point)))
573           (setq beg nil)))
574       (when beg
575         (insert-buffer-substring cache-buf beg end)
576         (insert "\n"))
577       (setq cached (cdr cached)))
578     (kill-buffer cache-buf)))
579
580 (defun gnus-cache-braid-heads (group cached)
581   (let ((cache-buf (gnus-get-buffer-create " *gnus-cache*")))
582     (save-excursion
583       (set-buffer cache-buf)
584       (erase-buffer))
585     (set-buffer nntp-server-buffer)
586     (goto-char (point-min))
587     (while cached
588       (while (and (not (eobp))
589                   (looking-at "2.. +\\([0-9]+\\) ")
590                   (< (progn (goto-char (match-beginning 1))
591                             (read (current-buffer)))
592                      (car cached)))
593         (search-forward "\n.\n" nil 'move))
594       (beginning-of-line)
595       (save-excursion
596         (set-buffer cache-buf)
597         (erase-buffer)
598         (let ((nnheader-file-coding-system gnus-cache-coding-system))
599           (nnheader-insert-file-contents
600            (gnus-cache-file-name group (car cached))))
601         (goto-char (point-min))
602         (insert "220 ")
603         (princ (car cached) (current-buffer))
604         (insert " Article retrieved.\n")
605         (search-forward "\n\n" nil 'move)
606         (delete-region (point) (point-max))
607         (forward-char -1)
608         (insert "."))
609       (insert-buffer-substring cache-buf)
610       (setq cached (cdr cached)))
611     (kill-buffer cache-buf)))
612
613 (defun gnus-cache-braid-headers (headers cached-headers)
614   (if cached-headers
615       (if headers
616           (let (cached-header hrest nhrest)
617             (nconc (catch 'tag
618                      (while cached-headers
619                        (setq cached-header (car cached-headers))
620                        (if (< (mail-header-number cached-header)
621                               (mail-header-number (car headers)))
622                            (throw 'tag (nreverse cached-headers))
623                          (setq hrest headers
624                                nhrest (cdr hrest))
625                          (while (and nhrest
626                                      (> (mail-header-number cached-header)
627                                         (mail-header-number (car nhrest))))
628                            (setq hrest nhrest
629                                  nhrest (cdr nhrest))
630                            )
631                          ;;(if nhrest
632                          (setcdr hrest (cons cached-header nhrest))
633                          ;; (setq headers
634                          ;;         (nconc headers (list cached-header)))
635                          ;; (throw 'tag nil)
636                          ;;)
637                          )
638                        (setq cached-headers (cdr cached-headers))))
639                    headers))
640         (nreverse cached-headers))
641     headers))
642
643 ;;;###autoload
644 (defun gnus-jog-cache ()
645   "Go through all groups and put the articles into the cache.
646
647 Usage:
648 $ emacs -batch -l ~/.emacs -l gnus -f gnus-jog-cache"
649   (interactive)
650   (let ((gnus-mark-article-hook nil)
651         (gnus-expert-user t)
652         (nnmail-spool-file nil)
653         (mail-sources nil)
654         (gnus-use-dribble-file nil)
655         (gnus-novice-user nil)
656         (gnus-large-newsgroup nil))
657     ;; Start Gnus.
658     (gnus)
659     ;; Go through all groups...
660     (gnus-group-mark-buffer)
661     (gnus-group-iterate nil
662       (lambda (group)
663         (let (gnus-auto-select-next)
664           (gnus-summary-read-group group nil t)
665           ;; ... and enter the articles into the cache.
666           (when (eq major-mode 'gnus-summary-mode)
667             (gnus-uu-mark-buffer)
668             (gnus-cache-enter-article)
669             (kill-buffer (current-buffer))))))))
670
671 (defun gnus-cache-read-active (&optional force)
672   "Read the cache active file."
673   (gnus-make-directory gnus-cache-directory)
674   (if (or (not (file-exists-p gnus-cache-active-file))
675           (zerop (nth 7 (file-attributes gnus-cache-active-file)))
676           force)
677       ;; There is no active file, so we generate one.
678       (gnus-cache-generate-active)
679     ;; We simply read the active file.
680     (save-excursion
681       (gnus-set-work-buffer)
682       (nnheader-insert-file-contents gnus-cache-active-file)
683       (gnus-active-to-gnus-format
684        nil (setq gnus-cache-active-hashtb
685                  (gnus-make-hashtable
686                   (count-lines (point-min) (point-max)))))
687       (setq gnus-cache-active-altered nil))))
688
689 (defun gnus-cache-write-active (&optional force)
690   "Write the active hashtb to the active file."
691   (when (or force
692             (and gnus-cache-active-hashtb
693                  gnus-cache-active-altered))
694     (gnus-write-active-file gnus-cache-active-file gnus-cache-active-hashtb t)
695     ;; Mark the active hashtb as unaltered.
696     (setq gnus-cache-active-altered nil)))
697
698 (defun gnus-cache-update-active (group number &optional low)
699   "Update the upper bound of the active info of GROUP to NUMBER.
700 If LOW, update the lower bound instead."
701   (let ((active (gnus-gethash group gnus-cache-active-hashtb)))
702     (if (null active)
703         ;; We just create a new active entry for this group.
704         (gnus-sethash group (cons number number) gnus-cache-active-hashtb)
705       ;; Update the lower or upper bound.
706       (if low
707           (setcar active number)
708         (setcdr active number)))
709     ;; Mark the active hashtb as altered.
710     (setq gnus-cache-active-altered t)))
711
712 ;;;###autoload
713 (defun gnus-cache-generate-active (&optional directory)
714   "Generate the cache active file."
715   (interactive)
716   (let* ((top (null directory))
717          (directory (expand-file-name (or directory gnus-cache-directory)))
718          (files (directory-files directory 'full))
719          (group
720           (if top
721               ""
722             (string-match
723              (concat "^" (regexp-quote
724                           (file-name-as-directory
725                            (expand-file-name gnus-cache-directory))))
726              (directory-file-name directory))
727             (nnheader-replace-chars-in-string
728              (substring (directory-file-name directory) (match-end 0))
729              ?/ ?.)))
730          nums alphs)
731     (when top
732       (gnus-message 5 "Generating the cache active file...")
733       (setq gnus-cache-active-hashtb (gnus-make-hashtable 123)))
734     (when (string-match "^\\(nn[^_]+\\)_" group)
735       (setq group (replace-match "\\1:" t t group)))
736     ;; Separate articles from all other files and directories.
737     (while files
738       (if (string-match "^[0-9]+$" (file-name-nondirectory (car files)))
739           (push (string-to-int (file-name-nondirectory (pop files))) nums)
740         (push (pop files) alphs)))
741     ;; If we have nums, then this is probably a valid group.
742     (when (setq nums (sort nums '<))
743       (gnus-sethash group (cons (car nums) (gnus-last-element nums))
744                     gnus-cache-active-hashtb))
745     ;; Go through all the other files.
746     (while alphs
747       (when (and (file-directory-p (car alphs))
748                  (not (string-match "^\\."
749                                     (file-name-nondirectory (car alphs)))))
750         ;; We descend directories.
751         (gnus-cache-generate-active (car alphs)))
752       (setq alphs (cdr alphs)))
753     ;; Write the new active file.
754     (when top
755       (gnus-cache-write-active t)
756       (gnus-message 5 "Generating the cache active file...done"))))
757
758 ;;;###autoload
759 (defun gnus-cache-generate-nov-databases (dir)
760   "Generate NOV files recursively starting in DIR."
761   (interactive (list gnus-cache-directory))
762   (gnus-cache-close)
763   (let ((nnml-generate-active-function 'identity))
764     (nnml-generate-nov-databases-1 dir)))
765
766 (defun gnus-cache-move-cache (dir)
767   "Move the cache tree to somewhere else."
768   (interactive "FMove the cache tree to: ")
769   (rename-file gnus-cache-directory dir))
770
771 (provide 'gnus-cache)
772
773 ;;; gnus-cache.el ends here