% Paper dimensions (http://www.pragma-ade.com/general/manuals/cont-enp.pdf, 3.2)
\setuppapersize[A4][A4] % defaults, likely redundant

% Page layout/margins, doublesided, header/footer
% (http://www.pragma-ade.com/general/manuals/cont-enp.pdf, 3.3)
\setuplayout[
  backspace=1in,
  leftmargin=.5in,
  leftmargindistance=0in,
  width=fit,
  rightmargindistance=0in,
  rightmargin=.5in,
  cutspace=.5in,
  topspace=.5in,
  header=.2in,
  height=fit,
  footer=.2in,
  bottomspace=.5in,
  ]
\setupheadertexts [alfa][beta][gamma][delta]
\setuppagenumbering[alternative=doublesided]

% Enable hyperlinking (wiki)
\setupinteraction[state=start]


% The use of startsectionblockenvironment appears critical, yet not documented.
% The following links were helpful, the rest took guessing.
% http://www.opensubscriber.com/message/ntg-context@ntg.nl/9815615.html
% http://wiki.contextgarden.net/Biochemistry_textbook
\startsectionblockenvironment[frontpart]
\setuppagenumbering[location={header,right}]
\setuppagenumbering[conversion=romannumerals]
\stopsectionblockenvironment

\startsectionblockenvironment[bodypart]
\setuppagenumbering[location={header,right}]
% (http://www.pragma-ade.com/general/manuals/cont-enp.pdf, page 76)
% This was *not obvious*. The fact that 'bytext' and 'bypart' are listed, and
% that 'by\em{section}' implies them both *in addition* to other things like
% chapter took a bit undertand.
\setuppagenumbering[way=bychapter]
% (http://www.pragma-ade.com/general/manuals/cont-enp.pdf, page 76)
% This was also *not obvious*. I believe it is an error to list it as
% \em{sectionnumber}. Rather, for consistency it should be \em{section}number.
% However, the description on http://texshow.contextgarden.net clarifies it.
\setuppagenumbering[chapternumber=yes]
\stopsectionblockenvironment

%\startsectionblockenvironment[backpart]
%\setuppagenumbering[location=]
%\setupheadertexts[][][][]
%\stopsectionblockenvironment

%\startsectionblockenvironment[appendix]
%\setuppagenumbering[location=]
%\setupheadertexts[][][][]
%\stopsectionblockenvironment

%\setupsectionblock[backpart][page=no]
%\setupsectionblock[appendix][page=no]

% This was not clear. http://texshow.contextgarden.net does not have italics,
% and so 'chapternumber' isn't obvious as an option. The same issue mentioned
% above with \em{sectionnumber} exists on 
% (http://www.pragma-ade.com/general/manuals/cont-enp.pdf, page 150)
\setupcombinedlist[content][chapternumber=yes,interaction=all]

% Getting rid of the 'blank' but 'headered' pages at the end of sections was
% not obvious. The (partial) answer is at
% http://www.opensubscriber.com/message/ntg-context@ntg.nl/4154001.html, once
% the typo is fixed (Mychapterpagebreak vs. mychapterpagebreak). There is also
% http://wiki.contextgarden.net/Titles#Truly_empty_pagebreak_before_chapters, which
% fixes the typo. However, 'definepagebreak' is not documented. Neither is the
% particular use of 'setuphead'.
% Also note that this does not appear to work for the last chapter (which still
% gets a non-empty last page with no text. 
\definepagebreak 
   [mychapterpagebreak] 
   [yes,header,right] 

\setuphead 
  [chapter] 
  [page=mychapterpagebreak] 


% DEBUGGING
\showframe

\starttext

\startfrontmatter
\completecontent[criterium=all]
\stopfrontmatter

\startbodymatter
\dorecurse{4}
{\chapter{Chapter}
\dorecurse{12}
{\section{Section}
  \input knuth \endgraf}}
\stopbodymatter

% This is required to avoid a strange page number on the last page. I remember
% a mail posting on this, but cannot find the reference now.
\page[empty]
\stoptext
