(gnus-dribble-read-file): Quote file-precious-flag.
[gnus] / lisp / gnus-cache.el
1 ;;; gnus-cache.el --- cache interface for Gnus
2
3 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4 ;;   2004, 2005 Free Software Foundation, Inc.
5
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
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., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
25
26 ;;; Commentary:
27
28 ;;; Code:
29
30 (eval-when-compile (require 'cl))
31
32 (require 'gnus)
33 (eval-when-compile
34   (unless (fboundp 'gnus-agent-load-alist)
35       (defun gnus-agent-load-alist (group)))
36   (require 'gnus-sum))
37
38 (defcustom gnus-cache-active-file
39   (expand-file-name "active" gnus-cache-directory)
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 (defvar gnus-cache-coding-system 'raw-text
82   "Coding system used on Gnus cache files.")
83
84 \f
85
86 ;;; Internal variables.
87
88 (defvar gnus-cache-removable-articles nil)
89 (defvar gnus-cache-buffer nil)
90 (defvar gnus-cache-active-hashtb nil)
91 (defvar gnus-cache-active-altered nil)
92 (defvar gnus-cache-total-fetched-hashtb nil)
93
94 (eval-and-compile
95   (autoload 'nnml-generate-nov-databases-1 "nnml")
96   (autoload 'nnvirtual-find-group-art "nnvirtual"))
97
98 \f
99
100 ;;; Functions called from Gnus.
101
102 (defun gnus-cache-open ()
103   "Initialize the cache."
104   (when (or (file-exists-p gnus-cache-directory)
105             (and gnus-use-cache
106                  (not (eq gnus-use-cache 'passive))))
107     (gnus-cache-read-active)))
108
109 ;; Complexities of byte-compiling make this kludge necessary.  Eeek.
110 (ignore-errors
111   (gnus-add-shutdown 'gnus-cache-close 'gnus))
112
113 (defun gnus-cache-close ()
114   "Shut down the cache."
115   (gnus-cache-write-active)
116   (gnus-cache-save-buffers)
117   (setq gnus-cache-active-hashtb nil))
118
119 (defun gnus-cache-save-buffers ()
120   ;; save the overview buffer if it exists and has been modified
121   ;; delete empty cache subdirectories
122   (when gnus-cache-buffer
123     (let ((buffer (cdr gnus-cache-buffer))
124           (overview-file (gnus-cache-file-name
125                           (car gnus-cache-buffer) ".overview")))
126       ;; write the overview only if it was modified
127       (when (and (buffer-live-p buffer) (buffer-modified-p buffer))
128         (with-current-buffer buffer
129           (if (> (buffer-size) 0)
130               ;; Non-empty overview, write it to a file.
131               (let ((coding-system-for-write
132                      gnus-cache-overview-coding-system))
133                 (gnus-write-buffer overview-file))
134             ;; Empty overview file, remove it
135             (when (file-exists-p overview-file)
136               (delete-file overview-file))
137             ;; If possible, remove group's cache subdirectory.
138             (condition-case nil
139                 ;; FIXME: we can detect the error type and warn the user
140                 ;; of any inconsistencies (articles w/o nov entries?).
141                 ;; for now, just be conservative...delete only if safe -- sj
142                 (delete-directory (file-name-directory overview-file))
143               (error)))
144
145           (gnus-cache-update-overview-total-fetched-for
146            (car gnus-cache-buffer) overview-file)))
147       ;; Kill the buffer -- it's either unmodified or saved.
148       (gnus-kill-buffer buffer)
149       (setq gnus-cache-buffer nil))))
150
151 (defun gnus-cache-possibly-enter-article
152   (group article ticked dormant unread &optional force)
153   (when (and (or force (not (eq gnus-use-cache 'passive)))
154              (numberp article)
155              (> article 0))             ; This might be a dummy article.
156     (let ((number article)
157           file headers lines-chars)
158       ;; If this is a virtual group, we find the real group.
159       (when (gnus-virtual-group-p group)
160         (let ((result (nnvirtual-find-group-art
161                        (gnus-group-real-name group) article)))
162           (setq group (car result)
163                 number (cdr result))))
164       (when (and number
165                  (> number 0)           ; Reffed article.
166                  (or force
167                      (and (gnus-cache-fully-p 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             (require 'gnus-art)
180             (let ((gnus-use-cache nil)
181                   (gnus-article-decode-hook nil))
182               (gnus-request-article-this-buffer number group))
183             (when (> (buffer-size) 0)
184               (let ((coding-system-for-write gnus-cache-coding-system))
185                 (gnus-write-buffer file)
186                 (gnus-cache-update-file-total-fetched-for group file))
187               (setq lines-chars (nnheader-get-lines-and-char))
188               (nnheader-remove-body)
189               (setq headers (nnheader-parse-naked-head))
190               (mail-header-set-number headers number)
191               (mail-header-set-lines headers (car lines-chars))
192               (mail-header-set-chars headers (cadr lines-chars))
193               (gnus-cache-change-buffer group)
194               (set-buffer (cdr gnus-cache-buffer))
195               (goto-char (point-max))
196               (forward-line -1)
197               (while (condition-case ()
198                          (when (not (bobp))
199                            (> (read (current-buffer)) number))
200                        (error
201                         ;; The line was malformed, so we just remove it!!
202                         (gnus-delete-line)
203                         t))
204                 (forward-line -1))
205               (if (bobp)
206                   (if (not (eobp))
207                       (progn
208                         (beginning-of-line)
209                         (when (< (read (current-buffer)) number)
210                           (forward-line 1)))
211                     (beginning-of-line))
212                 (forward-line 1))
213               (beginning-of-line)
214               (nnheader-insert-nov headers)
215               ;; Update the active info.
216               (set-buffer gnus-summary-buffer)
217               (gnus-cache-possibly-update-active group (cons number number))
218               (setq gnus-newsgroup-cached
219                     (gnus-add-to-sorted-list gnus-newsgroup-cached article))
220               (gnus-summary-update-secondary-mark article))
221             t))))))
222
223 (defun gnus-cache-enter-remove-article (article)
224   "Mark ARTICLE for later possible removal."
225   (when article
226     (push article gnus-cache-removable-articles)))
227
228 (defun gnus-cache-possibly-remove-articles ()
229   "Possibly remove some of the removable articles."
230   (if (not (gnus-virtual-group-p gnus-newsgroup-name))
231       (gnus-cache-possibly-remove-articles-1)
232     (let ((arts gnus-cache-removable-articles)
233           ga)
234       (while arts
235         (when (setq ga (nnvirtual-find-group-art
236                         (gnus-group-real-name gnus-newsgroup-name) (pop arts)))
237           (let ((gnus-cache-removable-articles (list (cdr ga)))
238                 (gnus-newsgroup-name (car ga)))
239             (gnus-cache-possibly-remove-articles-1)))))
240     (setq gnus-cache-removable-articles nil)))
241
242 (defun gnus-cache-possibly-remove-articles-1 ()
243   "Possibly remove some of the removable articles."
244   (when (gnus-cache-fully-p gnus-newsgroup-name)
245     (let ((cache-articles gnus-newsgroup-cached))
246       (gnus-cache-change-buffer gnus-newsgroup-name)
247       (dolist (article gnus-cache-removable-articles)
248         (when (memq article cache-articles)
249           ;; The article was in the cache, so we see whether we are
250           ;; supposed to remove it from the cache.
251           (gnus-cache-possibly-remove-article
252            article (memq article gnus-newsgroup-marked)
253            (memq article gnus-newsgroup-dormant)
254            (or (memq article gnus-newsgroup-unreads)
255                (memq article gnus-newsgroup-unselected))))))
256     ;; The overview file might have been modified, save it
257     ;; safe because we're only called at group exit anyway.
258     (gnus-cache-save-buffers)))
259
260 (defun gnus-cache-request-article (article group)
261   "Retrieve ARTICLE in GROUP from the cache."
262   (let ((file (gnus-cache-file-name group article))
263         (buffer-read-only nil))
264     (when (file-exists-p file)
265       (erase-buffer)
266       (gnus-kill-all-overlays)
267       (let ((coding-system-for-read gnus-cache-coding-system))
268         (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-difference articles cached))
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 ((coding-system-for-read
313                    gnus-cache-overview-coding-system))
314               (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-enter-article (&optional n)
328   "Enter the next N articles into the cache.
329 If not given a prefix, use the process marked articles instead.
330 Returns the list of articles entered."
331   (interactive "P")
332   (let (out)
333     (dolist (article (gnus-summary-work-articles n))
334       (gnus-summary-remove-process-mark article)
335       (if (natnump article)
336           (when (gnus-cache-possibly-enter-article
337                  gnus-newsgroup-name article
338                  nil nil nil t)
339             (setq gnus-newsgroup-undownloaded (delq article gnus-newsgroup-undownloaded))
340             (push article out))
341         (gnus-message 2 "Can't cache article %d" article))
342       (gnus-summary-update-download-mark article)
343       (gnus-summary-update-secondary-mark article))
344     (gnus-summary-next-subject 1)
345     (gnus-summary-position-point)
346     (nreverse out)))
347
348 (defun gnus-cache-remove-article (&optional n)
349   "Remove the next N articles from the cache.
350 If not given a prefix, use the process marked articles instead.
351 Returns the list of articles removed."
352   (interactive "P")
353   (gnus-cache-change-buffer gnus-newsgroup-name)
354   (let (out)
355     (dolist (article (gnus-summary-work-articles n))
356       (gnus-summary-remove-process-mark article)
357       (when (gnus-cache-possibly-remove-article article nil nil nil t)
358         (when gnus-newsgroup-agentized
359           (let ((alist (gnus-agent-load-alist gnus-newsgroup-name)))
360             (unless (cdr (assoc article alist))
361               (setq gnus-newsgroup-undownloaded
362                     (gnus-add-to-sorted-list
363                      gnus-newsgroup-undownloaded article)))))
364         (push article out))
365       (gnus-summary-update-download-mark article)
366       (gnus-summary-update-secondary-mark article))
367     (gnus-summary-next-subject 1)
368     (gnus-summary-position-point)
369     (nreverse out)))
370
371 (defun gnus-cached-article-p (article)
372   "Say whether ARTICLE is cached in the current group."
373   (memq article gnus-newsgroup-cached))
374
375 (defun gnus-summary-insert-cached-articles ()
376   "Insert all the articles cached for this group into the current buffer."
377   (interactive)
378   (let ((gnus-verbose (max 6 gnus-verbose)))
379     (if (not gnus-newsgroup-cached)
380         (gnus-message 3 "No cached articles for this group")
381       (gnus-summary-goto-subjects gnus-newsgroup-cached))))
382
383 (defun gnus-summary-limit-include-cached ()
384   "Limit the summary buffer to articles that are cached."
385   (interactive)
386   (let ((gnus-verbose (max 6 gnus-verbose)))
387     (if gnus-newsgroup-cached
388         (progn
389           (gnus-summary-limit gnus-newsgroup-cached)
390           (gnus-summary-position-point))
391       (gnus-message 3 "No cached articles for this group"))))
392
393 ;;; Internal functions.
394
395 (defun gnus-cache-change-buffer (group)
396   (and gnus-cache-buffer
397        ;; See if the current group's overview cache has been loaded.
398        (or (string= group (car gnus-cache-buffer))
399            ;; Another overview cache is current, save it.
400            (gnus-cache-save-buffers)))
401   ;; if gnus-cache buffer is nil, create it
402   (unless gnus-cache-buffer
403     ;; Create cache buffer
404     (save-excursion
405       (setq gnus-cache-buffer
406             (cons group
407                   (set-buffer (gnus-get-buffer-create
408                                " *gnus-cache-overview*"))))
409       ;; Insert the contents of this group's cache overview.
410       (erase-buffer)
411       (let ((file (gnus-cache-file-name group ".overview")))
412         (when (file-exists-p file)
413           (nnheader-insert-file-contents file)))
414       ;; We have a fresh (empty/just loaded) buffer,
415       ;; mark it as unmodified to save a redundant write later.
416       (set-buffer-modified-p nil))))
417
418 ;; Return whether an article is a member of a class.
419 (defun gnus-cache-member-of-class (class ticked dormant unread)
420   (or (and ticked (memq 'ticked class))
421       (and dormant (memq 'dormant class))
422       (and unread (memq 'unread class))
423       (and (not unread) (not ticked) (not dormant) (memq 'read class))))
424
425 (defun gnus-cache-file-name (group article)
426   (setq group (gnus-group-decoded-name group))
427   (expand-file-name
428    (if (stringp article) article (int-to-string article))
429    (file-name-as-directory
430     (expand-file-name
431      (nnheader-translate-file-chars
432       (if (gnus-use-long-file-name 'not-cache)
433           group
434         (let ((group (nnheader-replace-duplicate-chars-in-string
435                       (nnheader-replace-chars-in-string group ?/ ?_)
436                       ?. ?_)))
437           ;; Translate the first colon into a slash.
438           (when (string-match ":" group)
439                   (setq group (concat (substring group 0 (match-beginning 0))
440                                       "/" (substring group (match-end 0)))))
441           (nnheader-replace-chars-in-string group ?. ?/)))
442       t)
443      gnus-cache-directory))))
444
445 (defun gnus-cache-update-article (group article)
446   "If ARTICLE is in the cache, remove it and re-enter it."
447   (gnus-cache-change-buffer group)
448   (when (gnus-cache-possibly-remove-article article nil nil nil t)
449     (let ((gnus-use-cache nil))
450       (gnus-cache-possibly-enter-article
451        gnus-newsgroup-name article
452        nil nil nil t))))
453
454 (defun gnus-cache-possibly-remove-article (article ticked dormant unread
455                                                    &optional force)
456   "Possibly remove ARTICLE from the cache."
457   (let ((group gnus-newsgroup-name)
458         (number article)
459         file)
460     ;; If this is a virtual group, we find the real group.
461     (when (gnus-virtual-group-p group)
462       (let ((result (nnvirtual-find-group-art
463                      (gnus-group-real-name group) article)))
464         (setq group (car result)
465               number (cdr result))))
466     (setq file (gnus-cache-file-name group number))
467     (when (and (file-exists-p file)
468                (or force
469                    (gnus-cache-member-of-class
470                     gnus-cache-remove-articles ticked dormant unread)))
471       (save-excursion
472         (gnus-cache-update-file-total-fetched-for group file t)
473         (delete-file file)
474
475         (set-buffer (cdr gnus-cache-buffer))
476         (goto-char (point-min))
477         (when (or (looking-at (concat (int-to-string number) "\t"))
478                   (search-forward (concat "\n" (int-to-string number) "\t")
479                                   (point-max) t))
480             (gnus-delete-line)))
481       (unless (setq gnus-newsgroup-cached
482                     (delq article gnus-newsgroup-cached))
483         (gnus-sethash gnus-newsgroup-name nil gnus-cache-active-hashtb)
484         (setq gnus-cache-active-altered t))
485       (gnus-summary-update-secondary-mark article)
486       t)))
487
488 (defun gnus-cache-articles-in-group (group)
489   "Return a sorted list of cached articles in GROUP."
490   (let ((dir (file-name-directory (gnus-cache-file-name group 1)))
491         articles)
492     (when (file-exists-p dir)
493       (setq articles
494             (sort (mapcar (lambda (name) (string-to-number name))
495                           (directory-files dir nil "^[0-9]+$" t))
496                   '<))
497       ;; Update the cache active file, just to synch more.
498       (if articles
499           (progn
500             (gnus-cache-update-active group (car articles) t)
501             (gnus-cache-update-active group (car (last articles))))
502         (when (gnus-gethash group gnus-cache-active-hashtb)
503           (gnus-sethash group nil gnus-cache-active-hashtb)
504           (setq gnus-cache-active-altered t)))
505       articles)))
506
507 (defun gnus-cache-braid-nov (group cached &optional file)
508   (let ((cache-buf (gnus-get-buffer-create " *gnus-cache*"))
509         beg end)
510     (gnus-cache-save-buffers)
511     (save-excursion
512       (set-buffer cache-buf)
513       (erase-buffer)
514       (let ((coding-system-for-read
515              gnus-cache-overview-coding-system))
516         (insert-file-contents
517          (or file (gnus-cache-file-name group ".overview"))))
518       (goto-char (point-min))
519       (insert "\n")
520       (goto-char (point-min)))
521     (set-buffer nntp-server-buffer)
522     (goto-char (point-min))
523     (while cached
524       (while (and (not (eobp))
525                   (< (read (current-buffer)) (car cached)))
526         (forward-line 1))
527       (beginning-of-line)
528       (set-buffer cache-buf)
529       (if (search-forward (concat "\n" (int-to-string (car cached)) "\t")
530                           nil t)
531           (setq beg (point-at-bol)
532                 end (progn (end-of-line) (point)))
533         (setq beg nil))
534       (set-buffer nntp-server-buffer)
535       (when beg
536         (insert-buffer-substring cache-buf beg end)
537         (insert "\n"))
538       (setq cached (cdr cached)))
539     (kill-buffer cache-buf)))
540
541 (defun gnus-cache-braid-heads (group cached)
542   (let ((cache-buf (gnus-get-buffer-create " *gnus-cache*")))
543     (with-current-buffer cache-buf
544       (erase-buffer))
545     (set-buffer nntp-server-buffer)
546     (goto-char (point-min))
547     (dolist (entry cached)
548       (while (and (not (eobp))
549                   (looking-at "2.. +\\([0-9]+\\) ")
550                   (< (progn (goto-char (match-beginning 1))
551                             (read (current-buffer)))
552                      entry))
553         (search-forward "\n.\n" nil 'move))
554       (beginning-of-line)
555       (set-buffer cache-buf)
556       (erase-buffer)
557       (let ((coding-system-for-read
558              gnus-cache-coding-system))
559         (insert-file-contents (gnus-cache-file-name group entry)))
560       (goto-char (point-min))
561       (insert "220 ")
562       (princ (car cached) (current-buffer))
563       (insert " Article retrieved.\n")
564       (search-forward "\n\n" nil 'move)
565       (delete-region (point) (point-max))
566       (forward-char -1)
567       (insert ".")
568       (set-buffer nntp-server-buffer)
569       (insert-buffer-substring cache-buf))
570     (kill-buffer cache-buf)))
571
572 ;;;###autoload
573 (defun gnus-jog-cache ()
574   "Go through all groups and put the articles into the cache.
575
576 Usage:
577 $ emacs -batch -l ~/.emacs -l gnus -f gnus-jog-cache"
578   (interactive)
579   (let ((gnus-mark-article-hook nil)
580         (gnus-expert-user t)
581         (nnmail-spool-file nil)
582         (mail-sources nil)
583         (gnus-use-dribble-file nil)
584         (gnus-novice-user nil)
585         (gnus-large-newsgroup nil))
586     ;; Start Gnus.
587     (gnus)
588     ;; Go through all groups...
589     (gnus-group-mark-buffer)
590     (gnus-group-iterate nil
591       (lambda (group)
592         (let (gnus-auto-select-next)
593           (gnus-summary-read-group group nil t)
594           ;; ... and enter the articles into the cache.
595           (when (eq major-mode 'gnus-summary-mode)
596             (gnus-uu-mark-buffer)
597             (gnus-cache-enter-article)
598             (kill-buffer (current-buffer))))))))
599
600 (defun gnus-cache-read-active (&optional force)
601   "Read the cache active file."
602   (gnus-make-directory gnus-cache-directory)
603   (if (or (not (file-exists-p gnus-cache-active-file))
604           (zerop (nth 7 (file-attributes gnus-cache-active-file)))
605           force)
606       ;; There is no active file, so we generate one.
607       (gnus-cache-generate-active)
608     ;; We simply read the active file.
609     (save-excursion
610       (gnus-set-work-buffer)
611       (nnheader-insert-file-contents gnus-cache-active-file)
612       (gnus-active-to-gnus-format
613        nil (setq gnus-cache-active-hashtb
614                  (gnus-make-hashtable
615                   (count-lines (point-min) (point-max)))))
616       (setq gnus-cache-active-altered nil))))
617
618 (defun gnus-cache-write-active (&optional force)
619   "Write the active hashtb to the active file."
620   (when (or force
621             (and gnus-cache-active-hashtb
622                  gnus-cache-active-altered))
623     (gnus-write-active-file gnus-cache-active-file gnus-cache-active-hashtb t)
624     ;; Mark the active hashtb as unaltered.
625     (setq gnus-cache-active-altered nil)))
626
627 (defun gnus-cache-possibly-update-active (group active)
628   "Update active info bounds of GROUP with ACTIVE if necessary.
629 The update is performed if ACTIVE contains a higher or lower bound
630 than the current."
631   (let ((lower t) (higher t))
632     (if gnus-cache-active-hashtb
633         (let ((cache-active (gnus-gethash group gnus-cache-active-hashtb)))
634           (when cache-active
635             (unless (< (car active) (car cache-active))
636               (setq lower nil))
637             (unless (> (cdr active) (cdr cache-active))
638               (setq higher nil))))
639       (gnus-cache-read-active))
640     (when lower
641       (gnus-cache-update-active group (car active) t))
642     (when higher
643       (gnus-cache-update-active group (cdr active)))))
644
645 (defun gnus-cache-update-active (group number &optional low)
646   "Update the upper bound of the active info of GROUP to NUMBER.
647 If LOW, update the lower bound instead."
648   (let ((active (gnus-gethash group gnus-cache-active-hashtb)))
649     (if (null active)
650         ;; We just create a new active entry for this group.
651         (gnus-sethash group (cons number number) gnus-cache-active-hashtb)
652       ;; Update the lower or upper bound.
653       (if low
654           (setcar active number)
655         (setcdr active number)))
656     ;; Mark the active hashtb as altered.
657     (setq gnus-cache-active-altered t)))
658
659 ;;;###autoload
660 (defun gnus-cache-generate-active (&optional directory)
661   "Generate the cache active file."
662   (interactive)
663   (let* ((top (null directory))
664          (directory (expand-file-name (or directory gnus-cache-directory)))
665          (files (directory-files directory 'full))
666          (group
667           (if top
668               ""
669             (string-match
670              (concat "^" (regexp-quote
671                           (file-name-as-directory
672                            (expand-file-name gnus-cache-directory))))
673              (directory-file-name directory))
674             (nnheader-replace-chars-in-string
675              (substring (directory-file-name directory) (match-end 0))
676              ?/ ?.)))
677          nums alphs)
678     (when top
679       (gnus-message 5 "Generating the cache active file...")
680       (setq gnus-cache-active-hashtb (gnus-make-hashtable 123)))
681     (when (string-match "^\\(nn[^_]+\\)_" group)
682       (setq group (replace-match "\\1:" t nil group)))
683     ;; Separate articles from all other files and directories.
684     (while files
685       (if (string-match "^[0-9]+$" (file-name-nondirectory (car files)))
686           (push (string-to-number (file-name-nondirectory (pop files))) nums)
687         (push (pop files) alphs)))
688     ;; If we have nums, then this is probably a valid group.
689     (when (setq nums (sort nums '<))
690       (gnus-sethash group (cons (car nums) (gnus-last-element nums))
691                     gnus-cache-active-hashtb))
692     ;; Go through all the other files.
693     (dolist (file alphs)
694       (when (and (file-directory-p file)
695                  (not (string-match "^\\."
696                                     (file-name-nondirectory file))))
697         ;; We descend directories.
698         (gnus-cache-generate-active file)))
699     ;; Write the new active file.
700     (when top
701       (gnus-cache-write-active t)
702       (gnus-message 5 "Generating the cache active file...done"))))
703
704 ;;;###autoload
705 (defun gnus-cache-generate-nov-databases (dir)
706   "Generate NOV files recursively starting in DIR."
707   (interactive (list gnus-cache-directory))
708   (gnus-cache-close)
709   (let ((nnml-generate-active-function 'identity))
710     (nnml-generate-nov-databases-1 dir))
711
712   (setq gnus-cache-total-fetched-hashtb nil)
713
714   (gnus-cache-open))
715
716 (defun gnus-cache-move-cache (dir)
717   "Move the cache tree to somewhere else."
718   (interactive "FMove the cache tree to: ")
719   (rename-file gnus-cache-directory dir))
720
721 (defun gnus-cache-fully-p (&optional group)
722   "Returns non-nil if the cache should be fully used.
723 If GROUP is non-nil, also cater to `gnus-cacheable-groups' and
724 `gnus-uncacheable-groups'."
725   (and gnus-use-cache
726        (not (eq gnus-use-cache 'passive))
727        (if (null group)
728            t
729          (and (or (not gnus-cacheable-groups)
730                   (string-match gnus-cacheable-groups group))
731               (or (not gnus-uncacheable-groups)
732                   (not (string-match gnus-uncacheable-groups group)))))))
733
734 ;;;###autoload
735 (defun gnus-cache-rename-group (old-group new-group)
736   "Rename OLD-GROUP as NEW-GROUP.  Always updates the cache, even when
737 disabled, as the old cache files would corrupt gnus when the cache was
738 next enabled. Depends upon the caller to determine whether group renaming is supported."
739   (let ((old-dir (gnus-cache-file-name old-group ""))
740         (new-dir (gnus-cache-file-name new-group "")))
741     (gnus-rename-file old-dir new-dir t))
742
743   (gnus-cache-rename-group-total-fetched-for old-group new-group)
744
745   (let ((no-save gnus-cache-active-hashtb))
746     (unless gnus-cache-active-hashtb
747       (gnus-cache-read-active))
748     (let* ((old-group-hash-value (gnus-gethash old-group gnus-cache-active-hashtb))
749            (new-group-hash-value (gnus-gethash new-group gnus-cache-active-hashtb))
750            (delta                (or old-group-hash-value new-group-hash-value)))
751       (gnus-sethash new-group old-group-hash-value gnus-cache-active-hashtb)
752       (gnus-sethash old-group nil gnus-cache-active-hashtb)
753
754       (if no-save
755           (setq gnus-cache-active-altered delta)
756         (gnus-cache-write-active delta)))))
757
758 ;;;###autoload
759 (defun gnus-cache-delete-group (group)
760   "Delete GROUP.  Always updates the cache, even when
761 disabled, as the old cache files would corrupt gnus when the cache was
762 next enabled. Depends upon the caller to determine whether group deletion is supported."
763   (let ((dir (gnus-cache-file-name group "")))
764     (gnus-delete-directory dir))
765
766   (gnus-cache-delete-group-total-fetched-for group)
767
768   (let ((no-save gnus-cache-active-hashtb))
769     (unless gnus-cache-active-hashtb
770       (gnus-cache-read-active))
771     (let* ((group-hash-value (gnus-gethash group gnus-cache-active-hashtb)))
772       (gnus-sethash group nil gnus-cache-active-hashtb)
773
774       (if no-save
775           (setq gnus-cache-active-altered group-hash-value)
776         (gnus-cache-write-active group-hash-value)))))
777
778 (defvar gnus-cache-inhibit-update-total-fetched-for nil)
779 (defvar gnus-cache-need-update-total-fetched-for nil)
780
781 (defmacro gnus-cache-with-refreshed-group (group &rest body)
782   `(prog1 (let ((gnus-cache-inhibit-update-total-fetched-for t))
783             ,@body)
784      (when (and gnus-cache-need-update-total-fetched-for
785                 (not gnus-cache-inhibit-update-total-fetched-for))
786         (save-excursion
787           (set-buffer gnus-group-buffer)
788           (setq gnus-cache-need-update-total-fetched-for nil)
789           (gnus-group-update-group ,group t)))))
790
791 (defun gnus-cache-update-file-total-fetched-for (group file &optional subtract)
792   (when gnus-cache-total-fetched-hashtb
793     (gnus-cache-with-refreshed-group
794      group
795      (let* ((entry (or (gnus-gethash group gnus-cache-total-fetched-hashtb)
796                        (gnus-sethash group (make-vector 2 0)
797                                      gnus-cache-total-fetched-hashtb)))
798             size)
799
800        (if file
801            (setq size (or (nth 7 (file-attributes file)) 0))
802          (let ((files (directory-files (gnus-cache-file-name group "") 
803                                        t nil t))
804                file attrs)
805            (setq size 0.0)
806            (while (setq file (pop files))
807              (setq attrs (file-attributes file))
808              (unless (nth 0 attrs)
809                (incf size (float (nth 7 attrs)))))))         
810
811        (setq gnus-cache-need-update-total-fetched-for t)
812
813        (incf (nth 1 entry) (if subtract (- size) size))))))
814
815 (defun gnus-cache-update-overview-total-fetched-for (group file)
816   (when gnus-cache-total-fetched-hashtb
817     (gnus-cache-with-refreshed-group
818      group
819      (let* ((entry (or (gnus-gethash group gnus-cache-total-fetched-hashtb)
820                        (gnus-sethash group (make-list 2 0) 
821                                      gnus-cache-total-fetched-hashtb)))
822             (size (or (nth 7 (file-attributes 
823                               (or file
824                                   (gnus-cache-file-name group ".overview"))))
825                       0)))
826        (setq gnus-cache-need-update-total-fetched-for t)
827        (setf (nth 0 entry) size)))))
828
829 (defun gnus-cache-rename-group-total-fetched-for (old-group new-group)
830   "Record of disk space used by OLD-GROUP now associated with NEW-GROUP."
831   (when gnus-cache-total-fetched-hashtb
832     (let ((entry (gnus-gethash old-group gnus-cache-total-fetched-hashtb)))
833       (gnus-sethash new-group entry gnus-cache-total-fetched-hashtb)
834       (gnus-sethash old-group nil gnus-cache-total-fetched-hashtb))))
835
836 (defun gnus-cache-delete-group-total-fetched-for (group)
837   "Delete record of disk space used by GROUP being deleted."
838   (when gnus-cache-total-fetched-hashtb
839       (gnus-sethash group nil gnus-cache-total-fetched-hashtb)))
840
841 (defun gnus-cache-total-fetched-for (group &optional no-inhibit)
842   "Get total disk space used by the cache for the specified GROUP."
843   (unless gnus-cache-total-fetched-hashtb
844     (setq gnus-cache-total-fetched-hashtb (gnus-make-hashtable 1024)))
845
846   (let* ((entry (gnus-gethash group gnus-cache-total-fetched-hashtb)))
847     (if entry
848         (apply '+ entry)
849       (let ((gnus-cache-inhibit-update-total-fetched-for (not no-inhibit)))
850         (+ 
851          (gnus-cache-update-overview-total-fetched-for group nil)
852          (gnus-cache-update-file-total-fetched-for     group nil))))))
853
854 (provide 'gnus-cache)
855
856 ;;; arch-tag: 05a79442-8c58-4e65-bd0a-3cbb1b89a33a
857 ;;; gnus-cache.el ends here