1 ;;; gnus-async.el --- asynchronous support for Gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2003
3 ;; Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, USA.
29 (eval-when-compile (require 'cl))
36 (when (featurep 'xemacs)
37 (require 'timer-funcs)))
39 (defgroup gnus-asynchronous nil
40 "Support for asynchronous operations."
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-asynchronous nil
53 "*If nil, inhibit all Gnus asynchronicity.
54 If non-nil, let the other asynch variables be heeded."
55 :group 'gnus-asynchronous
58 (defcustom gnus-prefetched-article-deletion-strategy '(read exit)
59 "List of symbols that say when to remove articles from the prefetch buffer.
60 Possible values in this list are `read', which means that
61 articles are removed as they are read, and `exit', which means
62 that all articles belonging to a group are removed on exit
64 :group 'gnus-asynchronous
65 :type '(set (const read) (const exit)))
67 (defcustom gnus-use-header-prefetch nil
68 "*If non-nil, prefetch the headers to the next group."
69 :group 'gnus-asynchronous
72 (defcustom gnus-async-prefetch-article-p 'gnus-async-unread-p
73 "Function called to say whether an article should be prefetched or not.
74 The function is called with one parameter -- the article data.
75 It should return non-nil if the article is to be prefetched."
76 :group 'gnus-asynchronous
79 ;;; Internal variables.
81 (defvar gnus-async-prefetch-article-buffer " *Async Prefetch Article*")
82 (defvar gnus-async-article-alist nil)
83 (defvar gnus-async-article-semaphore '(nil))
84 (defvar gnus-async-fetch-list nil)
85 (defvar gnus-async-hashtb nil)
86 (defvar gnus-async-current-prefetch-group nil)
87 (defvar gnus-async-current-prefetch-article nil)
88 (defvar gnus-async-timer nil)
90 (defvar gnus-async-prefetch-headers-buffer " *Async Prefetch Headers*")
91 (defvar gnus-async-header-prefetched nil)
93 ;;; Utility functions.
95 (defun gnus-group-asynchronous-p (group)
96 "Say whether GROUP is fetched from a server that supports asynchronicity."
97 (gnus-asynchronous-p (gnus-find-method-for-group group)))
99 ;;; Somewhat bogus semaphores.
101 (defun gnus-async-get-semaphore (semaphore)
102 "Wait until SEMAPHORE is released."
103 (while (/= (length (nconc (symbol-value semaphore) (list nil))) 2)
106 (defun gnus-async-release-semaphore (semaphore)
108 (setcdr (symbol-value semaphore) nil))
110 (defmacro gnus-async-with-semaphore (&rest forms)
113 (gnus-async-get-semaphore 'gnus-async-article-semaphore)
115 (gnus-async-release-semaphore 'gnus-async-article-semaphore)))
117 (put 'gnus-async-with-semaphore 'lisp-indent-function 0)
118 (put 'gnus-async-with-semaphore 'edebug-form-spec '(body))
124 (gnus-add-shutdown 'gnus-async-close 'gnus)
125 (defun gnus-async-close ()
126 (gnus-kill-buffer gnus-async-prefetch-article-buffer)
127 (gnus-kill-buffer gnus-async-prefetch-headers-buffer)
128 (setq gnus-async-hashtb nil
129 gnus-async-article-alist nil
130 gnus-async-header-prefetched nil))
132 (defun gnus-async-set-buffer ()
133 (nnheader-set-temp-buffer gnus-async-prefetch-article-buffer t)
134 (unless gnus-async-hashtb
135 (setq gnus-async-hashtb (gnus-make-hashtable 1023))))
137 (defun gnus-async-halt-prefetch ()
139 (setq gnus-async-fetch-list nil))
141 (defun gnus-async-prefetch-next (group article summary)
142 "Possibly prefetch several articles starting with the article after ARTICLE."
143 (when (and (gnus-buffer-live-p summary)
145 (gnus-group-asynchronous-p group))
147 (set-buffer gnus-summary-buffer)
148 (let ((next (caadr (gnus-data-find-list article))))
150 (if (not (fboundp 'run-with-idle-timer))
151 ;; This is either an older Emacs or XEmacs, so we
152 ;; do this, which leads to slightly slower article
154 (gnus-async-prefetch-article group next summary)
155 (when gnus-async-timer
157 (nnheader-cancel-timer 'gnus-async-timer)))
158 (setq gnus-async-timer
160 0.1 nil 'gnus-async-prefetch-article
161 group next summary))))))))
163 (defun gnus-async-prefetch-article (group article summary &optional next)
164 "Possibly prefetch several articles starting with ARTICLE."
165 (if (not (gnus-buffer-live-p summary))
166 (gnus-async-with-semaphore
167 (setq gnus-async-fetch-list nil))
168 (when (and gnus-asynchronous
171 (gnus-async-with-semaphore
172 (pop gnus-async-fetch-list)))
173 (let ((do-fetch next)
174 (do-message t)) ;(eq major-mode 'gnus-summary-mode)))
175 (when (and (gnus-group-asynchronous-p group)
176 (gnus-buffer-live-p summary)
178 gnus-async-fetch-list))
179 (gnus-async-with-semaphore
181 (setq do-fetch (not gnus-async-fetch-list))
182 ;; Nix out any outstanding requests.
183 (setq gnus-async-fetch-list nil)
184 ;; Fill in the new list.
185 (let ((n gnus-use-article-prefetch)
186 (data (gnus-data-find-list article))
188 (while (and (setq d (pop data))
192 (unless (or (gnus-async-prefetched-article-entry
193 group (setq article (gnus-data-number d)))
194 (not (natnump article))
195 (not (funcall gnus-async-prefetch-article-p d)))
196 ;; Not already fetched -- so we add it to the list.
197 (push article gnus-async-fetch-list)))
198 (setq gnus-async-fetch-list
199 (nreverse gnus-async-fetch-list))))
202 (setq article (car gnus-async-fetch-list))))
204 (when (and do-fetch article)
205 ;; We want to fetch some more articles.
209 (gnus-async-set-buffer)
210 (goto-char (point-max))
211 (setq mark (point-marker))
212 (let ((nnheader-callback-function
213 (gnus-make-async-article-function
214 group article mark summary next))
216 (get-buffer gnus-async-prefetch-article-buffer)))
218 (gnus-message 9 "Prefetching article %d in group %s"
220 (setq gnus-async-current-prefetch-group group)
221 (setq gnus-async-current-prefetch-article article)
222 (gnus-request-article article group))))))))))
224 (defun gnus-make-async-article-function (group article mark summary next)
225 "Return a callback function."
227 (gnus-async-article-callback arg ,group ,article ,mark ,summary ,next)))
229 (defun gnus-async-article-callback (arg group article mark summary next)
230 "Function called when an async article is done being fetched."
232 (setq gnus-async-current-prefetch-article nil)
234 (gnus-async-set-buffer)
235 (gnus-async-with-semaphore
237 gnus-async-article-alist
238 (cons (list (intern (format "%s-%d" group article)
240 mark (set-marker (make-marker) (point-max))
242 gnus-async-article-alist))))
243 (if (not (gnus-buffer-live-p summary))
244 (gnus-async-with-semaphore
245 (setq gnus-async-fetch-list nil))
246 (gnus-async-prefetch-article group next summary t))))
248 (defun gnus-async-unread-p (data)
249 "Return non-nil if DATA represents an unread article."
250 (gnus-data-unread-p data))
252 (defun gnus-async-request-fetched-article (group article buffer)
253 "See whether we have ARTICLE from GROUP and put it in BUFFER."
254 (when (numberp article)
255 (when (and (equal group gnus-async-current-prefetch-group)
256 (eq article gnus-async-current-prefetch-article))
257 (gnus-async-wait-for-article article))
258 (let ((entry (gnus-async-prefetched-article-entry group article)))
261 (gnus-async-set-buffer)
262 (copy-to-buffer buffer (cadr entry) (caddr entry))
263 ;; Remove the read article from the prefetch buffer.
264 (when (memq 'read gnus-prefetched-article-deletion-strategy)
265 (gnus-async-delete-prefetched-entry entry))
268 (defun gnus-async-wait-for-article (article)
269 "Wait until ARTICLE is no longer the currently-being-fetched article."
271 (gnus-async-set-buffer)
272 (let ((proc (nntp-find-connection (current-buffer)))
273 (nntp-server-buffer (current-buffer))
274 (nntp-have-messaged nil)
278 ;; FIXME: we could stop waiting after some
279 ;; timeout, but this is the wrong place to do it.
280 ;; rather than checking time-spent-waiting, we
281 ;; should check time-since-last-output, which
282 ;; needs to be done in nntp.el.
283 (while (eq article gnus-async-current-prefetch-article)
285 (when (nntp-accept-process-output proc)
287 (when (and (not nntp-have-messaged)
289 (gnus-message 5 "Waiting for async article...")
290 (setq nntp-have-messaged t)))
292 ;; if the user interrupted on a slow/hung connection,
293 ;; do something friendly.
295 (setq gnus-async-current-prefetch-article nil))
297 (when nntp-have-messaged
298 (gnus-message 5 ""))))))
300 (defun gnus-async-delete-prefetched-entry (entry)
301 "Delete ENTRY from buffer and alist."
303 (delete-region (cadr entry) (caddr entry))
304 (set-marker (cadr entry) nil)
305 (set-marker (caddr entry) nil))
306 (gnus-async-with-semaphore
307 (setq gnus-async-article-alist
308 (delq entry gnus-async-article-alist))))
310 (defun gnus-async-prefetch-remove-group (group)
311 "Remove all articles belonging to GROUP from the prefetch buffer."
312 (when (and (gnus-group-asynchronous-p group)
313 (memq 'exit gnus-prefetched-article-deletion-strategy))
315 (gnus-async-set-buffer)
316 (dolist (entry gnus-async-article-alist)
317 (when (equal group (nth 3 entry))
318 (gnus-async-delete-prefetched-entry entry))))))
320 (defun gnus-async-prefetched-article-entry (group article)
321 "Return the entry for ARTICLE in GROUP iff it has been prefetched."
322 (let ((entry (save-excursion
323 (gnus-async-set-buffer)
324 (assq (intern (format "%s-%d" group article)
326 gnus-async-article-alist))))
327 ;; Perhaps something has emptied the buffer?
329 (= (cadr entry) (caddr entry)))
332 (set-marker (cadr entry) nil)
333 (set-marker (caddr entry) nil))
334 (setq gnus-async-article-alist
335 (delq entry gnus-async-article-alist))
343 (defun gnus-async-prefetch-headers (group)
344 "Prefetch the headers for group GROUP."
347 (when (and gnus-use-header-prefetch
349 (gnus-group-asynchronous-p group)
350 (listp gnus-async-header-prefetched)
351 (setq unread (gnus-list-of-unread-articles group)))
352 ;; Mark that a fetch is in progress.
353 (setq gnus-async-header-prefetched t)
354 (nnheader-set-temp-buffer gnus-async-prefetch-headers-buffer t)
356 (let ((nntp-server-buffer (current-buffer))
357 (nnheader-callback-function
359 (setq gnus-async-header-prefetched
360 ,(cons group unread)))))
361 (gnus-retrieve-headers unread group gnus-fetch-old-headers))))))
363 (defun gnus-async-retrieve-fetched-headers (articles group)
364 "See whether we have prefetched headers."
365 (when (and gnus-use-header-prefetch
366 (gnus-group-asynchronous-p group)
367 (listp gnus-async-header-prefetched)
368 (equal group (car gnus-async-header-prefetched))
369 (equal articles (cdr gnus-async-header-prefetched)))
371 (nnheader-set-temp-buffer gnus-async-prefetch-headers-buffer t)
373 (copy-to-buffer nntp-server-buffer (point-min) (point-max))
375 (setq gnus-async-header-prefetched nil)
378 (provide 'gnus-async)
380 ;;; gnus-async.el ends here