*** 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
52 (defvar gnus-async-prefetch-headers-buffer " *Async Prefetch Headers*")
53 (defvar gnus-asynch-header-prefetched nil)
54
55 ;;; Utility functions.
56
57 (defun gnus-group-asynchronous-p (group)
58   "Say whether GROUP is fetched from a server that supports asynchronocity."
59   (gnus-asynchronous-p (gnus-find-method-for-group group)))
60
61 ;;;
62 ;;; Article prefetch
63 ;;;
64
65 (gnus-add-shutdown 'gnus-async-close 'gnus)
66 (defun gnus-async-close ()
67   (gnus-kill-buffer gnus-async-prefetch-article-buffer)
68   (gnus-kill-buffer gnus-async-prefetch-headers-buffer)
69   (setq gnus-async-article-alist nil
70         gnus-asynch-header-prefetched nil))
71
72 (defun gnus-async-prefetch-next (group article summary)
73   "Possibly prefetch several articles starting with the article after ARTICLE."
74   (let ((next (caadr (gnus-data-find-list article))))
75     (when next
76       (gnus-async-prefetch-article group next summary))))
77
78 (defun gnus-async-prefetch-article (group article summary &optional number)
79   "Possibly prefetch several articles starting with ARTICLE."
80   (unless number
81     (setq number gnus-use-article-prefetch))
82   (when (and number
83              (or (not (numberp number))
84                  (> number 0))
85              (gnus-group-asynchronous-p group)
86              (gnus-buffer-live-p summary))
87     (when (numberp number)
88       (decf number))
89     (while (and article (gnus-async-prefetched-article-entry group article))
90       (setq article (caadr (gnus-data-find-list article)))
91       (when (numberp number)
92         (decf number)))
93     (when article
94       ;; We want to fetch some more articles.
95       (save-excursion
96         (set-buffer summary)
97         (let ((next (caadr (gnus-data-find-list article)))
98               mark)
99           (nnheader-set-temp-buffer gnus-async-prefetch-article-buffer t)
100           (goto-char (point-max))
101           (setq mark (point-marker))
102           (let ((nnheader-callback-function
103                  `(lambda (arg)
104                     (save-excursion
105                       (nnheader-set-temp-buffer
106                        gnus-async-prefetch-article-buffer t)
107                       (push (list ',(intern (format "%s-%d" group article))
108                                   ,mark (set-marker (make-marker) (point-max))
109                                   ,group ,article)
110                             gnus-async-article-alist)
111                       (when (gnus-buffer-live-p ,summary)
112                         ,(when next
113                            `(gnus-async-prefetch-article 
114                              ,group ,next ,summary ,number))))))
115                 (nntp-server-buffer (get-buffer
116                                      gnus-async-prefetch-article-buffer)))
117             (gnus-message 7 "Prefetching article %d in group %s"
118                           article group)
119             (gnus-request-article article group)))))))
120
121 (defun gnus-async-request-fetched-article (group article buffer)
122   "See whether we have ARTICLE from GROUP and put it in BUFFER."
123   (when (numberp article)
124     (let ((entry (gnus-async-prefetched-article-entry group article)))
125       (when entry
126         (save-excursion
127           (nnheader-set-temp-buffer gnus-async-prefetch-article-buffer t)
128           (copy-to-buffer buffer (cadr entry) (caddr entry))
129           ;; Remove the read article from the prefetch buffer.
130           (when (memq 'read gnus-prefetched-article-deletion-strategy)
131             (gnus-asynch-delete-prefected-entry entry))
132           ;; Decode the article.  Perhaps this shouldn't be done
133           ;; here?
134           (set-buffer buffer)
135           (nntp-decode-text)
136           (goto-char (point-min))
137           (gnus-delete-line)
138           t)))))
139
140 (defun gnus-asynch-delete-prefected-entry (entry)
141   "Delete ENTRY from buffer and alist."
142   (delete-region (cadr entry) (caddr entry))
143   (set-marker (cadr entry) nil)
144   (set-marker (caddr entry) nil)
145   (setq gnus-async-article-alist 
146         (delq entry gnus-async-article-alist)))
147
148 (defun gnus-async-prefetch-remove-group (group)
149   "Remove all articles belonging to GROUP from the prefetch buffer."
150   (when (and (gnus-group-asynchronous-p group)
151              (memq 'exit gnus-prefetched-article-deletion-strategy))
152     (let ((alist gnus-async-article-alist))
153       (save-excursion
154         (nnheader-set-temp-buffer gnus-async-prefetch-article-buffer t)
155         (while alist
156           (when (equal group (nth 3 (car alist)))
157             (gnus-asynch-delete-prefected-entry (car alist)))
158           (pop alist))))))
159           
160 (defun gnus-async-prefetched-article-entry (group article)
161   "Return the entry for ARTICLE in GROUP iff it has been prefetched."
162   (assq (intern (format "%s-%d" group article))
163         gnus-async-article-alist))
164
165 ;;;
166 ;;; Header prefetch
167 ;;;
168
169 (defun gnus-async-prefetch-headers (group)
170   "Prefetch the headers for group GROUP."
171   (save-excursion
172     (let (unread)
173       (when (and gnus-use-header-prefetch
174                  (gnus-group-asynchronous-p group)
175                  (listp gnus-asynch-header-prefetched)
176                  (setq unread (gnus-list-of-unread-articles group)))
177         ;; Mark that a fetch is in progress.
178         (setq gnus-asynch-header-prefetched t)
179         (nnheader-set-temp-buffer gnus-async-prefetch-headers-buffer t)
180         (erase-buffer)
181         (let ((nntp-server-buffer (current-buffer))
182               (nnheader-callback-function
183                `(lambda (arg)
184                   (setq gnus-asynch-header-prefetched
185                         ,(cons group unread)))))
186           (gnus-retrieve-headers unread group gnus-fetch-old-headers))))))
187
188 (defun gnus-asynch-retrieve-fetched-headers (articles group)
189   "See whether we have prefetched headers."
190   (when (and gnus-use-header-prefetch
191              (gnus-group-asynchronous-p group)
192              (listp gnus-asynch-header-prefetched)
193              (equal group (car gnus-asynch-header-prefetched))
194              (equal articles (cdr gnus-asynch-header-prefetched)))
195     (save-excursion
196       (nnheader-set-temp-buffer gnus-async-prefetch-headers-buffer t)
197       (nntp-decode-text)
198       (copy-to-buffer nntp-server-buffer (point-min) (point-max))
199       (erase-buffer)
200       (setq gnus-asynch-header-prefetched nil)
201       t)))
202   
203 (provide 'gnus-async)
204
205 ;;; gnus-async.el ends here