pymacs! :-)

François Pinard pinard at iro.umontreal.ca
Sat Sep 8 08:50:56 EDT 2001


> [Paul Winkler]

> > For example, let's say I have a a module, let's call it manglers.py,
> > containing this simple python function:

> > def break_on_whitespace(some_string):
> > 	"""Yes, it's trivial."""
> > 	words = some_string.split()
> > 	return '\n'.join(words)

> > Now I want to tell Emacs about this function so that I can call it on
> > a region of text and replace the region with the result of the
> > call.  And bind this action to a key, of course.

Hello, Paul.  I would like to add some further comments on your problem.
Yesterday, I wrote:

> q = lisp.quote
> lisp.global_set_key([lisp.f7], q(lisp.manglers_break_on_whitespace))

I might have been tired, or sleeping.  That's useless complexity.
Just write:

lisp.global_set_key([lisp.f7], lisp.manglers_break_on_whitespace)

Since arguments have been evaluated the Python way on the Python side,
it would be conceptual overkill evaluating them again the LISP way on the
LISP side, so Pymacs already quotes arguments to defeat LISP evaluation.
The same logic applies, the other way around.

> There is one thing that I know is currently missing, and that would
> prevent to above to work today.  At `python-load' time, the function will
> not have been marked "(interactive)", and I guess this is required for
> the function to be usefully bound to a key.  I hope I'll find a nice way
> for this, with the enlightening suggestions of this forum, of course! :-)

Thinking a bit more about this, this is a difficult problem, as Emacs
functions have the concept of user interaction for completing the
specification of their arguments while being called.  It would be very
unnatural to Python, trying to retrofit that facility on the Python side.
Best might be to write another trampoline, like this:


     (pymacs-load "manglers")

     (defun break-on-whitespace ()
       (interactive)
       (manglers-break-on-whitespace))

     (global-set-key [f7] 'break-on-whitespace)


This is a bit more LISP to write, but I hope not too frightening...

-- 
François Pinard   http://www.iro.umontreal.ca/~pinard




More information about the Python-list mailing list