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

% 1 settings

\suppressoutererror = 1
\frenchspacing

% unicode space characters
\def\hairspace{\kern .1em }
\def\threeperemspace{\kern .33333em }
\def\fourperemspace {\kern .25em }
\catcode"00A0 = \active \letcharcode"00A0 = ~          % no-break
\catcode"2002 = \active \letcharcode"2002 = \enskip
\catcode"2003 = \active \letcharcode"2003 = \quad
\catcode"2004 = \active \letcharcode"2004 = \threeperemspace
\catcode"2005 = \active \letcharcode"2005 = \fourperemspace
\catcode"2006 = \active \letcharcode"2006 = \thinspace % six-per-em
\catcode"2008 = \active \letcharcode"2008 = \thinspace % punctuation
\catcode"2009 = \active \letcharcode"2009 = \thinspace % thin
\catcode"200A = \active \letcharcode"200A = \hairspace % hair
\catcode"202F = \active \letcharcode"202F = \thinspace % narrow no-break

% 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

% \ProvidesFile identifies most problematic files
\ifdefined \ProvidesFile
    \let\minim:providesfile=\ProvidesFile\else
    \def\minim:providesfile#1#2[#3]{%
        \wlog{Loading file #1 version #3.}}\fi
\def\ProvidesFile#1{\begincsname minim:intercept:#1\endcsname
    \minim:providesfile{#1}}

% repair ltluatex, which, without apparent reason, resets all allocation 
% counters, even if they already exist
\expandafter\def\csname minim:intercept:ltluatex.tex\endcsname
    {\wlog{minim: applying ltluatex patches.}%
     \edef\endinput{\let\noexpand\endinput=\noexpand\minim: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}%
        % restore register allocation functions
        \noexpand\directlua{ callback.register =
            require('minim-callbacks').primitiveregister }%
        \noexpand\endinput}}
\let\minim:endinput = \endinput
\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}

% 1 allocation

% used on the lua side by make_alloc_old
\newcount \alloc:globoffset
\alloc:globoffset = 260

\ifdefined \documentclass
    % latex has no \globcount etc.
    \alloc:globoffset = 10
    \ifnum\count10<256 \count10=300 \fi
    \ifnum\count11<256 \count11=300 \fi
    \ifnum\count12<256 \count12=300 \fi
    \ifnum\count14<256 \count14=300 \fi
\else \ifdefined \globcount
    % 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
\fi \fi

% new allocation macros

\def\alloc:makenew#1#2#3{%
    \unless\ifcsname#3\endcsname
        \expandafter\newcount \csname#3\endcsname
        \csname#3\endcsname 0\fi
    \unless\ifcsname new\csstring#1\endcsname
        \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}}

% 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}
% ltluatex initialises the catcode tables 1鈥�4, so we make sure not to claim 
% those ourselves:
\ifnum\csname e@alloc@ccodetable@count\endcsname = 0
    \csname e@alloc@ccodetable@count\endcsname = 4 \fi
% an empty table
\newcatcodetable \minim:initcatcodes

% 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}'}}

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

% patch in our callback to \uselanguage
%  \minim:uselanguagecallback is defined from Lua
\ifcsname uselanguage@hook\endcsname
    \expandafter\let
        \expandafter\minim:uselanguagehook
            \lastnamedcs \fi
\expandafter\edef\csname uselanguage@hook\endcsname#1{%
    % our uselanguage callback
    \noexpand\minim:uselanguagecallback{#1}%
    % previous definitions
    \ifdefined\minim:uselanguagehook
        \noexpand\minim:uselanguagehook{#1}\fi}

%1 hooks

% the pre_shipout callback
\newbox\minim:shipout:box \let\minim:shipout:orig = \shipout
\def\minim:shipout:do{\minim:shipout:orig\box\minim:shipout:box}
\protected\def\minim:shipout:new{\directlua{
    tex.box[\the\minim:shipout:box] = token.scan_list()
    require('minim-callbacks').call_callback('pre_shipout', \the\minim:shipout:box)
    token.put_next(token.create('minim:shipout:do'))}}
\let\shipout = \minim:shipout:new

% invisibly adding to \everypar
\let\minim:everypar = \everypar
\newtoks\minim:ateverypar
\newtoks\minim:atnextpar
\newtoks\everypar \everypar\minim:everypar
\minim:everypar\expandafter{\expandafter
    \minim:changeparindent
    % expansion makes this process repeatable
    \expandafter\the\everypar
    \the\minim:ateverypar
    \minim:atnextpar\expandafter
        {\expandafter}\the\minim:atnextpar}
\let\minim:changeparindent = \relax % as of yet unused

% There is presently no need for the following.

%\let\minim:par = \par
%\newtoks\minim:aftereverypar
%\protected\def\par{\ifhmode \unskip
%    \the\minim:aftereverypar \fi \minim:par}


% 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