% \iffalse meta-comment
%
%% File: latex-lab-toc-kernel-changes.dtx (C) Copyright 2022-2026 LaTeX Project
%
% It may be distributed and/or modified under the conditions of the
% LaTeX Project Public License (LPPL), either version 1.3c of this
% license or (at your option) any later version.  The latest version
% of this license is in the file
%
%    https://www.latex-project.org/lppl.txt
%
%
% The latex-lab bundle is developed in the LaTeX2e GitHub.
% Issues may be reported at
%
%    https://github.com/latex3/latex2e/issues
%
% dates for latex-lab-kernel-changes.sty (pulled from various sources, see ins)
\def\ltlabkerneldate{2026-04-27}
\def\ltlabkernelversion{0.85j}
%<*driver>
\DocumentMetadata{tagging=on,pdfstandard=ua-2}
\documentclass[kernel]{l3in2edoc}

\EnableCrossrefs
\CodelineIndex
\begin{document}
  \DocInput{latex-lab-toc-kernel-changes.dtx}
\end{document}
%</driver>
%
% \fi
%
% \title{The \textsf{latex-lab-toc-kernel-changes} package\\
% Changes and additions to the kernel related to tagging and links in toc entries}
% \author{\LaTeX{} Project\thanks{Initial implementation done by Ulrike Fischer}}
% \date{Version \ltlabkernelversion\ \ltlabkerneldate}
%
% \maketitle
%
% \newcommand{\xt}[1]{\textsl{\textsf{#1}}}
% \newcommand{\TODO}[1]{\textbf{[TODO:} #1\textbf{]}}
% \newcommand{\docclass}{document class \marginpar{\raggedright document class
% customizations}}
%
% \providecommand\hook[1]{\texttt{#1}}
%
%
%
% \begin{abstract}
% The following module contains various changes to kernel commands and commands of the
% standard classes needed for the tagging of table of contents and similar lists.
% \end{abstract}
%
% \section{Package declaration}
%    \begin{macrocode}
%<*kernelchange>
\ProvidesPackage{latex-lab-kernel-changes}
        [\ltlabkerneldate\space v\ltlabkernelversion\space
         General kernel and class changes]
%    \end{macrocode}
%
% \changes{v0.85i}{2025/07/09}{removed now unneeded hyperref definition, socket declaration}
%
% \section{Assigning the headings level numbers}
%
% The code for the toc must be able to compare the level of |subsection| against
% |section|. So some numbers are needed. It uses for now the commands used by
% |hyperref| for the bookmark levels. While headings are more a class thing, it
% could make sense to provide a standard set already in the kernel.
% The numbers are a bit different to the one in hyperref, but imho sensible.
%
% \TODO{once that heading stuff is turned into template
% instances the template should set that number.}
%    \begin{macrocode}
\def\toclevel@document{-10}
\def\toclevel@part{-1}
\def\toclevel@chapter{0}
\def\toclevel@section{1}
\def\toclevel@subsection{2}
\def\toclevel@subsubsection{3}
\def\toclevel@paragraph{4}
\def\toclevel@subparagraph{5}
\def\toclevel@figure{1}
\def\toclevel@table{1}
%    \end{macrocode}
%
% \section{Storing relationship between target and structure}
% This is done in the kernel in a socket of \cs{MakeLinkTarget}
% \changes{0.85j}{2026-04-27}{Removed unneeded hyperref patch}
%    \begin{macrocode}
%</kernelchange>
%    \end{macrocode}
%
% \section{\cs{contentsline} extension}
%
% The tagging code must add code at the begin and end of \cs{contentsline}.
% This code must have access to the arguments. They are passed
% together in tagging socket which are declared in \texttt{lttagging}.
% The name of destination is forward through a command name to avoid
% problem with definitions of \cs{l@XX} which expect only two arguments.
%    \begin{macrocode}
%<*package>
\def\contentsline#1#2#3#4%
  {%
    \UseTaggingSocket{toc/contentsline/before}{{#1}{#2}{#3}{#4}}%
    \gdef\@contentsline@destination{#4}%
    \csname l@#1\endcsname{#2}{#3}%
    \UseTaggingSocket{toc/contentsline/after}{{#1}{#2}{#3}{#4}}%
  }
%    \end{macrocode}
%
% \section{\cs{addcontentsline} changes}
%
% \cs{addcontentsline} should always write the unique counter representation
% \cs{@currentHref} so that we can create the |/Ref| key. At the same time
% we need a hook, so that hyperref can add its bookmarks code, the generic hook
% with arguments is ok.
%
%    \begin{macrocode}
\def\addcontentsline#1#2#3{% #1=toc extension, #2= heading type, tag
    \addtocontents{#1}{%
      \protect\contentsline{#2}{#3}{\thepage}{\@currentHref}\protected@file@percent
    }}
%    \end{macrocode}
%
% \section{\cs{@starttoc}}
%
%    \begin{macrocode}
\def\@starttoc#1{%
  \begingroup
    \makeatletter
    \UseTaggingSocket{toc/starttoc/before}{#1}%
    \@input{\jobname.#1}%
    \UseTaggingSocket{toc/starttoc/after}{#1}%
    \if@filesw
      \expandafter\newwrite\csname tf@#1\endcsname
      \immediate\openout \csname tf@#1\endcsname \jobname.#1\relax
    \fi
    \@nobreakfalse
  \endgroup}
%    \end{macrocode}
%
% \section{Formatting of the toc entries}
%
% \subsection{Kernel commands}
%
% We need hooks before and after the content and before and after the page number.
% The number needs some thing so that it can be tagged as label.
% The text and the page
% hooks are used in manual \cs{l@xx} commands and in \cs{l@xx} defined by
% \cs{@dottedtocline} or by other means.
% The arguments of the following hooks are:
%  \begin{itemize}
%  \item[\#1] level as a \emph{number} (integer expression), see above for the standard
%   numbering. The standard first argument of \cs{contentsline} is not used, as
%   \cs{@dottedtocline} knows only a number.
%  \item[\#2] content (the second argument of a \cs{contentsline})
%  \item[\#3] page    (the third argument of a \cs{contentsline})
%  \item[\#4] target  (the fourth argument of a \cs{contentsline})
%  \end{itemize}
%    \begin{macrocode}
\NewMirroredHookPairWithArguments{contentsline/number/before}{contentsline/number/after}{1}%
\NewMirroredHookPairWithArguments{contentsline/text/before}{contentsline/text/after}{4}%
\NewMirroredHookPairWithArguments{contentsline/page/before}{contentsline/page/after}{4}%
%    \end{macrocode}
%
% The dots in the leader must be set as artifact.
% But luatex requires that the mc are set outside the leaders.
% So we need sockets.
%
% \cs{@dottedtocline} needs hooks and formatting commands.
% To make the leaders into artifact we use tagging sockets.
%
% The destination is accessed through \cs{@contentsline@destination}.
%    \begin{macrocode}
\def\@dottedtocline#1#2#3#4#5{%
  \ifnum #1>\c@tocdepth \else
    \vskip \z@ \@plus.2\p@
    {\leftskip #2\relax \rightskip \@tocrmarg \parfillskip -\rightskip
     \parindent #2\relax\@afterindenttrue
     \interlinepenalty\@M
     \leavevmode
     \@tempdima #3\relax
     \advance\leftskip \@tempdima \null\nobreak\hskip -\leftskip
     \UseHookWithArguments{contentsline/text/before}{4}{#1}{#4}{#5}{\@contentsline@destination}%
     \csname contentsline@text@#1@format\endcsname{#4}%
     \UseHookWithArguments{contentsline/text/after}{4}{#1}{#4}{#5}{\@contentsline@destination}%
     \nobreak
     \UseTaggingSocket{toc/leaders/before}\SuspendTagging{toc/leaders}%
       \leaders\hbox{$\m@th
         \mkern \@dotsep mu\hbox{.}\mkern \@dotsep
          mu$}\hfill
     \ResumeTagging{toc/leaders}\UseTaggingSocket{toc/leaders/after}%
     \nobreak
     \hb@xt@\@pnumwidth{\hfil\normalfont \normalcolor
     \UseHookWithArguments{contentsline/page/before}{4}{#1}{#4}{#5}{\@contentsline@destination}%
     #5%
     \UseHookWithArguments{contentsline/page/after}{4}{#1}{#4}{#5}{\@contentsline@destination}%
                        \kern-\p@\kern\p@}%
     \par}%
  \fi}
%    \end{macrocode}
%
% \subsection{\cs{numberline}}
%
% Inside or outside the box?
%    \begin{macrocode}
\def\numberline#1{\hb@xt@\@tempdima{%
  \UseHookWithArguments{contentsline/number/before}{1}{#1}%
  #1\hfil
  \UseHookWithArguments{contentsline/number/after}{1}{#1}%
  }}
%    \end{macrocode}
%
% \subsection{\cs{l@xx} in the classes}
% Depending on the class we have to redefine also more commands
% For other classes it should be documented what is needed.
%
%    \begin{macrocode}
\AddToHook{class/article/after}
 {
  \renewcommand*\l@part[2]{% as in contentsline: content, page
  \ifnum \c@tocdepth >-2\relax
    \addpenalty\@secpenalty
    \addvspace{2.25em \@plus\p@}%
    \setlength\@tempdima{3em}%
    \begingroup
      \parindent \z@ \rightskip \@pnumwidth
      \parfillskip -\@pnumwidth
      {\leavevmode
       \large \bfseries
       \UseHookWithArguments{contentsline/text/before}{4}
         {\toclevel@part}{#1}{#2}{\@contentsline@destination}%
       \csname contentsline@text@-1@format\endcsname{#1}%
       \UseHookWithArguments{contentsline/text/after}{4}
         {\toclevel@part}{#1}{#2}{\@contentsline@destination}%
       \hfil
       \hb@xt@\@pnumwidth{\hss
       \UseHookWithArguments{contentsline/page/before}{4}
         {\toclevel@part}{#1}{#2}{\@contentsline@destination}%
       #2%
       \UseHookWithArguments{contentsline/page/after}{4}
         {\toclevel@part}{#1}{#2}{\@contentsline@destination}%
                          \kern-\p@\kern\p@}}\par
       \nobreak
    \endgroup
  \fi}
\renewcommand*\l@section[2]{%
  \ifnum \c@tocdepth >\z@
    \addpenalty\@secpenalty
    \addvspace{1.0em \@plus\p@}%
    \setlength\@tempdima{1.5em}%
    \begingroup
      \parindent \z@ \rightskip \@pnumwidth
      \parfillskip -\@pnumwidth
      \leavevmode \bfseries
      \advance\leftskip\@tempdima
      \hskip -\leftskip
       \UseHookWithArguments{contentsline/text/before}{4}
        {\toclevel@section}{#1}{#2}{\@contentsline@destination}%
       \csname contentsline@text@1@format\endcsname{#1}%
       \UseHookWithArguments{contentsline/text/after}{4}
        {\toclevel@section}{#1}{#2}{\@contentsline@destination}%
       \nobreak\hfil
      \nobreak\hb@xt@\@pnumwidth{\hss
      \UseHookWithArguments{contentsline/page/before}{4}
        {\toclevel@section}{#1}{#2}{\@contentsline@destination}%
       #2%
      \UseHookWithArguments{contentsline/page/after}{4}
        {\toclevel@section}{#1}{#2}{\@contentsline@destination}%
      \kern-\p@\kern\p@}\par
    \endgroup
  \fi}
 }
\AddToHook{class/report/after}
 {
    \renewcommand*\l@part[2]{%
     \ifnum \c@tocdepth >-2\relax
       \addpenalty{-\@highpenalty}%
       \addvspace{2.25em \@plus\p@}%
       \setlength\@tempdima{3em}%
       \begingroup
         \parindent \z@ \rightskip \@pnumwidth
         \parfillskip -\@pnumwidth
         {\leavevmode
          \large \bfseries
          \UseHookWithArguments{contentsline/text/before}{4}
            {\toclevel@part}{#1}{#2}{\@contentsline@destination}%
          \csname contentsline@text@-1@format\endcsname{#1}%
          \UseHookWithArguments{contentsline/text/after}{4}
            {\toclevel@part}{#1}{#2}{\@contentsline@destination}%
          \hfil
          \hb@xt@\@pnumwidth{\hss
          \UseHookWithArguments{contentsline/page/before}{4}
            {\toclevel@part}{#1}{#2}{\@contentsline@destination}%
          #2%
          \UseHookWithArguments{contentsline/page/after}{4}
            {\toclevel@part}{#1}{#2}{\@contentsline@destination}%
                             \kern-\p@\kern\p@}}\par
          \nobreak
       \endgroup
     \fi}
   \renewcommand*\l@chapter[2]{%
     \ifnum \c@tocdepth >\m@ne
       \addpenalty{-\@highpenalty}%
       \vskip 1.0em \@plus\p@
       \setlength\@tempdima{1.5em}%
       \begingroup
         \parindent \z@ \rightskip \@pnumwidth
         \parfillskip -\@pnumwidth
         \leavevmode \bfseries
         \advance\leftskip\@tempdima
         \hskip -\leftskip
         \UseHookWithArguments{contentsline/text/before}{4}
           {\toclevel@chapter}{#1}{#2}{\@contentsline@destination}%
         \csname contentsline@text@0@format\endcsname
           {#1}%
         \UseHookWithArguments{contentsline/text/after}{4}
           {\toclevel@chapter}{#1}{#2}{\@contentsline@destination}%
          \nobreak\hfil
         \nobreak\hb@xt@\@pnumwidth{\hss
          \UseHookWithArguments{contentsline/page/before}{4}
            {\toclevel@chapter}{#1}{#2}{\@contentsline@destination}%%
          #2%
          \UseHookWithArguments{contentsline/page/after}{4}
            {\toclevel@chapter}{#1}{#2}{\@contentsline@destination}%%
                                    \kern-\p@\kern\p@}\par
         \penalty\@highpenalty
       \endgroup
     \fi}
 }
\AddToHook{class/book/after}
 {
    \renewcommand*\l@part[2]{%
     \ifnum \c@tocdepth >-2\relax
       \addpenalty{-\@highpenalty}%
       \addvspace{2.25em \@plus\p@}%
       \setlength\@tempdima{3em}%
       \begingroup
         \parindent \z@ \rightskip \@pnumwidth
         \parfillskip -\@pnumwidth
         {\leavevmode
          \large \bfseries
          \UseHookWithArguments{contentsline/text/before}{4}
            {\toclevel@part}{#1}{#2}{\@contentsline@destination}%%
          \csname contentsline@text@-1@format\endcsname{#1}%
          \UseHookWithArguments{contentsline/text/after}{4}
            {\toclevel@part}{#1}{#2}{\@contentsline@destination}%%
          \hfil
          \hb@xt@\@pnumwidth{\hss
          \UseHookWithArguments{contentsline/page/before}{4}
            {\toclevel@part}{#1}{#2}{\@contentsline@destination}%%
          #2%
          \UseHookWithArguments{contentsline/page/after}{4}
            {\toclevel@part}{#1}{#2}{\@contentsline@destination}%%
                             \kern-\p@\kern\p@}}\par
          \nobreak
       \endgroup
     \fi}
   \renewcommand*\l@chapter[2]{%
     \ifnum \c@tocdepth >\m@ne
       \addpenalty{-\@highpenalty}%
       \vskip 1.0em \@plus\p@
       \setlength\@tempdima{1.5em}%
       \begingroup
         \parindent \z@ \rightskip \@pnumwidth
         \parfillskip -\@pnumwidth
         \leavevmode \bfseries
         \advance\leftskip\@tempdima
         \hskip -\leftskip
         \UseHookWithArguments{contentsline/text/before}{4}
          {\toclevel@chapter}{#1}{#2}{\@contentsline@destination}%
         \csname contentsline@text@0@format\endcsname
           {#1}%
         \UseHookWithArguments{contentsline/text/after}{4}
          {\toclevel@chapter}{#1}{#2}{\@contentsline@destination}%
          \nobreak\hfil
         \nobreak\hb@xt@\@pnumwidth{\hss
          \UseHookWithArguments{contentsline/page/before}{4}
           {\toclevel@chapter}{#1}{#2}{\@contentsline@destination}%
          #2%
          \UseHookWithArguments{contentsline/page/after}{4}
           {\toclevel@chapter}{#1}{#2}{\@contentsline@destination}%
                                    \kern-\p@\kern\p@}\par
         \penalty\@highpenalty
       \endgroup
     \fi}
 }
%    \end{macrocode}
%    \begin{macrocode}
%</package>
%    \end{macrocode}
