evaluating buffer in emacs python-mode on remote host
I'm using emacs23 with tramp to modify python scripts on a remote host. I found that when I start the python shell within emacs it starts up python on the remote host.
My problem is that when I then try to call python-send-buffer via C-c C-c it comes up with the error
Traceback (most recent call last): File "", line 1,开发者_JS百科 in ? ImportError: No module named emacs
Traceback (most recent call last): File "", line 1, in ? NameError: name 'emacs' is not defined
Now, I must admit that I don't really know what's going on here. Is there a way for me to configure emacs so that I can evaluate the buffer on the remote host?
Many thanks.
Edit: I've followed eichin's advice and re-implemented python-send-region. See my answer below.
I'm currently trying to to get my unregistered question merged with this account, after which I'll be able to accept eichin's answer and edit my post to include my solution.
I followed eichin's suggestion and copied the emacs2.py emacs3.py and emacs.py files to the remote host and added their directory to PYTHONPATH in the tramp-remote-process-environment variable.
I then reimplemented the python-send-buffer function in my .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)))
))
I essentially copy the region into a new buffer, adjust the indentation and then write it into a temporary file, created with tramp-make-tramp-temp-file or make-temp-file, depending on whether the visited file is remote or local.
I had some problems with tramp-handle-write-region, which didn't seem to accept a string as a first argument, which is why I did all the formatting in a separate buffer first.
Let me know if there are still any problems with the code, but this is my first attempt at elisp coding, so please be gentle.
Short answer: not without writing some missing elisp code.
Long version: In python.el
, run-python
adds data-directory
(which on my Ubuntu 10.10 box is /usr/share/emacs/23.1/etc/
) to $PYTHONPATH
, specifically so that it can find emacs.py
(as supplied by the local emacs distribution.) Then it does a (python-send-string "import emacs")
and expects it to work...
It looks like the defadvice
wrappers that tramp
uses don't actually pass PYTHONPATH
, so this doesn't work even if you have the matching emacs version on the remote system.
If you M-x customize-variable RET tramp-remote-process-environment RET
then hit one of the INS
buttons and add PYTHONPATH=/usr/share/emacs/23.1/etc
then hit STATE
and set it to "current session" (just to test it, or "save for future sessions" if it works for you) it almost works - the complaint goes away, in any case, because the remote python can now find the remote emacs.py
. If you now go back to the original question, doing python-send-buffer
, you just run into a different error: No such file or directory: '/tmp/py24574XdA'
because python-mode
just stuffs the content into a temporary file and tells the python subprocess to load that.
You'd have to change python-send-region
(the other functions call it) and particularly the way it uses make-temp-file
to be tramp-aware - there's even a tramp-make-tramp-temp-file
you could probably build upon. (Be sure to post it if you do...)
精彩评论