%%% Krzysztof Leszczy\n{}ski: \csequence stack
% Often I need to save few macros but I don't want to
% \verb|\begingroup| and \verb|\global|-ly define those I want to
% keep after \verb|\endgroup|. Here is a simple stack:

\newcsstack \stackname % define a new stack
\pushcs \stackname \cs % push a control sequence
\popcs  \stackname \cs % pop a control sequence
\topcs  \stackname \cs % equivalent to  \popcs...\pushcs

\def \gobble#1{} % this macro is usually defined somewhere
\def \stackcs#1{\csname \ifnum\escapechar>-1
                \expandafter \expandafter \expandafter \gobble
                \expandafter \fi \string #1::\number#1\endcsname}

% temporarily un-outer newcount to define newcsstack

\let \topcs = \newcount \let \newcount = \relax
\def \newcsstack #1{\newcount #1\global#1=0\pushcs#1\relax}
\let \newcount = \topcs % restore \newcount

\def \pushcs#1#2{\global \advance#1 1
  \global \expandafter \expandafter \expandafter
  \let \stackcs{#1}= #2}

\def \topcs#1#2{\expandafter \expandafter \expandafter \let
  \expandafter \expandafter \expandafter #2\stackcs{#1}}

\def \popcs#1#2{\topcs#1#2%
        \global \expandafter \expandafter \expandafter
        \let \stackcs{#1}\relax \global \advance #1-1 }
