*** empty log message ***
[gnus] / lisp / gnus-cache.el
1 ;;; gnus-cache.el --- cache interface for Gnus
2 ;; Copyright (C) 1995,96,97,98 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 headers ticked dormant unread &optional force)
149   (when (and (or force (not (eq gnus-use-cache 'passive)))
150              (numberp article)
151              (> article 0)
152              (vectorp headers))         ; This might be a dummy article.
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               headers (copy-sequence headers))
159         (mail-header-set-number headers (cdr result))))
160     (let ((number (mail-header-number headers))
161           file)
162       (when (and number
163                  (> number 0)           ; Reffed article.
164                  (or force
165                      (and (or (not gnus-cacheable-groups)
166                               (string-match gnus-cacheable-groups group))
167                           (or (not gnus-uncacheable-groups)
168                               (not (string-match
169                                     gnus-uncacheable-groups group)))
170                           (gnus-cache-member-of-class
171                            gnus-cache-enter-articles ticked dormant unread)))
172                  (not (file-exists-p (setq file (gnus-cache-file-name
173                                                  group number)))))
174         ;; Possibly create the cache directory.
175         (gnus-make-directory (file-name-directory file))
176         ;; Save the article in the cache.
177         (if (file-exists-p file)
178             t                           ; The article already is saved.
179           (save-excursion
180             (set-buffer nntp-server-buffer)
181             (let ((gnus-use-cache nil)
182                   (gnus-article-decode-hook nil))
183               (gnus-request-article-this-buffer number group))
184             (when (> (buffer-size) 0)
185               (gnus-write-buffer file)
186               (gnus-cache-change-buffer group)
187               (set-buffer (cdr gnus-cache-buffer))
188               (goto-char (point-max))
189               (forward-line -1)
190               (while (condition-case ()
191                          (when (not (bobp))
192                            (> (read (current-buffer)) number))
193                        (error
194                         ;; The line was malformed, so we just remove it!!
195                         (gnus-delete-line)
196                         t))
197                 (forward-line -1))
198               (if (bobp)
199                   (if (not (eobp))
200                       (progn
201                         (beginning-of-line)
202                         (when (< (read (current-buffer)) number)
203                           (forward-line 1)))
204                     (beginning-of-line))
205                 (forward-line 1))
206               (beginning-of-line)
207               (nnheader-insert-nov headers)
208               ;; Update the active info.
209               (set-buffer gnus-summary-buffer)
210               (gnus-cache-update-active group number)
211               (push article gnus-newsgroup-cached)
212               (gnus-summary-update-secondary-mark article))
213             t))))))
214
215 (defun gnus-cache-enter-remove-article (article)
216   "Mark ARTICLE for later possible removal."
217   (when article
218     (push article gnus-cache-removable-articles)))
219
220 (defun gnus-cache-possibly-remove-articles ()
221   "Possibly remove some of the removable articles."
222   (if (not (gnus-virtual-group-p gnus-newsgroup-name))
223       (gnus-cache-possibly-remove-articles-1)
224     (let ((arts gnus-cache-removable-articles)
225           ga)
226       (while arts
227         (when (setq ga (nnvirtual-find-group-art
228                         (gnus-group-real-name gnus-newsgroup-name) (pop arts)))
229           (let ((gnus-cache-removable-articles (list (cdr ga)))
230                 (gnus-newsgroup-name (car ga)))
231             (gnus-cache-possibly-remove-articles-1)))))
232     (setq gnus-cache-removable-articles nil)))
233
234 (defun gnus-cache-possibly-remove-articles-1 ()
235   "Possibly remove some of the removable articles."
236   (unless (eq gnus-use-cache 'passive)
237     (let ((articles gnus-cache-removable-articles)
238           (cache-articles gnus-newsgroup-cached)
239           article)
240       (gnus-cache-change-buffer gnus-newsgroup-name)
241       (while articles
242         (when (memq (setq article (pop articles)) cache-articles)
243           ;; The article was in the cache, so we see whether we are
244           ;; supposed to remove it from the cache.
245           (gnus-cache-possibly-remove-article
246            article (memq article gnus-newsgroup-marked)
247            (memq article gnus-newsgroup-dormant)
248            (or (memq article gnus-newsgroup-unreads)
249                (memq article gnus-newsgroup-unselected))))))
250     ;; The overview file might have been modified, save it
251     ;; safe because we're only called at group exit anyway.
252     (gnus-cache-save-buffers)))
253
254 (defun gnus-cache-request-article (article group)
255   "Retrieve ARTICLE in GROUP from the cache."
256   (let ((file (gnus-cache-file-name group article))
257         (buffer-read-only nil))
258     (when (file-exists-p file)
259       (erase-buffer)
260       (gnus-kill-all-overlays)
261       (let ((coding-system-for-read gnus-cache-coding-system))
262         (insert-file-contents file))
263       t)))
264
265 (defun gnus-cache-possibly-alter-active (group active)
266   "Alter the ACTIVE info for GROUP to reflect the articles in the cache."
267   (when gnus-cache-active-hashtb
268     (let ((cache-active (gnus-gethash group gnus-cache-active-hashtb)))
269       (when cache-active
270         (when (< (car cache-active) (car active))
271           (setcar active (car cache-active)))
272         (when (> (cdr cache-active) (cdr active))
273           (setcdr active (cdr cache-active)))))))
274
275 (defun gnus-cache-retrieve-headers (articles group &optional fetch-old)
276   "Retrieve the headers for ARTICLES in GROUP."
277   (let ((cached
278          (setq gnus-newsgroup-cached (gnus-cache-articles-in-group group))))
279     (if (not cached)
280         ;; No cached articles here, so we just retrieve them
281         ;; the normal way.
282         (let ((gnus-use-cache nil))
283           (gnus-retrieve-headers articles group fetch-old))
284       (let ((uncached-articles (gnus-sorted-intersection
285                                 (gnus-sorted-complement articles cached)
286                                 articles))
287             (cache-file (gnus-cache-file-name group ".overview"))
288             type)
289         ;; We first retrieve all the headers that we don't have in
290         ;; the cache.
291         (let ((gnus-use-cache nil))
292           (when uncached-articles
293             (setq type (and articles
294                             (gnus-retrieve-headers
295                              uncached-articles group fetch-old)))))
296         (gnus-cache-save-buffers)
297         ;; Then we insert the cached headers.
298         (save-excursion
299           (cond
300            ((not (file-exists-p cache-file))
301             ;; There are no cached headers.
302             type)
303            ((null type)
304             ;; There were no uncached headers (or retrieval was
305             ;; unsuccessful), so we use the cached headers exclusively.
306             (set-buffer nntp-server-buffer)
307             (erase-buffer)
308             (insert-file-contents cache-file)
309             'nov)
310            ((eq type 'nov)
311             ;; We have both cached and uncached NOV headers, so we
312             ;; braid them.
313             (gnus-cache-braid-nov group cached)
314             type)
315            (t
316             ;; We braid HEADs.
317             (gnus-cache-braid-heads group (gnus-sorted-intersection
318                                            cached articles))
319             type)))))))
320
321 (defun gnus-cache-enter-article (&optional n)
322   "Enter the next N articles into the cache.
323 If not given a prefix, use the process marked articles instead.
324 Returns the list of articles entered."
325   (interactive "P")
326   (let ((articles (gnus-summary-work-articles n))
327         article out)
328     (while (setq article (pop articles))
329       (gnus-summary-remove-process-mark article)
330       (if (natnump article)
331           (when (gnus-cache-possibly-enter-article
332                  gnus-newsgroup-name article
333                  (gnus-summary-article-header article)
334                  nil nil nil t)
335             (push article out))
336         (gnus-message 2 "Can't cache article %d" article))
337       (gnus-summary-update-secondary-mark article))
338     (gnus-summary-next-subject 1)
339     (gnus-summary-position-point)
340     (nreverse out)))
341
342 (defun gnus-cache-remove-article (n)
343   "Remove the next N articles from the cache.
344 If not given a prefix, use the process marked articles instead.
345 Returns the list of articles removed."
346   (interactive "P")
347   (gnus-cache-change-buffer gnus-newsgroup-name)
348   (let ((articles (gnus-summary-work-articles n))
349         article out)
350     (while articles
351       (setq article (pop articles))
352       (gnus-summary-remove-process-mark article)
353       (when (gnus-cache-possibly-remove-article article nil nil nil t)
354         (push article out))
355       (gnus-summary-update-secondary-mark article))
356     (gnus-summary-next-subject 1)
357     (gnus-summary-position-point)
358     (nreverse out)))
359
360 (defun gnus-cached-article-p (article)
361   "Say whether ARTICLE is cached in the current group."
362   (memq article gnus-newsgroup-cached))
363
364 (defun gnus-summary-insert-cached-articles ()
365   "Insert all the articles cached for this group into the current buffer."
366   (interactive)
367   (let ((cached (sort (copy-sequence gnus-newsgroup-cached) '<))
368         (gnus-verbose (max 6 gnus-verbose)))
369     (unless cached
370       (gnus-message 3 "No cached articles for this group"))
371     (while cached
372       (gnus-summary-goto-subject (pop cached) t))))
373
374 (defalias 'gnus-summary-limit-include-cached
375   'gnus-summary-insert-cached-articles)
376
377 ;;; Internal functions.
378
379 (defun gnus-cache-change-buffer (group)
380   (and gnus-cache-buffer
381        ;; See if the current group's overview cache has been loaded.
382        (or (string= group (car gnus-cache-buffer))
383            ;; Another overview cache is current, save it.
384            (gnus-cache-save-buffers)))
385   ;; if gnus-cache buffer is nil, create it
386   (unless gnus-cache-buffer
387     ;; Create cache buffer
388     (save-excursion
389       (setq gnus-cache-buffer
390             (cons group
391                   (set-buffer (gnus-get-buffer-create
392                                " *gnus-cache-overview*"))))
393       ;; Insert the contents of this group's cache overview.
394       (erase-buffer)
395       (let ((file (gnus-cache-file-name group ".overview")))
396         (when (file-exists-p file)
397           (nnheader-insert-file-contents file)))
398       ;; We have a fresh (empty/just loaded) buffer,
399       ;; mark it as unmodified to save a redundant write later.
400       (set-buffer-modified-p nil))))
401
402 ;; Return whether an article is a member of a class.
403 (defun gnus-cache-member-of-class (class ticked dormant unread)
404   (or (and ticked (memq 'ticked class))
405       (and dormant (memq 'dormant class))
406       (and unread (memq 'unread class))
407       (and (not unread) (not ticked) (not dormant) (memq 'read class))))
408
409 (defun gnus-cache-file-name (group article)
410   (concat (file-name-as-directory gnus-cache-directory)
411           (file-name-as-directory
412            (nnheader-translate-file-chars
413             (if (gnus-use-long-file-name 'not-cache)
414                 group
415               (let ((group (nnheader-replace-chars-in-string group ?/ ?_)))
416                 ;; Translate the first colon into a slash.
417                 (when (string-match ":" group)
418                   (aset group (match-beginning 0) ?/))
419                 (nnheader-replace-chars-in-string group ?. ?/)))
420             t))
421           (if (stringp article) article (int-to-string article))))
422
423 (defun gnus-cache-update-article (group article)
424   "If ARTICLE is in the cache, remove it and re-enter it."
425   (gnus-cache-change-buffer group)
426   (when (gnus-cache-possibly-remove-article article nil nil nil t)    
427     (let ((gnus-use-cache nil))
428       (gnus-cache-possibly-enter-article
429        gnus-newsgroup-name article (gnus-summary-article-header article)
430        nil nil nil t))))
431
432 (defun gnus-cache-possibly-remove-article (article ticked dormant unread
433                                                    &optional force)
434   "Possibly remove ARTICLE from the cache."
435   (let ((group gnus-newsgroup-name)
436         (number article)
437         file)
438     ;; If this is a virtual group, we find the real group.
439     (when (gnus-virtual-group-p group)
440       (let ((result (nnvirtual-find-group-art
441                      (gnus-group-real-name group) article)))
442         (setq group (car result)
443               number (cdr result))))
444     (setq file (gnus-cache-file-name group number))
445     (when (and (file-exists-p file)
446                (or force
447                    (gnus-cache-member-of-class
448                     gnus-cache-remove-articles ticked dormant unread)))
449       (save-excursion
450         (delete-file file)
451         (set-buffer (cdr gnus-cache-buffer))
452         (goto-char (point-min))
453         (when (or (looking-at (concat (int-to-string number) "\t"))
454                   (search-forward (concat "\n" (int-to-string number) "\t")
455                                   (point-max) t))
456           (delete-region (progn (beginning-of-line) (point))
457                          (progn (forward-line 1) (point)))))
458       (setq gnus-newsgroup-cached
459             (delq article gnus-newsgroup-cached))
460       (gnus-summary-update-secondary-mark article)
461       t)))
462
463 (defun gnus-cache-articles-in-group (group)
464   "Return a sorted list of cached articles in GROUP."
465   (let ((dir (file-name-directory (gnus-cache-file-name group 1)))
466         articles)
467     (when (file-exists-p dir)
468       (setq articles
469             (sort (mapcar (lambda (name) (string-to-int name))
470                           (directory-files dir nil "^[0-9]+$" t))
471                   '<))
472       ;; Update the cache active file, just to synch more.
473       (when articles
474         (gnus-cache-update-active group (car articles) t)
475         (gnus-cache-update-active group (car (last articles))))
476       articles)))
477
478 (defun gnus-cache-braid-nov (group cached &optional file)
479   (let ((cache-buf (gnus-get-buffer-create " *gnus-cache*"))
480         beg end)
481     (gnus-cache-save-buffers)
482     (save-excursion
483       (set-buffer cache-buf)
484       (erase-buffer)
485       (insert-file-contents (or file (gnus-cache-file-name group ".overview")))
486       (goto-char (point-min))
487       (insert "\n")
488       (goto-char (point-min)))
489     (set-buffer nntp-server-buffer)
490     (goto-char (point-min))
491     (while cached
492       (while (and (not (eobp))
493                   (< (read (current-buffer)) (car cached)))
494         (forward-line 1))
495       (beginning-of-line)
496       (save-excursion
497         (set-buffer cache-buf)
498         (if (search-forward (concat "\n" (int-to-string (car cached)) "\t")
499                             nil t)
500             (setq beg (progn (beginning-of-line) (point))
501                   end (progn (end-of-line) (point)))
502           (setq beg nil)))
503       (when beg
504         (insert-buffer-substring cache-buf beg end)
505         (insert "\n"))
506       (setq cached (cdr cached)))
507     (kill-buffer cache-buf)))
508
509 (defun gnus-cache-braid-heads (group cached)
510   (let ((cache-buf (gnus-get-buffer-create " *gnus-cache*")))
511     (save-excursion
512       (set-buffer cache-buf)
513       (erase-buffer))
514     (set-buffer nntp-server-buffer)
515     (goto-char (point-min))
516     (while cached
517       (while (and (not (eobp))
518                   (looking-at "2.. +\\([0-9]+\\) ")
519                   (< (progn (goto-char (match-beginning 1))
520                             (read (current-buffer)))
521                      (car cached)))
522         (search-forward "\n.\n" nil 'move))
523       (beginning-of-line)
524       (save-excursion
525         (set-buffer cache-buf)
526         (erase-buffer)
527         (insert-file-contents (gnus-cache-file-name group (car cached)))
528         (goto-char (point-min))
529         (insert "220 ")
530         (princ (car cached) (current-buffer))
531         (insert " Article retrieved.\n")
532         (search-forward "\n\n" nil 'move)
533         (delete-region (point) (point-max))
534         (forward-char -1)
535         (insert "."))
536       (insert-buffer-substring cache-buf)
537       (setq cached (cdr cached)))
538     (kill-buffer cache-buf)))
539
540 ;;;###autoload
541 (defun gnus-jog-cache ()
542   "Go through all groups and put the articles into the cache.
543
544 Usage:
545 $ emacs -batch -l ~/.emacs -l gnus -f gnus-jog-cache"
546   (interactive)
547   (let ((gnus-mark-article-hook nil)
548         (gnus-expert-user t)
549         (nnmail-spool-file nil)
550         (gnus-use-dribble-file nil)
551         (gnus-novice-user nil)
552         (gnus-large-newsgroup nil))
553     ;; Start Gnus.
554     (gnus)
555     ;; Go through all groups...
556     (gnus-group-mark-buffer)
557     (gnus-group-iterate nil
558       (lambda (group)
559         (let (gnus-auto-select-next)
560           (gnus-summary-read-group group nil t)
561           ;; ... and enter the articles into the cache.
562           (when (eq major-mode 'gnus-summary-mode)
563             (gnus-uu-mark-buffer)
564             (gnus-cache-enter-article)
565             (kill-buffer (current-buffer))))))))
566
567 (defun gnus-cache-read-active (&optional force)
568   "Read the cache active file."
569   (gnus-make-directory gnus-cache-directory)
570   (if (or (not (file-exists-p gnus-cache-active-file))
571           (not (zerop (nth 7 (file-attributes gnus-cache-active-file))))
572           force)
573       ;; There is no active file, so we generate one.
574       (gnus-cache-generate-active)
575     ;; We simply read the active file.
576     (save-excursion
577       (gnus-set-work-buffer)
578       (insert-file-contents gnus-cache-active-file)
579       (gnus-active-to-gnus-format
580        nil (setq gnus-cache-active-hashtb
581                  (gnus-make-hashtable
582                   (count-lines (point-min) (point-max)))))
583       (setq gnus-cache-active-altered nil))))
584
585 (defun gnus-cache-write-active (&optional force)
586   "Write the active hashtb to the active file."
587   (when (or force
588             (and gnus-cache-active-hashtb
589                  gnus-cache-active-altered))
590     (with-temp-file gnus-cache-active-file
591       (mapatoms
592        (lambda (sym)
593          (when (and sym (boundp sym))
594            (insert (format "%s %d %d y\n"
595                            (symbol-name sym) (cdr (symbol-value sym))
596                            (car (symbol-value sym))))))
597        gnus-cache-active-hashtb))
598     ;; Mark the active hashtb as unaltered.
599     (setq gnus-cache-active-altered nil)))
600
601 (defun gnus-cache-update-active (group number &optional low)
602   "Update the upper bound of the active info of GROUP to NUMBER.
603 If LOW, update the lower bound instead."
604   (let ((active (gnus-gethash group gnus-cache-active-hashtb)))
605     (if (null active)
606         ;; We just create a new active entry for this group.
607         (gnus-sethash group (cons number number) gnus-cache-active-hashtb)
608       ;; Update the lower or upper bound.
609       (if low
610           (setcar active number)
611         (setcdr active number)))
612     ;; Mark the active hashtb as altered.
613     (setq gnus-cache-active-altered t)))
614
615 ;;;###autoload
616 (defun gnus-cache-generate-active (&optional directory)
617   "Generate the cache active file."
618   (interactive)
619   (let* ((top (null directory))
620          (directory (expand-file-name (or directory gnus-cache-directory)))
621          (files (directory-files directory 'full))
622          (group
623           (if top
624               ""
625             (string-match
626              (concat "^" (regexp-quote
627                           (file-name-as-directory
628                            (expand-file-name gnus-cache-directory))))
629              (directory-file-name directory))
630             (nnheader-replace-chars-in-string
631              (substring (directory-file-name directory) (match-end 0))
632              ?/ ?.)))
633          nums alphs)
634     (when top
635       (gnus-message 5 "Generating the cache active file...")
636       (setq gnus-cache-active-hashtb (gnus-make-hashtable 123)))
637     (when (string-match "^\\(nn[^_]+\\)_" group)
638       (setq group (replace-match "\\1:" t t group)))
639     ;; Separate articles from all other files and directories.
640     (while files
641       (if (string-match "^[0-9]+$" (file-name-nondirectory (car files)))
642           (push (string-to-int (file-name-nondirectory (pop files))) nums)
643         (push (pop files) alphs)))
644     ;; If we have nums, then this is probably a valid group.
645     (when (setq nums (sort nums '<))
646       (gnus-sethash group (cons (car nums) (gnus-last-element nums))
647                     gnus-cache-active-hashtb))
648     ;; Go through all the other files.
649     (while alphs
650       (when (and (file-directory-p (car alphs))
651                  (not (string-match "^\\."
652                                     (file-name-nondirectory (car alphs)))))
653         ;; We descend directories.
654         (gnus-cache-generate-active (car alphs)))
655       (setq alphs (cdr alphs)))
656     ;; Write the new active file.
657     (when top
658       (gnus-cache-write-active t)
659       (gnus-message 5 "Generating the cache active file...done"))))
660
661 ;;;###autoload
662 (defun gnus-cache-generate-nov-databases (dir)
663   "Generate NOV files recursively starting in DIR."
664   (interactive (list gnus-cache-directory))
665   (gnus-cache-close)
666   (let ((nnml-generate-active-function 'identity))
667     (nnml-generate-nov-databases-1 dir)))
668
669 (defun gnus-cache-move-cache (dir)
670   "Move the cache tree to somewhere else."
671   (interactive "FMove the cache tree to: ")
672   (rename-file gnus-cache-directory dir))
673
674 (provide 'gnus-cache)
675
676 ;;; gnus-cache.el ends here