*** 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 (defmacro gnus-async-with-semaphore (&rest forms)
79   `(unwind-protect
80        (progn
81          (gnus-async-get-semaphore 'gnus-async-article-semaphore)
82          ,@forms)
83      (gnus-async-release-semaphore 'gnus-async-article-semaphore)))
84
85 (put 'gnus-asynch-with-semaphore 'lisp-indent-function 0)
86 (put 'gnus-asynch-with-semaphore 'lisp-indent-hook 0)
87 (put 'gnus-asynch-with-semaphore 'edebug-form-spec '(body))
88         
89 ;;;
90 ;;; Article prefetch
91 ;;;
92
93 (gnus-add-shutdown 'gnus-async-close 'gnus)
94 (defun gnus-async-close ()
95   (gnus-kill-buffer gnus-async-prefetch-article-buffer)
96   (gnus-kill-buffer gnus-async-prefetch-headers-buffer)
97   (setq gnus-async-article-alist nil
98         gnus-async-header-prefetched nil))
99
100 (defun gnus-async-set-buffer ()
101   (nnheader-set-temp-buffer gnus-async-prefetch-article-buffer t))
102
103 (defun gnus-async-prefetch-next (group article summary)
104   "Possibly prefetch several articles starting with the article after ARTICLE."
105   (when (and (gnus-buffer-live-p summary)
106              gnus-asynchronous
107              (gnus-group-asynchronous-p group))
108     (save-excursion
109       (set-buffer gnus-summary-buffer)
110       (let ((next (caadr (gnus-data-find-list article))))
111         (when next
112           (if (not (fboundp 'run-with-idle-timer))
113               ;; This is either an older Emacs or XEmacs, so we
114               ;; do this, which leads to slightly slower article
115               ;; buffer display.
116               (gnus-async-prefetch-article group next summary)
117             (run-with-idle-timer 
118              0.1 nil 'gnus-async-prefetch-article group next summary)))))))
119
120 (defun gnus-async-prefetch-article (group article summary &optional next)
121   "Possibly prefetch several articles starting with ARTICLE."
122   (if (not (gnus-buffer-live-p summary))
123       (gnus-async-with-semaphore
124        (setq gnus-async-fetch-list nil))
125     (when (and gnus-asynchronous
126                (gnus-alive-p))
127       (when next
128         (gnus-async-with-semaphore
129          (pop gnus-async-fetch-list)))
130       (let ((do-fetch next))
131         (when (and (gnus-group-asynchronous-p group)
132                    (gnus-buffer-live-p summary)
133                    (or (not next)
134                        gnus-async-fetch-list))
135           (gnus-async-with-semaphore
136            (unless next
137              (setq do-fetch (not gnus-async-fetch-list))
138              ;; Nix out any outstanding requests.
139              (setq gnus-async-fetch-list nil)
140              ;; Fill in the new list.
141              (let ((n gnus-use-article-prefetch)
142                    (data (gnus-data-find-list article))
143                    d)
144                (while (and (setq d (pop data))
145                            (if (numberp n) 
146                                (natnump (decf n))
147                              n))
148                  (unless (or (gnus-async-prefetched-article-entry
149                               group (setq article (gnus-data-number d)))
150                              (not (natnump article)))
151                    ;; Not already fetched -- so we add it to the list.
152                    (push article gnus-async-fetch-list)))
153                (setq gnus-async-fetch-list
154                      (nreverse gnus-async-fetch-list))))
155
156            (when do-fetch
157              (setq article (car gnus-async-fetch-list))))
158     
159           (when (and do-fetch article)
160             ;; We want to fetch some more articles.
161             (save-excursion
162               (set-buffer summary)
163               (let (mark)
164                 (gnus-async-set-buffer)
165                 (goto-char (point-max))
166                 (setq mark (point-marker))
167                 (let ((nnheader-callback-function
168                        (gnus-make-async-article-function 
169                         group article mark summary next))
170                       (nntp-server-buffer 
171                        (get-buffer gnus-async-prefetch-article-buffer)))
172                   (gnus-message 7 "Prefetching article %d in group %s"
173                                 article group)
174                   (gnus-request-article article group))))))))))
175
176 (defun gnus-make-async-article-function (group article mark summary next)
177   "Return a callback function."
178   `(lambda (arg)
179      (save-excursion
180        (gnus-async-set-buffer)
181        (gnus-async-with-semaphore
182         (push (list ',(intern (format "%s-%d" group article))
183                     ,mark (set-marker (make-marker)
184                                       (point-max))
185                     ,group ,article)
186               gnus-async-article-alist))
187        (if (not (gnus-buffer-live-p ,summary))
188            (gnus-async-with-semaphore
189             (setq gnus-async-fetch-list nil))
190          (gnus-async-prefetch-article ,group ,next ,summary t)))))
191
192 (defun gnus-async-request-fetched-article (group article buffer)
193   "See whether we have ARTICLE from GROUP and put it in BUFFER."
194   (when (numberp article)
195     (let ((entry (gnus-async-prefetched-article-entry group article)))
196       (when entry
197         (save-excursion
198           (gnus-async-set-buffer)
199           (copy-to-buffer buffer (cadr entry) (caddr entry))
200           ;; Remove the read article from the prefetch buffer.
201           (when (memq 'read gnus-prefetched-article-deletion-strategy)
202             (gnus-async-delete-prefected-entry entry))
203           t)))))
204
205 (defun gnus-async-delete-prefected-entry (entry)
206   "Delete ENTRY from buffer and alist."
207   (delete-region (cadr entry) (caddr entry))
208   (set-marker (cadr entry) nil)
209   (set-marker (caddr entry) nil)
210   (gnus-async-with-semaphore
211    (setq gnus-async-article-alist 
212          (delq entry gnus-async-article-alist))))
213
214 (defun gnus-async-prefetch-remove-group (group)
215   "Remove all articles belonging to GROUP from the prefetch buffer."
216   (when (and (gnus-group-asynchronous-p group)
217              (memq 'exit gnus-prefetched-article-deletion-strategy))
218     (let ((alist gnus-async-article-alist))
219       (save-excursion
220         (gnus-async-set-buffer)
221         (while alist
222           (when (equal group (nth 3 (car alist)))
223             (gnus-async-delete-prefected-entry (car alist)))
224           (pop alist))))))
225           
226 (defun gnus-async-prefetched-article-entry (group article)
227   "Return the entry for ARTICLE in GROUP iff it has been prefetched."
228   (assq (intern (format "%s-%d" group article))
229         gnus-async-article-alist))
230
231 ;;;
232 ;;; Header prefetch
233 ;;;
234
235 (defun gnus-async-prefetch-headers (group)
236   "Prefetch the headers for group GROUP."
237   (save-excursion
238     (let (unread)
239       (when (and gnus-use-header-prefetch
240                  gnus-asynchronous
241                  (gnus-group-asynchronous-p group)
242                  (listp gnus-async-header-prefetched)
243                  (setq unread (gnus-list-of-unread-articles group)))
244         ;; Mark that a fetch is in progress.
245         (setq gnus-async-header-prefetched t)
246         (nnheader-set-temp-buffer gnus-async-prefetch-headers-buffer t)
247         (erase-buffer)
248         (let ((nntp-server-buffer (current-buffer))
249               (nnheader-callback-function
250                `(lambda (arg)
251                   (setq gnus-async-header-prefetched
252                         ,(cons group unread)))))
253           (gnus-retrieve-headers unread group gnus-fetch-old-headers))))))
254
255 (defun gnus-async-retrieve-fetched-headers (articles group)
256   "See whether we have prefetched headers."
257   (when (and gnus-use-header-prefetch
258              (gnus-group-asynchronous-p group)
259              (listp gnus-async-header-prefetched)
260              (equal group (car gnus-async-header-prefetched))
261              (equal articles (cdr gnus-async-header-prefetched)))
262     (save-excursion
263       (nnheader-set-temp-buffer gnus-async-prefetch-headers-buffer t)
264       (nntp-decode-text)
265       (copy-to-buffer nntp-server-buffer (point-min) (point-max))
266       (erase-buffer)
267       (setq gnus-async-header-prefetched nil)
268       t)))
269   
270 (provide 'gnus-async)
271
272 ;;; gnus-async.el ends here