I notice that Emacs waits for the end of a function to actually display the buffer modifications the function has produced. For instance, if a function looks like this:

(defun foo ()
  (interactive)
  (insert "Hello world!")
  (function-with-rather-long-execution-time))

Then, Emacs waits until the end of the execution of the whole function before displaying "Hello world!", whereas I would like the string to appear right away, before the execution of function-with-rather-long-execution-time. Is there a solution ?

Thanks!!

up vote 4 down vote accepted

You can use (sit-for 0) or (redisplay t).

The help for redisplay:

Perform redisplay. Optional arg FORCE, if non-nil, prevents redisplay from being preempted by arriving input, even if ‘redisplay-dont-pause’ is nil. If ‘redisplay-dont-pause’ is non-nil (the default), redisplay is never preempted by arriving input, so FORCE does nothing.

Return t if redisplay was performed, nil if redisplay was preempted immediately by pending input.

Example:

(defun foo ()
  (interactive)
  (insert "Hello world!")
  (redisplay t)
  ;; Stuff with rather long execution time:
  (let ((end-time (time-add (current-time) '(0 10 0 0))))
    (while (time-less-p (current-time) end-time))))

Both methods, (redisplay t) and (sit-for 0) were tested with:

GNU Emacs 26.1 (build 1, x86_64-unknown-cygwin, GTK+ Version 3.22.28) of 2018-05-28

  • Thanks, (redisplay t) worked fine! For some reason (sit-for 0) didn't work for me. – Joon Kwon Aug 14 at 13:51

The long running function will have to do something : like https://www.gnu.org/software/emacs/manual/html_node/elisp/Forcing-Redisplay.html#Forcing-Redisplay

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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