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 (defgroup gnus-asynchronous nil
33 "Support for asynchronous operations."
36 (defcustom gnus-asynchronous t
37 "*If nil, inhibit all Gnus asynchronicity.
38 If non-nil, let the other asynch variables be heeded."
39 :group 'gnus-asynchronous
42 (defcustom gnus-use-article-prefetch 30
43 "*If non-nil, prefetch articles in groups that allow this.
44 If a number, prefetch only that many articles forward;
45 if t, prefetch as many articles as possible."
46 :group 'gnus-asynchronous
47 :type '(choice (const :tag "off" nil)
49 (integer :tag "some" 0)))
51 (defcustom gnus-prefetched-article-deletion-strategy '(read exit)
52 "List of symbols that say when to remove articles from the prefetch buffer.
53 Possible values in this list are `read', which means that
54 articles are removed as they are read, and `exit', which means
55 that all articles belonging to a group are removed on exit
57 :group 'gnus-asynchronous
58 :type '(set (const read) (const exit)))
60 (defcustom gnus-use-header-prefetch nil
61 "*If non-nil, prefetch the headers to the next group."
62 :group 'gnus-asynchronous
65 (defcustom gnus-async-prefetch-article-p 'gnus-async-unread-p
66 "Function called to say whether an article should be prefetched or not.
67 The function is called with one parameter -- the article data.
68 It should return non-nil if the article is to be prefetched."
69 :group 'gnus-asynchronous
72 ;;; Internal variables.
74 (defvar gnus-async-prefetch-article-buffer " *Async Prefetch Article*")
75 (defvar gnus-async-article-alist nil)
76 (defvar gnus-async-article-semaphore '(nil))
77 (defvar gnus-async-fetch-list nil)
79 (defvar gnus-async-prefetch-headers-buffer " *Async Prefetch Headers*")
80 (defvar gnus-async-header-prefetched nil)
82 ;;; Utility functions.
84 (defun gnus-group-asynchronous-p (group)
85 "Say whether GROUP is fetched from a server that supports asynchronicity."
86 (gnus-asynchronous-p (gnus-find-method-for-group group)))
88 ;;; Somewhat bogus semaphores.
90 (defun gnus-async-get-semaphore (semaphore)
91 "Wait until SEMAPHORE is released."
92 (while (/= (length (nconc (symbol-value semaphore) (list nil))) 2)
95 (defun gnus-async-release-semaphore (semaphore)
97 (setcdr (symbol-value semaphore) nil))
99 (defmacro gnus-async-with-semaphore (&rest forms)
102 (gnus-async-get-semaphore 'gnus-async-article-semaphore)
104 (gnus-async-release-semaphore 'gnus-async-article-semaphore)))
106 (put 'gnus-asynch-with-semaphore 'lisp-indent-function 0)
107 (put 'gnus-asynch-with-semaphore 'lisp-indent-hook 0)
108 (put 'gnus-asynch-with-semaphore 'edebug-form-spec '(body))
114 (gnus-add-shutdown 'gnus-async-close 'gnus)
115 (defun gnus-async-close ()
116 (gnus-kill-buffer gnus-async-prefetch-article-buffer)
117 (gnus-kill-buffer gnus-async-prefetch-headers-buffer)
118 (setq gnus-async-article-alist nil
119 gnus-async-header-prefetched nil))
121 (defun gnus-async-set-buffer ()
122 (nnheader-set-temp-buffer gnus-async-prefetch-article-buffer t))
124 (defun gnus-async-prefetch-next (group article summary)
125 "Possibly prefetch several articles starting with the article after ARTICLE."
126 (when (and (gnus-buffer-live-p summary)
128 (gnus-group-asynchronous-p group))
130 (set-buffer gnus-summary-buffer)
131 (let ((next (caadr (gnus-data-find-list article))))
133 (if (not (fboundp 'run-with-idle-timer))
134 ;; This is either an older Emacs or XEmacs, so we
135 ;; do this, which leads to slightly slower article
137 (gnus-async-prefetch-article group next summary)
139 0.1 nil 'gnus-async-prefetch-article group next summary)))))))
141 (defun gnus-async-prefetch-article (group article summary &optional next)
142 "Possibly prefetch several articles starting with ARTICLE."
143 (if (not (gnus-buffer-live-p summary))
144 (gnus-async-with-semaphore
145 (setq gnus-async-fetch-list nil))
146 (when (and gnus-asynchronous
149 (gnus-async-with-semaphore
150 (pop gnus-async-fetch-list)))
151 (let ((do-fetch next))
152 (when (and (gnus-group-asynchronous-p group)
153 (gnus-buffer-live-p summary)
155 gnus-async-fetch-list))
156 (gnus-async-with-semaphore
158 (setq do-fetch (not gnus-async-fetch-list))
159 ;; Nix out any outstanding requests.
160 (setq gnus-async-fetch-list nil)
161 ;; Fill in the new list.
162 (let ((n gnus-use-article-prefetch)
163 (data (gnus-data-find-list article))
165 (while (and (setq d (pop data))
169 (unless (or (gnus-async-prefetched-article-entry
170 group (setq article (gnus-data-number d)))
171 (not (natnump article))
172 (not (funcall gnus-async-prefetch-article-p d)))
173 ;; Not already fetched -- so we add it to the list.
174 (push article gnus-async-fetch-list)))
175 (setq gnus-async-fetch-list
176 (nreverse gnus-async-fetch-list))))
179 (setq article (car gnus-async-fetch-list))))
181 (when (and do-fetch article)
182 ;; We want to fetch some more articles.
186 (gnus-async-set-buffer)
187 (goto-char (point-max))
188 (setq mark (point-marker))
189 (let ((nnheader-callback-function
190 (gnus-make-async-article-function
191 group article mark summary next))
193 (get-buffer gnus-async-prefetch-article-buffer)))
194 (gnus-message 7 "Prefetching article %d in group %s"
196 (gnus-request-article article group))))))))))
198 (defun gnus-make-async-article-function (group article mark summary next)
199 "Return a callback function."
202 (gnus-async-set-buffer)
203 (gnus-async-with-semaphore
204 (push (list ',(intern (format "%s-%d" group article))
205 ,mark (set-marker (make-marker)
208 gnus-async-article-alist))
209 (if (not (gnus-buffer-live-p ,summary))
210 (gnus-async-with-semaphore
211 (setq gnus-async-fetch-list nil))
212 (gnus-async-prefetch-article ,group ,next ,summary t)))))
214 (defun gnus-async-unread-p (data)
215 "Return non-nil if DATA represents an unread article."
216 (gnus-data-unread-p data))
218 (defun gnus-async-request-fetched-article (group article buffer)
219 "See whether we have ARTICLE from GROUP and put it in BUFFER."
220 (when (numberp article)
221 (let ((entry (gnus-async-prefetched-article-entry group article)))
224 (gnus-async-set-buffer)
225 (copy-to-buffer buffer (cadr entry) (caddr entry))
226 ;; Remove the read article from the prefetch buffer.
227 (when (memq 'read gnus-prefetched-article-deletion-strategy)
228 (gnus-async-delete-prefected-entry entry))
231 (defun gnus-async-delete-prefected-entry (entry)
232 "Delete ENTRY from buffer and alist."
233 (delete-region (cadr entry) (caddr entry))
234 (set-marker (cadr entry) nil)
235 (set-marker (caddr entry) nil)
236 (gnus-async-with-semaphore
237 (setq gnus-async-article-alist
238 (delq entry gnus-async-article-alist))))
240 (defun gnus-async-prefetch-remove-group (group)
241 "Remove all articles belonging to GROUP from the prefetch buffer."
242 (when (and (gnus-group-asynchronous-p group)
243 (memq 'exit gnus-prefetched-article-deletion-strategy))
244 (let ((alist gnus-async-article-alist))
246 (gnus-async-set-buffer)
248 (when (equal group (nth 3 (car alist)))
249 (gnus-async-delete-prefected-entry (car alist)))
252 (defun gnus-async-prefetched-article-entry (group article)
253 "Return the entry for ARTICLE in GROUP iff it has been prefetched."
254 (assq (intern (format "%s-%d" group article))
255 gnus-async-article-alist))
261 (defun gnus-async-prefetch-headers (group)
262 "Prefetch the headers for group GROUP."
265 (when (and gnus-use-header-prefetch
267 (gnus-group-asynchronous-p group)
268 (listp gnus-async-header-prefetched)
269 (setq unread (gnus-list-of-unread-articles group)))
270 ;; Mark that a fetch is in progress.
271 (setq gnus-async-header-prefetched t)
272 (nnheader-set-temp-buffer gnus-async-prefetch-headers-buffer t)
274 (let ((nntp-server-buffer (current-buffer))
275 (nnheader-callback-function
277 (setq gnus-async-header-prefetched
278 ,(cons group unread)))))
279 (gnus-retrieve-headers unread group gnus-fetch-old-headers))))))
281 (defun gnus-async-retrieve-fetched-headers (articles group)
282 "See whether we have prefetched headers."
283 (when (and gnus-use-header-prefetch
284 (gnus-group-asynchronous-p group)
285 (listp gnus-async-header-prefetched)
286 (equal group (car gnus-async-header-prefetched))
287 (equal articles (cdr gnus-async-header-prefetched)))
289 (nnheader-set-temp-buffer gnus-async-prefetch-headers-buffer t)
291 (copy-to-buffer nntp-server-buffer (point-min) (point-max))
293 (setq gnus-async-header-prefetched nil)
296 (provide 'gnus-async)
298 ;;; gnus-async.el ends here