Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to completely erase Emacs Calc's state?

Tags:

emacs

calc

This has got to be a stupid simple question, but I haven't been able to find an answer despite searching Google multiple times in multiple ways, and digging through the Calc documentation.

GIVEN an instance of Emacs, with Calc running, and having performed multiple calculations including storing variables and equations, some of which may have come from the "~/.emacs.d/calc.el" file,

HOW do you return Calc to a pristine state without restarting Emacs?

Pristine: Nothing on the stack. Nothing in the trail. No stored variables or equations. Etc.

like image 736
EdwinW Avatar asked Oct 24 '12 20:10

EdwinW


1 Answers

M-x calc-reset, which is also bound to C-x * 0, is what you need. From the info manual:

The C-x * 0' command ('calc-reset'; that's 'C-x *' followed by a zero) resets the Calculator to its initial state. This clears the stack, resets all the modes to their initial values (the values that were saved withm m' (calc-save-modes')), clears the caches (*note Caches::), and so on. (It does _not_ erase the values of any variables.) With an argument of 0, Calc will be reset to its default state; namely, the modes will be given their default values. With a positive prefix argument,C-x * 0' preserves the contents of the stack but resets everything else to its initial state; with a negative prefix argument, `C-x * 0' preserves the contents of the stack but resets everything else to its default state.

EDIT: Oops. Even that doesn't clear variables. I'm not sure if there is a straightforward way to get all the way back to pristine :(

EDIT 2: It looks like Calc stores all variables, including 'built-ins' like pi and e, as global variables with the prefix 'var-'. As far as I can tell, it doesn't keep track of which variables were set by the mode (like pi), and which were set by users. Furthermore, the default user variables are stored as var-q0, var-q1 etc. So in order to clear out all the variables, you'd need to compile a list of variables and states present at startup, erase everything not in that list, and then restore the original values of the variables in that list. That's certainly possible, but a little tedious.

Edit 3: Here's my attempt. I took another look at calc-mode, and at start up it defines the variables I've added to my-calc-builtin-vars below. The second line will remove all variables in Emacs that start with the prefix 'var-' and are not in this list. This will include any variables defined by you, or in another package. So let's hope no-one else uses the prefix 'var-'. And it will not reset the value of the built-in variables, so if you have redefined pi to 3, it will remain 3.

(setq my-calc-builtin-vars
      '("var-nan" "var-uinf" "var-sym" "var-lines" "var-Modes"
      "var-EvalRules" "var-inf" "var-phi" "var-pi" "var-gamma" "var-π"
      "var-φ" "var-γ" "var-spec" "var-e" "var-i")) 

(defun really-reset-calc ()
  (interactive)
  (calc-reset nil)
  (mapc #'(lambda (el) (unintern el)) 
        (remove nil (mapcar 
                     #'(lambda (el) (unless (member el my-calc-builtin-vars) el))
                     (all-completions "var-" obarray)))))

UPDATE: August 6, 2016

Current built-in vars list:

(setq my-calc-builtin-vars
      '("var-CommuteRules" "var-Decls" "var-DistribRules" "var-EvalRules"
      "var-FactorRules" "var-FitRules" "var-Holidays" "var-IntegAfterRules"
      "var-IntegLimit" "var-InvertRules" "var-JumpRules" "var-MergeRules"
      "var-Modes" "var-NegateRules" "var-e" "var-gamma" "var-i" "var-phi"
      "var-pi" "var-γ" "var-π" "var-φ"))
like image 196
Tyler Avatar answered Oct 25 '22 18:10

Tyler