*** empty log message ***
[gnus] / 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 'raw-text
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               (setq headers (nnheader-parse-head t))
184               (mail-header-set-number headers number)
185               (gnus-cache-change-buffer group)
186               (set-buffer (cdr gnus-cache-buffer))
187               (goto-char (point-max))
188               (forward-line -1)
189               (while (condition-case ()
190                          (when (not (bobp))
191                            (> (read (current-buffer)) number))
192                        (error
193                         ;; The line was malformed, so we just remove it!!
194                         (gnus-delete-line)
195                         t))
196                 (forward-line -1))
197               (if (bobp)
198                   (if (not (eobp))
199                       (progn
200                         (beginning-of-line)
201                         (when (< (read (current-buffer)) number)
202                           (forward-line 1)))
203                     (beginning-of-line))
204                 (forward-line 1))
205               (beginning-of-line)
206               (nnheader-insert-nov headers)
207               ;; Update the active info.
208               (set-buffer gnus-summary-buffer)
209               (gnus-cache-update-active group number)
210               (push article gnus-newsgroup-cached)
211               (gnus-summary-update-secondary-mark article))
212             t))))))
213
214 (defun gnus-cache-enter-remove-article (article)
215   "Mark ARTICLE for later possible removal."
216   (when article
217     (push article gnus-cache-removable-articles)))
218
219 (defun gnus-cache-possibly-remove-articles ()
220   "Possibly remove some of the removable articles."
221   (if (not (gnus-virtual-group-p gnus-newsgroup-name))
222       (gnus-cache-possibly-remove-articles-1)
223     (let ((arts gnus-cache-removable-articles)
224           ga)
225       (while arts
226         (when (setq ga (nnvirtual-find-group-art
227                         (gnus-group-real-name gnus-newsgroup-name) (pop arts)))
228           (let ((gnus-cache-removable-articles (list (cdr ga)))
229                 (gnus-newsgroup-name (car ga)))
230             (gnus-cache-possibly-remove-articles-1)))))
231     (setq gnus-cache-removable-articles nil)))
232
233 (defun gnus-cache-possibly-remove-articles-1 ()
234   "Possibly remove some of the removable articles."
235   (unless (eq gnus-use-cache 'passive)
236     (let ((articles gnus-cache-removable-articles)
237           (cache-articles gnus-newsgroup-cached)
238           article)
239       (gnus-cache-change-buffer gnus-newsgroup-name)
240       (while articles
241         (when (memq (setq article (pop articles)) cache-articles)
242           ;; The article was in the cache, so we see whether we are
243           ;; supposed to remove it from the cache.
244           (gnus-cache-possibly-remove-article
245            article (memq article gnus-newsgroup-marked)
246            (memq article gnus-newsgroup-dormant)
247            (or (memq article gnus-newsgroup-unreads)
248                (memq article gnus-newsgroup-unselected))))))
249     ;; The overview file might have been modified, save it
250     ;; safe because we're only called at group exit anyway.
251     (gnus-cache-save-buffers)))
252
253 (defun gnus-cache-request-article (article group)
254   "Retrieve ARTICLE in GROUP from the cache."
255   (let ((file (gnus-cache-file-name group article))
256         (buffer-read-only nil))
257     (when (file-exists-p file)
258       (erase-buffer)
259       (gnus-kill-all-overlays)
260       (let ((coding-system-for-read gnus-cache-coding-system))
261         (insert-file-contents file))
262       t)))
263
264 (defun gnus-cache-possibly-alter-active (group active)
265   "Alter the ACTIVE info for GROUP to reflect the articles in the cache."
266   (when gnus-cache-active-hashtb
267     (let ((cache-active (gnus-gethash group gnus-cache-active-hashtb)))
268       (when cache-active
269         (when (< (car cache-active) (car active))
270           (setcar active (car cache-active)))
271         (when (> (cdr cache-active) (cdr active))
272           (setcdr active (cdr cache-active)))))))
273
274 (defun gnus-cache-retrieve-headers (articles group &optional fetch-old)
275   "Retrieve the headers for ARTICLES in GROUP."
276   (let ((cached
277          (setq gnus-newsgroup-cached (gnus-cache-articles-in-group group))))
278     (if (not cached)
279         ;; No cached articles here, so we just retrieve them
280         ;; the normal way.
281         (let ((gnus-use-cache nil))
282           (gnus-retrieve-headers articles group fetch-old))
283       (let ((uncached-articles (gnus-sorted-intersection
284                                 (gnus-sorted-complement articles cached)
285                                 articles))
286             (cache-file (gnus-cache-file-name group ".overview"))
287             type)
288         ;; We first retrieve all the headers that we don't have in
289         ;; the cache.
290         (let ((gnus-use-cache nil))
291           (when uncached-articles
292             (setq type (and articles
293                             (gnus-retrieve-headers
294                              uncached-articles group fetch-old)))))
295         (gnus-cache-save-buffers)
296         ;; Then we insert the cached headers.
297         (save-excursion
298           (cond
299            ((not (file-exists-p cache-file))
300             ;; There are no cached headers.
301             type)
302            ((null type)
303             ;; There were no uncached headers (or retrieval was
304             ;; unsuccessful), so we use the cached headers exclusively.
305             (set-buffer nntp-server-buffer)
306             (erase-buffer)
307             (let ((coding-system-for-read 
308                    gnus-cache-overview-coding-system))
309               (insert-file-contents cache-file))
310             'nov)
311            ((eq type 'nov)
312             ;; We have both cached and uncached NOV headers, so we
313             ;; braid them.
314             (gnus-cache-braid-nov group cached)
315             type)
316            (t
317             ;; We braid HEADs.
318             (gnus-cache-braid-heads group (gnus-sorted-intersection
319                                            cached articles))
320             type)))))))
321
322 (defun gnus-cache-enter-article (&optional n)
323   "Enter the next N articles into the cache.
324 If not given a prefix, use the process marked articles instead.
325 Returns the list of articles entered."
326   (interactive "P")
327   (let ((articles (gnus-summary-work-articles n))
328         article out)
329     (while (setq article (pop articles))
330       (gnus-summary-remove-process-mark article)
331       (if (natnump article)
332           (when (gnus-cache-possibly-enter-article
333                  gnus-newsgroup-name 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
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       (let ((coding-system-for-read 
486             gnus-cache-overview-coding-system))
487         (insert-file-contents 
488          (or file (gnus-cache-file-name group ".overview"))))
489       (goto-char (point-min))
490       (insert "\n")
491       (goto-char (point-min)))
492     (set-buffer nntp-server-buffer)
493     (goto-char (point-min))
494     (while cached
495       (while (and (not (eobp))
496                   (< (read (current-buffer)) (car cached)))
497         (forward-line 1))
498       (beginning-of-line)
499       (save-excursion
500         (set-buffer cache-buf)
501         (if (search-forward (concat "\n" (int-to-string (car cached)) "\t")
502                             nil t)
503             (setq beg (progn (beginning-of-line) (point))
504                   end (progn (end-of-line) (point)))
505           (setq beg nil)))
506       (when beg
507         (insert-buffer-substring cache-buf beg end)
508         (insert "\n"))
509       (setq cached (cdr cached)))
510     (kill-buffer cache-buf)))
511
512 (defun gnus-cache-braid-heads (group cached)
513   (let ((cache-buf (gnus-get-buffer-create " *gnus-cache*")))
514     (save-excursion
515       (set-buffer cache-buf)
516       (erase-buffer))
517     (set-buffer nntp-server-buffer)
518     (goto-char (point-min))
519     (while cached
520       (while (and (not (eobp))
521                   (looking-at "2.. +\\([0-9]+\\) ")
522                   (< (progn (goto-char (match-beginning 1))
523                             (read (current-buffer)))
524                      (car cached)))
525         (search-forward "\n.\n" nil 'move))
526       (beginning-of-line)
527       (save-excursion
528         (set-buffer cache-buf)
529         (erase-buffer)
530         (let ((coding-system-for-read 
531                gnus-cache-coding-system))
532           (insert-file-contents (gnus-cache-file-name group (car cached))))
533         (goto-char (point-min))
534         (insert "220 ")
535         (princ (car cached) (current-buffer))
536         (insert " Article retrieved.\n")
537         (search-forward "\n\n" nil 'move)
538         (delete-region (point) (point-max))
539         (forward-char -1)
540         (insert "."))
541       (insert-buffer-substring cache-buf)
542       (setq cached (cdr cached)))
543     (kill-buffer cache-buf)))
544
545 ;;;###autoload
546 (defun gnus-jog-cache ()
547   "Go through all groups and put the articles into the cache.
548
549 Usage:
550 $ emacs -batch -l ~/.emacs -l gnus -f gnus-jog-cache"
551   (interactive)
552   (let ((gnus-mark-article-hook nil)
553         (gnus-expert-user t)
554         (nnmail-spool-file nil)
555         (gnus-use-dribble-file nil)
556         (gnus-novice-user nil)
557         (gnus-large-newsgroup nil))
558     ;; Start Gnus.
559     (gnus)
560     ;; Go through all groups...
561     (gnus-group-mark-buffer)
562     (gnus-group-iterate nil
563       (lambda (group)
564         (let (gnus-auto-select-next)
565           (gnus-summary-read-group group nil t)
566           ;; ... and enter the articles into the cache.
567           (when (eq major-mode 'gnus-summary-mode)
568             (gnus-uu-mark-buffer)
569             (gnus-cache-enter-article)
570             (kill-buffer (current-buffer))))))))
571
572 (defun gnus-cache-read-active (&optional force)
573   "Read the cache active file."
574   (gnus-make-directory gnus-cache-directory)
575   (if (or (not (file-exists-p gnus-cache-active-file))
576           (zerop (nth 7 (file-attributes gnus-cache-active-file)))
577           force)
578       ;; There is no active file, so we generate one.
579       (gnus-cache-generate-active)
580     ;; We simply read the active file.
581     (save-excursion
582       (gnus-set-work-buffer)
583       (insert-file-contents gnus-cache-active-file)
584       (gnus-active-to-gnus-format
585        nil (setq gnus-cache-active-hashtb
586                  (gnus-make-hashtable
587                   (count-lines (point-min) (point-max)))))
588       (setq gnus-cache-active-altered nil))))
589
590 (defun gnus-cache-write-active (&optional force)
591   "Write the active hashtb to the active file."
592   (when (or force
593             (and gnus-cache-active-hashtb
594                  gnus-cache-active-altered))
595     (gnus-write-active-file gnus-cache-active-file gnus-cache-active-hashtb)
596     ;; Mark the active hashtb as unaltered.
597     (setq gnus-cache-active-altered nil)))
598
599 (defun gnus-cache-update-active (group number &optional low)
600   "Update the upper bound of the active info of GROUP to NUMBER.
601 If LOW, update the lower bound instead."
602   (let ((active (gnus-gethash group gnus-cache-active-hashtb)))
603     (if (null active)
604         ;; We just create a new active entry for this group.
605         (gnus-sethash group (cons number number) gnus-cache-active-hashtb)
606       ;; Update the lower or upper bound.
607       (if low
608           (setcar active number)
609         (setcdr active number)))
610     ;; Mark the active hashtb as altered.
611     (setq gnus-cache-active-altered t)))
612
613 ;;;###autoload
614 (defun gnus-cache-generate-active (&optional directory)
615   "Generate the cache active file."
616   (interactive)
617   (let* ((top (null directory))
618          (directory (expand-file-name (or directory gnus-cache-directory)))
619          (files (directory-files directory 'full))
620          (group
621           (if top
622               ""
623             (string-match
624              (concat "^" (regexp-quote
625                           (file-name-as-directory
626                            (expand-file-name gnus-cache-directory))))
627              (directory-file-name directory))
628             (nnheader-replace-chars-in-string
629              (substring (directory-file-name directory) (match-end 0))
630              ?/ ?.)))
631          nums alphs)
632     (when top
633       (gnus-message 5 "Generating the cache active file...")
634       (setq gnus-cache-active-hashtb (gnus-make-hashtable 123)))
635     (when (string-match "^\\(nn[^_]+\\)_" group)
636       (setq group (replace-match "\\1:" t t group)))
637     ;; Separate articles from all other files and directories.
638     (while files
639       (if (string-match "^[0-9]+$" (file-name-nondirectory (car files)))
640           (push (string-to-int (file-name-nondirectory (pop files))) nums)
641         (push (pop files) alphs)))
642     ;; If we have nums, then this is probably a valid group.
643     (when (setq nums (sort nums '<))
644       (gnus-sethash group (cons (car nums) (gnus-last-element nums))
645                     gnus-cache-active-hashtb))
646     ;; Go through all the other files.
647     (while alphs
648       (when (and (file-directory-p (car alphs))
649                  (not (string-match "^\\."
650                                     (file-name-nondirectory (car alphs)))))
651         ;; We descend directories.
652         (gnus-cache-generate-active (car alphs)))
653       (setq alphs (cdr alphs)))
654     ;; Write the new active file.
655     (when top
656       (gnus-cache-write-active t)
657       (gnus-message 5 "Generating the cache active file...done"))))
658
659 ;;;###autoload
660 (defun gnus-cache-generate-nov-databases (dir)
661   "Generate NOV files recursively starting in DIR."
662   (interactive (list gnus-cache-directory))
663   (gnus-cache-close)
664   (let ((nnml-generate-active-function 'identity))
665     (nnml-generate-nov-databases-1 dir)))
666
667 (defun gnus-cache-move-cache (dir)
668   "Move the cache tree to somewhere else."
669   (interactive "FMove the cache tree to: ")
670   (rename-file gnus-cache-directory dir))
671
672 (provide 'gnus-cache)
673
674 ;;; gnus-cache.el ends here