
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                           %
% More ugly TeX magic, this time for namespace management. Use it like      %
%   \namespace{foobar}                                                      %
%      \def\foo{foom}                                                       %
%      \def\bar{blop}                                                       %
%   \endgroup                                                               %
% and then \usenamespace{foobar} will make all of the definitions appear.   %
% NB! This only works for \def, not for \let or other assignments.          %
%                                                                           %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\def\namespace#1{\begingroup
  \global\toks1={}%
  \gdef\namespace@a{\namespace@b{#1}}%
  \def\def##1{\global\toks1=\expandafter{\the\toks1 \do##1}%
              \expandafter\gdef\csname #1\string##1\endcsname}%
  \aftergroup\namespace@a
}
\def\namespace@b{\expandafter\namespace@c\expandafter{\the\toks1}}
\def\namespace@c#1#2{\expandafter\def\csname namespace@@#2\endcsname{#1}%
                     \tracingcommands0
                     \tracingmacros0}
\def\usenamespace#1{\let\namespace@do\do
                    \def\do##1{\expandafter\let\expandafter##1%
                               \csname #1\string ##1\endcsname}%
                    \csname namespace@@#1\endcsname
                    \let\do\namespace@do}
