我正在编写一个elisp函数,它将命令发送到现有的eshell缓冲区,等待命令完成,然后发送另一个命令。例如,我想发送:
python
2+3
到目前为止,我已尝试过以下方法,但未成功:
1.获取eshell流程以使用process-send-string
和accept-process-output
:
(process-send-string eshell-proc "python")
(accept-process-output eshell-proc 100)
但是我无法获得eshell流程。当前缓冲区为eshell时,(get-buffer-process (current-buffer))
返回nil
。
2.使用(eshell-send-input)
将命令插入缓冲区,并在发送下一个命令之前休眠一下:
(progn
(insert "python")
(eshell-send-input)
(sleep-for 1)
(insert "2+3")
(eshell-send-input))
这种方法的问题是在启动python子进程之前发送“2 + 3”。无论我睡觉的时间长短,都会发生这种情况。睡觉似乎冻结了所有emacs的子进程?
3.使用eshell-gather-process-output
:
如果我使用:
(eshell-gather-process-output "/usr/bin/python" nil)
或
(eshell-gather-process-output "/usr/bin/python" (list "somearg"))
我得到Debugger entered--Lisp error: (wrong-type-argument arrayp nil)
但如果我使用:
(eshell-gather-process-output "/usr/bin/python" (vector "somearg"))
我得到Debugger entered--Lisp error: (wrong-type-argument listp ["somearg"])
所以我对这个命令所期望的参数类型感到困惑。我无法找到使用此命令的单个示例。
为什么这么简单的事情会变得如此复杂?感谢您提供任何意见
答案 0 :(得分:1)
我明白你在说什么,但这似乎并不是“Emacs”做事的方式。您可以在python-mode中打开一个缓冲区,并将区域说2 + 2发送到python解释器。或者你可以做到
(python-shell-send-string "2 + 2")
或者查看python-mode的源代码,特别是这个函数:
(defun python-shell-send-string (string &optional process msg)
"Send STRING to inferior Python PROCESS.
When MSG is non-nil messages the first line of STRING."
(interactive "sPython command: ")
(let ((process (or process (python-shell-get-or-create-process)))
(lines (split-string string "\n" t)))
(and msg (message "Sent: %s..." (nth 0 lines)))
(if (> (length lines) 1)
(let* ((temporary-file-directory
(if (file-remote-p default-directory)
(concat (file-remote-p default-directory) "/tmp")
temporary-file-directory))
(temp-file-name (make-temp-file "py"))
(file-name (or (buffer-file-name) temp-file-name)))
(with-temp-file temp-file-name
(insert string)
(delete-trailing-whitespace))
(python-shell-send-file file-name process temp-file-name))
(comint-send-string process string)
(when (or (not (string-match "\n$" string))
(string-match "\n[ \t].*\n?$" string))
(comint-send-string process "\n")))))
与您要从Emacs发送命令的进程进行交互的过程类似。如果你深入研究上面函数的代码,你会发现python-mode负责获取/启动所需的进程,在本例中是python解释器。