1389ce7b75d74b226e7b8e01b3e1abc055becb87
[gnus] / lisp / gnus-async.el
1 ;;; gnus-async.el --- asynchronous support for Gnus
2 ;; Copyright (C) 1996 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 (require 'gnus)
29 (require 'gnus-sum)
30 (require 'nntp)
31
32 (defgroup gnus-asynchronous nil
33   "Support for asynchronous operations."
34   :group 'gnus)
35
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
40   :type 'boolean)
41
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)
48                  (const :tag "all" t)
49                  (integer :tag "some" 0)))
50
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
56 from that group."
57   :group 'gnus-asynchronous
58   :type '(set (const read) (const exit)))
59
60 (defcustom gnus-use-header-prefetch nil
61   "*If non-nil, prefetch the headers to the next group."
62   :group 'gnus-asynchronous
63   :type 'boolean)
64
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
70   :type 'function)
71
72 ;;; Internal variables.
73
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)
78
79 (defvar gnus-async-prefetch-headers-buffer " *Async Prefetch Headers*")
80 (defvar gnus-async-header-prefetched nil)
81
82 ;;; Utility functions.
83
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)))
87
88 ;;; Somewhat bogus semaphores.
89
90 (defun gnus-async-get-semaphore (semaphore)
91   "Wait until SEMAPHORE is released."
92   (while (/= (length (nconc (symbol-value semaphore) (list nil))) 2)
93     (sleep-for 1)))
94
95 (defun gnus-async-release-semaphore (semaphore)
96   "Release SEMAPHORE."
97   (setcdr (symbol-value semaphore) nil))
98
99 (defmacro gnus-async-with-semaphore (&rest forms)
100   `(unwind-protect
101        (progn
102          (gnus-async-get-semaphore 'gnus-async-article-semaphore)
103          ,@forms)
104      (gnus-async-release-semaphore 'gnus-async-article-semaphore)))
105
106 (put 'gnus-asynch-with-semaphore 'lisp-indent-function 0)
107 (put 'gnus-asynch-with-semaphore 'edebug-form-spec '(body))
108         
109 ;;;
110 ;;; Article prefetch
111 ;;;
112
113 (gnus-add-shutdown 'gnus-async-close 'gnus)
114 (defun gnus-async-close ()
115   (gnus-kill-buffer gnus-async-prefetch-article-buffer)
116   (gnus-kill-buffer gnus-async-prefetch-headers-buffer)
117   (setq gnus-async-article-alist nil
118         gnus-async-header-prefetched nil))
119
120 (defun gnus-async-set-buffer ()
121   (nnheader-set-temp-buffer gnus-async-prefetch-article-buffer t))
122
123 (defun gnus-async-prefetch-next (group article summary)
124   "Possibly prefetch several articles starting with the article after ARTICLE."
125   (when (and (gnus-buffer-live-p summary)
126              gnus-asynchronous
127              (gnus-group-asynchronous-p group))
128     (save-excursion
129       (set-buffer gnus-summary-buffer)
130       (let ((next (caadr (gnus-data-find-list article))))
131         (when next
132           (if (not (fboundp 'run-with-idle-timer))
133               ;; This is either an older Emacs or XEmacs, so we
134               ;; do this, which leads to slightly slower article
135               ;; buffer display.
136               (gnus-async-prefetch-article group next summary)
137             (run-with-idle-timer 
138              0.1 nil 'gnus-async-prefetch-article group next summary)))))))
139
140 (defun gnus-async-prefetch-article (group article summary &optional next)
141   "Possibly prefetch several articles starting with ARTICLE."
142   (if (not (gnus-buffer-live-p summary))
143       (gnus-async-with-semaphore
144        (setq gnus-async-fetch-list nil))
145     (when (and gnus-asynchronous
146                (gnus-alive-p))
147       (when next
148         (gnus-async-with-semaphore
149          (pop gnus-async-fetch-list)))
150       (let ((do-fetch next))
151         (when (and (gnus-group-asynchronous-p group)
152                    (gnus-buffer-live-p summary)
153                    (or (not next)
154                        gnus-async-fetch-list))
155           (gnus-async-with-semaphore
156            (unless next
157              (setq do-fetch (not gnus-async-fetch-list))
158              ;; Nix out any outstanding requests.
159              (setq gnus-async-fetch-list nil)
160              ;; Fill in the new list.
161              (let ((n gnus-use-article-prefetch)
162                    (data (gnus-data-find-list article))
163                    d)
164                (while (and (setq d (pop data))
165                            (if (numberp n)
166                                (natnump (decf n))
167                              n))
168                  (unless (or (gnus-async-prefetched-article-entry
169                               group (setq article (gnus-data-number d)))
170                              (not (natnump article))
171                              (not (funcall gnus-async-prefetch-article-p d)))
172                    ;; Not already fetched -- so we add it to the list.
173                    (push article gnus-async-fetch-list)))
174                (setq gnus-async-fetch-list
175                      (nreverse gnus-async-fetch-list))))
176
177            (when do-fetch
178              (setq article (car gnus-async-fetch-list))))
179     
180           (when (and do-fetch article)
181             ;; We want to fetch some more articles.
182             (save-excursion
183               (set-buffer summary)
184               (let (mark)
185                 (gnus-async-set-buffer)
186                 (goto-char (point-max))
187                 (setq mark (point-marker))
188                 (let ((nnheader-callback-function
189                        (gnus-make-async-article-function 
190                         group article mark summary next))
191                       (nntp-server-buffer 
192                        (get-buffer gnus-async-prefetch-article-buffer)))
193                   (gnus-message 7 "Prefetching article %d in group %s"
194                                 article group)
195                   (gnus-request-article article group))))))))))
196
197 (defun gnus-make-async-article-function (group article mark summary next)
198   "Return a callback function."
199   `(lambda (arg)
200      (save-excursion
201        (when arg
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) (point-max))
206                       ,group ,article)
207                 gnus-async-article-alist)))
208        (if (not (gnus-buffer-live-p ,summary))
209            (gnus-async-with-semaphore
210             (setq gnus-async-fetch-list nil))
211          (gnus-async-prefetch-article ,group ,next ,summary t)))))
212
213 (defun gnus-async-unread-p (data)
214   "Return non-nil if DATA represents an unread article."
215   (gnus-data-unread-p data))
216
217 (defun gnus-async-request-fetched-article (group article buffer)
218   "See whether we have ARTICLE from GROUP and put it in BUFFER."
219   (when (numberp article)
220     (let ((entry (gnus-async-prefetched-article-entry group article)))
221       (when entry
222         (save-excursion
223           (gnus-async-set-buffer)
224           (copy-to-buffer buffer (cadr entry) (caddr entry))
225           ;; Remove the read article from the prefetch buffer.
226           (when (memq 'read gnus-prefetched-article-deletion-strategy)
227             (gnus-async-delete-prefected-entry entry))
228           t)))))
229
230 (defun gnus-async-delete-prefected-entry (entry)
231   "Delete ENTRY from buffer and alist."
232   (delete-region (cadr entry) (caddr entry))
233   (set-marker (cadr entry) nil)
234   (set-marker (caddr entry) nil)
235   (gnus-async-with-semaphore
236    (setq gnus-async-article-alist 
237          (delq entry gnus-async-article-alist))))
238
239 (defun gnus-async-prefetch-remove-group (group)
240   "Remove all articles belonging to GROUP from the prefetch buffer."
241   (when (and (gnus-group-asynchronous-p group)
242              (memq 'exit gnus-prefetched-article-deletion-strategy))
243     (let ((alist gnus-async-article-alist))
244       (save-excursion
245         (gnus-async-set-buffer)
246         (while alist
247           (when (equal group (nth 3 (car alist)))
248             (gnus-async-delete-prefected-entry (car alist)))
249           (pop alist))))))
250           
251 (defun gnus-async-prefetched-article-entry (group article)
252   "Return the entry for ARTICLE in GROUP iff it has been prefetched."
253   (assq (intern (format "%s-%d" group article))
254         gnus-async-article-alist))
255
256 ;;;
257 ;;; Header prefetch
258 ;;;
259
260 (defun gnus-async-prefetch-headers (group)
261   "Prefetch the headers for group GROUP."
262   (save-excursion
263     (let (unread)
264       (when (and gnus-use-header-prefetch
265                  gnus-asynchronous
266                  (gnus-group-asynchronous-p group)
267                  (listp gnus-async-header-prefetched)
268                  (setq unread (gnus-list-of-unread-articles group)))
269         ;; Mark that a fetch is in progress.
270         (setq gnus-async-header-prefetched t)
271         (nnheader-set-temp-buffer gnus-async-prefetch-headers-buffer t)
272         (erase-buffer)
273         (let ((nntp-server-buffer (current-buffer))
274               (nnheader-callback-function
275                `(lambda (arg)
276                   (setq gnus-async-header-prefetched
277                         ,(cons group unread)))))
278           (gnus-retrieve-headers unread group gnus-fetch-old-headers))))))
279
280 (defun gnus-async-retrieve-fetched-headers (articles group)
281   "See whether we have prefetched headers."
282   (when (and gnus-use-header-prefetch
283              (gnus-group-asynchronous-p group)
284              (listp gnus-async-header-prefetched)
285              (equal group (car gnus-async-header-prefetched))
286              (equal articles (cdr gnus-async-header-prefetched)))
287     (save-excursion
288       (nnheader-set-temp-buffer gnus-async-prefetch-headers-buffer t)
289       (nntp-decode-text)
290       (copy-to-buffer nntp-server-buffer (point-min) (point-max))
291       (erase-buffer)
292       (setq gnus-async-header-prefetched nil)
293       t)))
294   
295 (provide 'gnus-async)
296
297 ;;; gnus-async.el ends here