[latex3-commits] [l3svn] 01/02: Move Lua code to a separate file

noreply at latex-project.org noreply at latex-project.org
Thu Nov 5 11:22:44 CET 2015


This is an automated email from the git hooks/post-receive script.

joseph pushed a commit to branch master
in repository l3svn.

commit c9e00293b3f3b7db95cf513c39c4a79b3609a41b
Author: Joseph Wright <joseph.wright at morningstar2.co.uk>
Date:   Thu Nov 5 10:10:52 2015 +0000

    Move Lua code to a separate file
    
    At present both format and package mode use the same .lua file:
    this might need to be adjusted in the future.
---
 l3kernel/build.lua       |    1 +
 l3kernel/l3.ins          |   17 ++++-
 l3kernel/l3bootstrap.dtx |  157 ++++++++++++----------------------------------
 l3kernel/l3format.ins    |   18 +++++-
 l3kernel/l3luatex.dtx    |   95 +++++++++++++++++++++++++++-
 5 files changed, 166 insertions(+), 122 deletions(-)

diff --git a/l3kernel/build.lua b/l3kernel/build.lua
index 29294ad..ff3a774 100644
--- a/l3kernel/build.lua
+++ b/l3kernel/build.lua
@@ -27,6 +27,7 @@ installfiles =
   {
     "l3dvipdfmx.def", "l3dvips.def", "l3pdfmode.def", "l3unicode-data.def",
     "l3xdvipdfmx.def",
+    "expl3.lua",
     "*.cls", "*.sty", "*.tex"
   }
 sourcefiles  = {"l3unicode-data.def", "*.dtx", "*.ins"}
diff --git a/l3kernel/l3.ins b/l3kernel/l3.ins
index 20063fa..903e359 100644
--- a/l3kernel/l3.ins
+++ b/l3kernel/l3.ins
@@ -77,7 +77,7 @@ Do not distribute a modified version of this file.
         \from{l3color.dtx}      {package}
         \from{l3candidates.dtx} {package}
         \from{l3sys.dtx}        {package}
-        \from{l3luatex.dtx}     {package}
+        \from{l3luatex.dtx}     {package,tex}
       }
   }
 
@@ -123,5 +123,20 @@ Do not distribute a modified version of this file.
 \generate{\file{l3tl.sty}         {\from{l3oldmodules.dtx}       {l3tl,oldmodules}}}
 \generate{\file{l3token.sty}      {\from{l3oldmodules.dtx}       {l3token,oldmodules}}}
 
+% Lua code
+
+\def\MetaPrefix{--}
+\preamble
+
+EXPERIMENTAL CODE
+
+Do not distribute this file without also distributing the
+source files specified above.
+
+Do not distribute a modified version of this file.
+
+\endpreamble
+\nopostamble
+\generate{\file{expl3.lua}{\from{l3luatex.dtx}{package,lua}}}
 
 \endbatchfile
diff --git a/l3kernel/l3bootstrap.dtx b/l3kernel/l3bootstrap.dtx
index 6c006d9..51bdf28 100644
--- a/l3kernel/l3bootstrap.dtx
+++ b/l3kernel/l3bootstrap.dtx
@@ -325,7 +325,7 @@
 %</package>
 %    \end{macrocode}
 %
-% \subsection{The \tn{pdfstrcmp} primitive with \XeTeX{} and \LuaTeX{}}
+% \subsection{The \tn{pdfstrcmp} primitive in \XeTeX{}}
 %
 % Only \pdfTeX{} has a primitive called \tn{pdfstrcmp}. The \XeTeX{}
 % version is just \tn{strcmp}, so there is some shuffling to do. As
@@ -337,131 +337,54 @@
 \fi
 %    \end{macrocode}
 %
-%   If \LuaTeX{} is in use then no primitive \tn{pdfstrcmp} is available.
-%   However, it can be emulated using some Lua code. In earlier versions of
-%   the code, the \pkg{pdftexcmds} package was loaded to do this task. However,
-%   that raises some issues in \enquote{generic} (it fails with Con\TeX{}t
-%   MkIV), and also adds a hardly-needed dependency.  Note that \LuaTeX{}
-%   prior to version $0.36$ is not supported by \pkg{expl3}: here that means
-%   simply skipping the definition, which will then be picked up later. This
-%   definition may need to be done twice: one \enquote{now} and once at the
-%   start of every job. The latter can occur in package mode if for example a
-%   custom format is being constructed. To achieve this while not requiring a
-%   separate file, the Lua code is saved into a macro then used twice.
-%   (In the long term, the Lua code here may be best moved to a separate
-%   file.)
-%
-%   No macro definition is given just yet: that is left until \pkg{l3basics}.
+% \subsection{Loading support \Lua{} code}
+%
+% When \LuaTeX{] is used there are various pieces of \Lua{} code which need to
+% be loaded. The code itself is defined in \pkg{l3luatex} and is extracted into
+% a separate file. Thus here the task is to load the \Lua{} code both now and
+% (if required) at the start of each job.
 %    \begin{macrocode}
-\begingroup
-  \expandafter\ifx\csname directlua\endcsname\relax
+\begingroup\expandafter\expandafter\expandafter\endgroup
+\expandafter\ifx\csname directlua\endcsname\relax
+\else
+  \ifnum\luatexversion<70 %
   \else
-    \ifnum\luatexversion<36 %
-    \else
-      \catcode`\_=11 %
-      \catcode`\:=11 %
-      \def\tempa
-        {%
-          l3kernel = l3kernel or { }
-          function l3kernel.strcmp(A, B)
-            if A == B then
-              tex.write("0")
-            elseif A < B then
-              tex.write("-1")
-            else
-              tex.write("1")
-            end
-          end
-        }
-      \directlua{\tempa}
 %    \end{macrocode}
-%  A test for \LuaTeX{} in Ini\TeX{} mode.
+%   In package mode a category code table is needed: either use a pre-loaded
+%   allocator or provide one using the \LaTeXe{}-based generic code. In format
+%   mode the table used here can be hard-coded into the \Lua{}.
 %    \begin{macrocode}
-      \ifnum 0%
-        \directlua
-          {%
-            if status.ini_version then
-              tex.write("1")
-            end
-          }>0 %
-        \global\everyjob\expandafter
-          {%
-            \the\expandafter\everyjob
-            \expandafter\lua_now_x:n\expandafter{\tempa}%
-          }
-      \fi
+%<*package>
+    \begingroup\expandafter\expandafter\expandafter\endgroup
+    \expandafter\ifx\csname newcatcodetable\endcsname\relax
+      \input{ltluatex}%
     \fi
-  \fi
-\endgroup
+    \newcatcodetable\ucharcat at table
+    \directlua{
+      l3kernel = l3kernel or { }
+      local charcat_table = \number\ucharcat at table
+      l3kernel.charcat_table = charcat_table
+    }%
+%</package>
+    \directlua{require("expl3")}
 %    \end{macrocode}
-%
-% \subsection{Emulating \tn{Ucharchar} in \LuaTeX{}}
-%
-% Expandably creating character tokens can be done in macros but for the
-% full Unicode range requires engine support in practical terms. That is
-% available in \XeTeX{} as \tn{Ucharcat} but in \LuaTeX{} some Lua code
-% is required. That is set up here, following the model above. Note that
-% we do not try to emulate the syntax of the primitive (doable with \LuaTeX{}
-% 0.80 or later, but not required by our use case).
-%
-% One minor wrinkle is that a catcode table is needed to return values.
-% For format mode, long-term we can simply take an arbitrary one and make
-% sure this is never allocated. In package mode use the same approach
-% (and table) as \pkg{ucharcat}: look for an allocator and use it if available,
-% or use a table unlikely to clash with any other use. We use the same table
-% as \pkg{ucharcat} but as this is \enquote{disposable} we are safe.
+%   As the user might be making a custom format, no assumption is made about
+%   matching package mode with only loading the \Lua{} code once. Instead, a
+%   query to \Lua{} will reveal what mode is in operation.
 %    \begin{macrocode}
-\begingroup
-  \expandafter\ifx\csname directlua\endcsname\relax
-  \else
-    \ifnum\luatexversion<70 %
-    \else
-%<*initex>
-       % TEMP
-       \catcode`\@=11 %
-       \chardef\ucharcat at table="8000 %
-%</initex>
-%<*package>
-      \begingroup\expandafter\expandafter\expandafter\endgroup
-      \expandafter\ifx\csname newcatcodetable\endcsname\relax
-        \directlua{tex.enableprimitives("",{"initcatcodetable"})}
-        \chardef\ucharcat at table"7000 %
-        \initcatcodetable\ucharcat at table
-      \else
-        \newcatcodetable\ucharcat at table
-      \fi
-%</package>
-      \catcode`\_=11 %
-      \catcode`\:=11 %
-      \def\tempa
-        {%
-          l3kernel = l3kernel or { }
-          local utf8_char = unicode.utf8.char
-          function l3kernel.charcat(charcode, catcode)
-            tex.setcatcode(\number\ucharcat at table, charcode, catcode)
-            tex.sprint(\number\ucharcat at table, utf8_char(charcode))
-          end
-        }
-      \directlua{\tempa}%
-      \ifnum 0%
-        \directlua
-          {%
-            if status.ini_version then
-              tex.write("1")
-            end
-          }>0 %
-%<*initex>
-        \edef\tempa{\tempa}% TEMP
-%</initex>
-        \global\everyjob\expandafter
-          {%
-            \the\expandafter\everyjob
-            \expandafter\lua_now_x:n\expandafter{\tempa}%
-          }
-      \fi
+    \ifnum 0%
+      \directlua{
+        if status.ini_version then
+          tex.write("1")
+        end
+      }>0 %
+      \everyjob\expandafter{%
+        \the\expandafter\everyjob
+        \csname\detokenize{lua_now_x:n}\endcsname{require("expl3")}%
+      }%
     \fi
   \fi
-\endgroup
+\fi
 %    \end{macrocode}
 %
 % \subsection{Engine requirements}
diff --git a/l3kernel/l3format.ins b/l3kernel/l3format.ins
index 3d967cc..9f3bd8b 100644
--- a/l3kernel/l3format.ins
+++ b/l3kernel/l3format.ins
@@ -77,7 +77,7 @@ Do not distribute a modified version of this file.
         \from{l3color.dtx}      {initex}
         \from{l3candidates.dtx} {initex}
         \from{l3sys.dtx}        {initex}
-        \from{l3luatex.dtx}     {initex}
+        \from{l3luatex.dtx}     {initex,tex}
         % ======== FORMAT ONLY =========
         \from{l3final.dtx}      {initex}
         % ==============================
@@ -90,4 +90,20 @@ Do not distribute a modified version of this file.
 
 \generate{\file{l3docstrip.tex}{\from{l3docstrip.dtx}{program}}}
 
+% Lua code (currently identical to the package-mode version)
+
+\def\MetaPrefix{--}
+\preamble
+
+EXPERIMENTAL CODE
+
+Do not distribute this file without also distributing the
+source files specified above.
+
+Do not distribute a modified version of this file.
+
+\endpreamble
+\nopostamble
+\generate{\file{expl3.lua}{\from{l3luatex.dtx}{package,lua}}}
+
 \endbatchfile
diff --git a/l3kernel/l3luatex.dtx b/l3kernel/l3luatex.dtx
index 9ffce3c..ab21575 100644
--- a/l3kernel/l3luatex.dtx
+++ b/l3kernel/l3luatex.dtx
@@ -36,10 +36,10 @@
 %<*driver>
 \documentclass[full]{l3doc}
 %</driver>
-%<*driver|package>
+%<*driver|package&tex>
 \GetIdInfo$Id$
   {L3 Experimental LuaTeX-specific functions}
-%</driver|package>
+%</driver|package&tex>
 %<*driver>
 \begin{document}
   \DocInput{\jobname.dtx}
@@ -77,6 +77,8 @@
 % error: use \cs{sys_if_engine_luatex:T} to avoid this. Details of coding
 % the \LuaTeX{} engine are detailed in the \LuaTeX{} manual.
 %
+% \subsection{\TeX{} code interfaces}
+%
 % \begin{function}[EXP, added = 2015-06-29]{\lua_now_x:n, \lua_now:n}
 %   \begin{syntax}
 %     \cs{lua_now:n} \Arg{token list}
@@ -142,6 +144,27 @@
 %   \end{texnote}
 % \end{function}
 %
+% \subsection{Lua interfaces}
+%
+% As well as interfaces for \TeX{}, there are a small number of Lua functions
+% provided here. Currently these are intended for internal use only.
+%
+% \begin{function}{l3kernel.strcmp}
+%   \begin{syntax}
+%     \cs{l3kernel.strcmp|(|\meta{str one}, \meta{str two}|)|
+%   \end{syntax}
+%   Compares the two strings and returns |0| to \TeX{}
+%   if the two are identical.
+% \end{function}
+%
+% \begin{function}{l3kernel.charcat}
+%   \begin{syntax}
+%     \cs{l3kernel.charcat|(|\meta{charcode}, \meta{catcode}|)|
+%   \end{syntax}
+%   Constructs a character of \meta{charcode} and \meta{catcode} and returns
+%   the result to \TeX{}.
+% \end{function}
+%
 % \end{documentation}
 %
 % \begin{implementation}
@@ -152,7 +175,11 @@
 %<*initex|package>
 %    \end{macrocode}
 %
-% \subsubsection{Breaking out to \Lua{}}
+% \subsection{Breaking out to \Lua{}}
+%
+%    \begin{macrocode}
+%<*tex>
+%    \end{macrocode}
 %
 % \begin{macro}[EXP]{\lua_now_x:n, \lua_now:n}
 % \begin{macro}{\lua_shipout_x:n, \lua_shipout:n}
@@ -206,6 +233,68 @@
 %    \end{macrocode}
 %
 %    \begin{macrocode}
+%</tex>
+%    \end{macrocode}
+%
+% \subsection{\Lua{} functions for internal use}
+%
+%    \begin{macrocode}
+%<*lua>
+%    \end{macrocode}
+%
+% \begin{macro}{l3kernel}
+%   Create a table for the kernel's own use.
+%    \begin{macrocode}
+l3kernel = l3kernel or { }
+%    \end{macrocode}
+% \end{macro}
+%
+%   Various local copies of standard functions: naming convention is to retain
+%   the full text but replace all |.| by |_|. 
+%    \begin{macrocode}
+local tex_setcatcode    = tex.setcatcode
+local tex_sprint        = tex.sprint
+local tex_write         = tex.write
+local unicode_utf8_char = unicode.utf8.char
+%    \end{macrocode}
+%
+% \begin{macro}{l3kernel.strcmp}
+%   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)
+  if A == B then
+    tex_write("0")
+  elseif A < B then
+    tex_write("-1")
+  else
+    tex_write("1")
+  end
+end
+l3kernel.strcmp = strcmp
+%    \end{macrocode}
+% \end{macro}
+%
+% \begin{macro}{l3kernel.charcat}
+%   Creating arbitrary chars needs a category code table. As set up here,
+%   one may have been assigned earlier (see \pkg{l3bootstrap}) or a hard-coded
+%   one is used. The latter is intended for format mode and should be adjusted
+%   to match an eventual allocator.
+%    \begin{macrocode}
+local charcat_table = l3kernel.charcat_table or 1
+local function charcat(charcode, catcode)
+  tex_setcatcode(charcat_table, charcode, catcode)
+  tex_sprint(charcat_table, unicode_utf8_char(charcode))
+end
+l3kernel.charcat = charcat
+%    \end{macrocode}
+% \end{macro}
+%
+%    \begin{macrocode}
+%</lua>
+%    \end{macrocode}
+%
+%    \begin{macrocode}
 %</initex|package>
 %    \end{macrocode}
 %

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the latex3-commits mailing list