| 1 |
% Memoize.edtx (this is not a .dtx file; to produce a .dtx, process it with edtx2dtx) |
| 2 |
% |
| 3 |
%% This file is a part of Memoize, a TeX package for externalization of |
| 4 |
%% graphics and memoization of compilation results in general, available at |
| 5 |
%% https://ctan.org/pkg/memoize and https://github.com/sasozivanovic/memoize. |
| 6 |
%% |
| 7 |
%% Copyright (c) 2020- Saso Zivanovic <[email protected]> |
| 8 |
%% (Sa\v{s}o \v{Z}ivanovi\'{c}) |
| 9 |
%% |
| 10 |
%% This work may be distributed and/or modified under the conditions of the |
| 11 |
%% LaTeX Project Public License, either version 1.3c of this license or (at |
| 12 |
%% your option) any later version. The latest version of this license is in |
| 13 |
%% https://www.latex-project.org/lppl.txt and version 1.3c or later is part of |
| 14 |
%% all distributions of LaTeX version 2008 or later. |
| 15 |
%% |
| 16 |
%% This work has the LPPL maintenance status `maintained'. |
| 17 |
%% The Current Maintainer of this work is Saso Zivanovic. |
| 18 |
%% |
| 19 |
%% The files belonging to this work and covered by LPPL are listed in |
| 20 |
%% (<texmf>/doc/generic/memoize/)FILES. |
| 21 |
% |
| 22 |
% \begin{macrocode} |
| 23 |
% |
| 24 |
% \relax |
| 25 |
% |
| 26 |
% This file contains the documented source code of |
| 27 |
% package \href{https://ctan.org/pkg/memoize}{Memoize} and, somewhat |
| 28 |
% unconventionally, its two independently distributed auxiliary |
| 29 |
% packages \href{https://ctan.org/pkg/advice}{Advice} |
| 30 |
% and \href{https://ctan.org/pkg/collargs}{CollArgs}. |
| 31 |
% |
| 32 |
% The source code of the \hologo{TeX} parts of the package resides in |
| 33 |
% |memoize.edtx|, |advice.edtx| and |collargs.edtx|. These files are written |
| 34 |
% in \href{https://ctan.org/pkg/easydtx}{EasyDTX}, a format of my own invention |
| 35 |
% which is almost like the DTX format but eliminates the need for all those |
| 36 |
% pesky |macrocode| environments: Any line introduced by a single comment |
| 37 |
% counts as documentation, and to top it off, documentation lines may be |
| 38 |
% indented. An |.edtx| file is converted to a |.dtx| by a little Perl script |
| 39 |
% called |edtx2dtx|; there is also a rudimentary Emacs mode, implemented in |
| 40 |
% |easydoctex-mode.el|, which takes care of fontification, indentation, and |
| 41 |
% forward and inverse search. |
| 42 |
% |
| 43 |
% The |.edtx| files contain the code for all three formats supported by the |
| 44 |
% three packages --- \hologo{LaTeX} (guard |latex|), \hologo{plainTeX} (guard |
| 45 |
% |plain|) and \hologo{ConTeXt} (guard |context|) --- but upon reading the |
| 46 |
% code, it will quickly become clear that Memoize was first developed for |
| 47 |
% \hologo{LaTeX}. In \S\ref{sec:code:identification}, we manually define |
| 48 |
% whatever \hologo{LaTeX} tools are ``missing'' in \hologo{plainTeX} and |
| 49 |
% \hologo{ConTeXt}. Even worse, \hologo{ConTeXt} code is often just the same |
| 50 |
% as \hologo{plainTeX} code, even in cases where I'm sure \hologo{ConTeXt} |
| 51 |
% offers the relevant tools. This nicely proves that I have no clue about |
| 52 |
% \hologo{ConTeXt}. If you are willing to \hologo{ConTeXt}-ualize my code --- |
| 53 |
% please do so, your help is welcome! |
| 54 |
% |
| 55 |
% The runtimes of Memoize (and also Advice) comprise of more than just the main |
| 56 |
% runtime for each format. Memoize ships with two additional stub packages, |
| 57 |
% |nomemoize| and |memoizable|, and a \hologo{TeX}-based extraction script |
| 58 |
% |memoize-extract-one|; Advice optionally offers a \TikZ; support defined in |
| 59 |
% |advice-tikz.code.tex|. For the relation between guards and runtimes, |
| 60 |
% consult the core of the |.ins| files below. |
| 61 |
% |
| 62 |
% \tcbset{ins listing/.style={listing only, fonttitle=\ttfamily\footnotesize, |
| 63 |
% leftupper=-1.5mm, lefttitle=2mm, right=0mm, top=2mm, bottom=2.65mm, |
| 64 |
% listing options app={basicstyle=\ttfamily\scriptsize}}} |
| 65 |
% |
| 66 |
% \begin{tcbraster}[raster columns=100] |
| 67 |
% \tcbinputlisting{raster multicolumn=55, ins listing, top=1mm, bottom=1mm, title=memoize.ins,listing file=../memoize.ins, linerange={27-40}, leftupper=1mm} |
| 68 |
% \begin{tcboxedraster}[raster columns=1]{blankest, raster multicolumn=45} |
| 69 |
% \tcbinputlisting{ins listing, title=advice.ins, listing file=../advice.ins, linerange=28-31} |
| 70 |
% \tcbinputlisting{ins listing, title=collar\raisebox{0pt}[\height][0pt]{g}s.ins, listing file=../collargs.ins, linerange=29-31} |
| 71 |
% \end{tcboxedraster} |
| 72 |
% \end{tcbraster} |
| 73 |
% |
| 74 |
% Memoize also contains two scripts, |memoize-extract| and |memoize-clean|. |
| 75 |
% Both come in two functionally equivalent implementations: Perl (|.pl|) and a |
| 76 |
% Python (|.py|). Their code is listed in \S\ref{sec:code:scripts}. |
| 77 |
% |
| 78 |
% |
| 79 |
% \thispagestyle{empty} |
| 80 |
% \clearpage |
| 81 |
% \tableofcontents |
| 82 |
% \clearpage |
| 83 |
% |
| 84 |
% \newgeometry{left=4cm,right=1cm,top=1cm,bottom=1cm, |
| 85 |
% marginparwidth=3cm,marginparsep=0pt,nohead,includefoot} |
| 86 |
% \settowidth\marginparsep{\ } |
| 87 |
% |
| 88 |
% \section{First things first} |
| 89 |
% \label{sec:code:identification} |
| 90 |
% |
| 91 |
% \paragraph{Identification} of |memoize|, |memoizable| and |nomemoize|. |
| 92 |
%<*mmz> |
| 93 |
%<latex>\ProvidesPackage{memoize}[2024/12/02 v1.4.1 Fast and flexible externalization] |
| 94 |
%<context>%D \module[ |
| 95 |
%<context>%D file=t-memoize.tex, |
| 96 |
%<context>%D version=1.4.1, |
| 97 |
%<context>%D title=Memoize, |
| 98 |
%<context>%D subtitle=Fast and flexible externalization, |
| 99 |
%<context>%D author=Saso Zivanovic, |
| 100 |
%<context>%D date=2024-12-02, |
| 101 |
%<context>%D copyright=Saso Zivanovic, |
| 102 |
%<context>%D license=LPPL, |
| 103 |
%<context>%D ] |
| 104 |
%<context>\writestatus{loading}{ConTeXt User Module / memoize} |
| 105 |
%<context>\unprotect |
| 106 |
%<context>\startmodule[memoize] |
| 107 |
%<plain>% Package memoize 2024/12/02 v1.4.1 |
| 108 |
%</mmz> |
| 109 |
%<*mmzable> |
| 110 |
%<latex>\ProvidesPackage{memoizable}[2024/12/02 v1.4.1 A programmer's stub for Memoize] |
| 111 |
%<context>%D \module[ |
| 112 |
%<context>%D file=t-memoizable.tex, |
| 113 |
%<context>%D version=1.4.1, |
| 114 |
%<context>%D title=Memoizable, |
| 115 |
%<context>%D subtitle=A programmer's stub for Memoize, |
| 116 |
%<context>%D author=Saso Zivanovic, |
| 117 |
%<context>%D date=2024-12-02, |
| 118 |
%<context>%D copyright=Saso Zivanovic, |
| 119 |
%<context>%D license=LPPL, |
| 120 |
%<context>%D ] |
| 121 |
%<context>\writestatus{loading}{ConTeXt User Module / memoizable} |
| 122 |
%<context>\unprotect |
| 123 |
%<context>\startmodule[memoizable] |
| 124 |
%<plain>% Package memoizable 2024/12/02 v1.4.1 |
| 125 |
%</mmzable> |
| 126 |
%<*nommz> |
| 127 |
%<latex>\ProvidesPackage{nomemoize}[2024/12/02 v1.4.1 A no-op stub for Memoize] |
| 128 |
%<context>%D \module[ |
| 129 |
%<context>%D file=t-nomemoize.tex, |
| 130 |
%<context>%D version=1.4.1, |
| 131 |
%<context>%D title=Memoize, |
| 132 |
%<context>%D subtitle=A no-op stub for Memoize, |
| 133 |
%<context>%D author=Saso Zivanovic, |
| 134 |
%<context>%D date=2024-12-02, |
| 135 |
%<context>%D copyright=Saso Zivanovic, |
| 136 |
%<context>%D license=LPPL, |
| 137 |
%<context>%D ] |
| 138 |
%<context>\writestatus{loading}{ConTeXt User Module / nomemoize} |
| 139 |
%<context>\unprotect |
| 140 |
%<context>\startmodule[nomemoize] |
| 141 |
%<mmz>% Package nomemoize 2024/12/02 v1.4.1 |
| 142 |
%</nommz> |
| 143 |
% |
| 144 |
% \paragraph{Required packages} and \hologo{LaTeX}ization of \hologo{plainTeX} |
| 145 |
% and \hologo{ConTeXt}. |
| 146 |
%<*(mmz,mmzable,nommz)&(plain,context)> |
| 147 |
\input miniltx |
| 148 |
%</(mmz,mmzable,nommz)&(plain,context)> |
| 149 |
% Some stuff which is ``missing'' in |miniltx|, copied here from |latex.ltx|. |
| 150 |
%<*mmz&(plain,context)> |
| 151 |
\def\PackageWarning#1#2{{% |
| 152 |
\newlinechar`\^^J\def\MessageBreak{^^J\space\space#1: }% |
| 153 |
\message{#1: #2}}} |
| 154 |
%</mmz&(plain,context)> |
| 155 |
% Same as the official definition, but without |\outer|. Needed for record |
| 156 |
% file declarations. |
| 157 |
%<*mmz&plain> |
| 158 |
\def\newtoks{\alloc@5\toks\toksdef\@cclvi} |
| 159 |
\def\newwrite{\alloc@7\write\chardef\sixt@@n} |
| 160 |
%</mmz&plain> |
| 161 |
% I can't really write any code without |etoolbox| \dots |
| 162 |
%<*mmz> |
| 163 |
%<latex>\RequirePackage{etoolbox} |
| 164 |
%<plain,context>\input etoolbox-generic |
| 165 |
% Setup the |memoize| namespace in \hologo{LuaTeX}. |
| 166 |
\ifdefined\luatexversion |
| 167 |
\directlua{memoize = {}} |
| 168 |
\fi |
| 169 |
% |pdftexcmds.sty| eases access to some PDF primitives, but I cannot manage to |
| 170 |
% load it in \hologo{ConTeXt}, even if it's supposed to be a generic |
| 171 |
% package. So let's load |pdftexcmds.lua| and copy--paste what we need from |
| 172 |
% |pdftexcmds.sty|. |
| 173 |
%<latex>\RequirePackage{pdftexcmds} |
| 174 |
%<plain>\input pdftexcmds.sty |
| 175 |
%<*context> |
| 176 |
\directlua{% |
| 177 |
require("pdftexcmds") |
| 178 |
tex.enableprimitives('pdf@', {'draftmode'}) |
| 179 |
} |
| 180 |
\long\def\pdf@mdfivesum#1{% |
| 181 |
\directlua{% |
| 182 |
oberdiek.pdftexcmds.mdfivesum("\luaescapestring{#1}", "byte")% |
| 183 |
}% |
| 184 |
}% |
| 185 |
\def\pdf@system#1{% |
| 186 |
\directlua{% |
| 187 |
oberdiek.pdftexcmds.system("\luaescapestring{#1}")% |
| 188 |
}% |
| 189 |
} |
| 190 |
\let\pdf@primitive\primitive |
| 191 |
% Lua function |oberdiek.pdftexcmds.filesize| requires the |kpse| library, |
| 192 |
% which is not loaded in \hologo{ConTeXt}, |
| 193 |
% see \url(https://){github.com/latex3/lua-uni-algos/issues/3}, so we define our |
| 194 |
% own filesize function. |
| 195 |
\directlua{% |
| 196 |
function memoize.filesize(filename) |
| 197 |
local filehandle = io.open(filename, "r") |
| 198 |
% We can't easily use |~=|, as |~| is an active character, so the |else| |
| 199 |
% workaround. |
| 200 |
if filehandle == nil then |
| 201 |
else |
| 202 |
tex.write(filehandle:seek("end")) |
| 203 |
io.close(filehandle) |
| 204 |
end |
| 205 |
end |
| 206 |
}% |
| 207 |
\def\pdf@filesize#1{% |
| 208 |
\directlua{memoize.filesize("\luaescapestring{#1}")}% |
| 209 |
} |
| 210 |
%</context> |
| 211 |
% Take care of some further differences between the engines. |
| 212 |
\ifdef\pdftexversion{% |
| 213 |
}{% |
| 214 |
\def\pdfhorigin{1true in}% |
| 215 |
\def\pdfvorigin{1true in}% |
| 216 |
\ifdef\XeTeXversion{% |
| 217 |
\let\quitvmode\leavevmode |
| 218 |
}{% |
| 219 |
\ifdef\luatexversion{% |
| 220 |
\let\pdfpagewidth\pagewidth |
| 221 |
\let\pdfpageheight\pageheight |
| 222 |
\def\pdfmajorversion{\pdfvariable majorversion}% |
| 223 |
\def\pdfminorversion{\pdfvariable minorversion}% |
| 224 |
}{% |
| 225 |
\PackageError{memoize}{Support for this TeX engine is not implemented}{}% |
| 226 |
}% |
| 227 |
}% |
| 228 |
} |
| 229 |
%</mmz> |
| 230 |
% |
| 231 |
% In \hologo{ConTeXt}, |\unexpanded| means |\protected|, and the usual |
| 232 |
% |\unexpanded| is available as |\normalunexpanded|. Option one: use dtx |
| 233 |
% guards to produce the correct control sequence. I tried this option. I find |
| 234 |
% it ugly, and I keep forgetting to guard. Option two: |\let| an internal |
| 235 |
% control sequence, like |\mmz@unexpanded|, to the correct thing, and use that |
| 236 |
% all the time. I never tried this, but I find it ugly, too, and I guess I |
| 237 |
% would forget to use the new control sequence, anyway. Option three: use |
| 238 |
% |\unexpanded| in the |.dtx|, and |sed| through the generated \hologo{ConTeXt} |
| 239 |
% files to replace all its occurrences by |\normalunexpanded|. Oh yeah! |
| 240 |
% |
| 241 |
% Load |pgfkeys| in |nomemoize| and |memoizable|. Not necessary in |memoize|, |
| 242 |
% as it is already loaded by CollArgs. |
| 243 |
%<*nommz,mmzable> |
| 244 |
%<latex>\RequirePackage{pgfkeys} |
| 245 |
%<plain>\input pgfkeys |
| 246 |
%<context>\input t-pgfkey |
| 247 |
%</nommz,mmzable> |
| 248 |
% |
| 249 |
% Different formats of |memoizable| merely load |memoizable.code.tex|, which |
| 250 |
% exists so that |memoizable| can be easily loaded by generic code, like a |
| 251 |
% |tikz| library. |
| 252 |
%<mmzable&!generic>\input memoizable.code.tex |
| 253 |
% |
| 254 |
% \paragraph.{Shipout} |
| 255 |
% We will next load our own auxiliary package, CollArgs, but before we do |
| 256 |
% that, we need to grab |\shipout| in \hologo{plainTeX}. The problem is, |
| 257 |
% Memoize needs to hack into the shipout routine, but it has best chances |
| 258 |
% of working as intended if it redefines the \emph{primitive} |\shipout|. |
| 259 |
% However, CollArgs loads |pgfkeys|, which in turn (and perhaps with no for |
| 260 |
% reason) loads |atbegshi|, which redefines |\shipout|. For details, see |
| 261 |
% section~\ref{sec:code:extern}. Below, we first check that the current |
| 262 |
% meaning of |\shipout| is primitive, and then redefine it. |
| 263 |
% |
| 264 |
%<*mmz> |
| 265 |
%<*plain> |
| 266 |
\def\mmz@regular@shipout{% |
| 267 |
\global\advance\mmzRegularPages1\relax |
| 268 |
\mmz@primitive@shipout |
| 269 |
} |
| 270 |
\edef\mmz@temp{\string\shipout}% |
| 271 |
\edef\mmz@tempa{\meaning\shipout}% |
| 272 |
\ifx\mmz@temp\mmz@tempa |
| 273 |
\let\mmz@primitive@shipout\shipout |
| 274 |
\let\shipout\mmz@regular@shipout |
| 275 |
\else |
| 276 |
\PackageError{memoize}{Cannot grab \string\shipout, it is already redefined}{}% |
| 277 |
\fi |
| 278 |
%</plain> |
| 279 |
% Our auxiliary package (\MS\ref{sec:ref:collargs}, \S\ref{sec:code:collargs}). |
| 280 |
% We also need it in |nomemoize|, to collect manual environments. |
| 281 |
%<latex>\RequirePackage{advice} |
| 282 |
%<plain>\input advice |
| 283 |
%<context>\input t-advice |
| 284 |
%</mmz> |
| 285 |
% |
| 286 |
% \paragraph.{Loading order} |memoize| and |nomemoize| are mutually exclusive, |
| 287 |
% and |memoizable| must be loaded before either of them. |\mmz@loadstatus|: 1 |
| 288 |
% = memoize, 2 = memoizable, 3 = nomemoize. |
| 289 |
%<*mmz,nommz> |
| 290 |
\def\ifmmz@loadstatus#1{% |
| 291 |
\ifnum#1=0\csname mmz@loadstatus\endcsname\relax |
| 292 |
\expandafter\@firstoftwo |
| 293 |
\else |
| 294 |
\expandafter\@secondoftwo |
| 295 |
\fi |
| 296 |
} |
| 297 |
%</mmz,nommz> |
| 298 |
%<*mmz> |
| 299 |
\ifmmz@loadstatus{3}{% |
| 300 |
\PackageError{memoize}{Cannot load the package, as "nomemoize" is already |
| 301 |
loaded. Memoization will NOT be in effect}{Packages "memoize" and |
| 302 |
"nomemoize" are mutually exclusive, please load either one or the other.}% |
| 303 |
%<latex>\pgfkeys{/memoize/package options/.unknown/.code={}} |
| 304 |
%<latex>\ProcessPgfPackageOptions{/memoize/package options} |
| 305 |
\endinput |
| 306 |
}{}% |
| 307 |
\ifmmz@loadstatus{2}{% |
| 308 |
\PackageError{memoize}{Cannot load the package, as "memoizable" is already |
| 309 |
loaded}{Package "memoizable" is loaded by packages which support |
| 310 |
memoization. Memoize must be loaded before all such packages. The |
| 311 |
compilation log can help you figure out which package loaded "memoizable"; |
| 312 |
please move |
| 313 |
%<latex>"\string\usepackage{memoize}" |
| 314 |
%<plain>"\string\input memoize" |
| 315 |
%<context>"\string\usemodule[memoize]" |
| 316 |
before the |
| 317 |
%<latex>"\string\usepackage" |
| 318 |
%<plain>"\string\input" |
| 319 |
%<context>"\string\usemodule" |
| 320 |
of that package.}% |
| 321 |
%<latex>\pgfkeys{/memoize/package options/.unknown/.code={}} |
| 322 |
%<latex>\ProcessPgfPackageOptions{/memoize/package options} |
| 323 |
\endinput |
| 324 |
}{}% |
| 325 |
\ifmmz@loadstatus{1}{\endinput}{}% |
| 326 |
\def\mmz@loadstatus{1}% |
| 327 |
%</mmz> |
| 328 |
%<*mmzable&generic> |
| 329 |
\ifcsname mmz@loadstatus\endcsname\endinput\fi |
| 330 |
\def\mmz@loadstatus{2}% |
| 331 |
%</mmzable&generic> |
| 332 |
%<*nommz> |
| 333 |
\ifmmz@loadstatus{1}{% |
| 334 |
\PackageError{nomemoize}{Cannot load the package, as "memoize" is already |
| 335 |
loaded; memoization will remain in effect}{Packages "memoize" and |
| 336 |
"nomemoize" are mutually exclusive, please load either one or the other.}% |
| 337 |
\endinput }{}% |
| 338 |
\ifmmz@loadstatus{2}{% |
| 339 |
\PackageError{nomemoize}{Cannot load the package, as "memoizable" is already |
| 340 |
loaded}{Package "memoizable" is loaded by packages which support |
| 341 |
memoization. (No)Memoize must be loaded before all such packages. The |
| 342 |
compilation log can help you figure out which package loaded |
| 343 |
"memoizable"; please move |
| 344 |
%<latex>"\string\usepackage{nomemoize}" |
| 345 |
%<plain>"\string\input memoize" |
| 346 |
%<context>"\string\usemodule[memoize]" |
| 347 |
before the |
| 348 |
%<latex>"\string\usepackage" |
| 349 |
%<plain>"\string\input" |
| 350 |
%<context>"\string\usemodule" |
| 351 |
of that package.}% |
| 352 |
\endinput |
| 353 |
}{}% |
| 354 |
\ifmmz@loadstatus{3}{\endinput}{}% |
| 355 |
\def\mmz@loadstatus{3}% |
| 356 |
%</nommz> |
| 357 |
% |
| 358 |
%<*mmz> |
| 359 |
% |
| 360 |
% \begin{macro}{\filetotoks} |
| 361 |
% Read \hologo{TeX} file |#2| into token register |#1| (under the current |
| 362 |
% category code regime); |\toksapp| is defined in CollArgs. |
| 363 |
\def\filetotoks#1#2{% |
| 364 |
\immediate\openin0{#2}% |
| 365 |
#1={}% |
| 366 |
\loop |
| 367 |
\unless\ifeof0 |
| 368 |
\read0 to \totoks@temp |
| 369 |
% We need the |\expandafter|s for our |\toksapp| macro. |
| 370 |
\expandafter\toksapp\expandafter#1\expandafter{\totoks@temp}% |
| 371 |
\repeat |
| 372 |
\immediate\closein0 |
| 373 |
} |
| 374 |
% \end{macro} |
| 375 |
% |
| 376 |
% \paragraph{Other} little things. |
| 377 |
\newif\ifmmz@temp |
| 378 |
\newtoks\mmz@temptoks |
| 379 |
\newbox\mmz@box |
| 380 |
\newwrite\mmz@out |
| 381 |
% |
| 382 |
% \section{The basic configuration} |
| 383 |
% \label{sec:code:configuration} |
| 384 |
% |
| 385 |
% \begin{macro}{\mmzset} |
| 386 |
% The user primarily interacts with Memoize through the |pgfkeys|-based |
| 387 |
% configuration macro |\mmzset|, which executes keys in path |/mmz|. In |
| 388 |
% |nomemoize| and |memoizable|, is exists as a no-op. |
| 389 |
\def\mmzset#1{\pgfqkeys{/mmz}{#1}\ignorespaces} |
| 390 |
%</mmz> |
| 391 |
%<*nommz,mmzable&generic> |
| 392 |
\def\mmzset#1{\ignorespaces} |
| 393 |
%</nommz,mmzable&generic> |
| 394 |
% \end{macro} |
| 395 |
% |
| 396 |
% \begin{macro}{\nommzkeys} |
| 397 |
% Any |/mmz| keys used outside of |\mmzset| must be declared by this macro |
| 398 |
% for |nomemoize| package to work. |
| 399 |
%<mmz>\def\nommzkeys#1{} |
| 400 |
%<*nommz,mmzable&generic> |
| 401 |
\def\nommzkeys{\pgfqkeys{/mmz}} |
| 402 |
\pgfqkeys{/mmz}{.unknown/.code={\pgfkeysdef{\pgfkeyscurrentkey}{}}} |
| 403 |
%</nommz,mmzable&generic> |
| 404 |
% \end{macro} |
| 405 |
% |
| 406 |
% \begin{key}{enable, disable} |
| 407 |
% \begin{macro}{\ifmemoize} |
| 408 |
% These keys set \hologo{TeX}-style conditional \cs{ifmemoize}, used as the |
| 409 |
% central on/off switch for the functionality of the package --- it is |
| 410 |
% inspected in |\Memoize| and by run conditions of automemoization handlers. |
| 411 |
% |
| 412 |
% If used in the preamble, the effect of these keys is delayed until the |
| 413 |
% beginning of the document. The delay is implemented through a special |
| 414 |
% style, |begindocument|, which is executed at |begindocument| hook in |
| 415 |
% \hologo{LaTeX}; in other formats, the user must invoke it manually |
| 416 |
% (\MS\ref{sec:ref:loading}). |
| 417 |
% |
| 418 |
% |Nomemoize| does not need the keys themselves, but it does need the |
| 419 |
% underlying conditional --- which will be always false. |
| 420 |
%<*mmz,nommz,mmzable&generic> |
| 421 |
\newif\ifmemoize |
| 422 |
%</mmz,nommz,mmzable&generic> |
| 423 |
%<*mmz> |
| 424 |
\mmzset{% |
| 425 |
enable/.style={begindocument/.append code=\memoizetrue}, |
| 426 |
disable/.style={begindocument/.append code=\memoizefalse}, |
| 427 |
begindocument/.append style={ |
| 428 |
enable/.code=\memoizetrue, |
| 429 |
disable/.code=\memoizefalse, |
| 430 |
}, |
| 431 |
% Memoize is enabled at the beginning of the document, unless explicitly |
| 432 |
% disabled by the user in the preamble. |
| 433 |
enable, |
| 434 |
% \end{macro} |
| 435 |
% \end{key} |
| 436 |
% |
| 437 |
% \begin{key}{options} |
| 438 |
% Execute the given value as a keylist of Memoize settings. |
| 439 |
options/.style={#1}, |
| 440 |
} |
| 441 |
% \end{key} |
| 442 |
% |
| 443 |
% \begin{key}{normal,readonly,recompile} |
| 444 |
% When Memoize is enabled, it can be in one of three modes |
| 445 |
% (\MS\ref{sec:tut:working-on-a-picture}): normal, readonly, and recompile. |
| 446 |
% The numeric constants are defined below. The mode is stored in |
| 447 |
% |\mmz@mode|, and only matters in |\Memoize| (and |
| 448 |
% |\mmz@process@ccmemo|).\footnote{In fact, this code treats anything but 1 |
| 449 |
% and 2 as normal.} |
| 450 |
\def\mmz@mode@normal{0} |
| 451 |
\def\mmz@mode@readonly{1} |
| 452 |
\def\mmz@mode@recompile{2} |
| 453 |
\let\mmz@mode\mmz@mode@normal |
| 454 |
\mmzset{% |
| 455 |
normal/.code={\let\mmz@mode\mmz@mode@normal}, |
| 456 |
readonly/.code={\let\mmz@mode\mmz@mode@readonly}, |
| 457 |
recompile/.code={\let\mmz@mode\mmz@mode@recompile}, |
| 458 |
} |
| 459 |
% \end{key} |
| 460 |
% \begin{key}{prefix} |
| 461 |
% Key |prefix| determines the location of memo and extern files |
| 462 |
% (|\mmz@prefix@dir|) and the first, fixed part of their basename |
| 463 |
% (|\mmz@prefix@name|). |
| 464 |
\mmzset{% |
| 465 |
prefix/.code={\mmz@parse@prefix{#1}}, |
| 466 |
} |
| 467 |
% \begin{macro}{\mmz@split@prefix} |
| 468 |
% This macro stores the detokenized expansion of |#1| into |\mmz@prefix|, |
| 469 |
% which it then splits into |\mmz@prefix@dir| and |\mmz@prefix@name| at the |
| 470 |
% final |/|. The slash goes into |\mmz@prefix@dir|. If there is no slash, |
| 471 |
% |\mmz@prefix@dir| is empty; in particular, it is empty under |no memo dir|. |
| 472 |
\begingroup |
| 473 |
\catcode`\/=12 |
| 474 |
\gdef\mmz@parse@prefix#1{% |
| 475 |
\edef\mmz@prefix{\detokenize\expandafter{\expanded{#1}}}% |
| 476 |
\def\mmz@prefix@dir{}% |
| 477 |
\def\mmz@prefix@name{}% |
| 478 |
\expandafter\mmz@parse@prefix@i\mmz@prefix/\mmz@eov |
| 479 |
} |
| 480 |
\gdef\mmz@parse@prefix@i#1/#2{% |
| 481 |
\ifx\mmzeov#2% |
| 482 |
\def\mmz@prefix@name{#1}% |
| 483 |
\else |
| 484 |
\appto\mmz@prefix@dir{#1/}% |
| 485 |
\expandafter\mmz@parse@prefix@i\expandafter#2% |
| 486 |
\fi |
| 487 |
} |
| 488 |
\endgroup |
| 489 |
% \end{macro} |
| 490 |
% |
| 491 |
% Key |prefix| concludes by performing two actions: it creates the given |
| 492 |
% directory if |mkdir| is in effect, and notes the new prefix in record files |
| 493 |
% (by eventually executing |record/prefix|, which typically puts a |\mmzPrefix| |
| 494 |
% line in the |.mmz| file). In the preamble, only the final setting of |
| 495 |
% |prefix| matters, so this key is only equipped with the action-triggering |
| 496 |
% code at the beginning of the document. |
| 497 |
\mmzset{% |
| 498 |
begindocument/.append style={ |
| 499 |
prefix/.append code=\mmz@maybe@mkmemodir\mmz@record@prefix, |
| 500 |
}, |
| 501 |
% Consequently, the post-prefix-setting actions must be triggered manually at |
| 502 |
% the beginning of the document. Below, we trigger directory creation; |
| 503 |
% |record/prefix| will be called from |record/begin|, which is executed at |
| 504 |
% the beginning of the document, so it shouldn't be mentioned here. |
| 505 |
begindocument/.append code=\mmz@maybe@mkmemodir, |
| 506 |
} |
| 507 |
% \end{key} |
| 508 |
% |
| 509 |
% \begin{key}{mkdir, mkdir command} |
| 510 |
% Should we create the memo/extern directory if it doesn't exist? And which |
| 511 |
% command should we use to create it? Initially, we attempt to create this |
| 512 |
% directory, and we attempt to do this via |memoize-extract.pl --mkdir|. The |
| 513 |
% roundabout way of setting the initial value of |mkdir command| allows |
| 514 |
% |extract=python| to change the initial value to |memoize-extract.py |
| 515 |
% --mkdir| only in the case the user did not modify it. |
| 516 |
\def\mmz@initial@mkdir@command{\mmzvalueof{perl extraction command} --mkdir} |
| 517 |
\mmzset{ |
| 518 |
% This conditional is perhaps a useless leftover from the early versions, but |
| 519 |
% we let it be. |
| 520 |
mkdir/.is if=mmz@mkdir, |
| 521 |
mkdir command/.store in=\mmz@mkdir@command, |
| 522 |
mkdir command/.expand once=\mmz@initial@mkdir@command, |
| 523 |
} |
| 524 |
% The underlying conditional \cs{ifmmz@mkdir} is only ever used in |
| 525 |
% |\mmz@maybe@mkmemodir| below, which is itself only executed at the end of |
| 526 |
% |prefix| and in |begindocument|. |
| 527 |
\newif\ifmmz@mkdir |
| 528 |
\mmz@mkdirtrue |
| 529 |
% We only attempt to create the memo directory if |\ifmmz@mkdir| is in effect |
| 530 |
% and if both |\mmz@mkdir@command| and |\mmz@prefix@dir| are specified (i.e.\ |
| 531 |
% non-empty). In particular, no attempt to create it will be made when |no |
| 532 |
% memo dir| is in effect. |
| 533 |
\def\mmz@maybe@mkmemodir{% |
| 534 |
\ifmmz@mkdir |
| 535 |
\ifdefempty\mmz@mkdir@command{}{% |
| 536 |
\ifdefempty\mmz@prefix@dir{}{% |
| 537 |
\mmz@remove@quotes{\mmz@prefix@dir}\mmz@temp |
| 538 |
\pdf@system{\mmz@mkdir@command\space"\mmz@temp"}% |
| 539 |
}% |
| 540 |
}% |
| 541 |
\fi |
| 542 |
} |
| 543 |
% \end{key} |
| 544 |
% |
| 545 |
% \begin{key}{memo dir, no memo dir} |
| 546 |
% Shortcuts for two handy settings of |prefix|. Key |no memo dir| will |
| 547 |
% place the memos and externs in the current directory, prefixed with |#1.|, |
| 548 |
% where |#1| defaults to (unquoted) |\jobname|. The default |memo dir| |
| 549 |
% places the memos and externs in a dedicated directory, |#1.memo.dir|; the |
| 550 |
% filenames themselves have no prefix. |
| 551 |
\mmzset{% |
| 552 |
memo dir/.style={prefix={#1.memo.dir/}}, |
| 553 |
memo dir/.default=\jobname, |
| 554 |
no memo dir/.style={prefix={#1.}}, |
| 555 |
no memo dir/.default=\jobname, |
| 556 |
memo dir, |
| 557 |
} |
| 558 |
% \end{key} |
| 559 |
% |
| 560 |
% \begin{macro}{\mmz@remove@quotes} |
| 561 |
% This macro removes fully expands |#1|, detokenizes the expansion and then |
| 562 |
% removes all double quotes the string. The result is stored in the control |
| 563 |
% sequence given in |#2|. |
| 564 |
% |
| 565 |
% We use this macro when we are passing a filename constructed from |
| 566 |
% |\jobname| to external programs. |
| 567 |
\def\mmz@remove@quotes#1#2{% |
| 568 |
\def\mmz@remove@quotes@end{\let#2\mmz@temp}% |
| 569 |
\def\mmz@temp{}% |
| 570 |
\expanded{% |
| 571 |
\noexpand\mmz@remove@quotes@i |
| 572 |
\detokenize\expandafter{\expanded{#1}}% |
| 573 |
"\noexpand\mmz@eov |
| 574 |
}% |
| 575 |
} |
| 576 |
\def\mmz@remove@quotes@i{% |
| 577 |
\CollectArgumentsRaw |
| 578 |
{\collargsReturnPlain |
| 579 |
\collargsNoDelimiterstrue |
| 580 |
\collargsAppendExpandablePostprocessor{{\the\collargsArg}}% |
| 581 |
}% |
| 582 |
{u"u\mmz@eov}% |
| 583 |
\mmz@remove@quotes@ii |
| 584 |
} |
| 585 |
\def\mmz@remove@quotes@ii#1#2{% |
| 586 |
\appto\mmz@temp{#1}% |
| 587 |
\ifx&% |
| 588 |
\mmz@remove@quotes@end |
| 589 |
\expandafter\@gobble |
| 590 |
\else |
| 591 |
\expandafter\@firstofone |
| 592 |
\fi |
| 593 |
{\mmz@remove@quotes@i#2\mmz@eov}% |
| 594 |
} |
| 595 |
% \end{macro} |
| 596 |
% |
| 597 |
% \begin{key}{ignore spaces} |
| 598 |
% The underlying conditional will be inspected by automemoization handlers, |
| 599 |
% to maybe put |\ignorespaces| after the invocation of the handler. |
| 600 |
\newif\ifmmz@ignorespaces |
| 601 |
\mmzset{ |
| 602 |
ignore spaces/.is if=mmz@ignorespaces, |
| 603 |
} |
| 604 |
% \end{key} |
| 605 |
% |
| 606 |
% \begin{key}{verbatim, verb, no verbatim} |
| 607 |
% These keys are tricky. For one, there's |verbatim|, which sets all |
| 608 |
% characters' category codes to other, and there's |verb|, which leaves |
| 609 |
% braces untouched (well, honestly, it redefines them). But Memoize itself |
| 610 |
% doesn't really care about this detail --- it only uses the underlying |
| 611 |
% conditional \cs{ifmmz@verbatim}. It is CollArgs which cares about the |
| 612 |
% difference between the ``long'' and the ``short'' verbatim, so we need to |
| 613 |
% tell it about it. That's why the verbatim options ``append themselves'' to |
| 614 |
% |\mmzRawCollectorOptions|, which is later passed on to |
| 615 |
% |\CollectArgumentsRaw| as a part of its optional argument. |
| 616 |
\newif\ifmmz@verbatim |
| 617 |
\def\mmzRawCollectorOptions{} |
| 618 |
\mmzset{ |
| 619 |
verbatim/.code={% |
| 620 |
\def\mmzRawCollectorOptions{\collargsVerbatim}% |
| 621 |
\mmz@verbatimtrue |
| 622 |
}, |
| 623 |
verb/.code={% |
| 624 |
\def\mmzRawCollectorOptions{\collargsVerb}% |
| 625 |
\mmz@verbatimtrue |
| 626 |
}, |
| 627 |
no verbatim/.code={% |
| 628 |
\def\mmzRawCollectorOptions{\collargsNoVerbatim}% |
| 629 |
\mmz@verbatimfalse |
| 630 |
}, |
| 631 |
} |
| 632 |
% \end{key} |
| 633 |
% \section{Memoization} |
| 634 |
% \label{sec:code:memoization} |
| 635 |
% |
| 636 |
% \subsection{Manual memoization} |
| 637 |
% \label{sec:code:memoization:manual} |
| 638 |
% |
| 639 |
% \begin{macro}{\mmz} |
| 640 |
% The core of this macro will be a simple invocation of |\Memoize|, but to |
| 641 |
% get there, we have to collect the optional argument carefully, because we |
| 642 |
% might have to collect the memoized code verbatim. |
| 643 |
\protected\def\mmz{\futurelet\mmz@temp\mmz@i} |
| 644 |
\def\mmz@i{% |
| 645 |
% Anyone who wants to call |\Memoize| must open a group, because |\Memoize| |
| 646 |
% will close a group. |
| 647 |
\begingroup |
| 648 |
% As the optional argument occurs after a control sequence (|\mmz|), any |
| 649 |
% spaces were consumed and we can immediately test for the opening bracket. |
| 650 |
\ifx\mmz@temp[%] |
| 651 |
\def\mmz@verbatim@fix{}% |
| 652 |
\expandafter\mmz@ii |
| 653 |
\else |
| 654 |
% If there was no optional argument, the opening brace (or the unlikely |
| 655 |
% single token) of our mandatory argument is already tokenized. If we are |
| 656 |
% requested to memoize in a verbatim mode, this non-verbatim tokenization |
| 657 |
% was wrong, so we will use option |\collargsFixFromNoVerbatim| to ask |
| 658 |
% CollArgs to fix the situation. (|\mmz@verbatim@fix| will only be used in |
| 659 |
% the verbatim mode.) |
| 660 |
\def\mmz@verbatim@fix{\noexpand\collargsFixFromNoVerbatim}% |
| 661 |
% No optional argument, so we can skip |\mmz@ii|. |
| 662 |
\expandafter\mmz@iii |
| 663 |
\fi |
| 664 |
} |
| 665 |
\def\mmz@ii[#1]{% |
| 666 |
% Apply the options given in the optional argument. |
| 667 |
\mmzset{#1}% |
| 668 |
\mmz@iii |
| 669 |
} |
| 670 |
\def\mmz@iii{% |
| 671 |
% In the non-verbatim mode, we avoid collecting the single mandatory argument |
| 672 |
% using |\CollectArguments|. |
| 673 |
\ifmmz@verbatim |
| 674 |
\expandafter\mmz@do@verbatim |
| 675 |
\else |
| 676 |
\expandafter\mmz@do |
| 677 |
\fi |
| 678 |
} |
| 679 |
% This macro grabs the mandatory argument of |\mmz| and calls |\Memoize|. |
| 680 |
\long\def\mmz@do#1{% |
| 681 |
\Memoize{#1}{#1}% |
| 682 |
}% |
| 683 |
% |
| 684 |
% The following macro uses |\CollectArgumentsRaw| of package CollArgs |
| 685 |
% (\S\ref{sec:code:collargs}) to grab the argument verbatim; the appropriate |
| 686 |
% verbatim mode triggering raw option was put in |\mmzRawCollectorOptions| by |
| 687 |
% key |verb(atim)|. The macro also |\mmz@verbatim@fix| contains the potential |
| 688 |
% request for a category code fix (\S\ref{sec:code:collargs:fix}). |
| 689 |
\def\mmz@do@verbatim#1{% |
| 690 |
\expanded{% |
| 691 |
\noexpand\CollectArgumentsRaw{% |
| 692 |
\noexpand\collargsCaller{\noexpand\mmz}% |
| 693 |
\expandonce\mmzRawCollectorOptions |
| 694 |
\mmz@verbatim@fix |
| 695 |
}% |
| 696 |
}{+m}\mmz@do |
| 697 |
} |
| 698 |
% \end{macro} |
| 699 |
% |
| 700 |
% \begin{environment}{memoize} |
| 701 |
% The definition of the manual memoization environment proceeds along the |
| 702 |
% same lines as the definition of |\mmz|, except that we also have to |
| 703 |
% implement space-trimming, and that we will collect the environment using |
| 704 |
% |\CollectArguments| in both the verbatim and the non-verbatim and mode. |
| 705 |
% |
| 706 |
% We define the \hologo{LaTeX}, \hologo{plainTeX} and \hologo{ConTeXt} |
| 707 |
% environments in parallel. The definition of the \hologo{plainTeX} and |
| 708 |
% \hologo{ConTeXt} version is complicated by the fact that space-trimming is |
| 709 |
% affected by the presence vs.\ absence of the optional argument (for |
| 710 |
% purposes of space-trimming, it counts as present even if it is empty). |
| 711 |
%<*latex> |
| 712 |
% We define the \hologo{LaTeX} environment using |\newenvironment|, which |
| 713 |
% kindly grabs any spaces in front of the optional argument, if it exists --- |
| 714 |
% and if doesn't, we want to trim spaces at the beginning of the environment |
| 715 |
% body anyway. |
| 716 |
\newenvironment{memoize}[1][\mmz@noarg]{% |
| 717 |
% We close the environment right away. We'll collect the environment body, |
| 718 |
% complete with the end-tag, so we have to reintroduce the end-tag somewhere. |
| 719 |
% Another place would be after the invocation of |\Memoize|, but that would |
| 720 |
% put memoization into a double group and |\mmzAfterMemoization| would not work. |
| 721 |
\end{memoize}% |
| 722 |
% % We open the group which will be closed by |\Memoize|. |
| 723 |
\begingroup |
| 724 |
% As with |\mmz| above, if there was no optional argument, we have to ask |
| 725 |
% Collargs for a fix. The difference is that, as we have collected the |
| 726 |
% optional argument via |\newcommand|, we have to test for its presence in a |
| 727 |
% roundabout way. |
| 728 |
\def\mmz@temp{#1}% |
| 729 |
\ifx\mmz@temp\mmz@noarg |
| 730 |
\def\mmz@verbatim@fix{\noexpand\collargsFixFromNoVerbatim}% |
| 731 |
\else |
| 732 |
\def\mmz@verbatim@fix{}% |
| 733 |
\mmzset{#1}% |
| 734 |
\fi |
| 735 |
\mmz@env@iii |
| 736 |
}{} |
| 737 |
\def\mmz@noarg{\mmz@noarg} |
| 738 |
%</latex> |
| 739 |
%<plain>\def\memoize{% |
| 740 |
%<context>\def\startmemoize{% |
| 741 |
%<*plain,context> |
| 742 |
\begingroup |
| 743 |
% In \hologo{plainTeX} and \hologo{ConTeXt}, we don't have to worry about any |
| 744 |
% spaces in front of the optional argument, as the environments are opened by |
| 745 |
% a control sequence. |
| 746 |
\futurelet\mmz@temp\mmz@env@i |
| 747 |
} |
| 748 |
\def\mmz@env@i{% |
| 749 |
\ifx\mmz@temp[%] |
| 750 |
\def\mmz@verbatim@fix{}% |
| 751 |
\expandafter\mmz@env@ii |
| 752 |
\else |
| 753 |
\def\mmz@verbatim@fix{\noexpand\collargsFixFromNoVerbatim}% |
| 754 |
\expandafter\mmz@env@iii |
| 755 |
\fi |
| 756 |
} |
| 757 |
\def\mmz@env@ii[#1]{% |
| 758 |
\mmzset{#1}% |
| 759 |
\mmz@env@iii |
| 760 |
} |
| 761 |
%</plain,context> |
| 762 |
\def\mmz@env@iii{% |
| 763 |
\long\edef\mmz@do##1{% |
| 764 |
% |\unskip| will ``trim'' spaces at the end of the environment body. |
| 765 |
\noexpand\Memoize{##1}{##1\unskip}% |
| 766 |
}% |
| 767 |
\expanded{% |
| 768 |
\noexpand\CollectArgumentsRaw{% |
| 769 |
% |\CollectArgumentsRaw| will adapt the caller to the format automatically. |
| 770 |
\noexpand\collargsCaller{memoize}% |
| 771 |
% |verb(atim)| is in here if it was requested. |
| 772 |
\expandonce\mmzRawCollectorOptions |
| 773 |
% The category code fix, if needed. |
| 774 |
\ifmmz@verbatim\mmz@verbatim@fix\fi |
| 775 |
}% |
| 776 |
% Spaces at the beginning of the environment body are trimmed by setting |
| 777 |
% the first argument to |!t<space>| and disappearing it with |
| 778 |
% |\collargsAppendExpandablePostprocessor{}|; note that this removes any number of space |
| 779 |
% tokens. |\CollectArgumentsRaw| automatically adapts the argument type |
| 780 |
% |b| to the format. |
| 781 |
% |
| 782 |
}{&&{\collargsAppendExpandablePostprocessor{}}!t{ }+b{memoize}}{\mmz@do}% |
| 783 |
}% |
| 784 |
%</mmz> |
| 785 |
% \end{environment} |
| 786 |
% |
| 787 |
% \begin{macro}{\nommz} |
| 788 |
% We throw away the optional argument if present, and replace the opening |
| 789 |
% brace with begin-group plus |\memoizefalse|. This way, the ``argument'' of |
| 790 |
% |\nommz| will be processed in a group (with Memoize disabled) and even the |
| 791 |
% verbatim code will work because the ``argument'' will not have been |
| 792 |
% tokenized. |
| 793 |
% |
| 794 |
% As a user command, |\nommz| has to make it into package |nomemoize| as |
| 795 |
% well, and we'll |\let| |\mmz| equal it there; it is not needed in |
| 796 |
% |mmzable|. |
| 797 |
%<*mmz,nommz> |
| 798 |
\protected\def\nommz#1#{% |
| 799 |
\afterassignment\nommz@i |
| 800 |
\let\mmz@temp |
| 801 |
} |
| 802 |
\def\nommz@i{% |
| 803 |
\bgroup |
| 804 |
\memoizefalse |
| 805 |
} |
| 806 |
%<nommz>\let\mmz\nommz |
| 807 |
% \end{macro} |
| 808 |
% |
| 809 |
% \begin{environment}{nomemoize} |
| 810 |
% We throw away the optional argument and take care of the spaces at the |
| 811 |
% beginning and at the end of the body. |
| 812 |
%<*latex> |
| 813 |
\newenvironment{nomemoize}[1][]{% |
| 814 |
\memoizefalse |
| 815 |
\ignorespaces |
| 816 |
}{% |
| 817 |
\unskip |
| 818 |
} |
| 819 |
%</latex> |
| 820 |
%<*plain,context> |
| 821 |
%<plain>\def\nomemoize{% |
| 822 |
%<context>\def\startnomemoize{% |
| 823 |
% Start a group to delimit |\memoizefalse|. |
| 824 |
\begingroup |
| 825 |
\memoizefalse |
| 826 |
\futurelet\mmz@temp\nommz@env@i |
| 827 |
} |
| 828 |
\def\nommz@env@i{% |
| 829 |
\ifx\mmz@temp[%] |
| 830 |
\expandafter\nommz@env@ii |
| 831 |
% No optional argument, no problems with spaces. |
| 832 |
\fi |
| 833 |
} |
| 834 |
\def\nommz@env@ii[#1]{% |
| 835 |
\ignorespaces |
| 836 |
} |
| 837 |
%<plain>\def\endnomemoize{% |
| 838 |
%<context>\def\stopnomemoize{% |
| 839 |
\endgroup |
| 840 |
\unskip |
| 841 |
} |
| 842 |
%</plain,context> |
| 843 |
%<*nommz> |
| 844 |
%<plain,latex>\let\memoize\nomemoize |
| 845 |
%<plain,latex>\let\endmemoize\endnomemoize |
| 846 |
%<context>\let\startmemoize\startnomemoize |
| 847 |
%<context>\let\stopmemoize\stopnomemoize |
| 848 |
%</nommz> |
| 849 |
%</mmz,nommz> |
| 850 |
% \end{environment} |
| 851 |
% |
| 852 |
% \subsection{The memoization process} |
| 853 |
% \label{sec:code:memoization-process} |
| 854 |
% |
| 855 |
% \begin{macro}{\ifmemoizing} |
| 856 |
% This conditional is set to true when we start memoization (but not when |
| 857 |
% we start regular compilation or utilization); it should never be set |
| 858 |
% anywhere else. It is checked by |\Memoize| to prevent nested |
| 859 |
% memoizations, deployed in advice run conditions set by |run only if |
| 860 |
% memoizing|, etc. |
| 861 |
%<*mmz,nommz,mmzable&generic> |
| 862 |
\newif\ifmemoizing |
| 863 |
% \end{macro} |
| 864 |
% |
| 865 |
% \begin{macro}{\ifinmemoize} |
| 866 |
% This conditional is set to true when we start either memoization or |
| 867 |
% regular compilation (but not when we start utilization); it should never |
| 868 |
% be set anywhere else. It is deployed in the default advice run conditions, |
| 869 |
% making sure that automemoized commands are not handled even when we're |
| 870 |
% regularly compiling some code submitted to memoization. |
| 871 |
\newif\ifinmemoize |
| 872 |
% \end{macro} |
| 873 |
% |
| 874 |
% \begin{macro}{\mmz@maybe@scantokens} |
| 875 |
% An auxiliary macro which rescans the given |
| 876 |
% code using |\scantokens| if the verbatim mode is active. We also need it |
| 877 |
% in NoMemoize, to properly grab verbatim manually memoized code. |
| 878 |
%</mmz,nommz,mmzable&generic> |
| 879 |
%<*mmz> |
| 880 |
\def\mmz@maybe@scantokens{% |
| 881 |
\ifmmz@verbatim |
| 882 |
\expandafter\mmz@scantokens |
| 883 |
\else |
| 884 |
\expandafter\@firstofone |
| 885 |
\fi |
| 886 |
} |
| 887 |
% Without |\newlinechar=13|, |\scantokens| would see receive the entire |
| 888 |
% argument as one long line --- but it would not \emph{see} the entire |
| 889 |
% argument, but only up to the first newline character, effectively losing most |
| 890 |
% of the tokens. (We need to manually save and restore |\newlinechar| because |
| 891 |
% we don't want to execute the memoized code in yet another group.) |
| 892 |
\long\def\mmz@scantokens#1{% |
| 893 |
\expanded{% |
| 894 |
\newlinechar=13 |
| 895 |
\unexpanded{\scantokens{#1\endinput}}% |
| 896 |
\newlinechar=\the\newlinechar |
| 897 |
}% |
| 898 |
} |
| 899 |
% \end{macro} |
| 900 |
% |
| 901 |
% \begin{macro}{\Memoize} |
| 902 |
% Memoization is invoked by executing |\Memoize|. This macro is a decision |
| 903 |
% hub. It test for the existence of the memos and externs associated with |
| 904 |
% the memoized code, and takes the appropriate action (memoization: |
| 905 |
% |\mmz@memoize|; regular compilation: |\mmz@compile|, utilization: |
| 906 |
% |\mmz@process@cmemo| plus |\mmz@process@ccmemo| plus further complications) |
| 907 |
% depending on the memoization mode (normal, readonly, recompile). Note that |
| 908 |
% one should open a \hologo{TeX} group prior to executing |\Memoize|, because |
| 909 |
% |\Memoize| will close a group (\MS\ref{sec:Memoize}). |
| 910 |
% |
| 911 |
% |\Memoize| takes two arguments, which contain two potentially different |
| 912 |
% versions of the code submitted to memoization: |#1| contains the code which |
| 913 |
% \meta{code MD5 sum} is computed off of, while |#2| contains the code which |
| 914 |
% is actually executed during memoization and regular compilation. The |
| 915 |
% arguments will contain the same code in the case of manual memoization, but |
| 916 |
% they will differ in the case of automemoization, where the executable code |
| 917 |
% will typically prefixed by |\AdviceOriginal|. As the two |
| 918 |
% codes will be used not only by |\Memoize| but also by macros called from |
| 919 |
% |\Memoize|, |\Memoize| stores them into dedicated toks registers, declared |
| 920 |
% below. |
| 921 |
\newtoks\mmz@mdfive@source |
| 922 |
\newtoks\mmz@exec@source |
| 923 |
% Finally, the definition of the macro. In package NoMemoize, we should simply |
| 924 |
% execute the code in the second argument. But in Memoize, we have work to do. |
| 925 |
\let\Memoize\@secondoftwo |
| 926 |
\long\def\Memoize#1#2{% |
| 927 |
% We store the first argument into token register |\mmz@mdfive@source| |
| 928 |
% because we might have to include it in tracing info (when |trace| is in |
| 929 |
% effect), or paste it into the c-memo (depending on |include source in |
| 930 |
% cmemo|). |
| 931 |
\mmz@mdfive@source{#1}% |
| 932 |
% We store the executable code in |\mmz@exec@source|. In the verbatim mode, |
| 933 |
% the code will have to be rescanned. This is implemented by |
| 934 |
% |\mmz@maybe@scantokens|, and we wrap the code into this macro right away, |
| 935 |
% once and for all. Even more, we pre-expand |\mmz@maybe@scantokens| (three |
| 936 |
% times), effectively applying the current \cs{ifmmz@verbatim} and |
| 937 |
% eliminating the need to save and restore this conditional in |
| 938 |
% |\mmz@compile|, which (regularly) compiles the code \emph{after} closing |
| 939 |
% the |\Memoize| group --- after this pre-expansion, |\mmz@exec@source| will |
| 940 |
% contain either |\mmz@scantokens{...}| or |\@firstofone{...}|. |
| 941 |
\expandafter\expandafter\expandafter\expandafter |
| 942 |
\expandafter\expandafter\expandafter |
| 943 |
\mmz@exec@source |
| 944 |
\expandafter\expandafter\expandafter\expandafter |
| 945 |
\expandafter\expandafter\expandafter |
| 946 |
{% |
| 947 |
\mmz@maybe@scantokens{#2}% |
| 948 |
}% |
| 949 |
\mmz@trace@Memoize |
| 950 |
% In most branches below, we end up with regular compilation, so let this be |
| 951 |
% the default action. |
| 952 |
\let\mmz@action\mmz@compile |
| 953 |
% If Memoize is disabled, or if memoization is currently taking place, we |
| 954 |
% will perform a regular compilation. |
| 955 |
\ifmemoizing |
| 956 |
\else |
| 957 |
\ifmemoize |
| 958 |
% Compute \meta{code md5sum} off of the salted source code, and globally |
| 959 |
% store it into |\mmz@code@mdfivesum| --- globally, because we need it in |
| 960 |
% utilization to include externs, but the |\Memoize| group is closed (by |
| 961 |
% |\mmzMemo|) while inputting the cc-memo. |
| 962 |
\xdef\mmz@code@mdfivesum{\pdf@mdfivesum{% |
| 963 |
\expanded{\the\mmzSalt}% |
| 964 |
\the\mmz@mdfive@source |
| 965 |
}}% |
| 966 |
\mmz@trace@code@mdfive |
| 967 |
% Recompile mode forces memoization. |
| 968 |
\ifnum\mmz@mode=\mmz@mode@recompile\relax |
| 969 |
\ifnum\pdf@draftmode=0 |
| 970 |
\let\mmz@action\mmz@memoize |
| 971 |
\fi |
| 972 |
\else |
| 973 |
% In the normal and the readonly mode, we try to utilize the memos. The |
| 974 |
% c-memo comes first. If the c-memo does not exist (or if something is |
| 975 |
% wrong with it), |\mmz@process@cmemo| (defined in |
| 976 |
% \S\ref{sec:code:c-memo}) will set |\ifmmz@abort| to true. It might |
| 977 |
% also set |\ifmmzUnmemoizable| which means we should compile normally |
| 978 |
% regardless of the mode. |
| 979 |
\mmz@process@cmemo |
| 980 |
\ifmmzUnmemoizable |
| 981 |
\mmz@trace@cmemo@unmemoizable |
| 982 |
\else |
| 983 |
\ifmmz@abort |
| 984 |
% If there is no c-memo, or it is invalid, we memoize, unless the |
| 985 |
% read-only mode is in effect. |
| 986 |
\mmz@trace@process@cmemo@fail |
| 987 |
\ifnum\mmz@mode=\mmz@mode@readonly\relax |
| 988 |
\else |
| 989 |
\ifnum\pdf@draftmode=0 |
| 990 |
\let\mmz@action\mmz@memoize |
| 991 |
\fi |
| 992 |
\fi |
| 993 |
\else |
| 994 |
\mmz@trace@process@cmemo@ok |
| 995 |
% If the c-memo was fine, the formal action decided upon is to try |
| 996 |
% utilizing the cc-memo. If it exists and everything is fine with |
| 997 |
% it, |\mmz@process@ccmemo| (defined in |
| 998 |
% section~\ref{sec:code:cc-memo}) will utilize it, i.e.\ the core |
| 999 |
% of the cc-memo (the part following |\mmzMemo|) will be executed |
| 1000 |
% (typically including the single extern). Otherwise, |
| 1001 |
% |\mmz@process@ccmemo| will trigger either memoization (in the |
| 1002 |
% normal mode) or regular compilation (in the readonly mode). This |
| 1003 |
% final decision is left to |\mmz@process@ccmemo| because if we |
| 1004 |
% made it here, the code would get complicated, as the cc-memo must |
| 1005 |
% be processed outside the |\Memoize| group and all the |
| 1006 |
% conditionals in this macro. |
| 1007 |
\let\mmz@action\mmz@process@ccmemo |
| 1008 |
\fi |
| 1009 |
\fi |
| 1010 |
\fi |
| 1011 |
\fi |
| 1012 |
\fi |
| 1013 |
\mmz@action |
| 1014 |
} |
| 1015 |
% \end{macro} |
| 1016 |
% |
| 1017 |
% \begin{macro}{\mmz@compile} |
| 1018 |
% This macro performs regular compilation --- this is signalled to the |
| 1019 |
% memoized code and the memoization driver by setting \cs{ifinmemoize} to |
| 1020 |
% true for the duration of the compilation; \cs{ifmemoizing} is not touched. |
| 1021 |
% The group opened prior to the invocation of |\Memoize| is closed before |
| 1022 |
% executing the code in |\mmz@exec@source|, so that compiling the code has |
| 1023 |
% the same local effect as if was not submitted to memoization; it is closing |
| 1024 |
% this group early which complicates the restoration of \cs{ifinmemoize} at |
| 1025 |
% the end of compilation. Note that |\mmz@exec@source| is already set to |
| 1026 |
% properly deal with the current verbatim mode, so any further inspection of |
| 1027 |
% \cs{ifmmz@verbatim} is unnecessary; the same goes for |
| 1028 |
% \cs{ifmmz@ignorespaces}, which was (or at least should be) taken care of by |
| 1029 |
% whoever called |\Memoize|. |
| 1030 |
\def\mmz@compile{% |
| 1031 |
\mmz@trace@compile |
| 1032 |
\expanded{% |
| 1033 |
\endgroup |
| 1034 |
\noexpand\inmemoizetrue |
| 1035 |
\the\mmz@exec@source |
| 1036 |
\ifinmemoize\noexpand\inmemoizetrue\else\noexpand\inmemoizefalse\fi |
| 1037 |
}% |
| 1038 |
} |
| 1039 |
% \end{macro} |
| 1040 |
% \begin{luafun}{abortOnError} |
| 1041 |
% \begin{macro}{\mmz@lua@atbeginmemoization,\mmz@lua@atendmemoization} |
| 1042 |
% In \hologo{LuaTeX}, we can whether an error occurred during memoization, |
| 1043 |
% and abort if it did. (We're going through |memoize.abort|, because |
| 1044 |
% |tex.print| does not seem to work during error handling.) We omit all |
| 1045 |
% this in \hologo{ConTeXt}, as it appears to stop on any error? |
| 1046 |
%<*!context> |
| 1047 |
\ifdefined\luatexversion |
| 1048 |
\directlua{% |
| 1049 |
luatexbase.add_to_callback( |
| 1050 |
"show_error_message", |
| 1051 |
function() |
| 1052 |
memoize.abort = true |
| 1053 |
texio.write_nl(status.lasterrorstring) |
| 1054 |
end, |
| 1055 |
"Abort memoization on error" |
| 1056 |
) |
| 1057 |
}% |
| 1058 |
\def\mmz@lua@atbeginmemoization{% |
| 1059 |
\directlua{memoize.abort = false}% |
| 1060 |
}% |
| 1061 |
\def\mmz@lua@atendmemoization{% |
| 1062 |
\directlua{% |
| 1063 |
if memoize.abort then |
| 1064 |
tex.print("\noexpand\\mmzAbort") |
| 1065 |
end |
| 1066 |
}% |
| 1067 |
}% |
| 1068 |
\else |
| 1069 |
%</!context> |
| 1070 |
\let\mmz@lua@atbeginmemoization\relax |
| 1071 |
\let\mmz@lua@atendmemoization\relax |
| 1072 |
%<!context>\fi |
| 1073 |
% \end{macro} |
| 1074 |
% \end{luafun} |
| 1075 |
% \begin{macro}{\mmz@memoize} |
| 1076 |
% This macro performs memoization --- this is signalled to the memoized code |
| 1077 |
% and the memoization driver by setting both \cs{ifinmemoize} and |
| 1078 |
% \cs{ifinmemoizing} to true. |
| 1079 |
\def\mmz@memoize{% |
| 1080 |
\mmz@trace@memoize |
| 1081 |
\memoizingtrue |
| 1082 |
\inmemoizetrue |
| 1083 |
% Initialize the various macros and registers used in memoization (to be |
| 1084 |
% described below, or later). Note that most of these are global, as they |
| 1085 |
% might be adjusted arbitrarily deep within the memoized code. |
| 1086 |
\edef\memoizinggrouplevel{\the\currentgrouplevel}% |
| 1087 |
\global\mmz@abortfalse |
| 1088 |
\global\mmzUnmemoizablefalse |
| 1089 |
\global\mmz@seq 0 |
| 1090 |
\global\setbox\mmz@tbe@box\vbox{}% |
| 1091 |
\global\mmz@ccmemo@resources{}% |
| 1092 |
\global\mmzCMemo{}% |
| 1093 |
\global\mmzCCMemo{}% |
| 1094 |
\global\mmzContextExtra{}% |
| 1095 |
\gdef\mmzAtEndMemoizationExtra{}% |
| 1096 |
\gdef\mmzAfterMemoizationExtra{}% |
| 1097 |
\mmz@lua@atbeginmemoization |
| 1098 |
% Execute the pre-memoization hook, the memoized code (wrapped in the |
| 1099 |
% driver), and the post-memoization hook. |
| 1100 |
\mmzAtBeginMemoization |
| 1101 |
\mmzDriver{\the\mmz@exec@source}% |
| 1102 |
\mmzAtEndMemoization |
| 1103 |
\mmzAtEndMemoizationExtra |
| 1104 |
\mmz@lua@atendmemoization |
| 1105 |
\ifmmzUnmemoizable |
| 1106 |
% To permanently prevent memoization, we have to write down the c-memo |
| 1107 |
% (containing |\mmzUnmemoizabletrue|). We don't need the extra context in |
| 1108 |
% this case. |
| 1109 |
\global\mmzContextExtra{}% |
| 1110 |
\gtoksapp\mmzCMemo{\global\mmzUnmemoizabletrue}% |
| 1111 |
\mmz@write@cmemo |
| 1112 |
\mmz@trace@endmemoize@unmemoizable |
| 1113 |
\PackageInfo{memoize}{Marking this code as unmemoizable}% |
| 1114 |
\else |
| 1115 |
\ifmmz@abort |
| 1116 |
% If memoization was aborted, we create an empty c-memo, to make sure that |
| 1117 |
% no leftover c-memo tricks Memoize into thinking that the code was |
| 1118 |
% successfully memoized. |
| 1119 |
\mmz@trace@endmemoize@aborted |
| 1120 |
\PackageInfo{memoize}{Memoization was aborted}% |
| 1121 |
\mmz@compute@context@mdfivesum |
| 1122 |
\mmz@write@cmemo |
| 1123 |
\else |
| 1124 |
% If memoization was not aborted, we compute the \meta{context md5sum}, |
| 1125 |
% open and write out the memos, and shipout the externs (as pages into the |
| 1126 |
% document). |
| 1127 |
\mmz@compute@context@mdfivesum |
| 1128 |
\mmz@write@cmemo |
| 1129 |
\mmz@write@ccmemo |
| 1130 |
\mmz@shipout@externs |
| 1131 |
\mmz@trace@endmemoize@ok |
| 1132 |
\fi |
| 1133 |
\fi |
| 1134 |
% After closing the group, we execute the final, after-memoization hook (we |
| 1135 |
% pre-expand the regular macro; the extra macro was assigned to globally). In |
| 1136 |
% the after-memoization code, |\mmzIncludeExtern| points to a macro which can |
| 1137 |
% include the extern from |\mmz@tbe@box|, which makes it possible to typeset |
| 1138 |
% the extern by dropping the contents of |\mmzCCMemo| into this hook --- but |
| 1139 |
% note that this will only work if |\ifmmzkeepexterns| was in effect at the |
| 1140 |
% end of memoization. |
| 1141 |
\expandafter\endgroup |
| 1142 |
\expandafter\let |
| 1143 |
\expandafter\mmzIncludeExtern\expandafter\mmz@include@extern@from@tbe@box |
| 1144 |
\mmzAfterMemoization |
| 1145 |
\mmzAfterMemoizationExtra |
| 1146 |
} |
| 1147 |
% \end{macro} |
| 1148 |
% |
| 1149 |
% \begin{macro}{\memoizinggrouplevel} |
| 1150 |
% This macro stores the group level at the beginning of memoization. It is |
| 1151 |
% deployed by |\IfMemoizing|, normally used by integrated drivers. |
| 1152 |
\def\memoizinggrouplevel{-1}% |
| 1153 |
% \end{macro} |
| 1154 |
% |
| 1155 |
% \begin{macro}{\mmzAbort} |
| 1156 |
% Memoized code may execute this macro to abort memoization. |
| 1157 |
\def\mmzAbort{\global\mmz@aborttrue} |
| 1158 |
% \end{macro} |
| 1159 |
% |
| 1160 |
% \begin{macro}{\ifmmz@abort} |
| 1161 |
% This conditional serves as a signal that something went wrong during |
| 1162 |
% memoization (where it is set to true by |\mmzAbort|), or c(c)-memo |
| 1163 |
% processing. The assignment to this conditional should always be global |
| 1164 |
% (because it may be set during memoization). |
| 1165 |
\newif\ifmmz@abort |
| 1166 |
% \end{macro} |
| 1167 |
% |
| 1168 |
% \begin{macro}{\mmzUnmemoizable} |
| 1169 |
% Memoized code may execute |\mmzUnmemoizable| to abort memoization and mark |
| 1170 |
% (in the c-memo) that memoization should never be attempted again. The |
| 1171 |
% c-memo is composed by |\mmz@memoize|. |
| 1172 |
\def\mmzUnmemoizable{\global\mmzUnmemoizabletrue} |
| 1173 |
% \end{macro} |
| 1174 |
% |
| 1175 |
% \begin{macro}{\ifmmzUnmemoizable} |
| 1176 |
% This conditional serves as a signal that the code should never be |
| 1177 |
% memoized. It can be set (a) during memoization (that's why it should be |
| 1178 |
% assigned globally), after which it is inspected by |\mmz@memoize|, and |
| 1179 |
% (b) from the c-memo, in which case it is inspected by |\Memoize|. |
| 1180 |
\newif\ifmmzUnmemoizable |
| 1181 |
% \end{macro} |
| 1182 |
% |
| 1183 |
% \begin{macro}{\mmzAtBeginMemoization,\mmzAtEndMemoization,\mmzAfterMemoization} |
| 1184 |
% \begin{key}{at begin memoization, at end memoization, after memoization} |
| 1185 |
% The memoization hooks and their keys. The hook macros may be set either |
| 1186 |
% before or during memoization. In the former case, one should modify the |
| 1187 |
% primary macro (|\mmzAtBeginMemoization|, |\mmzAtEndMemoization|, |
| 1188 |
% |\mmzAfterMemoization|), and the assignment should be local. In the |
| 1189 |
% latter case, one should modify the extra macro |
| 1190 |
% (|\mmzAtEndMemoizationExtra|, |\mmzAfterMemoizationExtra|; there is no |
| 1191 |
% |\mmzAtBeginMemoizationExtra|), and the assignment should be global. The |
| 1192 |
% keys automatically adapt to the situation, by appending either to the |
| 1193 |
% primary or the the extra macro; if |at begin memoization| is used during |
| 1194 |
% memoization, the given code is executed immediately. We will use this |
| 1195 |
% ``extra'' approach and the auto-adapting keys for other options, like |
| 1196 |
% |context|, as well. |
| 1197 |
\def\mmzAtBeginMemoization{} |
| 1198 |
\def\mmzAtEndMemoization{} |
| 1199 |
\def\mmzAfterMemoization{} |
| 1200 |
\mmzset{ |
| 1201 |
at begin memoization/.code={% |
| 1202 |
\ifmemoizing |
| 1203 |
\expandafter\@firstofone |
| 1204 |
\else |
| 1205 |
\expandafter\appto\expandafter\mmzAtBeginMemoization |
| 1206 |
\fi |
| 1207 |
{#1}% |
| 1208 |
}, |
| 1209 |
at end memoization/.code={% |
| 1210 |
\ifmemoizing |
| 1211 |
\expandafter\gappto\expandafter\mmzAtEndMemoizationExtra |
| 1212 |
\else |
| 1213 |
\expandafter\appto\expandafter\mmzAtEndMemoization |
| 1214 |
\fi |
| 1215 |
{#1}% |
| 1216 |
}, |
| 1217 |
after memoization/.code={% |
| 1218 |
\ifmemoizing |
| 1219 |
\expandafter\gappto\expandafter\mmzAfterMemoizationExtra |
| 1220 |
\else |
| 1221 |
\expandafter\appto\expandafter\mmzAfterMemoization |
| 1222 |
\fi |
| 1223 |
{#1}% |
| 1224 |
}, |
| 1225 |
} |
| 1226 |
% \end{key} |
| 1227 |
% \end{macro} |
| 1228 |
% |
| 1229 |
% \begin{key}{driver} |
| 1230 |
% This key sets the (formal) memoization driver. The function of the driver |
| 1231 |
% is to produce the memos and externs while executing the submitted code. |
| 1232 |
\mmzset{ |
| 1233 |
driver/.store in=\mmzDriver, |
| 1234 |
driver=\mmzSingleExternDriver, |
| 1235 |
} |
| 1236 |
% \end{key} |
| 1237 |
% |
| 1238 |
% \begin{macro}{\ifmmzkeepexterns} |
| 1239 |
% This conditional causes Memoize not to empty out |\mmz@tbe@box|, holding |
| 1240 |
% the externs collected during memoization, while shipping them out. |
| 1241 |
\newif\ifmmzkeepexterns |
| 1242 |
% \end{macro} |
| 1243 |
% |
| 1244 |
% \begin{macro}{\mmzSingleExternDriver} |
| 1245 |
% The default memoization driver externalizes the submitted code. It always |
| 1246 |
% produces exactly one extern, and including the extern will be the only |
| 1247 |
% effect of inputting the cc-memo (unless the memoized code contained some |
| 1248 |
% commands, like |\label|, which added extra instructions to the cc-memo.) |
| 1249 |
% The macro (i) adds |\quitvmode| to the cc-memo, if we're capturing into a |
| 1250 |
% horizontal box, and it puts it to the very front, so that it comes before |
| 1251 |
% any |\label| and |\index| replications, guaranteeing (hopefully) that they |
| 1252 |
% refer to the correct page; (ii) takes the code and typesets it in a box |
| 1253 |
% (|\mmz@box|); (iii) submits the box for externalization; (iv) adds the |
| 1254 |
% extern-inclusion code to the cc-memo, and (v) puts the box into the |
| 1255 |
% document (again prefixing it with |\quitvmode| if necessary). (The listing |
| 1256 |
% region markers help us present this code in the manual.) |
| 1257 |
% \begin{listingregion}{single-extern-driver} |
| 1258 |
\long\def\mmzSingleExternDriver#1{% |
| 1259 |
\xtoksapp\mmzCCMemo{\mmz@maybe@quitvmode}% |
| 1260 |
\setbox\mmz@box\mmz@capture{#1}% |
| 1261 |
\mmzExternalizeBox\mmz@box\mmz@temptoks |
| 1262 |
\xtoksapp\mmzCCMemo{\the\mmz@temptoks}% |
| 1263 |
\mmz@maybe@quitvmode\box\mmz@box |
| 1264 |
} |
| 1265 |
% \end{listingregion} |
| 1266 |
% \end{macro} |
| 1267 |
% |
| 1268 |
% \begin{key}{capture} |
| 1269 |
% The default memoization driver uses |\mmz@capture| and |
| 1270 |
% |\mmz@maybe@quitvmode|, which are set by this key. |\mmz@maybe@quitvmode| |
| 1271 |
% will be expanded, but for \hologo{XeTeX}, we have defined |\quitvmode| as a |
| 1272 |
% synonym for |\leavevmode|, which is a macro rather than a primitive, so we |
| 1273 |
% have to prevent its expansion in that case. It is easiest to just add |
| 1274 |
% |\noexpand|, regardless of the engine used. |
| 1275 |
\mmzset{ |
| 1276 |
capture/.is choice, |
| 1277 |
capture/hbox/.code={% |
| 1278 |
\let\mmz@capture\hbox |
| 1279 |
\def\mmz@maybe@quitvmode{\noexpand\quitvmode}% |
| 1280 |
}, |
| 1281 |
capture/vbox/.code={% |
| 1282 |
\let\mmz@capture\vbox |
| 1283 |
\def\mmz@maybe@quitvmode{}% |
| 1284 |
}, |
| 1285 |
capture=hbox, |
| 1286 |
} |
| 1287 |
% \end{key} |
| 1288 |
% The memoized code may be memoization-aware; in such a case, we say that the |
| 1289 |
% driver is \emph{integrated} into the code. Code containing an integrated |
| 1290 |
% driver must take care to execute it only when memoizing, and not during a |
| 1291 |
% regular compilation. The following key and macro can help here, see |
| 1292 |
% \MS\ref{sec:memoization-complex-single-driver} for details. |
| 1293 |
% |
| 1294 |
% \begin{mmzautokey}{integrated driver} |
| 1295 |
% This is an advice key, residing in |/mmz/auto|. Given \meta{suffix} as the |
| 1296 |
% only argument, it declares conditional \cs{ifmemoizing}\meta{suffix}, and |
| 1297 |
% sets the driver for the automemoized command to a macro which sets this |
| 1298 |
% conditional to true. The declared conditional is \emph{internal} and |
| 1299 |
% should not be used directly, but only via |\IfMemoizing| --- because it |
| 1300 |
% will not be declared when package NoMemoize or only Memoizable is loaded. |
| 1301 |
\mmzset{ |
| 1302 |
auto/integrated driver/.style={ |
| 1303 |
after setup={\expandafter\newif\csname ifmmz@memoizing#1\endcsname}, |
| 1304 |
driver/.expand once={% |
| 1305 |
\csname mmz@memoizing#1true\endcsname |
| 1306 |
% Without this, we would introduce an extra group around the memoized |
| 1307 |
% code. |
| 1308 |
\@firstofone |
| 1309 |
}% |
| 1310 |
}, |
| 1311 |
} |
| 1312 |
% \end{mmzautokey} |
| 1313 |
% |
| 1314 |
% \begin{macro}{\IfMemoizing} |
| 1315 |
% Without the optional argument, the condition is satisfied when the internal |
| 1316 |
% conditional \cs{ifmemoizing}\meta{suffix}, declared by |integrated driver|, |
| 1317 |
% is true. With the optional argument \meta{offset}, the current group level |
| 1318 |
% must additionally match the memoizing group level, modulo \meta{offset} --- |
| 1319 |
% this makes sure that the conditional comes out as false in a regular |
| 1320 |
% compilation embedded in a memoization. |
| 1321 |
\newcommand\IfMemoizing[2][\mmz@Ifmemoizing@nogrouplevel]{%>\fi |
| 1322 |
\csname ifmmz@memoizing#2\endcsname%>\if |
| 1323 |
% One |\relax| is for the |\numexpr|, another for |\ifnum|. Complications |
| 1324 |
% arise when |#1| is the optional argument default (defined below). In |
| 1325 |
% that case, the content of |\mmz@Ifmemoizing@nogrouplevel| closes off the |
| 1326 |
% |\ifnum| conditional (with both the true and the false branch empty), and |
| 1327 |
% opens up a new one, |\iftrue|. Effectively, we're not testing for the |
| 1328 |
% group level match. |
| 1329 |
\ifnum\currentgrouplevel=\the\numexpr\memoizinggrouplevel+#1\relax\relax |
| 1330 |
\expandafter\expandafter\expandafter\@firstoftwo |
| 1331 |
\else |
| 1332 |
\expandafter\expandafter\expandafter\@secondoftwo |
| 1333 |
\fi |
| 1334 |
\else |
| 1335 |
\expandafter\@secondoftwo |
| 1336 |
\fi |
| 1337 |
} |
| 1338 |
\def\mmz@Ifmemoizing@nogrouplevel{0\relax\relax\fi\iftrue} |
| 1339 |
% \end{macro} |
| 1340 |
% |
| 1341 |
% \paragraph.{Tracing} We populate the hooks which send the tracing info to the |
| 1342 |
% terminal. |
| 1343 |
\def\mmz@trace#1{\advice@typeout{[tracing memoize] #1}} |
| 1344 |
\def\mmz@trace@context{\mmz@trace{\space\space |
| 1345 |
Context: "\expandonce{\mmz@context@key}" --> \mmz@context@mdfivesum}} |
| 1346 |
\def\mmz@trace@Memoize@on{% |
| 1347 |
\mmz@trace{% |
| 1348 |
Entering \noexpand\Memoize (% |
| 1349 |
\ifmemoize enabled\else disabled\fi, |
| 1350 |
\ifnum\mmz@mode=\mmz@mode@recompile recompile\fi |
| 1351 |
\ifnum\mmz@mode=\mmz@mode@readonly readonly\fi |
| 1352 |
\ifnum\mmz@mode=\mmz@mode@normal normal\fi |
| 1353 |
\space mode) on line \the\inputlineno |
| 1354 |
}% |
| 1355 |
\mmz@trace{\space\space Code: \the\mmz@mdfive@source}% |
| 1356 |
} |
| 1357 |
\def\mmz@trace@code@mdfive@on{\mmz@trace{\space\space |
| 1358 |
Code md5sum: \mmz@code@mdfivesum}} |
| 1359 |
\def\mmz@trace@compile@on{\mmz@trace{\space\space Compiling}} |
| 1360 |
\def\mmz@trace@memoize@on{\mmz@trace{\space\space Memoizing}} |
| 1361 |
\def\mmz@trace@endmemoize@ok@on{\mmz@trace{\space\space |
| 1362 |
Memoization completed}}% |
| 1363 |
\def\mmz@trace@endmemoize@aborted@on{\mmz@trace{\space\space |
| 1364 |
Memoization was aborted}} |
| 1365 |
\def\mmz@trace@endmemoize@unmemoizable@on{\mmz@trace{\space\space |
| 1366 |
Marking this code as unmemoizable}} |
| 1367 |
% No need for |\mmz@trace@endmemoize@fail|, as abortion results in a package |
| 1368 |
% warning anyway. |
| 1369 |
\def\mmz@trace@process@cmemo@on{\mmz@trace{\space\space |
| 1370 |
Attempting to utilize c-memo \mmz@cmemo@path}} |
| 1371 |
\def\mmz@trace@process@no@cmemo@on{\mmz@trace{\space\space |
| 1372 |
C-memo does not exist}} |
| 1373 |
\def\mmz@trace@process@cmemo@ok@on{\mmz@trace{\space\space |
| 1374 |
C-memo was processed successfully}\mmz@trace@context} |
| 1375 |
\def\mmz@trace@process@cmemo@fail@on{\mmz@trace{\space\space |
| 1376 |
C-memo input failed}} |
| 1377 |
\def\mmz@trace@cmemo@unmemoizable@on{\mmz@trace{\space\space |
| 1378 |
This code was marked as unmemoizable}} |
| 1379 |
\def\mmz@trace@process@ccmemo@on{\mmz@trace{\space\space |
| 1380 |
Attempting to utilize cc-memo \mmz@ccmemo@path\space |
| 1381 |
(\ifmmz@direct@ccmemo@input\else in\fi direct input)}} |
| 1382 |
\def\mmz@trace@resource@on#1{\mmz@trace{\space\space |
| 1383 |
Extern file does not exist: #1}} |
| 1384 |
\def\mmz@trace@process@ccmemo@ok@on{% |
| 1385 |
\mmz@trace{\space\space Utilization successful}} |
| 1386 |
\def\mmz@trace@process@no@ccmemo@on{% |
| 1387 |
\mmz@trace{\space\space CC-memo does not exist}} |
| 1388 |
\def\mmz@trace@process@ccmemo@fail@on{% |
| 1389 |
\mmz@trace{\space\space Cc-memo input failed}} |
| 1390 |
% |
| 1391 |
% \begin{key}{tracing} |
| 1392 |
% \begin{macro}{\mmzTracingOn,\mmzTracingOff} |
| 1393 |
% The user interface for switching the tracing on and off; initially, it is |
| 1394 |
% off. Note that there is no underlying conditional. The off version |
| 1395 |
% simply |\let|s all the tracing hooks to |\relax|, so that the overhead of |
| 1396 |
% having the tracing functionality available is negligible. |
| 1397 |
\mmzset{% |
| 1398 |
trace/.is choice, |
| 1399 |
trace/.default=true, |
| 1400 |
trace/true/.code=\mmzTracingOn, |
| 1401 |
trace/false/.code=\mmzTracingOff, |
| 1402 |
} |
| 1403 |
\def\mmzTracingOn{% |
| 1404 |
\let\mmz@trace@Memoize\mmz@trace@Memoize@on |
| 1405 |
\let\mmz@trace@code@mdfive\mmz@trace@code@mdfive@on |
| 1406 |
\let\mmz@trace@compile\mmz@trace@compile@on |
| 1407 |
\let\mmz@trace@memoize\mmz@trace@memoize@on |
| 1408 |
\let\mmz@trace@process@cmemo\mmz@trace@process@cmemo@on |
| 1409 |
\let\mmz@trace@endmemoize@ok\mmz@trace@endmemoize@ok@on |
| 1410 |
\let\mmz@trace@endmemoize@unmemoizable\mmz@trace@endmemoize@unmemoizable@on |
| 1411 |
\let\mmz@trace@endmemoize@aborted\mmz@trace@endmemoize@aborted@on |
| 1412 |
\let\mmz@trace@process@cmemo\mmz@trace@process@cmemo@on |
| 1413 |
\let\mmz@trace@process@cmemo@ok\mmz@trace@process@cmemo@ok@on |
| 1414 |
\let\mmz@trace@process@no@cmemo\mmz@trace@process@no@cmemo@on |
| 1415 |
\let\mmz@trace@process@cmemo@fail\mmz@trace@process@cmemo@fail@on |
| 1416 |
\let\mmz@trace@cmemo@unmemoizable\mmz@trace@cmemo@unmemoizable@on |
| 1417 |
\let\mmz@trace@process@ccmemo\mmz@trace@process@ccmemo@on |
| 1418 |
\let\mmz@trace@resource\mmz@trace@resource@on |
| 1419 |
\let\mmz@trace@process@ccmemo@ok\mmz@trace@process@ccmemo@ok@on |
| 1420 |
\let\mmz@trace@process@no@ccmemo\mmz@trace@process@no@ccmemo@on |
| 1421 |
\let\mmz@trace@process@ccmemo@fail\mmz@trace@process@ccmemo@fail@on |
| 1422 |
} |
| 1423 |
\def\mmzTracingOff{% |
| 1424 |
\let\mmz@trace@Memoize\relax |
| 1425 |
\let\mmz@trace@code@mdfive\relax |
| 1426 |
\let\mmz@trace@compile\relax |
| 1427 |
\let\mmz@trace@memoize\relax |
| 1428 |
\let\mmz@trace@process@cmemo\relax |
| 1429 |
\let\mmz@trace@endmemoize@ok\relax |
| 1430 |
\let\mmz@trace@endmemoize@unmemoizable\relax |
| 1431 |
\let\mmz@trace@endmemoize@aborted\relax |
| 1432 |
\let\mmz@trace@process@cmemo\relax |
| 1433 |
\let\mmz@trace@process@cmemo@ok\relax |
| 1434 |
\let\mmz@trace@process@no@cmemo\relax |
| 1435 |
\let\mmz@trace@process@cmemo@fail\relax |
| 1436 |
\let\mmz@trace@cmemo@unmemoizable\relax |
| 1437 |
\let\mmz@trace@process@ccmemo\relax |
| 1438 |
\let\mmz@trace@resource\@gobble |
| 1439 |
\let\mmz@trace@process@ccmemo@ok\relax |
| 1440 |
\let\mmz@trace@process@no@ccmemo\relax |
| 1441 |
\let\mmz@trace@process@ccmemo@fail\relax |
| 1442 |
} |
| 1443 |
\mmzTracingOff |
| 1444 |
% \end{macro} |
| 1445 |
% \end{key} |
| 1446 |
% |
| 1447 |
% |
| 1448 |
% \subsection{Context} |
| 1449 |
% \label{sec:code:context} |
| 1450 |
% |
| 1451 |
% \begin{macro}{\mmzContext,\mmzContextExtra} |
| 1452 |
% The context expression is stored in two token registers. Outside |
| 1453 |
% memoization, we will locally assign to |\mmzContext|; during memoization, |
| 1454 |
% we will globally assign to |\mmzContextExtra|. |
| 1455 |
\newtoks\mmzContext |
| 1456 |
\newtoks\mmzContextExtra |
| 1457 |
% \end{macro} |
| 1458 |
% \begin{key}{context, clear context} |
| 1459 |
% The user interface keys for context manipulation hide the complexity |
| 1460 |
% underlying the context storage from the user. |
| 1461 |
\mmzset{% |
| 1462 |
context/.code={% |
| 1463 |
\ifmemoizing |
| 1464 |
\expandafter\gtoksapp\expandafter\mmzContextExtra |
| 1465 |
\else |
| 1466 |
\expandafter\toksapp\expandafter\mmzContext |
| 1467 |
\fi |
| 1468 |
% We append a comma to the given context chunk, for disambiguation. |
| 1469 |
{#1,}% |
| 1470 |
}, |
| 1471 |
clear context/.code={% |
| 1472 |
\ifmemoizing |
| 1473 |
\expandafter\global\expandafter\mmzContextExtra |
| 1474 |
\else |
| 1475 |
\expandafter\mmzContext |
| 1476 |
\fi |
| 1477 |
{}% |
| 1478 |
}, |
| 1479 |
clear context/.value forbidden, |
| 1480 |
% \end{key} |
| 1481 |
% \begin{key}{meaning to context, csname meaning to context, key meaning to |
| 1482 |
% context, key value to context, /handlers/.meaning to context, |
| 1483 |
% /handlers/.value to context} |
| 1484 |
% Utilities to put the meaning of various stuff into |context|. |
| 1485 |
% \indentmacrocode |
| 1486 |
meaning to context/.code={\mmz@Cos\forcsvlist\mmz@mtoc{#1}}, |
| 1487 |
csname meaning to context/.code={\mmz@Cos\mmz@mtoc@csname{#1}}, |
| 1488 |
key meaning to context/.code={\mmz@Cos |
| 1489 |
\forcsvlist\mmz@mtoc\mmz@mtoc@keycmd{#1}}, |
| 1490 |
key value to context/.code={\mmz@Cos\forcsvlist\mmz@mtoc@key{#1}}, |
| 1491 |
/handlers/.meaning to context/.code={\mmz@Cos\expanded{% |
| 1492 |
\noexpand\mmz@mtoc@csname{pgfk@\pgfkeyscurrentpath/.@cmd}}}, |
| 1493 |
/handlers/.value to context/.code={\mmz@Cos |
| 1494 |
\expanded{\noexpand\mmz@mtoc@csname{pgfk@\pgfkeyscurrentpath}}}, |
| 1495 |
} |
| 1496 |
% \begin{macro}{\mmzSalt} |
| 1497 |
% \begin{key}{salt, clear salt, meaning to salt, csname meaning to salt, |
| 1498 |
% key meaning to salt, key value to salt, |
| 1499 |
% /handlers/.meaning to salt, /handlers/.value to salt} |
| 1500 |
% Salt functions in the same way as context outside memoization, but it |
| 1501 |
% contributes to the source code hash, i.e.\ |\mmzSalt| is expanded when |
| 1502 |
% computing |\mmz@code@mdfivesum| in |\Memoize|. It was realized that it |
| 1503 |
% is needed for full Beamer support, |
| 1504 |
% see \href{https://github.com/sasozivanovic/memoize/issues/27}{GitHub |
| 1505 |
% issue \#27}. |
| 1506 |
\newtoks\mmzSalt |
| 1507 |
\mmzset{ |
| 1508 |
salt/.code=\expandafter\toksapp\expandafter\mmzSalt{#1,}, |
| 1509 |
clear salt/.value forbidden, |
| 1510 |
clear salt/.code=\mmzSalt{}, |
| 1511 |
meaning to salt/.code={\mmz@coS\forcsvlist\mmz@mtoc{#1}}, |
| 1512 |
csname meaning to salt/.code={\mmz@coS\mmz@mtoc@csname{#1}}, |
| 1513 |
key meaning to salt/.code={\mmz@coS |
| 1514 |
\forcsvlist\mmz@mtoc\mmz@mtoc@keycmd{#1}}, |
| 1515 |
key value to salt/.code={\mmz@coS |
| 1516 |
\forcsvlist\mmz@mtoc@key{#1}}, |
| 1517 |
/handlers/.meaning to salt/.code={\mmz@coS\expanded{% |
| 1518 |
\noexpand\mmz@mtoc@csname{pgfk@\pgfkeyscurrentpath/.@cmd}}}, |
| 1519 |
/handlers/.value to salt/.code={\mmz@coS |
| 1520 |
\expanded{\noexpand\mmz@mtoc@csname{pgfk@\pgfkeyscurrentpath}}}, |
| 1521 |
} |
| 1522 |
% \end{key} |
| 1523 |
% \end{macro} |
| 1524 |
% |
| 1525 |
% The following macros are shared between context and salt, so they should be |
| 1526 |
% preceded by either |\mmz@Cos| (for context) or |\mmz@coS| (for salt). |
| 1527 |
% \noindentmacrocode |
| 1528 |
\def\mmz@Cos{\def\mmz@context@or@salt{context}} |
| 1529 |
\def\mmz@coS{\def\mmz@context@or@salt{salt}} |
| 1530 |
\def\mmz@mtoc#1{% |
| 1531 |
\collargs@cs@cases{#1}% |
| 1532 |
{\mmz@mtoc@cmd{#1}}% |
| 1533 |
{\mmz@mtoc@error@notcsorenv{#1}}% |
| 1534 |
{% |
| 1535 |
\mmz@mtoc@csname{% |
| 1536 |
%<context>start% |
| 1537 |
#1}% |
| 1538 |
\mmz@mtoc@csname{% |
| 1539 |
%<latex,plain>end% |
| 1540 |
%<context>stop% |
| 1541 |
#1}% |
| 1542 |
}% |
| 1543 |
} |
| 1544 |
\def\mmz@mtoc@cmd#1{% |
| 1545 |
\begingroup |
| 1546 |
\escapechar=-1 |
| 1547 |
\expandafter\endgroup |
| 1548 |
\expandafter\mmz@mtoc@csname\expandafter{\string#1}% |
| 1549 |
} |
| 1550 |
\def\mmz@mtoc@csname#1{% |
| 1551 |
\pgfkeysvalueof{/mmz/\mmz@context@or@salt/.@cmd}% |
| 1552 |
\detokenize{#1}% |
| 1553 |
\ifcsname#1\endcsname |
| 1554 |
={\expandafter\meaning\csname#1\endcsname}% |
| 1555 |
\fi |
| 1556 |
\pgfeov |
| 1557 |
} |
| 1558 |
\def\mmz@mtoc@key#1{\mmz@mtoc@csname{pgfk@#1}} |
| 1559 |
\def\mmz@mtoc@keycmd#1{\mmz@mtoc@csname{pgfk@#1/.@cmd}} |
| 1560 |
\def\mmz@mtoc@error@notcsorenv#1{% |
| 1561 |
\PackageError{memoize}{'\detokenize{#1}' passed to key 'meaning to \mmz@context@or@salt' |
| 1562 |
is neither a command nor an environment}{}% |
| 1563 |
} |
| 1564 |
% \end{key} |
| 1565 |
% |
| 1566 |
% |
| 1567 |
% \subsection{C-memos} |
| 1568 |
% \label{sec:code:c-memo} |
| 1569 |
% |
| 1570 |
% \paragraph{The path} to a c-memo consists of the path prefix, the MD5 sum of |
| 1571 |
% the memoized code, and suffix |.memo|. |
| 1572 |
\def\mmz@cmemo@path{\mmz@prefix\mmz@[email protected]} |
| 1573 |
% \begin{macro}{\mmzCMemo} |
| 1574 |
% The additional, free-form content of the c-memo is collected in this token |
| 1575 |
% register. |
| 1576 |
\newtoks\mmzCMemo |
| 1577 |
% \end{macro} |
| 1578 |
% |
| 1579 |
% \begin{key}{include source in cmemo} |
| 1580 |
% \begin{macro}{\ifmmz@include@source} |
| 1581 |
% Should we include the memoized code in |
| 1582 |
% the c-memo? By default, yes. \bigskip |
| 1583 |
\mmzset{% |
| 1584 |
include source in cmemo/.is if=mmz@include@source, |
| 1585 |
} |
| 1586 |
\newif\ifmmz@include@source |
| 1587 |
\mmz@include@sourcetrue |
| 1588 |
% \end{macro} |
| 1589 |
% \end{key} |
| 1590 |
% |
| 1591 |
% \begin{macro}{\mmz@write@cmemo} |
| 1592 |
% This macro creates the c-memo from the contents of |\mmzContextExtra| and |
| 1593 |
% |\mmzCMemo|. |
| 1594 |
\def\mmz@write@cmemo{% |
| 1595 |
% Open the file for writing. |
| 1596 |
\immediate\openout\mmz@out{\mmz@cmemo@path}% |
| 1597 |
% The memo starts with the |\mmzMemo| marker (a signal that the memo is valid). |
| 1598 |
\immediate\write\mmz@out{\noexpand\mmzMemo}% |
| 1599 |
% We store the content of |\mmzContextExtra| by writing out a command that |
| 1600 |
% will (globally) assign its content back into this register. |
| 1601 |
\immediate\write\mmz@out{% |
| 1602 |
\global\mmzContextExtra{\the\mmzContextExtra}\collargs@percentchar |
| 1603 |
}% |
| 1604 |
% Write out the free-form part of the c-memo. |
| 1605 |
\immediate\write\mmz@out{\the\mmzCMemo\collargs@percentchar}% |
| 1606 |
% When |include source in cmemo| is in effect, add the memoized code, hiding |
| 1607 |
% it behind the |\mmzSource| marker. |
| 1608 |
\ifmmz@include@source |
| 1609 |
\immediate\write\mmz@out{\noexpand\mmzSource}% |
| 1610 |
\immediate\write\mmz@out{\the\mmz@mdfive@source}% |
| 1611 |
\fi |
| 1612 |
% Close the file. |
| 1613 |
\immediate\closeout\mmz@out |
| 1614 |
% Record that we wrote a new c-memo. |
| 1615 |
\pgfkeysalso{/mmz/record/new cmemo={\mmz@cmemo@path}}% |
| 1616 |
} |
| 1617 |
% \end{macro} |
| 1618 |
% |
| 1619 |
% \begin{macro}{\mmzSource} |
| 1620 |
% The c-memo memoized code marker. This macro is synonymous with |\endinput|, |
| 1621 |
% so the source following it is ignored when inputting the c-memo. |
| 1622 |
\let\mmzSource\endinput |
| 1623 |
% \end{macro} |
| 1624 |
% |
| 1625 |
% \begin{macro}{\mmz@process@cmemo} |
| 1626 |
% This macro inputs the c-memo, which will update the context code, which we |
| 1627 |
% can then compute the MD5 sum of. |
| 1628 |
\def\mmz@process@cmemo{% |
| 1629 |
\mmz@trace@process@cmemo |
| 1630 |
% |\ifmmz@abort| serves as a signal that the c-memo exists and is of correct |
| 1631 |
% form. |
| 1632 |
\global\mmz@aborttrue |
| 1633 |
% If c-memo sets |\ifmmzUnmemoizable|, we will compile regularly. |
| 1634 |
\global\mmzUnmemoizablefalse |
| 1635 |
\def\mmzMemo{\global\mmz@abortfalse}% |
| 1636 |
% Just a safeguard \dots\ c-memo assigns to |\mmzContextExtra| anyway. |
| 1637 |
\global\mmzContextExtra{}% |
| 1638 |
% Input the c-memo, if it exists, and record that we have used it. |
| 1639 |
\IfFileExists{\mmz@cmemo@path}{% |
| 1640 |
\input{\mmz@cmemo@path}% |
| 1641 |
\pgfkeysalso{/mmz/record/used cmemo={\mmz@cmemo@path}}% |
| 1642 |
}{% |
| 1643 |
\mmz@trace@process@no@cmemo |
| 1644 |
}% |
| 1645 |
% Compute the context MD5 sum. |
| 1646 |
\mmz@compute@context@mdfivesum |
| 1647 |
} |
| 1648 |
% \end{macro} |
| 1649 |
% |
| 1650 |
% \begin{macro}{\mmz@compute@context@mdfivesum} |
| 1651 |
% This macro computes the MD5 sum of the concatenation of |\mmzContext| and |
| 1652 |
% |\mmzContextExtra|, and writes out the tracing info when |trace context| is |
| 1653 |
% in effect. The argument is the tracing note. |
| 1654 |
% \end{macro} |
| 1655 |
\def\mmz@compute@context@mdfivesum{% |
| 1656 |
\xdef\mmz@context@key{\the\mmzContext\the\mmzContextExtra}% |
| 1657 |
% A special provision for padding, which occurs in the context by default, |
| 1658 |
% and may contain otherwise undefined macros referring to the extern |
| 1659 |
% dimensions. We make sure that when we expand the context key, |
| 1660 |
% |\mmz@paddings| contains the stringified |\width| etc., while these macros |
| 1661 |
% (which may be employed by the end user in the context expression), are |
| 1662 |
% returned to their original definitions. |
| 1663 |
\begingroup |
| 1664 |
\begingroup |
| 1665 |
\def\width{\string\width}% |
| 1666 |
\def\height{\string\height}% |
| 1667 |
\def\depth{\string\depth}% |
| 1668 |
\edef\mmz@paddings{\mmz@paddings}% |
| 1669 |
\expandafter\endgroup |
| 1670 |
\expandafter\def\expandafter\mmz@paddings\expandafter{\mmz@paddings}% |
| 1671 |
% We pre-expand the concatenated context, for tracing\slash inclusion in the |
| 1672 |
% cc-memo. In \hologo{LaTeX}, we protect the expansion, as the context |
| 1673 |
% expression may contain whatever. |
| 1674 |
%<latex>\protected@xdef |
| 1675 |
%<!latex>\xdef |
| 1676 |
\mmz@context@key{\mmz@context@key}% |
| 1677 |
\endgroup |
| 1678 |
% Compute the MD5 sum. We have to assign globally, because this macro is (also) |
| 1679 |
% called after inputting the c-memo, while the resulting MD5 sum is used to |
| 1680 |
% input the cc-memo, which happens outside the |\Memoize| group. |
| 1681 |
% |\mmz@context@mdfivesum|. |
| 1682 |
\xdef\mmz@context@mdfivesum{\pdf@mdfivesum{\expandonce\mmz@context@key}}% |
| 1683 |
} |
| 1684 |
% |
| 1685 |
% |
| 1686 |
% \subsection{Cc-memos} |
| 1687 |
% \label{sec:code:cc-memo} |
| 1688 |
% |
| 1689 |
% \paragraph{The path} to a cc-memo consists of the path prefix, the |
| 1690 |
% hyphen-separated MD5 sums of the memoized code and the (evaluated) context, |
| 1691 |
% and suffix |.memo|. |
| 1692 |
\def\mmz@ccmemo@path{% |
| 1693 |
\mmz@prefix\mmz@code@mdfivesum-\mmz@[email protected]} |
| 1694 |
% |
| 1695 |
% \paragraph{The structure} of a cc-memo: |
| 1696 |
% \begin{itemize} |
| 1697 |
% \item the list of resources consisting of calls to |\mmzResource|; |
| 1698 |
% \item the core memo code (which includes the externs when executed), |
| 1699 |
% introduced by marker |\mmzMemo|; and, |
| 1700 |
% \item optionally, the context expansion, introduced by marker |
| 1701 |
% |\mmzThisContext|. |
| 1702 |
% \end{itemize} |
| 1703 |
% |
| 1704 |
% We begin the cc-memo with a list of extern files included by the core memo |
| 1705 |
% code so that we can check whether these files exist prior to executing the |
| 1706 |
% core memo code. Checking this on the fly, while executing the core memo |
| 1707 |
% code, would be too late, as that code is arbitrary (and also executed outside |
| 1708 |
% the |\Memoize| group). |
| 1709 |
% |
| 1710 |
% \begin{macro}{\mmzCCMemo} |
| 1711 |
% During memoization, the core content of the cc-memo is collected into this |
| 1712 |
% token register. |
| 1713 |
\newtoks\mmzCCMemo |
| 1714 |
% \end{macro} |
| 1715 |
% |
| 1716 |
% \begin{key}{include context in ccmemo} |
| 1717 |
% \begin{macro}{\ifmmz@include@context} |
| 1718 |
% Should we include the context expansion in the cc-memo? By default, no. |
| 1719 |
% \bigskip |
| 1720 |
\newif\ifmmz@include@context |
| 1721 |
\mmzset{% |
| 1722 |
include context in ccmemo/.is if=mmz@include@context, |
| 1723 |
} |
| 1724 |
% \end{macro} |
| 1725 |
% \end{key} |
| 1726 |
% |
| 1727 |
% \begin{key}{direct ccmemo input} |
| 1728 |
% \begin{macro}{\ifmmz@direct@ccmemo@input} |
| 1729 |
% When this conditional is false, the |
| 1730 |
% cc-memo is read indirectly, via a token register, to facilitate |
| 1731 |
% inverse search. \bigskip |
| 1732 |
\newif\ifmmz@direct@ccmemo@input |
| 1733 |
\mmzset{% |
| 1734 |
direct ccmemo input/.is if=mmz@direct@ccmemo@input, |
| 1735 |
} |
| 1736 |
% \end{macro} |
| 1737 |
% \end{key} |
| 1738 |
% |
| 1739 |
% \begin{macro}{\mmz@write@ccmemo} |
| 1740 |
% This macro creates the cc-memo from the list of resources in |
| 1741 |
% |\mmz@ccmemo@resources| and the contents of |\mmzCCMemo|. |
| 1742 |
\def\mmz@write@ccmemo{% |
| 1743 |
% Open the cc-memo file for writing. Note that the filename contains the |
| 1744 |
% context MD5 sum, which can only be computed after memoization, as the |
| 1745 |
% memoized code can update the context. This is one of the two reasons why |
| 1746 |
% we couldn't write the cc-memo directly into the file, but had to collect |
| 1747 |
% its contents into token register |\mmzCCMemo|. |
| 1748 |
\immediate\openout\mmz@out{\mmz@ccmemo@path}% |
| 1749 |
% Token register |\mmz@ccmemo@resources| consists of calls to |
| 1750 |
% |\mmz@ccmemo@append@resource|, so the following code writes down the list |
| 1751 |
% of created externs into the cc-memo. Wanting to have this list at the top |
| 1752 |
% of the cc-memo is the other reason for the roundabout creation of the |
| 1753 |
% cc-memo --- the resources become known only during memoization, as well. |
| 1754 |
\begingroup |
| 1755 |
\the\mmz@ccmemo@resources |
| 1756 |
\endgroup |
| 1757 |
% Write down the content of |\mmzMemo|, but first introduce it by the |
| 1758 |
% |\mmzMemo| marker. |
| 1759 |
\immediate\write\mmz@out{\noexpand\mmzMemo}% |
| 1760 |
\immediate\write\mmz@out{\the\mmzCCMemo\collargs@percentchar}% |
| 1761 |
% Write down the context tracing info when |include context in ccmemo| is in |
| 1762 |
% effect. |
| 1763 |
\ifmmz@include@context |
| 1764 |
\immediate\write\mmz@out{\noexpand\mmzThisContext}% |
| 1765 |
\immediate\write\mmz@out{\expandonce{\mmz@context@key}}% |
| 1766 |
\fi |
| 1767 |
% Insert the end-of-file marker and close the file. |
| 1768 |
\immediate\write\mmz@out{\noexpand\mmzEndMemo}% |
| 1769 |
\immediate\closeout\mmz@out |
| 1770 |
% Record that we wrote a new cc-memo. |
| 1771 |
\pgfkeysalso{/mmz/record/new ccmemo={\mmz@ccmemo@path}}% |
| 1772 |
} |
| 1773 |
% \end{macro} |
| 1774 |
% |
| 1775 |
% \begin{macro}{\mmz@ccmemo@append@resource} |
| 1776 |
% Append the resource to the cc-memo (we are nice to external utilities and |
| 1777 |
% put each resource on its own line). |#1| is the sequential number of the |
| 1778 |
% extern belonging to the memoized code; below, we assign it to |\mmz@seq|, |
| 1779 |
% which appears in |\mmz@extern@name|. Note that |\mmz@extern@name| only |
| 1780 |
% contains the extern filename --- without the path, so that externs can be |
| 1781 |
% used by several projects, or copied around. |
| 1782 |
\def\mmz@ccmemo@append@resource#1{% |
| 1783 |
\mmz@seq=#1\relax |
| 1784 |
\immediate\write\mmz@out{% |
| 1785 |
\string\mmzResource{\mmz@extern@name}\collargs@percentchar}% |
| 1786 |
} |
| 1787 |
% \end{macro} |
| 1788 |
% |
| 1789 |
% \begin{macro}{\mmzResource} |
| 1790 |
% A list of these macros is located at the top of a cc-memo. The macro |
| 1791 |
% checks for the existence of the extern file, given as |#1|. If the extern |
| 1792 |
% does not exist, we redefine |\mmzMemo| to |\endinput|, so that the core |
| 1793 |
% content of the cc-memo is never executed; see also |\mmz@process@ccmemo| |
| 1794 |
% above. |
| 1795 |
\def\mmzResource#1{% |
| 1796 |
% We check for existence using |\pdffilesize|, because an empty PDF, which |
| 1797 |
% might be produced by a failed \hologo{TeX}-based extraction, should count |
| 1798 |
% as no file. The |0| behind |\ifnum| is there because |\pdffilesize| |
| 1799 |
% returns an empty string when the file does not exist. |
| 1800 |
\ifnum0\pdf@filesize{\mmz@prefix@dir#1}=0 |
| 1801 |
\ifmmz@direct@ccmemo@input |
| 1802 |
\let\mmzMemo\endinput |
| 1803 |
\else |
| 1804 |
% With indirect cc-memo input, we simulate end-of-input by grabbing |
| 1805 |
% everything up to the end-of-memo marker. In the indirect cc-memo |
| 1806 |
% input, a |\par| token shows up after |\mmzEndMemo|, I'm not sure why |
| 1807 |
% (|\everyeof={}| does not help). |
| 1808 |
\long\def\mmzMemo##1\mmzEndMemo\par{}% |
| 1809 |
\fi |
| 1810 |
\mmz@trace@resource{#1}% |
| 1811 |
\fi |
| 1812 |
% Map the extern sequential number to the path to the extern. We need this |
| 1813 |
% so that |prefix| can be changed via |\mmznext|. The problem is that when a |
| 1814 |
% cc-memo is utilized, |\mmzMemo| closes the Memoize group; the |prefix| is |
| 1815 |
% thereby forgotten. But at the point of execution of |\mmzResource|, the |
| 1816 |
% |prefix| setting is still known and can be baked into |
| 1817 |
% |\mmz@resource@<seq>|, which will be inspected by |\mmzIncludeExtern|. |
| 1818 |
\csxdef{mmz@resource@\the\mmz@seq}{\mmz@prefix@dir#1}% |
| 1819 |
\global\advance\mmz@seq1 |
| 1820 |
} |
| 1821 |
% \end{macro} |
| 1822 |
% |
| 1823 |
% \begin{macro}{\mmz@process@ccmemo,\mmzThisContext,\mmzEndMemo} |
| 1824 |
% This macro processes the cc-memo. |
| 1825 |
% \indentmacrocode |
| 1826 |
\def\mmz@process@ccmemo{% |
| 1827 |
\mmz@trace@process@ccmemo |
| 1828 |
% \noindentmacrocode |
| 1829 |
% The following conditional signals whether cc-memo was successfully |
| 1830 |
% utilized. If the cc-memo file does not exist, |\ifmmz@abort| will remain |
| 1831 |
% true. If it exists, it is headed by the list of resources. If a |
| 1832 |
% resource check fails, |\mmzMemo| (which follows the list of resources) is |
| 1833 |
% redefined to |\endinput|, so |\ifmmz@abort| remains true. However, if |
| 1834 |
% all resource checks are successful, |\mmzMemo| marker is reached with |
| 1835 |
% the below definition in effect, so |\ifmmz@abort| becomes false. Note |
| 1836 |
% that this marker also closes the |\Memoize| group, so that the core |
| 1837 |
% cc-memo content is executed in the original group --- and that this |
| 1838 |
% does not happen if anything goes wrong! |
| 1839 |
\global\mmz@aborttrue |
| 1840 |
% Note that |\mmzMemo| may be redefined by |\mmzResource| upon an unavailable |
| 1841 |
% extern file. |
| 1842 |
\def\mmzMemo{% |
| 1843 |
\endgroup |
| 1844 |
\global\mmz@abortfalse |
| 1845 |
% We |\let| the control sequence used for extern inclusion in the cc-memo to |
| 1846 |
% the macro which includes the extern from the extern file. |
| 1847 |
\let\mmzIncludeExtern\mmz@include@extern |
| 1848 |
}% |
| 1849 |
% Define |\mmzEndMemo| wrt |\ifmmz@direct@ccmemo@input|, whose value will be |
| 1850 |
% lost soon because |\mmMemo| will close the group --- that's also why this |
| 1851 |
% definition is global. |
| 1852 |
\xdef\mmzEndMemo{% |
| 1853 |
\ifmmz@direct@ccmemo@input |
| 1854 |
\noexpand\endinput |
| 1855 |
\else |
| 1856 |
% In the indirect cc-memo input, a |\par| token shows up after |
| 1857 |
% |\mmzEndMemo|, I'm not sure why (|\everyeof={}| does not help). |
| 1858 |
\unexpanded{% |
| 1859 |
\def\mmz@temp\par{}% |
| 1860 |
\mmz@temp |
| 1861 |
}% |
| 1862 |
\fi |
| 1863 |
}% |
| 1864 |
% The cc-memo context marker, again wrt |\ifmmz@direct@ccmemo@input| and |
| 1865 |
% globally. With direct cc-memo input, this macro is synonymous with |
| 1866 |
% |\endinput|, so the (expanded) context following it is ignored when |
| 1867 |
% inputting the cc-memo. With indirect input, we simulate end-of-input by |
| 1868 |
% grabbing everything up to the end-of-memo marker (plus gobble the |\par| |
| 1869 |
% mentioned above). |
| 1870 |
\xdef\mmzThisContext{% |
| 1871 |
\ifmmz@direct@ccmemo@input |
| 1872 |
\noexpand\endinput |
| 1873 |
\else |
| 1874 |
\unexpanded{% |
| 1875 |
\long\def\mmz@temp##1\mmzEndMemo\par{}% |
| 1876 |
\mmz@temp |
| 1877 |
}% |
| 1878 |
\fi |
| 1879 |
}% |
| 1880 |
% Reset |\mmz@seq| for |\mmzResorce|. |
| 1881 |
\global\mmz@seq=0 |
| 1882 |
% Input the cc-memo if it exists. |
| 1883 |
\IfFileExists{\mmz@ccmemo@path}{% |
| 1884 |
\ifmmz@direct@ccmemo@input |
| 1885 |
\input{\mmz@ccmemo@path}% |
| 1886 |
\else |
| 1887 |
% Indirect cc-memo input reads the cc-memo into a token register and |
| 1888 |
% executes the contents of this register. |
| 1889 |
\filetotoks\toks@{\mmz@ccmemo@path}% |
| 1890 |
\the\toks@ |
| 1891 |
\fi |
| 1892 |
% Record that we have used the cc-memo. |
| 1893 |
\pgfkeysalso{/mmz/record/used ccmemo={\mmz@ccmemo@path}}% |
| 1894 |
}{% |
| 1895 |
\mmz@trace@process@no@ccmemo |
| 1896 |
}% |
| 1897 |
\ifmmz@abort |
| 1898 |
% The cc-memo doesn't exist, or some of the resources don't. We need to |
| 1899 |
% memoize, but we'll do it only if |readonly| is not in effect, otherwise |
| 1900 |
% we'll perform a regular compilation. (Note that we are still in the group |
| 1901 |
% opened prior to executing |\Memoize|.) |
| 1902 |
\mmz@trace@process@ccmemo@fail |
| 1903 |
\ifnum\mmz@mode=\mmz@mode@readonly\relax |
| 1904 |
\expandafter\expandafter\expandafter\mmz@compile |
| 1905 |
\else |
| 1906 |
\expandafter\expandafter\expandafter\mmz@memoize |
| 1907 |
\fi |
| 1908 |
\else |
| 1909 |
\mmz@trace@process@ccmemo@ok |
| 1910 |
\fi |
| 1911 |
} |
| 1912 |
% \end{macro} |
| 1913 |
% |
| 1914 |
% \subsection{The externs} |
| 1915 |
% \label{sec:code:extern} |
| 1916 |
% |
| 1917 |
% \paragraph{The path} to an extern is like the path to a cc-memo, modulo |
| 1918 |
% suffix |.pdf|, of course. However, in case memoization of a chunk produces |
| 1919 |
% more than one extern, the filename of any non-first extern includes |
| 1920 |
% |\mmz@seq|, the sequential number of the extern as well (we start the |
| 1921 |
% numbering at $0$). We will have need for several parts of the full path to |
| 1922 |
% an extern: the basename, the filename, the path without the suffix, and the |
| 1923 |
% full path. |
| 1924 |
\newcount\mmz@seq |
| 1925 |
\def\mmz@extern@basename{% |
| 1926 |
\mmz@prefix@name\mmz@code@mdfivesum-\mmz@context@mdfivesum |
| 1927 |
\ifnum\mmz@seq>0 -\the\mmz@seq\fi |
| 1928 |
} |
| 1929 |
\def\mmz@extern@name{\mmz@[email protected]} |
| 1930 |
\def\mmz@extern@basepath{\mmz@prefix@dir\mmz@extern@basename} |
| 1931 |
\def\mmz@extern@path{\mmz@[email protected]} |
| 1932 |
% |
| 1933 |
% \begin{key}{padding left, padding right, padding top, padding bottom} |
| 1934 |
% These options set the amount of space surrounding the bounding box of the |
| 1935 |
% externalized graphics in the resulting PDF, i.e.\ in the extern file. This |
| 1936 |
% allows the user to deal with \TikZ; overlays, |\rlap| and |\llap|, etc. |
| 1937 |
\mmzset{ |
| 1938 |
padding left/.store in=\mmz@padding@left, |
| 1939 |
padding right/.store in=\mmz@padding@right, |
| 1940 |
padding top/.store in=\mmz@padding@top, |
| 1941 |
padding bottom/.store in=\mmz@padding@bottom, |
| 1942 |
% \end{key} |
| 1943 |
% |
| 1944 |
% \begin{key}{padding} |
| 1945 |
% A shortcut for setting all four paddings at once. |
| 1946 |
padding/.style={ |
| 1947 |
padding left=#1, padding right=#1, |
| 1948 |
padding top=#1, padding bottom=#1 |
| 1949 |
}, |
| 1950 |
% The default padding is what \hologo{pdfTeX} puts around the page anyway, 1 |
| 1951 |
% inch, but we'll use |1 in| rather than |1 true in|, which is the true |
| 1952 |
% default value of |\pdfhorigin| and |\pdfvorigin|, as we want the padding to |
| 1953 |
% adjust with magnification. |
| 1954 |
padding=1in, |
| 1955 |
% \end{key} |
| 1956 |
% |
| 1957 |
% \begin{key}{padding to context} |
| 1958 |
% This key adds padding to the context. Note that we add the padding |
| 1959 |
% expression (|\mmz@paddings|, defined below, refers to all the individual |
| 1960 |
% padding macros), not the actual value (at the time of expansion). This |
| 1961 |
% is so because |\width|, |\height| and |\depth| are not defined outside |
| 1962 |
% extern shipout routines, and the context is evaluated elsewhere. |
| 1963 |
padding to context/.style={ |
| 1964 |
context={padding=(\mmz@paddings)}, |
| 1965 |
}, |
| 1966 |
% \end{key} |
| 1967 |
% |
| 1968 |
% Padding nearly always belongs into the context --- the exception being |
| 1969 |
% memoized code which produces no externs (\MS\ref{sec:pure-memoization}) --- |
| 1970 |
% so we execute this key immediately. |
| 1971 |
padding to context, |
| 1972 |
} |
| 1973 |
\def\mmz@paddings{% |
| 1974 |
\mmz@padding@left,\mmz@padding@bottom,\mmz@padding@right,\mmz@padding@top |
| 1975 |
} |
| 1976 |
% |
| 1977 |
% \begin{macro}{\mmzExternalizeBox} |
| 1978 |
% This macro is the public interface to externalization. In Memoize itself, |
| 1979 |
% it is called from the default memoization driver, |\mmzSingleExternDriver|, |
| 1980 |
% but it should be called by any driver that wishes to produce an extern, see |
| 1981 |
% \MS\ref{sec:memoization-drivers} for details. It takes two arguments: |
| 1982 |
% \begin{enumerate}[\tt\#1] |
| 1983 |
% \item The box that we want to externalize. It's content will remain intact. |
| 1984 |
% The box may be given either as a control sequence, declared via |\newbox|, |
| 1985 |
% or as box number (say, 0). |
| 1986 |
% \item The token register which will receive the code that includes the extern |
| 1987 |
% into the document; it is the responsibility of the memoization driver to |
| 1988 |
% (globally) include the contents of the register in the cc-memo, i.e.\ in |
| 1989 |
% token register |\mmzCCMemo|. This argument may be either a control |
| 1990 |
% sequence, declared via |\newtoks|, or a |\toks|\meta{token register |
| 1991 |
% number}. |
| 1992 |
% \end{enumerate} |
| 1993 |
\def\mmzExternalizeBox#1#2{% |
| 1994 |
\begingroup |
| 1995 |
% A courtesy to the user, so they can define padding in terms of the size of |
| 1996 |
% the externalized graphics. |
| 1997 |
\def\width{\wd#1 }% |
| 1998 |
\def\height{\ht#1 }% |
| 1999 |
\def\depth{\dp#1 }% |
| 2000 |
% Store the extern-inclusion code in a temporary macro, which will be |
| 2001 |
% smuggled out of the group. |
| 2002 |
\xdef\mmz@global@temp{% |
| 2003 |
% Executing |\mmzIncludeExtern| from the cc-memo will include the extern |
| 2004 |
% into the document. |
| 2005 |
\noexpand\mmzIncludeExtern |
| 2006 |
% |\mmzIncludeExtern| identifies the extern by its sequence number, |
| 2007 |
% |\mmz@seq|. |
| 2008 |
{\the\mmz@seq}% |
| 2009 |
% What kind of box? We |\noexpand| the answer just in case someone |
| 2010 |
% redefined them. |
| 2011 |
\ifhbox#1\noexpand\hbox\else\noexpand\vbox\fi |
| 2012 |
% The dimensions of the extern. |
| 2013 |
{\the\wd#1}% |
| 2014 |
{\the\ht#1}% |
| 2015 |
{\the\dp#1}% |
| 2016 |
% The padding values. |
| 2017 |
{\the\dimexpr\mmz@padding@left}% |
| 2018 |
{\the\dimexpr\mmz@padding@bottom}% |
| 2019 |
{\the\dimexpr\mmz@padding@right}% |
| 2020 |
{\the\dimexpr\mmz@padding@top}% |
| 2021 |
}% |
| 2022 |
% Prepend the new extern box into the global extern box where we collect all |
| 2023 |
% the externs of this memo. Note that we |\copy| the extern box, retaining |
| 2024 |
% its content --- we will also want to place the extern box in its regular |
| 2025 |
% place in the document. |
| 2026 |
\global\setbox\mmz@tbe@box\vbox{\copy#1\unvbox\mmz@tbe@box}% |
| 2027 |
% Add the extern to the list of resources, which will be included at the top |
| 2028 |
% of the cc-memo, to check whether the extern files exists at the time the |
| 2029 |
% cc-memo is utilized. In the cc-memo, the list will contain full extern |
| 2030 |
% filenames, which are currently unknown, but no matter; right now, providing |
| 2031 |
% the extern sequence number suffices, the full extern filename will be |
| 2032 |
% produced at the end of memoization, once the context MD5 sum is known. |
| 2033 |
\xtoksapp\mmz@ccmemo@resources{% |
| 2034 |
\noexpand\mmz@ccmemo@append@resource{\the\mmz@seq}% |
| 2035 |
}% |
| 2036 |
% Increment the counter containing the sequence number of the extern within |
| 2037 |
% this memo. |
| 2038 |
\global\advance\mmz@seq1 |
| 2039 |
% Assign the extern-including code into the token register given in |#2|. |
| 2040 |
% This register may be given either as a control sequence or as |
| 2041 |
% |\toks|\meta{token register number}, and this is why we have temporarily |
| 2042 |
% stored the code (into |\mmz@global@temp|) globally: a local storage with |
| 2043 |
% |\expandafter\endgroup\expandafter| here would fail with the receiving |
| 2044 |
% token register given as |\toks|\meta{token register number}. |
| 2045 |
\endgroup |
| 2046 |
#2\expandafter{\mmz@global@temp}% |
| 2047 |
} |
| 2048 |
% \end{macro} |
| 2049 |
% |
| 2050 |
% \begin{macro}{\mmz@ccmemo@resources} |
| 2051 |
% This token register, populated by |\mmz@externalize@box| and used by |
| 2052 |
% |\mmz@write@ccmemo|, holds the list of externs produced by memoization of |
| 2053 |
% the current chunk. |
| 2054 |
\newtoks\mmz@ccmemo@resources |
| 2055 |
% \end{macro} |
| 2056 |
% |
| 2057 |
% \begin{macro}{\mmz@tbe@box} |
| 2058 |
% |\mmz@externalize@box| does not directly dump the extern into the document |
| 2059 |
% (as a special page). Rather, the externs are collected into |
| 2060 |
% |\mmz@tbe@box|, whose contents are dumped into the document at the end of |
| 2061 |
% memoization of the current chunk. In this way, we guarantee that aborted |
| 2062 |
% memoization does not pollute the document. |
| 2063 |
\newbox\mmz@tbe@box |
| 2064 |
% \end{macro} |
| 2065 |
% |
| 2066 |
% \begin{macro}{\mmz@shipout@externs} |
| 2067 |
% This macro is executed at the end of |
| 2068 |
% memoization, when the externs are waiting for us in |\mmz@tbe@box| and need |
| 2069 |
% to be dumped into the document. It loops through the contents of |
| 2070 |
% |\mmz@tbe@box|,\footnote{The looping code is based on TeX.SE |
| 2071 |
% answer \url(https://){tex.stackexchange.com/a/25142/16819} by Bruno Le |
| 2072 |
% Floch.}, putting each extern into |\mmz@box| and calling |
| 2073 |
% |\mmz@shipout@extern|. Note that the latter macro is executed within the |
| 2074 |
% group opened by |\vbox| below. |
| 2075 |
\def\mmz@shipout@externs{% |
| 2076 |
\global\mmz@seq 0 |
| 2077 |
\setbox\mmz@box\vbox{% |
| 2078 |
% Set the macros below to the dimensions of the extern box, so that the |
| 2079 |
% user can refer to them in the padding specification (which is in turn |
| 2080 |
% used in the page setup in |\mmz@shipout@extern|). |
| 2081 |
\def\width{\wd\mmz@box}% |
| 2082 |
\def\height{\ht\mmz@box}% |
| 2083 |
\def\depth{\dp\mmz@box}% |
| 2084 |
\vskip1pt |
| 2085 |
\ifmmzkeepexterns\expandafter\unvcopy\else\expandafter\unvbox\fi\mmz@tbe@box |
| 2086 |
\@whilesw\ifdim0pt=\lastskip\fi{% |
| 2087 |
\setbox\mmz@box\lastbox |
| 2088 |
\mmz@shipout@extern |
| 2089 |
}% |
| 2090 |
}% |
| 2091 |
} |
| 2092 |
% \end{macro} |
| 2093 |
% |
| 2094 |
% \begin{macro}{\mmz@shipout@extern} |
| 2095 |
% This macro ships out a single extern, which |
| 2096 |
% resides in |\mmz@box|, and records the creation of the new extern. |
| 2097 |
\def\mmz@shipout@extern{% |
| 2098 |
% Calculate the expected width and height. We have to do this now, before we |
| 2099 |
% potentially adjust the box size and paddings for magnification. |
| 2100 |
\edef\expectedwidth{\the\dimexpr |
| 2101 |
(\mmz@padding@left) + \wd\mmz@box + (\mmz@padding@right)}% |
| 2102 |
\edef\expectedheight{\the\dimexpr |
| 2103 |
(\mmz@padding@top) + \ht\mmz@box + \dp\mmz@box + (\mmz@padding@bottom)}% |
| 2104 |
% Apply the inverse magnification, if |\mag| is not at the default |
| 2105 |
% value. We'll do this in a group, which will last until shipout. |
| 2106 |
\begingroup |
| 2107 |
\ifnum\mag=1000 |
| 2108 |
\else |
| 2109 |
\mmz@shipout@mag |
| 2110 |
\fi |
| 2111 |
% Setup the geometry of the extern page. In \hologo{plainTeX} and |
| 2112 |
% \hologo{LaTeX}, setting |\pdfpagewidth| and |\pdfpageheight| seems to do |
| 2113 |
% the trick of setting the extern page dimensions. In \hologo{ConTeXt}, |
| 2114 |
% however, the resulting extern page ends up with the PDF |/CropBox| |
| 2115 |
% specification of the current regular page, which is then used (ignoring our |
| 2116 |
% |mediabox| requirement) when we're including the extern into the document |
| 2117 |
% by |\mmzIncludeExtern|. Typically, this results in a page-sized extern. |
| 2118 |
% I'm not sure how to deal with this correctly. In the workaround below, we |
| 2119 |
% use Lua function |backends.codeinjections.setupcanvas| to set up page |
| 2120 |
% dimensions: we first remember the current page dimensions |
| 2121 |
% (|\edef\mmz@temp|), then set up the extern page dimensions |
| 2122 |
% (|\expanded{...}|), and finally, after shipping |
| 2123 |
% out the extern page, revert to the current page dimensions by executing |
| 2124 |
% |\mmz@temp| at the very end of this macro. |
| 2125 |
%<*plain,latex> |
| 2126 |
\pdfpagewidth\dimexpr |
| 2127 |
(\mmz@padding@left) + \wd\mmz@box + (\mmz@padding@right)\relax |
| 2128 |
\pdfpageheight\dimexpr |
| 2129 |
(\mmz@padding@top) + \ht\mmz@box + \dp\mmz@box+ (\mmz@padding@bottom)\relax |
| 2130 |
%</plain,latex> |
| 2131 |
%<*context> |
| 2132 |
\edef\mmz@temp{% |
| 2133 |
\noexpand\directlua{ |
| 2134 |
backends.codeinjections.setupcanvas({ |
| 2135 |
paperwidth=\the\numexpr\pagewidth, |
| 2136 |
paperheight=\the\numexpr\pageheight |
| 2137 |
}) |
| 2138 |
}% |
| 2139 |
}% |
| 2140 |
\expanded{% |
| 2141 |
\noexpand\directlua{ |
| 2142 |
backends.codeinjections.setupcanvas({ |
| 2143 |
paperwidth=\the\numexpr\dimexpr |
| 2144 |
\mmz@padding@left + \wd\mmz@box + \mmz@padding@right\relax, |
| 2145 |
paperheight=\the\numexpr\dimexpr |
| 2146 |
\mmz@padding@top + \ht\mmz@box + \dp\mmz@box+ \mmz@padding@bottom\relax |
| 2147 |
}) |
| 2148 |
}% |
| 2149 |
}% |
| 2150 |
%</context> |
| 2151 |
\mmz@shipout@unrotate |
| 2152 |
% |
| 2153 |
% We complete the page setup by setting the content offset. |
| 2154 |
\hoffset\dimexpr\mmz@padding@left - \pdfhorigin\relax |
| 2155 |
\voffset\dimexpr\mmz@padding@top - \pdfvorigin\relax |
| 2156 |
% We shipout the extern page using the |\shipout| primitive, so that the |
| 2157 |
% extern page is not modified, or even registered, by the shipout code of the |
| 2158 |
% format or some package. I can't imagine those shipout routines ever |
| 2159 |
% needing to know about the extern page. In fact, most often knowing about |
| 2160 |
% it would be undesirable. For example, \hologo{LaTeX} and \hologo{ConTeXt} |
| 2161 |
% count the ``real'' pages, but usually to know whether they are shipping out |
| 2162 |
% an odd or an even page, or to make the total number of pages available to |
| 2163 |
% subsequent compilations. Taking the extern pages into account would |
| 2164 |
% disrupt these mechanisms. |
| 2165 |
% |
| 2166 |
% Another thing: delayed |\write|s. We have to make sure that any |
| 2167 |
% \hologo{LaTeX}-style protected stuff in those is not expanded. We don't |
| 2168 |
% bother introducing a special group, as we'll close the |\mag| group right |
| 2169 |
% after the shipout anyway. |
| 2170 |
%<latex>\let\protect\noexpand |
| 2171 |
\pdf@primitive\shipout\box\mmz@box |
| 2172 |
%<context>\mmz@temp |
| 2173 |
\endgroup |
| 2174 |
% Advance the counter of shipped-out externs. We do this before preparing |
| 2175 |
% the recording information below, because the extern extraction tools expect |
| 2176 |
% the extern page numbering to start with $1$. |
| 2177 |
\global\advance\mmzExternPages1 |
| 2178 |
% Prepare the macros which may be used in |record/<type>/new extern| code. |
| 2179 |
\edef\externbasepath{\mmz@extern@basepath}% |
| 2180 |
% Adding up the counters below should result in the real page number of the |
| 2181 |
% extern. Macro |\mmzRegularPages| holds the number of pages which were |
| 2182 |
% shipped out so far using the regular shipout routine of the format; |
| 2183 |
% |\mmzExternPages| holds the number of shipped-out extern pages; and |
| 2184 |
% |\mmzExtraPages| holds, or at least should hold, the number of pages |
| 2185 |
% shipped out using any other means. |
| 2186 |
\edef\pagenumber{% |
| 2187 |
\the\numexpr\mmzRegularPages |
| 2188 |
% In \hologo{LaTeX}, the |\mmzRegularPages| holds to number of pages |
| 2189 |
% already shipped out. In \hologo{ConTeXt}, the counter is already |
| 2190 |
% increased while processing the page, so we need to subtract $1$. |
| 2191 |
%<context> -1% |
| 2192 |
+\mmzExternPages+\mmzExtraPages |
| 2193 |
}% |
| 2194 |
% Record the creation of the new extern. We do this after shipping out the |
| 2195 |
% extern page, so that the recording mechanism can serve as an after-shipout |
| 2196 |
% hook, for the unlikely situation that some package really needs to do |
| 2197 |
% something when our shipout happens. Note that we absolutely refuse to |
| 2198 |
% provide a before-shipout hook, because we can't allow anyone messing with |
| 2199 |
% our extern, and that using this after-shipout ``hook'' is unnecessary for |
| 2200 |
% counting extern shipouts, as we already provide this information in the |
| 2201 |
% public counter |\mmzExternPages|. |
| 2202 |
\mmzset{record/new extern/.expanded=\mmz@extern@path}% |
| 2203 |
% Advance the sequential number of the extern, in the context of the current |
| 2204 |
% memoized code chunk. This extern numbering starts at 0, so we only do this |
| 2205 |
% after we wrote the cc-memo and called |record/new extern|. |
| 2206 |
\global\advance\mmz@seq1 |
| 2207 |
} |
| 2208 |
% \end{macro} |
| 2209 |
% |
| 2210 |
% \begin{macro}{\mmz@shipout@mag} |
| 2211 |
% This macro applies the inverse magnification, so that the extern ends up |
| 2212 |
% with its natural size on the extern page. |
| 2213 |
\def\mmz@shipout@mag{% |
| 2214 |
% We scale the extern box using the PDF primitives: |q| and |Q| save and |
| 2215 |
% restore the current graphics state; |cm| applies the given coordinate |
| 2216 |
% transformation matrix. ($a$ $b$ $c$ $d$ $e$ $f$ |cm| transforms $(x,y)$ |
| 2217 |
% into $(ax+cy+e,bx+dy+f)$.) |
| 2218 |
\setbox\mmz@box\hbox{% |
| 2219 |
\pdfliteral{q \mmz@inverse@mag\space 0 0 \mmz@inverse@mag\space 0 0 cm}% |
| 2220 |
\copy\mmz@box\relax |
| 2221 |
\pdfliteral{Q}% |
| 2222 |
}% |
| 2223 |
% We first have to scale the paddings, as they might refer to the |\width| |
| 2224 |
% etc.\ of the extern. |
| 2225 |
\dimen0=\dimexpr\mmz@padding@left\relax |
| 2226 |
\edef\mmz@padding@left{\the\dimexpr\mmz@inverse@mag\dimen0}% |
| 2227 |
\dimen0=\dimexpr\mmz@padding@bottom\relax |
| 2228 |
\edef\mmz@padding@bottom{\the\dimexpr\mmz@inverse@mag\dimen0}% |
| 2229 |
\dimen0=\dimexpr\mmz@padding@right\relax |
| 2230 |
\edef\mmz@padding@right{\the\dimexpr\mmz@inverse@mag\dimen0}% |
| 2231 |
\dimen0=\dimexpr\mmz@padding@top\relax |
| 2232 |
\edef\mmz@padding@top{\the\dimexpr\mmz@inverse@mag\dimen0}% |
| 2233 |
% Scale the extern box. |
| 2234 |
\wd\mmz@box=\mmz@inverse@mag\wd\mmz@box\relax |
| 2235 |
\ht\mmz@box=\mmz@inverse@mag\ht\mmz@box\relax |
| 2236 |
\dp\mmz@box=\mmz@inverse@mag\dp\mmz@box\relax |
| 2237 |
} |
| 2238 |
% \end{macro} |
| 2239 |
% |
| 2240 |
% \begin{macro}{\mmz@inverse@mag} |
| 2241 |
% The inverse magnification factor, i.e.\ the number we have to multiply the |
| 2242 |
% extern dimensions with so that they will end up in their natural size. We |
| 2243 |
% compute it, once and for all, at the beginning of the document. To do |
| 2244 |
% that, we borrow the little macro |\Pgf@geT| from |pgfutil-common| (but |
| 2245 |
% rename it). |
| 2246 |
{\catcode`\p=12\catcode`\t=12\gdef\mmz@Pgf@geT#1pt{#1}} |
| 2247 |
\mmzset{begindocument/.append code={% |
| 2248 |
\edef\mmz@inverse@mag{\expandafter\mmz@Pgf@geT\the\dimexpr 1000pt/\mag}% |
| 2249 |
}} |
| 2250 |
% \end{macro} |
| 2251 |
% |
| 2252 |
% |
| 2253 |
% \begin{macro}{\mmz@shipout@unrotate} |
| 2254 |
% Remove any rotation attribute given to the PDF page. I don't know enough |
| 2255 |
% about PDF page attributes to be sure this works in general. I have tested |
| 2256 |
% the code on simple documents with \hologo{LaTeX} and \hologo{plainTeX} in |
| 2257 |
% \hologo{TeX}live 2023 and 2024. It seems that the ``unrotation'' is |
| 2258 |
% unnecessary with \hologo{LaTeX3}'s PDF management module (activated by |
| 2259 |
% |\DocumentMetadata{}| before the document class declaration). |
| 2260 |
\ifdef\XeTeXversion{% |
| 2261 |
\def\mmz@shipout@unrotate{}% |
| 2262 |
}{% |
| 2263 |
\ifdef\luatexversion{% |
| 2264 |
\def\mmz@shipout@unrotate{% |
| 2265 |
%<latex>\IfPDFManagementActiveTF{}{\pdfvariable pageattr {/Rotate 0}}% |
| 2266 |
%<plain,context>\pdfvariable pageattr {/Rotate 0}% |
| 2267 |
}% |
| 2268 |
}{% |
| 2269 |
\def\mmz@shipout@unrotate{% |
| 2270 |
%<latex>\IfPDFManagementActiveTF{}{\pdfpageattr{/Rotate 0}}% |
| 2271 |
%<plain,context>\pdfpageattr{/Rotate 0}% |
| 2272 |
}% |
| 2273 |
}% |
| 2274 |
} |
| 2275 |
% \end{macro} |
| 2276 |
% |
| 2277 |
% \begin{macro}{\mmzRegularPages} |
| 2278 |
% This counter holds the number of pages shipped out by the format's shipout |
| 2279 |
% routine. \hologo{LaTeX} and \hologo{ConTeXt} keep track of this in |
| 2280 |
% dedicated counters, so we simply use those. In \hologo{plainTeX}, we have |
| 2281 |
% to hack the |\shipout| macro to install our own counter. In fact, we |
| 2282 |
% already did this while loading the required packages, in order to avoid it |
| 2283 |
% being redefined by |atbegshi| first. All that is left to do here is to |
| 2284 |
% declare the counter. |
| 2285 |
%<latex>\let\mmzRegularPages\ReadonlyShipoutCounter |
| 2286 |
%<context>\let\mmzRegularPages\realpageno |
| 2287 |
%<plain>\newcount\mmzRegularPages |
| 2288 |
% \end{macro} |
| 2289 |
% |
| 2290 |
% \begin{macro}{\mmzExternPages} |
| 2291 |
% This counter holds the number of extern pages shipped out so far. |
| 2292 |
\newcount\mmzExternPages |
| 2293 |
% \end{macro} |
| 2294 |
% |
| 2295 |
% The total number of new externs is announced at the end of the compilation, |
| 2296 |
% so that \hologo{TeX} editors, |latexmk| and such can propose recompilation. |
| 2297 |
\mmzset{ |
| 2298 |
enddocument/afterlastpage/.append code={% |
| 2299 |
\ifnum\mmzExternPages>0 |
| 2300 |
\PackageWarning{memoize}{The compilation produced \the\mmzExternPages\space |
| 2301 |
new extern\ifnum\mmzExternPages>1 s\fi}% |
| 2302 |
\fi |
| 2303 |
}, |
| 2304 |
} |
| 2305 |
% |
| 2306 |
% \begin{macro}{\mmzExtraPages} |
| 2307 |
% This counter will probably remain at zero forever. It should be advanced by |
| 2308 |
% any package which (like Memoize) ships out pages bypassing the regular |
| 2309 |
% shipout routine of the format. |
| 2310 |
\newcount\mmzExtraPages |
| 2311 |
% \end{macro} |
| 2312 |
% |
| 2313 |
% \begin{macro}{\mmz@include@extern} |
| 2314 |
% This macro, called from cc-memos as |\mmzIncludeExtern|, inserts an extern |
| 2315 |
% file into the document. We first look up the path to the extern based on |
| 2316 |
% the extern sequential number; the dictionary was set up by |\mmzResource| |
| 2317 |
% statements in the cc-memo. |
| 2318 |
\def\mmz@include@extern#1{% |
| 2319 |
\expandafter\expandafter\expandafter\mmz@include@extern@i |
| 2320 |
\expandafter\expandafter\expandafter{% |
| 2321 |
\csname mmz@resource@#1\endcsname}% |
| 2322 |
} |
| 2323 |
% |#1| is the path to the extern filename, |#2| is either |\hbox| or |\vbox|, |
| 2324 |
% |#3|, |#4| and |#5| are the (expected) width, height and the depth of the |
| 2325 |
% externalized box; |#6|--|#9| are the paddings (left, bottom, right, and top). |
| 2326 |
\def\mmz@include@extern@i#1#2#3#4#5#6#7#8#9{% |
| 2327 |
% Use the primitive PDF graphics inclusion commands to include the extern |
| 2328 |
% file. Set the correct depth or the resulting box, and shift it as |
| 2329 |
% specified by the padding. |
| 2330 |
\setbox\mmz@box=#2{% |
| 2331 |
\setbox0=\hbox{% |
| 2332 |
\lower\dimexpr #5+#7\relax\hbox{% |
| 2333 |
\hskip -#6\relax |
| 2334 |
\setbox0=\hbox{% |
| 2335 |
\mmz@insertpdfpage{#1}{1}% |
| 2336 |
}% |
| 2337 |
\unhbox0 |
| 2338 |
}% |
| 2339 |
}% |
| 2340 |
\wd0 \dimexpr\wd0-#8\relax |
| 2341 |
\ht0 \dimexpr\ht0-#9\relax |
| 2342 |
\dp0 #5\relax |
| 2343 |
\box0 |
| 2344 |
}% |
| 2345 |
% Check whether the size of the included extern is as expected. There is no |
| 2346 |
% need to check |\dp|, we have just set it. (|\mmz@if@roughly@equal| is |
| 2347 |
% defined in section~\ref{sec:code:extract:tex}.) |
| 2348 |
\mmz@tempfalse |
| 2349 |
\mmz@if@roughly@equal{\mmz@tolerance}{#3}{\wd\mmz@box}{% |
| 2350 |
\mmz@if@roughly@equal{\mmz@tolerance}{#4}{\ht\mmz@box}{% |
| 2351 |
\mmz@temptrue |
| 2352 |
}{}}{}% |
| 2353 |
\ifmmz@temp |
| 2354 |
\else |
| 2355 |
\mmz@use@memo@warning{\mmz@extern@path}{#3}{#4}{#5}% |
| 2356 |
\fi |
| 2357 |
% Use the extern box, with the precise size as remembered at memoization. |
| 2358 |
\wd\mmz@box=#3\relax |
| 2359 |
\ht\mmz@box=#4\relax |
| 2360 |
\box\mmz@box |
| 2361 |
% Record that we have used this extern. |
| 2362 |
\pgfkeysalso{/mmz/record/used extern={\mmz@extern@path}}% |
| 2363 |
} |
| 2364 |
% \end{macro} |
| 2365 |
\def\mmz@use@memo@warning#1#2#3#4{% |
| 2366 |
\PackageWarning{memoize}{Unexpected size of extern "#1"; |
| 2367 |
expected #2\space x \the\dimexpr #3+#4\relax, |
| 2368 |
got \the\wd\mmz@box\space x \the\dimexpr\the\ht\mmz@box+\the\dp\mmz@box\relax}% |
| 2369 |
} |
| 2370 |
% \begin{macro}{\mmz@insertpdfpage} |
| 2371 |
% This macro inserts a page from the PDF into the document. We define it |
| 2372 |
% according to which engine is being used. Note that \hologo{ConTeXt} always |
| 2373 |
% uses \hologo{LuaTeX}. |
| 2374 |
%<latex,plain>\ifdef\luatexversion{% |
| 2375 |
\def\mmz@insertpdfpage#1#2{% #1 = filename, #2 = page number |
| 2376 |
\saveimageresource page #2 mediabox {#1}% |
| 2377 |
\useimageresource\lastsavedimageresourceindex |
| 2378 |
}% |
| 2379 |
%<*latex,plain> |
| 2380 |
}{% |
| 2381 |
\ifdef\XeTeXversion{% |
| 2382 |
\def\mmz@insertpdfpage#1#2{% |
| 2383 |
\XeTeXpdffile #1 page #2 media |
| 2384 |
}% |
| 2385 |
}{% pdfLaTeX |
| 2386 |
\def\mmz@insertpdfpage#1#2{% |
| 2387 |
\pdfximage page #2 mediabox {#1}% |
| 2388 |
\pdfrefximage\pdflastximage |
| 2389 |
}% |
| 2390 |
}% |
| 2391 |
} |
| 2392 |
%</latex,plain> |
| 2393 |
% \end{macro} |
| 2394 |
% |
| 2395 |
% \begin{macro}{\mmz@include@extern@from@tbe@box} |
| 2396 |
% Include the extern number |#1| residing in |\mmz@tbe@box| into the |
| 2397 |
% document. It may be called as |\mmzIncludeExtern| from |after memoization| |
| 2398 |
% hook if |\ifmmzkeepexterns| was set to true during memoization. The macro |
| 2399 |
% takes the same arguments as |\mmzIncludeExtern| but disregards all but |
| 2400 |
% the first one, the extern sequential number. Using this macro, a complex |
| 2401 |
% memoization driver can process the cc-memo right after memoization, by |
| 2402 |
% issuing |
| 2403 |
% |\global\mmzkeepexternstrue\xtoksapp\mmzAfterMemoizationExtra{\the\mmzCCMemo}|. |
| 2404 |
\def\mmz@include@extern@from@tbe@box#1#2#3#4#5#6#7#8#9{% |
| 2405 |
\setbox0\vbox{% |
| 2406 |
\@tempcnta#1\relax |
| 2407 |
\vskip1pt |
| 2408 |
\unvcopy\mmz@tbe@box |
| 2409 |
\@whilenum\@tempcnta>0\do{% |
| 2410 |
\setbox0\lastbox |
| 2411 |
\advance\@tempcnta-1\relax |
| 2412 |
}% |
| 2413 |
\global\setbox1\lastbox |
| 2414 |
\@whilesw\ifdim0pt=\lastskip\fi{% |
| 2415 |
\setbox0\lastbox |
| 2416 |
}% |
| 2417 |
\box\mmz@box |
| 2418 |
}% |
| 2419 |
\box1 |
| 2420 |
} |
| 2421 |
% \end{macro} |
| 2422 |
% |
| 2423 |
% \section{Extraction} |
| 2424 |
% \label{sec:code:extract} |
| 2425 |
% |
| 2426 |
% \subsection{Extraction mode and method} |
| 2427 |
% \label{sec:code:extraction-mode-method} |
| 2428 |
% |
| 2429 |
% \begin{key}{extract} |
| 2430 |
% This key selects the extraction mode and method. It normally occurs in the |
| 2431 |
% package options list, less commonly in the preamble, and never in the |
| 2432 |
% document body. |
| 2433 |
\def\mmzvalueof#1{\pgfkeysvalueof{/mmz/#1}} |
| 2434 |
\mmzset{ |
| 2435 |
extract/.estore in=\mmz@extraction@method, |
| 2436 |
extract/.value required, |
| 2437 |
begindocument/.append style={extract/.code=\mmz@preamble@only@error}, |
| 2438 |
% \end{key} |
| 2439 |
% |
| 2440 |
% \begin{key}{extract/perl, extract/python} |
| 2441 |
% Any other value will select internal extraction with the given method. |
| 2442 |
% Memoize ships with two extraction scripts, a Perl script and a Python |
| 2443 |
% script, which are selected by |extract=perl| (the default) and |
| 2444 |
% |extract=python|, respectively. We run the scripts in verbose mode |
| 2445 |
% (without |-q|), and keep the |.mmz| file as is (without |-k|), i.e.\ |
| 2446 |
% we're not commenting out the |\mmzNewExtern| lines, because we're about |
| 2447 |
% to overwrite it anyway. We inform the script about the format of the |
| 2448 |
% document (|-F|). |
| 2449 |
extract/perl/.code={% |
| 2450 |
\mmz@clear@extraction@log |
| 2451 |
\pdf@system{% |
| 2452 |
\mmzvalueof{perl extraction command}\space |
| 2453 |
\mmzvalueof{perl extraction options}% |
| 2454 |
}% |
| 2455 |
\mmz@check@extraction@log{perl}% |
| 2456 |
}, |
| 2457 |
perl extraction command/.initial=memoize-extract.pl, |
| 2458 |
perl extraction options/.initial={\space |
| 2459 |
%<latex>-F latex |
| 2460 |
%<plain>-F plain |
| 2461 |
%<context>-F context |
| 2462 |
\jobname\space |
| 2463 |
}, |
| 2464 |
extract=perl, |
| 2465 |
extract/python/.code={% |
| 2466 |
\mmz@clear@extraction@log |
| 2467 |
\pdf@system{% |
| 2468 |
\mmzvalueof{python extraction command}\space |
| 2469 |
\mmzvalueof{python extraction options}% |
| 2470 |
}% |
| 2471 |
\mmz@check@extraction@log{python}% |
| 2472 |
% Change the initial value of |mkdir command| to |memoize-extract.py |
| 2473 |
% --mkdir|, but only in the case the user did not modify it. |
| 2474 |
\ifx\mmz@mkdir@command\mmz@initial@mkdir@command |
| 2475 |
\def\mmz@mkdir@command{\mmzvalueof{python extraction command} --mkdir}% |
| 2476 |
\fi |
| 2477 |
}, |
| 2478 |
python extraction command/.initial=memoize-extract.py, |
| 2479 |
python extraction options/.initial={\space |
| 2480 |
%<latex>-F latex |
| 2481 |
%<plain>-F plain |
| 2482 |
%<context>-F context |
| 2483 |
\jobname\space |
| 2484 |
}, |
| 2485 |
} |
| 2486 |
\def\mmz@preamble@only@error{% |
| 2487 |
\PackageError{memoize}{% |
| 2488 |
Ignoring the invocation of "\pgfkeyscurrentkey". |
| 2489 |
This key may only be executed in the preamble}{}% |
| 2490 |
} |
| 2491 |
% \end{key} |
| 2492 |
% |
| 2493 |
% \paragraph*{The extraction log} |
| 2494 |
% As we cannot access the exit status of a system command in \hologo{TeX}, we |
| 2495 |
% communicate with the system command via the ``extraction log file,'' produced |
| 2496 |
% by both \hologo{TeX}-based extraction and the Perl and Python extraction |
| 2497 |
% script. This file signals whether the embedded extraction was successful --- |
| 2498 |
% if it is, the file ends if |\endinput| --- and also contains any warnings and |
| 2499 |
% errors thrown by the script. As the log is really a \hologo{TeX} file, the |
| 2500 |
% idea is to simply input it after extracting each extern (for |
| 2501 |
% \hologo{TeX}-based extraction) or after the extraction of all externs (for |
| 2502 |
% the external scripts). |
| 2503 |
\def\mmz@clear@extraction@log{% |
| 2504 |
\begingroup |
| 2505 |
\immediate\openout0{\jobname.mmz.log}% |
| 2506 |
\immediate\closeout0 |
| 2507 |
\endgroup |
| 2508 |
} |
| 2509 |
% |#1| is the extraction method. |
| 2510 |
\def\mmz@check@extraction@log#1{% |
| 2511 |
\begingroup \def\extractionmethod{#1}% |
| 2512 |
\mmz@tempfalse \let\mmz@orig@endinput\endinput |
| 2513 |
\def\endinput{\mmz@temptrue\mmz@orig@endinput}% |
| 2514 |
\@input{\jobname.mmz.log}% |
| 2515 |
\ifmmz@temp \else \mmz@extraction@error \fi \endgroup } |
| 2516 |
\def\mmz@extraction@error{% |
| 2517 |
\PackageError{memoize}{Extraction of externs from document |
| 2518 |
"\jobname.pdf" using method "\extractionmethod" was |
| 2519 |
unsuccessful}{The extraction script "\mmzvalueof{\extractionmethod\space |
| 2520 |
extraction command}" wasn't executed or didn't finish execution |
| 2521 |
properly.}} |
| 2522 |
% |
| 2523 |
% \subsection{The record files} |
| 2524 |
% \label{sec:code:record} |
| 2525 |
% |
| 2526 |
% \begin{key}{record} |
| 2527 |
% This key activates a record \meta{type}: the hooks defined by that record |
| 2528 |
% \meta{type} will henceforth be executed at the appropriate places. |
| 2529 |
% |
| 2530 |
% A \meta{hook} of a particular \meta{type} resides in |pgfkeys| path |
| 2531 |
% |/mmz/record/|\meta{type}|/|\meta{hook}, and is invoked via |
| 2532 |
% |/mmz/record/|\meta{hook}. Record type activation thus appends a call of |
| 2533 |
% the former to the latter. It does so using handler |.try|, so that |
| 2534 |
% unneeded hooks may be left undefined. |
| 2535 |
% |
| 2536 |
\mmzset{ |
| 2537 |
record/.style={% |
| 2538 |
record/begin/.append style={ |
| 2539 |
/mmz/record/#1/begin/.try, |
| 2540 |
% The |begin| hook also executes the |prefix| hook, so that |\mmzPrefix| |
| 2541 |
% surely occurs at the top of the |.mmz| file. Listing each prefix type |
| 2542 |
% separately in this hook ensures that |prefix| of a certain type is |
| 2543 |
% executed after that type's |begin|. |
| 2544 |
/mmz/record/#1/prefix/.try/.expanded=\mmz@prefix, |
| 2545 |
}, |
| 2546 |
record/prefix/.append style={/mmz/record/#1/prefix/.try={##1}}, |
| 2547 |
record/new extern/.append style={/mmz/record/#1/new extern/.try={##1}}, |
| 2548 |
record/used extern/.append style={/mmz/record/#1/used extern/.try={##1}}, |
| 2549 |
record/new cmemo/.append style={/mmz/record/#1/new cmemo/.try={##1}}, |
| 2550 |
record/new ccmemo/.append style={/mmz/record/#1/new ccmemo/.try={##1}}, |
| 2551 |
record/used cmemo/.append style={/mmz/record/#1/used cmemo/.try={##1}}, |
| 2552 |
record/used ccmemo/.append style={/mmz/record/#1/used ccmemo/.try={##1}}, |
| 2553 |
record/end/.append style={/mmz/record/#1/end/.try}, |
| 2554 |
}, |
| 2555 |
} |
| 2556 |
% \end{key} |
| 2557 |
% |
| 2558 |
% \begin{key}{no record} |
| 2559 |
% This key deactivates all record types. Below, we use it to initialize the |
| 2560 |
% relevant keys; in the user code, it may be used to deactivate the |
| 2561 |
% preactivated |mmz| record type. |
| 2562 |
\mmzset{ |
| 2563 |
no record/.style={% |
| 2564 |
% The |begin| hook clears itself after invocation, to prevent double |
| 2565 |
% execution. Consequently, |record/begin| may be executed by the user in |
| 2566 |
% the preamble, without any ill effects. |
| 2567 |
record/begin/.style={record/begin/.style={}}, |
| 2568 |
% The |prefix| key invokes itself again when the group closes. This way, |
| 2569 |
% we can correctly track the path prefix changes in the |.mmz| even if |
| 2570 |
% |path| is executed in a group. |
| 2571 |
record/prefix/.code={\aftergroup\mmz@record@prefix}, |
| 2572 |
record/new extern/.code={}, |
| 2573 |
record/used extern/.code={}, |
| 2574 |
record/new cmemo/.code={}, |
| 2575 |
record/new ccmemo/.code={}, |
| 2576 |
record/used cmemo/.code={}, |
| 2577 |
record/used ccmemo/.code={}, |
| 2578 |
% The |end| hook clears itself after invocation, to prevent double |
| 2579 |
% execution. Consequently, |record/end| may be executed by the user before |
| 2580 |
% the end of the document, without any ill effects. |
| 2581 |
record/end/.style={record/end/.code={}}, |
| 2582 |
} |
| 2583 |
} |
| 2584 |
% \end{key} |
| 2585 |
% We define this macro because |\aftergroup|, used in |record/prefix|, only |
| 2586 |
% accepts a token. |
| 2587 |
\def\mmz@record@prefix{% |
| 2588 |
\mmzset{/mmz/record/prefix/.expanded=\mmz@prefix}% |
| 2589 |
} |
| 2590 |
% \paragraph{Initialize} the hook keys, preactivate |mmz| record type, and |
| 2591 |
% execute hooks |begin| and |end| at the edges of the document. |
| 2592 |
\mmzset{ |
| 2593 |
no record, |
| 2594 |
record=mmz, |
| 2595 |
begindocument/.append style={record/begin}, |
| 2596 |
enddocument/afterlastpage/.append style={record/end}, |
| 2597 |
} |
| 2598 |
% |
| 2599 |
% \subsubsection{The \texttt{.mmz} file} |
| 2600 |
% \label{sec:code:record:mmz} |
| 2601 |
% |
| 2602 |
% Think of the |.mmz| record file as a \hologo{TeX}-readable log file, which |
| 2603 |
% lets the extraction procedure know what happened in the previous compilation. |
| 2604 |
% The file is in \hologo{TeX} format, so that we can trigger internal |
| 2605 |
% \hologo{TeX}-based extraction by simply inputting it. The commands it |
| 2606 |
% contains are intentionally as simple as possible (just a macro plus braced |
| 2607 |
% arguments), to facilitate parsing by the external scripts. |
| 2608 |
% |
| 2609 |
% \begin{key}{record/mmz/...} |
| 2610 |
% These hooks simply put the calls of the corresponding macros into the file. |
| 2611 |
% All but hooks but |begin| and |end| receive the full path to the relevant |
| 2612 |
% file as the only argument (ok, |prefix| receives the full path prefix, as |
| 2613 |
% set by key |path|). |
| 2614 |
\mmzset{ |
| 2615 |
record/mmz/begin/.code={% |
| 2616 |
\newwrite\mmz@mmzout |
| 2617 |
% The record file has a fixed name (the jobname plus the |.mmz| suffix) and |
| 2618 |
% location (the current directory, i.e.\ the directory where \hologo{TeX} |
| 2619 |
% is executed from; usually, this will be the directory containing the |
| 2620 |
% \hologo{TeX} source). |
| 2621 |
\immediate\openout\mmz@mmzout{\jobname.mmz}% |
| 2622 |
}, |
| 2623 |
% The |\mmzPrefix| is used by the clean-up script, which will remove all |
| 2624 |
% files with the given path prefix but (unless called with |--all|) those |
| 2625 |
% mentioned in the |.mmz|. Now this script could in principle figure out |
| 2626 |
% what to remove by inspecting the paths to utilized\slash created |
| 2627 |
% memos\slash externs in the |.mmz| file, but this method could lead to |
| 2628 |
% problems in case of an incomplete (perhaps empty) |.mmz| file created by a |
| 2629 |
% failed compilation. Recording the path prefix in the |.mmz| radically |
| 2630 |
% increases the chances of a successful clean-up, which is doubly important, |
| 2631 |
% because a clean-up is sometimes precisely what we need to do to recover |
| 2632 |
% after a failed compilation. |
| 2633 |
record/mmz/prefix/.code={% |
| 2634 |
\immediate\write\mmz@mmzout{\noexpand\mmzPrefix{#1}}% |
| 2635 |
}, |
| 2636 |
record/mmz/new extern/.code={% |
| 2637 |
% While this key receives a single formal argument, Memoize also prepares |
| 2638 |
% macros |\externbasepath| (|#1| without the |.pdf| suffix), |\pagenumber| |
| 2639 |
% (of the extern page in the document PDF), and |\expectedwidth| and |
| 2640 |
% |\expectedheight| (of the extern page). |
| 2641 |
\immediate\write\mmz@mmzout{% |
| 2642 |
\noexpand\mmzNewExtern{#1}{\pagenumber}{\expectedwidth}{\expectedheight}% |
| 2643 |
}% |
| 2644 |
% Support |latexmk|: |
| 2645 |
%<latex>\typeout{No file #1}% |
| 2646 |
}, |
| 2647 |
record/mmz/new cmemo/.code={% |
| 2648 |
\immediate\write\mmz@mmzout{\noexpand\mmzNewCMemo{#1}}% |
| 2649 |
}, |
| 2650 |
record/mmz/new ccmemo/.code={% |
| 2651 |
\immediate\write\mmz@mmzout{\noexpand\mmzNewCCMemo{#1}}% |
| 2652 |
}, |
| 2653 |
record/mmz/used extern/.code={% |
| 2654 |
\immediate\write\mmz@mmzout{\noexpand\mmzUsedExtern{#1}}% |
| 2655 |
}, |
| 2656 |
record/mmz/used cmemo/.code={% |
| 2657 |
\immediate\write\mmz@mmzout{\noexpand\mmzUsedCMemo{#1}}% |
| 2658 |
}, |
| 2659 |
record/mmz/used ccmemo/.code={% |
| 2660 |
\immediate\write\mmz@mmzout{\noexpand\mmzUsedCCMemo{#1}}% |
| 2661 |
}, |
| 2662 |
record/mmz/end/.code={% |
| 2663 |
% Add the |\endinput| marker to signal that the file is complete. |
| 2664 |
\immediate\write\mmz@mmzout{\noexpand\endinput}% |
| 2665 |
\immediate\closeout\mmz@mmzout |
| 2666 |
}, |
| 2667 |
% |
| 2668 |
% \end{key} |
| 2669 |
% \subsubsection{The shell scripts} |
| 2670 |
% |
| 2671 |
% We define two shell script record types: |sh| for Linux, and |bat| for |
| 2672 |
% Windows. |
| 2673 |
% |
| 2674 |
% \begin{key}{sh, bat} |
| 2675 |
% These keys set the shell script filenames. |
| 2676 |
% \bigskip |
| 2677 |
sh/.store in=\mmz@shname, |
| 2678 |
sh=memoize-extract.\jobname.sh, |
| 2679 |
bat/.store in=\mmz@batname, |
| 2680 |
bat=memoize-extract.\jobname.bat, |
| 2681 |
% \end{key} |
| 2682 |
% |
| 2683 |
% \begin{key}{record/sh/...} Define the Linux shell script record type. |
| 2684 |
record/sh/begin/.code={% |
| 2685 |
\newwrite\mmz@shout |
| 2686 |
\immediate\openout\mmz@shout{\mmz@shname}% |
| 2687 |
}, |
| 2688 |
record/sh/new extern/.code={% |
| 2689 |
\begingroup |
| 2690 |
% Macro |\mmz@tex@extraction@systemcall| is customizable through |tex |
| 2691 |
% extraction command|, |tex extraction options| and |tex extraction |
| 2692 |
% script|. |
| 2693 |
\immediate\write\mmz@shout{\mmz@tex@extraction@systemcall}% |
| 2694 |
\endgroup |
| 2695 |
}, |
| 2696 |
record/sh/end/.code={% |
| 2697 |
\immediate\closeout\mmz@shout |
| 2698 |
}, |
| 2699 |
% \end{key} |
| 2700 |
% |
| 2701 |
% \begin{key}{record/bat/...} |
| 2702 |
% Rinse and repeat for Windows. |
| 2703 |
record/bat/begin/.code={% |
| 2704 |
\newwrite\mmz@batout |
| 2705 |
\immediate\openout\mmz@batout{\mmz@batname}% |
| 2706 |
}, |
| 2707 |
record/bat/new extern/.code={% |
| 2708 |
\begingroup |
| 2709 |
\immediate\write\mmz@batout{\mmz@tex@extraction@systemcall}% |
| 2710 |
\endgroup |
| 2711 |
}, |
| 2712 |
record/bat/end/.code={% |
| 2713 |
\immediate\closeout\mmz@batout |
| 2714 |
}, |
| 2715 |
% \end{key} |
| 2716 |
% |
| 2717 |
% \subsubsection{The Makefile} |
| 2718 |
% |
| 2719 |
% The implementation of the Makefile record type is the most complex so far, as |
| 2720 |
% we need to keep track of the targets. |
| 2721 |
% |
| 2722 |
% \begin{key}{makefile} |
| 2723 |
% This key sets the makefile filename. |
| 2724 |
makefile/.store in=\mmz@makefilename, |
| 2725 |
makefile=memoize-extract.\jobname.makefile, |
| 2726 |
} |
| 2727 |
% \end{key} |
| 2728 |
% |
| 2729 |
% We need to define a macro which expands to the tab character of catcode |
| 2730 |
% ``other'', to use as the recipe prefix. |
| 2731 |
\begingroup |
| 2732 |
\catcode`\^^I=12 |
| 2733 |
\gdef\mmz@makefile@recipe@prefix{^^I}% |
| 2734 |
\endgroup |
| 2735 |
% \begin{key}{record/makefile/...} |
| 2736 |
% Define the Makefile record type. |
| 2737 |
\mmzset{ |
| 2738 |
record/makefile/begin/.code={% |
| 2739 |
% We initialize the record type by opening the file and setting makefile |
| 2740 |
% variables |.DEFAULT_GOAL| and |.PHONY|. |
| 2741 |
\newwrite\mmz@makefileout |
| 2742 |
\newtoks\mmz@makefile@externs |
| 2743 |
\immediate\openout\mmz@makefileout{\mmz@makefilename}% |
| 2744 |
\immediate\write\mmz@makefileout{.DEFAULT_GOAL = externs}% |
| 2745 |
\immediate\write\mmz@makefileout{.PHONY: externs}% |
| 2746 |
}, |
| 2747 |
% The crucial part, writing out the extraction rule. The target comes first, |
| 2748 |
% then the recipe, which is whatever the user has set by |tex extraction |
| 2749 |
% command|, |tex extraction options| and |tex extraction script|. |
| 2750 |
record/makefile/new extern/.code={% |
| 2751 |
% The target extern file: |
| 2752 |
\immediate\write\mmz@makefileout{#1:}% |
| 2753 |
\begingroup |
| 2754 |
% The recipe is whatever the user set by |tex extraction |
| 2755 |
% command|, |tex extraction options| and |tex extraction script|. |
| 2756 |
\immediate\write\mmz@makefileout{% |
| 2757 |
\mmz@makefile@recipe@prefix\mmz@tex@extraction@systemcall}% |
| 2758 |
\endgroup |
| 2759 |
% Append the extern file to list of targets. |
| 2760 |
\xtoksapp\mmz@makefile@externs{#1\space}% |
| 2761 |
}, |
| 2762 |
record/makefile/end/.code={% |
| 2763 |
% Before closing the file, we list the extern files as the prerequisites of |
| 2764 |
% our phony default target, |externs|. |
| 2765 |
\immediate\write\mmz@makefileout{externs: \the\mmz@makefile@externs}% |
| 2766 |
\immediate\closeout\mmz@makefileout |
| 2767 |
}, |
| 2768 |
} |
| 2769 |
% \end{key} |
| 2770 |
% |
| 2771 |
% |
| 2772 |
% \subsection{\hologo{TeX}-based extraction} |
| 2773 |
% \label{sec:code:extract:tex} |
| 2774 |
% |
| 2775 |
% \begin{key}{extract/tex} |
| 2776 |
% We trigger the \hologo{TeX}-based extraction by inputting the |.mmz| record |
| 2777 |
% file. |
| 2778 |
\mmzset{ |
| 2779 |
extract/tex/.code={% |
| 2780 |
\begingroup |
| 2781 |
\@input{\jobname.mmz}% |
| 2782 |
\endgroup |
| 2783 |
}, |
| 2784 |
} |
| 2785 |
% \end{key} |
| 2786 |
% |
| 2787 |
% \begin{macro}{\mmzUsedCMemo,\mmzUsedCCMemo,\mmzUsedExtern,\mmzNewCMemo,\mmzNewCCMemo,\mmzPrefix} |
| 2788 |
% We can ignore everything but |\mmzNewExtern|s. All these macros receive a |
| 2789 |
% single argument. |
| 2790 |
% \indentmacrocode |
| 2791 |
\def\mmzUsedCMemo#1{} |
| 2792 |
\def\mmzUsedCCMemo#1{} |
| 2793 |
\def\mmzUsedExtern#1{} |
| 2794 |
\def\mmzNewCMemo#1{} |
| 2795 |
\def\mmzNewCCMemo#1{} |
| 2796 |
\def\mmzPrefix#1{} |
| 2797 |
% \end{macro} |
| 2798 |
% |
| 2799 |
% \begin{macro}{\mmzNewExtern} |
| 2800 |
% Command |\mmzNewExtern| takes four arguments. It instructs us to extract |
| 2801 |
% page |#2| of document |\jobname.pdf| to file |#1|. During the extraction, |
| 2802 |
% we will check whether the size of the extern matches the given expected |
| 2803 |
% width (|#3|) and total height (|#4|). |
| 2804 |
% |
| 2805 |
% We perform the extraction by an embedded \hologo{TeX} call. The system |
| 2806 |
% command that gets executed is stored in |\mmz@tex@extraction@systemcall|, |
| 2807 |
% which is set by |tex extraction command| and friends; by default, we |
| 2808 |
% execute |pdftex|. |
| 2809 |
% |
| 2810 |
\def\mmzNewExtern#1{% |
| 2811 |
% The \hologo{TeX} executable expects the basename as the argument, so we |
| 2812 |
% strip away the |.pdf| suffix. |
| 2813 |
\mmz@new@extern@i#1\mmz@temp |
| 2814 |
} |
| 2815 |
\def\mmz@new@extern@i#1.pdf\mmz@temp#2#3#4{% |
| 2816 |
\begingroup |
| 2817 |
% Define the macros used in |\mmz@tex@extraction@systemcall|. |
| 2818 |
\def\externbasepath{#1}% |
| 2819 |
\def\pagenumber{#2}% |
| 2820 |
\def\expectedwidth{#3}% |
| 2821 |
\def\expectedheight{#4}% |
| 2822 |
% Empty out the extraction log. |
| 2823 |
\mmz@clear@extraction@log |
| 2824 |
% Extract. |
| 2825 |
\pdf@system{\mmz@tex@extraction@systemcall}% |
| 2826 |
% Was the extraction successful? We temporarily redefine the extraction |
| 2827 |
% error message macro (suited for the external extraction scripts, which |
| 2828 |
% extract all externs in one go) to report the exact problematic extern page. |
| 2829 |
\let\mmz@extraction@error\mmz@pageextraction@error |
| 2830 |
\mmz@check@extraction@log{tex}% |
| 2831 |
\endgroup |
| 2832 |
} |
| 2833 |
% \end{macro} |
| 2834 |
\def\mmz@pageextraction@error{% |
| 2835 |
\PackageError{memoize}{Extraction of extern page \pagenumber\space from |
| 2836 |
document "jobname.pdf" using method "\extractionmethod" was |
| 2837 |
unsuccessful.}{Check the log file to see if the extraction script was |
| 2838 |
executed at all, and if it finished successfully. You might also want to |
| 2839 |
inspect "\externbasepath.log", the log file of the embedded TeX compilation |
| 2840 |
which ran the extraction script}} |
| 2841 |
% |
| 2842 |
% \begin{key}{tex extraction command, tex extraction options, tex extraction script} |
| 2843 |
% Using these keys, we set the system call |
| 2844 |
% which will be invoked for each extern page. The value of this key is |
| 2845 |
% expanded when executing the system command. The user may deploy the |
| 2846 |
% following macros in the value of these keys: |
| 2847 |
% \begin{itemize} |
| 2848 |
% \item |\externbasepath|: the extern PDF that should be produced, minus the |
| 2849 |
% |.pdf| suffix; |
| 2850 |
% \item |\pagenumber|: the page number to be extracted; |
| 2851 |
% \item |\expectedwidth|: the expected width of the extracted page; |
| 2852 |
% \item |\expectedheight|: the expected total height of the extracted page; |
| 2853 |
% \end{itemize} |
| 2854 |
\def\mmz@tex@extraction@systemcall{% |
| 2855 |
\mmzvalueof{tex extraction command}\space |
| 2856 |
\mmzvalueof{tex extraction options}\space |
| 2857 |
"\mmzvalueof{tex extraction script}"% |
| 2858 |
} |
| 2859 |
% \end{key} |
| 2860 |
% |
| 2861 |
% \paragraph{The default} system call for \hologo{TeX}-based extern extraction. |
| 2862 |
% As this method, despite being \hologo{TeX}-based, shares no code with the |
| 2863 |
% document, we're free to implement it with any engine and format we want. For |
| 2864 |
% reasons of speed, we clearly go for the plain \hologo{pdfTeX}.\footnote{I |
| 2865 |
% implemented the first version of \hologo{TeX}-based extraction using |
| 2866 |
% \hologo{LaTeX} and package \texttt{graphicx}, and it was (running with |
| 2867 |
% \hologo{pdfTeX} engine) almost four times slower than the current plain |
| 2868 |
% \hologo{TeX} implementation.} We perform the extraction by a little |
| 2869 |
% \hologo{TeX} script, |memoize-extract-one|, inputted at the end of the value |
| 2870 |
% given to |tex extraction script|. |
| 2871 |
\mmzset{ |
| 2872 |
tex extraction command/.initial=pdftex, |
| 2873 |
tex extraction options/.initial={% |
| 2874 |
% \begin{listingregion}{tex-extraction-options} |
| 2875 |
-halt-on-error |
| 2876 |
-interaction=batchmode |
| 2877 |
-jobname "\externbasepath" |
| 2878 |
% \end{listingregion} |
| 2879 |
}, |
| 2880 |
tex extraction script/.initial={% |
| 2881 |
% \begin{listingregion}{tex-extraction-script} ^^A todo: context |
| 2882 |
\def\noexpand\fromdocument{\jobname.pdf}% |
| 2883 |
\def\noexpand\pagenumber{\pagenumber}% |
| 2884 |
\def\noexpand\expectedwidth{\expectedwidth}% |
| 2885 |
\def\noexpand\expectedheight{\expectedheight}% |
| 2886 |
\def\noexpand\logfile{\jobname.mmz.log}% |
| 2887 |
\unexpanded{% |
| 2888 |
\def\warningtemplate{% |
| 2889 |
%<latex>\noexpand\PackageWarning{memoize}{\warningtext}% |
| 2890 |
%<plain>\warning{memoize: \warningtext}% |
| 2891 |
%<context>\warning{memoize: \warningtext}% |
| 2892 |
}}% |
| 2893 |
\ifdef\XeTeXversion{}{% |
| 2894 |
\def\noexpand\mmzpdfmajorversion{\the\pdfmajorversion}% |
| 2895 |
\def\noexpand\mmzpdfminorversion{\the\pdfminorversion}% |
| 2896 |
}% |
| 2897 |
\noexpand\input memoize-extract-one |
| 2898 |
% \end{listingregion} |
| 2899 |
}, |
| 2900 |
} |
| 2901 |
%</mmz> |
| 2902 |
% |
| 2903 |
% \subsubsection{\texttt{memoize-extract-one.tex}} |
| 2904 |
% |
| 2905 |
% The rest of the code of this section resides in file |
| 2906 |
% |memoize-extract-one.tex|. It is used to extract a single extern page from |
| 2907 |
% the document; it also checks whether the extern page dimensions are as |
| 2908 |
% expected, and passes a warning to the main job if that is not the case. For |
| 2909 |
% the reason of speed, the extraction script is in \hologo{plainTeX} format. |
| 2910 |
% For the same reason, it is compiled by \hologo{pdfTeX} engine by default, but |
| 2911 |
% we nevertheless take care that it will work with other (supported) engines as |
| 2912 |
% well. |
| 2913 |
% |
| 2914 |
%<*extract-one> |
| 2915 |
\catcode`\@11\relax |
| 2916 |
\def\@firstoftwo#1#2{#1} |
| 2917 |
\def\@secondoftwo#1#2{#2} |
| 2918 |
% |
| 2919 |
% Set the PDF version (maybe) passed to the script via |\mmzpdfmajorversion| |
| 2920 |
% and |\mmzpdfminorversion|. |
| 2921 |
\ifdefined\XeTeXversion |
| 2922 |
\else |
| 2923 |
\ifdefined\luatexversion |
| 2924 |
\def\pdfmajorversion{\pdfvariable majorversion}% |
| 2925 |
\def\pdfminorversion{\pdfvariable minorversion}% |
| 2926 |
\fi |
| 2927 |
\ifdefined\mmzpdfmajorversion |
| 2928 |
\pdfmajorversion\mmzpdfmajorversion\relax |
| 2929 |
\fi |
| 2930 |
\ifdefined\mmzpdfminorversion |
| 2931 |
\pdfminorversion\mmzpdfminorversion\relax |
| 2932 |
\fi |
| 2933 |
\fi |
| 2934 |
% Allocate a new output stream, always --- |\newwrite| is |\outer| and thus |
| 2935 |
% cannot appear in a conditional. |
| 2936 |
\newwrite\extractionlog |
| 2937 |
% Are we requested to produce a log file? |
| 2938 |
\ifdefined\logfile |
| 2939 |
\immediate\openout\extractionlog{\logfile}% |
| 2940 |
% Define a macro which both outputs the warning message and writes it to the |
| 2941 |
% extraction log. |
| 2942 |
\def\doublewarning#1{% |
| 2943 |
\message{#1}% |
| 2944 |
\def\warningtext{#1}% |
| 2945 |
% This script will be called from different formats, so it is up to the |
| 2946 |
% main job to tell us, by defining macro |\warningtemplate|, how to throw a |
| 2947 |
% warning in the log file. |
| 2948 |
\immediate\write\extractionlog{% |
| 2949 |
\ifdefined\warningtemplate\warningtemplate\else\warningtext\fi |
| 2950 |
}% |
| 2951 |
}% |
| 2952 |
\else |
| 2953 |
\let\doublewarning\message |
| 2954 |
\fi |
| 2955 |
\newif\ifforce |
| 2956 |
\ifdefined\force |
| 2957 |
\csname force\force\endcsname |
| 2958 |
\fi |
| 2959 |
% \begin{macro}{\mmz@if@roughly@equal} |
| 2960 |
% This macro checks whether the given dimensions (|#2| and |#3|) are equal |
| 2961 |
% within the tolerance given by |#1|. We use the macro both in the |
| 2962 |
% extraction script and in the main package. (We don't use |\ifpdfabsdim|, |
| 2963 |
% because it is unavailable in \hologo{XeTeX}.) |
| 2964 |
%</extract-one> |
| 2965 |
%<*mmz,extract-one> |
| 2966 |
\def\mmz@tolerance{0.01pt} |
| 2967 |
\def\mmz@if@roughly@equal#1#2#3{% |
| 2968 |
\dimen0=\dimexpr#2-#3\relax |
| 2969 |
\ifdim\dimen0<0pt |
| 2970 |
\dimen0=-\dimen0\relax |
| 2971 |
\fi |
| 2972 |
\ifdim\dimen0>#1\relax |
| 2973 |
\expandafter\@secondoftwo |
| 2974 |
\else |
| 2975 |
% The exact tolerated difference is, well, tolerated. This is a must to |
| 2976 |
% support |tolerance=0pt|. |
| 2977 |
\expandafter\@firstoftwo |
| 2978 |
\fi |
| 2979 |
}% |
| 2980 |
%</mmz,extract-one> |
| 2981 |
%<*extract-one> |
| 2982 |
% Grab the extern page from the document and put it in a box. |
| 2983 |
\ifdefined\XeTeXversion |
| 2984 |
\setbox0=\hbox{\XeTeXpdffile \fromdocument\space page \pagenumber media}% |
| 2985 |
\else |
| 2986 |
\ifdefined\luatexversion |
| 2987 |
\saveimageresource page \pagenumber mediabox {\fromdocument}% |
| 2988 |
\setbox0=\hbox{\useimageresource\lastsavedimageresourceindex}% |
| 2989 |
\else |
| 2990 |
\pdfximage page \pagenumber mediabox {\fromdocument}% |
| 2991 |
\setbox0=\hbox{\pdfrefximage\pdflastximage}% |
| 2992 |
\fi |
| 2993 |
\fi |
| 2994 |
% Check whether the extern page is of the expected size. |
| 2995 |
\newif\ifbaddimensions |
| 2996 |
\ifdefined\expectedwidth |
| 2997 |
\ifdefined\expectedheight |
| 2998 |
\mmz@if@roughly@equal{\mmz@tolerance}{\wd0}{\expectedwidth}{% |
| 2999 |
\mmz@if@roughly@equal{\mmz@tolerance}{\ht0}{\expectedheight}% |
| 3000 |
{}% |
| 3001 |
{\baddimensionstrue}% |
| 3002 |
}{\baddimensionstrue}% |
| 3003 |
\fi |
| 3004 |
\fi |
| 3005 |
% We'll setup the page geometry of the extern file and shipout the extern --- |
| 3006 |
% if all is well, or we're forced to do it. |
| 3007 |
\ifdefined\luatexversion |
| 3008 |
\let\pdfpagewidth\pagewidth |
| 3009 |
\let\pdfpageheight\pageheight |
| 3010 |
\def\pdfhorigin{\pdfvariable horigin}% |
| 3011 |
\def\pdfvorigin{\pdfvariable vorigin}% |
| 3012 |
\fi |
| 3013 |
\def\do@shipout{% |
| 3014 |
\pdfpagewidth=\wd0 |
| 3015 |
\pdfpageheight=\ht0 |
| 3016 |
\ifdefined\XeTeXversion |
| 3017 |
\hoffset -1 true in |
| 3018 |
\voffset -1 true in |
| 3019 |
\else |
| 3020 |
\pdfhorigin=0pt |
| 3021 |
\pdfvorigin=0pt |
| 3022 |
\fi |
| 3023 |
\shipout\box0 |
| 3024 |
} |
| 3025 |
\ifbaddimensions |
| 3026 |
\doublewarning{I refuse to extract page \pagenumber\space from |
| 3027 |
"\fromdocument", because its size (\the\wd0 \space x \the\ht0) is not |
| 3028 |
what I expected (\expectedwidth\space x \expectedheight)}% |
| 3029 |
\ifforce\do@shipout\fi |
| 3030 |
\else |
| 3031 |
\do@shipout |
| 3032 |
\fi |
| 3033 |
% If logging is in effect and the extern dimensions were not what we expected, |
| 3034 |
% write a warning into the log. |
| 3035 |
\ifdefined\logfile |
| 3036 |
\immediate\write\extractionlog{\noexpand\endinput}% |
| 3037 |
\immediate\closeout\extractionlog |
| 3038 |
\fi |
| 3039 |
\bye |
| 3040 |
%</extract-one> |
| 3041 |
% \end{macro} |
| 3042 |
% |
| 3043 |
% \section{Automemoization} |
| 3044 |
% \label{sec:code:automemoization} |
| 3045 |
% |
| 3046 |
% \paragraph{Install} the advising framework implemented by our auxiliary |
| 3047 |
% package Advice, which automemoization depends on. This will define keys |
| 3048 |
% |auto|, |activate| etc.\ in our keypath. |
| 3049 |
%<*mmz> |
| 3050 |
\mmzset{ |
| 3051 |
.install advice={setup key=auto, activation=deferred}, |
| 3052 |
% We switch to the immediate activation at the end of the preamble. |
| 3053 |
begindocument/before/.append style={activation=immediate}, |
| 3054 |
} |
| 3055 |
% |
| 3056 |
% \begin{key}{manual} |
| 3057 |
% Unless the user switched on |manual|, we perform the deferred |
| 3058 |
% (de)activations at the beginning of the document (and then clear the style, |
| 3059 |
% so that any further deferred activations will start with a clean slate). |
| 3060 |
% In \hologo{LaTeX}, we will use the latest possible hook, |
| 3061 |
% |begindocument/end|, as we want to hack into commands defined by other |
| 3062 |
% packages. (The \hologo{TeX} conditional needs to be defined before using |
| 3063 |
% it in |.append code| below. |
| 3064 |
\newif\ifmmz@manual |
| 3065 |
\mmzset{ |
| 3066 |
manual/.is if=mmz@manual, |
| 3067 |
begindocument/end/.append code={% |
| 3068 |
\ifmmz@manual |
| 3069 |
\else |
| 3070 |
\pgfkeysalso{activate deferred,activate deferred/.code={}}% |
| 3071 |
\fi |
| 3072 |
}, |
| 3073 |
% \end{key} |
| 3074 |
% |
| 3075 |
% \paragraph{Announce} Memoize's run conditions and handlers. |
| 3076 |
auto/.cd, |
| 3077 |
run if memoization is possible/.style={ |
| 3078 |
run conditions=\mmz@auto@rc@if@memoization@possible |
| 3079 |
}, |
| 3080 |
run if memoizing/.style={run conditions=\mmz@auto@rc@if@memoizing}, |
| 3081 |
apply options/.style={ |
| 3082 |
bailout handler=\mmz@auto@bailout, |
| 3083 |
outer handler=\mmz@auto@outer, |
| 3084 |
}, |
| 3085 |
memoize/.style={ |
| 3086 |
run if memoization is possible, |
| 3087 |
apply options, |
| 3088 |
inner handler=\mmz@auto@memoize |
| 3089 |
}, |
| 3090 |
%<*latex> |
| 3091 |
noop/.style={run if memoization is possible, noop \AdviceType}, |
| 3092 |
noop command/.style={apply options, inner handler=\mmz@auto@noop}, |
| 3093 |
noop environment/.style={ |
| 3094 |
outer handler=\mmz@auto@noop@env, bailout handler=\mmz@auto@bailout}, |
| 3095 |
%</latex> |
| 3096 |
%<plain,context>noop/.style={inner handler=\mmz@auto@noop}, |
| 3097 |
nomemoize/.style={noop, options=disable}, |
| 3098 |
replicate/.style={run if memoizing, inner handler=\mmz@auto@replicate}, |
| 3099 |
to context/.style={run if memoizing, outer handler=\mmz@auto@tocontext}, |
| 3100 |
} |
| 3101 |
% |
| 3102 |
% \paragraph{Abortion} |
| 3103 |
% We cheat and let the |run conditions| do the work --- it is cheaper to just |
| 3104 |
% always abort than to invoke the outer handler. (As we don't set |
| 3105 |
% |\AdviceRuntrue|, the run conditions will never be satisfied.) |
| 3106 |
% \begin{listingregion}{_auto-abort} |
| 3107 |
\mmzset{ |
| 3108 |
auto/abort/.style={run conditions=\mmzAbort}, |
| 3109 |
} |
| 3110 |
% \end{listingregion} |
| 3111 |
% And the same for |unmemoizable|: |
| 3112 |
\mmzset{ |
| 3113 |
auto/unmemoizable/.style={run conditions=\mmzUnmemoizable}, |
| 3114 |
} |
| 3115 |
% For one, we abort upon |\pdfsavepos| (called |\savepos| in \hologo{LuaTeX}). |
| 3116 |
% Second, unless in \hologo{LuaTeX}, we submit |\errmessage|, which allows us |
| 3117 |
% to detect at least some errors --- in \hologo{LuaTeX}, we have a more |
| 3118 |
% bullet-proof system of detecting errors, see |\mmz@memoize| in |
| 3119 |
% \S\ref{sec:code:memoization-process}. |
| 3120 |
\ifdef\luatexversion{% |
| 3121 |
\mmzset{auto=\savepos{abort}} |
| 3122 |
}{% |
| 3123 |
\mmzset{ |
| 3124 |
auto=\pdfsavepos{abort}, |
| 3125 |
auto=\errmessage{abort}, |
| 3126 |
} |
| 3127 |
} |
| 3128 |
% |
| 3129 |
% \begin{mmzautokey}{run if memoization is possible} |
| 3130 |
% \begin{macro}{\mmz@auto@rc@if@memoization@possible} |
| 3131 |
% These run conditions are used by |memoize| and |noop|: Memoize should be |
| 3132 |
% enabled, but we should not be already within Memoize, i.e.\ memoizing or |
| 3133 |
% normally compiling some code submitted to memoization. |
| 3134 |
% \begin{listingregion}{_auto-run-if-memoization-is-possible} |
| 3135 |
\def\mmz@auto@rc@if@memoization@possible{% |
| 3136 |
\ifmemoize |
| 3137 |
\ifinmemoize |
| 3138 |
\else |
| 3139 |
\AdviceRuntrue |
| 3140 |
\fi |
| 3141 |
\fi |
| 3142 |
} |
| 3143 |
% \end{listingregion} |
| 3144 |
% \end{macro} |
| 3145 |
% \end{mmzautokey} |
| 3146 |
% |
| 3147 |
% \begin{mmzautokey}{run if memoizing} |
| 3148 |
% \begin{macro}{\mmz@auto@rc@if@memoizing} |
| 3149 |
% These run conditions are used by |\label| and |\ref|: they should |
| 3150 |
% be handled only during memoization (which implies that Memoize is enabled). |
| 3151 |
\def\mmz@auto@rc@if@memoizing{% |
| 3152 |
\ifmemoizing\AdviceRuntrue\fi |
| 3153 |
} |
| 3154 |
% \end{macro} |
| 3155 |
% \end{mmzautokey} |
| 3156 |
% |
| 3157 |
% \begin{macro}{\mmznext} |
| 3158 |
% The next-options, set by this macro, will be applied to the next, and only |
| 3159 |
% next instance of automemoization. We set the next-options globally, so |
| 3160 |
% that only the linear order of the invocation matters. Note that |\mmznext|, |
| 3161 |
% being a user command, must also be defined in package |nomemoize|. |
| 3162 |
%</mmz> |
| 3163 |
%<nommz>\def\mmznext#1{\ignorespaces} |
| 3164 |
%<*mmz> |
| 3165 |
\def\mmznext#1{\gdef\mmz@next{#1}\ignorespaces} |
| 3166 |
\mmznext{}% |
| 3167 |
% \end{macro} |
| 3168 |
% |
| 3169 |
% \begin{mmzautokey}{apply options} |
| 3170 |
% \begin{macro}{\mmz@auto@outer,\mmz@auto@bailout} |
| 3171 |
% The outer and the bailout handler defined here work as a team. The outer |
| 3172 |
% handler's job is to apply the auto- and the next-options; therefore, the |
| 3173 |
% bailout handler must consume the next-options as well. To keep the |
| 3174 |
% option application local, the outer handler opens a group, which is |
| 3175 |
% expected to be closed by the inner handler. This key is used by |
| 3176 |
% |memoize| and |noop command|. |
| 3177 |
% \begin{listingregion}{_auto-memoize-outer} |
| 3178 |
\def\mmz@auto@outer{% |
| 3179 |
\begingroup |
| 3180 |
\mmzAutoInit |
| 3181 |
\AdviceCollector |
| 3182 |
} |
| 3183 |
% \end{listingregion} |
| 3184 |
% \begin{listingregion}{_auto-memoize-bailout} |
| 3185 |
\def\mmz@auto@bailout{% |
| 3186 |
\mmznext{}% |
| 3187 |
} |
| 3188 |
% \end{listingregion} |
| 3189 |
% \bigskip |
| 3190 |
% \end{macro} |
| 3191 |
% \end{mmzautokey} |
| 3192 |
% |
| 3193 |
% \begin{macro}{\mmzAutoInit} |
| 3194 |
% Apply first the auto-options, and then the next-options (and clear the |
| 3195 |
% latter). Finally, if we have any extra collector options (set by the |
| 3196 |
% |verbatim| keys), append them to Advice's (raw) collector options. |
| 3197 |
\def\mmzAutoInit{% |
| 3198 |
\ifdefempty\AdviceOptions{}{\expandafter\mmzset\expandafter{\AdviceOptions}}% |
| 3199 |
\ifdefempty\mmz@next{}{\expandafter\mmzset\expandafter{\mmz@next}\mmznext{}}% |
| 3200 |
\eappto\AdviceRawCollectorOptions{\expandonce\mmzRawCollectorOptions}% |
| 3201 |
} |
| 3202 |
% \end{macro} |
| 3203 |
% |
| 3204 |
% \begin{mmzautokey}{memoize} |
| 3205 |
% \begin{macro}{\mmz@auto@memoize} |
| 3206 |
% This key installs the inner handler for memoization. If you compare this |
| 3207 |
% handler to the definition of |\mmz| in |
| 3208 |
% section~\ref{sec:code:memoization:manual}, you will see that the only |
| 3209 |
% thing left to do here is to start memoization with |\Memoize|, everything |
| 3210 |
% else is already done by the advising framework, as customized by Memoize. |
| 3211 |
% |
| 3212 |
% The first argument to |\Memoize| is the memoization key (which the code |
| 3213 |
% md5sum is computed off of); it consists of the handled code (the contents |
| 3214 |
% of |\AdviceReplaced|) and its arguments, which were collected into |##1|. |
| 3215 |
% The second argument is the code which the memoization driver will |
| 3216 |
% execute. |\AdviceOriginal|, if invoked right away, would execute the |
| 3217 |
% original command; but as this macro is only guaranteed to refer to this |
| 3218 |
% command within the advice handlers, we expand it before calling |\Memoize|. |
| 3219 |
% that command. |
| 3220 |
% |
| 3221 |
% Note that we don't have to define different handlers for commands and |
| 3222 |
% environments, and for different \hologo{TeX} formats. When memoizing |
| 3223 |
% command |\foo|, |\AdviceReplaced| contains |\foo|. When memoizing |
| 3224 |
% environment |foo|, |\AdviceReplaced| contains |\begin{foo}|, |\foo| or |
| 3225 |
% |\startfoo|, depending on the format, while the closing tag |
| 3226 |
% (|\end{foo}|, |\endfoo| or |\stopfoo|) occurs at the end of the |
| 3227 |
% collected arguments, because |apply options| appended |
| 3228 |
% |\collargsEndTagtrue| to |raw collector options|. |
| 3229 |
% |
| 3230 |
% This macro has no formal parameters, because the collected arguments will |
| 3231 |
% be grabbed by |\mmz@marshal|, which we have to go through because |
| 3232 |
% executing |\Memoize| closes the memoization group and we lose the current |
| 3233 |
% value of |\ifmmz@ignorespaces|. (We also can't use |\aftergroup|, |
| 3234 |
% because closing the group is not the final thing |\Memoize| does.) |
| 3235 |
% \begin{listingregion}{_auto-memoize-inner} |
| 3236 |
\long\def\mmz@auto@memoize#1{% |
| 3237 |
\expanded{% |
| 3238 |
\noexpand\Memoize |
| 3239 |
{\expandonce\AdviceReplaced\unexpanded{#1}}% |
| 3240 |
{\expandonce\AdviceOriginal\unexpanded{#1}}% |
| 3241 |
\ifmmz@ignorespaces\ignorespaces\fi |
| 3242 |
}% |
| 3243 |
} |
| 3244 |
% \end{listingregion} |
| 3245 |
% \end{macro} |
| 3246 |
% \end{mmzautokey} |
| 3247 |
% |
| 3248 |
% \begin{mmzautokey}{noop} |
| 3249 |
% \begin{macro}{\mmz@auto@noop,\mmz@auto@noop@env} |
| 3250 |
% The no-operation handler can be used to apply certain options for the |
| 3251 |
% span of the execution of the handled command or environment. This is |
| 3252 |
% exploited by |auto/nomemoize|, which sets |disable| as an auto-option. |
| 3253 |
% |
| 3254 |
% The handler for commands and non-\hologo{LaTeX} environments is |
| 3255 |
% implemented as an inner handler. On its own, it does nothing except |
| 3256 |
% honor |verbatim| and |ignore spaces| (only takes care of |verbatim| and |
| 3257 |
% |ignore spaces| (in the same way as the memoization handler above), but |
| 3258 |
% it is intended to be used alongside the default outer handler, which |
| 3259 |
% applies the auto- and the next-options. As that handler opens a group |
| 3260 |
% (and this handler closes it), we have effectively delimited the effect of |
| 3261 |
% those options to this invocation of the handled command or environment. |
| 3262 |
\long\def\mmz@auto@noop#1{% |
| 3263 |
\expandafter\mmz@maybe@scantokens\expandafter{\AdviceOriginal#1}% |
| 3264 |
\expandafter\endgroup |
| 3265 |
\ifmmz@ignorespaces\ignorespaces\fi |
| 3266 |
} |
| 3267 |
% In \hologo{LaTeX}, and only there, commands and environments need separate |
| 3268 |
% treatment. As \hologo{LaTeX} environments introduce a group of their own, we |
| 3269 |
% can simply hook our initialization into the beginning of the environment (as |
| 3270 |
% a one-time hook). Consequently, we don't need to collect the environment |
| 3271 |
% body, so this can be an outer handler. |
| 3272 |
%<*latex> |
| 3273 |
\def\mmz@auto@noop@env{% |
| 3274 |
\AddToHookNext{env/\AdviceName/begin}{% |
| 3275 |
\mmzAutoInit |
| 3276 |
\ifmmz@ignorespaces\ignorespacesafterend\fi |
| 3277 |
}% |
| 3278 |
\AdviceOriginal |
| 3279 |
} |
| 3280 |
%</latex> |
| 3281 |
% \end{macro} |
| 3282 |
% \end{mmzautokey} |
| 3283 |
% |
| 3284 |
% \begin{mmzautokey}{replicate} |
| 3285 |
% \begin{macro}{\mmz@auto@replicate} |
| 3286 |
% This inner handler writes a copy of the handled command or environment's |
| 3287 |
% invocation into the cc-memo (and then executes it). As it is used |
| 3288 |
% alongside |run if memoizing|, the replicated command in the cc-memo will |
| 3289 |
% always execute the original command. The system works even if |
| 3290 |
% replication is off when the cc-memo is input; in that case, the control |
| 3291 |
% sequence in the cc-memo directly executes the original command. |
| 3292 |
% |
| 3293 |
% This handler takes an option, |expanded| --- if given, the collected |
| 3294 |
% arguments will be expanded (under protection) before being written into |
| 3295 |
% the cc-memo. |
| 3296 |
\def\mmz@auto@replicate#1{% |
| 3297 |
\begingroup |
| 3298 |
\let\mmz@auto@replicate@expansion\unexpanded |
| 3299 |
\expandafter\pgfqkeys\expanded{{/mmz/auto/replicate}{\AdviceOptions}}% |
| 3300 |
%<latex>\let\protect\noexpand |
| 3301 |
\expanded{% |
| 3302 |
\endgroup |
| 3303 |
\noexpand\gtoksapp\noexpand\mmzCCMemo{% |
| 3304 |
\expandonce\AdviceReplaced\mmz@auto@replicate@expansion{#1}}% |
| 3305 |
\expandonce\AdviceOriginal\unexpanded{#1}% |
| 3306 |
}% |
| 3307 |
} |
| 3308 |
\pgfqkeys{/mmz/auto/replicate}{ |
| 3309 |
expanded/.code={\let\mmz@auto@replicate@expansion\@firstofone}, |
| 3310 |
} |
| 3311 |
% \end{macro} |
| 3312 |
% \end{mmzautokey} |
| 3313 |
% |
| 3314 |
% \begin{mmzautokey}{to context} |
| 3315 |
% \begin{macro}{\mmz@auto@tocontext} |
| 3316 |
% This outer handler appends the original definition of the handled command |
| 3317 |
% to the context. The |\expandafter| are there to expand |\AdviceName| |
| 3318 |
% once before fully expanding |\AdviceGetOriginalCsname|. |
| 3319 |
\def\mmz@auto@tocontext{% |
| 3320 |
\expanded{% |
| 3321 |
\noexpand\pgfkeysvalueof{/mmz/context/.@cmd}% |
| 3322 |
original "\AdviceNamespace" csname "\AdviceCsname"={% |
| 3323 |
\noexpand\expanded{% |
| 3324 |
\noexpand\noexpand\noexpand\meaning |
| 3325 |
\noexpand\AdviceCsnameGetOriginal{\AdviceNamespace}{\AdviceCsname}% |
| 3326 |
}% |
| 3327 |
}% |
| 3328 |
}% |
| 3329 |
\pgfeov |
| 3330 |
\AdviceOriginal |
| 3331 |
} |
| 3332 |
% \end{macro} |
| 3333 |
% \end{mmzautokey} |
| 3334 |
% |
| 3335 |
% \subsection{\hologo{LaTeX}-specific handlers} |
| 3336 |
% |
| 3337 |
% We handle cross-referencing (both the |\label| and the |\ref| side) and |
| 3338 |
% indexing. Note that the latter is a straightforward instance of replication. |
| 3339 |
% |
| 3340 |
%<*latex> |
| 3341 |
\mmzset{ |
| 3342 |
auto/.cd, |
| 3343 |
ref/.style={outer handler=\mmz@auto@ref\mmzNoRef, run if memoizing}, |
| 3344 |
force ref/.style={outer handler=\mmz@auto@ref\mmzForceNoRef, run if memoizing}, |
| 3345 |
} |
| 3346 |
\mmzset{ |
| 3347 |
auto=\ref{ref}, |
| 3348 |
auto=\pageref{ref}, |
| 3349 |
auto=\label{run if memoizing, outer handler=\mmz@auto@label}, |
| 3350 |
auto=\index{replicate, args=m, expanded}, |
| 3351 |
} |
| 3352 |
% |
| 3353 |
% \begin{mmzautokey}{ref, force ref} |
| 3354 |
% \begin{macro}{\mmz@auto@ref} |
| 3355 |
% These keys install an outer handler which appends a cross-reference to the |
| 3356 |
% context. |force ref| does this even if the reference key is undefined, |
| 3357 |
% while |ref| aborts memoization in such a case --- the idea is that it makes |
| 3358 |
% no sense to memoize when we expect the context to change in the next |
| 3359 |
% compilation anyway. |
| 3360 |
% |
| 3361 |
% Any command taking a mandatory braced reference key argument potentially |
| 3362 |
% preceded by optional arguments of (almost) any kind may be submitted to |
| 3363 |
% these keys. This follows from the parameter list of |\mmz@auto@ref@i|, |
| 3364 |
% where |#2| grabs everything up to the first opening brace. The downside of |
| 3365 |
% the flexibility regarding the optional arguments is that unbraced |
| 3366 |
% single-token reference keys will cause an error, but as such usages of |
| 3367 |
% |\ref| and friends should be virtually inexistent, we let the bug stay. |
| 3368 |
% |
| 3369 |
% |#1| should be either |\mmzNoRef| or |\mmzForceNoRef|. |#2| will receive |
| 3370 |
% any optional arguments of |\ref| (or |\pageref|, or whatever), and |#3| in |
| 3371 |
% |\mmz@auto@ref@i| is the cross-reference key. |
| 3372 |
% \begin{listingregion}{_auto-memoize-ref} |
| 3373 |
\def\mmz@auto@ref#1#2#{\mmz@auto@ref@i#1{#2}} |
| 3374 |
\def\mmz@auto@ref@i#1#2#3{% |
| 3375 |
#1{#3}% |
| 3376 |
\AdviceOriginal#2{#3}% |
| 3377 |
} |
| 3378 |
% \end{listingregion} |
| 3379 |
% \end{macro} |
| 3380 |
% \end{mmzautokey} |
| 3381 |
% |
| 3382 |
% \begin{macro}{\mmzForceNoRef,\mmzNoRef} |
| 3383 |
% These macros do the real job in the outer handlers for cross-referencing, |
| 3384 |
% but it might be useful to have them publicly available. |\mmzForceNoRef| |
| 3385 |
% appends the reference key to the context. |\mmzNoRef| only does that if |
| 3386 |
% the reference is defined, otherwise it aborts the memoization. |
| 3387 |
\def\mmzForceNoRef#1{% |
| 3388 |
\mmz@Cos |
| 3389 |
\expandafter\mmz@mtoc@csname\expandafter{\expanded{r@#1}}% |
| 3390 |
\ignorespaces |
| 3391 |
} |
| 3392 |
\def\mmzNoRef#1{% |
| 3393 |
\ifcsundef{r@#1}{\mmzAbort}{\mmzForceNoRef{#1}}% |
| 3394 |
\ignorespaces |
| 3395 |
} |
| 3396 |
% \end{macro} |
| 3397 |
% |
| 3398 |
% \begin{mmzautokey}{refrange, force refrange} |
| 3399 |
% \begin{macro}{\mmz@auto@refrange} |
| 3400 |
% Let's rinse and repeat for reference ranges. The code is virtually the |
| 3401 |
% same as above, but we grab two reference key arguments (|#3| and |#4|) in |
| 3402 |
% the final macro.\indentmacrocode |
| 3403 |
\mmzset{ |
| 3404 |
auto/.cd, |
| 3405 |
refrange/.style={outer handler=\mmz@auto@refrange\mmzNoRef, |
| 3406 |
bailout handler=\relax, run if memoizing}, |
| 3407 |
force refrange/.style={outer handler=\mmz@auto@refrange\mmzForceNoRef, |
| 3408 |
bailout handler=\relax, run if memoizing}, |
| 3409 |
} |
| 3410 |
% \noindentmacrocode |
| 3411 |
\def\mmz@auto@refrange#1#2#{\mmz@auto@refrange@i#1{#2}} |
| 3412 |
\def\mmz@auto@refrange@i#1#2#3#4{% |
| 3413 |
#1{#3}% |
| 3414 |
#1{#4}% |
| 3415 |
\AdviceOriginal#2{#3}{#4}% |
| 3416 |
} |
| 3417 |
% \end{macro} |
| 3418 |
% \end{mmzautokey} |
| 3419 |
% |
| 3420 |
% \begin{mmzautokey}{multiref, force multiref} |
| 3421 |
% \begin{macro}{\mmz@auto@multiref} |
| 3422 |
% And one final time, for ``multi-references'', such as |cleveref|'s |
| 3423 |
% |\cref|, which can take a comma-separated list of reference keys in the |
| 3424 |
% sole argument. Again, only the final macro is any different, this time |
| 3425 |
% distributing |#1| (|\mmzNoRef| or |\mmzForceNoRef|) over |#3| by |
| 3426 |
% |\forcsvlist|. |
| 3427 |
\mmzset{ |
| 3428 |
auto/.cd, |
| 3429 |
multiref/.style={outer handler=\mmz@auto@multiref\mmzNoRef, |
| 3430 |
bailout handler=\relax, run if memoizing}, |
| 3431 |
force multiref/.style={outer handler=\mmz@auto@multiref\mmzForceNoRef, |
| 3432 |
bailout handler=\relax, run if memoizing}, |
| 3433 |
} |
| 3434 |
\def\mmz@auto@multiref#1#2#{\mmz@auto@multiref@i#1{#2}} |
| 3435 |
\def\mmz@auto@multiref@i#1#2#3{% |
| 3436 |
\forcsvlist{#1}{#3}% |
| 3437 |
\AdviceOriginal#2{#3}% |
| 3438 |
} |
| 3439 |
% \end{macro} |
| 3440 |
% \end{mmzautokey} |
| 3441 |
% |
| 3442 |
% \begin{macro}{\mmz@auto@label} |
| 3443 |
% The outer handler for \cs{label} must be defined specifically for this |
| 3444 |
% command. The generic replicating handler is not enough here, as we need to |
| 3445 |
% replicate both the invocation of |\label| and the definition of |
| 3446 |
% |\@currentlabel|. |
| 3447 |
\def\mmz@auto@label#1{% |
| 3448 |
\xtoksapp\mmzCCMemo{% |
| 3449 |
\noexpand\mmzLabel{#1}{\expandonce\@currentlabel}% |
| 3450 |
}% |
| 3451 |
\AdviceOriginal{#1}% |
| 3452 |
} |
| 3453 |
% \end{macro} |
| 3454 |
% |
| 3455 |
% \begin{macro}{\mmzLabel} |
| 3456 |
% This is the macro that |\label|'s handler writes into the cc-memo. The |
| 3457 |
% first argument is the reference key; the second argument is the value of |
| 3458 |
% |\@currentlabel| at the time of invocation |\label| during memoization, |
| 3459 |
% which this macro temporarily restores. |
| 3460 |
\def\mmzLabel#1#2{% |
| 3461 |
\begingroup |
| 3462 |
\def\@currentlabel{#2}% |
| 3463 |
\label{#1}% |
| 3464 |
\endgroup |
| 3465 |
} |
| 3466 |
%</latex> |
| 3467 |
% |
| 3468 |
% \end{macro} |
| 3469 |
% |
| 3470 |
% \section{Support for various classes and packages} |
| 3471 |
% |
| 3472 |
% As the support for foreign classes and packages is only loaded in |
| 3473 |
% |begindocument/before|, any keys defined there are undefined in the preamble, |
| 3474 |
% and can only be used in the document body. We don't want to burden the |
| 3475 |
% author with this detail, so we try executing any unknown keys once again at |
| 3476 |
% |begindocument/end|. |
| 3477 |
\def\mmz@unknown{} |
| 3478 |
\mmzset{ |
| 3479 |
.unknown/.code={% |
| 3480 |
\eappto\mmz@unknown{,\pgfkeyscurrentkey={#1}}% |
| 3481 |
}, |
| 3482 |
begindocument/end/.append code={% |
| 3483 |
\pgfkeyslet{/mmz/.unknown/.@cmd}\undefined |
| 3484 |
\expandafter\pgfkeysalso\expandafter{\mmz@unknown}% |
| 3485 |
}, |
| 3486 |
} |
| 3487 |
% |
| 3488 |
%<*latex> |
| 3489 |
\AddToHook{shipout/before}[memoize]{\global\advance\mmzExtraPages-1\relax} |
| 3490 |
\AddToHook{shipout/after}[memoize]{\global\advance\mmzExtraPages1\relax} |
| 3491 |
\mmzset{auto=\DiscardShipoutBox{ |
| 3492 |
outer handler=\global\advance\mmzExtraPages1\relax\AdviceOriginal}} |
| 3493 |
%</latex> |
| 3494 |
% An auxiliary macro for loading the support code |memoize-*.code.tex| from |
| 3495 |
% hook |begindocument/before|, where |@| already has catcode `other'. The |
| 3496 |
% |\input| statement should be enclosed in |\mmz@makeatletter| and |
| 3497 |
% |\mmz@restoreatcatcode|. |
| 3498 |
\def\mmz@makeatletter{% |
| 3499 |
\edef\mmz@restoreatcatcode{\catcode`\noexpand\@\the\catcode`\@\relax}% |
| 3500 |
\catcode`\@=11 |
| 3501 |
} |
| 3502 |
% |
| 3503 |
% Utility macro for clarity below. |#1| is the name of the package which should |
| 3504 |
% be loaded (used with \hologo{LaTeX}) and |#2| is the name of the command |
| 3505 |
% which should be defined (used with \hologo{plainTeX} and \hologo{ConTeXt}) |
| 3506 |
% for |#3| to be executed at the beginning of the document. |
| 3507 |
\def\mmz@if@package@loaded#1#2#3{% |
| 3508 |
\mmzset{% |
| 3509 |
begindocument/before/.append code={% |
| 3510 |
%<latex>\@ifpackageloaded{#1}{% |
| 3511 |
%<plain,context>\ifdefined#2% |
| 3512 |
#3% |
| 3513 |
%<plain,context>\fi |
| 3514 |
%<latex>}{}% |
| 3515 |
}% |
| 3516 |
}% |
| 3517 |
} |
| 3518 |
% |
| 3519 |
% \subsection[PGF]{\pkg{PGF}} |
| 3520 |
% |
| 3521 |
\mmz@if@package@loaded{pgf}{% |
| 3522 |
%<plain>\pgfpicture |
| 3523 |
%<context>\startpgfpicture |
| 3524 |
}{% |
| 3525 |
% We have very little code here, so we don't bother introducing |
| 3526 |
% |memoize-pgf.code.tex|. |
| 3527 |
\def\mmzPgfAtBeginMemoization{% |
| 3528 |
\edef\mmz@pgfpictureid{% |
| 3529 |
\the\pgf@picture@serial@count |
| 3530 |
}% |
| 3531 |
}% |
| 3532 |
\def\mmzPgfAtEndMemoization{% |
| 3533 |
\edef\mmz@temp{% |
| 3534 |
\the\numexpr\pgf@picture@serial@count-\mmz@pgfpictureid\relax |
| 3535 |
}% |
| 3536 |
\ifx\mmz@temp=0 |
| 3537 |
\else |
| 3538 |
\xtoksapp\mmzCCMemo{\noexpand\mmzStepPgfPictureId{\mmz@temp}}% |
| 3539 |
\fi |
| 3540 |
}% |
| 3541 |
\def\mmzStepPgfPictureId##1{% |
| 3542 |
\global\advance\pgf@picture@serial@count##1\relax |
| 3543 |
}% |
| 3544 |
% \begin{listingregion}{_support-pgf} |
| 3545 |
\mmzset{% |
| 3546 |
at begin memoization=\mmzPgfAtBeginMemoization, |
| 3547 |
at end memoization=\mmzPgfAtEndMemoization, |
| 3548 |
}% |
| 3549 |
% \end{listingregion} |
| 3550 |
} |
| 3551 |
% |
| 3552 |
% |
| 3553 |
% \subsection{\TikZ;} |
| 3554 |
% \label{sec:code:mmz:tikz} |
| 3555 |
% |
| 3556 |
% In this section, we activate \TikZ; support (the collector is defined by |
| 3557 |
% Advice). All the action happens at the end of the preamble, so that we can |
| 3558 |
% detect whether \TikZ; was loaded (regardless of whether Memoize was loaded |
| 3559 |
% before \TikZ;, or vice versa), but still input the definitions. |
| 3560 |
\mmz@if@package@loaded{tikz}{\tikz}{% |
| 3561 |
\input advice-tikz.code.tex |
| 3562 |
% We define and activate the automemoization handlers for the \TikZ; command |
| 3563 |
% and environment. |
| 3564 |
% \begin{listingregion}{_support-tikz} |
| 3565 |
\mmzset{% |
| 3566 |
auto={tikzpicture}{memoize}, |
| 3567 |
auto=\tikz{memoize, collector=\AdviceCollectTikZArguments}, |
| 3568 |
}% |
| 3569 |
% \end{listingregion} |
| 3570 |
} |
| 3571 |
% |
| 3572 |
% |
| 3573 |
% \subsection{Forest} |
| 3574 |
% \label{sec:code:forest} |
| 3575 |
% |
| 3576 |
% Forest will soon feature extensive memoization support, but for now, let's |
| 3577 |
% just enable the basic, single extern externalization. Command |\Forest| is |
| 3578 |
% defined using |xparse|, so |args| is unnecessary. |
| 3579 |
%<*latex> |
| 3580 |
\mmz@if@package@loaded{forest}{\Forest}{% |
| 3581 |
% \begin{listingregion}{_support-forest} |
| 3582 |
\mmzset{ |
| 3583 |
auto={forest}{memoize}, |
| 3584 |
auto=\Forest{memoize}, |
| 3585 |
}% |
| 3586 |
% \end{listingregion} |
| 3587 |
} |
| 3588 |
%</latex> |
| 3589 |
% |
| 3590 |
% \subsection{Beamer} |
| 3591 |
% \label{sec:code:beamer} |
| 3592 |
% |
| 3593 |
% The Beamer code is explained in \MS\ref{sec:per-overlay}. |
| 3594 |
% |
| 3595 |
%<*latex> |
| 3596 |
\AddToHook{begindocument/before}{% |
| 3597 |
\@ifclassloaded{beamer}{% |
| 3598 |
\mmz@makeatletter |
| 3599 |
\input{memoize-beamer.code.tex}% |
| 3600 |
\mmz@restoreatcatcode |
| 3601 |
}{}% |
| 3602 |
\@ifpackageloaded{beamerarticle}{% |
| 3603 |
\mmz@makeatletter |
| 3604 |
\input{memoize-beamer.code.tex}% |
| 3605 |
\mmz@restoreatcatcode |
| 3606 |
}{}% |
| 3607 |
} |
| 3608 |
%</latex> |
| 3609 |
%</mmz> |
| 3610 |
%<*beamer> |
| 3611 |
\mmzset{ |
| 3612 |
per overlay/.code={}, |
| 3613 |
beamer mode to prefix/.style={ |
| 3614 |
prefix=\mmz@prefix@dir\mmz@prefix@name\beamer@currentmode_mode. |
| 3615 |
}, |
| 3616 |
}% |
| 3617 |
\@ifclassloaded{beamer}{% |
| 3618 |
% \begin{listingregion}{per-overlay} |
| 3619 |
\mmzset{ |
| 3620 |
per overlay/.style={ |
| 3621 |
/mmz/context={% |
| 3622 |
overlay=\csname beamer@overlaynumber\endcsname, |
| 3623 |
pauses=\ifmemoizing |
| 3624 |
\mmzBeamerPauses |
| 3625 |
\else |
| 3626 |
\expandafter\the\csname c@beamerpauses\endcsname |
| 3627 |
\fi |
| 3628 |
}, |
| 3629 |
/mmz/at begin memoization={% |
| 3630 |
\xdef\mmzBeamerPauses{\the\c@beamerpauses}% |
| 3631 |
\xtoksapp\mmzCMemo{% |
| 3632 |
\noexpand\mmzSetBeamerOverlays{\mmzBeamerPauses}{\beamer@overlaynumber}}% |
| 3633 |
\gtoksapp\mmzCCMemo{% |
| 3634 |
\only<all:\mmzBeamerOverlays>{}}% |
| 3635 |
}, |
| 3636 |
/mmz/at end memoization={% |
| 3637 |
\xtoksapp\mmzCCMemo{% |
| 3638 |
\noexpand\setcounter{beamerpauses}{\the\c@beamerpauses}}% |
| 3639 |
}, |
| 3640 |
/mmz/per overlay/.code={}, |
| 3641 |
}, |
| 3642 |
} |
| 3643 |
\def\mmzSetBeamerOverlays#1#2{% |
| 3644 |
\ifnum\c@beamerpauses=#1\relax |
| 3645 |
\gdef\mmzBeamerOverlays{#2}% |
| 3646 |
\ifnum\beamer@overlaynumber<#2\relax \mmz@temptrue \else \mmz@tempfalse \fi |
| 3647 |
\else |
| 3648 |
\mmz@temptrue |
| 3649 |
\fi |
| 3650 |
\ifmmz@temp |
| 3651 |
\appto\mmzAtBeginMemoization{% |
| 3652 |
\gtoksapp\mmzCMemo{\mmzSetBeamerOverlays{#1}{#2}}}% |
| 3653 |
\fi |
| 3654 |
}% |
| 3655 |
% \end{listingregion} |
| 3656 |
}% |
| 3657 |
%</beamer> |
| 3658 |
%<*mmz> |
| 3659 |
% |
| 3660 |
% \subsection{Biblatex} |
| 3661 |
% \label{sec:biblatex} |
| 3662 |
% |
| 3663 |
% |
| 3664 |
%<*latex> |
| 3665 |
\mmzset{ |
| 3666 |
biblatex/.code={% |
| 3667 |
\mmz@if@package@loaded{biblatex}{}{% |
| 3668 |
\mmz@makeatletter |
| 3669 |
\input memoize-biblatex.code.tex |
| 3670 |
\mmz@restoreatcatcode |
| 3671 |
\mmzset{#1}% |
| 3672 |
}% |
| 3673 |
}, |
| 3674 |
} |
| 3675 |
%</latex> |
| 3676 |
%</mmz> |
| 3677 |
%<*biblatex> |
| 3678 |
\mmzset{% |
| 3679 |
% Advise macro |\entry| occurring in |.bbl| files to collect the entry, |
| 3680 |
% verbatim. |args|: |m| = citation key, |&&{...}u| = the entry, verbatim, |
| 3681 |
% braced --- so |\blx@bbl@entry| will receive two mandatory arguments. |
| 3682 |
auto=\blx@bbl@entry{ |
| 3683 |
inner handler=\mmz@biblatex@entry, |
| 3684 |
args={% |
| 3685 |
m% |
| 3686 |
&&{\collargsVerb |
| 3687 |
\collargsAppendExpandablePostprocessor{{\the\collargsArg}}% |
| 3688 |
}u{\endentry}% |
| 3689 |
}, |
| 3690 |
% No braces around the collected arguments, as each is already braced on |
| 3691 |
% its own. |
| 3692 |
raw collector options=\collargsReturnPlain, |
| 3693 |
}, |
| 3694 |
% \begin{mmzautokey}{cite,volcite,cites,volcites} |
| 3695 |
% \indentmacrocode Define handlers for citation commands. |
| 3696 |
auto/cite/.style={ |
| 3697 |
run conditions=\mmz@biblatex@cite@rc, |
| 3698 |
outer handler=\mmz@biblatex@cite@outer, |
| 3699 |
args=l*m, |
| 3700 |
raw collector options=\mmz@biblatex@def@star\collargsReturnNo, |
| 3701 |
inner handler=\mmz@biblatex@cite@inner, |
| 3702 |
}, |
| 3703 |
% \noindentmacrocode We need a dedicated |volcite| even though |\volcite| |
| 3704 |
% executes |\cite| because otherwise, we would end up with |
| 3705 |
% |\cite{volume}{key}| in the cc-memo when |biblatex ccmemo cite=replicate|. |
| 3706 |
auto/volcite/.style={ |
| 3707 |
run if memoizing, |
| 3708 |
outer handler=\mmz@biblatex@cite@outer, |
| 3709 |
args=lml*m, |
| 3710 |
raw collector options=\mmz@biblatex@def@star\collargsReturnNo, |
| 3711 |
inner handler=\mmz@biblatex@cite@inner, |
| 3712 |
}, |
| 3713 |
auto/cites/.style={ |
| 3714 |
run conditions=\mmz@biblatex@cites@rc, |
| 3715 |
outer handler=\mmz@biblatex@cites@outer, |
| 3716 |
args=l*m, |
| 3717 |
raw collector options= |
| 3718 |
\mmz@biblatex@def@star\collargsClearArgsfalse\collargsReturnNo, |
| 3719 |
inner handler=\mmz@biblatex@cites@inner, |
| 3720 |
}, |
| 3721 |
auto/volcites/.style={ |
| 3722 |
run if memoizing, |
| 3723 |
outer handler=\mmz@biblatex@cites@outer, |
| 3724 |
args=lml*m, |
| 3725 |
raw collector options= |
| 3726 |
\mmz@biblatex@def@star\collargsClearArgsfalse\collargsReturnNo, |
| 3727 |
inner handler=\mmz@biblatex@cites@inner, |
| 3728 |
}, |
| 3729 |
% \end{mmzautokey} |
| 3730 |
% \begin{mmzautokey}{biblatex ccmemo cite} |
| 3731 |
% What to put into the cc-memo, |\nocite| or the handled citation command? |
| 3732 |
biblatex ccmemo cite/.is choice, |
| 3733 |
biblatex ccmemo cite/nocite/.code={% |
| 3734 |
\let\mmz@biblatex@do@ccmemo\mmz@biblatex@do@nocite |
| 3735 |
}, |
| 3736 |
biblatex ccmemo cite/replicate/.code={% |
| 3737 |
\let\mmz@biblatex@do@ccmemo\mmz@biblatex@do@replicate |
| 3738 |
}, |
| 3739 |
% \end{mmzautokey} |
| 3740 |
}% |
| 3741 |
% \begin{macro}{\mmz@biblatex@entry} |
| 3742 |
% This macro stores the MD5 sum of the |\entry| when reading the |.bbl| file. |
| 3743 |
\def\mmz@biblatex@entry#1#2{% |
| 3744 |
\edef\mmz@temp{\pdf@mdfivesum{#2}}% |
| 3745 |
\global\cslet{mmz@bbl@#1}\mmz@temp |
| 3746 |
\mmz@scantokens{\AdviceOriginal{#1}#2}% |
| 3747 |
} |
| 3748 |
% \end{macro} |
| 3749 |
% \begin{macro}{\mmz@biblatex@cite@rc,\mmz@biblatex@cites@rc} |
| 3750 |
% \indentmacrocode Run if memoizing but not within a |\volcite| |
| 3751 |
% command. Applied to |\cite(s)|. |
| 3752 |
\def\mmz@biblatex@cite@rc{% |
| 3753 |
\ifmemoizing |
| 3754 |
% \noindentmacrocode We cannot use the official |\ifvolcite|, or even the |
| 3755 |
% |blx@volcite| toggle it depends on, because these are defined\slash set |
| 3756 |
% within the next-citation hook, which is yet to be executed. So we |
| 3757 |
% depend on the internal detail that |\volcite| and friends redefine |
| 3758 |
% |\blx@citeargs| to |\blx@volciteargs|. |
| 3759 |
\ifx\blx@citeargs\blx@volciteargs |
| 3760 |
\else |
| 3761 |
\AdviceRuntrue |
| 3762 |
\fi |
| 3763 |
\fi |
| 3764 |
} |
| 3765 |
\def\mmz@biblatex@cites@rc{% |
| 3766 |
\ifmemoizing |
| 3767 |
% The internal detail with |\volcites|: it defines a hook. |
| 3768 |
\ifdef\blx@hook@mcite@before{}{\AdviceRuntrue}% |
| 3769 |
\fi |
| 3770 |
} |
| 3771 |
% \end{macro} |
| 3772 |
% \begin{macro}{\mmz@biblatex@cite@outer} |
| 3773 |
% Initialize the macro receiving the citation key(s), and execute the |
| 3774 |
% collector. |
| 3775 |
\def\mmz@biblatex@cite@outer{% |
| 3776 |
\gdef\mmz@biblatex@keys{}% |
| 3777 |
\AdviceCollector |
| 3778 |
} |
| 3779 |
% \end{macro} |
| 3780 |
% \begin{macro}{\mmz@biblatex@mark@citation@key} |
| 3781 |
% We \emph{append} to |\mmz@biblatex@keys| to automatically collect all |
| 3782 |
% citation keys of a |\cites| command; note that we use this system for |
| 3783 |
% |\cite| as well. |
| 3784 |
\def\mmz@biblatex@def@star{% |
| 3785 |
\collargsAlias{*}{&&{\mmz@biblatex@mark@citation@key}}% |
| 3786 |
} |
| 3787 |
\def\mmz@biblatex@mark@citation@key{% |
| 3788 |
\collargsAppendPreprocessor{\xappto\mmz@biblatex@keys{,\the\collargsArg}}% |
| 3789 |
} |
| 3790 |
% \end{macro} |
| 3791 |
% \begin{macro}{\mmz@biblatex@cite@inner} |
| 3792 |
% This macro puts the cites reference keys into the context, and adds |
| 3793 |
% |\nocite|, or the handled citation command, to the cc-memo. |
| 3794 |
\def\mmz@biblatex@cite@inner{% |
| 3795 |
\mmz@biblatex@do@context |
| 3796 |
\mmz@biblatex@do@ccmemo |
| 3797 |
\expandafter\AdviceOriginal\the\collargsArgs |
| 3798 |
} |
| 3799 |
\def\mmz@biblatex@do@context{% |
| 3800 |
\expandafter\forcsvlist |
| 3801 |
\expandafter\mmz@biblatex@do@context@one |
| 3802 |
\expandafter{\mmz@biblatex@keys}% |
| 3803 |
} |
| 3804 |
\def\mmz@biblatex@do@context@one#1{% |
| 3805 |
\mmz@Cos |
| 3806 |
\mmz@mtoc@csname{mmz@bbl@#1}% |
| 3807 |
\ifcsdef{mmz@bbl@#1}{}{\mmzAbort}% |
| 3808 |
} |
| 3809 |
\def\mmz@biblatex@do@nocite{% |
| 3810 |
\xtoksapp\mmzCCMemo{% |
| 3811 |
\noexpand\nocite{\mmz@biblatex@keys}% |
| 3812 |
}% |
| 3813 |
} |
| 3814 |
\def\mmz@biblatex@do@replicate{% |
| 3815 |
\xtoksapp\mmzCCMemo{% |
| 3816 |
{% |
| 3817 |
\nullfont |
| 3818 |
% It is ok to use |\AdviceName| here, as the cc-memo is never input |
| 3819 |
% during memoization. |
| 3820 |
\expandonce\AdviceName\the\collargsArgs |
| 3821 |
}% |
| 3822 |
}% |
| 3823 |
} |
| 3824 |
\let\mmz@biblatex@do@ccmemo\mmz@biblatex@do@nocite |
| 3825 |
% \end{macro} |
| 3826 |
% \begin{macro}{\mmz@biblatex@cites@outer} |
| 3827 |
% Same as for |cite|, but we iterate the collector as long as the arguments |
| 3828 |
% continue. |
| 3829 |
\def\mmz@biblatex@cites@outer{% |
| 3830 |
\global\collargsArgs{}% |
| 3831 |
\gdef\mmz@biblatex@keys{}% |
| 3832 |
\AdviceCollector |
| 3833 |
} |
| 3834 |
\def\mmz@biblatex@cites@inner{% |
| 3835 |
\futurelet\mmz@temp\mmz@biblatex@cites@inner@again |
| 3836 |
} |
| 3837 |
% If the following token is an opening brace or bracket, the |
| 3838 |
% multicite arguments continue. |
| 3839 |
\def\mmz@biblatex@cites@inner@again{% |
| 3840 |
\mmz@tempfalse |
| 3841 |
\ifx\mmz@temp\bgroup |
| 3842 |
\mmz@temptrue |
| 3843 |
\else |
| 3844 |
\ifx\mmz@temp[%] |
| 3845 |
\mmz@temptrue |
| 3846 |
\fi |
| 3847 |
\fi |
| 3848 |
\ifmmz@temp |
| 3849 |
\expandafter\AdviceCollector |
| 3850 |
\else |
| 3851 |
\expandafter\mmz@biblatex@cites@inner@finish |
| 3852 |
\fi |
| 3853 |
} |
| 3854 |
\def\mmz@biblatex@cites@inner@finish{% |
| 3855 |
\mmz@biblatex@do@context |
| 3856 |
\mmz@biblatex@do@ccmemo |
| 3857 |
\expandafter\AdviceOriginal\the\collargsArgs |
| 3858 |
} |
| 3859 |
% \end{macro} |
| 3860 |
% |
| 3861 |
% \paragraph{Advise} the citation commands. |
| 3862 |
\mmzset{ |
| 3863 |
auto=\cite{cite}, |
| 3864 |
auto=\Cite{cite}, |
| 3865 |
auto=\parencite{cite}, |
| 3866 |
auto=\Parencite{cite}, |
| 3867 |
auto=\footcite{cite}, |
| 3868 |
auto=\footcitetext{cite}, |
| 3869 |
auto=\textcite{cite}, |
| 3870 |
auto=\Textcite{cite}, |
| 3871 |
auto=\smartcite{cite}, |
| 3872 |
auto=\Smartcite{cite}, |
| 3873 |
auto=\supercite{cite}, |
| 3874 |
auto=\cites{cites}, |
| 3875 |
auto=\Cites{cites}, |
| 3876 |
auto=\parencites{cites}, |
| 3877 |
auto=\Parencites{cites}, |
| 3878 |
auto=\footcites{cites}, |
| 3879 |
auto=\footcitetexts{cites}, |
| 3880 |
auto=\smartcites{cites}, |
| 3881 |
auto=\Smartcites{cites}, |
| 3882 |
auto=\textcites{cites}, |
| 3883 |
auto=\Textcites{cites}, |
| 3884 |
auto=\supercites{cites}, |
| 3885 |
auto=\autocite{cite}, |
| 3886 |
auto=\Autocite{cite}, |
| 3887 |
auto=\autocites{cites}, |
| 3888 |
auto=\Autocites{cites}, |
| 3889 |
auto=\citeauthor{cite}, |
| 3890 |
auto=\Citeauthor{cite}, |
| 3891 |
auto=\citetitle{cite}, |
| 3892 |
auto=\citeyear{cite}, |
| 3893 |
auto=\citedate{cite}, |
| 3894 |
auto=\citeurl{cite}, |
| 3895 |
auto=\nocite{cite}, |
| 3896 |
auto=\fullcite{cite}, |
| 3897 |
auto=\footfullcite{cite}, |
| 3898 |
auto=\volcite{volcite}, |
| 3899 |
auto=\Volcite{volcite}, |
| 3900 |
auto=\volcites{volcites}, |
| 3901 |
auto=\Volcites{volcites}, |
| 3902 |
auto=\pvolcite{volcite}, |
| 3903 |
auto=\Pvolcite{volcite}, |
| 3904 |
auto=\pvolcites{volcites}, |
| 3905 |
auto=\Pvolcites{volcites}, |
| 3906 |
auto=\fvolcite{volcite}, |
| 3907 |
auto=\Fvolcite{volcite}, |
| 3908 |
auto=\fvolcites{volcites}, |
| 3909 |
auto=\Fvolcites{volcites}, |
| 3910 |
auto=\ftvolcite{volcite}, |
| 3911 |
auto=\ftvolcites{volcites}, |
| 3912 |
auto=\Ftvolcite{volcite}, |
| 3913 |
auto=\Ftvolcites{volcites}, |
| 3914 |
auto=\svolcite{volcite}, |
| 3915 |
auto=\Svolcite{volcite}, |
| 3916 |
auto=\svolcites{volcites}, |
| 3917 |
auto=\Svolcites{volcites}, |
| 3918 |
auto=\tvolcite{volcite}, |
| 3919 |
auto=\Tvolcite{volcite}, |
| 3920 |
auto=\tvolcites{volcites}, |
| 3921 |
auto=\Tvolcites{volcites}, |
| 3922 |
auto=\avolcite{volcite}, |
| 3923 |
auto=\Avolcite{volcite}, |
| 3924 |
auto=\avolcites{volcites}, |
| 3925 |
auto=\Avolcites{volcites}, |
| 3926 |
auto=\notecite{cite}, |
| 3927 |
auto=\Notecite{cite}, |
| 3928 |
auto=\pnotecite{cite}, |
| 3929 |
auto=\Pnotecite{cite}, |
| 3930 |
auto=\fnotecite{cite}, |
| 3931 |
% Similar to |volcite|, these commands must be handled specifically in order |
| 3932 |
% to function correctly with |biblatex ccmemo cite=replicate|. |
| 3933 |
auto=\citename{cite, args=l*mlm}, |
| 3934 |
auto=\citelist{cite, args=l*mlm}, |
| 3935 |
auto=\citefield{cite, args=l*mlm}, |
| 3936 |
} |
| 3937 |
%</biblatex> |
| 3938 |
%<*mmz> |
| 3939 |
% |
| 3940 |
% |
| 3941 |
% \section{Initialization} |
| 3942 |
% \label{sec:code:initialization} |
| 3943 |
% |
| 3944 |
% \begin{key}{begindocument/before, begindocument, begindocument/end, |
| 3945 |
% enddocument/afterlastpage} |
| 3946 |
% These styles contain the initialization |
| 3947 |
% and the finalization code. They were populated throughout the source. |
| 3948 |
% Hook |begindocument/before| contains the package support code, which must |
| 3949 |
% be loaded while still in the preamble. Hook |begindocument| contains the |
| 3950 |
% initialization code whose execution doesn't require any particular timing, |
| 3951 |
% as long as it happens at the beginning of the document. Hook |
| 3952 |
% |begindocument/end| is where the commands are activated; this must |
| 3953 |
% crucially happen as late as possible, so that we successfully override |
| 3954 |
% foreign commands (like |hyperref|'s definitions). In \hologo{LaTeX}, we |
| 3955 |
% can automatically execute these hooks at appropriate places: |
| 3956 |
%<*latex> |
| 3957 |
\AddToHook{begindocument/before}{\mmzset{begindocument/before}} |
| 3958 |
\AddToHook{begindocument}{\mmzset{begindocument}} |
| 3959 |
\AddToHook{begindocument/end}{\mmzset{begindocument/end}} |
| 3960 |
\AddToHook{enddocument/afterlastpage}{\mmzset{enddocument/afterlastpage}} |
| 3961 |
%</latex> |
| 3962 |
% In \hologo{plainTeX}, the user must execute these hooks manually; but at |
| 3963 |
% least we can group them together and given them nice names. Provisionally, |
| 3964 |
% manual execution is required in \hologo{ConTeXt} as well, as I'm not sure |
| 3965 |
% where to execute them --- please help! |
| 3966 |
%<*plain,context> |
| 3967 |
\mmzset{ |
| 3968 |
begin document/.style={begindocument/before, begindocument, begindocument/end}, |
| 3969 |
end document/.style={enddocument/afterlastpage}, |
| 3970 |
} |
| 3971 |
%</plain,context> |
| 3972 |
% We clear the hooks after executing the last of them. |
| 3973 |
\mmzset{ |
| 3974 |
begindocument/end/.append style={ |
| 3975 |
begindocument/before/.code={}, |
| 3976 |
begindocument/.code={}, |
| 3977 |
begindocument/end/.code={}, |
| 3978 |
} |
| 3979 |
} |
| 3980 |
% \end{key} |
| 3981 |
% |
| 3982 |
% Formats other than \hologo{plainTeX} need a way to prevent extraction during |
| 3983 |
% package-loading. |
| 3984 |
%<!plain>\mmzset{extract/no/.code={}} |
| 3985 |
% |
| 3986 |
% \paragraph{\texttt{memoize.cfg}} Load the configuration file. Note that |
| 3987 |
% |nomemoize| must input this file as well, because any special |
| 3988 |
% memoization-related macros defined by the user should be available; for |
| 3989 |
% example, my |memoize.cfg| defines |\ifregion| (see |
| 3990 |
% \MS\ref{sec:tut:multifile}). |
| 3991 |
%</mmz> |
| 3992 |
%<mmz,nommz>\InputIfFileExists{memoize.cfg}{}{} |
| 3993 |
%<*mmz> |
| 3994 |
% |
| 3995 |
% For formats other than \hologo{plainTeX}, we also save the current (initial |
| 3996 |
% or |memoize.cfg|-set) value of |extract|, so that we can restore it when |
| 3997 |
% package options include |extract=no|. Then, |extract| can be called without |
| 3998 |
% an argument in the preamble, triggering extraction using this method; this is |
| 3999 |
% useful e.g.\ if Memoize is compiled into a format. |
| 4000 |
%<!plain>\let\mmz@initial@extraction@method\mmz@extraction@method |
| 4001 |
% |
| 4002 |
% \paragraph{Process} the package options (except in \hologo{plainTeX}). |
| 4003 |
%<*latex> |
| 4004 |
\DeclareUnknownKeyHandler[mmz]{% |
| 4005 |
\expanded{\noexpand\pgfqkeys{/mmz}{#1\IfBlankF{#2}{={#2}}}}} |
| 4006 |
\ProcessKeyOptions[mmz] |
| 4007 |
%</latex> |
| 4008 |
%<context>\expandafter\mmzset\expandafter{\currentmoduleparameters} |
| 4009 |
% |
| 4010 |
% In \hologo{LaTeX}, |nomemoize| has to process package options as well, |
| 4011 |
% otherwise \hologo{LaTeX} will complain. |
| 4012 |
%</mmz> |
| 4013 |
%<*latex&nommz> |
| 4014 |
\DeclareUnknownKeyHandler[mmz]{} |
| 4015 |
\ProcessKeyOptions[mmz] |
| 4016 |
%</latex&nommz> |
| 4017 |
% \paragraph{Extern extraction} |
| 4018 |
% We redefine |extract| to immediately trigger extraction. This is crucial in |
| 4019 |
% \hologo{plainTeX}, where extraction must be invoked after loading the |
| 4020 |
% package, but also potentially useful in other formats when package options |
| 4021 |
% include |extract=no|. |
| 4022 |
%<*mmz> |
| 4023 |
\mmzset{ |
| 4024 |
extract/.is choice, |
| 4025 |
extract/.default=\mmz@extraction@method, |
| 4026 |
% But only once: |
| 4027 |
extract/.append style={ |
| 4028 |
extract/.code={\PackageError{memoize}{Key "extract" was invoked twice.}{In |
| 4029 |
principle, externs should be extracted only once. If you really want |
| 4030 |
to extract again, execute "extract/<method>".}}, |
| 4031 |
}, |
| 4032 |
% In formats other than \hologo{plainTeX}, we remember the current |extract| |
| 4033 |
% code and then trigger the extraction. |
| 4034 |
%<!plain>/utils/exec={\pgfkeysgetvalue{/mmz/extract/.@cmd}\mmz@temp@extract}, |
| 4035 |
%<!plain>extract=\mmz@extraction@method, |
| 4036 |
} |
| 4037 |
% Option |extract=no| (which only exists in formats other than |
| 4038 |
% \hologo{plainTeX}) should allow for an explicit invocation of |extract| in |
| 4039 |
% the preamble. |
| 4040 |
%<*!plain> |
| 4041 |
\def\mmz@temp{no} |
| 4042 |
\ifx\mmz@extraction@method\mmz@temp |
| 4043 |
\pgfkeyslet{/mmz/extract/.@cmd}\mmz@temp@extract |
| 4044 |
\let\mmz@extraction@method\mmz@initial@extraction@method |
| 4045 |
\fi |
| 4046 |
\let\mmz@temp@extract\relax |
| 4047 |
%</!plain> |
| 4048 |
% |
| 4049 |
% Memoize was not really born for the draft mode, as it cannot produce new |
| 4050 |
% externs there. But we don't want to disable the package, as utilization and |
| 4051 |
% pure memoization are still perfectly valid in this mode, so let's just warn |
| 4052 |
% the user. |
| 4053 |
\ifnum\pdf@draftmode=1 |
| 4054 |
\PackageWarning{memoize}{No externalization will be performed in the draft mode}% |
| 4055 |
\fi |
| 4056 |
%</mmz> |
| 4057 |
% |
| 4058 |
% Several further things which need to be defined as dummies in |
| 4059 |
% |nomemoize|\slash|memoizable|. |
| 4060 |
%<*nommz,mmzable&generic> |
| 4061 |
\pgfkeys{% |
| 4062 |
/handlers/.meaning to context/.code={}, |
| 4063 |
/handlers/.value to context/.code={}, |
| 4064 |
} |
| 4065 |
\let\mmzAbort\relax |
| 4066 |
\let\mmzUnmemoizable\relax |
| 4067 |
\newcommand\IfMemoizing[2][]{\@secondoftwo} |
| 4068 |
\let\mmzNoRef\@gobble |
| 4069 |
\let\mmzForceNoRef\@gobble |
| 4070 |
\newtoks\mmzContext |
| 4071 |
\newtoks\mmzContextExtra |
| 4072 |
\newtoks\mmzCMemo |
| 4073 |
\newtoks\mmzCCMemo |
| 4074 |
\newcount\mmzExternPages |
| 4075 |
\newcount\mmzExtraPages |
| 4076 |
\let\mmzTracingOn\relax |
| 4077 |
\let\mmzTracingOff\relax |
| 4078 |
%</nommz,mmzable&generic> |
| 4079 |
% |
| 4080 |
% \paragraph{The end} of |memoize|, |nomemoize| and |memoizable|. |
| 4081 |
%<*mmz,nommz,mmzable> |
| 4082 |
%<plain>\resetatcatcode |
| 4083 |
%<context>\stopmodule |
| 4084 |
%<context>\protect |
| 4085 |
%</mmz,nommz,mmzable> |
| 4086 |
% That's all, folks! |
| 4087 |
% |
| 4088 |
% \end{macrocode} |
| 4089 |
% |
| 4090 |
% \endinput |
| 4091 |
% |
| 4092 |
% Local Variables: |
| 4093 |
% TeX-engine: luatex |
| 4094 |
% TeX-master: "doc/memoize-code.tex" |
| 4095 |
% TeX-auto-save: nil |
| 4096 |
% End: |