\unprotect

\installnamespace {userdata}
\installnamespace {userdataalternative}
\installnamespace {userdatarenderings}

\installcommandhandler \????userdata            {userdata}            \????userdata
\installcommandhandler \????userdataalternative {userdataalternative} \????userdataalternative

\unexpanded\def\startuserdata
  {\begingroup
   \let\currentuserdata\empty
   \doifelsenextoptionalcs\userdata_start_delayed\userdata_start_indeed}

% This variant works only when the userdata instance exists while the assignment check
% can also be used with undefined instances which falls back to the global settings.
%
% \def\userdata_start_delayed[#1]%
%   {\ifcsname\nameduserdatahash{\detokenize\expandafter{\normalexpanded{#1}}}\s!parent\endcsname
%      \expandafter\userdata_start_delayed_name
%    \else
%      \expandafter\userdata_start_delayed_parameters
%    \fi[#1]}

\def\userdata_start_delayed[#1]%
  {\doifelseassignment{#1}
     \userdata_start_delayed_parameters
     \userdata_start_delayed_name
   [#1]}

\def\userdata_start_delayed_parameters[#1]%
  {\setupcurrentuserdata[#1]%
   \userdata_start_indeed}

\def\userdata_start_delayed_name[#1]%
  {\edef\currentuserdata{#1}%
   \checkuserdataparent
   \doifelsenextoptionalcs\userdata_start_delayed_parameters\userdata_start_indeed}

\def\userdata_start_indeed
  {\grabbufferdatadirect\s!userdata{\csstring\startuserdata}{\csstring\stopuserdata}}

\unexpanded\def\stopuserdata
  {\useuserdatastyleandcolor\c!style\c!color
   \edef\currentuserdataalternative{\userdataparameter\c!alternative}%
   \ifcsname\currentuserdataalternativehash\s!parent\endcsname \else
     \let\currentuserdataalternative\s!default
   \fi
   \edef\p_renderingsetup{\userdataalternativeparameter\c!renderingsetup}%
   \directsetup\p_renderingsetup
   \endgroup}

\defineuserdataalternative
  [\s!default]
  [\c!renderingsetup=\????userdatarenderings:\s!default]

\startsetups[\????userdatarenderings:\s!default]
    \userdataparameter\c!before
    \usesetupsparameter\userdataparameter
    \getbufferdata[\s!userdata]
    \userdataparameter\c!after
\stopsetups

\setupuserdata
  [\c!alternative=\s!default]

% copied from tabl-xtb.lua to make the userdata environment work with lua documents
% 
% \startluacode
% 
% local context         = context
% local ctxcore         = context.core
% 
% local startuserdata   = ctxcore.startuserdata
% local stopuserdata    = ctxcore.stopuserdata
% 
% local startcollecting = context.startcollecting
% local stopcollecting  = context.stopcollecting
% 
% function ctxcore.startuserdata(...)
%     startcollecting()
%     startuserdata(...)
% end
% 
% function ctxcore.stopuserdata()
%     stopuserdata()
%     stopcollecting()
% end
% 
% \stopluacode

\protect

\starttext

\defineuserdata [test] [style=italic]

\samplefile{klein}

\startuserdata [before=\blank,after=\blank,color=red]
\samplefile{greenfield}
\stopuserdata

\samplefile{sapolsky}

\startuserdata [test]
\samplefile{bryson}
\stopuserdata

\samplefile{jojomayer}

\startuserdata [test] [before=\blank,after=\blank,color=red]
\samplefile{linden}
\stopuserdata

\samplefile{montgomery}

% \startluacode
% 
% context.startuserdata({color="blue"})
% context.samplefile("klein")
% context.stopuserdata()
% 
% \stopluacode

% \page
% 
% \defineuserdataalternative [epigraph] [renderingsetup=userdata:epigraph]
% 
% \startsetups [userdata:epigraph]
%     \startframedtext [location=right,frame=off,align={flushleft,broad},style=\tfx,offset=.25ex,width=.5\textwidth]
%     \begstrut\inlinebuffer[userdata]\endstrut
%     \hairline
%     \wordright{\userdataparameter{author}}
%     \stopframedtext
% \stopsetups
% 
% \defineuserdata
%   [epigraph]
%   [alternative=epigraph]
% 
% \startuserdata [epigraph] [author={Sean B. Carrol}]
% The fraction of fossil olfactory receptor genes is significantly
% higher in all species with full color vision. This suggests that
% the evolution of trichromatic vision --- which allows these
% primates to detect food, mates, and danger with visual cues ---
% has reduced their reliance on the sense of smell.
% \stopuserdata
% 
% \startuserdata [epigraph] [author={Sean B. Carrol}]
% \samplefile{carrol}
% \stopuserdata

\stoptext
