PageRenderTime 56ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/lisp/gnus/gnus-demon.el

https://gitlab.com/RobertCochran/emacs
Emacs Lisp | 279 lines | 202 code | 40 blank | 37 comment | 4 complexity | eb5c41693df09eca95fc53dea2cfcdda MD5 | raw file
  1. ;;; gnus-demon.el --- daemonic Gnus behavior
  2. ;; Copyright (C) 1995-2019 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 <https://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;;; Code:
  18. (eval-when-compile (require 'cl-lib))
  19. (require 'gnus)
  20. (require 'gnus-int)
  21. (require 'nnheader)
  22. (require 'nntp)
  23. (require 'nnmail)
  24. (defgroup gnus-demon nil
  25. "Demonic behavior."
  26. :group 'gnus)
  27. (defcustom gnus-demon-handlers nil
  28. "Alist of daemonic handlers to be run at intervals.
  29. Each handler is a list on the form
  30. \(FUNCTION TIME IDLE)
  31. FUNCTION is the function to be called. TIME is the number of
  32. `gnus-demon-timestep's between each call.
  33. If nil, never call. If t, call each `gnus-demon-timestep'.
  34. If IDLE is t, only call each time Emacs has been idle for TIME.
  35. If IDLE is a number, only call when Emacs has been idle more than
  36. this number of `gnus-demon-timestep's.
  37. If IDLE is nil, don't care about idleness.
  38. If IDLE is a number and TIME is nil, then call once each time
  39. Emacs has been idle for IDLE `gnus-demon-timestep's."
  40. :group 'gnus-demon
  41. :type '(repeat (list function
  42. (choice :tag "Time"
  43. (const :tag "never" nil)
  44. (const :tag "one" t)
  45. (integer :tag "steps" 1))
  46. (choice :tag "Idle"
  47. (const :tag "don't care" nil)
  48. (const :tag "for a while" t)
  49. (integer :tag "steps" 1)))))
  50. (defcustom gnus-demon-timestep 60
  51. "Number of seconds in each demon timestep."
  52. :group 'gnus-demon
  53. :type 'integer)
  54. ;;; Internal variables.
  55. (defvar gnus-demon-timers nil
  56. "Plist of idle timers which are running.")
  57. (defvar gnus-inhibit-demon nil
  58. "If non-nil, no daemonic function will be run.")
  59. ;;; Functions.
  60. (defun gnus-demon-add-handler (function time idle)
  61. "Add the handler FUNCTION to be run at TIME and IDLE."
  62. ;; First remove any old handlers that use this function.
  63. (gnus-demon-remove-handler function)
  64. ;; Then add the new one.
  65. (push (list function time idle) gnus-demon-handlers)
  66. (gnus-demon-init))
  67. (defun gnus-demon-remove-handler (function &optional no-init)
  68. "Remove the handler FUNCTION from the list of handlers."
  69. (gnus-alist-pull function gnus-demon-handlers)
  70. (unless no-init
  71. (gnus-demon-init)))
  72. (defun gnus-demon-idle-since ()
  73. "Return the number of seconds since when Emacs is idle."
  74. (float-time (or (current-idle-time) 0)))
  75. (defun gnus-demon-run-callback (func &optional idle time special)
  76. "Run FUNC if Emacs has been idle for longer than IDLE seconds.
  77. If not, and a TIME is given, restart a new idle timer, so FUNC
  78. can be called at the next opportunity. Such a special idle run
  79. is marked with SPECIAL."
  80. (unless gnus-inhibit-demon
  81. (cl-block run-callback
  82. (when (eq idle t)
  83. (setq idle 0.001))
  84. (cond (special
  85. (setq gnus-demon-timers
  86. (plist-put gnus-demon-timers func
  87. (run-with-timer time time 'gnus-demon-run-callback
  88. func idle time))))
  89. ((and idle (> idle (gnus-demon-idle-since)))
  90. (when time
  91. (cancel-timer (plist-get gnus-demon-timers func))
  92. (setq gnus-demon-timers
  93. (plist-put gnus-demon-timers func
  94. (run-with-idle-timer idle nil
  95. 'gnus-demon-run-callback
  96. func idle time t))))
  97. (cl-return-from run-callback)))
  98. (with-local-quit
  99. (ignore-errors
  100. (funcall func))))))
  101. (defun gnus-demon-init ()
  102. "Initialize the Gnus daemon."
  103. (interactive)
  104. (gnus-demon-cancel)
  105. (dolist (handler gnus-demon-handlers)
  106. ;; Set up the timer.
  107. (let* ((func (nth 0 handler))
  108. (time (nth 1 handler))
  109. (idle (nth 2 handler))
  110. ;; Compute time according with timestep.
  111. ;; If t, replace by 1
  112. (time (cond ((eq time t)
  113. gnus-demon-timestep)
  114. ((null time)
  115. nil)
  116. ((stringp time)
  117. (* (gnus-demon-time-to-step time) gnus-demon-timestep))
  118. (t
  119. (* time gnus-demon-timestep))))
  120. (idle (cond ((numberp idle)
  121. (* idle gnus-demon-timestep))
  122. ((and (eq idle t) (numberp time))
  123. time)
  124. (t
  125. idle)))
  126. (timer
  127. (cond
  128. ;; (func nil number)
  129. ;; Only call when Emacs has been idle for `idle'
  130. ((and (null time) (numberp idle))
  131. (run-with-idle-timer idle t 'gnus-demon-run-callback func))
  132. ;; (func number any)
  133. ;; Call every `time'
  134. ((integerp time)
  135. (run-with-timer time time 'gnus-demon-run-callback
  136. func idle time))
  137. ;; (func string any)
  138. ((stringp time)
  139. (run-with-timer time (* 24 60 60) 'gnus-demon-run-callback
  140. func idle)))))
  141. (when timer
  142. (setq gnus-demon-timers (plist-put gnus-demon-timers func timer))))))
  143. (defun gnus-demon-time-to-step (time)
  144. "Find out how many steps to TIME, which is on the form \"17:43\"."
  145. (let* ((now (current-time))
  146. ;; obtain NOW as discrete components -- make a vector for speed
  147. (nowParts (decode-time now))
  148. ;; obtain THEN as discrete components
  149. (thenParts (parse-time-string time))
  150. (thenHour (decoded-time-hour thenParts))
  151. (thenMin (decoded-time-minute thenParts))
  152. ;; convert time as elements into number of seconds since EPOCH.
  153. (then (encode-time
  154. 0
  155. thenMin
  156. thenHour
  157. ;; If THEN is earlier than NOW, make it
  158. ;; same time tomorrow. Doc for encode-time
  159. ;; says that this is OK.
  160. (+ (decoded-time-day nowParts)
  161. (if (or (< thenHour (decoded-time-hour nowParts))
  162. (and (= thenHour
  163. (decoded-time-hour nowParts))
  164. (<= thenMin
  165. (decoded-time-minute nowParts))))
  166. 1 0))
  167. (decoded-time-month nowParts)
  168. (decoded-time-year nowParts)
  169. (decoded-time-weekday nowParts)
  170. (decoded-time-dst nowParts)
  171. (decoded-time-zone nowParts)))
  172. (diff (float-time (time-subtract then now))))
  173. ;; Return number of timesteps in the number of seconds.
  174. (round diff gnus-demon-timestep)))
  175. (gnus-add-shutdown 'gnus-demon-cancel 'gnus)
  176. (defun gnus-demon-cancel ()
  177. "Cancel any Gnus daemons."
  178. (interactive)
  179. (dotimes (i (/ (length gnus-demon-timers) 2))
  180. (cancel-timer (nth (1+ (* i 2)) gnus-demon-timers)))
  181. (setq gnus-demon-timers nil))
  182. (defun gnus-demon-add-disconnection ()
  183. "Add daemonic server disconnection to Gnus."
  184. (gnus-demon-add-handler 'gnus-demon-close-connections nil 30))
  185. (defun gnus-demon-close-connections ()
  186. (save-window-excursion
  187. (gnus-close-backends)))
  188. (defun gnus-demon-add-nntp-close-connection ()
  189. "Add daemonic nntp server disconnection to Gnus.
  190. If no commands have gone out via nntp during the last five
  191. minutes, the connection is closed."
  192. (gnus-demon-add-handler 'gnus-demon-nntp-close-connection 5 nil))
  193. (defun gnus-demon-nntp-close-connection ()
  194. (save-window-excursion
  195. (when (time-less-p '(0 300) (time-since nntp-last-command-time))
  196. (nntp-close-server))))
  197. (defun gnus-demon-add-scanmail ()
  198. "Add daemonic scanning of mail from the mail backends."
  199. (gnus-demon-add-handler 'gnus-demon-scan-mail 120 60))
  200. (defun gnus-demon-scan-mail ()
  201. (save-window-excursion
  202. (let ((servers gnus-opened-servers)
  203. server
  204. (nnmail-fetched-sources (list t)))
  205. (while (setq server (car (pop servers)))
  206. (and (gnus-check-backend-function 'request-scan (car server))
  207. (or (gnus-server-opened server)
  208. (gnus-open-server server))
  209. (gnus-request-scan nil server))))))
  210. (defun gnus-demon-add-rescan ()
  211. "Add daemonic scanning of new articles from all backends."
  212. (gnus-demon-add-handler 'gnus-demon-scan-news 120 60))
  213. (defun gnus-demon-scan-news ()
  214. (let ((win (current-window-configuration)))
  215. (unwind-protect
  216. (save-window-excursion
  217. (when (gnus-alive-p)
  218. (with-current-buffer gnus-group-buffer
  219. (gnus-group-get-new-news))))
  220. (set-window-configuration win))))
  221. (defun gnus-demon-add-scan-timestamps ()
  222. "Add daemonic updating of timestamps in empty newgroups."
  223. (gnus-demon-add-handler 'gnus-demon-scan-timestamps nil 30))
  224. (defun gnus-demon-scan-timestamps ()
  225. "Set the timestamp on all newsgroups with no unread and no ticked articles."
  226. (when (gnus-alive-p)
  227. (let ((cur-time (current-time))
  228. (newsrc (cdr gnus-newsrc-alist))
  229. info group unread has-ticked)
  230. (while (setq info (pop newsrc))
  231. (setq group (gnus-info-group info)
  232. unread (gnus-group-unread group)
  233. has-ticked (cdr (assq 'tick (gnus-info-marks info))))
  234. (when (and (numberp unread)
  235. (= unread 0)
  236. (not has-ticked))
  237. (gnus-group-set-parameter group 'timestamp cur-time))))))
  238. (provide 'gnus-demon)
  239. ;;; gnus-demon.el ends here