Emacs:从流程接收输出的传统方式是什么?

时间:2014-09-23 01:37:57

标签: emacs elisp mozrepl

我的目标是从Emacs中获取进程的输出。

例如,M-x run-python给了我一个python shell *Python*,我可以发送python代码。如果我将print "hello world"发送给*Python*,我希望Emacs可以在执行完成后知道结果并在迷你缓冲区中回显它。

是否可以添加类似回调的内容?

1 个答案:

答案 0 :(得分:1)

感谢@lawlist的评论,我通过创建以下过滤器功能并使用*MozRepl*

将其分配给流程(在我的情况下为(set-process-filter (get-buffer-process "*MozRepl*") 'moz-controller-repl-filter))解决了我的问题
(defun moz-controller-repl-filter (proc string)
  "Filter function of *MozRepl*.

It gets the useful output of *MozRepl*, store it in `moz-controller-repl-output` and `kill-ring`"
  (when (buffer-live-p (process-buffer proc))
    (unless (string= string "repl> ")   ; ignore empty output (page up, page down, etc)
      (setq moz-controller-repl-output
            (replace-regexp-in-string "\"\\(.+\\)\"\nrepl> " "\\1" string))
      (kill-new moz-controller-repl-output) ; append to kill-ring
      (message moz-controller-repl-output) ; show the copied content in echo area
      )
    (with-current-buffer (process-buffer proc)
      (let ((moving (= (point) (process-mark proc))))
        (save-excursion
          ;; Insert the text, advancing the process marker.
          (goto-char (process-mark proc))
          (insert string)
          (set-marker (process-mark proc) (point)))
        (if moving (goto-char (process-mark proc)))))))