Sync up with Pterodactyl Gnus v0.99.
[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 (require 'gnus)
30 (require 'gnus-sum)
31 (require 'nntp)
32
33 (defgroup gnus-asynchronous nil
34   "Support for asynchronous operations."
35   :group 'gnus)
36
37 (defcustom gnus-asynchronous t
38   "*If nil, inhibit all Gnus asynchronicity.
39 If non-nil, let the other asynch variables be heeded."
40   :group 'gnus-asynchronous
41   :type 'boolean)
42
43 (defcustom gnus-use-article-prefetch 30
44   "*If non-nil, prefetch articles in groups that allow this.
45 If a number, prefetch only that many articles forward;
46 if t, prefetch as many articles as possible."
47   :group 'gnus-asynchronous
48   :type '(choice (const :tag "off" nil)
49                  (const :tag "all" t)
50                  (integer :tag "some" 0)))
51
52 (defcustom gnus-prefetched-article-deletion-strategy '(read exit)
53   "List of symbols that say when to remove articles from the prefetch buffer.
54 Possible values in this list are `read', which means that
55 articles are removed as they are read, and `exit', which means
56 that all articles belonging to a group are removed on exit
57 from that group."
58   :group 'gnus-asynchronous
59   :type '(set (const read) (const exit)))
60
61 (defcustom gnus-use-header-prefetch nil
62   "*If non-nil, prefetch the headers to the next group."
63   :group 'gnus-asynchronous
64   :type 'boolean)
65
66 (defcustom gnus-async-prefetch-article-p 'gnus-async-unread-p
67   "Function called to say whether an article should be prefetched or not.
68 The function is called with one parameter -- the article data.
69 It should return non-nil if the article is to be prefetched."
70   :group 'gnus-asynchronous
71   :type 'function)
72
73 ;;; Internal variables.
74
75 (defvar gnus-async-prefetch-article-buffer " *Async Prefetch Article*")
76 (defvar gnus-async-article-alist nil)
77 (defvar gnus-async-article-semaphore '(nil))
78 (defvar gnus-async-fetch-list nil)
79 (defvar gnus-async-hashtb nil)
80 (defvar gnus-async-current-prefetch-group nil)
81 (defvar gnus-async-current-prefetch-article nil)
82 (defvar gnus-async-timer 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             (when gnus-async-timer
150               (ignore-errors
151                 (nnheader-cancel-timer 'gnus-async-timer)))
152             (setq gnus-async-timer
153                   (run-with-idle-timer
154                    0.1 nil 'gnus-async-prefetch-article
155                    group next summary))))))))
156
157 (defun gnus-async-prefetch-article (group article summary &optional next)
158   "Possibly prefetch several articles starting with ARTICLE."
159   (if (not (gnus-buffer-live-p summary))
160       (gnus-async-with-semaphore
161        (setq gnus-async-fetch-list nil))
162     (when (and gnus-asynchronous
163                (gnus-alive-p))
164       (when next
165         (gnus-async-with-semaphore
166          (pop gnus-async-fetch-list)))
167       (let ((do-fetch next)
168             (do-message t)) ;(eq major-mode 'gnus-summary-mode)))
169         (when (and (gnus-group-asynchronous-p group)
170                    (gnus-buffer-live-p summary)
171                    (or (not next)
172                        gnus-async-fetch-list))
173           (gnus-async-with-semaphore
174            (unless next
175              (setq do-fetch (not gnus-async-fetch-list))
176              ;; Nix out any outstanding requests.
177              (setq gnus-async-fetch-list nil)
178              ;; Fill in the new list.
179              (let ((n gnus-use-article-prefetch)
180                    (data (gnus-data-find-list article))
181                    d)
182                (while (and (setq d (pop data))
183                            (if (numberp n)
184                                (natnump (decf n))
185                              n))
186                  (unless (or (gnus-async-prefetched-article-entry
187                               group (setq article (gnus-data-number d)))
188                              (not (natnump article))
189                              (not (funcall gnus-async-prefetch-article-p d)))
190                    ;; Not already fetched -- so we add it to the list.
191                    (push article gnus-async-fetch-list)))
192                (setq gnus-async-fetch-list
193                      (nreverse gnus-async-fetch-list))))
194
195            (when do-fetch
196              (setq article (car gnus-async-fetch-list))))
197
198           (when (and do-fetch article)
199             ;; We want to fetch some more articles.
200             (save-excursion
201               (set-buffer summary)
202               (let (mark)
203                 (gnus-async-set-buffer)
204                 (goto-char (point-max))
205                 (setq mark (point-marker))
206                 (let ((nnheader-callback-function
207                        (gnus-make-async-article-function
208                         group article mark summary next))
209                       (nntp-server-buffer
210                        (get-buffer gnus-async-prefetch-article-buffer)))
211                   (when do-message
212                     (gnus-message 9 "Prefetching article %d in group %s"
213                                   article group))
214                   (setq gnus-async-current-prefetch-group group)
215                   (setq gnus-async-current-prefetch-article article)
216                   (gnus-request-article article group))))))))))
217
218 (defun gnus-make-async-article-function (group article mark summary next)
219   "Return a callback function."
220   `(lambda (arg)
221      (gnus-async-article-callback arg ,group ,article ,mark ,summary ,next)))
222
223 (defun gnus-async-article-callback (arg group article mark summary next)
224   "Function called when an async article is done being fetched."
225   (save-excursion
226     (setq gnus-async-current-prefetch-article nil)
227     (when arg
228       (gnus-async-set-buffer)
229       (gnus-async-with-semaphore
230        (setq
231         gnus-async-article-alist
232         (cons (list (intern (format "%s-%d" group article)
233                             gnus-async-hashtb)
234                     mark (set-marker (make-marker) (point-max))
235                     group article)
236               gnus-async-article-alist))))
237     (if (not (gnus-buffer-live-p summary))
238         (gnus-async-with-semaphore
239          (setq gnus-async-fetch-list nil))
240       (gnus-async-prefetch-article group next summary t))))
241
242 (defun gnus-async-unread-p (data)
243   "Return non-nil if DATA represents an unread article."
244   (gnus-data-unread-p data))
245
246 (defun gnus-async-request-fetched-article (group article buffer)
247   "See whether we have ARTICLE from GROUP and put it in BUFFER."
248   (when (numberp article)
249     (when (and (equal group gnus-async-current-prefetch-group)
250                (eq article gnus-async-current-prefetch-article))
251       (gnus-async-wait-for-article article))
252     (let ((entry (gnus-async-prefetched-article-entry group article)))
253       (when entry
254         (save-excursion
255           (gnus-async-set-buffer)
256           (copy-to-buffer buffer (cadr entry) (caddr entry))
257           ;; Remove the read article from the prefetch buffer.
258           (when (memq 'read gnus-prefetched-article-deletion-strategy)
259             (gnus-async-delete-prefetched-entry entry))
260           t)))))
261
262 (defun gnus-async-wait-for-article (article)
263   "Wait until ARTICLE is no longer the currently-being-fetched article."
264   (save-excursion
265     (gnus-async-set-buffer)
266     (let ((proc (nntp-find-connection (current-buffer)))
267           (nntp-server-buffer (current-buffer))
268           (nntp-have-messaged nil)
269           (tries 0))
270       (condition-case nil
271           ;; FIXME: we could stop waiting after some
272           ;; timeout, but this is the wrong place to do it.
273           ;; rather than checking time-spent-waiting, we
274           ;; should check time-since-last-output, which
275           ;; needs to be done in nntp.el.
276           (while (eq article gnus-async-current-prefetch-article)
277             (incf tries)
278             (when (nntp-accept-process-output proc 1)
279               (setq tries 0))
280             (when (and (not nntp-have-messaged) (eq 3 tries))
281               (gnus-message 5 "Waiting for async article...")
282               (setq nntp-have-messaged t)))
283         (quit
284          ;; if the user interrupted on a slow/hung connection,
285          ;; do something friendly.
286          (when (< 3 tries)
287            (setq gnus-async-current-prefetch-article nil))
288          (signal 'quit nil)))
289       (when nntp-have-messaged
290         (gnus-message 5 "")))))
291
292 (defun gnus-async-delete-prefetched-entry (entry)
293   "Delete ENTRY from buffer and alist."
294   (ignore-errors
295     (delete-region (cadr entry) (caddr entry))
296     (set-marker (cadr entry) nil)
297     (set-marker (caddr entry) nil))
298   (gnus-async-with-semaphore
299    (setq gnus-async-article-alist
300          (delq entry gnus-async-article-alist))))
301
302 (defun gnus-async-prefetch-remove-group (group)
303   "Remove all articles belonging to GROUP from the prefetch buffer."
304   (when (and (gnus-group-asynchronous-p group)
305              (memq 'exit gnus-prefetched-article-deletion-strategy))
306     (let ((alist gnus-async-article-alist))
307       (save-excursion
308         (gnus-async-set-buffer)
309         (while alist
310           (when (equal group (nth 3 (car alist)))
311             (gnus-async-delete-prefetched-entry (car alist)))
312           (pop alist))))))
313
314 (defun gnus-async-prefetched-article-entry (group article)
315   "Return the entry for ARTICLE in GROUP iff it has been prefetched."
316   (let ((entry (save-excursion
317                  (gnus-async-set-buffer)
318                  (assq (intern (format "%s-%d" group article)
319                                gnus-async-hashtb)
320                        gnus-async-article-alist))))
321     ;; Perhaps something has emptied the buffer?
322     (if (and entry
323              (= (cadr entry) (caddr entry)))
324         (progn
325           (ignore-errors
326             (set-marker (cadr entry) nil)
327             (set-marker (caddr entry) nil))
328           (setq gnus-async-article-alist
329                 (delq entry gnus-async-article-alist))
330           nil)
331       entry)))
332
333 ;;;
334 ;;; Header prefetch
335 ;;;
336
337 (defun gnus-async-prefetch-headers (group)
338   "Prefetch the headers for group GROUP."
339   (save-excursion
340     (let (unread)
341       (when (and gnus-use-header-prefetch
342                  gnus-asynchronous
343                  (gnus-group-asynchronous-p group)
344                  (listp gnus-async-header-prefetched)
345                  (setq unread (gnus-list-of-unread-articles group)))
346         ;; Mark that a fetch is in progress.
347         (setq gnus-async-header-prefetched t)
348         (nnheader-set-temp-buffer gnus-async-prefetch-headers-buffer t)
349         (erase-buffer)
350         (let ((nntp-server-buffer (current-buffer))
351               (nnheader-callback-function
352                `(lambda (arg)
353                   (setq gnus-async-header-prefetched
354                         ,(cons group unread)))))
355           (gnus-retrieve-headers unread group gnus-fetch-old-headers))))))
356
357 (defun gnus-async-retrieve-fetched-headers (articles group)
358   "See whether we have prefetched headers."
359   (when (and gnus-use-header-prefetch
360              (gnus-group-asynchronous-p group)
361              (listp gnus-async-header-prefetched)
362              (equal group (car gnus-async-header-prefetched))
363              (equal articles (cdr gnus-async-header-prefetched)))
364     (save-excursion
365       (nnheader-set-temp-buffer gnus-async-prefetch-headers-buffer t)
366       (nntp-decode-text)
367       (copy-to-buffer nntp-server-buffer (point-min) (point-max))
368       (erase-buffer)
369       (setq gnus-async-header-prefetched nil)
370       t)))
371
372 (provide 'gnus-async)
373
374 ;;; gnus-async.el ends here