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)
82 (defvar gnus-async-timer nil)
84 (defvar gnus-async-prefetch-headers-buffer " *Async Prefetch Headers*")
85 (defvar gnus-async-header-prefetched nil)
87 ;;; Utility functions.
89 (defun gnus-group-asynchronous-p (group)
90 "Say whether GROUP is fetched from a server that supports asynchronicity."
91 (gnus-asynchronous-p (gnus-find-method-for-group group)))
93 ;;; Somewhat bogus semaphores.
95 (defun gnus-async-get-semaphore (semaphore)
96 "Wait until SEMAPHORE is released."
97 (while (/= (length (nconc (symbol-value semaphore) (list nil))) 2)
100 (defun gnus-async-release-semaphore (semaphore)
102 (setcdr (symbol-value semaphore) nil))
104 (defmacro gnus-async-with-semaphore (&rest forms)
107 (gnus-async-get-semaphore 'gnus-async-article-semaphore)
109 (gnus-async-release-semaphore 'gnus-async-article-semaphore)))
111 (put 'gnus-async-with-semaphore 'lisp-indent-function 0)
112 (put 'gnus-async-with-semaphore 'edebug-form-spec '(body))
118 (gnus-add-shutdown 'gnus-async-close 'gnus)
119 (defun gnus-async-close ()
120 (gnus-kill-buffer gnus-async-prefetch-article-buffer)
121 (gnus-kill-buffer gnus-async-prefetch-headers-buffer)
122 (setq gnus-async-hashtb nil
123 gnus-async-article-alist nil
124 gnus-async-header-prefetched nil))
126 (defun gnus-async-set-buffer ()
127 (nnheader-set-temp-buffer gnus-async-prefetch-article-buffer t)
128 (unless gnus-async-hashtb
129 (setq gnus-async-hashtb (gnus-make-hashtable 1023))))
131 (defun gnus-async-halt-prefetch ()
133 (setq gnus-async-fetch-list nil))
135 (defun gnus-async-prefetch-next (group article summary)
136 "Possibly prefetch several articles starting with the article after ARTICLE."
137 (when (and (gnus-buffer-live-p summary)
139 (gnus-group-asynchronous-p group))
141 (set-buffer gnus-summary-buffer)
142 (let ((next (caadr (gnus-data-find-list article))))
144 (if (not (fboundp 'run-with-idle-timer))
145 ;; This is either an older Emacs or XEmacs, so we
146 ;; do this, which leads to slightly slower article
148 (gnus-async-prefetch-article group next summary)
149 (when gnus-async-timer
151 (nnheader-cancel-timer 'gnus-async-timer)))
152 (setq gnus-async-timer
154 0.1 nil 'gnus-async-prefetch-article
155 group next summary))))))))
157 (defun gnus-async-prefetch-article (group article summary &optional next)
158 "Possibly prefetch several articles starting with ARTICLE."
159 (if (not (gnus-buffer-live-p summary))
160 (gnus-async-with-semaphore
161 (setq gnus-async-fetch-list nil))
162 (when (and gnus-asynchronous
165 (gnus-async-with-semaphore
166 (pop gnus-async-fetch-list)))
167 (let ((do-fetch next)
168 (do-message t)) ;(eq major-mode 'gnus-summary-mode)))
169 (when (and (gnus-group-asynchronous-p group)
170 (gnus-buffer-live-p summary)
172 gnus-async-fetch-list))
173 (gnus-async-with-semaphore
175 (setq do-fetch (not gnus-async-fetch-list))
176 ;; Nix out any outstanding requests.
177 (setq gnus-async-fetch-list nil)
178 ;; Fill in the new list.
179 (let ((n gnus-use-article-prefetch)
180 (data (gnus-data-find-list article))
182 (while (and (setq d (pop data))
186 (unless (or (gnus-async-prefetched-article-entry
187 group (setq article (gnus-data-number d)))
188 (not (natnump article))
189 (not (funcall gnus-async-prefetch-article-p d)))
190 ;; Not already fetched -- so we add it to the list.
191 (push article gnus-async-fetch-list)))
192 (setq gnus-async-fetch-list
193 (nreverse gnus-async-fetch-list))))
196 (setq article (car gnus-async-fetch-list))))
198 (when (and do-fetch article)
199 ;; We want to fetch some more articles.
203 (gnus-async-set-buffer)
204 (goto-char (point-max))
205 (setq mark (point-marker))
206 (let ((nnheader-callback-function
207 (gnus-make-async-article-function
208 group article mark summary next))
210 (get-buffer gnus-async-prefetch-article-buffer)))
212 (gnus-message 9 "Prefetching article %d in group %s"
214 (setq gnus-async-current-prefetch-group group)
215 (setq gnus-async-current-prefetch-article article)
216 (gnus-request-article article group))))))))))
218 (defun gnus-make-async-article-function (group article mark summary next)
219 "Return a callback function."
221 (gnus-async-article-callback arg ,group ,article ,mark ,summary ,next)))
223 (defun gnus-async-article-callback (arg group article mark summary next)
224 "Function called when an async article is done being fetched."
226 (setq gnus-async-current-prefetch-article nil)
228 (gnus-async-set-buffer)
229 (gnus-async-with-semaphore
231 gnus-async-article-alist
232 (cons (list (intern (format "%s-%d" group article)
234 mark (set-marker (make-marker) (point-max))
236 gnus-async-article-alist))))
237 (if (not (gnus-buffer-live-p summary))
238 (gnus-async-with-semaphore
239 (setq gnus-async-fetch-list nil))
240 (gnus-async-prefetch-article group next summary t))))
242 (defun gnus-async-unread-p (data)
243 "Return non-nil if DATA represents an unread article."
244 (gnus-data-unread-p data))
246 (defun gnus-async-request-fetched-article (group article buffer)
247 "See whether we have ARTICLE from GROUP and put it in BUFFER."
248 (when (numberp article)
249 (when (and (equal group gnus-async-current-prefetch-group)
250 (eq article gnus-async-current-prefetch-article))
251 (gnus-async-wait-for-article article))
252 (let ((entry (gnus-async-prefetched-article-entry group article)))
255 (gnus-async-set-buffer)
256 (copy-to-buffer buffer (cadr entry) (caddr entry))
257 ;; Remove the read article from the prefetch buffer.
258 (when (memq 'read gnus-prefetched-article-deletion-strategy)
259 (gnus-async-delete-prefetched-entry entry))
262 (defun gnus-async-wait-for-article (article)
263 "Wait until ARTICLE is no longer the currently-being-fetched article."
265 (gnus-async-set-buffer)
266 (let ((proc (nntp-find-connection (current-buffer)))
267 (nntp-server-buffer (current-buffer))
268 (nntp-have-messaged nil)
271 ;; FIXME: we could stop waiting after some
272 ;; timeout, but this is the wrong place to do it.
273 ;; rather than checking time-spent-waiting, we
274 ;; should check time-since-last-output, which
275 ;; needs to be done in nntp.el.
276 (while (eq article gnus-async-current-prefetch-article)
278 (when (nntp-accept-process-output proc 1)
280 (when (and (not nntp-have-messaged) (eq 3 tries))
281 (gnus-message 5 "Waiting for async article...")
282 (setq nntp-have-messaged t)))
284 ;; if the user interrupted on a slow/hung connection,
285 ;; do something friendly.
287 (setq gnus-async-current-prefetch-article nil))
289 (when nntp-have-messaged
290 (gnus-message 5 "")))))
292 (defun gnus-async-delete-prefetched-entry (entry)
293 "Delete ENTRY from buffer and alist."
295 (delete-region (cadr entry) (caddr entry))
296 (set-marker (cadr entry) nil)
297 (set-marker (caddr entry) nil))
298 (gnus-async-with-semaphore
299 (setq gnus-async-article-alist
300 (delq entry gnus-async-article-alist))))
302 (defun gnus-async-prefetch-remove-group (group)
303 "Remove all articles belonging to GROUP from the prefetch buffer."
304 (when (and (gnus-group-asynchronous-p group)
305 (memq 'exit gnus-prefetched-article-deletion-strategy))
306 (let ((alist gnus-async-article-alist))
308 (gnus-async-set-buffer)
310 (when (equal group (nth 3 (car alist)))
311 (gnus-async-delete-prefetched-entry (car alist)))
314 (defun gnus-async-prefetched-article-entry (group article)
315 "Return the entry for ARTICLE in GROUP iff it has been prefetched."
316 (let ((entry (save-excursion
317 (gnus-async-set-buffer)
318 (assq (intern (format "%s-%d" group article)
320 gnus-async-article-alist))))
321 ;; Perhaps something has emptied the buffer?
323 (= (cadr entry) (caddr entry)))
326 (set-marker (cadr entry) nil)
327 (set-marker (caddr entry) nil))
328 (setq gnus-async-article-alist
329 (delq entry gnus-async-article-alist))
337 (defun gnus-async-prefetch-headers (group)
338 "Prefetch the headers for group GROUP."
341 (when (and gnus-use-header-prefetch
343 (gnus-group-asynchronous-p group)
344 (listp gnus-async-header-prefetched)
345 (setq unread (gnus-list-of-unread-articles group)))
346 ;; Mark that a fetch is in progress.
347 (setq gnus-async-header-prefetched t)
348 (nnheader-set-temp-buffer gnus-async-prefetch-headers-buffer t)
350 (let ((nntp-server-buffer (current-buffer))
351 (nnheader-callback-function
353 (setq gnus-async-header-prefetched
354 ,(cons group unread)))))
355 (gnus-retrieve-headers unread group gnus-fetch-old-headers))))))
357 (defun gnus-async-retrieve-fetched-headers (articles group)
358 "See whether we have prefetched headers."
359 (when (and gnus-use-header-prefetch
360 (gnus-group-asynchronous-p group)
361 (listp gnus-async-header-prefetched)
362 (equal group (car gnus-async-header-prefetched))
363 (equal articles (cdr gnus-async-header-prefetched)))
365 (nnheader-set-temp-buffer gnus-async-prefetch-headers-buffer t)
367 (copy-to-buffer nntp-server-buffer (point-min) (point-max))
369 (setq gnus-async-header-prefetched nil)
372 (provide 'gnus-async)
374 ;;; gnus-async.el ends here