*** 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 next
97     (gnus-async-get-semaphore 'gnus-async-article-semaphore)
98     (pop gnus-async-fetch-list)
99     (gnus-async-release-semaphore 'gnus-async-article-semaphore))
100   (let ((do-fetch next))
101     (when (and (gnus-group-asynchronous-p group)
102                (gnus-buffer-live-p summary)
103                (or (not next)
104                    gnus-async-fetch-list))
105       (unwind-protect
106           (progn
107             (gnus-async-get-semaphore 'gnus-async-article-semaphore)
108             (unless next
109               (setq do-fetch (not gnus-async-fetch-list))
110               ;; Nix out any outstanding requests.
111               (setq gnus-async-fetch-list nil)
112               ;; Fill in the new list.
113               (let ((n gnus-use-article-prefetch)
114                     (data (gnus-data-find-list article))
115                     d)
116                 (while (and (setq d (pop data))
117                             (if (numberp n) 
118                                 (natnump (decf n))
119                               n))
120                   (unless (or (gnus-async-prefetched-article-entry
121                                group (setq article (gnus-data-number d)))
122                               (not (natnump article)))
123                     ;; Not already fetched -- so we add it to the list.
124                     (push article gnus-async-fetch-list)))
125                 (setq gnus-async-fetch-list (nreverse gnus-async-fetch-list))))
126
127             (when do-fetch
128               (setq article (pop gnus-async-fetch-list))))
129         
130         (gnus-async-release-semaphore 'gnus-async-article-semaphore))
131     
132       (when article
133         ;; We want to fetch some more articles.
134         (save-excursion
135           (set-buffer summary)
136           (let (mark)
137             (gnus-async-set-buffer)
138             (goto-char (point-max))
139             (setq mark (point-marker))
140             (let ((nnheader-callback-function
141                    (gnus-make-async-article-function 
142                     group article mark summary next))
143                   (nntp-server-buffer (get-buffer
144                                        gnus-async-prefetch-article-buffer)))
145               (gnus-message 7 "Prefetching article %d in group %s"
146                             article group)
147               (gnus-request-article article group))))))))
148
149 (defun gnus-make-async-article-function (group article mark summary next)
150   "Return a callback function."
151   `(lambda (arg)
152      (save-excursion
153        (gnus-async-set-buffer)
154        (gnus-async-get-semaphore 'gnus-async-article-semaphore)
155        (push (list ',(intern (format "%s-%d" group article))
156                    ,mark (set-marker (make-marker)
157                                      (point-max))
158                    ,group ,article)
159              gnus-async-article-alist)
160        (gnus-async-release-semaphore
161         'gnus-async-article-semaphore)
162        (when (gnus-buffer-live-p ,summary)
163          (gnus-async-prefetch-article 
164           ,group ,next ,summary t)))))
165
166 (defun gnus-async-request-fetched-article (group article buffer)
167   "See whether we have ARTICLE from GROUP and put it in BUFFER."
168   (when (numberp article)
169     (let ((entry (gnus-async-prefetched-article-entry group article)))
170       (when entry
171         (save-excursion
172           (gnus-async-set-buffer)
173           (copy-to-buffer buffer (cadr entry) (caddr entry))
174           ;; Remove the read article from the prefetch buffer.
175           (when (memq 'read gnus-prefetched-article-deletion-strategy)
176             (gnus-async-delete-prefected-entry entry))
177           t)))))
178
179 (defun gnus-async-delete-prefected-entry (entry)
180   "Delete ENTRY from buffer and alist."
181   (delete-region (cadr entry) (caddr entry))
182   (set-marker (cadr entry) nil)
183   (set-marker (caddr entry) nil)
184   (gnus-async-get-semaphore 'gnus-async-article-semaphore)
185   (setq gnus-async-article-alist 
186         (delq entry gnus-async-article-alist))
187   (gnus-async-release-semaphore 'gnus-async-article-semaphore))
188
189 (defun gnus-async-prefetch-remove-group (group)
190   "Remove all articles belonging to GROUP from the prefetch buffer."
191   (when (and (gnus-group-asynchronous-p group)
192              (memq 'exit gnus-prefetched-article-deletion-strategy))
193     (let ((alist gnus-async-article-alist))
194       (save-excursion
195         (gnus-async-set-buffer)
196         (while alist
197           (when (equal group (nth 3 (car alist)))
198             (gnus-async-delete-prefected-entry (car alist)))
199           (pop alist))))))
200           
201 (defun gnus-async-prefetched-article-entry (group article)
202   "Return the entry for ARTICLE in GROUP iff it has been prefetched."
203   (assq (intern (format "%s-%d" group article))
204         gnus-async-article-alist))
205
206 ;;;
207 ;;; Header prefetch
208 ;;;
209
210 (defun gnus-async-prefetch-headers (group)
211   "Prefetch the headers for group GROUP."
212   (save-excursion
213     (let (unread)
214       (when (and gnus-use-header-prefetch
215                  (gnus-group-asynchronous-p group)
216                  (listp gnus-async-header-prefetched)
217                  (setq unread (gnus-list-of-unread-articles group)))
218         ;; Mark that a fetch is in progress.
219         (setq gnus-async-header-prefetched t)
220         (nnheader-set-temp-buffer gnus-async-prefetch-headers-buffer t)
221         (erase-buffer)
222         (let ((nntp-server-buffer (current-buffer))
223               (nnheader-callback-function
224                `(lambda (arg)
225                   (setq gnus-async-header-prefetched
226                         ,(cons group unread)))))
227           (gnus-retrieve-headers unread group gnus-fetch-old-headers))))))
228
229 (defun gnus-async-retrieve-fetched-headers (articles group)
230   "See whether we have prefetched headers."
231   (when (and gnus-use-header-prefetch
232              (gnus-group-asynchronous-p group)
233              (listp gnus-async-header-prefetched)
234              (equal group (car gnus-async-header-prefetched))
235              (equal articles (cdr gnus-async-header-prefetched)))
236     (save-excursion
237       (nnheader-set-temp-buffer gnus-async-prefetch-headers-buffer t)
238       (nntp-decode-text)
239       (copy-to-buffer nntp-server-buffer (point-min) (point-max))
240       (erase-buffer)
241       (setq gnus-async-header-prefetched nil)
242       t)))
243   
244 (provide 'gnus-async)
245
246 ;;; gnus-async.el ends here