1 ;;; gnus-async.el --- asynchronous support for Gnus
2 ;; Copyright (C) 1996 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
32 (defvar gnus-use-article-prefetch 30
33 "*If non-nil, prefetch articles in groups that allow this.
34 If a number, prefetch only that many articles forward;
35 if t, prefetch as many articles as possible.")
37 (defvar gnus-prefetched-article-deletion-strategy '(read exit)
38 "List of symbols that say when to remove articles from the prefetch buffer.
39 Possible values in this list are `read', which means that
40 articles are removed as they are read, and `exit', which means
41 that all articles belonging to a group are removed on exit
44 (defvar gnus-use-header-prefetch nil
45 "*If non-nil, prefetch the headers to the next group.")
47 ;;; Internal variables.
49 (defvar gnus-async-prefetch-article-buffer " *Async Prefetch Article*")
50 (defvar gnus-async-article-alist nil)
51 (defvar gnus-async-article-semaphore '(nil))
52 (defvar gnus-async-fetch-list nil)
54 (defvar gnus-async-prefetch-headers-buffer " *Async Prefetch Headers*")
55 (defvar gnus-async-header-prefetched nil)
57 ;;; Utility functions.
59 (defun gnus-group-asynchronous-p (group)
60 "Say whether GROUP is fetched from a server that supports asynchronocity."
61 (gnus-asynchronous-p (gnus-find-method-for-group group)))
63 ;;; Somewhat bogus semaphores.
65 (defun gnus-async-get-semaphore (semaphore)
66 "Wait until SEMAPHORE is released."
67 (while (/= (length (nconc (symbol-value semaphore) (list nil))) 2)
70 (defun gnus-async-release-semaphore (semaphore)
72 (setcdr (symbol-value semaphore) nil))
78 (gnus-add-shutdown 'gnus-async-close 'gnus)
79 (defun gnus-async-close ()
80 (gnus-kill-buffer gnus-async-prefetch-article-buffer)
81 (gnus-kill-buffer gnus-async-prefetch-headers-buffer)
82 (setq gnus-async-article-alist nil
83 gnus-async-header-prefetched nil))
85 (defun gnus-async-set-buffer ()
86 (nnheader-set-temp-buffer gnus-async-prefetch-article-buffer t))
88 (defun gnus-async-prefetch-next (group article summary)
89 "Possibly prefetch several articles starting with the article after ARTICLE."
90 (let ((next (caadr (gnus-data-find-list article))))
92 (gnus-async-prefetch-article group next summary))))
94 (defun gnus-async-prefetch-article (group article summary &optional next)
95 "Possibly prefetch several articles starting with ARTICLE."
97 (gnus-async-get-semaphore 'gnus-async-article-semaphore)
98 (pop gnus-async-fetch-list)
99 (gnus-async-release-semaphore 'gnus-async-article-semaphore))
100 (let ((do-fetch next))
101 (when (and (gnus-group-asynchronous-p group)
102 (gnus-buffer-live-p summary)
104 gnus-async-fetch-list))
107 (gnus-async-get-semaphore 'gnus-async-article-semaphore)
109 (setq do-fetch (not gnus-async-fetch-list))
110 ;; Nix out any outstanding requests.
111 (setq gnus-async-fetch-list nil)
112 ;; Fill in the new list.
113 (let ((n gnus-use-article-prefetch)
114 (data (gnus-data-find-list article))
116 (while (and (setq d (pop data))
120 (unless (or (gnus-async-prefetched-article-entry
121 group (setq article (gnus-data-number d)))
122 (not (natnump article)))
123 ;; Not already fetched -- so we add it to the list.
124 (push article gnus-async-fetch-list)))
125 (setq gnus-async-fetch-list (nreverse gnus-async-fetch-list))))
128 (setq article (pop gnus-async-fetch-list))))
130 (gnus-async-release-semaphore 'gnus-async-article-semaphore))
133 ;; We want to fetch some more articles.
137 (gnus-async-set-buffer)
138 (goto-char (point-max))
139 (setq mark (point-marker))
140 (let ((nnheader-callback-function
141 (gnus-make-async-article-function
142 group article mark summary next))
143 (nntp-server-buffer (get-buffer
144 gnus-async-prefetch-article-buffer)))
145 (gnus-message 7 "Prefetching article %d in group %s"
147 (gnus-request-article article group))))))))
149 (defun gnus-make-async-article-function (group article mark summary next)
150 "Return a callback function."
153 (gnus-async-set-buffer)
154 (gnus-async-get-semaphore 'gnus-async-article-semaphore)
155 (push (list ',(intern (format "%s-%d" group article))
156 ,mark (set-marker (make-marker)
159 gnus-async-article-alist)
160 (gnus-async-release-semaphore
161 'gnus-async-article-semaphore)
162 (when (gnus-buffer-live-p ,summary)
163 (gnus-async-prefetch-article
164 ,group ,next ,summary t)))))
166 (defun gnus-async-request-fetched-article (group article buffer)
167 "See whether we have ARTICLE from GROUP and put it in BUFFER."
168 (when (numberp article)
169 (let ((entry (gnus-async-prefetched-article-entry group article)))
172 (gnus-async-set-buffer)
173 (copy-to-buffer buffer (cadr entry) (caddr entry))
174 ;; Remove the read article from the prefetch buffer.
175 (when (memq 'read gnus-prefetched-article-deletion-strategy)
176 (gnus-async-delete-prefected-entry entry))
179 (defun gnus-async-delete-prefected-entry (entry)
180 "Delete ENTRY from buffer and alist."
181 (delete-region (cadr entry) (caddr entry))
182 (set-marker (cadr entry) nil)
183 (set-marker (caddr entry) nil)
184 (gnus-async-get-semaphore 'gnus-async-article-semaphore)
185 (setq gnus-async-article-alist
186 (delq entry gnus-async-article-alist))
187 (gnus-async-release-semaphore 'gnus-async-article-semaphore))
189 (defun gnus-async-prefetch-remove-group (group)
190 "Remove all articles belonging to GROUP from the prefetch buffer."
191 (when (and (gnus-group-asynchronous-p group)
192 (memq 'exit gnus-prefetched-article-deletion-strategy))
193 (let ((alist gnus-async-article-alist))
195 (gnus-async-set-buffer)
197 (when (equal group (nth 3 (car alist)))
198 (gnus-async-delete-prefected-entry (car alist)))
201 (defun gnus-async-prefetched-article-entry (group article)
202 "Return the entry for ARTICLE in GROUP iff it has been prefetched."
203 (assq (intern (format "%s-%d" group article))
204 gnus-async-article-alist))
210 (defun gnus-async-prefetch-headers (group)
211 "Prefetch the headers for group GROUP."
214 (when (and gnus-use-header-prefetch
215 (gnus-group-asynchronous-p group)
216 (listp gnus-async-header-prefetched)
217 (setq unread (gnus-list-of-unread-articles group)))
218 ;; Mark that a fetch is in progress.
219 (setq gnus-async-header-prefetched t)
220 (nnheader-set-temp-buffer gnus-async-prefetch-headers-buffer t)
222 (let ((nntp-server-buffer (current-buffer))
223 (nnheader-callback-function
225 (setq gnus-async-header-prefetched
226 ,(cons group unread)))))
227 (gnus-retrieve-headers unread group gnus-fetch-old-headers))))))
229 (defun gnus-async-retrieve-fetched-headers (articles group)
230 "See whether we have prefetched headers."
231 (when (and gnus-use-header-prefetch
232 (gnus-group-asynchronous-p group)
233 (listp gnus-async-header-prefetched)
234 (equal group (car gnus-async-header-prefetched))
235 (equal articles (cdr gnus-async-header-prefetched)))
237 (nnheader-set-temp-buffer gnus-async-prefetch-headers-buffer t)
239 (copy-to-buffer nntp-server-buffer (point-min) (point-max))
241 (setq gnus-async-header-prefetched nil)
244 (provide 'gnus-async)
246 ;;; gnus-async.el ends here