emacs elisp 切换到缓冲并跟随

emacs elisp switch to buffer and follow

我正在尝试编写一个命令,使 Emacs 切换到一个新的缓冲区并执行一些操作(在本例中,执行 shell 命令)将输出写入缓冲区。

(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))))

在这种情况下,它 "works" 除了它在命令执行完成之前不会切换缓冲区。我想立即切换,然后按照 运行 的输出进行操作。有办法吗?

您需要在 switch-to-buffer 之后显式调用 redisplay 才能使其可见。

请注意,ls是一个相当"fast"的命令,不太可能显示零散的。您可能想尝试 shell 脚本,例如

while true; do
  date
  sleep 10
done

和运行是异步的(使用async-shell-command或在命令行末尾添加&)。

还要注意 shell-command 的帮助说:

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).