1 ;;; gnus-async.el --- asynchronous support for Gnus
2 ;; Copyright (C) 1996,97,98 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.
28 (eval-when-compile (require 'cl))
34 (defgroup gnus-asynchronous nil
35 "Support for asynchronous operations."
38 (defcustom gnus-asynchronous t
39 "*If nil, inhibit all Gnus asynchronicity.
40 If non-nil, let the other asynch variables be heeded."
41 :group 'gnus-asynchronous
44 (defcustom gnus-use-article-prefetch 30
45 "*If non-nil, prefetch articles in groups that allow this.
46 If a number, prefetch only that many articles forward;
47 if t, prefetch as many articles as possible."
48 :group 'gnus-asynchronous
49 :type '(choice (const :tag "off" nil)
51 (integer :tag "some" 0)))
53 (defcustom gnus-prefetched-article-deletion-strategy '(read exit)
54 "*List of symbols that say when to remove articles from the prefetch buffer.
55 Possible values in this list are `read', which means that
56 articles are removed as they are read, and `exit', which means
57 that all articles belonging to a group are removed on exit
59 :group 'gnus-asynchronous
60 :type '(set (const read) (const exit)))
62 (defcustom gnus-use-header-prefetch nil
63 "*If non-nil, prefetch the headers to the next group."
64 :group 'gnus-asynchronous
67 (defcustom gnus-async-prefetch-article-p 'gnus-async-unread-p
68 "*Function called to say whether an article should be prefetched or not.
69 The function is called with one parameter -- the article data.
70 It should return non-nil if the article is to be prefetched."
71 :group 'gnus-asynchronous
74 ;;; Internal variables.
76 (defvar gnus-async-prefetch-article-buffer " *Async Prefetch Article*")
77 (defvar gnus-async-article-alist nil)
78 (defvar gnus-async-article-semaphore '(nil))
79 (defvar gnus-async-fetch-list nil)
80 (defvar gnus-asynch-obarray nil)
82 (defvar gnus-async-prefetch-headers-buffer " *Async Prefetch Headers*")
83 (defvar gnus-async-header-prefetched nil)
85 ;;; Utility functions.
87 (defun gnus-group-asynchronous-p (group)
88 "Say whether GROUP is fetched from a server that supports asynchronicity."
89 (gnus-asynchronous-p (gnus-find-method-for-group group)))
91 ;;; Somewhat bogus semaphores.
93 (defun gnus-async-get-semaphore (semaphore)
94 "Wait until SEMAPHORE is released."
95 (while (/= (length (nconc (symbol-value semaphore) (list nil))) 2)
98 (defun gnus-async-release-semaphore (semaphore)
100 (setcdr (symbol-value semaphore) nil))
102 (defmacro gnus-async-with-semaphore (&rest forms)
105 (gnus-async-get-semaphore 'gnus-async-article-semaphore)
107 (gnus-async-release-semaphore 'gnus-async-article-semaphore)))
109 (put 'gnus-asynch-with-semaphore 'lisp-indent-function 0)
110 (put 'gnus-asynch-with-semaphore 'edebug-form-spec '(body))
116 (gnus-add-shutdown 'gnus-async-close 'gnus)
117 (defun gnus-async-close ()
118 (gnus-kill-buffer gnus-async-prefetch-article-buffer)
119 (gnus-kill-buffer gnus-async-prefetch-headers-buffer)
120 (setq gnus-async-article-alist nil
121 gnus-async-header-prefetched nil))
123 (defun gnus-async-set-buffer ()
124 (nnheader-set-temp-buffer gnus-async-prefetch-article-buffer t)
125 (unless gnus-asynch-obarray
126 (set (make-local-variable 'gnus-asynch-obarray)
127 (gnus-make-hashtable 1023))))
129 (defun gnus-async-halt-prefetch ()
131 (setq gnus-async-fetch-list nil))
133 (defun gnus-async-prefetch-next (group article summary)
134 "Possibly prefetch several articles starting with the article after ARTICLE."
135 (when (and (gnus-buffer-live-p summary)
137 (gnus-group-asynchronous-p group))
139 (set-buffer gnus-summary-buffer)
140 (let ((next (caadr (gnus-data-find-list article))))
142 (if (not (fboundp 'run-with-idle-timer))
143 ;; This is either an older Emacs or XEmacs, so we
144 ;; do this, which leads to slightly slower article
146 (gnus-async-prefetch-article group next summary)
148 0.1 nil 'gnus-async-prefetch-article group next summary)))))))
150 (defun gnus-async-prefetch-article (group article summary &optional next)
151 "Possibly prefetch several articles starting with ARTICLE."
152 (if (not (gnus-buffer-live-p summary))
153 (gnus-async-with-semaphore
154 (setq gnus-async-fetch-list nil))
155 (when (and gnus-asynchronous
158 (gnus-async-with-semaphore
159 (pop gnus-async-fetch-list)))
160 (let ((do-fetch next)
161 (do-message t)) ;(eq major-mode 'gnus-summary-mode)))
162 (when (and (gnus-group-asynchronous-p group)
163 (gnus-buffer-live-p summary)
165 gnus-async-fetch-list))
166 (gnus-async-with-semaphore
168 (setq do-fetch (not gnus-async-fetch-list))
169 ;; Nix out any outstanding requests.
170 (setq gnus-async-fetch-list nil)
171 ;; Fill in the new list.
172 (let ((n gnus-use-article-prefetch)
173 (data (gnus-data-find-list article))
175 (while (and (setq d (pop data))
179 (unless (or (gnus-async-prefetched-article-entry
180 group (setq article (gnus-data-number d)))
181 (not (natnump article))
182 (not (funcall gnus-async-prefetch-article-p d)))
183 ;; Not already fetched -- so we add it to the list.
184 (push article gnus-async-fetch-list)))
185 (setq gnus-async-fetch-list
186 (nreverse gnus-async-fetch-list))))
189 (setq article (car gnus-async-fetch-list))))
191 (when (and do-fetch article)
192 ;; We want to fetch some more articles.
196 (gnus-async-set-buffer)
197 (goto-char (point-max))
198 (setq mark (point-marker))
199 (let ((nnheader-callback-function
200 (gnus-make-async-article-function
201 group article mark summary next))
203 (get-buffer gnus-async-prefetch-article-buffer)))
205 (gnus-message 9 "Prefetching article %d in group %s"
207 (gnus-request-article article group))))))))))
209 (defun gnus-make-async-article-function (group article mark summary next)
210 "Return a callback function."
214 (gnus-async-set-buffer)
215 (gnus-async-with-semaphore
216 (push (list ',(intern (format "%s-%d" group article)
218 ,mark (set-marker (make-marker) (point-max))
220 gnus-async-article-alist)))
221 (if (not (gnus-buffer-live-p ,summary))
222 (gnus-async-with-semaphore
223 (setq gnus-async-fetch-list nil))
224 (gnus-async-prefetch-article ,group ,next ,summary t)))))
226 (defun gnus-async-unread-p (data)
227 "Return non-nil if DATA represents an unread article."
228 (gnus-data-unread-p data))
230 (defun gnus-async-request-fetched-article (group article buffer)
231 "See whether we have ARTICLE from GROUP and put it in BUFFER."
232 (when (numberp article)
233 (let ((entry (gnus-async-prefetched-article-entry group article)))
236 (gnus-async-set-buffer)
237 (copy-to-buffer buffer (cadr entry) (caddr entry))
238 ;; Remove the read article from the prefetch buffer.
239 (when (memq 'read gnus-prefetched-article-deletion-strategy)
240 (gnus-async-delete-prefected-entry entry))
243 (defun gnus-async-delete-prefected-entry (entry)
244 "Delete ENTRY from buffer and alist."
246 (delete-region (cadr entry) (caddr entry))
247 (set-marker (cadr entry) nil)
248 (set-marker (caddr entry) nil))
249 (gnus-async-with-semaphore
250 (setq gnus-async-article-alist
251 (delq entry gnus-async-article-alist))))
253 (defun gnus-async-prefetch-remove-group (group)
254 "Remove all articles belonging to GROUP from the prefetch buffer."
255 (when (and (gnus-group-asynchronous-p group)
256 (memq 'exit gnus-prefetched-article-deletion-strategy))
257 (let ((alist gnus-async-article-alist))
259 (gnus-async-set-buffer)
261 (when (equal group (nth 3 (car alist)))
262 (gnus-async-delete-prefected-entry (car alist)))
265 (defun gnus-async-prefetched-article-entry (group article)
266 "Return the entry for ARTICLE in GROUP iff it has been prefetched."
267 (let ((entry (save-excursion
268 (gnus-async-set-buffer)
269 (assq (intern (format "%s-%d" group article)
271 gnus-async-article-alist))))
272 ;; Perhaps something has emptied the buffer?
274 (= (cadr entry) (caddr entry)))
277 (set-marker (cadr entry) nil)
278 (set-marker (caddr entry) nil))
279 (setq gnus-async-article-alist
280 (delq entry gnus-async-article-alist))
288 (defun gnus-async-prefetch-headers (group)
289 "Prefetch the headers for group GROUP."
292 (when (and gnus-use-header-prefetch
294 (gnus-group-asynchronous-p group)
295 (listp gnus-async-header-prefetched)
296 (setq unread (gnus-list-of-unread-articles group)))
297 ;; Mark that a fetch is in progress.
298 (setq gnus-async-header-prefetched t)
299 (nnheader-set-temp-buffer gnus-async-prefetch-headers-buffer t)
301 (let ((nntp-server-buffer (current-buffer))
302 (nnheader-callback-function
304 (setq gnus-async-header-prefetched
305 ,(cons group unread)))))
306 (gnus-retrieve-headers unread group gnus-fetch-old-headers))))))
308 (defun gnus-async-retrieve-fetched-headers (articles group)
309 "See whether we have prefetched headers."
310 (when (and gnus-use-header-prefetch
311 (gnus-group-asynchronous-p group)
312 (listp gnus-async-header-prefetched)
313 (equal group (car gnus-async-header-prefetched))
314 (equal articles (cdr gnus-async-header-prefetched)))
316 (nnheader-set-temp-buffer gnus-async-prefetch-headers-buffer t)
318 (copy-to-buffer nntp-server-buffer (point-min) (point-max))
320 (setq gnus-async-header-prefetched nil)
323 (provide 'gnus-async)
325 ;;; gnus-async.el ends here