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., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
29 (eval-when-compile (require 'cl
))
35 (defgroup gnus-asynchronous nil
36 "Support for asynchronous operations."
39 (defcustom gnus-use-article-prefetch
30
40 "*If non-nil, prefetch articles in groups that allow this.
41 If a number, prefetch only that many articles forward;
42 if t, prefetch as many articles as possible."
43 :group
'gnus-asynchronous
44 :type
'(choice (const :tag
"off" nil
)
46 (integer :tag
"some" 0)))
48 (defcustom gnus-asynchronous nil
49 "*If nil, inhibit all Gnus asynchronicity.
50 If non-nil, let the other asynch variables be heeded."
51 :group
'gnus-asynchronous
54 (defcustom gnus-prefetched-article-deletion-strategy
'(read exit
)
55 "List of symbols that say when to remove articles from the prefetch buffer.
56 Possible values in this list are `read', which means that
57 articles are removed as they are read, and `exit', which means
58 that all articles belonging to a group are removed on exit
60 :group
'gnus-asynchronous
61 :type
'(set (const read
) (const exit
)))
63 (defcustom gnus-use-header-prefetch nil
64 "*If non-nil, prefetch the headers to the next group."
65 :group
'gnus-asynchronous
68 (defcustom gnus-async-prefetch-article-p
'gnus-async-unread-p
69 "Function called to say whether an article should be prefetched or not.
70 The function is called with one parameter -- the article data.
71 It should return non-nil if the article is to be prefetched."
72 :group
'gnus-asynchronous
75 ;;; Internal variables.
77 (defvar gnus-async-prefetch-article-buffer
" *Async Prefetch Article*")
78 (defvar gnus-async-article-alist nil
)
79 (defvar gnus-async-article-semaphore
'(nil))
80 (defvar gnus-async-fetch-list nil
)
81 (defvar gnus-async-hashtb nil
)
82 (defvar gnus-async-current-prefetch-group nil
)
83 (defvar gnus-async-current-prefetch-article nil
)
84 (defvar gnus-async-timer nil
)
86 (defvar gnus-async-prefetch-headers-buffer
" *Async Prefetch Headers*")
87 (defvar gnus-async-header-prefetched nil
)
89 ;;; Utility functions.
91 (defun gnus-group-asynchronous-p (group)
92 "Say whether GROUP is fetched from a server that supports asynchronicity."
93 (gnus-asynchronous-p (gnus-find-method-for-group group
)))
95 ;;; Somewhat bogus semaphores.
97 (defun gnus-async-get-semaphore (semaphore)
98 "Wait until SEMAPHORE is released."
99 (while (/= (length (nconc (symbol-value semaphore
) (list nil
))) 2)
102 (defun gnus-async-release-semaphore (semaphore)
104 (setcdr (symbol-value semaphore
) nil
))
106 (defmacro gnus-async-with-semaphore
(&rest forms
)
109 (gnus-async-get-semaphore 'gnus-async-article-semaphore
)
111 (gnus-async-release-semaphore 'gnus-async-article-semaphore
)))
113 (put 'gnus-async-with-semaphore
'lisp-indent-function
0)
114 (put 'gnus-async-with-semaphore
'edebug-form-spec
'(body))
120 (gnus-add-shutdown 'gnus-async-close
'gnus
)
121 (defun gnus-async-close ()
122 (gnus-kill-buffer gnus-async-prefetch-article-buffer
)
123 (gnus-kill-buffer gnus-async-prefetch-headers-buffer
)
124 (setq gnus-async-hashtb nil
125 gnus-async-article-alist nil
126 gnus-async-header-prefetched nil
))
128 (defun gnus-async-set-buffer ()
129 (nnheader-set-temp-buffer gnus-async-prefetch-article-buffer t
)
130 (unless gnus-async-hashtb
131 (setq gnus-async-hashtb
(gnus-make-hashtable 1023))))
133 (defun gnus-async-halt-prefetch ()
135 (setq gnus-async-fetch-list nil
))
137 (defun gnus-async-prefetch-next (group article summary
)
138 "Possibly prefetch several articles starting with the article after ARTICLE."
139 (when (and (gnus-buffer-live-p summary
)
141 (gnus-group-asynchronous-p group
))
143 (set-buffer gnus-summary-buffer
)
144 (let ((next (caadr (gnus-data-find-list article
))))
146 (if (not (fboundp 'run-with-idle-timer
))
147 ;; This is either an older Emacs or XEmacs, so we
148 ;; do this, which leads to slightly slower article
150 (gnus-async-prefetch-article group next summary
)
151 (when gnus-async-timer
153 (nnheader-cancel-timer 'gnus-async-timer
)))
154 (setq gnus-async-timer
156 0.1 nil
'gnus-async-prefetch-article
157 group next summary
))))))))
159 (defun gnus-async-prefetch-article (group article summary
&optional next
)
160 "Possibly prefetch several articles starting with ARTICLE."
161 (if (not (gnus-buffer-live-p summary
))
162 (gnus-async-with-semaphore
163 (setq gnus-async-fetch-list nil
))
164 (when (and gnus-asynchronous
167 (gnus-async-with-semaphore
168 (pop gnus-async-fetch-list
)))
169 (let ((do-fetch next
)
170 (do-message t
)) ;(eq major-mode 'gnus-summary-mode)))
171 (when (and (gnus-group-asynchronous-p group
)
172 (gnus-buffer-live-p summary
)
174 gnus-async-fetch-list
))
175 (gnus-async-with-semaphore
177 (setq do-fetch
(not gnus-async-fetch-list
))
178 ;; Nix out any outstanding requests.
179 (setq gnus-async-fetch-list nil
)
180 ;; Fill in the new list.
181 (let ((n gnus-use-article-prefetch
)
182 (data (gnus-data-find-list article
))
184 (while (and (setq d
(pop data
))
188 (unless (or (gnus-async-prefetched-article-entry
189 group
(setq article
(gnus-data-number d
)))
190 (not (natnump article
))
191 (not (funcall gnus-async-prefetch-article-p d
)))
192 ;; Not already fetched -- so we add it to the list.
193 (push article gnus-async-fetch-list
)))
194 (setq gnus-async-fetch-list
195 (nreverse gnus-async-fetch-list
))))
198 (setq article
(car gnus-async-fetch-list
))))
200 (when (and do-fetch article
)
201 ;; We want to fetch some more articles.
205 (gnus-async-set-buffer)
206 (goto-char (point-max))
207 (setq mark
(point-marker))
208 (let ((nnheader-callback-function
209 (gnus-make-async-article-function
210 group article mark summary next
))
212 (get-buffer gnus-async-prefetch-article-buffer
)))
214 (gnus-message 9 "Prefetching article %d in group %s"
216 (setq gnus-async-current-prefetch-group group
)
217 (setq gnus-async-current-prefetch-article article
)
218 (gnus-request-article article group
))))))))))
220 (defun gnus-make-async-article-function (group article mark summary next
)
221 "Return a callback function."
223 (gnus-async-article-callback arg
,group
,article
,mark
,summary
,next
)))
225 (defun gnus-async-article-callback (arg group article mark summary next
)
226 "Function called when an async article is done being fetched."
228 (setq gnus-async-current-prefetch-article nil
)
230 (gnus-async-set-buffer)
231 (gnus-async-with-semaphore
233 gnus-async-article-alist
234 (cons (list (intern (format "%s-%d" group article
)
236 mark
(set-marker (make-marker) (point-max))
238 gnus-async-article-alist
))))
239 (if (not (gnus-buffer-live-p summary
))
240 (gnus-async-with-semaphore
241 (setq gnus-async-fetch-list nil
))
242 (gnus-async-prefetch-article group next summary t
))))
244 (defun gnus-async-unread-p (data)
245 "Return non-nil if DATA represents an unread article."
246 (gnus-data-unread-p data
))
248 (defun gnus-async-request-fetched-article (group article buffer
)
249 "See whether we have ARTICLE from GROUP and put it in BUFFER."
250 (when (numberp article
)
251 (when (and (equal group gnus-async-current-prefetch-group
)
252 (eq article gnus-async-current-prefetch-article
))
253 (gnus-async-wait-for-article article
))
254 (let ((entry (gnus-async-prefetched-article-entry group article
)))
257 (gnus-async-set-buffer)
258 (copy-to-buffer buffer
(cadr entry
) (caddr entry
))
259 ;; Remove the read article from the prefetch buffer.
260 (when (memq 'read gnus-prefetched-article-deletion-strategy
)
261 (gnus-async-delete-prefetched-entry entry
))
264 (defun gnus-async-wait-for-article (article)
265 "Wait until ARTICLE is no longer the currently-being-fetched article."
267 (gnus-async-set-buffer)
268 (let ((proc (nntp-find-connection (current-buffer)))
269 (nntp-server-buffer (current-buffer))
270 (nntp-have-messaged nil
)
273 ;; FIXME: we could stop waiting after some
274 ;; timeout, but this is the wrong place to do it.
275 ;; rather than checking time-spent-waiting, we
276 ;; should check time-since-last-output, which
277 ;; needs to be done in nntp.el.
278 (while (eq article gnus-async-current-prefetch-article
)
280 (when (nntp-accept-process-output proc
)
282 (when (and (not nntp-have-messaged
)
284 (gnus-message 5 "Waiting for async article...")
285 (setq nntp-have-messaged t
)))
287 ;; if the user interrupted on a slow/hung connection,
288 ;; do something friendly.
290 (setq gnus-async-current-prefetch-article nil
))
292 (when nntp-have-messaged
293 (gnus-message 5 "")))))
295 (defun gnus-async-delete-prefetched-entry (entry)
296 "Delete ENTRY from buffer and alist."
298 (delete-region (cadr entry
) (caddr entry
))
299 (set-marker (cadr entry
) nil
)
300 (set-marker (caddr entry
) nil
))
301 (gnus-async-with-semaphore
302 (setq gnus-async-article-alist
303 (delq entry gnus-async-article-alist
))))
305 (defun gnus-async-prefetch-remove-group (group)
306 "Remove all articles belonging to GROUP from the prefetch buffer."
307 (when (and (gnus-group-asynchronous-p group
)
308 (memq 'exit gnus-prefetched-article-deletion-strategy
))
309 (let ((alist gnus-async-article-alist
))
311 (gnus-async-set-buffer)
313 (when (equal group
(nth 3 (car alist
)))
314 (gnus-async-delete-prefetched-entry (car alist
)))
317 (defun gnus-async-prefetched-article-entry (group article
)
318 "Return the entry for ARTICLE in GROUP iff 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