1 ;;; gnus-async.el --- asynchronous support for Gnus
3 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 3, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
30 (eval-when-compile (require 'cl
))
36 (defgroup gnus-asynchronous nil
37 "Support for asynchronous operations."
40 (defcustom gnus-use-article-prefetch
30
41 "*If non-nil, prefetch articles in groups that allow this.
42 If a number, prefetch only that many articles forward;
43 if t, prefetch as many articles as possible."
44 :group
'gnus-asynchronous
45 :type
'(choice (const :tag
"off" nil
)
47 (integer :tag
"some" 0)))
49 (defcustom gnus-asynchronous nil
50 "*If nil, inhibit all Gnus asynchronicity.
51 If non-nil, let the other asynch variables be heeded."
52 :group
'gnus-asynchronous
55 (defcustom gnus-prefetched-article-deletion-strategy
'(read exit
)
56 "List of symbols that say when to remove articles from the prefetch buffer.
57 Possible values in this list are `read', which means that
58 articles are removed as they are read, and `exit', which means
59 that all articles belonging to a group are removed on exit
61 :group
'gnus-asynchronous
62 :type
'(set (const read
) (const exit
)))
64 (defcustom gnus-use-header-prefetch nil
65 "*If non-nil, prefetch the headers to the next group."
66 :group
'gnus-asynchronous
69 (defcustom gnus-async-prefetch-article-p
'gnus-async-unread-p
70 "Function called to say whether an article should be prefetched or not.
71 The function is called with one parameter -- the article data.
72 It should return non-nil if the article is to be prefetched."
73 :group
'gnus-asynchronous
76 ;;; Internal variables.
78 (defvar gnus-async-prefetch-article-buffer
" *Async Prefetch Article*")
79 (defvar gnus-async-article-alist nil
)
80 (defvar gnus-async-article-semaphore
'(nil))
81 (defvar gnus-async-fetch-list nil
)
82 (defvar gnus-async-hashtb nil
)
83 (defvar gnus-async-current-prefetch-group nil
)
84 (defvar gnus-async-current-prefetch-article nil
)
85 (defvar gnus-async-timer nil
)
87 (defvar gnus-async-prefetch-headers-buffer
" *Async Prefetch Headers*")
88 (defvar gnus-async-header-prefetched nil
)
90 ;;; Utility functions.
92 (defun gnus-group-asynchronous-p (group)
93 "Say whether GROUP is fetched from a server that supports asynchronicity."
94 (gnus-asynchronous-p (gnus-find-method-for-group group
)))
96 ;;; Somewhat bogus semaphores.
98 (defun gnus-async-get-semaphore (semaphore)
99 "Wait until SEMAPHORE is released."
100 (while (/= (length (nconc (symbol-value semaphore
) (list nil
))) 2)
103 (defun gnus-async-release-semaphore (semaphore)
105 (setcdr (symbol-value semaphore
) nil
))
107 (defmacro gnus-async-with-semaphore
(&rest forms
)
110 (gnus-async-get-semaphore 'gnus-async-article-semaphore
)
112 (gnus-async-release-semaphore 'gnus-async-article-semaphore
)))
114 (put 'gnus-async-with-semaphore
'lisp-indent-function
0)
115 (put 'gnus-async-with-semaphore
'edebug-form-spec
'(body))
121 (gnus-add-shutdown 'gnus-async-close
'gnus
)
122 (defun gnus-async-close ()
123 (gnus-kill-buffer gnus-async-prefetch-article-buffer
)
124 (gnus-kill-buffer gnus-async-prefetch-headers-buffer
)
125 (setq gnus-async-hashtb nil
126 gnus-async-article-alist nil
127 gnus-async-header-prefetched nil
))
129 (defun gnus-async-set-buffer ()
130 (nnheader-set-temp-buffer gnus-async-prefetch-article-buffer t
)
131 (unless gnus-async-hashtb
132 (setq gnus-async-hashtb
(gnus-make-hashtable 1023))))
134 (defun gnus-async-halt-prefetch ()
136 (setq gnus-async-fetch-list nil
))
138 (defun gnus-async-prefetch-next (group article summary
)
139 "Possibly prefetch several articles starting with the article after ARTICLE."
140 (when (and (gnus-buffer-live-p summary
)
142 (gnus-group-asynchronous-p group
))
144 (set-buffer gnus-summary-buffer
)
145 (let ((next (caadr (gnus-data-find-list article
))))
147 (if (not (fboundp 'run-with-idle-timer
))
148 ;; This is either an older Emacs or XEmacs, so we
149 ;; do this, which leads to slightly slower article
151 (gnus-async-prefetch-article group next summary
)
152 (when gnus-async-timer
154 (nnheader-cancel-timer 'gnus-async-timer
)))
155 (setq gnus-async-timer
157 0.1 nil
'gnus-async-prefetch-article
158 group next summary
))))))))
160 (defun gnus-async-prefetch-article (group article summary
&optional next
)
161 "Possibly prefetch several articles starting with ARTICLE."
162 (if (not (gnus-buffer-live-p summary
))
163 (gnus-async-with-semaphore
164 (setq gnus-async-fetch-list nil
))
165 (when (and gnus-asynchronous
168 (gnus-async-with-semaphore
169 (pop gnus-async-fetch-list
)))
170 (let ((do-fetch next
)
171 (do-message t
)) ;(eq major-mode 'gnus-summary-mode)))
172 (when (and (gnus-group-asynchronous-p group
)
173 (gnus-buffer-live-p summary
)
175 gnus-async-fetch-list
))
176 (gnus-async-with-semaphore
178 (setq do-fetch
(not gnus-async-fetch-list
))
179 ;; Nix out any outstanding requests.
180 (setq gnus-async-fetch-list nil
)
181 ;; Fill in the new list.
182 (let ((n gnus-use-article-prefetch
)
183 (data (gnus-data-find-list article
))
185 (while (and (setq d
(pop data
))
189 (unless (or (gnus-async-prefetched-article-entry
190 group
(setq article
(gnus-data-number d
)))
191 (not (natnump article
))
192 (not (funcall gnus-async-prefetch-article-p d
)))
193 ;; Not already fetched -- so we add it to the list.
194 (push article gnus-async-fetch-list
)))
195 (setq gnus-async-fetch-list
196 (nreverse gnus-async-fetch-list
))))
199 (setq article
(car gnus-async-fetch-list
))))
201 (when (and do-fetch article
)
202 ;; We want to fetch some more articles.
206 (gnus-async-set-buffer)
207 (goto-char (point-max))
208 (setq mark
(point-marker))
209 (let ((nnheader-callback-function
210 (gnus-make-async-article-function
211 group article mark summary next
))
213 (get-buffer gnus-async-prefetch-article-buffer
)))
215 (gnus-message 9 "Prefetching article %d in group %s"
217 (setq gnus-async-current-prefetch-group group
)
218 (setq gnus-async-current-prefetch-article article
)
219 (gnus-request-article article group
))))))))))
221 (defun gnus-make-async-article-function (group article mark summary next
)
222 "Return a callback function."
224 (gnus-async-article-callback arg
,group
,article
,mark
,summary
,next
)))
226 (defun gnus-async-article-callback (arg group article mark summary next
)
227 "Function called when an async article is done being fetched."
229 (setq gnus-async-current-prefetch-article nil
)
231 (gnus-async-set-buffer)
232 (gnus-async-with-semaphore
234 gnus-async-article-alist
235 (cons (list (intern (format "%s-%d" group article
)
237 mark
(set-marker (make-marker) (point-max))
239 gnus-async-article-alist
))))
240 (if (not (gnus-buffer-live-p summary
))
241 (gnus-async-with-semaphore
242 (setq gnus-async-fetch-list nil
))
243 (gnus-async-prefetch-article group next summary t
))))
245 (defun gnus-async-unread-p (data)
246 "Return non-nil if DATA represents an unread article."
247 (gnus-data-unread-p data
))
249 (defun gnus-async-request-fetched-article (group article buffer
)
250 "See whether we have ARTICLE from GROUP and put it in BUFFER."
251 (when (numberp article
)
252 (when (and (equal group gnus-async-current-prefetch-group
)
253 (eq article gnus-async-current-prefetch-article
))
254 (gnus-async-wait-for-article article
))
255 (let ((entry (gnus-async-prefetched-article-entry group article
)))
258 (gnus-async-set-buffer)
259 (copy-to-buffer buffer
(cadr entry
) (caddr entry
))
260 ;; Remove the read article from the prefetch buffer.
261 (when (memq 'read gnus-prefetched-article-deletion-strategy
)
262 (gnus-async-delete-prefetched-entry entry
))
265 (defun gnus-async-wait-for-article (article)
266 "Wait until ARTICLE is no longer the currently-being-fetched article."
268 (gnus-async-set-buffer)
269 (let ((proc (nntp-find-connection (current-buffer)))
270 (nntp-server-buffer (current-buffer))
271 (nntp-have-messaged nil
)
275 ;; FIXME: we could stop waiting after some
276 ;; timeout, but this is the wrong place to do it.
277 ;; rather than checking time-spent-waiting, we
278 ;; should check time-since-last-output, which
279 ;; needs to be done in nntp.el.
280 (while (eq article gnus-async-current-prefetch-article
)
282 (when (nntp-accept-process-output proc
)
284 (when (and (not nntp-have-messaged
)
286 (gnus-message 5 "Waiting for async article...")
287 (setq nntp-have-messaged t
)))
289 ;; if the user interrupted on a slow/hung connection,
290 ;; do something friendly.
292 (setq gnus-async-current-prefetch-article nil
))
294 (when nntp-have-messaged
295 (gnus-message 5 ""))))))
297 (defun gnus-async-delete-prefetched-entry (entry)
298 "Delete ENTRY from buffer and alist."
300 (delete-region (cadr entry
) (caddr entry
))
301 (set-marker (cadr entry
) nil
)
302 (set-marker (caddr entry
) nil
))
303 (gnus-async-with-semaphore
304 (setq gnus-async-article-alist
305 (delq entry gnus-async-article-alist
))))
307 (defun gnus-async-prefetch-remove-group (group)
308 "Remove all articles belonging to GROUP from the prefetch buffer."
309 (when (and (gnus-group-asynchronous-p group
)
310 (memq 'exit gnus-prefetched-article-deletion-strategy
))
312 (gnus-async-set-buffer)
313 (dolist (entry gnus-async-article-alist
)
314 (when (equal group
(nth 3 entry
))
315 (gnus-async-delete-prefetched-entry entry
))))))
317 (defun gnus-async-prefetched-article-entry (group article
)
318 "Return the entry for ARTICLE in GROUP if it has been prefetched."
319 (let ((entry (save-excursion
320 (gnus-async-set-buffer)
321 (assq (intern (format "%s-%d" group article
)
323 gnus-async-article-alist
))))
324 ;; Perhaps something has emptied the buffer?
326 (= (cadr entry
) (caddr entry
)))
329 (set-marker (cadr entry
) nil
)
330 (set-marker (caddr entry
) nil
))
331 (setq gnus-async-article-alist
332 (delq entry gnus-async-article-alist
))
340 (defun gnus-async-prefetch-headers (group)
341 "Prefetch the headers for group GROUP."
344 (when (and gnus-use-header-prefetch
346 (gnus-group-asynchronous-p group
)
347 (listp gnus-async-header-prefetched
)
348 (setq unread
(gnus-list-of-unread-articles group
)))
349 ;; Mark that a fetch is in progress.
350 (setq gnus-async-header-prefetched t
)
351 (nnheader-set-temp-buffer gnus-async-prefetch-headers-buffer t
)
353 (let ((nntp-server-buffer (current-buffer))
354 (nnheader-callback-function
356 (setq gnus-async-header-prefetched
357 ,(cons group unread
)))))
358 (gnus-retrieve-headers unread group gnus-fetch-old-headers
))))))
360 (defun gnus-async-retrieve-fetched-headers (articles group
)
361 "See whether we have prefetched headers."
362 (when (and gnus-use-header-prefetch
363 (gnus-group-asynchronous-p group
)
364 (listp gnus-async-header-prefetched
)
365 (equal group
(car gnus-async-header-prefetched
))
366 (equal articles
(cdr gnus-async-header-prefetched
)))
368 (nnheader-set-temp-buffer gnus-async-prefetch-headers-buffer t
)
370 (copy-to-buffer nntp-server-buffer
(point-min) (point-max))
372 (setq gnus-async-header-prefetched nil
)
375 (provide 'gnus-async
)
377 ;; arch-tag: fee61de5-3ea2-4de6-8578-2f90ce89391d
378 ;;; gnus-async.el ends here