1 ;;; gnus-async.el --- asynchronous support for Gnus
2 ;; Copyright (C) 1996 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
7 ;; This file is part of GNU Emacs.
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)
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.
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.
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.")
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
44 (defvar gnus-use-header-prefetch nil
45 "*If non-nil, prefetch the headers to the next group.")
47 ;;; Internal variables.
49 (defvar gnus-async-prefetch-article-buffer " *Async Prefetch Article*")
50 (defvar gnus-async-article-alist nil)
52 (defvar gnus-async-prefetch-headers-buffer " *Async Prefetch Headers*")
53 (defvar gnus-asynch-header-prefetched nil)
55 ;;; Utility functions.
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)))
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))
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))))
76 (gnus-async-prefetch-article group next summary))))
78 (defun gnus-async-prefetch-article (group article summary &optional number)
79 "Possibly prefetch several articles starting with ARTICLE."
81 (setq number gnus-use-article-prefetch))
83 (or (not (numberp number))
85 (gnus-group-asynchronous-p group)
86 (gnus-buffer-live-p summary))
87 (when (numberp 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)
94 ;; We want to fetch some more articles.
97 (let ((next (caadr (gnus-data-find-list article)))
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
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))
110 gnus-async-article-alist)
111 (when (gnus-buffer-live-p ,summary)
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"
119 (gnus-request-article article group)))))))
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)))
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
136 (goto-char (point-min))
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)))
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))
154 (nnheader-set-temp-buffer gnus-async-prefetch-article-buffer t)
156 (when (equal group (nth 3 (car alist)))
157 (gnus-asynch-delete-prefected-entry (car alist)))
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))
169 (defun gnus-async-prefetch-headers (group)
170 "Prefetch the headers for group GROUP."
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)
181 (let ((nntp-server-buffer (current-buffer))
182 (nnheader-callback-function
184 (setq gnus-asynch-header-prefetched
185 ,(cons group unread)))))
186 (gnus-retrieve-headers unread group gnus-fetch-old-headers))))))
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)))
196 (nnheader-set-temp-buffer gnus-async-prefetch-headers-buffer t)
198 (copy-to-buffer nntp-server-buffer (point-min) (point-max))
200 (setq gnus-asynch-header-prefetched nil)
203 (provide 'gnus-async)
205 ;;; gnus-async.el ends here