I am running Emacs with python-mode.el for coding in Python. I hope to learn how to make a region of code indented well automatically.
Following code is not indented well.
while match != None:
if match.group(1):
titles.append(match.group(1))
if match.group(2):
if match.group(2) != '':
pns.append(int(match.group(2)))
else:
pns.append('')
else:
pns.append('')
if match.group(3):
closings.append(len(''.join(match.group(3).split())))
else:
closings.append(0)
match = pat.search(match.group(4))
If I select the region, and hit M-x indent-region
, it becomes totally wrong:
while match != None:
if match.group(1):
titles.append(match.group(1))
if match.group(2):
if match.group(2) != '':
pns.append(int(match.group(2)))
else:
pns.append('')
else:
pns.append('')
if match.group(3):
closings.append(len(''.join(match.group(3).split())))
else:
closings.append(0)
match = pat.search(match.group(4))
The ideal should be:
while match != None:
if match.group(1):
titles.append(match.group(1))
if match.group(2):
if match.group(2) != '':
pns.append(int(match.group(2)))
else:
pns.append('')
else:
pns.append('')
if match.group(3):
closings.append(len(''.join(match.group(3).split())))
else:
closings.append(0)
match = pat.search(match.group(4))
- Why does
M-x indent-region
incorrectly understand the indent relation between lines of code? Is it because my code is ambiguous? - What should I do then?
Thanks.