我的目标是从Emacs中的进程中获得输出。
例如,M-x run-python
给了我一个python外壳*Python*
,我可以将python代码发送到它。如果我将print "hello world"
发送到*Python*
,我希望Emacs在执行完成后可以知道结果,并在迷你缓冲区中回显它。
是否可以添加回调之类的内容?
感谢@lawlist的评论,我通过创建以下过滤函数并将其分配给具有(set-process-filter (get-buffer-process "*MozRepl*") 'moz-controller-repl-filter)
的进程(在我的情况下为*MozRepl*
)来解决问题
(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)))))))