Emacs - 对 shell-command-on-region 的非交互式调用总是删除区域?

标签 emacs elisp

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)),但也适用于以区域作为输入运行命令,然后在不删除区域的情况下插入结果的情况。

最佳答案

你错了

在 emacs lisp 代码中使用像 shell-command-on-region 这样的交互式命令不是一个好主意。使用 call-process-region相反。

Emacs 错误

shell-command-on-region 中存在错误:它不会将 replace 参数传递给 call-process-region;这是修复:

=== modified file 'lisp/simple.el'
--- lisp/simple.el  2013-05-16 03:41:52 +0000
+++ lisp/simple.el  2013-05-23 18:44:16 +0000
@@ -2923,7 +2923,7 @@ interactively, this is t."
      (goto-char start)
      (and replace (push-mark (point) 'nomsg))
      (setq exit-status
-       (call-process-region start end shell-file-name t
+       (call-process-region start end shell-file-name replace
                     (if error-file
                     (list t error-file)
                       t)

我会尽快提交。

关于Emacs - 对 shell-command-on-region 的非交互式调用总是删除区域?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16720458/

相关文章:

java - 为什么 try-with-resources 会破坏 Emacs 中的缩进?”

emacs - 配置 emacs 以显示固定宽度的内嵌图像

python - 在 emacs python 模式中不正确地退出缩进

emacs - 如何自动(重新)编译 ELPA 包?

可选部分的 Emacs 语法高亮

emacs - 为什么我不能更改 paredit 键绑定(bind)

emacs - 如何在 Emacs 23 中获得底部而不是右侧的编译缓冲区?

macos - 在Mac上升级控制台emacs(/usr/bin/emacs)

emacs - 提供和要求 Emacs Lisp 包子功能(如何)

function - ELISP:提示用户输入数字并询问用户该数量的字符串并将其插入列表的功能