将 emacs 缓冲区发送到任意 Python 进程
我喜欢 python-send-buffer 命令,但是我经常在应用程序中使用嵌入的 Python,或者通过自定义包管理系统启动 Python(以启动具有某些依赖项的 Python)。换句话说,我不能只运行“python”并获得一个有用的Python实例(python-send-buffer
依赖的东西)
我想要实现的是:
- 在任何Python解释器(或允许的应用程序中)来评估 Python 代码),导入
magic_emacs_python_server.py
模块(根据需要附加到sys.path
) - 在 emacs 中,运行
magic-emacs-python-send -buffer
这将评估远程 Python 实例中的缓冲区。
看起来应该很简单——Python 模块在线程中监听套接字。它在主线程中进行计算,并返回结果的 repr()
(或者可能捕获 stdout/stderr,或者可能两者都捕获)。 emacs 模块只会将文本发送到套接字,等待响应字符串,并将其显示在缓冲区中。
听起来很简单,这样的东西肯定已经存在了……IPython 有 ipy_vimserver,但这是错误的方法。还有 swank,虽然它看起来非常特定于 Lisp,但有一个 Javascript 后端 看起来很像我想要的...但是搜索几乎什么也没找到,除了一些模糊的(可能是真的)声称 SLIME 不能很好地与非 Lisp 语言一起工作
简而言之:
- 是否存在一个可以发送的项目代码从 emacs 缓冲区到现有的 Python 进程?
- 如果没有,你会如何建议我写这样的东西(对elisp不太熟悉)-SWANK? IPython的服务器代码?从头开始简单的 TCP 服务器?
I like the python-send-buffer
command, however I very often use Python embedded in applications, or launch Python via a custom package management system (to launch Python with certain dependencies).. In other words, I can't just run "python" and get a useful Python instance (something that python-send-buffer
relies on)
What I would like to achieve is:
- in any Python interpreter (or application that allows you to evaluate Python code), import a
magic_emacs_python_server.py
module (appending tosys.path
as necessary) - In emacs, run
magic-emacs-python-send-buffer
This would evaluate the buffer in the remote Python instance.
Seems like it should be pretty simple - the Python module listens on a socket, in a thread. It evaluates in the main thread, and returns the repr()
of the result (or maybe captures the stdout/stderr, or maybe both). The emacs module would just send text to the socket, waits for a string in response, and displays it in a buffer.
Sounds so simple something like this must exist already... IPython has ipy_vimserver
, but this is the wrong way around. There is also swank
, while it seems very Lisp-specific, there is a Javascript backend which looks very like what I want... but searching finds almost nothing, other than some vague (possibly true) claims that SLIME doesn't work nicely with non-Lisp languages
In short:
- Does a project exist to send code from an emacs buffer to an existing Python process?
- If not, how would you recommend I write such a thing (not being very familiar with elisp) - SWANK? IPython's server code? Simple TCP server from scratch?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
shell-command
没有给你你想要的东西吗?您可以编写包装器脚本或适当调整#!
和sys.path
。Doesn't
shell-command
give you what you are looking for? You could write a wrapper script or adjust the#!
andsys.path
appropriately.comint 为此类内容提供了大部分基础设施。有很多很好的例子,比如 this 或 this
它允许您运行命令,提供
comint-send-string
轻松实现send-region
类型命令。Github 上的 dbr/remoterepl 是我在问题。
它缺乏任何形式的修饰,但它基本上可以工作 - 您在目标解释器中导入
replify.py
模块,然后在修复后评估emacs-remote-repl.el
client.py
的愚蠢硬编码路径comint provides most of the infrastructure for stuff like this. There's a bunch of good examples, like this or this
It allows you to run a command, provides things
comint-send-string
to easily implementsend-region
type commands.dbr/remoterepl on Github is a crude proof-of-concept of what I described in the question.
It lacks any kind of polish, but it mostly works - you import the
replify.py
module in the target interpreter, then evaluate theemacs-remote-repl.el
after fixing the stupid hardcoded path toclient.py