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-asynchronous t
33 "*If nil, inhibit all Gnus asynchronicity.
34 If non-nil, let the other asynch variables be heeded.")
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.")
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
48 (defvar gnus-use-header-prefetch nil
49 "*If non-nil, prefetch the headers to the next group.")
51 ;;; Internal variables.
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)
58 (defvar gnus-async-prefetch-headers-buffer " *Async Prefetch Headers*")
59 (defvar gnus-async-header-prefetched nil)
61 ;;; Utility functions.
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)))
67 ;;; Somewhat bogus semaphores.
69 (defun gnus-async-get-semaphore (semaphore)
70 "Wait until SEMAPHORE is released."
71 (while (/= (length (nconc (symbol-value semaphore) (list nil))) 2)
74 (defun gnus-async-release-semaphore (semaphore)
76 (setcdr (symbol-value semaphore) nil))
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))
89 (defun gnus-async-set-buffer ()
90 (nnheader-set-temp-buffer gnus-async-prefetch-article-buffer t))
92 (defun gnus-async-prefetch-next (group article summary)
93 "Possibly prefetch several articles starting with the article after ARTICLE."
94 (let ((next (caadr (gnus-data-find-list article))))
96 (gnus-async-prefetch-article group next summary))))
98 (defun gnus-async-prefetch-article (group article summary &optional next)
99 "Possibly prefetch several articles starting with ARTICLE."
100 (when (and gnus-asynchronous
103 (gnus-async-get-semaphore 'gnus-async-article-semaphore)
104 (pop gnus-async-fetch-list)
105 (gnus-async-release-semaphore 'gnus-async-article-semaphore))
106 (let ((do-fetch next))
107 (when (and (gnus-group-asynchronous-p group)
108 (gnus-buffer-live-p summary)
110 gnus-async-fetch-list))
113 (gnus-async-get-semaphore 'gnus-async-article-semaphore)
115 (setq do-fetch (not gnus-async-fetch-list))
116 ;; Nix out any outstanding requests.
117 (setq gnus-async-fetch-list nil)
118 ;; Fill in the new list.
119 (let ((n gnus-use-article-prefetch)
120 (data (gnus-data-find-list article))
122 (while (and (setq d (pop data))
126 (unless (or (gnus-async-prefetched-article-entry
127 group (setq article (gnus-data-number d)))
128 (not (natnump article)))
129 ;; Not already fetched -- so we add it to the list.
130 (push article gnus-async-fetch-list)))
131 (setq gnus-async-fetch-list
132 (nreverse gnus-async-fetch-list))))
135 (setq article (car gnus-async-fetch-list))))
137 (gnus-async-release-semaphore 'gnus-async-article-semaphore))
139 (when (and do-fetch article)
140 ;; We want to fetch some more articles.
144 (gnus-async-set-buffer)
145 (goto-char (point-max))
146 (setq mark (point-marker))
147 (let ((nnheader-callback-function
148 (gnus-make-async-article-function
149 group article mark summary next))
150 (nntp-server-buffer (get-buffer
151 gnus-async-prefetch-article-buffer)))
152 (gnus-message 7 "Prefetching article %d in group %s"
154 (gnus-request-article article group)))))))))
156 (defun gnus-make-async-article-function (group article mark summary next)
157 "Return a callback function."
160 (gnus-async-set-buffer)
161 (gnus-async-get-semaphore 'gnus-async-article-semaphore)
162 (push (list ',(intern (format "%s-%d" group article))
163 ,mark (set-marker (make-marker)
166 gnus-async-article-alist)
167 (gnus-async-release-semaphore
168 'gnus-async-article-semaphore)
169 (when (gnus-buffer-live-p ,summary)
170 (gnus-async-prefetch-article
171 ,group ,next ,summary t)))))
173 (defun gnus-async-request-fetched-article (group article buffer)
174 "See whether we have ARTICLE from GROUP and put it in BUFFER."
175 (when (numberp article)
176 (let ((entry (gnus-async-prefetched-article-entry group article)))
179 (gnus-async-set-buffer)
180 (copy-to-buffer buffer (cadr entry) (caddr entry))
181 ;; Remove the read article from the prefetch buffer.
182 (when (memq 'read gnus-prefetched-article-deletion-strategy)
183 (gnus-async-delete-prefected-entry entry))
186 (defun gnus-async-delete-prefected-entry (entry)
187 "Delete ENTRY from buffer and alist."
188 (delete-region (cadr entry) (caddr entry))
189 (set-marker (cadr entry) nil)
190 (set-marker (caddr entry) nil)
191 (gnus-async-get-semaphore 'gnus-async-article-semaphore)
192 (setq gnus-async-article-alist
193 (delq entry gnus-async-article-alist))
194 (gnus-async-release-semaphore 'gnus-async-article-semaphore))
196 (defun gnus-async-prefetch-remove-group (group)
197 "Remove all articles belonging to GROUP from the prefetch buffer."
198 (when (and (gnus-group-asynchronous-p group)
199 (memq 'exit gnus-prefetched-article-deletion-strategy))
200 (let ((alist gnus-async-article-alist))
202 (gnus-async-set-buffer)
204 (when (equal group (nth 3 (car alist)))
205 (gnus-async-delete-prefected-entry (car alist)))
208 (defun gnus-async-prefetched-article-entry (group article)
209 "Return the entry for ARTICLE in GROUP iff it has been prefetched."
210 (assq (intern (format "%s-%d" group article))
211 gnus-async-article-alist))
217 (defun gnus-async-prefetch-headers (group)
218 "Prefetch the headers for group GROUP."
221 (when (and gnus-use-header-prefetch
223 (gnus-group-asynchronous-p group)
224 (listp gnus-async-header-prefetched)
225 (setq unread (gnus-list-of-unread-articles group)))
226 ;; Mark that a fetch is in progress.
227 (setq gnus-async-header-prefetched t)
228 (nnheader-set-temp-buffer gnus-async-prefetch-headers-buffer t)
230 (let ((nntp-server-buffer (current-buffer))
231 (nnheader-callback-function
233 (setq gnus-async-header-prefetched
234 ,(cons group unread)))))
235 (gnus-retrieve-headers unread group gnus-fetch-old-headers))))))
237 (defun gnus-async-retrieve-fetched-headers (articles group)
238 "See whether we have prefetched headers."
239 (when (and gnus-use-header-prefetch
240 (gnus-group-asynchronous-p group)
241 (listp gnus-async-header-prefetched)
242 (equal group (car gnus-async-header-prefetched))
243 (equal articles (cdr gnus-async-header-prefetched)))
245 (nnheader-set-temp-buffer gnus-async-prefetch-headers-buffer t)
247 (copy-to-buffer nntp-server-buffer (point-min) (point-max))
249 (setq gnus-async-header-prefetched nil)
252 (provide 'gnus-async)
254 ;;; gnus-async.el ends here