Sync up with chaos-1_12.
[elisp/gnus.git-] / lisp / gnus-cache.el
1 ;;; gnus-cache.el --- cache interface for Chaos
2 ;; Copyright (C) 1995,96,97,98,99 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;;         MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (eval-when-compile (require 'cl))
30
31 (require 'gnus)
32 (require 'gnus-int)
33 (require 'gnus-range)
34 (require 'gnus-start)
35 (eval-when-compile
36   (require 'gnus-sum))
37
38 (defcustom gnus-cache-active-file
39   (concat (file-name-as-directory gnus-cache-directory) "active")
40   "*The cache active file."
41   :group 'gnus-cache
42   :type 'file)
43
44 (defcustom gnus-cache-enter-articles '(ticked dormant)
45   "Classes of articles to enter into the cache."
46   :group 'gnus-cache
47   :type '(set (const ticked) (const dormant) (const unread) (const read)))
48
49 (defcustom gnus-cache-remove-articles '(read)
50   "Classes of articles to remove from the cache."
51   :group 'gnus-cache
52   :type '(set (const ticked) (const dormant) (const unread) (const read)))
53
54 (defcustom gnus-cacheable-groups nil
55   "*Groups that match this regexp will be cached.
56
57 If you only want to cache your nntp groups, you could set this
58 variable to \"^nntp\".
59
60 If a group matches both gnus-cacheable-groups and gnus-uncacheable-groups
61 it's not cached."
62   :group 'gnus-cache
63   :type '(choice (const :tag "off" nil)
64                 regexp))
65
66 (defcustom gnus-uncacheable-groups nil
67   "*Groups that match this regexp will not be cached.
68
69 If you want to avoid caching your nnml groups, you could set this
70 variable to \"^nnml\".
71
72 If a group matches both gnus-cacheable-groups and gnus-uncacheable-groups
73 it's not cached."
74   :group 'gnus-cache
75   :type '(choice (const :tag "off" nil)
76                  regexp))
77
78 (defvar gnus-cache-overview-coding-system 'raw-text
79   "Coding system used on Gnus cache files.")
80
81 \f
82
83 ;;; Internal variables.
84
85 (defvar gnus-cache-removable-articles nil)
86 (defvar gnus-cache-buffer nil)
87 (defvar gnus-cache-active-hashtb nil)
88 (defvar gnus-cache-active-altered nil)
89
90 (eval-and-compile
91   (autoload 'nnml-generate-nov-databases-1 "nnml")
92   (autoload 'nnvirtual-find-group-art "nnvirtual"))
93
94 \f
95
96 ;;; Functions called from Gnus.
97
98 (defun gnus-cache-open ()
99   "Initialize the cache."
100   (when (or (file-exists-p gnus-cache-directory)
101             (and gnus-use-cache
102                  (not (eq gnus-use-cache 'passive))))
103     (gnus-cache-read-active)))
104
105 ;; Complexities of byte-compiling make this kludge necessary.  Eeek.
106 (ignore-errors
107   (gnus-add-shutdown 'gnus-cache-close 'gnus))
108
109 (defun gnus-cache-close ()
110   "Shut down the cache."
111   (gnus-cache-write-active)
112   (gnus-cache-save-buffers)
113   (setq gnus-cache-active-hashtb nil))
114
115 (defun gnus-cache-save-buffers ()
116   ;; save the overview buffer if it exists and has been modified
117   ;; delete empty cache subdirectories
118   (when gnus-cache-buffer
119     (let ((buffer (cdr gnus-cache-buffer))
120           (overview-file (gnus-cache-file-name
121                           (car gnus-cache-buffer) ".overview")))
122       ;; write the overview only if it was modified
123       (when (buffer-modified-p buffer)
124         (save-excursion
125           (set-buffer buffer)
126           (if (> (buffer-size) 0)
127               ;; Non-empty overview, write it to a file.
128               (let ((coding-system-for-write
129                      gnus-cache-overview-coding-system))
130                 (gnus-write-buffer overview-file))
131             ;; Empty overview file, remove it
132             (when (file-exists-p overview-file)
133               (delete-file overview-file))
134             ;; If possible, remove group's cache subdirectory.
135             (condition-case nil
136                 ;; FIXME: we can detect the error type and warn the user
137                 ;; of any inconsistencies (articles w/o nov entries?).
138                 ;; for now, just be conservative...delete only if safe -- sj
139                 (delete-directory (file-name-directory overview-file))
140               (error nil)))))
141       ;; Kill the buffer -- it's either unmodified or saved.
142       (gnus-kill-buffer buffer)
143       (setq gnus-cache-buffer nil))))
144
145 (defun gnus-cache-possibly-enter-article
146   (group article headers ticked dormant unread &optional force)
147   (when (and (or force (not (eq gnus-use-cache 'passive)))
148              (numberp article)
149              (> article 0)
150              (vectorp headers))         ; This might be a dummy article.
151     ;; If this is a virtual group, we find the real group.
152     (when (gnus-virtual-group-p group)
153       (let ((result (nnvirtual-find-group-art
154                      (gnus-group-real-name group) article)))
155         (setq group (car result)
156               headers (copy-sequence headers))
157         (mail-header-set-number headers (cdr result))))
158     (let ((number (mail-header-number headers))
159           file)
160       (when (and number
161                  (> number 0)           ; Reffed article.
162                  (or force
163                      (and (or (not gnus-cacheable-groups)
164                               (string-match gnus-cacheable-groups group))
165                           (or (not gnus-uncacheable-groups)
166                               (not (string-match
167                                     gnus-uncacheable-groups group)))
168                           (gnus-cache-member-of-class
169                            gnus-cache-enter-articles ticked dormant unread)))
170                  (not (file-exists-p (setq file (gnus-cache-file-name
171                                                  group number)))))
172         ;; Possibly create the cache directory.
173         (gnus-make-directory (file-name-directory file))
174         ;; Save the article in the cache.
175         (if (file-exists-p file)
176             t                           ; The article already is saved.
177           (save-excursion
178             (set-buffer nntp-server-buffer)
179             (let ((gnus-use-cache nil))
180               (gnus-request-article-this-buffer number group))
181             (when (> (buffer-size) 0)
182               (gnus-write-buffer file)
183               (gnus-cache-change-buffer group)
184               (set-buffer (cdr gnus-cache-buffer))
185               (goto-char (point-max))
186               (forward-line -1)
187               (while (condition-case ()
188                          (when (not (bobp))
189                            (> (read (current-buffer)) number))
190                        (error
191                         ;; The line was malformed, so we just remove it!!
192                         (gnus-delete-line)
193                         t))
194                 (forward-line -1))
195               (if (bobp)
196                   (if (not (eobp))
197                       (progn
198                         (beginning-of-line)
199                         (when (< (read (current-buffer)) number)
200                           (forward-line 1)))
201                     (beginning-of-line))
202                 (forward-line 1))
203               (beginning-of-line)
204               ;; [number subject from date id references chars lines xref]
205               (insert (format "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t\n"
206                               (mail-header-number headers)
207                               (mime-fetch-field 'Subject headers)
208                               (mime-fetch-field 'From headers)
209                               (mail-header-date headers)
210                               (mail-header-id headers)
211                               (or (mail-header-references headers) "")
212                               (or (mail-header-chars headers) "")
213                               (or (mail-header-lines headers) "")
214                               (or (mail-header-xref headers) "")))
215               ;; Update the active info.
216               (set-buffer gnus-summary-buffer)
217               (gnus-cache-update-active group number)
218               (push article gnus-newsgroup-cached)
219               (gnus-summary-update-secondary-mark article))
220             t))))))
221
222 (defun gnus-cache-enter-remove-article (article)
223   "Mark ARTICLE for later possible removal."
224   (when article
225     (push article gnus-cache-removable-articles)))
226
227 (defun gnus-cache-possibly-remove-articles ()
228   "Possibly remove some of the removable articles."
229   (if (not (gnus-virtual-group-p gnus-newsgroup-name))
230       (gnus-cache-possibly-remove-articles-1)
231     (let ((arts gnus-cache-removable-articles)
232           ga)
233       (while arts
234         (when (setq ga (nnvirtual-find-group-art
235                         (gnus-group-real-name gnus-newsgroup-name) (pop arts)))
236           (let ((gnus-cache-removable-articles (list (cdr ga)))
237                 (gnus-newsgroup-name (car ga)))
238             (gnus-cache-possibly-remove-articles-1)))))
239     (setq gnus-cache-removable-articles nil)))
240
241 (defun gnus-cache-possibly-remove-articles-1 ()
242   "Possibly remove some of the removable articles."
243   (unless (eq gnus-use-cache 'passive)
244     (let ((articles gnus-cache-removable-articles)
245           (cache-articles gnus-newsgroup-cached)
246           article)
247       (gnus-cache-change-buffer gnus-newsgroup-name)
248       (while articles
249         (when (memq (setq article (pop articles)) cache-articles)
250           ;; The article was in the cache, so we see whether we are
251           ;; supposed to remove it from the cache.
252           (gnus-cache-possibly-remove-article
253            article (memq article gnus-newsgroup-marked)
254            (memq article gnus-newsgroup-dormant)
255            (or (memq article gnus-newsgroup-unreads)
256                (memq article gnus-newsgroup-unselected))))))
257     ;; The overview file might have been modified, save it
258     ;; safe because we're only called at group exit anyway.
259     (gnus-cache-save-buffers)))
260
261 (defun gnus-cache-request-article (article group)
262   "Retrieve ARTICLE in GROUP from the cache."
263   (let ((file (gnus-cache-file-name group article))
264         (buffer-read-only nil))
265     (when (file-exists-p file)
266       (erase-buffer)
267       (gnus-kill-all-overlays)
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             (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       (buffer-disable-undo (current-buffer))
459       ;; Insert the contents of this group's cache overview.
460       (erase-buffer)
461       (let ((file (gnus-cache-file-name group ".overview")))
462         (when (file-exists-p file)
463           (nnheader-insert-file-contents file)))
464       ;; We have a fresh (empty/just loaded) buffer,
465       ;; mark it as unmodified to save a redundant write later.
466       (set-buffer-modified-p nil))))
467
468 ;; Return whether an article is a member of a class.
469 (defun gnus-cache-member-of-class (class ticked dormant unread)
470   (or (and ticked (memq 'ticked class))
471       (and dormant (memq 'dormant class))
472       (and unread (memq 'unread class))
473       (and (not unread) (not ticked) (not dormant) (memq 'read class))))
474
475 (defun gnus-cache-file-name (group article)
476   (concat (file-name-as-directory gnus-cache-directory)
477           (file-name-as-directory
478            (nnheader-translate-file-chars
479             (if (gnus-use-long-file-name 'not-cache)
480                 group
481               (let ((group (nnheader-replace-chars-in-string group ?/ ?_)))
482                 ;; Translate the first colon into a slash.
483                 (when (string-match ":" group)
484                   (aset group (match-beginning 0) ?/))
485                 (nnheader-replace-chars-in-string group ?. ?/)))
486             t))
487           (if (stringp article) article (int-to-string article))))
488
489 (defun gnus-cache-update-article (group article)
490   "If ARTICLE is in the cache, remove it and re-enter it."
491   (gnus-cache-change-buffer group)
492   (when (gnus-cache-possibly-remove-article article nil nil nil t)    
493     (let ((gnus-use-cache nil))
494       (gnus-cache-possibly-enter-article
495        gnus-newsgroup-name article (gnus-summary-article-header article)
496        nil nil nil t))))
497
498 (defun gnus-cache-possibly-remove-article (article ticked dormant unread
499                                                    &optional force)
500   "Possibly remove ARTICLE from the cache."
501   (let ((group gnus-newsgroup-name)
502         (number article)
503         file)
504     ;; If this is a virtual group, we find the real group.
505     (when (gnus-virtual-group-p group)
506       (let ((result (nnvirtual-find-group-art
507                      (gnus-group-real-name group) article)))
508         (setq group (car result)
509               number (cdr result))))
510     (setq file (gnus-cache-file-name group number))
511     (when (and (file-exists-p file)
512                (or force
513                    (gnus-cache-member-of-class
514                     gnus-cache-remove-articles ticked dormant unread)))
515       (save-excursion
516         (delete-file file)
517         (set-buffer (cdr gnus-cache-buffer))
518         (goto-char (point-min))
519         (when (or (looking-at (concat (int-to-string number) "\t"))
520                   (search-forward (concat "\n" (int-to-string number) "\t")
521                                   (point-max) t))
522           (delete-region (progn (beginning-of-line) (point))
523                          (progn (forward-line 1) (point)))))
524       (setq gnus-newsgroup-cached
525             (delq article gnus-newsgroup-cached))
526       (gnus-summary-update-secondary-mark article)
527       t)))
528
529 (defun gnus-cache-articles-in-group (group)
530   "Return a sorted list of cached articles in GROUP."
531   (let ((dir (file-name-directory (gnus-cache-file-name group 1)))
532         articles)
533     (when (file-exists-p dir)
534       (setq articles
535             (sort (mapcar (lambda (name) (string-to-int name))
536                           (directory-files dir nil "^[0-9]+$" t))
537                   '<))
538       ;; Update the cache active file, just to synch more.
539       (when articles
540         (gnus-cache-update-active group (car articles) t)
541         (gnus-cache-update-active group (car (last articles))))
542       articles)))
543
544 (defun gnus-cache-braid-nov (group cached &optional file)
545   (let ((cache-buf (gnus-get-buffer-create " *gnus-cache*"))
546         beg end)
547     (gnus-cache-save-buffers)
548     (save-excursion
549       (set-buffer cache-buf)
550       (buffer-disable-undo (current-buffer))
551       (erase-buffer)
552       (nnheader-insert-file-contents (or file (gnus-cache-file-name group ".overview")))
553       (goto-char (point-min))
554       (insert "\n")
555       (goto-char (point-min)))
556     (set-buffer nntp-server-buffer)
557     (goto-char (point-min))
558     (while cached
559       (while (and (not (eobp))
560                   (< (read (current-buffer)) (car cached)))
561         (forward-line 1))
562       (beginning-of-line)
563       (save-excursion
564         (set-buffer cache-buf)
565         (if (search-forward (concat "\n" (int-to-string (car cached)) "\t")
566                             nil t)
567             (setq beg (progn (beginning-of-line) (point))
568                   end (progn (end-of-line) (point)))
569           (setq beg nil)))
570       (when beg
571         (insert-buffer-substring cache-buf beg end)
572         (insert "\n"))
573       (setq cached (cdr cached)))
574     (kill-buffer cache-buf)))
575
576 (defun gnus-cache-braid-heads (group cached)
577   (let ((cache-buf (gnus-get-buffer-create " *gnus-cache*")))
578     (save-excursion
579       (set-buffer cache-buf)
580       (buffer-disable-undo (current-buffer))
581       (erase-buffer))
582     (set-buffer nntp-server-buffer)
583     (goto-char (point-min))
584     (while cached
585       (while (and (not (eobp))
586                   (looking-at "2.. +\\([0-9]+\\) ")
587                   (< (progn (goto-char (match-beginning 1))
588                             (read (current-buffer)))
589                      (car cached)))
590         (search-forward "\n.\n" nil 'move))
591       (beginning-of-line)
592       (save-excursion
593         (set-buffer cache-buf)
594         (erase-buffer)
595         (nnheader-insert-file-contents (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           (not (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     (nnheader-temp-write gnus-cache-active-file
689       (mapatoms
690        (lambda (sym)
691          (when (and sym (boundp sym))
692            (insert (format "%s %d %d y\n"
693                            (symbol-name sym) (cdr (symbol-value sym))
694                            (car (symbol-value sym))))))
695        gnus-cache-active-hashtb))
696     ;; Mark the active hashtb as unaltered.
697     (setq gnus-cache-active-altered nil)))
698
699 (defun gnus-cache-update-active (group number &optional low)
700   "Update the upper bound of the active info of GROUP to NUMBER.
701 If LOW, update the lower bound instead."
702   (let ((active (gnus-gethash group gnus-cache-active-hashtb)))
703     (if (null active)
704         ;; We just create a new active entry for this group.
705         (gnus-sethash group (cons number number) gnus-cache-active-hashtb)
706       ;; Update the lower or upper bound.
707       (if low
708           (setcar active number)
709         (setcdr active number)))
710     ;; Mark the active hashtb as altered.
711     (setq gnus-cache-active-altered t)))
712
713 ;;;###autoload
714 (defun gnus-cache-generate-active (&optional directory)
715   "Generate the cache active file."
716   (interactive)
717   (let* ((top (null directory))
718          (directory (expand-file-name (or directory gnus-cache-directory)))
719          (files (directory-files directory 'full))
720          (group
721           (if top
722               ""
723             (string-match
724              (concat "^" (regexp-quote
725                           (file-name-as-directory
726                            (expand-file-name gnus-cache-directory))))
727              (directory-file-name directory))
728             (nnheader-replace-chars-in-string
729              (substring (directory-file-name directory) (match-end 0))
730              ?/ ?.)))
731          nums alphs)
732     (when top
733       (gnus-message 5 "Generating the cache active file...")
734       (setq gnus-cache-active-hashtb (gnus-make-hashtable 123)))
735     (when (string-match "^\\(nn[^_]+\\)_" group)
736       (setq group (replace-match "\\1:" t t group)))
737     ;; Separate articles from all other files and directories.
738     (while files
739       (if (string-match "^[0-9]+$" (file-name-nondirectory (car files)))
740           (push (string-to-int (file-name-nondirectory (pop files))) nums)
741         (push (pop files) alphs)))
742     ;; If we have nums, then this is probably a valid group.
743     (when (setq nums (sort nums '<))
744       (gnus-sethash group (cons (car nums) (gnus-last-element nums))
745                     gnus-cache-active-hashtb))
746     ;; Go through all the other files.
747     (while alphs
748       (when (and (file-directory-p (car alphs))
749                  (not (string-match "^\\."
750                                     (file-name-nondirectory (car alphs)))))
751         ;; We descend directories.
752         (gnus-cache-generate-active (car alphs)))
753       (setq alphs (cdr alphs)))
754     ;; Write the new active file.
755     (when top
756       (gnus-cache-write-active t)
757       (gnus-message 5 "Generating the cache active file...done"))))
758
759 ;;;###autoload
760 (defun gnus-cache-generate-nov-databases (dir)
761   "Generate NOV files recursively starting in DIR."
762   (interactive (list gnus-cache-directory))
763   (gnus-cache-close)
764   (let ((nnml-generate-active-function 'identity))
765     (nnml-generate-nov-databases-1 dir)))
766
767 (defun gnus-cache-move-cache (dir)
768   "Move the cache tree to somewhere else."
769   (interactive "FMove the cache tree to: ")
770   (rename-file gnus-cache-directory dir))
771
772 (provide 'gnus-cache)
773
774 ;;; gnus-cache.el ends here