I think it's fairly common to redefine macros. And I'm creating two functions to help storing and restoring these redefined macros.
And so I tried using the following:
\documentclass{article}
\usepackage{letltxmacro}
\makeatletter
\newcommand{\storecommand}[1]
{%
\LetLtxMacro{\\env@#1}{\\#1}%
}
\newcommand{\restorecommand}[1]
{%
\LetLtxMacro{\\#1}{\\env@#1}%
}
\makeatother
% Example usage
\begin{document}
\noindent
\makeatletter
Expected:\\
\newcommand{\foo}{bar}%
\foo\\
\LetLtxMacro{\env@foo}{\foo}%
\renewcommand{\foo}{baz}%
\foo\\
\LetLtxMacro{\foo}{\env@foo}%
\foo\\
\makeatother
\noindent
Actual:\\
\renewcommand{\foo}{bar}%
\foo\\
\storecommand{foo}%
\renewcommand{\foo}{baz}%
\foo\\
\restorecommand{foo}%
\foo
\end{document}
Expected:
bar
baz
bar
Actual:
bar
nv@foo=efoobaze oo=fenv@foobaz
I hoped that it'd know when I call it to expand the #1 and then take the entire thing as the command. However it doesn't.
How can I get storecommand to work the way I want?
To prevent an XY question:
I'm trying to make an environment that reverts to the previous state when it ends. Wiping all commands that have been made.

\LetLtxMacroneeds to be a command name token but you are passing\LetLtxMacro{\\foo}which is the newline command\\followed by the textfoo– David Carlisle Nov 10 '19 at 22:16\expandafter\LetLtxMacro\csname env@#1\expandafter\endcsname\csname #1\endcsnamebut just restoring via TeX grouping would normally be easier. – David Carlisle Nov 10 '19 at 22:20