gnus-async.el 13 KB

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