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