Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

python-mode's broken indentation behavior

21 views
Skip to first unread message

Florian Weimer

unread,
Jun 28, 2015, 3:47:05 PM6/28/15
to
Indentation in python-mode has been fairly broken for me, I believe
since Emacs 24. The electric colon makes this especially annoying.

In the following, _ marks the position of point.

If I press RET here:

def foo(x):
if x:
return 2
else:
raise Foo_

I get this, which is correct.

def foo(x):
if x:
return 2
else:
raise Foo
_

Continuing typing:

def foo(x):
if x:
return 2
else:
raise Foo
for i in range(0, 10)_

So far, so good. Now press ‘:’:

def foo(x):
if x:
return 2
else:
raise Foo
for i in range(0, 10):_

Going from:

def foo(x):
if x:
x()
else:
y()
for i in range(0, 10)_

to:

def foo(x):
if x:
x()
else:
y()
for i in range(0, 10):_

is equally unhelpful.

For me, this makes editing Python scripts with Emacs a fairly painful
experience.

Surely this is a known issue because it has been around for so long.
Is this the behavior the python-mode maintainers prefer? I find this
hard to believe.

Ian Zimmerman

unread,
Jun 28, 2015, 9:23:38 PM6/28/15
to help-gn...@gnu.org
While I completely agree that this behavior is broken in both cases (and
I feel smug using emacs23, which doesn't do this), I thought it's worth
pointing out that these cases are quite different. In the second case,
it is correct for the code to not unindent automatically, but it should
not restore the indent after you manually fix it.

Maybe you can just turn off the electric colon somehow? If nothing
else, you should be able to do

(add-hook 'python-mode-hook
(lambda ()
(local-unset-key ":")))

--
Please *no* private copies of mailing list or newsgroup messages.
Rule 420: All persons more than eight miles high to leave the court.


Stefan Monnier

unread,
Jun 28, 2015, 9:31:19 PM6/28/15
to help-gn...@gnu.org
> Indentation in python-mode has been fairly broken for me, I believe
> since Emacs 24.

These are usually bugs, so the better way to deal with them is to
report them. Which version of Emacs are you using?
I don't see the behavior you describe (at least in Emacs-25),

Stefan


Jude DaShiell

unread,
Jun 28, 2015, 10:55:38 PM6/28/15
to Florian Weimer, help-gn...@gnu.org
I've used python-mode and what I had to learn and I don't know if this
is nailing you was once an indent was arranged, when a new line is typed
only hit tab once. The python-mode for me then indented all code
correctly. If I examined the spacing of the indent and tried hitting
tab more than once (second guessing python_mode) that's when I got
myself into trouble. If this is nailing you, I hope this helps.

On Sun, 28 Jun 2015, Florian Weimer wrote:

> Date: Sun, 28 Jun 2015 15:47:03
> From: Florian Weimer <f...@deneb.enyo.de>
> To: help-gn...@gnu.org
> Newsgroups: gnu.emacs.help
> Subject: python-mode's broken indentation behavior
>
> Indentation in python-mode has been fairly broken for me, I believe
> since Emacs 24. The electric colon makes this especially annoying.
>
> In the following, _ marks the position of point.
>
> If I press RET here:
>
> def foo(x):
> if x:
> return 2
> else:
> raise Foo_
>
> I get this, which is correct.
>
> def foo(x):
> if x:
> return 2
> else:
> raise Foo
> _
>
> Continuing typing:
>
> def foo(x):
> if x:
> return 2
> else:
> raise Foo
> for i in range(0, 10)_
>
> So far, so good. Now press ?:?:
--


John Mastro

unread,
Jun 29, 2015, 3:29:31 PM6/29/15
to Florian Weimer, help-gn...@gnu.org
Florian Weimer <f...@deneb.enyo.de> wrote:
> Indentation in python-mode has been fairly broken for me, I believe
> since Emacs 24. The electric colon makes this especially annoying.

I find the indentation engine to be quite good overall, but use
something like the following to prevent the colon issue:

(defun no-electric-colon ()
(setq-local electric-indent-chars (remq ?: electric-indent-chars)))

(with-eval-after-load 'python
(add-hook 'python-mode-hook #'no-electric-colon))

Hope that helps

--
john

Florian Weimer

unread,
Jun 29, 2015, 3:33:34 PM6/29/15
to
* Ian Zimmerman:
I wanted to show that incorrect re-indent happens in both cases, both
for the unreachable case and the reachable case. I have no problem
with automatic deindenting after a statement which causes an abnormal
exit of the block.

Florian Weimer

unread,
Jun 29, 2015, 3:42:07 PM6/29/15
to
* Stefan Monnier:

>> Indentation in python-mode has been fairly broken for me, I believe
>> since Emacs 24.
>
> These are usually bugs, so the better way to deal with them is to
> report them.

The hideous indentation provided by sml-mode wasn't considered a bug.
:-)

> Which version of Emacs are you using?

Debian's Emacs 24.4.1. I've also seen it on Fedora with 24.5.1.

> I don't see the behavior you describe (at least in Emacs-25),

Hmm. If it's already fixed, then maybe it was commit
749813e9d4a844384e0450f6f7f88484b15e348a. Initial testing confirms
this.

Stefan Monnier

unread,
Jun 30, 2015, 9:40:41 AM6/30/15
to
>> These are usually bugs, so the better way to deal with them is to
>> report them.
> The hideous indentation provided by sml-mode wasn't considered a bug.
> :-)

Sometimes it's indeed a feature, but features can be configured ;-)


Stefan
0 new messages