Subject: bug#74339: 30.0.92; CC Mode stomps C TS Mode

From: Alan Mackenzie <[email-masked]>
Date: Fri, 15 Nov 2024 19:46:03 +0000

Hello, Stefan.

On Fri, Nov 15, 2024 at 14:01:26 -0500, Stefan Monnier wrote:
> > One can write, in the Local Variables: section

> >     mode: c-ts

> FWIW, unless this file is personal, this is very bad practice, and not
> only because `c-ts-mode` may not be available (in older Emacsen or if
> the user didn't build Emacs with tree-sitter support or doesn't have the
> appropriate grammar installed): the choice between `c-mode` and
> `c-ts-mode` is a personal preference, so it's not the file's business to
> tell the user which mode to use, just like it's not the file's business
> to tell the user which editor to use.

> There might be special situations where such a setting might be
> warranted, but they're sufficiently hypothetical or rare that they
> should not drive our decisions, as long as the users can still get the
> behavior they want in such cases (which they can, regardless of what we
> do with `major-mode-remap-defaults`).

Here is a section of a test file sent to bug-cc-mode some while ago:

/*
** Local Variables:
** mode:c
** indent-tabs-mode:nil
** c-basic-offset:4
** End:
*/

Here it is evident that by "mode:c" the OP means C Mode, not whatever
random mode happens to have taken the symbol `c-mode'.

> > ..  This will cause the buffer to start in c-ts-mode regardless of any
> > other current settings.

> Actually, no: `major-mode-remap-*` can also remap `c-ts-mode` to some
> other mode, such as `c-mode`.

Maybe I should have said "regardless of any non-crazy settings".

> > ..  When that line is followed by setting other CC Mode variables (as is
> > surely common for any such use of the major mode setting) that will
> > signal some sort of error on opening the buffer, should c-mode have been
> > "remapped" to c-ts-mode.  Or if it doesn't do that, those local variables
> > will be disregarded.  This is a Bad Thing.

> If you really insist, we can add

>     (add-to-list 'major-mode-remap-defaults '(c-ts-mode . c-mode))
>     (add-to-list 'major-mode-remap-defaults '(c++-ts-mode . c++-mode))

> to your `cc-mode.el` together with corresponding removals in
> `c-ts-mode.el`, even though in my book file-local settings of
> `mode: c-ts-mode` in non-personal files are just bugs.

The problem is that entries in major-mode-remap-defaults (and the other
one) do not accurately represent user preferences.

As a value for mode: in C files, we need three distinct settings: for C
Mode, for c-ts-mode, and for any mode which handles C.  Currently we've
only got two: c-mode and c-ts-mode.  This can't work properly.

> > ... there will be no way for a user to specify c-mode unambiguously ...

> Where/when?  AFAICT the only case where this might be true are the cases
> where it does not do any damage.

In an auto-mode-alist entry, for example.  As already discussed, in a
Local Variables: section, for another example.

> > OK, thanks.  He is proposing that the meaning of -*- c -*-, `c-mode'
> > as used in normal-mode, etc., should, from the user's point of view,
> > be changed in an opt-out fashion.  He is proposing that there be no
> > way to specify C Mode in a local variables section.  Such changes
> > should be opt-in, not opt-out.  They would certainly need an entry in
> > NEWS.  if there's not already one there.

> It is supposed to opt-in, indeed.

So, for a user trying out Emacs 30, with a desktop file containing at
least one file with major mode c-ts-mode; visiting a file with the above
Local Variables section will indeed start the buffer with C Mode?

I haven't looked just yet, but can I assume there's a recipe in the
Emacs 30 NEWS file telling users how to switch between old behaviour
(-*- c -*- always means C mode) and new behaviour (-*- c -*- can mean
c-ts-mode depending on other settings)?  Is the default to keep the old
behaviour, as is customary with new features in Emacs?

>         Stefan

-- 
Alan Mackenzie (Nuremberg, Germany).