[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Calling a function every random number of minutes, where random amo
From: |
vk0vk0vk0 |
Subject: |
Re: Calling a function every random number of minutes, where random amount changes |
Date: |
Thu, 29 Nov 2007 11:37:58 -0800 (PST) |
User-agent: |
G2/1.0 |
first of all. it's a wonderfully weird feature you want here.
Here are the functions i came up with.
(defun time-after-seconds (seconds)
"returns an internal emacs time that is 'seconds' seconds into the
future."
(seconds-to-time (+ seconds (time-to-seconds (current-time)))))
(defun timer-create-callfunc-after-seconds (func seconds)
"run 'func' after 'seconds' seconds."
(let ((timer (timer-create)))
(timer-set-function timer func)
(timer-set-time timer (time-after-seconds seconds))
(timer-activate timer)))
(defun callfunc-random-interval (func interval-from-seconds
interval-to-seconds)
"run 'func' with a random interval between 'interval-from-seconds'
and
'interval-to-seconds' seconds in a infinite loop."
(funcall func)
(timer-create-callfunc-after-seconds
(list 'lambda '()
(list 'funcall ''callfunc-random-interval
(list 'quote func) interval-from-seconds
interval-to-seconds))
(+ interval-from-seconds (random interval-to-seconds))))
;; (callfunc-random-interval (lambda () (insert " foo")) 2 3)
and how to make it work like you want it to:
(callfunc-random-interval 'color-theme-random (* 60 5) (* 60 60))
Hope this helps you in your insane experiment.
metaperl.com wrote:
> The code below randomly selects one of my favorite color-themes. I
> would like to set something up so that (color-theme-random) is called
> after n minutes have elapsed. Where n is non-constant and randomly
> chosen between 5 and 60.
>
> In other words, after 7 minutes it calll the function. Then after 30
> minutes it calls it again. Then 12 minutes... and so on and so on.
>
> (require 'color-theme)
>
> (random t)
>
> (defvar *color-theme-favorites*
> '(
>
> arjen andreas billw blippblopp blue-mood blue-sea calm-forest
> classic comidia dark-blue dark-blue2 deep-blue
> euphoria feng-shui fischmeister goldenrod gray1 gray30
> high-contrast hober infodoc jb-simple jonadabian
> jsc-dark jsc-light
> kingsajz
> late-night lawrence lethe
> marine marquardt midnight mistyday montz oswald parus
> robin-hood simple-1 subtle-blue tty-dark
> word-perfect
> xp
>
> ))
>
> (defun color-theme-random()
> (Interactive)
>
>
>
> (let* ((choice (nth (random (length *color-theme-favorites*))
> *color-theme-favorites*))
> (fn (intern (format "color-theme-%s" choice))))
> (funcall fn)))
>
> (color-theme-random)
>
> (provide 'color-theme-random)
> ;;;