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