1 ;;; gnus-demon.el --- daemonic Gnus behaviour
2 ;; Copyright (C) 1995,96 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
34 (defvar gnus-demon-handlers nil
35 "Alist of daemonic handlers to be run at intervals.
36 Each handler is a list on the form
40 FUNCTION is the function to be called.
41 TIME is the number of `gnus-demon-timestep's between each call.
42 If nil, never call. If t, call each `gnus-demon-timestep'.
43 If IDLE is t, only call if Emacs has been idle for a while. If IDLE
44 is a number, only call when Emacs has been idle more than this number
45 of `gnus-demon-timestep's. If IDLE is nil, don't care about
46 idleness. If IDLE is a number and TIME is nil, then call once each
47 time Emacs has been idle for IDLE `gnus-demon-timestep's.")
49 (defvar gnus-demon-timestep 60
50 "*Number of seconds in each demon timestep.")
52 ;;; Internal variables.
54 (defvar gnus-demon-timer nil)
55 (defvar gnus-demon-idle-has-been-called nil)
56 (defvar gnus-demon-idle-time 0)
57 (defvar gnus-demon-handler-state nil)
58 (defvar gnus-demon-is-idle nil)
59 (defvar gnus-demon-last-keys nil)
62 (autoload 'timezone-parse-date "timezone")
63 (autoload 'timezone-make-arpa-date "timezone"))
67 (defun gnus-demon-add-handler (function time idle)
68 "Add the handler FUNCTION to be run at TIME and IDLE."
69 ;; First remove any old handlers that use this function.
70 (gnus-demon-remove-handler function)
71 ;; Then add the new one.
72 (push (list function time idle) gnus-demon-handlers)
75 (defun gnus-demon-remove-handler (function &optional no-init)
76 "Remove the handler FUNCTION from the list of handlers."
77 (setq gnus-demon-handlers
78 (delq (assq function gnus-demon-handlers)
80 (or no-init (gnus-demon-init)))
82 (defun gnus-demon-init ()
83 "Initialize the Gnus daemon."
86 (if (null gnus-demon-handlers)
89 (setq gnus-demon-timer
91 gnus-demon-timestep gnus-demon-timestep 'gnus-demon))
92 ;; Reset control variables.
93 (setq gnus-demon-handler-state
96 (list (car handler) (gnus-demon-time-to-step (nth 1 handler))
99 (setq gnus-demon-idle-time 0)
100 (setq gnus-demon-idle-has-been-called nil)
101 (setq gnus-use-demon t)))
103 (gnus-add-shutdown 'gnus-demon-cancel 'gnus)
105 (defun gnus-demon-cancel ()
106 "Cancel any Gnus daemons."
108 (and gnus-demon-timer
109 (nnheader-cancel-timer gnus-demon-timer))
110 (setq gnus-demon-timer nil
112 (nnheader-cancel-function-timers 'gnus-demon))
114 (defun gnus-demon-is-idle-p ()
115 "Whether Emacs is idle or not."
116 ;; We do this simply by comparing the 100 most recent keystrokes
117 ;; with the ones we had last time. If they are the same, one might
118 ;; guess that Emacs is indeed idle. This only makes sense if one
119 ;; calls this function seldom -- like once a minute, which is what
121 (let ((keys (recent-keys)))
122 (or (equal keys gnus-demon-last-keys)
124 (setq gnus-demon-last-keys keys)
127 (defun gnus-demon-time-to-step (time)
128 "Find out how many seconds to TIME, which is on the form \"17:43\"."
129 (if (not (stringp time))
131 (let* ((date (current-time-string))
132 (dv (timezone-parse-date date))
133 (tdate (timezone-make-arpa-date
134 (string-to-number (aref dv 0))
135 (string-to-number (aref dv 1))
136 (string-to-number (aref dv 2)) time
137 (or (aref dv 4) "UT")))
138 (nseconds (gnus-time-minus
139 (gnus-encode-date tdate) (gnus-encode-date date))))
141 (/ (if (< nseconds 0)
142 (+ nseconds (* 60 60 24))
143 nseconds) gnus-demon-timestep)))))
146 "The Gnus daemon that takes care of running all Gnus handlers."
147 ;; Increase or reset the time Emacs has been idle.
148 (if (gnus-demon-is-idle-p)
149 (incf gnus-demon-idle-time)
150 (setq gnus-demon-idle-time 0)
151 (setq gnus-demon-idle-has-been-called nil))
152 ;; Then we go through all the handler and call those that are
153 ;; sufficiently ripe.
154 (let ((handlers gnus-demon-handler-state)
157 (setq handler (pop handlers))
159 ((numberp (setq time (nth 1 handler)))
160 ;; These handlers use a regular timeout mechanism. We decrease
161 ;; the timer if it hasn't reached zero yet.
163 (setcar (nthcdr 1 handler) (decf time)))
164 (and (zerop time) ; If the timer now is zero...
165 (or (not (setq idle (nth 2 handler))) ; Don't care about idle.
166 (and (numberp idle) ; Numerical idle...
167 (< idle gnus-demon-idle-time)) ; Idle timed out.
168 gnus-demon-is-idle) ; Or just need to be idle.
169 ;; So we call the handler.
171 (funcall (car handler))
172 ;; And reset the timer.
173 (setcar (nthcdr 1 handler)
174 (gnus-demon-time-to-step
175 (nth 1 (assq (car handler) gnus-demon-handlers)))))))
176 ;; These are only supposed to be called when Emacs is idle.
177 ((null (setq idle (nth 2 handler)))
180 ((not (numberp idle))
181 ;; We want to call this handler each and every time that
183 (funcall (car handler)))
185 ;; We want to call this handler only if Emacs has been idle
186 ;; for a specified number of timesteps.
187 (and (not (memq (car handler) gnus-demon-idle-has-been-called))
188 (< idle gnus-demon-idle-time)
190 (funcall (car handler))
191 ;; Make sure the handler won't be called once more in
193 (push (car handler) gnus-demon-idle-has-been-called))))))))
195 (defun gnus-demon-add-nocem ()
196 "Add daemonic NoCeM handling to Gnus."
197 (gnus-demon-add-handler 'gnus-demon-scan-nocem 60 t))
199 (defun gnus-demon-scan-nocem ()
200 "Scan NoCeM groups for NoCeM messages."
201 (gnus-nocem-scan-groups))
203 (defun gnus-demon-add-disconnection ()
204 "Add daemonic server disconnection to Gnus."
205 (gnus-demon-add-handler 'gnus-demon-close-connections nil 30))
207 (defun gnus-demon-close-connections ()
208 (gnus-close-backends))
210 (defun gnus-demon-add-scanmail ()
211 "Add daemonic scanning of mail from the mail backends."
212 (gnus-demon-add-handler 'gnus-demon-scan-mail 120 60))
214 (defun gnus-demon-scan-mail ()
215 (let ((servers gnus-opened-servers)
217 (while (setq server (car (pop servers)))
218 (and (gnus-check-backend-function 'request-scan (car server))
219 (or (gnus-server-opened server)
220 (gnus-open-server server))
221 (gnus-request-scan nil server)))))
223 (defun gnus-demon-add-rescan ()
224 "Add daemonic scanning of new articles from all backends."
225 (gnus-demon-add-handler 'gnus-demon-scan-news 120 60))
227 (defun gnus-demon-scan-news ()
230 (set-buffer gnus-group-buffer)
231 (gnus-group-get-new-news))))
233 (provide 'gnus-demon)
235 ;;; gnus-demon.el ends here