Say, I got a simple Python code snippet:
def demo(i):
if(i):
print "True"
else:
print "False"
I make indent-region for the whole buffer after save using before-save-hook
in my init.el
, so after C-x C-s
in this buffer, it whole buffer will be automatically indented, this works for C and elisp source code perfectly, but in Python code, that code snippet will become:
def demo(i):
if(i):
print "True"
else:
print "False"
- If I put point in
else
line, and typeTAB
, this line will be indented as it should be as the first code example. - If I mark the region of demo function or the whole buffer then type
TAB
or C-M-\ which isindent-region
, theelse
line will become the incorrect indention style. - If I
C-x C-s
to save this buffer which will also execute theindent-region
for the whole buffer as I said early, theelse
line will become the incorrect indention style as the later code example.
According to the https://stackoverflow.com/questions/19611681/emacs-python-indentation-for-single-line-if-else which was posted a year ago, the problem is not solved yet, so I post it here again to hope someone see it and give some solution.
BTW:
- GNU Emacs 24.4.1
- built-in python.el
- Fedora 21 32-bit