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