%D \module
%D   [       file=strc-blk,
%D        version=2008.10.20,
%D          title=\CONTEXT\ Structure Macros,
%D       subtitle=Blockmoves,
%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 Structure Macros / Blockmoves}

\registerctxluafile{strc-blk}{1.001}

\unprotect

% we run on top of buffers and sections
%
% todo: prefix numbers (needs further integration elsewhere)
%       check functionality
%       alternative files (needs further integration elsewhere)
%
% order matters: \c!before (think of: \c!before=\startitemize)
%
% no \endgroups

\installcorenamespace {block}
\installcorenamespace {blocktemp}

\installcommandhandler \??block {block} \??block

\appendtoks
    \ctxcommand{definestructureblock("\currentblock")}%
    \setuevalue{\e!begin\currentblock}{\dodoubleempty\strc_blocks_begin[\currentblock]}%
    \setuevalue{\e!end  \currentblock}{}%
\to \everydefineblock

\unexpanded\def\strc_blocks_begin[#1][#2]%
  {\normalexpanded{\buff_pickup{@block@}{\e!begin#1}{\e!end#1}}
     {}% before
     {\ctxcommand{savestructureblock("#1","#2","@block@")}}}% after

\let\strc_blocks_setup\relax

\unexpanded\def\dostarthiddenblock % called at lua end
  {\startnointerference
   \dostartnormalblock}

\unexpanded\def\dostophiddenblock % called at lua end
  {\dostopnormalblock
   \stopnointerference}

\unexpanded\def\dostartnormalblock#1% called at lua end
  {\bgroup
   \visibletrue  % will change
   \edef\currentblock{#1}%
   \strc_blocks_setup
   \let\strc_blocks_setup\relax
   \blockparameter\c!before
   \useblockstyleandcolor\c!style\c!color % maybe moev one line up (font spacing)
   \blockparameter\c!inner % better \c!setups
   \ignorespaces}

\unexpanded\def\dostopnormalblock % called at lua end
  {\removeunwantedspaces
   \blockparameter\c!after
   \par % todo: alternative = text, paragraph
   \egroup}

\def\strc_blocks_set_state[#1][#2][#3]% state name tag
  {\ctxcommand{setstructureblockstate("#1","#2","#3")}}

\def\strc_blocks_select[#1][#2][#3][#4]% state name tag setups
  {\bgroup
   \doifassignmentelse{#3}
     {\getparameters[\??blocktemp][\c!criterium=\v!text,#3]%
      \def\strc_blocks_setup{\setupcurrentblock[#3]}%
      \ctxcommand{selectstructureblock("#1","#2","","\csname\??blocktemp\c!criterium\endcsname")}}
     {\getparameters[\??blocktemp][\c!criterium=\v!text,#4]%
      \def\strc_blocks_setup{\setupcurrentblock[#4]}%
      \ctxcommand{selectstructureblock("#1","#2","#3","\csname\??blocktemp\c!criterium\endcsname")}}%
   \egroup}

% hide   : save, if [+] also hidden execute
% keep   : save and normal execute
% use    : normal execute unless [-]
% process: hidden execute unless [-]
% select : idem use

\unexpanded\def\hideblocks   {\dotripleempty   \strc_blocks_set_state[hide]}
\unexpanded\def\keepblocks   {\dotripleempty   \strc_blocks_set_state[keep]}
\unexpanded\def\useblocks    {\doquadrupleempty\strc_blocks_select   [use]}
\unexpanded\def\processblocks{\doquadrupleempty\strc_blocks_select   [process]}
\unexpanded\def\selectblocks {\doquadrupleempty\strc_blocks_select   [use]}

\protect \endinput
