%D \module
%D   [       file=strc-swd,
%D        version=2007.08.14,
%D          title=\CONTEXT\ Structure Macros,
%D       subtitle=Section Worlds,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright={PRAGMA / Hans Hagen \& Ton Otten}]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.

%D This is a prelude to a rewrite of sectioning.

% options : before after setups page text number label bookmark
% dodo    : listtext
%
% \startsectionworld[chapter][text={Test}]
% \stopsectionworld

\writestatus{loading}{ConTeXt Structure Macros / Sectionworlds}

\unprotect

\ifx\pushvalue\undefined

    \def\pushvalue#1{\expandafter\pushmacro\csname#1\endcsname}
    \def\popvalue #1{\expandafter\popmacro \csname#1\endcsname}

\fi

% brrr

\def\csnameexpanded#1#2%
  {\@EA\@EA\@EA\@EA\@EA\@EA\@EA#1\@EA\@EA\@EA\@EA\@EA\@EA\@EA#2\@EA\@EA\@EA\@EA\@EA\@EA\@EA}

\def\csnameexpandedoneargument#1#2%
  {\csnameexpanded\def\csexpandeda{#2}%
   \@EA#1\@EA{\csexpandeda}}

\def\csnameexpandedtwoarguments#1#2#3%
  {\csnameexpanded\def\csexpandeda{#2}%
   \csnameexpanded\def\csexpandedb{#3}%
   \@EA\@EA\@EA#1\@EA\@EA\@EA{\@EA\csexpandeda\@EA}\@EA{\csexpandedb}}

\def\csnameexpandedthreearguments#1#2#3#4%
  {\csnameexpanded\def\csexpandeda{#2}%
   \csnameexpanded\def\csexpandedb{#3}%
   \csnameexpanded\def\csexpandedc{#4}%
   \@EA\@EA\@EA\@EA\@EA\@EA\@EA#1\@EA\@EA\@EA\@EA\@EA\@EA\@EA{\@EA\@EA\@EA\csexpandeda\@EA\@EA\@EA}\@EA\@EA\@EA{\@EA\csexpandedb\@EA}\@EA{\csexpandedc}}

% \def\xx{XX}\setvalue{xx:yy}{abc \xx def}\def\param#1{\csname xx:#1\endcsname}
% \def\testa    #1{\defconvertedargument\ascii{#1}{\tttf\ascii}}
% \def\testb  #1#2{\defconvertedargument\ascii{#1 #2}{\tttf\ascii}}
% \def\testc#1#2#3{\defconvertedargument\ascii{#1 #2 #3}{\tttf\ascii}}
% \noindent 1 \csnameexpandedoneargument   \testa{\param{yy}}
% \noindent 2 \csnameexpandedtwoarguments  \testb{\param{yy}}{\param{yy}}
% \noindent 3 \csnameexpandedthreearguments\testc{\param{yy}}{\param{yy}}{\param{yy}}

% rewrite the sectioning to use the variables, a bit tricky because then
% we need a special expansion trick

\def\sectionworldparameter    #1{\csname\??sw\currentsectionworldname#1\endcsname}
\def\pushsectionworldparameter#1{\expandafter\pushmacro\csname\??sw\currentsectionworldname#1\endcsname}
\def\popsectionworldparameter #1{\expandafter\popmacro \csname\??sw\currentsectionworldname#1\endcsname}

\def\setupsectionworld
  {\dodoubleargument\dosetupsectionworld}

\def\dosetupsectionworld[#1][#2]%
  {\getparameters[\??sw#1][#2]} % maybe some extra things

\def\dochecksectionworld#1%
  {\ifcsname\??sw\currentsectionworldname\endcsname\else
     \getparameters
       [\??sw\currentsectionworldname]
       [\c!before=,
        \c!after=,
        \c!setups=,
        \c!page=]%
   \fi}

\def\startsectionworld
  {\dodoubleargument\dostartsectionworld}

\def\dostartsectionworld[#1][#2]%
  {\pushmacro\currentsectionworldname
   \def\currentsectionworldname{#1}%
   \dochecksectionworld\currentsectionworldname
   \pushsectionworldparameter\c!before
   \pushsectionworldparameter\c!after
   \pushsectionworldparameter\c!setups
   \getparameters
     [\??sw\currentsectionworldname]
     [\c!text=,\c!number=\finalsectionnumber,\c!label=,\c!bookmark=,#2]%
   \doifsomething{\sectionworldparameter\c!page}
     {\setsectieenkoppeling\currentsectionworldname
     %\handlepagebreak\currentsectionworldname
      \checknexthead\handlepagebreak\currentsectionworldname}%
   \sectionworldparameter\c!before
   \begingroup
   \doifsomething{\sectionworldparameter\c!page}
     {\settrue\ignorehandlepagebreak}%
   \doifsomething{\sectionworldparameter\c!setups}
     {\setups[\sectionworldparameter\c!setups]}%
   \csnameexpandedthreearguments \doconstructheadwithvars
     {\sectionworldparameter\c!label }%
     {\sectionworldparameter\c!number}%
     {\sectionworldparameter\c!text  }%
   \doifsomething{\sectionworldparameter\c!bookmark}
     {\expanded{\bookmark[\sectionworldparameter\c!bookmark]}}%
   \ignorespaces} % for inline heads

\def\doconstructheadwithvars#1%#2#3%
  {\dodododoconstructhead\currentsectionworldname[#1]}% {#2}{#3}}

\def\stopsectionworld
  {\endgraf
   \endgroup
   \sectionworldparameter\c!after
   \popsectionworldparameter\c!setups
   \popsectionworldparameter\c!after
   \popsectionworldparameter\c!before
   \popmacro\currentsectionworldname}

\protect \endinput
