4

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 ?

Drew
  • 75,699
  • 9
  • 109
  • 225
Joon Kwon
  • 363
  • 1
  • 6
  • Try to put `(sit-for 0)` behind `(insert "Hello world!")`. – Tobias Aug 14 '18 at 11:39
  • 1
    That didn't work for me, but (redisplay t) did – Joon Kwon Aug 14 '18 at 13:44
  • 3
    Possible duplicate of [If I insert text before playing a sound, why do I not see the text inserted until the sound finishes?](https://emacs.stackexchange.com/questions/17773/if-i-insert-text-before-playing-a-sound-why-do-i-not-see-the-text-inserted-unti) – zck Aug 14 '18 at 21:17

2 Answers2

4

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

Tobias
  • 32,569
  • 1
  • 34
  • 75
1

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

Jeeves
  • 611
  • 4
  • 5