Subject: Re: bug#31551: 27.0.50; emacs hangs in hexl-mode on modula2 files

From: Stefan Monnier <[email-masked]>
Date: Wed, 06 Jun 2018 10:00:04 -0400

>> I dig into the issue and it appears that the was because of the
>> show-paren-mode and the fact that the ":" character have meaning in
>> modula-2-mode. Emacs is stuck in the loop of the
>> m2-smie-refine-colon function because after the "00000000" that start
>> the hexl buffer there is no more sexp to parse so the tok variable is
>> empty but the (forward-sexp -1) call does not fail.

Clearly, this is a bug in m2-smie-refine-colon, indeed (not a big
surprise: you're the first user of Modual-2 I encounter since I wrote
that indentation code).

> Hmm... I think there's a more serious issue at hand here.  Hexl mode
> is not a normal major mode: it doesn't undo the effects of the
> previous major mode.

Indeed, it fails to call kill-all-local-variables.  It justifies it with:

    ;; We do not turn off the old major mode; instead we just
    ;; override most of it.  That way, we can restore it perfectly.

which I find unconvincing.

> So what happens here is that the display is entirely inappropriate for
> Modula2 sources, but the syntactic setup made by m2-mode is still in
> effect, and that setup attaches special meaning to ':' when scanning
> for parentheses.

Right, it failed to "unsetup" SMIE.

> On top of that, show-paren-mode will happily turn itself on in a
> buffer under Hexl mode, although it makes no sense to me: the Hexl
> display shows no parens to work with.

At the same time, it's only enabled if the user explicitly requests it
(as is the case in the OP's recipe), so I think we can consider this
a pilot error.

> The fact that syntactical customizations of a mode stay in effect when
> the display changes so radically sounds like a potential cause of many
> trouble.  So perhaps the right solution would be to temporarily
> disable mode syntax while in Hexl mode.

Agreed.  Hexl-mode should use a "normal" major mode (e.g. calling
kill-all-local-variables).  In order to properly reset the previous
major mode, it could either save&restore all previous local-vars, or
just save the previous major-mode and call that major-mode
function to restore the state.

BTW, this "save previous major mode and then restore it" is something
done in other places (at least doc-view-mode comes to mind, but
I believe there are others as well) and we should try and avoid
duplicating that code (i.e. develop a "standard" way to do it).

        Stefan