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