1 ;;; gnus-async.el --- asynchronous support for Gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
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.
28 (eval-when-compile (require 'cl))
34 (defgroup gnus-asynchronous nil
35 "Support for asynchronous operations."
38 (defcustom gnus-asynchronous nil
39 "*If nil, inhibit all Gnus asynchronicity.
40 If non-nil, let the other asynch variables be heeded."
41 :group 'gnus-asynchronous
44 (defcustom gnus-use-article-prefetch 30
45 "*If non-nil, prefetch articles in groups that allow this.
46 If a number, prefetch only that many articles forward;
47 if t, prefetch as many articles as possible."
48 :group 'gnus-asynchronous
49 :type '(choice (const :tag "off" nil)
51 (integer :tag "some" 0)))
53 (defcustom gnus-prefetched-article-deletion-strategy '(read exit)
54 "List of symbols that say when to remove articles from the prefetch buffer.
55 Possible values in this list are `read', which means that
56 articles are removed as they are read, and `exit', which means
57 that all articles belonging to a group are removed on exit
59 :group 'gnus-asynchronous
60 :type '(set (const read) (const exit)))
62 (defcustom gnus-use-header-prefetch nil
63 "*If non-nil, prefetch the headers to the next group."
64 :group 'gnus-asynchronous
67 (defcustom gnus-async-prefetch-article-p 'gnus-async-unread-p
68 "Function called to say whether an article should be prefetched or not.
69 The function is called with one parameter -- the article data.
70 It should return non-nil if the article is to be prefetched."
71 :group 'gnus-asynchronous
74 ;;; Internal variables.
76 (defvar gnus-async-prefetch-article-buffer " *Async Prefetch Article*")
77 (defvar gnus-async-article-alist nil)
78 (defvar gnus-async-article-semaphore '(nil))
79 (defvar gnus-async-fetch-list nil)
80 (defvar gnus-async-hashtb nil)
81 (defvar gnus-async-current-prefetch-group nil)
82 (defvar gnus-async-current-prefetch-article nil)
83 (defvar gnus-async-timer nil)
85 (defvar gnus-async-prefetch-headers-buffer " *Async Prefetch Headers*")
86 (defvar gnus-async-header-prefetched nil)
88 ;;; Utility functions.
90 (defun gnus-group-asynchronous-p (group)
91 "Say whether GROUP is fetched from a server that supports asynchronicity."
92 (gnus-asynchronous-p (gnus-find-method-for-group group)))
94 ;;; Somewhat bogus semaphores.
96 (defun gnus-async-get-semaphore (semaphore)
97 "Wait until SEMAPHORE is released."
98 (while (/= (length (nconc (symbol-value semaphore) (list nil))) 2)
101 (defun gnus-async-release-semaphore (semaphore)
103 (setcdr (symbol-value semaphore) nil))
105 (defmacro gnus-async-with-semaphore (&rest forms)
108 (gnus-async-get-semaphore 'gnus-async-article-semaphore)
110 (gnus-async-release-semaphore 'gnus-async-article-semaphore)))
112 (put 'gnus-async-with-semaphore 'lisp-indent-function 0)
113 (put 'gnus-async-with-semaphore 'edebug-form-spec '(body))
119 (gnus-add-shutdown 'gnus-async-close 'gnus)
120 (defun gnus-async-close ()
121 (gnus-kill-buffer gnus-async-prefetch-article-buffer)
122 (gnus-kill-buffer gnus-async-prefetch-headers-buffer)
123 (setq gnus-async-hashtb nil
124 gnus-async-article-alist nil
125 gnus-async-header-prefetched nil))
127 (defun gnus-async-set-buffer ()
128 (nnheader-set-temp-buffer gnus-async-prefetch-article-buffer t)
129 (unless gnus-async-hashtb
130 (setq gnus-async-hashtb (gnus-make-hashtable 1023))))
132 (defun gnus-async-halt-prefetch ()
134 (setq gnus-async-fetch-list nil))
136 (defun gnus-async-prefetch-next (group article summary)
137 "Possibly prefetch several articles starting with the article after ARTICLE."
138 (when (and (gnus-buffer-live-p summary)
140 (gnus-group-asynchronous-p group))
142 (set-buffer gnus-summary-buffer)
143 (let ((next (caadr (gnus-data-find-list article))))
145 (if (not (fboundp 'run-with-idle-timer))
146 ;; This is either an older Emacs or XEmacs, so we
147 ;; do this, which leads to slightly slower article
149 (gnus-async-prefetch-article group next summary)
150 (when gnus-async-timer
152 (nnheader-cancel-timer 'gnus-async-timer)))
153 (setq gnus-async-timer
155 0.1 nil 'gnus-async-prefetch-article
156 group next summary))))))))
158 (defun gnus-async-prefetch-article (group article summary &optional next)
159 "Possibly prefetch several articles starting with ARTICLE."
160 (if (not (gnus-buffer-live-p summary))
161 (gnus-async-with-semaphore
162 (setq gnus-async-fetch-list nil))
163 (when (and gnus-asynchronous
166 (gnus-async-with-semaphore
167 (pop gnus-async-fetch-list)))
168 (let ((do-fetch next)
169 (do-message t)) ;(eq major-mode 'gnus-summary-mode)))
170 (when (and (gnus-group-asynchronous-p group)
171 (gnus-buffer-live-p summary)
173 gnus-async-fetch-list))
174 (gnus-async-with-semaphore
176 (setq do-fetch (not gnus-async-fetch-list))
177 ;; Nix out any outstanding requests.
178 (setq gnus-async-fetch-list nil)
179 ;; Fill in the new list.
180 (let ((n gnus-use-article-prefetch)
181 (data (gnus-data-find-list article))
183 (while (and (setq d (pop data))
187 (unless (or (gnus-async-prefetched-article-entry
188 group (setq article (gnus-data-number d)))
189 (not (natnump article))
190 (not (funcall gnus-async-prefetch-article-p d)))
191 ;; Not already fetched -- so we add it to the list.
192 (push article gnus-async-fetch-list)))
193 (setq gnus-async-fetch-list
194 (nreverse gnus-async-fetch-list))))
197 (setq article (car gnus-async-fetch-list))))
199 (when (and do-fetch article)
200 ;; We want to fetch some more articles.
204 (gnus-async-set-buffer)
205 (goto-char (point-max))
206 (setq mark (point-marker))
207 (let ((nnheader-callback-function
208 (gnus-make-async-article-function
209 group article mark summary next))
211 (get-buffer gnus-async-prefetch-article-buffer)))
213 (gnus-message 9 "Prefetching article %d in group %s"
215 (setq gnus-async-current-prefetch-group group)
216 (setq gnus-async-current-prefetch-article article)
217 (gnus-request-article article group))))))))))
219 (defun gnus-make-async-article-function (group article mark summary next)
220 "Return a callback function."
222 (gnus-async-article-callback arg ,group ,article ,mark ,summary ,next)))
224 (defun gnus-async-article-callback (arg group article mark summary next)
225 "Function called when an async article is done being fetched."
227 (setq gnus-async-current-prefetch-article nil)
229 (gnus-async-set-buffer)
230 (gnus-async-with-semaphore
232 gnus-async-article-alist
233 (cons (list (intern (format "%s-%d" group article)
235 mark (set-marker (make-marker) (point-max))
237 gnus-async-article-alist))))
238 (if (not (gnus-buffer-live-p summary))
239 (gnus-async-with-semaphore
240 (setq gnus-async-fetch-list nil))
241 (gnus-async-prefetch-article group next summary t))))
243 (defun gnus-async-unread-p (data)
244 "Return non-nil if DATA represents an unread article."
245 (gnus-data-unread-p data))
247 (defun gnus-async-request-fetched-article (group article buffer)
248 "See whether we have ARTICLE from GROUP and put it in BUFFER."
249 (when (numberp article)
250 (when (and (equal group gnus-async-current-prefetch-group)
251 (eq article gnus-async-current-prefetch-article))
252 (gnus-async-wait-for-article article))
253 (let ((entry (gnus-async-prefetched-article-entry group article)))
256 (gnus-async-set-buffer)
257 (copy-to-buffer buffer (cadr entry) (caddr entry))
258 ;; Remove the read article from the prefetch buffer.
259 (when (memq 'read gnus-prefetched-article-deletion-strategy)
260 (gnus-async-delete-prefetched-entry entry))
263 (defun gnus-async-wait-for-article (article)
264 "Wait until ARTICLE is no longer the currently-being-fetched article."
266 (gnus-async-set-buffer)
267 (let ((proc (nntp-find-connection (current-buffer)))
268 (nntp-server-buffer (current-buffer))
269 (nntp-have-messaged nil)
272 ;; FIXME: we could stop waiting after some
273 ;; timeout, but this is the wrong place to do it.
274 ;; rather than checking time-spent-waiting, we
275 ;; should check time-since-last-output, which
276 ;; needs to be done in nntp.el.
277 (while (eq article gnus-async-current-prefetch-article)
279 (when (nntp-accept-process-output proc 1)
281 (when (and (not nntp-have-messaged)
283 (gnus-message 5 "Waiting for async article...")
284 (setq nntp-have-messaged t)))
286 ;; if the user interrupted on a slow/hung connection,
287 ;; do something friendly.
289 (setq gnus-async-current-prefetch-article nil))
291 (when nntp-have-messaged
292 (gnus-message 5 "")))))
294 (defun gnus-async-delete-prefetched-entry (entry)
295 "Delete ENTRY from buffer and alist."
297 (delete-region (cadr entry) (caddr entry))
298 (set-marker (cadr entry) nil)
299 (set-marker (caddr entry) nil))
300 (gnus-async-with-semaphore
301 (setq gnus-async-article-alist
302 (delq entry gnus-async-article-alist))))
304 (defun gnus-async-prefetch-remove-group (group)
305 "Remove all articles belonging to GROUP from the prefetch buffer."
306 (when (and (gnus-group-asynchronous-p group)
307 (memq 'exit gnus-prefetched-article-deletion-strategy))
308 (let ((alist gnus-async-article-alist))
310 (gnus-async-set-buffer)
312 (when (equal group (nth 3 (car alist)))
313 (gnus-async-delete-prefetched-entry (car alist)))
316 (defun gnus-async-prefetched-article-entry (group article)
317 "Return the entry for ARTICLE in GROUP iff it has been prefetched."
318 (let ((entry (save-excursion
319 (gnus-async-set-buffer)
320 (assq (intern (format "%s-%d" group article)
322 gnus-async-article-alist))))
323 ;; Perhaps something has emptied the buffer?
325 (= (cadr entry) (caddr entry)))
328 (set-marker (cadr entry) nil)
329 (set-marker (caddr entry) nil))
330 (setq gnus-async-article-alist
331 (delq entry gnus-async-article-alist))
339 (defun gnus-async-prefetch-headers (group)
340 "Prefetch the headers for group GROUP."
343 (when (and gnus-use-header-prefetch
345 (gnus-group-asynchronous-p group)
346 (listp gnus-async-header-prefetched)
347 (setq unread (gnus-list-of-unread-articles group)))
348 ;; Mark that a fetch is in progress.
349 (setq gnus-async-header-prefetched t)
350 (nnheader-set-temp-buffer gnus-async-prefetch-headers-buffer t)
352 (let ((nntp-server-buffer (current-buffer))
353 (nnheader-callback-function
355 (setq gnus-async-header-prefetched
356 ,(cons group unread)))))
357 (gnus-retrieve-headers unread group gnus-fetch-old-headers))))))
359 (defun gnus-async-retrieve-fetched-headers (articles group)
360 "See whether we have prefetched headers."
361 (when (and gnus-use-header-prefetch
362 (gnus-group-asynchronous-p group)
363 (listp gnus-async-header-prefetched)
364 (equal group (car gnus-async-header-prefetched))
365 (equal articles (cdr gnus-async-header-prefetched)))
367 (nnheader-set-temp-buffer gnus-async-prefetch-headers-buffer t)
369 (copy-to-buffer nntp-server-buffer (point-min) (point-max))
371 (setq gnus-async-header-prefetched nil)
374 (provide 'gnus-async)
376 ;;; gnus-async.el ends here