*** 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@gnus.org>
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-async-hashtb nil)
81 (defvar gnus-async-current-prefetch-group nil)
82 (defvar gnus-async-current-prefetch-article nil)
83
84 (defvar gnus-async-prefetch-headers-buffer " *Async Prefetch Headers*")
85 (defvar gnus-async-header-prefetched nil)
86
87 ;;; Utility functions.
88
89 (defun gnus-group-asynchronous-p (group)
90   "Say whether GROUP is fetched from a server that supports asynchronicity."
91   (gnus-asynchronous-p (gnus-find-method-for-group group)))
92
93 ;;; Somewhat bogus semaphores.
94
95 (defun gnus-async-get-semaphore (semaphore)
96   "Wait until SEMAPHORE is released."
97   (while (/= (length (nconc (symbol-value semaphore) (list nil))) 2)
98     (sleep-for 1)))
99
100 (defun gnus-async-release-semaphore (semaphore)
101   "Release SEMAPHORE."
102   (setcdr (symbol-value semaphore) nil))
103
104 (defmacro gnus-async-with-semaphore (&rest forms)
105   `(unwind-protect
106        (progn
107          (gnus-async-get-semaphore 'gnus-async-article-semaphore)
108          ,@forms)
109      (gnus-async-release-semaphore 'gnus-async-article-semaphore)))
110
111 (put 'gnus-asynch-with-semaphore 'lisp-indent-function 0)
112 (put 'gnus-asynch-with-semaphore 'edebug-form-spec '(body))
113
114 ;;;
115 ;;; Article prefetch
116 ;;;
117
118 (gnus-add-shutdown 'gnus-async-close 'gnus)
119 (defun gnus-async-close ()
120   (gnus-kill-buffer gnus-async-prefetch-article-buffer)
121   (gnus-kill-buffer gnus-async-prefetch-headers-buffer)
122   (setq gnus-async-hashtb nil
123         gnus-async-article-alist nil
124         gnus-async-header-prefetched nil))
125
126 (defun gnus-async-set-buffer ()
127   (nnheader-set-temp-buffer gnus-async-prefetch-article-buffer t)
128   (unless gnus-async-hashtb
129     (setq gnus-async-hashtb (gnus-make-hashtable 1023))))
130
131 (defun gnus-async-halt-prefetch ()
132   "Stop prefetching."
133   (setq gnus-async-fetch-list nil))
134
135 (defun gnus-async-prefetch-next (group article summary)
136   "Possibly prefetch several articles starting with the article after ARTICLE."
137   (when (and (gnus-buffer-live-p summary)
138              gnus-asynchronous
139              (gnus-group-asynchronous-p group))
140     (save-excursion
141       (set-buffer gnus-summary-buffer)
142       (let ((next (caadr (gnus-data-find-list article))))
143         (when next
144           (if (not (fboundp 'run-with-idle-timer))
145               ;; This is either an older Emacs or XEmacs, so we
146               ;; do this, which leads to slightly slower article
147               ;; buffer display.
148               (gnus-async-prefetch-article group next summary)
149             (run-with-idle-timer
150              0.1 nil 'gnus-async-prefetch-article group next summary)))))))
151
152 (defun gnus-async-prefetch-article (group article summary &optional next)
153   "Possibly prefetch several articles starting with ARTICLE."
154   (if (not (gnus-buffer-live-p summary))
155       (gnus-async-with-semaphore
156        (setq gnus-async-fetch-list nil))
157     (when (and gnus-asynchronous
158                (gnus-alive-p))
159       (when next
160         (gnus-async-with-semaphore
161          (pop gnus-async-fetch-list)))
162       (let ((do-fetch next)
163             (do-message t)) ;(eq major-mode 'gnus-summary-mode)))
164         (when (and (gnus-group-asynchronous-p group)
165                    (gnus-buffer-live-p summary)
166                    (or (not next)
167                        gnus-async-fetch-list))
168           (gnus-async-with-semaphore
169            (unless next
170              (setq do-fetch (not gnus-async-fetch-list))
171              ;; Nix out any outstanding requests.
172              (setq gnus-async-fetch-list nil)
173              ;; Fill in the new list.
174              (let ((n gnus-use-article-prefetch)
175                    (data (gnus-data-find-list article))
176                    d)
177                (while (and (setq d (pop data))
178                            (if (numberp n)
179                                (natnump (decf n))
180                              n))
181                  (unless (or (gnus-async-prefetched-article-entry
182                               group (setq article (gnus-data-number d)))
183                              (not (natnump article))
184                              (not (funcall gnus-async-prefetch-article-p d)))
185                    ;; Not already fetched -- so we add it to the list.
186                    (push article gnus-async-fetch-list)))
187                (setq gnus-async-fetch-list
188                      (nreverse gnus-async-fetch-list))))
189
190            (when do-fetch
191              (setq article (car gnus-async-fetch-list))))
192
193           (when (and do-fetch article)
194             ;; We want to fetch some more articles.
195             (save-excursion
196               (set-buffer summary)
197               (let (mark)
198                 (gnus-async-set-buffer)
199                 (goto-char (point-max))
200                 (setq mark (point-marker))
201                 (let ((nnheader-callback-function
202                        (gnus-make-async-article-function
203                         group article mark summary next))
204                       (nntp-server-buffer
205                        (get-buffer gnus-async-prefetch-article-buffer)))
206                   (when do-message
207                     (gnus-message 9 "Prefetching article %d in group %s"
208                                   article group))
209                   (setq gnus-async-current-prefetch-group group)
210                   (setq gnus-async-current-prefetch-article article)
211                   (gnus-request-article article group))))))))))
212
213 (defun gnus-make-async-article-function (group article mark summary next)
214   "Return a callback function."
215   `(lambda (arg)
216      (gnus-async-article-callback arg ,group ,article ,mark ,summary ,next)))
217
218 (defun gnus-async-article-callback (arg group article mark summary next)
219   "Function called when an async article is done being fetched."
220   (save-excursion
221     (setq gnus-async-current-prefetch-article nil)
222     (when arg
223       (gnus-async-set-buffer)
224       (gnus-async-with-semaphore
225        (setq
226         gnus-async-article-alist
227         (cons (list (intern (format "%s-%d" group article)
228                             gnus-async-hashtb)
229                     mark (set-marker (make-marker) (point-max))
230                     group article)
231               gnus-async-article-alist))))
232     (if (not (gnus-buffer-live-p summary))
233         (gnus-async-with-semaphore
234          (setq gnus-async-fetch-list nil))
235       (gnus-async-prefetch-article group next summary t))))
236
237 (defun gnus-async-unread-p (data)
238   "Return non-nil if DATA represents an unread article."
239   (gnus-data-unread-p data))
240
241 (defun gnus-async-request-fetched-article (group article buffer)
242   "See whether we have ARTICLE from GROUP and put it in BUFFER."
243   (when (numberp article)
244     (when (and gnus-async-current-prefetch-group
245                (string= group gnus-async-current-prefetch-group)
246                (eq article gnus-async-current-prefetch-article))
247       (save-excursion
248         (gnus-async-set-buffer)
249         (gnus-message 5 "Waiting for async article...")
250         (let ((proc (nntp-find-connection (current-buffer)))
251               (nntp-server-buffer (current-buffer))
252               (nntp-have-messaged nil))
253           (while (eq article (car gnus-async-fetch-list))
254             (nntp-accept-process-output proc)))
255         (gnus-message 5 "Waiting for async article...done")))
256     (let ((entry (gnus-async-prefetched-article-entry group article)))
257       (when entry
258         (save-excursion
259           (gnus-async-set-buffer)
260           (copy-to-buffer buffer (cadr entry) (caddr entry))
261           ;; Remove the read article from the prefetch buffer.
262           (when (memq 'read gnus-prefetched-article-deletion-strategy)
263             (gnus-async-delete-prefetched-entry entry))
264           t)))))
265
266 (defun gnus-async-delete-prefetched-entry (entry)
267   "Delete ENTRY from buffer and alist."
268   (ignore-errors
269     (delete-region (cadr entry) (caddr entry))
270     (set-marker (cadr entry) nil)
271     (set-marker (caddr entry) nil))
272   (gnus-async-with-semaphore
273    (setq gnus-async-article-alist
274          (delq entry gnus-async-article-alist))))
275
276 (defun gnus-async-prefetch-remove-group (group)
277   "Remove all articles belonging to GROUP from the prefetch buffer."
278   (when (and (gnus-group-asynchronous-p group)
279              (memq 'exit gnus-prefetched-article-deletion-strategy))
280     (let ((alist gnus-async-article-alist))
281       (save-excursion
282         (gnus-async-set-buffer)
283         (while alist
284           (when (equal group (nth 3 (car alist)))
285             (gnus-async-delete-prefetched-entry (car alist)))
286           (pop alist))))))
287
288 (defun gnus-async-prefetched-article-entry (group article)
289   "Return the entry for ARTICLE in GROUP iff it has been prefetched."
290   (let ((entry (save-excursion
291                  (gnus-async-set-buffer)
292                  (assq (intern (format "%s-%d" group article)
293                                gnus-async-hashtb)
294                        gnus-async-article-alist))))
295     ;; Perhaps something has emptied the buffer?
296     (if (and entry
297              (= (cadr entry) (caddr entry)))
298         (progn
299           (ignore-errors
300             (set-marker (cadr entry) nil)
301             (set-marker (caddr entry) nil))
302           (setq gnus-async-article-alist
303                 (delq entry gnus-async-article-alist))
304           nil)
305       entry)))
306
307 ;;;
308 ;;; Header prefetch
309 ;;;
310
311 (defun gnus-async-prefetch-headers (group)
312   "Prefetch the headers for group GROUP."
313   (save-excursion
314     (let (unread)
315       (when (and gnus-use-header-prefetch
316                  gnus-asynchronous
317                  (gnus-group-asynchronous-p group)
318                  (listp gnus-async-header-prefetched)
319                  (setq unread (gnus-list-of-unread-articles group)))
320         ;; Mark that a fetch is in progress.
321         (setq gnus-async-header-prefetched t)
322         (nnheader-set-temp-buffer gnus-async-prefetch-headers-buffer t)
323         (erase-buffer)
324         (let ((nntp-server-buffer (current-buffer))
325               (nnheader-callback-function
326                `(lambda (arg)
327                   (setq gnus-async-header-prefetched
328                         ,(cons group unread)))))
329           (gnus-retrieve-headers unread group gnus-fetch-old-headers))))))
330
331 (defun gnus-async-retrieve-fetched-headers (articles group)
332   "See whether we have prefetched headers."
333   (when (and gnus-use-header-prefetch
334              (gnus-group-asynchronous-p group)
335              (listp gnus-async-header-prefetched)
336              (equal group (car gnus-async-header-prefetched))
337              (equal articles (cdr gnus-async-header-prefetched)))
338     (save-excursion
339       (nnheader-set-temp-buffer gnus-async-prefetch-headers-buffer t)
340       (nntp-decode-text)
341       (copy-to-buffer nntp-server-buffer (point-min) (point-max))
342       (erase-buffer)
343       (setq gnus-async-header-prefetched nil)
344       t)))
345
346 (provide 'gnus-async)
347
348 ;;; gnus-async.el ends here