Re: AUCTeX and "builtin latex mode" integration

2022-09-25 Thread Ikumi Keita
Hi Stefan and all,

> Stefan Monnier  writes:
>> Then the idea of turning AUCTeX into minor mode itself does not help.

> Not necessarily, but it could if we can arrange for latex-mode and
> auctex-mode not to collide in their keymaps (and syntax table, ...), so
> that entries added to `latex-mode-map` wouldn't be hidden by the
> auctex-mode-map (e.g. if that map was added with lower priority than
> latex-mode-map).

Ah, indeed. It would be possible that `LaTeX-mode-map' has a parent
keymap composed of both `latex-mode-map' and `TeX-mode-map'. Similar
treatment would go for syntax table.

>> (a) In addition to being minor mode, AUCTeX uses lower case
>> `latex-mode-hook', `latex-mode-map' and `latex-mode-syntax-table'.
>> In this case, variable aliases `LaTeX-mode-hook' ->
>> `latex-mode-hook' etc. would also be necessary for compatibility.

> It could also just run `latex-mode-hook` in addition to what it already
> runs, without setting up any alias between that and `LaTeX-mode-hook`.

Indeed.

>> (b) AUCTeX abandons the relationship between `LaTeX-mode' and
>> `latex-mode'. I.e. `LaTeX-mode' no longer pretends to be
>> `latex-mode'.

> I think whether it pretends to be is a separate question from whether it
> redirects `latex-mode` to its own implementation.

> E.g. we could start by setting `major-mode` to `LaTeX-mode` rather than
> `latex-mode`.

Hmm, that approach didn't came to my mind. After reconsideration, it
seems faily reasonable than I first saw it.

Possible/potential caveats:
1. Suppose that the file has %%% mode:latex as file variable. When emacs
opens this file, it runs `latex-mode' accordingly, which in turn runs
AUCTeX `LaTeX-mode' by redirectiton. Then it sets `major-mode' to
`LaTeX-mode'. After that, `hack-local-variables' runs; it sees that
`major mode' doesn't match the %%% mode:latex line and runs `latex-mode'
again. In this way, a file with %%% mode:latex or -*- latex -*- always
runs `latex-mode' twice.
I hope that we can find a way to avoid such overheads.

2. In order to keep backward compatibility of an entry of the form
(latex (XXX . t) (YYY . 15) ...) in .dir-locals.el, `LaTeX-mode' should
be a derived mode of `latex-mode'. However, defining `LaTeX-mode' by
(define-derived-mode LaTeX-mode latex-mode ...) would cause infinite
loop when redirection is enabled.
I expect that we can circumvent the difficulty by putting
`derived-mode-parent' property on `LaTeX-mode', without
`define-derived-mode'.

Regards,
Ikumi Keita
#StandWithUkraine #StopWarInUkraine



Re: AUCTeX and "builtin latex mode" integration

2022-09-25 Thread Stefan Monnier
> 1. Suppose that the file has %%% mode:latex as file variable. When emacs
> opens this file, it runs `latex-mode' accordingly, which in turn runs
> AUCTeX `LaTeX-mode' by redirectiton. Then it sets `major-mode' to
> `LaTeX-mode'.  After that, `hack-local-variables' runs; it sees that
> `major mode' doesn't match the %%% mode:latex line and runs `latex-mode'
> again.

Why would `hack-local-variables` do that ?
AFAIK vanilla Emacs doesn't do such a thing (I just tried it in
Emacs-29 to confirm).


Stefan




Re: AUCTeX and "builtin latex mode" integration

2022-09-25 Thread Stefan Monnier
> be a derived mode of `latex-mode'. However, defining `LaTeX-mode' by
> (define-derived-mode LaTeX-mode latex-mode ...) would cause infinite
> loop when redirection is enabled.

Yes, it's ugly.  That's part of the reason for the weird way we define
`tex-mode` in `tex-mode.el` (and that was preceded by other weird ways
to do it).

I have an idea for how we should solve this in general in Emacs, but in
the mean time a hack like the (or delay-mode-hooks tex-mode--recursing)
I used in `tex--redirect-to-submode` is about as good as I could make
it :-(

> I expect that we can circumvent the difficulty by putting
> `derived-mode-parent' property on `LaTeX-mode', without
> `define-derived-mode'.

That's another option, indeed.


Stefan




Re: AUCTeX and "builtin latex mode" integration

2022-09-25 Thread Stefan Monnier
Stefan Monnier [2022-09-25 10:01:40] wrote:
>> 1. Suppose that the file has %%% mode:latex as file variable. When emacs
>> opens this file, it runs `latex-mode' accordingly, which in turn runs
>> AUCTeX `LaTeX-mode' by redirectiton. Then it sets `major-mode' to
>> `LaTeX-mode'.  After that, `hack-local-variables' runs; it sees that
>> `major mode' doesn't match the %%% mode:latex line and runs `latex-mode'
>> again.
> Why would `hack-local-variables` do that ?
> AFAIK vanilla Emacs doesn't do such a thing (I just tried it in
> Emacs-29 to confirm).

Oh, I see where that happens (and why it doesn't happen if we redirect
with `defalias`).


Stefan




Re: AUCTeX and "builtin latex mode" integration

2022-09-25 Thread Stefan Monnier
> Oh, I see where that happens (and why it doesn't happen if we redirect
> with `defalias`).

Hmm... no I still can't reproduce it, even with something else than
a defalias.


Stefan




Re: AUCTeX and "builtin latex mode" integration

2022-09-25 Thread Ikumi Keita
> Stefan Monnier  writes:
>> Oh, I see where that happens (and why it doesn't happen if we redirect
>> with `defalias`).

Sorry, I was talking based on my memory when I hacked
`japanese-latex-mode' in tex-jp.el to deal with double loading issue. I
forgot that `hack-one-local-variable' uses `indirect-function'. Yes, it
wouldn't happen when we use `defalias'.

> Hmm... no I still can't reproduce it, even with something else than
> a defalias.

Hmm. The double loading caused by mismatch between `major-mode' and
%%% mode: line did occur for `japanese-latex-mode' IIRC, but that might
not apply for `LaTeX-mode' case by some reason I'm looking over.

Regards,
Ikumi Keita
#StandWithUkraine #StopWarInUkraine