4fd933b49bdef5e3e72e249f7b671692af51471d
[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)
156              (vectorp headers))         ; This might be a dummy article.
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-retrieve-parsed-headers (articles group &optional fetch-old
330                                                     dependencies force-new)
331   "Retrieve the parsed-headers for ARTICLES in GROUP."
332   (let ((cached
333          (setq gnus-newsgroup-cached (gnus-cache-articles-in-group group))))
334     (if (not cached)
335         ;; No cached articles here, so we just retrieve them
336         ;; the normal way.
337         (let ((gnus-use-cache nil))
338           (gnus-retrieve-parsed-headers articles group fetch-old
339                                         dependencies force-new))
340       (let ((uncached-articles (gnus-sorted-intersection
341                                 (gnus-sorted-complement articles cached)
342                                 articles))
343             (cache-file (gnus-cache-file-name group ".overview")))
344         (gnus-cache-braid-headers
345          ;; We first retrieve all the headers that we don't have in
346          ;; the cache.
347          (prog1
348              (let ((gnus-use-cache nil))
349                (when uncached-articles
350                  (and articles
351                       (gnus-retrieve-parsed-headers
352                        uncached-articles group fetch-old
353                        dependencies))
354                  ))
355            (gnus-cache-save-buffers))
356          ;; Then we insert the cached headers.
357          (cond ((not (file-exists-p cache-file))
358                 ;; There are no cached headers.
359                 )
360                ((eq gnus-headers-retrieved-by 'nov)
361                 (with-current-buffer nntp-server-buffer
362                   (erase-buffer)
363                   (nnheader-insert-file-contents cache-file)
364                   (nnheader-get-newsgroup-headers-xover*
365                    articles nil dependencies group)
366                   ))
367                (t
368                 ;; We braid HEADs.
369                 (nnheader-retrieve-headers-from-directory*
370                  cached
371                  (expand-file-name
372                   (file-name-as-directory
373                    (nnheader-translate-file-chars
374                     (if (gnus-use-long-file-name 'not-cache)
375                         group
376                       (let ((group
377                              (nnheader-replace-chars-in-string group ?/ ?_)))
378                         ;; Translate the first colon into a slash.
379                         (when (string-match ":" group)
380                           (aset group (match-beginning 0) ?/))
381                         (nnheader-replace-chars-in-string group ?. ?/)))
382                     t))
383                   gnus-cache-directory)
384                  dependencies)
385                 )))
386         ))))
387
388 (defun gnus-cache-enter-article (&optional n)
389   "Enter the next N articles into the cache.
390 If not given a prefix, use the process marked articles instead.
391 Returns the list of articles entered."
392   (interactive "P")
393   (let ((articles (gnus-summary-work-articles n))
394         article out)
395     (while (setq article (pop articles))
396       (gnus-summary-remove-process-mark article)
397       (if (natnump article)
398           (when (gnus-cache-possibly-enter-article
399                  gnus-newsgroup-name article
400                  (gnus-summary-article-header article)
401                  nil nil nil t)
402             (push article out))
403         (gnus-message 2 "Can't cache article %d" article))
404       (gnus-summary-update-secondary-mark article))
405     (gnus-summary-next-subject 1)
406     (gnus-summary-position-point)
407     (nreverse out)))
408
409 (defun gnus-cache-remove-article (n)
410   "Remove the next N articles from the cache.
411 If not given a prefix, use the process marked articles instead.
412 Returns the list of articles removed."
413   (interactive "P")
414   (gnus-cache-change-buffer gnus-newsgroup-name)
415   (let ((articles (gnus-summary-work-articles n))
416         article out)
417     (while articles
418       (setq article (pop articles))
419       (gnus-summary-remove-process-mark article)
420       (when (gnus-cache-possibly-remove-article article nil nil nil t)
421         (push article out))
422       (gnus-summary-update-secondary-mark article))
423     (gnus-summary-next-subject 1)
424     (gnus-summary-position-point)
425     (nreverse out)))
426
427 (defun gnus-cached-article-p (article)
428   "Say whether ARTICLE is cached in the current group."
429   (memq article gnus-newsgroup-cached))
430
431 (defun gnus-summary-insert-cached-articles ()
432   "Insert all the articles cached for this group into the current buffer."
433   (interactive)
434   (let ((cached (sort (copy-sequence gnus-newsgroup-cached) '>))
435         (gnus-verbose (max 6 gnus-verbose)))
436     (unless cached
437       (gnus-message 3 "No cached articles for this group"))
438     (while cached
439       (gnus-summary-goto-subject (pop cached) t))))
440
441 (defalias 'gnus-summary-limit-include-cached
442   'gnus-summary-insert-cached-articles)
443
444 ;;; Internal functions.
445
446 (defun gnus-cache-change-buffer (group)
447   (and gnus-cache-buffer
448        ;; See if the current group's overview cache has been loaded.
449        (or (string= group (car gnus-cache-buffer))
450            ;; Another overview cache is current, save it.
451            (gnus-cache-save-buffers)))
452   ;; if gnus-cache buffer is nil, create it
453   (unless gnus-cache-buffer
454     ;; Create cache buffer
455     (save-excursion
456       (setq gnus-cache-buffer
457             (cons group
458                   (set-buffer (gnus-get-buffer-create
459                                " *gnus-cache-overview*"))))
460       ;; Insert the contents of this group's cache overview.
461       (erase-buffer)
462       (let ((file (gnus-cache-file-name group ".overview")))
463         (when (file-exists-p file)
464           (nnheader-insert-file-contents file)))
465       ;; We have a fresh (empty/just loaded) buffer,
466       ;; mark it as unmodified to save a redundant write later.
467       (set-buffer-modified-p nil))))
468
469 ;; Return whether an article is a member of a class.
470 (defun gnus-cache-member-of-class (class ticked dormant unread)
471   (or (and ticked (memq 'ticked class))
472       (and dormant (memq 'dormant class))
473       (and unread (memq 'unread class))
474       (and (not unread) (not ticked) (not dormant) (memq 'read class))))
475
476 (defun gnus-cache-file-name (group article)
477   (expand-file-name
478    (if (stringp article) article (int-to-string article))
479    (file-name-as-directory
480     (expand-file-name
481      (nnheader-translate-file-chars
482       (if (gnus-use-long-file-name 'not-cache)
483           group
484         (let ((group (nnheader-replace-duplicate-chars-in-string
485                       (nnheader-replace-chars-in-string group ?/ ?_)
486                       ?. ?_)))
487           ;; Translate the first colon into a slash.
488           (when (string-match ":" group)
489                   (setq group (concat (substring group 0 (match-beginning 0))
490                                       "/" (substring group (match-end 0)))))
491           (nnheader-replace-chars-in-string group ?. ?/)))
492       t)
493      gnus-cache-directory))))
494
495 (defun gnus-cache-update-article (group article)
496   "If ARTICLE is in the cache, remove it and re-enter it."
497   (gnus-cache-change-buffer group)
498   (when (gnus-cache-possibly-remove-article article nil nil nil t)
499     (let ((gnus-use-cache nil))
500       (gnus-cache-possibly-enter-article
501        gnus-newsgroup-name article (gnus-summary-article-header article)
502        nil nil nil t))))
503
504 (defun gnus-cache-possibly-remove-article (article ticked dormant unread
505                                                    &optional force)
506   "Possibly remove ARTICLE from the cache."
507   (let ((group gnus-newsgroup-name)
508         (number article)
509         file)
510     ;; If this is a virtual group, we find the real group.
511     (when (gnus-virtual-group-p group)
512       (let ((result (nnvirtual-find-group-art
513                      (gnus-group-real-name group) article)))
514         (setq group (car result)
515               number (cdr result))))
516     (setq file (gnus-cache-file-name group number))
517     (when (and (file-exists-p file)
518                (or force
519                    (gnus-cache-member-of-class
520                     gnus-cache-remove-articles ticked dormant unread)))
521       (save-excursion
522         (delete-file file)
523         (set-buffer (cdr gnus-cache-buffer))
524         (goto-char (point-min))
525         (when (or (looking-at (concat (int-to-string number) "\t"))
526                   (search-forward (concat "\n" (int-to-string number) "\t")
527                                   (point-max) t))
528           (delete-region (progn (beginning-of-line) (point))
529                          (progn (forward-line 1) (point)))))
530       (setq gnus-newsgroup-cached
531             (delq article gnus-newsgroup-cached))
532       (gnus-summary-update-secondary-mark article)
533       t)))
534
535 (defun gnus-cache-articles-in-group (group)
536   "Return a sorted list of cached articles in GROUP."
537   (let ((dir (file-name-directory (gnus-cache-file-name group 1)))
538         articles)
539     (when (file-exists-p dir)
540       (setq articles
541             (sort (mapcar (lambda (name) (string-to-int name))
542                           (directory-files dir nil "^[0-9]+$" t))
543                   '<))
544       ;; Update the cache active file, just to synch more.
545       (when articles
546         (gnus-cache-update-active group (car articles) t)
547         (gnus-cache-update-active group (car (last articles))))
548       articles)))
549
550 (defun gnus-cache-braid-nov (group cached &optional file)
551   (let ((cache-buf (gnus-get-buffer-create " *gnus-cache*"))
552         beg end)
553     (gnus-cache-save-buffers)
554     (save-excursion
555       (set-buffer cache-buf)
556       (erase-buffer)
557       (let ((nnheader-file-coding-system gnus-cache-overview-coding-system))
558         (nnheader-insert-file-contents
559          (or file (gnus-cache-file-name group ".overview"))))
560       (goto-char (point-min))
561       (insert "\n")
562       (goto-char (point-min)))
563     (set-buffer nntp-server-buffer)
564     (goto-char (point-min))
565     (while cached
566       (while (and (not (eobp))
567                   (< (read (current-buffer)) (car cached)))
568         (forward-line 1))
569       (beginning-of-line)
570       (save-excursion
571         (set-buffer cache-buf)
572         (if (search-forward (concat "\n" (int-to-string (car cached)) "\t")
573                             nil t)
574             (setq beg (progn (beginning-of-line) (point))
575                   end (progn (end-of-line) (point)))
576           (setq beg nil)))
577       (when beg
578         (insert-buffer-substring cache-buf beg end)
579         (insert "\n"))
580       (setq cached (cdr cached)))
581     (kill-buffer cache-buf)))
582
583 (defun gnus-cache-braid-heads (group cached)
584   (let ((cache-buf (gnus-get-buffer-create " *gnus-cache*")))
585     (save-excursion
586       (set-buffer cache-buf)
587       (erase-buffer))
588     (set-buffer nntp-server-buffer)
589     (goto-char (point-min))
590     (while cached
591       (while (and (not (eobp))
592                   (looking-at "2.. +\\([0-9]+\\) ")
593                   (< (progn (goto-char (match-beginning 1))
594                             (read (current-buffer)))
595                      (car cached)))
596         (search-forward "\n.\n" nil 'move))
597       (beginning-of-line)
598       (save-excursion
599         (set-buffer cache-buf)
600         (erase-buffer)
601         (let ((nnheader-file-coding-system gnus-cache-coding-system))
602           (nnheader-insert-file-contents
603            (gnus-cache-file-name group (car cached))))
604         (goto-char (point-min))
605         (insert "220 ")
606         (princ (car cached) (current-buffer))
607         (insert " Article retrieved.\n")
608         (search-forward "\n\n" nil 'move)
609         (delete-region (point) (point-max))
610         (forward-char -1)
611         (insert "."))
612       (insert-buffer-substring cache-buf)
613       (setq cached (cdr cached)))
614     (kill-buffer cache-buf)))
615
616 (defun gnus-cache-braid-headers (headers cached-headers)
617   (if cached-headers
618       (if headers
619           (let (cached-header hrest nhrest)
620             (nconc (catch 'tag
621                      (while cached-headers
622                        (setq cached-header (car cached-headers))
623                        (if (< (mail-header-number cached-header)
624                               (mail-header-number (car headers)))
625                            (throw 'tag (nreverse cached-headers))
626                          (setq hrest headers
627                                nhrest (cdr hrest))
628                          (while (and nhrest
629                                      (> (mail-header-number cached-header)
630                                         (mail-header-number (car nhrest))))
631                            (setq hrest nhrest
632                                  nhrest (cdr nhrest))
633                            )
634                          ;;(if nhrest
635                          (setcdr hrest (cons cached-header nhrest))
636                          ;; (setq headers
637                          ;;         (nconc headers (list cached-header)))
638                          ;; (throw 'tag nil)
639                          ;;)
640                          )
641                        (setq cached-headers (cdr cached-headers))))
642                    headers))
643         (nreverse cached-headers))
644     headers))
645
646 ;;;###autoload
647 (defun gnus-jog-cache ()
648   "Go through all groups and put the articles into the cache.
649
650 Usage:
651 $ emacs -batch -l ~/.emacs -l gnus -f gnus-jog-cache"
652   (interactive)
653   (let ((gnus-mark-article-hook nil)
654         (gnus-expert-user t)
655         (nnmail-spool-file nil)
656         (mail-sources nil)
657         (gnus-use-dribble-file nil)
658         (gnus-novice-user nil)
659         (gnus-large-newsgroup nil))
660     ;; Start Gnus.
661     (gnus)
662     ;; Go through all groups...
663     (gnus-group-mark-buffer)
664     (gnus-group-iterate nil
665       (lambda (group)
666         (let (gnus-auto-select-next)
667           (gnus-summary-read-group group nil t)
668           ;; ... and enter the articles into the cache.
669           (when (eq major-mode 'gnus-summary-mode)
670             (gnus-uu-mark-buffer)
671             (gnus-cache-enter-article)
672             (kill-buffer (current-buffer))))))))
673
674 (defun gnus-cache-read-active (&optional force)
675   "Read the cache active file."
676   (gnus-make-directory gnus-cache-directory)
677   (if (or (not (file-exists-p gnus-cache-active-file))
678           (zerop (nth 7 (file-attributes gnus-cache-active-file)))
679           force)
680       ;; There is no active file, so we generate one.
681       (gnus-cache-generate-active)
682     ;; We simply read the active file.
683     (save-excursion
684       (gnus-set-work-buffer)
685       (nnheader-insert-file-contents gnus-cache-active-file)
686       (gnus-active-to-gnus-format
687        nil (setq gnus-cache-active-hashtb
688                  (gnus-make-hashtable
689                   (count-lines (point-min) (point-max)))))
690       (setq gnus-cache-active-altered nil))))
691
692 (defun gnus-cache-write-active (&optional force)
693   "Write the active hashtb to the active file."
694   (when (or force
695             (and gnus-cache-active-hashtb
696                  gnus-cache-active-altered))
697     (gnus-write-active-file gnus-cache-active-file gnus-cache-active-hashtb t)
698     ;; Mark the active hashtb as unaltered.
699     (setq gnus-cache-active-altered nil)))
700
701 (defun gnus-cache-update-active (group number &optional low)
702   "Update the upper bound of the active info of GROUP to NUMBER.
703 If LOW, update the lower bound instead."
704   (let ((active (gnus-gethash group gnus-cache-active-hashtb)))
705     (if (null active)
706         ;; We just create a new active entry for this group.
707         (gnus-sethash group (cons number number) gnus-cache-active-hashtb)
708       ;; Update the lower or upper bound.
709       (if low
710           (setcar active number)
711         (setcdr active number)))
712     ;; Mark the active hashtb as altered.
713     (setq gnus-cache-active-altered t)))
714
715 ;;;###autoload
716 (defun gnus-cache-generate-active (&optional directory)
717   "Generate the cache active file."
718   (interactive)
719   (let* ((top (null directory))
720          (directory (expand-file-name (or directory gnus-cache-directory)))
721          (files (directory-files directory 'full))
722          (group
723           (if top
724               ""
725             (string-match
726              (concat "^" (regexp-quote
727                           (file-name-as-directory
728                            (expand-file-name gnus-cache-directory))))
729              (directory-file-name directory))
730             (nnheader-replace-chars-in-string
731              (substring (directory-file-name directory) (match-end 0))
732              ?/ ?.)))
733          nums alphs)
734     (when top
735       (gnus-message 5 "Generating the cache active file...")
736       (setq gnus-cache-active-hashtb (gnus-make-hashtable 123)))
737     (when (string-match "^\\(nn[^_]+\\)_" group)
738       (setq group (replace-match "\\1:" t t group)))
739     ;; Separate articles from all other files and directories.
740     (while files
741       (if (string-match "^[0-9]+$" (file-name-nondirectory (car files)))
742           (push (string-to-int (file-name-nondirectory (pop files))) nums)
743         (push (pop files) alphs)))
744     ;; If we have nums, then this is probably a valid group.
745     (when (setq nums (sort nums '<))
746       (gnus-sethash group (cons (car nums) (gnus-last-element nums))
747                     gnus-cache-active-hashtb))
748     ;; Go through all the other files.
749     (while alphs
750       (when (and (file-directory-p (car alphs))
751                  (not (string-match "^\\."
752                                     (file-name-nondirectory (car alphs)))))
753         ;; We descend directories.
754         (gnus-cache-generate-active (car alphs)))
755       (setq alphs (cdr alphs)))
756     ;; Write the new active file.
757     (when top
758       (gnus-cache-write-active t)
759       (gnus-message 5 "Generating the cache active file...done"))))
760
761 ;;;###autoload
762 (defun gnus-cache-generate-nov-databases (dir)
763   "Generate NOV files recursively starting in DIR."
764   (interactive (list gnus-cache-directory))
765   (gnus-cache-close)
766   (let ((nnml-generate-active-function 'identity))
767     (nnml-generate-nov-databases-1 dir))
768   (gnus-cache-open))
769
770 (defun gnus-cache-move-cache (dir)
771   "Move the cache tree to somewhere else."
772   (interactive "FMove the cache tree to: ")
773   (rename-file gnus-cache-directory dir))
774
775 (provide 'gnus-cache)
776
777 ;;; gnus-cache.el ends here