%D \module
%D   [       file=strc-sbe,
%D        version=2008.10.20,
%D          title=\CONTEXT\ Structure Macros,
%D       subtitle=Section Block Environments,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright=PRAGMA-ADE / Hans Hagen]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.

\writestatus{loading}{ConTeXt Structure Macros / Section Block Environments}

\unprotect

% \def\ChapterEntry#1#2#3%
%   {chapter : \hbox to \hsize{\strut\bf#2\hss#3}\endgraf\placelist[section]}
%
% \startfrontmatter % optional
%   \placelist[chapter][alternative=command,command=\ChapterEntry,criterium=text] \page
% \stopfrontmatter  % optional
%
% \startbodymatter % optional
%   \chapter{first}  \section{one}   test \section{two}  test \page
%   \chapter{second} \section{alpha} test \section{beta} test \page
% \stopbodymatter  % optional

\definesystemvariable {nb}

\def\v!structureblockenvironment{structureblockenvironment}

\def\definestructureblock{\dotripleargument\dodefinestructureblock}
\def\setupstructureblock {\dodoubleargument\dosetupstructureblock}
\def\setstructureblock   {\dosingleargument\dosetstructureblock}

% \def\structureblockparameter#1{\executeifdefined{\??nb\currentstructureblock#1}\empty}

\def\structureblockparameter#1%
  {\csname
     \ifcsname\??nb\currentstructureblock#1\endcsname\??nb\currentstructureblock#1\else\s!empty\fi
   \endcsname}

\newtoks \everybeforestructureblock
\newtoks \everyafterstructureblock

\def\dodefinestructureblock[#1][#2][#3]% singular plural settings
  {\getparameters
     [\??nb#1]
     [\c!number=\v!yes,
      \c!page=\v!right, % anders worden marks te vroeg gereset !
      #3]%
   \expandafter\newif\csname if#2\endcsname % better a mode
   \setstructureblockenvironment{#1}\empty
   \setvalue {\e!start#2}{\startstructureblock[#1]}%
   \setvalue {\e!stop #2}{\stopstructureblock}}

\appendtoks
    \doifsomething{\structureblockparameter\c!page}{\page[\structureblockparameter\c!page]}%
% TODO    \resetsectionmarks\zerosection
    \getstructureblockenvironment\currentstructureblock
    \structureblockparameter\c!before % don't move
\to \everybeforestructureblock

\appendtoks
    \structureblockparameter\c!after % don't move
    \doifsomething{\structureblockparameter\c!page}{\page[\structureblockparameter\c!page]}%
% TODO    \resetsectionmarks\zerosection
\to \everyafterstructureblock

\def\dosetupstructureblock[#1]%
  {\getparameters[\??nb#1]}% [#2]

\def\dosetstructureblock[#1]% used to set the default
  {\edef\currentstructureblock{\ctxlua{structure.sections.setblock("#1")}}}

\let\currentstructureblock\s!unknown

\def\startstructureblock[#1]%
  {\begingroup
   \edef\currentstructureblock{\ctxlua{structure.sections.pushblock("#1")}}%
   \csname #1true\endcsname % for old times sake
   \setsystemmode\currentstructureblock
   \the\everybeforestructureblock\relax
   \showmessage\m!structures1\currentstructureblock}

\def\stopstructureblock
 {\showmessage\m!structures2\currentstructureblock
  \the\everyafterstructureblock\relax
  \edef\currentstructureblock{\ctxlua{structure.sections.popblock()}}%
  \endgroup}

\long\def\setstructureblockenvironment#1#2%
  {\long\setvalue{\??nb\s!do#1}{\do{#2}}}

\def\getstructureblockenvironment#1%
  {\let\do\firstofoneargument
  %\structureblockparameter{\s!do#1}}
   \csname\??nb\s!do#1\endcsname}

%D \starttyping
%D \startsectionblockenvironment[frontpart]
%D   \setuppagenumbering[conversion=romannumerals]
%D \stopsectionblockenvironment
%D
%D \startsectionblockenvironment[bodypart]
%D   \setuppagenumber[number=1]
%D \stopsectionblockenvironment
%D
%D \startsectionblockenvironment[frontpart]
%D   \setuppagenumbering[conversion=character]
%D \stopsectionblockenvironment
%D
%D \starttext
%D   \startfrontmatter \chapter{test} \stopfrontmatter
%D   \startbodymatter  \chapter{test} \stopbodymatter
%D   \startappendices  \chapter{test} \stopappendices
%D \stoptext
%D \stoptyping

\setvalue{\e!start\v!structureblockenvironment}%
  {\dosingleargument\dostartstructureblockenvironment}

\def\dostartstructureblockenvironment[#1]% evt \pushendofline \popendofline
  {\long\def\do##1##2{\setstructureblockenvironment{#1}{##1##2}}%
  %\grabuntil{\e!stop\v!structureblockenvironment}{\structureblockparameter{\s!do#1}}}
   \grabuntil{\e!stop\v!structureblockenvironment}{\csname\??nb\s!do#1\endcsname}}

% this will become: (we ran in parallel for a while during transition)

\setvalue{\e!start\v!sectionblockenvironment}%
  {\dosingleargument\dostartsectionblockenvironment}

\def\dostartsectionblockenvironment[#1]% evt \pushendofline \popendofline
  {\long\def\do##1##2{\setstructureblockenvironment{#1}{##1##2}}%
  %\grabuntil{\e!stop\v!sectionblockenvironment}{\structureblockparameter{\s!do#1}}}
   \grabuntil{\e!stop\v!sectionblockenvironment}{\csname\??nb\s!do#1\endcsname}}

\protect \endinput
