* nnmail.el (nnmail-split-it): Revoke the change of 1999-08-19.
[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, 2003
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               (nnheader-remove-body)
187               (setq headers (nnheader-parse-naked-head))
188               (mail-header-set-number headers number)
189               (gnus-cache-change-buffer group)
190               (set-buffer (cdr gnus-cache-buffer))
191               (goto-char (point-max))
192               (forward-line -1)
193               (while (condition-case ()
194                          (when (not (bobp))
195                            (> (read (current-buffer)) number))
196                        (error
197                         ;; The line was malformed, so we just remove it!!
198                         (gnus-delete-line)
199                         t))
200                 (forward-line -1))
201               (if (bobp)
202                   (if (not (eobp))
203                       (progn
204                         (beginning-of-line)
205                         (when (< (read (current-buffer)) number)
206                           (forward-line 1)))
207                     (beginning-of-line))
208                 (forward-line 1))
209               (beginning-of-line)
210               (nnheader-insert-nov headers)
211               ;; Update the active info.
212               (set-buffer gnus-summary-buffer)
213               (gnus-cache-possibly-update-active group (cons number number))
214               (setq gnus-newsgroup-cached
215                     (gnus-add-to-sorted-list gnus-newsgroup-cached article))
216               (gnus-summary-update-secondary-mark article))
217             t))))))
218
219 (defun gnus-cache-enter-remove-article (article)
220   "Mark ARTICLE for later possible removal."
221   (when article
222     (push article gnus-cache-removable-articles)))
223
224 (defun gnus-cache-possibly-remove-articles ()
225   "Possibly remove some of the removable articles."
226   (if (not (gnus-virtual-group-p gnus-newsgroup-name))
227       (gnus-cache-possibly-remove-articles-1)
228     (let ((arts gnus-cache-removable-articles)
229           ga)
230       (while arts
231         (when (setq ga (nnvirtual-find-group-art
232                         (gnus-group-real-name gnus-newsgroup-name) (pop arts)))
233           (let ((gnus-cache-removable-articles (list (cdr ga)))
234                 (gnus-newsgroup-name (car ga)))
235             (gnus-cache-possibly-remove-articles-1)))))
236     (setq gnus-cache-removable-articles nil)))
237
238 (defun gnus-cache-possibly-remove-articles-1 ()
239   "Possibly remove some of the removable articles."
240   (when (gnus-cache-fully-p gnus-newsgroup-name)
241     (let ((articles gnus-cache-removable-articles)
242           (cache-articles gnus-newsgroup-cached)
243           article)
244       (gnus-cache-change-buffer gnus-newsgroup-name)
245       (while articles
246         (when (memq (setq article (pop articles)) cache-articles)
247           ;; The article was in the cache, so we see whether we are
248           ;; supposed to remove it from the cache.
249           (gnus-cache-possibly-remove-article
250            article (memq article gnus-newsgroup-marked)
251            (memq article gnus-newsgroup-dormant)
252            (or (memq article gnus-newsgroup-unreads)
253                (memq article gnus-newsgroup-unselected))))))
254     ;; The overview file might have been modified, save it
255     ;; safe because we're only called at group exit anyway.
256     (gnus-cache-save-buffers)))
257
258 (defun gnus-cache-request-article (article group)
259   "Retrieve ARTICLE in GROUP from the cache."
260   (let ((file (gnus-cache-file-name group article))
261         (buffer-read-only nil))
262     (when (file-exists-p file)
263       (erase-buffer)
264       (gnus-kill-all-overlays)
265       (let ((nnheader-file-coding-system gnus-cache-coding-system))
266         (nnheader-insert-file-contents file))
267       t)))
268
269 (defun gnus-cache-possibly-alter-active (group active)
270   "Alter the ACTIVE info for GROUP to reflect the articles in the cache."
271   (when gnus-cache-active-hashtb
272     (let ((cache-active (gnus-gethash group gnus-cache-active-hashtb)))
273       (when cache-active
274         (when (< (car cache-active) (car active))
275           (setcar active (car cache-active)))
276         (when (> (cdr cache-active) (cdr active))
277           (setcdr active (cdr cache-active)))))))
278
279 (defun gnus-cache-retrieve-headers (articles group &optional fetch-old)
280   "Retrieve the headers for ARTICLES in GROUP."
281   (let ((cached
282          (setq gnus-newsgroup-cached (gnus-cache-articles-in-group group))))
283     (if (not cached)
284         ;; No cached articles here, so we just retrieve them
285         ;; the normal way.
286         (let ((gnus-use-cache nil))
287           (gnus-retrieve-headers articles group fetch-old))
288       (let ((uncached-articles (gnus-sorted-difference articles cached))
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 (&optional 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 ((gnus-verbose (max 6 gnus-verbose)))
372     (if (not gnus-newsgroup-cached)
373         (gnus-message 3 "No cached articles for this group")
374       (gnus-summary-goto-subjects gnus-newsgroup-cached))))
375
376 (defun gnus-summary-limit-include-cached ()
377   "Limit the summary buffer to articles that are cached."
378   (interactive)
379   (let ((gnus-verbose (max 6 gnus-verbose)))
380     (if gnus-newsgroup-cached
381         (progn
382           (gnus-summary-limit gnus-newsgroup-cached)
383           (gnus-summary-position-point))
384       (gnus-message 3 "No cached articles for this group"))))
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       ;; Insert the contents of this group's cache overview.
403       (erase-buffer)
404       (let ((file (gnus-cache-file-name group ".overview")))
405         (when (file-exists-p file)
406           (nnheader-insert-file-contents file)))
407       ;; We have a fresh (empty/just loaded) buffer,
408       ;; mark it as unmodified to save a redundant write later.
409       (set-buffer-modified-p nil))))
410
411 ;; Return whether an article is a member of a class.
412 (defun gnus-cache-member-of-class (class ticked dormant unread)
413   (or (and ticked (memq 'ticked class))
414       (and dormant (memq 'dormant class))
415       (and unread (memq 'unread class))
416       (and (not unread) (not ticked) (not dormant) (memq 'read class))))
417
418 (defun gnus-cache-file-name (group article)
419   (expand-file-name
420    (if (stringp article) article (int-to-string article))
421    (file-name-as-directory
422     (expand-file-name
423      (nnheader-translate-file-chars
424       (if (gnus-use-long-file-name 'not-cache)
425           group
426         (let ((group (nnheader-replace-duplicate-chars-in-string
427                       (nnheader-replace-chars-in-string group ?/ ?_)
428                       ?. ?_)))
429           ;; Translate the first colon into a slash.
430           (when (string-match ":" group)
431             (setq group (concat (substring group 0 (match-beginning 0))
432                                 "/" (substring group (match-end 0)))))
433           (nnheader-replace-chars-in-string group ?. ?/)))
434       t)
435      gnus-cache-directory))))
436
437 (defun gnus-cache-update-article (group article)
438   "If ARTICLE is in the cache, remove it and re-enter it."
439   (gnus-cache-change-buffer group)
440   (when (gnus-cache-possibly-remove-article article nil nil nil t)
441     (let ((gnus-use-cache nil))
442       (gnus-cache-possibly-enter-article
443        gnus-newsgroup-name article (gnus-summary-article-header article)
444        nil nil nil t))))
445
446 (defun gnus-cache-possibly-remove-article (article ticked dormant unread
447                                                    &optional force)
448   "Possibly remove ARTICLE from the cache."
449   (let ((group gnus-newsgroup-name)
450         (number article)
451         file)
452     ;; If this is a virtual group, we find the real group.
453     (when (gnus-virtual-group-p group)
454       (let ((result (nnvirtual-find-group-art
455                      (gnus-group-real-name group) article)))
456         (setq group (car result)
457               number (cdr result))))
458     (setq file (gnus-cache-file-name group number))
459     (when (and (file-exists-p file)
460                (or force
461                    (gnus-cache-member-of-class
462                     gnus-cache-remove-articles ticked dormant unread)))
463       (save-excursion
464         (delete-file file)
465         (set-buffer (cdr gnus-cache-buffer))
466         (goto-char (point-min))
467         (when (or (looking-at (concat (int-to-string number) "\t"))
468                   (search-forward (concat "\n" (int-to-string number) "\t")
469                                   (point-max) t))
470           (gnus-delete-line)))
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 ((nnheader-file-coding-system gnus-cache-overview-coding-system))
505         (nnheader-insert-file-contents
506          (or file (gnus-cache-file-name group ".overview"))))
507       (goto-char (point-min))
508       (insert "\n")
509       (goto-char (point-min)))
510     (set-buffer nntp-server-buffer)
511     (goto-char (point-min))
512     (while cached
513       (while (and (not (eobp))
514                   (< (read (current-buffer)) (car cached)))
515         (forward-line 1))
516       (beginning-of-line)
517       (set-buffer cache-buf)
518       (if (search-forward (concat "\n" (int-to-string (car cached)) "\t")
519                           nil t)
520           (setq beg (gnus-point-at-bol)
521                 end (progn (end-of-line) (point)))
522         (setq beg nil))
523       (set-buffer nntp-server-buffer)
524       (when beg
525         (insert-buffer-substring cache-buf beg end)
526         (insert "\n"))
527       (setq cached (cdr cached)))
528     (kill-buffer cache-buf)))
529
530 (defun gnus-cache-braid-heads (group cached)
531   (let ((cache-buf (gnus-get-buffer-create " *gnus-cache*")))
532     (save-excursion
533       (set-buffer cache-buf)
534       (erase-buffer))
535     (set-buffer nntp-server-buffer)
536     (goto-char (point-min))
537     (while cached
538       (while (and (not (eobp))
539                   (looking-at "2.. +\\([0-9]+\\) ")
540                   (< (progn (goto-char (match-beginning 1))
541                             (read (current-buffer)))
542                      (car cached)))
543         (search-forward "\n.\n" nil 'move))
544       (beginning-of-line)
545       (save-excursion
546         (set-buffer cache-buf)
547         (erase-buffer)
548         (let ((nnheader-file-coding-system gnus-cache-coding-system))
549           (nnheader-insert-file-contents
550            (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       (insert-buffer-substring cache-buf)
560       (setq cached (cdr cached)))
561     (kill-buffer cache-buf)))
562
563 ;;;###autoload
564 (defun gnus-jog-cache ()
565   "Go through all groups and put the articles into the cache.
566
567 Usage:
568 $ emacs -batch -l ~/.emacs -l gnus -f gnus-jog-cache"
569   (interactive)
570   (let ((gnus-mark-article-hook nil)
571         (gnus-expert-user t)
572         (nnmail-spool-file nil)
573         (mail-sources nil)
574         (gnus-use-dribble-file nil)
575         (gnus-novice-user nil)
576         (gnus-large-newsgroup nil))
577     ;; Start Gnus.
578     (gnus)
579     ;; Go through all groups...
580     (gnus-group-mark-buffer)
581     (gnus-group-iterate nil
582       (lambda (group)
583         (let (gnus-auto-select-next)
584           (gnus-summary-read-group group nil t)
585           ;; ... and enter the articles into the cache.
586           (when (eq major-mode 'gnus-summary-mode)
587             (gnus-uu-mark-buffer)
588             (gnus-cache-enter-article)
589             (kill-buffer (current-buffer))))))))
590
591 (defun gnus-cache-read-active (&optional force)
592   "Read the cache active file."
593   (gnus-make-directory gnus-cache-directory)
594   (if (or (not (file-exists-p gnus-cache-active-file))
595           (zerop (nth 7 (file-attributes gnus-cache-active-file)))
596           force)
597       ;; There is no active file, so we generate one.
598       (gnus-cache-generate-active)
599     ;; We simply read the active file.
600     (save-excursion
601       (gnus-set-work-buffer)
602       (nnheader-insert-file-contents gnus-cache-active-file)
603       (gnus-active-to-gnus-format
604        nil (setq gnus-cache-active-hashtb
605                  (gnus-make-hashtable
606                   (count-lines (point-min) (point-max)))))
607       (setq gnus-cache-active-altered nil))))
608
609 (defun gnus-cache-write-active (&optional force)
610   "Write the active hashtb to the active file."
611   (when (or force
612             (and gnus-cache-active-hashtb
613                  gnus-cache-active-altered))
614     (gnus-write-active-file gnus-cache-active-file gnus-cache-active-hashtb t)
615     ;; Mark the active hashtb as unaltered.
616     (setq gnus-cache-active-altered nil)))
617
618 (defun gnus-cache-possibly-update-active (group active)
619   "Update active info bounds of GROUP with ACTIVE if necessary.
620 The update is performed if ACTIVE contains a higher or lower bound
621 than the current."
622   (let ((lower t) (higher t))
623     (if gnus-cache-active-hashtb
624         (let ((cache-active (gnus-gethash group gnus-cache-active-hashtb)))
625           (when cache-active
626             (unless (< (car active) (car cache-active))
627               (setq lower nil))
628             (unless (> (cdr active) (cdr cache-active))
629               (setq higher nil))))
630       (gnus-cache-read-active))
631     (when lower
632       (gnus-cache-update-active group (car active) t))
633     (when higher
634       (gnus-cache-update-active group (cdr active)))))
635
636 (defun gnus-cache-update-active (group number &optional low)
637   "Update the upper bound of the active info of GROUP to NUMBER.
638 If LOW, update the lower bound instead."
639   (let ((active (gnus-gethash group gnus-cache-active-hashtb)))
640     (if (null active)
641         ;; We just create a new active entry for this group.
642         (gnus-sethash group (cons number number) gnus-cache-active-hashtb)
643       ;; Update the lower or upper bound.
644       (if low
645           (setcar active number)
646         (setcdr active number)))
647     ;; Mark the active hashtb as altered.
648     (setq gnus-cache-active-altered t)))
649
650 ;;;###autoload
651 (defun gnus-cache-generate-active (&optional directory)
652   "Generate the cache active file."
653   (interactive)
654   (let* ((top (null directory))
655          (directory (expand-file-name (or directory gnus-cache-directory)))
656          (files (directory-files directory 'full))
657          (group
658           (if top
659               ""
660             (string-match
661              (concat "^" (regexp-quote
662                           (file-name-as-directory
663                            (expand-file-name gnus-cache-directory))))
664              (directory-file-name directory))
665             (nnheader-replace-chars-in-string
666              (substring (directory-file-name directory) (match-end 0))
667              ?/ ?.)))
668          nums alphs)
669     (when top
670       (gnus-message 5 "Generating the cache active file...")
671       (setq gnus-cache-active-hashtb (gnus-make-hashtable 123)))
672     (when (string-match "^\\(nn[^_]+\\)_" group)
673       (setq group (replace-match "\\1:" t t group)))
674     ;; Separate articles from all other files and directories.
675     (while files
676       (if (string-match "^[0-9]+$" (file-name-nondirectory (car files)))
677           (push (string-to-int (file-name-nondirectory (pop files))) nums)
678         (push (pop files) alphs)))
679     ;; If we have nums, then this is probably a valid group.
680     (when (setq nums (sort nums '<))
681       (gnus-sethash group (cons (car nums) (gnus-last-element nums))
682                     gnus-cache-active-hashtb))
683     ;; Go through all the other files.
684     (while alphs
685       (when (and (file-directory-p (car alphs))
686                  (not (string-match "^\\."
687                                     (file-name-nondirectory (car alphs)))))
688         ;; We descend directories.
689         (gnus-cache-generate-active (car alphs)))
690       (setq alphs (cdr alphs)))
691     ;; Write the new active file.
692     (when top
693       (gnus-cache-write-active t)
694       (gnus-message 5 "Generating the cache active file...done"))))
695
696 ;;;###autoload
697 (defun gnus-cache-generate-nov-databases (dir)
698   "Generate NOV files recursively starting in DIR."
699   (interactive (list gnus-cache-directory))
700   (gnus-cache-close)
701   (let ((nnml-generate-active-function 'identity))
702     (nnml-generate-nov-databases-1 dir))
703   (gnus-cache-open))
704
705 (defun gnus-cache-move-cache (dir)
706   "Move the cache tree to somewhere else."
707   (interactive "FMove the cache tree to: ")
708   (rename-file gnus-cache-directory dir))
709
710 (defun gnus-cache-fully-p (&optional group)
711   "Returns non-nil if the cache should be fully used.
712 If GROUP is non-nil, also cater to `gnus-cacheable-groups' and
713 `gnus-uncacheable-groups'."
714   (and gnus-use-cache
715        (not (eq gnus-use-cache 'passive))
716        (if (null group)
717            t
718          (and (or (not gnus-cacheable-groups)
719                   (string-match gnus-cacheable-groups group))
720               (or (not gnus-uncacheable-groups)
721                   (not (string-match gnus-uncacheable-groups group)))))))
722
723 (provide 'gnus-cache)
724
725 ;;; gnus-cache.el ends here