Importing Pterodactyl Gnus v0.93.
[elisp/gnus.git-] / lisp / gnus-async.el
1 ;;; gnus-async.el --- asynchronous support for Gnus
2 ;; Copyright (C) 1996,97,98,99 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-async-with-semaphore 'lisp-indent-function 0)
112 (put 'gnus-async-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 (equal group gnus-async-current-prefetch-group)
245                (eq article gnus-async-current-prefetch-article))
246       (gnus-async-wait-for-article article))
247     (let ((entry (gnus-async-prefetched-article-entry group article)))
248       (when entry
249         (save-excursion
250           (gnus-async-set-buffer)
251           (copy-to-buffer buffer (cadr entry) (caddr entry))
252           ;; Remove the read article from the prefetch buffer.
253           (when (memq 'read gnus-prefetched-article-deletion-strategy)
254             (gnus-async-delete-prefetched-entry entry))
255           t)))))
256
257 (defun gnus-async-wait-for-article (article)
258   "Wait until ARTICLE is no longer the currently-being-fetched article."
259   (save-excursion
260     (gnus-async-set-buffer)
261     (let ((proc (nntp-find-connection (current-buffer)))
262           (nntp-server-buffer (current-buffer))
263           (nntp-have-messaged nil)
264           (tries 0))
265       (condition-case nil
266           ;; FIXME: we could stop waiting after some
267           ;; timeout, but this is the wrong place to do it.
268           ;; rather than checking time-spent-waiting, we
269           ;; should check time-since-last-output, which
270           ;; needs to be done in nntp.el.
271           (while (eq article gnus-async-current-prefetch-article)
272             (incf tries)
273             (when (nntp-accept-process-output proc 1)
274               (setq tries 0))
275             (when (and (not nntp-have-messaged) (eq 3 tries))
276               (gnus-message 5 "Waiting for async article...")
277               (setq nntp-have-messaged t)))
278         (quit
279          ;; if the user interrupted on a slow/hung connection,
280          ;; do something friendly.
281          (when (< 3 tries)
282            (setq gnus-async-current-prefetch-article nil))
283          (signal 'quit nil)))
284       (when nntp-have-messaged
285         (gnus-message 5 "")))))
286
287 (defun gnus-async-delete-prefetched-entry (entry)
288   "Delete ENTRY from buffer and alist."
289   (ignore-errors
290     (delete-region (cadr entry) (caddr entry))
291     (set-marker (cadr entry) nil)
292     (set-marker (caddr entry) nil))
293   (gnus-async-with-semaphore
294    (setq gnus-async-article-alist
295          (delq entry gnus-async-article-alist))))
296
297 (defun gnus-async-prefetch-remove-group (group)
298   "Remove all articles belonging to GROUP from the prefetch buffer."
299   (when (and (gnus-group-asynchronous-p group)
300              (memq 'exit gnus-prefetched-article-deletion-strategy))
301     (let ((alist gnus-async-article-alist))
302       (save-excursion
303         (gnus-async-set-buffer)
304         (while alist
305           (when (equal group (nth 3 (car alist)))
306             (gnus-async-delete-prefetched-entry (car alist)))
307           (pop alist))))))
308
309 (defun gnus-async-prefetched-article-entry (group article)
310   "Return the entry for ARTICLE in GROUP iff it has been prefetched."
311   (let ((entry (save-excursion
312                  (gnus-async-set-buffer)
313                  (assq (intern (format "%s-%d" group article)
314                                gnus-async-hashtb)
315                        gnus-async-article-alist))))
316     ;; Perhaps something has emptied the buffer?
317     (if (and entry
318              (= (cadr entry) (caddr entry)))
319         (progn
320           (ignore-errors
321             (set-marker (cadr entry) nil)
322             (set-marker (caddr entry) nil))
323           (setq gnus-async-article-alist
324                 (delq entry gnus-async-article-alist))
325           nil)
326       entry)))
327
328 ;;;
329 ;;; Header prefetch
330 ;;;
331
332 (defun gnus-async-prefetch-headers (group)
333   "Prefetch the headers for group GROUP."
334   (save-excursion
335     (let (unread)
336       (when (and gnus-use-header-prefetch
337                  gnus-asynchronous
338                  (gnus-group-asynchronous-p group)
339                  (listp gnus-async-header-prefetched)
340                  (setq unread (gnus-list-of-unread-articles group)))
341         ;; Mark that a fetch is in progress.
342         (setq gnus-async-header-prefetched t)
343         (nnheader-set-temp-buffer gnus-async-prefetch-headers-buffer t)
344         (erase-buffer)
345         (let ((nntp-server-buffer (current-buffer))
346               (nnheader-callback-function
347                `(lambda (arg)
348                   (setq gnus-async-header-prefetched
349                         ,(cons group unread)))))
350           (gnus-retrieve-headers unread group gnus-fetch-old-headers))))))
351
352 (defun gnus-async-retrieve-fetched-headers (articles group)
353   "See whether we have prefetched headers."
354   (when (and gnus-use-header-prefetch
355              (gnus-group-asynchronous-p group)
356              (listp gnus-async-header-prefetched)
357              (equal group (car gnus-async-header-prefetched))
358              (equal articles (cdr gnus-async-header-prefetched)))
359     (save-excursion
360       (nnheader-set-temp-buffer gnus-async-prefetch-headers-buffer t)
361       (nntp-decode-text)
362       (copy-to-buffer nntp-server-buffer (point-min) (point-max))
363       (erase-buffer)
364       (setq gnus-async-header-prefetched nil)
365       t)))
366
367 (provide 'gnus-async)
368
369 ;;; gnus-async.el ends here