*** empty log message ***
[gnus] / lisp / gnus-async.el
1 ;;; gnus-async.el --- asynchronous support for Gnus
2 ;; Copyright (C) 1996,97,98 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
5 ;; Keywords: news
6
7 ;; This file is part of GNU Emacs.
8
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)
12 ;; any later version.
13
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.
18
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.
23
24 ;;; Commentary:
25
26 ;;; Code:
27
28 (eval-when-compile (require 'cl))
29
30 (require 'gnus)
31 (require 'gnus-sum)
32 (require 'nntp)
33
34 (defgroup gnus-asynchronous nil
35   "Support for asynchronous operations."
36   :group 'gnus)
37
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
42   :type 'boolean)
43
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)
50                  (const :tag "all" t)
51                  (integer :tag "some" 0)))
52
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
58 from that group."
59   :group 'gnus-asynchronous
60   :type '(set (const read) (const exit)))
61
62 (defcustom gnus-use-header-prefetch nil
63   "*If non-nil, prefetch the headers to the next group."
64   :group 'gnus-asynchronous
65   :type 'boolean)
66
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
72   :type 'function)
73
74 ;;; Internal variables.
75
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)
81
82 (defvar gnus-async-prefetch-headers-buffer " *Async Prefetch Headers*")
83 (defvar gnus-async-header-prefetched nil)
84
85 ;;; Utility functions.
86
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)))
90
91 ;;; Somewhat bogus semaphores.
92
93 (defun gnus-async-get-semaphore (semaphore)
94   "Wait until SEMAPHORE is released."
95   (while (/= (length (nconc (symbol-value semaphore) (list nil))) 2)
96     (sleep-for 1)))
97
98 (defun gnus-async-release-semaphore (semaphore)
99   "Release SEMAPHORE."
100   (setcdr (symbol-value semaphore) nil))
101
102 (defmacro gnus-async-with-semaphore (&rest forms)
103   `(unwind-protect
104        (progn
105          (gnus-async-get-semaphore 'gnus-async-article-semaphore)
106          ,@forms)
107      (gnus-async-release-semaphore 'gnus-async-article-semaphore)))
108
109 (put 'gnus-asynch-with-semaphore 'lisp-indent-function 0)
110 (put 'gnus-asynch-with-semaphore 'edebug-form-spec '(body))
111
112 ;;;
113 ;;; Article prefetch
114 ;;;
115
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))
122
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))))
128
129 (defun gnus-async-halt-prefetch ()
130   "Stop prefetching."
131   (setq gnus-async-fetch-list nil))
132
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)
136              gnus-asynchronous
137              (gnus-group-asynchronous-p group))
138     (save-excursion
139       (set-buffer gnus-summary-buffer)
140       (let ((next (caadr (gnus-data-find-list article))))
141         (when next
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
145               ;; buffer display.
146               (gnus-async-prefetch-article group next summary)
147             (run-with-idle-timer
148              0.1 nil 'gnus-async-prefetch-article group next summary)))))))
149
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
156                (gnus-alive-p))
157       (when next
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)
164                    (or (not next)
165                        gnus-async-fetch-list))
166           (gnus-async-with-semaphore
167            (unless next
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))
174                    d)
175                (while (and (setq d (pop data))
176                            (if (numberp n)
177                                (natnump (decf n))
178                              n))
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))))
187
188            (when do-fetch
189              (setq article (car gnus-async-fetch-list))))
190
191           (when (and do-fetch article)
192             ;; We want to fetch some more articles.
193             (save-excursion
194               (set-buffer summary)
195               (let (mark)
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))
202                       (nntp-server-buffer
203                        (get-buffer gnus-async-prefetch-article-buffer)))
204                   (when do-message
205                     (gnus-message 9 "Prefetching article %d in group %s"
206                                   article group))
207                   (gnus-request-article article group))))))))))
208
209 (defun gnus-make-async-article-function (group article mark summary next)
210   "Return a callback function."
211   `(lambda (arg)
212      (save-excursion
213        (when arg
214          (gnus-async-set-buffer)
215          (gnus-async-with-semaphore
216           (push (list ',(intern (format "%s-%d" group article)
217                                 gnus-asynch-obarray)
218                       ,mark (set-marker (make-marker) (point-max))
219                       ,group ,article)
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)))))
225
226 (defun gnus-async-unread-p (data)
227   "Return non-nil if DATA represents an unread article."
228   (gnus-data-unread-p data))
229
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)))
234       (when entry
235         (save-excursion
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))
241           t)))))
242
243 (defun gnus-async-delete-prefected-entry (entry)
244   "Delete ENTRY from buffer and alist."
245   (ignore-errors
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))))
252
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))
258       (save-excursion
259         (gnus-async-set-buffer)
260         (while alist
261           (when (equal group (nth 3 (car alist)))
262             (gnus-async-delete-prefected-entry (car alist)))
263           (pop alist))))))
264
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)
270                                gnus-asynch-obarray)
271                        gnus-async-article-alist))))
272     ;; Perhaps something has emptied the buffer?
273     (if (and entry
274              (= (cadr entry) (caddr entry)))
275         (progn
276           (ignore-errors
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))
281           nil)
282       entry)))
283
284 ;;;
285 ;;; Header prefetch
286 ;;;
287
288 (defun gnus-async-prefetch-headers (group)
289   "Prefetch the headers for group GROUP."
290   (save-excursion
291     (let (unread)
292       (when (and gnus-use-header-prefetch
293                  gnus-asynchronous
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)
300         (erase-buffer)
301         (let ((nntp-server-buffer (current-buffer))
302               (nnheader-callback-function
303                `(lambda (arg)
304                   (setq gnus-async-header-prefetched
305                         ,(cons group unread)))))
306           (gnus-retrieve-headers unread group gnus-fetch-old-headers))))))
307
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)))
315     (save-excursion
316       (nnheader-set-temp-buffer gnus-async-prefetch-headers-buffer t)
317       (nntp-decode-text)
318       (copy-to-buffer nntp-server-buffer (point-min) (point-max))
319       (erase-buffer)
320       (setq gnus-async-header-prefetched nil)
321       t)))
322
323 (provide 'gnus-async)
324
325 ;;; gnus-async.el ends here