Emacs-对区域上的shell命令的非交互调用总是删除区域吗?

dod*_*ler 2 emacs elisp

该功能的“ Emacs帮助”页面显示shell-command-on-region(省略空格):

(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.
Run Code Online (Sandbox Code Playgroud)

这不是最清楚的,但是刚才引用的最后几句话似乎是说,如果我希望将shell命令的输出插入到当前缓冲区中,此时缓冲区的其他内容保持不变,则应该传递一个非nil对参数OUTPUT-BUFFERnil进行REPLACE

但是,如果我在*scratch*缓冲区中执行此代码(不是我正在处理的实际代码,而是演示该问题的最小情况):

(shell-command-on-region
 (point-min) (point-max) "wc" t nil)
Run Code Online (Sandbox Code Playgroud)

缓冲区的全部内容将被删除,并替换为wc!的输出。

shell-command-on-region使用时,以非交互,破还是我误解的文件?如果是后者,如何更改上面的代码以插入wcat 的输出而不是替换缓冲区的内容?理想情况下,我希望有一个通用解决方案,该解决方案不仅可以像最小示例(即(point-min)通过(point-max))那样在整个缓冲区上运行命令,而且还可以运行以区域为输入的命令,然后插入结果的情况在不删除区域的情况下。

sds*_*sds 5

你错了

shell-command-on-region在emacs lisp代码中使用交互式命令不是一个好主意。使用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)
Run Code Online (Sandbox Code Playgroud)

我会尽快提交。