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.
437 % 2015-07-13 higher range in luatex
438 \edef \et@xmaxregs {\ifx\directlua\@undefined 32768\else 65536\fi}
439 % luatex/xetex also allow more math fam
440 \edef \et@xmaxfam {\ifx\Umathchar\@undefined\sixt@@n\else\@cclvi\fi}
444 \count 270=\et@xmaxregs % locally allocates \count registers
445 \count 271=\et@xmaxregs % ditto for \dimen registers
446 \count 272=\et@xmaxregs % ditto for \skip registers
447 \count 273=\et@xmaxregs % ditto for \muskip registers
448 \count 274=\et@xmaxregs % ditto for \box registers
449 \count 275=\et@xmaxregs % ditto for \toks registers
450 \count 276=\et@xmaxregs % ditto for \marks classes
453 % and 256 or 16 fam. (Done above due to plain/\LaTeX\ differences in
454 % \textsf{ltluatex}.)
456 % \outer\def\newfam{\alloc@8\fam\chardef\et@xmaxfam}
459 % End of proposed changes to \texttt{etex.src}
461 % \subsubsection{luatex specific settings}
463 % Switch to global cf |luatex.sty| to leave room for inserts
464 % not really needed for luatex but possibly most compatible
467 \expandafter\let\csname newcount\expandafter\expandafter\endcsname
468 \csname globcount\endcsname
469 \expandafter\let\csname newdimen\expandafter\expandafter\endcsname
470 \csname globdimen\endcsname
471 \expandafter\let\csname newskip\expandafter\expandafter\endcsname
472 \csname globskip\endcsname
473 \expandafter\let\csname newbox\expandafter\expandafter\endcsname
474 \csname globbox\endcsname
477 % Define|\e@alloc| as in latex (the existing macros in |etex.src|
478 % hard to extend to further register types as they assume specific
479 % 26x and 27x count range. For compatibility the existing register
480 % allocation is not changed.
483 \chardef\e@alloc@top=65535
484 \let\e@alloc@chardef\chardef
488 \def\e@alloc#1#2#3#4#5#6{%
489 \global\advance#3\@ne
490 \e@ch@ck{#3}{#4}{#5}#1%
491 \allocationnumber#3\relax
492 \global#2#6\allocationnumber
493 \wlog{\string#6=\string#1\the\allocationnumber}}%
497 \gdef\e@ch@ck#1#2#3#4{%
501 \ifx\count#4\advance#1 10 \fi
505 \errmessage{No room for a new \string#4}%
510 % Two simple \LaTeX\ macros used in |ltlatex.sty|.
512 \long\def\@gobble#1{}
513 \long\def\@firstofone#1{#1}
517 % 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
789 % \changes{v1.0b}{2015/10/02}{Fix backing out of \TeX{} code}
790 % \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 %% Horrible hack, locally reset the luatex version number
827 %% This is not required for the source version of luaotfload
828 %% but is required due to an error in the version check in the
829 %% public version (January 2017)
830 %% https://github.com/lualatex/luaotfload/issues/387
831 %% It is expected that this will be removed before TeXLive 2017
832 local tmp_version=tex.luatexversion %
833 tex.luatexversion=199 %
834 if xpcall(function ()%
835 require('luaotfload-main')%
836 end,texio.write_nl) then %
837 local _void = luaotfload.main ()%
839 texio.write_nl('Error in luaotfload: reverting to OT1')%
840 tex.print('\string\\def\string\\encodingdefault{OT1}')%
842 tex.luatexversion=tmp_version%
844 \let\f@encoding\encodingdefault
845 \expandafter\let\csname ver@luaotfload.sty\endcsname\fmtversion
846 %</2ekernel,latexrelease>
849 %<latexrelease>\EndIncludeInRelease
850 %<latexrelease>\IncludeInRelease{0000/00/00}%
851 %<latexrelease> {\fontencoding}{TU in everyjob}%
852 %<latexrelease>\fontencoding{OT1}\let\encodingdefault\f@encoding
853 %<latexrelease>\EndIncludeInRelease
857 %<2ekernel|latexrelease>\fi
858 %</2ekernel|tex|latexrelease>
861 % \subsection{Lua module preliminaries}
865 % \begingroup\lccode`~=`_
866 % \lowercase{\endgroup\let~}_
873 % Some set up for the Lua module which is needed for all of the Lua
874 % functionality added here.
876 % \begin{macro}{luatexbase}
877 % \changes{v1.0a}{2015/09/24}{Table added}
878 % Set up the table for the returned functions. This is used to expose
879 % all of the public functions.
881 luatexbase = luatexbase or { }
882 local luatexbase = luatexbase
886 % Some Lua best practice: use local versions of functions where possible.
888 local string_gsub = string.gsub
889 local tex_count = tex.count
890 local tex_setattribute = tex.setattribute
891 local tex_setcount = tex.setcount
892 local texio_write_nl = texio.write_nl
894 % \changes{v1.0i}{2015/11/29}{Declare this as local before used in the module error definitions (PHG)}
896 local luatexbase_warning
897 local luatexbase_error
900 % \subsection{Lua module utilities}
902 % \subsubsection{Module tracking}
904 % \begin{macro}{modules}
905 % \changes{v1.0a}{2015/09/24}{Function modified}
906 % To allow tracking of module usage, a structure is provided to store
907 % information and to return it.
909 local modules = modules or { }
913 % \begin{macro}{provides\_module}
914 % \changes{v1.0a}{2015/09/24}{Function added}
915 % \changes{v1.0f}{2015/10/03}{use luatexbase\_log}
916 % Local function to write to the log.
918 local function luatexbase_log(text)
919 texio_write_nl("log", text)
923 % Modelled on |\ProvidesPackage|, we store much the same information but
924 % with a little more structure.
926 local function provides_module(info)
927 if not (info and info.name) then
928 luatexbase_error("Missing module name for provides_module")
930 local function spaced(text)
931 return text and (" " .. text) or ""
934 "Lua module: " .. info.name
936 .. spaced(info.version)
937 .. spaced(info.description)
939 modules[info.name] = info
941 luatexbase.provides_module = provides_module
945 % \subsubsection{Module messages}
947 % There are various warnings and errors that need to be given. For warnings
948 % we can get exactly the same formatting as from \TeX{}. For errors we have to
949 % make some changes. Here we give the text of the error in the \LaTeX{} format
950 % then force an error from Lua to halt the run. Splitting the message text is
951 % done using |\n| which takes the place of |\MessageBreak|.
953 % First an auxiliary for the formatting: this measures up the message
954 % leader so we always get the correct indent.
955 % \changes{v1.0j}{2015/12/02}{Declaration/use of first\_head fixed (PHG)}
957 local function msg_format(mod, msg_type, text)
961 if mod == "LaTeX" then
962 cont = string_gsub(leader, ".", " ")
963 first_head = leader .. "LaTeX: "
965 first_head = leader .. "Module " .. msg_type
966 cont = "(" .. mod .. ")"
967 .. string_gsub(first_head, ".", " ")
968 first_head = leader .. "Module " .. mod .. " " .. msg_type .. ":"
970 if msg_type == "Error" then
971 first_head = "\n" .. first_head
973 if string.sub(text,-1) ~= "\n" then
976 return first_head .. " "
980 .. tex.inputlineno, "\n", "\n" .. cont .. " "
986 % \begin{macro}{module\_info}
987 % \changes{v1.0a}{2015/09/24}{Function added}
988 % \begin{macro}{module\_warning}
989 % \changes{v1.0a}{2015/09/24}{Function added}
990 % \begin{macro}{module\_error}
991 % \changes{v1.0a}{2015/09/24}{Function added}
994 local function module_info(mod, text)
995 texio_write_nl("log", msg_format(mod, "Info", text))
997 luatexbase.module_info = module_info
998 local function module_warning(mod, text)
999 texio_write_nl("term and log",msg_format(mod, "Warning", text))
1001 luatexbase.module_warning = module_warning
1002 local function module_error(mod, text)
1003 error(msg_format(mod, "Error", text))
1005 luatexbase.module_error = module_error
1011 % Dedicated versions for the rest of the code here.
1013 function luatexbase_warning(text)
1014 module_warning("luatexbase", text)
1016 function luatexbase_error(text)
1017 module_error("luatexbase", text)
1022 % \subsection{Accessing register numbers from Lua}
1024 % \changes{v1.0g}{2015/11/14}{Track Lua\TeX{} changes for
1025 % \texttt{(new)token.create}}
1026 % Collect up the data from the \TeX{} level into a Lua table: from
1027 % version~0.80, Lua\TeX{} makes that easy.
1028 % \changes{v1.0j}{2015/12/02}{Adjust hashtokens to store the result of tex.hashtokens()), not the function (PHG)}
1030 local luaregisterbasetable = { }
1031 local registermap = {
1032 attributezero = "assign_attr" ,
1033 charzero = "char_given" ,
1034 CountZero = "assign_int" ,
1035 dimenzero = "assign_dimen" ,
1036 mathcharzero = "math_given" ,
1037 muskipzero = "assign_mu_skip" ,
1038 skipzero = "assign_skip" ,
1039 tokszero = "assign_toks" ,
1042 if tex.luatexversion > 81 then
1043 createtoken = token.create
1044 elseif tex.luatexversion > 79 then
1045 createtoken = newtoken.create
1047 local hashtokens = tex.hashtokens()
1048 local luatexversion = tex.luatexversion
1049 for i,j in pairs (registermap) do
1050 if luatexversion < 80 then
1051 luaregisterbasetable[hashtokens[i][1]] =
1054 luaregisterbasetable[j] = createtoken(i).mode
1059 % \begin{macro}{registernumber}
1060 % Working out the correct return value can be done in two ways. For older
1061 % Lua\TeX{} releases it has to be extracted from the |hashtokens|. On the
1062 % other hand, newer Lua\TeX{}'s have |newtoken|, and whilst |.mode| isn't
1063 % currently documented, Hans Hagen pointed to this approach so we should be
1066 local registernumber
1067 if luatexversion < 80 then
1068 function registernumber(name)
1069 local nt = hashtokens[name]
1070 if(nt and luaregisterbasetable[nt[1]]) then
1071 return nt[2] - luaregisterbasetable[nt[1]]
1077 function registernumber(name)
1078 local nt = createtoken(name)
1079 if(luaregisterbasetable[nt.cmdname]) then
1080 return nt.mode - luaregisterbasetable[nt.cmdname]
1086 luatexbase.registernumber = registernumber
1090 % \subsection{Attribute allocation}
1092 % \begin{macro}{new\_attribute}
1093 % \changes{v1.0a}{2015/09/24}{Function added}
1094 % \changes{v1.1c}{2017/02/18}{Parameterise count used in tracking}
1095 % As attributes are used for Lua manipulations its useful to be able
1096 % to assign from this end.
1098 local attributes=setmetatable(
1101 __index = function(t,key)
1102 return registernumber(key) or nil
1105 luatexbase.attributes = attributes
1109 local attribute_count_name = attribute_count_name or "e@alloc@attribute@count"
1110 local function new_attribute(name)
1111 tex_setcount("global", attribute_count_name,
1112 tex_count[attribute_count_name] + 1)
1113 if tex_count[attribute_count_name] > 65534 then
1114 luatexbase_error("No room for a new \\attribute")
1116 attributes[name]= tex_count[attribute_count_name]
1117 luatexbase_log("Lua-only attribute " .. name .. " = " ..
1118 tex_count[attribute_count_name])
1119 return tex_count[attribute_count_name]
1121 luatexbase.new_attribute = new_attribute
1125 % \subsection{Custom whatsit allocation}
1127 % \begin{macro}{new\_whatsit}
1128 % \changes{v1.1c}{2017/02/18}{Parameterise count used in tracking}
1129 % Much the same as for attribute allocation in Lua.
1131 local whatsit_count_name = whatsit_count_name or "e@alloc@whatsit@count"
1132 local function new_whatsit(name)
1133 tex_setcount("global", whatsit_count_name,
1134 tex_count[whatsit_count_name] + 1)
1135 if tex_count[whatsit_count_name] > 65534 then
1136 luatexbase_error("No room for a new custom whatsit")
1138 luatexbase_log("Custom whatsit " .. (name or "") .. " = " ..
1139 tex_count[whatsit_count_name])
1140 return tex_count[whatsit_count_name]
1142 luatexbase.new_whatsit = new_whatsit
1146 % \subsection{Bytecode register allocation}
1148 % \begin{macro}{new\_bytecode}
1149 % \changes{v1.1c}{2017/02/18}{Parameterise count used in tracking}
1150 % Much the same as for attribute allocation in Lua.
1151 % The optional \meta{name} argument is used in the log if given.
1153 local bytecode_count_name = bytecode_count_name or "e@alloc@bytecode@count"
1154 local function new_bytecode(name)
1155 tex_setcount("global", bytecode_count_name,
1156 tex_count[bytecode_count_name] + 1)
1157 if tex_count[bytecode_count_name] > 65534 then
1158 luatexbase_error("No room for a new bytecode register")
1160 luatexbase_log("Lua bytecode " .. (name or "") .. " = " ..
1161 tex_count[bytecode_count_name])
1162 return tex_count[bytecode_count_name]
1164 luatexbase.new_bytecode = new_bytecode
1168 % \subsection{Lua chunk name allocation}
1170 % \begin{macro}{new\_chunkname}
1171 % \changes{v1.1c}{2017/02/18}{Parameterise count used in tracking}
1172 % As for bytecode registers but also store the name in the
1175 local chunkname_count_name = chunkname_count_name or "e@alloc@luachunk@count"
1176 local function new_chunkname(name)
1177 tex_setcount("global", chunkname_count_name,
1178 tex_count[chunkname_count_name] + 1)
1179 local chunkname_count = tex_count[chunkname_count_name]
1180 chunkname_count = chunkname_count + 1
1181 if chunkname_count > 65534 then
1182 luatexbase_error("No room for a new chunkname")
1184 lua.name[chunkname_count]=name
1185 luatexbase_log("Lua chunkname " .. (name or "") .. " = " ..
1186 chunkname_count .. "\n")
1187 return chunkname_count
1189 luatexbase.new_chunkname = new_chunkname
1193 % \subsection{Lua callback management}
1195 % The native mechanism for callbacks in Lua\TeX\ allows only one per function.
1196 % That is extremely restrictive and so a mechanism is needed to add and
1197 % remove callbacks from the appropriate hooks.
1199 % \subsubsection{Housekeeping}
1201 % The main table: keys are callback names, and values are the associated lists
1202 % of functions. More precisely, the entries in the list are tables holding the
1203 % actual function as |func| and the identifying description as |description|.
1204 % Only callbacks with a non-empty list of functions have an entry in this
1207 local callbacklist = callbacklist or { }
1210 % Numerical codes for callback types, and name-to-value association (the
1211 % table keys are strings, the values are numbers).
1213 local list, data, exclusive, simple = 1, 2, 3, 4
1217 exclusive = exclusive,
1222 % Now, list all predefined callbacks with their current type, based on the
1223 % Lua\TeX{} manual version~1.01. A full list of the currently-available
1224 % callbacks can be obtained using
1227 % for i,_ in pairs(callback.list()) do
1228 % texio.write_nl("- " .. i)
1233 % in plain Lua\TeX{}. (Some undocumented callbacks are omitted as they are
1236 local callbacktypes = callbacktypes or {
1238 % Section 8.2: file discovery callbacks.
1240 find_read_file = exclusive,
1241 find_write_file = exclusive,
1242 find_font_file = data,
1243 find_output_file = data,
1244 find_format_file = data,
1245 find_vf_file = data,
1246 find_map_file = data,
1247 find_enc_file = data,
1248 find_sfd_file = data,
1249 find_pk_file = data,
1250 find_data_file = data,
1251 find_opentype_file = data,
1252 find_truetype_file = data,
1253 find_type1_file = data,
1254 find_image_file = data,
1258 open_read_file = exclusive,
1259 read_font_file = exclusive,
1260 read_vf_file = exclusive,
1261 read_map_file = exclusive,
1262 read_enc_file = exclusive,
1263 read_sfd_file = exclusive,
1264 read_pk_file = exclusive,
1265 read_data_file = exclusive,
1266 read_truetype_file = exclusive,
1267 read_type1_file = exclusive,
1268 read_opentype_file = exclusive,
1270 % \changes{v1.0m}{2016/02/11}{read\_cidmap\_file added}
1271 % Not currently used by luatex but included for completeness.
1272 % may be used by a font handler.
1274 find_cidmap_file = data,
1275 read_cidmap_file = exclusive,
1277 % Section 8.3: data processing callbacks.
1278 % \changes{v1.0m}{2016/02/11}{token\_filter removed}
1280 process_input_buffer = data,
1281 process_output_buffer = data,
1282 process_jobname = data,
1284 % Section 8.4: node list processing callbacks.
1285 % \changes{v1.0m}{2016/02/11}
1286 % {process\_rule, [hv]pack\_quality append\_to\_vlist\_filter added}
1287 % \changes{v1.0n}{2016/03/13}{insert\_local\_par added}
1288 % \changes{v1.0n}{2016/03/13}{contribute\_filter added}
1290 contribute_filter = simple,
1291 buildpage_filter = simple,
1292 build_page_insert = exclusive,
1293 pre_linebreak_filter = list,
1294 linebreak_filter = list,
1295 append_to_vlist_filter = list,
1296 post_linebreak_filter = list,
1297 hpack_filter = list,
1298 vpack_filter = list,
1299 hpack_quality = list,
1300 vpack_quality = list,
1301 pre_output_filter = list,
1302 process_rule = list,
1304 ligaturing = simple,
1306 insert_local_par = simple,
1307 mlist_to_hlist = list,
1309 % Section 8.5: information reporting callbacks.
1310 % \changes{v1.0m}{2016/02/11}{show\_warning\_message added}
1311 % \changes{v1.0p}{2016/11/17}{call\_edit added}
1316 start_page_number = simple,
1317 stop_page_number = simple,
1318 show_error_hook = simple,
1319 show_warning_message = simple,
1320 show_error_message = simple,
1321 show_lua_error_hook = simple,
1322 start_file = simple,
1326 % Section 8.6: PDF-related callbacks.
1328 finish_pdffile = data,
1329 finish_pdfpage = data,
1331 % Section 8.7: font-related callbacks.
1333 define_font = exclusive,
1335 % \changes{v1.0m}{2016/02/11}{pdf\_stream\_filter\_callback removed}
1338 luatexbase.callbacktypes=callbacktypes
1341 % \begin{macro}{callback.register}
1342 % \changes{v1.0a}{2015/09/24}{Function modified}
1343 % Save the original function for registering callbacks and prevent the
1344 % original being used. The original is saved in a place that remains
1345 % available so other more sophisticated code can override the approach
1346 % taken by the kernel if desired.
1348 local callback_register = callback_register or callback.register
1349 function callback.register()
1350 luatexbase_error("Attempt to use callback.register() directly\n")
1355 % \subsubsection{Handlers}
1357 % The handler function is registered into the callback when the
1358 % first function is added to this callback's list. Then, when the callback
1359 % is called, the handler takes care of running all functions in the list.
1360 % When the last function is removed from the callback's list, the handler
1363 % More precisely, the functions below are used to generate a specialized
1364 % function (closure) for a given callback, which is the actual handler.
1367 % The way the functions are combined together depends on
1368 % the type of the callback. There are currently 4 types of callback, depending
1369 % on the calling convention of the functions the callback can hold:
1370 % \begin{description}
1371 % \item[simple] is for functions that don't return anything: they are called
1372 % in order, all with the same argument;
1373 % \item[data] is for functions receiving a piece of data of any type
1374 % except node list head (and possibly other arguments) and returning it
1375 % (possibly modified): the functions are called in order, and each is
1376 % passed the return value of the previous (and the other arguments
1377 % untouched, if any). The return value is that of the last function;
1378 % \item[list] is a specialized variant of \emph{data} for functions
1379 % filtering node lists. Such functions may return either the head of a
1380 % modified node list, or the boolean values |true| or |false|. The
1381 % functions are chained the same way as for \emph{data} except that for
1383 % one function returns |false|, then |false| is immediately returned and
1384 % the following functions are \emph{not} called. If one function returns
1385 % |true|, then the same head is passed to the next function. If all
1386 % functions return |true|, then |true| is returned, otherwise the return
1387 % value of the last function not returning |true| is used.
1388 % \item[exclusive] is for functions with more complex signatures; functions in
1389 % this type of callback are \emph{not} combined: An error is raised if
1390 % a second callback is registered..
1393 % Handler for |data| callbacks.
1395 local function data_handler(name)
1396 return function(data, ...)
1397 for _,i in ipairs(callbacklist[name]) do
1398 data = i.func(data,...)
1404 % Handler for |exclusive| callbacks. We can assume |callbacklist[name]| is not
1405 % empty: otherwise, the function wouldn't be registered in the callback any
1408 local function exclusive_handler(name)
1409 return function(...)
1410 return callbacklist[name][1].func(...)
1414 % Handler for |list| callbacks.
1415 % \changes{v1.0k}{2015/12/02}{resolve name and i.description (PHG)}
1417 local function list_handler(name)
1418 return function(head, ...)
1420 local alltrue = true
1421 for _,i in ipairs(callbacklist[name]) do
1422 ret = i.func(head, ...)
1423 if ret == false then
1425 "Function `" .. i.description .. "' returned false\n"
1426 .. "in callback `" .. name .."'"
1435 return alltrue and true or head
1439 % Handler for |simple| callbacks.
1441 local function simple_handler(name)
1442 return function(...)
1443 for _,i in ipairs(callbacklist[name]) do
1450 % Keep a handlers table for indexed access.
1453 [data] = data_handler,
1454 [exclusive] = exclusive_handler,
1455 [list] = list_handler,
1456 [simple] = simple_handler,
1460 % \subsubsection{Public functions for callback management}
1462 % Defining user callbacks perhaps should be in package code,
1463 % but impacts on |add_to_callback|.
1464 % If a default function is not required, it may be declared as |false|.
1465 % First we need a list of user callbacks.
1467 local user_callbacks_defaults = { }
1470 % \begin{macro}{create\_callback}
1471 % \changes{v1.0a}{2015/09/24}{Function added}
1472 % \changes{v1.0i}{2015/11/29}{Check name is not nil in error message (PHG)}
1473 % \changes{v1.0k}{2015/12/02}{Give more specific error messages (PHG)}
1474 % The allocator itself.
1476 local function create_callback(name, ctype, default)
1477 if not name or name == ""
1478 or not ctype or ctype == ""
1480 luatexbase_error("Unable to create callback:\n" ..
1481 "valid callback name and type required")
1483 if callbacktypes[name] then
1484 luatexbase_error("Unable to create callback `" .. name ..
1485 "':\ncallback is already defined")
1487 if default ~= false and type (default) ~= "function" then
1488 luatexbase_error("Unable to create callback `" .. name ..
1489 ":\ndefault is not a function")
1491 user_callbacks_defaults[name] = default
1492 callbacktypes[name] = types[ctype]
1494 luatexbase.create_callback = create_callback
1498 % \begin{macro}{call\_callback}
1499 % \changes{v1.0a}{2015/09/24}{Function added}
1500 % \changes{v1.0i}{2015/11/29}{Check name is not nil in error message (PHG)}
1501 % \changes{v1.0k}{2015/12/02}{Give more specific error messages (PHG)}
1502 % Call a user defined callback. First check arguments.
1504 local function call_callback(name,...)
1505 if not name or name == "" then
1506 luatexbase_error("Unable to create callback:\n" ..
1507 "valid callback name required")
1509 if user_callbacks_defaults[name] == nil then
1510 luatexbase_error("Unable to call callback `" .. name
1511 .. "':\nunknown or empty")
1513 local l = callbacklist[name]
1516 f = user_callbacks_defaults[name]
1521 f = handlers[callbacktypes[name]](name)
1525 luatexbase.call_callback=call_callback
1529 % \begin{macro}{add\_to\_callback}
1530 % \changes{v1.0a}{2015/09/24}{Function added}
1531 % Add a function to a callback. First check arguments.
1532 % \changes{v1.0k}{2015/12/02}{Give more specific error messages (PHG)}
1534 local function add_to_callback(name, func, description)
1535 if not name or name == "" then
1536 luatexbase_error("Unable to register callback:\n" ..
1537 "valid callback name required")
1539 if not callbacktypes[name] or
1540 type(func) ~= "function" or
1542 description == "" then
1544 "Unable to register callback.\n\n"
1545 .. "Correct usage:\n"
1546 .. "add_to_callback(<callback>, <function>, <description>)"
1550 % Then test if this callback is already in use. If not, initialise its list
1551 % and register the proper handler.
1553 local l = callbacklist[name]
1556 callbacklist[name] = l
1558 % If it is not a user defined callback use the primitive callback register.
1560 if user_callbacks_defaults[name] == nil then
1561 callback_register(name, handlers[callbacktypes[name]](name))
1565 % Actually register the function and give an error if more than one
1566 % |exclusive| one is registered.
1570 description = description,
1572 local priority = #l + 1
1573 if callbacktypes[name] == exclusive then
1576 "Cannot add second callback to exclusive function\n`" ..
1580 table.insert(l, priority, f)
1582 % Keep user informed.
1585 "Inserting `" .. description .. "' at position "
1586 .. priority .. " in `" .. name .. "'."
1589 luatexbase.add_to_callback = add_to_callback
1593 % \begin{macro}{remove\_from\_callback}
1594 % \changes{v1.0a}{2015/09/24}{Function added}
1595 % \changes{v1.0k}{2015/12/02}{adjust initialisation of cb local (PHG)}
1596 % \changes{v1.0k}{2015/12/02}{Give more specific error messages (PHG)}
1597 % Remove a function from a callback. First check arguments.
1599 local function remove_from_callback(name, description)
1600 if not name or name == "" then
1601 luatexbase_error("Unable to remove function from callback:\n" ..
1602 "valid callback name required")
1604 if not callbacktypes[name] or
1606 description == "" then
1608 "Unable to remove function from callback.\n\n"
1609 .. "Correct usage:\n"
1610 .. "remove_from_callback(<callback>, <description>)"
1613 local l = callbacklist[name]
1616 "No callback list for `" .. name .. "'\n")
1619 % Loop over the callback's function list until we find a matching entry.
1620 % Remove it and check if the list is empty: if so, unregister the
1624 for i,j in ipairs(l) do
1625 if j.description == description then
1632 "No callback `" .. description .. "' registered for `" ..
1636 table.remove(l, index)
1638 "Removing `" .. description .. "' from `" .. name .. "'."
1641 callbacklist[name] = nil
1642 callback_register(name, nil)
1644 return cb.func,cb.description
1646 luatexbase.remove_from_callback = remove_from_callback
1650 % \begin{macro}{in\_callback}
1651 % \changes{v1.0a}{2015/09/24}{Function added}
1652 % \changes{v1.0h}{2015/11/27}{Guard against undefined list latex/4445}
1653 % Look for a function description in a callback.
1655 local function in_callback(name, description)
1658 or not callbacklist[name]
1659 or not callbacktypes[name]
1660 or not description then
1663 for _, i in pairs(callbacklist[name]) do
1664 if i.description == description then
1670 luatexbase.in_callback = in_callback
1674 % \begin{macro}{disable\_callback}
1675 % \changes{v1.0a}{2015/09/24}{Function added}
1676 % As we subvert the engine interface we need to provide a way to access
1677 % this functionality.
1679 local function disable_callback(name)
1680 if(callbacklist[name] == nil) then
1681 callback_register(name, false)
1683 luatexbase_error("Callback list for " .. name .. " not empty")
1686 luatexbase.disable_callback = disable_callback
1690 % \begin{macro}{callback\_descriptions}
1691 % \changes{v1.0a}{2015/09/24}{Function added}
1692 % \changes{v1.0h}{2015/11/27}{Match test in in-callback latex/4445}
1693 % List the descriptions of functions registered for the given callback.
1695 local function callback_descriptions (name)
1699 or not callbacklist[name]
1700 or not callbacktypes[name]
1704 for k, i in pairs(callbacklist[name]) do
1710 luatexbase.callback_descriptions =callback_descriptions
1714 % \begin{macro}{uninstall}
1715 % \changes{v1.0e}{2015/10/02}{Function added}
1716 % Unlike at the \TeX{} level, we have to provide a back-out mechanism here
1717 % at the same time as the rest of the code. This is not meant for use by
1718 % anything other than \textsf{latexrelease}: as such this is
1719 % \emph{deliberately} not documented for users!
1721 local function uninstall()
1724 "Uninstalling kernel luatexbase code"
1726 callback.register = callback_register
1729 luatexbase.uninstall = uninstall
1738 % Reset the catcode of |@|.
1740 %<tex>\catcode`\@=\etatcatcode\relax