Emacs and Indentation

Michael Tiller mtiller at ford.com
Wed Nov 6 10:37:15 EST 2002


I'm relatively new to Python.  There are lots of things I like about Python
and I expect to be doing quite a bit of development in the future with it.
The one thing I don't quite understand is how people cope with the fact that
Emacs (at least, my Emacs) doesn't do a very good job of indenting code.  I
have no trouble reading the "unique" indentation scheme of Python, but I
find things get difficult when I'm rearranging code.

Sure, I can type stuff like this:

    if not x in filled:
        foo[x] = x[1]
        bar[x] = x[2]
    y = x

...and Emacs handles this just as I would expect.  BUT, what happens when I
try to put this code into a for loop?  After I add my for loop, I have:

    for x in mylist:
    if not x in filled:
        foo[x] = x[1]
        bar[x] = x[2]
    y = x

In other languages (Tcl, Java, C++) where the block delimiters are explicit,
I can just highlight the region and issue an "indent-region" and it would
automatically indent everything appropriately.  As far as I can tell, this
does nothing in Emacs (v21.4, windows).  So, I have to go and indent using
the TAB key.  The problem there is that Emacs doesn't really understand the
block structure so I get:

    for x in mylist:
        if not x in filled:
            foo[x] = x[1]
            bar[x] = x[2]
            y = x # Yikes!

What I find with Python is that I spend far more time than with other
languages fiddling with the indentation  (and debugging errors caused by
faulty indentation) simply because Emacs cannot automatically indent code
fragments.  Obviously, Emacs cannot automatically determine the indentations
but I'm guessing their are "tricks" that either allow me to hint at what I
intended or some special commands in Python mode that apply some heuristics.

  Surely I can't be the only person who is bugged by this?  Are there ways
around this?

--
Mike






More information about the Python-list mailing list