MichaelB
MichaelB

Reputation: 1132

emacs elisp switch to buffer and follow

I am trying to write a command that will cause Emacs to switch to a new buffer and do something (in this case, execute a shell command) that writes output to the buffer.

(defun test-func ()
  (interactive)
  (let ((bname "*temp*")
        (default-directory "/home/me"))
    (with-output-to-temp-buffer bname
      (switch-to-buffer bname)
      (shell-command "ls -l" bname))))

In this case, it "works" except that it doesn't switch the buffer until after the command is done executing. I wanted to switch immediately and then follow the output as it's running. Is there a way to do that?

Upvotes: 2

Views: 539

Answers (1)

sds
sds

Reputation: 60014

You need to call redisplay explicitly after switch-to-buffer to make it visible.

Note that ls is a fairly "fast" command, and it is unlikely to show piecemeal. You might want to try a shell script like

while true; do
  date
  sleep 10
done

and run is asynchronously (either use async-shell-command or add & to the end of the command line).

Note also that the help for shell-command says:

In Elisp, you will often be better served by calling call-process or start-process directly, since it offers more control and does not impose the use of a shell (with its need to quote arguments).

Upvotes: 1

Related Questions