English | 简体中文
Python Bridge is an application programming framework built on top of emacs-epc, which can be used to build multi-threaded Emacs plugins through Python.
It has been successfully applied to plugins such as EAF, lsp-bridge, blink-search, mind-wave, popweb, wraplish, key-echo and so on.
- Install Python dependencies: epc, sexpdata, six:
pip3 install epc sexpdata six
- Download this repository with
git clone
, and replace the load-path path in the configuration below - Add the following code to your configuration file ~/.emacs:
(add-to-list 'load-path "<path-to-python-bridge>")
(require 'python-bridge)
(python-bridge-enable)
Python side reads the value of Emacs variables:
from utils import *
print(get_emacs_var("emacs-version"))
print(get_emacs_vars(["emacs-version", "emacs-build-system"]))
Python side gets the return value of Emacs functions:
from utils import *
print(get_emacs_func_result("get-user-emacs-directory"))
If the Emacs function has extra parameters, you can directly write them in the subsequent parameters of get_emacs_func_result
Python side displays messages to Emacs Minibuffer:
from utils import *
message_emacs("hello from python-bridge")
Python side calls Emacs methods:
from utils import *
eval_in_emacs("message", "hello from python-bridge")
Python multithreading:
Please Google Python threading
to learn how to use Python to write multithreaded code.
So far, you have learned how to use python-bridge to handle the mutual calling of Elisp and Python. Next, you can use python-bridge to build your own plugins.
For example, if I want to build a plugin called python-hello
, here are my common methods:
- Search for all
python-bridge
keywords in the python-bridge directory and replace them withpython-hello
- Search for all
python_bridge
keywords in the python-bridge directory and replace them withpython_hello
- Search for all
PythonBridge
keywords in the python-bridge directory and replace them withPythonHello
- Search for all
PYTHONBRIDGE-EPC-SERVER
keywords in the python-bridge directory, replace withPYTHONHELLO-EPC-SERVER
- Change the file name
python-bridge-epc.el
topython-hello-epc.el
- Change the file name
python-bridge.el
topython-hello.el
- Change the file name
python_bridge.py
topython_hello.py
- Finally,
(require 'python-hello)
is enough
The reason for renaming is to hope that plugins built based on python-bridge
can have their own namespace and do not interfere with each other.
Happy hacking! ;)