(message-reply): Take an optional switch-buffer parameter so that Gnus window confs...
[gnus] / lisp / gnus-async.el
1 ;;; gnus-async.el --- asynchronous support for Gnus
2
3 ;; Copyright (C) 1996-2011 Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
22
23 ;;; Commentary:
24
25 ;;; Code:
26
27 (eval-when-compile (require 'cl))
28
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-use-article-prefetch 30
38   "*If non-nil, prefetch articles in groups that allow this.
39 If a number, prefetch only that many articles forward;
40 if t, prefetch as many articles as possible."
41   :group 'gnus-asynchronous
42   :type '(choice (const :tag "off" nil)
43                  (const :tag "all" t)
44                  (integer :tag "some" 0)))
45
46 (defcustom gnus-asynchronous nil
47   "*If nil, inhibit all Gnus asynchronicity.
48 If non-nil, let the other asynch variables be heeded."
49   :group 'gnus-asynchronous
50   :type 'boolean)
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 (defcustom gnus-async-post-fetch-function nil
74   "Function called after an article has been prefetched.
75 The function will be called narrowed to the region of the article
76 that was fetched."
77   :group 'gnus-asynchronous
78   :type 'function)
79
80 ;;; Internal variables.
81
82 (defvar gnus-async-prefetch-article-buffer " *Async Prefetch Article*")
83 (defvar gnus-async-article-alist nil)
84 (defvar gnus-async-article-semaphore '(nil))
85 (defvar gnus-async-fetch-list nil)
86 (defvar gnus-async-hashtb nil)
87 (defvar gnus-async-current-prefetch-group nil)
88 (defvar gnus-async-current-prefetch-article nil)
89 (defvar gnus-async-timer nil)
90
91 (defvar gnus-async-prefetch-headers-buffer " *Async Prefetch Headers*")
92 (defvar gnus-async-header-prefetched nil)
93
94 ;;; Utility functions.
95
96 (defun gnus-group-asynchronous-p (group)
97   "Say whether GROUP is fetched from a server that supports asynchronicity."
98   (gnus-asynchronous-p (gnus-find-method-for-group group)))
99
100 ;;; Somewhat bogus semaphores.
101
102 (defun gnus-async-get-semaphore (semaphore)
103   "Wait until SEMAPHORE is released."
104   (while (/= (length (nconc (symbol-value semaphore) (list nil))) 2)
105     (sleep-for 1)))
106
107 (defun gnus-async-release-semaphore (semaphore)
108   "Release SEMAPHORE."
109   (setcdr (symbol-value semaphore) nil))
110
111 (defmacro gnus-async-with-semaphore (&rest forms)
112   `(unwind-protect
113        (progn
114          (gnus-async-get-semaphore 'gnus-async-article-semaphore)
115          ,@forms)
116      (gnus-async-release-semaphore 'gnus-async-article-semaphore)))
117
118 (put 'gnus-async-with-semaphore 'lisp-indent-function 0)
119 (put 'gnus-async-with-semaphore 'edebug-form-spec '(body))
120
121 ;;;
122 ;;; Article prefetch
123 ;;;
124
125 (gnus-add-shutdown 'gnus-async-close 'gnus)
126 (defun gnus-async-close ()
127   (gnus-kill-buffer gnus-async-prefetch-article-buffer)
128   (gnus-kill-buffer gnus-async-prefetch-headers-buffer)
129   (setq gnus-async-hashtb nil
130         gnus-async-article-alist nil
131         gnus-async-header-prefetched nil))
132
133 (defun gnus-async-set-buffer ()
134   (nnheader-set-temp-buffer gnus-async-prefetch-article-buffer t)
135   (unless gnus-async-hashtb
136     (setq gnus-async-hashtb (gnus-make-hashtable 1023))))
137
138 (defun gnus-async-halt-prefetch ()
139   "Stop prefetching."
140   (setq gnus-async-fetch-list nil))
141
142 (defun gnus-async-prefetch-next (group article summary)
143   "Possibly prefetch several articles starting with the article after ARTICLE."
144   (when (and (gnus-buffer-live-p summary)
145              gnus-asynchronous
146              (gnus-group-asynchronous-p group))
147     (with-current-buffer gnus-summary-buffer
148       (let ((next (caadr (gnus-data-find-list article))))
149         (when next
150           (if (not (fboundp 'run-with-idle-timer))
151               ;; This is either an older Emacs or XEmacs, so we
152               ;; do this, which leads to slightly slower article
153               ;; buffer display.
154               (gnus-async-prefetch-article group next summary)
155             (when gnus-async-timer
156               (ignore-errors
157                 (nnheader-cancel-timer 'gnus-async-timer)))
158             (setq gnus-async-timer
159                   (run-with-idle-timer
160                    0.1 nil 'gnus-async-prefetch-article
161                    group next summary))))))))
162
163 (defun gnus-async-prefetch-article (group article summary &optional next)
164   "Possibly prefetch several articles starting with ARTICLE."
165   (if (not (gnus-buffer-live-p summary))
166       (gnus-async-with-semaphore
167         (setq gnus-async-fetch-list nil))
168     (when (and gnus-asynchronous
169                (gnus-alive-p))
170       (when next
171         (gnus-async-with-semaphore
172           (pop gnus-async-fetch-list)))
173       (let ((do-fetch next)
174             (do-message t))             ;(eq major-mode 'gnus-summary-mode)))
175         (when (and (gnus-group-asynchronous-p group)
176                    (gnus-buffer-live-p summary)
177                    (or (not next)
178                        gnus-async-fetch-list))
179           (gnus-async-with-semaphore
180             (unless next
181               (setq do-fetch (not gnus-async-fetch-list))
182               ;; Nix out any outstanding requests.
183               (setq gnus-async-fetch-list nil)
184               ;; Fill in the new list.
185               (let ((n gnus-use-article-prefetch)
186                     (data (gnus-data-find-list article))
187                     d)
188                 (while (and (setq d (pop data))
189                             (if (numberp n)
190                                 (natnump (decf n))
191                               n))
192                   (unless (or (gnus-async-prefetched-article-entry
193                                group (setq article (gnus-data-number d)))
194                               (not (natnump article))
195                               (not (funcall gnus-async-prefetch-article-p d)))
196                     ;; Not already fetched -- so we add it to the list.
197                     (push article gnus-async-fetch-list)))
198                 (setq gnus-async-fetch-list
199                       (nreverse gnus-async-fetch-list))))
200
201             (when do-fetch
202               (setq article (car gnus-async-fetch-list))))
203
204           (when (and do-fetch article)
205             ;; We want to fetch some more articles.
206             (with-current-buffer summary
207               (let (mark)
208                 (gnus-async-set-buffer)
209                 (goto-char (point-max))
210                 (setq mark (point-marker))
211                 (let ((nnheader-callback-function
212                        (gnus-make-async-article-function
213                         group article mark summary next))
214                       (nntp-server-buffer
215                        (get-buffer gnus-async-prefetch-article-buffer)))
216                   (when do-message
217                     (gnus-message 9 "Prefetching article %d in group %s"
218                                   article group))
219                   (setq gnus-async-current-prefetch-group group)
220                   (setq gnus-async-current-prefetch-article article)
221                   (gnus-request-article article group))))))))))
222
223 (defun gnus-make-async-article-function (group article mark summary next)
224   "Return a callback function."
225   `(lambda (arg)
226      (gnus-async-article-callback arg ,group ,article ,mark ,summary ,next)))
227
228 (eval-when-compile
229   (autoload 'gnus-html-prefetch-images "gnus-html"))
230
231 (defun gnus-async-article-callback (arg group article mark summary next)
232   "Function called when an async article is done being fetched."
233   (save-excursion
234     (setq gnus-async-current-prefetch-article nil)
235     (when arg
236       (gnus-async-set-buffer)
237       (save-excursion
238         (save-restriction
239           (narrow-to-region mark (point-max))
240           ;; Prefetch images for the groups that want that.
241           (when (fboundp 'gnus-html-prefetch-images)
242             (gnus-html-prefetch-images summary))
243           (when gnus-async-post-fetch-function
244             (funcall gnus-async-post-fetch-function summary))))
245       (gnus-async-with-semaphore
246         (setq
247          gnus-async-article-alist
248          (cons (list (intern (format "%s-%d" group article)
249                              gnus-async-hashtb)
250                      mark (set-marker (make-marker) (point-max))
251                      group article)
252                gnus-async-article-alist))))
253     (if (not (gnus-buffer-live-p summary))
254         (gnus-async-with-semaphore
255           (setq gnus-async-fetch-list nil))
256       (gnus-async-prefetch-article group next summary t))))
257
258 (defun gnus-async-unread-p (data)
259   "Return non-nil if DATA represents an unread article."
260   (gnus-data-unread-p data))
261
262 (defun gnus-async-request-fetched-article (group article buffer)
263   "See whether we have ARTICLE from GROUP and put it in BUFFER."
264   (when (numberp article)
265     (when (and (equal group gnus-async-current-prefetch-group)
266                (eq article gnus-async-current-prefetch-article))
267       (gnus-async-wait-for-article article))
268     (let ((entry (gnus-async-prefetched-article-entry group article)))
269       (when entry
270         (save-excursion
271           (gnus-async-set-buffer)
272           (copy-to-buffer buffer (cadr entry) (caddr entry))
273           ;; Remove the read article from the prefetch buffer.
274           (when (memq 'read gnus-prefetched-article-deletion-strategy)
275             (gnus-async-delete-prefetched-entry entry))
276           t)))))
277
278 (defun gnus-async-wait-for-article (article)
279   "Wait until ARTICLE is no longer the currently-being-fetched article."
280   (save-excursion
281     (gnus-async-set-buffer)
282     (let ((proc (nntp-find-connection (current-buffer)))
283           (nntp-server-buffer (current-buffer))
284           (nntp-have-messaged nil)
285           (tries 0))
286       (when proc
287         (condition-case nil
288             ;; FIXME: we could stop waiting after some
289             ;; timeout, but this is the wrong place to do it.
290             ;; rather than checking time-spent-waiting, we
291             ;; should check time-since-last-output, which
292             ;; needs to be done in nntp.el.
293             (while (eq article gnus-async-current-prefetch-article)
294               (incf tries)
295               (when (nntp-accept-process-output proc)
296                 (setq tries 0))
297               (when (and (not nntp-have-messaged)
298                          (= tries 3))
299                 (gnus-message 5 "Waiting for async article...")
300                 (setq nntp-have-messaged t)))
301           (quit
302            ;; if the user interrupted on a slow/hung connection,
303            ;; do something friendly.
304            (when (> tries 3)
305              (setq gnus-async-current-prefetch-article nil))
306            (signal 'quit nil)))
307         (when nntp-have-messaged
308           (gnus-message 5 ""))))))
309
310 (defun gnus-async-delete-prefetched-entry (entry)
311   "Delete ENTRY from buffer and alist."
312   (ignore-errors
313     (delete-region (cadr entry) (caddr entry))
314     (set-marker (cadr entry) nil)
315     (set-marker (caddr entry) nil))
316   (gnus-async-with-semaphore
317     (setq gnus-async-article-alist
318           (delq entry gnus-async-article-alist))
319     (unintern (car entry) gnus-async-hashtb)))
320
321 (defun gnus-async-prefetch-remove-group (group)
322   "Remove all articles belonging to GROUP from the prefetch buffer."
323   (when (and (gnus-group-asynchronous-p group)
324              (memq 'exit gnus-prefetched-article-deletion-strategy))
325     (save-excursion
326       (gnus-async-set-buffer)
327       (dolist (entry gnus-async-article-alist)
328         (when (equal group (nth 3 entry))
329           (gnus-async-delete-prefetched-entry entry))))))
330
331 (defun gnus-async-prefetched-article-entry (group article)
332   "Return the entry for ARTICLE in GROUP if it has been prefetched."
333   (let ((entry (save-excursion
334                  (gnus-async-set-buffer)
335                  (assq (intern-soft (format "%s-%d" group article)
336                                     gnus-async-hashtb)
337                        gnus-async-article-alist))))
338     ;; Perhaps something has emptied the buffer?
339     (if (and entry
340              (= (cadr entry) (caddr entry)))
341         (progn
342           (ignore-errors
343             (set-marker (cadr entry) nil)
344             (set-marker (caddr entry) nil))
345           (setq gnus-async-article-alist
346                 (delq entry gnus-async-article-alist))
347           nil)
348       entry)))
349
350 ;;;
351 ;;; Header prefetch
352 ;;;
353
354 (defun gnus-async-prefetch-headers (group)
355   "Prefetch the headers for group GROUP."
356   (save-excursion
357     (let (unread)
358       (when (and gnus-use-header-prefetch
359                  gnus-asynchronous
360                  (gnus-group-asynchronous-p group)
361                  (listp gnus-async-header-prefetched)
362                  (setq unread (gnus-list-of-unread-articles group)))
363         ;; Mark that a fetch is in progress.
364         (setq gnus-async-header-prefetched t)
365         (nnheader-set-temp-buffer gnus-async-prefetch-headers-buffer t)
366         (erase-buffer)
367         (let ((nntp-server-buffer (current-buffer))
368               (nnheader-callback-function
369                `(lambda (arg)
370                   (setq gnus-async-header-prefetched
371                         ,(cons group unread)))))
372           (gnus-retrieve-headers unread group gnus-fetch-old-headers))))))
373
374 (defun gnus-async-retrieve-fetched-headers (articles group)
375   "See whether we have prefetched headers."
376   (when (and gnus-use-header-prefetch
377              (gnus-group-asynchronous-p group)
378              (listp gnus-async-header-prefetched)
379              (equal group (car gnus-async-header-prefetched))
380              (equal articles (cdr gnus-async-header-prefetched)))
381     (save-excursion
382       (nnheader-set-temp-buffer gnus-async-prefetch-headers-buffer t)
383       (nntp-decode-text)
384       (copy-to-buffer nntp-server-buffer (point-min) (point-max))
385       (erase-buffer)
386       (setq gnus-async-header-prefetched nil)
387       t)))
388
389 (provide 'gnus-async)
390
391 ;;; gnus-async.el ends here