This commit was generated by cvs2svn to compensate for changes in r5947,
[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               (gnus-write-buffer-as-coding-system
128                gnus-cache-overview-coding-system overview-file)
129             ;; Empty overview file, remove it
130             (when (file-exists-p overview-file)
131               (delete-file overview-file))
132             ;; If possible, remove group's cache subdirectory.
133             (condition-case nil
134                 ;; FIXME: we can detect the error type and warn the user
135                 ;; of any inconsistencies (articles w/o nov entries?).
136                 ;; for now, just be conservative...delete only if safe -- sj
137                 (delete-directory (file-name-directory overview-file))
138               (error nil)))))
139       ;; Kill the buffer -- it's either unmodified or saved.
140       (gnus-kill-buffer buffer)
141       (setq gnus-cache-buffer nil))))
142
143 (defun gnus-cache-possibly-enter-article
144   (group article headers ticked dormant unread &optional force)
145   (when (and (or force (not (eq gnus-use-cache 'passive)))
146              (numberp article)
147              (> article 0)
148              (vectorp headers))         ; This might be a dummy article.
149     ;; If this is a virtual group, we find the real group.
150     (when (gnus-virtual-group-p group)
151       (let ((result (nnvirtual-find-group-art
152                      (gnus-group-real-name group) article)))
153         (setq group (car result)
154               headers (copy-sequence headers))
155         (mail-header-set-number headers (cdr result))))
156     (let ((number (mail-header-number headers))
157           file)
158       (when (and number
159                  (> number 0)           ; Reffed article.
160                  (or force
161                      (and (or (not gnus-cacheable-groups)
162                               (string-match gnus-cacheable-groups group))
163                           (or (not gnus-uncacheable-groups)
164                               (not (string-match
165                                     gnus-uncacheable-groups group)))
166                           (gnus-cache-member-of-class
167                            gnus-cache-enter-articles ticked dormant unread)))
168                  (not (file-exists-p (setq file (gnus-cache-file-name
169                                                  group number)))))
170         ;; Possibly create the cache directory.
171         (gnus-make-directory (file-name-directory file))
172         ;; Save the article in the cache.
173         (if (file-exists-p file)
174             t                           ; The article already is saved.
175           (save-excursion
176             (set-buffer nntp-server-buffer)
177             (let ((gnus-use-cache nil))
178               (gnus-request-article-this-buffer number group))
179             (when (> (buffer-size) 0)
180               (gnus-write-buffer file)
181               (gnus-cache-change-buffer group)
182               (set-buffer (cdr gnus-cache-buffer))
183               (goto-char (point-max))
184               (forward-line -1)
185               (while (condition-case ()
186                          (when (not (bobp))
187                            (> (read (current-buffer)) number))
188                        (error
189                         ;; The line was malformed, so we just remove it!!
190                         (gnus-delete-line)
191                         t))
192                 (forward-line -1))
193               (if (bobp)
194                   (if (not (eobp))
195                       (progn
196                         (beginning-of-line)
197                         (when (< (read (current-buffer)) number)
198                           (forward-line 1)))
199                     (beginning-of-line))
200                 (forward-line 1))
201               (beginning-of-line)
202               ;; [number subject from date id references chars lines xref]
203               (insert (format "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t\n"
204                               (mail-header-number headers)
205                               (mail-header-subject headers)
206                               (mail-header-from headers)
207                               (mail-header-date headers)
208                               (mail-header-id headers)
209                               (or (mail-header-references headers) "")
210                               (or (mail-header-chars headers) "")
211                               (or (mail-header-lines headers) "")
212                               (or (mail-header-xref headers) "")))
213               ;; Update the active info.
214               (set-buffer gnus-summary-buffer)
215               (gnus-cache-update-active group number)
216               (push article gnus-newsgroup-cached)
217               (gnus-summary-update-secondary-mark article))
218             t))))))
219
220 (defun gnus-cache-enter-remove-article (article)
221   "Mark ARTICLE for later possible removal."
222   (when article
223     (push article gnus-cache-removable-articles)))
224
225 (defun gnus-cache-possibly-remove-articles ()
226   "Possibly remove some of the removable articles."
227   (if (not (gnus-virtual-group-p gnus-newsgroup-name))
228       (gnus-cache-possibly-remove-articles-1)
229     (let ((arts gnus-cache-removable-articles)
230           ga)
231       (while arts
232         (when (setq ga (nnvirtual-find-group-art
233                         (gnus-group-real-name gnus-newsgroup-name) (pop arts)))
234           (let ((gnus-cache-removable-articles (list (cdr ga)))
235                 (gnus-newsgroup-name (car ga)))
236             (gnus-cache-possibly-remove-articles-1)))))
237     (setq gnus-cache-removable-articles nil)))
238
239 (defun gnus-cache-possibly-remove-articles-1 ()
240   "Possibly remove some of the removable articles."
241   (unless (eq gnus-use-cache 'passive)
242     (let ((articles gnus-cache-removable-articles)
243           (cache-articles gnus-newsgroup-cached)
244           article)
245       (gnus-cache-change-buffer gnus-newsgroup-name)
246       (while articles
247         (when (memq (setq article (pop articles)) cache-articles)
248           ;; The article was in the cache, so we see whether we are
249           ;; supposed to remove it from the cache.
250           (gnus-cache-possibly-remove-article
251            article (memq article gnus-newsgroup-marked)
252            (memq article gnus-newsgroup-dormant)
253            (or (memq article gnus-newsgroup-unreads)
254                (memq article gnus-newsgroup-unselected))))))
255     ;; The overview file might have been modified, save it
256     ;; safe because we're only called at group exit anyway.
257     (gnus-cache-save-buffers)))
258
259 (defun gnus-cache-request-article (article group)
260   "Retrieve ARTICLE in GROUP from the cache."
261   (let ((file (gnus-cache-file-name group article))
262         (buffer-read-only nil))
263     (when (file-exists-p file)
264       (erase-buffer)
265       (gnus-kill-all-overlays)
266       (nnheader-insert-file-contents file)
267       t)))
268
269 (defun gnus-cache-possibly-alter-active (group active)
270   "Alter the ACTIVE info for GROUP to reflect the articles in the cache."
271   (when gnus-cache-active-hashtb
272     (let ((cache-active (gnus-gethash group gnus-cache-active-hashtb)))
273       (when cache-active
274         (when (< (car cache-active) (car active))
275           (setcar active (car cache-active)))
276         (when (> (cdr cache-active) (cdr active))
277           (setcdr active (cdr cache-active)))))))
278
279 (defun gnus-cache-retrieve-headers (articles group &optional fetch-old)
280   "Retrieve the headers for ARTICLES in GROUP."
281   (let ((cached
282          (setq gnus-newsgroup-cached (gnus-cache-articles-in-group group))))
283     (if (not cached)
284         ;; No cached articles here, so we just retrieve them
285         ;; the normal way.
286         (let ((gnus-use-cache nil))
287           (gnus-retrieve-headers articles group fetch-old))
288       (let ((uncached-articles (gnus-sorted-intersection
289                                 (gnus-sorted-complement articles cached)
290                                 articles))
291             (cache-file (gnus-cache-file-name group ".overview"))
292             type)
293         ;; We first retrieve all the headers that we don't have in
294         ;; the cache.
295         (let ((gnus-use-cache nil))
296           (when uncached-articles
297             (setq type (and articles
298                             (gnus-retrieve-headers
299                              uncached-articles group fetch-old)))))
300         (gnus-cache-save-buffers)
301         ;; Then we insert the cached headers.
302         (save-excursion
303           (cond
304            ((not (file-exists-p cache-file))
305             ;; There are no cached headers.
306             type)
307            ((null type)
308             ;; There were no uncached headers (or retrieval was
309             ;; unsuccessful), so we use the cached headers exclusively.
310             (set-buffer nntp-server-buffer)
311             (erase-buffer)
312             (nnheader-insert-file-contents cache-file)
313             'nov)
314            ((eq type 'nov)
315             ;; We have both cached and uncached NOV headers, so we
316             ;; braid them.
317             (gnus-cache-braid-nov group cached)
318             type)
319            (t
320             ;; We braid HEADs.
321             (gnus-cache-braid-heads group (gnus-sorted-intersection
322                                            cached articles))
323             type)))))))
324
325 (defun gnus-cache-enter-article (&optional n)
326   "Enter the next N articles into the cache.
327 If not given a prefix, use the process marked articles instead.
328 Returns the list of articles entered."
329   (interactive "P")
330   (let ((articles (gnus-summary-work-articles n))
331         article out)
332     (while (setq article (pop articles))
333       (gnus-summary-remove-process-mark article)
334       (if (natnump article)
335           (when (gnus-cache-possibly-enter-article
336                  gnus-newsgroup-name article
337                  (gnus-summary-article-header article)
338                  nil nil nil t)
339             (push article out))
340         (gnus-message 2 "Can't cache article %d" article))
341       (gnus-summary-update-secondary-mark article))
342     (gnus-summary-next-subject 1)
343     (gnus-summary-position-point)
344     (nreverse out)))
345
346 (defun gnus-cache-remove-article (n)
347   "Remove the next N articles from the cache.
348 If not given a prefix, use the process marked articles instead.
349 Returns the list of articles removed."
350   (interactive "P")
351   (gnus-cache-change-buffer gnus-newsgroup-name)
352   (let ((articles (gnus-summary-work-articles n))
353         article out)
354     (while articles
355       (setq article (pop articles))
356       (gnus-summary-remove-process-mark article)
357       (when (gnus-cache-possibly-remove-article article nil nil nil t)
358         (push article out))
359       (gnus-summary-update-secondary-mark article))
360     (gnus-summary-next-subject 1)
361     (gnus-summary-position-point)
362     (nreverse out)))
363
364 (defun gnus-cached-article-p (article)
365   "Say whether ARTICLE is cached in the current group."
366   (memq article gnus-newsgroup-cached))
367
368 (defun gnus-summary-insert-cached-articles ()
369   "Insert all the articles cached for this group into the current buffer."
370   (interactive)
371   (let ((cached (sort (copy-sequence gnus-newsgroup-cached) '<))
372         (gnus-verbose (max 6 gnus-verbose)))
373     (unless cached
374       (gnus-message 3 "No cached articles for this group"))
375     (while cached
376       (gnus-summary-goto-subject (pop cached) t))))
377
378 (defalias 'gnus-summary-limit-include-cached
379   'gnus-summary-insert-cached-articles)
380
381 ;;; Internal functions.
382
383 (defun gnus-cache-change-buffer (group)
384   (and gnus-cache-buffer
385        ;; See if the current group's overview cache has been loaded.
386        (or (string= group (car gnus-cache-buffer))
387            ;; Another overview cache is current, save it.
388            (gnus-cache-save-buffers)))
389   ;; if gnus-cache buffer is nil, create it
390   (unless gnus-cache-buffer
391     ;; Create cache buffer
392     (save-excursion
393       (setq gnus-cache-buffer
394             (cons group
395                   (set-buffer (gnus-get-buffer-create
396                                " *gnus-cache-overview*"))))
397       (buffer-disable-undo (current-buffer))
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   (concat (file-name-as-directory gnus-cache-directory)
416           (file-name-as-directory
417            (nnheader-translate-file-chars
418             (if (gnus-use-long-file-name 'not-cache)
419                 group
420               (let ((group (nnheader-replace-chars-in-string group ?/ ?_)))
421                 ;; Translate the first colon into a slash.
422                 (when (string-match ":" group)
423                   (aset group (match-beginning 0) ?/))
424                 (nnheader-replace-chars-in-string group ?. ?/)))
425             t))
426           (if (stringp article) article (int-to-string article))))
427
428 (defun gnus-cache-update-article (group article)
429   "If ARTICLE is in the cache, remove it and re-enter it."
430   (gnus-cache-change-buffer group)
431   (when (gnus-cache-possibly-remove-article article nil nil nil t)    
432     (let ((gnus-use-cache nil))
433       (gnus-cache-possibly-enter-article
434        gnus-newsgroup-name article (gnus-summary-article-header article)
435        nil nil nil t))))
436
437 (defun gnus-cache-possibly-remove-article (article ticked dormant unread
438                                                    &optional force)
439   "Possibly remove ARTICLE from the cache."
440   (let ((group gnus-newsgroup-name)
441         (number article)
442         file)
443     ;; If this is a virtual group, we find the real group.
444     (when (gnus-virtual-group-p group)
445       (let ((result (nnvirtual-find-group-art
446                      (gnus-group-real-name group) article)))
447         (setq group (car result)
448               number (cdr result))))
449     (setq file (gnus-cache-file-name group number))
450     (when (and (file-exists-p file)
451                (or force
452                    (gnus-cache-member-of-class
453                     gnus-cache-remove-articles ticked dormant unread)))
454       (save-excursion
455         (delete-file file)
456         (set-buffer (cdr gnus-cache-buffer))
457         (goto-char (point-min))
458         (when (or (looking-at (concat (int-to-string number) "\t"))
459                   (search-forward (concat "\n" (int-to-string number) "\t")
460                                   (point-max) t))
461           (delete-region (progn (beginning-of-line) (point))
462                          (progn (forward-line 1) (point)))))
463       (setq gnus-newsgroup-cached
464             (delq article gnus-newsgroup-cached))
465       (gnus-summary-update-secondary-mark article)
466       t)))
467
468 (defun gnus-cache-articles-in-group (group)
469   "Return a sorted list of cached articles in GROUP."
470   (let ((dir (file-name-directory (gnus-cache-file-name group 1)))
471         articles)
472     (when (file-exists-p dir)
473       (setq articles
474             (sort (mapcar (lambda (name) (string-to-int name))
475                           (directory-files dir nil "^[0-9]+$" t))
476                   '<))
477       ;; Update the cache active file, just to synch more.
478       (when articles
479         (gnus-cache-update-active group (car articles) t)
480         (gnus-cache-update-active group (car (last articles))))
481       articles)))
482
483 (defun gnus-cache-braid-nov (group cached &optional file)
484   (let ((cache-buf (gnus-get-buffer-create " *gnus-cache*"))
485         beg end)
486     (gnus-cache-save-buffers)
487     (save-excursion
488       (set-buffer cache-buf)
489       (buffer-disable-undo (current-buffer))
490       (erase-buffer)
491       (nnheader-insert-file-contents (or file (gnus-cache-file-name group ".overview")))
492       (goto-char (point-min))
493       (insert "\n")
494       (goto-char (point-min)))
495     (set-buffer nntp-server-buffer)
496     (goto-char (point-min))
497     (while cached
498       (while (and (not (eobp))
499                   (< (read (current-buffer)) (car cached)))
500         (forward-line 1))
501       (beginning-of-line)
502       (save-excursion
503         (set-buffer cache-buf)
504         (if (search-forward (concat "\n" (int-to-string (car cached)) "\t")
505                             nil t)
506             (setq beg (progn (beginning-of-line) (point))
507                   end (progn (end-of-line) (point)))
508           (setq beg nil)))
509       (when beg
510         (insert-buffer-substring cache-buf beg end)
511         (insert "\n"))
512       (setq cached (cdr cached)))
513     (kill-buffer cache-buf)))
514
515 (defun gnus-cache-braid-heads (group cached)
516   (let ((cache-buf (gnus-get-buffer-create " *gnus-cache*")))
517     (save-excursion
518       (set-buffer cache-buf)
519       (buffer-disable-undo (current-buffer))
520       (erase-buffer))
521     (set-buffer nntp-server-buffer)
522     (goto-char (point-min))
523     (while cached
524       (while (and (not (eobp))
525                   (looking-at "2.. +\\([0-9]+\\) ")
526                   (< (progn (goto-char (match-beginning 1))
527                             (read (current-buffer)))
528                      (car cached)))
529         (search-forward "\n.\n" nil 'move))
530       (beginning-of-line)
531       (save-excursion
532         (set-buffer cache-buf)
533         (erase-buffer)
534         (nnheader-insert-file-contents (gnus-cache-file-name group (car cached)))
535         (goto-char (point-min))
536         (insert "220 ")
537         (princ (car cached) (current-buffer))
538         (insert " Article retrieved.\n")
539         (search-forward "\n\n" nil 'move)
540         (delete-region (point) (point-max))
541         (forward-char -1)
542         (insert "."))
543       (insert-buffer-substring cache-buf)
544       (setq cached (cdr cached)))
545     (kill-buffer cache-buf)))
546
547 ;;;###autoload
548 (defun gnus-jog-cache ()
549   "Go through all groups and put the articles into the cache.
550
551 Usage:
552 $ emacs -batch -l ~/.emacs -l gnus -f gnus-jog-cache"
553   (interactive)
554   (let ((gnus-mark-article-hook nil)
555         (gnus-expert-user t)
556         (nnmail-spool-file nil)
557         (gnus-use-dribble-file nil)
558         (gnus-novice-user nil)
559         (gnus-large-newsgroup nil))
560     ;; Start Gnus.
561     (gnus)
562     ;; Go through all groups...
563     (gnus-group-mark-buffer)
564     (gnus-group-iterate nil
565       (lambda (group)
566         (let (gnus-auto-select-next)
567           (gnus-summary-read-group group nil t)
568           ;; ... and enter the articles into the cache.
569           (when (eq major-mode 'gnus-summary-mode)
570             (gnus-uu-mark-buffer)
571             (gnus-cache-enter-article)
572             (kill-buffer (current-buffer))))))))
573
574 (defun gnus-cache-read-active (&optional force)
575   "Read the cache active file."
576   (gnus-make-directory gnus-cache-directory)
577   (if (or (not (file-exists-p gnus-cache-active-file))
578           (not (zerop (nth 7 (file-attributes gnus-cache-active-file))))
579           force)
580       ;; There is no active file, so we generate one.
581       (gnus-cache-generate-active)
582     ;; We simply read the active file.
583     (save-excursion
584       (gnus-set-work-buffer)
585       (nnheader-insert-file-contents gnus-cache-active-file)
586       (gnus-active-to-gnus-format
587        nil (setq gnus-cache-active-hashtb
588                  (gnus-make-hashtable
589                   (count-lines (point-min) (point-max)))))
590       (setq gnus-cache-active-altered nil))))
591
592 (defun gnus-cache-write-active (&optional force)
593   "Write the active hashtb to the active file."
594   (when (or force
595             (and gnus-cache-active-hashtb
596                  gnus-cache-active-altered))
597     (nnheader-temp-write gnus-cache-active-file
598       (mapatoms
599        (lambda (sym)
600          (when (and sym (boundp sym))
601            (insert (format "%s %d %d y\n"
602                            (symbol-name sym) (cdr (symbol-value sym))
603                            (car (symbol-value sym))))))
604        gnus-cache-active-hashtb))
605     ;; Mark the active hashtb as unaltered.
606     (setq gnus-cache-active-altered nil)))
607
608 (defun gnus-cache-update-active (group number &optional low)
609   "Update the upper bound of the active info of GROUP to NUMBER.
610 If LOW, update the lower bound instead."
611   (let ((active (gnus-gethash group gnus-cache-active-hashtb)))
612     (if (null active)
613         ;; We just create a new active entry for this group.
614         (gnus-sethash group (cons number number) gnus-cache-active-hashtb)
615       ;; Update the lower or upper bound.
616       (if low
617           (setcar active number)
618         (setcdr active number)))
619     ;; Mark the active hashtb as altered.
620     (setq gnus-cache-active-altered t)))
621
622 ;;;###autoload
623 (defun gnus-cache-generate-active (&optional directory)
624   "Generate the cache active file."
625   (interactive)
626   (let* ((top (null directory))
627          (directory (expand-file-name (or directory gnus-cache-directory)))
628          (files (directory-files directory 'full))
629          (group
630           (if top
631               ""
632             (string-match
633              (concat "^" (regexp-quote
634                           (file-name-as-directory
635                            (expand-file-name gnus-cache-directory))))
636              (directory-file-name directory))
637             (nnheader-replace-chars-in-string
638              (substring (directory-file-name directory) (match-end 0))
639              ?/ ?.)))
640          nums alphs)
641     (when top
642       (gnus-message 5 "Generating the cache active file...")
643       (setq gnus-cache-active-hashtb (gnus-make-hashtable 123)))
644     (when (string-match "^\\(nn[^_]+\\)_" group)
645       (setq group (replace-match "\\1:" t t group)))
646     ;; Separate articles from all other files and directories.
647     (while files
648       (if (string-match "^[0-9]+$" (file-name-nondirectory (car files)))
649           (push (string-to-int (file-name-nondirectory (pop files))) nums)
650         (push (pop files) alphs)))
651     ;; If we have nums, then this is probably a valid group.
652     (when (setq nums (sort nums '<))
653       (gnus-sethash group (cons (car nums) (gnus-last-element nums))
654                     gnus-cache-active-hashtb))
655     ;; Go through all the other files.
656     (while alphs
657       (when (and (file-directory-p (car alphs))
658                  (not (string-match "^\\."
659                                     (file-name-nondirectory (car alphs)))))
660         ;; We descend directories.
661         (gnus-cache-generate-active (car alphs)))
662       (setq alphs (cdr alphs)))
663     ;; Write the new active file.
664     (when top
665       (gnus-cache-write-active t)
666       (gnus-message 5 "Generating the cache active file...done"))))
667
668 ;;;###autoload
669 (defun gnus-cache-generate-nov-databases (dir)
670   "Generate NOV files recursively starting in DIR."
671   (interactive (list gnus-cache-directory))
672   (gnus-cache-close)
673   (let ((nnml-generate-active-function 'identity))
674     (nnml-generate-nov-databases-1 dir)))
675
676 (defun gnus-cache-move-cache (dir)
677   "Move the cache tree to somewhere else."
678   (interactive "FMove the cache tree to: ")
679   (rename-file gnus-cache-directory dir))
680
681 (provide 'gnus-cache)
682
683 ;;; gnus-cache.el ends here