在 Emacs Elisp 函数中,如何打开一个新的缓冲区窗口,向其打印字符串,然后用键盘按“q”关闭它?

zze*_*man 3 emacs elisp function

我对 elisp 比较陌生,我不知道如何表达我的问题以在 Google 上找到答案

我想定义一个函数:

  • 打开一个新的缓冲区
  • 将字符串打印到该缓冲区中
  • 当用户聚焦并按“q”时关闭缓冲区和窗口

我尝试过的是

(defun test ()
    (switch-to-buffer-other-window "*test*")
    (erase-buffer)
    (insert "hello from *test*")
    (kill-this-buffer))

(test)
Run Code Online (Sandbox Code Playgroud)

但这并没有按照我想要的方式工作。

为了清楚起见,下面是我希望该函数执行的操作的图像分解:

初始设置

在此输入图像描述

函数test被执行

在此输入图像描述

焦点保留在初始缓冲区上,然后指定的缓冲区*test*获得焦点并q按下键盘

在此输入图像描述

窗口配置现在没有*test*,焦点返回到初始缓冲区

我计划使用此函数将我的个人键绑定打印到缓冲区中*test*,这样我就不必打开我的.emacs来查看它们

Dre*_*rew 5

您可能正在寻找宏with-help-window

(defun test ()
  (interactive)
  (with-help-window "*test*"
    (princ "hello from test")))
Run Code Online (Sandbox Code Playgroud)

如果您希望能够使用insert而不是princ,那么您可以使用:

(defun test ()
  (interactive)
  (with-help-window "*test*"
    (with-current-buffer "*test*"
      (princ "hello from test"))))
Run Code Online (Sandbox Code Playgroud)

(如果您使用的旧版 Emacs 没有,with-help-window那么您可以使用with-output-to-temp-buffer它。)

(这interactive只是为了让您可以轻松测试它。)