【发布时间】:2013-05-23 17:40:11
【问题描述】:
函数 shell-command-on-region 的 Emacs 帮助页面显示(空格省略):
(shell-command-on-region START END COMMAND &optional OUTPUT-BUFFER
REPLACE ERROR-BUFFER DISPLAY-ERROR-BUFFER)
...
The noninteractive arguments are START, END, COMMAND,
OUTPUT-BUFFER, REPLACE, ERROR-BUFFER, and DISPLAY-ERROR-BUFFER.
...
If the optional fourth argument OUTPUT-BUFFER is non-nil,
that says to put the output in some other buffer.
If OUTPUT-BUFFER is a buffer or buffer name, put the output there.
If OUTPUT-BUFFER is not a buffer and not nil,
insert output in the current buffer.
In either case, the output is inserted after point (leaving mark after it).
If REPLACE, the optional fifth argument, is non-nil, that means insert
the output in place of text from START to END, putting point and mark
around it.
这不是最清楚的,但刚刚引用的最后几句话似乎是说,如果我希望将 shell 命令的输出插入到当前缓冲区中,而缓冲区的其他内容保持不变,我应该为OUTPUT-BUFFER 和nil 为REPLACE 传递一个非nil 参数。
但是,如果我在 *scratch* 缓冲区中执行此代码(不是我正在处理的真实代码,而是演示问题的最小案例):
(shell-command-on-region
(point-min) (point-max) "wc" t nil)
缓冲区的全部内容被删除并替换为wc的输出!
shell-command-on-region 在非交互式使用时是否损坏,还是我误读了文档?如果是后者,我如何更改上面的代码以插入 wc 的输出而不是替换缓冲区的内容?理想情况下,我想要一个通用的解决方案,它不仅可以像最小示例那样在整个缓冲区上运行命令(即(point-min) 到(point-max)),而且还可以用于运行带有区域的命令的情况输入,然后将结果插入点而不删除区域。
【问题讨论】: