Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Emacs Lisp: make newly-created buffer visible before the function returns?

In the following function in emacs Lisp,

(defun show-life ()
  (interactive)
  (switch-to-buffer "*Life-Window*") ; show how life goes on while living
  (live)) ; it takes 70 years to finish and return!

I'd like to create the buffer "Life-Window", and have the life event generated by (live) displayed continuously while live goes on.

Unfortunately, the buffer only becomes visible after the (live) returns (when it's over!)

I also tried pop-to-buffer, the behavior is the same.

like image 451
Yu Shen Avatar asked Jun 20 '09 12:06

Yu Shen


2 Answers

Add a call to 'sit-for just before the call to 'live, e.g.

(defun show-life ()
  (interactive)
  (switch-to-buffer "*Life-Window*")    ; show how life goes on while living
  (sit-for 0)                           ; perform redisplay
  (live))                               ; it takes 70 years to finish and return!

And, if you want to see the results of 'live, it should periodically call 'sit-for as well.

The doc string for 'sit-for is:

sit-for is a compiled Lisp function in `subr.el'. (sit-for seconds &optional nodisp)

Perform redisplay, then wait for seconds seconds or until input is available. seconds may be a floating-point value. (On operating systems that do not support waiting for fractions of a second, floating-point values are rounded down to the nearest integer.)

If optional arg nodisp is t, don't redisplay, just wait for input. Redisplay does not happen if input is available before it starts.

Value is t if waited the full time with no input arriving, and nil otherwise.

like image 175
Trey Jackson Avatar answered Sep 23 '22 23:09

Trey Jackson


I found out the solution. I have to use (sit-for <time-to-wait>) to get the buffer to show the update of life events.

So the code segment should be modified as follows:

(defun show-life ()
  (interactive)
  (switch-to-buffer "*Life-Window*") ; show how life goes on while living
  (sit-for 0)
  (live)) ; it takes 70 years to finish and return!

Maybe inside of the live body, sit-for should be called periodically.

like image 37
Yu Shen Avatar answered Sep 26 '22 23:09

Yu Shen