2010-12-16 32 views
5

我使用emacs23和tramp修改遠程主機上的python腳本。 我發現當我啓動emacs中的python shell時,它會在遠程主機上啓動 python。評估遠程主機上的emacs python-mode的緩衝區

我的問題是,當我再嘗試調用Python-發送緩衝區通過CC CC它與錯誤出現現在

Traceback (most recent call last): File "", line 1, in ? ImportError: No module named emacs

Traceback (most recent call last): File "", line 1, in ? NameError: name 'emacs' is not defined

,我必須承認,我真的不知道是怎麼回事這裏。有沒有辦法讓我配置emacs,以便我可以評估遠程主機上的緩衝區?

非常感謝。

編輯:我遵循eichin的建議並重新實現了python-send-region。請參閱下面的答案。

回答

2

簡短的回答:不寫一些遺漏的elisp代碼。

龍版本:python.elrun-python增加data-directory(這在我的Ubuntu 10.10盒/usr/share/emacs/23.1/etc/)到$PYTHONPATH,特別是使之能找到emacs.py(由當地Emacs發行提供)。然後它做了(python-send-string "import emacs"),預計它的工作...

它看起來像defadvice包裝是tramp使用實際上不通過PYTHONPATH,所以如果你有遠程系統上的匹配emacs的版本甚至不工作。

如果M-x customize-variable RET tramp-remote-process-environment RET 然後打INS按鈕中的一個,並添加PYTHONPATH=/usr/share/emacs/23.1/etc然後打STATE並將其設置爲「當前會話」(只是爲了測試它,或「保存爲將來的會話」,如果你的作品),它幾乎工程 - 投訴消失,無論如何,因爲遠程python現在可以找到遠程emacs.py。如果現在回到原始問題,執行python-send-buffer,則只會遇到另一個錯誤:No such file or directory: '/tmp/py24574XdA',因爲python-mode只是將內容填充到臨時文件中,並告知python子進程加載該內容。

你不得不改變python-send-region(其他函數調用它),特別是它採用make-temp-file是流浪漢感知的方式 - 甚至還有一個tramp-make-tramp-temp-file你很可能建立在。 (一定要發佈它,如果你這樣做...)

+0

非常感謝。我已經發布了下面的python-send-region的實現,它似乎工作。 – Adrian 2011-01-04 20:19:35

+0

如果這個問題在過去的4年中得到解決,有沒有這樣的話?如果更新的答案是「它現在正常工作」,那將會很棒。 – 2015-02-04 23:43:39

3

我目前正試圖讓我的未註冊問題與此帳戶合併,之後,我將能夠接受eichin的答案並編輯我的帖子,包括我的解決方案

我遵循eichin的建議,將emacs2.py emacs3.py和emacs.py文件複製到遠程主機,並將其目錄添加到tramp-remote-process-environment變量中的PYTHONPATH中。

然後我在我的.emacs

(require 'python) 

(defun python-send-region (start end) 
    "Send the region to the inferior Python process." 

    (interactive "r") 

    (let* ((loc_name) 
    (f (if (file-remote-p default-directory) 
     (let* ((con (tramp-dissect-file-name default-directory))) 
      (setq loc_name (tramp-make-tramp-temp-file con)) 
      (concat "/" 
       (tramp-file-name-method con) ":" 
       (tramp-file-name-user con) "@" 
       (tramp-file-name-host con) ":" 
       loc_name 
      )) 
      (setq loc_name (make-temp-file "py")))) 
    (command (format "emacs.eexecfile(%S)" loc_name)) 
    (orig-start (copy-marker start))) 
    (save-excursion 
     (let ((curbuf (current-buffer)) 
     (tempbuf (get-buffer-create "*python_temp*"))) 
    (set-buffer tempbuf) 
    (delete-region (point-min) (point-max)) 
    (insert-buffer-substring curbuf start end) 
    (python-mode) 
    (when (save-excursion 
     (goto-char (point-min)) 
     (/= 0 (current-indentation))) 
     (python-shift-left (point-min) (point-max))) 
    (write-region nil nil f nil 'nomsg)) 

    (python-send-command command) 
    (with-current-buffer (process-buffer (python-proc)) 
     ;; Tell compile.el to redirect error locations in file `f' to 
     ;; positions past marker `orig-start'. It has to be done *after* 
     ;; `python-send-command''s call to `compilation-forget-errors'. 
     (compilation-fake-loc orig-start f))) 
)) 

重新實現了Python-發送緩衝功能我基本上是區域複製到一個新的緩衝區,調整壓痕,然後將其寫入到一個臨時文件,以創建流浪漢-make-tramp-temp-file或make-temp-file,具體取決於訪問文件是遠程還是本地。

我遇到了一些問題,流浪漢手柄寫入區域,這似乎並沒有接受一個字符串作爲第一個參數,這就是爲什麼我做了所有的格式在一個單獨的緩衝器。

讓我知道是否還有與代碼的任何問題,但是這是我的elisp編碼的第一次嘗試,所以請溫柔。

+0

我試過上面的並得到以下錯誤:「ad-Orig-error:Invalid file-name」。任何指針? – 2012-03-06 01:18:54