私の目的は、Emacs のプロセスから出力を取得することです。
たとえば、Python コードを送信できるM-x run-python
Python シェルを提供してくれます。*Python*
に送信print "hello world"
すると*Python*
、実行が終了したら Emacs が結果を認識し、ミニバッファーにエコーできることを願っています。
コールバックのようなものを追加することは可能ですか?
@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)))))))