*** 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           ;; Decode the article.  Perhaps this shouldn't be done
170           ;; here?
171           (set-buffer buffer)
172           (nntp-decode-text)
173           (goto-char (point-min))
174           (gnus-delete-line)
175           t)))))
176
177 (defun gnus-async-delete-prefected-entry (entry)
178   "Delete ENTRY from buffer and alist."
179   (delete-region (cadr entry) (caddr entry))
180   (set-marker (cadr entry) nil)
181   (set-marker (caddr entry) nil)
182   (gnus-async-get-semaphore 'gnus-async-article-semaphore)
183   (setq gnus-async-article-alist 
184         (delq entry gnus-async-article-alist))
185   (gnus-async-release-semaphore 'gnus-async-article-semaphore))
186
187 (defun gnus-async-prefetch-remove-group (group)
188   "Remove all articles belonging to GROUP from the prefetch buffer."
189   (when (and (gnus-group-asynchronous-p group)
190              (memq 'exit gnus-prefetched-article-deletion-strategy))
191     (let ((alist gnus-async-article-alist))
192       (save-excursion
193         (gnus-async-set-buffer)
194         (while alist
195           (when (equal group (nth 3 (car alist)))
196             (gnus-async-delete-prefected-entry (car alist)))
197           (pop alist))))))
198           
199 (defun gnus-async-prefetched-article-entry (group article)
200   "Return the entry for ARTICLE in GROUP iff it has been prefetched."
201   (assq (intern (format "%s-%d" group article))
202         gnus-async-article-alist))
203
204 ;;;
205 ;;; Header prefetch
206 ;;;
207
208 (defun gnus-async-prefetch-headers (group)
209   "Prefetch the headers for group GROUP."
210   (save-excursion
211     (let (unread)
212       (when (and gnus-use-header-prefetch
213                  (gnus-group-asynchronous-p group)
214                  (listp gnus-async-header-prefetched)
215                  (setq unread (gnus-list-of-unread-articles group)))
216         ;; Mark that a fetch is in progress.
217         (setq gnus-async-header-prefetched t)
218         (nnheader-set-temp-buffer gnus-async-prefetch-headers-buffer t)
219         (erase-buffer)
220         (let ((nntp-server-buffer (current-buffer))
221               (nnheader-callback-function
222                `(lambda (arg)
223                   (setq gnus-async-header-prefetched
224                         ,(cons group unread)))))
225           (gnus-retrieve-headers unread group gnus-fetch-old-headers))))))
226
227 (defun gnus-async-retrieve-fetched-headers (articles group)
228   "See whether we have prefetched headers."
229   (when (and gnus-use-header-prefetch
230              (gnus-group-asynchronous-p group)
231              (listp gnus-async-header-prefetched)
232              (equal group (car gnus-async-header-prefetched))
233              (equal articles (cdr gnus-async-header-prefetched)))
234     (save-excursion
235       (nnheader-set-temp-buffer gnus-async-prefetch-headers-buffer t)
236       (nntp-decode-text)
237       (copy-to-buffer nntp-server-buffer (point-min) (point-max))
238       (erase-buffer)
239       (setq gnus-async-header-prefetched nil)
240       t)))
241   
242 (provide 'gnus-async)
243
244 ;;; gnus-async.el ends here