| 1 |
% \iffalse meta-comment |
| 2 |
% |
| 3 |
%% File: l3luatex.dtx |
| 4 |
% |
| 5 |
% Copyright (C) 2010-2026 The LaTeX Project |
| 6 |
% |
| 7 |
% It may be distributed and/or modified under the conditions of the |
| 8 |
% LaTeX Project Public License (LPPL), either version 1.3c of this |
| 9 |
% license or (at your option) any later version. The latest version |
| 10 |
% of this license is in the file |
| 11 |
% |
| 12 |
% https://www.latex-project.org/lppl.txt |
| 13 |
% |
| 14 |
% This file is part of the "l3kernel bundle" (The Work in LPPL) |
| 15 |
% and all files in that bundle must be distributed together. |
| 16 |
% |
| 17 |
% ----------------------------------------------------------------------- |
| 18 |
% |
| 19 |
% The development version of the bundle can be found at |
| 20 |
% |
| 21 |
% https://github.com/latex3/latex3 |
| 22 |
% |
| 23 |
% for those people who are interested. |
| 24 |
% |
| 25 |
%<*driver> |
| 26 |
\documentclass[full,kernel]{l3doc} |
| 27 |
\begin{document} |
| 28 |
\DocInput{\jobname.dtx} |
| 29 |
\end{document} |
| 30 |
%</driver> |
| 31 |
% \fi |
| 32 |
% |
| 33 |
% \title{^^A |
| 34 |
% The \pkg{l3luatex} module\\ \LuaTeX-specific functions^^A |
| 35 |
% } |
| 36 |
% |
| 37 |
% \author{^^A |
| 38 |
% The \LaTeX{} Project\thanks |
| 39 |
% {^^A |
| 40 |
% E-mail: |
| 41 |
% \href{mailto:[email protected]} |
| 42 |
% {[email protected]}^^A |
| 43 |
% }^^A |
| 44 |
% } |
| 45 |
% |
| 46 |
% \date{Released 2026-05-15} |
| 47 |
% |
| 48 |
% \maketitle |
| 49 |
% |
| 50 |
% \begin{documentation} |
| 51 |
% |
| 52 |
% The \LuaTeX{} engine provides access to the \Lua{} programming language, |
| 53 |
% and with it access to the \enquote{internals} of \TeX{}. In order to use |
| 54 |
% this within the framework provided here, a family of functions is |
| 55 |
% available. When used with \pdfTeX{}, \pTeX{}, \upTeX{} or \XeTeX{} these raise an |
| 56 |
% error: use \cs{sys_if_engine_luatex:T} to avoid this. Details on using |
| 57 |
% \Lua{} with the \LuaTeX{} engine are given in the \LuaTeX{} manual. |
| 58 |
% |
| 59 |
% \section{Breaking out to \Lua{}} |
| 60 |
% |
| 61 |
% \begin{function}[EXP, added = 2018-06-18]{\lua_now:n, \lua_now:e} |
| 62 |
% \begin{syntax} |
| 63 |
% \cs{lua_now:n} \Arg{token list} |
| 64 |
% \end{syntax} |
| 65 |
% The \meta{token list} is first tokenized by \TeX{}, which includes |
| 66 |
% converting line ends to spaces in the usual \TeX{} manner and which |
| 67 |
% respects currently-applicable \TeX{} category codes. The resulting |
| 68 |
% \meta{\Lua{} input} is passed to the \Lua{} interpreter for processing. |
| 69 |
% Each \cs{lua_now:n} block is treated by \Lua{} as a separate chunk. |
| 70 |
% The \Lua{} interpreter executes the \meta{\Lua{} input} immediately, |
| 71 |
% and in an expandable manner. |
| 72 |
% \begin{texnote} |
| 73 |
% \cs{lua_now:e} is a macro wrapper around \tn{directlua}: when |
| 74 |
% \LuaTeX{} is in use two expansions are required to yield the |
| 75 |
% result of the \Lua{} code. |
| 76 |
% \end{texnote} |
| 77 |
% \end{function} |
| 78 |
% |
| 79 |
% \begin{function}[added = 2018-06-18]{\lua_shipout_e:n, \lua_shipout:n} |
| 80 |
% \begin{syntax} |
| 81 |
% \cs{lua_shipout:n} \Arg{token list} |
| 82 |
% \end{syntax} |
| 83 |
% The \meta{token list} is first tokenized by \TeX{}, which includes |
| 84 |
% converting line ends to spaces in the usual \TeX{} manner and which |
| 85 |
% respects currently-applicable \TeX{} category codes. The resulting |
| 86 |
% \meta{\Lua{} input} is passed to the \Lua{} interpreter when the |
| 87 |
% current page is finalized (i.e., at shipout). Each |
| 88 |
% \cs{lua_shipout:n} block is treated by \Lua{} as a separate chunk. |
| 89 |
% The \Lua{} interpreter will execute the \meta{\Lua{} input} during the |
| 90 |
% page-building routine: no \TeX{} expansion of the \meta{\Lua{} input} |
| 91 |
% will occur at this stage. |
| 92 |
% |
| 93 |
% In the case of the \cs{lua_shipout_e:n} version the input is fully |
| 94 |
% expanded by \TeX{} in an \texttt{e}-type manner during the shipout |
| 95 |
% operation. |
| 96 |
% \begin{texnote} |
| 97 |
% At a \TeX{} level, the \meta{\Lua{} input} is stored as a |
| 98 |
% \enquote{whatsit}. |
| 99 |
% \end{texnote} |
| 100 |
% \end{function} |
| 101 |
% |
| 102 |
% \begin{function}[EXP, added = 2015-06-29]{\lua_escape:n, \lua_escape:e} |
| 103 |
% \begin{syntax} |
| 104 |
% \cs{lua_escape:n} \Arg{token list} |
| 105 |
% \end{syntax} |
| 106 |
% Converts the \meta{token list} such that it can safely be passed to |
| 107 |
% \Lua{}: embedded backslashes, double and single quotes, and newlines |
| 108 |
% and carriage returns are escaped. This is done by prepending an extra |
| 109 |
% token consisting of a backslash with category code~$12$, and for the line |
| 110 |
% endings, converting them to |\n| and |\r|, respectively. |
| 111 |
% \begin{texnote} |
| 112 |
% \cs{lua_escape:e} is a macro wrapper around \tn{luaescapestring}: |
| 113 |
% when \LuaTeX{} is in use two expansions are required to yield the |
| 114 |
% result of the \Lua{} code. |
| 115 |
% \end{texnote} |
| 116 |
% \end{function} |
| 117 |
% |
| 118 |
% \begin{function}[added = 2022-05-14]{\lua_load_module:n} |
| 119 |
% \begin{syntax} |
| 120 |
% \cs{lua_load_module:n} \Arg{Lua module name} |
| 121 |
% \end{syntax} |
| 122 |
% Loads a Lua module into the Lua interpreter. |
| 123 |
% |
| 124 |
% \cs{lua_now:n} passes its \Arg{token list} argument to the Lua interpreter |
| 125 |
% as a single line, with characters interpreted under the current catcode |
| 126 |
% régime. These two facts mean that \cs{lua_now:n} rarely behaves as expected |
| 127 |
% for larger pieces of code. Therefore, package authors should \textbf{not} |
| 128 |
% write significant amounts of Lua code in the arguments to \cs{lua_now:n}. |
| 129 |
% Instead, it is strongly recommended that they write the majorty of their Lua |
| 130 |
% code in a separate file, and then load it using \cs{lua_load_module:n}. |
| 131 |
% \begin{texnote} |
| 132 |
% This is a wrapper around the Lua call |require '|\meta{module}|'|. |
| 133 |
% \end{texnote} |
| 134 |
% \end{function} |
| 135 |
% |
| 136 |
% \section{Lua interfaces} |
| 137 |
% |
| 138 |
% As well as interfaces for \TeX{}, there are a small number of Lua functions |
| 139 |
% provided here. |
| 140 |
% |
| 141 |
% \begin{function}{ltx.utils} |
| 142 |
% Most public interfaces provided by the module are stored within the |
| 143 |
% |ltx.utils| table. |
| 144 |
% \end{function} |
| 145 |
% |
| 146 |
% \begin{function}{ltx.utils.filedump} |
| 147 |
% \begin{syntax} |
| 148 |
% \meta{dump}| = ltx.utils.filedump(|\meta{file}|,|\meta{offset}|,|\meta{length}|)| \\ |
| 149 |
% \end{syntax} |
| 150 |
% Returns the uppercase hexadecimal representation of the content of the |
| 151 |
% \meta{file} read as bytes. If the \meta{length} is given, only this part |
| 152 |
% of the file is returned; similarly, one may specify the \meta{offset} from |
| 153 |
% the start of the file. If the \meta{length} is not given, the entire file |
| 154 |
% is read starting at the \meta{offset}. |
| 155 |
% \end{function} |
| 156 |
% |
| 157 |
% \begin{function}{ltx.utils.filemd5sum} |
| 158 |
% \begin{syntax} |
| 159 |
% \meta{hash}| = ltx.utils.filemd5sum(|\meta{file}|)| \\ |
| 160 |
% \end{syntax} |
| 161 |
% Returns the MD5 sum of the file contents read as bytes; note that |
| 162 |
% the result will depend on the nature of the line endings used in the file, |
| 163 |
% in contrast to normal \TeX{} behavior. If the \meta{file} is not found, |
| 164 |
% nothing is returned with \emph{no error raised}. |
| 165 |
% \end{function} |
| 166 |
% |
| 167 |
% \begin{function}{ltx.utils.filemoddate} |
| 168 |
% \begin{syntax} |
| 169 |
% \meta{date}| = ltx.utils.filemoddate(|\meta{file}|)| \\ |
| 170 |
% \end{syntax} |
| 171 |
% Returns the date/time of last modification of the \meta{file} in the |
| 172 |
% format |
| 173 |
% \begin{quote} |
| 174 |
% |D:|\meta{year}\meta{month}\meta{day}\meta{hour}\meta{minute}^^A |
| 175 |
% \meta{second}\meta{offset} |
| 176 |
% \end{quote} |
| 177 |
% where the latter may be |Z| (UTC) or |
| 178 |
% \meta{plus-minus}\meta{hours}|'|\meta{minutes}|'|. If the \meta{file} is |
| 179 |
% not found, nothing is returned with \emph{no error raised}. |
| 180 |
% \end{function} |
| 181 |
% |
| 182 |
% \begin{function}{ltx.utils.filesize} |
| 183 |
% \begin{syntax} |
| 184 |
% |size = ltx.utils.filesize(|\meta{file}|)| \\ |
| 185 |
% \end{syntax} |
| 186 |
% Returns the size of the \meta{file} in bytes. If the \meta{file} is not |
| 187 |
% found, nothing is returned with \emph{no error raised}. |
| 188 |
% \end{function} |
| 189 |
% |
| 190 |
% \end{documentation} |
| 191 |
% |
| 192 |
% \begin{implementation} |
| 193 |
% |
| 194 |
% \section{\pkg{l3luatex} implementation} |
| 195 |
% |
| 196 |
% \begin{macrocode} |
| 197 |
%<@@=lua> |
| 198 |
% \end{macrocode} |
| 199 |
% |
| 200 |
% \subsection{Breaking out to \Lua{}} |
| 201 |
% |
| 202 |
% \begin{macrocode} |
| 203 |
%<*code> |
| 204 |
% \end{macrocode} |
| 205 |
% |
| 206 |
% \begin{macro}[EXP]{\@@_escape:n, \@@_now:n, \@@_shipout:n} |
| 207 |
% Copies of primitives. |
| 208 |
% \begin{macrocode} |
| 209 |
\cs_new_eq:NN \@@_escape:n \tex_luaescapestring:D |
| 210 |
\cs_new_eq:NN \@@_now:n \tex_directlua:D |
| 211 |
\cs_new_eq:NN \@@_shipout:n \tex_latelua:D |
| 212 |
% \end{macrocode} |
| 213 |
% \end{macro} |
| 214 |
% |
| 215 |
% These functions are set up in \pkg{l3str} for bootstrapping: we want to |
| 216 |
% replace them with a \enquote{proper} version at this stage, so clean up. |
| 217 |
% \begin{macrocode} |
| 218 |
\cs_undefine:N \lua_escape:e |
| 219 |
\cs_undefine:N \lua_now:e |
| 220 |
% \end{macrocode} |
| 221 |
% |
| 222 |
% \begin{macro}[EXP]{\lua_now:n, \lua_now:e} |
| 223 |
% \begin{macro}{\lua_shipout_e:n, \lua_shipout:n} |
| 224 |
% \begin{macro}[EXP]{\lua_escape:n, \lua_escape:e} |
| 225 |
% Wrappers around the primitives. |
| 226 |
% \begin{macrocode} |
| 227 |
\cs_new:Npn \lua_now:e #1 { \@@_now:n {#1} } |
| 228 |
\cs_new:Npn \lua_now:n #1 { \lua_now:e { \exp_not:n {#1} } } |
| 229 |
\cs_new_protected:Npn \lua_shipout_e:n #1 { \@@_shipout:n {#1} } |
| 230 |
\cs_new_protected:Npn \lua_shipout:n #1 |
| 231 |
{ \lua_shipout_e:n { \exp_not:n {#1} } } |
| 232 |
\cs_new:Npn \lua_escape:e #1 { \@@_escape:n {#1} } |
| 233 |
\cs_new:Npn \lua_escape:n #1 { \lua_escape:e { \exp_not:n {#1} } } |
| 234 |
% \end{macrocode} |
| 235 |
% \end{macro} |
| 236 |
% \end{macro} |
| 237 |
% \end{macro} |
| 238 |
% |
| 239 |
% \begin{macro}{\lua_load_module:n} |
| 240 |
% Wrapper around |require'|\meta{module}|'|. |
| 241 |
% \begin{macrocode} |
| 242 |
\str_new:N \l_@@_err_msg_str |
| 243 |
\cs_new_protected:Npn \lua_load_module:n #1 |
| 244 |
{ |
| 245 |
\bool_if:nF { \@@_load_module_p:n { #1 } } |
| 246 |
{ |
| 247 |
\msg_error:nnnV |
| 248 |
{ luatex } { module-not-found } { #1 } \l_@@_err_msg_str |
| 249 |
} |
| 250 |
} |
| 251 |
% \end{macrocode} |
| 252 |
% \end{macro} |
| 253 |
% |
| 254 |
% As with engines other than \LuaTeX{} |
| 255 |
% these have to be macros, we give them the same status in all cases. |
| 256 |
% When \LuaTeX{} is not in use, simply give an error message/ |
| 257 |
% \begin{macrocode} |
| 258 |
\sys_if_engine_luatex:F |
| 259 |
{ |
| 260 |
\clist_map_inline:nn |
| 261 |
{ |
| 262 |
\lua_escape:n , \lua_escape:e , |
| 263 |
\lua_now:n , \lua_now:e |
| 264 |
} |
| 265 |
{ |
| 266 |
\cs_gset:Npn #1 ##1 |
| 267 |
{ |
| 268 |
\msg_expandable_error:nnn |
| 269 |
{ luatex } { luatex-required } { #1 } |
| 270 |
} |
| 271 |
} |
| 272 |
\clist_map_inline:nn |
| 273 |
{ \lua_shipout_e:n , \lua_shipout:n, \lua_load_module:n } |
| 274 |
{ |
| 275 |
\cs_gset_protected:Npn #1 ##1 |
| 276 |
{ |
| 277 |
\msg_error:nnn |
| 278 |
{ luatex } { luatex-required } { #1 } |
| 279 |
} |
| 280 |
} |
| 281 |
} |
| 282 |
% \end{macrocode} |
| 283 |
% \subsection{Messages} |
| 284 |
% |
| 285 |
% \begin{macrocode} |
| 286 |
\msg_new:nnnn { luatex } { luatex-required } |
| 287 |
{ LuaTeX~engine~not~in~use!~Ignoring~#1. } |
| 288 |
{ |
| 289 |
The~feature~you~are~using~is~only~available~ |
| 290 |
with~the~LuaTeX~engine.~LaTeX3~ignored~'#1'. |
| 291 |
} |
| 292 |
\msg_new:nnnn { luatex } { module-not-found } |
| 293 |
{ Lua~module~`#1'~not~found. } |
| 294 |
{ |
| 295 |
The~file~`#1.lua'~could~not~be~found.~Please~ensure~ |
| 296 |
that~the~file~was~properly~installed~and~that~the~ |
| 297 |
filename~database~is~current. \\ \\ |
| 298 |
The~Lua~loader~provided~this~additional~information: \\ |
| 299 |
#2 |
| 300 |
} |
| 301 |
\prop_gput:Nnn \g_msg_module_name_prop { luatex } { LaTeX } |
| 302 |
\prop_gput:Nnn \g_msg_module_type_prop { luatex } { } |
| 303 |
% \end{macrocode} |
| 304 |
% |
| 305 |
% \begin{macrocode} |
| 306 |
%</code> |
| 307 |
% \end{macrocode} |
| 308 |
% |
| 309 |
% \subsection{\Lua{} functions for internal use} |
| 310 |
% |
| 311 |
% \begin{macrocode} |
| 312 |
%<*lua> |
| 313 |
% \end{macrocode} |
| 314 |
% |
| 315 |
% Most of the emulation of \pdfTeX{} here is based heavily on Heiko Oberdiek's |
| 316 |
% \pkg{pdftexcmds} package. |
| 317 |
% |
| 318 |
% \begin{macro}{ltx.utils} |
| 319 |
% Create a table for the kernel's own use. |
| 320 |
% \begin{macrocode} |
| 321 |
ltx = ltx or {utils={}} |
| 322 |
ltx.utils = ltx.utils or { } |
| 323 |
local ltxutils = ltx.utils |
| 324 |
% \end{macrocode} |
| 325 |
% \end{macro} |
| 326 |
% |
| 327 |
% Local copies of global tables. |
| 328 |
% \begin{macrocode} |
| 329 |
local io = io |
| 330 |
local kpse = kpse |
| 331 |
local lfs = lfs |
| 332 |
local math = math |
| 333 |
local md5 = md5 |
| 334 |
local os = os |
| 335 |
local string = string |
| 336 |
local tex = tex |
| 337 |
local texio = texio |
| 338 |
local tonumber = tonumber |
| 339 |
local token = token |
| 340 |
% \end{macrocode} |
| 341 |
% |
| 342 |
% Local copies of standard functions. |
| 343 |
% \begin{macrocode} |
| 344 |
local abs = math.abs |
| 345 |
local byte = string.byte |
| 346 |
local floor = math.floor |
| 347 |
local format = string.format |
| 348 |
local gsub = string.gsub |
| 349 |
local lfs_attr = lfs.attributes |
| 350 |
local open = io.open |
| 351 |
local os_date = os.date |
| 352 |
local setcatcode = tex.setcatcode |
| 353 |
local sprint = tex.sprint |
| 354 |
local cprint = tex.cprint |
| 355 |
local write = tex.write |
| 356 |
local write_nl = texio.write_nl |
| 357 |
local utf8_char = utf8.char |
| 358 |
local package_loaded = package.loaded |
| 359 |
local package_searchers = package.searchers |
| 360 |
local table_concat = table.concat |
| 361 |
|
| 362 |
local scan_csname = token.scancsname or token.scan_csname |
| 363 |
local scan_int = token.scan_int or token.scan_integer |
| 364 |
local scan_string = token.scanstring or token.scan_string |
| 365 |
local scan_keyword = token.scankeyword or token.scan_keyword |
| 366 |
local scan_argument = token.scanargument or token.scan_argument |
| 367 |
local get_next = token.scannext or token.get_next |
| 368 |
local put_next = token.putnext or token.put_next |
| 369 |
local token_create = token.create |
| 370 |
local get_macro = token.getmacro or token.get_macro |
| 371 |
local get_protected = token.getprotected or token.get_protected |
| 372 |
local get_csname = token.getcsname or token.get_csname |
| 373 |
local token_new = token.new |
| 374 |
local set_macro = token.setmacro or token.set_macro |
| 375 |
|
| 376 |
local active_prefix = status.luatex_engine == 'luametatex' and status.getconstants().active_character_namespace or utf8.char(0xFFFF) |
| 377 |
|
| 378 |
local lbrace, rbrace = token_create(byte'{'), token_create(byte'}') |
| 379 |
% \end{macrocode} |
| 380 |
% |
| 381 |
% Since token.create only returns useful values after the tokens |
| 382 |
% has been added to TeX's hash table, we define a variant which |
| 383 |
% defines it first if necessary. |
| 384 |
% \begin{macrocode} |
| 385 |
local token_create_safe |
| 386 |
do |
| 387 |
local is_defined = token.is_defined |
| 388 |
local set_char = token.set_char or tex.chardef |
| 389 |
local runtoks = tex.runtoks |
| 390 |
local let_token = token_create'let' |
| 391 |
|
| 392 |
function token_create_safe(s) |
| 393 |
local orig_token = token_create(s) |
| 394 |
if is_defined(s, true) then |
| 395 |
return orig_token |
| 396 |
end |
| 397 |
set_char(s, 0) |
| 398 |
local new_token = token_create(s) |
| 399 |
runtoks(function() |
| 400 |
put_next(let_token, new_token, orig_token) |
| 401 |
end) |
| 402 |
return new_token |
| 403 |
end |
| 404 |
end |
| 405 |
|
| 406 |
local true_tok = token_create_safe'prg_return_true:' |
| 407 |
local false_tok = token_create_safe'prg_return_false:' |
| 408 |
% \end{macrocode} |
| 409 |
% In Con\TeX{}t lmtx \texttt{token.command_id} does not exist, |
| 410 |
% but it can easily be emulated with Con\TeX{}t's \texttt{tokens.commands}. |
| 411 |
% \begin{macrocode} |
| 412 |
local command_id = token.command_id |
| 413 |
if not command_id and tokens and tokens.commands then |
| 414 |
local id_map = tokens.commands |
| 415 |
function command_id(name) |
| 416 |
return id_map[name] |
| 417 |
end |
| 418 |
end |
| 419 |
% \end{macrocode} |
| 420 |
% |
| 421 |
% Deal with Con\TeX{}t: doesn't use |kpse| library. |
| 422 |
% \begin{macrocode} |
| 423 |
local kpse_find = (resolvers and resolvers.findfile) or kpse.find_file |
| 424 |
% \end{macrocode} |
| 425 |
% |
| 426 |
% \begin{macro}[int]{escapehex} |
| 427 |
% An internal auxiliary to convert a string to the matching hex escape. |
| 428 |
% This works on a byte basis: extension to handled UTF-8 input is |
| 429 |
% covered in \pkg{pdftexcmds} but is not currently required here. |
| 430 |
% \begin{macrocode} |
| 431 |
local function escapehex(str) |
| 432 |
return (gsub(str, ".", |
| 433 |
function (ch) return format("%02X", byte(ch)) end)) |
| 434 |
end |
| 435 |
% \end{macrocode} |
| 436 |
% \end{macro} |
| 437 |
% |
| 438 |
% \begin{macro}{ltx.utils.filedump} |
| 439 |
% Similar comments here to the next function: read the file in binary mode |
| 440 |
% to avoid any line-end weirdness. |
| 441 |
% \begin{macrocode} |
| 442 |
local function filedump(name,offset,length) |
| 443 |
local file = kpse_find(name,"tex",true) |
| 444 |
if not file then return end |
| 445 |
local f = open(file,"rb") |
| 446 |
if not f then return end |
| 447 |
if offset and offset > 0 then |
| 448 |
f:seek("set", offset) |
| 449 |
end |
| 450 |
local data = f:read(length or 'a') |
| 451 |
f:close() |
| 452 |
return escapehex(data) |
| 453 |
end |
| 454 |
ltxutils.filedump = filedump |
| 455 |
% \end{macrocode} |
| 456 |
% \end{macro} |
| 457 |
% |
| 458 |
% \begin{macro}[int]{md5.HEX} |
| 459 |
% Hash a string and return the hash in uppercase hexadecimal format. |
| 460 |
% In some engines, this is built-in. For traditional \LuaTeX{}, the conversion |
| 461 |
% to hexadecimal has to be done by us. |
| 462 |
% \begin{macrocode} |
| 463 |
local md5_HEX = md5.HEX |
| 464 |
if not md5_HEX then |
| 465 |
local md5_sum = md5.sum |
| 466 |
function md5_HEX(data) |
| 467 |
return escapehex(md5_sum(data)) |
| 468 |
end |
| 469 |
md5.HEX = md5_HEX |
| 470 |
end |
| 471 |
% \end{macrocode} |
| 472 |
% \end{macro} |
| 473 |
% \begin{macro}{ltx.utils.filemd5sum} |
| 474 |
% Read an entire file and hash it: the hash function itself is a built-in. |
| 475 |
% As Lua is byte-based there is no work needed here in terms of UTF-8 |
| 476 |
% (see \pkg{pdftexcmds} and how it handles strings that have passed through |
| 477 |
% \LuaTeX{}). The file is read in binary mode so that no line ending |
| 478 |
% normalization occurs. |
| 479 |
% \begin{macrocode} |
| 480 |
local function filemd5sum(name) |
| 481 |
local file = kpse_find(name, "tex", true) if not file then return end |
| 482 |
local f = open(file, "rb") if not f then return end |
| 483 |
|
| 484 |
local data = f:read("*a") |
| 485 |
f:close() |
| 486 |
return md5_HEX(data) |
| 487 |
end |
| 488 |
ltxutils.filemd5sum = filemd5sum |
| 489 |
% \end{macrocode} |
| 490 |
% \end{macro} |
| 491 |
% |
| 492 |
% \begin{macro}{ltx.utils.filemoddate} |
| 493 |
% There are two cases: If the C standard library is C99 compliant, |
| 494 |
% we can use |%z| to get the timezone in almost the right format. |
| 495 |
% We only have to add primes and replace a zero or missing offset |
| 496 |
% with |Z|. |
| 497 |
% |
| 498 |
% Of course this would be boring, so Windows does things differently. |
| 499 |
% There we have to manually calculate the offset. |
| 500 |
% See procedure \texttt{makepdftime} in \texttt{utils.c} of |
| 501 |
% \pdfTeX{}. |
| 502 |
% \begin{macrocode} |
| 503 |
local filemoddate |
| 504 |
if os_date'%z':match'^[+-]%d%d%d%d$' then |
| 505 |
local pattern = lpeg.Cs(16 * |
| 506 |
(lpeg.Cg(lpeg.S'+-' * '0000' * lpeg.Cc'Z') |
| 507 |
+ 3 * lpeg.Cc"'" * 2 * lpeg.Cc"'" |
| 508 |
+ lpeg.Cc'Z') |
| 509 |
* -1) |
| 510 |
function filemoddate(name) |
| 511 |
local file = kpse_find(name, "tex", true) |
| 512 |
if not file then return end |
| 513 |
local date = lfs_attr(file, "modification") |
| 514 |
if not date then return end |
| 515 |
return pattern:match(os_date("D:%Y%m%d%H%M%S%z", date)) |
| 516 |
end |
| 517 |
else |
| 518 |
local function filemoddate(name) |
| 519 |
local file = kpse_find(name, "tex", true) |
| 520 |
if not file then return end |
| 521 |
local date = lfs_attr(file, "modification") |
| 522 |
if not date then return end |
| 523 |
local d = os_date("*t", date) |
| 524 |
local u = os_date("!*t", date) |
| 525 |
local off = 60 * (d.hour - u.hour) + d.min - u.min |
| 526 |
if d.year ~= u.year then |
| 527 |
if d.year > u.year then |
| 528 |
off = off + 1440 |
| 529 |
else |
| 530 |
off = off - 1440 |
| 531 |
end |
| 532 |
elseif d.yday ~= u.yday then |
| 533 |
if d.yday > u.yday then |
| 534 |
off = off + 1440 |
| 535 |
else |
| 536 |
off = off - 1440 |
| 537 |
end |
| 538 |
end |
| 539 |
local timezone |
| 540 |
if off == 0 then |
| 541 |
timezone = "Z" |
| 542 |
else |
| 543 |
if off < 0 then |
| 544 |
timezone = "-" |
| 545 |
off = -off |
| 546 |
else |
| 547 |
timezone = "+" |
| 548 |
end |
| 549 |
timezone = format("%s%02d'%02d'", timezone, hours // 60, hours % 60) |
| 550 |
end |
| 551 |
return format("D:%04d%02d%02d%02d%02d%02d%s", |
| 552 |
d.year, d.month, d.day, d.hour, d.min, d.sec, timezone) |
| 553 |
end |
| 554 |
end |
| 555 |
ltxutils.filemoddate = filemoddate |
| 556 |
% \end{macrocode} |
| 557 |
% \end{macro} |
| 558 |
% |
| 559 |
% \begin{macro}{ltx.utils.filesize} |
| 560 |
% A simple disk lookup. |
| 561 |
% \begin{macrocode} |
| 562 |
local function filesize(name) |
| 563 |
local file = kpse_find(name, "tex", true) |
| 564 |
if file then |
| 565 |
local size = lfs_attr(file, "size") |
| 566 |
if size then |
| 567 |
return size |
| 568 |
end |
| 569 |
end |
| 570 |
end |
| 571 |
ltxutils.filesize = filesize |
| 572 |
% \end{macrocode} |
| 573 |
% \end{macro} |
| 574 |
% |
| 575 |
% \begin{macro}[int]{luacmd} |
| 576 |
% An internal function for defining control sequences form Lua which behave |
| 577 |
% like primitives. This acts as a wrapper around |token.set_lua| which accepts |
| 578 |
% a function instead of an index into the functions table. |
| 579 |
% \begin{macrocode} |
| 580 |
local luacmd do |
| 581 |
local set_lua = token.setlua or token.set_lua |
| 582 |
local undefined_cs = command_id'undefined_cs' |
| 583 |
|
| 584 |
if not context and not luatexbase then require'ltluatex' end |
| 585 |
if luatexbase then |
| 586 |
local new_luafunction = luatexbase.new_luafunction |
| 587 |
local functions = lua.get_functions_table() |
| 588 |
function luacmd(name, func, ...) |
| 589 |
local id |
| 590 |
local tok = token_create(name) |
| 591 |
if tok.command == undefined_cs then |
| 592 |
id = new_luafunction(name) |
| 593 |
set_lua(name, id, ...) |
| 594 |
else |
| 595 |
id = tok.index or tok.mode |
| 596 |
end |
| 597 |
functions[id] = func |
| 598 |
end |
| 599 |
elseif context then |
| 600 |
local register = context.functions.register |
| 601 |
local functions = context.functions.known |
| 602 |
function luacmd(name, func, ...) |
| 603 |
local tok = token_create(name) |
| 604 |
if tok.command == undefined_cs then |
| 605 |
set_lua(name, register(func), ...) |
| 606 |
else |
| 607 |
functions[tok.index or tok.mode] = func |
| 608 |
end |
| 609 |
end |
| 610 |
end |
| 611 |
end |
| 612 |
% \end{macrocode} |
| 613 |
% \end{macro} |
| 614 |
% |
| 615 |
% \begin{macro}[int]{try_require} |
| 616 |
% Loads a Lua module. This function loads the module similarly to the standard |
| 617 |
% Lua global function |require|, with a few differences. On success, |
| 618 |
% |try_require| returns |true, module|. If the module cannot be found, it |
| 619 |
% returns |false, err_msg|. If the module is found, but something goes wrong |
| 620 |
% when loading it, the function throws an error. |
| 621 |
% \begin{macrocode} |
| 622 |
local function try_require(name) |
| 623 |
if package_loaded[name] then |
| 624 |
return true, package_loaded[name] |
| 625 |
end |
| 626 |
|
| 627 |
local failure_details = {} |
| 628 |
for _, searcher in ipairs(package_searchers) do |
| 629 |
local loader, data = searcher(name) |
| 630 |
if type(loader) == 'function' then |
| 631 |
package_loaded[name] = loader(name, data) or true |
| 632 |
return true, package_loaded[name] |
| 633 |
elseif type(loader) == 'string' then |
| 634 |
failure_details[#failure_details + 1] = loader |
| 635 |
end |
| 636 |
end |
| 637 |
|
| 638 |
return false, table_concat(failure_details, '\n') |
| 639 |
end |
| 640 |
% \end{macrocode} |
| 641 |
% \end{macro} |
| 642 |
% |
| 643 |
% \begin{macro}{\@@_load_module_p:n} |
| 644 |
% Check to see if we can load a module using |require|. If we |
| 645 |
% can load the module, then we load it immediately. Otherwise, we save the |
| 646 |
% error message in |\l_@@_err_msg_str|. |
| 647 |
% \begin{macrocode} |
| 648 |
local char_given = command_id'char_given' |
| 649 |
local c_true_bool = token_create(1, char_given) |
| 650 |
local c_false_bool = token_create(0, char_given) |
| 651 |
local c_str_cctab = token_create('c_str_cctab').mode |
| 652 |
|
| 653 |
luacmd('@@_load_module_p:n', function() |
| 654 |
local success, result = try_require(scan_string()) |
| 655 |
if success then |
| 656 |
set_macro(c_str_cctab, 'l_@@_err_msg_str', '') |
| 657 |
put_next(c_true_bool) |
| 658 |
else |
| 659 |
set_macro(c_str_cctab, 'l_@@_err_msg_str', result) |
| 660 |
put_next(c_false_bool) |
| 661 |
end |
| 662 |
end) |
| 663 |
% \end{macrocode} |
| 664 |
% \end{macro} |
| 665 |
% |
| 666 |
% \subsection{Preserving iniTeX Lua data for runs} |
| 667 |
% |
| 668 |
% The Lua state is not dumped when a format is written, therefore any Lua |
| 669 |
% variables filled doing format building need to be restored in order to |
| 670 |
% be accessible during normal runs. |
| 671 |
% |
| 672 |
% We provide some kernel-internal helpers for this. They will only be available if |
| 673 |
% \texttt{luatexbase} is available. This is not a big restriction though, because |
| 674 |
% Con\TeX{}t (which does not use \texttt{luatexbase}) does not load \pkg{expl3} |
| 675 |
% in the format. |
| 676 |
% |
| 677 |
% \begin{macrocode} |
| 678 |
local register_luadata, get_luadata |
| 679 |
|
| 680 |
if luatexbase then |
| 681 |
local register = token_create'@expl@luadata@bytecode'.index |
| 682 |
% \end{macrocode} |
| 683 |
% |
| 684 |
% \begin{macro}[int]{register_luadata} |
| 685 |
% \texttt{register_luadata} is only available during format generation. |
| 686 |
% It accept a string which uniquely identifies the data object and has to be |
| 687 |
% provided to retrieve it later. Additionally it accepts a function which is |
| 688 |
% called in the \texttt{pre_dump} callback and which has to return a string that |
| 689 |
% evaluates to a valid Lua object to be preserved. Note that format generation |
| 690 |
% does not necessarily mean the first run, because some packages such as |
| 691 |
% \pkg{mylatexformat} generates a format based on an existing format. |
| 692 |
% \begin{macrocode} |
| 693 |
if status.ini_version then |
| 694 |
local luadata, luadata_order = {}, {} |
| 695 |
|
| 696 |
function register_luadata(name, func) |
| 697 |
if luadata[name] then |
| 698 |
error(format("LaTeX error: data name %q already in use", name)) |
| 699 |
end |
| 700 |
luadata[name] = func |
| 701 |
luadata_order[#luadata_order + 1] = func and name |
| 702 |
end |
| 703 |
% \end{macrocode} |
| 704 |
% \end{macro} |
| 705 |
% |
| 706 |
% The actual work is done in \texttt{pre_dump}. The \texttt{luadata_order} is used |
| 707 |
% to ensure that the order is consistent over multiple runs. |
| 708 |
% \begin{macrocode} |
| 709 |
luatexbase.add_to_callback("pre_dump", function() |
| 710 |
if next(luadata) then |
| 711 |
local str = "return {" |
| 712 |
for i=1, #luadata_order do |
| 713 |
local name = luadata_order[i] |
| 714 |
str = format('%s[%q]=%s,', str, name, luadata[name]()) |
| 715 |
end |
| 716 |
lua.bytecode[register] = assert(load(str .. "}")) |
| 717 |
end |
| 718 |
end, "ltx.luadata") |
| 719 |
end |
| 720 |
% \end{macrocode} |
| 721 |
% |
| 722 |
% \begin{macro}[int]{get_luadata} |
| 723 |
% \texttt{get_luadata} is only available if data should be restored. |
| 724 |
% It accept the identifier which was used when the data object was registered and |
| 725 |
% returns the associated object. Every object can only be retrieved once. |
| 726 |
% Note that it is possible for both \texttt{register_luadata} and \texttt{get_luadata} |
| 727 |
% to be available, for example while compiling a precompiled preamble. |
| 728 |
% In such a case, data must be registered again to be kept in the next run. |
| 729 |
% \begin{macrocode} |
| 730 |
local luadata = lua.bytecode[register] |
| 731 |
if luadata then |
| 732 |
lua.bytecode[register] = nil |
| 733 |
luadata = luadata() |
| 734 |
function get_luadata(name) |
| 735 |
if not luadata then return end |
| 736 |
local data = luadata[name] |
| 737 |
luadata[name] = nil |
| 738 |
return data |
| 739 |
end |
| 740 |
end |
| 741 |
end |
| 742 |
% \end{macrocode} |
| 743 |
% \end{macro} |
| 744 |
% |
| 745 |
% \subsection{Parsing Unicode data files} |
| 746 |
% |
| 747 |
% \begin{macro}[int]{\__kernel_codepoint_data:wn} |
| 748 |
% \begin{macro}[int]{\__codepoint_nfd:w} |
| 749 |
% This provides a macro to access a Unicode data entry. The first argument is a |
| 750 |
% unbraced number, the second a string identifying the kind of data to read. |
| 751 |
% The supported kinds match the data tables supported by \cs\__kernel_codepoint_data:nn |
| 752 |
% in other engines. |
| 753 |
% \begin{macrocode} |
| 754 |
do |
| 755 |
if get_luadata then |
| 756 |
local saved_unidata = get_luadata'lua-uni-data' |
| 757 |
if saved_unidata then saved_unidata() end |
| 758 |
end |
| 759 |
if register_luadata then |
| 760 |
register_luadata('lua-uni-data', function() |
| 761 |
return string.format("load(%q, nil, 'b')", string.dump(require'lua-uni-data-preload'.generate_bytecode(), true)) |
| 762 |
end) |
| 763 |
end |
| 764 |
|
| 765 |
local uni_data = require'lua-uni-data' |
| 766 |
local tables, decomposition_mapping = uni_data.tables, uni_data.misc.decomposition_mapping |
| 767 |
|
| 768 |
luacmd('__kernel_codepoint_data:wn', function() |
| 769 |
local cp = scan_int() |
| 770 |
local table = scan_argument() |
| 771 |
return sprint(-2, tostring(tables[table][cp])) |
| 772 |
end) |
| 773 |
|
| 774 |
luacmd('__codepoint_nfd:w', function() |
| 775 |
local cp = scan_int() |
| 776 |
local decomposed = decomposition_mapping[cp] |
| 777 |
if decomposed then |
| 778 |
for i=1,2 do |
| 779 |
if decomposed[i] then |
| 780 |
sprint(-2, lbrace, tostring(decomposed[i]), rbrace) |
| 781 |
else |
| 782 |
sprint(-2, lbrace, rbrace) |
| 783 |
end |
| 784 |
end |
| 785 |
else |
| 786 |
return sprint(-2, lbrace, tostring(cp), rbrace, lbrace, rbrace) |
| 787 |
end |
| 788 |
end) |
| 789 |
end |
| 790 |
% \end{macrocode} |
| 791 |
% \end{macro} |
| 792 |
% \end{macro} |
| 793 |
% |
| 794 |
% \begin{macrocode} |
| 795 |
%</lua> |
| 796 |
% \end{macrocode} |
| 797 |
% |
| 798 |
%\end{implementation} |
| 799 |
% |
| 800 |
%\PrintIndex |