Regenerate ldefs-boot.el
[bpt/emacs.git] / lisp / gnus / gnus-cache.el
CommitLineData
eec82323 1;;; gnus-cache.el --- cache interface for Gnus
e84b4b86 2
ba318903 3;; Copyright (C) 1995-2014 Free Software Foundation, Inc.
eec82323 4
6748645f 5;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
eec82323
LMI
6;; Keywords: news
7
8;; This file is part of GNU Emacs.
9
5e809f55 10;; GNU Emacs is free software: you can redistribute it and/or modify
eec82323 11;; it under the terms of the GNU General Public License as published by
5e809f55
GM
12;; the Free Software Foundation, either version 3 of the License, or
13;; (at your option) any later version.
eec82323
LMI
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
5e809f55 21;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
eec82323
LMI
22
23;;; Commentary:
24
25;;; Code:
26
f0b7f5a8 27;; For Emacs <22.2 and XEmacs.
cef98b13
GM
28(eval-and-compile
29 (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
30
7df7482d
RS
31(eval-when-compile (require 'cl))
32
eec82323 33(require 'gnus)
cef98b13
GM
34(require 'gnus-sum)
35
eec82323 36(eval-when-compile
01c52d31 37 (unless (fboundp 'gnus-agent-load-alist)
cef98b13 38 (defun gnus-agent-load-alist (group))))
eec82323 39
eec82323 40(defcustom gnus-cache-active-file
6ab8077a 41 (expand-file-name "active" gnus-cache-directory)
eec82323
LMI
42 "*The cache active file."
43 :group 'gnus-cache
44 :type 'file)
45
46(defcustom gnus-cache-enter-articles '(ticked dormant)
47 "Classes of articles to enter into the cache."
48 :group 'gnus-cache
49 :type '(set (const ticked) (const dormant) (const unread) (const read)))
50
51(defcustom gnus-cache-remove-articles '(read)
52 "Classes of articles to remove from the cache."
53 :group 'gnus-cache
54 :type '(set (const ticked) (const dormant) (const unread) (const read)))
55
6748645f
LMI
56(defcustom gnus-cacheable-groups nil
57 "*Groups that match this regexp will be cached.
58
59If you only want to cache your nntp groups, you could set this
60variable to \"^nntp\".
61
62If a group matches both gnus-cacheable-groups and gnus-uncacheable-groups
63it's not cached."
64 :group 'gnus-cache
65 :type '(choice (const :tag "off" nil)
16409b0b 66 regexp))
6748645f 67
eec82323
LMI
68(defcustom gnus-uncacheable-groups nil
69 "*Groups that match this regexp will not be cached.
70
71If you want to avoid caching your nnml groups, you could set this
6748645f
LMI
72variable to \"^nnml\".
73
74If a group matches both gnus-cacheable-groups and gnus-uncacheable-groups
75it's not cached."
eec82323
LMI
76 :group 'gnus-cache
77 :type '(choice (const :tag "off" nil)
78 regexp))
79
6748645f
LMI
80(defvar gnus-cache-overview-coding-system 'raw-text
81 "Coding system used on Gnus cache files.")
82
16409b0b
GM
83(defvar gnus-cache-coding-system 'raw-text
84 "Coding system used on Gnus cache files.")
85
eec82323
LMI
86\f
87
88;;; Internal variables.
89
90(defvar gnus-cache-removable-articles nil)
91(defvar gnus-cache-buffer nil)
92(defvar gnus-cache-active-hashtb nil)
93(defvar gnus-cache-active-altered nil)
01c52d31 94(defvar gnus-cache-total-fetched-hashtb nil)
eec82323 95
cef98b13
GM
96(declare-function nnvirtual-find-group-art "nnvirtual" (group article))
97
8abf1b22
GM
98(autoload 'nnml-generate-nov-databases-directory "nnml")
99(autoload 'nnvirtual-find-group-art "nnvirtual")
eec82323
LMI
100
101\f
102
103;;; Functions called from Gnus.
104
105(defun gnus-cache-open ()
106 "Initialize the cache."
107 (when (or (file-exists-p gnus-cache-directory)
108 (and gnus-use-cache
109 (not (eq gnus-use-cache 'passive))))
110 (gnus-cache-read-active)))
111
112;; Complexities of byte-compiling make this kludge necessary. Eeek.
113(ignore-errors
114 (gnus-add-shutdown 'gnus-cache-close 'gnus))
115
116(defun gnus-cache-close ()
117 "Shut down the cache."
118 (gnus-cache-write-active)
119 (gnus-cache-save-buffers)
120 (setq gnus-cache-active-hashtb nil))
121
122(defun gnus-cache-save-buffers ()
123 ;; save the overview buffer if it exists and has been modified
124 ;; delete empty cache subdirectories
125 (when gnus-cache-buffer
126 (let ((buffer (cdr gnus-cache-buffer))
127 (overview-file (gnus-cache-file-name
128 (car gnus-cache-buffer) ".overview")))
129 ;; write the overview only if it was modified
f4dd4ae8
MB
130 (when (and (buffer-live-p buffer) (buffer-modified-p buffer))
131 (with-current-buffer buffer
eec82323
LMI
132 (if (> (buffer-size) 0)
133 ;; Non-empty overview, write it to a file.
6748645f
LMI
134 (let ((coding-system-for-write
135 gnus-cache-overview-coding-system))
136 (gnus-write-buffer overview-file))
01c52d31
MB
137 (let ((file-name-coding-system nnmail-pathname-coding-system))
138 ;; Empty overview file, remove it
139 (when (file-exists-p overview-file)
140 (delete-file overview-file))
141 ;; If possible, remove group's cache subdirectory.
142 (condition-case nil
143 ;; FIXME: we can detect the error type and warn the user
144 ;; of any inconsistencies (articles w/o nov entries?).
145 ;; for now, just be conservative...delete only if safe -- sj
146 (delete-directory (file-name-directory overview-file))
147 (error))))
148
149 (gnus-cache-update-overview-total-fetched-for
150 (car gnus-cache-buffer) overview-file)))
eec82323
LMI
151 ;; Kill the buffer -- it's either unmodified or saved.
152 (gnus-kill-buffer buffer)
153 (setq gnus-cache-buffer nil))))
154
155(defun gnus-cache-possibly-enter-article
16409b0b 156 (group article ticked dormant unread &optional force)
eec82323
LMI
157 (when (and (or force (not (eq gnus-use-cache 'passive)))
158 (numberp article)
16409b0b 159 (> article 0)) ; This might be a dummy article.
01c52d31
MB
160 (let ((number article)
161 file headers lines-chars
162 (file-name-coding-system nnmail-pathname-coding-system))
16409b0b
GM
163 ;; If this is a virtual group, we find the real group.
164 (when (gnus-virtual-group-p group)
165 (let ((result (nnvirtual-find-group-art
166 (gnus-group-real-name group) article)))
167 (setq group (car result)
168 number (cdr result))))
a8151ef7
LMI
169 (when (and number
170 (> number 0) ; Reffed article.
eec82323 171 (or force
23f87bed 172 (and (gnus-cache-fully-p group)
eec82323
LMI
173 (gnus-cache-member-of-class
174 gnus-cache-enter-articles ticked dormant unread)))
175 (not (file-exists-p (setq file (gnus-cache-file-name
176 group number)))))
177 ;; Possibly create the cache directory.
6748645f 178 (gnus-make-directory (file-name-directory file))
eec82323
LMI
179 ;; Save the article in the cache.
180 (if (file-exists-p file)
181 t ; The article already is saved.
20a673b2 182 (with-current-buffer nntp-server-buffer
16409b0b
GM
183 (require 'gnus-art)
184 (let ((gnus-use-cache nil)
185 (gnus-article-decode-hook nil))
eec82323
LMI
186 (gnus-request-article-this-buffer number group))
187 (when (> (buffer-size) 0)
16409b0b 188 (let ((coding-system-for-write gnus-cache-coding-system))
01c52d31
MB
189 (gnus-write-buffer file)
190 (gnus-cache-update-file-total-fetched-for group file))
191 (setq lines-chars (nnheader-get-lines-and-char))
23f87bed
MB
192 (nnheader-remove-body)
193 (setq headers (nnheader-parse-naked-head))
16409b0b 194 (mail-header-set-number headers number)
01c52d31
MB
195 (mail-header-set-lines headers (car lines-chars))
196 (mail-header-set-chars headers (cadr lines-chars))
eec82323
LMI
197 (gnus-cache-change-buffer group)
198 (set-buffer (cdr gnus-cache-buffer))
199 (goto-char (point-max))
200 (forward-line -1)
201 (while (condition-case ()
202 (when (not (bobp))
203 (> (read (current-buffer)) number))
204 (error
205 ;; The line was malformed, so we just remove it!!
206 (gnus-delete-line)
207 t))
208 (forward-line -1))
209 (if (bobp)
210 (if (not (eobp))
211 (progn
212 (beginning-of-line)
213 (when (< (read (current-buffer)) number)
214 (forward-line 1)))
215 (beginning-of-line))
216 (forward-line 1))
217 (beginning-of-line)
16409b0b 218 (nnheader-insert-nov headers)
eec82323
LMI
219 ;; Update the active info.
220 (set-buffer gnus-summary-buffer)
23f87bed
MB
221 (gnus-cache-possibly-update-active group (cons number number))
222 (setq gnus-newsgroup-cached
223 (gnus-add-to-sorted-list gnus-newsgroup-cached article))
eec82323
LMI
224 (gnus-summary-update-secondary-mark article))
225 t))))))
226
227(defun gnus-cache-enter-remove-article (article)
228 "Mark ARTICLE for later possible removal."
229 (when article
230 (push article gnus-cache-removable-articles)))
231
232(defun gnus-cache-possibly-remove-articles ()
233 "Possibly remove some of the removable articles."
234 (if (not (gnus-virtual-group-p gnus-newsgroup-name))
235 (gnus-cache-possibly-remove-articles-1)
236 (let ((arts gnus-cache-removable-articles)
237 ga)
238 (while arts
239 (when (setq ga (nnvirtual-find-group-art
240 (gnus-group-real-name gnus-newsgroup-name) (pop arts)))
241 (let ((gnus-cache-removable-articles (list (cdr ga)))
242 (gnus-newsgroup-name (car ga)))
243 (gnus-cache-possibly-remove-articles-1)))))
244 (setq gnus-cache-removable-articles nil)))
245
246(defun gnus-cache-possibly-remove-articles-1 ()
247 "Possibly remove some of the removable articles."
23f87bed 248 (when (gnus-cache-fully-p gnus-newsgroup-name)
01c52d31 249 (let ((cache-articles gnus-newsgroup-cached))
eec82323 250 (gnus-cache-change-buffer gnus-newsgroup-name)
01c52d31
MB
251 (dolist (article gnus-cache-removable-articles)
252 (when (memq article cache-articles)
eec82323
LMI
253 ;; The article was in the cache, so we see whether we are
254 ;; supposed to remove it from the cache.
255 (gnus-cache-possibly-remove-article
256 article (memq article gnus-newsgroup-marked)
257 (memq article gnus-newsgroup-dormant)
258 (or (memq article gnus-newsgroup-unreads)
259 (memq article gnus-newsgroup-unselected))))))
260 ;; The overview file might have been modified, save it
261 ;; safe because we're only called at group exit anyway.
262 (gnus-cache-save-buffers)))
263
264(defun gnus-cache-request-article (article group)
265 "Retrieve ARTICLE in GROUP from the cache."
266 (let ((file (gnus-cache-file-name group article))
01c52d31
MB
267 (buffer-read-only nil)
268 (file-name-coding-system nnmail-pathname-coding-system))
eec82323
LMI
269 (when (file-exists-p file)
270 (erase-buffer)
271 (gnus-kill-all-overlays)
16409b0b
GM
272 (let ((coding-system-for-read gnus-cache-coding-system))
273 (insert-file-contents file))
eec82323
LMI
274 t)))
275
276(defun gnus-cache-possibly-alter-active (group active)
277 "Alter the ACTIVE info for GROUP to reflect the articles in the cache."
eec82323
LMI
278 (when gnus-cache-active-hashtb
279 (let ((cache-active (gnus-gethash group gnus-cache-active-hashtb)))
a8151ef7
LMI
280 (when cache-active
281 (when (< (car cache-active) (car active))
282 (setcar active (car cache-active)))
283 (when (> (cdr cache-active) (cdr active))
284 (setcdr active (cdr cache-active)))))))
eec82323
LMI
285
286(defun gnus-cache-retrieve-headers (articles group &optional fetch-old)
287 "Retrieve the headers for ARTICLES in GROUP."
288 (let ((cached
289 (setq gnus-newsgroup-cached (gnus-cache-articles-in-group group))))
290 (if (not cached)
291 ;; No cached articles here, so we just retrieve them
292 ;; the normal way.
293 (let ((gnus-use-cache nil))
294 (gnus-retrieve-headers articles group fetch-old))
23f87bed 295 (let ((uncached-articles (gnus-sorted-difference articles cached))
eec82323 296 (cache-file (gnus-cache-file-name group ".overview"))
01c52d31
MB
297 type
298 (file-name-coding-system nnmail-pathname-coding-system))
eec82323
LMI
299 ;; We first retrieve all the headers that we don't have in
300 ;; the cache.
301 (let ((gnus-use-cache nil))
302 (when uncached-articles
303 (setq type (and articles
304 (gnus-retrieve-headers
305 uncached-articles group fetch-old)))))
306 (gnus-cache-save-buffers)
307 ;; Then we insert the cached headers.
308 (save-excursion
309 (cond
310 ((not (file-exists-p cache-file))
311 ;; There are no cached headers.
312 type)
313 ((null type)
314 ;; There were no uncached headers (or retrieval was
315 ;; unsuccessful), so we use the cached headers exclusively.
316 (set-buffer nntp-server-buffer)
317 (erase-buffer)
a1506d29 318 (let ((coding-system-for-read
16409b0b
GM
319 gnus-cache-overview-coding-system))
320 (insert-file-contents cache-file))
eec82323
LMI
321 'nov)
322 ((eq type 'nov)
323 ;; We have both cached and uncached NOV headers, so we
324 ;; braid them.
325 (gnus-cache-braid-nov group cached)
326 type)
327 (t
328 ;; We braid HEADs.
329 (gnus-cache-braid-heads group (gnus-sorted-intersection
330 cached articles))
331 type)))))))
332
333(defun gnus-cache-enter-article (&optional n)
334 "Enter the next N articles into the cache.
335If not given a prefix, use the process marked articles instead.
336Returns the list of articles entered."
337 (interactive "P")
01c52d31
MB
338 (let (out)
339 (dolist (article (gnus-summary-work-articles n))
6748645f 340 (gnus-summary-remove-process-mark article)
eec82323
LMI
341 (if (natnump article)
342 (when (gnus-cache-possibly-enter-article
343 gnus-newsgroup-name article
eec82323 344 nil nil nil t)
23f87bed 345 (setq gnus-newsgroup-undownloaded (delq article gnus-newsgroup-undownloaded))
eec82323
LMI
346 (push article out))
347 (gnus-message 2 "Can't cache article %d" article))
23f87bed 348 (gnus-summary-update-download-mark article)
eec82323
LMI
349 (gnus-summary-update-secondary-mark article))
350 (gnus-summary-next-subject 1)
351 (gnus-summary-position-point)
352 (nreverse out)))
353
23f87bed 354(defun gnus-cache-remove-article (&optional n)
eec82323
LMI
355 "Remove the next N articles from the cache.
356If not given a prefix, use the process marked articles instead.
357Returns the list of articles removed."
358 (interactive "P")
eec82323 359 (gnus-cache-change-buffer gnus-newsgroup-name)
01c52d31
MB
360 (let (out)
361 (dolist (article (gnus-summary-work-articles n))
6748645f 362 (gnus-summary-remove-process-mark article)
eec82323 363 (when (gnus-cache-possibly-remove-article article nil nil nil t)
23f87bed
MB
364 (when gnus-newsgroup-agentized
365 (let ((alist (gnus-agent-load-alist gnus-newsgroup-name)))
366 (unless (cdr (assoc article alist))
367 (setq gnus-newsgroup-undownloaded
e84b4b86 368 (gnus-add-to-sorted-list
23f87bed 369 gnus-newsgroup-undownloaded article)))))
eec82323 370 (push article out))
23f87bed 371 (gnus-summary-update-download-mark article)
eec82323
LMI
372 (gnus-summary-update-secondary-mark article))
373 (gnus-summary-next-subject 1)
374 (gnus-summary-position-point)
375 (nreverse out)))
376
377(defun gnus-cached-article-p (article)
378 "Say whether ARTICLE is cached in the current group."
379 (memq article gnus-newsgroup-cached))
380
381(defun gnus-summary-insert-cached-articles ()
382 "Insert all the articles cached for this group into the current buffer."
383 (interactive)
23f87bed 384 (let ((gnus-verbose (max 6 gnus-verbose)))
144b7b5c
G
385 (cond
386 ((not gnus-newsgroup-cached)
387 (gnus-message 3 "No cached articles for this group"))
388 ;; This is faster if there are few articles to insert.
389 ((< (length gnus-newsgroup-cached) 20)
390 (gnus-summary-goto-subjects gnus-newsgroup-cached))
391 (t
392 (gnus-summary-include-articles gnus-newsgroup-cached)))))
eec82323 393
23f87bed
MB
394(defun gnus-summary-limit-include-cached ()
395 "Limit the summary buffer to articles that are cached."
396 (interactive)
397 (let ((gnus-verbose (max 6 gnus-verbose)))
398 (if gnus-newsgroup-cached
399 (progn
400 (gnus-summary-limit gnus-newsgroup-cached)
401 (gnus-summary-position-point))
402 (gnus-message 3 "No cached articles for this group"))))
6748645f 403
eec82323
LMI
404;;; Internal functions.
405
406(defun gnus-cache-change-buffer (group)
407 (and gnus-cache-buffer
408 ;; See if the current group's overview cache has been loaded.
409 (or (string= group (car gnus-cache-buffer))
410 ;; Another overview cache is current, save it.
411 (gnus-cache-save-buffers)))
412 ;; if gnus-cache buffer is nil, create it
413 (unless gnus-cache-buffer
414 ;; Create cache buffer
415 (save-excursion
416 (setq gnus-cache-buffer
417 (cons group
6748645f
LMI
418 (set-buffer (gnus-get-buffer-create
419 " *gnus-cache-overview*"))))
eec82323
LMI
420 ;; Insert the contents of this group's cache overview.
421 (erase-buffer)
01c52d31
MB
422 (let ((file (gnus-cache-file-name group ".overview"))
423 (file-name-coding-system nnmail-pathname-coding-system))
eec82323
LMI
424 (when (file-exists-p file)
425 (nnheader-insert-file-contents file)))
426 ;; We have a fresh (empty/just loaded) buffer,
427 ;; mark it as unmodified to save a redundant write later.
428 (set-buffer-modified-p nil))))
429
430;; Return whether an article is a member of a class.
431(defun gnus-cache-member-of-class (class ticked dormant unread)
432 (or (and ticked (memq 'ticked class))
433 (and dormant (memq 'dormant class))
434 (and unread (memq 'unread class))
435 (and (not unread) (not ticked) (not dormant) (memq 'read class))))
436
01c52d31
MB
437(defvar gnus-cache-decoded-group-names nil
438 "Alist of original group names and decoded group names.
439Decoding is done according to `gnus-group-name-charset-method-alist'
440or `gnus-group-name-charset-group-alist'.")
441
442(defvar gnus-cache-unified-group-names nil
443 "Alist of unified decoded group names and original group names.
444A group name is decoded according to
445`gnus-group-name-charset-method-alist' or
446`gnus-group-name-charset-group-alist' first, and is encoded and
447decoded again according to `nnmail-pathname-coding-system',
448`file-name-coding-system', or `default-file-name-coding-system'.
449
450It is used when asking for a original group name from a cache
451directory name, in which non-ASCII characters might have been unified
452into the ones of a certain charset particularly if the `utf-8' coding
453system for example was used.")
454
455(defun gnus-cache-decoded-group-name (group)
456 "Return a decoded group name of GROUP."
457 (or (cdr (assoc group gnus-cache-decoded-group-names))
458 (let ((decoded (gnus-group-decoded-name group))
459 (coding (or nnmail-pathname-coding-system
460 (and (boundp 'file-name-coding-system)
461 file-name-coding-system)
462 (and (boundp 'default-file-name-coding-system)
463 default-file-name-coding-system))))
464 (push (cons group decoded) gnus-cache-decoded-group-names)
465 (push (cons (mm-decode-coding-string
466 (mm-encode-coding-string decoded coding)
467 coding)
468 group)
469 gnus-cache-unified-group-names)
470 decoded)))
471
eec82323 472(defun gnus-cache-file-name (group article)
01c52d31 473 (setq group (gnus-cache-decoded-group-name group))
6ab8077a
DL
474 (expand-file-name
475 (if (stringp article) article (int-to-string article))
476 (file-name-as-directory
477 (expand-file-name
478 (nnheader-translate-file-chars
479 (if (gnus-use-long-file-name 'not-cache)
480 group
481 (let ((group (nnheader-replace-duplicate-chars-in-string
482 (nnheader-replace-chars-in-string group ?/ ?_)
483 ?. ?_)))
484 ;; Translate the first colon into a slash.
485 (when (string-match ":" group)
23f87bed
MB
486 (setq group (concat (substring group 0 (match-beginning 0))
487 "/" (substring group (match-end 0)))))
6ab8077a
DL
488 (nnheader-replace-chars-in-string group ?. ?/)))
489 t)
490 gnus-cache-directory))))
eec82323
LMI
491
492(defun gnus-cache-update-article (group article)
493 "If ARTICLE is in the cache, remove it and re-enter it."
6748645f 494 (gnus-cache-change-buffer group)
16409b0b 495 (when (gnus-cache-possibly-remove-article article nil nil nil t)
eec82323
LMI
496 (let ((gnus-use-cache nil))
497 (gnus-cache-possibly-enter-article
16409b0b 498 gnus-newsgroup-name article
eec82323
LMI
499 nil nil nil t))))
500
501(defun gnus-cache-possibly-remove-article (article ticked dormant unread
502 &optional force)
503 "Possibly remove ARTICLE from the cache."
504 (let ((group gnus-newsgroup-name)
505 (number article)
01c52d31
MB
506 file
507 (file-name-coding-system nnmail-pathname-coding-system))
eec82323
LMI
508 ;; If this is a virtual group, we find the real group.
509 (when (gnus-virtual-group-p group)
510 (let ((result (nnvirtual-find-group-art
511 (gnus-group-real-name group) article)))
512 (setq group (car result)
513 number (cdr result))))
514 (setq file (gnus-cache-file-name group number))
515 (when (and (file-exists-p file)
516 (or force
517 (gnus-cache-member-of-class
518 gnus-cache-remove-articles ticked dormant unread)))
519 (save-excursion
01c52d31 520 (gnus-cache-update-file-total-fetched-for group file t)
eec82323 521 (delete-file file)
01c52d31 522
eec82323
LMI
523 (set-buffer (cdr gnus-cache-buffer))
524 (goto-char (point-min))
525 (when (or (looking-at (concat (int-to-string number) "\t"))
526 (search-forward (concat "\n" (int-to-string number) "\t")
527 (point-max) t))
01c52d31 528 (gnus-delete-line)))
23f87bed
MB
529 (unless (setq gnus-newsgroup-cached
530 (delq article gnus-newsgroup-cached))
531 (gnus-sethash gnus-newsgroup-name nil gnus-cache-active-hashtb)
532 (setq gnus-cache-active-altered t))
eec82323
LMI
533 (gnus-summary-update-secondary-mark article)
534 t)))
535
536(defun gnus-cache-articles-in-group (group)
537 "Return a sorted list of cached articles in GROUP."
a8151ef7 538 (let ((dir (file-name-directory (gnus-cache-file-name group 1)))
01c52d31
MB
539 articles
540 (file-name-coding-system nnmail-pathname-coding-system))
eec82323 541 (when (file-exists-p dir)
a8151ef7 542 (setq articles
e9bd5782 543 (sort (mapcar (lambda (name) (string-to-number name))
a8151ef7
LMI
544 (directory-files dir nil "^[0-9]+$" t))
545 '<))
546 ;; Update the cache active file, just to synch more.
23f87bed
MB
547 (if articles
548 (progn
549 (gnus-cache-update-active group (car articles) t)
550 (gnus-cache-update-active group (car (last articles))))
551 (when (gnus-gethash group gnus-cache-active-hashtb)
552 (gnus-sethash group nil gnus-cache-active-hashtb)
553 (setq gnus-cache-active-altered t)))
a8151ef7
LMI
554 articles)))
555
556(defun gnus-cache-braid-nov (group cached &optional file)
6748645f 557 (let ((cache-buf (gnus-get-buffer-create " *gnus-cache*"))
eec82323
LMI
558 beg end)
559 (gnus-cache-save-buffers)
20a673b2 560 (with-current-buffer cache-buf
eec82323 561 (erase-buffer)
01c52d31
MB
562 (let ((coding-system-for-read gnus-cache-overview-coding-system)
563 (file-name-coding-system nnmail-pathname-coding-system))
a1506d29 564 (insert-file-contents
16409b0b 565 (or file (gnus-cache-file-name group ".overview"))))
eec82323
LMI
566 (goto-char (point-min))
567 (insert "\n")
568 (goto-char (point-min)))
569 (set-buffer nntp-server-buffer)
570 (goto-char (point-min))
571 (while cached
572 (while (and (not (eobp))
573 (< (read (current-buffer)) (car cached)))
574 (forward-line 1))
575 (beginning-of-line)
23f87bed
MB
576 (set-buffer cache-buf)
577 (if (search-forward (concat "\n" (int-to-string (car cached)) "\t")
578 nil t)
01c52d31 579 (setq beg (point-at-bol)
23f87bed
MB
580 end (progn (end-of-line) (point)))
581 (setq beg nil))
582 (set-buffer nntp-server-buffer)
eec82323
LMI
583 (when beg
584 (insert-buffer-substring cache-buf beg end)
585 (insert "\n"))
586 (setq cached (cdr cached)))
587 (kill-buffer cache-buf)))
588
589(defun gnus-cache-braid-heads (group cached)
6748645f 590 (let ((cache-buf (gnus-get-buffer-create " *gnus-cache*")))
01c52d31 591 (with-current-buffer cache-buf
eec82323
LMI
592 (erase-buffer))
593 (set-buffer nntp-server-buffer)
594 (goto-char (point-min))
01c52d31 595 (dolist (entry cached)
eec82323
LMI
596 (while (and (not (eobp))
597 (looking-at "2.. +\\([0-9]+\\) ")
598 (< (progn (goto-char (match-beginning 1))
599 (read (current-buffer)))
01c52d31 600 entry))
eec82323
LMI
601 (search-forward "\n.\n" nil 'move))
602 (beginning-of-line)
23f87bed
MB
603 (set-buffer cache-buf)
604 (erase-buffer)
01c52d31
MB
605 (let ((coding-system-for-read gnus-cache-coding-system)
606 (file-name-coding-system nnmail-pathname-coding-system))
607 (insert-file-contents (gnus-cache-file-name group entry)))
23f87bed
MB
608 (goto-char (point-min))
609 (insert "220 ")
61b1af82 610 (princ (pop cached) (current-buffer))
23f87bed
MB
611 (insert " Article retrieved.\n")
612 (search-forward "\n\n" nil 'move)
613 (delete-region (point) (point-max))
614 (forward-char -1)
615 (insert ".")
616 (set-buffer nntp-server-buffer)
01c52d31 617 (insert-buffer-substring cache-buf))
eec82323
LMI
618 (kill-buffer cache-buf)))
619
620;;;###autoload
621(defun gnus-jog-cache ()
622 "Go through all groups and put the articles into the cache.
623
624Usage:
625$ emacs -batch -l ~/.emacs -l gnus -f gnus-jog-cache"
626 (interactive)
627 (let ((gnus-mark-article-hook nil)
628 (gnus-expert-user t)
16409b0b 629 (mail-sources nil)
eec82323
LMI
630 (gnus-use-dribble-file nil)
631 (gnus-novice-user nil)
632 (gnus-large-newsgroup nil))
633 ;; Start Gnus.
634 (gnus)
635 ;; Go through all groups...
636 (gnus-group-mark-buffer)
a8151ef7
LMI
637 (gnus-group-iterate nil
638 (lambda (group)
639 (let (gnus-auto-select-next)
640 (gnus-summary-read-group group nil t)
641 ;; ... and enter the articles into the cache.
642 (when (eq major-mode 'gnus-summary-mode)
643 (gnus-uu-mark-buffer)
644 (gnus-cache-enter-article)
645 (kill-buffer (current-buffer))))))))
eec82323
LMI
646
647(defun gnus-cache-read-active (&optional force)
648 "Read the cache active file."
649 (gnus-make-directory gnus-cache-directory)
a8151ef7 650 (if (or (not (file-exists-p gnus-cache-active-file))
6748645f 651 (zerop (nth 7 (file-attributes gnus-cache-active-file)))
a8151ef7 652 force)
eec82323
LMI
653 ;; There is no active file, so we generate one.
654 (gnus-cache-generate-active)
655 ;; We simply read the active file.
656 (save-excursion
657 (gnus-set-work-buffer)
16409b0b 658 (nnheader-insert-file-contents gnus-cache-active-file)
eec82323
LMI
659 (gnus-active-to-gnus-format
660 nil (setq gnus-cache-active-hashtb
661 (gnus-make-hashtable
662 (count-lines (point-min) (point-max)))))
663 (setq gnus-cache-active-altered nil))))
664
665(defun gnus-cache-write-active (&optional force)
666 "Write the active hashtb to the active file."
667 (when (or force
668 (and gnus-cache-active-hashtb
669 gnus-cache-active-altered))
16409b0b 670 (gnus-write-active-file gnus-cache-active-file gnus-cache-active-hashtb t)
eec82323
LMI
671 ;; Mark the active hashtb as unaltered.
672 (setq gnus-cache-active-altered nil)))
673
23f87bed
MB
674(defun gnus-cache-possibly-update-active (group active)
675 "Update active info bounds of GROUP with ACTIVE if necessary.
676The update is performed if ACTIVE contains a higher or lower bound
677than the current."
678 (let ((lower t) (higher t))
679 (if gnus-cache-active-hashtb
680 (let ((cache-active (gnus-gethash group gnus-cache-active-hashtb)))
681 (when cache-active
682 (unless (< (car active) (car cache-active))
683 (setq lower nil))
684 (unless (> (cdr active) (cdr cache-active))
685 (setq higher nil))))
686 (gnus-cache-read-active))
687 (when lower
688 (gnus-cache-update-active group (car active) t))
689 (when higher
690 (gnus-cache-update-active group (cdr active)))))
691
eec82323
LMI
692(defun gnus-cache-update-active (group number &optional low)
693 "Update the upper bound of the active info of GROUP to NUMBER.
694If LOW, update the lower bound instead."
695 (let ((active (gnus-gethash group gnus-cache-active-hashtb)))
696 (if (null active)
697 ;; We just create a new active entry for this group.
698 (gnus-sethash group (cons number number) gnus-cache-active-hashtb)
699 ;; Update the lower or upper bound.
700 (if low
701 (setcar active number)
702 (setcdr active number)))
703 ;; Mark the active hashtb as altered.
704 (setq gnus-cache-active-altered t)))
705
706;;;###autoload
707(defun gnus-cache-generate-active (&optional directory)
708 "Generate the cache active file."
709 (interactive)
710 (let* ((top (null directory))
711 (directory (expand-file-name (or directory gnus-cache-directory)))
01c52d31 712 (file-name-coding-system nnmail-pathname-coding-system)
eec82323
LMI
713 (files (directory-files directory 'full))
714 (group
715 (if top
716 ""
717 (string-match
6748645f
LMI
718 (concat "^" (regexp-quote
719 (file-name-as-directory
720 (expand-file-name gnus-cache-directory))))
eec82323
LMI
721 (directory-file-name directory))
722 (nnheader-replace-chars-in-string
723 (substring (directory-file-name directory) (match-end 0))
724 ?/ ?.)))
725 nums alphs)
726 (when top
727 (gnus-message 5 "Generating the cache active file...")
728 (setq gnus-cache-active-hashtb (gnus-make-hashtable 123)))
6748645f 729 (when (string-match "^\\(nn[^_]+\\)_" group)
23f87bed 730 (setq group (replace-match "\\1:" t nil group)))
eec82323
LMI
731 ;; Separate articles from all other files and directories.
732 (while files
733 (if (string-match "^[0-9]+$" (file-name-nondirectory (car files)))
e9bd5782 734 (push (string-to-number (file-name-nondirectory (pop files))) nums)
eec82323
LMI
735 (push (pop files) alphs)))
736 ;; If we have nums, then this is probably a valid group.
737 (when (setq nums (sort nums '<))
01c52d31
MB
738 ;; Use non-decoded group name.
739 ;; FIXME: this is kind of a workaround. The active file should
740 ;; be updated at the time articles are cached. It will make
741 ;; `gnus-cache-unified-group-names' needless.
742 (gnus-sethash (or (cdr (assoc group gnus-cache-unified-group-names))
743 group)
744 (cons (car nums) (gnus-last-element nums))
eec82323
LMI
745 gnus-cache-active-hashtb))
746 ;; Go through all the other files.
01c52d31
MB
747 (dolist (file alphs)
748 (when (and (file-directory-p file)
6748645f 749 (not (string-match "^\\."
01c52d31 750 (file-name-nondirectory file))))
eec82323 751 ;; We descend directories.
01c52d31 752 (gnus-cache-generate-active file)))
eec82323
LMI
753 ;; Write the new active file.
754 (when top
755 (gnus-cache-write-active t)
756 (gnus-message 5 "Generating the cache active file...done"))))
757
758;;;###autoload
759(defun gnus-cache-generate-nov-databases (dir)
760 "Generate NOV files recursively starting in DIR."
761 (interactive (list gnus-cache-directory))
762 (gnus-cache-close)
763 (let ((nnml-generate-active-function 'identity))
b890d447 764 (nnml-generate-nov-databases-directory dir))
01c52d31
MB
765
766 (setq gnus-cache-total-fetched-hashtb nil)
767
23f87bed 768 (gnus-cache-open))
eec82323
LMI
769
770(defun gnus-cache-move-cache (dir)
771 "Move the cache tree to somewhere else."
a8151ef7 772 (interactive "FMove the cache tree to: ")
eec82323
LMI
773 (rename-file gnus-cache-directory dir))
774
23f87bed
MB
775(defun gnus-cache-fully-p (&optional group)
776 "Returns non-nil if the cache should be fully used.
777If GROUP is non-nil, also cater to `gnus-cacheable-groups' and
778`gnus-uncacheable-groups'."
779 (and gnus-use-cache
780 (not (eq gnus-use-cache 'passive))
781 (if (null group)
782 t
783 (and (or (not gnus-cacheable-groups)
784 (string-match gnus-cacheable-groups group))
785 (or (not gnus-uncacheable-groups)
786 (not (string-match gnus-uncacheable-groups group)))))))
787
54506618
MB
788;;;###autoload
789(defun gnus-cache-rename-group (old-group new-group)
58090a8d
MB
790 "Rename OLD-GROUP as NEW-GROUP.
791Always updates the cache, even when disabled, as the old cache
792files would corrupt Gnus when the cache was next enabled. It
793depends on the caller to determine whether group renaming is
794supported."
54506618 795 (let ((old-dir (gnus-cache-file-name old-group ""))
01c52d31
MB
796 (new-dir (gnus-cache-file-name new-group ""))
797 (file-name-coding-system nnmail-pathname-coding-system))
54506618
MB
798 (gnus-rename-file old-dir new-dir t))
799
01c52d31
MB
800 (gnus-cache-rename-group-total-fetched-for old-group new-group)
801
54506618
MB
802 (let ((no-save gnus-cache-active-hashtb))
803 (unless gnus-cache-active-hashtb
804 (gnus-cache-read-active))
58090a8d
MB
805 (let* ((old-group-hash-value
806 (gnus-gethash old-group gnus-cache-active-hashtb))
807 (new-group-hash-value
808 (gnus-gethash new-group gnus-cache-active-hashtb))
809 (delta
810 (or old-group-hash-value new-group-hash-value)))
54506618
MB
811 (gnus-sethash new-group old-group-hash-value gnus-cache-active-hashtb)
812 (gnus-sethash old-group nil gnus-cache-active-hashtb)
813
814 (if no-save
815 (setq gnus-cache-active-altered delta)
816 (gnus-cache-write-active delta)))))
817
818;;;###autoload
819(defun gnus-cache-delete-group (group)
58090a8d
MB
820 "Delete GROUP from the cache.
821Always updates the cache, even when disabled, as the old cache
822files would corrupt gnus when the cache was next enabled.
823Depends upon the caller to determine whether group deletion is
824supported."
01c52d31
MB
825 (let ((dir (gnus-cache-file-name group ""))
826 (file-name-coding-system nnmail-pathname-coding-system))
aa0a8561 827 (gnus-delete-directory dir))
54506618 828
01c52d31
MB
829 (gnus-cache-delete-group-total-fetched-for group)
830
54506618
MB
831 (let ((no-save gnus-cache-active-hashtb))
832 (unless gnus-cache-active-hashtb
833 (gnus-cache-read-active))
834 (let* ((group-hash-value (gnus-gethash group gnus-cache-active-hashtb)))
835 (gnus-sethash group nil gnus-cache-active-hashtb)
836
837 (if no-save
838 (setq gnus-cache-active-altered group-hash-value)
839 (gnus-cache-write-active group-hash-value)))))
840
01c52d31
MB
841(defvar gnus-cache-inhibit-update-total-fetched-for nil)
842(defvar gnus-cache-need-update-total-fetched-for nil)
843
844(defmacro gnus-cache-with-refreshed-group (group &rest body)
845 `(prog1 (let ((gnus-cache-inhibit-update-total-fetched-for t))
846 ,@body)
847 (when (and gnus-cache-need-update-total-fetched-for
848 (not gnus-cache-inhibit-update-total-fetched-for))
20a673b2 849 (with-current-buffer gnus-group-buffer
01c52d31
MB
850 (setq gnus-cache-need-update-total-fetched-for nil)
851 (gnus-group-update-group ,group t)))))
852
853(defun gnus-cache-update-file-total-fetched-for (group file &optional subtract)
854 (when gnus-cache-total-fetched-hashtb
855 (gnus-cache-with-refreshed-group
856 group
857 (let* ((entry (or (gnus-gethash group gnus-cache-total-fetched-hashtb)
858 (gnus-sethash group (make-vector 2 0)
859 gnus-cache-total-fetched-hashtb)))
860 size)
861
862 (if file
863 (setq size (or (nth 7 (file-attributes file)) 0))
864 (let* ((file-name-coding-system nnmail-pathname-coding-system)
865 (files (directory-files (gnus-cache-file-name group "")
866 t nil t))
867 file attrs)
868 (setq size 0.0)
869 (while (setq file (pop files))
870 (setq attrs (file-attributes file))
871 (unless (nth 0 attrs)
c9fc72fa 872 (incf size (float (nth 7 attrs)))))))
01c52d31
MB
873
874 (setq gnus-cache-need-update-total-fetched-for t)
875
876 (incf (nth 1 entry) (if subtract (- size) size))))))
877
878(defun gnus-cache-update-overview-total-fetched-for (group file)
879 (when gnus-cache-total-fetched-hashtb
880 (gnus-cache-with-refreshed-group
881 group
882 (let* ((entry (or (gnus-gethash group gnus-cache-total-fetched-hashtb)
c9fc72fa 883 (gnus-sethash group (make-list 2 0)
01c52d31
MB
884 gnus-cache-total-fetched-hashtb)))
885 (file-name-coding-system nnmail-pathname-coding-system)
c9fc72fa 886 (size (or (nth 7 (file-attributes
01c52d31
MB
887 (or file
888 (gnus-cache-file-name group ".overview"))))
889 0)))
890 (setq gnus-cache-need-update-total-fetched-for t)
891 (setf (nth 0 entry) size)))))
892
893(defun gnus-cache-rename-group-total-fetched-for (old-group new-group)
894 "Record of disk space used by OLD-GROUP now associated with NEW-GROUP."
895 (when gnus-cache-total-fetched-hashtb
896 (let ((entry (gnus-gethash old-group gnus-cache-total-fetched-hashtb)))
897 (gnus-sethash new-group entry gnus-cache-total-fetched-hashtb)
898 (gnus-sethash old-group nil gnus-cache-total-fetched-hashtb))))
899
900(defun gnus-cache-delete-group-total-fetched-for (group)
901 "Delete record of disk space used by GROUP being deleted."
902 (when gnus-cache-total-fetched-hashtb
903 (gnus-sethash group nil gnus-cache-total-fetched-hashtb)))
904
905(defun gnus-cache-total-fetched-for (group &optional no-inhibit)
906 "Get total disk space used by the cache for the specified GROUP."
907 (unless (equal group "dummy.group")
908 (unless gnus-cache-total-fetched-hashtb
909 (setq gnus-cache-total-fetched-hashtb (gnus-make-hashtable 1024)))
910
911 (let* ((entry (gnus-gethash group gnus-cache-total-fetched-hashtb)))
912 (if entry
913 (apply '+ entry)
914 (let ((gnus-cache-inhibit-update-total-fetched-for (not no-inhibit)))
c9fc72fa 915 (+
01c52d31
MB
916 (gnus-cache-update-overview-total-fetched-for group nil)
917 (gnus-cache-update-file-total-fetched-for group nil)))))))
918
eec82323
LMI
919(provide 'gnus-cache)
920
921;;; gnus-cache.el ends here