| 1 |
Gates: a presentation |
| 2 |
===================== |
| 3 |
|
| 4 |
This part of the documentation is a general introduction to gates, |
| 5 |
illustrating many of their properties in the abstract. Then comes |
| 6 |
{a user manual for TeX} [see TeXUser_tag], |
| 7 |
{a complete reference for TeX} [see TeXReference_tag], |
| 8 |
{a user manual for Lua} [see LuaUser_tag] and |
| 9 |
{a complete reference for Lua} [see LuaReference_tag]. |
| 10 |
The manuals explain gates in action, while the references are |
| 11 |
alphabetical lists of all actions with their syntax. |
| 12 |
|
| 13 |
This documentation doesn't contain very complex examples of gates. To |
| 14 |
see gates in full regalia, you can have a look at PiTeX (the set of |
| 15 |
files used to typeset this documentation) for (mostly) TeX gates, and |
| 16 |
the Interpreter package, version 1.1, for Lua gates (version 1.0, wasn't |
| 17 |
written with Gates). Speaking of Interpreter, this documentation was |
| 18 |
written with it, so it can be read confortably in a text editor (see |
| 19 |
"gates-doc.txt"). |
| 20 |
|
| 21 |
|
| 22 |
What are gates, and what are they good for? |
| 23 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 24 |
|
| 25 |
At first sight, gates are just a convoluted way to define and execute |
| 26 |
macros. But they're better thought of as bricks to *construct* macros |
| 27 |
in such a way that each brick can be independantly controlled, and new |
| 28 |
bricks can be added to a wall already built. For each logical step in a |
| 29 |
big macro, the user can decide whether to execute that step, bypass it, |
| 30 |
add a new step before or after, etc. This way, you give the user the |
| 31 |
ability to tweak your macro as s/he wishes. |
| 32 |
|
| 33 |
Suppose for instance you write \BigMacro. \BigMacro does a lot of |
| 34 |
interesting things: first it does A, then B, then -- last but not least |
| 35 |
-- C. The problem with \BigMacro is that it's very good; so good that |
| 36 |
everybody uses it and wants to add his or her little twist to it, because |
| 37 |
\BigMacro really entices creativity. You have no time to add those |
| 38 |
twists yourself, nor do you think they'll interest anybody but their |
| 39 |
authors. Yet you keep receiving emails asking you to modify \BigMacro. |
| 40 |
Yes, \BigMacro is big, so most users don't care to try and understand |
| 41 |
it; even if they do, they're not so sure how it works: in A, there seems |
| 42 |
to happen something, and X becomes Y, and then in B, Y is passed to... |
| 43 |
or was it X? Plus those same people really don't want to copy one hundred |
| 44 |
lines of code just to add their touch to it. \BigMacro is like a plate |
| 45 |
of spaghetti: good, but spaghetti nonetheless, hard to decipher and |
| 46 |
rearrange. A few people will be able to hack it, but it is lost on users |
| 47 |
bold but lacking experience. |
| 48 |
|
| 49 |
Gates are made to avoid that situation. \BigMacro will now be made of |
| 50 |
the same logical steps, but they will be gates, which means users will |
| 51 |
be able to control their behavior without ever looking at how they're |
| 52 |
implemented. For instance, one person would prefer to avoid step B in |
| 53 |
some cases; if B is a gate, that can be done easily. Another don't like |
| 54 |
B at all and wants to remove it altogether: nothing simpler. A third |
| 55 |
person would like to get the output of B and modify it a bit before C |
| 56 |
kicks in; s/he just has to write a new gate (which is like writing a |
| 57 |
macro) and add it after B; whatever B returns will be passed to that |
| 58 |
new gate instead of C, and whatever the new gate returns will be passed |
| 59 |
to C. That's another thing with gates: they pass arguments between them |
| 60 |
when one follows the other; they can even return arguments. That's |
| 61 |
nothing new in Lua; but in TeX, arguments handling is generally not so |
| 62 |
simple. In most, if not all, programming languages, the following: |
| 63 |
|
| 64 |
dosomething(makesomething(<argument>)) |
| 65 |
|
| 66 |
is evaluated as follows: <argument> is passed to "makesomething", whose |
| 67 |
return value is passed to "dosomething". The equivalent in TeX doesn't |
| 68 |
do that, however: |
| 69 |
|
| 70 |
\dosomething{\makesomething{<argument>}} |
| 71 |
|
| 72 |
Here "\makesomething{<argument}" is passed to \dosomething and chaos |
| 73 |
is sure to ensue if the previous behavior is expected. Instead, one |
| 74 |
must generally store whatever \makesomething `returns' in a macro and |
| 75 |
pass that macro to \dosomething. With gates, however, the passing of |
| 76 |
arguments can be mimicked: if two gates follow each other in a list, |
| 77 |
whatever the first returns (no quotes here) is passed to the second. |
| 78 |
In our case, if \makesomething is called before \dosomething, the former |
| 79 |
will process its argument and pass its return value (if any) to the |
| 80 |
latter. No special care is needed to do so. |
| 81 |
|
| 82 |
Another property of gates, obviously, is that they are reusable. In the |
| 83 |
TeX world, however, it is customary to distinguish between public macros, |
| 84 |
to be manipulated by users, and private ones, traditionally marked by |
| 85 |
an "@" in their names, and that lay users should absolutely ignore. |
| 86 |
With gates, everything seems public. That's indeed the very reason why |
| 87 |
gates exist: so that macros are as hackable as possible, and users can |
| 88 |
do what they like with them. If you're afraid that chaos might ensue |
| 89 |
(because users are notoriously dangerous), don't worry too much: it is |
| 90 |
a little bit harder to grasp how gates work than to turn "@" into a |
| 91 |
letter so it can be used in private macros; a user who would manipulate |
| 92 |
gates thus probably knows what s/he's doing, or at least that what |
| 93 |
s/he's doing might break. (By the way, gates are very likely to contain |
| 94 |
"@"-marked private macros anyway, if only because they don't make sense |
| 95 |
outside the context of their use.) |
| 96 |
|
| 97 |
If you want to use gates but still insist that users shouldn't mess |
| 98 |
with them, there is another solution: don't document them. Indeed, gates |
| 99 |
are useful only to the extent that other people know where they occur |
| 100 |
and what they do. So documenting gates is an essential step in exploiting |
| 101 |
them fully; as will be said again below, though, there is no need to |
| 102 |
explain what a gate does internally: simply saying what arguments it |
| 103 |
takes, what it does with them (conceptually), and what it returns, is |
| 104 |
enough. In other words, you don't explain the code that makes up the |
| 105 |
gate, you explain its function in the larger picture. |
| 106 |
|
| 107 |
One last remark about gates, not expanded in this document: they can |
| 108 |
be useful to create libraries, i.e. snippets of code to be used in |
| 109 |
various places. Instead of writing macros to perform such and such |
| 110 |
operation, you can write gates; the difference is that they can be |
| 111 |
easily added to existing code, and externally controlled. |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
An overview of (almost) all actions |
| 116 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 117 |
|
| 118 |
There are two types of gates: macro gates (or `m-gates' for short) and |
| 119 |
list gates (`l-gates'). M-gates execute some code, like macros in TeX |
| 120 |
or functions in Lua; l-gates contain other gates (of either type) and |
| 121 |
call them in turn, passing arguments between them; from now on, `gate' |
| 122 |
means a gate of either type. Typically, a big macro is an l-gate with |
| 123 |
many subgates, themselves possibly l-gates containing gates of a lower |
| 124 |
level still, and so on and so forth. Then one can say, `In that l-gate, |
| 125 |
I want this gate to be ignored'; or `I want to add my own gate to this |
| 126 |
l-gate, in such position', etc. |
| 127 |
|
| 128 |
|
| 129 |
-- Defining and executing gates -- |
| 130 |
|
| 131 |
To define an m-gate, one uses the |def| action; the |list| action is |
| 132 |
used to declare an l-gate. Gates of either type can then be added to |
| 133 |
an l-gate with |add|; by default, the insertion is done at the end of |
| 134 |
the list, but a position relative to other gates can be optionally |
| 135 |
specified. Gates can be removed with the |remove| action. |
| 136 |
|
| 137 |
A gate can also be defined by |copy|ing another gate; in that case, the |
| 138 |
new gate is identical to the copied one, except its status (see below) |
| 139 |
is set to |open|, which is the default status when creating a gate. When |
| 140 |
copying an l-gate, the list is copied too (also it is the same subgates |
| 141 |
that occur in both l-gates). |
| 142 |
|
| 143 |
To call a gate, one uses |execute| (various shorthands are possible). |
| 144 |
Executing an m-gate is not very different from executing a macro or |
| 145 |
function: it performs its definition. The execution of an l-gate, on the |
| 146 |
other hand, consists in calling the gates that were added to it. Also, |
| 147 |
and most importantly, gates in a l-gate pass arguments between them: the |
| 148 |
first gate receives the arguments passed to the l-gate it belongs to, |
| 149 |
then passes them to the next gate, possibly modifying some of those |
| 150 |
arguments by |return|ing. (Returning arguments depends heavily on the |
| 151 |
implementation; see |return| and associates in TeX, and |autoreturn| |
| 152 |
in Lua.) |
| 153 |
|
| 154 |
For each language, see: |
| 155 |
{defining and executing gates in TeX} [see TeXDefinition_tag] |
| 156 |
and |
| 157 |
{defining and executing gates in Lua} [see LuaDefinition_tag]. |
| 158 |
|
| 159 |
|
| 160 |
-- Conditions -- |
| 161 |
|
| 162 |
Gates can be externally controlled in four ways. First, all gates have |
| 163 |
a |status|: if it is |open| (which is default), the gate is executed |
| 164 |
when encountered; if it is |close|, it is ignored; |ajar| means that the |
| 165 |
gate will be executed the next time it is encountered, and then its |
| 166 |
status will revert to |close|; |skip| is the opposite: the gate will be |
| 167 |
ignored the next time, then it will revert to |open|. In short, |ajar| |
| 168 |
and |skip| mean executing or ignoring the gate once. |
| 169 |
See {status in TeX} [see TeXStatus_tag] |
| 170 |
and {status in Lua} [see LuaStatus_tag]. |
| 171 |
|
| 172 |
Second, all gates may have a |conditional|: if it is true, the gate is |
| 173 |
executed, and ignored otherwise. This allows gates to depend on external |
| 174 |
states of affairs. But a gate's conditional takes the same arguments as |
| 175 |
the gate itself, so the its value can also depend on what is passed to |
| 176 |
the gate. Thus, what a gate does and why it does it are kept distinct. |
| 177 |
See {conditional in TeX} [see TeXConditional_tag] |
| 178 |
and {conditional in Lua} [see LuaConditional_tag]. |
| 179 |
|
| 180 |
Third, the same gate can be repeated with a |loop|: it is the same thing |
| 181 |
as a conditional, and the gate's arguments are also passed to it. The |
| 182 |
gate will be called repeatedly as long as the loop is true. When a gate |
| 183 |
is repeated, it passes its returned arguments to itself, and before |
| 184 |
that to the loop. It is as if the same gate had been added several times |
| 185 |
in a row to the same l-gate. |
| 186 |
See {loop in TeX} [see TeXLoop_tag] |
| 187 |
and {loop in Lua} [see LuaLoop_tag]. |
| 188 |
|
| 189 |
Fourth, |loopuntil| is another kind of loop: the gate is repeated as |
| 190 |
long as it is false; also, |loopuntil| is evaluated after the gate, so |
| 191 |
the latter is executed at least once. If a gate has both |loop| and |
| 192 |
|loopuntil|, the latter is ignored. |
| 193 |
See {loop-until in TeX} [see TeXLoopUntil_tag] |
| 194 |
and {loop-until in Lua} [see LuaLoopUntil_tag]. |
| 195 |
|
| 196 |
Finally, in Lua a gate may also have an |iterator|; to put it simply, |
| 197 |
it mimicks a "for" loop; for instance, a gate may be called with a |
| 198 |
table as its argument but actually be executed on every entry, if the |
| 199 |
|iterator| is the "pairs" function. |
| 200 |
See {iterator in Lua} [see LuaIterator_tag] for details. |
| 201 |
|
| 202 |
Actually, a gate doesn't have only one status, one conditional, etc. |
| 203 |
Rather, it has one global set of conditions, and one local set of |
| 204 |
conditions for each l-gate where it appears (note that if a gate appears |
| 205 |
several time in the same l-gate, it has only one local set of conditions). |
| 206 |
The global conditions are examined whenever the gate is encountered, |
| 207 |
either when it is called directly with |execute| or when it is called |
| 208 |
by an l-gate; local conditions are examined in the latter case only. |
| 209 |
|
| 210 |
It is important to understand the relative order of evaluation of the |
| 211 |
conditions, and how global and local conditions interact. Suppose a |
| 212 |
gate is called directly with |execute|; then the following happens: |
| 213 |
first, the gate's global status is checked; if |ajar| or |skip|, it is |
| 214 |
set back to |close| and |open| respectively; if the original status |
| 215 |
allows the execution of the gate (i.e. the status was |open| or |ajar|), |
| 216 |
the gate's global conditional is then evaluated; if it is true, and the |
| 217 |
gate has neither |loop| nor |loopuntil|, the gate is executed once; |
| 218 |
otherwise, it is repeated as long as |loop| is true or as long as |
| 219 |
|loopuntil| is false. Note that even if the gate is executed several |
| 220 |
times, the status and conditional aren't reevaluated, only the loops. |
| 221 |
The same is true with |iterator| in Lua. |
| 222 |
|
| 223 |
Suppose now the gate occurs in an l-gate. Then the same evaluation |
| 224 |
happens, this time with the local values for that given l-gate. If the |
| 225 |
gate is deemed good for execution, or rather, each time it is executed |
| 226 |
(if a loop calls it several time), the global values are evaluated again |
| 227 |
as we've just seen. In other words, when a gate occurs in an l-gate, |
| 228 |
its global conditions are examined if and only if the local conditions |
| 229 |
allows the execution of the gate, and each time they prompt it. This |
| 230 |
means for instance that if a gate is globally |ajar| and is encountered |
| 231 |
in an l-gate where it's local status is |close|, its global status will |
| 232 |
not be reverted to |open| because it will simply not be evaluated (of |
| 233 |
course, it can be evaluated elsewhere). |
| 234 |
|
| 235 |
This may seem a bit complicated, but actually situations where you have |
| 236 |
to specify both local and global conditions are rare (global status is |
| 237 |
always a bit dangerous, because all the instances of the gate are affected, |
| 238 |
which might not be expected if somebody reuses an existing gate); also, |
| 239 |
you may very well |open| and |close| a gate for whatever reasons, but |
| 240 |
if you use |ajar| and |skip| it means you're controlling it quite tightly |
| 241 |
and you probably won't use a conditional too. |
| 242 |
|
| 243 |
|
| 244 |
-- The shorthand notation -- |
| 245 |
|
| 246 |
Defining gates, adding them to l-gates, and setting their conditions, |
| 247 |
can be done with actions, as explained above. But that can be tedious, |
| 248 |
because when you're building a big macro (a big l-gate, rather), it's |
| 249 |
hard to see the larger picture: you're adding gates one after the other, |
| 250 |
and you can't readily figure out what the big l-gate looks like. It's |
| 251 |
as if you were looking at a house brick by brick without being able to |
| 252 |
take a few steps back and consider the whole building. |
| 253 |
|
| 254 |
But entire gates, replete with subgates down to whatever level, and |
| 255 |
specified for all conditions, can be created without ever mentioning any |
| 256 |
action. The overall architecture can be preserved, because gates are |
| 257 |
defined and added to an l-gate at once. It is a bit like writing a big |
| 258 |
macro or function, except you add a tag to each chunk of code, for further |
| 259 |
reference. The shorthand notation isn't explained further here, because |
| 260 |
it depends on the implementation and works very differently in TeX |
| 261 |
and Lua: see |
| 262 |
{TeX shorthand notation} [see TeXShorthand_tag] and |
| 263 |
{Lua shorthand notation} [see LuaShorthand_tag]. |
| 264 |
|
| 265 |
|
| 266 |
-- Gate families -- |
| 267 |
|
| 268 |
Since gates are designed to be hacked, it is all the more convenient if |
| 269 |
they have simple, descriptive names. Of course, you can call a gate |
| 270 |
"insftn", but "InsertFootnotes", or any other readable name, is definitely |
| 271 |
better; a user can then easily browse gates and find what s/he's |
| 272 |
looking for. |
| 273 |
|
| 274 |
However, such significant names increase the risk that two gates bear |
| 275 |
the same name and clash. This is avoided by creating gate families. A |
| 276 |
family is simply a prefix added to a name's gate, so that a gate whose |
| 277 |
name is "MyGate" is actually called "MyFamily:MyGate". But family are |
| 278 |
associated with calling commands in TeX and tables in Lua, so that when |
| 279 |
a gate is mentioned without its family, it is automatically added. For |
| 280 |
instances the \gates command in TeX and the "gates" table in Lua are |
| 281 |
associated with the family called "gates", so the name of a gate manipulated |
| 282 |
with them is actually "gates:<name>". |
| 283 |
|
| 284 |
You can define a new family, associated with a command or table, with |
| 285 |
the |new| action; gates manipulated by that command or table will then |
| 286 |
have a real name which includes the family. Thus, their apparent names |
| 287 |
can be identical to other gates, as long as they belong to other families. |
| 288 |
|
| 289 |
As said above, a gate's family is supplied when it is missing from the |
| 290 |
gate's name. This means conversely that if the family is specified in |
| 291 |
a gate's name, it is not added. So, if you mention gate "MyFam:MyGate", |
| 292 |
the family associated with the command or table where the gate is |
| 293 |
mentioned isn't specified again. You can thus very well use gates from |
| 294 |
other families without having to rely on the associated command or |
| 295 |
table. In other words, there's no hidden mechanism behind families: |
| 296 |
they're just prefixes added to a gate's name, and the automatic addition |
| 297 |
of a family is just an examination of a gate's name. |
| 298 |
|
| 299 |
Although a command or table must be declared with |new| to work properly, |
| 300 |
families themselves don't require that. Gate "MyFam:MyGate" can be used |
| 301 |
even if "MyFam" hasn't been declared (and the declaration can also take |
| 302 |
place later, so that the family is associated with a command or table). |
| 303 |
In other words, the command/table-family association is just a convenient |
| 304 |
way to make the names of your gates unique without thinking about it, |
| 305 |
but you can also think about it and use an explicit family prefix. |
| 306 |
See {families in TeX} [see TeXFamilies_tag] |
| 307 |
and {families in Lua} [see LuaFamilies_tag] |
| 308 |
|
| 309 |
|
| 310 |
-- If you get lost -- |
| 311 |
|
| 312 |
Gates can be quite complex. For instance, the big l-gate that creates |
| 313 |
section headings in this document is made of 16 subgates and subsubgates |
| 314 |
(as I am writing this, at least). The main function of the Interpreter |
| 315 |
package used to turn the source of this document into proper TeX is made |
| 316 |
of more than 25 subgates (not counting repeated ones), with 7 levels |
| 317 |
between the top l-gate and the most deeply embedded m-gate. |
| 318 |
|
| 319 |
There are a few actions to make things clearer. First, you can know |
| 320 |
whether a given gate is an m- or l-gate with |type|, which returns 1 |
| 321 |
(for m-gates), 2 (for l-gates), or 0 (if the name you passed isn't a |
| 322 |
gate). Similarly, a gate's status (either global or local to some l-gate) |
| 323 |
can be queried with the |status| action, which returns 1, 2, 3 or 4, |
| 324 |
depending on whether the gate's status is open, ajar, skip or close, |
| 325 |
and 0 if there is no gate with the given name. |
| 326 |
|
| 327 |
You can loop over all the subgates in an l-gate with |subgates|, and |
| 328 |
execute some code for each subgate. |
| 329 |
|
| 330 |
The family associated with a command or table can be queried by the |
| 331 |
|family| action. |
| 332 |
|
| 333 |
Finally, gates can be explored more thoroughly with |show| and |trace|. |
| 334 |
The |show| action prints (on the terminal and log file) a gate's name, |
| 335 |
type, and global conditions (the loops are omitted if not specified); |
| 336 |
if the gate is an l-gate, the same happens with its subgates, except |
| 337 |
local conditions are shown; and a subgate is an l-gate itself, the |
| 338 |
process goes on. Subgates are displayed in a manner similar to the TeX |
| 339 |
shorthand notation. |
| 340 |
|
| 341 |
The |trace| action shows gates when they are encountered: it is signalled |
| 342 |
whether they are executed or not (and why), and possibly the arguments |
| 343 |
passed to them are mentioned too. Again, subgates to an l-gate are |
| 344 |
marked as such. |
| 345 |
|
| 346 |
See {getting lost in TeX} [see TeXLost_tag] |
| 347 |
and {getting lost in Lua} [see LuaLost_tag] |
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
Gates in TeX :TeXUser_tag |
| 354 |
============ |
| 355 |
|
| 356 |
This part of the documentation explains how gates work in TeX. |
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
Loading and using Gates |
| 361 |
~~~~~~~~~~~~~~~~~~~~~~~ |
| 362 |
|
| 363 |
The way to load Gates depends on your format. In plain TeX, you use: |
| 364 |
|
| 365 |
\input gates |
| 366 |
|
| 367 |
In LaTeX: |
| 368 |
|
| 369 |
\usepackage{gates} |
| 370 |
|
| 371 |
And in ConTeXt: |
| 372 |
|
| 373 |
\usemodule[gates] |
| 374 |
|
| 375 |
Actions in TeX aren't executed with control sequences, but with a |
| 376 |
calling command (by default, \gates), followed by an action's name, |
| 377 |
followed by a space: |
| 378 |
|
| 379 |
\gates action {...} |
| 380 |
|
| 381 |
(Of course, the space can be the end of a line.) |
| 382 |
|
| 383 |
Note that loading Gates in TeX doesn't automatically load the Lua |
| 384 |
counterpart even in LuaTeX; in other words, Gates in Lua should be |
| 385 |
independantly loaded. |
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
Definition and execution :TeXDefinition_tag |
| 390 |
~~~~~~~~~~~~~~~~~~~~~~~~ |
| 391 |
|
| 392 |
Here it is shown how gates are created, concatenated, executed, and how |
| 393 |
arguments are passed between them. |
| 394 |
|
| 395 |
|
| 396 |
-- Defining gates -- |
| 397 |
|
| 398 |
Let's try an extremely simple example. You want to define a macro which, |
| 399 |
when given a number, adds 3 to it, multiplies everything by 2, and prints |
| 400 |
the result. How fascinating. Here's how you'd do it with gates. First |
| 401 |
you define your m-gates with |def|: |
| 402 |
|
| 403 |
\gates def {add} [1] {% |
| 404 |
\gates return {#1+3} |
| 405 |
} |
| 406 |
\gates def {multiply} [1] {% |
| 407 |
\gates return {(#1)*2} |
| 408 |
} |
| 409 |
\gates def {print} [1] {% |
| 410 |
\the\numexpr#1\relax |
| 411 |
} |
| 412 |
|
| 413 |
The number of argument (up to nine) is given after the name, between |
| 414 |
brackets, and can be omitted if the gate takes no argument. The |return| |
| 415 |
action is discussed more thoroughly below [see Return_tag]; just note |
| 416 |
for the moment that there is no need to add a comment at the end of the |
| 417 |
line to avoid spurious space, because material after a |return| is |
| 418 |
gobbled (so watch your \fi's!). |
| 419 |
|
| 420 |
(Instead of |def|, we could use the |edef| action; the difference is |
| 421 |
the same as between TeX's \def and \edef. There is no \gdef/\xdef |
| 422 |
variants, because operations on gates are always globals.) |
| 423 |
|
| 424 |
Now those gates should be added to an l-gate. Such a gate is declared |
| 425 |
with |list|, and an optional number of arguments can be specified too. |
| 426 |
|
| 427 |
\gates list {operation} [1] |
| 428 |
|
| 429 |
Finally we |add| the m-gates to the l-gate: |
| 430 |
|
| 431 |
\gates add {add, multiply, print} {operation} |
| 432 |
|
| 433 |
The |add| action can take one or more gates, separated by commas. Examples |
| 434 |
of |add| with specified position can be found below [see TeXAddWithPosition_tag]. |
| 435 |
|
| 436 |
|
| 437 |
-- Executing gates -- |
| 438 |
|
| 439 |
And that's it, our small \BigMacro is done. We can call it, with 4 as |
| 440 |
argument, for instance, and it will print 14; to do so, we use the |
| 441 |
|execute| action: |
| 442 |
|
| 443 |
\gates execute {operation}{4} |
| 444 |
|
| 445 |
Note that m-gates can be executed too, although *that* is a convoluted |
| 446 |
way to call a macro; in this case, do not worry about the return value, |
| 447 |
it simply vanishes. For instance, the following: |
| 448 |
|
| 449 |
\gates execute {print}{5+2} |
| 450 |
|
| 451 |
will produce "7". If we'd call "multiply" rather than "print", nothing |
| 452 |
would have happened, since it simply returns something, which doesn't |
| 453 |
make sense outside an l-gate. |
| 454 |
|
| 455 |
Execution can be called more directly as: |
| 456 |
|
| 457 |
\gates operation {4} |
| 458 |
|
| 459 |
I.e. instead of a gate action, you use the name of a gate, and that's |
| 460 |
equivalent to calling |execute| with that gate. However, this can be |
| 461 |
done if and only if the gate you want to call doesn't have the same |
| 462 |
name as an action; for instance, you can't do that with the "add" gate, |
| 463 |
because there exists an action called |add|, so you have to use |execute| |
| 464 |
instead. |
| 465 |
|
| 466 |
:TeXAddWithPosition_tag |
| 467 |
Now suppose we'd like to add a substraction after the multiplication -- |
| 468 |
that is, suppose we're a user who wants to add his or her touch to |
| 469 |
\BigMacro. Nothing simpler: |
| 470 |
|
| 471 |
\gates def {substract} [1] {% |
| 472 |
\gates return {#1-3} |
| 473 |
} |
| 474 |
\gates add {substract}[after multiply]{operation} |
| 475 |
|
| 476 |
And now "operation" returns 11 when fed 4. This was done by simply |
| 477 |
adding an optional argument to the |add| operation; this specifies where |
| 478 |
the new gate(s) should be added in the l-gate: by default, it is the |
| 479 |
end of the list, but you can say "first" to put the gate(s) at the |
| 480 |
beginning or "before <name>" or "after <name>" to make the insertion |
| 481 |
before or after the gate called <name>; instead of a name, you can also |
| 482 |
use "first" or "last" to denote the first and last gates of the list, |
| 483 |
so here we could have used "before last" instead of "after multiply". |
| 484 |
|
| 485 |
You can also |remove| a gate from a list. After: |
| 486 |
|
| 487 |
\gates remove {substract}{operation} |
| 488 |
|
| 489 |
the l-gate "operation" is now the same as before. |
| 490 |
|
| 491 |
|
| 492 |
-- Handling arguments -- :Return_tag |
| 493 |
|
| 494 |
One crucial properties of gates is that they pass arguments between |
| 495 |
them. This was already illustrated by our code above: "operation" takes |
| 496 |
one argument, which is passed to "add", "multiply" and "print"; or |
| 497 |
rather, it is passed to "add", and that gate returns an argument which |
| 498 |
is passed to "multiply", and again to "print". |
| 499 |
|
| 500 |
Arguments work as follows: a gate called with |execute| should be |
| 501 |
followed by as many arguments as it was declared with, just like any |
| 502 |
other macro in TeX. An l-gate passes its arguments to its first subgate; |
| 503 |
and for two consecutive gates, the second receives what the first |
| 504 |
returns. |
| 505 |
|
| 506 |
To return arguments, you use the |return| action, as illustrated above. |
| 507 |
It expects the same number of arguments as the gate where it appears. |
| 508 |
For instance, "add" was declared with one argument, so when it calls |
| 509 |
|return|, one argument is expected. But one might want to return a |
| 510 |
different number of arguments; in this case, one can use |return0|, |
| 511 |
|return1|, |return2|, etc., up to |return9|. Also, the |return!| action |
| 512 |
takes one big argument containing an indefinite number of arguments to |
| 513 |
be returned; for instance: |
| 514 |
|
| 515 |
\gates return2 {one}{two} |
| 516 |
\gates return! {{one}{two}} |
| 517 |
|
| 518 |
are equivalent, and they're also equivalent to a simple |return| with |
| 519 |
two arguments in a gate declared as taking two arguments. |
| 520 |
|
| 521 |
Since l-gates execute no code, you can't call any version of |return| |
| 522 |
with them; but they automatically return whatever was returned by their |
| 523 |
last gate, and that is passed whatever follows the l-gate. |
| 524 |
|
| 525 |
Until now, we have implicitly assumed that all gates in the same l-gate |
| 526 |
take the same number of arguments. That is not necessary: two gates |
| 527 |
with a different number of arguments can occur in the same l-gate; |
| 528 |
consequently, a gate doesn't need to take the same number of arguments |
| 529 |
as the l-gate it appears in. For instance, you can very well declare |
| 530 |
an l-gate with 3 arguments, and add to it an m-gate with 2 arguments |
| 531 |
and another one with 4. Let's do it, and see what happens: |
| 532 |
|
| 533 |
\gates list {mylist} [3] |
| 534 |
\gates def {macro1} [2] {% |
| 535 |
\immediate\write16{1: #1. 2: #2.}} |
| 536 |
\gates def {macro2} [4] {% |
| 537 |
\immediate\write16{1: #1. 2: #2. 3: #3. 4: #4.}} |
| 538 |
\gates add {macro1, macro2} {mylist} |
| 539 |
\gates execute {mylist} {one}{two}{three} |
| 540 |
|
| 541 |
This will print: |
| 542 |
|
| 543 |
1: one. 2: two |
| 544 |
1: one. 2: two. 3: three. 4: . |
| 545 |
|
| 546 |
The first two arguments of "mylist" are passed to "macro1", and the third |
| 547 |
is simply ignored. Then the three arguments are passed to "macro2", along |
| 548 |
with a empty fourth one. In other words, the number of arguments is |
| 549 |
adjusted so that every gate receives what it needs and nothing more. |
| 550 |
|
| 551 |
But haven't we said just above that a gate receives what the previous |
| 552 |
one returns? Since "macro1" returns nothing, shouldn't "macro2" receive |
| 553 |
four empty arguments? Things are actually a bit more complicated to |
| 554 |
explain, but simpler to use: given two consecutive gates A and B, the |
| 555 |
arguments passed to B are whatever A returns, plus additional arguments |
| 556 |
taken from those that A received if necessary (i.e. if B takes more |
| 557 |
arguments than A has returned). To put it differently, when a gate returns |
| 558 |
*n* arguments, they simply replace the first *n* arguments of the current |
| 559 |
arguments, which are passed to the next gate. In our example, "macro1" |
| 560 |
returned nothing, so the arguments that it received were restored and |
| 561 |
passed to "macro2". Here is another example: |
| 562 |
|
| 563 |
\gates list {mylist} [3] |
| 564 |
\gates def {macro1} [2] {% |
| 565 |
\immediate\write16{1: #1. 2: #2.}% |
| 566 |
\gates return1 {ONE}} |
| 567 |
\gates def {macro2} [4] {% |
| 568 |
\immediate\write16{1: #1. 2: #2. 3: #3. 4: #4.}% |
| 569 |
\gates return {first}{second}{third}{fourth}} |
| 570 |
\gates def {macro3} [4] {% |
| 571 |
\immediate\write16{1: #1. 2: #2. 3: #3. 4: #4.}} |
| 572 |
\gates add {macro1, macro2, macro3} {mylist} |
| 573 |
\gates execute {mylist} {one}{two}{three} |
| 574 |
|
| 575 |
And this prints: |
| 576 |
|
| 577 |
1: one. 2: two. |
| 578 |
1: ONE. 2: two. 3: three. 4: . |
| 579 |
1: first. 2: second. 3: third. 4: fourth. |
| 580 |
|
| 581 |
It works as follows: "macro1" uses the first two arguments from "mylist", |
| 582 |
and returns one; "macro2" thus takes that argument, plus the second, |
| 583 |
third and fourth original arguments (the fourth being empty, since |
| 584 |
"mylist" takes only three); four argument are returned, which is all |
| 585 |
"macro3" needs, so there is no empty argument. |
| 586 |
|
| 587 |
As said above, l-gates automatically return what their last gates return; |
| 588 |
more accurately, an l-gate declared with *n* arguments returns the same |
| 589 |
number of arguments, those being taken from what the last gate returns |
| 590 |
plus additional arguments restored as we've just seen. In our example, |
| 591 |
"mylist" will return "first", "second" and "third" (it takes, and thus |
| 592 |
returns, three arguments), because "macro3" returned nothing and the |
| 593 |
arguments that it received from "macro2" are restored. (Actually, |
| 594 |
returning doesn't mean anything here because "mylist" doesn't appear |
| 595 |
in a l-gate itself.) |
| 596 |
|
| 597 |
This behavior is quite convenient, because it means that when a gate |
| 598 |
doesn't modify an argument, it doesn't have to bother to return it. |
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
Controlling gates |
| 603 |
~~~~~~~~~~~~~~~~~ |
| 604 |
|
| 605 |
Here it is explained how to change a gate's conditions. |
| 606 |
|
| 607 |
-- Status -- :TeXStatus_tag |
| 608 |
|
| 609 |
The global status of a gate (i.e. its behavior wherever it is encountered) |
| 610 |
can be set with the following actions: |open!|, |ajar!|, |skip!| and |
| 611 |
|close!|, as in: |
| 612 |
|
| 613 |
\gates ajar! {mygate, myothergate} |
| 614 |
|
| 615 |
This also illustrates that you can set the status of several gates at |
| 616 |
once, separating them with commas (spaces are ignored). |
| 617 |
|
| 618 |
The local status (i.e. the behavior of a gate in a given l-gate) is |
| 619 |
set with the same action without an exclamation mark, followed by the |
| 620 |
l-gate where you want to specify the status: |
| 621 |
|
| 622 |
\gates open {mygate, myothergate}{mylist} |
| 623 |
|
| 624 |
But you can also use "before <gate>" and "after <gate>", meaning that |
| 625 |
the status of all gates before (resp. after) <gate> in the l-gate will |
| 626 |
be set accordingly. For instance: |
| 627 |
|
| 628 |
\gates close {before mygate}{mylist} |
| 629 |
|
| 630 |
As an example of a status-controlled gate, let's consider the problem |
| 631 |
of automatically removing the indentation of a paragraph. The usual |
| 632 |
solution is: |
| 633 |
|
| 634 |
\everypar={{\setbox0=\lastbox}} |
| 635 |
|
| 636 |
The \everypar token list is inserted every time TeX begins a paragraph; |
| 637 |
assigning \lastbox to a box register removes it from the list under |
| 638 |
construction: here the indentation box is thus removed from the paragraph; |
| 639 |
the assignment is done in a group so box 0 remains unaffected. The |
| 640 |
problem is that other resources might want to use \everypar; if the |
| 641 |
exploitation of the token list is always so direct, one resource might |
| 642 |
wipe another. With gates, the solution is to plant an l-gate in \everypar; |
| 643 |
then you can add whatever gate to it without disturbing the other gates |
| 644 |
possibly there too: |
| 645 |
|
| 646 |
\gates list {everypar} |
| 647 |
\gates def {noindent}{{\setbox0=\lastbox}} |
| 648 |
\gates add {noindent}{everypar} |
| 649 |
\everypar={\gates execute {everypar}} |
| 650 |
|
| 651 |
But we were interested in status. As is, all paragraphs will be unindented, |
| 652 |
because "noindent" will be executed for each paragraph. Instead, we |
| 653 |
should close it with |
| 654 |
|
| 655 |
\gates close {noindent}{everypar} |
| 656 |
|
| 657 |
and situations triggering an unindented paragraph (for instance, a |
| 658 |
section header) should call |
| 659 |
|
| 660 |
\gates ajar {noindent}{everypar} |
| 661 |
|
| 662 |
so that "noindent" will make its job once, and then close. |
| 663 |
|
| 664 |
|
| 665 |
-- Conditional -- :TeXConditional_tag |
| 666 |
|
| 667 |
Status is useful, but it suffers one flaw: you have to set it yourself. |
| 668 |
That's no problem when you're handling a local situation like the |
| 669 |
previous one; but things might a be a bit more far-reaching. You might |
| 670 |
want X to influence Y, and X and Y might not be related at all; also, |
| 671 |
X might change more than once. |
| 672 |
|
| 673 |
For more flexible control, gates can depend on a |conditional|, for |
| 674 |
instance (illustrating both global and local setting): |
| 675 |
|
| 676 |
\gates conditional! {mygate}{\ifSomething} |
| 677 |
\gates conditional {gate1, gate2}{mylist}{\ifSomethingElse} |
| 678 |
|
| 679 |
Now, "mygate" will be executed only when \ifSomething is true, just |
| 680 |
like "gate1" and "gate2" in "mylist" will be executed in "mylist" when |
| 681 |
\ifSomethingElse is true. The situations where conditionals can be put |
| 682 |
to use are countless. A preface in a book, for instance, will certainly |
| 683 |
make some \ifSpecialChapter conditional true, and many details will |
| 684 |
depend on that conditional: e.g. unnumbered section headings, page |
| 685 |
number in roman numerals, etc. If those are implemented with gates, |
| 686 |
setting \ifSpecialChapter as the conditional for those gates will make |
| 687 |
them depend on the larger picture (you could use status too, but the |
| 688 |
conditional is more powerful because many different things can depend |
| 689 |
on it, not only gates). |
| 690 |
|
| 691 |
What constitutes a valid conditional? Technically, whatever fits texapi's |
| 692 |
\ifexpression. Indeed, Gates is written with texapi, and conditionals |
| 693 |
rely internally on \ifexpression. What then fits \ifexpression? First, |
| 694 |
any traditional TeX conditional, no matter whether it's a primitive |
| 695 |
like \ifhmode or \ifcat or a macro defined with \newif. Second, |
| 696 |
argument-taking conditionals, i.e. macros working like |
| 697 |
|
| 698 |
\ifXXX {<true>}{<false>} |
| 699 |
|
| 700 |
can be used too (texapi itself defines a good deal of such conditionals). |
| 701 |
In both cases, what should be set in |conditional| is the test itself, |
| 702 |
not the <true> and <false> parts. For instance, with \ifSomething and |
| 703 |
\ifSomethingElse above, the true branch and the "\else ... \fi" |
| 704 |
continuation were left out of the picture. If you used, say, \ifnum, |
| 705 |
you'd specify something like (note the necessary space): |
| 706 |
|
| 707 |
\gates conditional {mygate}{mylist}{\ifnum0=1 } |
| 708 |
|
| 709 |
With a texapi conditional you could use: |
| 710 |
|
| 711 |
\gates conditional {mygate}{mylist}{\ifstring{foo}{bar}} |
| 712 |
|
| 713 |
(Here of course we have specified stupid conditionals which are always |
| 714 |
false; more useful examples can be found below). |
| 715 |
|
| 716 |
But \ifexpression (hence |conditional|) also allows using simple logical |
| 717 |
operators: "&" means *and*, "|" means *or*, "-" means *not*, and |
| 718 |
subexpressions can be created with braces. For instance, the following |
| 719 |
two conditionals are equivalent and are true if the absolute value of |
| 720 |
N is smaller than 100 (the space following an operator is ignored): |
| 721 |
|
| 722 |
\ifnum N > -100 & \ifnum N < 100 |
| 723 |
-{\ifnum N < -99 | \ifnum N > 99 } |
| 724 |
|
| 725 |
Pretty complex expressions can thus be assigned to a gate's conditional. |
| 726 |
But things are better yet: conditionals take arguments, the same as the |
| 727 |
gates they control, so that the test can depend not only on external |
| 728 |
conditions but on what a gate receives. For instance, with: |
| 729 |
|
| 730 |
\gates conditional {mygate}{mylist}{\ifnum#1>0 } |
| 731 |
|
| 732 |
"mygate" will be executed only when its first argument is positive. |
| 733 |
Another example: |
| 734 |
|
| 735 |
\gates conditional! {mygate}{-\ifstring{#1}{#2}} |
| 736 |
|
| 737 |
Here, "mygate" will be executed only when its first two arguments differ, |
| 738 |
no matter in which list "mygate" appear, since the global conditional |
| 739 |
is set here. |
| 740 |
|
| 741 |
If you don't want a gate to depend on a conditional anymore, simply |
| 742 |
declare something like: |
| 743 |
|
| 744 |
\gates conditional! {mygate}{\iftrue} |
| 745 |
|
| 746 |
|
| 747 |
-- Loop -- :TeXLoop_tag |
| 748 |
|
| 749 |
Third type of control: the execution of a gate can be repeated as long |
| 750 |
as some condition is true. To do so, you use the |loop| action (with |
| 751 |
an exclamation mark if you want to set the global loop). It takes the |
| 752 |
same stuff as |conditional|, and the gate's arguments are passed to it |
| 753 |
too. If the material evaluates to true, the gate is executed, then the |
| 754 |
material is reevaluated, and if it is true again the gate is executed |
| 755 |
again, and so on and so forth, so obviously something must happen so |
| 756 |
that the iteration stops. |
| 757 |
|
| 758 |
When a gate repeats the arguments it returns are passed back to |
| 759 |
itself, and before that to the loop. Here is an example: |
| 760 |
|
| 761 |
\gates def {myloop} [1] {% |
| 762 |
\immediate\write16{\the\numexpr#1}% |
| 763 |
\gates return {#1+1}} |
| 764 |
\gates loop! {myloop} {\ifnum\numexpr#1<5 } |
| 765 |
\gates execute {myloop}{1} |
| 766 |
|
| 767 |
This will print numbers from 1 to 4. Once 4 is printed, "myloop" returns |
| 768 |
5 ("1+1+1+1+1", really), so the conditional controlling the loop isn't |
| 769 |
true anymore and the iteration stops. If this happened in an l-gate, |
| 770 |
the return value of "myloop" would be passed to the next gate. |
| 771 |
|
| 772 |
When a gate shouldn't be controlled by a loop anymore, you can use |
| 773 |
|noloop| (with or without an exclamation mark): |
| 774 |
|
| 775 |
\gates noloop! {mygate} |
| 776 |
|
| 777 |
|
| 778 |
-- Loop until -- :TeXLoopUntil_tag |
| 779 |
|
| 780 |
You can specify another type of loop with |loopuntil|: it works like |
| 781 |
|loop|, except the gate is repeated as long as the material evaluates |
| 782 |
to false. Also, the evaluation takes place after the gate is executed, |
| 783 |
which means that the gate is always executed at least once; arguments |
| 784 |
are repeatedly passed to the gate and the conditional as with |loop|, |
| 785 |
except |loopuntil| always receives the return values of the gate it |
| 786 |
controls (whereas on the first evaluation |loop| uses the arguments |
| 787 |
passed to gate, not what it returns). Here's the previous gate rewritten |
| 788 |
with |loopuntil|; it will work slightly differently: |
| 789 |
|
| 790 |
\gates def {myloop} [1] {% |
| 791 |
\immediate\write16{\the\numexpr#1}% |
| 792 |
\gates return {#1+1}} |
| 793 |
\gates loopuntil! {myloop} {\ifnum\numexpr#1>4 } |
| 794 |
\gates execute {myloop}{1} |
| 795 |
|
| 796 |
(The difference with the previous version is that here "myloop" will be |
| 797 |
executed at least once, even if a number larger than 4 is passed to it. |
| 798 |
Also, it is important to use "\ifnum\numexpr#1>4" and not "\ifnum\numexpr#1=5"; |
| 799 |
the latter will work if "myloop" is always executed with a number smaller |
| 800 |
than 5, but it will enter an infinite loop otherwise, since the condition |
| 801 |
will never be true.) |
| 802 |
|
| 803 |
If you want a gate to stop being controlled by |loopuntil|, use |
| 804 |
|noloopuntil|. |
| 805 |
|
| 806 |
Note that if both |loop| and |loopuntil| are specified, the latter is |
| 807 |
ignored. |
| 808 |
|
| 809 |
|
| 810 |
|
| 811 |
The shorthand notation :TeXShorthand_tag |
| 812 |
~~~~~~~~~~~~~~~~~~~~~~ |
| 813 |
|
| 814 |
The shorthand notation allows you to define gates (e.g. l-gates with |
| 815 |
subgates), including conditions, in one go, without having to call any |
| 816 |
gate actions. This makes complex code much more readable. |
| 817 |
|
| 818 |
We've already seen the implicit call to |execute| by using a gate's |
| 819 |
name instead of an action. You can also define m- and l- gates more |
| 820 |
directly as follows: |
| 821 |
|
| 822 |
\gates def {mygate} [2] {...} |
| 823 |
\gates list {mylist} [2] |
| 824 |
|
| 825 |
are equivalent to |
| 826 |
|
| 827 |
\gates [mygate] [2] {...} |
| 828 |
\gates (mylist) [2] |
| 829 |
|
| 830 |
and the gate's name can be surrounded by space, it is trimmed away. (The |
| 831 |
examples in this section all use this shorthand, but they would be still |
| 832 |
valid with the explicit |def| and |list| instead.) Second, following the |
| 833 |
number of arguments (or the gate's name if the gate takes no argument), |
| 834 |
you can optionally specify global conditions by using "?" followed by a |
| 835 |
list of "key = value" pairs, where the keys are "status", "conditional", |
| 836 |
"loop" and "loopuntil"; for "status", the value should be one of "open", |
| 837 |
"ajar", "skip" or "close", and for the other conditions it should be a |
| 838 |
conditional as seen above. For instance, here's the creation of an m-gate |
| 839 |
with status "close" and some loop: |
| 840 |
|
| 841 |
\gates [mygate] [2] ?{status = close, |
| 842 |
loop = \ifnum#1<5 } {...} |
| 843 |
|
| 844 |
This is equivalent to: |
| 845 |
|
| 846 |
\gates [mygate] [2] {...} |
| 847 |
\gates close! {mygate} |
| 848 |
\gates loop! {mygate}{\ifnum#1<5 } |
| 849 |
|
| 850 |
Note that the key is trimmed, as is the value if the key is "status", |
| 851 |
so space can be used to make things readable. In the case of a conditional |
| 852 |
or loop's value, it isn't trimmed, because space can be important, as |
| 853 |
in our example here where it delimits the number "5". The space on the |
| 854 |
left is insignificant, though. |
| 855 |
|
| 856 |
And here comes the most interesting shorthand: when declaring an l-gate, |
| 857 |
you can define and add subgates at once by declaring them just after the |
| 858 |
l-gate (and its optional number of arguments and/or conditions, if any), |
| 859 |
using the same notations as with implicit definitions: "[mygate]" or |
| 860 |
"(mylist)", optionally followed by the number of arguments and/or the |
| 861 |
conditions, and with a definition in case of an m-gate. The only difference |
| 862 |
is that the conditions thus specified, if any, are the local ones relative |
| 863 |
to the l-gate under construction. In other words, the following: |
| 864 |
|
| 865 |
\gates (mylist) [2] |
| 866 |
[mygate] [2] ?{status = ajar} {...} |
| 867 |
(myotherlist) [1] ?{loop = \ifnum#1<5 } |
| 868 |
|
| 869 |
is equivalent to the much more verbose |
| 870 |
|
| 871 |
\gates (mylist) [2] |
| 872 |
\gates [mygate] [2] {...} |
| 873 |
\gates (myotherlist) [1] |
| 874 |
\gates add {mygate, myotherlist}{mylist} |
| 875 |
\gates ajar {mygate}{mylist} |
| 876 |
\gates loop {myotherlist}{mylist}{\ifnum#1<5 } |
| 877 |
|
| 878 |
The shorthand notation allows you to see at once that "mylist" contains |
| 879 |
"mygate" and "myotherlist". But then, the latter is an l-gate too, so |
| 880 |
it could be nice if we could add subgates to it in the same way; |
| 881 |
well, we can, it suffices to use a dot: |
| 882 |
|
| 883 |
\gates (mylist) [2] |
| 884 |
[mygate] [2] ?{status = ajar} {...} |
| 885 |
(myotherlist) [1] ?{loop = \ifnum#1<5 } |
| 886 |
. [mysubgate] [1] ?{...} {...} |
| 887 |
|
| 888 |
Here "mysubgate" will be added to "myotherlist" instead of "mylist", and |
| 889 |
the conditions, if any, will be the local conditions relative to |
| 890 |
"myotherlist", not "mylist". Now what if "myotherlist" contains an l-gate |
| 891 |
"mysublist" and you want to add subgates to that one? Well, you use two |
| 892 |
dots, and so on and so forth (I leave argument numbers and conditions |
| 893 |
aside to make things simpler): |
| 894 |
|
| 895 |
\gates (mylist) |
| 896 |
[mygate] {...} |
| 897 |
(myotherlist) |
| 898 |
. [mysubgate] {...} |
| 899 |
. (mysublist) |
| 900 |
. . [mysubsubgate] {...} |
| 901 |
. . (yetanotherlist) |
| 902 |
. . . (pleasestop) |
| 903 |
|
| 904 |
etc., etc. The notation with dots work as follows: if we assume that |
| 905 |
gates are added to l-gates of a given level, then dots denote that level: |
| 906 |
no dot means that the gate is added to the l-gate of level 0, i.e. the |
| 907 |
gate defined with |list| ("mylist" here), and is itself of level 1, one |
| 908 |
dot means that the gate is added to an l-gate of level 1 and is itself |
| 909 |
of level 2 (e.g. "mysubgate" added to "myotherlist"), and so on and so |
| 910 |
forth. Space around the dot is insignificant. The only (obvious) restriction |
| 911 |
is that an l-gate at level *n* isn't available anymore if followed by a |
| 912 |
gate at the same level or lower. This works a bit like a table of contents: |
| 913 |
a subsection can't be added if there isn't a section immediately above |
| 914 |
it. If you want to come back to a gate at another level, just use less |
| 915 |
dots. For instance, if the code above continued with |
| 916 |
|
| 917 |
. . [gateX] {...} |
| 918 |
[gateY] {...} |
| 919 |
|
| 920 |
then "gateX" and "gateY" would be defined and added to "mysublist" and |
| 921 |
"mylist" respectively. Of course, after "gateX", "yetanotherlist" and |
| 922 |
"pleasestop" aren't available for insertion anymore; similarly, after |
| 923 |
"gateY", only "mylist" can host incoming gates, until a new l-gate is |
| 924 |
declared. |
| 925 |
|
| 926 |
It's nice to define subgates and add them to an l-gate at once, but |
| 927 |
sometimes you might want to add a gate that already exists. Using the |
| 928 |
above notation would redefine it. Of course, you can still |add| and |
| 929 |
specify the position, but that's not very convenient. So, besides |
| 930 |
parentheses and square brackets, you can use "<mygate>" to add "mygate" |
| 931 |
without redefining it; you can actually add several gates at once, by |
| 932 |
separating them with commas. Local conditions can then be set as above. |
| 933 |
For instance: |
| 934 |
|
| 935 |
\gates (mylist) |
| 936 |
[gateA] {...} |
| 937 |
<gateB, gateC> ?{status = close} |
| 938 |
(myotherlist) |
| 939 |
. <gateD> |
| 940 |
|
| 941 |
creates l-gate "mylist" with subgates "gateA", "gateB", "gateC" and |
| 942 |
"myotherlist", "gateB" and "gateC" being locally closed, and "myotherlist" |
| 943 |
containing "gateD". This of course requires that "gateB", "gateC" and |
| 944 |
"gateD" already exist. If a gate thus added is an l-gate, the dot notation |
| 945 |
can be used to add subgates to it. |
| 946 |
|
| 947 |
The dot is actually only the default character to signal subgates. You |
| 948 |
can use others by declaring them with |subchar|, as in: |
| 949 |
|
| 950 |
\gates subchar # |
| 951 |
|
| 952 |
The character thus declared do not replace existing ones (this would be |
| 953 |
dangerous) but simply adds a new possibility. For obvious reason, the |
| 954 |
character can't be "[", "(", "<" or "?". |
| 955 |
|
| 956 |
|
| 957 |
|
| 958 |
Gate families :TeXFamilies_tag |
| 959 |
~~~~~~~~~~~~~ |
| 960 |
|
| 961 |
New gate families can be created with the |new| action. |
| 962 |
|
| 963 |
\gates new \MyGates {MyFamily} |
| 964 |
|
| 965 |
Now \MyGates will work exactly like \gates, except that when it manipulates |
| 966 |
gates, they will all have the prefix "MyFamily:" attached to them. For |
| 967 |
instance: |
| 968 |
|
| 969 |
\gates def {mygate}{...} |
| 970 |
\MyGates def {mygate}{...} |
| 971 |
|
| 972 |
defines two different gates, "gates:mygate" and "MyFamily:mygate". |
| 973 |
|
| 974 |
If a family is explicitly given in a gate's name, the family |
| 975 |
associated with the calling command isn't added. The following two |
| 976 |
lines are equivalent, for instance: |
| 977 |
|
| 978 |
\gates def {MyFamily:mygate}{...} |
| 979 |
\MyGates def {mygate}{...} |
| 980 |
|
| 981 |
Of course the shorthand notation allows you to mix families: |
| 982 |
|
| 983 |
\MyGates (MyList) |
| 984 |
(MySubList) |
| 985 |
. [MyGate] {...} |
| 986 |
. [AnotherFamily:AnotherGate] {...} |
| 987 |
|
| 988 |
The family associated with a command can be queried with the |family| |
| 989 |
action. For instance: |
| 990 |
|
| 991 |
\MyGates family |
| 992 |
|
| 993 |
returns "MyFamily". |
| 994 |
|
| 995 |
|
| 996 |
|
| 997 |
|
| 998 |
If you get lost :TeXLost_tag |
| 999 |
~~~~~~~~~~~~~~~ |
| 1000 |
|
| 1001 |
Both |type| and |status| return a number: |type| returns 0 if there is |
| 1002 |
no gate with that name, 1 if it is an m-gate and 2 for an l-gate; |
| 1003 |
|status| returns 0 for a non-existing gate, and 1 to 4 for "open", |
| 1004 |
"ajar", "skip" and "close" respectively; |status| can also be used |
| 1005 |
with an exclamation mark to query global status: |
| 1006 |
|
| 1007 |
\gates status {mygate}{mylist} |
| 1008 |
\gates status! {mygate} |
| 1009 |
|
| 1010 |
The |subgates| action takes a definition as its second argument, which |
| 1011 |
will be executed with "#1" replaced with the subgate's name. For instance, |
| 1012 |
the following shows all the subgates in "mylist", with their types: |
| 1013 |
|
| 1014 |
\gates subgates {mylist}{% |
| 1015 |
\immediate\write16{% |
| 1016 |
#1 \ifnum\gates type {#1}=1 |
| 1017 |
(m-gate)\else (l-gate)\fi}% |
| 1018 |
} |
| 1019 |
|
| 1020 |
The |show| action can be used to display a gate's construction and |
| 1021 |
conditions. It is displayed as the shorthand notation. The |trace| |
| 1022 |
action takes a number and works as follows: if the number is 0, gates |
| 1023 |
of the associated families aren't traced; if it is 1, it is mentionned |
| 1024 |
if they are called (in an l-gate) and executed or not, and why; with |
| 1025 |
2, it works like 1, except the arguments passed to the gates are shown |
| 1026 |
too. |
| 1027 |
|
| 1028 |
|
| 1029 |
|
| 1030 |
Reference manual for TeX :TeXReference_tag |
| 1031 |
======================== |
| 1032 |
|
| 1033 |
All actions are called with \gates, or any other calling command created |
| 1034 |
with |new|, as follows: |
| 1035 |
|
| 1036 |
> \gates <action><space> |
| 1037 |
Executes <action>; if there is a gate called <action>, this is |
| 1038 |
equivalent to "\gates execute {<action>}". The name of the action (or |
| 1039 |
gate) is delimited by a space; depending on <action>, arguments might |
| 1040 |
be required. |
| 1041 |
|
| 1042 |
In what follows, <gate list> denotes a comma-separated list of gates, |
| 1043 |
e.g. "mygate, myothergate, thirdgate"; <gate spec> denotes either a |
| 1044 |
<gate list> or a relative position of the form "before <gate>" or |
| 1045 |
"after <gate>" |
| 1046 |
|
| 1047 |
> add <gate list>[<position>]<l-gate> |
| 1048 |
This adds all the gates in <gate list> to <l-gate>. If <position> is |
| 1049 |
missing, the addition occurs at the end of the l-gate. Otherwise, it |
| 1050 |
should be one of the following: "first", meaning the gates will be |
| 1051 |
added at the beginning of the l-gate; "last", meaning they will be |
| 1052 |
added at the end (so this is similar to no <position>); "before <name>", |
| 1053 |
and the addition will occur before gate <name> in the l-gate (it |
| 1054 |
should of course exist); "after <name>", and the addition will occur |
| 1055 |
after gate <name>. Note that <name> can be "first" or "last", denoting |
| 1056 |
the first and last gates in <l-gate> ("before first" and "after last" |
| 1057 |
are obviously synonymous with "first" and "last"). |
| 1058 |
|
| 1059 |
> ajar <gate spec><l-gate> |
| 1060 |
Sets the local status in <l-gate> to "ajar" for the gates in <gate spec>. |
| 1061 |
|
| 1062 |
> ajar! <gate spec> |
| 1063 |
Sets the global status for the gates in <gate spec> to "ajar". |
| 1064 |
|
| 1065 |
> close <gate spec><l-gate> |
| 1066 |
Sets the local status in <l-gate> to "close" for the gates in <gate spec>. |
| 1067 |
|
| 1068 |
> close! <gate spec> |
| 1069 |
Sets the global status for the gates in <gate spec> to "close". |
| 1070 |
|
| 1071 |
> conditional <gate spec><l-gate><conditional> |
| 1072 |
Sets the local conditional in <l-gate> to <conditional> for the gates |
| 1073 |
in <gate spec>. The conditional should be anything that fits texapi's |
| 1074 |
\ifexpression. |
| 1075 |
|
| 1076 |
> conditional! <gate spec><conditional> |
| 1077 |
Sets the global conditional for the gates in <gate spec> to <conditional>. |
| 1078 |
|
| 1079 |
> copy <gate1><gate2> |
| 1080 |
Defines <gate1> as <gate2> (either an m- or l-gate). The current global |
| 1081 |
status is also copied. If <gate2> is an l-gate, its gate list is also |
| 1082 |
copied, along with the current status of the gates it contains. On the |
| 1083 |
other hand, if <gate2> occurs in some l-gate(s), this information isn't |
| 1084 |
copied. |
| 1085 |
|
| 1086 |
> def <name>[<number of arguments>]<optional conditions>{<definition>} |
| 1087 |
Defines <name> as an m-gate with the given number of arguments and |
| 1088 |
definition. Such an assignment is always global. The number of arguments, |
| 1089 |
ranging from 0 to 9, is optional, in which case the gate takes no |
| 1090 |
argument. In other words, the following lines are equivalent: |
| 1091 |
|
| 1092 |
\gates def {mygate}{<definition>} |
| 1093 |
\gates def {mygate}[0]{<definition>} |
| 1094 |
|
| 1095 |
Global conditions can be specified with "?" followed by a key-value |
| 1096 |
list, where a key is one of "status", "conditional", "loop" and |
| 1097 |
"loopuntil" and the value is whatever fits the condition, as when |
| 1098 |
explicitely calling the associated action with an exclamation mark. |
| 1099 |
For instance, |
| 1100 |
|
| 1101 |
\gates def {mygate} ?{conditional = \ifsomething} {...} |
| 1102 |
|
| 1103 |
is equivalent to |
| 1104 |
|
| 1105 |
\gates def {mygate}{...} |
| 1106 |
\gates conditional! {mygate}{\ifsomething} |
| 1107 |
|
| 1108 |
In the key-value list, the key is always trimmed of surrounding spaces, |
| 1109 |
as is the value if the key is "status"; values aren't trimmed for |
| 1110 |
other conditions, because space might be significant (e.g. with |
| 1111 |
\ifnum), but leading space is harmless (it is ignored by the internal |
| 1112 |
processing of the conditional). |
| 1113 |
|
| 1114 |
Giving an gate's name between brackets instead of an action is similar |
| 1115 |
to using |def| with that gate: |
| 1116 |
|
| 1117 |
\gates [mygate] [1] {...} |
| 1118 |
|
| 1119 |
> edef <name>[<number of arguments>]<optional conditions>{<definition>} |
| 1120 |
Similar to def, but performs \edef. |
| 1121 |
|
| 1122 |
> execute <name><arguments> |
| 1123 |
Executes gate <name>. The number of <arguments> should match what |
| 1124 |
<name> was defined with. If <name> is an m-gate, this is but a |
| 1125 |
convoluted way of calling a macro. If <name> is an l-gate, this will |
| 1126 |
launch sub-gates. Not that the execution depends on the gate's global |
| 1127 |
conditions. Using a gate's name as an action is similar to using |
| 1128 |
|execute| with that gate. |
| 1129 |
|
| 1130 |
> family |
| 1131 |
Returns the family associated with the calling command. |
| 1132 |
|
| 1133 |
> list <name>[<number of arguments>]<optional conditions><optional subgates> |
| 1134 |
Defines <name> as an l-gate with the given number of arguments (can |
| 1135 |
be missing if 0). The optional global conditions can be specified |
| 1136 |
with "?" followed by a key-value list; see |def| above. The |
| 1137 |
<optional subgates> constitute the shorthand notation explained |
| 1138 |
above [see TeXShorthand_tag], defining and adding subgates to the |
| 1139 |
l-gate under construction, and specifying the local conditions too. |
| 1140 |
|
| 1141 |
Giving an gate's name between parentheses instead of an action is |
| 1142 |
similar to using |def| with that gate: |
| 1143 |
|
| 1144 |
\gates (mylist) [2] ?{status = ajar} |
| 1145 |
|
| 1146 |
|
| 1147 |
> loop <gate spec><l-gate><conditional> |
| 1148 |
Sets the local while-loop in <l-gate> to <conditional> for the gates |
| 1149 |
in <gate spec>. The conditional is the same as with |conditional|; |
| 1150 |
the difference is that the gates will be executed again as long as |
| 1151 |
the conditional is true, so there'd better be something somewhere |
| 1152 |
which makes it false. |
| 1153 |
|
| 1154 |
> loop! <gate spec><conditional>. |
| 1155 |
Sets the global while-loop for the gates in <gate spec> to <conditional>. |
| 1156 |
|
| 1157 |
> loopuntil <gate spec><l-gate><conditional> |
| 1158 |
Sets the local until-loop in <l-gate> to <conditional> for the gates |
| 1159 |
in <gate spec>. The conditional is the same as with |conditional|; |
| 1160 |
the gates will be executed until the conditional is true; this means |
| 1161 |
they will be executed at least once. If |loop| is also specified, |
| 1162 |
|loopuntil| is ignored. |
| 1163 |
|
| 1164 |
> loopuntil! <gate spec><conditional>. |
| 1165 |
Sets the global until-loop for the gates in <gate spec> to <conditional>. |
| 1166 |
|
| 1167 |
> new <control sequence><family> |
| 1168 |
Defines <control sequence> as a calling command for gates, associated |
| 1169 |
with <family>. |
| 1170 |
|
| 1171 |
> noloop <gate spec><l-gate><conditional> |
| 1172 |
Unsets the local while-loop in <l-gate> to <conditional> for the gates |
| 1173 |
in <gate spec>. |
| 1174 |
|
| 1175 |
> noloop! <gate spec> |
| 1176 |
Unsets the global while-loop for the gates in <gate spec>. |
| 1177 |
|
| 1178 |
> noloopuntil <gate spec><l-gate><conditional> |
| 1179 |
Unsets the local until-loop in <l-gate> to <conditional> for the gates |
| 1180 |
in <gate spec>. |
| 1181 |
|
| 1182 |
> noloopuntil! <gate spec> |
| 1183 |
Unsets the global until-loop for the gates in <gate spec>. |
| 1184 |
|
| 1185 |
> open <gate spec><l-gate> |
| 1186 |
Sets the local status in <l-gate> to "open" for the gates in <gate spec>. |
| 1187 |
|
| 1188 |
> open! <gate spec> |
| 1189 |
Sets the global status for the gates in <gate spec> to "open". |
| 1190 |
|
| 1191 |
> remove <gate list><l-gate> |
| 1192 |
Removes the gates in <gate list> (names separated by commas) in <l-gate>. |
| 1193 |
|
| 1194 |
> return <arguments> |
| 1195 |
In an m-gate, pass <arguments> to the next one. There should be as |
| 1196 |
many <arguments> as the gate was declared with. Any material following |
| 1197 |
the statement in the gate's definition will be gobbled. |
| 1198 |
|
| 1199 |
> return0, return1, return2 ... return8, return9 |
| 1200 |
Makes the gate return the specified number of arguments, no matter |
| 1201 |
the number of arguments the gate was defined with. |
| 1202 |
|
| 1203 |
> return! <arguments> |
| 1204 |
Makes the gate return an indefinite number of arguments. If <arguments> |
| 1205 |
is empty, it is similar to 'return0'; if <arguments> contains one |
| 1206 |
argument, it is similar to 'return1'; if it contains two, it is similar |
| 1207 |
to 'return2', etc. An argument is defined as usual in TeX: a token, |
| 1208 |
or balanced text. For instance, in what follows the second and third |
| 1209 |
lines are equivalent; the first does the same job if and only if called |
| 1210 |
inside a gate defined with three arguments (otherwise chaos will ensue). |
| 1211 |
|
| 1212 |
\gates return {one}{two}{three} |
| 1213 |
\gates return3 {one}{two}{three} |
| 1214 |
\gates return! {{one}{two}{three}} |
| 1215 |
|
| 1216 |
> show <gate> |
| 1217 |
Writes to the log and terminal <gate>'s type, its number of arguments, |
| 1218 |
global status, conditional, loops (if specified), and recursively its |
| 1219 |
subgates if <gate> is an l-gate (showing the local conditions). |
| 1220 |
|
| 1221 |
> skip <gate spec><l-gate> |
| 1222 |
Sets the local status in <l-gate> to "skip" for the gates in <gate spec>. |
| 1223 |
|
| 1224 |
> skip! <gate spec> |
| 1225 |
Sets the global status for the gates in <gate spec> to "skip". |
| 1226 |
|
| 1227 |
> status <gate><l-gate> |
| 1228 |
Returns the local status of <gate> in <l-gate>: 1 if the gate is open, |
| 1229 |
2 if it is ajar, 3 if it is to be skipped, and 4 if it is closed; if |
| 1230 |
there is no <gate> in <l-gate> for whatever reason (including if there |
| 1231 |
exists no such l-gate), returns 0. |
| 1232 |
|
| 1233 |
> status! <gate> |
| 1234 |
Returns the global status of <gate>. |
| 1235 |
|
| 1236 |
> subchar <character> |
| 1237 |
Defines <character> as denoting a subgate in the shorthand notation. |
| 1238 |
"[", "(", "<" and "?" are forbidden. |
| 1239 |
|
| 1240 |
> subgates <l-gate><definition> |
| 1241 |
Executes <definition> with each gate in <l-gate>. In <definition>, |
| 1242 |
"#1" stands for the name of the subgate on each iteration. |
| 1243 |
|
| 1244 |
> type <name> |
| 1245 |
Returns 0 if <name> is not a gate, 1 if it is an m-gate and 2 if it |
| 1246 |
is an l-gate. |
| 1247 |
|
| 1248 |
> trace <number> |
| 1249 |
If <number> is 0, gate operations aren't reported; if 1, encountered |
| 1250 |
gates are reported, along with their status and conditional when |
| 1251 |
necessary; if 2, arguments passed to gates that are executed are also |
| 1252 |
displayed. Tracing affects only gates of the family associated with |
| 1253 |
the calling command. |
| 1254 |
|
| 1255 |
|
| 1256 |
|
| 1257 |
|
| 1258 |
|
| 1259 |
Gates in Lua :LuaUser_tag |
| 1260 |
============ |
| 1261 |
|
| 1262 |
In general, gates behave similarly in TeX and Lua, but the syntax |
| 1263 |
obviously differs. Also, there is one situation where both implementations |
| 1264 |
diverge: when arguments are returned. |
| 1265 |
|
| 1266 |
|
| 1267 |
|
| 1268 |
Loading and using gates |
| 1269 |
~~~~~~~~~~~~~~~~~~~~~~~ |
| 1270 |
|
| 1271 |
Gates in Lua aren't loaded automatically with the gates package. So one |
| 1272 |
of the following should be issued somewhere: |
| 1273 |
|
| 1274 |
dofile("gates.lua") |
| 1275 |
require("gates.lua") |
| 1276 |
|
| 1277 |
Actually "dofile" requires more precision, e.g. (in LuaTeX): |
| 1278 |
|
| 1279 |
dofile(kpse.find_file("gates.lua")) |
| 1280 |
|
| 1281 |
On the other hand, "require" is a little bit more clever (in LuaTeX, |
| 1282 |
it uses "kpathsea"). Note that even if "dofile" is used, "gates.lua" |
| 1283 |
won't be loaded twice, because the file returns at once if "gates" |
| 1284 |
already exists. |
| 1285 |
|
| 1286 |
In the above paragraphs, `in LuaTeX' was mentioned twice: that's because |
| 1287 |
Lua gates can be used in any Lua interpreter. Lua is obviously required, |
| 1288 |
but not Lua*TeX*. In other words, Lua Gates don't rely on any special |
| 1289 |
feature of Lua in LuaTeX (libraries, in particular, aren't used). |
| 1290 |
|
| 1291 |
The file "gates.lua" returns nothing when loaded; it simply creates the |
| 1292 |
"gates" table, in which everything takes place until a |new| one is |
| 1293 |
created with a family. This means that all actions are fields of the |
| 1294 |
"gates" table. |
| 1295 |
|
| 1296 |
|
| 1297 |
|
| 1298 |
Definition and execution :LuaDefinition_tag |
| 1299 |
~~~~~~~~~~~~~~~~~~~~~~~~ |
| 1300 |
|
| 1301 |
Here's how to define and execute gates in Lua. |
| 1302 |
|
| 1303 |
|
| 1304 |
-- Defining gates -- |
| 1305 |
|
| 1306 |
The |def| action creates an m-gate; it takes a table with (for now) two |
| 1307 |
entries: the entry at index 1 is the gate's name, the entry at index 2 |
| 1308 |
is the function performed by the gate. For instance, we can translate |
| 1309 |
our simple TeX example in Lua (since the table is the only argument to |
| 1310 |
|def|, the surrounding parentheses can be removed): |
| 1311 |
|
| 1312 |
gates.def {"add", function (n) return n + 3 end} |
| 1313 |
gates.def {"multiply", function (n) return n *2 end} |
| 1314 |
gates.def {"print", print} |
| 1315 |
|
| 1316 |
The function can be either an anonymous function created on the fly, |
| 1317 |
or a function variable, as with "print". However, such a syntax is |
| 1318 |
cumbersome in Lua, so you can directly assign to an entry in the "gates" |
| 1319 |
table, provided it hasn't the same name as an action (so it is impossible |
| 1320 |
with the "add" gate, for instance): |
| 1321 |
|
| 1322 |
function gates.multiply (n) |
| 1323 |
return n * 2 |
| 1324 |
end |
| 1325 |
|
| 1326 |
Actually, entries in the "gates" table be assigned any type; if a |
| 1327 |
function is assigned, a gate is created; otherwise, "gates" behaves as |
| 1328 |
an ordinary table with an ordinary entry: |
| 1329 |
|
| 1330 |
gates.mystring = "Hello" |
| 1331 |
print(gates.mystring) |
| 1332 |
|
| 1333 |
However, the entry can't be redefined as a gate anymore; if it is |
| 1334 |
assigned a function, it will be nothing more. In the following code, |
| 1335 |
"bar" is a gate, but "foo" isn't. |
| 1336 |
|
| 1337 |
gates.bar = function () ... end |
| 1338 |
gates.foo = "hello" |
| 1339 |
gates.foo = function () ... end |
| 1340 |
|
| 1341 |
But let's get back to proper gates. We want to add our m-gates to an |
| 1342 |
l-gate, which we declare beforehand with |list|: |
| 1343 |
|
| 1344 |
gates.list {"operation"} |
| 1345 |
|
| 1346 |
Like |def|, |list| takes a table as its single argument; the only |
| 1347 |
required field is the gate's name at index 1. We can now add the m-gates |
| 1348 |
to the l-gate: |
| 1349 |
|
| 1350 |
gates.add ({"add", "multiply", "print"}, "operation") |
| 1351 |
|
| 1352 |
The |add| action takes a table containing subgates as its first argument, |
| 1353 |
and a string representing the l-gate where the insertion is done as its |
| 1354 |
second. If you add a single subgate, the first argument can be a string. |
| 1355 |
More on |add| below [see LuaAddWithPosition_tag]. |
| 1356 |
|
| 1357 |
|
| 1358 |
-- Executing gates -- |
| 1359 |
|
| 1360 |
We can now execute our l-gate: |
| 1361 |
|
| 1362 |
gates.execute ("operation", 4) |
| 1363 |
|
| 1364 |
and "14" will be printed on the terminal. The |execute| action takes a |
| 1365 |
gate as its first arguments, and then the arguments that are to be |
| 1366 |
passed to the gate. Again, this syntax is a bit cumbersome, and gates |
| 1367 |
can be executed more naturally as: |
| 1368 |
|
| 1369 |
gates.execute.operation(4) |
| 1370 |
|
| 1371 |
or simpler, but provided the gate doesn't share a name with an action: |
| 1372 |
|
| 1373 |
gates.operation(4) |
| 1374 |
|
| 1375 |
The last two variants also let you retrieve the gate itself as a function, |
| 1376 |
instead of calling it: |
| 1377 |
|
| 1378 |
x = gates.execute.operation |
| 1379 |
callback.register("process_input_buffer", gates.operation) |
| 1380 |
|
| 1381 |
|
| 1382 |
:LuaAddWithPosition_tag |
| 1383 |
Now we can define another m-gate and add it to "operation", specifying |
| 1384 |
the position: |
| 1385 |
|
| 1386 |
gates.substract = function (n) return n-3 end |
| 1387 |
gates.add ("substract", "operation", "after multiply") |
| 1388 |
|
| 1389 |
And now "operation" returns 11 when fed 4. This was done by simply |
| 1390 |
adding an optional third argument to the |add| operation; this specifies |
| 1391 |
where the new gate(s) should be added in the l-gate: by default, it is |
| 1392 |
the end of the list, but you can say "first" to put the gate(s) at the |
| 1393 |
beginning or "before <name>" or "after <name>" to make the insertion |
| 1394 |
before or after the gate called <name>; instead of a name, you can also |
| 1395 |
use "first" or "last" to denote the first and last gates of the list, |
| 1396 |
so here we could have used "before last" instead of "after multiply". |
| 1397 |
|
| 1398 |
Finally, you can also |remove| a gate: |
| 1399 |
|
| 1400 |
gates.remove("substract", "operation") |
| 1401 |
|
| 1402 |
and now "operation" is what it was before. |
| 1403 |
|
| 1404 |
|
| 1405 |
-- Handling arguments -- |
| 1406 |
|
| 1407 |
When a gate is called, it may take arguments; in the case of an |
| 1408 |
l-gate, those arguments are passed to each subgates, one after the |
| 1409 |
other. Unlike gates in TeX, though, if a gate expects more arguments |
| 1410 |
than passed to the l-gate it belongs too, no empty argument is added. |
| 1411 |
For instance: |
| 1412 |
|
| 1413 |
gates.mygate = function (a, b) |
| 1414 |
print ("First: " .. tostring(a), |
| 1415 |
"Second: " .. tostring(b)) |
| 1416 |
end |
| 1417 |
gates.list {"mylist"} |
| 1418 |
gates.add ("mygate", "mylist") |
| 1419 |
gates.mylist("one") |
| 1420 |
|
| 1421 |
will print: |
| 1422 |
|
| 1423 |
First: one Second: nil |
| 1424 |
|
| 1425 |
Also, there is by default no `common argument pool', as there was in |
| 1426 |
TeX: a gate (in an l-gate) receives what the previous one returns and |
| 1427 |
nothing else; in other words if l-gate L receives four arguments and |
| 1428 |
subgate A returns only three, then subgate B will receive three |
| 1429 |
arguments. Consequently, all gates should return properly if arguments |
| 1430 |
are to be passed. |
| 1431 |
|
| 1432 |
But that is only default behavior. Lua gates can be made to work like |
| 1433 |
TeX gates (to some extent), by using |autoreturn|: |
| 1434 |
|
| 1435 |
gates.autoreturn ("mygate", true) |
| 1436 |
gates.autoreturn ("mygate", "mylist", true) |
| 1437 |
|
| 1438 |
When |autoreturn| is set for a gate (either globally, as in the first |
| 1439 |
line, or locally, as in the second), missing arguments will be restored |
| 1440 |
when the gate returns. To qualify as a missing argument, the following |
| 1441 |
conditions should hold: first, the value is "nil" (i.e. either "nil" |
| 1442 |
was explictly returned, or nothing was returned); second, no real |
| 1443 |
argument follows. For instance, suppose "mygate" is defined as follows: |
| 1444 |
|
| 1445 |
gates.mygate = function (A, B, C, D) |
| 1446 |
return nil, X |
| 1447 |
end |
| 1448 |
|
| 1449 |
If |autoreturn| is false, the following gate will receive "nil" and |
| 1450 |
"X"; if it is true, the arguments will be restored to "nil", "X", "C" |
| 1451 |
and "D"; the first argument isn't restored, even though it is "nil", |
| 1452 |
because it is followed by real argument "X". |
| 1453 |
|
| 1454 |
One can also uses |autoreturn| to completely disregard whatever a gate |
| 1455 |
returns; this happens when |autoreturn| is a function instead of a |
| 1456 |
boolean; the function is passed the original arguments given to the |
| 1457 |
gate, and what it returns overrides the gate's return values. For |
| 1458 |
instance, given the same definition of "mygate", if |autoreturn| is: |
| 1459 |
|
| 1460 |
gates.autoreturn ("mygate", |
| 1461 |
function (A, B, C, D) return D, C, B, A end) |
| 1462 |
|
| 1463 |
then no matter what "mygate" returns (including what it returns on |
| 1464 |
several iterations of itself, if any), the following gate will always |
| 1465 |
receive the original arguments in reverse order. This can be particularly |
| 1466 |
useful if the gate is controlled by an |iterator|, in which case the |
| 1467 |
arguments it receives (e.g. the entries of a table) differ from the |
| 1468 |
original ones (e.g. the table itself), yet you still want to pass the |
| 1469 |
latter to the following gate. |
| 1470 |
|
| 1471 |
L-gates automatically returns whatever their last gate returns. Also, |
| 1472 |
unlike the TeX implementation (but in line with the Lua language), when |
| 1473 |
a gate returns outside an l-gate, the returned values can be used, as |
| 1474 |
with any other function. |
| 1475 |
|
| 1476 |
|
| 1477 |
|
| 1478 |
Controlling gates |
| 1479 |
~~~~~~~~~~~~~~~~~ |
| 1480 |
|
| 1481 |
Here it is explained how gates can be controlled externally. |
| 1482 |
|
| 1483 |
|
| 1484 |
-- Status -- :LuaStatus_tag |
| 1485 |
|
| 1486 |
A gate's status can be set with the |open|, |ajar|, |skip| and |
| 1487 |
|close| actions; they take at least one argument, either a table: |
| 1488 |
|
| 1489 |
gates.open ({"mygate", "myothergate"}) |
| 1490 |
|
| 1491 |
or a simple string if only one gate is to be affected: |
| 1492 |
|
| 1493 |
gates.open ("mygate") |
| 1494 |
|
| 1495 |
With one argument, the actions set the global status; if a second |
| 1496 |
argument is present, it is the l-gate where the local status is to be |
| 1497 |
set: |
| 1498 |
|
| 1499 |
gates.ajar ({"mygate", "myothergate"}, "mylist") |
| 1500 |
|
| 1501 |
In this case, the first argument can denote a relative position of the |
| 1502 |
form "before <gate>" or "after <gate>": |
| 1503 |
|
| 1504 |
gates.ajar ("before mygate", "mylist") |
| 1505 |
|
| 1506 |
|
| 1507 |
-- Conditional -- :LuaConditional_tag |
| 1508 |
|
| 1509 |
To make a gate depends on an external state of affair rather than status |
| 1510 |
only, |conditional| can be used. The syntax is the same as for status, |
| 1511 |
except a second (in case of global conditional) or third (in case of |
| 1512 |
local conditional) argument is given. This should be a function, and |
| 1513 |
the gate's execution depends on what the function returns: the gate is |
| 1514 |
executed the function returns nothing or "false". The arguments that |
| 1515 |
are to be passed to the gate are passed beforehand to the conditional |
| 1516 |
function. For instance, if a gate receives arguments "A" and "B", and |
| 1517 |
its conditional function is "ControlFunction", then the gate's execution |
| 1518 |
can be schematized as: |
| 1519 |
|
| 1520 |
if ControlFunction(A, B) then |
| 1521 |
-- Execute gate |
| 1522 |
end |
| 1523 |
|
| 1524 |
If you don't want a gate to depend on a conditional anymore, you can |
| 1525 |
declare something like: |
| 1526 |
|
| 1527 |
gates.conditional ("mygate", "mylist", |
| 1528 |
function () return true end) |
| 1529 |
|
| 1530 |
|
| 1531 |
-- Loop -- :LuaLoop_tag |
| 1532 |
|
| 1533 |
The |loop| action allows a gate to be repeated; the syntax is the same |
| 1534 |
as for |conditional|, and the loop will be repeated as long as the |
| 1535 |
function evaluates to true (as with a "while" loop). Also, the gate's |
| 1536 |
arguments are repeatedly passed to the loop conditional. To delete a |
| 1537 |
gate's loop, use |loop| with "nil" as the third argument. As an |
| 1538 |
example, the following will print number from 1 to 4: |
| 1539 |
|
| 1540 |
function gates.mygate (n) |
| 1541 |
print(n) |
| 1542 |
return n + 1 |
| 1543 |
end |
| 1544 |
gates.loop("mygate", function (n) return n < 5 end) |
| 1545 |
gates.mygate(1) |
| 1546 |
|
| 1547 |
|
| 1548 |
-- Loop until -- :LuaLoopUntil_tag |
| 1549 |
|
| 1550 |
The |loopuntil| action is like |loop|, except the gate is repeated until |
| 1551 |
the conditional evaluates to false. Also, the conditional is evaluated |
| 1552 |
after the gate is executed, so the execution takes place at least once. |
| 1553 |
If both |loop| and |loopuntil| are set for a gate, the latter is ignored. |
| 1554 |
|
| 1555 |
|
| 1556 |
-- Iterator -- :LuaIterator_tag |
| 1557 |
|
| 1558 |
The |iterator| action is a bit more complex. If set, it is fed the |
| 1559 |
arguments passed to the gate and should return a function, plus possibly |
| 1560 |
a state, plus possibly an initial variable; in other words, it should |
| 1561 |
return whatever fits a "for" loop in Lua. Then the function is called |
| 1562 |
with the state and variable, and the gate itself is called on whatever |
| 1563 |
this function returns; the process repeats until the function returns |
| 1564 |
"nil" as its first argument. As an example: |
| 1565 |
|
| 1566 |
gates.mygate = function (key, value) |
| 1567 |
print("The value of " .. key .. " is " .. value) |
| 1568 |
end |
| 1569 |
gates.iterator ("mygate", pairs) |
| 1570 |
gates.mygate({x = 55, y = 22}) |
| 1571 |
|
| 1572 |
will print: |
| 1573 |
|
| 1574 |
The value of x is 55 |
| 1575 |
The value of y is 22 |
| 1576 |
|
| 1577 |
Here "pairs" was used to return the function, state and variable mentioned |
| 1578 |
above; but of course you can make you own function: |
| 1579 |
|
| 1580 |
gates.mygate = function (w) |
| 1581 |
print(w .. " has " .. #w .. " characters.") |
| 1582 |
end |
| 1583 |
gates.iterator ("mygate", |
| 1584 |
function (s) |
| 1585 |
local t = string.explode(s) |
| 1586 |
local i = 0 |
| 1587 |
local function enum () |
| 1588 |
i = i+1 |
| 1589 |
return t[i] |
| 1590 |
end |
| 1591 |
return enum |
| 1592 |
end) |
| 1593 |
gates.mygate("two words") |
| 1594 |
|
| 1595 |
And the result is: |
| 1596 |
|
| 1597 |
two has 3 characters. |
| 1598 |
words has 5 characters. |
| 1599 |
|
| 1600 |
Here the function registered in |iterator| produces a single function, |
| 1601 |
without a state, and goes through all the entries of the table created |
| 1602 |
by splitting the original string. In sum, what |iterator| expects is |
| 1603 |
what Lua's generic |for| expects, and understanding the latter is |
| 1604 |
understanding the former. |
| 1605 |
|
| 1606 |
An important point to keep in mind when using |iterator| is that there |
| 1607 |
is a discrepancy between the arguments passed to the gate and the ones |
| 1608 |
it really processes; in the first example, "mygate" is called with a |
| 1609 |
table, but it receives two strings. This means that its definition |
| 1610 |
doesn't match the way it is called; in that respect, unlike |loop| and |
| 1611 |
associates above, you just can't impose an |iterator| on a aready defined |
| 1612 |
gate and expect everything to be fine, since its definition probably |
| 1613 |
won't match the arguments it will now receive. |
| 1614 |
|
| 1615 |
Another point is the return values, if any; during the iteration, the |
| 1616 |
gate's return values are ignored; then the last ones are passed to the |
| 1617 |
following gate, possibly augmented with some of the original arguments |
| 1618 |
if |autoreturn| is "true". Note that the original arguments here are |
| 1619 |
those passed to the gate before the iteration states. This holds if |
| 1620 |
|autoreturn| is a function too: |
| 1621 |
|
| 1622 |
gates.mygate = function (what, ever) |
| 1623 |
-- Whatever. |
| 1624 |
end |
| 1625 |
gates.iterator ("mygate", pairs) |
| 1626 |
gates.autoreturn ("mygate", |
| 1627 |
function (t) return t end) |
| 1628 |
gates.mygate (mytable) |
| 1629 |
|
| 1630 |
Here "mygate" will loop on the entries in "mytable" (thanks to "pairs"), |
| 1631 |
and once the iterations are over, "mytable" is returned. |
| 1632 |
|
| 1633 |
Of course, |iterator| can be used with l-gates too; that works the |
| 1634 |
same: whatever the iterator returns is simply passed to the subgates. |
| 1635 |
Finally, if a gate has either |loop| or |loopuntil|, |iterator| is |
| 1636 |
ignored. |
| 1637 |
|
| 1638 |
|
| 1639 |
|
| 1640 |
|
| 1641 |
The shorthand notation :LuaShorthand_tag |
| 1642 |
~~~~~~~~~~~~~~~~~~~~~~ |
| 1643 |
|
| 1644 |
Gates can be created and manipulated by actions, as we've done up to |
| 1645 |
now, but they can also be declared much faster. First, |def| and |list| |
| 1646 |
take tables as their arguments; that is for a good reason: entries |
| 1647 |
indexed with certain keys are equivalent to actions. The keys are |
| 1648 |
|autoreturn|, |status|, |conditional|, |loop|, |loopuntil| and |iterator| |
| 1649 |
and setting them when declaring a gate is like globally setting the |
| 1650 |
associated action (except there is a single action for |status|, which |
| 1651 |
takes a string as its value: "open", "ajar", "skip" or "close"). Thus |
| 1652 |
the following defines an m-gate with global status "ajar" and a |loop|: |
| 1653 |
|
| 1654 |
gates.def {"mygate", |
| 1655 |
status = "ajar", |
| 1656 |
loop = function (n) return n < 5 end, |
| 1657 |
function (n) |
| 1658 |
print(n) |
| 1659 |
return n + 1 |
| 1660 |
end} |
| 1661 |
|
| 1662 |
Note that the entries can be given in any order as long as the gate's |
| 1663 |
name is at index 1 and its definition at index 2. |
| 1664 |
|
| 1665 |
L-gates have another property: as said above, the entry at index 1 |
| 1666 |
should be the gate's name; but you can put tables representing gates |
| 1667 |
at index 2, 3, etc., and they represent the l-gate's subgates. Those |
| 1668 |
tables are the same as the ones passed to |def| and |list|; the only |
| 1669 |
difference is that setting of |autoreturn|, |status|, etc., is local |
| 1670 |
to the l-gate where they are added. The following code creates l-gate |
| 1671 |
"mylist" with subgates "mygate" and "myothergate", the former subject |
| 1672 |
to a local loop: |
| 1673 |
|
| 1674 |
gates.list {"mylist", |
| 1675 |
{"mygate", loop = function (n) return n < 5 end, |
| 1676 |
function (n) |
| 1677 |
print (n) |
| 1678 |
return n + 1 |
| 1679 |
end}, |
| 1680 |
{"myothergate", |
| 1681 |
function (n) |
| 1682 |
print("We're done!") |
| 1683 |
end}} |
| 1684 |
|
| 1685 |
Since the subtables are the same as the tables passed to |def| and |
| 1686 |
|list|, it means that an l-gate thus declared can host subgates too: |
| 1687 |
|
| 1688 |
gates.list {"mylist", |
| 1689 |
{"mysublist", |
| 1690 |
{"mygate", function () ... end}, |
| 1691 |
{"myothergate", function () ... end}}, |
| 1692 |
{"anothergate", function () ... end}} |
| 1693 |
|
| 1694 |
If you want to add an already existing gate, you can do so either by |
| 1695 |
using a simple string instead of a table, or (if you want to set |status| |
| 1696 |
and associates) a table with the name at index 1 and nothing at higher |
| 1697 |
indices: |
| 1698 |
|
| 1699 |
gates.list {"mylist", |
| 1700 |
"AnExistingSubgate", |
| 1701 |
{"AnExistingSubgateWithOption", status = "ajar"}} |
| 1702 |
|
| 1703 |
Note however that the second version is similar in form to an l-gate |
| 1704 |
defined without subgates; this means that you can't redefine an l-gate |
| 1705 |
thus, but the situations where you would like to do so (redefining an |
| 1706 |
existing l-gate, and redefining it without subgates, and in shorthand |
| 1707 |
notation) aren't many. In that case you should use |list| and |add| |
| 1708 |
instead. (On the other hand, if you do specify subgates, redefinition |
| 1709 |
will happen with an l-gate with that name already exists; in other |
| 1710 |
words, it won't be interpreted as inserting an existing l-gate and add |
| 1711 |
subgates to it.) |
| 1712 |
|
| 1713 |
|
| 1714 |
|
| 1715 |
Gate families :LuaFamilies_tag |
| 1716 |
~~~~~~~~~~~~~ |
| 1717 |
|
| 1718 |
The |new| action takes a string (the family name) as its sole argument |
| 1719 |
an return a calling table: |
| 1720 |
|
| 1721 |
MyGates = gates.new ("MyFamily") |
| 1722 |
|
| 1723 |
"MyGates" can now be used like "gates" (to which is associated the |
| 1724 |
"gates" family), except "MyFamily" will be used as the family name when |
| 1725 |
necessary. |
| 1726 |
|
| 1727 |
A table's family is stored in the |family| entry; note that it is not |
| 1728 |
an action, hence not a function, but a string: |
| 1729 |
|
| 1730 |
print(MyGates.family) |
| 1731 |
|
| 1732 |
|
| 1733 |
|
| 1734 |
|
| 1735 |
If you get lost :LuaLost_tag |
| 1736 |
~~~~~~~~~~~~~~~ |
| 1737 |
|
| 1738 |
Both |type| and |status| return a number: |type| returns 0 if there is |
| 1739 |
no gate with that name, 1 if it is an m-gate and 2 for an l-gate; |
| 1740 |
|status| returns 0 for a non-existing gate, and 1 to 4 for "open", |
| 1741 |
"ajar", "skip" and "close" respectively; |status| can also be used |
| 1742 |
with one or two arguments, indicating global or local status |
| 1743 |
respectively: |
| 1744 |
|
| 1745 |
gates.status ("mygate") |
| 1746 |
gates.status ("mygate", "mylist") |
| 1747 |
|
| 1748 |
The |subgates| action passes the names of all the subgates in a given |
| 1749 |
l-gate to a function. For instance, the following prints all |
| 1750 |
"mylist"'s subgates and their types: |
| 1751 |
|
| 1752 |
gates.subgates("mylist", |
| 1753 |
function (g) |
| 1754 |
local t |
| 1755 |
if gates.type(g) == 1 then |
| 1756 |
t = "(m-gate)" |
| 1757 |
else |
| 1758 |
t = "(l-gate)" |
| 1759 |
end |
| 1760 |
print (g, t) |
| 1761 |
end) |
| 1762 |
|
| 1763 |
The |show| action can be used to display a gate's construction and |
| 1764 |
conditions. It is displayed as the shorthand notation. The |trace| |
| 1765 |
action takes a number and works as follows: if the number is 0, gates |
| 1766 |
of the associated families aren't traced; if it is 1, it is mentionned |
| 1767 |
if they are called (in an l-gate) and executed or not, and why; with |
| 1768 |
2, it works like 1, except the arguments passed to the gates are shown |
| 1769 |
too. |
| 1770 |
|
| 1771 |
|
| 1772 |
|
| 1773 |
|
| 1774 |
|
| 1775 |
|
| 1776 |
|
| 1777 |
|
| 1778 |
Reference manual for Lua :LuaReference_tag |
| 1779 |
======================== |
| 1780 |
|
| 1781 |
There are some differences between gates in TeX and gates in Lua, owing to the |
| 1782 |
differences in syntax between the two languages. That said, the same |
| 1783 |
operations can be found in both interfaces. |
| 1784 |
|
| 1785 |
In what follows, <gate list> denotes either a table with gate names at |
| 1786 |
successive indices (e.g. "{'mygate', 'myothergate'}") or a single string |
| 1787 |
denoting a single gate.; <gate spec> denotes either a <gate list> or a |
| 1788 |
relative position of the form "before <gate>" or "after <gate>" (the |
| 1789 |
latter case only if an l-gate is also specified, obviously). |
| 1790 |
|
| 1791 |
> add (<gate list>, <l-gate>[, <position>]) |
| 1792 |
Adds the gates in <gate list> to <l-gate>. The optional <position> |
| 1793 |
should be one of the following: "first", meaning the gates will be |
| 1794 |
added at the beginning of the l-gate; "last", meaning they will be |
| 1795 |
added at the end (so this is similar to no <position>); "before <name>", |
| 1796 |
and the addition will occur before gate <name> in the l-gate (it |
| 1797 |
should of course exist); "after <name>", and the addition will occur |
| 1798 |
after gate <name>. Note that <name> can be "first" or "last", denoting |
| 1799 |
the first and last gates in <l-gate> ("before first" and "after last" |
| 1800 |
are obviously synonymous with "first" and "last"). |
| 1801 |
|
| 1802 |
> ajar (<gate spec>[, <l-gate>]) |
| 1803 |
Without <l-gate>, sets the global status for the gates in <gate spec> to |
| 1804 |
"ajar". With <l-gate>, sets the local status in <l-gate> to "ajar" for the |
| 1805 |
gates in <gate spec>. |
| 1806 |
|
| 1807 |
> autoreturn (<gate spec>[, <l-gate>], <boolean or function>) |
| 1808 |
Without <l-gate>, sets the global |autoreturn| for the gates in |
| 1809 |
<gate spec>; with <l-gate>, the local |autoreturn| is done. When a |
| 1810 |
gate's |autoreturn| is set to "true", arguments will be restored if |
| 1811 |
it returns less than what it was passed; if |autoreturn| is a |
| 1812 |
function, it is passed the original arguments passed to the gates |
| 1813 |
and what it returns will be the ultimate return values of the gate. |
| 1814 |
In the latter case, the gate's conditions are ignored, i.e. the |
| 1815 |
arguments passed to |autoreturn| are the ones fed to the gate before |
| 1816 |
it loops or iterates (if it does that). |
| 1817 |
|
| 1818 |
> close (<gate spec>[, <l-gate>]) |
| 1819 |
Without <l-gate>, sets the global status for the gates in <gate spec> to |
| 1820 |
"close". With <l-gate>, sets the local status in <l-gate> to "close" for the |
| 1821 |
gates in <gate spec>. |
| 1822 |
|
| 1823 |
> conditional (<gate spec>[, <l-gate>], <function>) |
| 1824 |
Without <l-gate>, sets the global conditional for the gates in |
| 1825 |
<gate spec> to <function>. With <l-gate>, sets the local conditional |
| 1826 |
in <l-gate> to <function> for the gates in <gate list>. That the |
| 1827 |
conditional is a function means that the gate(s) will be executed |
| 1828 |
only if <function> returns anything but "nil". |
| 1829 |
|
| 1830 |
> copy (<gate1>, <gate2>) |
| 1831 |
Defines <gate1> as <gate2> (either an m- or l-gate). The current global |
| 1832 |
status is also copied. If <gate2> is an l-gate, its gate list is also |
| 1833 |
copied, along with the current status of the gates it contains. On the |
| 1834 |
other hand, if <gate2> occurs in some l-gate(s), this information isn't |
| 1835 |
copied. |
| 1836 |
|
| 1837 |
> def (<table>) |
| 1838 |
Defines an m-gate whose name is the entry at index 1 in <table>, and |
| 1839 |
the defintion the function at index 2; |autoreturn|, |status|, |
| 1840 |
|conditional|, |loop| and |loopuntil| can also be used as key to |
| 1841 |
globally specify those settings. |
| 1842 |
|
| 1843 |
Assigning to an entry in the general "gates" table is a shorthand for |
| 1844 |
|def|; in other words, the following two lines are synonymous, provided |
| 1845 |
there isn't a gate action called "foo". |
| 1846 |
|
| 1847 |
gates.def {"foo", function (...) ... end} |
| 1848 |
gates.foo = function (...) ... end |
| 1849 |
|
| 1850 |
In the second case, if what is assigned isn't a function, then a new |
| 1851 |
entry is simply added to the "gates" table, as if it were a simple |
| 1852 |
table, unless the index is an existing action or gate (in which case |
| 1853 |
an error is raised). Note that this entry will then be unavailable to |
| 1854 |
host a gate if redefined. |
| 1855 |
|
| 1856 |
> execute (<gate>[, ...]) |
| 1857 |
Calls <gate> with the other arguments. There exists two shorthands: |
| 1858 |
"gates.<gate>(...)", provided <gate> doesn't clash with an action's |
| 1859 |
name, and "gates.execute.<gate>(...)". Those shorthands are also the |
| 1860 |
only way to retrieve the gate itself instead of executing it. |
| 1861 |
|
| 1862 |
> family |
| 1863 |
The family associated with a table created with |new|. This is a |
| 1864 |
string, not a function. |
| 1865 |
|
| 1866 |
> iterator (<gate spec>[, <l-gate>], <function>) |
| 1867 |
Without <l-gate>, sets the global iterator for the gates in <gate spec> |
| 1868 |
to <function>. With <l-gate>, sets the local iterator in <l-gate> to |
| 1869 |
<function> for the gates in <gate list>. The <function> is passed the |
| 1870 |
gates' original arguments and should return a function, plus possibly |
| 1871 |
a state and also a variable; then the function will be called repeatedly |
| 1872 |
with the state and variable and the gates will be called on what the |
| 1873 |
function returns until it returns "nil" as the first argument. What |
| 1874 |
the gates return is ignored until the last iteration, in which case |
| 1875 |
it is passed to the next gate (unless |autoreturn| is set). To delete |
| 1876 |
a gate's iterator, use the same action with "nil" as <function> (but |
| 1877 |
that's generally not a good idea, since gates with iterators are |
| 1878 |
tailored to the arguments the iterator returns, not to the arguments |
| 1879 |
they are called with). |
| 1880 |
|
| 1881 |
> list (<table>) |
| 1882 |
Declares an l-gate whose name is the entry at index 1 in <table>. |
| 1883 |
Additional entries can be specified as with |def|. Tables at indices |
| 1884 |
2, 3, etc., are subgates created and added at once to <table>, with |
| 1885 |
|status| and associates indicating local (not global) settings. |
| 1886 |
Existing gates can also be added without being redefined, by giving |
| 1887 |
their names (a simple string) instead of a full table, or a table |
| 1888 |
with nothing at indices 2 and higher. |
| 1889 |
|
| 1890 |
> loop (<gate spec>[, <l-gate>], <function>) |
| 1891 |
Without <l-gate>, sets the global while-loop for the gates in <gate spec> |
| 1892 |
to <function>. With <l-gate>, sets the local while-loop in <l-gate> |
| 1893 |
to <function> for the gates in <gate list>. The gates will be executed |
| 1894 |
again as long as <function> evaluates to true. To delete a gate's |
| 1895 |
while-loop, use the same action with "nil" as <function>. |
| 1896 |
|
| 1897 |
> loopuntil (<gate spec>[, <l-gate>], <function>) |
| 1898 |
Without <l-gate>, sets the global until-loop for the gates in <gate spec> |
| 1899 |
to <function>. With <l-gate>, sets the local until-loop in <l-gate> |
| 1900 |
to <function> for the gates in <gate list>. The gates will be executed |
| 1901 |
again as long as <function> doesn't evaluate to true. To delete a |
| 1902 |
gate's until-loop, use the same action with "nil" as <function>. |
| 1903 |
|
| 1904 |
> open (<gate spec>[, <l-gate>]) |
| 1905 |
Without <l-gate>, sets the global status for the gates in <gate spec> to |
| 1906 |
"open". With <l-gate>, sets the local status in <l-gate> to "open" for the |
| 1907 |
gates in <gate spec>. |
| 1908 |
|
| 1909 |
> remove (<gate list>, <l-gate>) |
| 1910 |
Removes the gates in <gate list> from <l-gate>. |
| 1911 |
|
| 1912 |
> show (<gate>) |
| 1913 |
Writes to the terminal (and log file, if in LuaTeX) <gate>'s type, |
| 1914 |
its number of arguments, global status, conditional, loops (if |
| 1915 |
specified), "autoreturn" if set to true, and recursively its subgates |
| 1916 |
if <gate> is an l-gate (showing the local settings). |
| 1917 |
|
| 1918 |
> skip (<gate spec>[, <l-gate>]) |
| 1919 |
Without <l-gate>, sets the global status for the gates in <gate spec> to |
| 1920 |
"skip". With <l-gate>, sets the local status in <l-gate> to "skip" for the |
| 1921 |
gates in <gate spec>. |
| 1922 |
|
| 1923 |
> status (<gate>[, <l-gate]) |
| 1924 |
Without <l-gate>, returns the global status of <gate>; with <l-gate>, |
| 1925 |
the local status is returned: 1 if the gate is open, 2 if it is ajar, |
| 1926 |
3 if it is to be skipped, and 4 if it is closed; if there <gate> |
| 1927 |
doesn't exist, of doesn't exist in <l-gate>, 0 is returned. |
| 1928 |
|
| 1929 |
> subgates (<l-gate>, <function>) |
| 1930 |
Executes <function> with each gate (represented by its name as a |
| 1931 |
string) in <l-gate>. |
| 1932 |
|
| 1933 |
> trace (<number>) |
| 1934 |
If <number> is 0, gate operations aren't reported; if 1, encountered |
| 1935 |
gate are reported, along with their status and conditional when |
| 1936 |
necessary; if 2, arguments passed to gates that are executed are also |
| 1937 |
displayed. Tracing affects only gates of the family associated with |
| 1938 |
the calling command. |
| 1939 |
|
| 1940 |
> type (<name>) |
| 1941 |
Returns the type of <name>: 0 if it isn't a gate, 1 if it is an m-gate, 2 if |
| 1942 |
it is an l-gate. |
| 1943 |
|
| 1944 |
|
| 1945 |
|
| 1946 |
|
| 1947 |
|