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