1 ;;; gnus-demon.el --- daemonic Gnus behavior
3 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
28 (eval-when-compile (require 'cl))
37 (defgroup gnus-demon nil
41 (defcustom gnus-demon-handlers nil
42 "Alist of daemonic handlers to be run at intervals.
43 Each handler is a list on the form
47 FUNCTION is the function to be called. TIME is the number of
48 `gnus-demon-timestep's between each call.
49 If nil, never call. If t, call each `gnus-demon-timestep'.
51 If IDLE is t, only call each time Emacs has been idle for TIME.
52 If IDLE is a number, only call when Emacs has been idle more than
53 this number of `gnus-demon-timestep's.
54 If IDLE is nil, don't care about idleness.
55 If IDLE is a number and TIME is nil, then call once each time
56 Emacs has been idle for IDLE `gnus-demon-timestep's."
58 :type '(repeat (list function
60 (const :tag "never" nil)
62 (integer :tag "steps" 1))
64 (const :tag "don't care" nil)
65 (const :tag "for a while" t)
66 (integer :tag "steps" 1)))))
68 (defcustom gnus-demon-timestep 60
69 "Number of seconds in each demon timestep."
73 ;;; Internal variables.
75 (defvar gnus-demon-timers nil
76 "List of idle timers which are running.")
77 (defvar gnus-inhibit-demon nil
78 "If non-nil, no daemonic function will be run.")
82 (defun gnus-demon-add-handler (function time idle)
83 "Add the handler FUNCTION to be run at TIME and IDLE."
84 ;; First remove any old handlers that use this function.
85 (gnus-demon-remove-handler function)
86 ;; Then add the new one.
87 (push (list function time idle) gnus-demon-handlers)
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)
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)
103 (defun gnus-demon-run-callback (func &optional idle)
104 "Run FUNC if Emacs has been idle for longer than IDLE seconds."
105 (unless gnus-inhibit-demon
107 (<= idle (gnus-demon-idle-since)))
108 (gnus-with-local-quit
112 (defun gnus-demon-init ()
113 "Initialize the Gnus daemon."
116 (dolist (handler gnus-demon-handlers)
118 (let* ((func (nth 0 handler))
119 (time (nth 1 handler))
120 (idle (nth 2 handler))
121 ;; Compute time according with timestep.
122 ;; If t, replace by 1
123 (time (cond ((eq time t)
126 (t (* time gnus-demon-timestep))))
130 ;; Call when Emacs has been idle for `time'
131 ((and (numberp time) (eq idle t))
132 (run-with-timer t time 'gnus-demon-run-callback func time))
133 ;; (func number number)
134 ;; Call every `time' when Emacs has been idle for `idle'
135 ((and (numberp time) (numberp idle))
136 (run-with-timer t time 'gnus-demon-run-callback func idle))
138 ;; Only call when Emacs has been idle for `idle'
139 ((and (null time) (numberp idle))
140 (run-with-idle-timer (* idle gnus-demon-timestep) t
141 'gnus-demon-run-callback func))
144 ((and (numberp time) (null idle))
145 (run-with-timer t time 'gnus-demon-run-callback func)))))
147 (add-to-list 'gnus-demon-timers timer)))))
149 (gnus-add-shutdown 'gnus-demon-cancel 'gnus)
151 (defun gnus-demon-cancel ()
152 "Cancel any Gnus daemons."
154 (dolist (timer gnus-demon-timers)
155 (nnheader-cancel-timer timer))
156 (setq gnus-demon-timers nil))
158 (defun gnus-demon-add-disconnection ()
159 "Add daemonic server disconnection to Gnus."
160 (gnus-demon-add-handler 'gnus-demon-close-connections nil 30))
162 (defun gnus-demon-close-connections ()
163 (save-window-excursion
164 (gnus-close-backends)))
166 (defun gnus-demon-add-nntp-close-connection ()
167 "Add daemonic nntp server disconnection to Gnus.
168 If no commands have gone out via nntp during the last five
169 minutes, the connection is closed."
170 (gnus-demon-add-handler 'gnus-demon-nntp-close-connection 5 nil))
172 (defun gnus-demon-nntp-close-connection ()
173 (save-window-excursion
174 (when (time-less-p '(0 300) (time-since nntp-last-command-time))
175 (nntp-close-server))))
177 (defun gnus-demon-add-scanmail ()
178 "Add daemonic scanning of mail from the mail backends."
179 (gnus-demon-add-handler 'gnus-demon-scan-mail 120 60))
181 (defun gnus-demon-scan-mail ()
182 (save-window-excursion
183 (let ((servers gnus-opened-servers)
185 (nnmail-fetched-sources (list t)))
186 (while (setq server (car (pop servers)))
187 (and (gnus-check-backend-function 'request-scan (car server))
188 (or (gnus-server-opened server)
189 (gnus-open-server server))
190 (gnus-request-scan nil server))))))
192 (defun gnus-demon-add-rescan ()
193 "Add daemonic scanning of new articles from all backends."
194 (gnus-demon-add-handler 'gnus-demon-scan-news 120 60))
196 (defun gnus-demon-scan-news ()
197 (let ((win (current-window-configuration)))
199 (save-window-excursion
201 (with-current-buffer gnus-group-buffer
202 (gnus-group-get-new-news))))
203 (set-window-configuration win))))
205 (defun gnus-demon-add-scan-timestamps ()
206 "Add daemonic updating of timestamps in empty newgroups."
207 (gnus-demon-add-handler 'gnus-demon-scan-timestamps nil 30))
209 (defun gnus-demon-scan-timestamps ()
210 "Set the timestamp on all newsgroups with no unread and no ticked articles."
212 (let ((cur-time (current-time))
213 (newsrc (cdr gnus-newsrc-alist))
214 info group unread has-ticked)
215 (while (setq info (pop newsrc))
216 (setq group (gnus-info-group info)
217 unread (gnus-group-unread group)
218 has-ticked (cdr (assq 'tick (gnus-info-marks info))))
219 (when (and (numberp unread)
222 (gnus-group-set-parameter group 'timestamp cur-time))))))
224 (provide 'gnus-demon)
226 ;;; gnus-demon.el ends here