%\RequirePackage[enable-debug]{expl3}[2018/06/14]
\ProvidesExplPackage {tagpdfdocu-patches} {2026-03-19} {0.99z}
 {patches/commands for the tagpdf documentation}
\RequirePackage{etoolbox,xpatch}

%Logos
% only spacing changes:
\ExplSyntaxOn
\DeclareRobustCommand\TeX{
  \leavevmode
     T\kern-.1667em\lower.5ex\hbox{E}\kern-.05emX\@ %changed from -.125em
   }
\DeclareRobustCommand{\LaTeX}{
        \leavevmode
        L\kern-.25em % %changed from -0.36em
        \sbox\z@ T%
         \vbox to\ht\z@{\hbox{\check@mathfonts
                              \fontsize\sf@size\z@
                              \math@fontsfalse\selectfont
                              A}%
                        \vss}%
        \kern-.1em % %changed from 0.15em
        T\kern-.1667em\lower.5ex\hbox{E}\kern-.05emX\@ %changed from -0.125
    }
  
% minisec, correct tagging is still unclear.  
\newcommand\minisec[1]{%
    \if@noskipsec \leavevmode \fi
    \par
    \@afterindentfalse
    \if@nobreak
      \everypar{}%
    \else
      \addpenalty\@secpenalty\addvspace{1.5ex}%
    \fi
  {\tagpdfsetup{para/maintag=P,para/tag=Strong}\parindent \z@    
   \setlength{\parfillskip}{\z@ plus 1fil}%
    \raggedright\normalfont\bfseries\nobreak
    \nobreak\interlinepenalty \@M #1\par\nobreak%
  }\nobreak
  \@afterheading
}                    

% listings. 
% The following seems to work, but it errors if the last line of
% a listing is empty. 
% from previous tries there is also a copy taglstlisting.
% Support for numbers:

\NewTaggingSocket{listings/number}{2}
\NewTaggingSocketPlug{listings/number}{default}
 {
   \tag_mc_end_push:
   \tagstructbegin{tag=Lbl}
    \tagmcbegin{}
     #2
    \tagmcend
   \tagstructend
   \tag_mc_begin_pop:n{} 
 }
\AssignTaggingSocketPlug{listings/number}{default} 
\ExplSyntaxOff
\lst@Key{numbers}{none}{%
    \let\lst@PlaceNumber\@empty
    \lstKV@SwitchCases{#1}%
    {none:\\%
     left:\def\lst@PlaceNumber{\llap{\normalfont
                \UseTaggingSocket{listings/number}{}{\lst@numberstyle{\thelstnumber}}\kern\lst@numbersep}}\\%
     right:\def\lst@PlaceNumber{\rlap{\normalfont
                \kern\linewidth \kern\lst@numbersep
                \UseTaggingSocket{listings/number}{}{\lst@numberstyle{\thelstnumber}}}}%
    }{\PackageError{Listings}{value `#1' for keyword `numbers' not supported}\@ehc}}
\ExplSyntaxOn

% environment tagging. Updated 2025-12-27 to new block code

% new code from 2025-12-27, requires latex-dev     
  \DeclareInstance{blockenv}{lstlisting}{std}
   {
    name           = lstlisting, 
    tag-name       = verbatim,
    tag-attr-class = ,           
    tagging-recipe = standard,
    inner-level-counter  = ,
    transparent-level = true,    
    legacy-code     = ,
    block-instance = verbatim ,
    inner-instance = ,
    final-code     = \AssignStructureRole {para/textblock}%
                         {\UseStructureName{block/verbatim/codeline}}\ignorespaces,
    tagging-suppress-paras = true 
   }  
   
 \lst@AddToHook{PreInit}
  { 
    \MathCollectFalse 
    \ifx\lst@float\relax
     \SimpleBlockEnv{lstlisting}{}%
    \else 
     \SimpleBlockEnv{lstlisting}{tagging-recipe = standalone}
    \fi
  }

 \lst@AddToHook{DeInit}
   {\BlockEnvEnd}% 

 \tagpdfsetup{float/new=lstlisting}

% ======== marginnote ==========
% 2024-03-23 currently marginnote is no longer used, we use marginpar
% TODO marginnote has a bug (a \par is missing) so it messes up tagging.
% but currently unneeded as we marked them up as artifacts anyway as they don't contain
% meaningful contents
%\renewcommand*{\mn@parboxrestore}{\tagpdfparaOff}%

%\NewDocumentCommand\sidenote{m}
% {%
%   %\tag_mc_artifact_group_begin:n{notype}\tagpdfparaOff\marginnote{#1}\tag_mc_artifact_group_end:
%   \marginpar{#1}%
% }

\ExplSyntaxOff

%======== tcolorbox ========
% We switch of paratagging at the begin and reenable it locally in before upper.
% the before upper setting is dangerous as it can be overwritten by
% users. So a more stable hook is needed.
% we force also a \par and add a div structure, to avoid clashes with the block
% tagging code. This needs revisiting!

%\AddToHook{env/tcolorbox/before}{\par\tagstructbegin{tag=Div}}
%\AddToHook{env/tcolorbox/begin}{%
%\AssignTaggingSocketPlug{minipage/before}{noop}%
%\AssignTaggingSocketPlug{minipage/after}{noop}%
%\tagpdfparaOff \tcbset{before upper=\tagpdfparaOn}}
%\AddToHook{env/tcolorbox/after}{\par\tagstructend}

% ========= doc Commands from tcolorbox
% Not sure if this is generally usable but one must avoid tagstop if there can
% be a pagebreak 

\NewTaggingSocketPlug{para/begin}{tagpdf/mc}{\tagmcbegin{}}
\NewTaggingSocketPlug{para/end}{tagpdf/mc}{\tagmcend} 
 
\DeclareInstance{blockenv}{docCommand}{std}
  {
    name       = docCommand,
    tag-name       = Div,
    tag-attr-class      = ,
    tagging-recipe = standalone,
    inner-level-counter  = ,
    transparent-level = true,
    legacy-code     = ,
    block-instance = std-display ,
  }

\RenewDocumentEnvironment{tcb@manual@entry}{}
  {\SimpleBlockEnv{docCommand} 
     {
       leftmargin=\kvtcb@doc@left,
       rightmargin=\kvtcb@doc@right,
       tag-name=verbatim
       }%
   \MathCollectFalse   
   \AssignStructureRole {tcb/box}{codeline}%
   \AssignTaggingSocketPlug{para/begin}{tagpdf/mc}
   \AssignTaggingSocketPlug{para/end}{tagpdf/mc}
   \AssignTaggingSocketPlug{minipage/before}{noop}%
   \AssignTaggingSocketPlug{minipage/after}{noop}%      
 }   
 {\BlockEnvEnd } 


% the \meta command uses \langle/\rangle which produce formulas in code.
% this need some general solution! For now we suppress tagging of \ensuremath.
 \long\def\@ensuredmath#1{{\MathCollectFalse$\relax#1$}}


% ====== hyperref ========
% this should probably go into hyperref, but it is related to
% problem of pdf strings and context ....

\AddToHook{package/hyperref/after}
 {%
  \pdfstringdefDisableCommands{%
   \let\tagstructbegin\@gobble
   \let\tagmcbegin\@gobble
   \let\tagmcend\relax
   \let\tagstructend\relax
 }}{}



\endinput
