Why are lstlang*.sty files from listings package loaded 3 times?

During compilation of the following MCE:

\documentclass{article}
\usepackage{listings}
\lstset{language=TeX}
\begin{document}
\begin{lstlisting}
Foo
\end{lstlisting}
\end{document}


the lstlang*.sty files are loaded 3 times (and lstmisc.sty 2 times):

[...]
(/usr/local/texlive/2013/texmf-dist/tex/latex/listings/lstmisc.sty)
(/usr/local/texlive/2013/texmf-dist/tex/latex/listings/listings.cfg))
(/usr/local/texlive/2013/texmf-dist/tex/latex/listings/lstlang1.sty)
(/usr/local/texlive/2013/texmf-dist/tex/latex/listings/lstlang2.sty)
(/usr/local/texlive/2013/texmf-dist/tex/latex/listings/lstlang3.sty)
(/usr/local/texlive/2013/texmf-dist/tex/latex/listings/lstlang1.sty)
(/usr/local/texlive/2013/texmf-dist/tex/latex/listings/lstlang2.sty)
(/usr/local/texlive/2013/texmf-dist/tex/latex/listings/lstlang3.sty)
(/usr/local/texlive/2013/texmf-dist/tex/latex/listings/lstlang1.sty)
(/usr/local/texlive/2013/texmf-dist/tex/latex/listings/lstlang2.sty)
(/usr/local/texlive/2013/texmf-dist/tex/latex/listings/lstlang3.sty)
(/usr/local/texlive/2013/texmf-dist/tex/latex/listings/lstmisc.sty)
[...]


Is it necessary?

-
Very strange indeed. It seems to have something to do with inputting .sty files and related to what ever listings is doing to \lstlanguagefiles, though I have no idea what it is doing to that macro and why the lang files are loaded after listings.cfg – daleif Jul 18 '13 at 8:01
The lstlangX.sty files are loaded by mean of \lst@Require with \InputIfFileExists rather than with \usepackage, so they can be loaded more than once. Why they are input three times is not clear, but I suspect this is part of the search for aliases and dialects for a given main language. – egreg Aug 23 '13 at 16:26

As egreg observes in comments, this seems to be due to the dialect system in listings. If you 'break in' to the process of file loading, say in \lst@LAS, you find that for TeX the three passes correlate with three different argument searches:

• []{TeX}
• [common]{TeX}
• [primitive]{TeX}

and also will note the following:

• The number of load cycles depends on how many dialects there are (e.g. just the one for Delphi, two for COBOL, ...)
• The number of files loaded depends on where the language happens to be defined.

Thus it seems what happens is roughly as follows. On the first pass, listings always looks for the 'bare' language unless it has a preset dialect. It stops reading files as soon as it's found and processed this 'base' position. If this has been defined as depending on a second dialect, the files are then re-read to load it. This itself may be dependent on a third dialect, and so on until the bottom is reached.

In the TeX case, the config file has already set the default to [plain], so listings loads that. The plain definition says [plain]{TeX}[common]{TeX}, so a second pass loads [common]{TeX}. That is then dependent on [primitive]{TeX}, which forces a third pass. The TeX stuff is all in the third language file, so they are all read three times, while the misc file is skipped on the third pass as everything is resolved.

-
With this knowledge it is possible to specify the load languages: \lstloadlanguages{TeX, [common]TeX, [primitive]TeX}. Then package listings reads the language definition files only once. – Heiko Oberdiek Aug 26 '13 at 20:45
In the case of TeX, this process seems not to be the optimal one as, AFAIK, common and primitive are not really dialects but more subsets of TeX, and as, I guess, one would seldom load only one of these subsets. BTW, Jobst Hoffmann, the new listings maintainer, is aware of this exchange. – Denis Bitouzé Aug 27 '13 at 7:33
@DenisBitouzé Certainly unlikely, but I do see the idea of making things modular. There is no real cost to loading more than once other than log noise, is there? – Joseph Wright Aug 27 '13 at 7:48
@JosephWright Things made modular are nice but maybe only if the modules are not too tiny :) There is indeed no real cost since nowadays computers are fast but, anyway, I'm puzzled... – Denis Bitouzé Aug 29 '13 at 5:39