我的目标是从 Emacs 中的进程中获取输出。
例如,给了我一个可以发送 python 代码M-x run-python
的 python shell 。*Python*
如果我发送print "hello world"
到*Python*
,我希望 Emacs 能够在执行完成后知道结果并在 mini-buffer 中回显它。
是否可以添加类似回调的东西?
感谢@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)))))))