%D \module
%D   [       file=luat-ini,
%D        version=2005.08.11,
%D          title=\CONTEXT\ Lua Macros,
%D       subtitle=Initialization,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright=PRAGMA]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.

\writestatus{loading}{Lua Support Macros (initialization)}

\unprotect

%D We have to load this module in a very early stage. Therefore we
%D cannot rely on support macros being available.

% \long\def\rescan#1{\expanded{\scantextokens{#1}}}

%D Loading lua code can be done using \type {startup.lua}. The following
%D method uses the \TEX\ input file locator of kpse. At least we need to
%D use that way of loading when we haven't yet define our own code, which
%D we keep outside the format. We will keep code outside \TEX\ files as
%D much as possible.

\ifx\setnaturalcatcodes\undefined \let\setnaturalcatcodes\relax \fi
\ifx\obeylualines      \undefined \let\obeylualines      \relax \fi
\ifx\obeyluatokens     \undefined \let\obeyluatokens     \relax \fi

% \def\loadluacode#1#2% instance filename
%   {\bgroup
%    \everyeof{\noexpand}% hack to make \input nicely expandable
%    \setnaturalcatcodes
%    \obeylualines
%    %message{[Lua Load: #2]}%
%    \directlua#1\expandafter{\normalinput#2\space}\relax
%    \egroup}

%D A few more goodies:

\long\def\dostartlua#1%
  {\begingroup
   \obeylualines
   \dodostartlua{#1}}

\long\def\dodostartlua#1#2\stoplua
  {\expanded{\endgroup\noexpand\directlua#1{#2}}}

\long\def\dostartluacode#1%
  {\begingroup
   \obeylualines
   \obeyluatokens
   \dodostartluacode{#1}}

\long\def\dodostartluacode#1#2\stopluacode
  {\expanded{\endgroup\noexpand\directlua#1{#2}}}

\def\startlua    {\dostartlua    \zerocount}
\def\startluacode{\dostartluacode\zerocount}

%D Some delayed definitions:

\ifx\obeylines        \undefined \let\obeylines        \relax \fi
\ifx\obeyedline       \undefined \let\obeyedline       \relax \fi
\ifx\obeyspaces       \undefined \let\obeyspaces       \relax \fi
\ifx\obeyedspace      \undefined \let\obeyedspace      \relax \fi
\ifx\outputnewlinechar\undefined \let\outputnewlinechar\relax \fi

\def\obeylualines
  {\obeylines  \let\obeyedline \outputnewlinechar
   \obeyspaces \let\obeyedspace\space}

\def\obeyluatokens % todo: make this a proper catcode table, use let's
  {\catcode`\%=12 \catcode`\#=12
   \catcode`\_=12 \catcode`\^=12
   \catcode`\&=12 \catcode`\|=12
   \catcode`\{=12 \catcode`\}=12
   \catcode`\~=12 \catcode`\$=12
   \def\\{\string\\}\def\|{\string\|}\def\-{\string\-}%
   \def\({\string\(}\def\){\string\)}\def\{{\string\{}\def\}{\string\}}%
   \def\'{\string\'}\def\"{\string\"}%
   \def\n{\string\n}\def\r{\string\r}\def\f{\string\f}\def\t{\string\t}%
   \def\a{\string\a}\def\b{\string\b}\def\v{\string\v}\def\s{\string\s}%
   \def\1{\string1}\def\2{\string2}\def\3{\string3}\def\4{\string\4}\def\5{\string\5}%
   \def\6{\string6}\def\7{\string7}\def\8{\string8}\def\9{\string\9}\def\0{\string\0}}

%D We provide an interface for defining instances:

\def\s!lua{lua} \def\v!code{code} \let\@EA\expandafter

\def\definelua[#1]%
  {\ifcsname#1\s!lua\endcsname\else\expandafter\newlua\csname#1\s!lua\endcsname\fi
   \setevalue{\e!start#1\s!lua       }{\noexpand\dostartlua    \csname#1\s!lua\endcsname}%
   \setevalue{\e!start#1\s!lua\v!code}{\noexpand\dostartluacode\csname#1\s!lua\endcsname}%
   \setvalue {\e!stop #1\s!lua       }{\dostoplua    }%
   \setvalue {\e!stop #1\s!lua\v!code}{\dostopluacode}}

\definelua[CTX]

\protect \endinput
