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))
34 (defgroup gnus-asynchronous nil
35 "Support for asynchronous operations."
38 (defcustom gnus-asynchronous t
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)
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)
150 0.1 nil 'gnus-async-prefetch-article group next summary)))))))
152 (defun gnus-async-prefetch-article (group article summary &optional next)
153 "Possibly prefetch several articles starting with ARTICLE."
154 (if (not (gnus-buffer-live-p summary))
155 (gnus-async-with-semaphore
156 (setq gnus-async-fetch-list nil))
157 (when (and gnus-asynchronous
160 (gnus-async-with-semaphore
161 (pop gnus-async-fetch-list)))
162 (let ((do-fetch next)
163 (do-message t)) ;(eq major-mode 'gnus-summary-mode)))
164 (when (and (gnus-group-asynchronous-p group)
165 (gnus-buffer-live-p summary)
167 gnus-async-fetch-list))
168 (gnus-async-with-semaphore
170 (setq do-fetch (not gnus-async-fetch-list))
171 ;; Nix out any outstanding requests.
172 (setq gnus-async-fetch-list nil)
173 ;; Fill in the new list.
174 (let ((n gnus-use-article-prefetch)
175 (data (gnus-data-find-list article))
177 (while (and (setq d (pop data))
181 (unless (or (gnus-async-prefetched-article-entry
182 group (setq article (gnus-data-number d)))
183 (not (natnump article))
184 (not (funcall gnus-async-prefetch-article-p d)))
185 ;; Not already fetched -- so we add it to the list.
186 (push article gnus-async-fetch-list)))
187 (setq gnus-async-fetch-list
188 (nreverse gnus-async-fetch-list))))
191 (setq article (car gnus-async-fetch-list))))
193 (when (and do-fetch article)
194 ;; We want to fetch some more articles.
198 (gnus-async-set-buffer)
199 (goto-char (point-max))
200 (setq mark (point-marker))
201 (let ((nnheader-callback-function
202 (gnus-make-async-article-function
203 group article mark summary next))
205 (get-buffer gnus-async-prefetch-article-buffer)))
207 (gnus-message 9 "Prefetching article %d in group %s"
209 (setq gnus-async-current-prefetch-group group)
210 (setq gnus-async-current-prefetch-article article)
211 (gnus-request-article article group))))))))))
213 (defun gnus-make-async-article-function (group article mark summary next)
214 "Return a callback function."
216 (gnus-async-article-callback arg ,group ,article ,mark ,summary ,next)))
218 (defun gnus-async-article-callback (arg group article mark summary next)
219 "Function called when an async article is done being fetched."
221 (setq gnus-async-current-prefetch-article nil)
223 (gnus-async-set-buffer)
224 (gnus-async-with-semaphore
226 gnus-async-article-alist
227 (cons (list (intern (format "%s-%d" group article)
229 mark (set-marker (make-marker) (point-max))
231 gnus-async-article-alist))))
232 (if (not (gnus-buffer-live-p summary))
233 (gnus-async-with-semaphore
234 (setq gnus-async-fetch-list nil))
235 (gnus-async-prefetch-article group next summary t))))
237 (defun gnus-async-unread-p (data)
238 "Return non-nil if DATA represents an unread article."
239 (gnus-data-unread-p data))
241 (defun gnus-async-request-fetched-article (group article buffer)
242 "See whether we have ARTICLE from GROUP and put it in BUFFER."
243 (when (numberp article)
244 (when (and (equal group gnus-async-current-prefetch-group)
245 (eq article gnus-async-current-prefetch-article))
246 (gnus-async-wait-for-article article))
247 (let ((entry (gnus-async-prefetched-article-entry group article)))
250 (gnus-async-set-buffer)
251 (copy-to-buffer buffer (cadr entry) (caddr entry))
252 ;; Remove the read article from the prefetch buffer.
253 (when (memq 'read gnus-prefetched-article-deletion-strategy)
254 (gnus-async-delete-prefetched-entry entry))
257 (defun gnus-async-wait-for-article (article)
258 "Wait until ARTICLE is no longer the currently-being-fetched article."
260 (gnus-async-set-buffer)
261 (let ((proc (nntp-find-connection (current-buffer)))
262 (nntp-server-buffer (current-buffer))
263 (nntp-have-messaged nil)
266 ;; FIXME: we could stop waiting after some
267 ;; timeout, but this is the wrong place to do it.
268 ;; rather than checking time-spent-waiting, we
269 ;; should check time-since-last-output, which
270 ;; needs to be done in nntp.el.
271 (while (eq article gnus-async-current-prefetch-article)
273 (when (nntp-accept-process-output proc 1)
275 (when (and (not nntp-have-messaged) (eq 3 tries))
276 (gnus-message 5 "Waiting for async article...")
277 (setq nntp-have-messaged t)))
279 ;; if the user interrupted on a slow/hung connection,
280 ;; do something friendly.
282 (setq gnus-async-current-prefetch-article nil))
284 (when nntp-have-messaged
285 (gnus-message 5 "")))))
287 (defun gnus-async-delete-prefetched-entry (entry)
288 "Delete ENTRY from buffer and alist."
290 (delete-region (cadr entry) (caddr entry))
291 (set-marker (cadr entry) nil)
292 (set-marker (caddr entry) nil))
293 (gnus-async-with-semaphore
294 (setq gnus-async-article-alist
295 (delq entry gnus-async-article-alist))))
297 (defun gnus-async-prefetch-remove-group (group)
298 "Remove all articles belonging to GROUP from the prefetch buffer."
299 (when (and (gnus-group-asynchronous-p group)
300 (memq 'exit gnus-prefetched-article-deletion-strategy))
301 (let ((alist gnus-async-article-alist))
303 (gnus-async-set-buffer)
305 (when (equal group (nth 3 (car alist)))
306 (gnus-async-delete-prefetched-entry (car alist)))
309 (defun gnus-async-prefetched-article-entry (group article)
310 "Return the entry for ARTICLE in GROUP iff it has been prefetched."
311 (let ((entry (save-excursion
312 (gnus-async-set-buffer)
313 (assq (intern (format "%s-%d" group article)
315 gnus-async-article-alist))))
316 ;; Perhaps something has emptied the buffer?
318 (= (cadr entry) (caddr entry)))
321 (set-marker (cadr entry) nil)
322 (set-marker (caddr entry) nil))
323 (setq gnus-async-article-alist
324 (delq entry gnus-async-article-alist))
332 (defun gnus-async-prefetch-headers (group)
333 "Prefetch the headers for group GROUP."
336 (when (and gnus-use-header-prefetch
338 (gnus-group-asynchronous-p group)
339 (listp gnus-async-header-prefetched)
340 (setq unread (gnus-list-of-unread-articles group)))
341 ;; Mark that a fetch is in progress.
342 (setq gnus-async-header-prefetched t)
343 (nnheader-set-temp-buffer gnus-async-prefetch-headers-buffer t)
345 (let ((nntp-server-buffer (current-buffer))
346 (nnheader-callback-function
348 (setq gnus-async-header-prefetched
349 ,(cons group unread)))))
350 (gnus-retrieve-headers unread group gnus-fetch-old-headers))))))
352 (defun gnus-async-retrieve-fetched-headers (articles group)
353 "See whether we have prefetched headers."
354 (when (and gnus-use-header-prefetch
355 (gnus-group-asynchronous-p group)
356 (listp gnus-async-header-prefetched)
357 (equal group (car gnus-async-header-prefetched))
358 (equal articles (cdr gnus-async-header-prefetched)))
360 (nnheader-set-temp-buffer gnus-async-prefetch-headers-buffer t)
362 (copy-to-buffer nntp-server-buffer (point-min) (point-max))
364 (setq gnus-async-header-prefetched nil)
367 (provide 'gnus-async)
369 ;;; gnus-async.el ends here