1 ;;; gnus-async.el --- asynchronous support for Gnus
2 ;; Copyright (C) 1996,97,98,99 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))
33 (defgroup gnus-asynchronous nil
34 "Support for asynchronous operations."
37 (defcustom gnus-asynchronous t
38 "*If nil, inhibit all Gnus asynchronicity.
39 If non-nil, let the other asynch variables be heeded."
40 :group 'gnus-asynchronous
43 (defcustom gnus-use-article-prefetch 30
44 "*If non-nil, prefetch articles in groups that allow this.
45 If a number, prefetch only that many articles forward;
46 if t, prefetch as many articles as possible."
47 :group 'gnus-asynchronous
48 :type '(choice (const :tag "off" nil)
50 (integer :tag "some" 0)))
52 (defcustom gnus-prefetched-article-deletion-strategy '(read exit)
53 "List of symbols that say when to remove articles from the prefetch buffer.
54 Possible values in this list are `read', which means that
55 articles are removed as they are read, and `exit', which means
56 that all articles belonging to a group are removed on exit
58 :group 'gnus-asynchronous
59 :type '(set (const read) (const exit)))
61 (defcustom gnus-use-header-prefetch nil
62 "*If non-nil, prefetch the headers to the next group."
63 :group 'gnus-asynchronous
66 (defcustom gnus-async-prefetch-article-p 'gnus-async-unread-p
67 "Function called to say whether an article should be prefetched or not.
68 The function is called with one parameter -- the article data.
69 It should return non-nil if the article is to be prefetched."
70 :group 'gnus-asynchronous
73 ;;; Internal variables.
75 (defvar gnus-async-prefetch-article-buffer " *Async Prefetch Article*")
76 (defvar gnus-async-article-alist nil)
77 (defvar gnus-async-article-semaphore '(nil))
78 (defvar gnus-async-fetch-list nil)
79 (defvar gnus-async-hashtb nil)
80 (defvar gnus-async-current-prefetch-group nil)
81 (defvar gnus-async-current-prefetch-article nil)
83 (defvar gnus-async-prefetch-headers-buffer " *Async Prefetch Headers*")
84 (defvar gnus-async-header-prefetched nil)
86 ;;; Utility functions.
88 (defun gnus-group-asynchronous-p (group)
89 "Say whether GROUP is fetched from a server that supports asynchronicity."
90 (gnus-asynchronous-p (gnus-find-method-for-group group)))
92 ;;; Somewhat bogus semaphores.
94 (defun gnus-async-get-semaphore (semaphore)
95 "Wait until SEMAPHORE is released."
96 (while (/= (length (nconc (symbol-value semaphore) (list nil))) 2)
99 (defun gnus-async-release-semaphore (semaphore)
101 (setcdr (symbol-value semaphore) nil))
103 (defmacro gnus-async-with-semaphore (&rest forms)
106 (gnus-async-get-semaphore 'gnus-async-article-semaphore)
108 (gnus-async-release-semaphore 'gnus-async-article-semaphore)))
110 (put 'gnus-async-with-semaphore 'lisp-indent-function 0)
111 (put 'gnus-async-with-semaphore 'edebug-form-spec '(body))
117 (gnus-add-shutdown 'gnus-async-close 'gnus)
118 (defun gnus-async-close ()
119 (gnus-kill-buffer gnus-async-prefetch-article-buffer)
120 (gnus-kill-buffer gnus-async-prefetch-headers-buffer)
121 (setq gnus-async-hashtb nil
122 gnus-async-article-alist nil
123 gnus-async-header-prefetched nil))
125 (defun gnus-async-set-buffer ()
126 (nnheader-set-temp-buffer gnus-async-prefetch-article-buffer t)
127 (unless gnus-async-hashtb
128 (setq gnus-async-hashtb (gnus-make-hashtable 1023))))
130 (defun gnus-async-halt-prefetch ()
132 (setq gnus-async-fetch-list nil))
134 (defun gnus-async-prefetch-next (group article summary)
135 "Possibly prefetch several articles starting with the article after ARTICLE."
136 (when (and (gnus-buffer-live-p summary)
138 (gnus-group-asynchronous-p group))
140 (set-buffer gnus-summary-buffer)
141 (let ((next (caadr (gnus-data-find-list article))))
143 (if (not (fboundp 'run-with-idle-timer))
144 ;; This is either an older Emacs or XEmacs, so we
145 ;; do this, which leads to slightly slower article
147 (gnus-async-prefetch-article group next summary)
149 0.1 nil 'gnus-async-prefetch-article group next summary)))))))
151 (defun gnus-async-prefetch-article (group article summary &optional next)
152 "Possibly prefetch several articles starting with ARTICLE."
153 (if (not (gnus-buffer-live-p summary))
154 (gnus-async-with-semaphore
155 (setq gnus-async-fetch-list nil))
156 (when (and gnus-asynchronous
159 (gnus-async-with-semaphore
160 (pop gnus-async-fetch-list)))
161 (let ((do-fetch next)
162 (do-message t)) ;(eq major-mode 'gnus-summary-mode)))
163 (when (and (gnus-group-asynchronous-p group)
164 (gnus-buffer-live-p summary)
166 gnus-async-fetch-list))
167 (gnus-async-with-semaphore
169 (setq do-fetch (not gnus-async-fetch-list))
170 ;; Nix out any outstanding requests.
171 (setq gnus-async-fetch-list nil)
172 ;; Fill in the new list.
173 (let ((n gnus-use-article-prefetch)
174 (data (gnus-data-find-list article))
176 (while (and (setq d (pop data))
180 (unless (or (gnus-async-prefetched-article-entry
181 group (setq article (gnus-data-number d)))
182 (not (natnump article))
183 (not (funcall gnus-async-prefetch-article-p d)))
184 ;; Not already fetched -- so we add it to the list.
185 (push article gnus-async-fetch-list)))
186 (setq gnus-async-fetch-list
187 (nreverse gnus-async-fetch-list))))
190 (setq article (car gnus-async-fetch-list))))
192 (when (and do-fetch article)
193 ;; We want to fetch some more articles.
197 (gnus-async-set-buffer)
198 (goto-char (point-max))
199 (setq mark (point-marker))
200 (let ((nnheader-callback-function
201 (gnus-make-async-article-function
202 group article mark summary next))
204 (get-buffer gnus-async-prefetch-article-buffer)))
206 (gnus-message 9 "Prefetching article %d in group %s"
208 (setq gnus-async-current-prefetch-group group)
209 (setq gnus-async-current-prefetch-article article)
210 (gnus-request-article article group))))))))))
212 (defun gnus-make-async-article-function (group article mark summary next)
213 "Return a callback function."
215 (gnus-async-article-callback arg ,group ,article ,mark ,summary ,next)))
217 (defun gnus-async-article-callback (arg group article mark summary next)
218 "Function called when an async article is done being fetched."
220 (setq gnus-async-current-prefetch-article nil)
222 (gnus-async-set-buffer)
223 (gnus-async-with-semaphore
225 gnus-async-article-alist
226 (cons (list (intern (format "%s-%d" group article)
228 mark (set-marker (make-marker) (point-max))
230 gnus-async-article-alist))))
231 (if (not (gnus-buffer-live-p summary))
232 (gnus-async-with-semaphore
233 (setq gnus-async-fetch-list nil))
234 (gnus-async-prefetch-article group next summary t))))
236 (defun gnus-async-unread-p (data)
237 "Return non-nil if DATA represents an unread article."
238 (gnus-data-unread-p data))
240 (defun gnus-async-request-fetched-article (group article buffer)
241 "See whether we have ARTICLE from GROUP and put it in BUFFER."
242 (when (numberp article)
243 (when (and (equal group gnus-async-current-prefetch-group)
244 (eq article gnus-async-current-prefetch-article))
245 (gnus-async-wait-for-article article))
246 (let ((entry (gnus-async-prefetched-article-entry group article)))
249 (gnus-async-set-buffer)
250 (copy-to-buffer buffer (cadr entry) (caddr entry))
251 ;; Remove the read article from the prefetch buffer.
252 (when (memq 'read gnus-prefetched-article-deletion-strategy)
253 (gnus-async-delete-prefetched-entry entry))
256 (defun gnus-async-wait-for-article (article)
257 "Wait until ARTICLE is no longer the currently-being-fetched article."
259 (gnus-async-set-buffer)
260 (let ((proc (nntp-find-connection (current-buffer)))
261 (nntp-server-buffer (current-buffer))
262 (nntp-have-messaged nil)
265 ;; FIXME: we could stop waiting after some
266 ;; timeout, but this is the wrong place to do it.
267 ;; rather than checking time-spent-waiting, we
268 ;; should check time-since-last-output, which
269 ;; needs to be done in nntp.el.
270 (while (eq article gnus-async-current-prefetch-article)
272 (when (nntp-accept-process-output proc 1)
274 (when (and (not nntp-have-messaged) (eq 3 tries))
275 (gnus-message 5 "Waiting for async article...")
276 (setq nntp-have-messaged t)))
278 ;; if the user interrupted on a slow/hung connection,
279 ;; do something friendly.
281 (setq gnus-async-current-prefetch-article nil))
283 (when nntp-have-messaged
284 (gnus-message 5 "")))))
286 (defun gnus-async-delete-prefetched-entry (entry)
287 "Delete ENTRY from buffer and alist."
289 (delete-region (cadr entry) (caddr entry))
290 (set-marker (cadr entry) nil)
291 (set-marker (caddr entry) nil))
292 (gnus-async-with-semaphore
293 (setq gnus-async-article-alist
294 (delq entry gnus-async-article-alist))))
296 (defun gnus-async-prefetch-remove-group (group)
297 "Remove all articles belonging to GROUP from the prefetch buffer."
298 (when (and (gnus-group-asynchronous-p group)
299 (memq 'exit gnus-prefetched-article-deletion-strategy))
300 (let ((alist gnus-async-article-alist))
302 (gnus-async-set-buffer)
304 (when (equal group (nth 3 (car alist)))
305 (gnus-async-delete-prefetched-entry (car alist)))
308 (defun gnus-async-prefetched-article-entry (group article)
309 "Return the entry for ARTICLE in GROUP iff it has been prefetched."
310 (let ((entry (save-excursion
311 (gnus-async-set-buffer)
312 (assq (intern (format "%s-%d" group article)
314 gnus-async-article-alist))))
315 ;; Perhaps something has emptied the buffer?
317 (= (cadr entry) (caddr entry)))
320 (set-marker (cadr entry) nil)
321 (set-marker (caddr entry) nil))
322 (setq gnus-async-article-alist
323 (delq entry gnus-async-article-alist))
331 (defun gnus-async-prefetch-headers (group)
332 "Prefetch the headers for group GROUP."
335 (when (and gnus-use-header-prefetch
337 (gnus-group-asynchronous-p group)
338 (listp gnus-async-header-prefetched)
339 (setq unread (gnus-list-of-unread-articles group)))
340 ;; Mark that a fetch is in progress.
341 (setq gnus-async-header-prefetched t)
342 (nnheader-set-temp-buffer gnus-async-prefetch-headers-buffer t)
344 (let ((nntp-server-buffer (current-buffer))
345 (nnheader-callback-function
347 (setq gnus-async-header-prefetched
348 ,(cons group unread)))))
349 (gnus-retrieve-headers unread group gnus-fetch-old-headers))))))
351 (defun gnus-async-retrieve-fetched-headers (articles group)
352 "See whether we have prefetched headers."
353 (when (and gnus-use-header-prefetch
354 (gnus-group-asynchronous-p group)
355 (listp gnus-async-header-prefetched)
356 (equal group (car gnus-async-header-prefetched))
357 (equal articles (cdr gnus-async-header-prefetched)))
359 (nnheader-set-temp-buffer gnus-async-prefetch-headers-buffer t)
361 (copy-to-buffer nntp-server-buffer (point-min) (point-max))
363 (setq gnus-async-header-prefetched nil)
366 (provide 'gnus-async)
368 ;;; gnus-async.el ends here