1 % \iffalse meta-comment
4 % The LaTeX3 Project and any individual authors listed elsewhere
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:
12 % http://www.latex-project.org/lppl.txt
16 %<2ekernel>%%% From File: ltluatex.dtx
17 %<plain>\ifx\newluafunction\undefined\else\expandafter\endinput\fi
19 %<tex> \ProvidesFile\undefined\begingroup\def\ProvidesFile
20 %<tex> #1#2[#3]{\endgroup\immediate\write-1{File: #1 #3}}
22 %<plain>\ProvidesFile{ltluatex.tex}
24 \ProvidesFile{ltluatex.dtx}
29 %<plain> LuaTeX support for plain TeX (core)
32 \edef\etatcatcode{\the\catcode`\@}
36 \documentclass{ltxdoc}
37 \GetFileInfo{ltluatex.dtx}
39 \title{\filename\\(Lua\TeX{}-specific support)}
40 \author{David Carlisle and Joseph Wright\footnote{Significant portions
41 of the code here are adapted/simplified from the packages \textsf{luatex} and
42 \textsf{luatexbase} written by Heiko Oberdiek, \'{E}lie Roux,
43 Manuel P\'{e}gouri\'{e}-Gonnar and Philipp Gesang.}}
46 \setcounter{tocdepth}{2}
56 % Lua\TeX{} adds a number of engine-specific functions to \TeX{}. Several of
57 % these require set up that is best done in the kernel or need related support
58 % functions. This file provides \emph{basic} support for Lua\TeX{} at the
59 % \LaTeXe{} kernel level plus as a loadable file which can be used with
60 % plain \TeX{} and \LaTeX{}.
62 % This file contains code for both \TeX{} (to be stored as part of the format)
63 % and Lua (to be loaded at the start of each job). In the Lua code, the kernel
64 % uses the namespace |luatexbase|.
66 % The following |\count| registers are used here for register allocation:
68 % \item[\texttt{\string\e@alloc@attribute@count}] Attributes (default~258)
69 % \item[\texttt{\string\e@alloc@ccodetable@count}] Category code tables
71 % \item[\texttt{\string\e@alloc@luafunction@count}] Lua functions
73 % \item[\texttt{\string\e@alloc@whatsit@count}] User whatsits (default~261)
74 % \item[\texttt{\string\e@alloc@bytecode@count}] Lua bytecodes (default~262)
75 % \item[\texttt{\string\e@alloc@luachunk@count}] Lua chunks (default~263)
77 % (|\count 256| is used for |\newmarks| allocation and |\count 257|
78 % is used for\linebreak
79 % |\newXeTeXintercharclass| with Xe\TeX{}, with code defined in
80 % \texttt{ltfinal.dtx}).
81 % With any \LaTeXe{} kernel from 2015 onward these registers are part of
82 % the block in the extended area reserved by the kernel (prior to 2015 the
83 % \LaTeXe{} kernel did not provide any functionality for the extended
86 % \section{Core \TeX{} functionality}
88 % The commands defined here are defined for
89 % possible inclusion in a future \LaTeX{} format, however also extracted
90 % to the file |ltluatex.tex| which may be used with older \LaTeX\
91 % formats, and with plain \TeX.
94 % \DescribeMacro{\newattribute}
95 % |\newattribute{|\meta{attribute}|}|\\
96 % Defines a named \cs{attribute}, indexed from~$1$
97 % (\emph{i.e.}~|\attribute0| is never defined). Attributes initially
98 % have the marker value |-"7FFFFFFF| (`unset') set by the engine.
101 % \DescribeMacro{\newcatcodetable}
102 % |\newcatcodetable{|\meta{catcodetable}|}|\\
103 % Defines a named \cs{catcodetable}, indexed from~$1$
104 % (|\catcodetable0| is never assigned). A new catcode table will be
105 % populated with exactly those values assigned by Ini\TeX{} (as described
106 % in the Lua\TeX{} manual).
109 % \DescribeMacro{\newluafunction}
110 % |\newluafunction{|\meta{function}|}|\\
111 % Defines a named \cs{luafunction}, indexed from~$1$. (Lua indexes
112 % tables from $1$ so |\luafunction0| is not available).
115 % \DescribeMacro{\newwhatsit}
116 % |\newwhatsit{|\meta{whatsit}|}|\\
117 % Defines a custom \cs{whatsit}, indexed from~$1$.
120 % \DescribeMacro{\newluabytecode}
121 % |\newluabytecode{|\meta{bytecode}|}|\\
122 % Allocates a number for Lua bytecode register, indexed from~$1$.
125 % \DescribeMacro{\newluachunkname}
126 % |newluachunkname{|\meta{chunkname}|}|\\
127 % Allocates a number for Lua chunk register, indexed from~$1$.
128 % Also enters the name of the regiser (without backslash) into the
129 % \verb|lua.name| table to be used in stack traces.
132 % \DescribeMacro{\catcodetable@initex}
133 % \DescribeMacro{\catcodetable@string}
134 % \DescribeMacro{\catcodetable@latex}
135 % \DescribeMacro{\catcodetable@atletter}
136 % Predefined category code tables with the obvious assignments. Note
137 % that the |latex| and |atletter| tables set the full Unicode range
138 % to the codes predefined by the kernel.
141 % \DescribeMacro{\setattribute}
142 % \DescribeMacro{\unsetattribute}
143 % |\setattribute{|\meta{attribute}|}{|\meta{value}|}|\\
144 % |\unsetattribute{|\meta{attribute}|}|\\
145 % Set and unset attributes in a manner analogous to |\setlength|. Note that
146 % attributes take a marker value when unset so this operation is distinct
147 % from setting the value to zero.
149 % \section{Plain \TeX\ interface}
151 % The \textsf{ltluatex} interface may be used with plain \TeX\ using
152 % |\input{ltluatex}|. This inputs |ltluatex.tex| which inputs
153 % |etex.src| (or |etex.sty| if used with \LaTeX)
154 % if it is not already input, and then defines some internal commands to
155 % allow the \textsf{ltluatex} interface to be defined.
157 % The \textsf{luatexbase} package interface may also be used in plain \TeX,
158 % as before, by inputting the package |\input luatexbase.sty|. The new
159 % version of \textsf{luatexbase} is based on this \textsf{ltluatex}
160 % code but implements a compatibility layer providing the interface
161 % of the original package.
163 % \section{Lua functionality}
167 % \begingroup\lccode`~=`_
168 % \lowercase{\endgroup\let~}_
171 % \subsection{Allocators in Lua}
173 % \DescribeMacro{new_attribute}
174 % |luatexbase.new_attribute(|\meta{attribute}|)|\\
175 % Returns an allocation number for the \meta{attribute}, indexed from~$1$.
176 % The attribute will be initialised with the marker value |-"7FFFFFFF|
177 % (`unset'). The attribute allocation sequence is shared with the \TeX{}
178 % code but this function does \emph{not} define a token using
180 % The attribute name is recorded in the |attributes| table. A
181 % metatable is provided so that the table syntax can be used
182 % consistently for attributes declared in \TeX\ or Lua.
185 % \DescribeMacro{new_whatsit}
186 % |luatexbase.new_whatsit(|\meta{whatsit}|)|\\
187 % Returns an allocation number for the custom \meta{whatsit}, indexed from~$1$.
190 % \DescribeMacro{new_bytecode}
191 % |luatexbase.new_bytecode(|\meta{bytecode}|)|\\
192 % Returns an allocation number for a bytecode register, indexed from~$1$.
193 % The optional \meta{name} argument is just used for logging.
196 % \DescribeMacro{new_chunkname}
197 % |luatexbase.new_chunkname(|\meta{chunkname}|)|\\
198 % Returns an allocation number for a Lua chunk name for use with
199 % |\directlua| and |\latelua|, indexed from~$1$.
200 % The number is returned and also \meta{name} argument is added to the
201 % |lua.name| array at that index.
203 % These functions all require access to a named \TeX{} count register
204 % to manage their allocations. The standard names are those defined
205 % above for access from \TeX{}, \emph{e.g.}~\string\e@alloc@attribute@count,
206 % but these can be adjusted by defining the variable
207 % \texttt{\meta{type}\_count\_name} before loading |ltluatex.lua|, for example
209 % local attribute_count_name = "attributetracker"
210 % require("ltluatex")
212 % would use a \TeX{} |\count| (|\countdef|'d token) called |attributetracker|
213 % in place of \string\e@alloc@attribute@count.
215 % \subsection{Lua access to \TeX{} register numbers}
217 % \DescribeMacro{registernumber}
218 % |luatexbase.registernumer(|\meta{name}|)|\\
219 % Sometimes (notably in the case of Lua attributes) it is necessary to
220 % access a register \emph{by number} that has been allocated by \TeX{}.
221 % This package provides a function to look up the relevant number
222 % using Lua\TeX{}'s internal tables. After for example
223 % |\newattribute\myattrib|, |\myattrib| would be defined by (say)
224 % |\myattrib=\attribute15|. |luatexbase.registernumer("myattrib")|
225 % would then return the register number, $15$ in this case. If the string passed
226 % as argument does not correspond to a token defined by |\attributedef|,
227 % |\countdef| or similar commands, the Lua value |false| is returned.
229 % As an example, consider the input:
231 % \newcommand\test[1]{%
232 % \typeout{#1: \expandafter\meaning\csname#1\endcsname^^J
233 % \space\space\space\space
234 % \directlua{tex.write(luatexbase.registernumber("#1") or "bad input")}%
237 % \test{undefinedrubbish}
252 % \attrbutedef\myattr=12
258 % If the demonstration code is processed with Lua\LaTeX{} then the following
259 % would be produced in the log and terminal output.
261 % undefinedrubbish: \relax
267 % @MM: \mathchar"4E20
269 % @tempdima: \dimen14
271 % @tempdimb: \dimen15
277 % myattr: \attribute12
281 % Notice how undefined commands, or commands unrelated to registers
282 % do not produce an error, just return |false| and so print
283 % |bad input| here. Note also that commands defined by |\newbox| work and
284 % return the number of the box register even though the actual command
285 % holding this number is a |\chardef| defined token (there is no
288 % \subsection{Module utilities}
290 % \DescribeMacro{provides_module}
291 % |luatexbase.provides_module(|\meta{info}|)|\\
292 % This function is used by modules to identify themselves; the |info| should be
293 % a table containing information about the module. The required field
294 % |name| must contain the name of the module. It is recommended to provide a
295 % field |date| in the usual \LaTeX{} format |yyyy/mm/dd|. Optional fields
296 % |version| (a string) and |description| may be used if present. This
297 % information will be recorded in the log. Other fields are ignored.
300 % \DescribeMacro{module_info}
301 % \DescribeMacro{module_warning}
302 % \DescribeMacro{module_error}
303 % |luatexbase.module_info(|\meta{module}, \meta{text}|)|\\
304 % |luatexbase.module_warning(|\meta{module}, \meta{text}|)|\\
305 % |luatexbase.module_error(|\meta{module}, \meta{text}|)|\\
306 % These functions are similar to \LaTeX{}'s |\PackageError|, |\PackageWarning|
307 % and |\PackageInfo| in the way they format the output. No automatic line
308 % breaking is done, you may still use |\n| as usual for that, and the name of
309 % the package will be prepended to each output line.
311 % Note that |luatexbase.module_error| raises an actual Lua error with |error()|,
312 % which currently means a call stack will be dumped. While this may not
313 % look pretty, at least it provides useful information for tracking the
316 % \subsection{Callback management}
319 % \DescribeMacro{add_to_callback}
320 % |luatexbase.add_to_callback(|^^A
321 % \meta{callback}, \meta{function}, \meta{description}|)|
322 % Registers the \meta{function} into the \meta{callback} with a textual
323 % \meta{description} of the function. Functions are inserted into the callback
324 % in the order loaded.
327 % \DescribeMacro{remove_from_callback}
328 % |luatexbase.remove_from_callback(|\meta{callback}, \meta{description}|)|
329 % Removes the callback function with \meta{description} from the \meta{callback}.
330 % The removed function and its description
331 % are returned as the results of this function.
334 % \DescribeMacro{in_callback}
335 % |luatexbase.in_callback(|\meta{callback}, \meta{description}|)|
336 % Checks if the \meta{description} matches one of the functions added
337 % to the list for the \meta{callback}, returning a boolean value.
340 % \DescribeMacro{disable_callback}
341 % |luatexbase.disable_callback(|\meta{callback}|)|
342 % Sets the \meta{callback} to \texttt{false} as described in the Lua\TeX{}
343 % manual for the underlying \texttt{callback.register} built-in. Callbacks
344 % will only be set to false (and thus be skipped entirely) if there are
345 % no functions registered using the callback.
348 % \DescribeMacro{callback_descriptions}
349 % A list of the descriptions of functions registered to the specified
350 % callback is returned. |{}| is returned if there are no functions registered.
353 % \DescribeMacro{create_callback}
354 % |luatexbase.create_callback(|\meta{name},meta{type},\meta{default}|)|
355 % Defines a user defined callback. The last argument is a default
356 % function or |false|.
359 % \DescribeMacro{call_callback}
360 % |luatexbase.call_callback(|\meta{name},\ldots|)|
361 % Calls a user defined callback with the supplied arguments.
367 % \section{Implementation}
370 %<*2ekernel|tex|latexrelease>
371 %<2ekernel|latexrelease>\ifx\directlua\@undefined\else
375 % \changes{v1.0j}{2015/12/02}{Remove nonlocal iteration variables (PHG)}
376 % \changes{v1.0j}{2015/12/02}{Assorted typos fixed (PHG)}
377 % \changes{v1.0j}{2015/12/02}{Remove unreachable code after calls to error() (PHG)}
378 % \subsection{Minimum Lua\TeX{} version}
380 % Lua\TeX{} has changed a lot over time. In the kernel support for ancient
381 % versions is not provided: trying to build a format with a very old binary
382 % therefore gives some information in the log and loading stops. The cut-off
383 % selected here relates to the tree-searching behaviour of |require()|:
384 % from version~0.60, Lua\TeX{} will correctly find Lua files in the |texmf|
385 % tree without `help'.
387 %<latexrelease>\IncludeInRelease{2015/10/01}
388 %<latexrelease> {\newluafunction}{LuaTeX}%
389 \ifnum\luatexversion<60 %
390 \wlog{***************************************************}
391 \wlog{* LuaTeX version too old for ltluatex support *}
392 \wlog{***************************************************}
393 \expandafter\endinput
397 % \subsection{Older \LaTeX{}/Plain \TeX\ setup}
403 % Older \LaTeX{} formats don't have the primitives with `native' names:
404 % sort that out. If they already exist this will still be safe.
406 \directlua{tex.enableprimitives("",tex.extraprimitives("luatex"))}
410 \ifx\e@alloc\@undefined
413 % In pre-2014 \LaTeX{}, or plain \TeX{}, load |etex.{sty,src}|.
415 \ifx\documentclass\@undefined
416 \ifx\loccount\@undefined
420 \outer\expandafter\def\csname newfam\endcsname
421 {\alloc@8\fam\chardef\et@xmaxfam}
423 \RequirePackage{etex}
424 \expandafter\def\csname newfam\endcsname
425 {\alloc@8\fam\chardef\et@xmaxfam}
426 \expandafter\let\expandafter\new@mathgroup\csname newfam\endcsname
430 % \subsubsection{Fixes to \texttt{etex.src}/\texttt{etex.sty}}
432 % These could and probably should be made directly in an
433 % update to |etex.src| which already has some Lua\TeX-specific
434 % code, but does not define the correct range for Lua\TeX.
436 % 2015-07-13 higher range in luatex.
438 \edef \et@xmaxregs {\ifx\directlua\@undefined 32768\else 65536\fi}
440 % luatex/xetex also allow more math fam.
442 \edef \et@xmaxfam {\ifx\Umathchar\@undefined\sixt@@n\else\@cclvi\fi}
446 \count 270=\et@xmaxregs % locally allocates \count registers
447 \count 271=\et@xmaxregs % ditto for \dimen registers
448 \count 272=\et@xmaxregs % ditto for \skip registers
449 \count 273=\et@xmaxregs % ditto for \muskip registers
450 \count 274=\et@xmaxregs % ditto for \box registers
451 \count 275=\et@xmaxregs % ditto for \toks registers
452 \count 276=\et@xmaxregs % ditto for \marks classes
455 % and 256 or 16 fam. (Done above due to plain/\LaTeX\ differences in
456 % \textsf{ltluatex}.)
458 % \outer\def\newfam{\alloc@8\fam\chardef\et@xmaxfam}
461 % End of proposed changes to \texttt{etex.src}
463 % \subsubsection{luatex specific settings}
465 % Switch to global cf |luatex.sty| to leave room for inserts
466 % not really needed for luatex but possibly most compatible
469 \expandafter\let\csname newcount\expandafter\expandafter\endcsname
470 \csname globcount\endcsname
471 \expandafter\let\csname newdimen\expandafter\expandafter\endcsname
472 \csname globdimen\endcsname
473 \expandafter\let\csname newskip\expandafter\expandafter\endcsname
474 \csname globskip\endcsname
475 \expandafter\let\csname newbox\expandafter\expandafter\endcsname
476 \csname globbox\endcsname
479 % Define|\e@alloc| as in latex (the existing macros in |etex.src|
480 % hard to extend to further register types as they assume specific
481 % 26x and 27x count range. For compatibility the existing register
482 % allocation is not changed.
485 \chardef\e@alloc@top=65535
486 \let\e@alloc@chardef\chardef
490 \def\e@alloc#1#2#3#4#5#6{%
491 \global\advance#3\@ne
492 \e@ch@ck{#3}{#4}{#5}#1%
493 \allocationnumber#3\relax
494 \global#2#6\allocationnumber
495 \wlog{\string#6=\string#1\the\allocationnumber}}%
499 \gdef\e@ch@ck#1#2#3#4{%
503 \ifx\count#4\advance#1 10 \fi
507 \errmessage{No room for a new \string#4}%
512 % Two simple \LaTeX\ macros used in |ltlatex.sty|.
514 \long\def\@gobble#1{}
515 \long\def\@firstofone#1{#1}
518 % Fix up allocations not to clash with |etex.src|.
521 \expandafter\csname newcount\endcsname\e@alloc@attribute@count
522 \expandafter\csname newcount\endcsname\e@alloc@ccodetable@count
523 \expandafter\csname newcount\endcsname\e@alloc@luafunction@count
524 \expandafter\csname newcount\endcsname\e@alloc@whatsit@count
525 \expandafter\csname newcount\endcsname\e@alloc@bytecode@count
526 \expandafter\csname newcount\endcsname\e@alloc@luachunk@count
529 % End of conditional setup for plain \TeX\ / old \LaTeX.
536 % \subsection{Attributes}
538 % \begin{macro}{\newattribute}
539 % \changes{v1.0a}{2015/09/24}{Macro added}
540 % As is generally the case for the Lua\TeX{} registers we start here
541 % from~$1$. Notably, some code assumes that |\attribute0| is never used so
542 % this is important in this case.
544 \ifx\e@alloc@attribute@count\@undefined
545 \countdef\e@alloc@attribute@count=258
547 \def\newattribute#1{%
548 \e@alloc\attribute\attributedef
549 \e@alloc@attribute@count\m@ne\e@alloc@top#1%
551 \e@alloc@attribute@count=\z@
555 % \begin{macro}{\setattribute}
556 % \begin{macro}{\unsetattribute}
559 \def\setattribute#1#2{#1=\numexpr#2\relax}
560 \def\unsetattribute#1{#1=-"7FFFFFFF\relax}
565 % \subsection{Category code tables}
567 % \begin{macro}{\newcatcodetable}
568 % \changes{v1.0a}{2015/09/24}{Macro added}
569 % Category code tables are allocated with a limit half of that used by Lua\TeX{}
570 % for everything else. At the end of allocation there needs to be an
571 % initialisation step. Table~$0$ is already taken (it's the global one for
572 % current use) so the allocation starts at~$1$.
574 \ifx\e@alloc@ccodetable@count\@undefined
575 \countdef\e@alloc@ccodetable@count=259
577 \def\newcatcodetable#1{%
578 \e@alloc\catcodetable\chardef
579 \e@alloc@ccodetable@count\m@ne{"8000}#1%
580 \initcatcodetable\allocationnumber
582 \e@alloc@ccodetable@count=\z@
586 % \changes{v1.0l}{2015/12/18}{Load Unicode data from source}
587 % \begin{macro}{\catcodetable@initex}
588 % \changes{v1.0a}{2015/09/24}{Macro added}
589 % \begin{macro}{\catcodetable@string}
590 % \changes{v1.0a}{2015/09/24}{Macro added}
591 % \begin{macro}{\catcodetable@latex}
592 % \changes{v1.0a}{2015/09/24}{Macro added}
593 % \begin{macro}{\catcodetable@atletter}
594 % \changes{v1.0a}{2015/09/24}{Macro added}
595 % Save a small set of standard tables. The Unicode data is read
596 % here in using a parser simplified from that in |load-unicode-data|:
597 % only the nature of letters needs to be detected.
599 \newcatcodetable\catcodetable@initex
600 \newcatcodetable\catcodetable@string
602 \def\setrangecatcode#1#2#3{%
606 \expandafter\@firstofone
610 \expandafter\setrangecatcode\expandafter
611 {\number\numexpr#1 + 1\relax}{#2}{#3}
615 \catcodetable\catcodetable@initex
619 \setrangecatcode{65}{90}{12}%
620 \setrangecatcode{97}{122}{12}%
623 \savecatcodetable\catcodetable@string
626 \newcatcodetable\catcodetable@latex
627 \newcatcodetable\catcodetable@atletter
629 \def\parseunicodedataI#1;#2;#3;#4\relax{%
630 \parseunicodedataII#1;#3;#2 First>\relax
632 \def\parseunicodedataII#1;#2;#3 First>#4\relax{%
634 \expandafter\parseunicodedataIII
636 \expandafter\parseunicodedataIV
640 \def\parseunicodedataIII#1#2#3\relax{%
648 \def\parseunicodedataIV#1#2#3\relax{%
649 \read\unicoderead to \unicodedataline
652 \expandafter\parseunicodedataV\unicodedataline\relax
655 \def\parseunicodedataV#1;#2\relax{%
657 \unless\ifnum\count0>"#1 %
659 \advance\count0 by 1 %
662 \def\storedpar{\par}%
663 \chardef\unicoderead=\numexpr\count16 + 1\relax
664 \openin\unicoderead=UnicodeData.txt %
665 \loop\unless\ifeof\unicoderead %
666 \read\unicoderead to \unicodedataline
667 \unless\ifx\unicodedataline\storedpar
668 \expandafter\parseunicodedataI\unicodedataline\relax
674 \savecatcodetable\catcodetable@latex
676 \savecatcodetable\catcodetable@atletter
685 % \subsection{Named Lua functions}
687 % \begin{macro}{\newluafunction}
688 % \changes{v1.0a}{2015/09/24}{Macro added}
689 % Much the same story for allocating Lua\TeX{} functions except here they are
690 % just numbers so they are allocated in the same way as boxes.
691 % Lua indexes from~$1$ so once again slot~$0$ is skipped.
693 \ifx\e@alloc@luafunction@count\@undefined
694 \countdef\e@alloc@luafunction@count=260
696 \def\newluafunction{%
697 \e@alloc\luafunction\e@alloc@chardef
698 \e@alloc@luafunction@count\m@ne\e@alloc@top
700 \e@alloc@luafunction@count=\z@
704 % \subsection{Custom whatsits}
706 % \begin{macro}{\newwhatsit}
707 % \changes{v1.0a}{2015/09/24}{Macro added}
708 % These are only settable from Lua but for consistency are definable
711 \ifx\e@alloc@whatsit@count\@undefined
712 \countdef\e@alloc@whatsit@count=261
715 \e@alloc\whatsit\e@alloc@chardef
716 \e@alloc@whatsit@count\m@ne\e@alloc@top#1%
718 \e@alloc@whatsit@count=\z@
722 % \subsection{Lua bytecode registers}
724 % \begin{macro}{\newluabytecode}
725 % \changes{v1.0a}{2015/09/24}{Macro added}
726 % These are only settable from Lua but for consistency are definable
729 \ifx\e@alloc@bytecode@count\@undefined
730 \countdef\e@alloc@bytecode@count=262
732 \def\newluabytecode#1{%
733 \e@alloc\luabytecode\e@alloc@chardef
734 \e@alloc@bytecode@count\m@ne\e@alloc@top#1%
736 \e@alloc@bytecode@count=\z@
740 % \subsection{Lua chunk registers}
742 % \begin{macro}{\newluachunkname}
743 % \changes{v1.0a}{2015/09/24}{Macro added}
744 % As for bytecode registers, but in addition we need to add a string
745 % to the \verb|lua.name| table to use in stack tracing. We use the
746 % name of the command passed to the allocator, with no backslash.
748 \ifx\e@alloc@luachunk@count\@undefined
749 \countdef\e@alloc@luachunk@count=263
751 \def\newluachunkname#1{%
752 \e@alloc\luachunk\e@alloc@chardef
753 \e@alloc@luachunk@count\m@ne\e@alloc@top#1%
755 \directlua{lua.name[\the\allocationnumber]="\string#1"}}%
757 \e@alloc@luachunk@count=\z@
761 % \subsection{Lua loader}
763 % Load the Lua code at the start of every job.
764 % For the conversion of \TeX{} into numbers at the Lua side we need some
765 % known registers: for convenience we use a set of systematic names, which
766 % means using a group around the Lua loader.
768 %<2ekernel>\everyjob\expandafter{%
769 %<2ekernel> \the\everyjob
771 \attributedef\attributezero=0 %
772 \chardef \charzero =0 %
774 % Note name change required on older luatex, for hash table access.
776 \countdef \CountZero =0 %
777 \dimendef \dimenzero =0 %
778 \mathchardef \mathcharzero =0 %
779 \muskipdef \muskipzero =0 %
780 \skipdef \skipzero =0 %
781 \toksdef \tokszero =0 %
782 \directlua{require("ltluatex")}
785 %<latexrelease>\EndIncludeInRelease
788 % \changes{v1.0b}{2015/10/02}{Fix backing out of \TeX{} code}
789 % \changes{v1.0c}{2015/10/02}{Allow backing out of Lua code}
791 %<latexrelease>\IncludeInRelease{0000/00/00}
792 %<latexrelease> {\newluafunction}{LuaTeX}%
793 %<latexrelease>\let\e@alloc@attribute@count\@undefined
794 %<latexrelease>\let\newattribute\@undefined
795 %<latexrelease>\let\setattribute\@undefined
796 %<latexrelease>\let\unsetattribute\@undefined
797 %<latexrelease>\let\e@alloc@ccodetable@count\@undefined
798 %<latexrelease>\let\newcatcodetable\@undefined
799 %<latexrelease>\let\catcodetable@initex\@undefined
800 %<latexrelease>\let\catcodetable@string\@undefined
801 %<latexrelease>\let\catcodetable@latex\@undefined
802 %<latexrelease>\let\catcodetable@atletter\@undefined
803 %<latexrelease>\let\e@alloc@luafunction@count\@undefined
804 %<latexrelease>\let\newluafunction\@undefined
805 %<latexrelease>\let\e@alloc@luafunction@count\@undefined
806 %<latexrelease>\let\newwhatsit\@undefined
807 %<latexrelease>\let\e@alloc@whatsit@count\@undefined
808 %<latexrelease>\let\newluabytecode\@undefined
809 %<latexrelease>\let\e@alloc@bytecode@count\@undefined
810 %<latexrelease>\let\newluachunkname\@undefined
811 %<latexrelease>\let\e@alloc@luachunk@count\@undefined
812 %<latexrelease>\directlua{luatexbase.uninstall()}
813 %<latexrelease>\EndIncludeInRelease
816 % In \verb|\everyjob|, if luaotfload is available, load it and switch to TU.
818 %<latexrelease>\IncludeInRelease{2017/01/01}%
819 %<latexrelease> {\fontencoding}{TU in everyjob}%
820 %<latexrelease>\fontencoding{TU}\let\encodingdefault\f@encoding
821 %<latexrelease>\ifx\directlua\@undefined\else
822 %<2ekernel>\everyjob\expandafter{%
823 %<2ekernel> \the\everyjob
824 %<*2ekernel,latexrelease>
826 if xpcall(function ()%
827 require('luaotfload-main')%
828 end,texio.write_nl) then %
829 local _void = luaotfload.main ()%
831 texio.write_nl('Error in luaotfload: reverting to OT1')%
832 tex.print('\string\\def\string\\encodingdefault{OT1}')%
835 \let\f@encoding\encodingdefault
836 \expandafter\let\csname ver@luaotfload.sty\endcsname\fmtversion
837 %</2ekernel,latexrelease>
840 %<latexrelease>\EndIncludeInRelease
841 %<latexrelease>\IncludeInRelease{0000/00/00}%
842 %<latexrelease> {\fontencoding}{TU in everyjob}%
843 %<latexrelease>\fontencoding{OT1}\let\encodingdefault\f@encoding
844 %<latexrelease>\EndIncludeInRelease
848 %<2ekernel|latexrelease>\fi
849 %</2ekernel|tex|latexrelease>
852 % \subsection{Lua module preliminaries}
856 % \begingroup\lccode`~=`_
857 % \lowercase{\endgroup\let~}_
864 % Some set up for the Lua module which is needed for all of the Lua
865 % functionality added here.
867 % \begin{macro}{luatexbase}
868 % \changes{v1.0a}{2015/09/24}{Table added}
869 % Set up the table for the returned functions. This is used to expose
870 % all of the public functions.
872 luatexbase = luatexbase or { }
873 local luatexbase = luatexbase
877 % Some Lua best practice: use local versions of functions where possible.
879 local string_gsub = string.gsub
880 local tex_count = tex.count
881 local tex_setattribute = tex.setattribute
882 local tex_setcount = tex.setcount
883 local texio_write_nl = texio.write_nl
885 % \changes{v1.0i}{2015/11/29}{Declare this as local before used in the module error definitions (PHG)}
887 local luatexbase_warning
888 local luatexbase_error
891 % \subsection{Lua module utilities}
893 % \subsubsection{Module tracking}
895 % \begin{macro}{modules}
896 % \changes{v1.0a}{2015/09/24}{Function modified}
897 % To allow tracking of module usage, a structure is provided to store
898 % information and to return it.
900 local modules = modules or { }
904 % \begin{macro}{provides_module}
905 % \changes{v1.0a}{2015/09/24}{Function added}
906 % \changes{v1.0f}{2015/10/03}{use luatexbase\_log}
907 % Local function to write to the log.
909 local function luatexbase_log(text)
910 texio_write_nl("log", text)
914 % Modelled on |\ProvidesPackage|, we store much the same information but
915 % with a little more structure.
917 local function provides_module(info)
918 if not (info and info.name) then
919 luatexbase_error("Missing module name for provides_module")
921 local function spaced(text)
922 return text and (" " .. text) or ""
925 "Lua module: " .. info.name
927 .. spaced(info.version)
928 .. spaced(info.description)
930 modules[info.name] = info
932 luatexbase.provides_module = provides_module
936 % \subsubsection{Module messages}
938 % There are various warnings and errors that need to be given. For warnings
939 % we can get exactly the same formatting as from \TeX{}. For errors we have to
940 % make some changes. Here we give the text of the error in the \LaTeX{} format
941 % then force an error from Lua to halt the run. Splitting the message text is
942 % done using |\n| which takes the place of |\MessageBreak|.
944 % First an auxiliary for the formatting: this measures up the message
945 % leader so we always get the correct indent.
946 % \changes{v1.0j}{2015/12/02}{Declaration/use of first\_head fixed (PHG)}
948 local function msg_format(mod, msg_type, text)
952 if mod == "LaTeX" then
953 cont = string_gsub(leader, ".", " ")
954 first_head = leader .. "LaTeX: "
956 first_head = leader .. "Module " .. msg_type
957 cont = "(" .. mod .. ")"
958 .. string_gsub(first_head, ".", " ")
959 first_head = leader .. "Module " .. mod .. " " .. msg_type .. ":"
961 if msg_type == "Error" then
962 first_head = "\n" .. first_head
964 if string.sub(text,-1) ~= "\n" then
967 return first_head .. " "
971 .. tex.inputlineno, "\n", "\n" .. cont .. " "
977 % \begin{macro}{module_info}
978 % \changes{v1.0a}{2015/09/24}{Function added}
979 % \begin{macro}{module_warning}
980 % \changes{v1.0a}{2015/09/24}{Function added}
981 % \begin{macro}{module_error}
982 % \changes{v1.0a}{2015/09/24}{Function added}
985 local function module_info(mod, text)
986 texio_write_nl("log", msg_format(mod, "Info", text))
988 luatexbase.module_info = module_info
989 local function module_warning(mod, text)
990 texio_write_nl("term and log",msg_format(mod, "Warning", text))
992 luatexbase.module_warning = module_warning
993 local function module_error(mod, text)
994 error(msg_format(mod, "Error", text))
996 luatexbase.module_error = module_error
1002 % Dedicated versions for the rest of the code here.
1004 function luatexbase_warning(text)
1005 module_warning("luatexbase", text)
1007 function luatexbase_error(text)
1008 module_error("luatexbase", text)
1013 % \subsection{Accessing register numbers from Lua}
1015 % \changes{v1.0g}{2015/11/14}{Track Lua\TeX{} changes for
1016 % \texttt{(new)token.create}}
1017 % Collect up the data from the \TeX{} level into a Lua table: from
1018 % version~0.80, Lua\TeX{} makes that easy.
1019 % \changes{v1.0j}{2015/12/02}{Adjust hashtokens to store the result of tex.hashtokens()), not the function (PHG)}
1021 local luaregisterbasetable = { }
1022 local registermap = {
1023 attributezero = "assign_attr" ,
1024 charzero = "char_given" ,
1025 CountZero = "assign_int" ,
1026 dimenzero = "assign_dimen" ,
1027 mathcharzero = "math_given" ,
1028 muskipzero = "assign_mu_skip" ,
1029 skipzero = "assign_skip" ,
1030 tokszero = "assign_toks" ,
1033 if tex.luatexversion > 81 then
1034 createtoken = token.create
1035 elseif tex.luatexversion > 79 then
1036 createtoken = newtoken.create
1038 local hashtokens = tex.hashtokens()
1039 local luatexversion = tex.luatexversion
1040 for i,j in pairs (registermap) do
1041 if luatexversion < 80 then
1042 luaregisterbasetable[hashtokens[i][1]] =
1045 luaregisterbasetable[j] = createtoken(i).mode
1050 % \begin{macro}{registernumber}
1051 % Working out the correct return value can be done in two ways. For older
1052 % Lua\TeX{} releases it has to be extracted from the |hashtokens|. On the
1053 % other hand, newer Lua\TeX{}'s have |newtoken|, and whilst |.mode| isn't
1054 % currently documented, Hans Hagen pointed to this approach so we should be
1057 local registernumber
1058 if luatexversion < 80 then
1059 function registernumber(name)
1060 local nt = hashtokens[name]
1061 if(nt and luaregisterbasetable[nt[1]]) then
1062 return nt[2] - luaregisterbasetable[nt[1]]
1068 function registernumber(name)
1069 local nt = createtoken(name)
1070 if(luaregisterbasetable[nt.cmdname]) then
1071 return nt.mode - luaregisterbasetable[nt.cmdname]
1077 luatexbase.registernumber = registernumber
1081 % \subsection{Attribute allocation}
1083 % \begin{macro}{new_attribute}
1084 % \changes{v1.0a}{2015/09/24}{Function added}
1085 % \changes{v1.1c}{2017/02/18}{Parameterise count used in tracking}
1086 % As attributes are used for Lua manipulations its useful to be able
1087 % to assign from this end.
1089 local attributes=setmetatable(
1092 __index = function(t,key)
1093 return registernumber(key) or nil
1096 luatexbase.attributes = attributes
1100 local attribute_count_name =
1101 attribute_count_name or "e@alloc@attribute@count"
1102 local function new_attribute(name)
1103 tex_setcount("global", attribute_count_name,
1104 tex_count[attribute_count_name] + 1)
1105 if tex_count[attribute_count_name] > 65534 then
1106 luatexbase_error("No room for a new \\attribute")
1108 attributes[name]= tex_count[attribute_count_name]
1109 luatexbase_log("Lua-only attribute " .. name .. " = " ..
1110 tex_count[attribute_count_name])
1111 return tex_count[attribute_count_name]
1113 luatexbase.new_attribute = new_attribute
1117 % \subsection{Custom whatsit allocation}
1119 % \begin{macro}{new_whatsit}
1120 % \changes{v1.1c}{2017/02/18}{Parameterise count used in tracking}
1121 % Much the same as for attribute allocation in Lua.
1123 local whatsit_count_name = whatsit_count_name or "e@alloc@whatsit@count"
1124 local function new_whatsit(name)
1125 tex_setcount("global", whatsit_count_name,
1126 tex_count[whatsit_count_name] + 1)
1127 if tex_count[whatsit_count_name] > 65534 then
1128 luatexbase_error("No room for a new custom whatsit")
1130 luatexbase_log("Custom whatsit " .. (name or "") .. " = " ..
1131 tex_count[whatsit_count_name])
1132 return tex_count[whatsit_count_name]
1134 luatexbase.new_whatsit = new_whatsit
1138 % \subsection{Bytecode register allocation}
1140 % \begin{macro}{new_bytecode}
1141 % \changes{v1.1c}{2017/02/18}{Parameterise count used in tracking}
1142 % Much the same as for attribute allocation in Lua.
1143 % The optional \meta{name} argument is used in the log if given.
1145 local bytecode_count_name =
1146 bytecode_count_name or "e@alloc@bytecode@count"
1147 local function new_bytecode(name)
1148 tex_setcount("global", bytecode_count_name,
1149 tex_count[bytecode_count_name] + 1)
1150 if tex_count[bytecode_count_name] > 65534 then
1151 luatexbase_error("No room for a new bytecode register")
1153 luatexbase_log("Lua bytecode " .. (name or "") .. " = " ..
1154 tex_count[bytecode_count_name])
1155 return tex_count[bytecode_count_name]
1157 luatexbase.new_bytecode = new_bytecode
1161 % \subsection{Lua chunk name allocation}
1163 % \begin{macro}{new_chunkname}
1164 % \changes{v1.1c}{2017/02/18}{Parameterise count used in tracking}
1165 % As for bytecode registers but also store the name in the
1168 local chunkname_count_name =
1169 chunkname_count_name or "e@alloc@luachunk@count"
1170 local function new_chunkname(name)
1171 tex_setcount("global", chunkname_count_name,
1172 tex_count[chunkname_count_name] + 1)
1173 local chunkname_count = tex_count[chunkname_count_name]
1174 chunkname_count = chunkname_count + 1
1175 if chunkname_count > 65534 then
1176 luatexbase_error("No room for a new chunkname")
1178 lua.name[chunkname_count]=name
1179 luatexbase_log("Lua chunkname " .. (name or "") .. " = " ..
1180 chunkname_count .. "\n")
1181 return chunkname_count
1183 luatexbase.new_chunkname = new_chunkname
1187 % \subsection{Lua callback management}
1189 % The native mechanism for callbacks in Lua\TeX\ allows only one per function.
1190 % That is extremely restrictive and so a mechanism is needed to add and
1191 % remove callbacks from the appropriate hooks.
1193 % \subsubsection{Housekeeping}
1195 % The main table: keys are callback names, and values are the associated lists
1196 % of functions. More precisely, the entries in the list are tables holding the
1197 % actual function as |func| and the identifying description as |description|.
1198 % Only callbacks with a non-empty list of functions have an entry in this
1201 local callbacklist = callbacklist or { }
1204 % Numerical codes for callback types, and name-to-value association (the
1205 % table keys are strings, the values are numbers).
1207 local list, data, exclusive, simple = 1, 2, 3, 4
1211 exclusive = exclusive,
1216 % Now, list all predefined callbacks with their current type, based on the
1217 % Lua\TeX{} manual version~1.01. A full list of the currently-available
1218 % callbacks can be obtained using
1221 % for i,_ in pairs(callback.list()) do
1222 % texio.write_nl("- " .. i)
1227 % in plain Lua\TeX{}. (Some undocumented callbacks are omitted as they are
1230 local callbacktypes = callbacktypes or {
1232 % Section 8.2: file discovery callbacks.
1234 find_read_file = exclusive,
1235 find_write_file = exclusive,
1236 find_font_file = data,
1237 find_output_file = data,
1238 find_format_file = data,
1239 find_vf_file = data,
1240 find_map_file = data,
1241 find_enc_file = data,
1242 find_sfd_file = data,
1243 find_pk_file = data,
1244 find_data_file = data,
1245 find_opentype_file = data,
1246 find_truetype_file = data,
1247 find_type1_file = data,
1248 find_image_file = data,
1252 open_read_file = exclusive,
1253 read_font_file = exclusive,
1254 read_vf_file = exclusive,
1255 read_map_file = exclusive,
1256 read_enc_file = exclusive,
1257 read_sfd_file = exclusive,
1258 read_pk_file = exclusive,
1259 read_data_file = exclusive,
1260 read_truetype_file = exclusive,
1261 read_type1_file = exclusive,
1262 read_opentype_file = exclusive,
1264 % \changes{v1.0m}{2016/02/11}{read\_cidmap\_file added}
1265 % Not currently used by luatex but included for completeness.
1266 % may be used by a font handler.
1268 find_cidmap_file = data,
1269 read_cidmap_file = exclusive,
1271 % Section 8.3: data processing callbacks.
1272 % \changes{v1.0m}{2016/02/11}{token\_filter removed}
1274 process_input_buffer = data,
1275 process_output_buffer = data,
1276 process_jobname = data,
1278 % Section 8.4: node list processing callbacks.
1279 % \changes{v1.0m}{2016/02/11}
1280 % {process\_rule, [hv]pack\_quality append\_to\_vlist\_filter added}
1281 % \changes{v1.0n}{2016/03/13}{insert\_local\_par added}
1282 % \changes{v1.0n}{2016/03/13}{contribute\_filter added}
1284 contribute_filter = simple,
1285 buildpage_filter = simple,
1286 build_page_insert = exclusive,
1287 pre_linebreak_filter = list,
1288 linebreak_filter = list,
1289 append_to_vlist_filter = list,
1290 post_linebreak_filter = list,
1291 hpack_filter = list,
1292 vpack_filter = list,
1293 hpack_quality = list,
1294 vpack_quality = list,
1295 pre_output_filter = list,
1296 process_rule = list,
1298 ligaturing = simple,
1300 insert_local_par = simple,
1301 mlist_to_hlist = list,
1303 % Section 8.5: information reporting callbacks.
1304 % \changes{v1.0m}{2016/02/11}{show\_warning\_message added}
1305 % \changes{v1.0p}{2016/11/17}{call\_edit added}
1310 start_page_number = simple,
1311 stop_page_number = simple,
1312 show_error_hook = simple,
1313 show_warning_message = simple,
1314 show_error_message = simple,
1315 show_lua_error_hook = simple,
1316 start_file = simple,
1320 % Section 8.6: PDF-related callbacks.
1322 finish_pdffile = data,
1323 finish_pdfpage = data,
1325 % Section 8.7: font-related callbacks.
1326 % \changes{v1.1e}{2017/03/28}{glyph\_stream\_provider added}
1328 define_font = exclusive,
1329 glyph_stream_provider = exclusive,
1331 % \changes{v1.0m}{2016/02/11}{pdf\_stream\_filter\_callback removed}
1334 luatexbase.callbacktypes=callbacktypes
1337 % \begin{macro}{callback.register}
1338 % \changes{v1.0a}{2015/09/24}{Function modified}
1339 % Save the original function for registering callbacks and prevent the
1340 % original being used. The original is saved in a place that remains
1341 % available so other more sophisticated code can override the approach
1342 % taken by the kernel if desired.
1344 local callback_register = callback_register or callback.register
1345 function callback.register()
1346 luatexbase_error("Attempt to use callback.register() directly\n")
1351 % \subsubsection{Handlers}
1353 % The handler function is registered into the callback when the
1354 % first function is added to this callback's list. Then, when the callback
1355 % is called, the handler takes care of running all functions in the list.
1356 % When the last function is removed from the callback's list, the handler
1359 % More precisely, the functions below are used to generate a specialized
1360 % function (closure) for a given callback, which is the actual handler.
1363 % The way the functions are combined together depends on
1364 % the type of the callback. There are currently 4 types of callback, depending
1365 % on the calling convention of the functions the callback can hold:
1366 % \begin{description}
1367 % \item[simple] is for functions that don't return anything: they are called
1368 % in order, all with the same argument;
1369 % \item[data] is for functions receiving a piece of data of any type
1370 % except node list head (and possibly other arguments) and returning it
1371 % (possibly modified): the functions are called in order, and each is
1372 % passed the return value of the previous (and the other arguments
1373 % untouched, if any). The return value is that of the last function;
1374 % \item[list] is a specialized variant of \emph{data} for functions
1375 % filtering node lists. Such functions may return either the head of a
1376 % modified node list, or the boolean values |true| or |false|. The
1377 % functions are chained the same way as for \emph{data} except that for
1379 % one function returns |false|, then |false| is immediately returned and
1380 % the following functions are \emph{not} called. If one function returns
1381 % |true|, then the same head is passed to the next function. If all
1382 % functions return |true|, then |true| is returned, otherwise the return
1383 % value of the last function not returning |true| is used.
1384 % \item[exclusive] is for functions with more complex signatures; functions in
1385 % this type of callback are \emph{not} combined: An error is raised if
1386 % a second callback is registered..
1389 % Handler for |data| callbacks.
1391 local function data_handler(name)
1392 return function(data, ...)
1393 for _,i in ipairs(callbacklist[name]) do
1394 data = i.func(data,...)
1400 % Handler for |exclusive| callbacks. We can assume |callbacklist[name]| is not
1401 % empty: otherwise, the function wouldn't be registered in the callback any
1404 local function exclusive_handler(name)
1405 return function(...)
1406 return callbacklist[name][1].func(...)
1410 % Handler for |list| callbacks.
1411 % \changes{v1.0k}{2015/12/02}{resolve name and i.description (PHG)}
1413 local function list_handler(name)
1414 return function(head, ...)
1416 local alltrue = true
1417 for _,i in ipairs(callbacklist[name]) do
1418 ret = i.func(head, ...)
1419 if ret == false then
1421 "Function `" .. i.description .. "' returned false\n"
1422 .. "in callback `" .. name .."'"
1431 return alltrue and true or head
1435 % Handler for |simple| callbacks.
1437 local function simple_handler(name)
1438 return function(...)
1439 for _,i in ipairs(callbacklist[name]) do
1446 % Keep a handlers table for indexed access.
1449 [data] = data_handler,
1450 [exclusive] = exclusive_handler,
1451 [list] = list_handler,
1452 [simple] = simple_handler,
1456 % \subsubsection{Public functions for callback management}
1458 % Defining user callbacks perhaps should be in package code,
1459 % but impacts on |add_to_callback|.
1460 % If a default function is not required, it may be declared as |false|.
1461 % First we need a list of user callbacks.
1463 local user_callbacks_defaults = { }
1466 % \begin{macro}{create_callback}
1467 % \changes{v1.0a}{2015/09/24}{Function added}
1468 % \changes{v1.0i}{2015/11/29}{Check name is not nil in error message (PHG)}
1469 % \changes{v1.0k}{2015/12/02}{Give more specific error messages (PHG)}
1470 % The allocator itself.
1472 local function create_callback(name, ctype, default)
1473 if not name or name == ""
1474 or not ctype or ctype == ""
1476 luatexbase_error("Unable to create callback:\n" ..
1477 "valid callback name and type required")
1479 if callbacktypes[name] then
1480 luatexbase_error("Unable to create callback `" .. name ..
1481 "':\ncallback is already defined")
1483 if default ~= false and type (default) ~= "function" then
1484 luatexbase_error("Unable to create callback `" .. name ..
1485 ":\ndefault is not a function")
1487 user_callbacks_defaults[name] = default
1488 callbacktypes[name] = types[ctype]
1490 luatexbase.create_callback = create_callback
1494 % \begin{macro}{call_callback}
1495 % \changes{v1.0a}{2015/09/24}{Function added}
1496 % \changes{v1.0i}{2015/11/29}{Check name is not nil in error message (PHG)}
1497 % \changes{v1.0k}{2015/12/02}{Give more specific error messages (PHG)}
1498 % Call a user defined callback. First check arguments.
1500 local function call_callback(name,...)
1501 if not name or name == "" then
1502 luatexbase_error("Unable to create callback:\n" ..
1503 "valid callback name required")
1505 if user_callbacks_defaults[name] == nil then
1506 luatexbase_error("Unable to call callback `" .. name
1507 .. "':\nunknown or empty")
1509 local l = callbacklist[name]
1512 f = user_callbacks_defaults[name]
1517 f = handlers[callbacktypes[name]](name)
1521 luatexbase.call_callback=call_callback
1525 % \begin{macro}{add_to_callback}
1526 % \changes{v1.0a}{2015/09/24}{Function added}
1527 % Add a function to a callback. First check arguments.
1528 % \changes{v1.0k}{2015/12/02}{Give more specific error messages (PHG)}
1530 local function add_to_callback(name, func, description)
1531 if not name or name == "" then
1532 luatexbase_error("Unable to register callback:\n" ..
1533 "valid callback name required")
1535 if not callbacktypes[name] or
1536 type(func) ~= "function" or
1538 description == "" then
1540 "Unable to register callback.\n\n"
1541 .. "Correct usage:\n"
1542 .. "add_to_callback(<callback>, <function>, <description>)"
1546 % Then test if this callback is already in use. If not, initialise its list
1547 % and register the proper handler.
1549 local l = callbacklist[name]
1552 callbacklist[name] = l
1554 % If it is not a user defined callback use the primitive callback register.
1556 if user_callbacks_defaults[name] == nil then
1557 callback_register(name, handlers[callbacktypes[name]](name))
1561 % Actually register the function and give an error if more than one
1562 % |exclusive| one is registered.
1566 description = description,
1568 local priority = #l + 1
1569 if callbacktypes[name] == exclusive then
1572 "Cannot add second callback to exclusive function\n`" ..
1576 table.insert(l, priority, f)
1578 % Keep user informed.
1581 "Inserting `" .. description .. "' at position "
1582 .. priority .. " in `" .. name .. "'."
1585 luatexbase.add_to_callback = add_to_callback
1589 % \begin{macro}{remove_from_callback}
1590 % \changes{v1.0a}{2015/09/24}{Function added}
1591 % \changes{v1.0k}{2015/12/02}{adjust initialisation of cb local (PHG)}
1592 % \changes{v1.0k}{2015/12/02}{Give more specific error messages (PHG)}
1593 % Remove a function from a callback. First check arguments.
1595 local function remove_from_callback(name, description)
1596 if not name or name == "" then
1597 luatexbase_error("Unable to remove function from callback:\n" ..
1598 "valid callback name required")
1600 if not callbacktypes[name] or
1602 description == "" then
1604 "Unable to remove function from callback.\n\n"
1605 .. "Correct usage:\n"
1606 .. "remove_from_callback(<callback>, <description>)"
1609 local l = callbacklist[name]
1612 "No callback list for `" .. name .. "'\n")
1615 % Loop over the callback's function list until we find a matching entry.
1616 % Remove it and check if the list is empty: if so, unregister the
1620 for i,j in ipairs(l) do
1621 if j.description == description then
1628 "No callback `" .. description .. "' registered for `" ..
1632 table.remove(l, index)
1634 "Removing `" .. description .. "' from `" .. name .. "'."
1637 callbacklist[name] = nil
1638 callback_register(name, nil)
1640 return cb.func,cb.description
1642 luatexbase.remove_from_callback = remove_from_callback
1646 % \begin{macro}{in_callback}
1647 % \changes{v1.0a}{2015/09/24}{Function added}
1648 % \changes{v1.0h}{2015/11/27}{Guard against undefined list latex/4445}
1649 % Look for a function description in a callback.
1651 local function in_callback(name, description)
1654 or not callbacklist[name]
1655 or not callbacktypes[name]
1656 or not description then
1659 for _, i in pairs(callbacklist[name]) do
1660 if i.description == description then
1666 luatexbase.in_callback = in_callback
1670 % \begin{macro}{disable_callback}
1671 % \changes{v1.0a}{2015/09/24}{Function added}
1672 % As we subvert the engine interface we need to provide a way to access
1673 % this functionality.
1675 local function disable_callback(name)
1676 if(callbacklist[name] == nil) then
1677 callback_register(name, false)
1679 luatexbase_error("Callback list for " .. name .. " not empty")
1682 luatexbase.disable_callback = disable_callback
1686 % \begin{macro}{callback_descriptions}
1687 % \changes{v1.0a}{2015/09/24}{Function added}
1688 % \changes{v1.0h}{2015/11/27}{Match test in in-callback latex/4445}
1689 % List the descriptions of functions registered for the given callback.
1691 local function callback_descriptions (name)
1695 or not callbacklist[name]
1696 or not callbacktypes[name]
1700 for k, i in pairs(callbacklist[name]) do
1706 luatexbase.callback_descriptions =callback_descriptions
1710 % \begin{macro}{uninstall}
1711 % \changes{v1.0e}{2015/10/02}{Function added}
1712 % Unlike at the \TeX{} level, we have to provide a back-out mechanism here
1713 % at the same time as the rest of the code. This is not meant for use by
1714 % anything other than \textsf{latexrelease}: as such this is
1715 % \emph{deliberately} not documented for users!
1717 local function uninstall()
1720 "Uninstalling kernel luatexbase code"
1722 callback.register = callback_register
1725 luatexbase.uninstall = uninstall
1734 % Reset the catcode of |@|.
1736 %<tex>\catcode`\@=\etatcatcode\relax