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