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 (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.")
56 ;;; Internal variables.
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)
63 (defvar gnus-async-prefetch-headers-buffer " *Async Prefetch Headers*")
64 (defvar gnus-async-header-prefetched nil)
66 ;;; Utility functions.
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)))
72 ;;; Somewhat bogus semaphores.
74 (defun gnus-async-get-semaphore (semaphore)
75 "Wait until SEMAPHORE is released."
76 (while (/= (length (nconc (symbol-value semaphore) (list nil))) 2)
79 (defun gnus-async-release-semaphore (semaphore)
81 (setcdr (symbol-value semaphore) nil))
83 (defmacro gnus-async-with-semaphore (&rest forms)
86 (gnus-async-get-semaphore 'gnus-async-article-semaphore)
88 (gnus-async-release-semaphore 'gnus-async-article-semaphore)))
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))
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))
105 (defun gnus-async-set-buffer ()
106 (nnheader-set-temp-buffer gnus-async-prefetch-article-buffer t))
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)
112 (gnus-group-asynchronous-p group))
114 (set-buffer gnus-summary-buffer)
115 (let ((next (caadr (gnus-data-find-list article))))
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
121 (gnus-async-prefetch-article group next summary)
123 0.1 nil 'gnus-async-prefetch-article group next summary)))))))
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
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)
139 gnus-async-fetch-list))
140 (gnus-async-with-semaphore
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))
149 (while (and (setq d (pop data))
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))))
163 (setq article (car gnus-async-fetch-list))))
165 (when (and do-fetch article)
166 ;; We want to fetch some more articles.
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))
177 (get-buffer gnus-async-prefetch-article-buffer)))
178 (gnus-message 7 "Prefetching article %d in group %s"
180 (gnus-request-article article group))))))))))
182 (defun gnus-make-async-article-function (group article mark summary next)
183 "Return a callback function."
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)
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)))))
198 (defun gnus-async-unread-p (data)
199 "Return non-nil if DATA represents an unread article."
200 (gnus-data-unread-p data))
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)))
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))
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))))
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))
230 (gnus-async-set-buffer)
232 (when (equal group (nth 3 (car alist)))
233 (gnus-async-delete-prefected-entry (car alist)))
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))
245 (defun gnus-async-prefetch-headers (group)
246 "Prefetch the headers for group GROUP."
249 (when (and gnus-use-header-prefetch
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)
258 (let ((nntp-server-buffer (current-buffer))
259 (nnheader-callback-function
261 (setq gnus-async-header-prefetched
262 ,(cons group unread)))))
263 (gnus-retrieve-headers unread group gnus-fetch-old-headers))))))
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)))
273 (nnheader-set-temp-buffer gnus-async-prefetch-headers-buffer t)
275 (copy-to-buffer nntp-server-buffer (point-min) (point-max))
277 (setq gnus-async-header-prefetched nil)
280 (provide 'gnus-async)
282 ;;; gnus-async.el ends here