Synch to No Gnus 200404061321.
[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
33 (require 'gnus)
34 (require 'gnus-int)
35 (require 'gnus-range)
36 (require 'gnus-start)
37 (eval-when-compile
38   (if (not (fboundp 'gnus-agent-load-alist))
39       (defun gnus-agent-load-alist (group)))
40   (require 'gnus-sum))
41
42 (defcustom gnus-cache-active-file
43   (expand-file-name "active" gnus-cache-directory)
44   "*The cache active file."
45   :group 'gnus-cache
46   :type 'file)
47
48 (defcustom gnus-cache-enter-articles '(ticked dormant)
49   "Classes of articles to enter into the cache."
50   :group 'gnus-cache
51   :type '(set (const ticked) (const dormant) (const unread) (const read)))
52
53 (defcustom gnus-cache-remove-articles '(read)
54   "Classes of articles to remove from the cache."
55   :group 'gnus-cache
56   :type '(set (const ticked) (const dormant) (const unread) (const read)))
57
58 (defcustom gnus-cacheable-groups nil
59   "*Groups that match this regexp will be cached.
60
61 If you only want to cache your nntp groups, you could set this
62 variable to \"^nntp\".
63
64 If a group matches both gnus-cacheable-groups and gnus-uncacheable-groups
65 it's not cached."
66   :group 'gnus-cache
67   :type '(choice (const :tag "off" nil)
68                  regexp))
69
70 (defcustom gnus-uncacheable-groups nil
71   "*Groups that match this regexp will not be cached.
72
73 If you want to avoid caching your nnml groups, you could set this
74 variable to \"^nnml\".
75
76 If a group matches both gnus-cacheable-groups and gnus-uncacheable-groups
77 it's not cached."
78   :group 'gnus-cache
79   :type '(choice (const :tag "off" nil)
80                  regexp))
81
82 (defvar gnus-cache-overview-coding-system 'raw-text
83   "Coding system used on Gnus cache files.")
84
85 (defvar gnus-cache-coding-system 'raw-text
86   "Coding system used on Gnus cache files.")
87
88 \f
89
90 ;;; Internal variables.
91
92 (defvar gnus-cache-removable-articles nil)
93 (defvar gnus-cache-buffer nil)
94 (defvar gnus-cache-active-hashtb nil)
95 (defvar gnus-cache-active-altered nil)
96 (defvar gnus-cache-write-file-coding-system 'raw-text)
97
98 (eval-and-compile
99   (autoload 'nnml-generate-nov-databases-1 "nnml")
100   (autoload 'nnvirtual-find-group-art "nnvirtual"))
101
102 \f
103
104 ;;; Functions called from Gnus.
105
106 (defun gnus-cache-open ()
107   "Initialize the cache."
108   (when (or (file-exists-p gnus-cache-directory)
109             (and gnus-use-cache
110                  (not (eq gnus-use-cache 'passive))))
111     (gnus-cache-read-active)))
112
113 ;; Complexities of byte-compiling make this kludge necessary.  Eeek.
114 (ignore-errors
115   (gnus-add-shutdown 'gnus-cache-close 'gnus))
116
117 (defun gnus-cache-close ()
118   "Shut down the cache."
119   (gnus-cache-write-active)
120   (gnus-cache-save-buffers)
121   (setq gnus-cache-active-hashtb nil))
122
123 (defun gnus-cache-save-buffers ()
124   ;; save the overview buffer if it exists and has been modified
125   ;; delete empty cache subdirectories
126   (when gnus-cache-buffer
127     (let ((buffer (cdr gnus-cache-buffer))
128           (overview-file (gnus-cache-file-name
129                           (car gnus-cache-buffer) ".overview")))
130       ;; write the overview only if it was modified
131       (when (and (buffer-live-p buffer) (buffer-modified-p buffer))
132         (with-current-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             (setq gnus-newsgroup-undownloaded (delq article gnus-newsgroup-undownloaded))
340             (push article out))
341         (gnus-message 2 "Can't cache article %d" article))
342       (gnus-summary-update-download-mark article)
343       (gnus-summary-update-secondary-mark article))
344     (gnus-summary-next-subject 1)
345     (gnus-summary-position-point)
346     (nreverse out)))
347
348 (defun gnus-cache-remove-article (&optional n)
349   "Remove the next N articles from the cache.
350 If not given a prefix, use the process marked articles instead.
351 Returns the list of articles removed."
352   (interactive "P")
353   (gnus-cache-change-buffer gnus-newsgroup-name)
354   (let ((articles (gnus-summary-work-articles n))
355         article out)
356     (while articles
357       (setq article (pop articles))
358       (gnus-summary-remove-process-mark article)
359       (when (gnus-cache-possibly-remove-article article nil nil nil t)
360         (when gnus-newsgroup-agentized
361           (let ((alist (gnus-agent-load-alist gnus-newsgroup-name)))
362             (unless (cdr (assoc article alist))
363               (setq gnus-newsgroup-undownloaded
364                     (gnus-add-to-sorted-list 
365                      gnus-newsgroup-undownloaded article)))))
366         (push article out))
367       (gnus-summary-update-download-mark article)
368       (gnus-summary-update-secondary-mark article))
369     (gnus-summary-next-subject 1)
370     (gnus-summary-position-point)
371     (nreverse out)))
372
373 (defun gnus-cached-article-p (article)
374   "Say whether ARTICLE is cached in the current group."
375   (memq article gnus-newsgroup-cached))
376
377 (defun gnus-summary-insert-cached-articles ()
378   "Insert all the articles cached for this group into the current buffer."
379   (interactive)
380   (let ((gnus-verbose (max 6 gnus-verbose)))
381     (if (not gnus-newsgroup-cached)
382         (gnus-message 3 "No cached articles for this group")
383       (gnus-summary-goto-subjects gnus-newsgroup-cached))))
384
385 (defun gnus-summary-limit-include-cached ()
386   "Limit the summary buffer to articles that are cached."
387   (interactive)
388   (let ((gnus-verbose (max 6 gnus-verbose)))
389     (if gnus-newsgroup-cached
390         (progn
391           (gnus-summary-limit gnus-newsgroup-cached)
392           (gnus-summary-position-point))
393       (gnus-message 3 "No cached articles for this group"))))
394
395 ;;; Internal functions.
396
397 (defun gnus-cache-change-buffer (group)
398   (and gnus-cache-buffer
399        ;; See if the current group's overview cache has been loaded.
400        (or (string= group (car gnus-cache-buffer))
401            ;; Another overview cache is current, save it.
402            (gnus-cache-save-buffers)))
403   ;; if gnus-cache buffer is nil, create it
404   (unless gnus-cache-buffer
405     ;; Create cache buffer
406     (save-excursion
407       (setq gnus-cache-buffer
408             (cons group
409                   (set-buffer (gnus-get-buffer-create
410                                " *gnus-cache-overview*"))))
411       ;; Insert the contents of this group's cache overview.
412       (erase-buffer)
413       (let ((file (gnus-cache-file-name group ".overview")))
414         (when (file-exists-p file)
415           (nnheader-insert-file-contents file)))
416       ;; We have a fresh (empty/just loaded) buffer,
417       ;; mark it as unmodified to save a redundant write later.
418       (set-buffer-modified-p nil))))
419
420 ;; Return whether an article is a member of a class.
421 (defun gnus-cache-member-of-class (class ticked dormant unread)
422   (or (and ticked (memq 'ticked class))
423       (and dormant (memq 'dormant class))
424       (and unread (memq 'unread class))
425       (and (not unread) (not ticked) (not dormant) (memq 'read class))))
426
427 (defun gnus-cache-file-name (group article)
428   (expand-file-name
429    (if (stringp article) article (int-to-string article))
430    (file-name-as-directory
431     (expand-file-name
432      (nnheader-translate-file-chars
433       (if (gnus-use-long-file-name 'not-cache)
434           group
435         (let ((group (nnheader-replace-duplicate-chars-in-string
436                       (nnheader-replace-chars-in-string group ?/ ?_)
437                       ?. ?_)))
438           ;; Translate the first colon into a slash.
439           (when (string-match ":" group)
440             (setq group (concat (substring group 0 (match-beginning 0))
441                                 "/" (substring group (match-end 0)))))
442           (nnheader-replace-chars-in-string group ?. ?/)))
443       t)
444      gnus-cache-directory))))
445
446 (defun gnus-cache-update-article (group article)
447   "If ARTICLE is in the cache, remove it and re-enter it."
448   (gnus-cache-change-buffer group)
449   (when (gnus-cache-possibly-remove-article article nil nil nil t)
450     (let ((gnus-use-cache nil))
451       (gnus-cache-possibly-enter-article
452        gnus-newsgroup-name article (gnus-summary-article-header article)
453        nil nil nil t))))
454
455 (defun gnus-cache-possibly-remove-article (article ticked dormant unread
456                                                    &optional force)
457   "Possibly remove ARTICLE from the cache."
458   (let ((group gnus-newsgroup-name)
459         (number article)
460         file)
461     ;; If this is a virtual group, we find the real group.
462     (when (gnus-virtual-group-p group)
463       (let ((result (nnvirtual-find-group-art
464                      (gnus-group-real-name group) article)))
465         (setq group (car result)
466               number (cdr result))))
467     (setq file (gnus-cache-file-name group number))
468     (when (and (file-exists-p file)
469                (or force
470                    (gnus-cache-member-of-class
471                     gnus-cache-remove-articles ticked dormant unread)))
472       (save-excursion
473         (delete-file file)
474         (set-buffer (cdr gnus-cache-buffer))
475         (goto-char (point-min))
476         (when (or (looking-at (concat (int-to-string number) "\t"))
477                   (search-forward (concat "\n" (int-to-string number) "\t")
478                                   (point-max) t))
479           (gnus-delete-line)))
480       (unless (setq gnus-newsgroup-cached
481                     (delq article gnus-newsgroup-cached))
482         (gnus-sethash gnus-newsgroup-name nil gnus-cache-active-hashtb)
483         (setq gnus-cache-active-altered t))
484       (gnus-summary-update-secondary-mark article)
485       t)))
486
487 (defun gnus-cache-articles-in-group (group)
488   "Return a sorted list of cached articles in GROUP."
489   (let ((dir (file-name-directory (gnus-cache-file-name group 1)))
490         articles)
491     (when (file-exists-p dir)
492       (setq articles
493             (sort (mapcar (lambda (name) (string-to-int name))
494                           (directory-files dir nil "^[0-9]+$" t))
495                   '<))
496       ;; Update the cache active file, just to synch more.
497       (if articles
498           (progn
499             (gnus-cache-update-active group (car articles) t)
500             (gnus-cache-update-active group (car (last articles))))
501         (when (gnus-gethash group gnus-cache-active-hashtb)
502           (gnus-sethash group nil gnus-cache-active-hashtb)
503           (setq gnus-cache-active-altered t)))
504       articles)))
505
506 (defun gnus-cache-braid-nov (group cached &optional file)
507   (let ((cache-buf (gnus-get-buffer-create " *gnus-cache*"))
508         beg end)
509     (gnus-cache-save-buffers)
510     (save-excursion
511       (set-buffer cache-buf)
512       (erase-buffer)
513       (let ((nnheader-file-coding-system gnus-cache-overview-coding-system))
514         (nnheader-insert-file-contents
515          (or file (gnus-cache-file-name group ".overview"))))
516       (goto-char (point-min))
517       (insert "\n")
518       (goto-char (point-min)))
519     (set-buffer nntp-server-buffer)
520     (goto-char (point-min))
521     (while cached
522       (while (and (not (eobp))
523                   (< (read (current-buffer)) (car cached)))
524         (forward-line 1))
525       (beginning-of-line)
526       (set-buffer cache-buf)
527       (if (search-forward (concat "\n" (int-to-string (car cached)) "\t")
528                           nil t)
529           (setq beg (point-at-bol)
530                 end (progn (end-of-line) (point)))
531         (setq beg nil))
532       (set-buffer nntp-server-buffer)
533       (when beg
534         (insert-buffer-substring cache-buf beg end)
535         (insert "\n"))
536       (setq cached (cdr cached)))
537     (kill-buffer cache-buf)))
538
539 (defun gnus-cache-braid-heads (group cached)
540   (let ((cache-buf (gnus-get-buffer-create " *gnus-cache*")))
541     (save-excursion
542       (set-buffer cache-buf)
543       (erase-buffer))
544     (set-buffer nntp-server-buffer)
545     (goto-char (point-min))
546     (while cached
547       (while (and (not (eobp))
548                   (looking-at "2.. +\\([0-9]+\\) ")
549                   (< (progn (goto-char (match-beginning 1))
550                             (read (current-buffer)))
551                      (car cached)))
552         (search-forward "\n.\n" nil 'move))
553       (beginning-of-line)
554       (save-excursion
555         (set-buffer cache-buf)
556         (erase-buffer)
557         (let ((nnheader-file-coding-system gnus-cache-coding-system))
558           (nnheader-insert-file-contents
559            (gnus-cache-file-name group (car cached))))
560         (goto-char (point-min))
561         (insert "220 ")
562         (princ (car cached) (current-buffer))
563         (insert " Article retrieved.\n")
564         (search-forward "\n\n" nil 'move)
565         (delete-region (point) (point-max))
566         (forward-char -1)
567         (insert "."))
568       (insert-buffer-substring cache-buf)
569       (setq cached (cdr cached)))
570     (kill-buffer cache-buf)))
571
572 ;;;###autoload
573 (defun gnus-jog-cache ()
574   "Go through all groups and put the articles into the cache.
575
576 Usage:
577 $ emacs -batch -l ~/.emacs -l gnus -f gnus-jog-cache"
578   (interactive)
579   (let ((gnus-mark-article-hook nil)
580         (gnus-expert-user t)
581         (nnmail-spool-file nil)
582         (mail-sources nil)
583         (gnus-use-dribble-file nil)
584         (gnus-novice-user nil)
585         (gnus-large-newsgroup nil))
586     ;; Start Gnus.
587     (gnus)
588     ;; Go through all groups...
589     (gnus-group-mark-buffer)
590     (gnus-group-iterate nil
591       (lambda (group)
592         (let (gnus-auto-select-next)
593           (gnus-summary-read-group group nil t)
594           ;; ... and enter the articles into the cache.
595           (when (eq major-mode 'gnus-summary-mode)
596             (gnus-uu-mark-buffer)
597             (gnus-cache-enter-article)
598             (kill-buffer (current-buffer))))))))
599
600 (defun gnus-cache-read-active (&optional force)
601   "Read the cache active file."
602   (gnus-make-directory gnus-cache-directory)
603   (if (or (not (file-exists-p gnus-cache-active-file))
604           (zerop (nth 7 (file-attributes gnus-cache-active-file)))
605           force)
606       ;; There is no active file, so we generate one.
607       (gnus-cache-generate-active)
608     ;; We simply read the active file.
609     (save-excursion
610       (gnus-set-work-buffer)
611       (nnheader-insert-file-contents gnus-cache-active-file)
612       (gnus-active-to-gnus-format
613        nil (setq gnus-cache-active-hashtb
614                  (gnus-make-hashtable
615                   (count-lines (point-min) (point-max)))))
616       (setq gnus-cache-active-altered nil))))
617
618 (defun gnus-cache-write-active (&optional force)
619   "Write the active hashtb to the active file."
620   (when (or force
621             (and gnus-cache-active-hashtb
622                  gnus-cache-active-altered))
623     (gnus-write-active-file gnus-cache-active-file gnus-cache-active-hashtb t)
624     ;; Mark the active hashtb as unaltered.
625     (setq gnus-cache-active-altered nil)))
626
627 (defun gnus-cache-possibly-update-active (group active)
628   "Update active info bounds of GROUP with ACTIVE if necessary.
629 The update is performed if ACTIVE contains a higher or lower bound
630 than the current."
631   (let ((lower t) (higher t))
632     (if gnus-cache-active-hashtb
633         (let ((cache-active (gnus-gethash group gnus-cache-active-hashtb)))
634           (when cache-active
635             (unless (< (car active) (car cache-active))
636               (setq lower nil))
637             (unless (> (cdr active) (cdr cache-active))
638               (setq higher nil))))
639       (gnus-cache-read-active))
640     (when lower
641       (gnus-cache-update-active group (car active) t))
642     (when higher
643       (gnus-cache-update-active group (cdr active)))))
644
645 (defun gnus-cache-update-active (group number &optional low)
646   "Update the upper bound of the active info of GROUP to NUMBER.
647 If LOW, update the lower bound instead."
648   (let ((active (gnus-gethash group gnus-cache-active-hashtb)))
649     (if (null active)
650         ;; We just create a new active entry for this group.
651         (gnus-sethash group (cons number number) gnus-cache-active-hashtb)
652       ;; Update the lower or upper bound.
653       (if low
654           (setcar active number)
655         (setcdr active number)))
656     ;; Mark the active hashtb as altered.
657     (setq gnus-cache-active-altered t)))
658
659 ;;;###autoload
660 (defun gnus-cache-generate-active (&optional directory)
661   "Generate the cache active file."
662   (interactive)
663   (let* ((top (null directory))
664          (directory (expand-file-name (or directory gnus-cache-directory)))
665          (files (directory-files directory 'full))
666          (group
667           (if top
668               ""
669             (string-match
670              (concat "^" (regexp-quote
671                           (file-name-as-directory
672                            (expand-file-name gnus-cache-directory))))
673              (directory-file-name directory))
674             (nnheader-replace-chars-in-string
675              (substring (directory-file-name directory) (match-end 0))
676              ?/ ?.)))
677          nums alphs)
678     (when top
679       (gnus-message 5 "Generating the cache active file...")
680       (setq gnus-cache-active-hashtb (gnus-make-hashtable 123)))
681     (when (string-match "^\\(nn[^_]+\\)_" group)
682       (setq group (replace-match "\\1:" t nil group)))
683     ;; Separate articles from all other files and directories.
684     (while files
685       (if (string-match "^[0-9]+$" (file-name-nondirectory (car files)))
686           (push (string-to-int (file-name-nondirectory (pop files))) nums)
687         (push (pop files) alphs)))
688     ;; If we have nums, then this is probably a valid group.
689     (when (setq nums (sort nums '<))
690       (gnus-sethash group (cons (car nums) (gnus-last-element nums))
691                     gnus-cache-active-hashtb))
692     ;; Go through all the other files.
693     (while alphs
694       (when (and (file-directory-p (car alphs))
695                  (not (string-match "^\\."
696                                     (file-name-nondirectory (car alphs)))))
697         ;; We descend directories.
698         (gnus-cache-generate-active (car alphs)))
699       (setq alphs (cdr alphs)))
700     ;; Write the new active file.
701     (when top
702       (gnus-cache-write-active t)
703       (gnus-message 5 "Generating the cache active file...done"))))
704
705 ;;;###autoload
706 (defun gnus-cache-generate-nov-databases (dir)
707   "Generate NOV files recursively starting in DIR."
708   (interactive (list gnus-cache-directory))
709   (gnus-cache-close)
710   (let ((nnml-generate-active-function 'identity))
711     (nnml-generate-nov-databases-1 dir))
712   (gnus-cache-open))
713
714 (defun gnus-cache-move-cache (dir)
715   "Move the cache tree to somewhere else."
716   (interactive "FMove the cache tree to: ")
717   (rename-file gnus-cache-directory dir))
718
719 (defun gnus-cache-fully-p (&optional group)
720   "Returns non-nil if the cache should be fully used.
721 If GROUP is non-nil, also cater to `gnus-cacheable-groups' and
722 `gnus-uncacheable-groups'."
723   (and gnus-use-cache
724        (not (eq gnus-use-cache 'passive))
725        (if (null group)
726            t
727          (and (or (not gnus-cacheable-groups)
728                   (string-match gnus-cacheable-groups group))
729               (or (not gnus-uncacheable-groups)
730                   (not (string-match gnus-uncacheable-groups group)))))))
731
732 (provide 'gnus-cache)
733
734 ;;; gnus-cache.el ends here