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