将 emacs 缓冲区发送到任意 Python 进程

发布于 2024-10-14 06:41:40 字数 904 浏览 3 评论 0原文

我喜欢 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 to sys.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

別甾虛僞 2024-10-21 06:41:40

shell-command 没有给你你想要的东西吗?您可以编写包装器脚本或适当调整 #!sys.path

Doesn't shell-command give you what you are looking for? You could write a wrapper script or adjust the #! and sys.path appropriately.

時窥 2024-10-21 06:41:40

comint 为此类内容提供了大部分基础设施。有很多很好的例子,比如 thisthis

它允许您运行命令,提供 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 implement send-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 the emacs-remote-repl.el after fixing the stupid hardcoded path to client.py

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文