| 1 |
% \iffalse |
| 2 |
% |
| 3 |
% advice.edtx (this is not a .dtx file; to produce a .dtx, process it with edtx2dtx) |
| 4 |
% |
| 5 |
%% This file is a part of Advice, a TeX package implementing a generic |
| 6 |
%% framework for extending the functionality of selected commands and |
| 7 |
%% environments, available at https://ctan.org/pkg/advice and |
| 8 |
%% https://github.com/sasozivanovic/advice. |
| 9 |
%% |
| 10 |
%% Copyright (c) 2023- Saso Zivanovic <[email protected]> |
| 11 |
%% (Sa\v{s}o \v{Z}ivanovi\'{c}) |
| 12 |
%% |
| 13 |
%% This work may be distributed and/or modified under the conditions of the |
| 14 |
%% LaTeX Project Public License, either version 1.3c of this license or (at |
| 15 |
%% your option) any later version. The latest version of this license is in |
| 16 |
%% https://www.latex-project.org/lppl.txt and version 1.3c or later is part of |
| 17 |
%% all distributions of LaTeX version 2008 or later. |
| 18 |
%% |
| 19 |
%% This work has the LPPL maintenance status `maintained'. |
| 20 |
%% The Current Maintainer of this work is Saso Zivanovic. |
| 21 |
%% |
| 22 |
%% The files belonging to this work and covered by LPPL are listed in |
| 23 |
%% (<texmf>/doc/generic/advice/)FILES. |
| 24 |
% |
| 25 |
% \fi |
| 26 |
% |
| 27 |
% \begin{macrocode} |
| 28 |
% |
| 29 |
% \relax |
| 30 |
% |
| 31 |
%<*main> |
| 32 |
%<latex>\ProvidesPackage{advice}[2024/03/15 v1.1.1 Extend commands and environments] |
| 33 |
%<context>%D \module[ |
| 34 |
%<context>%D file=t-advice.tex, |
| 35 |
%<context>%D version=1.1.1, |
| 36 |
%<context>%D title=Advice, |
| 37 |
%<context>%D subtitle=Extend commands and environments, |
| 38 |
%<context>%D author=Saso Zivanovic, |
| 39 |
%<context>%D date=2024-03-15, |
| 40 |
%<context>%D copyright=Saso Zivanovic, |
| 41 |
%<context>%D license=LPPL, |
| 42 |
%<context>%D ] |
| 43 |
%<context>\writestatus{loading}{ConTeXt User Module / advice} |
| 44 |
%<context>\unprotect |
| 45 |
%<context>\startmodule[advice] |
| 46 |
% \paragraph{Required packages} |
| 47 |
%<plain,context>\input miniltx |
| 48 |
%<latex>\RequirePackage{collargs} |
| 49 |
%<plain>\input collargs |
| 50 |
%<context>\input t-collargs |
| 51 |
% In \hologo{LaTeX}, we also require |xparse|. Even though |
| 52 |
% |\NewDocumentCommand| and friends are integrated into the \hologo{LaTeX} |
| 53 |
% kernel, |\GetDocumentCommandArgSpec| is only available through |xparse|. |
| 54 |
%<latex>\RequirePackage{xparse} |
| 55 |
% |
| 56 |
% \subsubsection{Installation into a keypath} |
| 57 |
% |
| 58 |
% \begin{handlerskey}{.install advice} |
| 59 |
% This handler installs the advising mechanism into the handled path, which |
| 60 |
% we shall henceforth also call the (advice) namespace. |
| 61 |
\pgfkeys{ |
| 62 |
/handlers/.install advice/.code={% |
| 63 |
\edef\auto@install@namespace{\pgfkeyscurrentpath}% |
| 64 |
\def\advice@install@setupkey{advice}% |
| 65 |
\def\advice@install@activation{immediate}% |
| 66 |
\pgfqkeys{/advice/install}{#1}% |
| 67 |
\expanded{\noexpand\advice@install |
| 68 |
{\auto@install@namespace}% |
| 69 |
{\advice@install@setupkey}% |
| 70 |
{\advice@install@activation}% |
| 71 |
}% |
| 72 |
}, |
| 73 |
% \end{handlerskey} |
| 74 |
% |
| 75 |
% \begin{adviceinstallkey}{setup key,activation} |
| 76 |
% These keys can be used in the argument of |.install advice| to configure |
| 77 |
% the installation. By default, the setup key is |advice| and |activation| is |
| 78 |
% |immediate|. |
| 79 |
/advice/install/.cd, |
| 80 |
setup key/.store in=\advice@install@setupkey, |
| 81 |
activation/.is choice, |
| 82 |
activation/.append code=\def\advice@install@activation{#1}, |
| 83 |
activation/immediate/.code={}, |
| 84 |
activation/deferred/.code={}, |
| 85 |
} |
| 86 |
% \end{adviceinstallkey} |
| 87 |
% |
| 88 |
% |#1| is the installation keypath (in Memoize, |/mmz|); |#2| is the setup key |
| 89 |
% name (in Memoize, |auto|, and this is why we document it as such); |#3| is |
| 90 |
% the initial activation regime. |
| 91 |
\def\advice@install#1#2#3{% |
| 92 |
% Switch to the installation keypath. |
| 93 |
\pgfqkeys{#1}{% |
| 94 |
% \begin{key}{auto, auto csname, auto key, auto', auto csname', auto key'} |
| 95 |
% These keys submit a command or environment to advising. The |
| 96 |
% namespace is hard-coded into these keys via |#1|; their arguments are |
| 97 |
% the command/environment (cs)name, and setup keys belonging to path |
| 98 |
% \meta{installation keypath}|/\meta{setup key name}|. |
| 99 |
% \indentmacrocode |
| 100 |
#2/.code 2 args={% |
| 101 |
% \noindentmacrocode |
| 102 |
% Call the internal setup macro, wrapping the received keylist into a |
| 103 |
% |pgfkeys| invocation. |
| 104 |
\AdviceSetup{#1}{#2}{##1}{\pgfqkeys{#1/#2}{##2}}% |
| 105 |
% Activate if not already activated (this can happen when updating the |
| 106 |
% configuration). Note we don't call |\advice@activate| directly, but use |
| 107 |
% the public keys; in this way, activation is automatically deferred if |
| 108 |
% so requested. (We don't use |\pgfkeysalso| to allow |auto| being called |
| 109 |
% from any path.) |
| 110 |
\pgfqkeys{#1}{try activate, activate={##1}}% |
| 111 |
}, |
| 112 |
% A variant without activation. |
| 113 |
#2'/.code 2 args={% |
| 114 |
\AdviceSetup{#1}{#2}{##1}{\pgfqkeys{#1/#2}{##2}}% |
| 115 |
}, |
| 116 |
#2 csname/.style 2 args={ |
| 117 |
#2/.expand once=\expandafter{\csname ##1\endcsname}{##2}, |
| 118 |
}, |
| 119 |
#2 csname'/.style 2 args={ |
| 120 |
#2'/.expand once=\expandafter{\csname ##1\endcsname}{##2}, |
| 121 |
}, |
| 122 |
#2 key/.style 2 args={ |
| 123 |
#2/.expand once=% |
| 124 |
\expandafter{\csname pgfk@##1/.@cmd\endcsname}% |
| 125 |
{collector=\advice@pgfkeys@collector,##2}, |
| 126 |
}, |
| 127 |
#2 key'/.style 2 args={ |
| 128 |
#2'/.expand once=% |
| 129 |
\expandafter{\csname pgfk@##1/.@cmd\endcsname}% |
| 130 |
{collector=\advice@pgfkeys@collector,##2}, |
| 131 |
}, |
| 132 |
% \end{key} |
| 133 |
% |
| 134 |
% \begin{key}{activation} |
| 135 |
% This key, residing in the installation keypath, forwards the request to |
| 136 |
% the |/advice| path |activation| subkeys, which define |activate| and |
| 137 |
% friends in the installation keypath. Initially, the activation regime |
| 138 |
% is whatever the user has requested using the |.install advice| argument |
| 139 |
% (here |#3|). |
| 140 |
activation/.style={/advice/activation/##1={#1}}, |
| 141 |
activation=#3, |
| 142 |
% \end{key} |
| 143 |
% |
| 144 |
% \begin{key}{activate deferred} |
| 145 |
% The deferred activations are collected in this style, see |
| 146 |
% section~ref{sec:code:advice:activation} for details. |
| 147 |
activate deferred/.code={}, |
| 148 |
% \end{key} |
| 149 |
% |
| 150 |
% \begin{key}{activate csname, deactivate csname} |
| 151 |
% For simplicity of implementation, the |csname| versions of |activate| |
| 152 |
% and |deactivate| accept a single \meta{csname}. This way, they can be |
| 153 |
% defined right away, as they don't change with the type of activation |
| 154 |
% (immediate vs.\ deferred). |
| 155 |
activate csname/.style={activate/.expand once={\csname##1\endcsname}}, |
| 156 |
deactivate csname/.style={deactivate/.expand once={\csname##1\endcsname}}, |
| 157 |
% \end{key} |
| 158 |
% |
| 159 |
% \begin{key}{activate key, deactivate key} |
| 160 |
% (De)activation of |pgfkeys| keys. Accepts a list of key names, requires |
| 161 |
% full key names. |
| 162 |
activate key/.style={activate@key={#1/activate}{##1}}, |
| 163 |
deactivate key/.style={activate@key={#1/deactivate}{##1}}, |
| 164 |
activate@key/.code n args=2{% |
| 165 |
\def\advice@temp{}% |
| 166 |
\def\advice@do####1{% |
| 167 |
\eappto\advice@temp{,\expandonce{\csname pgfk@####1/.@cmd\endcsname}}}% |
| 168 |
\forcsvlist\advice@do{##2}% |
| 169 |
\pgfkeysalso{##1/.expand once=\advice@temp}% |
| 170 |
}, |
| 171 |
% \end{key} |
| 172 |
% |
| 173 |
% The rest of the keys defined below reside in the |auto| subfolder of the |
| 174 |
% installation keypath. |
| 175 |
#2/.cd, |
| 176 |
% |
| 177 |
% \begin{mmzautokey}{run conditions, outer handler, bailout handler, collector, |
| 178 |
% args, collector options, clear collector options, raw collector options, |
| 179 |
% clear raw collector options, inner handler, options, clear options} |
| 180 |
% These keys are used to setup the handling of the command or |
| 181 |
% environment. The storage macros (|\AdviceRunConditions| etc.) have |
| 182 |
% public names as they also play a crucial role in the handler |
| 183 |
% definitions, see section~\ref{sec:code:advice:handle}. |
| 184 |
% \indentmacrocode[-0.5em] |
| 185 |
run conditions/.store in=\AdviceRunConditions, |
| 186 |
bailout handler/.store in=\AdviceBailoutHandler, |
| 187 |
outer handler/.store in=\AdviceOuterHandler, |
| 188 |
collector/.store in=\AdviceCollector, |
| 189 |
collector options/.code={\appto\AdviceCollectorOptions{,##1}}, |
| 190 |
clear collector options/.code={\def\AdviceCollectorOptions{}}, |
| 191 |
raw collector options/.code={\appto\AdviceRawCollectorOptions{##1}}, |
| 192 |
clear raw collector options/.code={\def\AdviceRawCollectorOptions{}}, |
| 193 |
args/.store in=\AdviceArgs, |
| 194 |
inner handler/.store in=\AdviceInnerHandler, |
| 195 |
options/.code={\appto\AdviceOptions{,##1}}, |
| 196 |
clear options/.code={\def\AdviceOptions{}}, |
| 197 |
% \noindentmacrocode |
| 198 |
% A user-friendly way to set |options|: any unknown key is an option. |
| 199 |
.unknown/.code={% |
| 200 |
\eappto{\AdviceOptions}{,\pgfkeyscurrentname={\unexpanded{##1}}}% |
| 201 |
}, |
| 202 |
% The default values of the keys, which equal the initial values for |
| 203 |
% commands, as assigned by |\advice@setup@init@command|. |
| 204 |
run conditions/.default=\AdviceRuntrue, |
| 205 |
bailout handler/.default=\relax, |
| 206 |
outer handler/.default=\AdviceCollector, |
| 207 |
collector/.default=\advice@CollectArgumentsRaw, |
| 208 |
collector options/.value required, |
| 209 |
raw collector options/.value required, |
| 210 |
args/.default=\advice@noargs, |
| 211 |
inner handler/.default=\advice@error@noinnerhandler, |
| 212 |
options/.value required, |
| 213 |
% \end{mmzautokey} |
| 214 |
% |
| 215 |
% \begin{mmzautokey}{reset} |
| 216 |
% This key resets the advice settings to their initial values, which |
| 217 |
% depend on whether we're handling a command or environment. |
| 218 |
reset/.code={\csname\advice@setup@init@\AdviceType\endcsname}, |
| 219 |
% \end{mmzautokey} |
| 220 |
% |
| 221 |
% \begin{mmzautokey}{after setup} |
| 222 |
% The code given here will be executed once we exit the setup group. |
| 223 |
% |integrated driver| of Memoize uses it to declare a conditional. |
| 224 |
after setup/.code={\appto\AdviceAfterSetup{##1}}, |
| 225 |
% \end{mmzautokey} |
| 226 |
% |
| 227 |
% In \hologo{LaTeX}, we finish the installation by submitting |\begin|; the |
| 228 |
% submission is funky, because the run conditions handler actually hacks |
| 229 |
% the standard handling procedure. Note that if |\begin| is not |
| 230 |
% activated, environments will not be handled, and that the automatic |
| 231 |
% activation might be deffered. |
| 232 |
%<latex>#1/#2=\begin{run conditions=\advice@begin@rc}, |
| 233 |
}% |
| 234 |
} |
| 235 |
% |
| 236 |
% |
| 237 |
% \subsubsection{Submitting a command or environment} |
| 238 |
% \label{sec:code:advice:setup} |
| 239 |
% |
| 240 |
% \begin{macro}{\AdviceSetup,\AdviceName,\AdviceType} |
| 241 |
% Macro |\advice@setup| is called by key |auto| to submit a command or |
| 242 |
% environment to advising. It receives four arguments: |#1| is the |
| 243 |
% installation keypath / storage namespace: |#2| is the name of the setup |
| 244 |
% key; |#3| is the submitted command or environment; |#4| is the setup code |
| 245 |
% (which is only grabbed by |\advice@setup@i|). |
| 246 |
% |
| 247 |
% Executing this macro defines macros |\AdviceName|, holding the control |
| 248 |
% sequence of the submitted command or the environment name, and |\AdviceType|, |
| 249 |
% holding |command| or |environment|; they are used to set up some initial |
| 250 |
% values, and may be used by user-defined keys in the |auto| path, as well |
| 251 |
% (see |/mmz/auto/noop| for an example). The macro then performs internal |
| 252 |
% initialization, and finally calls the second part, |\advice@setup@i|, with |
| 253 |
% the command's \emph{storage} name as the first argument. |
| 254 |
% |
| 255 |
% This macro also serves as the programmer's interface to |auto|, the idea |
| 256 |
% being that an advanced user may write code |#4| which defined the settings |
| 257 |
% macros (|\AdviceOuterHandler| etc.) without deploying |pgfkeys|. (Also note |
| 258 |
% that activation at the end only occurs through the |auto| interface.) |
| 259 |
\def\AdviceSetup#1#2#3{% |
| 260 |
% Open a group, so that we allow for embedded |auto| invocations. |
| 261 |
\begingroup |
| 262 |
\def\AdviceName{#3}% |
| 263 |
\advice@def@AdviceCsname |
| 264 |
% Command, complain, or environment? |
| 265 |
\collargs@cs@cases{#3}{% |
| 266 |
\def\AdviceType{command}% |
| 267 |
\advice@setup@init@command |
| 268 |
\advice@setup@i{#3}{#1}{#3}% |
| 269 |
}{% |
| 270 |
\advice@error@advice@notcs{#1/#2}{#3}% |
| 271 |
}{% |
| 272 |
\def\AdviceType{environment}% |
| 273 |
\advice@setup@init@environment |
| 274 |
%<latex>\advice@setup@i{#3}% |
| 275 |
%<plain>\expandafter\advice@setup@i\expandafter{\csname #3\endcsname}% |
| 276 |
%<context>\expandafter\advice@setup@i\expandafter{\csname start#3\endcsname}% |
| 277 |
{#1}{#3}% |
| 278 |
}% |
| 279 |
} |
| 280 |
% The arguments of |\advice@setup@i| are a bit different than for |
| 281 |
% |\advice@setup|, because we have inserted the storage name as |#1| above, and |
| 282 |
% we lost the setup key name |#2|. Here, |#2| is the installation keypath / |
| 283 |
% storage namespace, |#3| is the submitted command or environment; and |#4| is |
| 284 |
% the setup code. |
| 285 |
% |
| 286 |
% What is the difference between the storage name (|#1|) and the command\slash |
| 287 |
% environment name (|#3|, and also the contents of |\AdviceName|), and why do we |
| 288 |
% need both? For commands, there is actually no difference; for example, when |
| 289 |
% submitting command |\foo|, we end up with |#1|=|#3|=|\foo|. And there is |
| 290 |
% also no difference for \hologo{LaTeX} environments; when submitting |
| 291 |
% environment |foo|, we get |#1|=|#3|=|foo|. But in \hologo{plainTeX}, |
| 292 |
% |#1|=|\foo| and |#3|=|foo|, and in \hologo{ConTeXt}, |#1|=|\startfoo| and |
| 293 |
% |#3|=|foo| --- which should explain the guards and |\expandafter|s above. |
| 294 |
% |
| 295 |
% And why both |#1| and |#3|? When a handled command is executed, it loads its |
| 296 |
% configuration from a macro determined by the storage namespace and the |
| 297 |
% (|\string|ified) storage name, e.g.\ |/mmz| and |\foo|. In \hologo{plainTeX} |
| 298 |
% and \hologo{ConTeXt}, each environment is started by a dedicated command, |
| 299 |
% |\foo| or |\startfoo|, so these control sequences (|\string|ified) must act |
| 300 |
% as storage names. (Not so in \hologo{LaTeX}, where an environment |
| 301 |
% configuration is loaded by |\begin|'s handler, which can easily work with |
| 302 |
% storage name |foo|. Even more, having |\foo| as an environment storage |
| 303 |
% name would conflict with the storage name for the (environment-internal) |
| 304 |
% command |\foo| --- yes, we can submit either |foo| or |\foo|, or both, to |
| 305 |
% advising.) |
| 306 |
\def\advice@setup@i#1#2#3#4{% |
| 307 |
% Load the current configuration of the handled command or environment --- if |
| 308 |
% it exists. |
| 309 |
\advice@setup@init@i{#2}{#1}% |
| 310 |
\advice@setup@init@I{#2}{#1}% |
| 311 |
\def\AdviceAfterSetup{}% |
| 312 |
% Apply the setup code/keys. |
| 313 |
#4% |
| 314 |
% Save the resulting configuration. This closes the group, because the config |
| 315 |
% is saved outside it. |
| 316 |
\advice@setup@save{#2}{#1}% |
| 317 |
} |
| 318 |
% \end{macro} |
| 319 |
% |
| 320 |
% \paragraph{Initialize} the configuration of a command or environment. Note |
| 321 |
% that the default values of the keys equal the initial values for commands. |
| 322 |
% Nothing would go wrong if these were not the same, but it's nice that the |
| 323 |
% end-user can easily revert to the initial values. |
| 324 |
\def\advice@setup@init@common{% |
| 325 |
\def\AdviceRunConditions{\AdviceRuntrue}% |
| 326 |
\def\AdviceBailoutHandler{\relax}% |
| 327 |
\def\AdviceOuterHandler{\AdviceCollector}% |
| 328 |
\def\AdviceCollector{\advice@CollectArgumentsRaw}% |
| 329 |
\def\AdviceCollectorOptions{}% |
| 330 |
\def\AdviceInnerHandler{\advice@error@noinnerhandler}% |
| 331 |
\def\AdviceOptions{}% |
| 332 |
} |
| 333 |
\def\advice@setup@init@command{% |
| 334 |
\advice@setup@init@common |
| 335 |
\def\AdviceRawCollectorOptions{}% |
| 336 |
\def\AdviceArgs{\advice@noargs}% |
| 337 |
} |
| 338 |
\def\advice@setup@init@environment{% |
| 339 |
\advice@setup@init@common |
| 340 |
\edef\AdviceRawCollectorOptions{% |
| 341 |
\noexpand\collargsEnvironment{\AdviceName}% |
| 342 |
% When grabbing an environment body, the end-tag will be included. This |
| 343 |
% makes it possible to have the same inner handler for commands and |
| 344 |
% environments. |
| 345 |
\noexpand\collargsEndTagtrue |
| 346 |
}% |
| 347 |
\def\AdviceArgs{+b}% |
| 348 |
} |
| 349 |
% We need to initialize |\AdviceOuterHandler| etc.\ so that |\advice@setup@store| |
| 350 |
% will work. |
| 351 |
\advice@setup@init@command |
| 352 |
% |
| 353 |
% \paragraph.{The configuration storage} |
| 354 |
% |
| 355 |
% The remaining macros in this subsection deal with the configuration storage |
| 356 |
% space, which is set up in a way to facilitate fast loading during the |
| 357 |
% execution of handled commands and environments. |
| 358 |
% |
| 359 |
% The configuration of a command or environment is stored in two parts: the |
| 360 |
% first stage settings comprise the run conditions, the bailout handler and the |
| 361 |
% outer handler; the second stage settings contain the rest. When a handled |
| 362 |
% command is invoked, only the first stage settings are immediately loaded, for |
| 363 |
% speed; the second stage settings are only loaded if the run conditions are |
| 364 |
% satisfied. |
| 365 |
% |
| 366 |
% \begin{macro}{\advice@init@i,\advice@init@I} |
| 367 |
% The two-stage settings are stored in control sequences |
| 368 |
% |\advice@i|\meta{namespace}|//|\meta{storage name} and |
| 369 |
% |\advice@I|\meta{namespace}|//|\meta{storage name}, respectively, and |
| 370 |
% accessed using macros |\advice@init@i| and |\advice@init@I|. |
| 371 |
% |
| 372 |
% Each setting storage macro contains a sequence of items, where each item is |
| 373 |
% either of form |\def\AdviceSetting|\marg{value}. This allows us store |
| 374 |
% multiple settings in a single macro (rather than define each |
| 375 |
% control-sequence-valued setting separately, which would use more string |
| 376 |
% memory), and also has the consequence that we don't require the handlers to |
| 377 |
% be defined when submitting a command (whether that's good or bad could be |
| 378 |
% debated: as things stand, any typos in handler declarations will only yield |
| 379 |
% an error once the handled command is executed). |
| 380 |
\def\advice@init@i#1#2{\csname advice@i#1//\string#2\endcsname} |
| 381 |
\def\advice@init@I#1#2{\csname advice@I#1//\string#2\endcsname} |
| 382 |
% We make a copy of these for setup; the originals might be swapped for |
| 383 |
% tracing purposes. |
| 384 |
\let\advice@setup@init@i\advice@init@i |
| 385 |
\let\advice@setup@init@I\advice@init@I |
| 386 |
% \end{macro} |
| 387 |
% |
| 388 |
% \begin{macro}{\advice@setup@save} |
| 389 |
% To save the configuration at the end of the setup, we construct the storage |
| 390 |
% macros out of |\AdviceRunConditions| and friends. Stage-one contains only |
| 391 |
% |\AdviceRunConditions| and |\AdviceBailoutHandler|, so that |
| 392 |
% |\advice@handle| can bail out as quickly as possible if the run conditions |
| 393 |
% are not met. |
| 394 |
\def\advice@setup@save#1#2{% |
| 395 |
\expanded{% |
| 396 |
% Close the group before saving. Note that |\expanded| has already expanded |
| 397 |
% the settings macros. |
| 398 |
\endgroup |
| 399 |
\noexpand\csdef{advice@i#1//\string#2}{% |
| 400 |
\def\noexpand\AdviceRunConditions{\expandonce\AdviceRunConditions}% |
| 401 |
\def\noexpand\AdviceBailoutHandler{\expandonce\AdviceBailoutHandler}% |
| 402 |
}% |
| 403 |
\noexpand\csdef{advice@I#1//\string#2}{% |
| 404 |
\def\noexpand\AdviceOuterHandler{\expandonce\AdviceOuterHandler}% |
| 405 |
\def\noexpand\AdviceCollector{\expandonce\AdviceCollector}% |
| 406 |
\def\noexpand\AdviceRawCollectorOptions{% |
| 407 |
\expandonce\AdviceRawCollectorOptions}% |
| 408 |
\def\noexpand\AdviceCollectorOptions{\expandonce\AdviceCollectorOptions}% |
| 409 |
\def\noexpand\AdviceArgs{\expandonce\AdviceArgs}% |
| 410 |
\def\noexpand\AdviceInnerHandler{\expandonce\AdviceInnerHandler}% |
| 411 |
\def\noexpand\AdviceOptions{\expandonce\AdviceOptions}% |
| 412 |
}% |
| 413 |
\expandonce{\AdviceAfterSetup}% |
| 414 |
}% |
| 415 |
} |
| 416 |
% \end{macro} |
| 417 |
% \label{sec:code:advice:activation} |
| 418 |
% |
| 419 |
% \begin{advicekey}{activation/immediate, activation/deferred} |
| 420 |
% These two subkeys of |/advice/activation| install the immediate and the |
| 421 |
% deferred activation code into the installation keypath. They are invoked |
| 422 |
% by key \meta{installation keypath}|/activation=|\meta{type}. |
| 423 |
% |
| 424 |
% Under the deferred activation regime, the commands are not (de)activated |
| 425 |
% right away. Rather, the (de)activation calls are collected in style |
| 426 |
% |activate deferred|, which should be executed by the installation keypath |
| 427 |
% owner, if and when they so desire. (Be sure to switch to |
| 428 |
% |activation=immediate| before executing |activate deferred|, otherwise the |
| 429 |
% activation will only be deferred once again.) |
| 430 |
\pgfkeys{ |
| 431 |
/advice/activation/deferred/.style={ |
| 432 |
#1/activate/.style={% |
| 433 |
activate deferred/.append style={#1/activate={##1}}}, |
| 434 |
#1/deactivate/.style={% |
| 435 |
activate deferred/.append style={#1/deactivate={##1}}}, |
| 436 |
#1/force activate/.style={% |
| 437 |
activate deferred/.append style={#1/force activate={##1}}}, |
| 438 |
#1/try activate/.style={% |
| 439 |
activate deferred/.append style={#1/try activate={##1}}}, |
| 440 |
}, |
| 441 |
% \end{advicekey} |
| 442 |
% |
| 443 |
% \begin{key}{activate, deactivate, force activate, try activate} |
| 444 |
% The ``real,'' immediate |activate| and |deactivate| take a comma-separated |
| 445 |
% list of commands or environments and (de)activate them. If |try activate| |
| 446 |
% is in effect, no error is thrown upon failure. If |force activate| is in |
| 447 |
% effect, activation proceeds even if we already had the original definition; |
| 448 |
% it does not apply to deactivation. These conditionals are set to false |
| 449 |
% after every invocation of key |(de)activate|, so that they only apply to |
| 450 |
% the immediately following |(de)activate|. (|#1| below is the |
| 451 |
% \meta{namespace}; |##1| is the list of commands to be (de)activated.) |
| 452 |
/advice/activation/immediate/.style={ |
| 453 |
#1/activate/.code={% |
| 454 |
\forcsvlist{\advice@activate{#1}}{##1}% |
| 455 |
\advice@activate@forcefalse |
| 456 |
\advice@activate@tryfalse |
| 457 |
}, |
| 458 |
#1/deactivate/.code={% |
| 459 |
\forcsvlist{\advice@deactivate{#1}}{##1}% |
| 460 |
\advice@activate@forcefalse |
| 461 |
\advice@activate@tryfalse |
| 462 |
}, |
| 463 |
#1/force activate/.is if=advice@activate@force, |
| 464 |
#1/try activate/.is if=advice@activate@try, |
| 465 |
}, |
| 466 |
} |
| 467 |
\newif\ifadvice@activate@force |
| 468 |
\newif\ifadvice@activate@try |
| 469 |
% \end{key} |
| 470 |
% |
| 471 |
% \begin{macro}{\advice@original@csname,\advice@original@cs,\AdviceGetOriginal} |
| 472 |
% Activation replaces the original meaning of the handled command with our |
| 473 |
% definition. We store the original definition into control sequence |
| 474 |
% |\advice@o|\meta{namespace}|//|\meta{storage name} (with a |\string|ified |
| 475 |
% \meta{storage name}). Internally, during (de)activation and handling, we |
| 476 |
% access it using |\advice@original@csname| and |\advice@original@cs|. Publicly |
| 477 |
% it should always be accessed by |\AdviceGetOriginal|, which returns the |
| 478 |
% argument control sequence if that control sequence is not handled. |
| 479 |
% |
| 480 |
% Using the internal command outside the handling context, we could fall |
| 481 |
% victim to scenario such as the following. When we memoize something |
| 482 |
% containing a |\label|, the produced cc-memo contains code eventually |
| 483 |
% executing the original |\label|. If we called the original |\label| via |
| 484 |
% the internal macro there, and the user |deactivate|d |\label| on a |
| 485 |
% subsequent compilation, the cc-memo would not call |\label| anymore, but |
| 486 |
% |\relax|, resulting in a silent error. Using |\AdviceGetOriginal|, the |
| 487 |
% original |\label| will be executed even when not activated. |
| 488 |
% |
| 489 |
% However, not all is bright with |\AdviceGetOriginal|. Given an activated |
| 490 |
% control sequence (|#2|), a typo in the namespace argument (|#1|) will lead |
| 491 |
% to an infinite loop upon the execution of |\AdviceGetOriginal|. In the |
| 492 |
% manual, we recommend defining a namespace-specific macro to avoid such |
| 493 |
% typos. |
| 494 |
\def\advice@original@csname#1#2{advice@o#1//\string#2} |
| 495 |
\def\advice@original@cs#1#2{\csname advice@o#1//\string#2\endcsname} |
| 496 |
\def\AdviceGetOriginal#1#2{% |
| 497 |
\ifcsname advice@o#1//\string#2\endcsname |
| 498 |
\expandonce{\csname advice@o#1//\string#2\expandafter\endcsname\expandafter}% |
| 499 |
\else |
| 500 |
\unexpanded\expandafter{\expandafter#2\expandafter}% |
| 501 |
\fi |
| 502 |
} |
| 503 |
% \end{macro} |
| 504 |
% |
| 505 |
% \begin{macro}{\AdviceCsnameGetOriginal} |
| 506 |
% A version of |\AdviceGetOriginal| which accepts a control sequence name as |
| 507 |
% the second argument. |
| 508 |
\begingroup |
| 509 |
\catcode`\/=0 |
| 510 |
\catcode`\\=12 |
| 511 |
/gdef/advice@backslash@other{\}% |
| 512 |
/endgroup |
| 513 |
\def\AdviceCsnameGetOriginal#1#2{% |
| 514 |
\ifcsname advice@o#1//\advice@backslash@other#2\endcsname |
| 515 |
\expandonce{\csname advice@o#1//\advice@backslash@other#2\expandafter\endcsname |
| 516 |
\expandafter}% |
| 517 |
\else |
| 518 |
\expandonce{\csname#2\expandafter\endcsname\expandafter}% |
| 519 |
\fi |
| 520 |
} |
| 521 |
% \end{macro} |
| 522 |
% |
| 523 |
% \begin{macro}{\advice@activate,\advice@deactivate} |
| 524 |
% These macros execute either the command, or the environment (de)activator. |
| 525 |
\def\advice@activate#1#2{% |
| 526 |
\collargs@cs@cases{#2}% |
| 527 |
{\advice@activate@cmd{#1}{#2}}% |
| 528 |
{\advice@error@activate@notcsorenv{}{#1}}% |
| 529 |
{\advice@activate@env{#1}{#2}}% |
| 530 |
} |
| 531 |
\def\advice@deactivate#1#2{% |
| 532 |
\collargs@cs@cases{#2}% |
| 533 |
{\advice@deactivate@cmd{#1}{#2}}% |
| 534 |
{\advice@error@activate@notcsorenv{de}{#1}}% |
| 535 |
{\advice@deactivate@env{#1}{#2}}% |
| 536 |
} |
| 537 |
% \end{macro} |
| 538 |
% |
| 539 |
% \begin{macro}{\advice@activate@cmd} |
| 540 |
% We are very careful when we're activating a command, because activating |
| 541 |
% means rewriting its original definition. Configuration by |auto| did not |
| 542 |
% touch the original command; activation will. So, the leitmotif of this |
| 543 |
% macro: safety first. (|#1| is the namespace, and |#2| is the command to be |
| 544 |
% activated.) |
| 545 |
\def\advice@activate@cmd#1#2{% |
| 546 |
% Is the command defined? |
| 547 |
\ifdef{#2}{% |
| 548 |
% Yes, the command is defined. Let's see if it's safe to activate it. |
| 549 |
% We'll do this by checking whether we have its original definition in our |
| 550 |
% storage. If we do, this means that we have already activated the |
| 551 |
% command. Activating it twice would lead to the loss of the original |
| 552 |
% definition (because the second activation would store our own |
| 553 |
% redefinition as the original definition) and consequently an infinite |
| 554 |
% loop (because once --- well, if --- the handler tries to invoke the |
| 555 |
% original command, it will execute itself all over). |
| 556 |
\ifcsdef{\advice@original@csname{#1}{#2}}{% |
| 557 |
% Yes, we have the original definition, so the safety check failed, and |
| 558 |
% we shouldn't activate again. Unless \dots\ how does its current |
| 559 |
% definition look like? |
| 560 |
\advice@if@our@definition{#1}{#2}{% |
| 561 |
% Well, the current definition of the command matches what we would put |
| 562 |
% there ourselves. The command is definitely activated, and we refuse |
| 563 |
% to activate again, as that would destroy the original definition. |
| 564 |
\advice@activate@error@activated{#1}{#2}{Command}{already}% |
| 565 |
}{% |
| 566 |
% We don't recognize the current definition as our own code (despite |
| 567 |
% the fact that we have surely activated the commmand before, given the |
| 568 |
% result of the first safety check). It appears that someone else was |
| 569 |
% playing fast and loose with the same command, and redefined it after |
| 570 |
% our activation. (In fact, if that someone else was another instance |
| 571 |
% of Advice, from another namespace, forcing the activation will result |
| 572 |
% in the loss of the original definition and the infinite loop.) So it |
| 573 |
% \emph{should} be safe to activate it (again) \dots\ but we won't do |
| 574 |
% it unless the user specifically requested this using |force |
| 575 |
% activate|. Note that without |force activate|, we would be stuck in |
| 576 |
% this branch, as we could neither activate (again) nor deactivate the |
| 577 |
% command. |
| 578 |
\ifadvice@activate@force |
| 579 |
\advice@activate@cmd@do{#1}{#2}% |
| 580 |
\else |
| 581 |
\advice@activate@error@activated{#1}{#2}{Command}{already}% |
| 582 |
\fi |
| 583 |
}% |
| 584 |
}{% |
| 585 |
% No, we don't have the command's original definition, so it was not yet |
| 586 |
% activated, and we may activate it. |
| 587 |
\advice@activate@cmd@do{#1}{#2}% |
| 588 |
}% |
| 589 |
}{% |
| 590 |
\advice@activate@error@undefined{#1}{#2}{Command}{}% |
| 591 |
}% |
| 592 |
} |
| 593 |
% \end{macro} |
| 594 |
% |
| 595 |
% \begin{macro}{\advice@deactivate@cmd} |
| 596 |
% The deactivation of a command follows the same template as activation, but |
| 597 |
% with a different logic, and of course a different effect. In order to |
| 598 |
% deactivate a command, both safety checks discussed above must be satisfied: |
| 599 |
% we must have the command's original definition, \emph{and} our redefinition |
| 600 |
% must still reside in the command's control sequence --- the latter |
| 601 |
% condition prevents overwriting someone else's redefinition with the |
| 602 |
% original command. As both conditions must be unavoidably fulfilled, |force |
| 603 |
% activate| has no effect in deactivation (but |try activate| has). |
| 604 |
\def\advice@deactivate@cmd#1#2{% |
| 605 |
\ifdef{#2}{% |
| 606 |
\ifcsdef{\advice@original@csname{#1}{#2}}{% |
| 607 |
\advice@if@our@definition{#1}{#2}{% |
| 608 |
\advice@deactivate@cmd@do{#1}{#2}% |
| 609 |
}{% |
| 610 |
\advice@deactivate@error@changed{#1}{#2}% |
| 611 |
}% |
| 612 |
}{% |
| 613 |
\advice@activate@error@activated{#1}{#2}{Command}{not yet}% |
| 614 |
}% |
| 615 |
}{% |
| 616 |
\advice@activate@error@undefined{#1}{#2}{Command}{de}% |
| 617 |
}% |
| 618 |
} |
| 619 |
% \end{macro} |
| 620 |
% |
| 621 |
% \begin{macro}{\advice@if@our@definition} |
| 622 |
% This macro checks whether control sequence |#2| was already activated (in |
| 623 |
% namespace |#1|) in the sense that its current definition contains the code |
| 624 |
% our activation would put there: |\advice@handle{#1}{#2}| (protected). |
| 625 |
\def\advice@if@our@definition#1#2{% |
| 626 |
\protected\def\advice@temp{\advice@handle{#1}{#2}}% |
| 627 |
\ifx#2\advice@temp |
| 628 |
\expandafter\@firstoftwo |
| 629 |
\else |
| 630 |
\expandafter\@secondoftwo |
| 631 |
\fi |
| 632 |
} |
| 633 |
% \end{macro} |
| 634 |
% |
| 635 |
% \begin{macro}{\advice@activate@cmd@do} |
| 636 |
% This macro saves the original command, and redefines its control sequence. |
| 637 |
% Our redefinition must be |\protected| --- even if the original command |
| 638 |
% wasn't fragile, our replacement certainly is. (Note that as we require |
| 639 |
% \hologo{eTeX} anyway, we don't have to pay attention to \hologo{LaTeX}'s |
| 640 |
% robust commands by redefining their ``inner'' command. Protecting our |
| 641 |
% replacement suffices.) |
| 642 |
\def\advice@activate@cmd@do#1#2{% |
| 643 |
\cslet{\advice@original@csname{#1}{#2}}#2% |
| 644 |
\protected\def#2{\advice@handle{#1}{#2}}% |
| 645 |
\PackageInfo{advice (#1)}{Activated command "\string#2"}% |
| 646 |
} |
| 647 |
% \end{macro} |
| 648 |
% |
| 649 |
% \begin{macro}{\advice@deactivate@cmd@do} |
| 650 |
% This macro restores the original command, and removes its definition from |
| 651 |
% our storage --- this also serves as a signal that the command is not |
| 652 |
% activated anymore. |
| 653 |
\def\advice@deactivate@cmd@do#1#2{% |
| 654 |
\letcs#2{\advice@original@csname{#1}{#2}}% |
| 655 |
\csundef{\advice@original@csname{#1}{#2}}% |
| 656 |
\PackageInfo{advice (#1)}{Deactivated command "\string#2"}% |
| 657 |
} |
| 658 |
% \end{macro} |
| 659 |
% |
| 660 |
% |
| 661 |
% \subsubsection{Executing a handled command} |
| 662 |
% \label{sec:code:advice:handle} |
| 663 |
% |
| 664 |
% \begin{macro}{\advice@handle} |
| 665 |
% An invocation of this macro is what replaces the original command and runs |
| 666 |
% the whole shebang. The system is designed to bail out as quickly as |
| 667 |
% necessary if the run conditions are not met (plus \hologo{LaTeX}'s |\begin| |
| 668 |
% will receive a very special treatment for this reason). |
| 669 |
% |
| 670 |
% We first check the run conditions, and bail out if they are not satisfied. |
| 671 |
% Note that only the stage-one config is loaded at this point. It sets up |
| 672 |
% the following macros (while they are public, neither the end user not the |
| 673 |
% installation keypath owner should ever have to use them): |
| 674 |
% |
| 675 |
% \begin{itemize} |
| 676 |
% \item \PrintMainMacro{\AdviceRunConditions} executes |\AdviceRuntrue| if the |
| 677 |
% command should be handled; set by |run conditions|. |
| 678 |
% \item \PrintMainMacro{\AdviceBailoutHandler} will be executed if the |
| 679 |
% command will not be handled, after all; set by |bailout handler|. |
| 680 |
% \end{itemize} |
| 681 |
\def\advice@handle#1#2{% |
| 682 |
\advice@init@i{#1}{#2}% |
| 683 |
\AdviceRunfalse |
| 684 |
\AdviceRunConditions |
| 685 |
\advice@handle@rc{#1}{#2}% |
| 686 |
} |
| 687 |
% \end{macro} |
| 688 |
% |
| 689 |
% \begin{macro}{\advice@handle@rc} |
| 690 |
% We continue the handling in a new macro, because this is the point where |
| 691 |
% the handler for |\begin| will hack into the regular flow of events. |
| 692 |
\def\advice@handle@rc#1#2{% |
| 693 |
\ifAdviceRun |
| 694 |
\expandafter\advice@handle@outer |
| 695 |
\else |
| 696 |
% Bailout is simple: we first execute the handler, and then the original command. |
| 697 |
\AdviceBailoutHandler |
| 698 |
\expandafter\advice@original@cs |
| 699 |
\fi |
| 700 |
{#1}{#2}% |
| 701 |
} |
| 702 |
% \end{macro} |
| 703 |
% |
| 704 |
% \begin{macro}{\advice@handle@outer} |
| 705 |
% To actually handle the command, we first setup some macros: |
| 706 |
% |
| 707 |
% \begin{itemize} |
| 708 |
% \item \PrintMainMacro{\AdviceNamespace} holds the installation keypath / |
| 709 |
% storage name space. |
| 710 |
% \item \PrintMainMacro{\AdviceName} holds the control sequence of the handled |
| 711 |
% command, or the environment name. |
| 712 |
% \item \PrintMainMacro{\AdviceReplaced} holds the ``substituted'' code. For |
| 713 |
% commands, this is the same as |\AdviceName|. For environment |foo|, it |
| 714 |
% equals |\begin{foo}| in \hologo{LaTeX}, |\foo| in \hologo{plainTeX} and |
| 715 |
% |\startfoo| in \hologo{ConTeXt}. |
| 716 |
% \item \PrintMainMacro{\AdviceOriginal} executes the original definition of |
| 717 |
% the handled command or environment. |
| 718 |
% \end{itemize} |
| 719 |
\def\advice@handle@outer#1#2{% |
| 720 |
\def\AdviceNamespace{#1}% |
| 721 |
\def\AdviceName{#2}% |
| 722 |
\advice@def@AdviceCsname |
| 723 |
\let\AdviceReplaced\AdviceName |
| 724 |
\def\AdviceOriginal{\AdviceGetOriginal{#1}{#2}}% |
| 725 |
% We then load the stage-two settings. This defines the following macros: |
| 726 |
% \begin{itemize} |
| 727 |
% \item \PrintMainMacro{\AdviceOuterHandler} will effectively replace the |
| 728 |
% command, if it will be handled; set by |outer handler|. |
| 729 |
% \item \PrintMainMacro{\AdviceCollector} collects the arguments of the handled |
| 730 |
% command, perhaps consulting |\AdviceArgs| to learn about its argument |
| 731 |
% structure. |
| 732 |
% \item \PrintMainMacro{\AdviceRawCollectorOptions} contains the options which |
| 733 |
% will be passed to the argument collector, in the ``raw'' format. |
| 734 |
% \item \PrintMainMacro{\AdviceCollectorOptions} contains the additional, |
| 735 |
% user-specified options which will be passed to the argument collector. |
| 736 |
% \item \PrintMainMacro{\AdviceArgs} contains the |xparse|-style argument |
| 737 |
% specification of the command, or equals |\advice@noargs| to signal that |
| 738 |
% command was defined using |xparse| and that the argument specification |
| 739 |
% should be retrieved automatically. |
| 740 |
% \item \PrintMainMacro{\AdviceInnerHandler} is called by the argument |
| 741 |
% collector once it finishes its work. It receives all the collected |
| 742 |
% arguments as a single (braced) argument. |
| 743 |
% \item \PrintMainMacro{\AdviceOptions} holds options which may be used by the |
| 744 |
% outer or the inner handler; Advice does not need or touch |
| 745 |
% them. |
| 746 |
% \end{itemize} |
| 747 |
\advice@init@I{#1}{#2}% |
| 748 |
% All prepared, we execute the outer handler. |
| 749 |
\AdviceOuterHandler |
| 750 |
} |
| 751 |
\def\advice@def@AdviceCsname{% |
| 752 |
\begingroup |
| 753 |
\escapechar=-1 |
| 754 |
\expandafter\expandafter\expandafter\endgroup |
| 755 |
\expandafter\expandafter\expandafter\def |
| 756 |
\expandafter\expandafter\expandafter\AdviceCsname |
| 757 |
\expandafter\expandafter\expandafter{\expandafter\string\AdviceName}% |
| 758 |
} |
| 759 |
% \end{macro} |
| 760 |
% |
| 761 |
% \begin{macro}{\ifAdviceRun} |
| 762 |
% This conditional is set by the run conditions macro to signal whether we |
| 763 |
% should run the outer (true) or the bailout (false) handler. |
| 764 |
\newif\ifAdviceRun |
| 765 |
% \end{macro} |
| 766 |
% |
| 767 |
% \begin{macro}{\advice@CollectArgumentsRaw} |
| 768 |
% This is the default collector, which will collect the argument using |
| 769 |
% CollArgs' command |\CollectArgumentsRaw|. It will provide that command |
| 770 |
% with: |
| 771 |
% \begin{itemize} |
| 772 |
% \item the collector options, given in the raw format: |
| 773 |
% \begin{itemize} |
| 774 |
% \item the caller (|\collargsCaller|), |
| 775 |
% \item the raw options (|\AdviceRawCollectorOptions|), and |
| 776 |
% \item the user options (|\AdviceRawCollectorOptions|, wrapped in |
| 777 |
% |\collargsSet|; |
| 778 |
% \end{itemize} |
| 779 |
% \item the argument specification |\AdviceArgs| of the handled command; and |
| 780 |
% \item the inner handler |\AdviceInnerHandler| to execute after collecting the |
| 781 |
% arguments; the inner handler receives the collected arguments as a single |
| 782 |
% braced argument. |
| 783 |
% \end{itemize} |
| 784 |
% \hangindent=0pt If the argument specification is not defined (either the user |
| 785 |
% did not set it, or has reset it by writing |args| without a value), it is |
| 786 |
% assumed that the handled command was defined by |xparse| and |\AdviceArgs| |
| 787 |
% will be retrieved by |\GetDocumentCommandArgSpec|. |
| 788 |
% \begin{listingregion}{_advice-CollectArgumentsRaw.tex} |
| 789 |
\def\advice@CollectArgumentsRaw{% |
| 790 |
\AdviceIfArgs{}{% |
| 791 |
\expandafter\GetDocumentCommandArgSpec\expandafter{\AdviceName}% |
| 792 |
\let\AdviceArgs\ArgumentSpecification |
| 793 |
}% |
| 794 |
\expanded{% |
| 795 |
\noexpand\CollectArgumentsRaw{% |
| 796 |
\noexpand\collargsCaller{\expandonce\AdviceName}% |
| 797 |
\expandonce\AdviceRawCollectorOptions |
| 798 |
\ifdefempty\AdviceCollectorOptions{}{% |
| 799 |
\noexpand\collargsSet{\expandonce\AdviceCollectorOptions}% |
| 800 |
}% |
| 801 |
}% |
| 802 |
{\expandonce\AdviceArgs}% |
| 803 |
{\expandonce\AdviceInnerHandler}% |
| 804 |
}% |
| 805 |
} |
| 806 |
% \end{listingregion} |
| 807 |
% \end{macro} |
| 808 |
% |
| 809 |
% \begin{macro}{\AdviceIfArgs} |
| 810 |
% If the value of |args| is ``real'', i.e.\ an |xparse| argument |
| 811 |
% specification, execute the first argument. If |args| was set to the |
| 812 |
% special value |\advice@noargs|, signaling a command defined by |
| 813 |
% |\NewDocumentCommand| or friends, execute the second argument. (Ok, in |
| 814 |
% reality anything other than |\advice@noargs| counts as real ``real''.) |
| 815 |
\def\advice@noargs@text{\advice@noargs} |
| 816 |
\def\AdviceIfArgs{% |
| 817 |
\ifx\AdviceArgs\advice@noargs@text |
| 818 |
\expandafter\@secondoftwo |
| 819 |
\else |
| 820 |
\expandafter\@firstoftwo |
| 821 |
\fi |
| 822 |
} |
| 823 |
% \end{macro} |
| 824 |
% |
| 825 |
% \begin{macro}{\advice@pgfkeys@collector} |
| 826 |
% A |pgfkeys| collector is very simple: the sole argument of the any key |
| 827 |
% macro, regardless of the argument structure of the key, is everything up to |
| 828 |
% |\pgfeov|. |
| 829 |
\def\advice@pgfkeys@collector#1\pgfeov{% |
| 830 |
\AdviceInnerHandler{#1}% |
| 831 |
} |
| 832 |
% \end{macro} |
| 833 |
% |
| 834 |
% \subsubsection{Environments} |
| 835 |
% \label{sec:code:advice:environments} |
| 836 |
% |
| 837 |
% \begin{macro}{\advice@activate@env,\advice@deactivate@env} |
| 838 |
% Things are simple in \hologo{TeX} and \hologo{ConTeXt}, as their environments |
| 839 |
% are really commands. So rather than activating environment name |#2|, we |
| 840 |
% (de)activate command |\#2| or |\start#2|, depending on the format. |
| 841 |
%<*plain,context> |
| 842 |
\def\advice@activate@env#1#2{% |
| 843 |
\expanded{% |
| 844 |
\noexpand\advice@activate@cmd{#1}{\expandonce{\csname |
| 845 |
%<context>start% |
| 846 |
#2\endcsname}}% |
| 847 |
}% |
| 848 |
} |
| 849 |
\def\advice@deactivate@env#1#2{% |
| 850 |
\expanded{% |
| 851 |
\noexpand\advice@deactivate@cmd{#1}{\expandonce{\csname |
| 852 |
%<context>start% |
| 853 |
#2\endcsname}}% |
| 854 |
}% |
| 855 |
} |
| 856 |
%</plain,context> |
| 857 |
% |
| 858 |
% We activate commands by redefining them; that's the only way to do it. But |
| 859 |
% we won't activate a \hologo{LaTeX} environment |foo| by redefining command |
| 860 |
% |\foo|, where the user's definition for the start of the environment actually |
| 861 |
% resides, as such a redefinition would be executed too late, deep within the |
| 862 |
% group opened by |\begin|, following many internal operations and public |
| 863 |
% hooks. We handle \hologo{LaTeX} environments by defining an outer handler |
| 864 |
% for |\begin| (consequently, \hologo{LaTeX} environment support can be |
| 865 |
% (de)activated by the user by saying (|de|)|activate=\begin|), and |
| 866 |
% activating an environment will be nothing but setting a mark, by |
| 867 |
% defining a dummy control sequence |\advice@original@csname{#1}{#2}|, |
| 868 |
% which that handler will inspect. Note that |force activate| has no |
| 869 |
% effect here. |
| 870 |
%<*latex> |
| 871 |
\def\advice@activate@env#1#2{% |
| 872 |
\ifcsdef{\advice@original@csname{#1}{#2}}{% |
| 873 |
\advice@activate@error@activated{#1}{#2}{Environment}{already}% |
| 874 |
}{% |
| 875 |
\csdef{\advice@original@csname{#1}{#2}}{}% |
| 876 |
\PackageInfo{advice (#1)}{Activated environment "#2"}% |
| 877 |
}% |
| 878 |
} |
| 879 |
\def\advice@deactivate@env#1#2{% |
| 880 |
\ifcsdef{\advice@original@csname{#1}{#2}}{% |
| 881 |
\csundef{\advice@original@csname{#1}{#2}}{}% |
| 882 |
}{% |
| 883 |
\advice@activate@error@activated{#1}{#2}{Environment}{not yet}% |
| 884 |
\PackageInfo{advice (#1)}{Dectivated environment "#2"}% |
| 885 |
}% |
| 886 |
} |
| 887 |
% \end{macro} |
| 888 |
% |
| 889 |
% \begin{macro}{\advice@begin@rc} |
| 890 |
% This is the handler for |\begin|. It is very special, for speed. It is |
| 891 |
% meant to be declared as the run conditions component, and it hacks into |
| 892 |
% the normal flow of handling. It knows that after executing the run |
| 893 |
% conditions macro, |\advice@handle| eventually (the tracing info may |
| 894 |
% interrupt here as |#1|) continues by |
| 895 |
% |\advice@handle@rc|\marg{namespace}\marg{handled control sequence}, so it |
| 896 |
% grabs all these (|#2| is the \meta{namespace} and |#3| is the |
| 897 |
% \meta{handled control sequence}, i.e.\ |\begin|) plus the environment |
| 898 |
% name (|#4|). |
| 899 |
\def\advice@begin@rc#1\advice@handle@rc#2#3#4{% |
| 900 |
% We check whether environment |#4| is activated (in namespace |#2|) by |
| 901 |
% inspecting whether activation dummy is defined. If it is not, we execute |
| 902 |
% the original |\begin| (|\advice@original@cs{#2}{#3}|), followed by the |
| 903 |
% environment name (|#4|). Note that we \emph{don't} execute the |
| 904 |
% environment's bailout handler here: we haven't checked its run conditions |
| 905 |
% yet, as the environment is simply not activated. |
| 906 |
\ifcsname\advice@original@csname{#2}{#4}\endcsname |
| 907 |
\expandafter\advice@begin@env@rc |
| 908 |
\else |
| 909 |
\expandafter\advice@original@cs |
| 910 |
\fi |
| 911 |
{#2}{#3}{#4}% |
| 912 |
} |
| 913 |
% \end{macro} |
| 914 |
% |
| 915 |
% \begin{macro}{\advice@begin@env@rc} |
| 916 |
% Starting from this point, we essentially replicate the workings of |
| 917 |
% |\advice@handle|, adapted to \hologo{LaTeX} environments. |
| 918 |
\def\advice@begin@env@rc#1#2#3{% |
| 919 |
% We first load the stage-one configuration for environment |#3| in namespace |
| 920 |
% |#1|. |
| 921 |
\advice@init@i{#1}{#3}% |
| 922 |
% This defined |\AdviceRunConditions| for the environment. We can now check its |
| 923 |
% run conditions. If they are not satisfied, we bail out by executing the |
| 924 |
% environment's bailout handler followed by the original |\begin| |
| 925 |
% (|\advice@original@cs{#1}{#2}|) plus the environment name (|#3|). |
| 926 |
\AdviceRunConditions |
| 927 |
\ifAdviceRun |
| 928 |
\expandafter\advice@begin@env@outer |
| 929 |
\else |
| 930 |
\AdviceBailoutHandler |
| 931 |
\expandafter\advice@original@cs |
| 932 |
\fi |
| 933 |
{#1}{#2}{#3}% |
| 934 |
} |
| 935 |
% \end{macro} |
| 936 |
% |
| 937 |
% \begin{macro}{\advice@begin@env@outer} |
| 938 |
% We define the macros expected by the outer handler, see |
| 939 |
% |\advice@handle@outer|, load the second-stage configuration, and execute the |
| 940 |
% environment's outer handler. |
| 941 |
\def\advice@begin@env@outer#1#2#3{% |
| 942 |
\def\AdviceNamespace{#1}% |
| 943 |
\def\AdviceName{#3}% |
| 944 |
\let\AdviceCsname\advice@undefined |
| 945 |
\def\AdviceReplaced{#2{#3}}% |
| 946 |
\def\AdviceOriginal{\AdviceGetOriginal{#1}{#2}{#3}}% |
| 947 |
\advice@init@I{#1}{#3}% |
| 948 |
\AdviceOuterHandler |
| 949 |
} |
| 950 |
%</latex> |
| 951 |
% \end{macro} |
| 952 |
% |
| 953 |
% |
| 954 |
% \subsubsection{Error messages} |
| 955 |
% |
| 956 |
% Define error messages for the entire package. Note that |
| 957 |
% |\advice@(de)activate@error@...| implement |try activate|. |
| 958 |
% |
| 959 |
\def\advice@activate@error@activated#1#2#3#4{% |
| 960 |
\ifadvice@activate@try |
| 961 |
\else |
| 962 |
\PackageError{advice (#1)}{#3 "\string#2" is #4 activated}{}% |
| 963 |
\fi |
| 964 |
} |
| 965 |
\def\advice@activate@error@undefined#1#2#3#4{% |
| 966 |
\ifadvice@activate@try |
| 967 |
\else |
| 968 |
\PackageError{advice (#1)}{% |
| 969 |
#3 "\string#2" you are trying to #4activate is not defined}{}% |
| 970 |
\fi |
| 971 |
} |
| 972 |
\def\advice@deactivate@error@changed#1#2{% |
| 973 |
\ifadvice@activate@try |
| 974 |
\else |
| 975 |
\PackageError{advice (#1)}{The definition of "\string#2" has changed since we |
| 976 |
have activated it. Has somebody overridden our command?}{If you have tried |
| 977 |
to deactivate so that you could immediately reactivate, you may want to try |
| 978 |
"force activate".}% |
| 979 |
\fi |
| 980 |
} |
| 981 |
\def\advice@error@advice@notcs#1#2{% |
| 982 |
\PackageError{advice}{The first argument of key "#1" should be either a single |
| 983 |
control sequence or an environment name, not "#2"}{}% |
| 984 |
} |
| 985 |
\def\advice@error@activate@notcsorenv#1#2{% |
| 986 |
\PackageError{advice}{Each item in the value of key "#1activate" should be |
| 987 |
either a control sequence or an environment name, not "#2".}{}% |
| 988 |
} |
| 989 |
\def\advice@error@storecs@notcs#1#2{% |
| 990 |
\PackageError{advice}{The value of key "#1" should be a single control sequence, |
| 991 |
not "\string#2"}{}% |
| 992 |
} |
| 993 |
\def\advice@error@noinnerhandler#1{% |
| 994 |
\PackageError{advice (\AdviceNamespace)}{The inner handler for |
| 995 |
"\expandafter\string\AdviceName" is not defined}{}% |
| 996 |
} |
| 997 |
% \subsubsection{Tracing} |
| 998 |
% |
| 999 |
% We implement tracing by adding the tracing information to the handlers after |
| 1000 |
% we load them. So it is the handlers themselves which, if and when they are |
| 1001 |
% executed, will print out that this is happening. |
| 1002 |
% |
| 1003 |
% \begin{macro}{\AdviceTracingOn,\AdviceTracingOff} |
| 1004 |
% Enable and disable tracing. |
| 1005 |
\def\AdviceTracingOn{% |
| 1006 |
\let\advice@init@i\advice@trace@init@i |
| 1007 |
\let\advice@init@I\advice@trace@init@I |
| 1008 |
} |
| 1009 |
\def\AdviceTracingOff{% |
| 1010 |
\let\advice@init@i\advice@setup@init@i |
| 1011 |
\let\advice@init@I\advice@setup@init@I |
| 1012 |
} |
| 1013 |
% \end{macro} |
| 1014 |
% \begin{macro}{\advice@typeout,\advice@trace} |
| 1015 |
% The tracing output routine; the typeout macro depends on the format. In |
| 1016 |
% \hologo{LaTeX}, we use stream |\@unused|, which is guaranteed to be |
| 1017 |
% unopened, so that the output will go to the terminal and the log. |
| 1018 |
% \hologo{ConTeXt}, we don't muck about with write streams but simply use Lua |
| 1019 |
% function |texio.write_nl|. In \hologo{plainTeX}, we use either Lua or the |
| 1020 |
% stream, depending on the engine; we use a high stream number 128 although |
| 1021 |
% the good old 16 would probably work just as well. |
| 1022 |
%<plain>\ifdefined\luatexversion |
| 1023 |
%<!latex>\long\def\advice@typeout#1{\directlua{texio.write_nl("\luaescapestring{#1}")}} |
| 1024 |
%<plain>\else |
| 1025 |
%<latex>\def\advice@typeout{\immediate\write\@unused} |
| 1026 |
%<plain>\def\advice@typeout{\immediate\write128} |
| 1027 |
%<plain>\fi |
| 1028 |
\def\advice@trace#1{\advice@typeout{[tracing advice] #1}} |
| 1029 |
% \end{macro} |
| 1030 |
% \begin{macro}{\advice@trace@init@i,\advice@trace@init@I} |
| 1031 |
% Install the tracing code. |
| 1032 |
\def\advice@trace@init@i#1#2{% |
| 1033 |
\advice@trace{Advising \detokenize\expandafter{\string#2} (\detokenize{#1})}% |
| 1034 |
\advice@trace{\space\space Original command meaning: |
| 1035 |
\expandafter\expandafter\expandafter\meaning\advice@original@cs{#1}{#2}}% |
| 1036 |
\advice@setup@init@i{#1}{#2}% |
| 1037 |
\edef\AdviceRunConditions{% |
| 1038 |
% We first execute the original run conditions, so that we can show the |
| 1039 |
% result. |
| 1040 |
\expandonce\AdviceRunConditions |
| 1041 |
\noexpand\advice@trace{\space\space |
| 1042 |
Executing run conditions: |
| 1043 |
\detokenize\expandafter{\AdviceRunConditions} |
| 1044 |
--> |
| 1045 |
\noexpand\ifAdviceRun true\noexpand\else false\noexpand\fi |
| 1046 |
}% |
| 1047 |
}% |
| 1048 |
\edef\AdviceBailoutHandler{% |
| 1049 |
\noexpand\advice@trace{\space\space |
| 1050 |
Executing bailout handler: |
| 1051 |
\detokenize\expandafter{\AdviceBailoutHandler}}% |
| 1052 |
\expandonce\AdviceBailoutHandler |
| 1053 |
}% |
| 1054 |
} |
| 1055 |
\def\advice@trace@init@I#1#2{% |
| 1056 |
\advice@setup@init@I{#1}{#2}% |
| 1057 |
\edef\AdviceOuterHandler{% |
| 1058 |
\noexpand\advice@trace{\space\space |
| 1059 |
Executing outer handler: |
| 1060 |
\detokenize\expandafter{\AdviceOuterHandler}}% |
| 1061 |
\expandonce\AdviceOuterHandler |
| 1062 |
}% |
| 1063 |
\edef\AdviceCollector{% |
| 1064 |
\noexpand\advice@trace{\space\space |
| 1065 |
Executing collector: |
| 1066 |
\detokenize\expandafter{\AdviceCollector}}% |
| 1067 |
\noexpand\advice@trace{\space\space\space\space |
| 1068 |
Argument specification: |
| 1069 |
\detokenize\expandafter{\AdviceArgs}}% |
| 1070 |
\noexpand\advice@trace{\space\space\space\space |
| 1071 |
Options: |
| 1072 |
\detokenize\expandafter{\AdviceCollectorOptions}}% |
| 1073 |
\noexpand\advice@trace{\space\space\space\space |
| 1074 |
Raw options: |
| 1075 |
\detokenize\expandafter{\AdviceRawCollectorOptions}}% |
| 1076 |
% Collargs' |return| complicates tracing of the received argument. We put |
| 1077 |
% the code for remembering its value among the raw collector options. The |
| 1078 |
% default is 0; it is needed when we're using a collector other that |
| 1079 |
% |\CollectArguments|, the assumption being that external collectors will |
| 1080 |
% always return the collected arguments braced. |
| 1081 |
\unexpanded{% |
| 1082 |
\gdef\advice@collargs@return{0}% |
| 1083 |
\appto\AdviceRawCollectorOptions{\advice@remember@collargs@return}% |
| 1084 |
}% |
| 1085 |
\expandonce\AdviceCollector |
| 1086 |
}% |
| 1087 |
\edef\advice@inner@handler@trace@do{% |
| 1088 |
\noexpand\advice@trace{\space\space |
| 1089 |
Executing inner handler: |
| 1090 |
\detokenize\expandafter{\AdviceInnerHandler}}% |
| 1091 |
% When this macro is executed, the received arguments are waiting for us in |
| 1092 |
% |\toks0|. |
| 1093 |
\noexpand\advice@trace{\space\space\space\space |
| 1094 |
Received arguments\noexpand\advice@inner@handler@trace@printcollargsreturn: |
| 1095 |
\noexpand\detokenize\noexpand\expandafter{\unexpanded{\the\toks0}}}% |
| 1096 |
\noexpand\advice@trace{\space\space\space\space |
| 1097 |
Options: |
| 1098 |
\detokenize\expandafter{\AdviceOptions}}% |
| 1099 |
\expandonce{\AdviceInnerHandler}% |
| 1100 |
}% |
| 1101 |
\def\AdviceInnerHandler{\advice@inner@handler@trace}% |
| 1102 |
} |
| 1103 |
\def\advice@remember@collargs@return{% |
| 1104 |
\global\let\advice@collargs@return\collargsReturn |
| 1105 |
} |
| 1106 |
% This is the entry point into the tracing inner handler. It will either get |
| 1107 |
% the received arguments as a braced argument (when Collargs' |return=0|), or |
| 1108 |
% from |\collargsArgs| otherwise. We don't simply always inspect |
| 1109 |
% |\collargsArgs| because foreign argument collectors will not use this token |
| 1110 |
% register; the assumption is that they will always return the collected |
| 1111 |
% arguments braced. |
| 1112 |
\def\advice@inner@handler@trace{% |
| 1113 |
\ifnum\advice@collargs@return=0 |
| 1114 |
\expandafter\advice@inner@handler@trace@i |
| 1115 |
\else |
| 1116 |
\expandafter\advice@inner@handler@trace@ii |
| 1117 |
\fi |
| 1118 |
} |
| 1119 |
\def\advice@inner@handler@trace@i#1{% |
| 1120 |
\toks0={#1}% |
| 1121 |
\advice@inner@handler@trace@do{#1}% |
| 1122 |
} |
| 1123 |
\def\advice@inner@handler@trace@ii{% |
| 1124 |
\expandafter\toks\expandafter0\expandafter{\the\collargsArgs}% |
| 1125 |
\advice@inner@handler@trace@do |
| 1126 |
} |
| 1127 |
\def\advice@inner@handler@trace@printcollargsreturn{% |
| 1128 |
\ifnum\advice@collargs@return=0 |
| 1129 |
\else |
| 1130 |
\space(collargs return=% |
| 1131 |
\ifcase\advice@collargs@return braced\or plain\or no\fi |
| 1132 |
)% |
| 1133 |
\fi |
| 1134 |
} |
| 1135 |
% \end{macro} |
| 1136 |
%<plain>\resetatcatcode |
| 1137 |
%<context>\stopmodule |
| 1138 |
%<context>\protect |
| 1139 |
%</main> |
| 1140 |
% |
| 1141 |
% \subsubsection{The \TikZ; collector} |
| 1142 |
% \label{sec:code:advice:tikz} |
| 1143 |
% |
| 1144 |
% In this section, we implement the argument collector for command |\tikz|, |
| 1145 |
% which has idiosyncratic syntax, see \PGFmanual{12.2.2}: |
| 1146 |
% \begin{itemize} |
| 1147 |
% \item |\tikz|\meta{animation spec}|[|\meta{options}|]{|\meta{picture code}|}| |
| 1148 |
% \item |\tikz|\meta{animation spec}|[|\meta{options}|]|\meta{picture command}|;| |
| 1149 |
% \end{itemize} |
| 1150 |
% where \meta{animation spec} = |
| 1151 |
% (|:|\meta{key}|={|\meta{value}|}|)*. |
| 1152 |
% |
| 1153 |
% The \TikZ; code resides in a special file. It is meant to be |\input| at any |
| 1154 |
% time, so we need to temporarily assign |@| category code 11. |
| 1155 |
%<*tikz> |
| 1156 |
\edef\adviceresetatcatcode{\catcode`\noexpand\@\the\catcode`\@\relax}% |
| 1157 |
\catcode`\@=11 |
| 1158 |
\def\AdviceCollectTikZArguments{% |
| 1159 |
% We initialize the token register which will hold the collected arguments, |
| 1160 |
% and start the collection. Nothing of note happens until \dots |
| 1161 |
\toks0={}% |
| 1162 |
\advice@tikz@anim |
| 1163 |
} |
| 1164 |
\def\advice@tikz@anim{% |
| 1165 |
\pgfutil@ifnextchar[{\advice@tikz@opt}{% |
| 1166 |
\pgfutil@ifnextchar:{\advice@tikz@anim@a}{% |
| 1167 |
\advice@tikz@code}}%] |
| 1168 |
} |
| 1169 |
\def\advice@tikz@anim@a#1=#2{% |
| 1170 |
\toksapp0{#1={#2}}% |
| 1171 |
\advice@tikz@anim |
| 1172 |
} |
| 1173 |
\def\advice@tikz@opt[#1]{% |
| 1174 |
\toksapp0{[#1]}% |
| 1175 |
\advice@tikz@code |
| 1176 |
} |
| 1177 |
\def\advice@tikz@code{% |
| 1178 |
\pgfutil@ifnextchar\bgroup\advice@tikz@braced\advice@tikz@single |
| 1179 |
} |
| 1180 |
\long\def\advice@tikz@braced#1{\toksapp0{{#1}}\advice@tikz@done} |
| 1181 |
\def\advice@tikz@single#1;{\toksapp0{#1;}\advice@tikz@done} |
| 1182 |
% \dots\ we finish collecting the arguments, when we execute the inner handler, |
| 1183 |
% with the (braced) collected arguments is its sole argument. |
| 1184 |
\def\advice@tikz@done{% |
| 1185 |
\expandafter\AdviceInnerHandler\expandafter{\the\toks0}% |
| 1186 |
} |
| 1187 |
\adviceresetatcatcode |
| 1188 |
%</tikz> |
| 1189 |
% \end{macrocode} |
| 1190 |
% |
| 1191 |
% Local Variables: |
| 1192 |
% TeX-engine: luatex |
| 1193 |
% TeX-master: "doc/memoize-code.tex" |
| 1194 |
% TeX-auto-save: nil |
| 1195 |
% End: |