% macros=mkvi
%% Copyright (C) 2009-2014
%%
%%      by  Elie Roux      <elie.roux@telecom-bretagne.eu>
%%      and Khaled Hosny   <khaledhosny@eglug.org>
%%      and Philipp Gesang <philipp.gesang@alumni.uni-heidelberg.de>
%%
%% This file is part of Luaotfload.
%%
%%      Home:      https://github.com/lualatex/luaotfload
%%      Support:   <lualatex-dev@tug.org>.
%%
%% Luaotfload is under the GPL v2.0 (exactly) license.
%%
%% ----------------------------------------------------------------------------
%%
%% Luaotfload is free software; you can redistribute it and/or
%% modify it under the terms of the GNU General Public License
%% as published by the Free Software Foundation; version 2
%% of the License.
%%
%% Luaotfload is distributed in the hope that it will be useful,
%% but WITHOUT ANY WARRANTY; without even the implied warranty of
%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
%% GNU General Public License for more details.
%%
%% You should have received a copy of the GNU General Public License
%% along with Luaotfload; if not, see <http://www.gnu.org/licenses/>.
%%
%% ----------------------------------------------------------------------------
%%

\unprotect

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% layout and paper
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\setuppapersize [A5] [A5] %% 148×210

\definelayout [mainlayout] [
  backspace=15mm, %% 133
  textwidth=103mm,
  topspace=15mm,
]

\setuplayout [mainlayout]

\setuppagenumbering [location=,alternative=doublesided]

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% colors
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\usecolors [x11]
\definecolor [primarycolor]   [dodgerblue4]
\definecolor [secondarycolor] [goldenrod4]

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% interaction
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\setupinteraction [
  state=start,
  page=no,
  click=yes,
  style=italic,
  color=primarycolor,
  contrastcolor=secondarycolor,
  title={The Luaotfload package},
  subtitle={OpenType layout system for Plain TeX and LaTeX},
  author={Elie Roux & Khaled Hosny & Philipp Gesang},
  keywords={luatex, lualatex, unicode, opentype},
]

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% fonts
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\usemodule [simplefonts]

\definefontfeature [default]   [default] [mode=base,liga=yes,dlig=yes,tlig=yes,onum=yes]
\definefontfeature [monospace]           [liga=no,tlig=no,onum=no]

\definefontfamily [mainface] [serif] [Linux Libertine O] [features=default]
%definefontfamily [mainface] [serif] [Liberation Serif] [feature=default]
%definefontfamily [mainface] [sans]  [Iwona]             [feature=default]
\definefontfamily [mainface] [sans]  [Iwona Medium] [
  feature=default,
  it=file:IwonaMedium-Italic.otf,
  tf=file:IwonaMedium-Regular.otf,
  bf=file:Iwona-Bold.otf,
  bi=file:Iwona-BoldItalic.otf,
]
%definefontfamily [mainface] [sans]  [DejaVu Sans]       [feature=default]
\definefontfamily [mainface] [mono]  [Liberation Mono]   [scale=0.85,features=monospace]

\setupbodyfont [mainface,10pt]

\def \LUA      {Lua}
\def \LUALATEX {Lua\LATEX}
\def \OpenType {\identifier{Open\kern-.25ex Type}}

\definealternativestyle [emphasis:texmacro]        [\ss \it \letterbackslash]       [\ss \it \letterbackslash]
\definealternativestyle [emphasis:identifier]      [\ss]                            [\ss]
\definealternativestyle [emphasis:normal]          [\sl]                            [\sl]
\definealternativestyle [emphasis:abbrev]          [{\feature [+][smallcaps]}]      [{\feature [+][smallcaps]}]
\definealternativestyle [emphasis:Largefont]       [{\switchtobodyfont[14pt]}]      [{\switchtobodyfont[14pt]}]
\definealternativestyle [emphasis:smallcaps]       [{\feature [+][smallcaps]}]      [{\feature [+][smallcaps]}]
%definealternativestyle [emphasis:nonproportional] [\mono]                          [\mono]
\definealternativestyle [emphasis:nonproportional] [\tt]                            [\tt]
\definealternativestyle [head:section]             [{\roman\feature[+][smallcaps]}] [{\roman\feature[+][smallcaps]}]
\definealternativestyle [head:subsection]          [{\roman\feature[+][smallcaps]}] [{\roman\feature[+][smallcaps]}]
\definealternativestyle [head:subsubsection]       [{\roman\feature[+][smallcaps]}] [{\roman\feature[+][smallcaps]}]
\definealternativestyle [typing:luafunction]       [\italic]                        [\italic]
\definealternativestyle [typing:fileent]           [\tt]                            [\tt]

\definehighlight        [texmacro] [style=emphasis:texmacro]        %% cs
\definehighlight      [identifier] [style=emphasis:identifier]      %% names
\definehighlight          [abbrev] [style=emphasis:abbrev]          %% acronyms
\definehighlight        [emphasis] [style=emphasis:normal]          %% level 1 emph

\definehighlight       [Largefont] [style=emphasis:Largefont]       %% font size
\definehighlight       [smallcaps] [style=emphasis:smallcaps]       %% font feature
\definehighlight [nonproportional] [style=emphasis:nonproportional] %% font switch

\definetype [fileent]     [style=typing:fileent]
\definetype [luafunction] [style=typing:luafunction]
\setuptyping [style=ttx]

\definebodyfontenvironment  [8pt]
\definebodyfontenvironment [10pt]
\definebodyfontenvironment [12pt]

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% headings
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\setuphead [section]       [style=head:section,      alternative=inmargin]
\setuphead [subsection]    [style=head:subsection,   alternative=inmargin]
\setuphead [subsubsection] [style=head:subsubsection,alternative=inmargin]

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% running headers
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\setupheadertexts
  [{\tfx \getmarking[section]}] [pagenumber]
  [pagenumber] [{\tfx \fileent{Luaotfload} Manual}]

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% structurals
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%% section
\def \beginsection {\dosingleempty \section_begin_indeed}

\def \section_begin_indeed [#ref]#title{%
  \iffirstargument
    \startsection [reference=#ref,title=#title]%
  \else
    \startsection [title=#title]%
  \fi
}

\let \endsection \stopsection

%% subsection
\def \beginsubsection {\dosingleempty \section_begin_indeed}

\def \subsection_begin_indeed [#ref]#title{%
  \iffirstargument
    \startsubsection [reference=#ref,title=#title]%
  \else
    \startsubsection [title=#title]%
  \fi
}

\let \endsubsection \stopsection

%% subsubsection
\def \beginsubsubsection {\dosingleempty \section_begin_indeed}

\def \subsubsection_begin_indeed [#ref]#title{%
  \iffirstargument
    \startsubsubsection [reference=#ref,title=#title]%
  \else
    \startsubsubsection [title=#title]%
  \fi
}

\let \endsubsubsection \stopsection

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% inline verbatim
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%% Context offers both \type{…} and \type<<…>>, but not an unbalanced
%% one that we could map directly onto Latex’s \verb|…|.

\definetype [inlinecode_indeed] [style=emphasis:nonproportional]

%% The listings macros don’t seem to handle backslashes and braces
%% well. We emulate this behavior by handling the escaping in Lua.

\startluacode
  local lpeg           = require "lpeg"
  local Cs, P, S       = lpeg.Cs, lpeg.P, lpeg.S
  local lpegmatch      = lpeg.match
  local unescape_char  = S[[\letterbackslash\letterleftbrace\letterrightbrace]]
  local backslash      = P[[\letterbackslash]]
  local unescape       = Cs (((backslash / "" * unescape_char) + 1)^0)
  commands.unescape_things = function (str)
    context.type (lpegmatch (unescape, str))
  end
\stopluacode

\unexpanded \def \inlinecode #content{%
  \ctxcommand {unescape_things \!!bs \detokenize {#content}\!!es}%
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% codelistings
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Now *that’s* what I call easy.

\unexpanded \def \beginlisting {%
  \grabbufferdatadirect{listing}{beginlisting}{endlisting}%
}

\unexpanded \def \endlisting {\typebuffer [listing]}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% enumerations and lists
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\definedescription [descriptionitem] [
  align=right,
  alternative=hanging,
  width=2em,
]

\def \begindescriptions {%
  \begingroup
    \def \beginnormalitem ##1\endnormalitem{%
      \startitem##1\stopitem
    }
    \let \endnormalitem   \relax
    \let \beginaltitem    \startdescriptionitem
    \let \endaltitem      \stopdescriptionitem
}

\let \enddescriptions \endgroup


\definedescription [definitionitem] [
  align=right,
  alternative=hanging,
]

\def \begindefinitions {%
  \begingroup
    \def \beginnormalitem ##1\endnormalitem{%
      \startitem##1\stopitem
    }
    \let \endnormalitem   \relax
    \let \beginaltitem    \startdefinitionitem
    \let \endaltitem      \stopdefinitionitem
}

\let \enddefinitions \endgroup


\definedescription [filelistitem] [
  align=normal,
  alternative=hanging,
  headstyle=typing:fileent,
  width=4cm,
]

\def \beginfilelist {%
  \begingroup
    \def \beginnormalitem ##1\endnormalitem{%
      \startitem##1\stopitem
    }
    \let \endnormalitem   \relax
    \let \beginaltitem    \startfilelistitem
    \let \endaltitem      \stopfilelistitem
}

\let \endfilelist \endgroup

\definedescription [functionlistitem] [
  align=normal,
  alternative=hanging,
  headstyle=typing:luafunction,
  width=4cm,
]

\def \beginfunctionlist {%
  \begingroup
    \def \beginnormalitem ##1\endnormalitem{%
      \startitem##1\stopitem
    }
    \let \endnormalitem   \relax
    \let \beginaltitem    \startfunctionlistitem
    \let \endaltitem      \stopfunctionlistitem
}

\let \endfunctionlist \endgroup

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% columns
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\def \begindoublecolumns {\startcolumns [2]}
\let \enddoublecolumns   \stopcolumns

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% alignment
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\setupnarrower [before={\blank[line]},after={\blank[line]}]
\let \beginnarrower \startnarrower
\let \endnarrower   \stopnarrower


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% special elements
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\definefont [lmromantenregular] [file:lmroman10-regular.otf*default]

\def \meta #1{%
  {\lmromantenregular<}%
  {\italic #1}%
  {\lmromantenregular>}%
}

\def \beginabstractcontent {%
  \grabbufferdatadirect{abstractcontent}{beginabstractcontent}{endabstractcontent}%
}

\let \endabstractcontent \relax

\def \setdocumenttitle   #1{\setvalue  {document_title}{#1}}
\def \setdocumentdate    #1{\setvalue   {document_date}{#1}}
\def \setdocumentauthor  #1{\setvalue {document_author}{#1}}

\let \typesetdocumenttitle \relax
\let \beginfrontmatter \relax

\def \endfrontmatter {
  \startstandardmakeup
    \vfill
    \strut \hfill
      \startframed [frame=off,align=middle,width=.5\textwidth]
        \Largefont{\getvalue {document_title}}
      \stopframed
    \hfill \strut \par

    \blank [2*big]

    \strut \hfill
      \startframed [frame=off,align=middle,width=.65\textwidth]
        \setuplocalinterlinespace [18pt]
        \getvalue {document_author}
      \stopframed
    \hfill \strut \par

    \vfill
    \strut \hfill \getvalue {document_date} \hfill \strut
    \blank [2*big]

    \strut \hfill
    \startframed [width=.7\textwidth,align=normal,style=tfx,frame=off]%
      \getbuffer [abstractcontent]
    \stopframed
    \hfill \strut 
  \stopstandardmakeup
}

\let \typesetcontent \completecontent

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% floats
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% XXX we can improve on this part later

\usemodule [vim]
\definevimtyping [bnf] [syntax=bnf]
\definefloat [syntax] [figure]

\def \beginsyntaxfloat #reference#caption{%
  \begingroup
    \edef \currentreference {#reference}%
    \edef \currentcaption   {#caption}%
    \grabbufferdatadirect{rawsyntaxdata}{beginsyntaxfloat}{endsyntaxfloat}%
}

\def \endsyntaxfloat {%
    \savebuffer [rawsyntaxdata] [rawsyntaxdata]
    \startplacesyntax [
      reference=\currentreference,
      title={\currentcaption},
    ]
      %% there’s no \typebnfbuffer in t-vim :(
      \typebnffile {\jobname-rawsyntaxdata.tmp}
    \stopplacesyntax
  \endgroup%
}

\def \figurefloat #reference#caption#file{%
  \startplacefigure [
    reference=#reference,
    title={#caption},
  ]
    \externalfigure [#file] [width=\textwidth]
  \stopplacefigure
}


\def \tablefloat #reference#caption#content{%
  \startplacetable [
    reference=#reference,
    title={#caption},
  ]
    #content
  \stopplacetable
}


%% tables


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% tables
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\setupxtable [frame=off,option=stretch,textwidth=\dimexpr(\textwidth/2)]

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% hyperlinks and references
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\unexpanded \def \hyperlink{%
  \dosingleempty \hyperlink_indeed%
}

\def \hyperlink_indeed [#text]#url{%
  \iffirstargument
    \useURL [temporary_url] [#url] [] [#text]%
  \else
    \useURL [temporary_url] [#url]%
  \fi%
  \from [temporary_url]%
}


\def \email #1{\goto{#1}[url(mailto:#1)]}

\def \label #tag{\reference [#tag]\empty}
\def \pageref #tag{\at{page}{#tag}}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% escaped characters
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\let \charpercent   \letterpercent
\let \charbackslash \letterbackslash
\let \chartilde     \lettertilde

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% main
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\protect

\newif \ifcontextmkiv \contextmkivtrue

\starttext
  \input luaotfload-main.tex
\stoptext