Update Gnus to No Gnus 0.7 from the Gnus CVS trunk
[bpt/emacs.git] / lisp / gnus / gnus-async.el
CommitLineData
eec82323 1;;; gnus-async.el --- asynchronous support for Gnus
e84b4b86 2
b6c2d8c6 3;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
d7a0267c 4;; 2005, 2006, 2007 Free Software Foundation, Inc.
eec82323 5
6748645f 6;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
eec82323
LMI
7;; Keywords: news
8
9;; This file is part of GNU Emacs.
10
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
5a9dffec 13;; the Free Software Foundation; either version 3, or (at your option)
eec82323
LMI
14;; any later version.
15
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.
20
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
3a35cf56
LK
23;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24;; Boston, MA 02110-1301, USA.
eec82323
LMI
25
26;;; Commentary:
27
28;;; Code:
29
5ab7173c
RS
30(eval-when-compile (require 'cl))
31
eec82323
LMI
32(require 'gnus)
33(require 'gnus-sum)
34(require 'nntp)
35
36(defgroup gnus-asynchronous nil
37 "Support for asynchronous operations."
38 :group 'gnus)
39
eec82323
LMI
40(defcustom gnus-use-article-prefetch 30
41 "*If non-nil, prefetch articles in groups that allow this.
42If a number, prefetch only that many articles forward;
43if t, prefetch as many articles as possible."
44 :group 'gnus-asynchronous
45 :type '(choice (const :tag "off" nil)
16409b0b
GM
46 (const :tag "all" t)
47 (integer :tag "some" 0)))
eec82323 48
23f87bed
MB
49(defcustom gnus-asynchronous nil
50 "*If nil, inhibit all Gnus asynchronicity.
51If non-nil, let the other asynch variables be heeded."
52 :group 'gnus-asynchronous
53 :type 'boolean)
54
eec82323
LMI
55(defcustom gnus-prefetched-article-deletion-strategy '(read exit)
56 "List of symbols that say when to remove articles from the prefetch buffer.
57Possible values in this list are `read', which means that
58articles are removed as they are read, and `exit', which means
59that all articles belonging to a group are removed on exit
60from that group."
61 :group 'gnus-asynchronous
62 :type '(set (const read) (const exit)))
63
64(defcustom gnus-use-header-prefetch nil
65 "*If non-nil, prefetch the headers to the next group."
66 :group 'gnus-asynchronous
67 :type 'boolean)
68
69(defcustom gnus-async-prefetch-article-p 'gnus-async-unread-p
70 "Function called to say whether an article should be prefetched or not.
71The function is called with one parameter -- the article data.
72It should return non-nil if the article is to be prefetched."
73 :group 'gnus-asynchronous
74 :type 'function)
75
76;;; Internal variables.
77
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)
16409b0b
GM
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)
eec82323
LMI
86
87(defvar gnus-async-prefetch-headers-buffer " *Async Prefetch Headers*")
88(defvar gnus-async-header-prefetched nil)
89
90;;; Utility functions.
91
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)))
95
96;;; Somewhat bogus semaphores.
97
98(defun gnus-async-get-semaphore (semaphore)
99 "Wait until SEMAPHORE is released."
100 (while (/= (length (nconc (symbol-value semaphore) (list nil))) 2)
101 (sleep-for 1)))
102
103(defun gnus-async-release-semaphore (semaphore)
104 "Release SEMAPHORE."
105 (setcdr (symbol-value semaphore) nil))
106
107(defmacro gnus-async-with-semaphore (&rest forms)
108 `(unwind-protect
109 (progn
110 (gnus-async-get-semaphore 'gnus-async-article-semaphore)
111 ,@forms)
112 (gnus-async-release-semaphore 'gnus-async-article-semaphore)))
113
16409b0b
GM
114(put 'gnus-async-with-semaphore 'lisp-indent-function 0)
115(put 'gnus-async-with-semaphore 'edebug-form-spec '(body))
eec82323
LMI
116
117;;;
118;;; Article prefetch
119;;;
120
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)
16409b0b
GM
125 (setq gnus-async-hashtb nil
126 gnus-async-article-alist nil
eec82323
LMI
127 gnus-async-header-prefetched nil))
128
129(defun gnus-async-set-buffer ()
6748645f 130 (nnheader-set-temp-buffer gnus-async-prefetch-article-buffer t)
16409b0b
GM
131 (unless gnus-async-hashtb
132 (setq gnus-async-hashtb (gnus-make-hashtable 1023))))
eec82323
LMI
133
134(defun gnus-async-halt-prefetch ()
135 "Stop prefetching."
136 (setq gnus-async-fetch-list nil))
137
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)
141 gnus-asynchronous
142 (gnus-group-asynchronous-p group))
143 (save-excursion
144 (set-buffer gnus-summary-buffer)
145 (let ((next (caadr (gnus-data-find-list article))))
146 (when next
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
150 ;; buffer display.
151 (gnus-async-prefetch-article group next summary)
16409b0b
GM
152 (when gnus-async-timer
153 (ignore-errors
154 (nnheader-cancel-timer 'gnus-async-timer)))
155 (setq gnus-async-timer
156 (run-with-idle-timer
157 0.1 nil 'gnus-async-prefetch-article
158 group next summary))))))))
eec82323
LMI
159
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
16409b0b 164 (setq gnus-async-fetch-list nil))
eec82323
LMI
165 (when (and gnus-asynchronous
166 (gnus-alive-p))
167 (when next
168 (gnus-async-with-semaphore
16409b0b 169 (pop gnus-async-fetch-list)))
eec82323 170 (let ((do-fetch next)
16409b0b 171 (do-message t)) ;(eq major-mode 'gnus-summary-mode)))
eec82323
LMI
172 (when (and (gnus-group-asynchronous-p group)
173 (gnus-buffer-live-p summary)
174 (or (not next)
175 gnus-async-fetch-list))
176 (gnus-async-with-semaphore
16409b0b
GM
177 (unless next
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))
184 d)
185 (while (and (setq d (pop data))
186 (if (numberp n)
187 (natnump (decf n))
188 n))
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))))
197
198 (when do-fetch
199 (setq article (car gnus-async-fetch-list))))
eec82323
LMI
200
201 (when (and do-fetch article)
202 ;; We want to fetch some more articles.
203 (save-excursion
204 (set-buffer summary)
205 (let (mark)
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))
212 (nntp-server-buffer
213 (get-buffer gnus-async-prefetch-article-buffer)))
214 (when do-message
215 (gnus-message 9 "Prefetching article %d in group %s"
216 article group))
16409b0b
GM
217 (setq gnus-async-current-prefetch-group group)
218 (setq gnus-async-current-prefetch-article article)
eec82323
LMI
219 (gnus-request-article article group))))))))))
220
221(defun gnus-make-async-article-function (group article mark summary next)
222 "Return a callback function."
223 `(lambda (arg)
16409b0b
GM
224 (gnus-async-article-callback arg ,group ,article ,mark ,summary ,next)))
225
226(defun gnus-async-article-callback (arg group article mark summary next)
227 "Function called when an async article is done being fetched."
228 (save-excursion
229 (setq gnus-async-current-prefetch-article nil)
230 (when arg
231 (gnus-async-set-buffer)
232 (gnus-async-with-semaphore
233 (setq
234 gnus-async-article-alist
235 (cons (list (intern (format "%s-%d" group article)
236 gnus-async-hashtb)
237 mark (set-marker (make-marker) (point-max))
238 group article)
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))))
eec82323
LMI
244
245(defun gnus-async-unread-p (data)
246 "Return non-nil if DATA represents an unread article."
247 (gnus-data-unread-p data))
248
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)
16409b0b
GM
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))
eec82323
LMI
255 (let ((entry (gnus-async-prefetched-article-entry group article)))
256 (when entry
257 (save-excursion
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)
16409b0b 262 (gnus-async-delete-prefetched-entry entry))
eec82323
LMI
263 t)))))
264
16409b0b
GM
265(defun gnus-async-wait-for-article (article)
266 "Wait until ARTICLE is no longer the currently-being-fetched article."
267 (save-excursion
268 (gnus-async-set-buffer)
269 (let ((proc (nntp-find-connection (current-buffer)))
270 (nntp-server-buffer (current-buffer))
271 (nntp-have-messaged nil)
272 (tries 0))
01c52d31
MB
273 (when proc
274 (condition-case 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)
281 (incf tries)
282 (when (nntp-accept-process-output proc)
283 (setq tries 0))
284 (when (and (not nntp-have-messaged)
285 (= tries 3))
286 (gnus-message 5 "Waiting for async article...")
287 (setq nntp-have-messaged t)))
288 (quit
289 ;; if the user interrupted on a slow/hung connection,
290 ;; do something friendly.
291 (when (> tries 3)
292 (setq gnus-async-current-prefetch-article nil))
293 (signal 'quit nil)))
294 (when nntp-have-messaged
295 (gnus-message 5 ""))))))
16409b0b
GM
296
297(defun gnus-async-delete-prefetched-entry (entry)
eec82323
LMI
298 "Delete ENTRY from buffer and alist."
299 (ignore-errors
300 (delete-region (cadr entry) (caddr entry))
301 (set-marker (cadr entry) nil)
302 (set-marker (caddr entry) nil))
303 (gnus-async-with-semaphore
16409b0b
GM
304 (setq gnus-async-article-alist
305 (delq entry gnus-async-article-alist))))
eec82323
LMI
306
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))
01c52d31
MB
311 (save-excursion
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))))))
eec82323
LMI
316
317(defun gnus-async-prefetched-article-entry (group article)
e7f767c2 318 "Return the entry for ARTICLE in GROUP if it has been prefetched."
6748645f
LMI
319 (let ((entry (save-excursion
320 (gnus-async-set-buffer)
321 (assq (intern (format "%s-%d" group article)
16409b0b 322 gnus-async-hashtb)
6748645f 323 gnus-async-article-alist))))
eec82323
LMI
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)))
335
336;;;
337;;; Header prefetch
338;;;
339
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))))))
359
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)))
374
375(provide 'gnus-async)
376
ab5796a9 377;;; arch-tag: fee61de5-3ea2-4de6-8578-2f90ce89391d
eec82323 378;;; gnus-async.el ends here