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