0

I would like to eval elisp expressions in a "session" so that side-effects only affect expressions which happen in that "session". For instance defuns that are evaluated in the session would only be visible within the session and would not clobber the global session.

Is there a way to do that?

Drew
  • 77,472
  • 10
  • 114
  • 243
Wilder
  • 95
  • 6
  • https://emacs.stackexchange.com/tags/elisp/info – Drew Dec 31 '22 at 16:52
  • The question is unclear. Please specify what you mean by an (apparently) "local session", as opposed "the global session". – Drew Dec 31 '22 at 16:54
  • 2
    You can create a second Emacs instance and evaluate them there. – NickD Dec 31 '22 at 17:47
  • @Drew thanks for your correction and your question. I think what it boils down to, more specifically, is to be able to unbind all variables and functions that are defined in the eval "session" once I go back to the global "session". What i would actually need is: store all bound symbols, run the "session", store all bound symbol again, unbind those symbols that are in the second list but not in the first – Wilder Dec 31 '22 at 20:46
  • I think i could wrap something using a combination of load and unload-feature... – Wilder Dec 31 '22 at 21:10

2 Answers2

1

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.

phils
  • 50,977
  • 3
  • 79
  • 122
0

I think you could perhaps use with-temp-buffer and defvar-local to limit the scope of the variables you set to buffer local but I'm pretty sure that any functions you define will be global-scoped. I should say that I'm relatively inexperienced with elisp though.

As I've been learning and desiring something similar to what you describe, as NickD said, I end up running multiple Emacs sessions using chemacs to fire up separate configs. Not ideal but it kinda works.

trollybus
  • 21
  • 3