summaryrefslogtreecommitdiff
path: root/tex/context/base/luat-ini.mkiv
diff options
context:
space:
mode:
Diffstat (limited to 'tex/context/base/luat-ini.mkiv')
-rw-r--r--tex/context/base/luat-ini.mkiv198
1 files changed, 106 insertions, 92 deletions
diff --git a/tex/context/base/luat-ini.mkiv b/tex/context/base/luat-ini.mkiv
index a3a590311..b455a4158 100644
--- a/tex/context/base/luat-ini.mkiv
+++ b/tex/context/base/luat-ini.mkiv
@@ -34,7 +34,7 @@
% the \type {\normalexpanded} around \type {\directlua}. Something to discuss
% in the team.
-\unexpanded\def\startlua % \stoplua
+\normalprotected\def\startlua % \stoplua
{\begingroup
\obeylualines
\luat_start_lua_indeed}
@@ -42,7 +42,7 @@
\def\luat_start_lua_indeed#1\stoplua
{\normalexpanded{\endgroup\noexpand\directlua{#1}}} % \zerocount is default
-\unexpanded\def\startluacode % \stopluacode
+\normalprotected\def\startluacode % \stopluacode
{\begingroup
\obeylualines
\obeyluatokens
@@ -77,7 +77,7 @@
\edef\lua_letter_seven {\string\7} \edef\lua_letter_eight {\string\8}
\edef\lua_letter_nine {\string\9} \edef\lua_letter_zero {\string\0}
-\appendtoks
+\everyluacode {% \appendtoks
\let\\\lua_letter_backslash
\let\|\lua_letter_bar \let\-\lua_letter_dash
\let\(\lua_letter_lparent \let\)\lua_letter_rparent
@@ -92,85 +92,15 @@
\let\5\lua_letter_five \let\6\lua_letter_six
\let\7\lua_letter_seven \let\8\lua_letter_eight
\let\9\lua_letter_nine \let\0\lua_letter_zero
-\to \everyluacode
+} % \to \everyluacode
-\unexpanded\def\obeyluatokens
+\normalprotected\def\obeyluatokens
{\setcatcodetable\luacatcodes
\the\everyluacode}
-
\edef\luamajorversion{\ctxwrite{_MINORVERSION}}
\edef\luaminorversion{\ctxwrite{_MAJORVERSION}}
-%D \macros
-%D {definenamedlua}
-%D
-%D We provide an interface for defining instances:
-
-\def\s!lua{lua} \def\v!code{code} \def\!!name{name} \def\s!data{data}
-
-%D Beware: because \type {\expanded} is een convert command, the error
-%D message will show \type{<inserted text>} as part of the message.
-
-\installcorenamespace{luacode}
-
-\unexpanded\def\luat_start_named_lua_code#1%
- {\begingroup
- \obeylualines
- \obeyluatokens
- \csname\??luacode#1\endcsname}
-
-\unexpanded\def\definenamedlua[#1]#2[#3]% no optional arg handling here yet / we could use numbers instead (more efficient)
- {\ifcsname\??luacode#1\endcsname \else
- \scratchcounter\ctxlua{lua.registername("#1","#3")}%
- \normalexpanded{\xdef\csname\??luacode#1\endcsname##1\csname\e!stop#1\v!code\endcsname}%
- %{\endgroup\noexpand\directlua\the\scratchcounter{local _ENV=protect("#1\s!data")##1}}%
- {\noexpand\normalexpanded{\endgroup\noexpand\directlua\the\scratchcounter{local _ENV=protect("#1\s!data")##1}}}%
- \expandafter\edef\csname\e!start#1\v!code\endcsname {\luat_start_named_lua_code{#1}}%
- \expandafter\edef\csname #1\v!code\endcsname##1{\noexpand\directlua\the\scratchcounter{local _ENV=protect("#1\s!data")##1}}%
- \fi}
-
-%D We predefine a few.
-
-% \definenamedlua[module][module instance] % not needed
-
-\definenamedlua[user] [private user instance]
-\definenamedlua[third] [third party module instance]
-\definenamedlua[isolated][isolated instance]
-
-%D In practice this works out as follows:
-%D
-%D \startbuffer
-%D \startluacode
-%D context("LUA")
-%D \stopluacode
-%D
-%D \startusercode
-%D global.context("USER 1")
-%D context.par()
-%D context("USER 2")
-%D context.par()
-%D if characters then
-%D context("ACCESS directly")
-%D elseif global.characters then
-%D context("ACCESS via global")
-%D else
-%D context("NO ACCESS at all")
-%D end
-%D context.par()
-%D if bogus then
-%D context("ACCESS directly")
-%D elseif global.bogus then
-%D context("ACCESS via global")
-%D else
-%D context("NO ACCESS at all")
-%D end
-%D context.par()
-%D \stopusercode
-%D \stopbuffer
-%D
-%D \typebuffer
-
%D We need a way to pass strings safely to \LUA\ without the
%D need for tricky escaping. Compare:
%D
@@ -188,23 +118,28 @@
%D for it:
%D
%D \starttyping
-%D \long\edef\luaescapestring#1{\!!bs#1\!!es}
+%D \edef\luaescapestring#1{\!!bs#1\!!es}
%D \stoptyping
-\def\setdocumentfilename #1#2{\ctxlua{document.setfilename(#1,"#2")}}
-\def\setdocumentargument #1#2{\ctxlua{document.setargument("#1","#2")}}
-\def\setdocumentargumentdefault#1#2{\ctxlua{document.setdefaultargument("#1","#2")}}
-\def\getdocumentfilename #1{\ctxlua{document.getfilename("#1")}}
-\def\getdocumentargument #1{\ctxlua{document.getargument("#1")}}
-\def\getdocumentargumentdefault#1#2{\ctxlua{document.getargument("#1","#2")}}
-\def\doifdocumentargumentelse #1{\doifsomethingelse{\getdocumentargument{#1}}}
-\def\doifdocumentargument #1{\doifsomething {\getdocumentargument{#1}}}
-\def\doifnotdocumentargument #1{\doifnothing {\getdocumentargument{#1}}}
-\def\doifdocumentfilenameelse #1{\doifsomethingelse{\getdocumentfilename{#1}}}
-\def\doifdocumentfilename #1{\doifsomething {\getdocumentfilename{#1}}}
-\def\doifnotdocumentfilename #1{\doifnothing {\getdocumentfilename{#1}}}
-
-\let\doifelsedocumentargument\doifdocumentargumentelse
+\def\setdocumentfilename #1#2{\clf_setdocumentfilename\numexpr#1\relax{#2}}
+\def\setdocumentargument #1#2{\clf_setdocumentargument{#1}{#2}}
+\def\setdocumentargumentdefault#1#2{\clf_setdocumentdefaultargument{#1}{#2}}
+\def\getdocumentfilename #1{\clf_getdocumentfilename\numexpr#1\relax}
+\def\getdocumentargument #1{\clf_getdocumentargument{#1}{}}
+\def\setdocumentargument #1#2{\clf_setdocumentargument{#1}{#2}}
+\def\getdocumentargumentdefault#1#2{\clf_getdocumentargument{#1}{#2}}
+
+% seldom used so no need for speedy variants:
+
+\def\doifelsedocumentargument #1{\doifelsesomething{\clf_getdocumentargument{#1}}}
+\def\doifdocumentargument #1{\doifsomething {\clf_getdocumentargument{#1}}}
+\def\doifnotdocumentargument #1{\doifnothing {\clf_getdocumentargument{#1}}}
+\def\doifelsedocumentfilename #1{\doifelsesomething{\clf_getdocumentfilename\numexpr#1\relax}}
+\def\doifdocumentfilename #1{\doifsomething {\clf_getdocumentfilename\numexpr#1\relax}}
+\def\doifnotdocumentfilename #1{\doifnothing {\clf_getdocumentfilename\numexpr#1\relax}}
+
+\let\doifdocumentargumentelse\doifelsedocumentargument
+\let\doifdocumentfilenameelse\doifelsedocumentfilename
%D A handy helper:
@@ -212,7 +147,7 @@
%D Experimental:
-\unexpanded\def\startluaparameterset[#1]%
+\normalprotected\def\startluaparameterset[#1]%
{\begingroup
\obeylualines
\obeyluatokens
@@ -251,7 +186,7 @@
%D \ctxluacode{context("%0.5f",1/3)}
%D \stoptyping
-\unexpanded\def\ctxluacode
+\normalprotected\def\ctxluacode
{\begingroup
\obeylualines
\obeyluatokens
@@ -266,4 +201,83 @@
\def\luat_lua_code
{\normalexpanded{\endgroup\noexpand\directlua\expandafter{\the\scratchtoks}}} % \zerocount is default
+% \startctxfunction MyFunctionA
+% context(" A1 ")
+% \stopctxfunction
+%
+% \startctxfunctiondefinition MyFunctionB
+% context(" B2 ")
+% \stopctxfunctiondefinition
+%
+% \starttext
+% \dorecurse{10000}{\ctxfunction{MyFunctionA}} \page
+% \dorecurse{10000}{\MyFunctionB} \page
+% \dorecurse{10000}{\ctxlua{context(" C3 ")}} \page
+% \stoptext
+
+\installsystemnamespace{ctxfunction}
+
+\normalprotected\def\startctxfunctiondefinition #1 %
+ {\begingroup \obeylualines \obeyluatokens \luat_start_lua_function_definition_indeed{#1}}
+
+\installsystemnamespace{luafunction}
+
+\def\luat_start_lua_function_definition_indeed#1#2\stopctxfunctiondefinition
+ {\endgroup
+ \expandafter\chardef\csname\??luafunction#1\endcsname\ctxcommand{ctxfunction(\!!bs#2\!!es)}\relax
+ \expandafter\edef\csname#1\endcsname{\noexpand\luafunction\csname\??luafunction#1\endcsname}}
+
+\normalprotected\def\setctxluafunction#1#2% experiment
+ {\expandafter\chardef\csname\??luafunction#1\endcsname#2\relax
+ \expandafter\edef\csname#1\endcsname{\noexpand\luafunction\csname\??luafunction#1\endcsname}}
+
+\let\stopctxfunctiondefinition\relax
+
+\normalprotected\def\startctxfunction #1 %
+ {\begingroup \obeylualines \obeyluatokens \luat_start_lua_function_indeed{#1}}
+
+\def\luat_start_lua_function_indeed#1#2\stopctxfunction
+ {\endgroup\expandafter\edef\csname\??ctxfunction#1\endcsname{\noexpand\luafunction\ctxcommand{ctxfunction(\!!bs#2\!!es)}\relax}}
+
+\let\stopctxfunction\relax
+
+\def\ctxfunction#1%
+ {\csname\??ctxfunction#1\endcsname}
+
+% In theory this is faster due to the call not being wrapped in a function but in
+% practice the speedup can't be noticed. The actions called for often have lots of
+% lookups so an extra one doesn't matter much. The kind of calls differs a lot per
+% document and often there are other ways to optimize a style. For instance we can
+% gain a lot when defining a font, but when a frozen definition is used that gain
+% gets completely lost. For some calls (take list writers) it can get worse if only
+% because readability gets worse and passing is already efficient due to selective
+% flushing, while with the token scanners one has to scan all of them.
+
+% \startctxfunctiondefinition foo commands.foo() \stopctxfunctiondefinition
+%
+% \installctxfunction\foo{commands.foo}
+
+% This is a forward definition:
+
+\def\checkedstrippedcsname#1% this permits \strippedcsname{\xxx} and \strippedcsname{xxx}
+ {\expandafter\syst_helpers_checked_stripped_csname\string#1}
+
+\def\syst_helpers_checked_stripped_csname#1%
+ {\if\noexpand#1\letterbackslash\else#1\fi}
+
+\normalprotected\def\installctxfunction#1#2%
+ {\edef\m_syst_name{\checkedstrippedcsname#1}%
+ \global\expandafter\chardef\csname\??luafunction\m_syst_name\endcsname\ctxcommand{ctxfunction("#2",true)}\relax
+ \expandafter\xdef\csname\m_syst_name\endcsname{\noexpand\luafunction\csname\??luafunction\m_syst_name\endcsname}}
+
+\normalprotected\def\installctxscanner#1#2%
+ {\edef\m_syst_name{\checkedstrippedcsname#1}%
+ \global\expandafter\chardef\csname\??luafunction\m_syst_name\endcsname\ctxcommand{ctxscanner("\m_syst_name","#2",true)}\relax
+ \expandafter\xdef\csname\m_syst_name\endcsname{\noexpand\luafunction\csname\??luafunction\m_syst_name\endcsname}}
+
+\normalprotected\def\resetctxscanner#1%
+ {\edef\m_syst_name{\checkedstrippedcsname#1}%
+ \global\expandafter\chardef\csname\??luafunction\m_syst_name\endcsname\zerocount
+ \global\expandafter\let\csname\m_syst_name\endcsname\relax}
+
\protect \endinput