%D \module
%D   [       file=attr-ini,
%D        version=2007.06.06,
%D          title=\CONTEXT\ Attribute Macros,
%D       subtitle=Initialization,
%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 Attribute Macros / Initialization}

%D Although it's still somewhat experimental, here we introduce code
%D related to attributes. Housekeeping will move completely to \LUA\
%D and \type {\newattribute} will go away.

\unprotect

\registerctxluafile{attr-ini}{1.001}

\installcorenamespace{attributecount}  % the counter representing the attribute (attrdef'd)
\installcorenamespace{attributeid}     % the internal number
\installcorenamespace{attributestack}  % the attribute specific stack

\unexpanded\def\pushattribute#1%
  {\global\advance\csname\??attributestack\string#1\endcsname\plusone
   \expandafter\xdef\csname\??attributestack\string#1:\number\csname\??attributestack\string#1\endcsname\endcsname{\number\attribute#1}}

\unexpanded\def\popattribute#1%
  {\attribute#1\csname\??attributestack\string#1:\number\csname\??attributestack\string#1\endcsname\endcsname\relax
   \global\advance\csname\??attributestack\string#1\endcsname\minusone}

\unexpanded\def\installattributestack#1%
  {\ifcsname\??attributestack\string#1\endcsname \else
     \expandafter\newcount\csname\??attributestack\string#1\endcsname
   \fi}

\newtoks \attributesresetlist

\ifdefined \s!global    \else \def\s!global   {global}    \fi % for metatex % or hard check later
\ifdefined \s!public    \else \def\s!public   {public}    \fi % for metatex % or hard check later
\ifdefined \s!private   \else \def\s!private  {private}   \fi % for metatex % or hard check later
\ifdefined \s!attribute \else \def\s!attribute{attribute} \fi % for metatex % or hard check later

\unexpanded\def\defineattribute      {\dodoubleempty\attr_basics_define}
\unexpanded\def\definesystemattribute{\dodoubleempty\attr_basics_define_system}

\def\attr_basics_define       {\attr_basics_define_indeed\s!public}
\def\attr_basics_define_system{\attr_basics_define_indeed\s!private}

\def\attr_basics_define_indeed#1[#2][#3]%
  {\scratchcounter\clf_defineattribute{#2}{#1}\relax
  %\writestatus\m!system{defining #1 attribute #2 with number \number\scratchcounter}%
   \expandafter\attributedef\csname\??attributecount#2\endcsname\scratchcounter
   \expandafter\newconstant \csname\??attributeid#2\endcsname
   \csname\??attributeid#2\endcsname\scratchcounter
   % some attributes are always global
   \doifnotinset\s!global{#3}{\appendetoks\csname\??attributecount#2\endcsname\attributeunsetvalue\to\attributesresetlist}%
   % here public means 'visible' so it's not to be confused with 'public' at the lua end
   \doifinset   \s!public{#3}{\expandafter\let\csname#2\s!attribute\expandafter\endcsname\csname\??attributeid#2\endcsname}}

\unexpanded\def\newattribute#1%
  {\attr_basics_define_indeed\s!public[\strippedcsname#1][]%
   \expandafter\let\expandafter#1\csname\??attributeid\strippedcsname#1\endcsname}

% expandable so we can \edef them for speed

\def\dosetattribute#1#2{\csname\??attributecount#1\endcsname#2\relax}
\def\doresetattribute#1{\csname\??attributecount#1\endcsname\attributeunsetvalue}
\def\dogetattribute  #1{\number\csname\??attributecount#1\endcsname}
\def\dogetattributeid#1{\number\csname\??attributeid#1\endcsname}

\let\dompattribute\gobbletwoarguments

\unexpanded\def\resetallattributes{\the\attributesresetlist}

%D Rather special.

\unexpanded\def\savecurrentattributes   #1{\clf_savecurrentattributes   {#1}}
\unexpanded\def\restorecurrentattributes#1{\clf_restorecurrentattributes{#1}}

%D For the moment we put this here (later it will move to where it's used):

\definesystemattribute [state]
\definesystemattribute [color]               [public] % global
\definesystemattribute [colormodel]          [public,global]
\definesystemattribute [skip]
\definesystemattribute [penalty]
\definesystemattribute [transparency]        [public]
\definesystemattribute [background]          [public]
\definesystemattribute [alignbackground]     [public]
\definesystemattribute [colorintent]         [public]
\definesystemattribute [negative]            [public]
\definesystemattribute [effect]              [public]
\definesystemattribute [viewerlayer]         [public]
\definesystemattribute [layoutcomponent]     [public]
\definesystemattribute [reference]           [public]
\definesystemattribute [destination]         [public]
\definesystemattribute [internal]            [public]
\definesystemattribute [ruled]               [public]
\definesystemattribute [shifted]             [public]
\definesystemattribute [checkedbreak]        [public]
\definesystemattribute [vboxtohboxseparator] [public]

\unexpanded\def\showattributes{\clf_showattributes}

\protect \endinput
