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