4

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"
  1. If I put point in else line, and type TAB, this line will be indented as it should be as the first code example.
  2. If I mark the region of demo function or the whole buffer then type TAB or C-M-\ which is indent-region, the else line will become the incorrect indention style.
  3. If I C-x C-s to save this buffer which will also execute the indent-region for the whole buffer as I said early, the else 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
  • Perhaps you can accept the answer below if it solves the problem :) – Sean Allred Apr 7 '15 at 16:30
5

This is just a bug. It's fixed in Emacs trunk. Maybe you can grab a newer version of the mode from there, or dig the bug fix out of the git history.

  • 6
    Generally for these types of answers, I like to proactive and point to the commit to fix it -- partly as 'proof' and partly as 'this is the fix'. That's just my practice, but I think it would be helpful here :) – Sean Allred Apr 7 '15 at 16:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.