Feedback from `t-gnus-6_15-quimby' branch.
[elisp/gnus.git-] / lisp / gnus-cache.el
1 ;;; gnus-cache.el --- cache interface for Gnus
2 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;;         Tatsuya Ichikawa <t-ichi@po.shiojiri.ne.jp>
7 ;;         MORIOKA Tomohiko <morioka@jaist.ac.jp>
8 ;; Keywords: news
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
26
27 ;;; Commentary:
28
29 ;;; Code:
30
31 (eval-when-compile (require 'cl))
32 (eval-when-compile (require 'gnus-clfns))
33
34 (require 'gnus)
35 (require 'gnus-int)
36 (require 'gnus-range)
37 (require 'gnus-start)
38 (eval-when-compile
39   (require 'gnus-sum))
40
41 (defcustom gnus-cache-active-file
42   (expand-file-name "active" gnus-cache-directory)
43   "*The cache active file."
44   :group 'gnus-cache
45   :type 'file)
46
47 (defcustom gnus-cache-enter-articles '(ticked dormant)
48   "Classes of articles to enter into the cache."
49   :group 'gnus-cache
50   :type '(set (const ticked) (const dormant) (const unread) (const read)))
51
52 (defcustom gnus-cache-remove-articles '(read)
53   "Classes of articles to remove from the cache."
54   :group 'gnus-cache
55   :type '(set (const ticked) (const dormant) (const unread) (const read)))
56
57 (defcustom gnus-cacheable-groups nil
58   "*Groups that match this regexp will be cached.
59
60 If you only want to cache your nntp groups, you could set this
61 variable to \"^nntp\".
62
63 If a group matches both gnus-cacheable-groups and gnus-uncacheable-groups
64 it's not cached."
65   :group 'gnus-cache
66   :type '(choice (const :tag "off" nil)
67                  regexp))
68
69 (defcustom gnus-uncacheable-groups nil
70   "*Groups that match this regexp will not be cached.
71
72 If you want to avoid caching your nnml groups, you could set this
73 variable to \"^nnml\".
74
75 If a group matches both gnus-cacheable-groups and gnus-uncacheable-groups
76 it's not cached."
77   :group 'gnus-cache
78   :type '(choice (const :tag "off" nil)
79                  regexp))
80
81 (defvar gnus-cache-overview-coding-system 'raw-text
82   "Coding system used on Gnus cache files.")
83
84 (defvar gnus-cache-coding-system 'raw-text
85   "Coding system used on Gnus cache files.")
86
87 \f
88
89 ;;; Internal variables.
90
91 (defvar gnus-cache-removable-articles nil)
92 (defvar gnus-cache-buffer nil)
93 (defvar gnus-cache-active-hashtb nil)
94 (defvar gnus-cache-active-altered nil)
95 (defvar gnus-cache-write-file-coding-system 'raw-text)
96
97 (eval-and-compile
98   (autoload 'nnml-generate-nov-databases-1 "nnml")
99   (autoload 'nnvirtual-find-group-art "nnvirtual"))
100
101 \f
102
103 ;;; Functions called from Gnus.
104
105 (defun gnus-cache-open ()
106   "Initialize the cache."
107   (when (or (file-exists-p gnus-cache-directory)
108             (and gnus-use-cache
109                  (not (eq gnus-use-cache 'passive))))
110     (gnus-cache-read-active)))
111
112 ;; Complexities of byte-compiling make this kludge necessary.  Eeek.
113 (ignore-errors
114   (gnus-add-shutdown 'gnus-cache-close 'gnus))
115
116 (defun gnus-cache-close ()
117   "Shut down the cache."
118   (gnus-cache-write-active)
119   (gnus-cache-save-buffers)
120   (setq gnus-cache-active-hashtb nil))
121
122 (defun gnus-cache-save-buffers ()
123   ;; save the overview buffer if it exists and has been modified
124   ;; delete empty cache subdirectories
125   (when gnus-cache-buffer
126     (let ((buffer (cdr gnus-cache-buffer))
127           (overview-file (gnus-cache-file-name
128                           (car gnus-cache-buffer) ".overview")))
129       ;; write the overview only if it was modified
130       (when (buffer-modified-p buffer)
131         (save-excursion
132           (set-buffer buffer)
133           (if (> (buffer-size) 0)
134               ;; Non-empty overview, write it to a file.
135               (gnus-write-buffer-as-coding-system
136                gnus-cache-overview-coding-system overview-file)
137             ;; Empty overview file, remove it
138             (when (file-exists-p overview-file)
139               (delete-file overview-file))
140             ;; If possible, remove group's cache subdirectory.
141             (condition-case nil
142                 ;; FIXME: we can detect the error type and warn the user
143                 ;; of any inconsistencies (articles w/o nov entries?).
144                 ;; for now, just be conservative...delete only if safe -- sj
145                 (delete-directory (file-name-directory overview-file))
146               (error nil)))))
147       ;; Kill the buffer -- it's either unmodified or saved.
148       (gnus-kill-buffer buffer)
149       (setq gnus-cache-buffer nil))))
150
151 (defun gnus-cache-possibly-enter-article
152   (group article headers ticked dormant unread &optional force)
153   (when (and (or force (not (eq gnus-use-cache 'passive)))
154              (numberp article)
155              (> article 0)              ; This might be a dummy article.
156              (vectorp headers))
157     (let ((number article) file)
158       ;; If this is a virtual group, we find the real group.
159       (when (gnus-virtual-group-p group)
160         (let ((result (nnvirtual-find-group-art
161                        (gnus-group-real-name group) article)))
162           (setq group (car result)
163                 number (cdr result))))
164       (when (and number
165                  (> number 0)           ; Reffed article.
166                  (or force
167                      (and (gnus-cache-fully-p 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             (require 'gnus-art)
180             (let ((gnus-use-cache nil)
181                   (gnus-article-decode-hook nil))
182               (gnus-request-article-this-buffer number group))
183             (when (> (buffer-size) 0)
184               (gnus-write-buffer-as-coding-system
185                gnus-cache-write-file-coding-system file)
186               (setq headers (nnheader-parse-head t))
187               (mail-header-set-number headers number)
188               (gnus-cache-change-buffer group)
189               (set-buffer (cdr gnus-cache-buffer))
190               (goto-char (point-max))
191               (forward-line -1)
192               (while (condition-case ()
193                          (when (not (bobp))
194                            (> (read (current-buffer)) number))
195                        (error
196                         ;; The line was malformed, so we just remove it!!
197                         (gnus-delete-line)
198                         t))
199                 (forward-line -1))
200               (if (bobp)
201                   (if (not (eobp))
202                       (progn
203                         (beginning-of-line)
204                         (when (< (read (current-buffer)) number)
205                           (forward-line 1)))
206                     (beginning-of-line))
207                 (forward-line 1))
208               (beginning-of-line)
209               (nnheader-insert-nov headers)
210               ;; Update the active info.
211               (set-buffer gnus-summary-buffer)
212               (gnus-cache-possibly-update-active group (cons number number))
213               (push article gnus-newsgroup-cached)
214               (gnus-summary-update-secondary-mark article))
215             t))))))
216
217 (defun gnus-cache-enter-remove-article (article)
218   "Mark ARTICLE for later possible removal."
219   (when article
220     (push article gnus-cache-removable-articles)))
221
222 (defun gnus-cache-possibly-remove-articles ()
223   "Possibly remove some of the removable articles."
224   (if (not (gnus-virtual-group-p gnus-newsgroup-name))
225       (gnus-cache-possibly-remove-articles-1)
226     (let ((arts gnus-cache-removable-articles)
227           ga)
228       (while arts
229         (when (setq ga (nnvirtual-find-group-art
230                         (gnus-group-real-name gnus-newsgroup-name) (pop arts)))
231           (let ((gnus-cache-removable-articles (list (cdr ga)))
232                 (gnus-newsgroup-name (car ga)))
233             (gnus-cache-possibly-remove-articles-1)))))
234     (setq gnus-cache-removable-articles nil)))
235
236 (defun gnus-cache-possibly-remove-articles-1 ()
237   "Possibly remove some of the removable articles."
238   (when (gnus-cache-fully-p gnus-newsgroup-name)
239     (let ((articles gnus-cache-removable-articles)
240           (cache-articles gnus-newsgroup-cached)
241           article)
242       (gnus-cache-change-buffer gnus-newsgroup-name)
243       (while articles
244         (when (memq (setq article (pop articles)) cache-articles)
245           ;; The article was in the cache, so we see whether we are
246           ;; supposed to remove it from the cache.
247           (gnus-cache-possibly-remove-article
248            article (memq article gnus-newsgroup-marked)
249            (memq article gnus-newsgroup-dormant)
250            (or (memq article gnus-newsgroup-unreads)
251                (memq article gnus-newsgroup-unselected))))))
252     ;; The overview file might have been modified, save it
253     ;; safe because we're only called at group exit anyway.
254     (gnus-cache-save-buffers)))
255
256 (defun gnus-cache-request-article (article group)
257   "Retrieve ARTICLE in GROUP from the cache."
258   (let ((file (gnus-cache-file-name group article))
259         (buffer-read-only nil))
260     (when (file-exists-p file)
261       (erase-buffer)
262       (gnus-kill-all-overlays)
263       (let ((nnheader-file-coding-system gnus-cache-coding-system))
264         (nnheader-insert-file-contents file))
265       t)))
266
267 (defun gnus-cache-possibly-alter-active (group active)
268   "Alter the ACTIVE info for GROUP to reflect the articles in the cache."
269   (when gnus-cache-active-hashtb
270     (let ((cache-active (gnus-gethash group gnus-cache-active-hashtb)))
271       (when cache-active
272         (when (< (car cache-active) (car active))
273           (setcar active (car cache-active)))
274         (when (> (cdr cache-active) (cdr active))
275           (setcdr active (cdr cache-active)))))))
276
277 (defun gnus-cache-retrieve-headers (articles group &optional fetch-old)
278   "Retrieve the headers for ARTICLES in GROUP."
279   (let ((cached
280          (setq gnus-newsgroup-cached (gnus-cache-articles-in-group group))))
281     (if (not cached)
282         ;; No cached articles here, so we just retrieve them
283         ;; the normal way.
284         (let ((gnus-use-cache nil))
285           (gnus-retrieve-headers articles group fetch-old))
286       (let ((uncached-articles (gnus-sorted-intersection
287                                 (gnus-sorted-complement articles cached)
288                                 articles))
289             (cache-file (gnus-cache-file-name group ".overview"))
290             type)
291         ;; We first retrieve all the headers that we don't have in
292         ;; the cache.
293         (let ((gnus-use-cache nil))
294           (when uncached-articles
295             (setq type (and articles
296                             (gnus-retrieve-headers
297                              uncached-articles group fetch-old)))))
298         (gnus-cache-save-buffers)
299         ;; Then we insert the cached headers.
300         (save-excursion
301           (cond
302            ((not (file-exists-p cache-file))
303             ;; There are no cached headers.
304             type)
305            ((null type)
306             ;; There were no uncached headers (or retrieval was
307             ;; unsuccessful), so we use the cached headers exclusively.
308             (set-buffer nntp-server-buffer)
309             (erase-buffer)
310             (let ((nnheader-file-coding-system
311                    gnus-cache-overview-coding-system))
312               (nnheader-insert-file-contents cache-file))
313             'nov)
314            ((eq type 'nov)
315             ;; We have both cached and uncached NOV headers, so we
316             ;; braid them.
317             (gnus-cache-braid-nov group cached)
318             type)
319            (t
320             ;; We braid HEADs.
321             (gnus-cache-braid-heads group (gnus-sorted-intersection
322                                            cached articles))
323             type)))))))
324
325 (defun gnus-cache-enter-article (&optional n)
326   "Enter the next N articles into the cache.
327 If not given a prefix, use the process marked articles instead.
328 Returns the list of articles entered."
329   (interactive "P")
330   (let ((articles (gnus-summary-work-articles n))
331         article out)
332     (while (setq article (pop articles))
333       (gnus-summary-remove-process-mark article)
334       (if (natnump article)
335           (when (gnus-cache-possibly-enter-article
336                  gnus-newsgroup-name article
337                  (gnus-summary-article-header article)
338                  nil nil nil t)
339             (push article out))
340         (gnus-message 2 "Can't cache article %d" article))
341       (gnus-summary-update-secondary-mark article))
342     (gnus-summary-next-subject 1)
343     (gnus-summary-position-point)
344     (nreverse out)))
345
346 (defun gnus-cache-remove-article (n)
347   "Remove the next N articles from the cache.
348 If not given a prefix, use the process marked articles instead.
349 Returns the list of articles removed."
350   (interactive "P")
351   (gnus-cache-change-buffer gnus-newsgroup-name)
352   (let ((articles (gnus-summary-work-articles n))
353         article out)
354     (while articles
355       (setq article (pop articles))
356       (gnus-summary-remove-process-mark article)
357       (when (gnus-cache-possibly-remove-article article nil nil nil t)
358         (push article out))
359       (gnus-summary-update-secondary-mark article))
360     (gnus-summary-next-subject 1)
361     (gnus-summary-position-point)
362     (nreverse out)))
363
364 (defun gnus-cached-article-p (article)
365   "Say whether ARTICLE is cached in the current group."
366   (memq article gnus-newsgroup-cached))
367
368 (defun gnus-summary-insert-cached-articles ()
369   "Insert all the articles cached for this group into the current buffer."
370   (interactive)
371   (let ((cached (sort (copy-sequence gnus-newsgroup-cached) '>))
372         (gnus-verbose (max 6 gnus-verbose)))
373     (unless cached
374       (gnus-message 3 "No cached articles for this group"))
375     (while cached
376       (gnus-summary-goto-subject (pop cached) t))))
377
378 (defun gnus-summary-limit-include-cached ()
379   "Limit the summary buffer to articles that are cached."
380   (interactive)
381   (let ((cached (sort (copy-sequence gnus-newsgroup-cached) '>))
382         (gnus-verbose (max 6 gnus-verbose)))
383     (if cached
384         (progn
385           (gnus-summary-limit cached)
386           (gnus-summary-position-point))
387       (gnus-message 3 "No cached articles for this group"))))
388
389 ;;; Internal functions.
390
391 (defun gnus-cache-change-buffer (group)
392   (and gnus-cache-buffer
393        ;; See if the current group's overview cache has been loaded.
394        (or (string= group (car gnus-cache-buffer))
395            ;; Another overview cache is current, save it.
396            (gnus-cache-save-buffers)))
397   ;; if gnus-cache buffer is nil, create it
398   (unless gnus-cache-buffer
399     ;; Create cache buffer
400     (save-excursion
401       (setq gnus-cache-buffer
402             (cons group
403                   (set-buffer (gnus-get-buffer-create
404                                " *gnus-cache-overview*"))))
405       ;; Insert the contents of this group's cache overview.
406       (erase-buffer)
407       (let ((file (gnus-cache-file-name group ".overview")))
408         (when (file-exists-p file)
409           (nnheader-insert-file-contents file)))
410       ;; We have a fresh (empty/just loaded) buffer,
411       ;; mark it as unmodified to save a redundant write later.
412       (set-buffer-modified-p nil))))
413
414 ;; Return whether an article is a member of a class.
415 (defun gnus-cache-member-of-class (class ticked dormant unread)
416   (or (and ticked (memq 'ticked class))
417       (and dormant (memq 'dormant class))
418       (and unread (memq 'unread class))
419       (and (not unread) (not ticked) (not dormant) (memq 'read class))))
420
421 (defun gnus-cache-file-name (group article)
422   (expand-file-name
423    (if (stringp article) article (int-to-string article))
424    (file-name-as-directory
425     (expand-file-name
426      (nnheader-translate-file-chars
427       (if (gnus-use-long-file-name 'not-cache)
428           group
429         (let ((group (nnheader-replace-duplicate-chars-in-string
430                       (nnheader-replace-chars-in-string group ?/ ?_)
431                       ?. ?_)))
432           ;; Translate the first colon into a slash.
433           (when (string-match ":" group)
434             (setq group (concat (substring group 0 (match-beginning 0))
435                                 "/" (substring group (match-end 0)))))
436           (nnheader-replace-chars-in-string group ?. ?/)))
437       t)
438      gnus-cache-directory))))
439
440 (defun gnus-cache-update-article (group article)
441   "If ARTICLE is in the cache, remove it and re-enter it."
442   (gnus-cache-change-buffer group)
443   (when (gnus-cache-possibly-remove-article article nil nil nil t)
444     (let ((gnus-use-cache nil))
445       (gnus-cache-possibly-enter-article
446        gnus-newsgroup-name article (gnus-summary-article-header article)
447        nil nil nil t))))
448
449 (defun gnus-cache-possibly-remove-article (article ticked dormant unread
450                                                    &optional force)
451   "Possibly remove ARTICLE from the cache."
452   (let ((group gnus-newsgroup-name)
453         (number article)
454         file)
455     ;; If this is a virtual group, we find the real group.
456     (when (gnus-virtual-group-p group)
457       (let ((result (nnvirtual-find-group-art
458                      (gnus-group-real-name group) article)))
459         (setq group (car result)
460               number (cdr result))))
461     (setq file (gnus-cache-file-name group number))
462     (when (and (file-exists-p file)
463                (or force
464                    (gnus-cache-member-of-class
465                     gnus-cache-remove-articles ticked dormant unread)))
466       (save-excursion
467         (delete-file file)
468         (set-buffer (cdr gnus-cache-buffer))
469         (goto-char (point-min))
470         (when (or (looking-at (concat (int-to-string number) "\t"))
471                   (search-forward (concat "\n" (int-to-string number) "\t")
472                                   (point-max) t))
473           (delete-region (progn (beginning-of-line) (point))
474                          (progn (forward-line 1) (point)))))
475       (setq gnus-newsgroup-cached
476             (delq article gnus-newsgroup-cached))
477       (gnus-summary-update-secondary-mark article)
478       t)))
479
480 (defun gnus-cache-articles-in-group (group)
481   "Return a sorted list of cached articles in GROUP."
482   (let ((dir (file-name-directory (gnus-cache-file-name group 1)))
483         articles)
484     (when (file-exists-p dir)
485       (setq articles
486             (sort (mapcar (lambda (name) (string-to-int name))
487                           (directory-files dir nil "^[0-9]+$" t))
488                   '<))
489       ;; Update the cache active file, just to synch more.
490       (when articles
491         (gnus-cache-update-active group (car articles) t)
492         (gnus-cache-update-active group (car (last articles))))
493       articles)))
494
495 (defun gnus-cache-braid-nov (group cached &optional file)
496   (let ((cache-buf (gnus-get-buffer-create " *gnus-cache*"))
497         beg end)
498     (gnus-cache-save-buffers)
499     (save-excursion
500       (set-buffer cache-buf)
501       (erase-buffer)
502       (let ((nnheader-file-coding-system gnus-cache-overview-coding-system))
503         (nnheader-insert-file-contents
504          (or file (gnus-cache-file-name group ".overview"))))
505       (goto-char (point-min))
506       (insert "\n")
507       (goto-char (point-min)))
508     (set-buffer nntp-server-buffer)
509     (goto-char (point-min))
510     (while cached
511       (while (and (not (eobp))
512                   (< (read (current-buffer)) (car cached)))
513         (forward-line 1))
514       (beginning-of-line)
515       (save-excursion
516         (set-buffer cache-buf)
517         (if (search-forward (concat "\n" (int-to-string (car cached)) "\t")
518                             nil t)
519             (setq beg (progn (beginning-of-line) (point))
520                   end (progn (end-of-line) (point)))
521           (setq beg nil)))
522       (when beg
523         (insert-buffer-substring cache-buf beg end)
524         (insert "\n"))
525       (setq cached (cdr cached)))
526     (kill-buffer cache-buf)))
527
528 (defun gnus-cache-braid-heads (group cached)
529   (let ((cache-buf (gnus-get-buffer-create " *gnus-cache*")))
530     (save-excursion
531       (set-buffer cache-buf)
532       (erase-buffer))
533     (set-buffer nntp-server-buffer)
534     (goto-char (point-min))
535     (while cached
536       (while (and (not (eobp))
537                   (looking-at "2.. +\\([0-9]+\\) ")
538                   (< (progn (goto-char (match-beginning 1))
539                             (read (current-buffer)))
540                      (car cached)))
541         (search-forward "\n.\n" nil 'move))
542       (beginning-of-line)
543       (save-excursion
544         (set-buffer cache-buf)
545         (erase-buffer)
546         (let ((nnheader-file-coding-system gnus-cache-coding-system))
547           (nnheader-insert-file-contents
548            (gnus-cache-file-name group (car cached))))
549         (goto-char (point-min))
550         (insert "220 ")
551         (princ (car cached) (current-buffer))
552         (insert " Article retrieved.\n")
553         (search-forward "\n\n" nil 'move)
554         (delete-region (point) (point-max))
555         (forward-char -1)
556         (insert "."))
557       (insert-buffer-substring cache-buf)
558       (setq cached (cdr cached)))
559     (kill-buffer cache-buf)))
560
561 ;;;###autoload
562 (defun gnus-jog-cache ()
563   "Go through all groups and put the articles into the cache.
564
565 Usage:
566 $ emacs -batch -l ~/.emacs -l gnus -f gnus-jog-cache"
567   (interactive)
568   (let ((gnus-mark-article-hook nil)
569         (gnus-expert-user t)
570         (nnmail-spool-file nil)
571         (mail-sources nil)
572         (gnus-use-dribble-file nil)
573         (gnus-novice-user nil)
574         (gnus-large-newsgroup nil))
575     ;; Start Gnus.
576     (gnus)
577     ;; Go through all groups...
578     (gnus-group-mark-buffer)
579     (gnus-group-iterate nil
580       (lambda (group)
581         (let (gnus-auto-select-next)
582           (gnus-summary-read-group group nil t)
583           ;; ... and enter the articles into the cache.
584           (when (eq major-mode 'gnus-summary-mode)
585             (gnus-uu-mark-buffer)
586             (gnus-cache-enter-article)
587             (kill-buffer (current-buffer))))))))
588
589 (defun gnus-cache-read-active (&optional force)
590   "Read the cache active file."
591   (gnus-make-directory gnus-cache-directory)
592   (if (or (not (file-exists-p gnus-cache-active-file))
593           (zerop (nth 7 (file-attributes gnus-cache-active-file)))
594           force)
595       ;; There is no active file, so we generate one.
596       (gnus-cache-generate-active)
597     ;; We simply read the active file.
598     (save-excursion
599       (gnus-set-work-buffer)
600       (nnheader-insert-file-contents gnus-cache-active-file)
601       (gnus-active-to-gnus-format
602        nil (setq gnus-cache-active-hashtb
603                  (gnus-make-hashtable
604                   (count-lines (point-min) (point-max)))))
605       (setq gnus-cache-active-altered nil))))
606
607 (defun gnus-cache-write-active (&optional force)
608   "Write the active hashtb to the active file."
609   (when (or force
610             (and gnus-cache-active-hashtb
611                  gnus-cache-active-altered))
612     (gnus-write-active-file gnus-cache-active-file gnus-cache-active-hashtb t)
613     ;; Mark the active hashtb as unaltered.
614     (setq gnus-cache-active-altered nil)))
615
616 (defun gnus-cache-possibly-update-active (group active)
617   "Update active info bounds of GROUP with ACTIVE if necessary.
618 The update is performed if ACTIVE contains a higher or lower bound
619 than the current."
620   (let ((lower t) (higher t))
621     (if gnus-cache-active-hashtb
622         (let ((cache-active (gnus-gethash group gnus-cache-active-hashtb)))
623           (when cache-active
624             (unless (< (car active) (car cache-active))
625               (setq lower nil))
626             (unless (> (cdr active) (cdr cache-active))
627               (setq higher nil))))
628       (gnus-cache-read-active))
629     (when lower
630       (gnus-cache-update-active group (car active) t))
631     (when higher
632       (gnus-cache-update-active group (cdr active)))))
633
634 (defun gnus-cache-update-active (group number &optional low)
635   "Update the upper bound of the active info of GROUP to NUMBER.
636 If LOW, update the lower bound instead."
637   (let ((active (gnus-gethash group gnus-cache-active-hashtb)))
638     (if (null active)
639         ;; We just create a new active entry for this group.
640         (gnus-sethash group (cons number number) gnus-cache-active-hashtb)
641       ;; Update the lower or upper bound.
642       (if low
643           (setcar active number)
644         (setcdr active number)))
645     ;; Mark the active hashtb as altered.
646     (setq gnus-cache-active-altered t)))
647
648 ;;;###autoload
649 (defun gnus-cache-generate-active (&optional directory)
650   "Generate the cache active file."
651   (interactive)
652   (let* ((top (null directory))
653          (directory (expand-file-name (or directory gnus-cache-directory)))
654          (files (directory-files directory 'full))
655          (group
656           (if top
657               ""
658             (string-match
659              (concat "^" (regexp-quote
660                           (file-name-as-directory
661                            (expand-file-name gnus-cache-directory))))
662              (directory-file-name directory))
663             (nnheader-replace-chars-in-string
664              (substring (directory-file-name directory) (match-end 0))
665              ?/ ?.)))
666          nums alphs)
667     (when top
668       (gnus-message 5 "Generating the cache active file...")
669       (setq gnus-cache-active-hashtb (gnus-make-hashtable 123)))
670     (when (string-match "^\\(nn[^_]+\\)_" group)
671       (setq group (replace-match "\\1:" t t group)))
672     ;; Separate articles from all other files and directories.
673     (while files
674       (if (string-match "^[0-9]+$" (file-name-nondirectory (car files)))
675           (push (string-to-int (file-name-nondirectory (pop files))) nums)
676         (push (pop files) alphs)))
677     ;; If we have nums, then this is probably a valid group.
678     (when (setq nums (sort nums '<))
679       (gnus-sethash group (cons (car nums) (gnus-last-element nums))
680                     gnus-cache-active-hashtb))
681     ;; Go through all the other files.
682     (while alphs
683       (when (and (file-directory-p (car alphs))
684                  (not (string-match "^\\."
685                                     (file-name-nondirectory (car alphs)))))
686         ;; We descend directories.
687         (gnus-cache-generate-active (car alphs)))
688       (setq alphs (cdr alphs)))
689     ;; Write the new active file.
690     (when top
691       (gnus-cache-write-active t)
692       (gnus-message 5 "Generating the cache active file...done"))))
693
694 ;;;###autoload
695 (defun gnus-cache-generate-nov-databases (dir)
696   "Generate NOV files recursively starting in DIR."
697   (interactive (list gnus-cache-directory))
698   (gnus-cache-close)
699   (let ((nnml-generate-active-function 'identity))
700     (nnml-generate-nov-databases-1 dir))
701   (gnus-cache-open))
702
703 (defun gnus-cache-move-cache (dir)
704   "Move the cache tree to somewhere else."
705   (interactive "FMove the cache tree to: ")
706   (rename-file gnus-cache-directory dir))
707
708 (defun gnus-cache-fully-p (&optional group)
709   "Returns non-nil if the cache should be fully used.
710 If GROUP is non-nil, also cater to `gnus-cacheable-groups' and
711 `gnus-uncacheable-groups'."
712   (and gnus-use-cache
713        (not (eq gnus-use-cache 'passive))
714        (if (null group)
715            t
716          (and (or (not gnus-cacheable-groups)
717                   (string-match gnus-cacheable-groups group))
718               (or (not gnus-uncacheable-groups)
719                   (not (string-match gnus-uncacheable-groups group)))))))
720
721 (provide 'gnus-cache)
722
723 ;;; gnus-cache.el ends here