我的目标是从Emacs中获取进程的输出。
例如,M-x run-python
给了我一个python shell *Python*
,我可以发送python代码。如果我将print "hello world"
发送给*Python*
,我希望Emacs可以在执行完成后知道结果并在迷你缓冲区中回显它。
是否可以添加类似回调的内容?
答案 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)))))))