Subject: bug#17658: Python mode has odd indentation behavior

From: Stefan Monnier <[email-masked]>
Date: Mon, 02 Jun 2014 09:36:56 -0400

>> Makes no difference,
> Did you try it?

I tried it in Emacs-24.3 which doesn't enable electric-indent-mode by
default, but suffers from the same problem.  Admittedly, In 24.4,
I half-fixed the problem by linking this python-mode feature to
electric-indent-mode, so that it can be turned off.

> The chars, which trigger an indent, may differ. In this case a comma
> triggers it.

Any char triggers it.  As I said:

   The important detail is not to hit "," but to insert a char right
   before the inner close paren.

The comma is not in electric-indent-chars.

Here's the offending code in python-indent-post-self-insert-function:

     ((and (not (bolp))
           (memq (char-after) '(?\) ?\] ?\})))
      (save-excursion
        (goto-char (line-beginning-position))
        ;; If after going to the beginning of line the point
        ;; is still inside a paren it's ok to do the trick
        (when (python-syntax-context 'paren)
          (let ((indentation (python-indent-calculate-indentation)))
            (when (< (current-indentation) indentation)
              (indent-line-to indentation))))))

This means that current line is reindented whenever you insert a char
right before a close-paren and line-beginning-position is inside some
parentheses.  Since reindenting a line is problematic in Python, it
should only be done when we really know for sure that the current indent
is wrong.  So clearly the above condition triggers too often.
But I don't understand the intention of the code, i.e. I don't
understand when the above is meant to trigger reindentation, so I don't
know how to fix it.  Presumably Fabián should be able to shed some
light here.

        Stefan