Pymacs -- usage example

For a recurring task I wanted to read a key in the minibuffer of Emacs and insert data from a xml file using xpath.

The simplified version of my solution using pymacs:

from Pymacs import lisp
from lxml import etree

interactions = {}

def insert():

  doc = etree.parse(filename)
  templates = doc.xpath('//tours/templates/template/@name')

  # list of templates to insert with numbers from 0 to n
  tlist = ["[%d] %s" % (i, templates[i]) for i in range(len(templates))]

  # show the list of templates to choose
  lisp.message("Which template (%s)?" % tlist)
  # read character
  key = lisp.read_char()
  # check if key is a number
  if key >= ord('0') and key <= ord('%d' % len(templates)):
    tempnum = key - ord('0')

    # save current curser position
    start = lisp.point()
    # insert string
    lisp.insert("%s\n" %
                etree.tostring(doc.xpath("//tours/templates/template[@name='%s']" %
                                         templates[tempnum])[0], pretty_print=True,
    # goto saved position

interactions[insert] = ''
# set f6 for this function (mypymacsmodule is the name of this .py file)
lisp.global_set_key((lisp.f6,), lisp.mypymacsmodule_insert)

The version I use has to modify the data from the xml file using a jinja based template.