Debian Codegraph @

texmf-dist/source/luatex/luatexbase/luatexbase.dtx

raw
1 % \iffalse meta-comment
2 %
3 % Copyright (C) 2009-2013 by Élie Roux <[email protected]>
4 % Copyright (C) 2010, 2011 by Manuel Pégourié-Gonnard <[email protected]>
5 % Copyright (C) 2015 David Carlisle and Joseph Wright
6 %
7 % It may be distributed and/or modified under the conditions of
8 % the LaTeX Project Public License (LPPL), either version 1.3c of
9 % this license or (at your option) any later version. The latest
10 % version of this license is in the file:
11 %
12 % http://www.latex-project.org/lppl.txt
13 %
14 %<emu>\ifx\BeginCatcodeRegime\undefined\else\expandafter\endinput\fi
15 %<tex,sty>
16 %<tex,sty>\ifx
17 %<sty> \ProvidesPackage\undefined\begingroup\def\ProvidesPackage
18 %<tex> \ProvidesFile\undefined\begingroup\def\ProvidesFile
19 %<tex,sty> #1#2[#3]{\endgroup\immediate\write-1{Package: #1 #3}}
20 %<tex,sty>\fi
21 %<emu>\ProvidesPackage{luatexbase}
22 %<emu-cmp>\ProvidesPackage{luatexbase-compat}
23 %<emu-mod>\ProvidesPackage{luatexbase-modutils}
24 %<emu-loa>\ProvidesPackage{luatexbase-loader}
25 %<emu-reg>\ProvidesPackage{luatexbase-regs}
26 %<emu-att>\ProvidesPackage{luatexbase-attr}
27 %<emu-cct>\ProvidesPackage{luatexbase-cctb}
28 %<emu-mcb>\ProvidesPackage{luatexbase-mcb}
29 %<*driver>
30 \ProvidesFile{luatexbase.dtx}
31 %</driver>
32 %<*tex,sty>
33 [2015/10/04 v1.3
34 %</tex,sty>
35 %<emu> luatexbase interface to LuaTeX
36 %<emu-cmp> luatexbase interface to LuaTeX (legacy subpackage)
37 %<emu-mod> luatexbase interface to LuaTeX (legacy subpackage)
38 %<emu-loa> luatexbase interface to LuaTeX (legacy subpackage)
39 %<emu-reg> luatexbase interface to LuaTeX (legacy subpackage)
40 %<emu-att> luatexbase interface to LuaTeX (legacy subpackage)
41 %<emu-cct> luatexbase interface to LuaTeX (legacy subpackage)
42 %<emu-mcb> luatexbase interface to LuaTeX (legacy subpackage)
43 %<*tex,sty>
44 ]
45 %</tex,sty>
46 %<*driver>
47 \documentclass{ltxdoc}
48 \GetFileInfo{luatexbase.dtx}
49 \begin{document}
50 \title{\filename\\(Lua\TeX{}-specific support, luatexbase interface)}
51 \author{David Carlisle and Joseph Wright\footnote{Significant portions
52 of the code here are adapted/simplified from the packages \textsf{luatex} and
53 \textsf{luatexbase} written by Heiko Oberdiek, \'{E}lie Roux,
54 Manuel P\'{e}gouri\'{e}-Gonnar and Philipp Gesang.}}
55 \date{\filedate}
56 \maketitle
57 \setcounter{tocdepth}{2}
58 \tableofcontents
59 \DocInput{\filename}
60 \end{document}
61 %</driver>
62 % \fi
63 %
64 % \section{Overview}
65 %
66 % Lua\TeX{} adds a number of engine-specific functions to \TeX{}. Support
67 % for those is now available for this area in the \LaTeX{} kernel and as
68 % an equivalent stand-alone file |ltluatex.tex| for plain users. The
69 % functionality there is derived from the earlier \textsf{luatex}
70 % and \textsf{luatexbase} packages by Heiko Oberdiek, \'{E}lie Roux,
71 % Manuel P\'{e}gouri\'{e}-Gonnar and Philipp Gesang. However, the
72 % interfaces are not all identical.
73 %
74 % The interfaces defined in this package are closely modelled on the original
75 % \textsf{luatexbase} package, and provide a compatibility layer between
76 % the new kernel-level support and existing code using \textsf{luatexbase}.
77 %
78 % \section{The \textsf{luatexbase} package interface}
79 %
80 % \subsection
81 % [Catcode tables]
82 % {Catcode tables\footnote{This
83 % interface was previously defined in the \textsf{luatexbase-cctbl}
84 % sub-package.}}
85 %
86 % \subsubsection{\TeX}
87 %
88 %
89 % \noindent
90 % \DescribeMacro{\CatcodeTableIniTeX}
91 % \DescribeMacro{\CatcodeTableString}
92 % \DescribeMacro{\CatcodeTableLaTeX}
93 % \DescribeMacro{\CatcodeTableLaTeXAtLetter}
94 % \DescribeMacro{\CatcodeTableOther}
95 % \DescribeMacro{\CatcodeTableExpl}
96 % \TeX\ access to predefined catcode tables.
97 %
98 % The first four tables are aliases giving alternative names for some
99 % catcodetables that are defined in the \textsf{ltluatex} core.
100 %
101 % |\CatcodeTableOther| is like |\CatcodeTableString| except that
102 % the catcode of space is $12$ (other).
103 %
104 % |\CatcodeTableExpl| is similar to the environment set by the
105 % \textsf{expl3} command |\ExplSyntaxOn| note that this only affects
106 % catcode settings, not for example |\endlinechar|.
107 %
108 % One difference between this implementation and the tables defined
109 % in the earlier \textsf{luatexbase} package is that these tables are
110 % defined to match the settings used by \LaTeX\ over the full Unicode
111 % range (as set in the file \textsf{unicode-letters.def}).
112 %
113 % \noindent
114 % \DescribeMacro{\SetCatcodeRange}
115 % An alias for |\@setrangecatcode| which is defined in the
116 % \textsf{ctablestack} package imported into this version of
117 % \textsf{luatexbase}. (The order of arguments is the
118 % same despite the variation in the naming). This is useful for
119 % setting up a new catcode table and assigns a given catcode to a
120 % range of characters.
121 %
122 % \noindent
123 % \DescribeMacro{\BeginCatcodeRegime}
124 % \DescribeMacro{\EndCatcodeRegime}
125 % A simple wrapper around |\@pushcatcodetable| providing a slightly
126 % different interface. The usage is:\\%
127 % \verb|\BeginCatcodeRegime|\meta{catcode table}\\
128 % \verb| |\meta{code}\\
129 % \verb|\EndCatcodeRegime|
130 %
131 % \noindent
132 % \DescribeMacro{\PushCatcodeTableNumStack}
133 % \DescribeMacro{\PopCatcodeTableNumStack}
134 % These are defined to be aliases for |\@pushcatcodetable| and
135 % |\@popcatcodetable| although the actual implementation is quite different
136 % to the older packages, the use of the commands should match.
137 %
138 % \noindent
139 % \DescribeMacro{\newluatexcatcodetable}
140 % \DescribeMacro{\setluatexcatcodetable}
141 % Aliases for the \textsf{ltluatex} functions
142 % dropping |luatex| from the name to match the convention of not
143 % using |luatex|-prefixed names for the Lua\TeX\ primitives.
144 %
145 % \subsubsection{Lua}
146 %
147 % The standard way to access catcode table numbers from Lua in
148 % \textsf{ltluatex} is the |registernumber| function. This
149 % package provides a |catcodetables| table with a metatable
150 % that accesses this function and is extended with aliases for the
151 % predefined tables so you can use |catcodetables.expl| as an
152 % alternative to |catcodetables.CatcodeTableExpl|, both being
153 % equivalent to\\
154 % |registernumber('CatcodeTableExpl')|.
155 %
156 %
157 % \subsection
158 % [Lua Callbacks]
159 % {Lua Callbacks\footnote{This
160 % interface was previously defined in the \textsf{luatexbase-mcb}
161 % sub-package.}}
162 %
163 % The |luatexbase| table is extended with some additional Lua
164 % functions to provide the interfaces provided by the previous
165 % implementation.
166 %
167 %
168 % \noindent
169 % \DescribeMacro{priority\_in\_callback}\meta{name}\meta{description}\\%
170 % As in the earlier interfaces the function
171 % is provided to return a number indicating the position of a
172 % specified function in a callback list. However it is usually used
173 % just as a boolean test that the function is registered with the
174 % callback. Kernel-level support does not directly expose the priority numbers,
175 % however the function here is defined to return the number of the specified
176 % function in the list returned by |luatexbase.callback_descriptions|.
177 %
178 %
179 % \noindent
180 % \DescribeMacro{is\_active\_callback}\meta{name}\meta{description}\\%
181 % This boolean function was defined in the development sources
182 % of the previous implementation. Here it is defined as an alias for
183 % the function |in_callback| provided by \textsf{ltluatex}.
184 % Given a callback and a description string, it returns true if a
185 % callback function with that description is currently registered.
186 %
187 % \noindent
188 % \DescribeMacro{reset\_callback}\meta{name}\meta{make\_false}\\
189 % This function unregisters all functions registered for the callback
190 % \meta{name}. If \meta{make\_false} is true, the callback is then set
191 % to false (rather than nil). Unlike the earlier implementation
192 % This version does call |remove_from_callback| on each function in
193 % the callback list for \meta{name}, and each removal will be recorded
194 % in the log.
195 %
196 % \noindent
197 % \DescribeMacro{remove\_from\_callback}\meta{name}\meta{description}\\%
198 % This function is unchanged from the kernel-level implementation.
199 % It is backward compatible with the previous \textsf{luatexbase}
200 % package but enhanced as it returns the removed callback and its
201 % description.
202 % Together with the |callback_descriptions| function this allows much
203 % finer control over the order of functions in a callback list as the
204 % functions can be removed then re-added to the list in any desired order.
205 %
206 % \noindent
207 % \DescribeMacro{add\_to\_callback}\meta{name}\meta{function}\meta{description}\meta{priority}\\%
208 % This function is defined as a wrapper around the kernel-level
209 % implementation, which does not have the fourth \meta{priority}
210 % argument.
211 %
212 % If multiple callbacks are registered to a callback of type
213 % \textsf{exclusive} then \textsf{ltluatex} raises an error, but
214 % here it is allowed if \texttt{priority} is $1$, in which case the
215 % \verb|reset_callback| is first called to remove the existing
216 % callback.
217 %
218 % In general the \texttt{priority} argument is implemented by
219 % temporarily removing some callbacks from the list and replacing
220 % them after having added the new callback.
221 %
222 % \noindent
223 % \DescribeMacro{create\_callback}\meta{name}\meta{type}\meta{default}\\%
224 % This function is unchanged from kernel-level
225 % implementation, the only change is a change of terminology for the
226 % types of callback, the type |first| is now classified as |exclusive|
227 % and the kernel code raises an error if multiple callback functions
228 % are registered. The previous \textsf{luatexbase} implementation
229 % allowed multiple functions to be registered, but only activated the
230 % first in the list.
231 %
232 % \subsection
233 % [Module declaration]
234 % {Module declaration\footnote{This
235 % interface was previously defined in the \textsf{luatexbase-modutils}
236 % sub-package.}}
237 %
238 % \subsubsection{\TeX}
239 %
240 % \noindent
241 % \DescribeMacro{\RequireLuaModule}\meta{file}\oarg{info}\\
242 % This command is provided as a wrapper around
243 % |\directlua{require(|\meta{file}|}|, and executes the Lua code in
244 % the specified file.
245 % The optional argument is accepted but ignored.
246 %
247 % Current versions of Lua\TeX\ all use the |kpse| \TeX\ path searching
248 % library with the |require| function, so the more complicated
249 % definition used in earlier implementations is no
250 % longer needed.
251 %
252 % \subsubsection{Lua}
253 %
254 % \noindent
255 % \DescribeMacro{provides\_module}\meta{info}\\
256 % The \textsf{luatexbase} version of |provides_module| returns a list of log
257 % and error functions so that it is usually called as:\\
258 % |local err, warning, info, log = luatexbase.provides_module({name=..|
259 %
260 % The returned functions are all instances of the functions provided by
261 % the kernel: |module_error|,
262 % |module_warning| and |module_info|, They all use their first argument
263 % as a format string fo rany later arguments.
264 %
265 % \DescribeMacro{errwarinf}\meta{name}\\
266 % Returns four error and warning functions associated with \meta{name}
267 % mostly a helper function for \verb|provides\_module|, but can be called
268 % separately.
269 %
270 % \subsection
271 % [Lua Attributes]
272 % {Lua Attributes and Whatsits\footnote{This
273 % interface was previously defined in the \textsf{luatexbase-attr}
274 % sub-package.}}
275 %
276 %
277 % \subsubsection{\TeX}
278 %
279 % \noindent
280 % \DescribeMacro{\newluatexattribute}
281 % \DescribeMacro{\setluatexattribute}
282 % \DescribeMacro{\unsetluatexattribute}
283 % As for catcode tables, aliases for the attribute allocation functions are
284 % provided with |luatex| in the names.
285 %
286 %
287 % \subsubsection{Lua}
288 % The lua code in this section is concerned with an experimental
289 % whatsit handling suite of functions in the original package.
290 % This is not fully documented here and is guraded by the
291 % \textsf{docstrip} guard \verb|whatsit| so it may optionally be
292 % included or excluded from the sources when the package is built.
293 %
294 % \subsection{Prefixed names for lua\TeX\ primitives}
295 % \noindent
296 % \DescribeMacro{\luatexattributedef}
297 % \DescribeMacro{\luatexcatcodetable}
298 % \DescribeMacro{\luatexluaescapestring}
299 % \DescribeMacro{\luatexlatelua}
300 % \DescribeMacro{\luatexoutputbox}
301 % \DescribeMacro{\luatexscantextokens}
302 % Aliases for commonly ued lua\TeX\ primitives that existing packages
303 % using \textsf{luatexbase} use with prefixed names.
304 %
305 % If additional primtives are required it is recommended that the
306 % code is updated to use unprefixed names. To ensure that the code
307 % works with the original \textsf{luatexbase} package on older formats
308 % you may use the lua function \texttt{tex.enableprimitives} to enable
309 % some or all primitives to be available with unprefixed names.
310 %
311 % \StopEventually{}
312 %
313 % \section{Implementation}
314 %
315 % \subsection{\textsf{luatexbase} interface}
316 %
317 % \begin{macrocode}
318 %<*emu>
319 \edef\emuatcatcode{\the\catcode`\@}
320 \catcode`\@=11
321 % \end{macrocode}
322 %
323 % Load |ctablestack|.
324 % \begin{macrocode}
325 \ifx\@setrangecatcode\@undefined
326 \ifx\RequirePackage\@undefined
327 \input{ctablestack.sty}%
328 \else
329 \RequirePackage{ctablestack}
330 \fi
331 \fi
332 % \end{macrocode}
333 %
334 % Simple require wrapper as we now assume |require| implicitly uses the
335 % |kpathsea| search library.
336 % \begin{macrocode}
337 \def\RequireLuaModule#1{\directlua{require("#1")}\@gobbleoptarg}
338 % \end{macrocode}
339 %
340 % In \LaTeX\ (or plain macro package that has defined |\@ifnextchar|)
341 % use |\@ifnextchar| otherwise use a simple alternative, in practice this
342 % will never be followed by a brace group, so full version of |\@ifnextchar|
343 % not needed.
344 % \begin{macrocode}
345 \ifdefined\@ifnextchar
346 \def\@gobbleoptarg{\@ifnextchar[\@gobble@optarg{}}%
347 \else
348 \long\def\@gobbleoptarg#1{\ifx[#1\expandafter\@gobble@optarg\fi#1}%
349 \fi
350 % \end{macrocode}
351 %
352 % \begin{macrocode}
353 \def\@gobble@optarg[#1]{}
354 % \end{macrocode}
355 %
356 % Extended catcode table support. Use the names from the previous
357 % \textsf{luatexbase} and \textsf{luatex} packages.
358 % \begin{macrocode}
359 \let\CatcodeTableIniTeX\catcodetable@initex
360 \let\CatcodeTableString\catcodetable@string
361 \let\CatcodeTableLaTeX\catcodetable@latex
362 \let\CatcodeTableLaTeXAtLetter\catcodetable@atletter
363 % \end{macrocode}
364 %
365 % Additional tables declared in the previous interface.
366 % \begin{macrocode}
367 \newcatcodetable\CatcodeTableOther
368 \@setcatcodetable\CatcodeTableOther{%
369 \catcodetable\CatcodeTableString
370 \catcode32 12 }
371 % \end{macrocode}
372 %
373 % \begin{macrocode}
374 \newcatcodetable\CatcodeTableExpl
375 \@setcatcodetable\CatcodeTableExpl{%
376 \catcodetable\CatcodeTableLaTeX
377 \catcode126 10 % tilde is a space char
378 \catcode32 9 % space is ignored
379 \catcode9 9 % tab also ignored
380 \catcode95 11 % underscore letter
381 \catcode58 11 % colon letter
382 }
383 % \end{macrocode}
384 %
385 % Top level access to catcodetable stack.
386 % \begin{macrocode}
387 \def\BeginCatcodeRegime#1{%
388 \@pushcatcodetable
389 \catcodetable#1\relax}
390 % \end{macrocode}
391 %
392 % \begin{macrocode}
393 \def\EndCatcodeRegime{%
394 \@popcatcodetable}
395 % \end{macrocode}
396 %
397 % The implementation of the stack is completely
398 % different, but usage should match.
399 % \begin{macrocode}
400 \let\PushCatcodeTableNumStack\@pushcatcodetable
401 \let\PopCatcodeTableNumStack\@popcatcodetable
402 % \end{macrocode}
403 %
404 % A simple copy.
405 % \begin{macrocode}
406 \let\SetCatcodeRange\@setrangecatcode
407 % \end{macrocode}
408 %
409 % Another copy.
410 % \begin{macrocode}
411 \let\setcatcodetable\@setcatcodetable
412 % \end{macrocode}
413 %
414 % \subsubsection{Additional lua code}
415 % \begin{macrocode}
416 \directlua{
417 % \end{macrocode}
418 %
419 % Remove all registered callbacks, then disable.
420 % Set to false if optional second argument is |true|.
421 % \begin{macrocode}
422 function luatexbase.reset_callback(name,make_false)
423 for _,v in pairs(luatexbase.callback_descriptions(name))
424 do
425 luatexbase.remove_from_callback(name,v)
426 end
427 if make_false == true then
428 luatexbase.disable_callback(name)
429 end
430 end
431 % \end{macrocode}
432 %
433 % Allow exclusive callbacks to be over-written if priority argument is
434 % 1 to match the ``first'' semantics of the original package.
435 % \changes{v1.1}{2015/10/02}{Fully handle priority argument}
436 %
437 % First save the kernel function.
438 % \begin{macrocode}
439 luatexbase.base_add_to_callback=luatexbase.add_to_callback
440 % \end{macrocode}
441 %
442 % Implement the priority argument by taking off existing callbacks
443 % that have higher priority than the new one, adding the new one,
444 % Then putting the saved callbacks back.
445 % \begin{macrocode}
446 function luatexbase.add_to_callback(name,fun,description,priority)
447 % \end{macrocode}
448 %^^A texio.write_nl('\string\n HERE: adding ' ..
449 %^^A description ..
450 %^^A ' to ' ..
451 %^^A name ..
452 %^^A ' with priority ' ..
453 %^^A (priority or '@@@'))
454 %^^A texio.write_nl('Original list')
455 %^^A for k,v in pairs(luatexbase.callback_descriptions(name)) do
456 %^^A texio.write_nl(' ' .. k .. ': ' .. v)
457 %^^A end
458 % \begin{macrocode}
459 local priority= priority
460 if priority==nil then
461 priority=\string#luatexbase.callback_descriptions(name)+1
462 end
463 if(luatexbase.callbacktypes[name] == 3 and
464 priority == 1 and
465 \string#luatexbase.callback_descriptions(name)==1) then
466 luatexbase.module_warning("luatexbase",
467 "resetting exclusive callback: " .. name)
468 luatexbase.reset_callback(name)
469 end
470 local saved_callback={},ff,dd
471 for k,v in pairs(luatexbase.callback_descriptions(name)) do
472 if k >= priority then
473 ff,dd= luatexbase.remove_from_callback(name, v)
474 saved_callback[k]={ff,dd}
475 end
476 end
477 luatexbase.base_add_to_callback(name,fun,description)
478 for k,v in pairs(saved_callback) do
479 luatexbase.base_add_to_callback(name,v[1],v[2])
480 end
481 % \end{macrocode}
482 %^^A texio.write_nl('New list')
483 %^^A for k,v in pairs(luatexbase.callback_descriptions(name)) do
484 %^^A texio.write_nl(' ' .. k .. ': ' .. v)
485 %^^A end
486 % \begin{macrocode}
487 return
488 end
489 % \end{macrocode}
490 %
491 % Emulate the catcodetables table.
492 % Explicitly fill the table rather than rely on the metatable call to
493 % |registernumber| as that is unreliable on old Lua\TeX{}.
494 % \begin{macrocode}
495 luatexbase.catcodetables=setmetatable(
496 {['latex-package'] = \number\CatcodeTableLaTeXAtLetter,
497 ini = \number\CatcodeTableIniTeX,
498 string = \number\CatcodeTableString,
499 other = \number\CatcodeTableOther,
500 latex = \number\CatcodeTableLaTeX,
501 expl = \number\CatcodeTableExpl,
502 expl3 = \number\CatcodeTableExpl},
503 { __index = function(t,key)
504 return luatexbase.registernumber(key) or nil
505 end}
506 )}
507 % \end{macrocode}
508 %
509 % On old Lua\TeX{} workaround hashtable issues.
510 % Allocate in \TeX{}, and also directly add to |luatexbase.catcodetables|.
511 % \begin{macrocode}
512 \ifnum\luatexversion<80 %
513 \def\newcatcodetable#1{%
514 \e@alloc\catcodetable\chardef
515 \e@alloc@ccodetable@count\m@ne{"8000}#1%
516 \initcatcodetable\allocationnumber
517 {\escapechar=\m@ne
518 \directlua{luatexbase.catcodetables['\string#1']=%
519 \the\allocationnumber}}%
520 }
521 \fi
522 % \end{macrocode}
523 %
524 % \begin{macrocode}
525 \directlua{
526 % \end{macrocode}
527 %
528 % |priority_in_callback| returns position in the callback list.
529 % Not provided by default by the kernel as usually it is just used
530 % as a boolean test, for which |in_callback| is provided.
531 % \begin{macrocode}
532 function luatexbase.priority_in_callback (name,description)
533 for i,v in ipairs(luatexbase.callback_descriptions(name))
534 do
535 if v == description then
536 return i
537 end
538 end
539 return false
540 end
541 % \end{macrocode}
542 %
543 % The (unreleased) version~0.7 of \textsf{luatexbase} provided this
544 % boolean test under a different name, so we provide an alias here.
545 % \begin{macrocode}
546 luatexbase.is_active_callback = luatexbase.in_callback
547 % \end{macrocode}
548 %
549 % \textsf{ltluatex} implementation of |provides_module| does not return
550 % print functions so define modified version here.
551 % \changes{v1.3}{2015/10/03}{Use the first argument as a format string for
552 % later arguments}
553 % \begin{macrocode}
554 luatexbase.base_provides_module=luatexbase.provides_module
555 function luatexbase.errwarinf(name)
556 return
557 function(s,...) return luatexbase.module_error(name, s:format(...)) end,
558 function(s,...) return luatexbase.module_warning(name, s:format(...)) end,
559 function(s,...) return luatexbase.module_info(name, s:format(...)) end,
560 function(s,...) return luatexbase.module_info(name, s:format(...)) end
561 end
562 function luatexbase.provides_module(info)
563 luatexbase.base_provides_module(info)
564 return luatexbase.errwarinf(info.name)
565 end
566 }
567 % \end{macrocode}
568 %
569 % Same for attribute table as catcode tables. In old Lua\TeX{}, add to the
570 % |luatexbase.attributes| table directly.
571 % \begin{macrocode}
572 \ifnum\luatexversion<80 %
573 \def\newattribute#1{%
574 \e@alloc\attribute\attributedef
575 \e@alloc@attribute@count\m@ne\e@alloc@top#1%
576 {\escapechar=\m@ne
577 \directlua{luatexbase.attributes['\string#1']=%
578 \the\allocationnumber}}%
579 }
580 \fi
581 % \end{macrocode}
582 %
583 % Define a safe percent command for plain \TeX.
584 % \begin{macrocode}
585 \ifx\@percentchar\@undefined
586 {\catcode`\%=12 \gdef\@percentchar{%}}
587 \fi
588 % \end{macrocode}
589 % \changes{v1.2a}{2015/10/03}{Add missing local definitions for whatsit code}
590 %
591 % \begin{macrocode}
592 %<*whatsit>
593 \directlua{%
594 % \end{macrocode}
595 %
596 % \begin{macrocode}
597 local copynode = node.copy
598 local newnode = node.new
599 local nodesubtype = node.subtype
600 local nodetype = node.id
601 local stringformat = string.format
602 local tableunpack = unpack or table.unpack
603 local texiowrite_nl = texio.write_nl
604 local texiowrite = texio.write
605 local whatsit_t = nodetype"whatsit"
606 local user_defined_t = nodesubtype"user_defined"
607 local unassociated = "__unassociated"
608 local user_whatsits = { __unassociated = { } }
609 local whatsit_ids = { }
610 local anonymous_whatsits = 0
611 local anonymous_prefix = "anon"
612 % \end{macrocode}
613 %
614 % User whatsit allocation is split into two functions:
615 % \verb|new_user_whatsit_id| registers a new id (an integer)
616 % and returns it. This is a wrapper around \verb|new_whatsit|
617 % but with the extra \texttt{package} argument, and recording
618 % the mapping in lua tables
619 %
620 % If no name given, generate a name from a counter.
621 %
622 % \begin{macrocode}
623 local new_user_whatsit_id = function (name, package)
624 if name then
625 if not package then
626 package = unassociated
627 end
628 else % anonymous
629 anonymous_whatsits = anonymous_whatsits + 1
630 warning("defining anonymous user whatsit no. \@percentchar
631 d", anonymous_whatsits)
632 package = unassociated
633 name = anonymous_prefix .. tostring(anonymous_whatsits)
634 end
635
636 local whatsitdata = user_whatsits[package]
637 if not whatsitdata then
638 whatsitdata = { }
639 user_whatsits[package] = whatsitdata
640 end
641
642 local id = whatsitdata[name]
643 if id then %- warning
644 warning("replacing whatsit \@percentchar s:\@percentchar
645 s (\@percentchar d)", package, name, id)
646 else %- new id
647 id=luatexbase.new_whatsit(name)
648 whatsitdata[name] = id
649 whatsit_ids[id] = { name, package }
650 end
651 return id
652 end
653 luatexbase.new_user_whatsit_id = new_user_whatsit_id
654 % \end{macrocode}
655 %
656 % \verb|new_user_whatsit| first registers a new id and then also
657 % creates the corresponding whatsit node of subtype “user-defined”.
658 % Return a nullary function that delivers copies of the whatsit.
659 %
660 % Alternatively, the first argument can be a whatsit node that
661 % will then be used as prototype.
662 %
663 % \begin{macrocode}
664 local new_user_whatsit = function (req, package)
665 local id, whatsit
666 if type(req) == "string" then
667 id = new_user_whatsit_id(req, package)
668 whatsit = newnode(whatsit_t, user_defined_t)
669 whatsit.user_id = id
670 elseif req.id == whatsit_t and req.subtype == user_defined_t then
671 id = req.user_id
672 whatsit = copynode(req)
673 if not whatsit_ids[id] then
674 warning("whatsit id \@percentchar d unregistered; "
675 .. "inconsistencies may arise", id)
676 end
677 end
678 return function () return copynode(whatsit) end, id
679 end
680 luatexbase.new_user_whatsit = new_user_whatsit
681 % \end{macrocode}
682 %
683 % If one knows the name of a user whatsit, its corresponding id
684 % can be retrieved by means of \verb|get_user_whatsit_id|.
685 %
686 % \begin{macrocode}
687 local get_user_whatsit_id = function (name, package)
688 if not package then
689 package = unassociated
690 end
691 return user_whatsits[package][name]
692 end
693 luatexbase.get_user_whatsit_id = get_user_whatsit_id
694 % \end{macrocode}
695 %
696 % The inverse lookup is also possible via \verb|get_user_whatsit_name|.
697 % \begin{macrocode}
698 local get_user_whatsit_name = function (asked)
699 local id
700 if type(asked) == "number" then
701 id = asked
702 elseif type(asked) == "function" then
703 %- node generator
704 local n = asked()
705 id = n.user_id
706 else %- node
707 id = asked.user_id
708 end
709 local metadata = whatsit_ids[id]
710 if not metadata then % unknown
711 warning("whatsit id \@percentchar d unregistered;
712 inconsistencies may arise", id)
713 return "", ""
714 end
715 return tableunpack(metadata)
716 end
717 luatexbase.get_user_whatsit_name = get_user_whatsit_name
718 % \end{macrocode}
719 % A function that outputs the
720 % current allocation status to the terminal.
721 %
722 % \begin{macrocode}
723 local dump_registered_whatsits = function (asked_package)
724 local whatsit_list = { }
725 if asked_package then
726 local whatsitdata = user_whatsits[asked_package]
727 if not whatsitdata then
728 error("(no user whatsits registered for package
729 \@percentchar s)", asked_package)
730 return
731 end
732 texiowrite_nl("(user whatsit allocation stats for " ..
733 asked_package)
734 for name, id in next, whatsitdata do
735 whatsit_list[\string#whatsit_list+1] =
736 stringformat("(\@percentchar s:\@percentchar
737 s \@percentchar d)", asked_package, name, id)
738 end
739 else
740 texiowrite_nl("(user whatsit allocation stats")
741 texiowrite_nl(stringformat(" ((total \@percentchar d)\string\n
742 (anonymous \@percentchar d))",
743 current_whatsit, anonymous_whatsits))
744 for package, whatsitdata in next, user_whatsits do
745 for name, id in next, whatsitdata do
746 whatsit_list[\string#whatsit_list+1] =
747 stringformat("(\@percentchar s:\@percentchar
748 s \@percentchar d)", package, name, id)
749 end
750 end
751 end
752 texiowrite_nl" ("
753 local first = true
754 for i=1, \string#whatsit_list do
755 if first then
756 first = false
757 else % indent
758 texiowrite_nl" "
759 end
760 texiowrite(whatsit_list[i])
761 end
762 texiowrite"))\string\n"
763 end
764 luatexbase.dump_registered_whatsits = dump_registered_whatsits
765 % \end{macrocode}
766 % Lastly, we define a couple synonyms for convenience.
767 % \begin{macrocode}
768 luatexbase.newattribute = new_attribute
769 luatexbase.newuserwhatsit = new_user_whatsit
770 luatexbase.newuserwhatsitid = new_user_whatsit_id
771 luatexbase.getuserwhatsitid = get_user_whatsit_id
772 luatexbase.getuserwhatsitname = get_user_whatsit_name
773 luatexbase.dumpregisteredwhatsits = dump_registered_whatsits
774 % \end{macrocode}
775 %
776 % \begin{macrocode}
777 }
778 %</whatsit>
779 % \end{macrocode}
780 %
781 % Resolve name clashes and prefixed name issues.
782 %
783 % Top level \textsf{luatexbase} macros
784 % \begin{macrocode}
785 \let\newluatexattribute\newattribute
786 \let\setluatexattribute\setattribute
787 \let\unsetluatexattribute\unsetattribute
788 \let\newluatexcatcodetable\newcatcodetable
789 \let\setluatexcatcodetable\setcatcodetable
790 % \end{macrocode}
791 %
792 % Internal \textsf{luatexbase} macros
793 % \begin{macrocode}
794 \let\luatexbase@directlua\directlua
795 \let\luatexbase@ensure@primitive\@gobble
796 % \end{macrocode}
797 %
798 % Lua\TeX\ primitives
799 % \begin{macrocode}
800 \let\luatexattribute\attribute
801 \let\luatexattributedef\attributedef
802 \let\luatexcatcodetable\catcodetable
803 \let\luatexluaescapestring\luaescapestring
804 \let\luatexlatelua\latelua
805 \let\luatexoutputbox\outputbox
806 \let\luatexscantextokens\scantextokens
807 % \end{macrocode}
808 %
809 % Reset catcode of |@|.
810 % \begin{macrocode}
811 \catcode`\@=\emuatcatcode\relax
812 % \end{macrocode}
813 %
814 % \begin{macrocode}
815 %</emu>
816 % \end{macrocode}
817 %
818 % \subsection{Legacy \textsf{luatexbase} sub-packages}
819 %
820 % The original \textsf{luatexbase} was comprised of seven sub packages
821 % that could in principle be loaded separately. Here we define them all
822 % with the same code that just loads the main package, they are
823 % distinguished just by the |\ProvidesPackage| specified above at the start
824 % of the file.
825 % \begin{macrocode}
826 %<*emu-cmp,emu-mod,emu-loa,emu-reg,emu-att,emu-cct,emu-mcb>
827 % \end{macrocode}
828 %
829 % \begin{macrocode}
830 \ifx\RequirePackage\undefined
831 \input{luatexbase.sty}%
832 \else
833 \RequirePackage{luatexbase}
834 \fi
835 % \end{macrocode}
836 %
837 % \begin{macrocode}
838 %</emu-cmp,emu-mod,emu-loa,emu-reg,emu-att,emu-cct,emu-mcb>
839 % \end{macrocode}
840 %
841 % \subsection{Legacy Lua code}
842 %
843 % \changes{v1.2}{2015/10/03}{Provide \texttt{luatexbase.loader.lua}}
844 %
845 % The original \textsf{luatexbase} included a file |luatexbase.loader.lua|
846 % that could be loaded independently of the rest of the package. This really
847 % doesn't need to do anything!
848 % \begin{macrocode}
849 %<*emu-lua>
850 % \end{macrocode}
851 %
852 % \begin{macrocode}
853 luatexbase = luatexbase or { }
854 % \end{macrocode}
855 %
856 % \begin{macrocode}
857 %</emu-lua>
858 % \end{macrocode}
859 %
860 % \Finale