Change release version from 21.4 to 22.1 throughout.
[emacs.git] / lisp / gnus / gnus-async.el
blob43ab0bc887d7b6df99ab512e0ec47459ae53b61b
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>
6 ;; Keywords: news
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)
13 ;; any later version.
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.
25 ;;; Commentary:
27 ;;; Code:
29 (eval-when-compile (require 'cl))
31 (require 'gnus)
32 (require 'gnus-sum)
33 (require 'nntp)
35 (defgroup gnus-asynchronous nil
36 "Support for asynchronous operations."
37 :group 'gnus)
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)
45 (const :tag "all" t)
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
52 :type 'boolean)
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
59 from that group."
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
66 :type 'boolean)
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
73 :type 'function)
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)
100 (sleep-for 1)))
102 (defun gnus-async-release-semaphore (semaphore)
103 "Release SEMAPHORE."
104 (setcdr (symbol-value semaphore) nil))
106 (defmacro gnus-async-with-semaphore (&rest forms)
107 `(unwind-protect
108 (progn
109 (gnus-async-get-semaphore 'gnus-async-article-semaphore)
110 ,@forms)
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))
117 ;;; Article prefetch
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 ()
134 "Stop prefetching."
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)
140 gnus-asynchronous
141 (gnus-group-asynchronous-p group))
142 (save-excursion
143 (set-buffer gnus-summary-buffer)
144 (let ((next (caadr (gnus-data-find-list article))))
145 (when next
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
149 ;; buffer display.
150 (gnus-async-prefetch-article group next summary)
151 (when gnus-async-timer
152 (ignore-errors
153 (nnheader-cancel-timer 'gnus-async-timer)))
154 (setq gnus-async-timer
155 (run-with-idle-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
165 (gnus-alive-p))
166 (when next
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)
173 (or (not next)
174 gnus-async-fetch-list))
175 (gnus-async-with-semaphore
176 (unless next
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))
185 (if (numberp n)
186 (natnump (decf n))
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))))
197 (when do-fetch
198 (setq article (car gnus-async-fetch-list))))
200 (when (and do-fetch article)
201 ;; We want to fetch some more articles.
202 (save-excursion
203 (set-buffer summary)
204 (let (mark)
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))
211 (nntp-server-buffer
212 (get-buffer gnus-async-prefetch-article-buffer)))
213 (when do-message
214 (gnus-message 9 "Prefetching article %d in group %s"
215 article group))
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."
222 `(lambda (arg)
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."
227 (save-excursion
228 (setq gnus-async-current-prefetch-article nil)
229 (when arg
230 (gnus-async-set-buffer)
231 (gnus-async-with-semaphore
232 (setq
233 gnus-async-article-alist
234 (cons (list (intern (format "%s-%d" group article)
235 gnus-async-hashtb)
236 mark (set-marker (make-marker) (point-max))
237 group article)
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)))
255 (when entry
256 (save-excursion
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))
262 t)))))
264 (defun gnus-async-wait-for-article (article)
265 "Wait until ARTICLE is no longer the currently-being-fetched article."
266 (save-excursion
267 (gnus-async-set-buffer)
268 (let ((proc (nntp-find-connection (current-buffer)))
269 (nntp-server-buffer (current-buffer))
270 (nntp-have-messaged nil)
271 (tries 0))
272 (condition-case 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)
279 (incf tries)
280 (when (nntp-accept-process-output proc)
281 (setq tries 0))
282 (when (and (not nntp-have-messaged)
283 (= tries 3))
284 (gnus-message 5 "Waiting for async article...")
285 (setq nntp-have-messaged t)))
286 (quit
287 ;; if the user interrupted on a slow/hung connection,
288 ;; do something friendly.
289 (when (> tries 3)
290 (setq gnus-async-current-prefetch-article nil))
291 (signal 'quit 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."
297 (ignore-errors
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))
310 (save-excursion
311 (gnus-async-set-buffer)
312 (while alist
313 (when (equal group (nth 3 (car alist)))
314 (gnus-async-delete-prefetched-entry (car alist)))
315 (pop 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)
322 gnus-async-hashtb)
323 gnus-async-article-alist))))
324 ;; Perhaps something has emptied the buffer?
325 (if (and entry
326 (= (cadr entry) (caddr entry)))
327 (progn
328 (ignore-errors
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))
333 nil)
334 entry)))
337 ;;; Header prefetch
340 (defun gnus-async-prefetch-headers (group)
341 "Prefetch the headers for group GROUP."
342 (save-excursion
343 (let (unread)
344 (when (and gnus-use-header-prefetch
345 gnus-asynchronous
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)
352 (erase-buffer)
353 (let ((nntp-server-buffer (current-buffer))
354 (nnheader-callback-function
355 `(lambda (arg)
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)))
367 (save-excursion
368 (nnheader-set-temp-buffer gnus-async-prefetch-headers-buffer t)
369 (nntp-decode-text)
370 (copy-to-buffer nntp-server-buffer (point-min) (point-max))
371 (erase-buffer)
372 (setq gnus-async-header-prefetched nil)
373 t)))
375 (provide 'gnus-async)
377 ;;; arch-tag: fee61de5-3ea2-4de6-8578-2f90ce89391d
378 ;;; gnus-async.el ends here