%D \module
%D   [       file=buff-par,
%D        version=2010.12.05,
%D          title=\CONTEXT\ Buffer Macros,
%D       subtitle=Parallel,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
%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 Buffer Macros / Parallel}

\registerctxluafile{buff-par}{1.001}

%D This module is developped for Thomas Schmitz as part of a project. There is
%D no documentation yet.
%D
%D \starttyping
%D \defineparallel[main][one,two]
%D
%D \startmain
%D \startone
%D     first 1
%D     [reference] first 2
%D     first 3
%D \stopone
%D \starttwo
%D     second 1
%D \stoptwo
%D \stopmain
%D
%D \placeparallel[main][one,two][criterium=all]

%D criterium=all start=<number> n=<number>

\unprotect

\installcorenamespace{parallel}

\installcommandhandler \??parallel {parallel} \??parallel

\setupparallel
  [\c!setups=parallel:place:default]

\let\buff_parallel_define_saved\defineparallel

\unexpanded\def\defineparallel
  {\dodoubleargument\buff_parallel_define}

\def\buff_parallel_define[#name][#instances]%
  {\buff_parallel_define_saved[#name]
   \ctxcommand{defineparallel("#name","#instances")}%
   \processcommalist[#instances]\buff_parallel_define_instance
   \setuevalue{\e!start#name}{\buff_parallel_start{#name}}%
   \setuevalue{\e!stop #name}{\buff_parallel_stop}}

\def\buff_parallel_define_instance#instance%
  {\normalexpanded{\buff_parallel_define_saved[#instance][\currentparallel]}%
   \definebuffer[#instance]%
   \setuevalue{\e!stop#instance}{\buff_parallel_save{#instance}}}

\unexpanded\def\buff_parallel_start#name%
  {\pushmacro\currentparallel
   \edef\currentparallel{#name}%
   \ctxcommand{nextparallel("\currentparallel")}}

\unexpanded\def\buff_parallel_stop
  {\popmacro\currentparallel}

\unexpanded\def\buff_parallel_save#instance% defined moet ook aan de lua kant kunnen
  {\ctxcommand{saveparallel("\currentparallel","#instance",buffers.raw("\thedefinedbuffer{#instance}"))}}

\unexpanded\def\placeparallel
  {\dotripleempty\buff_parallel_place}

\def\buff_parallel_place[#name][#instance][#settings]%
  {\begingroup
   \edef\currentparallel{#name}%
   \ctxcommand{placeparallel("\currentparallel","#instance","#settings")}% -- todo: pass options as k/v
   \endgroup}

\def\doflushparallel#instance#status#line#label#content% called at lua end
  {\begingroup
   \def\currentparallelinstance{#instance}%
   \def\currentparallelnumber  {#status}%
   \def\currentparallelline    {#line}%
   \def\currentparallellabel   {#label}%
   \def\currentparallelcontent {#content}%
   \ifcase#status\relax
     \expandafter\buff_parallel_flush_nop
   \or
     \expandafter\buff_parallel_flush_yes
   \fi
   \endgroup}

\def\buff_parallel_flush_nop
  {}

\def\buff_parallel_flush_yes
  {\directsetup{\namedparallelparameter\currentparallelinstance\c!setups}}

\unexpanded\def\doifelseparallel#name#instance%
  {\ctxcommand{doifelseparallel("#name","#instance")}}

\unexpanded\def\resetparallel
  {\dodoubleempty\buff_parallel_reset}

\def\buff_parallel_reset[#name][#instance]%
  {\ctxcommand{resetparallel("#name","#instance"))}}

\startsetups parallel:place:default
    \hangafter\plusone
    \hangindent4em
    \dontleavehmode
    \hbox to 3em \bgroup
        \hss
        \bf
        \doifsomething \currentparallellabel {
            \textreference[\currentparallellabel]{\currentparallelline}
        }
    \currentparallelline
    \quad
    \egroup
    \currentparallelcontent
    \par
\stopsetups

\protect \endinput
