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

\unprotect

% todo: reset and increment will go

%D Labels are implemented somewhat inefficient but have the advantage that they
%D can be controlled like other constructions. Who knows when that comes in
%D handy?

\installcorenamespace{labels}

\installcommandhandler \??labels {label} \??labels % plural hash, else clash with label (or rename that one)

\installcounterassociation{label}

\let\setuplabels\setuplabel

\setuplabel
  [\c!alternative=\v!intext,
   \c!headstyle=\v!bold,
   \c!titlestyle=\v!bold,
   \c!distance=\zeropoint,
   \c!width=\v!fit,
   \c!titledistance=.5\emwidth,
   \c!margin=\v!no,
   \c!titleleft=(,
   \c!titleright=),
   \c!display=\v!no,
   \c!titlecommand=,
   \c!expansion=\v!no,
   \c!way=\v!by\v!text,
   \c!prefix=\v!no,
   \c!prefixconnector=.,
   \c!text=\currentlabel,
   \c!number=\v!yes, % else description
   \c!start=0,
   \c!state=\v!start,
   \c!levels=3]

\protected\def\strc_define_commands_label#1#2#3% current level parent
  {\ifempty{#3}%
     \expanded{\defineconstruction[#1][\s!handler=\v!label,\c!level=#2]}%
     \edefcsname\??label#1:\s!parent\endcsname{\??label}%
   \else
     \expanded{\defineconstruction[#1][#3][\s!handler=\v!label,\c!level=#2]}%
     \edefcsname\??label#1:\s!parent\endcsname{\??label#3}%
   \fi
   \ifconditional\c_strc_constructions_define_commands
     \protected\frozen\instance\edefcsname\e!next #1\endcsname{\strc_labels_next {#1}{\number#2}}% obsolete
     \protected\frozen\instance\edefcsname\v!reset#1\endcsname{\strc_labels_reset{#1}{\number#2}}% obsolete % should be \e!reset anyway
     %protected\frozen\instance\edefcsname\c!set  #1\endcsname{\strc_labels_set  {#1}{\number#2}}% obsolete
     \ifcsname\v!current#1\endcsname
       % we play safe
     \else
       \protected\edefcsname\v!current#1\endcsname{\strc_labels_current{#1}}%          % obsolete % should be \e!current anyway
     \fi
     \protected\frozen\instance\edefcsname#1\endcsname{\strc_labels_command[#1]}%
   \fi}

% todo: \strc_labels_command for user

\lettonothing\m_strc_labels_sub

\appendtoks
    \lettonothing\m_strc_labels_sub
    \ifempty\currentlabelparent
       % clone => parent | subclone => clone | subsubclone => subclone
       \strc_define_commands_label
         {\m_strc_labels_sub\currentlabel}%
         \plusone
         \empty
       \edef\p_levels{\labelparameter\c!levels}%
       \dostepwiserecurse\plustwo\p_levels\plusone
         {\strc_define_commands_label
            {\v!sub\m_strc_labels_sub\currentlabel}%
            \recurselevel
            {\m_strc_labels_sub\currentlabel}%
          \edef\m_strc_labels_sub{\v!sub\m_strc_labels_sub}}%
   \else
       % clone => parent | subclone => subparent | subsubclone => subsubparent
       \edef\p_levels{\labelparameter\c!levels}%
       \dorecurse\p_levels
         {\strc_define_commands_label
            {\m_strc_labels_sub\currentlabel}%
            \recurselevel
            {\m_strc_labels_sub\currentlabelparent}%
          \edef\m_strc_labels_sub{\v!sub\m_strc_labels_sub}}%
   \fi
   \edef\p_counter{\labelparameter\s!counter}% can inherit from parent
   \ifempty\p_counter %
       \let\p_counter\currentlabel
   \fi
   \doifelsecounter\p_counter\donothing{\strc_labels_define_counter\p_counter}%
   \letlabelparameter\s!counter\p_counter
\to \everydefinelabel

\newconditional\c_strc_constructions_number_keep

\defcsname\??constructioninitializer\v!label\endcsname
  {\let         \currentlabel                    \currentconstruction
   \enforced\let\constructionparameter           \labelparameter
   \enforced\let\constructionnamespace           \??label
   \enforced\let\detokenizedconstructionparameter\detokenizedlabelparameter
   \enforced\let\letconstructionparameter        \letlabelparameter
   \enforced\let\useconstructionstyleandcolor    \uselabelstyleandcolor
   \enforced\let\setupcurrentconstruction        \setupcurrentlabel
   % shared with enumerations
   \ifcstok{\constructionparameter\c!number}\v!yes
     \c_strc_constructions_number_state\conditionaltrue
     \iftrialtypesetting
       \strc_counters_save\currentconstructionnumber
     \fi
     \ifconditional\c_strc_constructions_number_keep \else
       \strc_counters_increment_sub\currentconstructionnumber\currentconstructionlevel
     \fi
   \else
     \c_strc_constructions_number_state\conditionalfalse
   \fi
   \ifcstok{\constructionparameter\c!title}\v!yes
     \c_strc_constructions_title_state\conditionaltrue
   \else
     \c_strc_constructions_title_state\conditionalfalse
   \fi}

\defcsname\??constructionfinalizer\v!label\endcsname
  {\ifconditional\c_strc_constructions_number_state
     \iftrialtypesetting
       \strc_counters_restore\currentconstructionnumber
     \fi
   \fi}

%D Interfaces:

\protected\def\strc_labels_command{\c_strc_constructions_number_keep\conditionalfalse\nameddescription}
\protected\def\strc_labels_current{\c_strc_constructions_number_keep\conditionaltrue\nameddescription}

\protected\def\strc_labels_next   {\strc_constructions_next_indeed \namedlabelparameter} % #1#2
\protected\def\strc_labels_reset  {\strc_constructions_reset_indeed\namedlabelparameter} % #1#2
%protected\def\strc_labels_set    {\strc_constructions_set_indeed  \namedlabelparameter} % #1#2

% similar to enumerations

\def\strc_labels_define_counter#1%
  {\definecounter[#1]%
   \registerlabelcounter{#1}}

\appendtoks
    \synchronizelabelcounters
\to \everysetuplabel

\appendtoks
    \synchronizelabelcounters
\to \everydefinelabel

% no start stop here

\letcsname\??constructionmainhandler\v!label\expandafter\endcsname\csname\??constructionmainhandler\v!description\endcsname

\protected\edefcsname\??constructioncommandhandler\v!label\endcsname
  {\csname\??constructionstarthandler\v!construction\endcsname
   \csname\??constructionstophandler \v!construction\endcsname
   \endgroup}

\protected\defcsname\??constructiontexthandler\v!label\endcsname
  {\begingroup
   \useconstructionstyleandcolor\c!headstyle\c!headcolor
   \strc_labels_text
   \endgroup}

\let\strc_labels_text\strc_enumerations_text

% inline variant

\defineconstructionalternative
  [\v!intext]
  [\c!renderingsetup=\??constructionrenderings:\v!intext]

\startsetups[\??constructionrenderings:\v!intext]
    \dontleavehmode\box\constructionheadbox
\stopsetups

\protect \endinput
