* nnagent.el (nnagent-retrieve-headers): Use gnus-sorted-difference.
[gnus] / lisp / gnus-cache.el
1 ;;; gnus-cache.el --- cache interface for Gnus
2 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; 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               (push article gnus-newsgroup-cached)
210               (gnus-summary-update-secondary-mark article))
211             t))))))
212
213 (defun gnus-cache-enter-remove-article (article)
214   "Mark ARTICLE for later possible removal."
215   (when article
216     (push article gnus-cache-removable-articles)))
217
218 (defun gnus-cache-possibly-remove-articles ()
219   "Possibly remove some of the removable articles."
220   (if (not (gnus-virtual-group-p gnus-newsgroup-name))
221       (gnus-cache-possibly-remove-articles-1)
222     (let ((arts gnus-cache-removable-articles)
223           ga)
224       (while arts
225         (when (setq ga (nnvirtual-find-group-art
226                         (gnus-group-real-name gnus-newsgroup-name) (pop arts)))
227           (let ((gnus-cache-removable-articles (list (cdr ga)))
228                 (gnus-newsgroup-name (car ga)))
229             (gnus-cache-possibly-remove-articles-1)))))
230     (setq gnus-cache-removable-articles nil)))
231
232 (defun gnus-cache-possibly-remove-articles-1 ()
233   "Possibly remove some of the removable articles."
234   (when (gnus-cache-fully-p gnus-newsgroup-name)
235     (let ((articles gnus-cache-removable-articles)
236           (cache-articles gnus-newsgroup-cached)
237           article)
238       (gnus-cache-change-buffer gnus-newsgroup-name)
239       (while articles
240         (when (memq (setq article (pop articles)) cache-articles)
241           ;; The article was in the cache, so we see whether we are
242           ;; supposed to remove it from the cache.
243           (gnus-cache-possibly-remove-article
244            article (memq article gnus-newsgroup-marked)
245            (memq article gnus-newsgroup-dormant)
246            (or (memq article gnus-newsgroup-unreads)
247                (memq article gnus-newsgroup-unselected))))))
248     ;; The overview file might have been modified, save it
249     ;; safe because we're only called at group exit anyway.
250     (gnus-cache-save-buffers)))
251
252 (defun gnus-cache-request-article (article group)
253   "Retrieve ARTICLE in GROUP from the cache."
254   (let ((file (gnus-cache-file-name group article))
255         (buffer-read-only nil))
256     (when (file-exists-p file)
257       (erase-buffer)
258       (gnus-kill-all-overlays)
259       (let ((coding-system-for-read gnus-cache-coding-system))
260         (insert-file-contents file))
261       t)))
262
263 (defun gnus-cache-possibly-alter-active (group active)
264   "Alter the ACTIVE info for GROUP to reflect the articles in the cache."
265   (when gnus-cache-active-hashtb
266     (let ((cache-active (gnus-gethash group gnus-cache-active-hashtb)))
267       (when cache-active
268         (when (< (car cache-active) (car active))
269           (setcar active (car cache-active)))
270         (when (> (cdr cache-active) (cdr active))
271           (setcdr active (cdr cache-active)))))))
272
273 (defun gnus-cache-retrieve-headers (articles group &optional fetch-old)
274   "Retrieve the headers for ARTICLES in GROUP."
275   (let ((cached
276          (setq gnus-newsgroup-cached (gnus-cache-articles-in-group group))))
277     (if (not cached)
278         ;; No cached articles here, so we just retrieve them
279         ;; the normal way.
280         (let ((gnus-use-cache nil))
281           (gnus-retrieve-headers articles group fetch-old))
282       (let ((uncached-articles (gnus-sorted-difference articles cached))
283             (cache-file (gnus-cache-file-name group ".overview"))
284             type)
285         ;; We first retrieve all the headers that we don't have in
286         ;; the cache.
287         (let ((gnus-use-cache nil))
288           (when uncached-articles
289             (setq type (and articles
290                             (gnus-retrieve-headers
291                              uncached-articles group fetch-old)))))
292         (gnus-cache-save-buffers)
293         ;; Then we insert the cached headers.
294         (save-excursion
295           (cond
296            ((not (file-exists-p cache-file))
297             ;; There are no cached headers.
298             type)
299            ((null type)
300             ;; There were no uncached headers (or retrieval was
301             ;; unsuccessful), so we use the cached headers exclusively.
302             (set-buffer nntp-server-buffer)
303             (erase-buffer)
304             (let ((coding-system-for-read
305                    gnus-cache-overview-coding-system))
306               (insert-file-contents cache-file))
307             'nov)
308            ((eq type 'nov)
309             ;; We have both cached and uncached NOV headers, so we
310             ;; braid them.
311             (gnus-cache-braid-nov group cached)
312             type)
313            (t
314             ;; We braid HEADs.
315             (gnus-cache-braid-heads group (gnus-sorted-intersection
316                                            cached articles))
317             type)))))))
318
319 (defun gnus-cache-enter-article (&optional n)
320   "Enter the next N articles into the cache.
321 If not given a prefix, use the process marked articles instead.
322 Returns the list of articles entered."
323   (interactive "P")
324   (let ((articles (gnus-summary-work-articles n))
325         article out)
326     (while (setq article (pop articles))
327       (gnus-summary-remove-process-mark article)
328       (if (natnump article)
329           (when (gnus-cache-possibly-enter-article
330                  gnus-newsgroup-name article
331                  nil nil nil t)
332             (push article out))
333         (gnus-message 2 "Can't cache article %d" article))
334       (gnus-summary-update-secondary-mark article))
335     (gnus-summary-next-subject 1)
336     (gnus-summary-position-point)
337     (nreverse out)))
338
339 (defun gnus-cache-remove-article (n)
340   "Remove the next N articles from the cache.
341 If not given a prefix, use the process marked articles instead.
342 Returns the list of articles removed."
343   (interactive "P")
344   (gnus-cache-change-buffer gnus-newsgroup-name)
345   (let ((articles (gnus-summary-work-articles n))
346         article out)
347     (while articles
348       (setq article (pop articles))
349       (gnus-summary-remove-process-mark article)
350       (when (gnus-cache-possibly-remove-article article nil nil nil t)
351         (push article out))
352       (gnus-summary-update-secondary-mark article))
353     (gnus-summary-next-subject 1)
354     (gnus-summary-position-point)
355     (nreverse out)))
356
357 (defun gnus-cached-article-p (article)
358   "Say whether ARTICLE is cached in the current group."
359   (memq article gnus-newsgroup-cached))
360
361 (defun gnus-summary-insert-cached-articles ()
362   "Insert all the articles cached for this group into the current buffer."
363   (interactive)
364   (let ((cached (sort (copy-sequence gnus-newsgroup-cached) '>))
365         (gnus-verbose (max 6 gnus-verbose)))
366     (unless cached
367       (gnus-message 3 "No cached articles for this group"))
368     (while cached
369       (gnus-summary-goto-subject (pop cached) t))))
370
371 (defun gnus-summary-limit-include-cached ()
372   "Limit the summary buffer to articles that are cached."
373   (interactive)
374   (let ((cached (sort (copy-sequence gnus-newsgroup-cached) '>))
375         (gnus-verbose (max 6 gnus-verbose)))
376     (if cached
377         (progn
378           (gnus-summary-limit cached)
379           (gnus-summary-position-point))
380         (gnus-message 3 "No cached articles for this group"))))
381
382 ;;; Internal functions.
383
384 (defun gnus-cache-change-buffer (group)
385   (and gnus-cache-buffer
386        ;; See if the current group's overview cache has been loaded.
387        (or (string= group (car gnus-cache-buffer))
388            ;; Another overview cache is current, save it.
389            (gnus-cache-save-buffers)))
390   ;; if gnus-cache buffer is nil, create it
391   (unless gnus-cache-buffer
392     ;; Create cache buffer
393     (save-excursion
394       (setq gnus-cache-buffer
395             (cons group
396                   (set-buffer (gnus-get-buffer-create
397                                " *gnus-cache-overview*"))))
398       ;; Insert the contents of this group's cache overview.
399       (erase-buffer)
400       (let ((file (gnus-cache-file-name group ".overview")))
401         (when (file-exists-p file)
402           (nnheader-insert-file-contents file)))
403       ;; We have a fresh (empty/just loaded) buffer,
404       ;; mark it as unmodified to save a redundant write later.
405       (set-buffer-modified-p nil))))
406
407 ;; Return whether an article is a member of a class.
408 (defun gnus-cache-member-of-class (class ticked dormant unread)
409   (or (and ticked (memq 'ticked class))
410       (and dormant (memq 'dormant class))
411       (and unread (memq 'unread class))
412       (and (not unread) (not ticked) (not dormant) (memq 'read class))))
413
414 (defun gnus-cache-file-name (group article)
415   (expand-file-name
416    (if (stringp article) article (int-to-string article))
417    (file-name-as-directory
418     (expand-file-name
419      (nnheader-translate-file-chars
420       (if (gnus-use-long-file-name 'not-cache)
421           group
422         (let ((group (nnheader-replace-duplicate-chars-in-string
423                       (nnheader-replace-chars-in-string group ?/ ?_)
424                       ?. ?_)))
425           ;; Translate the first colon into a slash.
426           (when (string-match ":" group)
427                   (setq group (concat (substring group 0 (match-beginning 0))
428                                       "/" (substring group (match-end 0)))))
429           (nnheader-replace-chars-in-string group ?. ?/)))
430       t)
431      gnus-cache-directory))))
432
433 (defun gnus-cache-update-article (group article)
434   "If ARTICLE is in the cache, remove it and re-enter it."
435   (gnus-cache-change-buffer group)
436   (when (gnus-cache-possibly-remove-article article nil nil nil t)
437     (let ((gnus-use-cache nil))
438       (gnus-cache-possibly-enter-article
439        gnus-newsgroup-name article
440        nil nil nil t))))
441
442 (defun gnus-cache-possibly-remove-article (article ticked dormant unread
443                                                    &optional force)
444   "Possibly remove ARTICLE from the cache."
445   (let ((group gnus-newsgroup-name)
446         (number article)
447         file)
448     ;; If this is a virtual group, we find the real group.
449     (when (gnus-virtual-group-p group)
450       (let ((result (nnvirtual-find-group-art
451                      (gnus-group-real-name group) article)))
452         (setq group (car result)
453               number (cdr result))))
454     (setq file (gnus-cache-file-name group number))
455     (when (and (file-exists-p file)
456                (or force
457                    (gnus-cache-member-of-class
458                     gnus-cache-remove-articles ticked dormant unread)))
459       (save-excursion
460         (delete-file file)
461         (set-buffer (cdr gnus-cache-buffer))
462         (goto-char (point-min))
463         (when (or (looking-at (concat (int-to-string number) "\t"))
464                   (search-forward (concat "\n" (int-to-string number) "\t")
465                                   (point-max) t))
466           (delete-region (progn (beginning-of-line) (point))
467                          (progn (forward-line 1) (point)))))
468       (setq gnus-newsgroup-cached
469             (delq article gnus-newsgroup-cached))
470       (gnus-summary-update-secondary-mark article)
471       t)))
472
473 (defun gnus-cache-articles-in-group (group)
474   "Return a sorted list of cached articles in GROUP."
475   (let ((dir (file-name-directory (gnus-cache-file-name group 1)))
476         articles)
477     (when (file-exists-p dir)
478       (setq articles
479             (sort (mapcar (lambda (name) (string-to-int name))
480                           (directory-files dir nil "^[0-9]+$" t))
481                   '<))
482       ;; Update the cache active file, just to synch more.
483       (when articles
484         (gnus-cache-update-active group (car articles) t)
485         (gnus-cache-update-active group (car (last articles))))
486       articles)))
487
488 (defun gnus-cache-braid-nov (group cached &optional file)
489   (let ((cache-buf (gnus-get-buffer-create " *gnus-cache*"))
490         beg end)
491     (gnus-cache-save-buffers)
492     (save-excursion
493       (set-buffer cache-buf)
494       (erase-buffer)
495       (let ((coding-system-for-read
496              gnus-cache-overview-coding-system))
497         (insert-file-contents
498          (or file (gnus-cache-file-name group ".overview"))))
499       (goto-char (point-min))
500       (insert "\n")
501       (goto-char (point-min)))
502     (set-buffer nntp-server-buffer)
503     (goto-char (point-min))
504     (while cached
505       (while (and (not (eobp))
506                   (< (read (current-buffer)) (car cached)))
507         (forward-line 1))
508       (beginning-of-line)
509       (save-excursion
510         (set-buffer cache-buf)
511         (if (search-forward (concat "\n" (int-to-string (car cached)) "\t")
512                             nil t)
513             (setq beg (progn (beginning-of-line) (point))
514                   end (progn (end-of-line) (point)))
515           (setq beg nil)))
516       (when beg
517         (insert-buffer-substring cache-buf beg end)
518         (insert "\n"))
519       (setq cached (cdr cached)))
520     (kill-buffer cache-buf)))
521
522 (defun gnus-cache-braid-heads (group cached)
523   (let ((cache-buf (gnus-get-buffer-create " *gnus-cache*")))
524     (save-excursion
525       (set-buffer cache-buf)
526       (erase-buffer))
527     (set-buffer nntp-server-buffer)
528     (goto-char (point-min))
529     (while cached
530       (while (and (not (eobp))
531                   (looking-at "2.. +\\([0-9]+\\) ")
532                   (< (progn (goto-char (match-beginning 1))
533                             (read (current-buffer)))
534                      (car cached)))
535         (search-forward "\n.\n" nil 'move))
536       (beginning-of-line)
537       (save-excursion
538         (set-buffer cache-buf)
539         (erase-buffer)
540         (let ((coding-system-for-read
541                gnus-cache-coding-system))
542           (insert-file-contents (gnus-cache-file-name group (car cached))))
543         (goto-char (point-min))
544         (insert "220 ")
545         (princ (car cached) (current-buffer))
546         (insert " Article retrieved.\n")
547         (search-forward "\n\n" nil 'move)
548         (delete-region (point) (point-max))
549         (forward-char -1)
550         (insert "."))
551       (insert-buffer-substring cache-buf)
552       (setq cached (cdr cached)))
553     (kill-buffer cache-buf)))
554
555 ;;;###autoload
556 (defun gnus-jog-cache ()
557   "Go through all groups and put the articles into the cache.
558
559 Usage:
560 $ emacs -batch -l ~/.emacs -l gnus -f gnus-jog-cache"
561   (interactive)
562   (let ((gnus-mark-article-hook nil)
563         (gnus-expert-user t)
564         (nnmail-spool-file nil)
565         (mail-sources nil)
566         (gnus-use-dribble-file nil)
567         (gnus-novice-user nil)
568         (gnus-large-newsgroup nil))
569     ;; Start Gnus.
570     (gnus)
571     ;; Go through all groups...
572     (gnus-group-mark-buffer)
573     (gnus-group-iterate nil
574       (lambda (group)
575         (let (gnus-auto-select-next)
576           (gnus-summary-read-group group nil t)
577           ;; ... and enter the articles into the cache.
578           (when (eq major-mode 'gnus-summary-mode)
579             (gnus-uu-mark-buffer)
580             (gnus-cache-enter-article)
581             (kill-buffer (current-buffer))))))))
582
583 (defun gnus-cache-read-active (&optional force)
584   "Read the cache active file."
585   (gnus-make-directory gnus-cache-directory)
586   (if (or (not (file-exists-p gnus-cache-active-file))
587           (zerop (nth 7 (file-attributes gnus-cache-active-file)))
588           force)
589       ;; There is no active file, so we generate one.
590       (gnus-cache-generate-active)
591     ;; We simply read the active file.
592     (save-excursion
593       (gnus-set-work-buffer)
594       (nnheader-insert-file-contents gnus-cache-active-file)
595       (gnus-active-to-gnus-format
596        nil (setq gnus-cache-active-hashtb
597                  (gnus-make-hashtable
598                   (count-lines (point-min) (point-max)))))
599       (setq gnus-cache-active-altered nil))))
600
601 (defun gnus-cache-write-active (&optional force)
602   "Write the active hashtb to the active file."
603   (when (or force
604             (and gnus-cache-active-hashtb
605                  gnus-cache-active-altered))
606     (gnus-write-active-file gnus-cache-active-file gnus-cache-active-hashtb t)
607     ;; Mark the active hashtb as unaltered.
608     (setq gnus-cache-active-altered nil)))
609
610 (defun gnus-cache-possibly-update-active (group active)
611   "Update active info bounds of GROUP with ACTIVE if necessary.
612 The update is performed if ACTIVE contains a higher or lower bound
613 than the current."
614   (let ((lower t) (higher t))
615     (if gnus-cache-active-hashtb
616         (let ((cache-active (gnus-gethash group gnus-cache-active-hashtb)))
617           (when cache-active
618             (unless (< (car active) (car cache-active))
619               (setq lower nil))
620             (unless (> (cdr active) (cdr cache-active))
621               (setq higher nil))))
622       (gnus-cache-read-active))
623     (when lower
624       (gnus-cache-update-active group (car active) t))
625     (when higher
626       (gnus-cache-update-active group (cdr active)))))
627
628 (defun gnus-cache-update-active (group number &optional low)
629   "Update the upper bound of the active info of GROUP to NUMBER.
630 If LOW, update the lower bound instead."
631   (let ((active (gnus-gethash group gnus-cache-active-hashtb)))
632     (if (null active)
633         ;; We just create a new active entry for this group.
634         (gnus-sethash group (cons number number) gnus-cache-active-hashtb)
635       ;; Update the lower or upper bound.
636       (if low
637           (setcar active number)
638         (setcdr active number)))
639     ;; Mark the active hashtb as altered.
640     (setq gnus-cache-active-altered t)))
641
642 ;;;###autoload
643 (defun gnus-cache-generate-active (&optional directory)
644   "Generate the cache active file."
645   (interactive)
646   (let* ((top (null directory))
647          (directory (expand-file-name (or directory gnus-cache-directory)))
648          (files (directory-files directory 'full))
649          (group
650           (if top
651               ""
652             (string-match
653              (concat "^" (regexp-quote
654                           (file-name-as-directory
655                            (expand-file-name gnus-cache-directory))))
656              (directory-file-name directory))
657             (nnheader-replace-chars-in-string
658              (substring (directory-file-name directory) (match-end 0))
659              ?/ ?.)))
660          nums alphs)
661     (when top
662       (gnus-message 5 "Generating the cache active file...")
663       (setq gnus-cache-active-hashtb (gnus-make-hashtable 123)))
664     (when (string-match "^\\(nn[^_]+\\)_" group)
665       (setq group (replace-match "\\1:" t t group)))
666     ;; Separate articles from all other files and directories.
667     (while files
668       (if (string-match "^[0-9]+$" (file-name-nondirectory (car files)))
669           (push (string-to-int (file-name-nondirectory (pop files))) nums)
670         (push (pop files) alphs)))
671     ;; If we have nums, then this is probably a valid group.
672     (when (setq nums (sort nums '<))
673       (gnus-sethash group (cons (car nums) (gnus-last-element nums))
674                     gnus-cache-active-hashtb))
675     ;; Go through all the other files.
676     (while alphs
677       (when (and (file-directory-p (car alphs))
678                  (not (string-match "^\\."
679                                     (file-name-nondirectory (car alphs)))))
680         ;; We descend directories.
681         (gnus-cache-generate-active (car alphs)))
682       (setq alphs (cdr alphs)))
683     ;; Write the new active file.
684     (when top
685       (gnus-cache-write-active t)
686       (gnus-message 5 "Generating the cache active file...done"))))
687
688 ;;;###autoload
689 (defun gnus-cache-generate-nov-databases (dir)
690   "Generate NOV files recursively starting in DIR."
691   (interactive (list gnus-cache-directory))
692   (gnus-cache-close)
693   (let ((nnml-generate-active-function 'identity))
694     (nnml-generate-nov-databases-1 dir))
695   (gnus-cache-open))
696
697 (defun gnus-cache-move-cache (dir)
698   "Move the cache tree to somewhere else."
699   (interactive "FMove the cache tree to: ")
700   (rename-file gnus-cache-directory dir))
701
702 (defun gnus-cache-fully-p (&optional group)
703   "Returns non-nil if the cache should be fully used.
704 If GROUP is non-nil, also cater to `gnus-cacheable-groups' and
705 `gnus-uncacheable-groups'."
706   (and gnus-use-cache
707        (not (eq gnus-use-cache 'passive))
708        (if (null group)
709            t
710          (and (or (not gnus-cacheable-groups)
711                   (string-match gnus-cacheable-groups group))
712               (or (not gnus-uncacheable-groups)
713                   (not (string-match gnus-uncacheable-groups group)))))))
714        
715
716 (provide 'gnus-cache)
717
718 ;;; gnus-cache.el ends here