Emacs Asked by itsjeyd on November 22, 2021
Q: Using Elisp, how can I reset a variable to its default value without knowing what that value is?
I tried to figure out how to do this by bringing up the Customize interface for a variable I set to a non-default value in my init-file and pressing C-h k followed by a click on the “Revert…” button. That brought up the documentation for widget-button-click
, which obviously didn’t help much. (Similarly, C-h k RET with point on the button shows help for Custom-newline
, which is also not what I am looking for.)
Building up from the accepted answer I've made a function for it.
(defun custom/reset-var (symbl)
"Reset SYMBL to its standard value."
(set symbl (eval (car (get symbl 'standard-value)))))
(custom/reset-var 'somevar)
Answered by Rigotti on November 22, 2021
Another, probably cleaner, way to reset the value of a variable:
custom-reevaluate-setting
is a compiled Lisp function incustom.el
.
(custom-reevaluate-setting SYMBOL)
Reset the value of
SYMBOL
by re-evaluating its saved or standard value. Use the:set
function to do so. This is useful for customizable options that are defined before their standard value can really be computed. E.g. dumped variables whose default depends on run-time information.
So for example:
(custom-reevaluate-setting 'emms-player-next-function)
will replace the previous value with that symbol's standard-value
.
EDIT
Read @phils comment below for a very important detail regarding this function.
Answered by undostres on November 22, 2021
C-hig (elisp) Variable Definitions
Internally,
defcustom
uses the symbol propertystandard-value
to record the expression for the standard value,saved-value
to record the value saved by the user with the customization buffer, andcustomized-value
to record the value set by the user with the customization buffer, but not saved. See Symbol Properties. These properties are lists, thecar
of which is an expression that evaluates to the value.
Hence:
(setq foo (eval (car (get 'foo 'standard-value))))
Note that this only applies to defcustom
variables (i.e. "user options"). Emacs does not save the initial value of a defvar
variable anywhere.
Answered by phils on November 22, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP