如何将一个外部进程的输出通过管道传输到另一个进程?

How to pipe output from one external process into another?

我编写了一个函数,它获取所选区域的内容,然后通过两个外部进程运行它。实际上我想要复制的行为是 M-| smartypants -2 | ascii2uni -a D -q.

以下函数有效,但需要两次调用 call-process-region 并将第一个进程的输出临时存储在缓冲区中。有更好的方法吗?

(defun convert-ascii-to-unicode (&optional b e)
  "Converts ascii punctuation marks (quotes, dashes, and ellipses) into their unicode equivilents."
  (interactive "r")
  (let ((output-buffer (generate-new-buffer "*ASCII to Unicode Output*")))
    (call-process-region b e "smartypants" nil output-buffer nil "-2")
    (set-buffer output-buffer)
    (call-process-region (point-min) (point-max) "ascii2uni" t output-buffer nil "-a" "D" "-q")
    (switch-to-buffer-other-window output-buffer)))

只需在 shell 命令中使用管道,下面应该做你想做的:

(defun convert-ascii-to-unicode (beg end)
  (interactive "r")
  (shell-command
   (format "smartypants -2 %s | ascii2uni -a D -q"
           (shell-quote-argument (buffer-substring beg end)))))

切换到 *Shell Command Output* 以查看输出。