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