Subject: Re: bug#72343: [PATCH] Fix eglot-server-programs for TeX modes

From: Augusto Stoffel <[email-masked]>
Date: Tue, 06 Aug 2024 20:48:00 +0200

On Tue,  6 Aug 2024 at 19:08, João Távora wrote:

> On Tue, Aug 6, 2024 at 5:38 PM Augusto Stoffel <[email-masked]> wrote:
>
>> > So there's no "single pick" in general.
>> You don't need to explain that to me ;-).
>
> It is you who alluded to a single pick, so I thought I had to.  Also
> earlier you proposed multiple entries in eglot-server-programs, which
> would impede this multi-language-per-session feature.>  So I figured you
> wouldn't be familiar with it.

Fair enough.

>> What I want to achieve (or, if I may, what Eglot should do) in case it
>> wasn't clear yet, is: file x.tex is sent language id "latex" and file
>> y.tex is sent language id "plain-tex".
>
> Indeed it wasn't clear, so I'm very glad I asked.  I don't see
> "plain-tex" in the list over at
>
>   https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/
>
> I just see "tex" and "latex" and "bibtex".

I know about the list, but unfortunately "tex" as a language id makes no
sense.  It's like saying a program is written in "lisp" without saying
which Lisp dialect.

> But it won't for many other cases.  The fact that Emacs models
> latex-mode as a descendent of tex-mode is something Eglot can't do
> anything about.  It means that Emacs thinks that that every LaTex
> program is a TeX program at least at some syntactic level (is this true?
> no idea).

Again, every TeX program belongs to a specific dialect: plain TeX, LaTeX
and ConTeXt being the main ones.  Accordingly, tex-mode is not a real
major mode; it's just an "abstract base mode" from which the actual TeX
modes derive (see the docstring for details).

> I do think that's the lesser of two evils though.  So anyway this is my
> suggestion:
>
> @@ -292,7 +292,7 @@ eglot-server-programs
>      (scala-mode . ,(eglot-alternatives
>                      '("metals" "metals-emacs")))
>      (racket-mode . ("racket" "-l" "racket-langserver"))
> -    ((tex-mode context-mode texinfo-mode bibtex-mode)
> +    ((latex-mode (plain-tex-mode :language-id "tex") context-mode
> texinfo-mode bibtex-mode)
>       . ,(eglot-alternatives '("digestif" "texlab")))
>      (erlang-mode . ("erlang_ls" "--transport" "stdio"))
>      ((yaml-ts-mode yaml-mode) . ("yaml-language-server" "--stdio"))

So again, for the reason above, I wouldn't do what you suggest here.

> In theory, this could also be fixed by keeping tex-mode in there, and
> then adjusting Eglot's heuristic, by ranking the potential ancestors and
> selecting the closest one, in 'eglot--languageId'.  Could be tricky or
> could be easy, feel free to give it a shot.

(The status quo is already way too complicated, and this doesn't sound
like an improvement in that regard...)

> Yet another idea is to not reorder modes at all in 'eglot--lookup-mode'
> and use an explicit order to express this ranking.  I admit I don't
> remember what the reordering idea (main-mode-sym is the local var name)
> is for.  it couldprecede the multiple-language-ids feature.  Or it could
> be essential for some other thing: testing would be needed.
>
>> This doesn't matter, as it's an Eglot issue.  Any TeX server will do.
>
> This is not established, at least not yet.  You are requesting for a
> "plain-tex" LSP languageId property to be sent and that is outside the
> spec as far as I can tell from reading the spec.  Maybe your digestif
> server supports and likes it, though.  But it might break "texlab" for
> all I know.

Note also that the LSP spec says nothing about ConTeXt or Texinfo.
Moreover, texlab only supports latex and bibtex.

> Even the fact that "tex" is all that's being sent at the
> moment may or may not be a bug -- only LaTex/Tex specialists can say
> that.  And certainly a capable enough language server, which is tasked
> with analysing a file syntactically, can do much the same as our own
> tex-mode.el and figure out the correct language from the contents.
> Might do it better than Emacs.

That's true, and in fact it's true all languages.  So a server could as
well ignore the language id and deduce it on its own -- except for the
caveat that ultimately the user might know better and it's useful to
rely on the user's choice of major mode / language id.

> That said, if you really want to send "plain-tex", then there are two
> options.  You can change your user value of eglot-server-programs,
> obviously.  Or you can confirm with "texlab" that they don't mind, in
> which case -- but only in that case -- your latest patch is acceptable.

I didn't ask but I don't see what could go wrong, given that texlab is
for latex and bibtex only.