% engine=luatex

\startenvironment luatexref-env

% something rests this, but i hav eno time not to see where

\unprotect \bgroup

    \ifx\normalsuber\undefined \def\normalsuber{_} \fi
    \ifx\normalsuper\undefined \def\normalsuper{^} \fi

    \catcode`_=\active
    \catcode`^=\active

    \gdef\nonknuthmode
      {\appendtoks\let_\normalsuber\let^\normalsuper\to\everymathematics
       \mathcode`_="8000
       \mathcode`^="8000
       \catcode`_=\@@other
       \catcode`^=\@@other
       }% \let\nonknuthmode\relax}

    \gdef\donknuthmode
      {\catcode`_=\@@subscript
       \catcode`^=\@@superscript}

\egroup \protect

\usemodule[abr-02]

% \tex    : tex command
% \lua    : native lua code
% \luatex : lua lib, function, value defined in luatex

\nonknuthmode

\setuptolerance[stretch,tolerant]

\setuptype[lines=hyphenated] \setuptyping[lines=hyphenated]

\let\lua    \type
\let\luatex \type

\def\|{\string|}
\def\>{\string>}

\def\showfields#1{\ctxlua
    {   do
            local t = string.split('#1',',')
            local r = { }
            for _,a in pairs(node.fields(t[1],t[2])) do
                if not (a == 'id' or a == 'subtype' or a =='next' or a=='prev') then
                    table.insert(r,'\\type{'.. a .. '}')
                end
            end
            tex.sprint(table.concat(r, ', '))
        end
    }%
}

\def\showid#1{\ctxlua
    {   do
            local t = string.split('#1',',')
            tex.sprint('\\type{'.. node.id(t[1]) .. '}')
            if t[2] then 
              tex.sprint(', \\type{'.. node.subtype(t[2]) .. '}')
            end
        end
    }%
}

\def\todo#1%
  {\dontleavehmode{\bf\red <TODO: #1>}}

\setuplayout
  [height=middle,
   width=middle,
   backspace=2cm,
   topspace=2cm]

\setuppagenumbering
  [alternative=doublesided]

\setupitemize[each][packed]

\setupwhitespace[medium]

\preloadtypescripts

\definetypeface[mainfacenormal]  [ss][sans] [iwona]       [default]
\definetypeface[mainfacenormal]  [rm][serif][palatino]    [default]
\definetypeface[mainfacenormal]  [tt][mono] [modern]      [default][rscale=1.1]
\definetypeface[mainfacenormal]  [mm][math] [iwona]       [default][encoding=default]

\definetypeface[mainfacemedium]  [ss][sans] [iwona-medium][default]
\definetypeface[mainfacenormal]  [rm][serif][palatino]    [default]
\definetypeface[mainfacemedium]  [tt][mono] [modern]      [default][rscale=1.1]
\definetypeface[mainfacemedium]  [mm][math] [iwona-medium][default][encoding=default]

\definetypeface[mainfacenarrowtt][tt][mono] [modern] [default][rscale=1.1]

\setupbodyfont[mainfacenormal,11pt]

\definehead
  [remark]
  [subsubsubject]

\setuphead [chapter]      [style=\mainfacemedium\bfd,color=blue]
\setuphead [section]      [style=\mainfacemedium\bfc,color=blue]
\setuphead [subsection]   [style=\mainfacemedium\bfb,color=blue]
\setuphead [subsubsection][style=\mainfacemedium\bfa,color=blue]

\setupheadertexts
  []

\setupcolors
  [state=start]

\setuptyping
  [color=blue,
]%   style=\mainfacenarrowtt]

\definetyping[functioncall]

\setuptype
  [color=blue,
]%   style=\mainfacenarrowtt]

\definecolor[blue] [b=.5]
\definecolor[red]  [r=.5]
\definecolor[green][g=.5]
\runMPgraphicstrue
\startuseMPgraphic{lualogo}
    color   luaplanetcolor ; luaplanetcolor := .5blue  ;
    color   luaholecolor   ; luaholecolor   :=   white ;
    numeric luaextraangle  ; luaextraangle  := 0 ;

    vardef lualogo = image (
        % Graphic design by A. Nakonechnyj. Copyright (c) 1998, All rights reserved.

        save luaorbitcolor, d, r, p ; color luaorbitcolor ; numeric d, r, p ;

        luaorbitcolor := .5luaholecolor ; d := sqrt(2)/4 ; r := 1/4 ; p := r/8 ;

        fill fullcircle scaled 1 withcolor luaplanetcolor ;
        draw fullcircle rotated 40.5 scaled (1+r) dashed evenly scaled p withpen pencircle scaled (p/2) withcolor luaorbitcolor ;
        fill fullcircle scaled r shifted (d+1/8,d+1/8) rotated luaextraangle withcolor luaplanetcolor ;
        fill fullcircle scaled r shifted (d-1/8,d-1/8) withcolor luaholecolor   ;
    )  enddef ;

\stopuseMPgraphic

\startuseMPgraphic{luapage}
    \includeMPgraphic{lualogo}

    StartPage ;

        fill Page withcolor (white-luaplanetcolor) ;

        picture p ; p := lualogo xsized (3PaperWidth/5) ;
        draw p shifted center Page shifted (0,-ypart center ulcorner p) ;

    StopPage ;
\stopuseMPgraphic

\def\luaextraangle{\directlua{
  if \lastpage == 0 then
    luaextraangle = 0;
  else
    luaextraangle = \realfolio*360/\lastpage;
  end
  tex.print(luaextraangle)
}}

\startuseMPgraphic{luanumber}
    \includeMPgraphic{lualogo}
    % luaextraangle := \realfolio*360/\lastpage ;
    % luaextraangle := 10 * ( (\realfolio-2)/(\lastpage-2)*36 ) ;
    luaextraangle := \luaextraangle;
    picture p ; p := lualogo ;
    setbounds p to boundingbox fullcircle ;
    draw p ysized 1cm ;
\stopuseMPgraphic

\definelayer
  [page]
  [width=\paperwidth,
   height=\paperheight]

\setupbackgrounds
  [leftpage]
  [background=page]

\setupbackgrounds
  [rightpage]
  [background=page]

\startsetups pagenumber:right
  \setlayerframed
    [page]
    [preset=rightbottom,offset=1cm]
    [frame=off,height=1cm,offset=overlay]
    {\useMPgraphic{luanumber}}
  \setlayerframed
    [page]
    [preset=rightbottom,offset=1cm,x=1.5cm]
    [frame=off,height=1cm,width=1cm,offset=overlay]
    {\pagenumber}
  \setlayerframed
    [page]
    [preset=rightbottom,offset=1cm,x=2.5cm]
    [frame=off,height=1cm,offset=overlay]
    {\getmarking[chapter]}% Lua\TeX\ Reference Manual}
\stopsetups

\startsetups pagenumber:left
  \setlayerframed
    [page]
    [preset=leftbottom,offset=1cm,x=2.5cm]
    [frame=off,height=1cm,offset=overlay]
    {\getmarking[chapter]}
  \setlayerframed
    [page]
    [preset=leftbottom,offset=1cm,x=1.5cm]
    [frame=off,height=1cm,width=1cm,offset=overlay]
    {\pagenumber}
  \setlayerframed
    [page]
    [preset=leftbottom,offset=1cm]
    [frame=off,height=1cm,offset=overlay]
    {\useMPgraphic{luanumber}}
\stopsetups

\startsetups [titlepage]

    \startstandardmakeup

        \switchtobodyfont[mainfacemedium]
        \definedfont[Bold at \the\dimexpr.08\paperheight\relax] \setupinterlinespace

        \setlayer[page]{\useMPgraphic{luapage}}

        \setlayerframed
          [page]
          [preset=middletop,
           voffset=.05\paperheight]
          [align=middle,
           foregroundcolor=blue,
           frame=off]
          {Lua\TeX\\Reference}

        \definedfont[Bold at 24pt] \setupinterlinespace

        \setlayerframed
          [page]
          [preset=middletop,
           voffset=.35\paperheight]
          [align=middle,
           foregroundcolor=blue,
           frame=off]
          {\doifsomething
               {\getvariable{document}{snapshot}}
               {snapshot \getvariable{document}{snapshot}}%
           \doifsomething
               {\getvariable{document}{beta}}
               {beta \getvariable{document}{beta}}%
            }

    \stopstandardmakeup

    \startstandardmakeup

        \raggedleft

        \definedfont[Bold at 48pt] \setupinterlinespace

        \start \blue Lua\TeX \endgraf Reference \endgraf Manual \stop

        \blank

        \definedfont[Bold at 24pt] \setupinterlinespace

        \blank

        \vfill

        \definedfont[Bold at 12pt] \setupinterlinespace

        copyright: Lua\TeX\ development team \endgraf
        more info: www.luatex.org \endgraf
        version: \currentdate \doifsomething{\getvariable{document}{snapshot}}{(snapshot \getvariable{document}{snapshot})} \endgraf

    \stopstandardmakeup

    \setupbackgrounds
      [leftpage]
      [setups=pagenumber:left]

    \setupbackgrounds
      [rightpage]
      [setups=pagenumber:right]

\stopsetups

\def\nonterminal#1>{\mathematics{\langle\hbox{{\rm #1}}\rangle}}

\ifx\directlua\undefined

  \definetyping[syntax]
  \definetype[syntax]

\else

    % taco's brainwave -)

    \newcatcodetable\syntaxcodetable

    \def\makesyntaxcodetable
      {\begingroup
       \catcode`\<=13 \catcode`\|=12
       \catcode`\!= 0 \catcode`\\=12
       \savecatcodetable\syntaxcodetable
       \endgroup}

    \makesyntaxcodetable

    \def\startsyntax {\begingroup\catcodetable\syntaxcodetable  \dostartsyntax}
    \def\syntax      {\begingroup\catcodetable\syntaxcodetable  \dosyntax}

    \def\syntaxenvbody#1%
      {\par
       \tt
       \startnarrower
       {\blue #1}%
       \stopnarrower
       \par}

    \def\syntaxbody#1%
      {{\blue \tt #1}}

    \bgroup \catcodetable\syntaxcodetable

    !gdef!dostartsyntax#1\stopsyntax{!let<!nonterminal!syntaxenvbody{#1}!endgroup}
    !gdef!dosyntax     #1{!let<!nonterminal!syntaxbody{#1}!endgroup}

    !egroup

\fi

\setupinteraction
  [state=start,
   focus=standard, 
   color=,
   contrastcolor=]

\placebookmarks
  [chapter,section,subsection]

\setuplist
  [chapter,section,subsection,subsubsection]
   [interaction=all]

% Hans doesn't like the bookmarks opening by default
% \setupinteractionscreen[option=bookmark]

\appendtoks\let\luatex\firstofoneargument\to\simplifiedcommands

\stopenvironment

