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
  • 77,472
  • 10
  • 114
  • 243
Joon Kwon
  • 363
  • 1
  • 7

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
  • 33,167
  • 1
  • 37
  • 77
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
  • 621
  • 4
  • 5