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