Debian Codegraph @

texmf-dist/source/latex/memoize-ext/memoize-ext-expl3.dtx

raw
1 % \iffalse meta-comment
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 % memoize-ext.dtx
4 % Additions and changes Copyright (C) 2024-2026 Clea F. Rees.
5 % Code from skeleton.dtx Copyright (C) 2015-2024 Scott Pakin (see below).
6 %
7 % This work may be distributed and/or modified under the
8 % conditions of the LaTeX Project Public License, either version 1.3c
9 % of this license or (at your option) any later version.
10 % The latest version of this license is in
11 % https://www.latex-project.org/lppl.txt
12 % and version 1.3c or later is part of all distributions of LaTeX
13 % version 2008-05-04 or later.
14 %
15 % This work has the LPPL maintenance status 'muaintained'.
16 %
17 % The Current Maintainer of this work is Clea F. Rees.
18 %
19 % This work consists of all files listed in manifest.txt.
20 %
21 % The file memoize-ext.dtx is a derived work under the terms of the
22 % LPPL. It is based on version 2.4 of skeleton.dtx which is part of
23 % dtxtut by Scott Pakin. A copy of dtxtut, including the
24 % unmodified version of skeleton.dtx is available from
25 % https://www.ctan.org/pkg/dtxtut and released under the LPPL.
26 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
27 % \fi
28 %
29 % \iffalse
30 %<*driver>
31 \GetIdInfo $Id: memoize-ext-expl3.dtx 11815 2026-03-25 19:50:49Z cfrees $ {Extensions for Memoize}
32 \ProvidesExplFile{\ExplFileName}{\ExplFileDate}{v0.3.5 \ExplFileVersion}{\ExplFileDescription}
33 \begin{document}
34 \let\MakePrivateLetters\MyMakePrivateLetters
35 \DocInput{\filename}
36 \end{document}
37 %</driver>
38 % \fi
39 %
40 % \title{\ExplFileName}
41 % \date{\ExplFileVersion~\ExplFileDate}
42 % \maketitle
43 %
44 % \begin{abstract}
45 % \noindent Provides \pkg{memoize-ext-expl3} and \pkg{memoize-ext-expl3-common}.
46 % Part of \pkg{memoize-ext}.
47 % \end{abstract}
48 %
49 % \tableofcontents
50 %
51 %
52 % \MaybeStop{%
53 % \PrintChanges
54 % \PrintIndex
55 % }
56 %
57 % ^^A \section{Implementation}
58 %
59 %<@@=mmzx>
60 % ^^A common <<<
61 %<*common>
62 % \begin{macrocode}
63 \GetIdInfo $Id: memoize-ext-expl3.dtx 11815 2026-03-25 19:50:49Z cfrees $ {Extensions for Memoize for expl3 code}
64 %<!debug> \ProvidesExplPackage{\ExplFileName-common}{\ExplFileDate}{v0.0 %
65 %<!debug> \ExplFileVersion}{\ExplFileDescription}
66 %<debug> \ProvidesExplPackage{\ExplFileName-common-debug}{\ExplFileDate}{v0.0 %
67 %<debug> \ExplFileVersion}{\ExplFileDescription}
68 %
69 %<!debug> \disable@package@load {memoize-ext-expl3-common-debug}
70 %<debug> \disable@package@load {memoize-ext-expl3-common}
71 { Only~one~of~memoize-ext-expl3-common~and~memoize-ext-expl3-common-debug~
72 should~be~loaded.
73 Since~
74 %<!debug> memoize-ext-expl3-common
75 %<debug> memoize-ext-expl3-common-debug
76 ~has~been~loaded,~I~will~ignore~your~request~for~
77 %<debug> memoize-ext-expl3-common
78 %<!debug> memoize-ext-expl3-common-debug
79 .}
80 % \end{macrocode}
81 % \begin{macrocode}
82 %<!debug> \RequirePackage{memoize-ext}
83 %<debug> \RequirePackage{memoize-ext-debug}
84 %
85 % \end{macrocode}
86 % We don't want inconsistent names in hooks.
87 % \begin{macrocode}
88 \SetDefaultHookLabel{memoize-ext}
89 % \end{macrocode}
90 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
91 % \begin{var}{\l_@@_replicating_bool}
92 % Internal variable to track whether currently replicating.
93 % \begin{macrocode}
94 \bool_new:N \l_@@_replicating_bool
95 \bool_set_false:N \l_@@_replicating_bool
96 % \end{macrocode}
97 % \end{var}
98 % \begin{fn}{%
99 % \@@_if_replicating:TF,
100 % \@@_if_replicating_p:,
101 % }
102 % \begin{macrocode}
103 \prg_new_conditional:Npnn \@@_if_replicating: {p,T,TF,F}
104 {
105 \if_bool:N \l_@@_replicating_bool
106 %<debug> \@@_debug:n {Replicating~true.}
107 \prg_return_true:
108 \else:
109 %<debug> \@@_debug:n {Replicating~false.}
110 \prg_return_false:
111 \fi:
112 }
113 % \end{macrocode}
114 % \end{fn}
115 % \begin{macro}{\AdviceRunIfNotReplicating}
116 % Run condition.
117 % \begin{macrocode}
118 \cs_new:Npn \AdviceRunIfNotReplicating
119 {
120 \@@_if_replicating:F
121 {
122 %<debug> \@@_debug:n {Not~replicating,~so~proceeding.}
123 \ifmemoizing \AdviceRuntrue \fi
124 }
125 }
126 % \end{macrocode}
127 % \end{macro}
128 % \begin{pgfkey}{auto/run~if~not~replicating}
129 % Style.
130 % \begin{macrocode}
131 \mmzset{
132 auto/run~if~not~replicating/.style = {
133 run~conditions={\AdviceRunIfNotReplicating},
134 },
135 }
136 % \end{macrocode}
137 % \end{pgfkey}
138 % Constants
139 % \begin{macrocode}
140 \cctab_const:Nn \c_@@_expl_at_cctab {
141 \cctab_select:N \c_code_cctab
142 \makeatletter
143 }
144 \cctab_const:Nn \c_@@_nexpl_at_cctab {
145 \cctab_select:N \c_code_cctab
146 \makeatletter
147 \int_set:Nn \tex_endlinechar:D { 13 }
148 \char_set_catcode_space:n { 9 }
149 \char_set_catcode_space:n { 32 }
150 \char_set_catcode_active:n { 126 } % tilde
151 }
152 % \end{macrocode}
153 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
154 % expl3, memoizing côd ynddo
155 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
156 % hooks instead of \texse[David Carlisle]{a/748807/}
157 % \begin{var}{\l_@@_expl_bool}
158 % A boolean to track whether \texttt{expl3} syntax is active or not.
159 % \texseans[yn lle ateb \textbar{} in place of]{748807}[David Carlisle]
160 % \begin{macrocode}
161 \bool_new:N \l_@@_expl_bool
162 % \end{macrocode}
163 % \end{var}
164 % \begin{fn}{%
165 % \@@_restore_ccmemo_input:,
166 % \@@_saved_mmzxExplAtBegin:,
167 % \@@_saved_mmzxExplAtEnd:,
168 % }
169 % Initialise.
170 % \begin{macrocode}
171 \cs_new_protected_nopar:Npn \@@_restore_ccmemo_input: {}
172 \cs_new_protected_nopar:Npn \@@_saved_mmzxExplAtBegin: {}
173 \cs_new_protected_nopar:Npn \@@_saved_mmzxExplAtEnd: {}
174 % \end{macrocode}
175 % \end{fn}
176 % Auto-switching for \texttt{expl3} syntax.
177 % \begin{macrocode}
178 \hook_gput_code:nnn {begindocument/end}{.}
179 {
180 \bool_set_false:N \l_@@_expl_bool
181 }
182 % \end{macrocode}
183 % \begin{macrocode}
184 \hook_gput_code:nnn {begindocument/end}{.}
185 {
186 %<debug> \@@_debug:n {Tracking~expl3~syntax~changes.}
187 \bool_set_false:N \l_@@_expl_bool
188 \hook_gput_code:nnn {cmd/ExplSyntaxOn/before} { . }
189 {
190 \bool_if:NF \l_@@_expl_bool
191 {
192 \bool_set_true:N \l_@@_expl_bool
193 \ifmmz@direct@ccmemo@input
194 \relax
195 \else
196 \cs_set_protected_nopar:Npn \@@_restore_ccmemo_input:
197 {
198 \mmz@direct@ccmemo@inputfalse
199 }
200 \fi
201 \mmz@direct@ccmemo@inputtrue
202 \cs_set_eq:NN \@@_saved_mmzxExplAtBegin: \mmzxExplAtBegin
203 \cs_set_eq:NN \@@_saved_mmzxExplAtEnd: \mmzxExplAtEnd
204 \@@_expl_at_start:
205 }
206 }
207 % \end{macrocode}
208 % \cs{ExplSyntaxOn} rewrites \cs{ExplSyntaxOff}, so it doesn't work to add hook code to \cs{ExplSyntaxOff} directly.
209 % \begin{macrocode}
210 \hook_gput_code:nnn {cmd/ExplSyntaxOn/after} { . }
211 {
212 \hook_gput_code:nnn {cmd/ExplSyntaxOff/before} { . }
213 {
214 \bool_set_false:N \l_@@_expl_bool
215 \cs_set_eq:NN \mmzxExplAtBegin \@@_saved_mmzxExplAtBegin:
216 \cs_set_eq:NN \mmzxExplAtEnd \@@_saved_mmzxExplAtEnd:
217 \@@_restore_ccmemo_input:
218 }
219 }
220 }
221 % \end{macrocode}
222 % fns mewnol
223 % \begin{fn}{\@@_cctab_end:}
224 % just for symmetry \dots
225 % \begin{macrocode}
226 \cs_new_eq:NN \@@_cctab_end: \cctab_end:
227 % \end{macrocode}
228 % \end{fn}
229 % \begin{fn}{%
230 % \@@_expl_at_begin:,
231 % \@@_nexpl_at_begin:,
232 % \@@_expl_at_start:,
233 % \@@_nexpl_at_start:,
234 % \@@_cctab_stop:,
235 % }
236 % Convenience wrappers for cat code changes.
237 % \begin{macrocode}
238 \cs_new_protected_nopar:Npn \@@_expl_at_begin:
239 {
240 \cctab_begin:N \c_@@_expl_at_cctab
241 }
242 \cs_new_protected_nopar:Npn \@@_nexpl_at_begin:
243 {
244 \cctab_begin:N \c_@@_nexpl_at_cctab
245 }
246 \cs_new_nopar:Npn \@@_expl_at_start:
247 {
248 \cs_set_nopar:Npn \mmzxExplAtBegin {@@_expl_at_begin:}
249 \cs_set_nopar:Npn \mmzxExplAtEnd {@@_cctab_end:}
250 }
251 \cs_new_nopar:Npn \@@_nexpl_at_start:
252 {
253 \cs_set_nopar:Npn \mmzxExplAtBegin {@@_nexpl_at_begin:}
254 \cs_set_nopar:Npn \mmzxExplAtEnd {@@_cctab_end:}
255 }
256 \cs_new_protected_nopar:Npn \@@_cctab_stop:
257 {
258 \cs_set_nopar:Npn \mmzxExplAtBegin {relax}
259 \cs_set_nopar:Npn \mmzxExplAtEnd {relax}
260 }
261 % \end{macrocode}
262 % \end{fn}
263 % toks memoize (cyhoeddus yn unig)
264 %
265 % The code here is constant but the meaning changes, so what is added to the memos reflects the configuration at the time.
266 % \changes{v0.3.2}{2026-03-14}{Modified \cs{xtoksapp} means all engines can use the same code here.}
267 % \begin{macrocode}
268 \appto\mmzAtBeginMemoization{
269 %<debug> \@@_debug:n {Appending~expl~begin~to~ccmemo~at~end~
270 %<debug> \string\mmzAtBeginMemoization.}
271 \xtoksapp\mmzCCMemo
272 {
273 \exp_not:N \csname \mmzxExplAtBegin \exp_not:N \endcsname
274 }
275 %<debug> \exp_args:No \@@_debug:n {\the\mmzCCMemo}
276 }
277 %<debug> \cs_log:N \mmzAtBeginMemoization
278 \preto\mmzAtEndMemoization{
279 %<debug> \@@_debug:n {Appending~expl~end~to~ccmemo~at~start~
280 %<debug> \string\mmzAtEndMemoization.}
281 \xtoksapp\mmzCCMemo
282 {
283 \exp_not:N \csname \mmzxExplAtEnd \exp_not:N \endcsname
284 }
285 }
286 %<debug> \cs_log:N \mmzAtEndMemoization
287 % \end{macrocode}
288 % init
289 %
290 % ^^A If tagging, we allow \texttt{expl3} syntax and \verb|@|, but don't switch spaces or new lines as that would screw up marked content.
291 % \begin{macrocode}
292 \hook_gput_code:nnn { begindocument/end } {mmzx}
293 {
294 % ^^A \tag_if_active:TF
295 % ^^A {
296 % ^^A %<debug> \@@_debug:n {Enabling~direct~ccmemo~input~and~cat~code}
297 % ^^A %<debug> \@@_debug:n {management~required~for~tagging.}
298 % ^^A \mmz@direct@ccmemo@inputtrue
299 % ^^A \@@_nexpl_at_start:
300 % ^^A } {
301 % ^^A %<debug> \@@_debug:n {Tagging~not~active.~Adjusting~cat~code~management.}
302 \@@_cctab_stop:
303 % ^^A }
304 }
305 % \end{macrocode}
306 %</common>
307 % ^^A >>>
308 %
309 % ^^A sty <<<
310 %<*sty>
311 % \begin{macrocode}
312 \GetIdInfo $Id: memoize-ext-expl3.dtx 11815 2026-03-25 19:50:49Z cfrees $ {Extensions for Memoize for replicating expl3 functions and variables}
313 %<!debug> \ProvidesExplPackage{\ExplFileName}{\ExplFileDate}
314 %<!debug> {v0.3.5 \ExplFileVersion}{\ExplFileDescription}
315 %<debug> \ProvidesExplPackage{\ExplFileName-debug}{\ExplFileDate}
316 %<debug> {v0.3.5 \ExplFileVersion}{\ExplFileDescription}
317 %
318 %<!debug> \disable@package@load {memoize-ext-expl3-debug}
319 %<debug> \disable@package@load {memoize-ext-expl3}
320 { Only~one~of~memoize-ext-expl3~and~memoize-ext-expl3-debug~
321 should~be~loaded.
322 Since~
323 %<!debug> memoize-ext-expl3
324 %<debug> memoize-ext-expl3-debug
325 ~has~been~loaded,~I~will~ignore~your~request~for~
326 %<debug> memoize-ext-expl3
327 %<!debug> memoize-ext-expl3-debug
328 .}
329 % \end{macrocode}
330 % \begin{macrocode}
331 %<!debug> \RequirePackage{memoize-ext}
332 %<debug> \RequirePackage{memoize-ext-debug}
333 %<!debug> \RequirePackage{memoize-ext-expl3-common}
334 %<debug> \RequirePackage{memoize-ext-expl3-common-debug}
335 %
336 % \end{macrocode}
337 % We don't want inconsistent names in hooks.
338 % \begin{macrocode}
339 \SetDefaultHookLabel{memoize-ext}
340 % \end{macrocode}
341 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
342 % expl3 replicators
343 %
344 % todo: figure out how to maintain correct grouping \dots
345 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
346 % \begin{var}{%
347 % \g_@@_expl_replicate__bb_tl,
348 % \g_@@_expl_replicate__ba_tl,
349 % \g_@@_expl_replicate__tb_tl,
350 % }
351 % Variables
352 % \begin{macrocode}
353 \tl_new:N \g_@@_expl_replicate__bb_tl
354 \tl_new:N \g_@@_expl_replicate__ba_tl
355 \tl_new:N \g_@@_expl_replicate__tb_tl
356 \tl_gput_right:NV \g_@@_expl_replicate__bb_tl \c_left_brace_str
357 \tl_gput_right:Nn \g_@@_expl_replicate__bb_tl {#}
358 \tl_gput_right:NV \g_@@_expl_replicate__ba_tl \c_right_brace_str
359 \tl_gput_right:Nn \g_@@_expl_replicate__tb_tl {#}
360 % \end{macrocode}
361 % \end{var}
362 % \begin{fn}{%
363 % \@@_expl_replicate_fn_aux:nnN,
364 % \@@_expl_replicate__aux:n,
365 % }
366 % Generic auxiliary functions for replication.
367 % The first does the actual replicating; the second delegates details according to the argument specification.
368 % \begin{macrocode}
369 \cs_new:Npn \@@_expl_replicate_fn_aux:nnN #1#2#3
370 {
371 \cs_if_exist:cF { __mmzx_rep_#1:#2 }
372 {
373 \int_zero:N \l_@@_tmpa_int
374 \tl_clear:N \l_@@_tmpa_tl
375 \tl_clear:N \l_@@_tmpc_tl
376 \tl_map_function:nN {#2} \@@_expl_replicate__aux:n
377 \cs_if_exist:cF { __mmzx_rep_#1:\l_@@_tmpc_tl }
378 {
379 \cs_gset_protected:ce {__mmzx_rep_#1:\l_@@_tmpc_tl}
380 {
381 \xtoksapp\mmzCCMemo{
382 \exp_after:wN \exp_not:N \cs:w #1:#2 \cs_end: \l_@@_tmpa_tl
383 }
384 \exp_not:N \expandonce \exp_not:N \AdviceOriginal \l_@@_tmpa_tl
385 \exp_not:N \group_end:
386 \@@_tmp_cctab_stop:
387 }
388 }
389 \str_if_eq:eeF {#2}{\l_@@_tmpc_tl}
390 { % ych!
391 \cs_new_eq:cc {__mmzx_rep_#1:#2} {__mmzx_rep_#1:\l_@@_tmpc_tl}
392 }
393 }
394 \use:c { __mmzx_rep_#1:#2 }
395 }
396 \cs_new:Npn \@@_expl_replicate__aux:n #1
397 {
398 \int_incr:N \l_@@_tmpa_int
399 \str_case:nnF { #1 }
400 {
401 {c} { \@@_expl_replicate__b: }
402 {e} { \@@_expl_replicate__b: }
403 {o} { \@@_expl_replicate__b: }
404 {p} { }
405 {n} { \@@_expl_replicate__b: }
406 {v} { \@@_expl_replicate__b: }
407 {D} { }
408 {F} { \@@_expl_replicate__b: }
409 {N} { \@@_expl_replicate__t: }
410 {T} { \@@_expl_replicate__b: }
411 {V} { \@@_expl_replicate__t: }
412 {w} { \@@_expl_replicate__e: }
413 }{
414 \@@_expl_replicate__e:
415 }
416 }
417 % \end{macrocode}
418 % \end{fn}
419 % \begin{fn}{%
420 % \@@_expl_replicate__b:,
421 % \@@_expl_replicate__t:,
422 % \@@_expl_replicate__e:,
423 % }
424 % Type-specific auxiliaries.
425 % We don't need to be very specific here.
426 % We just need to distinguish argument specifiers which expect braced groups from those which expect single tokens and from those we cannot automate.
427 % \begin{macrocode}
428 \cs_new_nopar:Npn \@@_expl_replicate__b:
429 {
430 \tl_put_right:Nn \l_@@_tmpc_tl {n}
431 \tl_put_right:NV \l_@@_tmpa_tl \g_@@_expl_replicate__bb_tl
432 \tl_put_right:Ne \l_@@_tmpa_tl { \int_to_arabic:n { \l_@@_tmpa_int } }
433 \tl_put_right:NV \l_@@_tmpa_tl \g_@@_expl_replicate__ba_tl
434 }
435 \cs_new_nopar:Npn \@@_expl_replicate__t:
436 {
437 \tl_put_right:Nn \l_@@_tmpc_tl {N}
438 \tl_put_right:NV \l_@@_tmpa_tl \g_@@_expl_replicate__tb_tl
439 \tl_put_right:Ne \l_@@_tmpa_tl { \int_to_arabic:n { \l_@@_tmpa_int } }
440 }
441 \cs_new_nopar:Npn \@@_expl_replicate__e:
442 {
443 \PackageError{mmzx}{No~do,~sorry.~Use~replicate~with~args~instead.}{}
444 }
445 % \end{macrocode}
446 % \end{fn}
447 % \begin{fn}{%
448 % \@@_expl_replicate_fn:,
449 % \@@_tmp_cctab_stop:,
450 % }
451 % \begin{macro}{\mmzx@expl@replicate@fn}
452 % For replicating \texttt{expl3} \emph{functions}.
453 % \changes{v0.3.3}{2026-03-14}{Switch cat codes if necessary.}
454 % \begin{macrocode}
455 \cs_new_eq:NN \@@_tmp_cctab_stop: \@@_noop:
456 \cs_new:Npn \@@_expl_replicate_fn:
457 {
458 \bool_set_false:N \l_@@_tmpa_bool
459 \str_if_eq:eeT {\mmzxExplAtEnd} {relax}
460 {
461 \bool_set_true:N \l_@@_tmpa_bool
462 \@@_nexpl_at_start:
463 \xtoksapp\mmzCCMemo {
464 \exp_not:N \csname \mmzxExplAtBegin \exp_not:N \endcsname
465 }
466 \cs_set:Npn \@@_tmp_cctab_stop:
467 {
468 \xtoksapp\mmzCCMemo {
469 \exp_not:N \csname \mmzxExplAtEnd \exp_not:N \endcsname
470 }
471 \@@_cctab_stop:
472 }
473 }{
474 \cs_set_eq:NN \@@_tmp_cctab_stop: \@@_noop:
475 }
476 \group_begin:
477 \bool_set_true:N \l_@@_replicating_bool
478 \exp_last_unbraced:Ne \@@_expl_replicate_fn_aux:nnN
479 { \exp_args:NV \cs_split_function:N \AdviceReplaced }
480 }
481 \cs_new_eq:NN \mmzx@expl@replicate@fn \@@_expl_replicate_fn:
482 % \end{macrocode}
483 % \end{macro}
484 % \end{fn}
485 % \begin{pgfkey}{%
486 % mmz/auto/replicate~expl~fn,
487 % mmz/auto/replicate~expl~var,
488 % }
489 % By default we handle |\tex_savepos:D| since this commonly requires replication in the kinds of environments typically subject to memoization.
490 % Another candidate is |\int_gincr:N|, but that results in a large number of additions and it is not at all clear these are generally required or desirable.
491 % Don't do this.
492 % It breaks stuff.
493 % \begin{macrocode}
494 \mmzset{% config <<<
495 auto/replicate~expl~fn/.style={
496 run~if~not~replicating,
497 outer~handler=\mmzx@expl@replicate@fn,
498 },
499 }% >>>
500 % \end{macrocode}
501 % ^^A auto~csname={tex_savepos:D}{replicate~expl~fn,},
502 % \end{pgfkey}
503 %</sty>
504 % ^^A >>>
505 %
506 %\Finale
507 % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
508 %^^A vim: et:tw=0:sw=2:ts=2:foldmethod=marker:fmr=<<<,>>>: