我正在编写一个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))这种方法的问题是,"2+3“在Python子进程启动之前发送。不管我的睡眠时间有多长,这种情况都会发生。睡眠似乎冻结了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"])
所以我真的搞不懂这个命令需要什么类型的参数。我还找不到一个使用此命令的示例。
为什么这么简单的事情会变得这么复杂?感谢您的任何意见
发布于 2013-11-15 10:47:59
我知道你在说什么,但这似乎不是真正的"Emacs“做事方式。您只需在python模式下打开一个缓冲区,然后将区域(例如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解释器。
https://stackoverflow.com/questions/15933756
复制相似问题