* lisp/gnus-art.el (article-decode-encoded-words): Renamed from
[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       ;; Insert the contents of this group's cache overview.
407       (erase-buffer)
408       (let ((file (gnus-cache-file-name group ".overview")))
409         (when (file-exists-p file)
410           (nnheader-insert-file-contents file)))
411       ;; We have a fresh (empty/just loaded) buffer,
412       ;; mark it as unmodified to save a redundant write later.
413       (set-buffer-modified-p nil))))
414
415 ;; Return whether an article is a member of a class.
416 (defun gnus-cache-member-of-class (class ticked dormant unread)
417   (or (and ticked (memq 'ticked class))
418       (and dormant (memq 'dormant class))
419       (and unread (memq 'unread class))
420       (and (not unread) (not ticked) (not dormant) (memq 'read class))))
421
422 (defun gnus-cache-file-name (group article)
423   (concat (file-name-as-directory gnus-cache-directory)
424           (file-name-as-directory
425            (nnheader-translate-file-chars
426             (if (gnus-use-long-file-name 'not-cache)
427                 group
428               (let ((group (nnheader-replace-chars-in-string group ?/ ?_)))
429                 ;; Translate the first colon into a slash.
430                 (when (string-match ":" group)
431                   (aset group (match-beginning 0) ?/))
432                 (nnheader-replace-chars-in-string group ?. ?/)))
433             t))
434           (if (stringp article) article (int-to-string article))))
435
436 (defun gnus-cache-update-article (group article)
437   "If ARTICLE is in the cache, remove it and re-enter it."
438   (gnus-cache-change-buffer group)
439   (when (gnus-cache-possibly-remove-article article nil nil nil t)    
440     (let ((gnus-use-cache nil))
441       (gnus-cache-possibly-enter-article
442        gnus-newsgroup-name article (gnus-summary-article-header article)
443        nil nil nil t))))
444
445 (defun gnus-cache-possibly-remove-article (article ticked dormant unread
446                                                    &optional force)
447   "Possibly remove ARTICLE from the cache."
448   (let ((group gnus-newsgroup-name)
449         (number article)
450         file)
451     ;; If this is a virtual group, we find the real group.
452     (when (gnus-virtual-group-p group)
453       (let ((result (nnvirtual-find-group-art
454                      (gnus-group-real-name group) article)))
455         (setq group (car result)
456               number (cdr result))))
457     (setq file (gnus-cache-file-name group number))
458     (when (and (file-exists-p file)
459                (or force
460                    (gnus-cache-member-of-class
461                     gnus-cache-remove-articles ticked dormant unread)))
462       (save-excursion
463         (delete-file file)
464         (set-buffer (cdr gnus-cache-buffer))
465         (goto-char (point-min))
466         (when (or (looking-at (concat (int-to-string number) "\t"))
467                   (search-forward (concat "\n" (int-to-string number) "\t")
468                                   (point-max) t))
469           (delete-region (progn (beginning-of-line) (point))
470                          (progn (forward-line 1) (point)))))
471       (setq gnus-newsgroup-cached
472             (delq article gnus-newsgroup-cached))
473       (gnus-summary-update-secondary-mark article)
474       t)))
475
476 (defun gnus-cache-articles-in-group (group)
477   "Return a sorted list of cached articles in GROUP."
478   (let ((dir (file-name-directory (gnus-cache-file-name group 1)))
479         articles)
480     (when (file-exists-p dir)
481       (setq articles
482             (sort (mapcar (lambda (name) (string-to-int name))
483                           (directory-files dir nil "^[0-9]+$" t))
484                   '<))
485       ;; Update the cache active file, just to synch more.
486       (when articles
487         (gnus-cache-update-active group (car articles) t)
488         (gnus-cache-update-active group (car (last articles))))
489       articles)))
490
491 (defun gnus-cache-braid-nov (group cached &optional file)
492   (let ((cache-buf (gnus-get-buffer-create " *gnus-cache*"))
493         beg end)
494     (gnus-cache-save-buffers)
495     (save-excursion
496       (set-buffer cache-buf)
497       (erase-buffer)
498       (nnheader-insert-file-contents (or file (gnus-cache-file-name group ".overview")))
499       (goto-char (point-min))
500       (insert "\n")
501       (goto-char (point-min)))
502     (set-buffer nntp-server-buffer)
503     (goto-char (point-min))
504     (while cached
505       (while (and (not (eobp))
506                   (< (read (current-buffer)) (car cached)))
507         (forward-line 1))
508       (beginning-of-line)
509       (save-excursion
510         (set-buffer cache-buf)
511         (if (search-forward (concat "\n" (int-to-string (car cached)) "\t")
512                             nil t)
513             (setq beg (progn (beginning-of-line) (point))
514                   end (progn (end-of-line) (point)))
515           (setq beg nil)))
516       (when beg
517         (insert-buffer-substring cache-buf beg end)
518         (insert "\n"))
519       (setq cached (cdr cached)))
520     (kill-buffer cache-buf)))
521
522 (defun gnus-cache-braid-heads (group cached)
523   (let ((cache-buf (gnus-get-buffer-create " *gnus-cache*")))
524     (save-excursion
525       (set-buffer cache-buf)
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     (with-temp-file 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