% \iffalse meta-comment
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% memoize-ext.dtx
% Additions and changes Copyright (C) 2024-2026 Clea F. Rees.
% Code from skeleton.dtx Copyright (C) 2015-2024 Scott Pakin (see below).
%
% This work may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.3c
% of this license or (at your option) any later version.
% The latest version of this license is in
%   https://www.latex-project.org/lppl.txt
% and version 1.3c or later is part of all distributions of LaTeX
% version 2008-05-04 or later.
%
% This work has the LPPL maintenance status 'muaintained'.
%
% The Current Maintainer of this work is Clea F. Rees.
%
% This work consists of all files listed in manifest.txt.
%
% The file memoize-ext.dtx is a derived work under the terms of the
% LPPL. It is based on version 2.4 of skeleton.dtx which is part of
% dtxtut by Scott Pakin. A copy of dtxtut, including the
% unmodified version of skeleton.dtx is available from
% https://www.ctan.org/pkg/dtxtut and released under the LPPL.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% \fi
%
% \iffalse
%<*driver>
\GetIdInfo $Id: memoize-ext-tag.dtx 11705 2026-02-27 21:46:12Z cfrees $ {Extensions for Memoize}
\ProvidesExplFile{\ExplFileName}{\ExplFileDate}{v0.2 \ExplFileVersion}{\ExplFileDescription}
\begin{document}
\let\MakePrivateLetters\MyMakePrivateLetters
\DocInput{\filename}
\end{document}
%</driver>
% \fi
% 
%
% \title{\ExplFileName}
% \date{\ExplFileVersion~\ExplFileDate}
% \maketitle
%
% \begin{abstract}
%   \noindent\pkg{memoize-ext-tag} is part of \pkg{memoize-ext}.
%   It supports tagging memoized content/the memoization of tagged content.
% \end{abstract}
%
%
% \tableofcontents
% 
% 
% \MaybeStop{%
% \PrintChanges
% \PrintIndex
% }
% 
% ^^A \section{Implementation}
% Should probably use unique prefix \dots?
%
% Uses and/or redefines the following internals, primitives and other nefarious methods:
% \begin{itemize}
%   \item \verb|\l__socket_assigned_plug:n| thing
%   \begin{itemize}
%     \item If a public interface for retrieving the plug is provided at some point, I will see if I can use that.
%       However, they do not wish it to be expandable. 
%       This is manageable, but it is very nice having an expandable function here, so I will see if I realise, remember and can do it not-too-painfully, I guess.
%   \end{itemize}
%   \item \pkg{latex-lab} variables, keys etc.
%   \begin{itemize}
%     \item This is fairly fragile: patching patches to make them work with patched patches \dots.
%     \item But I guess the \pkg{l3keys} and \pkg{pgfkeys} are public.
%       I'm not sure about the sockets/plugs.
%       Are these supposed to be used by external code?
%     \item The use of \verb|l__tikz_tagging_alt_tl| and \verb|l__tikz_tagging_actualtext_tl| is definitely ungood, but I do not currently see a better way.
%       Hopefully most people will use the \pkg{pgfkeys} interface, as that's much more natural here?
%   \end{itemize}
%   \item \verb|\tex_savepos:D|
%   \begin{itemize}
%     \item This is more-or-less routine and actually documented, so no worries really here?
%   \end{itemize}
%   \item \cs{mmzIncludeExtern}
%   \begin{itemize}
%     \item Documented as internal, certainly not something to redefine, but maybe I can persuade Sašo to add the sockets I need here?
%   \end{itemize}
% \end{itemize}
%
%<*sty>
%<@@=mmzx>
%    \begin{macrocode}
\GetIdInfo $Id: memoize-ext-tag.dtx 11705 2026-02-27 21:46:12Z cfrees $ {Extensions for Memoize for tagged tikz pictures}
%<!debug>    \ProvidesExplPackage{\ExplFileName}{\ExplFileDate}
%<!debug>     {v0.2 \ExplFileVersion}{\ExplFileDescription}
%<debug>    \ProvidesExplPackage{\ExplFileName-debug}{\ExplFileDate}
%<debug>      {v0.2 \ExplFileVersion}{\ExplFileDescription}
%
%<!debug>    \disable@package@load {memoize-ext-tag-debug}
%<debug>    \disable@package@load {memoize-ext-tag}
{ Only~one~of~memoize-ext-tag~and~memoize-ext-tag-debug~
  should~be~loaded.
  Since~
%<!debug>    memoize-ext-tag
%<debug>    memoize-ext-tag-debug
  ~has~been~loaded,~I~will~ignore~your~request~for~
%<debug>    memoize-ext-tag
%<!debug>    memoize-ext-tag-debug
.}
%    \end{macrocode}
%    \begin{macrocode}
%<!debug>    \RequirePackage{memoize-ext}
%<debug>    \RequirePackage{memoize-ext-debug}
%
%    \end{macrocode}
% We don't want inconsistent names in hooks.
%    \begin{macrocode}
\SetDefaultHookLabel{memoize-ext}
%    \end{macrocode}
% \begin{var}{\g_@@_tagpic_int,\l_@@_toks_tl,\l_@@_ok_bool}
% \begin{macro}{\mmzxtagtoks}
%   Tracking \& storage variables.
%    \begin{macrocode}
\bool_new:N \l_@@_ok_bool
\int_new:N \g_@@_tagpic_int
\tl_new:N \l_@@_toks_tl
\newtoks\mmzxtagtoks
%    \end{macrocode}
% \end{macro}
% \end{var}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% \begin{socket}{tagsupport/memoize/include/extern/before,tagsupport/memoize/include/extern/after}
%   New sockets for extern utilisation.
%    \begin{macrocode}
\socket_new:nn {tagsupport/memoize/include/extern/before} {3}
\socket_new:nn {tagsupport/memoize/include/extern/after} {0}
%    \end{macrocode}
% \end{socket}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Ulrike's pgf/tikz keys don't do anything with the arguments they receive? 
% It only seems they do because \texttt{init} passes them also to the l3keys?
%
% And so we have to pass them from tikz to l3keys and back to tikz \dots.
%
% This creates a problem of synchronisation.
% It would be nice to use module-specific names to track \pkg{latex-lab} internal variables for ease of maintenance, but I don't think this is possible.
% If I let a new name to a token list variable, I'll just get the current value. 
% 
% On the one hand, if users use \pkg{pgfkeys} to set the values for \texttt{alt} and \texttt{actualtext}, we can easily avoid \pkg{latex-lab} internals, at least.
% But we do that by introducing additional variables and then have the problem of synchronisation.
%
% On the other hand, we could avoid the synchronisation problems by using \pkg{latex-lab} internals more consistently, but then even the \pkg{pgfkeys} interface will be dependent on the internal implementation\footnote{%
%   I get the prohibition on internals.
%   I really do.
%   But there are cases where it just makes things much more complicated and error-prone.
%   And sometimes it just doesn't not seem worth the additional fragility that introduces, even at the cost of some additional fragility due to the use of internals.%
% }.
% \changes{v0.2}{2026-02-27}{See \texttt{init}.}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%    \begin{macrocode}
\hook_gput_code:nnn {package/tikz/after} {.}
{
  \tikzset{
    alt/.forward~to=/mmz/alt,
    actualtext/.forward~to=/mmz/actualtext,
    artifact/.forward~to=/mmz/artifact,
  }
  \mmzset{
    alt/.code={
      \keys_set:nn {tikz/tagging}{alt={#1}}
      \bool_set_true:N \l_@@_ok_bool
      \exp_args:Ne \mmzxtagtoks { \text_purify:n {#1} }
      \socket_assign_plug:nn 
        {tagsupport/memoize/include/extern/before} {mmzx/alt}
      \socket_assign_plug:nn 
        {tagsupport/memoize/include/extern/after} {mmzx/alt}
%<debug>    \tl_log:e {\exp_args:No \exp_not:n {\the\mmzCCMemo}}
    },
    actualtext/.code={
      \keys_set:nn {tikz/tagging}{actualtext={#1}}
      \bool_set_true:N \l_@@_ok_bool
      \exp_args:Ne \mmzxtagtoks { \text_purify:n {#1} }
      \socket_assign_plug:nn 
        {tagsupport/memoize/include/extern/before} {mmzx/actualtext}
      \socket_assign_plug:nn 
        {tagsupport/memoize/include/extern/after} {mmzx/actualtext}
%<debug>    \tl_log:e {\exp_args:No \exp_not:n {\the\mmzCCMemo}}
    },
    artifact/.code={
      \keys_set:nn {tikz/tagging}{artifact}
      \bool_set_true:N \l_@@_ok_bool
      \mmzxtagtoks {}
      \socket_assign_plug:nn 
        {tagsupport/memoize/include/extern/before} {mmzx/artifact}
      \socket_assign_plug:nn 
        {tagsupport/memoize/include/extern/after} {mmzx/artifact}
%<debug>    \tl_log:e {\exp_args:No \exp_not:n {\the\mmzCCMemo}}
    },
    tagging-setup/.is~choice,
    tagging-setup/alt/.forward~to=/mmz/alt,
    tagging-setup/actualtext/.forward~to=/mmz/actualtext,
    tagging-setup/artifact/.forward~to=/mmz/artifact,
    tagging-setup/.unknown/.code =
    {
      \exp_args:Nno
      \keys_set:nn {tikz/tagging}{\pgfkeyscurrentname={#1}}
    },
    /mmzx/tagging@setup/.is~family,
    alt/.belongs~to~family=/mmzx/tagging@setup,
    actualtext/.belongs~to~family=/mmzx/tagging@setup,
    artifact/.belongs~to~family=/mmzx/tagging@setup,
    tagging-setup/.belongs~to~family=/mmzx/tagging@setup,
    tagging-setup/alt/.belongs~to~family=/mmzx/tagging@setup,
    tagging-setup/actualtext/.belongs~to~family=/mmzx/tagging@setup,
    tagging-setup/artifact/.belongs~to~family=/mmzx/tagging@setup,
  }
}
%    \end{macrocode}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% So it is possible to do without this, but it is \emph{much} more straightforward to do with.
%
% The basic idea is this:
% \begin{itemize}
%   \item At the start of memoization, we redefine the (internal) command \cs{mmzIncludeExtern}.
%   \item In its place, we use simple wrapper around a copy of the original.
%   \item The wrapper defines a socket before and after executing the original.
% \end{itemize}
% This lets us wrap utilisation of the extern in an appropriate tagging structure.
% At least, that's the idea.
%
% It would be nice to assign the plug here just based on whichever plugs are installed, but it is too early, while \cs{mmzAtEndMemoization} is too late.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%    \begin{macrocode}
\appto\mmzAtBeginMemoization{
  \gtoksapp\mmzCCMemo{
    \let\mmzxIncludeExternOrig\mmzIncludeExtern
    \let\mmzIncludeExtern\mmzxIncludeExtern
  }
}
%    \end{macrocode}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% \begin{fn}{\@@_noop:nNnnnnnnn,\@@_include_extern:nNnnnnnnn}
% \begin{macro}{\mmzxIncludeExtern,\mmzxIncludeExternOrig}
%   \cs{mmzIncludeExtern} only seems to be defined during utilisation, so we set up a command \cs{mmzxIncludeExternOrig} and set it to noop here, then redefine it locally when the extern is utilised.
%   \cs{mmzIncludeExtern} is then redefined to \cs{mmzxIncludeExtern}, which wraps \cs{mmzxIncludeExternOrig} in a tagging structure\footnote{%
%   Note to self: check output of tests visually if you do not want documents to be inaccessible to that tiny group of people who rely on vision to read.%
% }.
%
%   Note this not only uses, but redefines an internal command which the manual says users should never need to even use \dots.
%
%   On the other hand, this is exactly the kind of thing \pkg{memoize} does to \emph{other} packages' internals and, indeed, to the \LaTeX{} Project's.
%   Which is more than can be said to defend my use of their internals \dots.
%
%   But does that lessen my guilt? :-)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%    \begin{macrocode}
\cs_new_protected_nopar:Npn 
  \@@_noop:nNnnnnnnn #1#2#3#4#5#6#7#8#9 {}
\cs_new_protected_nopar:Npn \@@_include_extern:nNnnnnnnn #1#2#3#4#5#6#7#8#9
{
  \int_gincr:N \g_@@_tagpic_int
%<debug>    \@@_debug:n {Args:~#1;~#2;~#3;~#4;~#5;~#6;~#7;~#8;~#9}
  \socket_use:nnnn {tagsupport/memoize/include/extern/before}
    {#3}{#4}{#5}
%<debug>    \@@_debug:n {Executing~original~inclusion~macro.}
  \mmzxIncludeExternOrig {#1}#2{#3}{#4}{#5}{#6}{#7}{#8}{#9}
%<debug>    \@@_debug:n {Closing~tagging~structures.}
  \socket_use:n {tagsupport/memoize/include/extern/after}
}
\cs_new_eq:NN \mmzxIncludeExtern \@@_include_extern:nNnnnnnnn
\cs_new_eq:NN \mmzxIncludeExternOrig \@@_noop:nNnnnnnnn
%    \end{macrocode}
% \end{macro}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% \end{fn}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% \begin{plug}{tagsupport/memoize/include/extern/before~mmzx/alt}
% pgf/tikz
% addaswyd o latex-lab-testphase-tika.sty
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%    \begin{macrocode}
\socket_new_plug:nnn {tagsupport/memoize/include/extern/before} {mmzx/alt}
{
%<debug>    \@@_debug:n {Executing~plug~mmzx/alt~in~socket~
%<debug>      tagsupport/memoize/include/extern/before.}
  \mode_if_vertical:T
  {
    \if@inlabel
      \mode_leave_vertical:
    \else
      \tag_socket_use:n {para/begin}
    \fi
  }
  \tag_mc_end_push:
  \tl_set:No \l_@@_toks_tl {\the\mmzxtagtoks}
  \tag_struct_begin:n 
  {
    tag=Figure,
    alt=\l_@@_toks_tl,
  }
  \tag_mc_begin:n {tag=Figure}
  \cs_new:cpe {mmzx@tag@tikz@mark@pos@\int_to_arabic:n {\g_@@_tagpic_int}}
  {
    \@@_pgftikz_tag_bbox:ennn {mmzx-id\int_to_arabic:n {\g_@@_tagpic_int}}
    {#1}{#2}{#3}
  }
%<debug>    \cs_log:c {mmzx@tag@tikz@mark@pos@\int_to_arabic:n 
%<debug>      {\g_@@_tagpic_int}}
  \tag_struct_gput:ene
  {\tag_get:n {struct_num}}
  {attribute}
  {
    /O /Layout /BBox~
    [
      \use:c
      {mmzx@tag@tikz@mark@pos@\int_to_arabic:n {\g_@@_tagpic_int}}
    ]
  }
%<debug>    \@@_debug:e {Recording~xpos,~
%<debug>      ypos~of~mmxc-id\int_to_arabic:n {\g_@@_tagpic_int}.}
  \tex_savepos:D
  \@@_property_record_orig:ee
    {mmzx-id\int_to_arabic:n {\g_@@_tagpic_int}}
    {xpos,ypos}
  \tex_savepos:D
}
%    \end{macrocode}
% \end{plug}
% \begin{plug}{tagsupport/memoize/include/extern/after~mmzx/alt}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% pgf/tikz
% addaswyd o latex-lab-testphase-tika.sty
% bod yn onest, cafodd ei ddwyn o \pkg{latex-lab} yn hollol
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%    \begin{macrocode}
\socket_new_plug:nnn {tagsupport/memoize/include/extern/after} {mmzx/alt}
{
%<debug>    \@@_debug:n {Executing~plug~mmzx/alt~in~socket~
%<debug>      tagsupport/memoize/include/extern/after.}
  \tag_mc_end:
  \tag_struct_end:
  \tag_mc_begin_pop:n {}
}
%    \end{macrocode}
% \end{plug}
% \begin{plug}{%
%   tagsupport/memoize/include/extern/before~mmzx/actualtext,
%   tagsupport/memoize/include/extern/after~mmzx/actualtext,
% }
% addaswyd o latex-lab-testphase-tika.sty
%    \begin{macrocode}
\socket_new_plug:nnn {tagsupport/memoize/include/extern/before} {mmzx/actualtext}
{
%<debug>    \@@_debug:n {Executing~plug~mmzx/actualtext~in~socket~
%<debug>      tagsupport/memoize/include/extern/before.}
  \keys_set:nn {tikz/tagging} {actualtext:o = {\the\mmzxtagtoks},}
  \socket_use:n {tagsupport/tikz/picture/begin}
}
\socket_new_plug:nnn {tagsupport/memoize/include/extern/after} {mmzx/actualtext}
{
%<debug>    \@@_debug:n {Executing~plug~mmzx/actualtext~in~socket~
%<debug>      tagsupport/memoize/include/extern/after.}
  \socket_use:n {tagsupport/tikz/picture/end}
}
%    \end{macrocode}
% \end{plug}
% \begin{plug}{%
%   tagsupport/memoize/include/extern/before~mmzx/artifact,
%   tagsupport/memoize/include/extern/after~mmzx/artifact,
% }
% addaswyd o latex-lab-testphase-tika.sty
%    \begin{macrocode}
\socket_new_plug:nnn {tagsupport/memoize/include/extern/before} {mmzx/artifact}
{
%<debug>    \@@_debug:n {Executing~plug~mmzx/artifact~in~socket~
%<debug>      tagsupport/memoize/include/extern/before.}
  \keys_set:nn {tikz/tagging} {artifact,}
  \socket_use:n {tagsupport/tikz/picture/begin}
}
\socket_new_plug:nnn {tagsupport/memoize/include/extern/after} {mmzx/artifact}
{
%<debug>    \@@_debug:n {Executing~plug~mmzx/artifact~in~socket~
%<debug>      tagsupport/memoize/include/extern/after.}
  \socket_use:n {tagsupport/tikz/picture/end}
}
%    \end{macrocode}
% \end{plug}
% \begin{fn}{\@@_pgftikz_tag_bbox:nnnn,\@@_pgftikz_tag_bbox:ennn}
%   A \pkg{memoize}-friendly alternative to get the bounding box for the \texttt{alt} plug.
%    \begin{macrocode}
\cs_new_nopar:Npn \@@_pgftikz_tag_bbox:nnnn #1#2#3#4
{
  \_@@_pgftikz_tag_bbox_aux:eennn
  {
    \mmzx_property_ref_orig:ee {#1}{xpos}
  }
  {
    \mmzx_property_ref_orig:ee {#1}{ypos}
  }
  {#2}{#3}{#4}
}
\cs_generate_variant:Nn \@@_pgftikz_tag_bbox:nnnn {ennn}
%    \end{macrocode}
% \end{fn}
% \begin{fn}{\@@_pgftikz_tag_bbox_aux:nnnnn,\@@_pgftikz_tag_bbox_aux:eennn}
%   Auxiliary. 
%    \begin{macrocode}
\cs_new_nopar:Npn \@@_pgftikz_tag_bbox_aux:nnnnn #1#2#3#4#5
{
  \dim_to_decimal_in_bp:n {#1sp}
  \c_space_tl
  \dim_to_decimal_in_bp:n {#2sp-#5}
  \c_space_tl
  \dim_to_decimal_in_bp:n {#1sp+#3}
  \c_space_tl
  \dim_to_decimal_in_bp:n {#2sp+#4+#5}
}
\cs_generate_variant:Nn \@@_pgftikz_tag_bbox_aux:nnnnn {eennn}
%    \end{macrocode}
% \end{fn}
%    \begin{macrocode}
\hook_gput_code_with_args:nnn {cmd/tikz@picture/before} {mmzx}
{
  \tag_if_active:T
  {
%<debug>    \@@_debug:n {Executing~mmzx~code~in~hook~cmd/tikz@picture/before.}
    \socket_assign_plug:nn {tagsupport/tikz/picture/init} {mmzx}
  }
}
%    \end{macrocode}
% \begin{plug}{tagsupport/tikz/picture/init~mmzx}
%   Originally, I made something much more complicated, which worked, but meh.
%   Here the idea is that \emph{if we are memoizing, we do not tag at all}.
%   There's no real downside to this: a document which includes code memoized during the current run is not usable anyway and tagging the memoized code is pointless and inefficient.
%   (Actually, so is executing the original code for use during the current run, which is, I believe, how it works. 
%   But that is not my fault.)
%
%   Instead, we tag \emph{only the utilisation of memos}. 
%   We don't need to get the bounding box --- \pkg{memoize} already does that.
%   All we need do is get the position on the page during utilisation.
%   Everything else is for free.
%
%   Note that this code uses multiple format/\pkg{latex-lab} internals.
%   If the support for \pkg{tikz} used exclusively \pkg{pgfkeys}, this would be easily avoided: we could stick to the public interface for all but one of these usages.
%   But because it supports also \pkg{l3keys}, I don't see a way to avoid depending on internal variables there, too. 
%   \pkg{l3keys} does not have a \verb|.forward_to:n| or similar.
%   There is \verb|.inherit:n|, but that does not work at all in the same way.
% \changes{v0.2}{2026-02-27}{Avoid processing non-tagging keys too early and too often.
%   Save and restore existing filter state.%
% }
% Filtering and family code (below) is copied from the code I suggested for \pkg{latex-lab}'s \TikZ{} support (\href{https://github.com/latex3/latex2e/pull/2004}{\#2004}).
%    \begin{macrocode}
\socket_new_plug:nnn {tagsupport/tikz/picture/init} {mmzx}
{
%<debug>    \@@_debug:n {Executing~plug~mmzx~in~socket~
%<debug>      tagsupport/tikz/picture/init.}
  \bool_set_false:N \l_@@_ok_bool
  \legacy_if:nT {memoizing}
  {
%<debug>    \@@_debug:n {Arg~to~tagsupport/tikz/picture/init~is~#1.}
    \pgfkeyssavekeyfilterstateto\mmzx@tagging@setup@saved@filterstate
    \pgfkeysinstallkeyfilter{/pgf/key~filters/active~families}{}
    \pgfqkeysactivatesinglefamilyandfilteroptions{/mmzx/tagging@setup}{/mmz}{#1}
%<debug>    \@@_debug:e { Restoring~filter~state:~\exp_not:V 
%<debug>      \mmzx@tagging@setup@saved@filterstate }
    \mmzx@tagging@setup@saved@filterstate
    \bool_if:NF \l_@@_ok_bool
    {
%<debug>            \@@_debug:n {
%<debug>              No~plug~set~for~utilisation.~Trying~to~match~latex-lab~plug.
%<debug>            }
      \str_case_e:nnF
      {\@@_socket_assigned_plug:n {tagsupport/tikz/picture/begin}}
      {
        {alt} {
          \exp_args:Ne \mmzset{
            alt = \exp_not:V \l__tikz_tagging_alt_tl,
          }
%<debug>            \@@_debug:n {Using~alt~plug.}
        }
        {actualtext} {
          \exp_args:Ne \mmzset{
            actualtext = \exp_not:V \l__tikz_tagging_actualtext_tl,
          }
%<debug>            \@@_debug:n {Using~actualtext~plug.}
        }
        {artifact} {
          \mmzset{artifact,}
%<debug>            \@@_debug:n {Using~artifact~plug.}
        }
      }{
%<debug>            \@@_debug:e {Found~unsupported~
%<debug>              \@@_socket_assigned_plug:n
%<debug>              {tagsupport/tikz/picture/begin}~plug.}
        \PackageWarning{memoize-ext}{Unsupported~tag~config~for~tikz~picture.~
          Please~use~one~of~alt,~actualtext~and~artifact.~
          Note~that~text~(the~latex-lab~default)~is~NOT~supported.~
          Aborting~memoization.
        }
        \mmzAbort
      }
    }
  }
  \bool_if:NTF \l_@@_ok_bool
  {
    \xtoksapp\mmzCCMemo{
      \exp_not:N \mmzxtagtoks
      =
      \c_left_brace_str
      \the\mmzxtagtoks
      \c_right_brace_str
      \exp_not:N \AssignSocketPlug
      \c_left_brace_str
      tagsupport/memoize/include/extern/before
      \c_right_brace_str
      \c_left_brace_str
      \@@_socket_assigned_plug:n {tagsupport/memoize/include/extern/before}
      \c_right_brace_str
      \exp_not:N \AssignSocketPlug
      \c_left_brace_str
      tagsupport/memoize/include/extern/after
      \c_right_brace_str
      \c_left_brace_str
      \@@_socket_assigned_plug:n {tagsupport/memoize/include/extern/after}
      \c_right_brace_str
    }
%<debug>    \@@_debug:n {Disabling~tagging~for~current~tikz~picture~during~
%<debug>      current~run.}
    \socket_assign_plug:nn {tagsupport/tikz/picture/begin} {noop}
    \socket_assign_plug:nn {tagsupport/tikz/picture/end} {noop}
    \socket_assign_plug:nn {tagsupport/tikz/picture/text/begin} {noop}
    \socket_assign_plug:nn {tagsupport/tikz/picture/text/end} {noop}
  } {
%<debug>    \@@_debug:n {Not~memoizing.~Enabling~tagging~for~current~tikz~
%<debug>      picture.}
    \socket_assign_plug:nn {tagsupport/tikz/picture/init} {default}
    \socket_use:nn {tagsupport/tikz/picture/init} {#1}
  }
}
%    \end{macrocode}
% \end{plug}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% pgf/tikz
% addaswyd o latex-lab-testphase-tika.sty
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Hook rules to ensure our substitutes override the format's.
%    \begin{macrocode}
\hook_gset_rule:nnnn {cmd/tikz@picture/before}{latex-lab-testphase-tikz}{>}{.}
\hook_gset_rule:nnnn {cmd/tikz@picture/before}{tagpdf}{>}{.}
\hook_gset_rule:nnnn {cmd/tikz@picture/before}{tikz}{>}{.}
\hook_gset_rule:nnnn {cmd/endpgfpicture/after}{latex-lab-testphase-tikz}{>}{.}
\hook_gset_rule:nnnn {cmd/endpgfpicture/after}{tikz}{>}{.}
\hook_gset_rule:nnnn {cmd/endpgfpicture/after}{tagpdf}{>}{.}
\hook_gset_rule:nnnn {package/tikz/after} {.} {>} {latex-lab-testphase-tikz}
\hook_gset_rule:nnnn {package/tikz/after} {.} {>} {tagpdf}
\hook_gset_rule:nnnn {package/tikz/after} {.} {>} {tikz}
%<debug>       \hook_log:n {package/tikz/after}
\hook_gput_code:nnn {begindocument/end} {.}
{
%<debug>       \hook_log:n {cmd/tikz@picture/before}
%<debug>       \hook_log:n {cmd/endpgfpicture/after}
%    \end{macrocode}
% \begin{fn}{\mmzx_property_ref_orig:nn}
%   Avoid dependency on \pkg{memoize-ext-properties}.
%    \begin{macrocode}
  \cs_if_free:NT \mmzx_property_ref_orig:nn
  {
    \cs_new_eq:NN \mmzx_property_ref_orig:nn \property_ref:nn
    \cs_generate_variant:Nn \mmzx_property_ref_orig:nn {ee}
  }
%    \end{macrocode}
% \end{fn}
% \begin{fn}{\@@_property_record_orig:nn}
%   Avoid dependency on \pkg{memoize-ext-properties}.
%    \begin{macrocode}
  \cs_if_free:NT \@@_property_record_orig:nn
  {
    \cs_new_eq:NN \@@_property_record_orig:nn \property_record:nn
    \cs_generate_variant:Nn \@@_property_record_orig:nn {ee}
  }
%    \end{macrocode}
% \end{fn}
%    \begin{macrocode}
}
%    \end{macrocode}
%</sty>
% 
%\Finale
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
%^^A vim: et:tw=0:sw=2:ts=2:foldmethod=marker:fmr=<<<,>>>:
