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