Require `cl' using `eval-when-compile'.
[elisp/gnus.git-] / lisp / gnus-async.el
1 ;;; gnus-async.el --- asynchronous support for Gnus
2 ;; Copyright (C) 1996,97,98,99 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;; Keywords: news
6
7 ;; This file is part of GNU Emacs.
8
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; any later version.
13
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 ;; GNU General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
23
24 ;;; Commentary:
25
26 ;;; Code:
27
28 (eval-when-compile (require 'cl))
29 (require 'gnus)
30 (require 'gnus-sum)
31 (require 'nntp)
32
33 (defgroup gnus-asynchronous nil
34   "Support for asynchronous operations."
35   :group 'gnus)
36
37 (defcustom gnus-asynchronous t
38   "*If nil, inhibit all Gnus asynchronicity.
39 If non-nil, let the other asynch variables be heeded."
40   :group 'gnus-asynchronous
41   :type 'boolean)
42
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)
49                  (const :tag "all" t)
50                  (integer :tag "some" 0)))
51
52 (defcustom gnus-prefetched-article-deletion-strategy '(read exit)
53   "List of symbols that say when to remove articles from the prefetch buffer.
54 Possible values in this list are `read', which means that
55 articles are removed as they are read, and `exit', which means
56 that all articles belonging to a group are removed on exit
57 from that group."
58   :group 'gnus-asynchronous
59   :type '(set (const read) (const exit)))
60
61 (defcustom gnus-use-header-prefetch nil
62   "*If non-nil, prefetch the headers to the next group."
63   :group 'gnus-asynchronous
64   :type 'boolean)
65
66 (defcustom gnus-async-prefetch-article-p 'gnus-async-unread-p
67   "Function called to say whether an article should be prefetched or not.
68 The function is called with one parameter -- the article data.
69 It should return non-nil if the article is to be prefetched."
70   :group 'gnus-asynchronous
71   :type 'function)
72
73 ;;; Internal variables.
74
75 (defvar gnus-async-prefetch-article-buffer " *Async Prefetch Article*")
76 (defvar gnus-async-article-alist nil)
77 (defvar gnus-async-article-semaphore '(nil))
78 (defvar gnus-async-fetch-list nil)
79 (defvar gnus-async-hashtb nil)
80 (defvar gnus-async-current-prefetch-group nil)
81 (defvar gnus-async-current-prefetch-article nil)
82
83 (defvar gnus-async-prefetch-headers-buffer " *Async Prefetch Headers*")
84 (defvar gnus-async-header-prefetched nil)
85
86 ;;; Utility functions.
87
88 (defun gnus-group-asynchronous-p (group)
89   "Say whether GROUP is fetched from a server that supports asynchronicity."
90   (gnus-asynchronous-p (gnus-find-method-for-group group)))
91
92 ;;; Somewhat bogus semaphores.
93
94 (defun gnus-async-get-semaphore (semaphore)
95   "Wait until SEMAPHORE is released."
96   (while (/= (length (nconc (symbol-value semaphore) (list nil))) 2)
97     (sleep-for 1)))
98
99 (defun gnus-async-release-semaphore (semaphore)
100   "Release SEMAPHORE."
101   (setcdr (symbol-value semaphore) nil))
102
103 (defmacro gnus-async-with-semaphore (&rest forms)
104   `(unwind-protect
105        (progn
106          (gnus-async-get-semaphore 'gnus-async-article-semaphore)
107          ,@forms)
108      (gnus-async-release-semaphore 'gnus-async-article-semaphore)))
109
110 (put 'gnus-async-with-semaphore 'lisp-indent-function 0)
111 (put 'gnus-async-with-semaphore 'edebug-form-spec '(body))
112
113 ;;;
114 ;;; Article prefetch
115 ;;;
116
117 (gnus-add-shutdown 'gnus-async-close 'gnus)
118 (defun gnus-async-close ()
119   (gnus-kill-buffer gnus-async-prefetch-article-buffer)
120   (gnus-kill-buffer gnus-async-prefetch-headers-buffer)
121   (setq gnus-async-hashtb nil
122         gnus-async-article-alist nil
123         gnus-async-header-prefetched nil))
124
125 (defun gnus-async-set-buffer ()
126   (nnheader-set-temp-buffer gnus-async-prefetch-article-buffer t)
127   (unless gnus-async-hashtb
128     (setq gnus-async-hashtb (gnus-make-hashtable 1023))))
129
130 (defun gnus-async-halt-prefetch ()
131   "Stop prefetching."
132   (setq gnus-async-fetch-list nil))
133
134 (defun gnus-async-prefetch-next (group article summary)
135   "Possibly prefetch several articles starting with the article after ARTICLE."
136   (when (and (gnus-buffer-live-p summary)
137              gnus-asynchronous
138              (gnus-group-asynchronous-p group))
139     (save-excursion
140       (set-buffer gnus-summary-buffer)
141       (let ((next (caadr (gnus-data-find-list article))))
142         (when next
143           (if (not (fboundp 'run-with-idle-timer))
144               ;; This is either an older Emacs or XEmacs, so we
145               ;; do this, which leads to slightly slower article
146               ;; buffer display.
147               (gnus-async-prefetch-article group next summary)
148             (run-with-idle-timer
149              0.1 nil 'gnus-async-prefetch-article group next summary)))))))
150
151 (defun gnus-async-prefetch-article (group article summary &optional next)
152   "Possibly prefetch several articles starting with ARTICLE."
153   (if (not (gnus-buffer-live-p summary))
154       (gnus-async-with-semaphore
155        (setq gnus-async-fetch-list nil))
156     (when (and gnus-asynchronous
157                (gnus-alive-p))
158       (when next
159         (gnus-async-with-semaphore
160          (pop gnus-async-fetch-list)))
161       (let ((do-fetch next)
162             (do-message t)) ;(eq major-mode 'gnus-summary-mode)))
163         (when (and (gnus-group-asynchronous-p group)
164                    (gnus-buffer-live-p summary)
165                    (or (not next)
166                        gnus-async-fetch-list))
167           (gnus-async-with-semaphore
168            (unless next
169              (setq do-fetch (not gnus-async-fetch-list))
170              ;; Nix out any outstanding requests.
171              (setq gnus-async-fetch-list nil)
172              ;; Fill in the new list.
173              (let ((n gnus-use-article-prefetch)
174                    (data (gnus-data-find-list article))
175                    d)
176                (while (and (setq d (pop data))
177                            (if (numberp n)
178                                (natnump (decf n))
179                              n))
180                  (unless (or (gnus-async-prefetched-article-entry
181                               group (setq article (gnus-data-number d)))
182                              (not (natnump article))
183                              (not (funcall gnus-async-prefetch-article-p d)))
184                    ;; Not already fetched -- so we add it to the list.
185                    (push article gnus-async-fetch-list)))
186                (setq gnus-async-fetch-list
187                      (nreverse gnus-async-fetch-list))))
188
189            (when do-fetch
190              (setq article (car gnus-async-fetch-list))))
191
192           (when (and do-fetch article)
193             ;; We want to fetch some more articles.
194             (save-excursion
195               (set-buffer summary)
196               (let (mark)
197                 (gnus-async-set-buffer)
198                 (goto-char (point-max))
199                 (setq mark (point-marker))
200                 (let ((nnheader-callback-function
201                        (gnus-make-async-article-function
202                         group article mark summary next))
203                       (nntp-server-buffer
204                        (get-buffer gnus-async-prefetch-article-buffer)))
205                   (when do-message
206                     (gnus-message 9 "Prefetching article %d in group %s"
207                                   article group))
208                   (setq gnus-async-current-prefetch-group group)
209                   (setq gnus-async-current-prefetch-article article)
210                   (gnus-request-article article group))))))))))
211
212 (defun gnus-make-async-article-function (group article mark summary next)
213   "Return a callback function."
214   `(lambda (arg)
215      (gnus-async-article-callback arg ,group ,article ,mark ,summary ,next)))
216
217 (defun gnus-async-article-callback (arg group article mark summary next)
218   "Function called when an async article is done being fetched."
219   (save-excursion
220     (setq gnus-async-current-prefetch-article nil)
221     (when arg
222       (gnus-async-set-buffer)
223       (gnus-async-with-semaphore
224        (setq
225         gnus-async-article-alist
226         (cons (list (intern (format "%s-%d" group article)
227                             gnus-async-hashtb)
228                     mark (set-marker (make-marker) (point-max))
229                     group article)
230               gnus-async-article-alist))))
231     (if (not (gnus-buffer-live-p summary))
232         (gnus-async-with-semaphore
233          (setq gnus-async-fetch-list nil))
234       (gnus-async-prefetch-article group next summary t))))
235
236 (defun gnus-async-unread-p (data)
237   "Return non-nil if DATA represents an unread article."
238   (gnus-data-unread-p data))
239
240 (defun gnus-async-request-fetched-article (group article buffer)
241   "See whether we have ARTICLE from GROUP and put it in BUFFER."
242   (when (numberp article)
243     (when (and (equal group gnus-async-current-prefetch-group)
244                (eq article gnus-async-current-prefetch-article))
245       (gnus-async-wait-for-article article))
246     (let ((entry (gnus-async-prefetched-article-entry group article)))
247       (when entry
248         (save-excursion
249           (gnus-async-set-buffer)
250           (copy-to-buffer buffer (cadr entry) (caddr entry))
251           ;; Remove the read article from the prefetch buffer.
252           (when (memq 'read gnus-prefetched-article-deletion-strategy)
253             (gnus-async-delete-prefetched-entry entry))
254           t)))))
255
256 (defun gnus-async-wait-for-article (article)
257   "Wait until ARTICLE is no longer the currently-being-fetched article."
258   (save-excursion
259     (gnus-async-set-buffer)
260     (let ((proc (nntp-find-connection (current-buffer)))
261           (nntp-server-buffer (current-buffer))
262           (nntp-have-messaged nil)
263           (tries 0))
264       (condition-case nil
265           ;; FIXME: we could stop waiting after some
266           ;; timeout, but this is the wrong place to do it.
267           ;; rather than checking time-spent-waiting, we
268           ;; should check time-since-last-output, which
269           ;; needs to be done in nntp.el.
270           (while (eq article gnus-async-current-prefetch-article)
271             (incf tries)
272             (when (nntp-accept-process-output proc 1)
273               (setq tries 0))
274             (when (and (not nntp-have-messaged) (eq 3 tries))
275               (gnus-message 5 "Waiting for async article...")
276               (setq nntp-have-messaged t)))
277         (quit
278          ;; if the user interrupted on a slow/hung connection,
279          ;; do something friendly.
280          (when (< 3 tries)
281            (setq gnus-async-current-prefetch-article nil))
282          (signal 'quit nil)))
283       (when nntp-have-messaged
284         (gnus-message 5 "")))))
285
286 (defun gnus-async-delete-prefetched-entry (entry)
287   "Delete ENTRY from buffer and alist."
288   (ignore-errors
289     (delete-region (cadr entry) (caddr entry))
290     (set-marker (cadr entry) nil)
291     (set-marker (caddr entry) nil))
292   (gnus-async-with-semaphore
293    (setq gnus-async-article-alist
294          (delq entry gnus-async-article-alist))))
295
296 (defun gnus-async-prefetch-remove-group (group)
297   "Remove all articles belonging to GROUP from the prefetch buffer."
298   (when (and (gnus-group-asynchronous-p group)
299              (memq 'exit gnus-prefetched-article-deletion-strategy))
300     (let ((alist gnus-async-article-alist))
301       (save-excursion
302         (gnus-async-set-buffer)
303         (while alist
304           (when (equal group (nth 3 (car alist)))
305             (gnus-async-delete-prefetched-entry (car alist)))
306           (pop alist))))))
307
308 (defun gnus-async-prefetched-article-entry (group article)
309   "Return the entry for ARTICLE in GROUP iff it has been prefetched."
310   (let ((entry (save-excursion
311                  (gnus-async-set-buffer)
312                  (assq (intern (format "%s-%d" group article)
313                                gnus-async-hashtb)
314                        gnus-async-article-alist))))
315     ;; Perhaps something has emptied the buffer?
316     (if (and entry
317              (= (cadr entry) (caddr entry)))
318         (progn
319           (ignore-errors
320             (set-marker (cadr entry) nil)
321             (set-marker (caddr entry) nil))
322           (setq gnus-async-article-alist
323                 (delq entry gnus-async-article-alist))
324           nil)
325       entry)))
326
327 ;;;
328 ;;; Header prefetch
329 ;;;
330
331 (defun gnus-async-prefetch-headers (group)
332   "Prefetch the headers for group GROUP."
333   (save-excursion
334     (let (unread)
335       (when (and gnus-use-header-prefetch
336                  gnus-asynchronous
337                  (gnus-group-asynchronous-p group)
338                  (listp gnus-async-header-prefetched)
339                  (setq unread (gnus-list-of-unread-articles group)))
340         ;; Mark that a fetch is in progress.
341         (setq gnus-async-header-prefetched t)
342         (nnheader-set-temp-buffer gnus-async-prefetch-headers-buffer t)
343         (erase-buffer)
344         (let ((nntp-server-buffer (current-buffer))
345               (nnheader-callback-function
346                `(lambda (arg)
347                   (setq gnus-async-header-prefetched
348                         ,(cons group unread)))))
349           (gnus-retrieve-headers unread group gnus-fetch-old-headers))))))
350
351 (defun gnus-async-retrieve-fetched-headers (articles group)
352   "See whether we have prefetched headers."
353   (when (and gnus-use-header-prefetch
354              (gnus-group-asynchronous-p group)
355              (listp gnus-async-header-prefetched)
356              (equal group (car gnus-async-header-prefetched))
357              (equal articles (cdr gnus-async-header-prefetched)))
358     (save-excursion
359       (nnheader-set-temp-buffer gnus-async-prefetch-headers-buffer t)
360       (nntp-decode-text)
361       (copy-to-buffer nntp-server-buffer (point-min) (point-max))
362       (erase-buffer)
363       (setq gnus-async-header-prefetched nil)
364       t)))
365
366 (provide 'gnus-async)
367
368 ;;; gnus-async.el ends here