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