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