Subject: Re: bug#15478: cc-mode does not obey electric-indent-mode

From: Stefan Monnier <[email-masked]>
Date: Wed, 02 Oct 2013 21:50:06 -0400

> Without electricity, correct indentation would require continual pressing
> of the <tab> key.

Yes.  Just as is the case in all major modes.

> "|" indicates the position of point.  Now type "{".  With electricity,
> the "{" is instantly indented to its correct position under the "if".
> Without electricity, the user needs to remember to type <tab> before C-j
> on L4.  This is an unacceptable default state, IMAO.

That's because *you* like electric-indent-mode.  Not because C is special.

>> Most major modes don't enable electric-layout by default (and AFAICT
>> most users care more about "indent after newline", which cc-mode
>> doesn't enable anyway).
> "Indent after newline" seems redundant in CC Mode;

Redundancy is not a problem, AFAIK.  In my case, for example, CC-mode's
electric indentation on {, }, and semi-colon is redundant, because I hit
TAB anyway without even thinking about it (and C-x C-s very soon after
that ;-).

> Are you saying that, in CC Mode, users would prefer electric
> indentation on the C-j rather than the semicolon, etc.?

No.  I'm saying that if they like electric indentation on {, }, and ;,
then they probably also like it on RET.  And in my experience, beginning
users ask a lot more about "how do I get Emacs to put point at the right
place after RET" than after any other key.

> Such a change could involve extensive work

Could be.  And maybe not only in CC-mode but also in electric.el.

> the electric behaviour is coded individually in defuns like
> `c-electric-brace' and includes more electric behaviour than just
> indentation - for example, auto-newlining.

`electric-layout-mode' provides similar functionality, IIUC.

> As an exercise, yes.  But disregarding existing behaviour should not be
> done frivolously; CC Mode's electric behaviour has been remarkably
> stable, with (as far as I am aware) only one complaint about it (not
> counting the current one) in at least 12 years (see below).

There's been several request to "turn off indentation" over the years
(usually answered with something like "set c-syntactic-indentation")
which would not have occurred without those electric keys: it's easy to
rebind TAB or avoid hitting TAB, but if after that "random other keys"
keep insisting on indenting for you, it gets very frustrating.

>> For me, I'd like cc-mode to do as little as possible besides adding
>> ?\;, ?\{, and ?\} to electric-indent-chars.
> These characters should not trigger electric indentation when typed
> inside a string or a comment.  electric-indent-mode isn't best placed to
> make such distinctions.

Why not?

> It doesn't seem to be the Right Thing to split the electric activity
> between electric-indent-mode (for indentation) and c-electric-brace
> and friends (for auto-newlining and clean-ups).

As explained, there's electric-layout-mode for auto-newlining.  Not sure
what "clean-ups" is about, but we can probably work something out.

> I think electric-indent-mode, as it currently is, is capable of
> improvement.  It is a single flag, but really needs to be major-mode
> dependent; it fouls up Python indentation (unless that's been recently
> fixed) and I think I recall reading that it messed up something in
> Outline Mode; yet CC Mode needs electricity.  electric-indent-mode needs
> to be buffer local.

I'm all for improving electric-indent-mode.  And indeed, it needs
improvement for indentation-sensitive modes like Python and Haskell.

> Each major mode needs its own default for e-i-m:

I disagree with it: some major modes need their own default because
their syntax has something very special, e.g. incompatible with
electric-indent-mode (Python/Coffescript/Haskell), but most modes should
just obey the default setting which reflects the user's preference.

> something like `electric-indent-mode-alist', analogous with
> `auto-mode-alist'.  This default would be consulted at mode
> initialisation time.

I don't see why the major mode can't just set a var in its major-mode
function for the rare cases where it can be needed, and why the user
can't make his own choice via the major-mode's hook, if needed.

> A buffer's setting of e-i-m should also be more than just nil or t.  That
> is inflexible to an un-Emacs like degree.  At the very least, there
> should be some sort of setting that means "electric indentation is
> performed entirely by the major mode".

I don't understand what you're suggesting.

        Stefan