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