% engine=luatex

%D First a hack to the core. two changes:
%D * don't force end_cs to be \relax
%D * don't remove end_cs from the input stream

\ctxlua{
function collectors.install(tag,end_cs)
    collectors.data[tag] = { }
    local data   = collectors.data[tag]
    local call   = token.command_id("call")
    local endcs  = token.csname_id(end_cs)
    local expand = collectors.registered
    local get    = token.get_next
    while true do
        local t = get()
        local a, b = t[1], t[3]
        if b == endcs then
            tex.print('\\' ..end_cs)
            return
        elseif a == call and expand[b] then
            token.expand()
        else
            data[\string#data+1] = t
        end
    end
end 
}

%D a small extension to the core interface, to have a 
%D nice wrapper around the lua code

\ctxlua {
function collectors.handle(tag,handle)
    collectors.data[tag] = handle(collectors.data[tag])
end
}
\def\handletokens[#1][#2]{\ctxlua{collectors.handle("#1",#2)}}

%D Here starts the document-specific code

%D Start capturing tokens in the buffer named 'babel', stop
%D at \stopbabel

\def\startbabel 
  {\ctxlua{collectors.install("babel", "stopbabel")}}


%D The lua mutation function. str is a table containing the captured
%D tokens, each itself a three-item table (this is explained in the 
%D luatex manual)

\ctxlua {
function convert_babel(str)
    local t = { }
    for k,v in ipairs(str) do
        t[\string#t+1] = tokens.other('*')
        t[\string#t+1] = v
    end
    return t
end
}

%D convert the tokens using that lua function, then 
%D flush the result
\def\stopbabel  
  {\handletokens[babel][convert_babel]
   \flushtokens[babel]}

\usetypescript[palatino][ec]

\starttext

\section{With Taco's \type{\startbabel}}
\startbabel%
some stuff here \blank[medium] some other stuff

\switchtobodyfont[palatino]

\subsection{More stuff}

stuff stuff stuff
\stopbabel

\blank[big]

\section{With an \tt OTP}

% Do the same as convert_babel, with a simple OTP (stars.otp)
\ocp\stars=stars
\ocplist\StarsOCP=\addbeforeocplist1\stars\nullocplist

\pushocplist\StarsOCP

some stuff here \blank[medium] some other stuff

\switchtobodyfont[palatino]

\subsection{More stuff}

stuff stuff stuff

\stoptext
