Run a separate Emacs instance each time.
Think of your Emacs session as a blob of clay that is being shaped into a sculpture, and you're not the sculptor -- you are only telling them what you want them to do at a higher level, and they are making the detailed decisions.
You might be able to "undo" any given set of manipulations by describing the inverse of each change in reverse order, but it's a bit of a brittle approach -- you're only ending up at a facsimile of the earlier state, and it requires a deep understanding of what is happening to Emacs' state at each step -- if you miss anything at all (or if not all changes are reversible) then you don't even have the intended facsimile (you may or may not have something which is "good enough", but that's going to depend on the specific circumstances).
There's no facility within Emacs for taking a snapshot of the entire state and subsequently reverting to that earlier state, so if you really need to do this you should use a genuine separate Emacs instance each time.
Note that emacs has -batch
and -script
options for running Emacs without a UI, if that helps in your case. You might also take inspiration from (or make direct use of) the async
package, which is precisely for executing elisp in, and obtaining results from, a separate instance of Emacs.
load
andunload-feature
... – Wilder Dec 31 '22 at 21:10