*** empty log message ***
[gnus] / lisp / gnus-async.el
1 ;;; gnus-async.el --- asynchronous support for Gnus
2 ;; Copyright (C) 1996 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
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 (require 'gnus-load)
29 (require 'gnus-sum)
30 (require 'nntp)
31
32 (defvar gnus-use-article-prefetch 30
33   "*If non-nil, prefetch articles in groups that allow this.
34 If a number, prefetch only that many articles forward;
35 if t, prefetch as many articles as possible.")
36
37 (defvar gnus-prefetched-article-deletion-strategy '(read exit)
38   "List of symbols that say when to remove articles from the prefetch buffer.
39 Possible values in this list are `read', which means that 
40 articles are removed as they are read, and `exit', which means
41 that all articles belonging to a group are removed on exit
42 from that group.")
43
44 (defvar gnus-use-header-prefetch nil
45   "*If non-nil, prefetch the headers to the next group.")
46
47 ;;; Internal variables.
48
49 (defvar gnus-async-prefetch-article-buffer " *Async Prefetch Article*")
50 (defvar gnus-async-article-alist nil)
51 (defvar gnus-async-article-semaphore '(nil))
52 (defvar gnus-async-fetch-list nil)
53
54 (defvar gnus-async-prefetch-headers-buffer " *Async Prefetch Headers*")
55 (defvar gnus-async-header-prefetched nil)
56
57 ;;; Utility functions.
58
59 (defun gnus-group-asynchronous-p (group)
60   "Say whether GROUP is fetched from a server that supports asynchronocity."
61   (gnus-asynchronous-p (gnus-find-method-for-group group)))
62
63 ;;; Somewhat bogus semaphores.
64
65 (defun gnus-async-get-semaphore (semaphore)
66   "Wait until SEMAPHORE is released."
67   (while (/= (length (nconc (symbol-value semaphore) (list nil))) 2)
68     (sleep-for 1)))
69
70 (defun gnus-async-release-semaphore (semaphore)
71   "Release SEMAPHORE."
72   (setcdr (symbol-value semaphore) nil))
73
74 ;;;
75 ;;; Article prefetch
76 ;;;
77
78 (gnus-add-shutdown 'gnus-async-close 'gnus)
79 (defun gnus-async-close ()
80   (gnus-kill-buffer gnus-async-prefetch-article-buffer)
81   (gnus-kill-buffer gnus-async-prefetch-headers-buffer)
82   (setq gnus-async-article-alist nil
83         gnus-async-header-prefetched nil))
84
85 (defun gnus-async-set-buffer ()
86   (nnheader-set-temp-buffer gnus-async-prefetch-article-buffer t))
87
88 (defun gnus-async-prefetch-next (group article summary)
89   "Possibly prefetch several articles starting with the article after ARTICLE."
90   (let ((next (caadr (gnus-data-find-list article))))
91     (when next
92       (gnus-async-prefetch-article group next summary))))
93
94 (defun gnus-async-prefetch-article (group article summary &optional next)
95   "Possibly prefetch several articles starting with ARTICLE."
96   (when (and (gnus-group-asynchronous-p group)
97              (gnus-buffer-live-p summary)
98              (or (not next)
99                  gnus-async-fetch-list))
100     (unwind-protect
101         (progn
102           (gnus-async-get-semaphore 'gnus-async-article-semaphore)
103           (unless next
104             ;; Nix out any outstanding requests.
105             (setq gnus-async-fetch-list nil)
106             ;; Fill in the new list.
107             (let ((n gnus-use-article-prefetch)
108                   (data (gnus-data-find-list article))
109                   d)
110               (while (and (setq d (pop data))
111                           (if (numberp n) 
112                               (natnump (decf n))
113                             n))
114                 (unless (gnus-async-prefetched-article-entry
115                          group (setq article (gnus-data-number d)))
116                   ;; Not already fetched -- so we add it to the list.
117                   (push article gnus-async-fetch-list)))
118               (setq gnus-async-fetch-list (nreverse gnus-async-fetch-list))))
119
120           (setq article (pop gnus-async-fetch-list)))
121         
122       (gnus-async-release-semaphore 'gnus-async-article-semaphore))
123     
124     (when article
125       ;; We want to fetch some more articles.
126       (save-excursion
127         (set-buffer summary)
128         (let (mark)
129           (gnus-async-set-buffer)
130           (goto-char (point-max))
131           (setq mark (point-marker))
132           (let ((nnheader-callback-function
133                  (gnus-make-async-article-function 
134                   group article mark summary next))
135                 (nntp-server-buffer (get-buffer
136                                      gnus-async-prefetch-article-buffer)))
137             (gnus-message 7 "Prefetching article %d in group %s"
138                           article group)
139             (gnus-request-article article group)))))))
140
141 (defun gnus-make-async-article-function (group article mark summary next)
142   "Return a callback function."
143   `(lambda (arg)
144      (save-excursion
145        (gnus-async-set-buffer)
146        (gnus-async-get-semaphore 'gnus-async-article-semaphore)
147        (push (list ',(intern (format "%s-%d" group article))
148                    ,mark (set-marker (make-marker)
149                                      (point-max))
150                    ,group ,article)
151              gnus-async-article-alist)
152        (gnus-async-release-semaphore
153         'gnus-async-article-semaphore)
154        (when (gnus-buffer-live-p ,summary)
155          (gnus-async-prefetch-article 
156           ,group ,next ,summary t)))))
157
158 (defun gnus-async-request-fetched-article (group article buffer)
159   "See whether we have ARTICLE from GROUP and put it in BUFFER."
160   (when (numberp article)
161     (let ((entry (gnus-async-prefetched-article-entry group article)))
162       (when entry
163         (save-excursion
164           (gnus-async-set-buffer)
165           (copy-to-buffer buffer (cadr entry) (caddr entry))
166           ;; Remove the read article from the prefetch buffer.
167           (when (memq 'read gnus-prefetched-article-deletion-strategy)
168             (gnus-async-delete-prefected-entry entry))
169           t)))))
170
171 (defun gnus-async-delete-prefected-entry (entry)
172   "Delete ENTRY from buffer and alist."
173   (delete-region (cadr entry) (caddr entry))
174   (set-marker (cadr entry) nil)
175   (set-marker (caddr entry) nil)
176   (gnus-async-get-semaphore 'gnus-async-article-semaphore)
177   (setq gnus-async-article-alist 
178         (delq entry gnus-async-article-alist))
179   (gnus-async-release-semaphore 'gnus-async-article-semaphore))
180
181 (defun gnus-async-prefetch-remove-group (group)
182   "Remove all articles belonging to GROUP from the prefetch buffer."
183   (when (and (gnus-group-asynchronous-p group)
184              (memq 'exit gnus-prefetched-article-deletion-strategy))
185     (let ((alist gnus-async-article-alist))
186       (save-excursion
187         (gnus-async-set-buffer)
188         (while alist
189           (when (equal group (nth 3 (car alist)))
190             (gnus-async-delete-prefected-entry (car alist)))
191           (pop alist))))))
192           
193 (defun gnus-async-prefetched-article-entry (group article)
194   "Return the entry for ARTICLE in GROUP iff it has been prefetched."
195   (assq (intern (format "%s-%d" group article))
196         gnus-async-article-alist))
197
198 ;;;
199 ;;; Header prefetch
200 ;;;
201
202 (defun gnus-async-prefetch-headers (group)
203   "Prefetch the headers for group GROUP."
204   (save-excursion
205     (let (unread)
206       (when (and gnus-use-header-prefetch
207                  (gnus-group-asynchronous-p group)
208                  (listp gnus-async-header-prefetched)
209                  (setq unread (gnus-list-of-unread-articles group)))
210         ;; Mark that a fetch is in progress.
211         (setq gnus-async-header-prefetched t)
212         (nnheader-set-temp-buffer gnus-async-prefetch-headers-buffer t)
213         (erase-buffer)
214         (let ((nntp-server-buffer (current-buffer))
215               (nnheader-callback-function
216                `(lambda (arg)
217                   (setq gnus-async-header-prefetched
218                         ,(cons group unread)))))
219           (gnus-retrieve-headers unread group gnus-fetch-old-headers))))))
220
221 (defun gnus-async-retrieve-fetched-headers (articles group)
222   "See whether we have prefetched headers."
223   (when (and gnus-use-header-prefetch
224              (gnus-group-asynchronous-p group)
225              (listp gnus-async-header-prefetched)
226              (equal group (car gnus-async-header-prefetched))
227              (equal articles (cdr gnus-async-header-prefetched)))
228     (save-excursion
229       (nnheader-set-temp-buffer gnus-async-prefetch-headers-buffer t)
230       (nntp-decode-text)
231       (copy-to-buffer nntp-server-buffer (point-min) (point-max))
232       (erase-buffer)
233       (setq gnus-async-header-prefetched nil)
234       t)))
235   
236 (provide 'gnus-async)
237
238 ;;; gnus-async.el ends here