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