Using Python inside Emacs

The Python integration is at an early stage and may crash or hang emacs when you experiment with it. However as Python is turned off by default it cannot effect the stablity of emacs until enabled.

If you wish to use python with emacs install a Python 2.2 kit. You can get a Windows python kit from www.python.org or www.activestate.com.

Use the Python interface

Before you can use the Python interface it is neccessary to initiailise Python inside bemacs. Use the Python-console command to initiailise Python.

If you do not have Python 2.2 installed Python-console will report an error.

stdin, stdout and stderr in Python are connect to the Python Console buffer. This means that the output of the Python print command will appear in the console window.

Python integration commands

Command              Description
Python-consoleenable Python integration. You can execute commands in the Python Console buffer by typing ENTER.
Python-execexecute the string parameter in Python.
Python-evalevaluate the string paramter in Python and return the result to emacs.
Python-importimport the named module into Python

Accsss BEmacs from Python

From Python to access emacs use the bemacs module.

import bemacs

The bemacs module implements the following interface:

bemacs interfaceDescription
bemacs.variable.mlisp_var_name get or set variables in emacs. To access mlisp variables with hypens in their names use underscore instead. E.g. to access journal-frequency use bemacs.variable.journal_frequency
bemacs.call_function( mlisp-func-name, arg, arg )call the emacs function name mlisp-func-name.
bemacs.function.mlisp_func_name( arg, arg ) call emacs function. To access mlisp functions with hypens in their names use underscore instead. E.g. to access forward-character use bemacs.function.forward_character()
bemacs.buffer is a python dictionary like object of all the Emacs buffers. The indices of the dictionary can be either an integer index into the of buffers or a string with the name of a buffer. The values of the dictionary are "BEmacs buffer"objects.
"BEmacs buffer" interfaceDescription
dataThe data of the buffer. Acts like a string. Use slice syntax to access a range of characters.
For example:
  • bemacs.buffers['Help'].data[0:100]
    gets the first 100 characters of the Help buffer.
  • bemacs.buffers['Help'].data[0:0] = 'A new heading'
    inserts text into the buffer
syntaxThe syntax infomation of the buffer. Acts like a list. Use slice syntax to access a range of syntax information.
For example:
  • bemacs.buffers['Help'].syntax[10:20]
    get the synatx information for 10 characters in the Help buffer.
extentReturns a tuple of (first-buffer-possion,last-buffer-position) that the user wishs editing to effect.
file_nameIf buffer is a file buffer: the file name this buffer was read from and will be saved to.
file_permissionsIf buffer is a file buffer: the files permission bits
file_timeIf buffer is a file buffer: the files time stamp
file_synchonise_permissionsIf buffer is a file buffer: the files permission bits when last synchonised
file_synchonise_timeIf buffer is a file buffer: the files time stamp when last synchonised
typethe buffer type.