4

Is there any way to write something like this without taking over emacs?

(defun dumb-wait (seconds)
    (let ((done (+ (second (current-time)) seconds)))
        (while (< (second (current-time)) done)
            (message "waiting"))))

(dump-wait 5) will block emacs from 5 seconds. Is there anyway to write this so it doesn't block? I just want to be in a loop and check some condition from time to time, and still be able to use emacs.

Thanks!

3
  • sit-for? You can also yield to redisplay.
    – jrockway
    Sep 25, 2009 at 4:05
  • this dumb-wait function can be used for a prank on a fellow emacser. M-: (dumb-wait 3600). But then there's not many emacsers who have screensavers with no password.
    – Yoo
    Sep 26, 2009 at 19:38
  • At the place where I work you will get into all sorts of trouble if you leave your computer unlocked and leave your desk. A hanging emacs will the be least of your concerns :)
    – killdash10
    Sep 27, 2009 at 17:03

2 Answers 2

6

(run-at-time time repeat function &rest args) should do it. nil as time means now.

(setq my-timer 
      (run-at-time nil 5 (lambda () (message "waiting")))) ; returns timer object
;; or
(setq my-timer
      (run-at-time nil 5 'message "waiting"))

(cancel-timer my-timer) ; use timer object from above

Edit:

The parameter repeat expects a number as seconds, however there's a function timer-duration, which you can use instead of the number. It returns the number of seconds as provided with a string parameter. This is somewhat easier to read for big intervals.

(timer-duration "3 hours 2 seconds 1 millisec") ; => 10802.001

Possible word you can use are defined in the variable timer-duration-words.

2

On a related note, there's no general way to write Emacs Lisp code that doesn't block because the language doesn't have features like coroutines, continuations and threading (yet). Instead, you've got to look for asynchronous inbuilts that do something closest to what you want. Example: url-retrieve and async-shell-command. Some applications like SLIME have managed to work around this issue and have implemented a sort of threading on their own, while others like gnus are waiting for Emacs Lisp to improve.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.