2013-04-10 56 views
2

我正在寫一個elisp函數,它將命令發送到現有的eshell緩衝區,等待命令完成併發送另一個命令。例如,我想送:從eslip發送命令到eshell

python 
2+3 

到目前爲止,我曾嘗試以下,不成功:

1.Getting的ESHELL過程中使用process-send-stringaccept-process-output

(process-send-string eshell-proc "python") 
(accept-process-output eshell-proc 100) 

但我一直無法獲得eshell進程。 (get-buffer-process (current-buffer))當前緩衝區爲eshell時返回nil

在緩衝器2.Inserting命令,使用(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"])

所以我真的很困惑這種命令期望什麼類型的參數。我一直無法找到使用此命令的單個示例。

爲什麼這麼簡單變得如此複雜?感謝您的任何輸入

+0

1 - eshell不啓動外部進程,所以這不起作用 - 沒有進程發送到。 2適合我。 – Tyler 2013-04-10 21:56:58

+0

2是一個非常脆弱的解決方案。取決於系統的負載,啓動子進程所需的時間會有所不同。在這個解決方案下,我僅限於硬編碼我將遇到的預期最大加載時間,並且每次都等待這段時間。這是我想要替代品的原因。 – erjoalgo 2013-05-07 05:18:36

回答

1

我明白你在說什麼,但這似乎並不是真正的「Emacs」做事方式。你可以在python模式下打開一個緩衝區,並向python解釋器發送2 + 2區域。或者你也可以做

(python-shell-send-string "2 + 2") 

或者看看在源蟒蛇模式,尤其是這個函數:

(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模式負責獲取/啓動所需的進程,在本例中爲python解釋器。