[latex3-commits] [git/LaTeX3-latex3-latex3] luacmd: Emulate Ucharcat primitive in LuaTeX (834e4b2ec)
Marcel Fabian Krüger
tex at 2krueger.de
Fri Aug 21 14:50:17 CEST 2020
Repository : https://github.com/latex3/latex3
On branch : luacmd
Link : https://github.com/latex3/latex3/commit/834e4b2ec0afe6893c02619dfea4120cab232a71
>---------------------------------------------------------------
commit 834e4b2ec0afe6893c02619dfea4120cab232a71
Author: Marcel Fabian Krüger <tex at 2krueger.de>
Date: Tue Aug 18 00:13:52 2020 +0200
Emulate Ucharcat primitive in LuaTeX
>---------------------------------------------------------------
834e4b2ec0afe6893c02619dfea4120cab232a71
l3kernel/l3.ins | 7 ++++--
l3kernel/l3luatex.dtx | 59 +++++++++++++++++++++++++++++++++++++++++---------
l3kernel/l3names.dtx | 30 +++++++++++++++++++++++++
l3kernel/l3token.dtx | 51 +++++++++++++++++--------------------------
l3kernel/l3unicode.dtx | 19 +++-------------
5 files changed, 107 insertions(+), 59 deletions(-)
diff --git a/l3kernel/l3.ins b/l3kernel/l3.ins
index 5e7d1bfff..4498c5b21 100644
--- a/l3kernel/l3.ins
+++ b/l3kernel/l3.ins
@@ -60,7 +60,7 @@ and all files in that bundle must be distributed together.
{
\from{expl3.dtx} {package}
\from{l3bootstrap.dtx} {package}
- \from{l3names.dtx} {package}
+ \from{l3names.dtx} {package,tex}
\from{l3basics.dtx} {package}
\from{l3expan.dtx} {package}
\from{l3quark.dtx} {package}
@@ -165,6 +165,9 @@ and all files in that bundle must be distributed together.
\endpreamble
\nopostamble
-\generate{\file{expl3.lua}{\from{l3luatex.dtx}{package,lua}}}
+\generate{\file{expl3.lua}{
+ \from{l3luatex.dtx}{package,lua}
+ \from{l3names.dtx}{package,lua}
+}}
\endbatchfile
diff --git a/l3kernel/l3luatex.dtx b/l3kernel/l3luatex.dtx
index 01686ced0..4e87ca2bc 100644
--- a/l3kernel/l3luatex.dtx
+++ b/l3kernel/l3luatex.dtx
@@ -316,6 +316,9 @@
% Create a table for the kernel's own use.
% \begin{macrocode}
l3kernel = l3kernel or { }
+local l3kernel = l3kernel
+ltxutil = ltxutil or { }
+local l3kernel = l3kernel
% \end{macrocode}
% \end{macro}
%
@@ -352,12 +355,8 @@ local sprint = tex.sprint
local cprint = tex.cprint
local write = tex.write
local write_nl = texio.write_nl
-% \end{macrocode}
-%
-% Newer Con\TeX{}t releases replace the |unicode| library by |utf| and
-% since Lua 5.3 we can even use the Lua standard |utf8| library.
-% \begin{macrocode}
-local utf8_char = (utf8 and utf8.char) or (utf and utf.char) or unicode.utf8.char
+local scan_int = token.scan_int or token.scan_integer
+local utf8_char = utf8.char
% \end{macrocode}
%
% Deal with Con\TeX{}t: doesn't use |kpse| library.
@@ -379,9 +378,10 @@ end
%
% \begin{macro}{l3kernel.charcat}
% Creating arbitrary chars using |tex.cprint|.
+% The alternative approach using |token.put_next(token.create(...))|
+% would be about 10\% slower.
% \begin{macrocode}
-local charcat
-function charcat(charcode, catcode)
+local function charcat(charcode, catcode)
cprint(catcode, utf8_char(charcode))
end
l3kernel.charcat = charcat
@@ -526,7 +526,7 @@ l3kernel.filesize = filesize
% String comparison which gives the same results as \pdfTeX{}'s
% \tn{pdfstrcmp}, although the ordering should likely not be relied upon!
% \begin{macrocode}
-local function strcmp(A, B)
+function l3kernel.strcmp(A, B)
if A == B then
write("0")
elseif A < B then
@@ -535,7 +535,6 @@ local function strcmp(A, B)
write("1")
end
end
-l3kernel.strcmp = strcmp
% \end{macrocode}
% \end{macro}
%
@@ -556,6 +555,46 @@ l3kernel.shellescape = shellescape
% \end{macrocode}
% \end{macro}
%
+% \begin{macro}[int]{luadef}
+% An internal function for defining control sequences form Lua which behave
+% like primitives. This acts as a wrapper around |token.set_lua| which accepts
+% a function instead of an index into the functions table.
+% \begin{macrocode}
+local luacmd do
+ local token_create = token.create
+ local set_lua = token.set_lua
+ local undefined_cs = token.command_id'undefined_cs'
+
+ if luatexbase then
+ local new_luafunction = luatexbase.new_luafunction
+ local functions = lua.get_functions_table()
+ function luacmd(name, func, ...)
+ local id
+ local tok = token_create(name)
+ if tok.command == undefined_cs then
+ id = new_luafunction(name)
+ set_lua(name, id, ...)
+ else
+ id = tok.index or tok.mode
+ end
+ functions[id] = func
+ end
+ elseif context then
+ local register = context.functions.register
+ local functions = context.functions.known
+ function luacmd(name, func, ...)
+ local tok = token.create(name)
+ if tok.command == undefined_cs then
+ token.set_lua(name, register(func), ...)
+ else
+ functions[tok.index or tok.mode] = func
+ end
+ end
+ end
+end
+% \end{macrocode}
+% \end{macro}
+%
% \begin{macrocode}
%</lua>
% \end{macrocode}
diff --git a/l3kernel/l3names.dtx b/l3kernel/l3names.dtx
index d070bb295..994355c38 100644
--- a/l3kernel/l3names.dtx
+++ b/l3kernel/l3names.dtx
@@ -78,6 +78,10 @@
%<*package>
% \end{macrocode}
%
+% \begin{macrocode}
+%<*tex>
+% \end{macrocode}
+%
% The prefix here is \texttt{kernel}. A few places need |@@| to be left
% as is; this is obtained as |@@@@|.
% \begin{macrocode}
@@ -111,7 +115,9 @@
%
% To allow extracting \enquote{just the names}, a bit of DocStrip fiddling.
% \begin{macrocode}
+%</tex>
%</package>
+%<*names|tex>
%<*names|package>
% \end{macrocode}
%
@@ -1187,7 +1193,9 @@
% End of the \enquote{just the names} part of the source.
% \begin{macrocode}
%</names|package>
+%</names|tex>
%<*package>
+%<*tex>
% \end{macrocode}
%
% The job is done: close the group (using the primitive renamed!).
@@ -1431,6 +1439,28 @@
% \end{macrocode}
%
% \begin{macrocode}
+%</tex>
+% \end{macrocode}
+%
+% In \LuaTeX{}, we additionally emulate some primitives using Lua code.
+% \begin{macrocode}
+%<*lua>
+% \end{macrocode}
+%
+% \begin{macro}{\tex_Ucharcat:D}
+% \begin{macrocode}
+local cprint = tex.cprint
+luacmd('tex_Ucharcat:D', function()
+ local charcode = scan_int()
+ local catcode = scan_int()
+ cprint(catcode, utf8_char(charcode))
+end, 'global')
+%
+% \begin{macrocode}
+%</lua>
+% \end{macrocode}
+%
+% \begin{macrocode}
%</package>
% \end{macrocode}
%
diff --git a/l3kernel/l3token.dtx b/l3kernel/l3token.dtx
index 11f4fc4bc..56e2b6720 100644
--- a/l3kernel/l3token.dtx
+++ b/l3kernel/l3token.dtx
@@ -1454,37 +1454,26 @@
\if_int_odd:w 0
\sys_if_engine_luatex:T { 1 }
\sys_if_engine_xetex:T { 1 } \exp_stop_f:
- \sys_if_engine_luatex:TF
- {
- \cs_new:Npn \@@_generate_aux:nnw #1#2#3 \exp_end:
- {
- #3
- \exp_after:wN \exp_after:wN \exp_after:wN \exp_end:
- \lua_now:e { l3kernel.charcat(#1, #2) }
- }
- }
- {
- \cs_new:Npn \@@_generate_aux:nnw #1#2#3 \exp_end:
- {
- #3
- \exp_after:wN \exp_end:
- \tex_Ucharcat:D #1 \exp_stop_f: #2 \exp_stop_f:
- }
- \cs_if_exist:NF \tex_expanded:D
- {
- \cs_new_eq:NN \@@_generate_auxii:nnw \@@_generate_aux:nnw
- \cs_gset:Npn \@@_generate_aux:nnw #1#2#3 \exp_end:
- {
- #3
- \if_int_compare:w #2 = 13 \exp_stop_f:
- \__kernel_msg_expandable_error:nn { kernel } { char-active }
- \else:
- \@@_generate_auxii:nnw {#1} {#2}
- \fi:
- \exp_end:
- }
- }
- }
+ \cs_new:Npn \@@_generate_aux:nnw #1#2#3 \exp_end:
+ {
+ #3
+ \exp_after:wN \exp_end:
+ \tex_Ucharcat:D #1 \exp_stop_f: #2 \exp_stop_f:
+ }
+ \cs_if_exist:NF \tex_expanded:D
+ {
+ \cs_new_eq:NN \@@_generate_auxii:nnw \@@_generate_aux:nnw
+ \cs_gset:Npn \@@_generate_aux:nnw #1#2#3 \exp_end:
+ {
+ #3
+ \if_int_compare:w #2 = 13 \exp_stop_f:
+ \__kernel_msg_expandable_error:nn { kernel } { char-active }
+ \else:
+ \@@_generate_auxii:nnw {#1} {#2}
+ \fi:
+ \exp_end:
+ }
+ }
\else:
% \end{macrocode}
% For engines where \tn{Ucharcat} isn't available or emulated, we have
diff --git a/l3kernel/l3unicode.dtx b/l3kernel/l3unicode.dtx
index 3c55fdfc8..3fbbe90dd 100644
--- a/l3kernel/l3unicode.dtx
+++ b/l3kernel/l3unicode.dtx
@@ -97,22 +97,9 @@
{
\exp_not:N \tex_unexpanded:D \exp_not:N \exp_after:wN
{
- \sys_if_engine_luatex:TF
- {
- \exp_not:N \tex_directlua:D
- {
- l3kernel.charcat
- (
- \exp_not:N \tex_number:D #1 ,
- \exp_not:N \tex_the:D \tex_catcode:D #1
- )
- }
- }
- {
- \exp_not:N \tex_Ucharcat:D
- #1 ~
- \tex_catcode:D #1 ~
- }
+ \exp_not:N \tex_Ucharcat:D
+ #1 ~
+ \tex_catcode:D #1 ~
}
}
% \end{macrocode}
More information about the latex3-commits
mailing list.