简体   繁体   English

在 Emacs 上,获取通过 Emacs-Lisp 发送到缓冲区的命令的 output

[英]On Emacs, getting the output of a command sent to a buffer via Emacs-Lisp

I would like to write a small script in ELisp that would:我想在 ELisp 中编写一个小脚本:

  1. send a command to a given buffer向给定缓冲区发送命令
  2. get its output得到它的 output
  3. parse it解析它
  4. send it to another buffer将其发送到另一个缓冲区

I am struggling with point 2: I cant get the output of a command.我在第 2 点上苦苦挣扎:我无法获得命令的 output。 For example, if I have a shell buffer on, I can use例如,如果我有一个 shell 缓冲区,我可以使用

(process-send-string "shell" "help\n")

to send "help" to my shell buffer.向我的 shell 缓冲区发送“帮助”。 It will then show the list of the commands available.然后它将显示可用命令的列表。 But how can I get this list to use it somewhere else?但是我怎样才能让这个列表在其他地方使用呢?

Thanks,谢谢,

S4m S4m

(buffer-string) returns the contents of the current buffer, so (with-current-buffer <buf> (buffer-string)) will return the contents of <buf> . (buffer-string)返回当前缓冲区的内容,因此(with-current-buffer <buf> (buffer-string))将返回<buf>的内容。

I don't know the exact emacs commands for this off the top of my head, but one option would be to do the following:我不知道确切的 emacs 命令,但一种选择是执行以下操作:

  • Set the mark in the shell buffer right below the command line在命令行正下方的 shell 缓冲区中设置标记
  • Execute the command.执行命令。
  • Move the point to the end of the file and kill the text between there and the mark.将点移动到文件的末尾并删除那里和标记之间的文本。
  • Move to the destination buffer and yank the text into there.移动到目标缓冲区并将文本拉到那里。

Have you considered using the shell-command or shell-command-to-string functions?您是否考虑过使用shell-commandshell-command-to-string函数?

The don't "send a command to a buffer" like you asked, but they do both allow running a command through a process that will be started just for that purpose and either dumping the output into a target buffer or collecting it into a string.不要像您问的那样“将命令发送到缓冲区”,但它们都允许通过将为此目的启动的进程运行命令,并将 output 转储到目标缓冲区或将其收集到字符串中.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM