(message-reply): Take an optional switch-buffer parameter so that Gnus window confs...
[gnus] / lisp / gnus-demon.el
1 ;;; gnus-demon.el --- daemonic Gnus behavior
2
3 ;; Copyright (C) 1995-2011 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   "List 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 (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))
87
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)))
93
94 (defun gnus-demon-idle-since ()
95   "Return the number of seconds since when Emacs is idle."
96   (if (featurep 'xemacs)
97       (itimer-time-difference (current-time) last-command-event-time)
98     (float-time (or (current-idle-time)
99                     '(0 0 0)))))
100
101 (defun gnus-demon-run-callback (func &optional idle)
102   "Run FUNC if Emacs has been idle for longer than IDLE seconds."
103   (unless gnus-inhibit-demon
104     (when (or (not idle)
105               (<= idle (gnus-demon-idle-since)))
106       (with-local-quit
107        (ignore-errors
108          (funcall func))))))
109
110 (defun gnus-demon-init ()
111   "Initialize the Gnus daemon."
112   (interactive)
113   (gnus-demon-cancel)
114   (dolist (handler gnus-demon-handlers)
115     ;; Set up the timer.
116     (let* ((func (nth 0 handler))
117            (time (nth 1 handler))
118            (idle (nth 2 handler))
119            ;; Compute time according with timestep.
120            ;; If t, replace by 1
121            (time (cond ((eq time t)
122                         gnus-demon-timestep)
123                        ((null time) nil)
124                        (t (* time gnus-demon-timestep))))
125            (timer
126             (cond
127              ;; (func number t)
128              ;; Call when Emacs has been idle for `time'
129              ((and (numberp time) (eq idle t))
130               (run-with-timer time time 'gnus-demon-run-callback func time))
131              ;; (func number number)
132              ;; Call every `time' when Emacs has been idle for `idle'
133              ((and (numberp time) (numberp idle))
134               (run-with-timer time time 'gnus-demon-run-callback func idle))
135              ;; (func nil number)
136              ;; Only call when Emacs has been idle for `idle'
137              ((and (null time) (numberp idle))
138               (run-with-idle-timer (* idle gnus-demon-timestep) t
139                                    'gnus-demon-run-callback func))
140              ;; (func number nil)
141              ;; Call every `time'
142              ((and (numberp time) (null idle))
143               (run-with-timer t time 'gnus-demon-run-callback func)))))
144       (when timer
145         (add-to-list 'gnus-demon-timers timer)))))
146
147 (gnus-add-shutdown 'gnus-demon-cancel 'gnus)
148
149 (defun gnus-demon-cancel ()
150   "Cancel any Gnus daemons."
151   (interactive)
152   (dolist (timer gnus-demon-timers)
153     (nnheader-cancel-timer timer))
154   (setq gnus-demon-timers nil))
155
156 (defun gnus-demon-add-disconnection ()
157   "Add daemonic server disconnection to Gnus."
158   (gnus-demon-add-handler 'gnus-demon-close-connections nil 30))
159
160 (defun gnus-demon-close-connections ()
161   (save-window-excursion
162     (gnus-close-backends)))
163
164 (defun gnus-demon-add-nntp-close-connection ()
165   "Add daemonic nntp server disconnection to Gnus.
166 If no commands have gone out via nntp during the last five
167 minutes, the connection is closed."
168   (gnus-demon-add-handler 'gnus-demon-nntp-close-connection 5 nil))
169
170 (defun gnus-demon-nntp-close-connection ()
171   (save-window-excursion
172     (when (time-less-p '(0 300) (time-since nntp-last-command-time))
173       (nntp-close-server))))
174
175 (defun gnus-demon-add-scanmail ()
176   "Add daemonic scanning of mail from the mail backends."
177   (gnus-demon-add-handler 'gnus-demon-scan-mail 120 60))
178
179 (defun gnus-demon-scan-mail ()
180   (save-window-excursion
181     (let ((servers gnus-opened-servers)
182           server
183           (nnmail-fetched-sources (list t)))
184       (while (setq server (car (pop servers)))
185         (and (gnus-check-backend-function 'request-scan (car server))
186              (or (gnus-server-opened server)
187                  (gnus-open-server server))
188              (gnus-request-scan nil server))))))
189
190 (defun gnus-demon-add-rescan ()
191   "Add daemonic scanning of new articles from all backends."
192   (gnus-demon-add-handler 'gnus-demon-scan-news 120 60))
193
194 (defun gnus-demon-scan-news ()
195   (let ((win (current-window-configuration)))
196     (unwind-protect
197         (save-window-excursion
198           (when (gnus-alive-p)
199             (with-current-buffer gnus-group-buffer
200               (gnus-group-get-new-news))))
201       (set-window-configuration win))))
202
203 (defun gnus-demon-add-scan-timestamps ()
204   "Add daemonic updating of timestamps in empty newgroups."
205   (gnus-demon-add-handler 'gnus-demon-scan-timestamps nil 30))
206
207 (defun gnus-demon-scan-timestamps ()
208   "Set the timestamp on all newsgroups with no unread and no ticked articles."
209   (when (gnus-alive-p)
210     (let ((cur-time (current-time))
211           (newsrc (cdr gnus-newsrc-alist))
212           info group unread has-ticked)
213       (while (setq info (pop newsrc))
214         (setq group (gnus-info-group info)
215               unread (gnus-group-unread group)
216               has-ticked (cdr (assq 'tick (gnus-info-marks info))))
217         (when (and (numberp unread)
218                    (= unread 0)
219                    (not has-ticked))
220           (gnus-group-set-parameter group 'timestamp cur-time))))))
221
222 (provide 'gnus-demon)
223
224 ;;; gnus-demon.el ends here