From: Brian Kendig
Subject: How do I write a non-blocking 'sleep-for' in elisp?
Date: 
Message-ID: <bskendigDn0urz.9M8@netcom.com>
I have an artificial intelligence program that runs in Emacs Lisp (for
portability).  It is designed to interact with other characters in real
time in an on-line MUD game.

Right now I am trying to solve this problem with it: the program
instantly generates and sends out a response to anything you type to it,
which immediately reveals that it's an AI and not a human typing on the
other end.

I've implemented an 'output queue' for the program; whenever it wants to
"type" some text to the game, it queues that text onto the end of an
output list.  It will periodically go through this list in order, and
for each string in the list, it will do a (sleep-for) for a length of
time based on the length of the string to simulate a delay for typing,
and then it will print that string.  This is so the main program can
keep running and dumping output into the queue without having to
actually wait for each line of text to be printed.

Problem is, every time it does a (sleep-for), it grinds the whole rest
of the program to a halt until the (sleep-for) time has expired.
There's no need to have a delay before printing to affect the execution
of the rest of the program, and I don't want this to happen.

Is there any way to tell Emacs Lisp "do such-and-such in N seconds and
don't tie up the system in the meantime"?

-- 
_/_/_/   Be insatiably curious.                 Je ne suis fait comme aucun
/_/_/    Ask "why" a lot.                de ceux que j'ai vus; j'ose croire
_/_/                           n'etre fait comme aucun de ceux qui existent.
  /    Brian Kendig         Si je ne vaux pas mieux, au moins je suis autre.
 /    ········@netcom.com                                       -- Rousseau
     http://home.netscape.com/people/brian/

From: Paul D. Smith
Subject: Re: How do I write a non-blocking 'sleep-for' in elisp?
Date: 
Message-ID: <p5d97brzzo.fsf@lemming.wellfleet.com>
%% Regarding How do I write a non-blocking 'sleep-for' in elisp?;
%% ········@netcom.com (Brian Kendig) writes:

    bk> Right now I am trying to solve this problem with it: the program
    bk> instantly generates and sends out a response to anything you
    bk> type to it, which immediately reveals that it's an AI and not a
    bk> human typing on the other end.

Sounds cool! :)

    bk> Problem is, every time it does a (sleep-for), it grinds the
    bk> whole rest of the program to a halt until the (sleep-for) time
    bk> has expired.

    bk> Is there any way to tell Emacs Lisp "do such-and-such in N
    bk> seconds and don't tie up the system in the meantime"?

Sure; M-x apropos RET timer RET reveals (among other things):

  run-with-timer: an interactive autoloaded Lisp function in `timer'.

  Perform an action after a delay of SECS seconds.
  Repeat the action every REPEAT seconds, if REPEAT is non-nil.
  SECS and REPEAT may be integers or floating point numbers.
  The action is to call FUNCTION with arguments ARGS.

  This function returns a timer object which you can use in `cancel-timer'.

This sounds like what you want.  Check it out.
-- 
-------------------------------------------------------------------------------
 Paul D. Smith <······@baynetworks.com>         Network Management Development
 Senior Software Engineer                                   Bay Networks, Inc.
-----------------------------------------------==<http://www.baynetworks.com/>-
 "Please remain calm...I may be mad, but I am a professional." --Mad Scientist
-------------------------------------------------------------------------------
     These are my opinions--Bay Networks takes no responsibility for them.
From: Erik Naggum
Subject: Re: How do I write a non-blocking 'sleep-for' in elisp?
Date: 
Message-ID: <19960219T185035Z@arcana.naggum.no>
[Brian Kendig]

|   Is there any way to tell Emacs Lisp "do such-and-such in N seconds and
|   don't tie up the system in the meantime"?

the new built-in timers that come with 19.31 (RSN) should help.

#<Erik 3033744634253976>
-- 
the Internet made me do it
From: Jari Aalto
Subject: Re: How do I write a non-blocking 'sleep-for' in elisp?
Date: 
Message-ID: <JAALTO.96Feb20112438@tre.tele.nokia.fi>
|Mon 19.2.96, ········@netcom.com (Brian Kendig), gnu.emacs.help
| 
| Problem is, every time it does a (sleep-for), it grinds the whole rest
| of the program to a halt until the (sleep-for) time has expired.

Howabout sit-for ?

sit-for: a built-in function.

Perform redisplay, then wait for SECONDS seconds or until input is available.
					         ^^^^^^^^^^^^^^

--
/Jari, "The Man who makes no mistakes does not usually make anything."