4

The situation.

Say I have opened up a file in emacs, and I type in something like:

x \and \top

Then, a Python program reads that file, and:

  • replaces all \and to
  • replaces all \top to T
  • it does some thinking on the expression x \and \top and decides that that is the same as x, so will append an = x

The final result of all the changes made by the program to the file will leave it like so:

x ∧ T = x

Question: how should I pass messages between emacs and a Python program?

I don't want the Python program to be constantly polling the file for changes (using a while loop), and I probably don't want emacs to be constantly polling the file for updates (which I know how to do using the auto-revert command).

Maybe I press some key combination, and then that sends a message to a Python script that its time to read the file and make updates and then the Python script would message emacs and ask it to update what it is displaying in its buffer.

How could this be done?

My goal: I want to take advantage of the fact that emacs knows how to intelligently display pixels on a screen in an interactive way (it can manage dividing up the screen space, display text and allow editing, syntax highlighting, etc.). I want to take advantage of my knowledge of Python to do heavy programming (rather than using emacs Lisp), and emacs would be the UI for this program. I view communication between emacs, and the Python program as crucial to achieving this goal. If there is a better way to think about this though, I'd love to learn (for instance, maybe I really should be involving emacs Lisp a lot more, and eliminating Python entirely?).

  • The original description sounds like you just want to have one of the countless prettification modes enabled. And no, usually the level of interaction between Emacs is limited to calling processes. – wasamasa Aug 26 '16 at 7:04
  • @wasamasa but you can send input to and get output from processes. Wouldn't an asynchronous process be relevant here? (not sure, so I'm asking :-) – JeanPierre Aug 26 '16 at 9:54
  • I'm not sure how that is a contradiction to what I wrote. Sure, feel free to create an asynchronous process and use it accordingly. – wasamasa Aug 26 '16 at 17:11
2

you could check out https://github.com/tkf/python-epc

or if you like org-mode: https://github.com/gregsexton/ob-ipython

(actually the regular python support in org-mode might be sufficient. You can run sessions so you can have separate "blocks" of code).

  • 1
    I think python-epc is what I am looking for, so I have accepted this answer. – user89 Aug 26 '16 at 18:21
2

I developed a Python <-> Emacs messaging prototype using an HTTP server running on each, but I don't have the code anymore. Elnode is an asynchronous background Server for Emacs and Emacs-Request allows you to post HTTP requests to the Python server. Python of course has Simple Server and Requests, as well as more robust solutions.

It works, and it's reliable (provided you handle dropped requests), but it's a limited solution. Because of how Emacs deals with asynchronous processes, the latency is very high - in the region of one second per request. If that isn't a problem, this is the most versatile solution and it is easy enough to implement.

A more robust solution is probably Pymacs, but I haven't looked into it myself. It's an interface between Python and Emacs-Lisp. Ropemacs is built on it, and I believe Elpy also uses it. The documentation is no longer available from their website, but it is available on the Wayback Machine. You may also find useful code snippets by looking at the source code for Elpy, Ropemacs, Emacs-Jedi, Sphinx-doc and other Python-interfacing packages.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.