| 1 |
% \iffalse meta-comment |
| 2 |
% |
| 3 |
% Copyright (C) 2023-2025 |
| 4 |
% Frank Mittelbach, The LaTeX Project |
| 5 |
% |
| 6 |
% This file is part of the LaTeX base system. |
| 7 |
% ------------------------------------------- |
| 8 |
% |
| 9 |
% It may be distributed and/or modified under the |
| 10 |
% conditions of the LaTeX Project Public License, either version 1.3c |
| 11 |
% of this license or (at your option) any later version. |
| 12 |
% The latest version of this license is in |
| 13 |
% https://www.latex-project.org/lppl.txt |
| 14 |
% and version 1.3c or later is part of all distributions of LaTeX |
| 15 |
% version 2008 or later. |
| 16 |
% |
| 17 |
% This file has the LPPL maintenance status "maintained". |
| 18 |
% |
| 19 |
% The list of all files belonging to the LaTeX base distribution is |
| 20 |
% given in the file `manifest.txt'. See also `legal.txt' for additional |
| 21 |
% information. |
| 22 |
% |
| 23 |
% The list of derived (unpacked) files belonging to the distribution |
| 24 |
% and covered by LPPL is defined by the unpacking scripts (with |
| 25 |
% extension .ins) which are part of the distribution. |
| 26 |
% |
| 27 |
% \fi |
| 28 |
% |
| 29 |
% \iffalse |
| 30 |
% |
| 31 |
%%% From File: ltsockets.dtx |
| 32 |
% |
| 33 |
%<*driver> |
| 34 |
% \fi |
| 35 |
\ProvidesFile{ltsockets.dtx} |
| 36 |
[2024/12/27 v0.9c LaTeX Kernel (Sockets)] |
| 37 |
% \iffalse |
| 38 |
% |
| 39 |
\documentclass{l3doc} |
| 40 |
\GetFileInfo{ltsockets.dtx} |
| 41 |
|
| 42 |
\providecommand\InternalDetectionOff{} |
| 43 |
\providecommand\InternalDetectionOn{} |
| 44 |
|
| 45 |
\EnableCrossrefs |
| 46 |
\CodelineIndex |
| 47 |
\begin{document} |
| 48 |
\DocInput{ltsockets.dtx} |
| 49 |
\end{document} |
| 50 |
%</driver> |
| 51 |
% |
| 52 |
% \fi |
| 53 |
% |
| 54 |
% |
| 55 |
% |
| 56 |
% |
| 57 |
% \title{\LaTeX{}'s socket management\thanks{This module has version |
| 58 |
% \fileversion\ dated \filedate, \copyright\ \LaTeX\ |
| 59 |
% Project.}} |
| 60 |
% |
| 61 |
% \author{Frank Mittelbach} |
| 62 |
% |
| 63 |
% \maketitle |
| 64 |
% |
| 65 |
% ^^A this needs some cleanup: |
| 66 |
% \long\def\fmi#1{\begin{quote} TODO: \itshape #1\end{quote}} |
| 67 |
% |
| 68 |
% \providecommand\pkg[1]{\texttt{#1}} |
| 69 |
% \providecommand\hook[1]{\texttt{#1}} |
| 70 |
% \providecommand\env[1]{\texttt{#1}} |
| 71 |
% \providecommand\plug[1]{\texttt{#1}} |
| 72 |
% \providecommand\socket[1]{\texttt{#1}} |
| 73 |
% |
| 74 |
% |
| 75 |
% \begin{abstract} |
| 76 |
% This code implements sockets which are places in the code into |
| 77 |
% which predeclared chunks of code (plugs) can be placed. Both the sockets |
| 78 |
% and the plugs are \enquote{named} and each socket is |
| 79 |
% assigned exactly one plug at any given time. |
| 80 |
% \end{abstract} |
| 81 |
% |
| 82 |
% \tableofcontents |
| 83 |
% |
| 84 |
% \section{Introduction} |
| 85 |
% |
| 86 |
% A \LaTeX{} source file is transformed into a typeset document by |
| 87 |
% executing code for each command or environment in the document |
| 88 |
% source. Through various steps this code transforms the input and |
| 89 |
% eventually generates typeset output appearing in a \enquote{galley} |
| 90 |
% from which individual pages are cut off in an asynchronous way. This |
| 91 |
% page generating process is normally not directly associated with |
| 92 |
% commands in the input\footnote{Excepts for directives such as |
| 93 |
% \cs{newpage}.} but is triggered whenever the galley has received |
| 94 |
% enough material to form another page (giving current settings). |
| 95 |
% |
| 96 |
% As part of this transformation input data may get stored in some form |
| 97 |
% and later reused, for example, as part of the output routine |
| 98 |
% processing. |
| 99 |
% |
| 100 |
% \section{Configuration of the transformation process} |
| 101 |
% |
| 102 |
% There are three different major methods offered by \LaTeX{} to |
| 103 |
% configure the transformation process: |
| 104 |
% \begin{itemize} |
| 105 |
% \item through the template mechanism, |
| 106 |
% \item through the hook mechanism, or |
| 107 |
% \item through sockets and plugs. |
| 108 |
% \end{itemize} |
| 109 |
% They offer different possibilities (with different features and |
| 110 |
% limitations) and are intended for specific use cases, though it is |
| 111 |
% possible to combine them. |
| 112 |
% |
| 113 |
% \subsection{The template mechanism} |
| 114 |
% |
| 115 |
% The template mechanism is intended for more complex document-level |
| 116 |
% elements (e.g., headings such as \verb=\section= or environments like |
| 117 |
% \env{itemize}). The template code implements the overall processing |
| 118 |
% logic for such an element and offers a set of parameters to influence |
| 119 |
% the final result. |
| 120 |
% |
| 121 |
% The document element is then implemented by (a) selecting a suitable |
| 122 |
% template (there may be more than one available for the kind of |
| 123 |
% document element) and (b) by setting its parameters to desired |
| 124 |
% values. This then forms a so-called instance which is executed when |
| 125 |
% the document element is found in the source. |
| 126 |
% |
| 127 |
% By altering the parameter values (in a document class or in the |
| 128 |
% document preamble) or, if more drastic layout changes are desired, by |
| 129 |
% selecting a different template and then adjusting its parameters, a |
| 130 |
% wide variety of layouts can be realized through simple configuration |
| 131 |
% setups without the need to develop new code. |
| 132 |
% |
| 133 |
% The target audience of this method are therefore document class |
| 134 |
% developers or users who wish to alter an existing layout (implemented |
| 135 |
% by a document class) in certain (minor) ways. |
| 136 |
% |
| 137 |
% The template mechanism is currently documented as part of the |
| 138 |
% \pkg{xtemplate} package and one more elaborate implementation can be |
| 139 |
% found as part of the \texttt{latex-lab} code for lists (to be |
| 140 |
% documented further). |
| 141 |
% |
| 142 |
% \subsection{The hook mechanism} |
| 143 |
% |
| 144 |
% Hooks are places in the kernel code (or in packages) that offer |
| 145 |
% packages the possibility to inject additional code at specific |
| 146 |
% points in the processing in a controlled way without the need to |
| 147 |
% replace the existing code block (and thereby overwriting |
| 148 |
% modifications/extensions made by other packages). The target |
| 149 |
% audience is therefore mainly package developers, even though some |
| 150 |
% hooks can be useful for document authors. |
| 151 |
% |
| 152 |
% Obviously, what can reasonably be added into a hook depends on the |
| 153 |
% individual hook (hopefully documented as part of the hook |
| 154 |
% documentation), but in general the idea behind hooks is that more |
| 155 |
% than one package could add code into the hook at the same |
| 156 |
% time. Perhaps the most famous hook (that \LaTeX{} had for a very |
| 157 |
% long time) is \hook{begindocument} into which many packages add code |
| 158 |
% to through \cs{AtBeginDocument}\marg{code} (which is nowadays |
| 159 |
% implemented as a shorthand for |
| 160 |
% \cs{AddToHook}\texttt{\{\hook{begindocument}\}}\marg{code}). To |
| 161 |
% resolve possible conflicts between injections by different packages |
| 162 |
% there is a rule mechanism by which code chunks in a hook can be |
| 163 |
% ordered in a certain way and by which incompatible packages can be |
| 164 |
% detected if a resolution is impossible. |
| 165 |
% |
| 166 |
% In contrast to template code, there is no standard configuration |
| 167 |
% method through parameters for hooks, i.e., the code added to a hook |
| 168 |
% \enquote{is} the configuration. If it wants to provide for |
| 169 |
% configuration through parameters it has to also provide its own |
| 170 |
% method to set such parameters in some way. However, in that case it |
| 171 |
% is likely that using a hook is not the right approach and the |
| 172 |
% developer better calls a template instance instead which then offers |
| 173 |
% configuration through a key/value interface. |
| 174 |
% |
| 175 |
% In most cases, hooks do not take any arguments as input. Instead, the data |
| 176 |
% that they can (and are allowed to) access depends on the surrounding |
| 177 |
% context. |
| 178 |
% |
| 179 |
% For example, the various hooks available during the page shipout |
| 180 |
% process in \LaTeX's output routine can (and have to) access the |
| 181 |
% accumulated page material stored in a box named |
| 182 |
% \verb=\ShipoutBox=. This way, code added to, say, the |
| 183 |
% \hook{shipout/before} hook could access the page content, alter it, |
| 184 |
% and then write it back into \verb=\ShipoutBox= and any other code |
| 185 |
% added to this hook could then operate on the modified content. Of |
| 186 |
% course, for such a scheme to work the code prior to executing the hook |
| 187 |
% would need to setup up data in appropriate places and the hook |
| 188 |
% documentation would need to document what kind of storage can be |
| 189 |
% accessed (and possibly altered) by the hook. |
| 190 |
% |
| 191 |
% There are also hooks that take arguments (typically portions of |
| 192 |
% document data) and in that case the hook code can access these |
| 193 |
% arguments through \verb=#1=, \verb=#2=, etc. |
| 194 |
% |
| 195 |
% The hook mechanism is documented in \texttt{lthooks-doc.pdf}. |
| 196 |
% |
| 197 |
% |
| 198 |
% |
| 199 |
% \subsection{The socket mechanism} |
| 200 |
% |
| 201 |
% In some cases there is code that implements a certain programming |
| 202 |
% logic (for example, combining footnotes, floats, and the text for the |
| 203 |
% current page to be shipped out) and if this logic should change (e.g., |
| 204 |
% footnotes to be placed above bottom floats instead of below) then this |
| 205 |
% whole code block needs to be replaced with different code. |
| 206 |
% |
| 207 |
% In theory, this could be implemented with templates, i.e., the code |
| 208 |
% simply calls some instance that implements the logic and that instance |
| 209 |
% is altered by selecting a different templates and/or adjusting their |
| 210 |
% parameters. However, in many cases customization through parameters is |
| 211 |
% overkill in such a case (or otherwise awkward, because parameterization |
| 212 |
% is better done on a higher level instead of individually for small |
| 213 |
% blocks of code) and using the template mechanism just to replace one |
| 214 |
% block of code with a different one results in a fairly high |
| 215 |
% performance hit. It is therefore usually not a good choice. |
| 216 |
% |
| 217 |
% In theory, it would also be possible to use a hook, but again that is |
| 218 |
% basically a misuse of the concept, because in this use case there should |
| 219 |
% never be more than one block of code inside the hook; thus, to alter |
| 220 |
% the processing logic one would need to set up rules that replace code |
| 221 |
% rather than (as intended) execute all code added to the hook. |
| 222 |
% |
| 223 |
% |
| 224 |
% |
| 225 |
% For this reason \LaTeX{} now offers a third mechanism: |
| 226 |
% \enquote{sockets} into which one can place exactly one code block |
| 227 |
% --- a \enquote{plug}. |
| 228 |
% |
| 229 |
% In a nutshell: instead of having a fixed code block somewhere as part |
| 230 |
% of the code, implementing a certain programming logic there is a |
| 231 |
% reference to a named socket at this point. |
| 232 |
% This is done by first declaring the named socket with: |
| 233 |
% \begin{quote} |
| 234 |
% \cs{NewSocket}\marg{socket-name}\marg{number-of-inputs} |
| 235 |
% \end{quote} |
| 236 |
% This is then referenced at the point where the replaceable code block |
| 237 |
% should be executed with: |
| 238 |
% \begin{quote} |
| 239 |
% \cs{UseSocket}\marg{socket-name} |
| 240 |
% \end{quote} |
| 241 |
% or, if the socket should take a number of inputs (additional |
| 242 |
% arguments beside the name) with |
| 243 |
% \begin{quote} |
| 244 |
% \cs{UseSocket}\marg{socket-name}\marg{arg\textsubscript{1}}\ldots |
| 245 |
% \marg{arg\textsubscript{\meta{number-of-inputs}}} |
| 246 |
% \end{quote} |
| 247 |
% |
| 248 |
% In addition, several code blocks (a.k.a.\ plugs) implementing different logic for this |
| 249 |
% socket are set up, each with a declaration of the form: |
| 250 |
% \begin{quote} |
| 251 |
% \cs{NewSocketPlug}\marg{socket-name}\marg{socket-plug-name}\marg{code} |
| 252 |
% \end{quote} |
| 253 |
% Finally, |
| 254 |
% one of them is assigned to the socket: |
| 255 |
% \begin{quote} |
| 256 |
% \cs{AssignSocketPlug}\marg{socket-name}\marg{socket-plug-name} |
| 257 |
% \end{quote} |
| 258 |
% If the programming logic should change, then all that is necessary is |
| 259 |
% to make a new assignment with \cs{AssignSocketPlug} to a different |
| 260 |
% \marg{socket-plug-name}. This assignment obeys scope so that an |
| 261 |
% environment can alter a socket without the need to restore the |
| 262 |
% previous setting manually. |
| 263 |
% |
| 264 |
% If the socket takes inputs, then those need to be provided to |
| 265 |
% \cs{UseSocket} and in that case they can be referenced in the \meta{code} |
| 266 |
% argument of \cs{NewSocketPlug} with \verb=#1=, \verb=#2=, etc. |
| 267 |
% |
| 268 |
% In most cases a named socket is used only in a single place, but there |
| 269 |
% is, of course, nothing wrong with using it in several places, as long |
| 270 |
% as the code in all places is supposed to change in the same way. |
| 271 |
% |
| 272 |
% |
| 273 |
% |
| 274 |
% |
| 275 |
% |
| 276 |
% \subsubsection{Examples} |
| 277 |
% |
| 278 |
% |
| 279 |
% We start by declaring a new socket named \socket{foo} that expects |
| 280 |
% two inputs: |
| 281 |
%\begin{verbatim} |
| 282 |
% \NewSocket{foo}{2} |
| 283 |
%\end{verbatim} |
| 284 |
% \NewSocket{foo}{2} |
| 285 |
% |
| 286 |
% Such a declaration has do be unique across the whole \LaTeX{} |
| 287 |
% run. Thus, if another package attempts to use the same name |
| 288 |
% (regardless of the number of inputs) it will generate an error: |
| 289 |
%\begin{verbatim} |
| 290 |
% \NewSocket{foo}{2} |
| 291 |
% \NewSocket{foo}{1} |
| 292 |
% \end{verbatim} |
| 293 |
% Both declarations would therefore produce: |
| 294 |
% \begin{verbatim} |
| 295 |
% ! LaTeX socket Error: Socket 'foo' already declared! |
| 296 |
%\end{verbatim} |
| 297 |
% |
| 298 |
% You also get an error if you attempt to declare some socket plug and |
| 299 |
% the socket name is not yet declared, e.g., |
| 300 |
%\begin{verbatim} |
| 301 |
% \NewSocketPlug{baz}{undeclared}{some code} |
| 302 |
%\end{verbatim} |
| 303 |
% generates |
| 304 |
%\begin{verbatim} |
| 305 |
% ! LaTeX socket Error: Socket 'baz' undeclared! |
| 306 |
%\end{verbatim} |
| 307 |
% |
| 308 |
% |
| 309 |
% Setting up plugs for the socket is done like this: |
| 310 |
%\begin{verbatim} |
| 311 |
% \NewSocketPlug{foo}{plug-A} |
| 312 |
% {\begin{quote}\itshape foo-A: #1!#2\end{quote}} |
| 313 |
% \NewSocketPlug{foo}{plug-B} |
| 314 |
% {\begin{quote}\sffamily foo-B: #2\textsuperscript{2}\end{quote}} |
| 315 |
%\end{verbatim} |
| 316 |
% This will set up the plugs \texttt{plug-A} and \texttt{plug-B} for |
| 317 |
% this socket. |
| 318 |
% |
| 319 |
%\NewSocketPlug{foo}{plug-A}{\begin{quote}\itshape foo-A: #1!#2\end{quote}} |
| 320 |
%\NewSocketPlug{foo}{plug-B}{\begin{quote}\sffamily foo-B: #2\textsuperscript{2}\end{quote}} |
| 321 |
% |
| 322 |
% We still have to assign one or the other to the socket, thus without |
| 323 |
% doing that the line |
| 324 |
% \begin{verbatim} |
| 325 |
% \UseSocket{foo}{hello}{world} |
| 326 |
% \end{verbatim} |
| 327 |
% produces nothing because the default plug for sockets with 2 inputs |
| 328 |
% is \plug{noop} (which grabs the additional arguments and throws them |
| 329 |
% away).\footnote{If socket \socket{foo} would have been a socket with one |
| 330 |
% input, then the default plug would be \plug{identity}, in which case |
| 331 |
% the socket input would remain without braces and gets typeset!} |
| 332 |
% |
| 333 |
% \UseSocket{foo}{hello}{world} ^^A nothing happens |
| 334 |
% |
| 335 |
% So let's do the assignment |
| 336 |
% \begin{verbatim} |
| 337 |
% \AssignSocketPlug{foo}{plug-A} |
| 338 |
% \end{verbatim} |
| 339 |
% and then |
| 340 |
% \begin{verbatim} |
| 341 |
% \UseSocket{foo}{hello}{world} |
| 342 |
% \end{verbatim} |
| 343 |
% will properly typeset |
| 344 |
% \AssignSocketPlug{foo}{plug-A}\UseSocket{foo}{hello}{world} |
| 345 |
% and after |
| 346 |
% \begin{verbatim} |
| 347 |
% \AssignSocketPlug{foo}{plug-B} |
| 348 |
% \end{verbatim} |
| 349 |
% and another call to |
| 350 |
% \begin{verbatim} |
| 351 |
% \UseSocket{foo}{hello}{world} |
| 352 |
% \end{verbatim} |
| 353 |
% we get |
| 354 |
% \AssignSocketPlug{foo}{plug-B}\UseSocket{foo}{hello}{world} |
| 355 |
% |
| 356 |
% If we attempt to assign a plug that was not defined, e.g., |
| 357 |
% \begin{verbatim} |
| 358 |
% \AssignSocketPlug{foo}{plug-C} |
| 359 |
% \end{verbatim} |
| 360 |
% then we get an error during the assignment |
| 361 |
% \begin{verbatim} |
| 362 |
% ! LaTeX socket Error: Plug 'plug-C' for socket 'foo' undeclared! |
| 363 |
% \end{verbatim} |
| 364 |
% and the previous assignment remains in place. |
| 365 |
% |
| 366 |
% To see what is known about a socket and its plugs you can use |
| 367 |
% \cs{ShowSocket} or \cs{LogSocket} which displays information similar |
| 368 |
% to this on the terminal or in the transcript file: |
| 369 |
% \begin{verbatim} |
| 370 |
% Socket foo: |
| 371 |
% number of inputs = 2 |
| 372 |
% available plugs = noop, plug-A, plug-B |
| 373 |
% current plug = plug-B |
| 374 |
% definition = \long macro:#1#2->\begin {quote}\sffamily |
| 375 |
% foo-B: #2\textsuperscript {2}\end {quote} |
| 376 |
% \end{verbatim} |
| 377 |
% \LogSocket{foo} |
| 378 |
% |
| 379 |
% |
| 380 |
% \subsubsection{Details and semantics} |
| 381 |
% |
| 382 |
% In this section we collect some normative statements. |
| 383 |
% |
| 384 |
% \begin{itemize} |
| 385 |
% |
| 386 |
% \item |
| 387 |
% From a functional point of view sockets are like simple \TeX{} macros, |
| 388 |
% i.e., they expect 0 to 9 mandatory arguments (the socket inputs) and get replaced by |
| 389 |
% their \enquote{expansion} |
| 390 |
% |
| 391 |
% \item |
| 392 |
% A socket is \enquote{named} and the name consists of ASCII letters |
| 393 |
% \texttt{[a-z]}, |
| 394 |
% \texttt{[A-Z]}, \texttt{[0-9]}, \texttt{[-/@]} only |
| 395 |
% |
| 396 |
% \item |
| 397 |
% Socket names have to be unique, i.e., there can be only one socket |
| 398 |
% named \meta{name}. This is ensured by declaring each socket with |
| 399 |
% \cs{NewSocket}. |
| 400 |
% |
| 401 |
% However, there is no requirement that sockets and |
| 402 |
% hook names have to be different. In fact, if a certain action that |
| 403 |
% could otherwise be specified as hook code has to be executed always |
| 404 |
% last (or first) one could ensure this by placing a socket (single |
| 405 |
% action) after a hook (or vice versa) and using the same name to |
| 406 |
% indicate the relationship, e.g., |
| 407 |
%\begin{verbatim} |
| 408 |
% \UseHook{foo} % different package can add code here |
| 409 |
% \UseSocket{foo} % only one package can assign a plug |
| 410 |
%\end{verbatim} |
| 411 |
% This avoids the need to order the hook code to ensure that |
| 412 |
% something is always last. |
| 413 |
% |
| 414 |
% \item |
| 415 |
% Best practice naming conventions are \ldots\ \emph{to be documented} |
| 416 |
% |
| 417 |
% \item |
| 418 |
% A socket has documented inputs which are |
| 419 |
% |
| 420 |
% \begin{itemize} |
| 421 |
% \item |
| 422 |
% the positional arguments (if any) with a description of what |
| 423 |
% they contain when used |
| 424 |
% |
| 425 |
% \item |
| 426 |
% implicit data (registers and other 2e/expl3 data stores) that |
| 427 |
% the socket is allowed to make use of, with a documented description |
| 428 |
% of what they contain (if relevant for the task at hand---no need to |
| 429 |
% describe the whole \LaTeX{} universe) |
| 430 |
% |
| 431 |
% \item |
| 432 |
% information about the state of the \TeX{} engine (again when |
| 433 |
% relevant), e.g. is called in mmode or vmode or in the output routine or \ldots |
| 434 |
% |
| 435 |
% \item |
| 436 |
% \ldots\ \empty{anything missing?} |
| 437 |
% \end{itemize} |
| 438 |
% |
| 439 |
% |
| 440 |
% \item |
| 441 |
% A socket has documented results/outputs which can be |
| 442 |
% |
| 443 |
% \begin{itemize} |
| 444 |
% \item |
| 445 |
% what kind of data it should write to the current list (if that |
| 446 |
% is part of its task) |
| 447 |
% |
| 448 |
% \item |
| 449 |
% what kind of registers and other 2e/expl3 data stores it should |
| 450 |
% modify and in what way |
| 451 |
% |
| 452 |
% \item |
| 453 |
% what kind of state changes it should do (if any) |
| 454 |
% |
| 455 |
% \item |
| 456 |
% \emph{\ldots\ anything else?} |
| 457 |
% \end{itemize} |
| 458 |
% |
| 459 |
% \item |
| 460 |
% |
| 461 |
% At any time a socket has one block of code (a plug \iffalse(\fi:-)\,) |
| 462 |
% associated with it. Such code is itself named and the association |
| 463 |
% is done by linking the socket name to the code name (putting a |
| 464 |
% plug into the socket). |
| 465 |
% |
| 466 |
% \item |
| 467 |
% |
| 468 |
% The name of a plug consists of ASCII letters \texttt{[a-z]}, |
| 469 |
% \texttt{[A-Z]}, \texttt{[0-9]}, \texttt{[-/@]} only. |
| 470 |
% |
| 471 |
% \item |
| 472 |
% Socket plug names have to be unique within on a per socket basis, |
| 473 |
% but it is perfectly allowed (and sensible in some cases) to use |
| 474 |
% the same plug name with different sockets (where based on the |
| 475 |
% sockets' purposes, different actions may be associated with the plug |
| 476 |
% name). For example \plug{noop} is a plug name declared for every |
| 477 |
% socket, yet it action \enquote{grab the socket inputs and throw |
| 478 |
% them away} obviously differs depending on how many inputs the |
| 479 |
% socket has. |
| 480 |
% |
| 481 |
% \item |
| 482 |
% |
| 483 |
% When declaring a plug it is stated for which socket it is meant |
| 484 |
% (i.e., its code can only be used with that socket). This means |
| 485 |
% that the same plug name can be used with different sockets |
| 486 |
% referring to different code in each case. |
| 487 |
% |
| 488 |
% \item |
| 489 |
% Configuration of a socket can only be done by |
| 490 |
% linking different code to it. Nevertheless the code linked to it can |
| 491 |
% provide its own means of configuration (but this is outside of the |
| 492 |
% spec). |
| 493 |
% |
| 494 |
% \item |
| 495 |
% Technically execution of a socket (\cs{UseSocket}) involves |
| 496 |
% |
| 497 |
% \begin{itemize} |
| 498 |
% \item |
| 499 |
% doing any house keeping (like writing debugging info, \ldots); |
| 500 |
% |
| 501 |
% \item |
| 502 |
% looking up the current code association (what plug is in the socket); |
| 503 |
% |
| 504 |
% \item |
| 505 |
% executing this code which will pick up the mandatory arguments |
| 506 |
% (happens at this point, not |
| 507 |
% before), i.e., it is like calling a csname defined with |
| 508 |
% \begin{verbatim} |
| 509 |
% \def\foo#1#2...{...#1...#2...} |
| 510 |
% \end{verbatim} |
| 511 |
% |
| 512 |
% \item |
| 513 |
% do some further house keeping (if needed). |
| 514 |
% \end{itemize} |
| 515 |
% |
| 516 |
% \item |
| 517 |
% A socket is typically only used in one place in code, but this is not |
| 518 |
% a requirement, i.e., if the same operation with the same inputs need |
| 519 |
% to be carried out in several places the same named socket can be used. |
| 520 |
% |
| 521 |
% \end{itemize} |
| 522 |
% |
| 523 |
% |
| 524 |
% |
| 525 |
% |
| 526 |
% |
| 527 |
% \subsubsection{Command syntax} |
| 528 |
% |
| 529 |
% We give both the \LaTeXe{} and the L3 programming layer command names. |
| 530 |
% |
| 531 |
% \begin{function}{\NewSocket,\socket_new:nn} |
| 532 |
% \begin{syntax} |
| 533 |
% \cs{NewSocket} \Arg{socket-name} \Arg{number-of-inputs} |
| 534 |
% \cs{socket_new:nn} \Arg{socket-name} \Arg{number-of-inputs} |
| 535 |
% \end{syntax} |
| 536 |
% Declares a new socket with name \meta{socket-name} having |
| 537 |
% \meta{number-of-inputs} inputs. There is automatically a |
| 538 |
% plug \plug{noop} declared for it, which does nothing, i.e., it gobbles |
| 539 |
% the socket inputs (if any). This is made the default plug except for |
| 540 |
% sockets with one input which additionally define the plug |
| 541 |
% \plug{identity} and assign that as their default. |
| 542 |
% |
| 543 |
% This \plug{identity} plug simply returns the socket input without |
| 544 |
% its outer braces. |
| 545 |
% The use case for this plug are situations like this: |
| 546 |
% \begin{quote} |
| 547 |
% \cs{UseSocket}\verb={tagsupport/footnote}=\Arg{code} |
| 548 |
% \end{quote} |
| 549 |
% If tagging is not active and the socket contains the plug \plug{identity} |
| 550 |
% then this returns \meta{code} without the outer braces and to |
| 551 |
% activate tagging all that is necessary is to change the plug to |
| 552 |
% say \plug{tagpdf} so |
| 553 |
% that it surrounds \meta{code} by some tagging magic. |
| 554 |
% This is the most common use case for sockets with one input, which |
| 555 |
% is why they have this special default. |
| 556 |
% |
| 557 |
% The socket documentation should describe its purpose, its inputs and the |
| 558 |
% expected results as discussed above. |
| 559 |
% |
| 560 |
% The declaration is only allowed at top-level, i.e., not inside a group. |
| 561 |
% \end{function} |
| 562 |
% |
| 563 |
% |
| 564 |
% \begin{function}{\NewSocketPlug,\socket_new_plug:nnn,\socket_set_plug:nnn} |
| 565 |
% \begin{syntax} |
| 566 |
% \cs{NewSocketPlug} \Arg{socket-name} \Arg{socket-plug-name} \Arg{code} |
| 567 |
% \cs{socket_new_plug:nnn} \Arg{socket-name} \Arg{socket-plug-name} \Arg{code} |
| 568 |
% \cs{socket_set_plug:nnn} \Arg{socket-name} \Arg{socket-plug-name} \Arg{code} |
| 569 |
% \end{syntax} |
| 570 |
% Declares a new plug for socket \meta{socket-name} that runs |
| 571 |
% \meta{code} when executing. It complains if the plug was already |
| 572 |
% declared previously. |
| 573 |
% |
| 574 |
% The form \cs{socket_set_plug:nnn} changes an existing plug. As |
| 575 |
% this should normally not be necessary, we currently have only an L3 |
| 576 |
% layer name for the few cases it might be useful. |
| 577 |
% |
| 578 |
% The declarations can be made inside a group and obey scope, i.e., |
| 579 |
% they vanish if the group ends. |
| 580 |
% \end{function} |
| 581 |
% |
| 582 |
% |
| 583 |
% \begin{function}{\AssignSocketPlug,\socket_assign_plug:nn} |
| 584 |
% \begin{syntax} |
| 585 |
% \cs{AssignSocketPlug} \Arg{socket-name} \Arg{socket-plug-name} |
| 586 |
% \cs{socket_assign_plug:nn} \Arg{socket-name} \Arg{socket-plug-name} |
| 587 |
% \end{syntax} |
| 588 |
% Assigns the plug \meta{socket-plug-name} to the socket |
| 589 |
% \meta{socket-name}. It errors if either socket or plug is not |
| 590 |
% defined. |
| 591 |
% |
| 592 |
% The assignment is local, i.e., it obeys scope. |
| 593 |
% \end{function} |
| 594 |
% |
| 595 |
% |
| 596 |
% \begin{function}{\UseSocket,\socket_use:nw,\socket_use:n,\socket_use:nn,\socket_use:nnn,\socket_use:nnnn} |
| 597 |
% \begin{syntax} |
| 598 |
% \cs{UseSocket} \Arg{socket-name} |
| 599 |
% \cs{socket_use:nnn} \Arg{socket-name} \Arg{socket-arg\textsubscript{1}} \Arg{socket-arg\textsubscript{2}} |
| 600 |
% \end{syntax} |
| 601 |
% Executes the socket \meta{socket-name} by retrieving the |
| 602 |
% \meta{code} of the current plug assigned to the socket. This is |
| 603 |
% the only command that would appear inside macro code in packages. |
| 604 |
% |
| 605 |
% For performance reasons there is no explicit check that the socket |
| 606 |
% was declared! |
| 607 |
% |
| 608 |
% The different L3 programming layer commands are really doing the |
| 609 |
% same thing: they grab as many arguments as defined as inputs for the socket |
| 610 |
% and then pass them to the plug. The different names are only there |
| 611 |
% to make the code more readable, i.e., to indicate how many |
| 612 |
% arguments are grabbed in total (note that no runtime check is made to |
| 613 |
% verify that this is actually true). We only provide them for |
| 614 |
% sockets with up to 3 inputs (most likely those with zero or one input would |
| 615 |
% have been sufficient). If you happen to have a socket with more |
| 616 |
% inputs, use \cs{socket_use:nw}. |
| 617 |
% \end{function} |
| 618 |
% |
| 619 |
% |
| 620 |
% \begin{function}[EXP]{\socket_use_expandable:nw,\socket_use_expandable:n} |
| 621 |
% \begin{syntax} |
| 622 |
% \cs{socket_use_expandable:n} \Arg{socket-name} |
| 623 |
% \end{syntax} |
| 624 |
% Fully expandable variant of \cs{socket_use:n}. This can be used in macro code |
| 625 |
% to retrieve code from sockets which need to appear in an expandable context. |
| 626 |
% |
| 627 |
% This usually requires the plug to only contain expandable code and should therefore |
| 628 |
% only be used for sockets which are clearly documented to be used in an expandable context. |
| 629 |
% This command does not print any debugging info when \cs{DebugSocketsOn} is active |
| 630 |
% and should therefore be avoided whenever possible. |
| 631 |
% |
| 632 |
% For performance reasons there is no explicit check that the socket was declared! |
| 633 |
% \end{function} |
| 634 |
% |
| 635 |
% |
| 636 |
% \begin{function}{\ShowSocket,\LogSocket,\socket_show:n,\socket_log:n} |
| 637 |
% \begin{syntax} |
| 638 |
% \cs{ShowSocket} \Arg{socket-name} |
| 639 |
% \cs{socket_show:n} \Arg{socket-name} |
| 640 |
% \end{syntax} |
| 641 |
% Displays information about the socket \meta{socket-name} and its |
| 642 |
% state then stops and waits for further instructions --- at the |
| 643 |
% moment some what rudimentary. |
| 644 |
% |
| 645 |
% \cs{LogSocket} and \cs{socket_log:n} only differ in that they don't stop. |
| 646 |
% \end{function} |
| 647 |
% |
| 648 |
% |
| 649 |
% It is sometimes necessary/helpful to know if a particular socket or |
| 650 |
% plug exists (or is assigned to a certain socket) and based on that |
| 651 |
% take different actions. |
| 652 |
% |
| 653 |
% \begin{function}[EXP]{\IfSocketExistsTF,\socket_if_exist:nTF} |
| 654 |
% \begin{syntax} |
| 655 |
% \cs{IfSocketExistsTF} \Arg{socket-name} \Arg{true code} \Arg{false code} |
| 656 |
% \end{syntax} |
| 657 |
% If socket \meta{socket-name} exists then execute \meta{true code} |
| 658 |
% otherwise \meta{false code}. Variants with only \texttt{T} or |
| 659 |
% \texttt{F} are also available. |
| 660 |
% \end{function} |
| 661 |
% |
| 662 |
% \begin{function}[EXP]{\IfSocketPlugExistsTF,\socket_if_plug_exist:nnTF} |
| 663 |
% \begin{syntax} |
| 664 |
% \cs{IfSocketPlugExistsTF} \Arg{socket-name} \Arg{plug-name} |
| 665 |
% \qquad \Arg{true code} \Arg{false code} |
| 666 |
% \end{syntax} |
| 667 |
% If plug \meta{plug-name} for socket \meta{socket-name} exists then |
| 668 |
% execute \meta{true code} otherwise \meta{false code}. Variants |
| 669 |
% with only \texttt{T} or \texttt{F} are also available. |
| 670 |
% \end{function} |
| 671 |
% |
| 672 |
% \begin{function}[EXP]{\IfSocketPlugAssignedTF,\socket_if_plug_assigned:nnTF} |
| 673 |
% \begin{syntax} |
| 674 |
% \cs{IfSocketPlugAssignedTF} \Arg{socket-name} \Arg{plug-name} |
| 675 |
% \qquad \Arg{true code} \Arg{false code} |
| 676 |
% \end{syntax} |
| 677 |
% If plug \meta{plug-name} is assigned to socket \meta{socket-name} |
| 678 |
% then execute \meta{true code} otherwise \meta{false |
| 679 |
% code}. Variants with only \texttt{T} or \texttt{F} are also |
| 680 |
% available. |
| 681 |
% \end{function} |
| 682 |
% |
| 683 |
% |
| 684 |
% |
| 685 |
% |
| 686 |
% \begin{function}{\DebugSocketsOn,\DebugSocketsOff, |
| 687 |
% \socket_debug_on:,\socket_debug_off:} |
| 688 |
% \begin{syntax} |
| 689 |
% \cs{DebugSocketsOn} \ldots\ \cs{DebugSocketsOff} |
| 690 |
% \end{syntax} |
| 691 |
% Turns debugging of sockets on or off. |
| 692 |
% \end{function} |
| 693 |
% |
| 694 |
% |
| 695 |
% |
| 696 |
% \subsubsection{Rationale for error handling} |
| 697 |
% |
| 698 |
% The errors during the declarations are produced to help with |
| 699 |
% typos---after all, such declarations might be part of a document |
| 700 |
% preamble (not that likely, but possible). However, \cs{UseSocket} is |
| 701 |
% not doing much checking, e.g., |
| 702 |
% \begin{verbatim} |
| 703 |
% \UseSocket{mispelled-socket}{hello}{world} |
| 704 |
% \end{verbatim} |
| 705 |
% will generate a rather low-level error and then typesets |
| 706 |
% ``{hello}{world}'' because there is no dedicated runtime check if |
| 707 |
% \texttt{mispelled-socket} is a known socket. |
| 708 |
% |
| 709 |
% The reason is that if the misspelling is in the code, then this is a |
| 710 |
% programming error in the package and for speed reasons \LaTeX{} does |
| 711 |
% not repeately make runtime checks for coding errors unless they can or |
| 712 |
% are likely to be user introduced. |
| 713 |
% |
| 714 |
% |
| 715 |
% \MaybeStop{\setlength\IndexMin{200pt} \PrintIndex } |
| 716 |
% |
| 717 |
% |
| 718 |
% \section{The Implementation} |
| 719 |
% |
| 720 |
% The implementation of the socket mechanism should be (partially) |
| 721 |
% redone and we should probably store the different code chunks in |
| 722 |
% a property list so that we can have a decent \cs{ShowSocket} |
| 723 |
% command that shows the available alternatives.\fmi{implement?} |
| 724 |
% |
| 725 |
% \begin{macrocode} |
| 726 |
%<*2ekernel|latexrelease> |
| 727 |
% \end{macrocode} |
| 728 |
% |
| 729 |
% \begin{macrocode} |
| 730 |
\ExplSyntaxOn |
| 731 |
% \end{macrocode} |
| 732 |
% |
| 733 |
% \begin{macrocode} |
| 734 |
%<@@=socket> |
| 735 |
% \end{macrocode} |
| 736 |
% |
| 737 |
% \begin{macrocode} |
| 738 |
%<latexrelease>\NewModuleRelease{2023/11/01}{ltsockets} |
| 739 |
%<latexrelease> {The~socket~management~system} |
| 740 |
% \end{macrocode} |
| 741 |
% |
| 742 |
% |
| 743 |
% |
| 744 |
% \subsection{Debugging the socket structures} |
| 745 |
% |
| 746 |
% Code and commands in this section are not final, it needs more |
| 747 |
% experimentation to see what kind of tracing information is going to |
| 748 |
% be useful in practice. For now the tracing is mainly meant to be used |
| 749 |
% for code testing and not so much for application testing. |
| 750 |
% |
| 751 |
% It is quite likely that the |
| 752 |
% commands and the behavior of the tracing might change in the |
| 753 |
% future once we gained some experience with it. |
| 754 |
% |
| 755 |
% \begin{macro}{\g_@@_debug_bool} |
| 756 |
% Holds the current debugging state. |
| 757 |
% \begin{macrocode} |
| 758 |
\bool_new:N \g_@@_debug_bool |
| 759 |
% \end{macrocode} |
| 760 |
% \end{macro} |
| 761 |
% |
| 762 |
% \begin{macro}{\socket_debug_on:,\socket_debug_off:} |
| 763 |
% \begin{macro}{\@@_debug:n, \@@_debug_term:n} |
| 764 |
% \begin{macro}{\@@_debug_gset:} |
| 765 |
% Turns debugging on and off by redefining \cs{@@_debug:n} and |
| 766 |
% \cs{@@_debug_term:n}. By default they do nothing. |
| 767 |
% \begin{macrocode} |
| 768 |
\cs_new_eq:NN \@@_debug:n \use_none:n |
| 769 |
\cs_new_eq:NN \@@_debug_term:n \use_none:n |
| 770 |
% \end{macrocode} |
| 771 |
% |
| 772 |
% \begin{macrocode} |
| 773 |
\cs_new_protected:Npn \socket_debug_on: |
| 774 |
{ |
| 775 |
\bool_gset_true:N \g_@@_debug_bool |
| 776 |
\@@_debug_gset: |
| 777 |
} |
| 778 |
\cs_new_protected:Npn \socket_debug_off: |
| 779 |
{ |
| 780 |
\bool_gset_false:N \g_@@_debug_bool |
| 781 |
\@@_debug_gset: |
| 782 |
} |
| 783 |
\cs_new_protected:Npn \@@_debug_gset: |
| 784 |
{ |
| 785 |
\cs_gset_protected:Npx \@@_debug:n ##1 |
| 786 |
{ \bool_if:NT \g_@@_debug_bool {##1} } |
| 787 |
\cs_gset_protected:Npx \@@_debug_term:n ##1 |
| 788 |
{ \bool_if:NT \g_@@_debug_bool |
| 789 |
{ \iow_term:x { ^^J [Sockets]~ ==>~ ##1} } } |
| 790 |
} |
| 791 |
% \end{macrocode} |
| 792 |
% \end{macro} |
| 793 |
% \end{macro} |
| 794 |
% \end{macro} |
| 795 |
% |
| 796 |
% |
| 797 |
% \subsection{The L3 layer commands} |
| 798 |
% |
| 799 |
% \begin{macro}{\socket_new:nn} |
| 800 |
% |
| 801 |
% Declaring a socket creates a str to hold the name (a pointer) to the |
| 802 |
% code that should be used when the socket is executed, and an integer to |
| 803 |
% hold the number of inputs of that socket. Initially, an |
| 804 |
% ``empty'' code chunk is created and assigned so the socket |
| 805 |
% does nothing by default other than swallowing its inputs (if any). |
| 806 |
% \begin{macrocode} |
| 807 |
\cs_new_protected:Npn \socket_new:nn #1 #2 { |
| 808 |
\socket_if_exist:nTF {#1} |
| 809 |
{ |
| 810 |
\msg_error:nnn { socket } { already-declared } {#1} |
| 811 |
} |
| 812 |
{ |
| 813 |
% \end{macrocode} |
| 814 |
% We only support declarations on top-level. |
| 815 |
% \begin{macrocode} |
| 816 |
\int_if_zero:nTF \tex_currentgrouplevel:D |
| 817 |
{ |
| 818 |
\str_new:c { l_@@_#1_plug_str } |
| 819 |
\seq_new:c { l_@@_#1_plugs_seq } |
| 820 |
\int_const:cn { c_@@_#1_args_int } {#2} |
| 821 |
\socket_new_plug:nnn {#1} { noop } {} |
| 822 |
\int_compare:nNnTF {#2} = 1 |
| 823 |
{ |
| 824 |
\socket_new_plug:nnn {#1} { identity } {##1} |
| 825 |
\socket_assign_plug:nn {#1} { identity } |
| 826 |
} |
| 827 |
{ \socket_assign_plug:nn {#1} { noop } } |
| 828 |
\@@_debug_term:n |
| 829 |
{ Socket~ '#1'~ declared~ with~ #2~ input(s) } |
| 830 |
} |
| 831 |
{ |
| 832 |
\msg_error:nn { socket } { not-top-level } |
| 833 |
} |
| 834 |
} |
| 835 |
} |
| 836 |
% \end{macrocode} |
| 837 |
% \end{macro} |
| 838 |
% |
| 839 |
% |
| 840 |
% |
| 841 |
% \begin{macro}[pTF, EXP] { \socket_if_exist:n } |
| 842 |
% Conditional testing the existance of a socket. |
| 843 |
% The argument is fully expanded as part of the csname generation. |
| 844 |
% \changes{v0.9c}{2024/12/27}{Conditionals for sockets, plugs, and |
| 845 |
% assignments (gh/1577)} |
| 846 |
% \begin{macrocode} |
| 847 |
\prg_new_conditional:Npnn \socket_if_exist:n #1 { p , T , F , TF } |
| 848 |
{ \str_if_exist:cTF { l_@@_#1_plug_str } |
| 849 |
\prg_return_true: |
| 850 |
\prg_return_false: |
| 851 |
} |
| 852 |
% \end{macrocode} |
| 853 |
% \end{macro} |
| 854 |
% |
| 855 |
% |
| 856 |
% \begin{macro}{\socket_log:n,\socket_show:n} |
| 857 |
% Show the current state of the socket --- for now this is just a |
| 858 |
% quick draft and should be redone and extended. |
| 859 |
% \begin{macrocode} |
| 860 |
\cs_new_protected:Npn \socket_log:n #1 { |
| 861 |
\typeout{ Socket~ #1:} |
| 862 |
\socket_if_exist:nTF {#1} |
| 863 |
{ |
| 864 |
\typeout{ \@spaces number~ of~ inputs~ =~ |
| 865 |
\int_use:c { c_@@_#1_args_int } } |
| 866 |
\typeout{ \@spaces available~plugs~ =~ |
| 867 |
\seq_use:cnnn { l_@@_#1_plugs_seq }{,~}{,~}{,~} } |
| 868 |
\typeout{ \@spaces current~ plug~ =~ |
| 869 |
\str_use:c { l_@@_#1_plug_str } } |
| 870 |
\typeout{ \@spaces definition~ =~ |
| 871 |
\cs_meaning:c |
| 872 |
{ @@_#1_plug_ \str_use:c { l_@@_#1_plug_str } :w } } |
| 873 |
\typeout{} |
| 874 |
} |
| 875 |
{ |
| 876 |
% \end{macrocode} |
| 877 |
% If we are showing a socket it is not an error if it doesn't exist. |
| 878 |
% \begin{macrocode} |
| 879 |
\typeout { Socket~ is~ not~ declared! } |
| 880 |
} |
| 881 |
} |
| 882 |
% \end{macrocode} |
| 883 |
% And here the version that stops: |
| 884 |
% \begin{macrocode} |
| 885 |
\cs_new_protected:Npn \socket_show:n #1 {\socket_log:n {#1} \errmessage{}} |
| 886 |
% \end{macrocode} |
| 887 |
% \end{macro} |
| 888 |
% |
| 889 |
% |
| 890 |
% |
| 891 |
% |
| 892 |
% |
| 893 |
% \begin{macro}{\socket_new_plug:nnn,\socket_set_plug:nnn} |
| 894 |
% \changes{v0.9b}{2024/10/27}{Make plug definition non-protected} |
| 895 |
% |
| 896 |
% Declaring a code for a socket is just making a definition, taking |
| 897 |
% the number of arguments from the saved int. |
| 898 |
% \begin{macrocode} |
| 899 |
\cs_new_protected:Npn \socket_new_plug:nnn #1#2#3 { |
| 900 |
\socket_if_exist:nTF {#1} |
| 901 |
{ |
| 902 |
\socket_if_plug_exist:nnTF {#1} {#2} |
| 903 |
{ |
| 904 |
\msg_error:nnnn { socket } { plug-already-declared } {#1} {#2} |
| 905 |
} |
| 906 |
{ |
| 907 |
\cs_generate_from_arg_count:cNnn |
| 908 |
{ @@_#1_plug_#2:w } |
| 909 |
\cs_new:Npn |
| 910 |
{ \int_use:c { c_@@_#1_args_int } } |
| 911 |
{#3} |
| 912 |
% \end{macrocode} |
| 913 |
% This is a new declaration so we add the name to a seq for the |
| 914 |
% debugging info. |
| 915 |
% \begin{macrocode} |
| 916 |
\seq_put_right:cn { l_@@_#1_plugs_seq } {#2} |
| 917 |
\@@_debug_term:n |
| 918 |
{ Plug~ '#2'~ for~ socket~ '#1'~ declared. } |
| 919 |
} |
| 920 |
} |
| 921 |
{ \msg_error:nnn { socket } { undeclared } {#1} } |
| 922 |
} |
| 923 |
% \end{macrocode} |
| 924 |
% Changing the plug of an existing socket is rather similar, except |
| 925 |
% that we don't have to deal with adding it to the debugging |
| 926 |
% sequence. |
| 927 |
% \begin{macrocode} |
| 928 |
\cs_new_protected:Npn \socket_set_plug:nnn #1#2#3 { |
| 929 |
\socket_if_exist:nTF {#1} |
| 930 |
{ |
| 931 |
\socket_if_plug_exist:nnTF {#1} {#2} |
| 932 |
{ |
| 933 |
\cs_generate_from_arg_count:cNnn |
| 934 |
{ @@_#1_plug_#2:w } |
| 935 |
\cs_set:Npn |
| 936 |
{ \int_use:c { c_@@_#1_args_int } } |
| 937 |
{#3} |
| 938 |
\@@_debug_term:n |
| 939 |
{ Plug~ '#2'~ for~ socket~ '#1'~ changed. } |
| 940 |
} |
| 941 |
{ |
| 942 |
\msg_error:nnnn { socket } { plug-undeclared } {#1} {#2} |
| 943 |
} |
| 944 |
} |
| 945 |
{ \msg_error:nnn { socket } { undeclared } {#1} } |
| 946 |
} |
| 947 |
% \end{macrocode} |
| 948 |
% \end{macro} |
| 949 |
% |
| 950 |
% |
| 951 |
% |
| 952 |
% \begin{macro}[pTF, EXP] { \socket_if_plug_exist:nn } |
| 953 |
% Conditional testing the existance of a plug. |
| 954 |
% Both arguments are fully expanded as part of the csname generation. |
| 955 |
% \changes{v0.9c}{2024/12/27}{Conditionals for sockets, plugs, and |
| 956 |
% assignments (gh/1577)} |
| 957 |
% \begin{macrocode} |
| 958 |
\prg_new_conditional:Npnn \socket_if_plug_exist:nn #1#2 { p , T , F , TF } |
| 959 |
{ \cs_if_exist:cTF { @@_#1_plug_#2:w } |
| 960 |
\prg_return_true: |
| 961 |
\prg_return_false: |
| 962 |
} |
| 963 |
% \end{macrocode} |
| 964 |
% \end{macro} |
| 965 |
% |
| 966 |
% |
| 967 |
% |
| 968 |
% \begin{macro}{\socket_assign_plug:nn} |
| 969 |
% |
| 970 |
% Assigning a plug to a socket just changes the name in |
| 971 |
% the socket string. The assignment is local to the current group. |
| 972 |
% \begin{macrocode} |
| 973 |
\cs_new_protected:Npn \socket_assign_plug:nn #1 #2 { |
| 974 |
\socket_if_exist:nTF {#1} |
| 975 |
{ |
| 976 |
\socket_if_plug_exist:nnTF {#1} {#2} |
| 977 |
{ |
| 978 |
\@@_debug_term:n |
| 979 |
{ Replacing~ plug~ '\str_use:c { l_@@_#1_plug_str }'~ |
| 980 |
with~ '#2'~ in~ socket~ '#1'. } |
| 981 |
\str_set:cn { l_@@_#1_plug_str } {#2} |
| 982 |
} |
| 983 |
{ |
| 984 |
\msg_error:nnnn { socket } { plug-undeclared } {#1} {#2} |
| 985 |
} |
| 986 |
} |
| 987 |
{ \msg_error:nnn { socket } { undeclared } {#1} } |
| 988 |
} |
| 989 |
% \end{macrocode} |
| 990 |
% \end{macro} |
| 991 |
% |
| 992 |
% |
| 993 |
% |
| 994 |
% \begin{macro}[pTF, EXP] { \socket_if_plug_assigned:nn } |
| 995 |
% Conditional testing the assignment of a plug. |
| 996 |
% Both arguments are fully expanded. |
| 997 |
% \changes{v0.9c}{2024/12/27}{Conditionals for sockets, plugs, and |
| 998 |
% assignments (gh/1577)} |
| 999 |
% \begin{macrocode} |
| 1000 |
\prg_new_conditional:Npnn \socket_if_plug_assigned:nn #1#2 { p , T , F , TF } |
| 1001 |
{ \exp_args:Ne |
| 1002 |
\str_if_eq:nvTF {#2} { l__socket_#1_plug_str } |
| 1003 |
\prg_return_true: |
| 1004 |
\prg_return_false: |
| 1005 |
} |
| 1006 |
% \end{macrocode} |
| 1007 |
% \end{macro} |
| 1008 |
% |
| 1009 |
% |
| 1010 |
% |
| 1011 |
% \begin{macro}{\socket_use:nw,\socket_use:n,\socket_use:nn,\socket_use:nnn,\socket_use:nnnn} |
| 1012 |
% |
| 1013 |
% And using it is more or less a \cs{use:c} so very lightweight. We do not |
| 1014 |
% add a runtime check for speed reasons! |
| 1015 |
% |
| 1016 |
% This command is named |
| 1017 |
% \cs{socket_use:nw} because we don't know how many inputs the |
| 1018 |
% socket has until we have looked at the socket name (in argument \verb=#1=). |
| 1019 |
% But, of course, the developer knows so we also offer a few aliases |
| 1020 |
% \cs{socket_use:nn}, etc.\ so that one can indicate the correct number of |
| 1021 |
% arguments (socket inputs plus one) in the L3 layer code. |
| 1022 |
% |
| 1023 |
% \begin{macrocode} |
| 1024 |
\cs_new_protected:Npn \socket_use:nw #1 { |
| 1025 |
\@@_debug_term:n |
| 1026 |
{ Socket~ '#1'~ containing~ plug~ |
| 1027 |
'\str_use:c { l_@@_#1_plug_str }'~ used. } |
| 1028 |
\use:c { @@_#1_plug_ \str_use:c { l_@@_#1_plug_str } :w } |
| 1029 |
} |
| 1030 |
% \end{macrocode} |
| 1031 |
% To make code a bit more readable we also define functions that |
| 1032 |
% indicate how many arguments are picked up. However, this is just for |
| 1033 |
% code documentation: internally they all do the same and the number |
| 1034 |
% of arguments isn't checked by default. |
| 1035 |
% \begin{macrocode} |
| 1036 |
\cs_new_eq:NN \socket_use:n \socket_use:nw % socket with no inputs |
| 1037 |
\cs_new_eq:NN \socket_use:nn \socket_use:nw % socket with one input |
| 1038 |
\cs_new_eq:NN \socket_use:nnn \socket_use:nw % socket with two inputs |
| 1039 |
\cs_new_eq:NN \socket_use:nnnn \socket_use:nw % socket with three inputs |
| 1040 |
% \end{macrocode} |
| 1041 |
% The above commands could be changed to check how many inputs |
| 1042 |
% the socket is declared with (for example, when checking is in |
| 1043 |
% force). |
| 1044 |
% \fmi{Implement?} |
| 1045 |
% \end{macro} |
| 1046 |
% |
| 1047 |
% \begin{macro}{\socket_use_expandable:nw,\socket_use_expandable:n} |
| 1048 |
% \changes{v0.9b}{2024/10/27}{Added \cs{socket_use_expandable:n}} |
| 1049 |
% The same as the non-expandable code, except for the missing debug output. |
| 1050 |
% \begin{macrocode} |
| 1051 |
\cs_new:Npn \socket_use_expandable:nw #1 { |
| 1052 |
\use:c { @@_#1_plug_ \str_use:c { l_@@_#1_plug_str } :w } |
| 1053 |
} |
| 1054 |
\cs_new_eq:NN \socket_use_expandable:n \socket_use_expandable:nw % socket with no inputs |
| 1055 |
% \end{macrocode} |
| 1056 |
% \end{macro} |
| 1057 |
% |
| 1058 |
% |
| 1059 |
% |
| 1060 |
% |
| 1061 |
% |
| 1062 |
% |
| 1063 |
% |
| 1064 |
% \subsection{Error messages} |
| 1065 |
% |
| 1066 |
|
| 1067 |
% |
| 1068 |
% \begin{macrocode} |
| 1069 |
\msg_new:nnnn { socket } { already-declared } |
| 1070 |
{ Socket~ '#1'~ already~ declared! } |
| 1071 |
{ A~ socket~ can~ only~ be~ declared~ once.~ The~ name~ '#1'~ is~ |
| 1072 |
already~ taken.~ Use~ \ShowSocket{#1}~ to~ see~ its~ definition. } |
| 1073 |
|
| 1074 |
\msg_new:nnnn { socket } { undeclared } |
| 1075 |
{ Socket~ '#1'~ undeclared! } |
| 1076 |
{ You~ tried~ to~ use~ a~ socket~ that~ was~ not~ declared~ before. } |
| 1077 |
|
| 1078 |
\msg_new:nnnn { socket } { not-top-level } |
| 1079 |
{ Sockets~ can~ only~ be~ declared~ at~ top-level! } |
| 1080 |
{ It~ is~ not~ allowed~ to~ declare~ sockets~ inside~ a~ |
| 1081 |
group.~ Move~ the~ declaration~ to~ the~ top-level. } |
| 1082 |
% \end{macrocode} |
| 1083 |
% |
| 1084 |
% \begin{macrocode} |
| 1085 |
\msg_new:nnnn { socket } { plug-already-declared } |
| 1086 |
{ Plug~ '#2'~ for~ socket~ '#1'~ already~ declared! } |
| 1087 |
{ You~ can't~ change~ an~ existing~ plug~ with~ \NewSocketPlug~ and~ it~ |
| 1088 |
is~ normally~ not~ sensible~ to~ do~ so.~ Use~ the~ L3~ programming~ |
| 1089 |
layer~ function~ \socket_set_plug:nnn~ if~ you~ really~ have~ to. } |
| 1090 |
|
| 1091 |
\msg_new:nnnn { socket } { plug-undeclared } |
| 1092 |
{ Plug~ '#2'~ for~ socket~ '#1'~ undeclared! } |
| 1093 |
{ The~ plug~ name~ is~ unknown.~ Is~ the~ name~ misspelled~ or~ did~ you~ |
| 1094 |
intend~ to~ assign~ it~ to~ a~ different~ socket? } |
| 1095 |
% \end{macrocode} |
| 1096 |
% |
| 1097 |
% \begin{macrocode} |
| 1098 |
\prop_gput:Nnn \g_msg_module_type_prop { socket } { LaTeX } |
| 1099 |
% \end{macrocode} |
| 1100 |
% |
| 1101 |
% |
| 1102 |
% |
| 1103 |
% |
| 1104 |
% \subsection{The \LaTeXe{} interface commands} |
| 1105 |
% |
| 1106 |
% \begin{macro}{\NewSocket,\NewSocketPlug, |
| 1107 |
% \ShowSocket,\LogSocket, |
| 1108 |
% \AssignSocketPlug,\UseSocket, |
| 1109 |
% \DebugSocketsOn,\DebugSocketsOff} |
| 1110 |
% As we expect that there are existing \LaTeXe{} packages that may |
| 1111 |
% want to make use of the socket mechanism, we provide 2e names for |
| 1112 |
% most of the commands. |
| 1113 |
% \begin{macrocode} |
| 1114 |
\cs_new_eq:NN \NewSocket \socket_new:nn |
| 1115 |
\cs_new_eq:NN \ShowSocket \socket_show:n |
| 1116 |
\cs_new_eq:NN \LogSocket \socket_log:n |
| 1117 |
% \end{macrocode} |
| 1118 |
% |
| 1119 |
% \begin{macrocode} |
| 1120 |
\cs_new_eq:NN \NewSocketPlug \socket_new_plug:nnn |
| 1121 |
\cs_new_eq:NN \AssignSocketPlug \socket_assign_plug:nn |
| 1122 |
\cs_new_eq:NN \UseSocket \socket_use:nw |
| 1123 |
% \end{macrocode} |
| 1124 |
% |
| 1125 |
% \begin{macrocode} |
| 1126 |
\cs_new_eq:NN \DebugSocketsOn \socket_debug_on: |
| 1127 |
\cs_new_eq:NN \DebugSocketsOff \socket_debug_off: |
| 1128 |
% \end{macrocode} |
| 1129 |
% \end{macro} |
| 1130 |
% |
| 1131 |
% |
| 1132 |
% \begin{macro}{\IfSocketExistsTF,\IfSocketExistsT,\IfSocketExistsF, |
| 1133 |
% \IfSocketPlugExistsTF,\IfSocketPlugExistsT, |
| 1134 |
% \IfSocketPlugExistsF, |
| 1135 |
% \IfSocketPlugAssignedTF,\IfSocketPlugAssignedT, |
| 1136 |
% \IfSocketPlugAssignedF} |
| 1137 |
% A bunch of conditionals: |
| 1138 |
% \changes{v0.9c}{2024/12/27}{Conditionals for sockets, plugs, and |
| 1139 |
% assignments (gh/1577)} |
| 1140 |
% \begin{macrocode} |
| 1141 |
\cs_new_eq:NN \IfSocketExistsTF \socket_if_exist:nTF |
| 1142 |
\cs_new_eq:NN \IfSocketExistsT \socket_if_exist:nT |
| 1143 |
\cs_new_eq:NN \IfSocketExistsF \socket_if_exist:nF |
| 1144 |
% \end{macrocode} |
| 1145 |
% |
| 1146 |
% \begin{macrocode} |
| 1147 |
\cs_new_eq:NN \IfSocketPlugExistsTF \socket_if_plug_exist:nnTF |
| 1148 |
\cs_new_eq:NN \IfSocketPlugExistsT \socket_if_plug_exist:nnT |
| 1149 |
\cs_new_eq:NN \IfSocketPlugExistsF \socket_if_plug_exist:nnF |
| 1150 |
% \end{macrocode} |
| 1151 |
% |
| 1152 |
% \begin{macrocode} |
| 1153 |
\cs_new_eq:NN \IfSocketPlugAssignedTF \socket_if_plug_assigned:nnTF |
| 1154 |
\cs_new_eq:NN \IfSocketPlugAssignedT \socket_if_plug_assigned:nnT |
| 1155 |
\cs_new_eq:NN \IfSocketPlugAssignedF \socket_if_plug_assigned:nnF |
| 1156 |
% \end{macrocode} |
| 1157 |
% \end{macro} |
| 1158 |
% |
| 1159 |
% \begin{macrocode} |
| 1160 |
% |
| 1161 |
%<latexrelease>\IncludeInRelease{0000/00/00}{ltsockets} |
| 1162 |
%<latexrelease> {The~socket~management~(undo)}% |
| 1163 |
%<latexrelease> |
| 1164 |
%<latexrelease>\let \NewSocket \@undefined |
| 1165 |
%<latexrelease>\let \ShowSocket \@undefined |
| 1166 |
%<latexrelease>\let \LogSocket \@undefined |
| 1167 |
%<latexrelease> |
| 1168 |
%<latexrelease>\let \NewSocketPlug \@undefined |
| 1169 |
%<latexrelease>\let \AssignSocketPlug \@undefined |
| 1170 |
%<latexrelease>\let \UseSocket \@undefined |
| 1171 |
%<latexrelease> |
| 1172 |
%<latexrelease>\let \DebugSocketsOn \@undefined |
| 1173 |
%<latexrelease>\let \DebugSocketsOff \@undefined |
| 1174 |
%<latexrelease> |
| 1175 |
%<latexrelease>\let \IfSocketExistsTF \@undefined |
| 1176 |
%<latexrelease>\let \IfSocketExistsT \@undefined |
| 1177 |
%<latexrelease>\let \IfSocketExistsF \@undefined |
| 1178 |
%<latexrelease>\let \IfSocketPlugExistsTF \@undefined |
| 1179 |
%<latexrelease>\let \IfSocketPlugExistsT \@undefined |
| 1180 |
%<latexrelease>\let \IfSocketPlugExistsF \@undefined |
| 1181 |
%<latexrelease>\let \IfSocketPlugAssignedTF \@undefined |
| 1182 |
%<latexrelease>\let \IfSocketPlugAssignedT \@undefined |
| 1183 |
%<latexrelease>\let \IfSocketPlugAssignedF \@undefined |
| 1184 |
%<latexrelease> |
| 1185 |
%<latexrelease>\EndModuleRelease |
| 1186 |
% \end{macrocode} |
| 1187 |
% |
| 1188 |
% \begin{macrocode} |
| 1189 |
\ExplSyntaxOff |
| 1190 |
% \end{macrocode} |
| 1191 |
% |
| 1192 |
% \begin{macrocode} |
| 1193 |
%</2ekernel|latexrelease> |
| 1194 |
% \end{macrocode} |
| 1195 |
% |
| 1196 |
% Reset module prefix: |
| 1197 |
% \begin{macrocode} |
| 1198 |
%<@@=> |
| 1199 |
% \end{macrocode} |
| 1200 |
% |
| 1201 |
% |
| 1202 |
% \Finale |
| 1203 |
% |
| 1204 |
% |
| 1205 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1206 |
\endinput |
| 1207 |
|
| 1208 |
|
| 1209 |
|
| 1210 |
^^A Needed for emacs |
| 1211 |
^^A |
| 1212 |
^^A Local Variables: |
| 1213 |
^^A mode: latex |
| 1214 |
^^A coding: utf-8-unix |
| 1215 |
^^A End: |
| 1216 |
|
| 1217 |
|
| 1218 |
|