(X)Emacs, python-mode, mmm-mode, and Spyce

Beni Cherniavsky cben at techunix.technion.ac.il
Wed Feb 5 18:52:50 CET 2003


On 2003-02-03, Thomas Köllmann wrote:

> I want to use XEmacs (or if need be, GNU Emacs) with
> python-mode to edit HTML pages including Spyce code
> (http://spyce.sourceforge.net).
>
> Spyce uses delimiters [[ and ]]; between
> [[\
>         and
> ]]
> you put regularly indented Python code.
>
> So, having files consisting of HTML and Python, I've set up both
> Emacsen like this (Spyce files have .spy names):
>
>[mmm setup omitted)
>
> This works generally, nicely colouring ("font-locking") both the HTML
> and the Python code, but there is a problem: when pressing TAB inside
> this Spyce block at the beginning of the "print i," line:
>
> [[\
> 	print 'world!',
> 	for i in range(10):
>             print i,
> ]]
>
> the line gets indented like this:
>
> [[\
> 	print 'world!',
> 	for i in range(10):
> 	print i,
> ]]
>
> which is wrong, of course. This happens at any code line ending with
> ":" as far as I can see. I have also tried two-mode-mode instead of
> mmm-mode; the result is the same.
>
I've never used mmm (I guess I should) but when editing empy code
expanding to xml I just bound two function keys to python-mode and
xml-mode and switched at will.  It's almost convenient so you could try it
if mmm fails...

> Above code in a .py file / python-mode only buffer gets indented
> correctly.
>
Just the code - yes.  But when I also put the ``[[`` (with the backslash
or without), it doesn't indent correctly for me (in plain python-mode).
Actually sometimes it even flushes some code far to the left:

[[\
    #the empty line is significant
    if x:
if y:
    foo

]]

The problem, of course, is that python-mode scans backards and sees that
it's inside a list context, where indentation is not significant.  I don't
think mmm (or similars) can work around this.  The only chance I see is to
narrow to the python part for the duration of the command.  I tried it and
it seems to work (except for the first line - reindenting it keeps
increasing the indentation unless it's in the left column.  So write a
command to narrow to the lines between the ``[[`` and `]]`.  It will serve
you for big editing.  For quick usage, rebind TAB to call the narrowing
function, indent and widen.

If this doun't work, my best idea is to prefix the ``[[`` and ``]]``
markers with ``#``, so that they don't confuse python-mode and filter them
to get a version that works with Spyce.  If you want to be fancy, you can
convince emacs to run the filtering automatically when reading/saving...

-- 
Beni Cherniavsky <cben at tx.technion.ac.il>

Do not feed the Bugzillas.




More information about the Python-list mailing list