1 ;;; gnus-async.el --- asynchronous support for Gnus
3 ;; Copyright (C) 1996-2016 Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
8 ;; This file is part of GNU Emacs.
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.
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.
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/>.
27 (eval-when-compile (require 'cl))
33 (defgroup gnus-asynchronous nil
34 "Support for asynchronous operations."
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)
44 (integer :tag "some" 0)))
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
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
58 :group 'gnus-asynchronous
59 :type '(set (const read) (const exit)))
61 (defcustom gnus-use-header-prefetch nil
62 "*If non-nil, prefetch the headers to the next group."
63 :group 'gnus-asynchronous
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
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
78 :group 'gnus-asynchronous
79 :type '(choice (const nil) function))
81 ;;; Internal variables.
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)
92 (defvar gnus-async-prefetch-headers-buffer " *Async Prefetch Headers*")
93 (defvar gnus-async-header-prefetched nil)
95 ;;; Utility functions.
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)))
101 ;;; Somewhat bogus semaphores.
103 (defun gnus-async-get-semaphore (semaphore)
104 "Wait until SEMAPHORE is released."
105 (while (/= (length (nconc (symbol-value semaphore) (list nil))) 2)
108 (defun gnus-async-release-semaphore (semaphore)
110 (setcdr (symbol-value semaphore) nil))
112 (defmacro gnus-async-with-semaphore (&rest forms)
115 (gnus-async-get-semaphore 'gnus-async-article-semaphore)
117 (gnus-async-release-semaphore 'gnus-async-article-semaphore)))
119 (put 'gnus-async-with-semaphore 'lisp-indent-function 0)
120 (put 'gnus-async-with-semaphore 'edebug-form-spec '(body))
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))
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))))
139 (defun gnus-async-halt-prefetch ()
141 (setq gnus-async-fetch-list nil))
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)
147 (gnus-group-asynchronous-p group))
148 (with-current-buffer gnus-summary-buffer
149 (let ((next (caadr (gnus-data-find-list article))))
151 (when gnus-async-timer
153 (nnheader-cancel-timer 'gnus-async-timer)))
154 (setq gnus-async-timer
156 0.1 nil 'gnus-async-prefetch-article
157 group next summary)))))))
159 (defun gnus-async-prefetch-article (group article summary &optional next)
160 "Possibly prefetch several articles starting with ARTICLE."
161 (if (not (gnus-buffer-live-p summary))
162 (gnus-async-with-semaphore
163 (setq gnus-async-fetch-list nil))
164 (when (and gnus-asynchronous
167 (gnus-async-with-semaphore
168 (pop gnus-async-fetch-list)))
169 (let ((do-fetch next)
170 (do-message t)) ;(eq major-mode 'gnus-summary-mode)))
171 (when (and (gnus-group-asynchronous-p group)
172 (gnus-buffer-live-p summary)
174 gnus-async-fetch-list))
175 (gnus-async-with-semaphore
177 (setq do-fetch (not gnus-async-fetch-list))
178 ;; Nix out any outstanding requests.
179 (setq gnus-async-fetch-list nil)
180 ;; Fill in the new list.
181 (let ((n gnus-use-article-prefetch)
182 (data (gnus-data-find-list article))
184 (while (and (setq d (pop data))
188 (unless (or (gnus-async-prefetched-article-entry
189 group (setq article (gnus-data-number d)))
190 (not (natnump article))
191 (not (funcall gnus-async-prefetch-article-p d)))
192 ;; Not already fetched -- so we add it to the list.
193 (push article gnus-async-fetch-list)))
194 (setq gnus-async-fetch-list
195 (nreverse gnus-async-fetch-list))))
198 (setq article (car gnus-async-fetch-list))))
200 (when (and do-fetch article)
201 ;; We want to fetch some more articles.
202 (with-current-buffer summary
204 (gnus-async-set-buffer)
205 (goto-char (point-max))
206 (setq mark (point-marker))
207 (let ((nnheader-callback-function
208 (gnus-make-async-article-function
209 group article mark summary next))
211 (get-buffer gnus-async-prefetch-article-buffer)))
213 (gnus-message 9 "Prefetching article %d in group %s"
215 (setq gnus-async-current-prefetch-group group)
216 (setq gnus-async-current-prefetch-article article)
217 (gnus-request-article article group))))))))))
219 (defun gnus-make-async-article-function (group article mark summary next)
220 "Return a callback function."
222 (gnus-async-article-callback arg ,group ,article ,mark ,summary ,next)))
224 (defun gnus-async-article-callback (arg group article mark summary next)
225 "Function called when an async article is done being fetched."
227 (setq gnus-async-current-prefetch-article nil)
229 (gnus-async-set-buffer)
232 (narrow-to-region mark (point-max))
233 ;; Put the articles into the agent, if they aren't already.
234 (when (and gnus-agent
235 (gnus-agent-group-covered-p group))
237 (narrow-to-region mark (point-max))
238 (gnus-agent-store-article article group)))
239 ;; Prefetch images for the groups that want that.
240 (when (fboundp 'gnus-html-prefetch-images)
241 (gnus-html-prefetch-images summary))
242 (when gnus-async-post-fetch-function
243 (funcall gnus-async-post-fetch-function summary))))
244 (gnus-async-with-semaphore
246 gnus-async-article-alist
247 (cons (list (intern (format "%s-%d" group article)
249 mark (point-max-marker)
251 gnus-async-article-alist))))
252 (if (not (gnus-buffer-live-p summary))
253 (gnus-async-with-semaphore
254 (setq gnus-async-fetch-list nil))
255 (gnus-async-prefetch-article group next summary t))))
257 (defun gnus-async-unread-p (data)
258 "Return non-nil if DATA represents an unread article."
259 (gnus-data-unread-p data))
261 (defun gnus-async-request-fetched-article (group article buffer)
262 "See whether we have ARTICLE from GROUP and put it in BUFFER."
263 (when (numberp article)
264 (when (and (equal group gnus-async-current-prefetch-group)
265 (eq article gnus-async-current-prefetch-article))
266 (gnus-async-wait-for-article article))
267 (let ((entry (gnus-async-prefetched-article-entry group article)))
270 (gnus-async-set-buffer)
271 (copy-to-buffer buffer (cadr entry) (caddr entry))
272 ;; Remove the read article from the prefetch buffer.
273 (when (memq 'read gnus-prefetched-article-deletion-strategy)
274 (gnus-async-delete-prefetched-entry entry))
277 (defun gnus-async-wait-for-article (article)
278 "Wait until ARTICLE is no longer the currently-being-fetched article."
280 (gnus-async-set-buffer)
281 (let ((proc (nntp-find-connection (current-buffer)))
282 (nntp-server-buffer (current-buffer))
283 (nntp-have-messaged nil)
287 ;; FIXME: we could stop waiting after some
288 ;; timeout, but this is the wrong place to do it.
289 ;; rather than checking time-spent-waiting, we
290 ;; should check time-since-last-output, which
291 ;; needs to be done in nntp.el.
292 (while (eq article gnus-async-current-prefetch-article)
294 (when (nntp-accept-process-output proc)
296 (when (and (not nntp-have-messaged)
298 (gnus-message 5 "Waiting for async article...")
299 (setq nntp-have-messaged t)))
301 ;; if the user interrupted on a slow/hung connection,
302 ;; do something friendly.
304 (setq gnus-async-current-prefetch-article nil))
306 (when nntp-have-messaged
307 (gnus-message 5 ""))))))
309 (defun gnus-async-delete-prefetched-entry (entry)
310 "Delete ENTRY from buffer and alist."
312 (delete-region (cadr entry) (caddr entry))
313 (set-marker (cadr entry) nil)
314 (set-marker (caddr entry) nil))
315 (gnus-async-with-semaphore
316 (setq gnus-async-article-alist
317 (delq entry gnus-async-article-alist))
318 (unintern (car entry) gnus-async-hashtb)))
320 (defun gnus-async-prefetch-remove-group (group)
321 "Remove all articles belonging to GROUP from the prefetch buffer."
322 (when (and (gnus-group-asynchronous-p group)
323 (memq 'exit gnus-prefetched-article-deletion-strategy))
325 (gnus-async-set-buffer)
326 (dolist (entry gnus-async-article-alist)
327 (when (equal group (nth 3 entry))
328 (gnus-async-delete-prefetched-entry entry))))))
330 (defun gnus-async-prefetched-article-entry (group article)
331 "Return the entry for ARTICLE in GROUP if it has been prefetched."
332 (let ((entry (save-excursion
333 (gnus-async-set-buffer)
334 (assq (intern-soft (format "%s-%d" group article)
336 gnus-async-article-alist))))
337 ;; Perhaps something has emptied the buffer?
339 (= (cadr entry) (caddr entry)))
342 (set-marker (cadr entry) nil)
343 (set-marker (caddr entry) nil))
344 (setq gnus-async-article-alist
345 (delq entry gnus-async-article-alist))
353 (defun gnus-async-prefetch-headers (group)
354 "Prefetch the headers for group GROUP."
357 (when (and gnus-use-header-prefetch
359 (gnus-group-asynchronous-p group)
360 (listp gnus-async-header-prefetched)
361 (setq unread (gnus-list-of-unread-articles group)))
362 ;; Mark that a fetch is in progress.
363 (setq gnus-async-header-prefetched t)
364 (nnheader-set-temp-buffer gnus-async-prefetch-headers-buffer t)
366 (let ((nntp-server-buffer (current-buffer))
367 (nnheader-callback-function
369 (setq gnus-async-header-prefetched
370 ,(cons group unread)))))
371 (gnus-retrieve-headers unread group gnus-fetch-old-headers))))))
373 (defun gnus-async-retrieve-fetched-headers (articles group)
374 "See whether we have prefetched headers."
375 (when (and gnus-use-header-prefetch
376 (gnus-group-asynchronous-p group)
377 (listp gnus-async-header-prefetched)
378 (equal group (car gnus-async-header-prefetched))
379 (equal articles (cdr gnus-async-header-prefetched)))
381 (nnheader-set-temp-buffer gnus-async-prefetch-headers-buffer t)
383 (copy-to-buffer nntp-server-buffer (point-min) (point-max))
385 (setq gnus-async-header-prefetched nil)
388 (provide 'gnus-async)
390 ;;; gnus-async.el ends here