Problems with py-indent-region in XEmacs

Thomas A. Bryan tbryan at python.net
Sat Jan 6 08:55:13 EST 2001


redhouse at my-deja.com wrote:

> py-indent-region just leaves it the way it is. When I hit tab line by
> line from the top, the right thing happens. I understand the the
> indentation has meaning, but I want to be able to paste in a def and
> then have XEMacs fix the code. What's up with this?

>From the docs, (C-c ?)

"""
Warning: The region must be consistently indented before this function
is called!  This function does not compute proper indentation from
scratch (that's impossible in Python), it merely adjusts the existing
indentation to be correct in context.
"""

That is, it's useful, when you have a block of code that you decide to 
wrap in a funciton or a portion of a function body that you decide 
should be in another function/method.  The block is properly indented, 
it just needs to be "shifted".  The entire block has to have good 
indentation before Python can make a good guess.

The tab key is simply pushing everything under the most deeply nested 
block.  If py-indent-region worked the way you wanted it to, it would 
actually be quite dangerous.  Say that I had this instead

def foo():
    a=5
b=6
c=7
if 1:
    if 0:
        print "an if with no else"
else:
    print "else of the first if"

What I want is 

def foo():
    a=5
    b=6
    c=7
    if 1:
        if 0:
            print "an if with no else"
    else:
        print "else of the first if"


What I would get from hitting tab on each line is 

def foo():
    a=5
    b=6
    c=7
    if 1:
        if 0:
            print "an if with no else"
        else:  # oops!  The second if has no else
            print "else of the first if"

This is why some people hate Python.  Usually it's not 
a problem, but if the indentation ever does get garbled, 
then there are no other syntatic indications of what 
the correct indentation is.  py-indent-region merely shifts 
a region with correct indentation based on its context. 
Tab tries to put everything in the deepest block until you 
hit backspace to indicate that the block should be done.

In your case, what you probably want to do is select just 
the region that needs to be indented and use py-shift-region-right.

---Tom



More information about the Python-list mailing list