Skip to main content

Python interface to GNU Emacs.

Project description

python-emacs

CI Documentation Status

This package provides an interface between Python and GNU Emacs. It allows you to easily pass data from Python to Emacs, execute Emacs Lisp code, and transfer the resulting data back to Python again. It also provides utilities for building Emacs Lisp expressions in Python.

Installation

Install using pip:

pip install python-emacs

Or directly from the repository:

git clone https://github.com/jlumpe/python-emacs
cd python-emacs
pip install .

Usage

Create an interface to Emacs using either EmacsBatch or EmacsClient. The first runs a new Emacs process in batch mode with every command, the second uses emacsclient to communicate with a running server. Both follow the same API.

>>> from emacs import EmacsBatch
>>> emacs = EmacsBatch(args=['-Q'])  # Don't load user config with each invocation

Execute an Elisp expression and get the result:

>>> emacs.eval('(+ 1 2)')
3

>>> emacs.eval('(format "One plus two is %d" (+ 1 2))')
'One plus two is 3'

>>> src = '''
... (progn
...   (require 'cl)
...   (cl-loop
...     for i in '(1 2 3 4 5)
...     collect (* i i)))
... '''

>>> emacs.eval(src)
[1, 4, 9, 16, 25]

Evaluation errors are caught in Emacs and raised in Python:

>>> emacs.eval('(+ 1 "foo")')
ElispException: Wrong type argument: number-or-marker-p, "foo"

Write Elisp programs in Python

>>> import emacs.elisp as el

>>> src = el.to_elisp((el.Symbol('format'), 'One plus two is %d', (el.Symbol('+'), 1, 2)))
>>> src
<el (format "One plus two is %d" (+ 1 2))>

>>> emacs.eval(src)
'One plus two is 3'

Using a terrible DSL:

>>> from emacs.elisp import E

>>> prog = E.with_output_to_string(
      E.dolist(
        (E.i, E.number_sequence(1, 20)),
        E.princ(E.i),
        E.when(E['='](E['%'](E.i, 3), 0), E.princ("fizz")),
        E.when(E['='](E['%'](E.i, 5), 0), E.princ("buzz")),
        E.princ('\n')))

>>> prog
<el (with-output-to-string (dolist (i (number-sequence 1 20)) (princ i) (when (= (% i 3) 0) (princ "fizz")) (when (= (% i 5) 0) (princ "buzz")) (princ "\n")))>

>>> print(emacs.eval(prog))
1
2
3fizz
4
5buzz
6fizz
7
8
9fizz
10buzz
11
12fizz
13
14
15fizzbuzz
16
17
18fizz
19
20buzz

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

python-emacs-0.2.1.tar.gz (13.3 kB view details)

Uploaded Source

Built Distribution

python_emacs-0.2.1-py3-none-any.whl (12.6 kB view details)

Uploaded Python 3

File details

Details for the file python-emacs-0.2.1.tar.gz.

File metadata

  • Download URL: python-emacs-0.2.1.tar.gz
  • Upload date:
  • Size: 13.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.10

File hashes

Hashes for python-emacs-0.2.1.tar.gz
Algorithm Hash digest
SHA256 37f868e665fdfdb4fc37ca6c3463a742fa1486fe561d568db30257b1ef38bbef
MD5 5f93d0d18b307210f9173682225e7185
BLAKE2b-256 2583311d4de47d60b167d32edd3139899327afb585eae62a9a966d5e1b399f5c

See more details on using hashes here.

File details

Details for the file python_emacs-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: python_emacs-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 12.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.10

File hashes

Hashes for python_emacs-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 0d60cd3b26e458f458ffde9d2e1bbe6d17b7a1002416f4cdab7946bad5fb7580
MD5 1a4c2920c6a5f362255ef2bfb2eb0698
BLAKE2b-256 50671e1d928a0185f2430287146002abd821b96ae0f81d9324151f29e30d98d4

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page