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