
\ifdefined \minimloaded
    \message{(skipped)}
    \expandafter\endinput\fi
\chardef\minimloaded=\catcode`\:
\catcode`\:=11

% 1 settings

\suppressoutererror = 1
\frenchspacing

% 1 programming macros

\newbox\voidbox \setbox0=\box\voidbox

\long\def\ignore#1{}
\long\def\unbrace#1{#1}
\long\def\firstoftwo#1#2{#1}
\long\def\secondoftwo#1#2{#2}

% \spacechar is equivalent to a space character
{\let\@\relax \global\futurelet\spacechar\@ \relax}

% \nextif c {yes} {no}
\protected\def\nextif{\nextif:\if}
% \nextifx c {yes} {no}
\protected\def\nextifx{\nextif:\ifx}
% \nextifcat c {yes} {no}
\protected\def\nextifcat{\nextif:\ifcat}

\long\def\nextif:#1#2#3#4{\begingroup % \if c {yes} {no}
    \def\nextif:test{#1\nextif:token#2\relax
        \expandafter\firstoftwo\else
        \expandafter\secondoftwo\fi
        {\endgroup#3}{\endgroup#4}}%
    \futurelet\nextif:token\nextif:test}

% \withoptions [default] {code}
\protected\def\withoptions[#1]#2{\nextifx[{#2}{#2[#1]}} %]

% \splitcommalist {code} {list}
\def\splitcommalist#1#2{\splitcommalist:parse{#1}#2,\splitcommalist:end,}
\def\splitcommalist:parse#1#2,{% command partial-list,
    \ifx\splitcommalist:end#2\empty\else % test for end-of-list
        \expandafter\splitcommalist:item
            \expandafter{\scantextokens{#2}}{#1}\fi} % remove spaces
\def\splitcommalist:item#1#2\fi{\fi
    \ifx\splitcommalist:end#1\splitcommalist:end\else#2{#1}\fi
    \splitcommalist:parse{#2}}
\protected\def\splitcommalist:end{\splitcommalist:end}

% \decompressedpdf
\def\decompressedpdf{%
    \pdfvariable compresslevel    = 0
    \pdfvariable objcompresslevel = 0
    \pdfvariable recompress       = 1
}

% \unset
\newcount\unset \unset = -"7FFFFFFF

% 1 ltluatex compatibility

% repair ltluatex, which, without apparent reason, resets all allocation 
% counters, even if they already exist
\def\minim:ltltx:file{ltluatex.tex}
\let\ProvidesFile:saved=\ProvidesFile
\def\ProvidesFile#1#2[#3]{%
    \ifdefined\ProvidesFile:saved \ProvidesFile:saved{#1}#2[#3]\fi
    \def\minim:ltltx:arg{#1}%
    \ifx\minim:ltltx:file\minim:ltltx:arg
        \let\ProvidesFile=\ProvidesFile:saved
        \let\endinput:saved=\endinput
        \protected\edef\endinput{%
            \minim:ltltx:fix{e@alloc@attribute@count}%
            \minim:ltltx:fix{e@alloc@whatsit@count}%
            \minim:ltltx:fix{e@alloc@bytecode@count}%
            \minim:ltltx:fix{e@alloc@luafunction@count}%
            \minim:ltltx:fix{e@alloc@luachunk@count}%
            \minim:ltltx:fix{e@alloc@ccodetable@count}%
            % also patch in remembering allocations to lua
            \alloc:patch \noexpand\attribute
            \alloc:patch \noexpand\whatsit
            \alloc:patch \noexpand\luabytecode
            \alloc:patch \noexpand\luachunkname
            \alloc:patch \noexpand\catcodetable
            \alloc:patch \noexpand\userrule
            % restore register allocation functions
            \directlua{ local C = require('minim-callbacks')
                callback.register, callback.find, callback.list
                    = C.register, C.find, C.list }%
            \let\noexpand\endinput=\noexpand\endinput:saved
            \noexpand\endinput}\fi}
\def\minim:ltltx:fix#1{%
    \ifnum0<\the\csname#1\endcsname
        \wlog{Restoring \csname#1\endcsname to previous value \the\csname#1\endcsname}%
        \expandafter\noexpand\csname#1\endcsname =\the\csname#1\endcsname\relax\fi}
\protected\def\alloc:patch#1{%
    \expandafter\expandafter\expandafter\let
        \expandafter\expandafter\csname\csstring#1:ltluatex:new\endcsname
            \csname new\csstring#1\endcsname
    \expandafter\edef\csname new\csstring#1\endcsname##1{%
        \noexpand\minim:rememberalloc{\csstring#1}{\noexpand\csstring##1}%
        \expandafter\noexpand\csname\csstring#1:ltluatex:new\endcsname{##1}}}

% 1 allocation

% use global allocation (see etex.src)
\let\newcount  = \globcount
\let\newdimen  = \globdimen
\let\newskip   = \globskip
\let\newmuskip = \globmuskip
\let\newbox    = \globbox
\let\newtoks   = \globtoks
\let\newmarks  = \globmarks

% new allocation macros

\def\alloc:makenew#1#2#3{%
    \unless\ifcsname#3\endcsname
        \expandafter\newcount \csname#3\endcsname
        \csname#3\endcsname 0\fi
    \ifcsname new\csstring#1\endcsname
        \alloc:patch#1\else
        \expandafter\edef\csname new\csstring#1\endcsname{%
            \noexpand\minim:alloc\noexpand#1\noexpand#2%
            \expandafter\noexpand\csname#3\endcsname}\fi}

\def\minim:alloc#1#2#3#4{% \register \chardef \alloccount \name
    \global\advance#31%
    \allocationnumber#3%
    \global#2#4\allocationnumber
    \wlog{\string#4=\string#1\the\allocationnumber}%
    % minim:rememberalloc will be defined from lua side
    \minim:rememberalloc{\csstring#1}{\csstring#4}}

% all names and counters below are identical to those from ltluatex
% note: we cannot use \newluafunction, or ltluatex will not load
\alloc:makenew \function     \chardef         {e@alloc@luafunction@count}
\alloc:makenew \attribute    \attributedef    {e@alloc@attribute@count}
\alloc:makenew \whatsit      \chardef         {e@alloc@whatsit@count}
\alloc:makenew \luabytecode  \chardef         {e@alloc@bytecode@count}
\alloc:makenew \luachunkname \chardef:chunk   {e@alloc@luachunk@count}
\alloc:makenew \catcodetable \catcode:chardef {e@alloc@ccodetable@count}
\alloc:makenew \userrule     \chardef         {e@alloc@rule@count}

% initialise new catcode tables
\def\catcode:chardef#1#2{\chardef#1#2\initcatcodetable#2}
\csname e@alloc@ccodetable@count\endcsname = 4 % because ltluatex allocates 4...

% set initial chunk name value
\def\chardef:chunk#1#2{\chardef#1#2\directlua{lua.name[\the#2]='\csstring#1'}}

% \setluachunkname
\protected\def\setluachunkname#1#2{\newluachunkname#1%
    \directlua{lua.name[\the#1]='\luaescapestring{#2}'}}

% patch in remembering box allocations
\def\minim:boxchardef#1{%
    \minim:rememberalloc{box}{\csstring#1}%
    \mathchardef#1}
\edef\newbox{%
    \expandafter\noexpand\csname et@xglob\endcsname
    4\box\noexpand\minim:boxchardef}

% undefine our helper functions
\let\alloc:makenew=\undefined

% 1 format file compatibility

% all other work is done at the lua end
\directlua { require ('minim-alloc') }
\directlua { require ('minim-callbacks') }

% restore lua modules (the macro will be defined from the lua end)
\toksapp\everyjob{\minim:restoremodules
    \message{... all done.}}

% 

\catcode`\:=\minimloaded
\endinput

