| 1 |
% \iffalse meta-comment |
| 2 |
% |
| 3 |
% Copyright (C) 2015-2025 |
| 4 |
% The LaTeX Project and any individual authors listed elsewhere |
| 5 |
% in this file. |
| 6 |
% |
| 7 |
% This file is part of the LaTeX base system. |
| 8 |
% ------------------------------------------- |
| 9 |
% |
| 10 |
% It may be distributed and/or modified under the |
| 11 |
% conditions of the LaTeX Project Public License, either version 1.3c |
| 12 |
% of this license or (at your option) any later version. |
| 13 |
% The latest version of this license is in |
| 14 |
% https://www.latex-project.org/lppl.txt |
| 15 |
% and version 1.3c or later is part of all distributions of LaTeX |
| 16 |
% version 2008 or later. |
| 17 |
% |
| 18 |
% This file has the LPPL maintenance status "maintained". |
| 19 |
% |
| 20 |
%<2ekernel>%%% From File: ltluatex.dtx |
| 21 |
%<plain>\ifx\newluafunction\undefined\else\expandafter\endinput\fi |
| 22 |
%<tex>\ifx |
| 23 |
%<tex> \ProvidesFile\undefined\begingroup\def\ProvidesFile |
| 24 |
%<tex> #1#2[#3]{\endgroup\immediate\write-1{File: #1 #3}} |
| 25 |
%<tex>\fi |
| 26 |
%<*dtx> |
| 27 |
\ProvidesFile{ltluatex.dtx} |
| 28 |
%</dtx> |
| 29 |
%<plain>\ProvidesFile{ltluatex.tex} |
| 30 |
%<*plain> |
| 31 |
% \fi |
| 32 |
% \ProvidesFile{ltluatex.dtx} |
| 33 |
[2025/01/11 v1.2e |
| 34 |
% LaTeX Kernel (LuaTeX support)^^A |
| 35 |
%\iffalse |
| 36 |
%<plain> LuaTeX support for plain TeX (core)% |
| 37 |
%\fi |
| 38 |
] |
| 39 |
% \iffalse |
| 40 |
%</plain> |
| 41 |
%<*tex> |
| 42 |
\edef\etatcatcode{\the\catcode`\@} |
| 43 |
\catcode`\@=11 |
| 44 |
%</tex> |
| 45 |
%<*driver> |
| 46 |
\documentclass{ltxdoc} |
| 47 |
|
| 48 |
\providecommand\InternalDetectionOff{} |
| 49 |
\providecommand\InternalDetectionOn{} |
| 50 |
|
| 51 |
\GetFileInfo{ltluatex.dtx} |
| 52 |
\begin{document} |
| 53 |
\title{\filename\\(Lua\TeX{}-specific support)} |
| 54 |
\author{David Carlisle and Joseph Wright\footnote{Significant portions |
| 55 |
of the code here are adapted/simplified from the packages \textsf{luatex} and |
| 56 |
\textsf{luatexbase} written by Heiko Oberdiek, \'{E}lie Roux, |
| 57 |
Manuel P\'{e}gouri\'{e}-Gonnar and Philipp Gesang.}} |
| 58 |
\date{\filedate} |
| 59 |
\maketitle |
| 60 |
\setcounter{tocdepth}{2} |
| 61 |
\tableofcontents |
| 62 |
\DocInput{\filename} |
| 63 |
\end{document} |
| 64 |
%</driver> |
| 65 |
% \fi |
| 66 |
% |
| 67 |
% |
| 68 |
% \section{Overview} |
| 69 |
% |
| 70 |
% Lua\TeX{} adds a number of engine-specific functions to \TeX{}. Several of |
| 71 |
% these require set up that is best done in the kernel or need related support |
| 72 |
% functions. This file provides \emph{basic} support for Lua\TeX{} at the |
| 73 |
% \LaTeXe{} kernel level plus as a loadable file which can be used with |
| 74 |
% plain \TeX{} and \LaTeX{}. |
| 75 |
% |
| 76 |
% This file contains code for both \TeX{} (to be stored as part of the format) |
| 77 |
% and Lua (to be loaded at the start of each job). In the Lua code, the kernel |
| 78 |
% uses the namespace |luatexbase|. |
| 79 |
% |
| 80 |
% The following |\count| registers are used here for register allocation: |
| 81 |
% \begin{itemize} |
| 82 |
% \item[\texttt{\string\e@alloc@attribute@count}] Attributes (default~258) |
| 83 |
% \item[\texttt{\string\e@alloc@ccodetable@count}] Category code tables |
| 84 |
% (default~259) |
| 85 |
% \item[\texttt{\string\e@alloc@luafunction@count}] Lua functions |
| 86 |
% (default~260) |
| 87 |
% \item[\texttt{\string\e@alloc@whatsit@count}] User whatsits (default~261) |
| 88 |
% \item[\texttt{\string\e@alloc@bytecode@count}] Lua bytecodes (default~262) |
| 89 |
% \item[\texttt{\string\e@alloc@luachunk@count}] Lua chunks (default~263) |
| 90 |
% \end{itemize} |
| 91 |
% (|\count 256| is used for |\newmarks| allocation and |\count 257| |
| 92 |
% is used for\linebreak |
| 93 |
% |\newXeTeXintercharclass| with Xe\TeX{}, with code defined in |
| 94 |
% \texttt{ltfinal.dtx}). |
| 95 |
% With any \LaTeXe{} kernel from 2015 onward these registers are part of |
| 96 |
% the block in the extended area reserved by the kernel (prior to 2015 the |
| 97 |
% \LaTeXe{} kernel did not provide any functionality for the extended |
| 98 |
% allocation area). |
| 99 |
% |
| 100 |
% \section{Core \TeX{} functionality} |
| 101 |
% |
| 102 |
% The commands defined here are defined for |
| 103 |
% possible inclusion in a future \LaTeX{} format, however also extracted |
| 104 |
% to the file |ltluatex.tex| which may be used with older \LaTeX\ |
| 105 |
% formats, and with plain \TeX. |
| 106 |
% |
| 107 |
% \noindent |
| 108 |
% \DescribeMacro{\newattribute} |
| 109 |
% |\newattribute{|\meta{attribute}|}|\\ |
| 110 |
% Defines a named \cs{attribute}, indexed from~$1$ |
| 111 |
% (\emph{i.e.}~|\attribute0| is never defined). Attributes initially |
| 112 |
% have the marker value |-"7FFFFFFF| (`unset') set by the engine. |
| 113 |
% |
| 114 |
% \noindent |
| 115 |
% \DescribeMacro{\newcatcodetable} |
| 116 |
% |\newcatcodetable{|\meta{catcodetable}|}|\\ |
| 117 |
% Defines a named \cs{catcodetable}, indexed from~$1$ |
| 118 |
% (|\catcodetable0| is never assigned). A new catcode table will be |
| 119 |
% populated with exactly those values assigned by Ini\TeX{} (as described |
| 120 |
% in the Lua\TeX{} manual). |
| 121 |
% |
| 122 |
% \noindent |
| 123 |
% \DescribeMacro{\newluafunction} |
| 124 |
% |\newluafunction{|\meta{function}|}|\\ |
| 125 |
% Defines a named \cs{luafunction}, indexed from~$1$. (Lua indexes |
| 126 |
% tables from $1$ so |\luafunction0| is not available). |
| 127 |
% |
| 128 |
% \noindent |
| 129 |
% \DescribeMacro{\newluacmd} |
| 130 |
% |\newluadef{|\meta{function}|}|\\ |
| 131 |
% Like \cs{newluafunction}, but defines the command using \cs{luadef} |
| 132 |
% instead of just assigning an integer. |
| 133 |
% |
| 134 |
% \noindent |
| 135 |
% \DescribeMacro{\newprotectedluacmd} |
| 136 |
% |\newluadef{|\meta{function}|}|\\ |
| 137 |
% Like \cs{newluacmd}, but the defined command is not expandable. |
| 138 |
% |
| 139 |
% \noindent |
| 140 |
% \DescribeMacro{\newwhatsit} |
| 141 |
% |\newwhatsit{|\meta{whatsit}|}|\\ |
| 142 |
% Defines a custom \cs{whatsit}, indexed from~$1$. |
| 143 |
% |
| 144 |
% \noindent |
| 145 |
% \DescribeMacro{\newluabytecode} |
| 146 |
% |\newluabytecode{|\meta{bytecode}|}|\\ |
| 147 |
% Allocates a number for Lua bytecode register, indexed from~$1$. |
| 148 |
% |
| 149 |
% \noindent |
| 150 |
% \DescribeMacro{\newluachunkname} |
| 151 |
% |newluachunkname{|\meta{chunkname}|}|\\ |
| 152 |
% Allocates a number for Lua chunk register, indexed from~$1$. |
| 153 |
% Also enters the name of the register (without backslash) into the |
| 154 |
% \verb|lua.name| table to be used in stack traces. |
| 155 |
% |
| 156 |
% \noindent |
| 157 |
% \DescribeMacro{\catcodetable@initex} |
| 158 |
% \DescribeMacro{\catcodetable@string} |
| 159 |
% \DescribeMacro{\catcodetable@latex} |
| 160 |
% \DescribeMacro{\catcodetable@atletter} |
| 161 |
% Predefined category code tables with the obvious assignments. Note |
| 162 |
% that the |latex| and |atletter| tables set the full Unicode range |
| 163 |
% to the codes predefined by the kernel. |
| 164 |
% |
| 165 |
% \noindent |
| 166 |
% \DescribeMacro{\setattribute} |
| 167 |
% \DescribeMacro{\unsetattribute} |
| 168 |
% |\setattribute{|\meta{attribute}|}{|\meta{value}|}|\\ |
| 169 |
% |\unsetattribute{|\meta{attribute}|}|\\ |
| 170 |
% Set and unset attributes in a manner analogous to |\setlength|. Note that |
| 171 |
% attributes take a marker value when unset so this operation is distinct |
| 172 |
% from setting the value to zero. |
| 173 |
% |
| 174 |
% \section{Plain \TeX\ interface} |
| 175 |
% |
| 176 |
% The \textsf{ltluatex} interface may be used with plain \TeX\ using |
| 177 |
% |\input{ltluatex}|. This inputs |ltluatex.tex| which inputs |
| 178 |
% |etex.src| (or |etex.sty| if used with \LaTeX) |
| 179 |
% if it is not already input, and then defines some internal commands to |
| 180 |
% allow the \textsf{ltluatex} interface to be defined. |
| 181 |
% |
| 182 |
% The \textsf{luatexbase} package interface may also be used in plain \TeX, |
| 183 |
% as before, by inputting the package |\input luatexbase.sty|. The new |
| 184 |
% version of \textsf{luatexbase} is based on this \textsf{ltluatex} |
| 185 |
% code but implements a compatibility layer providing the interface |
| 186 |
% of the original package. |
| 187 |
% |
| 188 |
% \section{Lua functionality} |
| 189 |
% |
| 190 |
% \begingroup |
| 191 |
% |
| 192 |
% \begingroup\lccode`~=`_ |
| 193 |
% \lowercase{\endgroup\let~}_ |
| 194 |
% \catcode`_=12 |
| 195 |
% |
| 196 |
% \subsection{Allocators in Lua} |
| 197 |
% |
| 198 |
% \DescribeMacro{new_attribute} |
| 199 |
% |luatexbase.new_attribute(|\meta{attribute}|)|\\ |
| 200 |
% Returns an allocation number for the \meta{attribute}, indexed from~$1$. |
| 201 |
% The attribute will be initialised with the marker value |-"7FFFFFFF| |
| 202 |
% (`unset'). The attribute allocation sequence is shared with the \TeX{} |
| 203 |
% code but this function does \emph{not} define a token using |
| 204 |
% |\attributedef|. |
| 205 |
% The attribute name is recorded in the |attributes| table. A |
| 206 |
% metatable is provided so that the table syntax can be used |
| 207 |
% consistently for attributes declared in \TeX\ or Lua. |
| 208 |
% |
| 209 |
% \noindent |
| 210 |
% \DescribeMacro{new_whatsit} |
| 211 |
% |luatexbase.new_whatsit(|\meta{whatsit}|)|\\ |
| 212 |
% Returns an allocation number for the custom \meta{whatsit}, indexed from~$1$. |
| 213 |
% |
| 214 |
% \noindent |
| 215 |
% \DescribeMacro{new_bytecode} |
| 216 |
% |luatexbase.new_bytecode(|\meta{bytecode}|)|\\ |
| 217 |
% Returns an allocation number for a bytecode register, indexed from~$1$. |
| 218 |
% The optional \meta{name} argument is just used for logging. |
| 219 |
% |
| 220 |
% \noindent |
| 221 |
% \DescribeMacro{new_chunkname} |
| 222 |
% |luatexbase.new_chunkname(|\meta{chunkname}|)|\\ |
| 223 |
% Returns an allocation number for a Lua chunk name for use with |
| 224 |
% |\directlua| and |\latelua|, indexed from~$1$. |
| 225 |
% The number is returned and also \meta{name} argument is added to the |
| 226 |
% |lua.name| array at that index. |
| 227 |
% |
| 228 |
% \begin{sloppypar} |
| 229 |
% \noindent |
| 230 |
% \DescribeMacro{new_luafunction} |
| 231 |
% |luatexbase.new_luafunction(|\meta{functionname}|)|\\ |
| 232 |
% Returns an allocation number for a lua function for use |
| 233 |
% with |\luafunction|, |\lateluafunction|, and |\luadef|, |
| 234 |
% indexed from~$1$. The optional \meta{functionname} argument |
| 235 |
% is just used for logging. |
| 236 |
% \end{sloppypar} |
| 237 |
% |
| 238 |
% These functions all require access to a named \TeX{} count register |
| 239 |
% to manage their allocations. The standard names are those defined |
| 240 |
% above for access from \TeX{}, \emph{e.g.}~\string\e@alloc@attribute@count, |
| 241 |
% but these can be adjusted by defining the variable |
| 242 |
% \texttt{\meta{type}\_count\_name} before loading |ltluatex.lua|, for example |
| 243 |
% \begin{verbatim} |
| 244 |
% local attribute_count_name = "attributetracker" |
| 245 |
% require("ltluatex") |
| 246 |
% \end{verbatim} |
| 247 |
% would use a \TeX{} |\count| (|\countdef|'d token) called |attributetracker| |
| 248 |
% in place of \string\e@alloc@attribute@count. |
| 249 |
% |
| 250 |
% \subsection{Lua access to \TeX{} register numbers} |
| 251 |
% |
| 252 |
% \DescribeMacro{registernumber} |
| 253 |
% |luatexbase.registernumer(|\meta{name}|)|\\ |
| 254 |
% Sometimes (notably in the case of Lua attributes) it is necessary to |
| 255 |
% access a register \emph{by number} that has been allocated by \TeX{}. |
| 256 |
% This package provides a function to look up the relevant number |
| 257 |
% using Lua\TeX{}'s internal tables. After for example |
| 258 |
% |\newattribute\myattrib|, |\myattrib| would be defined by (say) |
| 259 |
% |\myattrib=\attribute15|. |luatexbase.registernumer("myattrib")| |
| 260 |
% would then return the register number, $15$ in this case. If the string passed |
| 261 |
% as argument does not correspond to a token defined by |\attributedef|, |
| 262 |
% |\countdef| or similar commands, the Lua value |false| is returned. |
| 263 |
% |
| 264 |
% As an example, consider the input: |
| 265 |
%\begin{verbatim} |
| 266 |
% \newcommand\test[1]{% |
| 267 |
% \typeout{#1: \expandafter\meaning\csname#1\endcsname^^J |
| 268 |
% \space\space\space\space |
| 269 |
% \directlua{tex.write(luatexbase.registernumber("#1") or "bad input")}% |
| 270 |
% }} |
| 271 |
% |
| 272 |
% \test{undefinedrubbish} |
| 273 |
% |
| 274 |
% \test{space} |
| 275 |
% |
| 276 |
% \test{hbox} |
| 277 |
% |
| 278 |
% \test{@MM} |
| 279 |
% |
| 280 |
% \test{@tempdima} |
| 281 |
% \test{@tempdimb} |
| 282 |
% |
| 283 |
% \test{strutbox} |
| 284 |
% |
| 285 |
% \test{sixt@@n} |
| 286 |
% |
| 287 |
% \attrbutedef\myattr=12 |
| 288 |
% \myattr=200 |
| 289 |
% \test{myattr} |
| 290 |
% |
| 291 |
%\end{verbatim} |
| 292 |
% |
| 293 |
% If the demonstration code is processed with Lua\LaTeX{} then the following |
| 294 |
% would be produced in the log and terminal output. |
| 295 |
%\begin{verbatim} |
| 296 |
% undefinedrubbish: \relax |
| 297 |
% bad input |
| 298 |
% space: macro:-> |
| 299 |
% bad input |
| 300 |
% hbox: \hbox |
| 301 |
% bad input |
| 302 |
% @MM: \mathchar"4E20 |
| 303 |
% 20000 |
| 304 |
% @tempdima: \dimen14 |
| 305 |
% 14 |
| 306 |
% @tempdimb: \dimen15 |
| 307 |
% 15 |
| 308 |
% strutbox: \char"B |
| 309 |
% 11 |
| 310 |
% sixt@@n: \char"10 |
| 311 |
% 16 |
| 312 |
% myattr: \attribute12 |
| 313 |
% 12 |
| 314 |
%\end{verbatim} |
| 315 |
% |
| 316 |
% Notice how undefined commands, or commands unrelated to registers |
| 317 |
% do not produce an error, just return |false| and so print |
| 318 |
% |bad input| here. Note also that commands defined by |\newbox| work and |
| 319 |
% return the number of the box register even though the actual command |
| 320 |
% holding this number is a |\chardef| defined token (there is no |
| 321 |
% |\boxdef|). |
| 322 |
% |
| 323 |
% \subsection{Module utilities} |
| 324 |
% |
| 325 |
% \DescribeMacro{provides_module} |
| 326 |
% |luatexbase.provides_module(|\meta{info}|)|\\ |
| 327 |
% This function is used by modules to identify themselves; the |info| should be |
| 328 |
% a table containing information about the module. The required field |
| 329 |
% |name| must contain the name of the module. It is recommended to provide a |
| 330 |
% field |date| in the usual \LaTeX{} format |yyyy/mm/dd|. Optional fields |
| 331 |
% |version| (a string) and |description| may be used if present. This |
| 332 |
% information will be recorded in the log. Other fields are ignored. |
| 333 |
% If the |version| begins with a digit, a \texttt{v} will be added at the |
| 334 |
% start in the log. |
| 335 |
% |
| 336 |
% \noindent |
| 337 |
% \DescribeMacro{module_info} |
| 338 |
% \DescribeMacro{module_warning} |
| 339 |
% \DescribeMacro{module_error} |
| 340 |
% |luatexbase.module_info(|\meta{module}, \meta{text}|)|\\ |
| 341 |
% |luatexbase.module_warning(|\meta{module}, \meta{text}|)|\\ |
| 342 |
% |luatexbase.module_error(|\meta{module}, \meta{text}|)|\\ |
| 343 |
% These functions are similar to \LaTeX{}'s |\PackageError|, |\PackageWarning| |
| 344 |
% and |\PackageInfo| in the way they format the output. No automatic line |
| 345 |
% breaking is done, you may still use |\n| as usual for that, and the name of |
| 346 |
% the package will be prepended to each output line. |
| 347 |
% |
| 348 |
% Note that |luatexbase.module_error| raises an actual Lua error with |error()|, |
| 349 |
% which currently means a call stack will be dumped. While this may not |
| 350 |
% look pretty, at least it provides useful information for tracking the |
| 351 |
% error down. |
| 352 |
% |
| 353 |
% \subsection{Callback management} |
| 354 |
% |
| 355 |
% \noindent |
| 356 |
% \DescribeMacro{add_to_callback} |
| 357 |
% |luatexbase.add_to_callback(|^^A |
| 358 |
% \meta{callback}, \meta{function}, \meta{description}|)| |
| 359 |
% Registers the \meta{function} into the \meta{callback} with a textual |
| 360 |
% \meta{description} of the function. Functions are inserted into the callback |
| 361 |
% in the order loaded. |
| 362 |
% |
| 363 |
% \noindent |
| 364 |
% \DescribeMacro{remove_from_callback} |
| 365 |
% |luatexbase.remove_from_callback(|\meta{callback}, \meta{description}|)| |
| 366 |
% Removes the callback function with \meta{description} from the \meta{callback}. |
| 367 |
% The removed function and its description |
| 368 |
% are returned as the results of this function. |
| 369 |
% |
| 370 |
% \noindent |
| 371 |
% \DescribeMacro{in_callback} |
| 372 |
% |luatexbase.in_callback(|\meta{callback}, \meta{description}|)| |
| 373 |
% Checks if the \meta{description} matches one of the functions added |
| 374 |
% to the list for the \meta{callback}, returning a boolean value. |
| 375 |
% |
| 376 |
% \noindent |
| 377 |
% \DescribeMacro{disable_callback} |
| 378 |
% |luatexbase.disable_callback(|\meta{callback}|)| |
| 379 |
% Sets the \meta{callback} to \texttt{false} as described in the Lua\TeX{} |
| 380 |
% manual for the underlying \texttt{callback.register} built-in. Callbacks |
| 381 |
% will only be set to false (and thus be skipped entirely) if there are |
| 382 |
% no functions registered using the callback. |
| 383 |
% |
| 384 |
% \noindent |
| 385 |
% \DescribeMacro{callback_descriptions} |
| 386 |
% A list of the descriptions of functions registered to the specified |
| 387 |
% callback is returned. |{}| is returned if there are no functions registered. |
| 388 |
% |
| 389 |
% \noindent |
| 390 |
% \DescribeMacro{create_callback} |
| 391 |
% |luatexbase.create_callback(|\meta{name},\meta{type},\meta{default}|)| |
| 392 |
% Defines a user defined callback. The last argument is a default |
| 393 |
% function or |false|. |
| 394 |
% |
| 395 |
% \noindent |
| 396 |
% \DescribeMacro{call_callback} |
| 397 |
% |luatexbase.call_callback(|\meta{name},\ldots|)| |
| 398 |
% Calls a user defined callback with the supplied arguments. |
| 399 |
% |
| 400 |
% \noindent |
| 401 |
% \DescribeMacro{declare_callback_rule} |
| 402 |
% |luatexbase.declare_callback_rule(|\meta{name}, \meta{first}, \meta{relation}, \meta{second}|)| |
| 403 |
% Adds an ordering constraint between two callback functions for callback \meta{name}. |
| 404 |
% |
| 405 |
% The kind of constraint added depends on \meta{relation}: |
| 406 |
% \begin{description} |
| 407 |
% \item[before] The callback function with description \meta{first} will be |
| 408 |
% executed before the function with description \meta{second}. |
| 409 |
% \item[after] The callback function with description \meta{first} will be |
| 410 |
% executed after the function with description \meta{second}. |
| 411 |
% \item[incompatible-warning] When both a callback function with description \meta{first} |
| 412 |
% and with description \meta{second} is registered, then a warning is printed when |
| 413 |
% the callback is executed. |
| 414 |
% \item[incompatible-error] When both a callback function with description \meta{first} |
| 415 |
% and with description \meta{second} is registered, then an error is printed when |
| 416 |
% the callback is executed. |
| 417 |
% \item[unrelated] Any previously declared callback rule between \meta{first} |
| 418 |
% and \meta{second} gets disabled. |
| 419 |
% \end{description} |
| 420 |
% Every call to \texttt{declare_callback_rule} with a specific callback \meta{name} |
| 421 |
% and descriptions \meta{first} and \meta{second} overwrites all previous calls with |
| 422 |
% same callback and descriptions. |
| 423 |
% |
| 424 |
% The callback functions do not have to be registered yet when the functions is called. |
| 425 |
% Only the constraints for which both callback descriptions refer to callbacks |
| 426 |
% registered at the time the callback is called will have an effect. |
| 427 |
% |
| 428 |
% \endgroup |
| 429 |
% |
| 430 |
% \MaybeStop{} |
| 431 |
% |
| 432 |
% \section{Implementation} |
| 433 |
% |
| 434 |
% \begin{macrocode} |
| 435 |
%<*2ekernel|tex|latexrelease> |
| 436 |
%<2ekernel|latexrelease>\ifx\directlua\@undefined\else |
| 437 |
% \end{macrocode} |
| 438 |
% |
| 439 |
% |
| 440 |
% \changes{v1.0j}{2015/12/02}{Remove nonlocal iteration variables (PHG)} |
| 441 |
% \changes{v1.0j}{2015/12/02}{Assorted typos fixed (PHG)} |
| 442 |
% \changes{v1.0j}{2015/12/02}{Remove unreachable code after calls to error() (PHG)} |
| 443 |
% \subsection{Minimum Lua\TeX{} version} |
| 444 |
% |
| 445 |
% Lua\TeX{} has changed a lot over time. In the kernel support for ancient |
| 446 |
% versions is not provided: trying to build a format with a very old binary |
| 447 |
% therefore gives some information in the log and loading stops. The cut-off |
| 448 |
% selected here relates to the tree-searching behaviour of |require()|: |
| 449 |
% from version~0.60, Lua\TeX{} will correctly find Lua files in the |texmf| |
| 450 |
% tree without `help'. |
| 451 |
% \begin{macrocode} |
| 452 |
%<latexrelease>\IncludeInRelease{2015/10/01} |
| 453 |
%<latexrelease> {\newluafunction}{LuaTeX}% |
| 454 |
\ifnum\luatexversion<60 % |
| 455 |
\wlog{***************************************************} |
| 456 |
\wlog{* LuaTeX version too old for ltluatex support *} |
| 457 |
\wlog{***************************************************} |
| 458 |
\expandafter\endinput |
| 459 |
\fi |
| 460 |
% \end{macrocode} |
| 461 |
% |
| 462 |
% \changes{v1.1n}{2020/06/10}{Define \cs{@gobble}/\cs{@firstofone} even for \LaTeX\ to allow early loading.} |
| 463 |
% Two simple \LaTeX\ macros from |ltdefns.dtx| have to be defined here |
| 464 |
% because ltdefns.dtx is not loaded yet when ltluatex.dtx is executed. |
| 465 |
% \begin{macrocode} |
| 466 |
\long\def\@gobble#1{} |
| 467 |
\long\def\@firstofone#1{#1} |
| 468 |
% \end{macrocode} |
| 469 |
% |
| 470 |
% \subsection{Older \LaTeX{}/Plain \TeX\ setup} |
| 471 |
% |
| 472 |
% \begin{macrocode} |
| 473 |
%<*tex> |
| 474 |
% \end{macrocode} |
| 475 |
% |
| 476 |
% Older \LaTeX{} formats don't have the primitives with `native' names: |
| 477 |
% sort that out. If they already exist this will still be safe. |
| 478 |
% \begin{macrocode} |
| 479 |
\directlua{tex.enableprimitives("",tex.extraprimitives("luatex"))} |
| 480 |
% \end{macrocode} |
| 481 |
% |
| 482 |
% \begin{macrocode} |
| 483 |
\ifx\e@alloc\@undefined |
| 484 |
% \end{macrocode} |
| 485 |
% |
| 486 |
% In pre-2014 \LaTeX{}, or plain \TeX{}, load |etex.{sty,src}|. |
| 487 |
% \begin{macrocode} |
| 488 |
\ifx\documentclass\@undefined |
| 489 |
\ifx\loccount\@undefined |
| 490 |
\input{etex.src}% |
| 491 |
\fi |
| 492 |
\catcode`\@=11 % |
| 493 |
\outer\expandafter\def\csname newfam\endcsname |
| 494 |
{\alloc@8\fam\chardef\et@xmaxfam} |
| 495 |
\else |
| 496 |
\RequirePackage{etex} |
| 497 |
\expandafter\def\csname newfam\endcsname |
| 498 |
{\alloc@8\fam\chardef\et@xmaxfam} |
| 499 |
\expandafter\let\expandafter\new@mathgroup\csname newfam\endcsname |
| 500 |
\fi |
| 501 |
% \end{macrocode} |
| 502 |
% |
| 503 |
% \subsubsection{Fixes to \texttt{etex.src}/\texttt{etex.sty}} |
| 504 |
% |
| 505 |
% These could and probably should be made directly in an |
| 506 |
% update to |etex.src| which already has some Lua\TeX-specific |
| 507 |
% code, but does not define the correct range for Lua\TeX. |
| 508 |
% |
| 509 |
% 2015-07-13 higher range in luatex. |
| 510 |
% \begin{macrocode} |
| 511 |
\edef \et@xmaxregs {\ifx\directlua\@undefined 32768\else 65536\fi} |
| 512 |
% \end{macrocode} |
| 513 |
% luatex/xetex also allow more math fam. |
| 514 |
% \begin{macrocode} |
| 515 |
\edef \et@xmaxfam {\ifx\Umathcode\@undefined\sixt@@n\else\@cclvi\fi} |
| 516 |
% \end{macrocode} |
| 517 |
% |
| 518 |
% \begin{macrocode} |
| 519 |
\count 270=\et@xmaxregs % locally allocates \count registers |
| 520 |
\count 271=\et@xmaxregs % ditto for \dimen registers |
| 521 |
\count 272=\et@xmaxregs % ditto for \skip registers |
| 522 |
\count 273=\et@xmaxregs % ditto for \muskip registers |
| 523 |
\count 274=\et@xmaxregs % ditto for \box registers |
| 524 |
\count 275=\et@xmaxregs % ditto for \toks registers |
| 525 |
\count 276=\et@xmaxregs % ditto for \marks classes |
| 526 |
% \end{macrocode} |
| 527 |
% |
| 528 |
% and 256 or 16 fam. (Done above due to plain/\LaTeX\ differences in |
| 529 |
% \textsf{ltluatex}.) |
| 530 |
% \begin{macrocode} |
| 531 |
% \outer\def\newfam{\alloc@8\fam\chardef\et@xmaxfam} |
| 532 |
% \end{macrocode} |
| 533 |
% |
| 534 |
% End of proposed changes to \texttt{etex.src} |
| 535 |
% |
| 536 |
% \subsubsection{luatex specific settings} |
| 537 |
% |
| 538 |
% Switch to global cf |luatex.sty| to leave room for inserts |
| 539 |
% not really needed for luatex but possibly most compatible |
| 540 |
% with existing use. |
| 541 |
% \begin{macrocode} |
| 542 |
\expandafter\let\csname newcount\expandafter\expandafter\endcsname |
| 543 |
\csname globcount\endcsname |
| 544 |
\expandafter\let\csname newdimen\expandafter\expandafter\endcsname |
| 545 |
\csname globdimen\endcsname |
| 546 |
\expandafter\let\csname newskip\expandafter\expandafter\endcsname |
| 547 |
\csname globskip\endcsname |
| 548 |
\expandafter\let\csname newbox\expandafter\expandafter\endcsname |
| 549 |
\csname globbox\endcsname |
| 550 |
% \end{macrocode} |
| 551 |
% |
| 552 |
% Define|\e@alloc| as in \LaTeX{} (the existing macros in |etex.src| are |
| 553 |
% hard to extend to further register types as they assume specific |
| 554 |
% 26x and 27x count range). For compatibility the existing register |
| 555 |
% allocation is not changed. |
| 556 |
% |
| 557 |
% \begin{macrocode} |
| 558 |
\chardef\e@alloc@top=65535 |
| 559 |
\let\e@alloc@chardef\chardef |
| 560 |
% \end{macrocode} |
| 561 |
% |
| 562 |
% \begin{macrocode} |
| 563 |
\def\e@alloc#1#2#3#4#5#6{% |
| 564 |
\global\advance#3\@ne |
| 565 |
\e@ch@ck{#3}{#4}{#5}#1% |
| 566 |
\allocationnumber#3\relax |
| 567 |
\global#2#6\allocationnumber |
| 568 |
\wlog{\string#6=\string#1\the\allocationnumber}}% |
| 569 |
% \end{macrocode} |
| 570 |
% |
| 571 |
% \begin{macrocode} |
| 572 |
\gdef\e@ch@ck#1#2#3#4{% |
| 573 |
\ifnum#1<#2\else |
| 574 |
\ifnum#1=#2\relax |
| 575 |
#1\@cclvi |
| 576 |
\ifx\count#4\advance#1 10 \fi |
| 577 |
\fi |
| 578 |
\ifnum#1<#3\relax |
| 579 |
\else |
| 580 |
\errmessage{No room for a new \string#4}% |
| 581 |
\fi |
| 582 |
\fi}% |
| 583 |
% \end{macrocode} |
| 584 |
% |
| 585 |
% Fix up allocations not to clash with |etex.src|. |
| 586 |
% |
| 587 |
% \begin{macrocode} |
| 588 |
\expandafter\csname newcount\endcsname\e@alloc@attribute@count |
| 589 |
\expandafter\csname newcount\endcsname\e@alloc@ccodetable@count |
| 590 |
\expandafter\csname newcount\endcsname\e@alloc@luafunction@count |
| 591 |
\expandafter\csname newcount\endcsname\e@alloc@whatsit@count |
| 592 |
\expandafter\csname newcount\endcsname\e@alloc@bytecode@count |
| 593 |
\expandafter\csname newcount\endcsname\e@alloc@luachunk@count |
| 594 |
% \end{macrocode} |
| 595 |
% |
| 596 |
% End of conditional setup for plain \TeX\ / old \LaTeX. |
| 597 |
% \begin{macrocode} |
| 598 |
\fi |
| 599 |
%</tex> |
| 600 |
% \end{macrocode} |
| 601 |
% |
| 602 |
% \subsection{Attributes} |
| 603 |
% |
| 604 |
% \begin{macro}{\newattribute} |
| 605 |
% \changes{v1.0a}{2015/09/24}{Macro added} |
| 606 |
% \changes{v1.1q}{2020/08/02}{Move reset to $0$ inside conditional} |
| 607 |
% As is generally the case for the Lua\TeX{} registers we start here |
| 608 |
% from~$1$. Notably, some code assumes that |\attribute0| is never used so |
| 609 |
% this is important in this case. |
| 610 |
% \begin{macrocode} |
| 611 |
\ifx\e@alloc@attribute@count\@undefined |
| 612 |
\countdef\e@alloc@attribute@count=258 |
| 613 |
\e@alloc@attribute@count=\z@ |
| 614 |
\fi |
| 615 |
\def\newattribute#1{% |
| 616 |
\e@alloc\attribute\attributedef |
| 617 |
\e@alloc@attribute@count\m@ne\e@alloc@top#1% |
| 618 |
} |
| 619 |
% \end{macrocode} |
| 620 |
% \end{macro} |
| 621 |
% |
| 622 |
% \begin{macro}{\setattribute} |
| 623 |
% \begin{macro}{\unsetattribute} |
| 624 |
% Handy utilities. |
| 625 |
% \begin{macrocode} |
| 626 |
\def\setattribute#1#2{#1=\numexpr#2\relax} |
| 627 |
\def\unsetattribute#1{#1=-"7FFFFFFF\relax} |
| 628 |
% \end{macrocode} |
| 629 |
% \end{macro} |
| 630 |
% \end{macro} |
| 631 |
% |
| 632 |
% \subsection{Category code tables} |
| 633 |
% |
| 634 |
% \begin{macro}{\newcatcodetable} |
| 635 |
% \changes{v1.0a}{2015/09/24}{Macro added} |
| 636 |
% Category code tables are allocated with a limit half of that used by Lua\TeX{} |
| 637 |
% for everything else. At the end of allocation there needs to be an |
| 638 |
% initialization step. Table~$0$ is already taken (it's the global one for |
| 639 |
% current use) so the allocation starts at~$1$. |
| 640 |
% \begin{macrocode} |
| 641 |
\ifx\e@alloc@ccodetable@count\@undefined |
| 642 |
\countdef\e@alloc@ccodetable@count=259 |
| 643 |
\e@alloc@ccodetable@count=\z@ |
| 644 |
\fi |
| 645 |
\def\newcatcodetable#1{% |
| 646 |
\e@alloc\catcodetable\chardef |
| 647 |
\e@alloc@ccodetable@count\m@ne{"8000}#1% |
| 648 |
\initcatcodetable\allocationnumber |
| 649 |
} |
| 650 |
% \end{macrocode} |
| 651 |
% \end{macro} |
| 652 |
% |
| 653 |
% \changes{v1.0l}{2015/12/18}{Load Unicode data from source} |
| 654 |
% \begin{macro}{\catcodetable@initex} |
| 655 |
% \changes{v1.0a}{2015/09/24}{Macro added} |
| 656 |
% \begin{macro}{\catcodetable@string} |
| 657 |
% \changes{v1.0a}{2015/09/24}{Macro added} |
| 658 |
% \begin{macro}{\catcodetable@latex} |
| 659 |
% \changes{v1.0a}{2015/09/24}{Macro added} |
| 660 |
% \begin{macro}{\catcodetable@atletter} |
| 661 |
% \changes{v1.0a}{2015/09/24}{Macro added} |
| 662 |
% Save a small set of standard tables. The Unicode data is read |
| 663 |
% here in using a parser simplified from that in |load-unicode-data|: |
| 664 |
% only the nature of letters needs to be detected. |
| 665 |
% \begin{macrocode} |
| 666 |
\newcatcodetable\catcodetable@initex |
| 667 |
\newcatcodetable\catcodetable@string |
| 668 |
\begingroup |
| 669 |
\def\setrangecatcode#1#2#3{% |
| 670 |
\ifnum#1>#2 % |
| 671 |
\expandafter\@gobble |
| 672 |
\else |
| 673 |
\expandafter\@firstofone |
| 674 |
\fi |
| 675 |
{% |
| 676 |
\catcode#1=#3 % |
| 677 |
\expandafter\setrangecatcode\expandafter |
| 678 |
{\number\numexpr#1 + 1\relax}{#2}{#3} |
| 679 |
}% |
| 680 |
} |
| 681 |
\@firstofone{% |
| 682 |
\catcodetable\catcodetable@initex |
| 683 |
\catcode0=12 % |
| 684 |
\catcode13=12 % |
| 685 |
\catcode37=12 % |
| 686 |
\setrangecatcode{65}{90}{12}% |
| 687 |
\setrangecatcode{97}{122}{12}% |
| 688 |
\catcode92=12 % |
| 689 |
\catcode127=12 % |
| 690 |
\savecatcodetable\catcodetable@string |
| 691 |
\endgroup |
| 692 |
}% |
| 693 |
\newcatcodetable\catcodetable@latex |
| 694 |
\newcatcodetable\catcodetable@atletter |
| 695 |
\begingroup |
| 696 |
\def\parseunicodedataI#1;#2;#3;#4\relax{% |
| 697 |
\parseunicodedataII#1;#3;#2 First>\relax |
| 698 |
}% |
| 699 |
\def\parseunicodedataII#1;#2;#3 First>#4\relax{% |
| 700 |
\ifx\relax#4\relax |
| 701 |
\expandafter\parseunicodedataIII |
| 702 |
\else |
| 703 |
\expandafter\parseunicodedataIV |
| 704 |
\fi |
| 705 |
{#1}#2\relax% |
| 706 |
}% |
| 707 |
\def\parseunicodedataIII#1#2#3\relax{% |
| 708 |
\ifnum 0% |
| 709 |
\if L#21\fi |
| 710 |
\if M#21\fi |
| 711 |
>0 % |
| 712 |
\catcode"#1=11 % |
| 713 |
\fi |
| 714 |
}% |
| 715 |
\def\parseunicodedataIV#1#2#3\relax{% |
| 716 |
\read\unicoderead to \unicodedataline |
| 717 |
\if L#2% |
| 718 |
\count0="#1 % |
| 719 |
\expandafter\parseunicodedataV\unicodedataline\relax |
| 720 |
\fi |
| 721 |
}% |
| 722 |
\def\parseunicodedataV#1;#2\relax{% |
| 723 |
\loop |
| 724 |
\unless\ifnum\count0>"#1 % |
| 725 |
\catcode\count0=11 % |
| 726 |
\advance\count0 by 1 % |
| 727 |
\repeat |
| 728 |
}% |
| 729 |
\def\storedpar{\par}% |
| 730 |
\chardef\unicoderead=\numexpr\count16 + 1\relax |
| 731 |
\openin\unicoderead=UnicodeData.txt % |
| 732 |
\loop\unless\ifeof\unicoderead % |
| 733 |
\read\unicoderead to \unicodedataline |
| 734 |
\unless\ifx\unicodedataline\storedpar |
| 735 |
\expandafter\parseunicodedataI\unicodedataline\relax |
| 736 |
\fi |
| 737 |
\repeat |
| 738 |
\closein\unicoderead |
| 739 |
\@firstofone{% |
| 740 |
\catcode64=12 % |
| 741 |
\savecatcodetable\catcodetable@latex |
| 742 |
\catcode64=11 % |
| 743 |
\savecatcodetable\catcodetable@atletter |
| 744 |
} |
| 745 |
\endgroup |
| 746 |
% \end{macrocode} |
| 747 |
% \end{macro} |
| 748 |
% \end{macro} |
| 749 |
% \end{macro} |
| 750 |
% \end{macro} |
| 751 |
% |
| 752 |
% \subsection{Named Lua functions} |
| 753 |
% |
| 754 |
% \begin{macro}{\newluafunction} |
| 755 |
% \changes{v1.0a}{2015/09/24}{Macro added} |
| 756 |
% \changes{v1.1q}{2020/08/02}{Move reset to $0$ inside conditional} |
| 757 |
% Much the same story for allocating Lua\TeX{} functions except here they are |
| 758 |
% just numbers so they are allocated in the same way as boxes. |
| 759 |
% Lua indexes from~$1$ so once again slot~$0$ is skipped. |
| 760 |
% \begin{macrocode} |
| 761 |
\ifx\e@alloc@luafunction@count\@undefined |
| 762 |
\countdef\e@alloc@luafunction@count=260 |
| 763 |
\e@alloc@luafunction@count=\z@ |
| 764 |
\fi |
| 765 |
\def\newluafunction{% |
| 766 |
\e@alloc\luafunction\e@alloc@chardef |
| 767 |
\e@alloc@luafunction@count\m@ne\e@alloc@top |
| 768 |
} |
| 769 |
% \end{macrocode} |
| 770 |
% \end{macro} |
| 771 |
% |
| 772 |
% \begin{macro}{\newluacmd, \newprotectedluacmd} |
| 773 |
% \changes{v1.1x}{2021/12/27}{Macros added} |
| 774 |
% Additionally two variants are provided to make the passed control |
| 775 |
% sequence call the function directly. |
| 776 |
% \begin{macrocode} |
| 777 |
\def\newluacmd{% |
| 778 |
\e@alloc\luafunction\luadef |
| 779 |
\e@alloc@luafunction@count\m@ne\e@alloc@top |
| 780 |
} |
| 781 |
\def\newprotectedluacmd{% |
| 782 |
\e@alloc\luafunction{\protected\luadef} |
| 783 |
\e@alloc@luafunction@count\m@ne\e@alloc@top |
| 784 |
} |
| 785 |
% \end{macrocode} |
| 786 |
% \end{macro} |
| 787 |
% |
| 788 |
% \subsection{Custom whatsits} |
| 789 |
% |
| 790 |
% \begin{macro}{\newwhatsit} |
| 791 |
% \changes{v1.0a}{2015/09/24}{Macro added} |
| 792 |
% \changes{v1.1q}{2020/08/02}{Move reset to $0$ inside conditional} |
| 793 |
% These are only settable from Lua but for consistency are definable |
| 794 |
% here. |
| 795 |
% \begin{macrocode} |
| 796 |
\ifx\e@alloc@whatsit@count\@undefined |
| 797 |
\countdef\e@alloc@whatsit@count=261 |
| 798 |
\e@alloc@whatsit@count=\z@ |
| 799 |
\fi |
| 800 |
\def\newwhatsit#1{% |
| 801 |
\e@alloc\whatsit\e@alloc@chardef |
| 802 |
\e@alloc@whatsit@count\m@ne\e@alloc@top#1% |
| 803 |
} |
| 804 |
% \end{macrocode} |
| 805 |
% \end{macro} |
| 806 |
% |
| 807 |
% \subsection{Lua bytecode registers} |
| 808 |
% |
| 809 |
% \begin{macro}{\newluabytecode} |
| 810 |
% \changes{v1.0a}{2015/09/24}{Macro added} |
| 811 |
% \changes{v1.1q}{2020/08/02}{Move reset to $0$ inside conditional} |
| 812 |
% These are only settable from Lua but for consistency are definable |
| 813 |
% here. |
| 814 |
% \begin{macrocode} |
| 815 |
\ifx\e@alloc@bytecode@count\@undefined |
| 816 |
\countdef\e@alloc@bytecode@count=262 |
| 817 |
\e@alloc@bytecode@count=\z@ |
| 818 |
\fi |
| 819 |
\def\newluabytecode#1{% |
| 820 |
\e@alloc\luabytecode\e@alloc@chardef |
| 821 |
\e@alloc@bytecode@count\m@ne\e@alloc@top#1% |
| 822 |
} |
| 823 |
% \end{macrocode} |
| 824 |
% \end{macro} |
| 825 |
% |
| 826 |
% \subsection{Lua chunk registers} |
| 827 |
|
| 828 |
% \begin{macro}{\newluachunkname} |
| 829 |
% \changes{v1.0a}{2015/09/24}{Macro added} |
| 830 |
% \changes{v1.1q}{2020/08/02}{Move reset to $0$ inside conditional} |
| 831 |
% As for bytecode registers, but in addition we need to add a string |
| 832 |
% to the \verb|lua.name| table to use in stack tracing. We use the |
| 833 |
% name of the command passed to the allocator, with no backslash. |
| 834 |
% \begin{macrocode} |
| 835 |
\ifx\e@alloc@luachunk@count\@undefined |
| 836 |
\countdef\e@alloc@luachunk@count=263 |
| 837 |
\e@alloc@luachunk@count=\z@ |
| 838 |
\fi |
| 839 |
\def\newluachunkname#1{% |
| 840 |
\e@alloc\luachunk\e@alloc@chardef |
| 841 |
\e@alloc@luachunk@count\m@ne\e@alloc@top#1% |
| 842 |
{\escapechar\m@ne |
| 843 |
\directlua{lua.name[\the\allocationnumber]="\string#1"}}% |
| 844 |
} |
| 845 |
% \end{macrocode} |
| 846 |
% \end{macro} |
| 847 |
% |
| 848 |
% \subsection{Lua loader} |
| 849 |
% \changes{v1.1r}{2020/08/10}{Load ltluatex Lua module during format building} |
| 850 |
% |
| 851 |
% Lua code loaded in the format often has to be loaded again at the |
| 852 |
% beginning of every job, so we define a helper which allows us to avoid |
| 853 |
% duplicated code: |
| 854 |
% |
| 855 |
% \begin{macrocode} |
| 856 |
\def\now@and@everyjob#1{% |
| 857 |
\everyjob\expandafter{\the\everyjob |
| 858 |
#1% |
| 859 |
}% |
| 860 |
#1% |
| 861 |
} |
| 862 |
% \end{macrocode} |
| 863 |
% |
| 864 |
% Load the Lua code at the start of every job. |
| 865 |
% For the conversion of \TeX{} into numbers at the Lua side we need some |
| 866 |
% known registers: for convenience we use a set of systematic names, which |
| 867 |
% means using a group around the Lua loader. |
| 868 |
% \begin{macrocode} |
| 869 |
%<2ekernel>\now@and@everyjob{% |
| 870 |
\begingroup |
| 871 |
\attributedef\attributezero=0 % |
| 872 |
\chardef \charzero =0 % |
| 873 |
% \end{macrocode} |
| 874 |
% Note name change required on older luatex, for hash table access. |
| 875 |
% \begin{macrocode} |
| 876 |
\countdef \CountZero =0 % |
| 877 |
\dimendef \dimenzero =0 % |
| 878 |
\mathchardef \mathcharzero =0 % |
| 879 |
\muskipdef \muskipzero =0 % |
| 880 |
\skipdef \skipzero =0 % |
| 881 |
\toksdef \tokszero =0 % |
| 882 |
\directlua{require("ltluatex")} |
| 883 |
\endgroup |
| 884 |
%<2ekernel>} |
| 885 |
%<latexrelease>\EndIncludeInRelease |
| 886 |
% \end{macrocode} |
| 887 |
% |
| 888 |
% \changes{v1.0b}{2015/10/02}{Fix backing out of \TeX{} code} |
| 889 |
% \changes{v1.0c}{2015/10/02}{Allow backing out of Lua code} |
| 890 |
% \begin{macrocode} |
| 891 |
%<latexrelease>\IncludeInRelease{0000/00/00} |
| 892 |
%<latexrelease> {\newluafunction}{LuaTeX}% |
| 893 |
%<latexrelease>\let\e@alloc@attribute@count\@undefined |
| 894 |
%<latexrelease>\let\newattribute\@undefined |
| 895 |
%<latexrelease>\let\setattribute\@undefined |
| 896 |
%<latexrelease>\let\unsetattribute\@undefined |
| 897 |
%<latexrelease>\let\e@alloc@ccodetable@count\@undefined |
| 898 |
%<latexrelease>\let\newcatcodetable\@undefined |
| 899 |
%<latexrelease>\let\catcodetable@initex\@undefined |
| 900 |
%<latexrelease>\let\catcodetable@string\@undefined |
| 901 |
%<latexrelease>\let\catcodetable@latex\@undefined |
| 902 |
%<latexrelease>\let\catcodetable@atletter\@undefined |
| 903 |
%<latexrelease>\let\e@alloc@luafunction@count\@undefined |
| 904 |
%<latexrelease>\let\newluafunction\@undefined |
| 905 |
%<latexrelease>\let\e@alloc@luafunction@count\@undefined |
| 906 |
%<latexrelease>\let\newwhatsit\@undefined |
| 907 |
%<latexrelease>\let\e@alloc@whatsit@count\@undefined |
| 908 |
%<latexrelease>\let\newluabytecode\@undefined |
| 909 |
%<latexrelease>\let\e@alloc@bytecode@count\@undefined |
| 910 |
%<latexrelease>\let\newluachunkname\@undefined |
| 911 |
%<latexrelease>\let\e@alloc@luachunk@count\@undefined |
| 912 |
%<latexrelease>\directlua{luatexbase.uninstall()} |
| 913 |
%<latexrelease>\EndIncludeInRelease |
| 914 |
% \end{macrocode} |
| 915 |
% |
| 916 |
% In \verb|\everyjob|, if luaotfload is available, load it and switch to TU. |
| 917 |
% \begin{macrocode} |
| 918 |
%<latexrelease>\IncludeInRelease{2017/01/01}% |
| 919 |
%<latexrelease> {\fontencoding}{TU in everyjob}% |
| 920 |
%<latexrelease>\fontencoding{TU}\let\encodingdefault\f@encoding |
| 921 |
%<latexrelease>\ifx\directlua\@undefined\else |
| 922 |
%<2ekernel>\everyjob\expandafter{% |
| 923 |
%<2ekernel> \the\everyjob |
| 924 |
%<*2ekernel,latexrelease> |
| 925 |
\directlua{% |
| 926 |
if xpcall(function ()% |
| 927 |
require('luaotfload-main')% |
| 928 |
end,texio.write_nl) then % |
| 929 |
local _void = luaotfload.main ()% |
| 930 |
else % |
| 931 |
texio.write_nl('Error in luaotfload: reverting to OT1')% |
| 932 |
tex.print('\string\\def\string\\encodingdefault{OT1}')% |
| 933 |
end % |
| 934 |
}% |
| 935 |
\let\f@encoding\encodingdefault |
| 936 |
\expandafter\let\csname [email protected]\endcsname\fmtversion |
| 937 |
%</2ekernel,latexrelease> |
| 938 |
%<latexrelease>\fi |
| 939 |
%<2ekernel> } |
| 940 |
%<latexrelease>\EndIncludeInRelease |
| 941 |
%<latexrelease>\IncludeInRelease{0000/00/00}% |
| 942 |
%<latexrelease> {\fontencoding}{TU in everyjob}% |
| 943 |
%<latexrelease>\fontencoding{OT1}\let\encodingdefault\f@encoding |
| 944 |
%<latexrelease>\EndIncludeInRelease |
| 945 |
% \end{macrocode} |
| 946 |
% |
| 947 |
% \begin{macrocode} |
| 948 |
%<2ekernel|latexrelease>\fi |
| 949 |
%</2ekernel|tex|latexrelease> |
| 950 |
% \end{macrocode} |
| 951 |
% |
| 952 |
% \subsection{Lua module preliminaries} |
| 953 |
% |
| 954 |
% \begingroup |
| 955 |
% |
| 956 |
% \begingroup\lccode`~=`_ |
| 957 |
% \lowercase{\endgroup\let~}_ |
| 958 |
% \catcode`_=12 |
| 959 |
% |
| 960 |
% \begin{macrocode} |
| 961 |
%<*lua> |
| 962 |
% \end{macrocode} |
| 963 |
% |
| 964 |
% Some set up for the Lua module which is needed for all of the Lua |
| 965 |
% functionality added here. |
| 966 |
% |
| 967 |
% \begin{macro}{luatexbase} |
| 968 |
% \changes{v1.0a}{2015/09/24}{Table added} |
| 969 |
% Set up the table for the returned functions. This is used to expose |
| 970 |
% all of the public functions. |
| 971 |
% \begin{macrocode} |
| 972 |
luatexbase = luatexbase or { } |
| 973 |
local luatexbase = luatexbase |
| 974 |
% \end{macrocode} |
| 975 |
% \end{macro} |
| 976 |
% |
| 977 |
% Some Lua best practice: use local versions of functions where possible. |
| 978 |
% \changes{v1.1u}{2021/08/11}{Define missing local function} |
| 979 |
% \changes{v1.2b}{2023/01/19}{Remove unused local variable tex_setattribute} |
| 980 |
% \begin{macrocode} |
| 981 |
local string_gsub = string.gsub |
| 982 |
local tex_count = tex.count |
| 983 |
local tex_setcount = tex.setcount |
| 984 |
local texio_write_nl = texio.write_nl |
| 985 |
local flush_list = node.flush_list |
| 986 |
% \end{macrocode} |
| 987 |
% \changes{v1.0i}{2015/11/29}{Declare this as local before used in the module error definitions (PHG)} |
| 988 |
% \begin{macrocode} |
| 989 |
local luatexbase_warning |
| 990 |
local luatexbase_error |
| 991 |
% \end{macrocode} |
| 992 |
% |
| 993 |
% \subsection{Lua module utilities} |
| 994 |
% |
| 995 |
% \subsubsection{Module tracking} |
| 996 |
% |
| 997 |
% \begin{macro}{modules} |
| 998 |
% \changes{v1.0a}{2015/09/24}{Function modified} |
| 999 |
% To allow tracking of module usage, a structure is provided to store |
| 1000 |
% information and to return it. |
| 1001 |
% \begin{macrocode} |
| 1002 |
local modules = modules or { } |
| 1003 |
% \end{macrocode} |
| 1004 |
% \end{macro} |
| 1005 |
% |
| 1006 |
% \begin{macro}{provides_module} |
| 1007 |
% \changes{v1.0a}{2015/09/24}{Function added} |
| 1008 |
% \changes{v1.0f}{2015/10/03}{use luatexbase\_log} |
| 1009 |
% \changes{v1.2d}{2024/06/04}{Add \texttt{v} to version string if required (gh/1364)} |
| 1010 |
% \changes{v1.2e}{2024/08/16}{Support missing version string (gh/1443)} |
| 1011 |
% Local function to write to the log. |
| 1012 |
% \begin{macrocode} |
| 1013 |
local function luatexbase_log(text) |
| 1014 |
texio_write_nl("log", text) |
| 1015 |
end |
| 1016 |
% \end{macrocode} |
| 1017 |
% |
| 1018 |
% Modelled on |\ProvidesPackage|, we store much the same information but |
| 1019 |
% with a little more structure. |
| 1020 |
% \begin{macrocode} |
| 1021 |
local function provides_module(info) |
| 1022 |
if not (info and info.name) then |
| 1023 |
luatexbase_error("Missing module name for provides_module") |
| 1024 |
end |
| 1025 |
local function spaced(text) |
| 1026 |
return text and (" " .. text) or "" |
| 1027 |
end |
| 1028 |
luatexbase_log( |
| 1029 |
"Lua module: " .. info.name |
| 1030 |
.. spaced(info.date) |
| 1031 |
.. spaced(info.version and string_gsub(info.version or "","^(%d)","v%1")) |
| 1032 |
.. spaced(info.description) |
| 1033 |
) |
| 1034 |
modules[info.name] = info |
| 1035 |
end |
| 1036 |
luatexbase.provides_module = provides_module |
| 1037 |
% \end{macrocode} |
| 1038 |
% \end{macro} |
| 1039 |
% |
| 1040 |
% \subsubsection{Module messages} |
| 1041 |
% |
| 1042 |
% There are various warnings and errors that need to be given. For warnings |
| 1043 |
% we can get exactly the same formatting as from \TeX{}. For errors we have to |
| 1044 |
% make some changes. Here we give the text of the error in the \LaTeX{} format |
| 1045 |
% then force an error from Lua to halt the run. Splitting the message text is |
| 1046 |
% done using |\n| which takes the place of |\MessageBreak|. |
| 1047 |
% |
| 1048 |
% First an auxiliary for the formatting: this measures up the message |
| 1049 |
% leader so we always get the correct indent. |
| 1050 |
% \changes{v1.0j}{2015/12/02}{Declaration/use of first\_head fixed (PHG)} |
| 1051 |
% \begin{macrocode} |
| 1052 |
local function msg_format(mod, msg_type, text) |
| 1053 |
local leader = "" |
| 1054 |
local cont |
| 1055 |
local first_head |
| 1056 |
if mod == "LaTeX" then |
| 1057 |
cont = string_gsub(leader, ".", " ") |
| 1058 |
first_head = leader .. "LaTeX: " |
| 1059 |
else |
| 1060 |
first_head = leader .. "Module " .. msg_type |
| 1061 |
cont = "(" .. mod .. ")" |
| 1062 |
.. string_gsub(first_head, ".", " ") |
| 1063 |
first_head = leader .. "Module " .. mod .. " " .. msg_type .. ":" |
| 1064 |
end |
| 1065 |
if msg_type == "Error" then |
| 1066 |
first_head = "\n" .. first_head |
| 1067 |
end |
| 1068 |
if string.sub(text,-1) ~= "\n" then |
| 1069 |
text = text .. " " |
| 1070 |
end |
| 1071 |
return first_head .. " " |
| 1072 |
.. string_gsub( |
| 1073 |
text |
| 1074 |
.. "on input line " |
| 1075 |
.. tex.inputlineno, "\n", "\n" .. cont .. " " |
| 1076 |
) |
| 1077 |
.. "\n" |
| 1078 |
end |
| 1079 |
% \end{macrocode} |
| 1080 |
% |
| 1081 |
% \begin{macro}{module_info} |
| 1082 |
% \changes{v1.0a}{2015/09/24}{Function added} |
| 1083 |
% \begin{macro}{module_warning} |
| 1084 |
% \changes{v1.0a}{2015/09/24}{Function added} |
| 1085 |
% \begin{macro}{module_error} |
| 1086 |
% \changes{v1.0a}{2015/09/24}{Function added} |
| 1087 |
% Write messages. |
| 1088 |
% \begin{macrocode} |
| 1089 |
local function module_info(mod, text) |
| 1090 |
texio_write_nl("log", msg_format(mod, "Info", text)) |
| 1091 |
end |
| 1092 |
luatexbase.module_info = module_info |
| 1093 |
local function module_warning(mod, text) |
| 1094 |
texio_write_nl("term and log",msg_format(mod, "Warning", text)) |
| 1095 |
end |
| 1096 |
luatexbase.module_warning = module_warning |
| 1097 |
local function module_error(mod, text) |
| 1098 |
error(msg_format(mod, "Error", text)) |
| 1099 |
end |
| 1100 |
luatexbase.module_error = module_error |
| 1101 |
% \end{macrocode} |
| 1102 |
% \end{macro} |
| 1103 |
% \end{macro} |
| 1104 |
% \end{macro} |
| 1105 |
% |
| 1106 |
% Dedicated versions for the rest of the code here. |
| 1107 |
% \begin{macrocode} |
| 1108 |
function luatexbase_warning(text) |
| 1109 |
module_warning("luatexbase", text) |
| 1110 |
end |
| 1111 |
function luatexbase_error(text) |
| 1112 |
module_error("luatexbase", text) |
| 1113 |
end |
| 1114 |
% \end{macrocode} |
| 1115 |
% |
| 1116 |
% |
| 1117 |
% \subsection{Accessing register numbers from Lua} |
| 1118 |
% |
| 1119 |
% \changes{v1.0g}{2015/11/14}{Track Lua\TeX{} changes for |
| 1120 |
% \texttt{(new)token.create}} |
| 1121 |
% Collect up the data from the \TeX{} level into a Lua table: from |
| 1122 |
% version~0.80, Lua\TeX{} makes that easy. |
| 1123 |
% \changes{v1.0j}{2015/12/02}{Adjust hashtokens to store the result of tex.hashtokens()), not the function (PHG)} |
| 1124 |
% \begin{macrocode} |
| 1125 |
local luaregisterbasetable = { } |
| 1126 |
local registermap = { |
| 1127 |
attributezero = "assign_attr" , |
| 1128 |
charzero = "char_given" , |
| 1129 |
CountZero = "assign_int" , |
| 1130 |
dimenzero = "assign_dimen" , |
| 1131 |
mathcharzero = "math_given" , |
| 1132 |
muskipzero = "assign_mu_skip" , |
| 1133 |
skipzero = "assign_skip" , |
| 1134 |
tokszero = "assign_toks" , |
| 1135 |
} |
| 1136 |
local createtoken |
| 1137 |
if tex.luatexversion > 81 then |
| 1138 |
createtoken = token.create |
| 1139 |
elseif tex.luatexversion > 79 then |
| 1140 |
createtoken = newtoken.create |
| 1141 |
end |
| 1142 |
local hashtokens = tex.hashtokens() |
| 1143 |
local luatexversion = tex.luatexversion |
| 1144 |
for i,j in pairs (registermap) do |
| 1145 |
if luatexversion < 80 then |
| 1146 |
luaregisterbasetable[hashtokens[i][1]] = |
| 1147 |
hashtokens[i][2] |
| 1148 |
else |
| 1149 |
luaregisterbasetable[j] = createtoken(i).mode |
| 1150 |
end |
| 1151 |
end |
| 1152 |
% \end{macrocode} |
| 1153 |
% |
| 1154 |
% \begin{macro}{registernumber} |
| 1155 |
% Working out the correct return value can be done in two ways. For older |
| 1156 |
% Lua\TeX{} releases it has to be extracted from the |hashtokens|. On the |
| 1157 |
% other hand, newer Lua\TeX{}'s have |newtoken|, and whilst |.mode| isn't |
| 1158 |
% currently documented, Hans Hagen pointed to this approach so we should be |
| 1159 |
% OK. |
| 1160 |
% \begin{macrocode} |
| 1161 |
local registernumber |
| 1162 |
if luatexversion < 80 then |
| 1163 |
function registernumber(name) |
| 1164 |
local nt = hashtokens[name] |
| 1165 |
if(nt and luaregisterbasetable[nt[1]]) then |
| 1166 |
return nt[2] - luaregisterbasetable[nt[1]] |
| 1167 |
else |
| 1168 |
return false |
| 1169 |
end |
| 1170 |
end |
| 1171 |
else |
| 1172 |
function registernumber(name) |
| 1173 |
local nt = createtoken(name) |
| 1174 |
if(luaregisterbasetable[nt.cmdname]) then |
| 1175 |
return nt.mode - luaregisterbasetable[nt.cmdname] |
| 1176 |
else |
| 1177 |
return false |
| 1178 |
end |
| 1179 |
end |
| 1180 |
end |
| 1181 |
luatexbase.registernumber = registernumber |
| 1182 |
% \end{macrocode} |
| 1183 |
% \end{macro} |
| 1184 |
% |
| 1185 |
% \subsection{Attribute allocation} |
| 1186 |
% |
| 1187 |
% \begin{macro}{new_attribute} |
| 1188 |
% \changes{v1.0a}{2015/09/24}{Function added} |
| 1189 |
% \changes{v1.1c}{2017/02/18}{Parameterize count used in tracking} |
| 1190 |
% As attributes are used for Lua manipulations its useful to be able |
| 1191 |
% to assign from this end. |
| 1192 |
% \InternalDetectionOff |
| 1193 |
% \begin{macrocode} |
| 1194 |
local attributes=setmetatable( |
| 1195 |
{}, |
| 1196 |
{ |
| 1197 |
__index = function(t,key) |
| 1198 |
return registernumber(key) or nil |
| 1199 |
end} |
| 1200 |
) |
| 1201 |
luatexbase.attributes = attributes |
| 1202 |
% \end{macrocode} |
| 1203 |
% |
| 1204 |
% \begin{macrocode} |
| 1205 |
local attribute_count_name = |
| 1206 |
attribute_count_name or "e@alloc@attribute@count" |
| 1207 |
local function new_attribute(name) |
| 1208 |
tex_setcount("global", attribute_count_name, |
| 1209 |
tex_count[attribute_count_name] + 1) |
| 1210 |
if tex_count[attribute_count_name] > 65534 then |
| 1211 |
luatexbase_error("No room for a new \\attribute") |
| 1212 |
end |
| 1213 |
attributes[name]= tex_count[attribute_count_name] |
| 1214 |
luatexbase_log("Lua-only attribute " .. name .. " = " .. |
| 1215 |
tex_count[attribute_count_name]) |
| 1216 |
return tex_count[attribute_count_name] |
| 1217 |
end |
| 1218 |
luatexbase.new_attribute = new_attribute |
| 1219 |
% \end{macrocode} |
| 1220 |
% \InternalDetectionOn |
| 1221 |
% \end{macro} |
| 1222 |
% |
| 1223 |
% \subsection{Custom whatsit allocation} |
| 1224 |
% |
| 1225 |
% \begin{macro}{new_whatsit} |
| 1226 |
% \changes{v1.1c}{2017/02/18}{Parameterize count used in tracking} |
| 1227 |
% Much the same as for attribute allocation in Lua. |
| 1228 |
% \begin{macrocode} |
| 1229 |
local whatsit_count_name = whatsit_count_name or "e@alloc@whatsit@count" |
| 1230 |
local function new_whatsit(name) |
| 1231 |
tex_setcount("global", whatsit_count_name, |
| 1232 |
tex_count[whatsit_count_name] + 1) |
| 1233 |
if tex_count[whatsit_count_name] > 65534 then |
| 1234 |
luatexbase_error("No room for a new custom whatsit") |
| 1235 |
end |
| 1236 |
luatexbase_log("Custom whatsit " .. (name or "") .. " = " .. |
| 1237 |
tex_count[whatsit_count_name]) |
| 1238 |
return tex_count[whatsit_count_name] |
| 1239 |
end |
| 1240 |
luatexbase.new_whatsit = new_whatsit |
| 1241 |
% \end{macrocode} |
| 1242 |
% \end{macro} |
| 1243 |
% |
| 1244 |
% \subsection{Bytecode register allocation} |
| 1245 |
% |
| 1246 |
% \begin{macro}{new_bytecode} |
| 1247 |
% \changes{v1.1c}{2017/02/18}{Parameterize count used in tracking} |
| 1248 |
% Much the same as for attribute allocation in Lua. |
| 1249 |
% The optional \meta{name} argument is used in the log if given. |
| 1250 |
% \begin{macrocode} |
| 1251 |
local bytecode_count_name = |
| 1252 |
bytecode_count_name or "e@alloc@bytecode@count" |
| 1253 |
local function new_bytecode(name) |
| 1254 |
tex_setcount("global", bytecode_count_name, |
| 1255 |
tex_count[bytecode_count_name] + 1) |
| 1256 |
if tex_count[bytecode_count_name] > 65534 then |
| 1257 |
luatexbase_error("No room for a new bytecode register") |
| 1258 |
end |
| 1259 |
luatexbase_log("Lua bytecode " .. (name or "") .. " = " .. |
| 1260 |
tex_count[bytecode_count_name]) |
| 1261 |
return tex_count[bytecode_count_name] |
| 1262 |
end |
| 1263 |
luatexbase.new_bytecode = new_bytecode |
| 1264 |
% \end{macrocode} |
| 1265 |
% \end{macro} |
| 1266 |
% |
| 1267 |
% \subsection{Lua chunk name allocation} |
| 1268 |
% |
| 1269 |
% \begin{macro}{new_chunkname} |
| 1270 |
% \changes{v1.1c}{2017/02/18}{Parameterize count used in tracking} |
| 1271 |
% As for bytecode registers but also store the name in the |
| 1272 |
% |lua.name| table. |
| 1273 |
% \begin{macrocode} |
| 1274 |
local chunkname_count_name = |
| 1275 |
chunkname_count_name or "e@alloc@luachunk@count" |
| 1276 |
local function new_chunkname(name) |
| 1277 |
tex_setcount("global", chunkname_count_name, |
| 1278 |
tex_count[chunkname_count_name] + 1) |
| 1279 |
local chunkname_count = tex_count[chunkname_count_name] |
| 1280 |
chunkname_count = chunkname_count + 1 |
| 1281 |
if chunkname_count > 65534 then |
| 1282 |
luatexbase_error("No room for a new chunkname") |
| 1283 |
end |
| 1284 |
lua.name[chunkname_count]=name |
| 1285 |
luatexbase_log("Lua chunkname " .. (name or "") .. " = " .. |
| 1286 |
chunkname_count .. "\n") |
| 1287 |
return chunkname_count |
| 1288 |
end |
| 1289 |
luatexbase.new_chunkname = new_chunkname |
| 1290 |
% \end{macrocode} |
| 1291 |
% \end{macro} |
| 1292 |
% |
| 1293 |
% \subsection{Lua function allocation} |
| 1294 |
% |
| 1295 |
% \begin{macro}{new_luafunction} |
| 1296 |
% \changes{v1.1i}{2018/10/21}{Function added} |
| 1297 |
% \changes{v1.2c}{2023/07/02}{Ensure existing table entries not overwritten gh/1100} |
| 1298 |
% Much the same as for attribute allocation in Lua. |
| 1299 |
% The optional \meta{name} argument is used in the log if given. |
| 1300 |
% \begin{macrocode} |
| 1301 |
local luafunction_count_name = |
| 1302 |
luafunction_count_name or "e@alloc@luafunction@count" |
| 1303 |
local function new_luafunction(name) |
| 1304 |
tex_setcount("global", luafunction_count_name, |
| 1305 |
math.max( |
| 1306 |
#(lua.get_functions_table()), |
| 1307 |
tex_count[luafunction_count_name]) |
| 1308 |
+ 1) |
| 1309 |
lua.get_functions_table()[tex_count[luafunction_count_name]] = false |
| 1310 |
if tex_count[luafunction_count_name] > 65534 then |
| 1311 |
luatexbase_error("No room for a new luafunction register") |
| 1312 |
end |
| 1313 |
luatexbase_log("Lua function " .. (name or "") .. " = " .. |
| 1314 |
tex_count[luafunction_count_name]) |
| 1315 |
return tex_count[luafunction_count_name] |
| 1316 |
end |
| 1317 |
luatexbase.new_luafunction = new_luafunction |
| 1318 |
% \end{macrocode} |
| 1319 |
% \end{macro} |
| 1320 |
% |
| 1321 |
% \subsection{Lua callback management} |
| 1322 |
% |
| 1323 |
% The native mechanism for callbacks in Lua\TeX\ allows only one per function. |
| 1324 |
% That is extremely restrictive and so a mechanism is needed to add and |
| 1325 |
% remove callbacks from the appropriate hooks. |
| 1326 |
% |
| 1327 |
% \subsubsection{Housekeeping} |
| 1328 |
% |
| 1329 |
% The main table: keys are callback names, and values are the associated lists |
| 1330 |
% of functions. More precisely, the entries in the list are tables holding the |
| 1331 |
% actual function as |func| and the identifying description as |description|. |
| 1332 |
% Only callbacks with a non-empty list of functions have an entry in this |
| 1333 |
% list. |
| 1334 |
% |
| 1335 |
% Actually there are two tables: |realcallbacklist| directly contains the entries |
| 1336 |
% as described above while |callbacklist| only directly contains the already sorted |
| 1337 |
% entries. Other entries can be queried through |callbacklist| too which triggers a |
| 1338 |
% resort. |
| 1339 |
% |
| 1340 |
% Additionally |callbackrules| describes the ordering constraints: It contains two |
| 1341 |
% element tables with the descriptions of the constrained callback implementations. |
| 1342 |
% It can additionally contain a |type| entry indicating the kind of rule. A missing |
| 1343 |
% value indicates a normal ordering constraint. |
| 1344 |
% |
| 1345 |
% \changes{v1.2a}{2022/10/03}{Add rules for callback ordering} |
| 1346 |
% \begin{macrocode} |
| 1347 |
local realcallbacklist = {} |
| 1348 |
local callbackrules = {} |
| 1349 |
local callbacklist = setmetatable({}, { |
| 1350 |
__index = function(t, name) |
| 1351 |
local list = realcallbacklist[name] |
| 1352 |
local rules = callbackrules[name] |
| 1353 |
if list and rules then |
| 1354 |
local meta = {} |
| 1355 |
for i, entry in ipairs(list) do |
| 1356 |
local t = {value = entry, count = 0, pos = i} |
| 1357 |
meta[entry.description], list[i] = t, t |
| 1358 |
end |
| 1359 |
local count = #list |
| 1360 |
local pos = count |
| 1361 |
for i, rule in ipairs(rules) do |
| 1362 |
local rule = rules[i] |
| 1363 |
local pre, post = meta[rule[1]], meta[rule[2]] |
| 1364 |
if pre and post then |
| 1365 |
if rule.type then |
| 1366 |
if not rule.hidden then |
| 1367 |
assert(rule.type == 'incompatible-warning' and luatexbase_warning |
| 1368 |
or rule.type == 'incompatible-error' and luatexbase_error)( |
| 1369 |
"Incompatible functions \"" .. rule[1] .. "\" and \"" .. rule[2] |
| 1370 |
.. "\" specified for callback \"" .. name .. "\".") |
| 1371 |
rule.hidden = true |
| 1372 |
end |
| 1373 |
else |
| 1374 |
local post_count = post.count |
| 1375 |
post.count = post_count+1 |
| 1376 |
if post_count == 0 then |
| 1377 |
local post_pos = post.pos |
| 1378 |
if post_pos ~= pos then |
| 1379 |
local new_post_pos = list[pos] |
| 1380 |
new_post_pos.pos = post_pos |
| 1381 |
list[post_pos] = new_post_pos |
| 1382 |
end |
| 1383 |
list[pos] = nil |
| 1384 |
pos = pos - 1 |
| 1385 |
end |
| 1386 |
pre[#pre+1] = post |
| 1387 |
end |
| 1388 |
end |
| 1389 |
end |
| 1390 |
for i=1, count do -- The actual sort begins |
| 1391 |
local current = list[i] |
| 1392 |
if current then |
| 1393 |
meta[current.value.description] = nil |
| 1394 |
for j, cur in ipairs(current) do |
| 1395 |
local count = cur.count |
| 1396 |
if count == 1 then |
| 1397 |
pos = pos + 1 |
| 1398 |
list[pos] = cur |
| 1399 |
else |
| 1400 |
cur.count = count - 1 |
| 1401 |
end |
| 1402 |
end |
| 1403 |
list[i] = current.value |
| 1404 |
else |
| 1405 |
-- Cycle occurred. TODO: Show cycle for debugging |
| 1406 |
-- list[i] = ... |
| 1407 |
local remaining = {} |
| 1408 |
for name, entry in next, meta do |
| 1409 |
local value = entry.value |
| 1410 |
list[#list + 1] = entry.value |
| 1411 |
remaining[#remaining + 1] = name |
| 1412 |
end |
| 1413 |
table.sort(remaining) |
| 1414 |
local first_name = remaining[1] |
| 1415 |
for j, name in ipairs(remaining) do |
| 1416 |
local entry = meta[name] |
| 1417 |
list[i + j - 1] = entry.value |
| 1418 |
for _, post_entry in ipairs(entry) do |
| 1419 |
local post_name = post_entry.value.description |
| 1420 |
if not remaining[post_name] then |
| 1421 |
remaining[post_name] = name |
| 1422 |
end |
| 1423 |
end |
| 1424 |
end |
| 1425 |
local cycle = {first_name} |
| 1426 |
local index = 1 |
| 1427 |
local last_name = first_name |
| 1428 |
repeat |
| 1429 |
cycle[last_name] = index |
| 1430 |
last_name = remaining[last_name] |
| 1431 |
index = index + 1 |
| 1432 |
cycle[index] = last_name |
| 1433 |
until cycle[last_name] |
| 1434 |
local length = index - cycle[last_name] + 1 |
| 1435 |
table.move(cycle, cycle[last_name], index, 1) |
| 1436 |
for i=2, length//2 do |
| 1437 |
cycle[i], cycle[length + 1 - i] = cycle[length + 1 - i], cycle[i] |
| 1438 |
end |
| 1439 |
error('Cycle occurred at ' .. table.concat(cycle, ' -> ', 1, length)) |
| 1440 |
end |
| 1441 |
end |
| 1442 |
end |
| 1443 |
realcallbacklist[name] = list |
| 1444 |
t[name] = list |
| 1445 |
return list |
| 1446 |
end |
| 1447 |
}) |
| 1448 |
% \end{macrocode} |
| 1449 |
% |
| 1450 |
% Numerical codes for callback types, and name-to-value association (the |
| 1451 |
% table keys are strings, the values are numbers). |
| 1452 |
% \begin{macrocode} |
| 1453 |
local list, data, exclusive, simple, reverselist = 1, 2, 3, 4, 5 |
| 1454 |
local types = { |
| 1455 |
list = list, |
| 1456 |
data = data, |
| 1457 |
exclusive = exclusive, |
| 1458 |
simple = simple, |
| 1459 |
reverselist = reverselist, |
| 1460 |
} |
| 1461 |
% \end{macrocode} |
| 1462 |
% |
| 1463 |
% Now, list all predefined callbacks with their current type, based on the |
| 1464 |
% Lua\TeX{} manual version~1.01. A full list of the currently-available |
| 1465 |
% callbacks can be obtained using |
| 1466 |
% \begin{verbatim} |
| 1467 |
% \directlua{ |
| 1468 |
% for i,_ in pairs(callback.list()) do |
| 1469 |
% texio.write_nl("- " .. i) |
| 1470 |
% end |
| 1471 |
% } |
| 1472 |
% \bye |
| 1473 |
% \end{verbatim} |
| 1474 |
% in plain Lua\TeX{}. (Some undocumented callbacks are omitted as they are |
| 1475 |
% to be removed.) |
| 1476 |
% \begin{macrocode} |
| 1477 |
local callbacktypes = callbacktypes or { |
| 1478 |
% \end{macrocode} |
| 1479 |
% Section 8.2: file discovery callbacks. |
| 1480 |
% \changes{v1.1g}{2018/05/02}{find\_sfd\_file removed} |
| 1481 |
% \begin{macrocode} |
| 1482 |
find_read_file = exclusive, |
| 1483 |
find_write_file = exclusive, |
| 1484 |
find_font_file = data, |
| 1485 |
find_output_file = data, |
| 1486 |
find_format_file = data, |
| 1487 |
find_vf_file = data, |
| 1488 |
find_map_file = data, |
| 1489 |
find_enc_file = data, |
| 1490 |
find_pk_file = data, |
| 1491 |
find_data_file = data, |
| 1492 |
find_opentype_file = data, |
| 1493 |
find_truetype_file = data, |
| 1494 |
find_type1_file = data, |
| 1495 |
find_image_file = data, |
| 1496 |
% \end{macrocode} |
| 1497 |
% \changes{v1.1g}{2018/05/02}{read\_sfd\_file removed} |
| 1498 |
% \begin{macrocode} |
| 1499 |
open_read_file = exclusive, |
| 1500 |
read_font_file = exclusive, |
| 1501 |
read_vf_file = exclusive, |
| 1502 |
read_map_file = exclusive, |
| 1503 |
read_enc_file = exclusive, |
| 1504 |
read_pk_file = exclusive, |
| 1505 |
read_data_file = exclusive, |
| 1506 |
read_truetype_file = exclusive, |
| 1507 |
read_type1_file = exclusive, |
| 1508 |
read_opentype_file = exclusive, |
| 1509 |
% \end{macrocode} |
| 1510 |
% \changes{v1.0m}{2016/02/11}{read\_cidmap\_file added} |
| 1511 |
% Not currently used by luatex but included for completeness. |
| 1512 |
% may be used by a font handler. |
| 1513 |
% \begin{macrocode} |
| 1514 |
find_cidmap_file = data, |
| 1515 |
read_cidmap_file = exclusive, |
| 1516 |
% \end{macrocode} |
| 1517 |
% Section 8.3: data processing callbacks. |
| 1518 |
% \changes{v1.0m}{2016/02/11}{token\_filter removed} |
| 1519 |
% \begin{macrocode} |
| 1520 |
process_input_buffer = data, |
| 1521 |
process_output_buffer = data, |
| 1522 |
process_jobname = data, |
| 1523 |
% \end{macrocode} |
| 1524 |
% Section 8.4: node list processing callbacks. |
| 1525 |
% \changes{v1.0m}{2016/02/11} |
| 1526 |
% {process\_rule, [hv]pack\_quality append\_to\_vlist\_filter added} |
| 1527 |
% \changes{v1.0n}{2016/03/13}{insert\_local\_par added} |
| 1528 |
% \changes{v1.0n}{2016/03/13}{contribute\_filter added} |
| 1529 |
% \changes{v1.1h}{2018/08/18}{append\_to\_vlist\_filter is \texttt{exclusive}} |
| 1530 |
% \changes{v1.1j}{2019/06/18}{new\_graf added} |
| 1531 |
% \changes{v1.1k}{2019/10/02}{linebreak\_filter is \texttt{exclusive}} |
| 1532 |
% \changes{v1.1k}{2019/10/02}{process\_rule is \texttt{exclusive}} |
| 1533 |
% \changes{v1.1k}{2019/10/02}{mlist\_to\_hlist is \texttt{exclusive}} |
| 1534 |
% \changes{v1.1l}{2020/02/02}{post\_linebreak\_filter is \texttt{reverselist}} |
| 1535 |
% \changes{v1.1p}{2020/08/01}{new\_graf is \texttt{exclusive}} |
| 1536 |
% \changes{v1.1w}{2021/11/17}{hpack\_quality is \texttt{exclusive}} |
| 1537 |
% \changes{v1.1w}{2021/11/17}{vpack\_quality is \texttt{exclusive}} |
| 1538 |
% \begin{macrocode} |
| 1539 |
contribute_filter = simple, |
| 1540 |
buildpage_filter = simple, |
| 1541 |
build_page_insert = exclusive, |
| 1542 |
pre_linebreak_filter = list, |
| 1543 |
linebreak_filter = exclusive, |
| 1544 |
append_to_vlist_filter = exclusive, |
| 1545 |
post_linebreak_filter = reverselist, |
| 1546 |
hpack_filter = list, |
| 1547 |
vpack_filter = list, |
| 1548 |
hpack_quality = exclusive, |
| 1549 |
vpack_quality = exclusive, |
| 1550 |
pre_output_filter = list, |
| 1551 |
process_rule = exclusive, |
| 1552 |
hyphenate = simple, |
| 1553 |
ligaturing = simple, |
| 1554 |
kerning = simple, |
| 1555 |
insert_local_par = simple, |
| 1556 |
% mlist_to_hlist = exclusive, |
| 1557 |
new_graf = exclusive, |
| 1558 |
% \end{macrocode} |
| 1559 |
% Section 8.5: information reporting callbacks. |
| 1560 |
% \changes{v1.0m}{2016/02/11}{show\_warning\_message added} |
| 1561 |
% \changes{v1.0p}{2016/11/17}{call\_edit added} |
| 1562 |
% \changes{v1.1g}{2018/05/02}{finish\_synctex\_callback added} |
| 1563 |
% \changes{v1.1j}{2019/06/18}{finish\_synctex\_callback renamed finish\_synctex} |
| 1564 |
% \changes{v1.1j}{2019/06/18}{wrapup\_run added} |
| 1565 |
% \begin{macrocode} |
| 1566 |
pre_dump = simple, |
| 1567 |
start_run = simple, |
| 1568 |
stop_run = simple, |
| 1569 |
start_page_number = simple, |
| 1570 |
stop_page_number = simple, |
| 1571 |
show_error_hook = simple, |
| 1572 |
show_warning_message = simple, |
| 1573 |
show_error_message = simple, |
| 1574 |
show_lua_error_hook = simple, |
| 1575 |
start_file = simple, |
| 1576 |
stop_file = simple, |
| 1577 |
call_edit = simple, |
| 1578 |
finish_synctex = simple, |
| 1579 |
wrapup_run = simple, |
| 1580 |
% \end{macrocode} |
| 1581 |
% Section 8.6: PDF-related callbacks. |
| 1582 |
% \changes{v1.1j}{2019/06/18}{page\_objnum\_provider added} |
| 1583 |
% \changes{v1.1j}{2019/06/18}{process\_pdf\_image\_content added} |
| 1584 |
% \changes{v1.1j}{2019/10/22}{page\_objnum\_provider and process\_pdf\_image\_content classified data} |
| 1585 |
% \changes{v1.1l}{2020/02/02}{page\_order\_index added} |
| 1586 |
% \begin{macrocode} |
| 1587 |
finish_pdffile = data, |
| 1588 |
finish_pdfpage = data, |
| 1589 |
page_objnum_provider = data, |
| 1590 |
page_order_index = data, |
| 1591 |
process_pdf_image_content = data, |
| 1592 |
% \end{macrocode} |
| 1593 |
% Section 8.7: font-related callbacks. |
| 1594 |
% \changes{v1.1e}{2017/03/28}{glyph\_stream\_provider added} |
| 1595 |
% \changes{v1.1g}{2018/05/02}{glyph\_not\_found added} |
| 1596 |
% \changes{v1.1j}{2019/06/18}{make\_extensible added} |
| 1597 |
% \changes{v1.1j}{2019/06/18}{font\_descriptor\_objnum\_provider added} |
| 1598 |
% \changes{v1.1l}{2020/02/02}{glyph\_info added} |
| 1599 |
% \changes{v1.1t}{2021/04/18}{input\_level\_string added} |
| 1600 |
% \changes{v1.1v}{2021/10/15}{provide\_charproc\_data added} |
| 1601 |
% \begin{macrocode} |
| 1602 |
define_font = exclusive, |
| 1603 |
glyph_info = exclusive, |
| 1604 |
glyph_not_found = exclusive, |
| 1605 |
glyph_stream_provider = exclusive, |
| 1606 |
make_extensible = exclusive, |
| 1607 |
font_descriptor_objnum_provider = exclusive, |
| 1608 |
input_level_string = exclusive, |
| 1609 |
provide_charproc_data = exclusive, |
| 1610 |
% \end{macrocode} |
| 1611 |
% \changes{v1.0m}{2016/02/11}{pdf\_stream\_filter\_callback removed} |
| 1612 |
% \begin{macrocode} |
| 1613 |
} |
| 1614 |
luatexbase.callbacktypes=callbacktypes |
| 1615 |
% \end{macrocode} |
| 1616 |
% |
| 1617 |
% \changes{v1.1y}{2022/08/13}{shared\_callbacks added} |
| 1618 |
% Sometimes multiple callbacks correspond to a single underlying engine level callback. |
| 1619 |
% Then the engine level callback should be registered as long as at least one of these |
| 1620 |
% callbacks is in use. This is implemented though a shared table which counts how many |
| 1621 |
% of the involved callbacks are currently in use. The enging level callback is registered |
| 1622 |
% iff this count is not 0. |
| 1623 |
% |
| 1624 |
% We add |mlist_to_hlist| directly to the list to demonstrate this, but the handler gets |
| 1625 |
% added later when it is actually defined. |
| 1626 |
% |
| 1627 |
% All callbacks in this list are treated as user defined callbacks. |
| 1628 |
% |
| 1629 |
% \begin{macrocode} |
| 1630 |
local shared_callbacks = { |
| 1631 |
mlist_to_hlist = { |
| 1632 |
callback = "mlist_to_hlist", |
| 1633 |
count = 0, |
| 1634 |
handler = nil, |
| 1635 |
}, |
| 1636 |
} |
| 1637 |
shared_callbacks.pre_mlist_to_hlist_filter = shared_callbacks.mlist_to_hlist |
| 1638 |
shared_callbacks.post_mlist_to_hlist_filter = shared_callbacks.mlist_to_hlist |
| 1639 |
% \end{macrocode} |
| 1640 |
% |
| 1641 |
% \begin{macro}{callback.register} |
| 1642 |
% \changes{v1.0a}{2015/09/24}{Function modified} |
| 1643 |
% Save the original function for registering callbacks and prevent the |
| 1644 |
% original being used. The original is saved in a place that remains |
| 1645 |
% available so other more sophisticated code can override the approach |
| 1646 |
% taken by the kernel if desired. |
| 1647 |
% \begin{macrocode} |
| 1648 |
local callback_register = callback_register or callback.register |
| 1649 |
function callback.register() |
| 1650 |
luatexbase_error("Attempt to use callback.register() directly\n") |
| 1651 |
end |
| 1652 |
% \end{macrocode} |
| 1653 |
% \end{macro} |
| 1654 |
% |
| 1655 |
% \subsubsection{Handlers} |
| 1656 |
% |
| 1657 |
% The handler function is registered into the callback when the |
| 1658 |
% first function is added to this callback's list. Then, when the callback |
| 1659 |
% is called, the handler takes care of running all functions in the list. |
| 1660 |
% When the last function is removed from the callback's list, the handler |
| 1661 |
% is unregistered. |
| 1662 |
% |
| 1663 |
% More precisely, the functions below are used to generate a specialized |
| 1664 |
% function (closure) for a given callback, which is the actual handler. |
| 1665 |
% |
| 1666 |
% |
| 1667 |
% The way the functions are combined together depends on |
| 1668 |
% the type of the callback. There are currently 4 types of callback, depending |
| 1669 |
% on the calling convention of the functions the callback can hold: |
| 1670 |
% \begin{description} |
| 1671 |
% \item[simple] is for functions that don't return anything: they are called |
| 1672 |
% in order, all with the same argument; |
| 1673 |
% \item[data] is for functions receiving a piece of data of any type |
| 1674 |
% except node list head (and possibly other arguments) and returning it |
| 1675 |
% (possibly modified): the functions are called in order, and each is |
| 1676 |
% passed the return value of the previous (and the other arguments |
| 1677 |
% untouched, if any). The return value is that of the last function; |
| 1678 |
% \item[list] is a specialized variant of \emph{data} for functions |
| 1679 |
% filtering node lists. Such functions are called with a node list head |
| 1680 |
% as the first argument and may return either the head of a modified node list, |
| 1681 |
% or the boolean values |true| or |false|. |
| 1682 |
% The functions are chained the same way as for \emph{data} except for |
| 1683 |
% the following cases. |
| 1684 |
% If a function returns |false|, then |false| is immediately returned and |
| 1685 |
% the following functions are \emph{not} called. If a function returns |
| 1686 |
% |true|, then the same head is passed to the next function. If all |
| 1687 |
% functions return |true|, then the original head is returned, otherwise the return |
| 1688 |
% value of the last function not returning |true| is used. |
| 1689 |
% \item[reverselist] is a specialized variant of \emph{list} which executes |
| 1690 |
% functions in inverse order. |
| 1691 |
% \item[exclusive] is for functions with more complex signatures; functions in |
| 1692 |
% this type of callback are \emph{not} combined: An error is raised if |
| 1693 |
% a second callback is registered. |
| 1694 |
% \end{description} |
| 1695 |
% |
| 1696 |
% Handler for |data| callbacks. |
| 1697 |
% \begin{macrocode} |
| 1698 |
local function data_handler(name) |
| 1699 |
return function(data, ...) |
| 1700 |
for _,i in ipairs(callbacklist[name]) do |
| 1701 |
data = i.func(data,...) |
| 1702 |
end |
| 1703 |
return data |
| 1704 |
end |
| 1705 |
end |
| 1706 |
% \end{macrocode} |
| 1707 |
% Default for user-defined |data| callbacks without explicit default. |
| 1708 |
% \begin{macrocode} |
| 1709 |
local function data_handler_default(value) |
| 1710 |
return value |
| 1711 |
end |
| 1712 |
% \end{macrocode} |
| 1713 |
% Handler for |exclusive| callbacks. We can assume |callbacklist[name]| is not |
| 1714 |
% empty: otherwise, the function wouldn't be registered in the callback any |
| 1715 |
% more. |
| 1716 |
% \begin{macrocode} |
| 1717 |
local function exclusive_handler(name) |
| 1718 |
return function(...) |
| 1719 |
return callbacklist[name][1].func(...) |
| 1720 |
end |
| 1721 |
end |
| 1722 |
% \end{macrocode} |
| 1723 |
% Handler for |list| callbacks. |
| 1724 |
% \changes{v1.0k}{2015/12/02}{resolve name and i.description (PHG)} |
| 1725 |
% \changes{v1.1s}{2020/12/02}{Fix return value of list callbacks} |
| 1726 |
% \changes{v1.1w}{2021/11/17}{Never pass on \texttt{true} return values for list callbacks} |
| 1727 |
% \begin{macrocode} |
| 1728 |
local function list_handler(name) |
| 1729 |
return function(head, ...) |
| 1730 |
local ret |
| 1731 |
for _,i in ipairs(callbacklist[name]) do |
| 1732 |
ret = i.func(head, ...) |
| 1733 |
if ret == false then |
| 1734 |
luatexbase_warning( |
| 1735 |
"Function `" .. i.description .. "' returned false\n" |
| 1736 |
.. "in callback `" .. name .."'" |
| 1737 |
) |
| 1738 |
return false |
| 1739 |
end |
| 1740 |
if ret ~= true then |
| 1741 |
head = ret |
| 1742 |
end |
| 1743 |
end |
| 1744 |
return head |
| 1745 |
end |
| 1746 |
end |
| 1747 |
% \end{macrocode} |
| 1748 |
% Default for user-defined |list| and |reverselist| callbacks without explicit default. |
| 1749 |
% \begin{macrocode} |
| 1750 |
local function list_handler_default(head) |
| 1751 |
return head |
| 1752 |
end |
| 1753 |
% \end{macrocode} |
| 1754 |
% Handler for |reverselist| callbacks. |
| 1755 |
% \changes{v1.1l}{2020/02/02}{Add reverselist callback type} |
| 1756 |
% \begin{macrocode} |
| 1757 |
local function reverselist_handler(name) |
| 1758 |
return function(head, ...) |
| 1759 |
local ret |
| 1760 |
local callbacks = callbacklist[name] |
| 1761 |
for i = #callbacks, 1, -1 do |
| 1762 |
local cb = callbacks[i] |
| 1763 |
ret = cb.func(head, ...) |
| 1764 |
if ret == false then |
| 1765 |
luatexbase_warning( |
| 1766 |
"Function `" .. cb.description .. "' returned false\n" |
| 1767 |
.. "in callback `" .. name .."'" |
| 1768 |
) |
| 1769 |
return false |
| 1770 |
end |
| 1771 |
if ret ~= true then |
| 1772 |
head = ret |
| 1773 |
end |
| 1774 |
end |
| 1775 |
return head |
| 1776 |
end |
| 1777 |
end |
| 1778 |
% \end{macrocode} |
| 1779 |
% Handler for |simple| callbacks. |
| 1780 |
% \begin{macrocode} |
| 1781 |
local function simple_handler(name) |
| 1782 |
return function(...) |
| 1783 |
for _,i in ipairs(callbacklist[name]) do |
| 1784 |
i.func(...) |
| 1785 |
end |
| 1786 |
end |
| 1787 |
end |
| 1788 |
% \end{macrocode} |
| 1789 |
% Default for user-defined |simple| callbacks without explicit default. |
| 1790 |
% \begin{macrocode} |
| 1791 |
local function simple_handler_default() |
| 1792 |
end |
| 1793 |
% \end{macrocode} |
| 1794 |
% |
| 1795 |
% Keep a handlers table for indexed access and a table with the corresponding default functions. |
| 1796 |
% \begin{macrocode} |
| 1797 |
local handlers = { |
| 1798 |
[data] = data_handler, |
| 1799 |
[exclusive] = exclusive_handler, |
| 1800 |
[list] = list_handler, |
| 1801 |
[reverselist] = reverselist_handler, |
| 1802 |
[simple] = simple_handler, |
| 1803 |
} |
| 1804 |
local defaults = { |
| 1805 |
[data] = data_handler_default, |
| 1806 |
[exclusive] = nil, |
| 1807 |
[list] = list_handler_default, |
| 1808 |
[reverselist] = list_handler_default, |
| 1809 |
[simple] = simple_handler_default, |
| 1810 |
} |
| 1811 |
% \end{macrocode} |
| 1812 |
% |
| 1813 |
% \subsubsection{Public functions for callback management} |
| 1814 |
% |
| 1815 |
% Defining user callbacks perhaps should be in package code, |
| 1816 |
% but impacts on |add_to_callback|. |
| 1817 |
% If a default function is not required, it may be declared as |false|. |
| 1818 |
% First we need a list of user callbacks. |
| 1819 |
% \begin{macrocode} |
| 1820 |
local user_callbacks_defaults = {} |
| 1821 |
% \end{macrocode} |
| 1822 |
% |
| 1823 |
% \begin{macro}{create_callback} |
| 1824 |
% \changes{v1.0a}{2015/09/24}{Function added} |
| 1825 |
% \changes{v1.0i}{2015/11/29}{Check name is not nil in error message (PHG)} |
| 1826 |
% \changes{v1.0k}{2015/12/02}{Give more specific error messages (PHG)} |
| 1827 |
% \changes{v1.1l}{2020/02/02}{Provide proper fallbacks for user-defined callbacks without user-provided default handler} |
| 1828 |
% The allocator itself. |
| 1829 |
% \begin{macrocode} |
| 1830 |
local function create_callback(name, ctype, default) |
| 1831 |
local ctype_id = types[ctype] |
| 1832 |
if not name or name == "" |
| 1833 |
or not ctype_id |
| 1834 |
then |
| 1835 |
luatexbase_error("Unable to create callback:\n" .. |
| 1836 |
"valid callback name and type required") |
| 1837 |
end |
| 1838 |
if callbacktypes[name] then |
| 1839 |
luatexbase_error("Unable to create callback `" .. name .. |
| 1840 |
"':\ncallback is already defined") |
| 1841 |
end |
| 1842 |
default = default or defaults[ctype_id] |
| 1843 |
if not default then |
| 1844 |
luatexbase_error("Unable to create callback `" .. name .. |
| 1845 |
"':\ndefault is required for `" .. ctype .. |
| 1846 |
"' callbacks") |
| 1847 |
elseif type (default) ~= "function" then |
| 1848 |
luatexbase_error("Unable to create callback `" .. name .. |
| 1849 |
"':\ndefault is not a function") |
| 1850 |
end |
| 1851 |
user_callbacks_defaults[name] = default |
| 1852 |
callbacktypes[name] = ctype_id |
| 1853 |
end |
| 1854 |
luatexbase.create_callback = create_callback |
| 1855 |
% \end{macrocode} |
| 1856 |
% \end{macro} |
| 1857 |
% |
| 1858 |
% \begin{macro}{call_callback} |
| 1859 |
% \changes{v1.0a}{2015/09/24}{Function added} |
| 1860 |
% \changes{v1.0i}{2015/11/29}{Check name is not nil in error message (PHG)} |
| 1861 |
% \changes{v1.0k}{2015/12/02}{Give more specific error messages (PHG)} |
| 1862 |
% Call a user defined callback. First check arguments. |
| 1863 |
% \begin{macrocode} |
| 1864 |
local function call_callback(name,...) |
| 1865 |
if not name or name == "" then |
| 1866 |
luatexbase_error("Unable to create callback:\n" .. |
| 1867 |
"valid callback name required") |
| 1868 |
end |
| 1869 |
if user_callbacks_defaults[name] == nil then |
| 1870 |
luatexbase_error("Unable to call callback `" .. name |
| 1871 |
.. "':\nunknown or empty") |
| 1872 |
end |
| 1873 |
local l = callbacklist[name] |
| 1874 |
local f |
| 1875 |
if not l then |
| 1876 |
f = user_callbacks_defaults[name] |
| 1877 |
else |
| 1878 |
f = handlers[callbacktypes[name]](name) |
| 1879 |
end |
| 1880 |
return f(...) |
| 1881 |
end |
| 1882 |
luatexbase.call_callback=call_callback |
| 1883 |
% \end{macrocode} |
| 1884 |
% \end{macro} |
| 1885 |
% |
| 1886 |
% \begin{macro}{add_to_callback} |
| 1887 |
% \changes{v1.0a}{2015/09/24}{Function added} |
| 1888 |
% Add a function to a callback. First check arguments. |
| 1889 |
% \changes{v1.0k}{2015/12/02}{Give more specific error messages (PHG)} |
| 1890 |
% \changes{v1.2a}{2022/10/03}{Add rules for callback ordering} |
| 1891 |
% \begin{macrocode} |
| 1892 |
local function add_to_callback(name, func, description) |
| 1893 |
if not name or name == "" then |
| 1894 |
luatexbase_error("Unable to register callback:\n" .. |
| 1895 |
"valid callback name required") |
| 1896 |
end |
| 1897 |
if not callbacktypes[name] or |
| 1898 |
type(func) ~= "function" or |
| 1899 |
not description or |
| 1900 |
description == "" then |
| 1901 |
luatexbase_error( |
| 1902 |
"Unable to register callback.\n\n" |
| 1903 |
.. "Correct usage:\n" |
| 1904 |
.. "add_to_callback(<callback>, <function>, <description>)" |
| 1905 |
) |
| 1906 |
end |
| 1907 |
% \end{macrocode} |
| 1908 |
% Then test if this callback is already in use. If not, initialise its list |
| 1909 |
% and register the proper handler. |
| 1910 |
% \begin{macrocode} |
| 1911 |
local l = realcallbacklist[name] |
| 1912 |
if l == nil then |
| 1913 |
l = { } |
| 1914 |
realcallbacklist[name] = l |
| 1915 |
% \end{macrocode} |
| 1916 |
% \changes{v1.1y}{2022/08/13}{Adapted code for shared\_callbacks} |
| 1917 |
% Handle count for shared engine callbacks. |
| 1918 |
% \begin{macrocode} |
| 1919 |
local shared = shared_callbacks[name] |
| 1920 |
if shared then |
| 1921 |
shared.count = shared.count + 1 |
| 1922 |
if shared.count == 1 then |
| 1923 |
callback_register(shared.callback, shared.handler) |
| 1924 |
end |
| 1925 |
% \end{macrocode} |
| 1926 |
% If it is not a user defined callback use the primitive callback register. |
| 1927 |
% \begin{macrocode} |
| 1928 |
elseif user_callbacks_defaults[name] == nil then |
| 1929 |
callback_register(name, handlers[callbacktypes[name]](name)) |
| 1930 |
end |
| 1931 |
end |
| 1932 |
% \end{macrocode} |
| 1933 |
% \changes{v1.2a}{2022/10/03}{Add rules for callback ordering} |
| 1934 |
% Actually register the function and give an error if more than one |
| 1935 |
% |exclusive| one is registered. |
| 1936 |
% \begin{macrocode} |
| 1937 |
local f = { |
| 1938 |
func = func, |
| 1939 |
description = description, |
| 1940 |
} |
| 1941 |
if callbacktypes[name] == exclusive then |
| 1942 |
if #l == 1 then |
| 1943 |
luatexbase_error( |
| 1944 |
"Cannot add second callback to exclusive function\n`" .. |
| 1945 |
name .. "'") |
| 1946 |
end |
| 1947 |
end |
| 1948 |
table.insert(l, f) |
| 1949 |
callbacklist[name] = nil |
| 1950 |
% \end{macrocode} |
| 1951 |
% Keep user informed. |
| 1952 |
% \begin{macrocode} |
| 1953 |
luatexbase_log( |
| 1954 |
"Inserting `" .. description .. "' in `" .. name .. "'." |
| 1955 |
) |
| 1956 |
end |
| 1957 |
luatexbase.add_to_callback = add_to_callback |
| 1958 |
% \end{macrocode} |
| 1959 |
% \end{macro} |
| 1960 |
% |
| 1961 |
% \begin{macro}{declare_callback_rule} |
| 1962 |
% \changes{v1.2a}{2022/10/03}{Add function} |
| 1963 |
% Add an ordering constraint between two callback implementations |
| 1964 |
% \begin{macrocode} |
| 1965 |
local function declare_callback_rule(name, desc1, relation, desc2) |
| 1966 |
if not callbacktypes[name] or |
| 1967 |
not desc1 or not desc2 or |
| 1968 |
desc1 == "" or desc2 == "" then |
| 1969 |
luatexbase_error( |
| 1970 |
"Unable to create ordering constraint. " |
| 1971 |
.. "Correct usage:\n" |
| 1972 |
.. "declare_callback_rule(<callback>, <description_a>, <description_b>)" |
| 1973 |
) |
| 1974 |
end |
| 1975 |
if relation == 'before' then |
| 1976 |
relation = nil |
| 1977 |
elseif relation == 'after' then |
| 1978 |
desc2, desc1 = desc1, desc2 |
| 1979 |
relation = nil |
| 1980 |
elseif relation == 'incompatible-warning' or relation == 'incompatible-error' then |
| 1981 |
elseif relation == 'unrelated' then |
| 1982 |
else |
| 1983 |
luatexbase_error( |
| 1984 |
"Unknown relation type in declare_callback_rule" |
| 1985 |
) |
| 1986 |
end |
| 1987 |
callbacklist[name] = nil |
| 1988 |
local rules = callbackrules[name] |
| 1989 |
if rules then |
| 1990 |
for i, rule in ipairs(rules) do |
| 1991 |
if rule[1] == desc1 and rule[2] == desc2 or rule[1] == desc2 and rule[2] == desc1 then |
| 1992 |
if relation == 'unrelated' then |
| 1993 |
table.remove(rules, i) |
| 1994 |
else |
| 1995 |
rule[1], rule[2], rule.type = desc1, desc2, relation |
| 1996 |
end |
| 1997 |
return |
| 1998 |
end |
| 1999 |
end |
| 2000 |
if relation ~= 'unrelated' then |
| 2001 |
rules[#rules + 1] = {desc1, desc2, type = relation} |
| 2002 |
end |
| 2003 |
elseif relation ~= 'unrelated' then |
| 2004 |
callbackrules[name] = {{desc1, desc2, type = relation}} |
| 2005 |
end |
| 2006 |
end |
| 2007 |
luatexbase.declare_callback_rule = declare_callback_rule |
| 2008 |
% \end{macrocode} |
| 2009 |
% \end{macro} |
| 2010 |
% |
| 2011 |
% \begin{macro}{remove_from_callback} |
| 2012 |
% \changes{v1.0a}{2015/09/24}{Function added} |
| 2013 |
% \changes{v1.0k}{2015/12/02}{adjust initialization of cb local (PHG)} |
| 2014 |
% \changes{v1.0k}{2015/12/02}{Give more specific error messages (PHG)} |
| 2015 |
% \changes{v1.1m}{2020/03/07}{Do not call callback.register for user-defined callbacks} |
| 2016 |
% \changes{v1.1y}{2022/08/13}{Adapted code for shared\_callbacks} |
| 2017 |
% \changes{v1.2a}{2022/10/03}{Add rules for callback ordering} |
| 2018 |
% Remove a function from a callback. First check arguments. |
| 2019 |
% \begin{macrocode} |
| 2020 |
local function remove_from_callback(name, description) |
| 2021 |
if not name or name == "" then |
| 2022 |
luatexbase_error("Unable to remove function from callback:\n" .. |
| 2023 |
"valid callback name required") |
| 2024 |
end |
| 2025 |
if not callbacktypes[name] or |
| 2026 |
not description or |
| 2027 |
description == "" then |
| 2028 |
luatexbase_error( |
| 2029 |
"Unable to remove function from callback.\n\n" |
| 2030 |
.. "Correct usage:\n" |
| 2031 |
.. "remove_from_callback(<callback>, <description>)" |
| 2032 |
) |
| 2033 |
end |
| 2034 |
local l = realcallbacklist[name] |
| 2035 |
if not l then |
| 2036 |
luatexbase_error( |
| 2037 |
"No callback list for `" .. name .. "'\n") |
| 2038 |
end |
| 2039 |
% \end{macrocode} |
| 2040 |
% Loop over the callback's function list until we find a matching entry. |
| 2041 |
% Remove it and check if the list is empty: if so, unregister the |
| 2042 |
% callback handler. |
| 2043 |
% \begin{macrocode} |
| 2044 |
local index = false |
| 2045 |
for i,j in ipairs(l) do |
| 2046 |
if j.description == description then |
| 2047 |
index = i |
| 2048 |
break |
| 2049 |
end |
| 2050 |
end |
| 2051 |
if not index then |
| 2052 |
luatexbase_error( |
| 2053 |
"No callback `" .. description .. "' registered for `" .. |
| 2054 |
name .. "'\n") |
| 2055 |
end |
| 2056 |
local cb = l[index] |
| 2057 |
table.remove(l, index) |
| 2058 |
luatexbase_log( |
| 2059 |
"Removing `" .. description .. "' from `" .. name .. "'." |
| 2060 |
) |
| 2061 |
if #l == 0 then |
| 2062 |
realcallbacklist[name] = nil |
| 2063 |
callbacklist[name] = nil |
| 2064 |
local shared = shared_callbacks[name] |
| 2065 |
if shared then |
| 2066 |
shared.count = shared.count - 1 |
| 2067 |
if shared.count == 0 then |
| 2068 |
callback_register(shared.callback, nil) |
| 2069 |
end |
| 2070 |
elseif user_callbacks_defaults[name] == nil then |
| 2071 |
callback_register(name, nil) |
| 2072 |
end |
| 2073 |
end |
| 2074 |
return cb.func,cb.description |
| 2075 |
end |
| 2076 |
luatexbase.remove_from_callback = remove_from_callback |
| 2077 |
% \end{macrocode} |
| 2078 |
% \end{macro} |
| 2079 |
% |
| 2080 |
% \begin{macro}{in_callback} |
| 2081 |
% \changes{v1.0a}{2015/09/24}{Function added} |
| 2082 |
% \changes{v1.0h}{2015/11/27}{Guard against undefined list latex/4445} |
| 2083 |
% Look for a function description in a callback. |
| 2084 |
% \begin{macrocode} |
| 2085 |
local function in_callback(name, description) |
| 2086 |
if not name |
| 2087 |
or name == "" |
| 2088 |
or not realcallbacklist[name] |
| 2089 |
or not callbacktypes[name] |
| 2090 |
or not description then |
| 2091 |
return false |
| 2092 |
end |
| 2093 |
for _, i in pairs(realcallbacklist[name]) do |
| 2094 |
if i.description == description then |
| 2095 |
return true |
| 2096 |
end |
| 2097 |
end |
| 2098 |
return false |
| 2099 |
end |
| 2100 |
luatexbase.in_callback = in_callback |
| 2101 |
% \end{macrocode} |
| 2102 |
% \end{macro} |
| 2103 |
% |
| 2104 |
% \begin{macro}{disable_callback} |
| 2105 |
% \changes{v1.0a}{2015/09/24}{Function added} |
| 2106 |
% As we subvert the engine interface we need to provide a way to access |
| 2107 |
% this functionality. |
| 2108 |
% \begin{macrocode} |
| 2109 |
local function disable_callback(name) |
| 2110 |
if(realcallbacklist[name] == nil) then |
| 2111 |
callback_register(name, false) |
| 2112 |
else |
| 2113 |
luatexbase_error("Callback list for " .. name .. " not empty") |
| 2114 |
end |
| 2115 |
end |
| 2116 |
luatexbase.disable_callback = disable_callback |
| 2117 |
% \end{macrocode} |
| 2118 |
% \end{macro} |
| 2119 |
% |
| 2120 |
% \begin{macro}{callback_descriptions} |
| 2121 |
% \changes{v1.0a}{2015/09/24}{Function added} |
| 2122 |
% \changes{v1.0h}{2015/11/27}{Match test in in-callback latex/4445} |
| 2123 |
% List the descriptions of functions registered for the given callback. |
| 2124 |
% This will sort the list if necessary. |
| 2125 |
% \begin{macrocode} |
| 2126 |
local function callback_descriptions (name) |
| 2127 |
local d = {} |
| 2128 |
if not name |
| 2129 |
or name == "" |
| 2130 |
or not realcallbacklist[name] |
| 2131 |
or not callbacktypes[name] |
| 2132 |
then |
| 2133 |
return d |
| 2134 |
else |
| 2135 |
for k, i in pairs(callbacklist[name]) do |
| 2136 |
d[k]= i.description |
| 2137 |
end |
| 2138 |
end |
| 2139 |
return d |
| 2140 |
end |
| 2141 |
luatexbase.callback_descriptions =callback_descriptions |
| 2142 |
% \end{macrocode} |
| 2143 |
% \end{macro} |
| 2144 |
% |
| 2145 |
% \begin{macro}{uninstall} |
| 2146 |
% \changes{v1.0e}{2015/10/02}{Function added} |
| 2147 |
% Unlike at the \TeX{} level, we have to provide a back-out mechanism here |
| 2148 |
% at the same time as the rest of the code. This is not meant for use by |
| 2149 |
% anything other than \textsf{latexrelease}: as such this is |
| 2150 |
% \emph{deliberately} not documented for users! |
| 2151 |
% \begin{macrocode} |
| 2152 |
local function uninstall() |
| 2153 |
module_info( |
| 2154 |
"luatexbase", |
| 2155 |
"Uninstalling kernel luatexbase code" |
| 2156 |
) |
| 2157 |
callback.register = callback_register |
| 2158 |
luatexbase = nil |
| 2159 |
end |
| 2160 |
luatexbase.uninstall = uninstall |
| 2161 |
% \end{macrocode} |
| 2162 |
% \end{macro} |
| 2163 |
% \begin{macro}{mlist_to_hlist} |
| 2164 |
% \changes{v1.1l}{2020/02/02}{|pre/post_mlist_to_hlist| added} |
| 2165 |
% \changes{v1.1y}{2022/08/13}{Use shared\_callback system for pre/post/mlist_to_hlist} |
| 2166 |
% \changes{v1.2c}{2023/08/03}{Fix callback type of post_mlist_to_hlist_callback} |
| 2167 |
% To emulate these callbacks, the ``real'' |mlist_to_hlist| is replaced by a |
| 2168 |
% wrapper calling the wrappers before and after. |
| 2169 |
% \begin{macrocode} |
| 2170 |
create_callback('pre_mlist_to_hlist_filter', 'list') |
| 2171 |
create_callback('mlist_to_hlist', 'exclusive', node.mlist_to_hlist) |
| 2172 |
create_callback('post_mlist_to_hlist_filter', 'reverselist') |
| 2173 |
function shared_callbacks.mlist_to_hlist.handler(head, display_type, need_penalties) |
| 2174 |
local current = call_callback("pre_mlist_to_hlist_filter", head, display_type, need_penalties) |
| 2175 |
if current == false then |
| 2176 |
flush_list(head) |
| 2177 |
return nil |
| 2178 |
end |
| 2179 |
current = call_callback("mlist_to_hlist", current, display_type, need_penalties) |
| 2180 |
local post = call_callback("post_mlist_to_hlist_filter", current, display_type, need_penalties) |
| 2181 |
if post == false then |
| 2182 |
flush_list(current) |
| 2183 |
return nil |
| 2184 |
end |
| 2185 |
return post |
| 2186 |
end |
| 2187 |
% \end{macrocode} |
| 2188 |
% \end{macro} |
| 2189 |
% \endgroup |
| 2190 |
% |
| 2191 |
% \begin{macrocode} |
| 2192 |
%</lua> |
| 2193 |
% \end{macrocode} |
| 2194 |
% |
| 2195 |
% Reset the catcode of |@|. |
| 2196 |
% \begin{macrocode} |
| 2197 |
%<tex>\catcode`\@=\etatcatcode\relax |
| 2198 |
% \end{macrocode} |
| 2199 |
% |
| 2200 |
% |
| 2201 |
% \Finale |