%D \module
%D   [       file=strc-ind, % was part of strc-des,
%D        version=2008.10.20,
%D          title=\CONTEXT\ Structure Macros,
%D       subtitle=Indented Text,
%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 / Indented Text}

\unprotect

%D \macros
%D   {defineindentedtext,
%D    setupindentedtext}
%D
%D Ok, we keep it but with a different command as it all looks too
%D much like indentation and indenting. We also assume start/stop
%D usage or some explicit par.

\installcorenamespace{indentedtext}

\installcommandhandler \??indentedtext {indentedtext} \??indentedtext

%D \startbuffer
%D \defineindentedtext[one][text=one]
%D \defineindentedtext[two][text=two]
%D
%D \one test test \par
%D \subone test test \par
%D \subtwo test test \par
%D \subsubone test test \par
%D
%D \startone
%D     test test
%D     \startone
%D         test test
%D         \startone
%D             test test
%D         \stopone
%D         \starttwo
%D             test test
%D             \startone
%D                 test test
%D             \stopone
%D         \stoptwo
%D     \stopone
%D \stopone
%D \stopbuffer
%D
%D \typebuffer \getbuffer

\newinteger  \c_strc_indentedtexts_nesting
\newdimension\d_strc_indentedtexts_width
\newdimension\d_strc_indentedtexts_distance

\appendtoks
   \protected\frozen\instance\edefcsname\e!start\currentindentedtext\endcsname{\strc_indentedtexts_start{\currentindentedtext}\c_strc_indentedtexts_nesting}%
   \protected\frozen\instance\edefcsname\e!stop \currentindentedtext\endcsname{\strc_indentedtexts_stop}%
   % to be avoided ... might go away
   \protected\frozen\instance\edefcsname            \currentindentedtext\endcsname{\strc_indentedtexts_direct{\currentindentedtext}{0}}%
   \protected\frozen\instance\edefcsname      \v!sub\currentindentedtext\endcsname{\strc_indentedtexts_direct{\currentindentedtext}{1}}%
   \protected\frozen\instance\edefcsname\v!sub\v!sub\currentindentedtext\endcsname{\strc_indentedtexts_direct{\currentindentedtext}{2}}%
\to \everydefineindentedtext

\protected\def\strc_indentedtexts_start#1#2% we need to get rid of \spr
  {\par
   \begingroup
   \cdef\currentindentedtext{#1}%
   \c_strc_indentedtexts_nesting#2\relax
   \indentedtextparameter\c!before
   \d_strc_indentedtexts_distance{\indentedtextparameter\c!distance}%
   \ifempty{\indentedtextparameter\c!sample}%
     \setindentedtextparameter\c!sample{\indentedtextparameter\c!text}%
   \fi
   \assignwidth
     {\indentedtextparameter\c!width}%
     {\d_strc_indentedtexts_width}%
     {\useindentedtextstyleandcolor\c!headstyle\c!headcolor
      \indentedtextparameter\c!sample
      \spr{\indentedtextparameter\c!separator}}%
     {\d_strc_indentedtexts_distance}%
   \advanceby\d_strc_indentedtexts_width \d_strc_indentedtexts_distance
   \setbox\scratchbox\hbox to \d_strc_indentedtexts_width
     {\useindentedtextstyleandcolor\c!headstyle\c!headcolor
      \strut
      \indentedtextparameter\c!text
      \hss
      \spr{\indentedtextparameter\c!separator}%
      \hskip\d_strc_indentedtexts_distance}%
   \parindent\zeropoint
   \hskip\c_strc_indentedtexts_nesting\d_strc_indentedtexts_width
   \advanceby\c_strc_indentedtexts_nesting\plusone
   \dontleavehmode\box\scratchbox
   \hangindent\c_strc_indentedtexts_nesting\d_strc_indentedtexts_width
   \useindentedtextstyleandcolor\c!style\c!color}

\protected\def\strc_indentedtexts_stop
  {\indentedtextparameter\c!after
   \par
   \endgroup}

\permanent\tolerant\protected\def\startindentedtext[#1]%
  {\strc_indentedtexts_start{#1}\c_strc_indentedtexts_nesting}

\permanent\let\stopindentedtext\strc_indentedtexts_stop

% \protected\def\strc_indentedtexts_direct#1#2#3\par % no longer clever grabpar trickery
%   {\strc_indentedtexts_start{#1}{#2}#3\strc_indentedtexts_stop}

\protected\def\strc_indentedtexts_direct#1#2% more modern anyway
  {\strc_indentedtexts_start{#1}{#2}\wrapuppar{\strc_indentedtexts_stop}}

\setupindentedtext
  [\c!style=\v!normal,
   \c!headstyle=\v!normal,
  %\c!color=,
  %\c!headcolor=,
   \c!width=\v!fit,
   \c!text=\unknown,
  %\c!sample=,
   \c!before=\blank,
   \c!after=\blank,
   \c!distance=1em,
   \c!separator={ :}]

\protect \endinput
