Globally assigning a coffin

After some consideration, particularly in relation to box scope, the team will be adding support for global coffins to expl3 in the next release (2019-01-24). (The code was added in https://github.com/latex3/latex3/pull/525.) Global coffins follow the same pattern as for other data structures, e.g.

\hcoffin_gset:Nn \g_my_coffin { Tokens }

I have fiddled around with LaTeX3 coffins to the point that I have become frustrated with the absence of \vcoffin_gset:Nnw. To that end, I have arrived at this somewhat baroque workaround. It works by putting TypesetCoffin<\your coffin> in a global lrbox (see the code below). Frankly, I didn't think this would work, but, Lo!, and, Behold! it seems to. The object here is to create a coffin and use its contents outside an environment or group. There are probably limitations of which I am unaware.

\documentclass{article}

\usepackage{xparse,xcoffins}
\usepackage{etoolbox}
\usepackage{lipsum}

\NewCoffin\MyTestCoffin
\newsavebox{\testingbox}

%% Thanks to egreg, makes glrbox global (requires etoolbox.sty):
%% https://tex.stackexchange.com/questions/43046/beamer-content-in-box-register-does-not-survive-endframe/43063#43063
\cslet{glrbox}\lrbox
\expandafter\patchcmd\csname glrbox\endcsname{\setbox}{\global\setbox}{}{}
\cslet{endglrbox}\endlrbox

\ExplSyntaxOn 

\newcommand\typesetit{\usebox{\testingbox}}% <<- Redifined.

\newenvironment{test} {%
    \vcoffin_set:Nnw\l_tmpa_coffin{4in}
}{%
    \vcoffin_set_end:
    \begin{glrbox}{\testingbox}{\TypesetCoffin\l_tmpa_coffin}\end{glrbox}%
}

\newenvironment{testi}{%
    \vcoffin_set:Nnw\l_tmpa_coffin{4in}
}{%
    \vcoffin_set_end:
    \coffin_typeset:Nnnnn\l_tmpa_coffin{l}{T}{0pt}{0pt}%
}

\ExplSyntaxOff

\begin{document}

\begin{test}
    This is the first one.
    \lipsum[1]
\end{test}

\typesetit % Not empty :)

\bigskip

\begin{testi}
    Blabla bla.
    \lipsum[1]
\end{testi}

\end{document}

Comments welcome.

enter image description here

Tags:

Expl3

Xcoffins