|
1 | 1 | # python-emacs |
| 2 | +[](https://travis-ci.org/jlumpe/python-emacs) |
| 3 | + |
2 | 4 | Python interface to GNU Emacs. |
| 5 | + |
| 6 | + |
| 7 | +## Installation |
| 8 | + |
| 9 | +Install using pip: |
| 10 | + |
| 11 | + pip install python-emacs |
| 12 | + |
| 13 | +Or directly from the repository: |
| 14 | + |
| 15 | + git clone https://github.com/jlumpe/python-emacs |
| 16 | + cd python-emacs |
| 17 | + python setup.py install |
| 18 | + |
| 19 | + |
| 20 | +## Usage |
| 21 | + |
| 22 | +Create an interface to Emacs using either `Emacs.batch()` or `Emacs.client()`. The first runs a new Emacs process in batch mode with every command, the second uses `emacsclient` to communicate with an already-running process. |
| 23 | + |
| 24 | +```python-console |
| 25 | +>>> from emacs import Emacs |
| 26 | +>>> emacs = Emacs.batch(['-q']) |
| 27 | +``` |
| 28 | + |
| 29 | +Execute some Elisp code and get the output: |
| 30 | + |
| 31 | +```python-console |
| 32 | +>>> src = '(princ (format "One plus two is %d" (+ 1 2)))' |
| 33 | +>>> emacs.eval(src) |
| 34 | +'One plus two is 3' |
| 35 | +``` |
| 36 | + |
| 37 | +Get the result of an expression as a Python value: |
| 38 | + |
| 39 | +```python-console |
| 40 | +>>> emacs.getresult('(format "One plus two is %d" (+ 1 2))') |
| 41 | +'One plus two is 3' |
| 42 | + |
| 43 | +>>> emacs.getresult('(cl-loop for i in \'(1 2 3 4 5) collect (* i i))') |
| 44 | +[1, 4, 9, 16, 25] |
| 45 | +``` |
| 46 | + |
| 47 | + |
| 48 | +## Write Elisp programs in Python |
| 49 | + |
| 50 | +```python-console |
| 51 | +>>> import emacs.elisp as el |
| 52 | + |
| 53 | +>>> src = el.to_elisp((el.Symbol('format'), 'One plus two is %d', (el.Symbol('+'), 1, 2))) |
| 54 | +>>> src |
| 55 | +<el (format "One plus two is %d" (+ 1 2))> |
| 56 | + |
| 57 | +>>> emacs.getresult(src) |
| 58 | +'One plus two is 3' |
| 59 | +``` |
| 60 | + |
| 61 | +Using a terrible DSL: |
| 62 | + |
| 63 | +```python-console |
| 64 | +>>> from emacs.elisp import E |
| 65 | + |
| 66 | +>>> prog = E.dolist((E.i, E.number_sequence(1, 20)), |
| 67 | + E.princ(E.i), |
| 68 | + E.when(E['='](E['%'](E.i, 3), 0), E.princ("fizz")), |
| 69 | + E.when(E['='](E['%'](E.i, 5), 0), E.princ("buzz")), |
| 70 | + E.princ('\n'), |
| 71 | + ) |
| 72 | +>>> prog |
| 73 | +<el (dolist (i (number-sequence 1 20)) (princ i) (when (= (% i 3) 0) (princ "fizz")) (when (= (% i 5) 0) (princ "buzz")) (princ "\n"))> |
| 74 | + |
| 75 | +>>> print(emacs.eval(prog)) |
| 76 | +1 |
| 77 | +2 |
| 78 | +3fizz |
| 79 | +4 |
| 80 | +5buzz |
| 81 | +6fizz |
| 82 | +7 |
| 83 | +8 |
| 84 | +9fizz |
| 85 | +10buzz |
| 86 | +11 |
| 87 | +12fizz |
| 88 | +13 |
| 89 | +14 |
| 90 | +15fizzbuzz |
| 91 | +16 |
| 92 | +17 |
| 93 | +18fizz |
| 94 | +19 |
| 95 | +20buzz |
| 96 | +``` |
0 commit comments