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