Add hooks for gcc handling
[gnus] / lisp / gnus-demon.el
index 94a4952..d0baf25 100644 (file)
@@ -1,7 +1,6 @@
 ;;; gnus-demon.el --- daemonic Gnus behavior
 
-;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
-;;   2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
+;; Copyright (C) 1995-2012 Free Software Foundation, Inc.
 
 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
 ;; Keywords: news
@@ -103,6 +102,7 @@ Emacs has been idle for IDLE `gnus-demon-timestep's."
   "Run FUNC if Emacs has been idle for longer than IDLE seconds."
   (unless gnus-inhibit-demon
     (when (or (not idle)
+              (and (eq idle t) (> (gnus-demon-idle-since) 0))
               (<= idle (gnus-demon-idle-since)))
       (with-local-quit
        (ignore-errors
@@ -116,35 +116,70 @@ Emacs has been idle for IDLE `gnus-demon-timestep's."
     ;; Set up the timer.
     (let* ((func (nth 0 handler))
            (time (nth 1 handler))
+           (time-type (type-of time))
            (idle (nth 2 handler))
            ;; Compute time according with timestep.
            ;; If t, replace by 1
            (time (cond ((eq time t)
                         gnus-demon-timestep)
-                       ((null time))
-                       (t (* time gnus-demon-timestep))))
+                       ((null time)
+                       nil)
+                      ((stringp time)
+                       (* (gnus-demon-time-to-step time) gnus-demon-timestep))
+                       (t
+                       (* time gnus-demon-timestep))))
+           (idle (if (numberp idle)
+                     (* idle gnus-demon-timestep)
+                   idle))
+
            (timer
             (cond
-             ;; (func number t)
-             ;; Call when Emacs has been idle for `time'
-             ((and (numberp time) (eq idle t))
-              (run-with-timer t time 'gnus-demon-run-callback func time))
-             ;; (func number number)
-             ;; Call every `time' when Emacs has been idle for `idle'
-             ((and (numberp time) (numberp idle))
-              (run-with-timer t time 'gnus-demon-run-callback func idle))
              ;; (func nil number)
              ;; Only call when Emacs has been idle for `idle'
              ((and (null time) (numberp idle))
-              (run-with-idle-timer (* idle gnus-demon-timestep) t
-                                   'gnus-demon-run-callback func))
-             ;; (func number nil)
+              (run-with-idle-timer idle t 'gnus-demon-run-callback func))
+             ;; (func number any)
              ;; Call every `time'
-             ((and (numberp time) (null idle))
-              (run-with-timer t time 'gnus-demon-run-callback func)))))
+             ((eq time-type 'integer)
+              (run-with-timer time time 'gnus-demon-run-callback func idle))
+             ;; (func string any)
+             ((eq time-type 'string)
+              (run-with-timer time (* 24 60 60) 'gnus-demon-run-callback func idle)))))
       (when timer
         (add-to-list 'gnus-demon-timers timer)))))
 
+(defun gnus-demon-time-to-step (time)
+  "Find out how many steps to TIME, which is on the form \"17:43\"."
+  (let* ((now (current-time))
+        ;; obtain NOW as discrete components -- make a vector for speed
+        (nowParts (decode-time now))
+        ;; obtain THEN as discrete components
+        (thenParts (parse-time-string time))
+        (thenHour (elt thenParts 2))
+        (thenMin (elt thenParts 1))
+        ;; convert time as elements into number of seconds since EPOCH.
+        (then (encode-time 0
+                           thenMin
+                           thenHour
+                           ;; If THEN is earlier than NOW, make it
+                           ;; same time tomorrow.  Doc for encode-time
+                           ;; says that this is OK.
+                           (+ (elt nowParts 3)
+                              (if (or (< thenHour (elt nowParts 2))
+                                      (and (= thenHour (elt nowParts 2))
+                                           (<= thenMin (elt nowParts 1))))
+                                  1 0))
+                           (elt nowParts 4)
+                           (elt nowParts 5)
+                           (elt nowParts 6)
+                           (elt nowParts 7)
+                           (elt nowParts 8)))
+        ;; calculate number of seconds between NOW and THEN
+        (diff (+ (* 65536 (- (car then) (car now)))
+                 (- (cadr then) (cadr now)))))
+    ;; return number of timesteps in the number of seconds
+    (round (/ diff gnus-demon-timestep))))
+
 (gnus-add-shutdown 'gnus-demon-cancel 'gnus)
 
 (defun gnus-demon-cancel ()