% engine=luatex

\startenvironment luatexref-env

\usemodule[abr-02]

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

% \nonknuthmode

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

\setuppagenumbering
  [alternative=doublesided]

\setuptolerance
  [stretch,tolerant]

\setuptype
  [lines=hyphenated]

\setuptyping
  [lines=hyphenated]

\setupitemize
  [each]
  [packed]

\setupwhitespace
  [medium]

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

\starttexdefinition unexpanded todo #1
    \dontleavehmode
    \startcolor[red]
        \bf<TODO: #1>
    \stopcolor
\stoptexdefinition


\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
  []

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

\definetyping
  [functioncall]

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

\definecolor[blue] [b=.5]
\definecolor[red]  [r=.5]
\definecolor[green][g=.5]

\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

\starttexdefinition luaextraangle
    % we can also just access the last page and so in mp directly
    \ctxlua {
        context(\lastpage == 0 and 0 or \realfolio*360/\lastpage)
    }
\stoptexdefinition

\startuseMPgraphic{luanumber}
    \includeMPgraphic{lualogo}
    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}}

% 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

% end of wave

\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 so we comment this:
%
% \setupinteractionscreen[option=bookmark]

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

\stopenvironment

